Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
0fdd1e2c | 3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
fac6795d | 4 | * |
54d01ffb DG |
5 | * This program is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; only version 2 of the License. | |
91d76f53 | 8 | * |
54d01ffb DG |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
91d76f53 | 13 | * |
54d01ffb DG |
14 | * You should have received a copy of the GNU General Public License along with |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
16 | * Place - Suite 330, Boston, MA 02111-1307, USA. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <fcntl.h> | |
21 | #include <getopt.h> | |
22 | #include <grp.h> | |
23 | #include <limits.h> | |
24 | #include <pthread.h> | |
8c0faa1d | 25 | #include <semaphore.h> |
fac6795d DG |
26 | #include <signal.h> |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
0fdd1e2c | 30 | #include <sys/mman.h> |
b73401da | 31 | #include <sys/mount.h> |
1e307fab | 32 | #include <sys/resource.h> |
fac6795d DG |
33 | #include <sys/socket.h> |
34 | #include <sys/stat.h> | |
35 | #include <sys/types.h> | |
0fdd1e2c DG |
36 | #include <sys/wait.h> |
37 | #include <urcu/futex.h> | |
fac6795d | 38 | #include <unistd.h> |
3bd1e081 | 39 | #include <config.h> |
fac6795d | 40 | |
3bd1e081 | 41 | #include <lttng-consumerd.h> |
e88129fc | 42 | #include <lttng-sessiond-comm.h> |
3bd1e081 | 43 | #include <lttng/lttng-consumer.h> |
1e307fab | 44 | #include <lttngerr.h> |
fac6795d | 45 | |
54d01ffb | 46 | #include "channel.h" |
5eb91c98 | 47 | #include "compat/poll.h" |
099e26bd | 48 | #include "context.h" |
54d01ffb | 49 | #include "event.h" |
099e26bd | 50 | #include "futex.h" |
20fe2104 | 51 | #include "kernel-ctl.h" |
1e307fab | 52 | #include "ltt-sessiond.h" |
0fdd1e2c | 53 | #include "shm.h" |
56fff090 | 54 | #include "ust-app.h" |
1e307fab | 55 | #include "ust-ctl.h" |
8e68d1c8 | 56 | #include "utils.h" |
0177d773 | 57 | #include "ust-ctl.h" |
fac6795d | 58 | |
3bd1e081 MD |
59 | struct consumer_data { |
60 | enum lttng_consumer_type type; | |
61 | ||
62 | pthread_t thread; /* Worker thread interacting with the consumer */ | |
63 | sem_t sem; | |
64 | ||
65 | /* Mutex to control consumerd pid assignation */ | |
66 | pthread_mutex_t pid_mutex; | |
67 | pid_t pid; | |
68 | ||
69 | int err_sock; | |
70 | int cmd_sock; | |
71 | ||
72 | /* consumer error and command Unix socket path */ | |
73 | char err_unix_sock_path[PATH_MAX]; | |
74 | char cmd_unix_sock_path[PATH_MAX]; | |
75 | }; | |
76 | ||
75462a81 | 77 | /* Const values */ |
686204ab | 78 | const char default_home_dir[] = DEFAULT_HOME_DIR; |
64a23ac4 | 79 | const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP; |
686204ab MD |
80 | const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR; |
81 | const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE; | |
82 | ||
fac6795d | 83 | /* Variables */ |
1d4b027a | 84 | int opt_verbose; /* Not static for lttngerr.h */ |
3bd1e081 | 85 | int opt_verbose_consumer; /* Not static for lttngerr.h */ |
1d4b027a | 86 | int opt_quiet; /* Not static for lttngerr.h */ |
d063d709 | 87 | |
fac6795d DG |
88 | const char *progname; |
89 | const char *opt_tracing_group; | |
5b8719f5 | 90 | static int opt_sig_parent; |
fac6795d DG |
91 | static int opt_daemon; |
92 | static int is_root; /* Set to 1 if the daemon is running as root */ | |
1d4b027a | 93 | static pid_t ppid; /* Parent PID for --sig-parent option */ |
3bd1e081 MD |
94 | |
95 | /* Consumer daemon specific control data */ | |
96 | static struct consumer_data kconsumer_data = { | |
97 | .type = LTTNG_CONSUMER_KERNEL, | |
98 | }; | |
99 | static struct consumer_data ustconsumer_data = { | |
100 | .type = LTTNG_CONSUMER_UST, | |
101 | }; | |
102 | ||
099e26bd | 103 | static int dispatch_thread_exit; |
fac6795d | 104 | |
54d01ffb DG |
105 | /* Global application Unix socket path */ |
106 | static char apps_unix_sock_path[PATH_MAX]; | |
107 | /* Global client Unix socket path */ | |
108 | static char client_unix_sock_path[PATH_MAX]; | |
54d01ffb DG |
109 | /* global wait shm path for UST */ |
110 | static char wait_shm_path[PATH_MAX]; | |
fac6795d | 111 | |
1d4b027a | 112 | /* Sockets and FDs */ |
d6f42150 DG |
113 | static int client_sock; |
114 | static int apps_sock; | |
20fe2104 | 115 | static int kernel_tracer_fd; |
7a485870 | 116 | static int kernel_poll_pipe[2]; |
1d4b027a | 117 | |
273ea72c DG |
118 | /* |
119 | * Quit pipe for all threads. This permits a single cancellation point | |
120 | * for all threads when receiving an event on the pipe. | |
121 | */ | |
122 | static int thread_quit_pipe[2]; | |
123 | ||
099e26bd DG |
124 | /* |
125 | * This pipe is used to inform the thread managing application communication | |
126 | * that a command is queued and ready to be processed. | |
127 | */ | |
128 | static int apps_cmd_pipe[2]; | |
129 | ||
1d4b027a | 130 | /* Pthread, Mutexes and Semaphores */ |
1d4b027a | 131 | static pthread_t apps_thread; |
099e26bd | 132 | static pthread_t reg_apps_thread; |
1d4b027a | 133 | static pthread_t client_thread; |
7a485870 | 134 | static pthread_t kernel_thread; |
099e26bd | 135 | static pthread_t dispatch_thread; |
5eb91c98 | 136 | |
fac6795d | 137 | |
099e26bd DG |
138 | /* |
139 | * UST registration command queue. This queue is tied with a futex and uses a N | |
140 | * wakers / 1 waiter implemented and detailed in futex.c/.h | |
141 | * | |
142 | * The thread_manage_apps and thread_dispatch_ust_registration interact with | |
143 | * this queue and the wait/wake scheme. | |
144 | */ | |
145 | static struct ust_cmd_queue ust_cmd_queue; | |
146 | ||
b5541356 DG |
147 | /* |
148 | * Pointer initialized before thread creation. | |
149 | * | |
150 | * This points to the tracing session list containing the session count and a | |
151 | * mutex lock. The lock MUST be taken if you iterate over the list. The lock | |
152 | * MUST NOT be taken if you call a public function in session.c. | |
04ea676f | 153 | * |
d063d709 | 154 | * The lock is nested inside the structure: session_list_ptr->lock. Please use |
54d01ffb | 155 | * session_lock_list and session_unlock_list for lock acquisition. |
b5541356 DG |
156 | */ |
157 | static struct ltt_session_list *session_list_ptr; | |
158 | ||
5eb91c98 DG |
159 | /* |
160 | * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set. | |
161 | */ | |
162 | static int create_thread_poll_set(struct lttng_poll_event *events, | |
163 | unsigned int size) | |
164 | { | |
165 | int ret; | |
166 | ||
167 | if (events == NULL || size == 0) { | |
168 | ret = -1; | |
169 | goto error; | |
170 | } | |
171 | ||
172 | ret = lttng_poll_create(events, size, LTTNG_CLOEXEC); | |
173 | if (ret < 0) { | |
174 | goto error; | |
175 | } | |
176 | ||
177 | /* Add quit pipe */ | |
178 | ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN); | |
179 | if (ret < 0) { | |
180 | goto error; | |
181 | } | |
182 | ||
183 | return 0; | |
184 | ||
185 | error: | |
186 | return ret; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Check if the thread quit pipe was triggered. | |
191 | * | |
192 | * Return 1 if it was triggered else 0; | |
193 | */ | |
194 | static int check_thread_quit_pipe(int fd, uint32_t events) | |
195 | { | |
196 | if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) { | |
197 | return 1; | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
0fdd1e2c DG |
203 | /* |
204 | * Remove modules in reverse load order. | |
205 | */ | |
206 | static int modprobe_remove_kernel_modules(void) | |
207 | { | |
208 | int ret = 0, i; | |
209 | char modprobe[256]; | |
210 | ||
211 | for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) { | |
212 | ret = snprintf(modprobe, sizeof(modprobe), | |
213 | "/sbin/modprobe --remove --quiet %s", | |
214 | kernel_modules_list[i].name); | |
215 | if (ret < 0) { | |
216 | perror("snprintf modprobe --remove"); | |
217 | goto error; | |
218 | } | |
219 | modprobe[sizeof(modprobe) - 1] = '\0'; | |
220 | ret = system(modprobe); | |
221 | if (ret == -1) { | |
222 | ERR("Unable to launch modprobe --remove for module %s", | |
223 | kernel_modules_list[i].name); | |
224 | } else if (kernel_modules_list[i].required | |
225 | && WEXITSTATUS(ret) != 0) { | |
226 | ERR("Unable to remove module %s", | |
227 | kernel_modules_list[i].name); | |
228 | } else { | |
229 | DBG("Modprobe removal successful %s", | |
230 | kernel_modules_list[i].name); | |
231 | } | |
232 | } | |
233 | ||
234 | error: | |
235 | return ret; | |
236 | } | |
237 | ||
238 | /* | |
239 | * Return group ID of the tracing group or -1 if not found. | |
240 | */ | |
996b65c8 MD |
241 | static gid_t allowed_group(void) |
242 | { | |
243 | struct group *grp; | |
244 | ||
274e143b MD |
245 | if (opt_tracing_group) { |
246 | grp = getgrnam(opt_tracing_group); | |
247 | } else { | |
248 | grp = getgrnam(default_tracing_group); | |
249 | } | |
996b65c8 MD |
250 | if (!grp) { |
251 | return -1; | |
252 | } else { | |
253 | return grp->gr_gid; | |
254 | } | |
255 | } | |
256 | ||
273ea72c | 257 | /* |
5eb91c98 | 258 | * Init thread quit pipe. |
273ea72c DG |
259 | * |
260 | * Return -1 on error or 0 if all pipes are created. | |
261 | */ | |
262 | static int init_thread_quit_pipe(void) | |
263 | { | |
264 | int ret; | |
265 | ||
266 | ret = pipe2(thread_quit_pipe, O_CLOEXEC); | |
267 | if (ret < 0) { | |
268 | perror("thread quit pipe"); | |
269 | goto error; | |
270 | } | |
271 | ||
272 | error: | |
273 | return ret; | |
274 | } | |
275 | ||
fac6795d | 276 | /* |
d063d709 DG |
277 | * Complete teardown of a kernel session. This free all data structure related |
278 | * to a kernel session and update counter. | |
fac6795d | 279 | */ |
1d4b027a | 280 | static void teardown_kernel_session(struct ltt_session *session) |
fac6795d | 281 | { |
1d4b027a DG |
282 | if (session->kernel_session != NULL) { |
283 | DBG("Tearing down kernel session"); | |
d9800920 DG |
284 | |
285 | /* | |
286 | * If a custom kernel consumer was registered, close the socket before | |
287 | * tearing down the complete kernel session structure | |
288 | */ | |
3bd1e081 | 289 | if (session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) { |
d9800920 DG |
290 | lttcomm_close_unix_sock(session->kernel_session->consumer_fd); |
291 | } | |
292 | ||
62499ad6 | 293 | trace_kernel_destroy_session(session->kernel_session); |
1d4b027a DG |
294 | /* Extra precaution */ |
295 | session->kernel_session = NULL; | |
fac6795d | 296 | } |
fac6795d DG |
297 | } |
298 | ||
099e26bd DG |
299 | /* |
300 | * Stop all threads by closing the thread quit pipe. | |
301 | */ | |
cf3af59e MD |
302 | static void stop_threads(void) |
303 | { | |
5eb91c98 DG |
304 | int ret; |
305 | ||
cf3af59e MD |
306 | /* Stopping all threads */ |
307 | DBG("Terminating all threads"); | |
54d01ffb | 308 | ret = notify_thread_pipe(thread_quit_pipe[1]); |
5eb91c98 DG |
309 | if (ret < 0) { |
310 | ERR("write error on thread quit pipe"); | |
311 | } | |
312 | ||
099e26bd DG |
313 | /* Dispatch thread */ |
314 | dispatch_thread_exit = 1; | |
315 | futex_nto1_wake(&ust_cmd_queue.futex); | |
cf3af59e MD |
316 | } |
317 | ||
fac6795d | 318 | /* |
d063d709 | 319 | * Cleanup the daemon |
fac6795d | 320 | */ |
cf3af59e | 321 | static void cleanup(void) |
fac6795d | 322 | { |
1d4b027a DG |
323 | int ret; |
324 | char *cmd; | |
af9737e9 | 325 | struct ltt_session *sess, *stmp; |
fac6795d | 326 | |
1d4b027a | 327 | DBG("Cleaning up"); |
e07ae692 | 328 | |
1d4b027a | 329 | /* <fun> */ |
2f50c8a3 | 330 | MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm" |
273ea72c DG |
331 | "Matthew, BEET driven development works!%c[%dm", |
332 | 27, 1, 31, 27, 0, 27, 1, 33, 27, 0); | |
1d4b027a | 333 | /* </fun> */ |
fac6795d | 334 | |
5eb91c98 DG |
335 | if (is_root) { |
336 | DBG("Removing %s directory", LTTNG_RUNDIR); | |
337 | ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR); | |
338 | if (ret < 0) { | |
339 | ERR("asprintf failed. Something is really wrong!"); | |
340 | } | |
5461b305 | 341 | |
5eb91c98 DG |
342 | /* Remove lttng run directory */ |
343 | ret = system(cmd); | |
344 | if (ret < 0) { | |
345 | ERR("Unable to clean " LTTNG_RUNDIR); | |
346 | } | |
1d4b027a | 347 | } |
5461b305 | 348 | |
1d4b027a | 349 | DBG("Cleaning up all session"); |
fac6795d | 350 | |
b5541356 | 351 | /* Destroy session list mutex */ |
273ea72c DG |
352 | if (session_list_ptr != NULL) { |
353 | pthread_mutex_destroy(&session_list_ptr->lock); | |
354 | ||
355 | /* Cleanup ALL session */ | |
54d01ffb DG |
356 | cds_list_for_each_entry_safe(sess, stmp, |
357 | &session_list_ptr->head, list) { | |
273ea72c DG |
358 | teardown_kernel_session(sess); |
359 | // TODO complete session cleanup (including UST) | |
360 | } | |
361 | } | |
362 | ||
099e26bd | 363 | DBG("Closing all UST sockets"); |
56fff090 | 364 | ust_app_clean_list(); |
099e26bd | 365 | |
3bd1e081 | 366 | pthread_mutex_destroy(&kconsumer_data.pid_mutex); |
b5541356 | 367 | |
f3ed775e | 368 | DBG("Closing kernel fd"); |
1d4b027a | 369 | close(kernel_tracer_fd); |
ab147185 | 370 | |
2f50c8a3 DG |
371 | if (is_root) { |
372 | DBG("Unloading kernel modules"); | |
373 | modprobe_remove_kernel_modules(); | |
374 | } | |
5eb91c98 DG |
375 | |
376 | close(thread_quit_pipe[0]); | |
377 | close(thread_quit_pipe[1]); | |
fac6795d DG |
378 | } |
379 | ||
e065084a | 380 | /* |
d063d709 | 381 | * Send data on a unix socket using the liblttsessiondcomm API. |
e065084a | 382 | * |
d063d709 | 383 | * Return lttcomm error code. |
e065084a DG |
384 | */ |
385 | static int send_unix_sock(int sock, void *buf, size_t len) | |
386 | { | |
387 | /* Check valid length */ | |
388 | if (len <= 0) { | |
389 | return -1; | |
390 | } | |
391 | ||
392 | return lttcomm_send_unix_sock(sock, buf, len); | |
393 | } | |
394 | ||
5461b305 | 395 | /* |
d063d709 | 396 | * Free memory of a command context structure. |
5461b305 | 397 | */ |
a2fb29a5 | 398 | static void clean_command_ctx(struct command_ctx **cmd_ctx) |
5461b305 | 399 | { |
a2fb29a5 DG |
400 | DBG("Clean command context structure"); |
401 | if (*cmd_ctx) { | |
402 | if ((*cmd_ctx)->llm) { | |
403 | free((*cmd_ctx)->llm); | |
5461b305 | 404 | } |
a2fb29a5 DG |
405 | if ((*cmd_ctx)->lsm) { |
406 | free((*cmd_ctx)->lsm); | |
5461b305 | 407 | } |
a2fb29a5 DG |
408 | free(*cmd_ctx); |
409 | *cmd_ctx = NULL; | |
5461b305 DG |
410 | } |
411 | } | |
412 | ||
f3ed775e | 413 | /* |
d063d709 | 414 | * Send all stream fds of kernel channel to the consumer. |
f3ed775e | 415 | */ |
3bd1e081 MD |
416 | static int send_consumer_channel_streams(struct consumer_data *consumer_data, |
417 | int sock, struct ltt_kernel_channel *channel) | |
f3ed775e DG |
418 | { |
419 | int ret; | |
f3ed775e | 420 | struct ltt_kernel_stream *stream; |
3bd1e081 | 421 | struct lttcomm_consumer_msg lkm; |
f3ed775e | 422 | |
3bd1e081 | 423 | DBG("Sending streams of channel %s to kernel consumer", |
54d01ffb | 424 | channel->channel->name); |
f3ed775e | 425 | |
3bd1e081 MD |
426 | /* Send channel */ |
427 | lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; | |
428 | lkm.u.channel.channel_key = channel->fd; | |
429 | lkm.u.channel.max_sb_size = channel->channel->attr.subbuf_size; | |
430 | lkm.u.channel.mmap_len = 0; /* for kernel */ | |
431 | DBG("Sending channel %d to consumer", lkm.u.stream.stream_key); | |
432 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
f3ed775e | 433 | if (ret < 0) { |
3bd1e081 | 434 | perror("send consumer channel"); |
f3ed775e DG |
435 | goto error; |
436 | } | |
437 | ||
3bd1e081 | 438 | /* Send streams */ |
7a485870 | 439 | cds_list_for_each_entry(stream, &channel->stream_list.head, list) { |
3bd1e081 MD |
440 | if (!stream->fd) { |
441 | continue; | |
442 | } | |
443 | lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM; | |
444 | lkm.u.stream.channel_key = channel->fd; | |
445 | lkm.u.stream.stream_key = stream->fd; | |
446 | lkm.u.stream.state = stream->state; | |
447 | lkm.u.stream.output = channel->channel->attr.output; | |
448 | lkm.u.stream.mmap_len = 0; /* for kernel */ | |
449 | strncpy(lkm.u.stream.path_name, stream->pathname, PATH_MAX - 1); | |
450 | lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; | |
451 | DBG("Sending stream %d to consumer", lkm.u.stream.stream_key); | |
452 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
453 | if (ret < 0) { | |
454 | perror("send consumer stream"); | |
455 | goto error; | |
456 | } | |
457 | ret = lttcomm_send_fds_unix_sock(sock, &stream->fd, 1); | |
458 | if (ret < 0) { | |
459 | perror("send consumer stream ancillary data"); | |
460 | goto error; | |
f3ed775e DG |
461 | } |
462 | } | |
463 | ||
3bd1e081 | 464 | DBG("consumer channel streams sent"); |
f3ed775e DG |
465 | |
466 | return 0; | |
467 | ||
468 | error: | |
469 | return ret; | |
470 | } | |
471 | ||
472 | /* | |
d063d709 | 473 | * Send all stream fds of the kernel session to the consumer. |
f3ed775e | 474 | */ |
3bd1e081 MD |
475 | static int send_consumer_session_streams(struct consumer_data *consumer_data, |
476 | struct ltt_kernel_session *session) | |
f3ed775e DG |
477 | { |
478 | int ret; | |
479 | struct ltt_kernel_channel *chan; | |
3bd1e081 MD |
480 | struct lttcomm_consumer_msg lkm; |
481 | int sock = session->consumer_fd; | |
7a485870 DG |
482 | |
483 | DBG("Sending metadata stream fd"); | |
484 | ||
d9800920 DG |
485 | /* Extra protection. It's NOT suppose to be set to 0 at this point */ |
486 | if (session->consumer_fd == 0) { | |
3bd1e081 | 487 | session->consumer_fd = consumer_data->cmd_sock; |
d9800920 DG |
488 | } |
489 | ||
7a485870 | 490 | if (session->metadata_stream_fd != 0) { |
3bd1e081 MD |
491 | /* Send metadata channel fd */ |
492 | lkm.cmd_type = LTTNG_CONSUMER_ADD_CHANNEL; | |
493 | lkm.u.channel.channel_key = session->metadata->fd; | |
494 | lkm.u.channel.max_sb_size = session->metadata->conf->attr.subbuf_size; | |
495 | lkm.u.channel.mmap_len = 0; /* for kernel */ | |
496 | DBG("Sending metadata channel %d to consumer", lkm.u.stream.stream_key); | |
497 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
498 | if (ret < 0) { | |
499 | perror("send consumer channel"); | |
500 | goto error; | |
501 | } | |
502 | ||
503 | /* Send metadata stream fd */ | |
504 | lkm.cmd_type = LTTNG_CONSUMER_ADD_STREAM; | |
505 | lkm.u.stream.channel_key = session->metadata->fd; | |
506 | lkm.u.stream.stream_key = session->metadata_stream_fd; | |
507 | lkm.u.stream.state = LTTNG_CONSUMER_ACTIVE_STREAM; | |
508 | lkm.u.stream.output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
509 | lkm.u.stream.mmap_len = 0; /* for kernel */ | |
510 | strncpy(lkm.u.stream.path_name, session->metadata->pathname, PATH_MAX - 1); | |
511 | lkm.u.stream.path_name[PATH_MAX - 1] = '\0'; | |
512 | DBG("Sending metadata stream %d to consumer", lkm.u.stream.stream_key); | |
513 | ret = lttcomm_send_unix_sock(sock, &lkm, sizeof(lkm)); | |
514 | if (ret < 0) { | |
515 | perror("send consumer stream"); | |
516 | goto error; | |
517 | } | |
518 | ret = lttcomm_send_fds_unix_sock(sock, &session->metadata_stream_fd, 1); | |
7a485870 | 519 | if (ret < 0) { |
3bd1e081 | 520 | perror("send consumer stream"); |
7a485870 DG |
521 | goto error; |
522 | } | |
523 | } | |
f3ed775e | 524 | |
f3ed775e | 525 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
3bd1e081 | 526 | ret = send_consumer_channel_streams(consumer_data, sock, chan); |
f3ed775e | 527 | if (ret < 0) { |
7a485870 | 528 | goto error; |
f3ed775e DG |
529 | } |
530 | } | |
531 | ||
3bd1e081 | 532 | DBG("consumer fds (metadata and channel streams) sent"); |
7a485870 | 533 | |
f3ed775e DG |
534 | return 0; |
535 | ||
536 | error: | |
537 | return ret; | |
538 | } | |
539 | ||
fac6795d | 540 | /* |
0fdd1e2c | 541 | * Notify UST applications using the shm mmap futex. |
fac6795d | 542 | */ |
0fdd1e2c | 543 | static int notify_ust_apps(int active) |
fac6795d | 544 | { |
0fdd1e2c | 545 | char *wait_shm_mmap; |
fac6795d | 546 | |
0fdd1e2c | 547 | DBG("Notifying applications of session daemon state: %d", active); |
e07ae692 | 548 | |
0fdd1e2c DG |
549 | /* See shm.c for this call implying mmap, shm and futex calls */ |
550 | wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root); | |
551 | if (wait_shm_mmap == NULL) { | |
fac6795d DG |
552 | goto error; |
553 | } | |
554 | ||
0fdd1e2c DG |
555 | /* Wake waiting process */ |
556 | futex_wait_update((int32_t *) wait_shm_mmap, active); | |
557 | ||
558 | /* Apps notified successfully */ | |
559 | return 0; | |
fac6795d DG |
560 | |
561 | error: | |
0fdd1e2c | 562 | return -1; |
fac6795d DG |
563 | } |
564 | ||
e065084a | 565 | /* |
d063d709 DG |
566 | * Setup the outgoing data buffer for the response (llm) by allocating the |
567 | * right amount of memory and copying the original information from the lsm | |
568 | * structure. | |
ca95a216 | 569 | * |
d063d709 | 570 | * Return total size of the buffer pointed by buf. |
ca95a216 | 571 | */ |
5461b305 | 572 | static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size) |
ca95a216 | 573 | { |
f3ed775e | 574 | int ret, buf_size; |
ca95a216 | 575 | |
f3ed775e | 576 | buf_size = size; |
5461b305 DG |
577 | |
578 | cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size); | |
579 | if (cmd_ctx->llm == NULL) { | |
ca95a216 | 580 | perror("malloc"); |
5461b305 | 581 | ret = -ENOMEM; |
ca95a216 DG |
582 | goto error; |
583 | } | |
584 | ||
5461b305 DG |
585 | /* Copy common data */ |
586 | cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type; | |
9f19cc17 | 587 | cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid; |
5461b305 | 588 | |
5461b305 DG |
589 | cmd_ctx->llm->data_size = size; |
590 | cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size; | |
591 | ||
ca95a216 DG |
592 | return buf_size; |
593 | ||
594 | error: | |
595 | return ret; | |
596 | } | |
597 | ||
7a485870 | 598 | /* |
5eb91c98 | 599 | * Update the kernel poll set of all channel fd available over all tracing |
d063d709 | 600 | * session. Add the wakeup pipe at the end of the set. |
7a485870 | 601 | */ |
5eb91c98 | 602 | static int update_kernel_poll(struct lttng_poll_event *events) |
7a485870 | 603 | { |
5eb91c98 | 604 | int ret; |
7a485870 DG |
605 | struct ltt_session *session; |
606 | struct ltt_kernel_channel *channel; | |
607 | ||
5eb91c98 | 608 | DBG("Updating kernel poll set"); |
7a485870 | 609 | |
54d01ffb | 610 | session_lock_list(); |
b5541356 | 611 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 612 | session_lock(session); |
7a485870 | 613 | if (session->kernel_session == NULL) { |
54d01ffb | 614 | session_unlock(session); |
7a485870 DG |
615 | continue; |
616 | } | |
7a485870 | 617 | |
54d01ffb DG |
618 | cds_list_for_each_entry(channel, |
619 | &session->kernel_session->channel_list.head, list) { | |
5eb91c98 DG |
620 | /* Add channel fd to the kernel poll set */ |
621 | ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM); | |
622 | if (ret < 0) { | |
54d01ffb | 623 | session_unlock(session); |
5eb91c98 DG |
624 | goto error; |
625 | } | |
626 | DBG("Channel fd %d added to kernel set", channel->fd); | |
7a485870 | 627 | } |
54d01ffb | 628 | session_unlock(session); |
7a485870 | 629 | } |
54d01ffb | 630 | session_unlock_list(); |
7a485870 | 631 | |
5eb91c98 | 632 | return 0; |
7a485870 DG |
633 | |
634 | error: | |
54d01ffb | 635 | session_unlock_list(); |
7a485870 DG |
636 | return -1; |
637 | } | |
638 | ||
639 | /* | |
54d01ffb | 640 | * Find the channel fd from 'fd' over all tracing session. When found, check |
d063d709 | 641 | * for new channel stream and send those stream fds to the kernel consumer. |
7a485870 | 642 | * |
d063d709 | 643 | * Useful for CPU hotplug feature. |
7a485870 | 644 | */ |
3bd1e081 | 645 | static int update_stream(struct consumer_data *consumer_data, int fd) |
7a485870 DG |
646 | { |
647 | int ret = 0; | |
648 | struct ltt_session *session; | |
649 | struct ltt_kernel_channel *channel; | |
650 | ||
651 | DBG("Updating kernel streams for channel fd %d", fd); | |
652 | ||
54d01ffb | 653 | session_lock_list(); |
b5541356 | 654 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { |
54d01ffb | 655 | session_lock(session); |
7a485870 | 656 | if (session->kernel_session == NULL) { |
54d01ffb | 657 | session_unlock(session); |
7a485870 DG |
658 | continue; |
659 | } | |
d9800920 DG |
660 | |
661 | /* This is not suppose to be 0 but this is an extra security check */ | |
662 | if (session->kernel_session->consumer_fd == 0) { | |
3bd1e081 | 663 | session->kernel_session->consumer_fd = consumer_data->cmd_sock; |
d9800920 DG |
664 | } |
665 | ||
5eb91c98 DG |
666 | cds_list_for_each_entry(channel, |
667 | &session->kernel_session->channel_list.head, list) { | |
7a485870 DG |
668 | if (channel->fd == fd) { |
669 | DBG("Channel found, updating kernel streams"); | |
670 | ret = kernel_open_channel_stream(channel); | |
671 | if (ret < 0) { | |
b3c750d2 | 672 | goto error; |
7a485870 | 673 | } |
d9800920 | 674 | |
7a485870 | 675 | /* |
5eb91c98 DG |
676 | * Have we already sent fds to the consumer? If yes, it means |
677 | * that tracing is started so it is safe to send our updated | |
678 | * stream fds. | |
7a485870 | 679 | */ |
3bd1e081 MD |
680 | if (session->kernel_session->consumer_fds_sent == 1) { |
681 | ret = send_consumer_channel_streams(consumer_data, | |
5eb91c98 | 682 | session->kernel_session->consumer_fd, channel); |
7a485870 | 683 | if (ret < 0) { |
b3c750d2 | 684 | goto error; |
7a485870 DG |
685 | } |
686 | } | |
b3c750d2 | 687 | goto error; |
7a485870 DG |
688 | } |
689 | } | |
54d01ffb | 690 | session_unlock(session); |
7a485870 | 691 | } |
54d01ffb | 692 | session_unlock_list(); |
b3c750d2 | 693 | return ret; |
7a485870 | 694 | |
b3c750d2 | 695 | error: |
54d01ffb DG |
696 | session_unlock(session); |
697 | session_unlock_list(); | |
7a485870 DG |
698 | return ret; |
699 | } | |
700 | ||
701 | /* | |
d063d709 | 702 | * This thread manage event coming from the kernel. |
7a485870 | 703 | * |
d063d709 DG |
704 | * Features supported in this thread: |
705 | * -) CPU Hotplug | |
7a485870 DG |
706 | */ |
707 | static void *thread_manage_kernel(void *data) | |
708 | { | |
5eb91c98 DG |
709 | int ret, i, pollfd, update_poll_flag = 1; |
710 | uint32_t revents, nb_fd; | |
7a485870 | 711 | char tmp; |
5eb91c98 | 712 | struct lttng_poll_event events; |
7a485870 DG |
713 | |
714 | DBG("Thread manage kernel started"); | |
715 | ||
5eb91c98 DG |
716 | ret = create_thread_poll_set(&events, 2); |
717 | if (ret < 0) { | |
718 | goto error; | |
719 | } | |
720 | ||
721 | ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN); | |
722 | if (ret < 0) { | |
723 | goto error; | |
724 | } | |
725 | ||
7a485870 DG |
726 | while (1) { |
727 | if (update_poll_flag == 1) { | |
5f822d0a DG |
728 | /* |
729 | * Reset number of fd in the poll set. Always 2 since there is the thread | |
730 | * quit pipe and the kernel pipe. | |
731 | */ | |
732 | events.nb_fd = 2; | |
733 | ||
5eb91c98 DG |
734 | ret = update_kernel_poll(&events); |
735 | if (ret < 0) { | |
7a485870 DG |
736 | goto error; |
737 | } | |
738 | update_poll_flag = 0; | |
739 | } | |
740 | ||
5eb91c98 DG |
741 | nb_fd = LTTNG_POLL_GETNB(&events); |
742 | ||
743 | DBG("Thread kernel polling on %d fds", nb_fd); | |
744 | ||
745 | /* Zeroed the poll events */ | |
746 | lttng_poll_reset(&events); | |
7a485870 DG |
747 | |
748 | /* Poll infinite value of time */ | |
5eb91c98 | 749 | ret = lttng_poll_wait(&events, -1); |
7a485870 | 750 | if (ret < 0) { |
7a485870 DG |
751 | goto error; |
752 | } else if (ret == 0) { | |
753 | /* Should not happen since timeout is infinite */ | |
85611738 DG |
754 | ERR("Return value of poll is 0 with an infinite timeout.\n" |
755 | "This should not have happened! Continuing..."); | |
7a485870 DG |
756 | continue; |
757 | } | |
758 | ||
5eb91c98 DG |
759 | for (i = 0; i < nb_fd; i++) { |
760 | /* Fetch once the poll data */ | |
761 | revents = LTTNG_POLL_GETEV(&events, i); | |
762 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
7a485870 | 763 | |
5eb91c98 DG |
764 | /* Thread quit pipe has been closed. Killing thread. */ |
765 | ret = check_thread_quit_pipe(pollfd, revents); | |
766 | if (ret) { | |
767 | goto error; | |
768 | } | |
7a485870 | 769 | |
5eb91c98 DG |
770 | /* Check for data on kernel pipe */ |
771 | if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) { | |
772 | ret = read(kernel_poll_pipe[0], &tmp, 1); | |
773 | update_poll_flag = 1; | |
774 | continue; | |
775 | } else { | |
776 | /* | |
777 | * New CPU detected by the kernel. Adding kernel stream to | |
778 | * kernel session and updating the kernel consumer | |
779 | */ | |
780 | if (revents & LPOLLIN) { | |
3bd1e081 | 781 | ret = update_stream(&kconsumer_data, pollfd); |
5eb91c98 DG |
782 | if (ret < 0) { |
783 | continue; | |
784 | } | |
785 | break; | |
786 | /* | |
787 | * TODO: We might want to handle the LPOLLERR | LPOLLHUP | |
788 | * and unregister kernel stream at this point. | |
789 | */ | |
7a485870 | 790 | } |
7a485870 DG |
791 | } |
792 | } | |
793 | } | |
794 | ||
795 | error: | |
796 | DBG("Kernel thread dying"); | |
273ea72c DG |
797 | close(kernel_poll_pipe[0]); |
798 | close(kernel_poll_pipe[1]); | |
5eb91c98 DG |
799 | |
800 | lttng_poll_clean(&events); | |
801 | ||
7a485870 DG |
802 | return NULL; |
803 | } | |
804 | ||
1d4b027a | 805 | /* |
3bd1e081 | 806 | * This thread manage the consumer error sent back to the session daemon. |
1d4b027a | 807 | */ |
3bd1e081 | 808 | static void *thread_manage_consumer(void *data) |
1d4b027a | 809 | { |
5eb91c98 DG |
810 | int sock = 0, i, ret, pollfd; |
811 | uint32_t revents, nb_fd; | |
1d4b027a | 812 | enum lttcomm_return_code code; |
5eb91c98 | 813 | struct lttng_poll_event events; |
3bd1e081 | 814 | struct consumer_data *consumer_data = data; |
1d4b027a | 815 | |
3bd1e081 | 816 | DBG("[thread] Manage consumer started"); |
1d4b027a | 817 | |
3bd1e081 | 818 | ret = lttcomm_listen_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
819 | if (ret < 0) { |
820 | goto error; | |
821 | } | |
822 | ||
5eb91c98 DG |
823 | /* |
824 | * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock. | |
825 | * Nothing more will be added to this poll set. | |
826 | */ | |
827 | ret = create_thread_poll_set(&events, 2); | |
828 | if (ret < 0) { | |
829 | goto error; | |
830 | } | |
273ea72c | 831 | |
3bd1e081 | 832 | ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP); |
5eb91c98 DG |
833 | if (ret < 0) { |
834 | goto error; | |
835 | } | |
836 | ||
837 | nb_fd = LTTNG_POLL_GETNB(&events); | |
273ea72c DG |
838 | |
839 | /* Inifinite blocking call, waiting for transmission */ | |
5eb91c98 | 840 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 841 | if (ret < 0) { |
273ea72c DG |
842 | goto error; |
843 | } | |
844 | ||
5eb91c98 DG |
845 | for (i = 0; i < nb_fd; i++) { |
846 | /* Fetch once the poll data */ | |
847 | revents = LTTNG_POLL_GETEV(&events, i); | |
848 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
849 | ||
850 | /* Thread quit pipe has been closed. Killing thread. */ | |
851 | ret = check_thread_quit_pipe(pollfd, revents); | |
852 | if (ret) { | |
853 | goto error; | |
854 | } | |
855 | ||
856 | /* Event on the registration socket */ | |
3bd1e081 | 857 | if (pollfd == consumer_data->err_sock) { |
5eb91c98 | 858 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
3bd1e081 | 859 | ERR("consumer err socket poll error"); |
5eb91c98 DG |
860 | goto error; |
861 | } | |
862 | } | |
273ea72c DG |
863 | } |
864 | ||
3bd1e081 | 865 | sock = lttcomm_accept_unix_sock(consumer_data->err_sock); |
1d4b027a DG |
866 | if (sock < 0) { |
867 | goto error; | |
868 | } | |
869 | ||
3bd1e081 | 870 | DBG2("Receiving code from consumer err_sock"); |
ee0b0061 | 871 | |
712ea556 | 872 | /* Getting status code from kconsumerd */ |
54d01ffb DG |
873 | ret = lttcomm_recv_unix_sock(sock, &code, |
874 | sizeof(enum lttcomm_return_code)); | |
1d4b027a DG |
875 | if (ret <= 0) { |
876 | goto error; | |
877 | } | |
878 | ||
3bd1e081 MD |
879 | if (code == CONSUMERD_COMMAND_SOCK_READY) { |
880 | consumer_data->cmd_sock = | |
881 | lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path); | |
882 | if (consumer_data->cmd_sock < 0) { | |
883 | sem_post(&consumer_data->sem); | |
884 | perror("consumer connect"); | |
1d4b027a DG |
885 | goto error; |
886 | } | |
887 | /* Signal condition to tell that the kconsumerd is ready */ | |
3bd1e081 MD |
888 | sem_post(&consumer_data->sem); |
889 | DBG("consumer command socket ready"); | |
1d4b027a | 890 | } else { |
3bd1e081 | 891 | ERR("consumer error when waiting for SOCK_READY : %s", |
1d4b027a DG |
892 | lttcomm_get_readable_code(-code)); |
893 | goto error; | |
894 | } | |
895 | ||
54d01ffb | 896 | /* Remove the kconsumerd error sock since we've established a connexion */ |
3bd1e081 | 897 | ret = lttng_poll_del(&events, consumer_data->err_sock); |
72079cae | 898 | if (ret < 0) { |
72079cae DG |
899 | goto error; |
900 | } | |
901 | ||
5eb91c98 DG |
902 | ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP); |
903 | if (ret < 0) { | |
72079cae | 904 | goto error; |
5eb91c98 DG |
905 | } |
906 | ||
907 | /* Update number of fd */ | |
908 | nb_fd = LTTNG_POLL_GETNB(&events); | |
909 | ||
910 | /* Inifinite blocking call, waiting for transmission */ | |
911 | ret = lttng_poll_wait(&events, -1); | |
912 | if (ret < 0) { | |
72079cae DG |
913 | goto error; |
914 | } | |
915 | ||
5eb91c98 DG |
916 | for (i = 0; i < nb_fd; i++) { |
917 | /* Fetch once the poll data */ | |
918 | revents = LTTNG_POLL_GETEV(&events, i); | |
919 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
920 | ||
921 | /* Thread quit pipe has been closed. Killing thread. */ | |
922 | ret = check_thread_quit_pipe(pollfd, revents); | |
923 | if (ret) { | |
924 | goto error; | |
925 | } | |
926 | ||
927 | /* Event on the kconsumerd socket */ | |
928 | if (pollfd == sock) { | |
929 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
3bd1e081 | 930 | ERR("consumer err socket second poll error"); |
5eb91c98 DG |
931 | goto error; |
932 | } | |
933 | } | |
934 | } | |
935 | ||
712ea556 | 936 | /* Wait for any kconsumerd error */ |
54d01ffb DG |
937 | ret = lttcomm_recv_unix_sock(sock, &code, |
938 | sizeof(enum lttcomm_return_code)); | |
712ea556 | 939 | if (ret <= 0) { |
3bd1e081 | 940 | ERR("consumer closed the command socket"); |
712ea556 | 941 | goto error; |
6f61d021 | 942 | } |
1d4b027a | 943 | |
3bd1e081 | 944 | ERR("consumer return code : %s", lttcomm_get_readable_code(-code)); |
712ea556 | 945 | |
1d4b027a | 946 | error: |
3bd1e081 MD |
947 | DBG("consumer thread dying"); |
948 | close(consumer_data->err_sock); | |
949 | close(consumer_data->cmd_sock); | |
5eb91c98 | 950 | close(sock); |
273ea72c | 951 | |
3bd1e081 MD |
952 | unlink(consumer_data->err_unix_sock_path); |
953 | unlink(consumer_data->cmd_unix_sock_path); | |
954 | consumer_data->pid = 0; | |
1d4b027a | 955 | |
5eb91c98 | 956 | lttng_poll_clean(&events); |
0177d773 | 957 | |
5eb91c98 | 958 | return NULL; |
099e26bd DG |
959 | } |
960 | ||
099e26bd DG |
961 | /* |
962 | * This thread manage application communication. | |
1d4b027a DG |
963 | */ |
964 | static void *thread_manage_apps(void *data) | |
099e26bd | 965 | { |
5eb91c98 DG |
966 | int i, ret, pollfd; |
967 | uint32_t revents, nb_fd; | |
099e26bd | 968 | struct ust_command ust_cmd; |
5eb91c98 | 969 | struct lttng_poll_event events; |
099e26bd DG |
970 | |
971 | DBG("[thread] Manage application started"); | |
972 | ||
5eb91c98 DG |
973 | ret = create_thread_poll_set(&events, 2); |
974 | if (ret < 0) { | |
975 | goto error; | |
976 | } | |
099e26bd | 977 | |
5eb91c98 DG |
978 | ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP); |
979 | if (ret < 0) { | |
980 | goto error; | |
981 | } | |
099e26bd | 982 | |
5eb91c98 DG |
983 | while (1) { |
984 | /* Zeroed the events structure */ | |
985 | lttng_poll_reset(&events); | |
0177d773 | 986 | |
5eb91c98 | 987 | nb_fd = LTTNG_POLL_GETNB(&events); |
099e26bd DG |
988 | |
989 | DBG("Apps thread polling on %d fds", nb_fd); | |
990 | ||
991 | /* Inifinite blocking call, waiting for transmission */ | |
5eb91c98 | 992 | ret = lttng_poll_wait(&events, -1); |
099e26bd | 993 | if (ret < 0) { |
099e26bd DG |
994 | goto error; |
995 | } | |
996 | ||
5eb91c98 DG |
997 | for (i = 0; i < nb_fd; i++) { |
998 | /* Fetch once the poll data */ | |
999 | revents = LTTNG_POLL_GETEV(&events, i); | |
1000 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
1001 | ||
1002 | /* Thread quit pipe has been closed. Killing thread. */ | |
1003 | ret = check_thread_quit_pipe(pollfd, revents); | |
1004 | if (ret) { | |
099e26bd | 1005 | goto error; |
5eb91c98 | 1006 | } |
099e26bd | 1007 | |
5eb91c98 DG |
1008 | /* Inspect the apps cmd pipe */ |
1009 | if (pollfd == apps_cmd_pipe[0]) { | |
1010 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1011 | ERR("Apps command pipe error"); | |
0177d773 | 1012 | goto error; |
5eb91c98 DG |
1013 | } else if (revents & LPOLLIN) { |
1014 | /* Empty pipe */ | |
1015 | ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd)); | |
1016 | if (ret < 0 || ret < sizeof(ust_cmd)) { | |
1017 | perror("read apps cmd pipe"); | |
1018 | goto error; | |
1019 | } | |
099e26bd | 1020 | |
5eb91c98 | 1021 | /* Register applicaton to the session daemon */ |
56fff090 | 1022 | ret = ust_app_register(&ust_cmd.reg_msg, |
54d01ffb | 1023 | ust_cmd.sock); |
5eb91c98 DG |
1024 | if (ret < 0) { |
1025 | /* Only critical ENOMEM error can be returned here */ | |
1026 | goto error; | |
1027 | } | |
1028 | ||
1029 | ret = ustctl_register_done(ust_cmd.sock); | |
1030 | if (ret < 0) { | |
1031 | /* | |
1032 | * If the registration is not possible, we simply | |
1033 | * unregister the apps and continue | |
1034 | */ | |
56fff090 | 1035 | ust_app_unregister(ust_cmd.sock); |
5eb91c98 DG |
1036 | } else { |
1037 | /* | |
1038 | * We just need here to monitor the close of the UST | |
1039 | * socket and poll set monitor those by default. | |
1040 | */ | |
1041 | ret = lttng_poll_add(&events, ust_cmd.sock, 0); | |
1042 | if (ret < 0) { | |
1043 | goto error; | |
1044 | } | |
1045 | ||
54d01ffb DG |
1046 | DBG("Apps with sock %d added to poll set", |
1047 | ust_cmd.sock); | |
5eb91c98 DG |
1048 | } |
1049 | break; | |
0177d773 | 1050 | } |
5eb91c98 DG |
1051 | } else { |
1052 | /* | |
54d01ffb DG |
1053 | * At this point, we know that a registered application made |
1054 | * the event at poll_wait. | |
5eb91c98 DG |
1055 | */ |
1056 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1057 | /* Removing from the poll set */ | |
1058 | ret = lttng_poll_del(&events, pollfd); | |
1059 | if (ret < 0) { | |
1060 | goto error; | |
1061 | } | |
099e26bd | 1062 | |
5eb91c98 | 1063 | /* Socket closed */ |
56fff090 | 1064 | ust_app_unregister(pollfd); |
5eb91c98 DG |
1065 | break; |
1066 | } | |
099e26bd DG |
1067 | } |
1068 | } | |
099e26bd DG |
1069 | } |
1070 | ||
1071 | error: | |
1072 | DBG("Application communication apps dying"); | |
1073 | close(apps_cmd_pipe[0]); | |
1074 | close(apps_cmd_pipe[1]); | |
1075 | ||
5eb91c98 | 1076 | lttng_poll_clean(&events); |
099e26bd DG |
1077 | |
1078 | return NULL; | |
1079 | } | |
1080 | ||
1081 | /* | |
1082 | * Dispatch request from the registration threads to the application | |
1083 | * communication thread. | |
1084 | */ | |
1085 | static void *thread_dispatch_ust_registration(void *data) | |
1086 | { | |
1087 | int ret; | |
1088 | struct cds_wfq_node *node; | |
1089 | struct ust_command *ust_cmd = NULL; | |
1090 | ||
1091 | DBG("[thread] Dispatch UST command started"); | |
1092 | ||
1093 | while (!dispatch_thread_exit) { | |
1094 | /* Atomically prepare the queue futex */ | |
1095 | futex_nto1_prepare(&ust_cmd_queue.futex); | |
1096 | ||
1097 | do { | |
1098 | /* Dequeue command for registration */ | |
1099 | node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue); | |
1100 | if (node == NULL) { | |
00a17c97 | 1101 | DBG("Woken up but nothing in the UST command queue"); |
099e26bd DG |
1102 | /* Continue thread execution */ |
1103 | break; | |
1104 | } | |
1105 | ||
1106 | ust_cmd = caa_container_of(node, struct ust_command, node); | |
1107 | ||
2f50c8a3 DG |
1108 | DBG("Dispatching UST registration pid:%d ppid:%d uid:%d" |
1109 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1110 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1111 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1112 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1113 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
099e26bd DG |
1114 | /* |
1115 | * Inform apps thread of the new application registration. This | |
1116 | * call is blocking so we can be assured that the data will be read | |
1117 | * at some point in time or wait to the end of the world :) | |
1118 | */ | |
1119 | ret = write(apps_cmd_pipe[1], ust_cmd, | |
1120 | sizeof(struct ust_command)); | |
1121 | if (ret < 0) { | |
1122 | perror("write apps cmd pipe"); | |
1123 | if (errno == EBADF) { | |
1124 | /* | |
1125 | * We can't inform the application thread to process | |
1126 | * registration. We will exit or else application | |
1127 | * registration will not occur and tracing will never | |
1128 | * start. | |
1129 | */ | |
1130 | goto error; | |
1131 | } | |
1132 | } | |
1133 | free(ust_cmd); | |
1134 | } while (node != NULL); | |
1135 | ||
1136 | /* Futex wait on queue. Blocking call on futex() */ | |
1137 | futex_nto1_wait(&ust_cmd_queue.futex); | |
1138 | } | |
1139 | ||
1140 | error: | |
1141 | DBG("Dispatch thread dying"); | |
1142 | return NULL; | |
1143 | } | |
1144 | ||
1145 | /* | |
1146 | * This thread manage application registration. | |
1147 | */ | |
1148 | static void *thread_registration_apps(void *data) | |
1d4b027a | 1149 | { |
5eb91c98 DG |
1150 | int sock = 0, i, ret, pollfd; |
1151 | uint32_t revents, nb_fd; | |
1152 | struct lttng_poll_event events; | |
099e26bd DG |
1153 | /* |
1154 | * Get allocated in this thread, enqueued to a global queue, dequeued and | |
1155 | * freed in the manage apps thread. | |
1156 | */ | |
1157 | struct ust_command *ust_cmd = NULL; | |
1d4b027a | 1158 | |
099e26bd | 1159 | DBG("[thread] Manage application registration started"); |
1d4b027a DG |
1160 | |
1161 | ret = lttcomm_listen_unix_sock(apps_sock); | |
1162 | if (ret < 0) { | |
1163 | goto error; | |
1164 | } | |
1165 | ||
5eb91c98 DG |
1166 | /* |
1167 | * Pass 2 as size here for the thread quit pipe and apps socket. Nothing | |
1168 | * more will be added to this poll set. | |
1169 | */ | |
1170 | ret = create_thread_poll_set(&events, 2); | |
1171 | if (ret < 0) { | |
1172 | goto error; | |
1173 | } | |
273ea72c | 1174 | |
5eb91c98 DG |
1175 | /* Add the application registration socket */ |
1176 | ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP); | |
1177 | if (ret < 0) { | |
1178 | goto error; | |
1179 | } | |
273ea72c | 1180 | |
1d4b027a | 1181 | /* Notify all applications to register */ |
0fdd1e2c DG |
1182 | ret = notify_ust_apps(1); |
1183 | if (ret < 0) { | |
1184 | ERR("Failed to notify applications or create the wait shared memory.\n" | |
54d01ffb DG |
1185 | "Execution continues but there might be problem for already\n" |
1186 | "running applications that wishes to register."); | |
0fdd1e2c | 1187 | } |
1d4b027a DG |
1188 | |
1189 | while (1) { | |
1190 | DBG("Accepting application registration"); | |
273ea72c | 1191 | |
5eb91c98 DG |
1192 | nb_fd = LTTNG_POLL_GETNB(&events); |
1193 | ||
273ea72c | 1194 | /* Inifinite blocking call, waiting for transmission */ |
5eb91c98 | 1195 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 1196 | if (ret < 0) { |
273ea72c DG |
1197 | goto error; |
1198 | } | |
1199 | ||
5eb91c98 DG |
1200 | for (i = 0; i < nb_fd; i++) { |
1201 | /* Fetch once the poll data */ | |
1202 | revents = LTTNG_POLL_GETEV(&events, i); | |
1203 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
273ea72c | 1204 | |
5eb91c98 DG |
1205 | /* Thread quit pipe has been closed. Killing thread. */ |
1206 | ret = check_thread_quit_pipe(pollfd, revents); | |
1207 | if (ret) { | |
90014c57 DG |
1208 | goto error; |
1209 | } | |
1d4b027a | 1210 | |
5eb91c98 DG |
1211 | /* Event on the registration socket */ |
1212 | if (pollfd == apps_sock) { | |
1213 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
1214 | ERR("Register apps socket poll error"); | |
1215 | goto error; | |
1216 | } else if (revents & LPOLLIN) { | |
1217 | sock = lttcomm_accept_unix_sock(apps_sock); | |
1218 | if (sock < 0) { | |
1219 | goto error; | |
1220 | } | |
099e26bd | 1221 | |
5eb91c98 DG |
1222 | /* Create UST registration command for enqueuing */ |
1223 | ust_cmd = malloc(sizeof(struct ust_command)); | |
1224 | if (ust_cmd == NULL) { | |
1225 | perror("ust command malloc"); | |
1226 | goto error; | |
1227 | } | |
1d4b027a | 1228 | |
5eb91c98 DG |
1229 | /* |
1230 | * Using message-based transmissions to ensure we don't | |
1231 | * have to deal with partially received messages. | |
1232 | */ | |
1233 | ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg, | |
1234 | sizeof(struct ust_register_msg)); | |
1235 | if (ret < 0 || ret < sizeof(struct ust_register_msg)) { | |
1236 | if (ret < 0) { | |
1237 | perror("lttcomm_recv_unix_sock register apps"); | |
1238 | } else { | |
1239 | ERR("Wrong size received on apps register"); | |
1240 | } | |
1241 | free(ust_cmd); | |
1242 | close(sock); | |
1243 | continue; | |
1244 | } | |
099e26bd | 1245 | |
5eb91c98 | 1246 | ust_cmd->sock = sock; |
099e26bd | 1247 | |
5eb91c98 DG |
1248 | DBG("UST registration received with pid:%d ppid:%d uid:%d" |
1249 | " gid:%d sock:%d name:%s (version %d.%d)", | |
1250 | ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid, | |
1251 | ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid, | |
1252 | ust_cmd->sock, ust_cmd->reg_msg.name, | |
1253 | ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor); | |
54d01ffb | 1254 | |
5eb91c98 DG |
1255 | /* |
1256 | * Lock free enqueue the registration request. The red pill | |
54d01ffb | 1257 | * has been taken! This apps will be part of the *system*. |
5eb91c98 DG |
1258 | */ |
1259 | cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node); | |
1260 | ||
1261 | /* | |
1262 | * Wake the registration queue futex. Implicit memory | |
1263 | * barrier with the exchange in cds_wfq_enqueue. | |
1264 | */ | |
1265 | futex_nto1_wake(&ust_cmd_queue.futex); | |
1266 | } | |
1267 | } | |
90014c57 | 1268 | } |
1d4b027a DG |
1269 | } |
1270 | ||
1271 | error: | |
0fdd1e2c DG |
1272 | DBG("UST Registration thread dying"); |
1273 | ||
1274 | /* Notify that the registration thread is gone */ | |
1275 | notify_ust_apps(0); | |
1276 | ||
1277 | close(apps_sock); | |
1278 | close(sock); | |
273ea72c | 1279 | unlink(apps_unix_sock_path); |
0fdd1e2c | 1280 | |
5eb91c98 DG |
1281 | lttng_poll_clean(&events); |
1282 | ||
1d4b027a DG |
1283 | return NULL; |
1284 | } | |
1285 | ||
8c0faa1d | 1286 | /* |
3bd1e081 | 1287 | * Start the thread_manage_consumer. This must be done after a lttng-consumerd |
d063d709 | 1288 | * exec or it will fails. |
8c0faa1d | 1289 | */ |
3bd1e081 | 1290 | static int spawn_consumer_thread(struct consumer_data *consumer_data) |
8c0faa1d DG |
1291 | { |
1292 | int ret; | |
ee0b0061 DG |
1293 | struct timespec timeout; |
1294 | ||
1295 | timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT; | |
1296 | timeout.tv_nsec = 0; | |
8c0faa1d DG |
1297 | |
1298 | /* Setup semaphore */ | |
3bd1e081 | 1299 | ret = sem_init(&consumer_data->sem, 0, 0); |
ee0b0061 | 1300 | if (ret < 0) { |
3bd1e081 | 1301 | PERROR("sem_init consumer semaphore"); |
ee0b0061 DG |
1302 | goto error; |
1303 | } | |
8c0faa1d | 1304 | |
3bd1e081 MD |
1305 | ret = pthread_create(&consumer_data->thread, NULL, |
1306 | thread_manage_consumer, consumer_data); | |
8c0faa1d | 1307 | if (ret != 0) { |
3bd1e081 | 1308 | PERROR("pthread_create consumer"); |
ee0b0061 | 1309 | ret = -1; |
8c0faa1d DG |
1310 | goto error; |
1311 | } | |
1312 | ||
ee0b0061 DG |
1313 | /* Get time for sem_timedwait absolute timeout */ |
1314 | ret = clock_gettime(CLOCK_REALTIME, &timeout); | |
1315 | if (ret < 0) { | |
3bd1e081 | 1316 | PERROR("clock_gettime spawn consumer"); |
ee0b0061 | 1317 | /* Infinite wait for the kconsumerd thread to be ready */ |
3bd1e081 | 1318 | ret = sem_wait(&consumer_data->sem); |
ee0b0061 DG |
1319 | } else { |
1320 | /* Normal timeout if the gettime was successful */ | |
1321 | timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT; | |
3bd1e081 | 1322 | ret = sem_timedwait(&consumer_data->sem, &timeout); |
ee0b0061 | 1323 | } |
8c0faa1d | 1324 | |
ee0b0061 DG |
1325 | if (ret < 0) { |
1326 | if (errno == ETIMEDOUT) { | |
1327 | /* | |
1328 | * Call has timed out so we kill the kconsumerd_thread and return | |
1329 | * an error. | |
1330 | */ | |
3bd1e081 MD |
1331 | ERR("The consumer thread was never ready. Killing it"); |
1332 | ret = pthread_cancel(consumer_data->thread); | |
ee0b0061 | 1333 | if (ret < 0) { |
3bd1e081 | 1334 | PERROR("pthread_cancel consumer thread"); |
ee0b0061 DG |
1335 | } |
1336 | } else { | |
3bd1e081 | 1337 | PERROR("semaphore wait failed consumer thread"); |
ee0b0061 DG |
1338 | } |
1339 | goto error; | |
1340 | } | |
1341 | ||
3bd1e081 MD |
1342 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1343 | if (consumer_data->pid == 0) { | |
712ea556 | 1344 | ERR("Kconsumerd did not start"); |
3bd1e081 | 1345 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 DG |
1346 | goto error; |
1347 | } | |
3bd1e081 | 1348 | pthread_mutex_unlock(&consumer_data->pid_mutex); |
712ea556 | 1349 | |
8c0faa1d DG |
1350 | return 0; |
1351 | ||
1352 | error: | |
1353 | return ret; | |
1354 | } | |
1355 | ||
d9800920 | 1356 | /* |
3bd1e081 | 1357 | * Join consumer thread |
d9800920 | 1358 | */ |
3bd1e081 | 1359 | static int join_consumer_thread(struct consumer_data *consumer_data) |
cf3af59e MD |
1360 | { |
1361 | void *status; | |
1362 | int ret; | |
1363 | ||
3bd1e081 MD |
1364 | if (consumer_data->pid != 0) { |
1365 | ret = kill(consumer_data->pid, SIGTERM); | |
cf3af59e | 1366 | if (ret) { |
3bd1e081 | 1367 | ERR("Error killing consumer daemon"); |
cf3af59e MD |
1368 | return ret; |
1369 | } | |
3bd1e081 | 1370 | return pthread_join(consumer_data->thread, &status); |
cf3af59e MD |
1371 | } else { |
1372 | return 0; | |
1373 | } | |
1374 | } | |
1375 | ||
8c0faa1d | 1376 | /* |
3bd1e081 | 1377 | * Fork and exec a consumer daemon (consumerd). |
8c0faa1d | 1378 | * |
d063d709 | 1379 | * Return pid if successful else -1. |
8c0faa1d | 1380 | */ |
3bd1e081 | 1381 | static pid_t spawn_consumerd(struct consumer_data *consumer_data) |
8c0faa1d DG |
1382 | { |
1383 | int ret; | |
1384 | pid_t pid; | |
53086306 | 1385 | const char *verbosity; |
8c0faa1d | 1386 | |
3bd1e081 | 1387 | DBG("Spawning consumerd"); |
c49dc785 | 1388 | |
8c0faa1d DG |
1389 | pid = fork(); |
1390 | if (pid == 0) { | |
1391 | /* | |
3bd1e081 | 1392 | * Exec consumerd. |
8c0faa1d | 1393 | */ |
3bd1e081 | 1394 | if (opt_verbose > 1 || opt_verbose_consumer) { |
53086306 DG |
1395 | verbosity = "--verbose"; |
1396 | } else { | |
1397 | verbosity = "--quiet"; | |
1398 | } | |
3bd1e081 MD |
1399 | switch (consumer_data->type) { |
1400 | case LTTNG_CONSUMER_KERNEL: | |
1401 | execl(INSTALL_BIN_PATH "/lttng-consumerd", | |
1402 | "lttng-consumerd", verbosity, "-k", NULL); | |
1403 | break; | |
1404 | case LTTNG_CONSUMER_UST: | |
1405 | execl(INSTALL_BIN_PATH "/lttng-consumerd", | |
1406 | "lttng-consumerd", verbosity, "-u", NULL); | |
1407 | break; | |
1408 | default: | |
1409 | perror("unknown consumer type"); | |
1410 | exit(EXIT_FAILURE); | |
1411 | } | |
8c0faa1d DG |
1412 | if (errno != 0) { |
1413 | perror("kernel start consumer exec"); | |
1414 | } | |
1415 | exit(EXIT_FAILURE); | |
1416 | } else if (pid > 0) { | |
1417 | ret = pid; | |
8c0faa1d | 1418 | } else { |
3bd1e081 | 1419 | perror("start consumer fork"); |
8c0faa1d | 1420 | ret = -errno; |
8c0faa1d | 1421 | } |
8c0faa1d DG |
1422 | return ret; |
1423 | } | |
1424 | ||
693bd40b | 1425 | /* |
3bd1e081 | 1426 | * Spawn the consumerd daemon and session daemon thread. |
693bd40b | 1427 | */ |
3bd1e081 | 1428 | static int start_consumerd(struct consumer_data *consumer_data) |
693bd40b DG |
1429 | { |
1430 | int ret; | |
1431 | ||
3bd1e081 MD |
1432 | pthread_mutex_lock(&consumer_data->pid_mutex); |
1433 | if (consumer_data->pid != 0) { | |
1434 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 DG |
1435 | goto end; |
1436 | } | |
693bd40b | 1437 | |
3bd1e081 | 1438 | ret = spawn_consumerd(consumer_data); |
c49dc785 | 1439 | if (ret < 0) { |
3bd1e081 MD |
1440 | ERR("Spawning consumerd failed"); |
1441 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
c49dc785 | 1442 | goto error; |
693bd40b | 1443 | } |
c49dc785 | 1444 | |
3bd1e081 MD |
1445 | /* Setting up the consumer_data pid */ |
1446 | consumer_data->pid = ret; | |
1447 | DBG2("consumer pid %d", consumer_data->pid); | |
1448 | pthread_mutex_unlock(&consumer_data->pid_mutex); | |
693bd40b | 1449 | |
3bd1e081 MD |
1450 | DBG2("Spawning consumer control thread"); |
1451 | ret = spawn_consumer_thread(consumer_data); | |
693bd40b | 1452 | if (ret < 0) { |
3bd1e081 | 1453 | ERR("Fatal error spawning consumer control thread"); |
693bd40b DG |
1454 | goto error; |
1455 | } | |
1456 | ||
c49dc785 | 1457 | end: |
693bd40b DG |
1458 | return 0; |
1459 | ||
1460 | error: | |
1461 | return ret; | |
1462 | } | |
1463 | ||
b73401da | 1464 | /* |
d063d709 | 1465 | * modprobe_kernel_modules |
b73401da DG |
1466 | */ |
1467 | static int modprobe_kernel_modules(void) | |
1468 | { | |
ab147185 | 1469 | int ret = 0, i; |
b73401da DG |
1470 | char modprobe[256]; |
1471 | ||
ab147185 MD |
1472 | for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) { |
1473 | ret = snprintf(modprobe, sizeof(modprobe), | |
1474 | "/sbin/modprobe %s%s", | |
1475 | kernel_modules_list[i].required ? "" : "--quiet ", | |
1476 | kernel_modules_list[i].name); | |
b73401da DG |
1477 | if (ret < 0) { |
1478 | perror("snprintf modprobe"); | |
1479 | goto error; | |
1480 | } | |
ab147185 | 1481 | modprobe[sizeof(modprobe) - 1] = '\0'; |
b73401da | 1482 | ret = system(modprobe); |
ab147185 MD |
1483 | if (ret == -1) { |
1484 | ERR("Unable to launch modprobe for module %s", | |
1485 | kernel_modules_list[i].name); | |
1486 | } else if (kernel_modules_list[i].required | |
d9800920 | 1487 | && WEXITSTATUS(ret) != 0) { |
ab147185 MD |
1488 | ERR("Unable to load module %s", |
1489 | kernel_modules_list[i].name); | |
1490 | } else { | |
1491 | DBG("Modprobe successfully %s", | |
1492 | kernel_modules_list[i].name); | |
1493 | } | |
1494 | } | |
1495 | ||
1496 | error: | |
1497 | return ret; | |
1498 | } | |
1499 | ||
b73401da | 1500 | /* |
d063d709 | 1501 | * mount_debugfs |
b73401da DG |
1502 | */ |
1503 | static int mount_debugfs(char *path) | |
1504 | { | |
1505 | int ret; | |
1506 | char *type = "debugfs"; | |
1507 | ||
996b65c8 | 1508 | ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid()); |
b73401da | 1509 | if (ret < 0) { |
7272acf5 | 1510 | PERROR("Cannot create debugfs path"); |
b73401da DG |
1511 | goto error; |
1512 | } | |
1513 | ||
1514 | ret = mount(type, path, type, 0, NULL); | |
1515 | if (ret < 0) { | |
7272acf5 | 1516 | PERROR("Cannot mount debugfs"); |
b73401da DG |
1517 | goto error; |
1518 | } | |
1519 | ||
1520 | DBG("Mounted debugfs successfully at %s", path); | |
1521 | ||
1522 | error: | |
1523 | return ret; | |
1524 | } | |
1525 | ||
8c0faa1d | 1526 | /* |
d063d709 | 1527 | * Setup necessary data for kernel tracer action. |
8c0faa1d | 1528 | */ |
f3ed775e | 1529 | static void init_kernel_tracer(void) |
8c0faa1d | 1530 | { |
b73401da DG |
1531 | int ret; |
1532 | char *proc_mounts = "/proc/mounts"; | |
1533 | char line[256]; | |
684d34d2 | 1534 | char *debugfs_path = NULL, *lttng_path = NULL; |
b73401da DG |
1535 | FILE *fp; |
1536 | ||
1537 | /* Detect debugfs */ | |
1538 | fp = fopen(proc_mounts, "r"); | |
1539 | if (fp == NULL) { | |
1540 | ERR("Unable to probe %s", proc_mounts); | |
1541 | goto error; | |
1542 | } | |
1543 | ||
1544 | while (fgets(line, sizeof(line), fp) != NULL) { | |
1545 | if (strstr(line, "debugfs") != NULL) { | |
1546 | /* Remove first string */ | |
1547 | strtok(line, " "); | |
1548 | /* Dup string here so we can reuse line later on */ | |
1549 | debugfs_path = strdup(strtok(NULL, " ")); | |
1550 | DBG("Got debugfs path : %s", debugfs_path); | |
1551 | break; | |
1552 | } | |
1553 | } | |
1554 | ||
1555 | fclose(fp); | |
1556 | ||
1557 | /* Mount debugfs if needded */ | |
1558 | if (debugfs_path == NULL) { | |
1559 | ret = asprintf(&debugfs_path, "/mnt/debugfs"); | |
1560 | if (ret < 0) { | |
1561 | perror("asprintf debugfs path"); | |
1562 | goto error; | |
1563 | } | |
1564 | ret = mount_debugfs(debugfs_path); | |
1565 | if (ret < 0) { | |
7272acf5 | 1566 | perror("Cannot mount debugfs"); |
b73401da DG |
1567 | goto error; |
1568 | } | |
1569 | } | |
1570 | ||
1571 | /* Modprobe lttng kernel modules */ | |
1572 | ret = modprobe_kernel_modules(); | |
1573 | if (ret < 0) { | |
1574 | goto error; | |
1575 | } | |
1576 | ||
1577 | /* Setup lttng kernel path */ | |
1578 | ret = asprintf(<tng_path, "%s/lttng", debugfs_path); | |
1579 | if (ret < 0) { | |
1580 | perror("asprintf lttng path"); | |
1581 | goto error; | |
1582 | } | |
1583 | ||
1584 | /* Open debugfs lttng */ | |
1585 | kernel_tracer_fd = open(lttng_path, O_RDWR); | |
f3ed775e | 1586 | if (kernel_tracer_fd < 0) { |
b73401da DG |
1587 | DBG("Failed to open %s", lttng_path); |
1588 | goto error; | |
8c0faa1d DG |
1589 | } |
1590 | ||
b73401da DG |
1591 | free(lttng_path); |
1592 | free(debugfs_path); | |
f3ed775e | 1593 | DBG("Kernel tracer fd %d", kernel_tracer_fd); |
b73401da DG |
1594 | return; |
1595 | ||
1596 | error: | |
1597 | if (lttng_path) { | |
1598 | free(lttng_path); | |
1599 | } | |
1600 | if (debugfs_path) { | |
1601 | free(debugfs_path); | |
1602 | } | |
1603 | WARN("No kernel tracer available"); | |
1604 | kernel_tracer_fd = 0; | |
1605 | return; | |
f3ed775e | 1606 | } |
33a2b854 | 1607 | |
f3ed775e | 1608 | /* |
54d01ffb | 1609 | * Init tracing by creating trace directory and sending fds kernel consumer. |
f3ed775e | 1610 | */ |
54d01ffb | 1611 | static int init_kernel_tracing(struct ltt_kernel_session *session) |
f3ed775e | 1612 | { |
f40799e8 | 1613 | int ret = 0; |
8c0faa1d | 1614 | |
3bd1e081 | 1615 | if (session->consumer_fds_sent == 0) { |
d9800920 | 1616 | /* |
54d01ffb DG |
1617 | * Assign default kernel consumer socket if no consumer assigned to the |
1618 | * kernel session. At this point, it's NOT suppose to be 0 but this is | |
1619 | * an extra security check. | |
d9800920 DG |
1620 | */ |
1621 | if (session->consumer_fd == 0) { | |
3bd1e081 | 1622 | session->consumer_fd = kconsumer_data.cmd_sock; |
d9800920 DG |
1623 | } |
1624 | ||
3bd1e081 | 1625 | ret = send_consumer_session_streams(&kconsumer_data, session); |
f3ed775e | 1626 | if (ret < 0) { |
f3ed775e DG |
1627 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
1628 | goto error; | |
1629 | } | |
1d4b027a | 1630 | |
3bd1e081 | 1631 | session->consumer_fds_sent = 1; |
f3ed775e | 1632 | } |
1d4b027a DG |
1633 | |
1634 | error: | |
1635 | return ret; | |
8c0faa1d DG |
1636 | } |
1637 | ||
0177d773 DG |
1638 | /* |
1639 | * Create an UST session and add it to the session ust list. | |
1640 | */ | |
44d3bd01 DG |
1641 | static int create_ust_session(struct ltt_session *session, |
1642 | struct lttng_domain *domain) | |
0177d773 | 1643 | { |
44d3bd01 | 1644 | int ret; |
13384287 | 1645 | struct ltt_ust_session *lus = NULL; |
56fff090 | 1646 | struct ust_app *app; |
44d3bd01 DG |
1647 | |
1648 | switch (domain->type) { | |
1649 | case LTTNG_DOMAIN_UST_PID: | |
56fff090 | 1650 | app = ust_app_get_by_pid(domain->attr.pid); |
44d3bd01 DG |
1651 | if (app == NULL) { |
1652 | ret = LTTCOMM_APP_NOT_FOUND; | |
1653 | goto error; | |
1654 | } | |
1655 | break; | |
1656 | default: | |
13384287 | 1657 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
44d3bd01 DG |
1658 | goto error; |
1659 | } | |
0177d773 DG |
1660 | |
1661 | DBG("Creating UST session"); | |
1662 | ||
44d3bd01 | 1663 | lus = trace_ust_create_session(session->path, domain->attr.pid, domain); |
0177d773 | 1664 | if (lus == NULL) { |
44d3bd01 | 1665 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
1666 | goto error; |
1667 | } | |
1668 | ||
1669 | ret = mkdir_recursive(lus->path, S_IRWXU | S_IRWXG, | |
1670 | geteuid(), allowed_group()); | |
1671 | if (ret < 0) { | |
1672 | if (ret != -EEXIST) { | |
1673 | ERR("Trace directory creation error"); | |
44d3bd01 | 1674 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
1675 | goto error; |
1676 | } | |
1677 | } | |
1678 | ||
1679 | /* Create session on the UST tracer */ | |
44d3bd01 | 1680 | ret = ustctl_create_session(app->sock, lus); |
0177d773 | 1681 | if (ret < 0) { |
44d3bd01 | 1682 | ret = LTTCOMM_UST_SESS_FAIL; |
0177d773 DG |
1683 | goto error; |
1684 | } | |
1685 | ||
44d3bd01 DG |
1686 | cds_list_add(&lus->list, &session->ust_session_list.head); |
1687 | session->ust_session_list.count++; | |
1688 | ||
1689 | return LTTCOMM_OK; | |
0177d773 DG |
1690 | |
1691 | error: | |
1692 | free(lus); | |
1693 | return ret; | |
1694 | } | |
1695 | ||
333f285e | 1696 | /* |
d063d709 | 1697 | * Create a kernel tracer session then create the default channel. |
333f285e | 1698 | */ |
f3ed775e | 1699 | static int create_kernel_session(struct ltt_session *session) |
333f285e | 1700 | { |
f3ed775e | 1701 | int ret; |
f3ed775e DG |
1702 | |
1703 | DBG("Creating kernel session"); | |
1704 | ||
1705 | ret = kernel_create_session(session, kernel_tracer_fd); | |
1706 | if (ret < 0) { | |
1707 | ret = LTTCOMM_KERN_SESS_FAIL; | |
1708 | goto error; | |
333f285e DG |
1709 | } |
1710 | ||
d9800920 | 1711 | /* Set kernel consumer socket fd */ |
3bd1e081 MD |
1712 | if (kconsumer_data.cmd_sock) { |
1713 | session->kernel_session->consumer_fd = kconsumer_data.cmd_sock; | |
d9800920 DG |
1714 | } |
1715 | ||
63053e7c DG |
1716 | ret = mkdir_recursive(session->kernel_session->trace_path, |
1717 | S_IRWXU | S_IRWXG, geteuid(), allowed_group()); | |
7a485870 | 1718 | if (ret < 0) { |
996b65c8 | 1719 | if (ret != -EEXIST) { |
7a485870 DG |
1720 | ERR("Trace directory creation error"); |
1721 | goto error; | |
1722 | } | |
1723 | } | |
1724 | ||
f3ed775e DG |
1725 | error: |
1726 | return ret; | |
333f285e DG |
1727 | } |
1728 | ||
6c9cc2ab DG |
1729 | /* |
1730 | * Using the session list, filled a lttng_session array to send back to the | |
1731 | * client for session listing. | |
1732 | * | |
1733 | * The session list lock MUST be acquired before calling this function. Use | |
54d01ffb | 1734 | * session_lock_list() and session_unlock_list(). |
6c9cc2ab DG |
1735 | */ |
1736 | static void list_lttng_sessions(struct lttng_session *sessions) | |
1737 | { | |
1738 | int i = 0; | |
1739 | struct ltt_session *session; | |
1740 | ||
1741 | DBG("Getting all available session"); | |
1742 | /* | |
1743 | * Iterate over session list and append data after the control struct in | |
1744 | * the buffer. | |
1745 | */ | |
1746 | cds_list_for_each_entry(session, &session_list_ptr->head, list) { | |
1747 | strncpy(sessions[i].path, session->path, PATH_MAX); | |
99497cd0 | 1748 | sessions[i].path[PATH_MAX - 1] = '\0'; |
6c9cc2ab | 1749 | strncpy(sessions[i].name, session->name, NAME_MAX); |
99497cd0 | 1750 | sessions[i].name[NAME_MAX - 1] = '\0'; |
6c9cc2ab DG |
1751 | i++; |
1752 | } | |
1753 | } | |
1754 | ||
9f19cc17 DG |
1755 | /* |
1756 | * Fill lttng_channel array of all channels. | |
1757 | */ | |
1758 | static void list_lttng_channels(struct ltt_session *session, | |
1759 | struct lttng_channel *channels) | |
1760 | { | |
1761 | int i = 0; | |
1762 | struct ltt_kernel_channel *kchan; | |
1763 | ||
1764 | DBG("Listing channels for session %s", session->name); | |
1765 | ||
1766 | /* Kernel channels */ | |
1767 | if (session->kernel_session != NULL) { | |
54d01ffb DG |
1768 | cds_list_for_each_entry(kchan, |
1769 | &session->kernel_session->channel_list.head, list) { | |
9f19cc17 DG |
1770 | /* Copy lttng_channel struct to array */ |
1771 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
1772 | channels[i].enabled = kchan->enabled; | |
1773 | i++; | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | /* TODO: Missing UST listing */ | |
1778 | } | |
1779 | ||
1780 | /* | |
1781 | * Fill lttng_event array of all events in the channel. | |
1782 | */ | |
1783 | static void list_lttng_events(struct ltt_kernel_channel *kchan, | |
1784 | struct lttng_event *events) | |
1785 | { | |
1786 | /* | |
1787 | * TODO: This is ONLY kernel. Need UST support. | |
1788 | */ | |
1789 | int i = 0; | |
1790 | struct ltt_kernel_event *event; | |
1791 | ||
1792 | DBG("Listing events for channel %s", kchan->channel->name); | |
1793 | ||
1794 | /* Kernel channels */ | |
1795 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
1796 | strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
99497cd0 | 1797 | events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
9f19cc17 DG |
1798 | events[i].enabled = event->enabled; |
1799 | switch (event->event->instrumentation) { | |
1800 | case LTTNG_KERNEL_TRACEPOINT: | |
1801 | events[i].type = LTTNG_EVENT_TRACEPOINT; | |
1802 | break; | |
1803 | case LTTNG_KERNEL_KPROBE: | |
1804 | case LTTNG_KERNEL_KRETPROBE: | |
1805 | events[i].type = LTTNG_EVENT_PROBE; | |
1806 | memcpy(&events[i].attr.probe, &event->event->u.kprobe, | |
1807 | sizeof(struct lttng_kernel_kprobe)); | |
1808 | break; | |
1809 | case LTTNG_KERNEL_FUNCTION: | |
1810 | events[i].type = LTTNG_EVENT_FUNCTION; | |
1811 | memcpy(&events[i].attr.ftrace, &event->event->u.ftrace, | |
1812 | sizeof(struct lttng_kernel_function)); | |
1813 | break; | |
0133c199 MD |
1814 | case LTTNG_KERNEL_NOOP: |
1815 | events[i].type = LTTNG_EVENT_NOOP; | |
1816 | break; | |
a54bd42d MD |
1817 | case LTTNG_KERNEL_SYSCALL: |
1818 | events[i].type = LTTNG_EVENT_SYSCALL; | |
0133c199 | 1819 | break; |
7a3d1328 MD |
1820 | case LTTNG_KERNEL_ALL: |
1821 | assert(0); | |
1822 | break; | |
9f19cc17 DG |
1823 | } |
1824 | i++; | |
1825 | } | |
1826 | } | |
1827 | ||
fac6795d | 1828 | /* |
54d01ffb | 1829 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. |
fac6795d | 1830 | */ |
54d01ffb DG |
1831 | static int cmd_disable_channel(struct ltt_session *session, |
1832 | int domain, char *channel_name) | |
fac6795d | 1833 | { |
54d01ffb | 1834 | int ret; |
379473d2 | 1835 | |
54d01ffb DG |
1836 | switch (domain) { |
1837 | case LTTNG_DOMAIN_KERNEL: | |
1838 | ret = channel_kernel_disable(session->kernel_session, | |
1839 | channel_name); | |
1840 | if (ret != LTTCOMM_OK) { | |
333f285e DG |
1841 | goto error; |
1842 | } | |
54d01ffb DG |
1843 | |
1844 | kernel_wait_quiescent(kernel_tracer_fd); | |
d0254c7c | 1845 | break; |
44d3bd01 DG |
1846 | case LTTNG_DOMAIN_UST_PID: |
1847 | break; | |
d0254c7c | 1848 | default: |
44d3bd01 | 1849 | ret = LTTCOMM_UNKNOWN_DOMAIN; |
54d01ffb | 1850 | goto error; |
20fe2104 DG |
1851 | } |
1852 | ||
54d01ffb | 1853 | ret = LTTCOMM_OK; |
d65106b1 | 1854 | |
54d01ffb DG |
1855 | error: |
1856 | return ret; | |
1857 | } | |
1858 | ||
56fff090 DG |
1859 | /* |
1860 | * Copy channel from attributes and set it in the application channel list. | |
1861 | */ | |
1862 | static int copy_ust_channel_to_app(struct ltt_ust_session *usess, | |
1863 | struct lttng_channel *attr, struct ust_app *app) | |
1864 | { | |
1865 | int ret; | |
1866 | struct ltt_ust_channel *uchan, *new_chan; | |
1867 | ||
1868 | uchan = trace_ust_get_channel_by_name(attr->name, usess); | |
1869 | if (uchan == NULL) { | |
1870 | ret = LTTCOMM_FATAL; | |
1871 | goto error; | |
1872 | } | |
1873 | ||
1874 | new_chan = trace_ust_create_channel(attr, usess->path); | |
1875 | if (new_chan == NULL) { | |
1876 | PERROR("malloc ltt_ust_channel"); | |
1877 | ret = LTTCOMM_FATAL; | |
1878 | goto error; | |
1879 | } | |
1880 | ||
1881 | ret = channel_ust_copy(new_chan, uchan); | |
1882 | if (ret < 0) { | |
1883 | ret = LTTCOMM_FATAL; | |
1884 | goto error; | |
1885 | } | |
1886 | ||
1887 | /* Add channel to the ust app channel list */ | |
1888 | cds_list_add(&new_chan->list, &app->channels.head); | |
1889 | app->channels.count++; | |
1890 | ||
1891 | error: | |
1892 | return ret; | |
1893 | } | |
1894 | ||
54d01ffb DG |
1895 | /* |
1896 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1897 | */ | |
44d3bd01 DG |
1898 | static int cmd_enable_channel(struct ltt_session *session, |
1899 | struct lttng_domain *domain, struct lttng_channel *attr) | |
54d01ffb DG |
1900 | { |
1901 | int ret; | |
d65106b1 | 1902 | |
44d3bd01 DG |
1903 | switch (domain->type) { |
1904 | case LTTNG_DOMAIN_KERNEL: | |
1905 | { | |
1906 | struct ltt_kernel_channel *kchan; | |
54d01ffb | 1907 | |
44d3bd01 DG |
1908 | kchan = trace_kernel_get_channel_by_name(attr->name, |
1909 | session->kernel_session); | |
1910 | if (kchan == NULL) { | |
1911 | ret = channel_kernel_create(session->kernel_session, | |
1912 | attr, kernel_poll_pipe[1]); | |
1913 | } else { | |
1914 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1915 | } | |
1916 | ||
1917 | if (ret != LTTCOMM_OK) { | |
1918 | goto error; | |
1919 | } | |
1920 | ||
1921 | kernel_wait_quiescent(kernel_tracer_fd); | |
1922 | break; | |
1923 | } | |
1924 | case LTTNG_DOMAIN_UST_PID: | |
1925 | { | |
56fff090 DG |
1926 | int sock; |
1927 | struct ltt_ust_channel *uchan; | |
44d3bd01 | 1928 | struct ltt_ust_session *usess; |
56fff090 | 1929 | struct ust_app *app; |
44d3bd01 DG |
1930 | |
1931 | usess = trace_ust_get_session_by_pid(&session->ust_session_list, | |
1932 | domain->attr.pid); | |
1933 | if (usess == NULL) { | |
1934 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
1935 | goto error; | |
1936 | } | |
1937 | ||
56fff090 | 1938 | app = ust_app_get_by_pid(domain->attr.pid); |
44d3bd01 DG |
1939 | if (app == NULL) { |
1940 | ret = LTTCOMM_APP_NOT_FOUND; | |
1941 | goto error; | |
1942 | } | |
56fff090 | 1943 | sock = app->sock; |
44d3bd01 DG |
1944 | |
1945 | uchan = trace_ust_get_channel_by_name(attr->name, usess); | |
1946 | if (uchan == NULL) { | |
56fff090 | 1947 | ret = channel_ust_create(usess, attr, sock); |
44d3bd01 | 1948 | } else { |
56fff090 | 1949 | ret = channel_ust_enable(usess, uchan, sock); |
44d3bd01 DG |
1950 | } |
1951 | ||
1952 | if (ret != LTTCOMM_OK) { | |
1953 | goto error; | |
1954 | } | |
1955 | ||
56fff090 DG |
1956 | ret = copy_ust_channel_to_app(usess, attr, app); |
1957 | if (ret != LTTCOMM_OK) { | |
44d3bd01 DG |
1958 | goto error; |
1959 | } | |
1960 | ||
44d3bd01 DG |
1961 | DBG("UST channel %s created for app sock %d with pid %d", |
1962 | attr->name, app->sock, domain->attr.pid); | |
1963 | break; | |
1964 | } | |
1965 | default: | |
1966 | ret = LTTCOMM_UNKNOWN_DOMAIN; | |
1967 | goto error; | |
54d01ffb DG |
1968 | } |
1969 | ||
1970 | ret = LTTCOMM_OK; | |
1971 | ||
1972 | error: | |
1973 | return ret; | |
1974 | } | |
1975 | ||
1976 | /* | |
1977 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1978 | */ | |
1979 | static int cmd_disable_event(struct ltt_session *session, int domain, | |
1980 | char *channel_name, char *event_name) | |
1981 | { | |
1982 | int ret; | |
1983 | struct ltt_kernel_channel *kchan; | |
1984 | ||
1985 | switch (domain) { | |
1986 | case LTTNG_DOMAIN_KERNEL: | |
1987 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
1988 | session->kernel_session); | |
1989 | if (kchan == NULL) { | |
1990 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
1991 | goto error; | |
d65106b1 DG |
1992 | } |
1993 | ||
7a3d1328 | 1994 | ret = event_kernel_disable_tracepoint(session->kernel_session, kchan, event_name); |
54d01ffb DG |
1995 | if (ret != LTTCOMM_OK) { |
1996 | goto error; | |
1997 | } | |
1998 | ||
1999 | kernel_wait_quiescent(kernel_tracer_fd); | |
d65106b1 | 2000 | break; |
54d01ffb DG |
2001 | default: |
2002 | /* TODO: Userspace tracing */ | |
2003 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2004 | goto error; | |
d65106b1 | 2005 | } |
26cc6b4e | 2006 | |
54d01ffb DG |
2007 | ret = LTTCOMM_OK; |
2008 | ||
2009 | error: | |
2010 | return ret; | |
2011 | } | |
2012 | ||
2013 | /* | |
2014 | * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread. | |
2015 | */ | |
2016 | static int cmd_disable_event_all(struct ltt_session *session, int domain, | |
2017 | char *channel_name) | |
2018 | { | |
2019 | int ret; | |
2020 | struct ltt_kernel_channel *kchan; | |
2021 | ||
2022 | switch (domain) { | |
2023 | case LTTNG_DOMAIN_KERNEL: | |
2024 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2025 | session->kernel_session); | |
2026 | if (kchan == NULL) { | |
2027 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2028 | goto error; | |
26cc6b4e DG |
2029 | } |
2030 | ||
54d01ffb DG |
2031 | ret = event_kernel_disable_all(session->kernel_session, kchan); |
2032 | if (ret != LTTCOMM_OK) { | |
0b97ec54 | 2033 | goto error; |
26cc6b4e DG |
2034 | } |
2035 | ||
54d01ffb | 2036 | kernel_wait_quiescent(kernel_tracer_fd); |
26cc6b4e | 2037 | break; |
54d01ffb DG |
2038 | default: |
2039 | /* TODO: Userspace tracing */ | |
2040 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2041 | goto error; | |
26cc6b4e | 2042 | } |
e953ef25 | 2043 | |
54d01ffb | 2044 | ret = LTTCOMM_OK; |
e953ef25 | 2045 | |
54d01ffb DG |
2046 | error: |
2047 | return ret; | |
2048 | } | |
f5177a38 | 2049 | |
54d01ffb DG |
2050 | /* |
2051 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
2052 | */ | |
2053 | static int cmd_add_context(struct ltt_session *session, int domain, | |
2054 | char *channel_name, char *event_name, struct lttng_event_context *ctx) | |
2055 | { | |
2056 | int ret; | |
f5177a38 | 2057 | |
54d01ffb DG |
2058 | switch (domain) { |
2059 | case LTTNG_DOMAIN_KERNEL: | |
2060 | /* Add kernel context to kernel tracer */ | |
2061 | ret = context_kernel_add(session->kernel_session, ctx, | |
2062 | event_name, channel_name); | |
2063 | if (ret != LTTCOMM_OK) { | |
f5177a38 | 2064 | goto error; |
e953ef25 DG |
2065 | } |
2066 | ||
e953ef25 | 2067 | break; |
54d01ffb DG |
2068 | default: |
2069 | /* TODO: Userspace tracing */ | |
2070 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2071 | goto error; | |
e953ef25 | 2072 | } |
950131af | 2073 | |
54d01ffb | 2074 | ret = LTTCOMM_OK; |
950131af | 2075 | |
54d01ffb DG |
2076 | error: |
2077 | return ret; | |
2078 | } | |
2079 | ||
2080 | /* | |
2081 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
2082 | */ | |
2083 | static int cmd_enable_event(struct ltt_session *session, int domain, | |
2084 | char *channel_name, struct lttng_event *event) | |
2085 | { | |
2086 | int ret; | |
2087 | struct ltt_kernel_channel *kchan; | |
f6cd6b0f | 2088 | struct lttng_channel *attr; |
54d01ffb DG |
2089 | |
2090 | switch (domain) { | |
2091 | case LTTNG_DOMAIN_KERNEL: | |
2092 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2093 | session->kernel_session); | |
2094 | if (kchan == NULL) { | |
f6cd6b0f DG |
2095 | attr = channel_new_default_attr(domain); |
2096 | if (attr == NULL) { | |
2097 | ret = LTTCOMM_FATAL; | |
2098 | goto error; | |
2099 | } | |
2100 | snprintf(attr->name, NAME_MAX, "%s", channel_name); | |
2101 | ||
54d01ffb | 2102 | /* This call will notify the kernel thread */ |
44d3bd01 | 2103 | ret = channel_kernel_create(session->kernel_session, |
f6cd6b0f | 2104 | attr, kernel_poll_pipe[1]); |
54d01ffb | 2105 | if (ret != LTTCOMM_OK) { |
f5177a38 DG |
2106 | goto error; |
2107 | } | |
54d01ffb | 2108 | } |
950131af | 2109 | |
54d01ffb DG |
2110 | /* Get the newly created kernel channel pointer */ |
2111 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2112 | session->kernel_session); | |
2113 | if (kchan == NULL) { | |
2114 | /* This sould not happen... */ | |
2115 | ret = LTTCOMM_FATAL; | |
2116 | goto error; | |
2117 | } | |
f5177a38 | 2118 | |
7a3d1328 | 2119 | ret = event_kernel_enable_tracepoint(session->kernel_session, kchan, event); |
54d01ffb | 2120 | if (ret != LTTCOMM_OK) { |
f5177a38 | 2121 | goto error; |
950131af DG |
2122 | } |
2123 | ||
54d01ffb | 2124 | kernel_wait_quiescent(kernel_tracer_fd); |
950131af | 2125 | break; |
54d01ffb DG |
2126 | default: |
2127 | /* TODO: Userspace tracing */ | |
2128 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2129 | goto error; | |
950131af | 2130 | } |
d36b8583 | 2131 | |
54d01ffb | 2132 | ret = LTTCOMM_OK; |
d36b8583 | 2133 | |
54d01ffb DG |
2134 | error: |
2135 | return ret; | |
2136 | } | |
7d29a247 | 2137 | |
54d01ffb DG |
2138 | /* |
2139 | * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread. | |
2140 | */ | |
2141 | static int cmd_enable_event_all(struct ltt_session *session, int domain, | |
8c9ae521 | 2142 | char *channel_name, int event_type) |
54d01ffb DG |
2143 | { |
2144 | int ret; | |
2145 | struct ltt_kernel_channel *kchan; | |
7d29a247 | 2146 | |
54d01ffb DG |
2147 | switch (domain) { |
2148 | case LTTNG_DOMAIN_KERNEL: | |
2149 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2150 | session->kernel_session); | |
2151 | if (kchan == NULL) { | |
2152 | /* This call will notify the kernel thread */ | |
44d3bd01 DG |
2153 | ret = channel_kernel_create(session->kernel_session, NULL, |
2154 | kernel_poll_pipe[1]); | |
54d01ffb DG |
2155 | if (ret != LTTCOMM_OK) { |
2156 | goto error; | |
d36b8583 | 2157 | } |
54d01ffb | 2158 | } |
0d0c377a | 2159 | |
54d01ffb DG |
2160 | /* Get the newly created kernel channel pointer */ |
2161 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2162 | session->kernel_session); | |
2163 | if (kchan == NULL) { | |
2164 | /* This sould not happen... */ | |
2165 | ret = LTTCOMM_FATAL; | |
2166 | goto error; | |
2167 | } | |
0fdd1e2c | 2168 | |
7a3d1328 MD |
2169 | switch (event_type) { |
2170 | case LTTNG_KERNEL_SYSCALL: | |
2171 | ret = event_kernel_enable_all_syscalls(session->kernel_session, | |
8c9ae521 | 2172 | kchan, kernel_tracer_fd); |
7a3d1328 MD |
2173 | break; |
2174 | case LTTNG_KERNEL_TRACEPOINT: | |
8c9ae521 | 2175 | /* |
7a3d1328 MD |
2176 | * This call enables all LTTNG_KERNEL_TRACEPOINTS and |
2177 | * events already registered to the channel. | |
8c9ae521 | 2178 | */ |
7a3d1328 MD |
2179 | ret = event_kernel_enable_all_tracepoints(session->kernel_session, |
2180 | kchan, kernel_tracer_fd); | |
2181 | break; | |
2182 | case LTTNG_KERNEL_ALL: | |
2183 | /* Enable syscalls and tracepoints */ | |
8c9ae521 DG |
2184 | ret = event_kernel_enable_all(session->kernel_session, |
2185 | kchan, kernel_tracer_fd); | |
7a3d1328 MD |
2186 | break; |
2187 | default: | |
2188 | ret = LTTCOMM_KERN_ENABLE_FAIL; | |
2189 | goto error; | |
8c9ae521 | 2190 | } |
54d01ffb | 2191 | if (ret != LTTCOMM_OK) { |
0d0c377a | 2192 | goto error; |
d36b8583 DG |
2193 | } |
2194 | ||
54d01ffb | 2195 | kernel_wait_quiescent(kernel_tracer_fd); |
d36b8583 | 2196 | break; |
54d01ffb DG |
2197 | default: |
2198 | /* TODO: Userspace tracing */ | |
2199 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2200 | goto error; | |
d36b8583 | 2201 | } |
f3ed775e | 2202 | |
54d01ffb DG |
2203 | ret = LTTCOMM_OK; |
2204 | ||
2205 | error: | |
2206 | return ret; | |
2207 | } | |
2208 | ||
2209 | /* | |
2210 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
2211 | */ | |
2212 | static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events) | |
2213 | { | |
2214 | int ret; | |
2215 | ssize_t nb_events = 0; | |
2216 | ||
2217 | switch (domain) { | |
2218 | case LTTNG_DOMAIN_KERNEL: | |
2219 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
2220 | if (nb_events < 0) { | |
2221 | ret = LTTCOMM_KERN_LIST_FAIL; | |
2222 | goto error; | |
894be886 | 2223 | } |
54d01ffb DG |
2224 | break; |
2225 | default: | |
2226 | /* TODO: Userspace listing */ | |
2227 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2228 | goto error; | |
2229 | } | |
894be886 | 2230 | |
54d01ffb | 2231 | return nb_events; |
7d29a247 | 2232 | |
54d01ffb DG |
2233 | error: |
2234 | /* Return negative value to differentiate return code */ | |
2235 | return -ret; | |
2236 | } | |
b389abbe | 2237 | |
54d01ffb DG |
2238 | /* |
2239 | * Command LTTNG_START_TRACE processed by the client thread. | |
2240 | */ | |
2241 | static int cmd_start_trace(struct ltt_session *session) | |
2242 | { | |
2243 | int ret; | |
2244 | struct ltt_kernel_channel *kchan; | |
2245 | struct ltt_kernel_session *ksession; | |
b389abbe | 2246 | |
54d01ffb DG |
2247 | /* Short cut */ |
2248 | ksession = session->kernel_session; | |
5eb91c98 | 2249 | |
54d01ffb DG |
2250 | /* Kernel tracing */ |
2251 | if (ksession != NULL) { | |
2252 | /* Open kernel metadata */ | |
2253 | if (ksession->metadata == NULL) { | |
2254 | ret = kernel_open_metadata(ksession, ksession->trace_path); | |
2255 | if (ret < 0) { | |
2256 | ret = LTTCOMM_KERN_META_FAIL; | |
2257 | goto error; | |
b389abbe | 2258 | } |
54d01ffb | 2259 | } |
7d29a247 | 2260 | |
54d01ffb DG |
2261 | /* Open kernel metadata stream */ |
2262 | if (ksession->metadata_stream_fd == 0) { | |
2263 | ret = kernel_open_metadata_stream(ksession); | |
2264 | if (ret < 0) { | |
2265 | ERR("Kernel create metadata stream failed"); | |
2266 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
2267 | goto error; | |
2268 | } | |
2269 | } | |
2270 | ||
2271 | /* For each channel */ | |
2272 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
2273 | if (kchan->stream_count == 0) { | |
2274 | ret = kernel_open_channel_stream(kchan); | |
2275 | if (ret < 0) { | |
2276 | ret = LTTCOMM_KERN_STREAM_FAIL; | |
7d29a247 DG |
2277 | goto error; |
2278 | } | |
54d01ffb DG |
2279 | /* Update the stream global counter */ |
2280 | ksession->stream_count_global += ret; | |
7d29a247 | 2281 | } |
54d01ffb DG |
2282 | } |
2283 | ||
2284 | /* Setup kernel consumer socket and send fds to it */ | |
2285 | ret = init_kernel_tracing(ksession); | |
2286 | if (ret < 0) { | |
2287 | ret = LTTCOMM_KERN_START_FAIL; | |
2288 | goto error; | |
2289 | } | |
2290 | ||
2291 | /* This start the kernel tracing */ | |
2292 | ret = kernel_start_session(ksession); | |
2293 | if (ret < 0) { | |
2294 | ret = LTTCOMM_KERN_START_FAIL; | |
2295 | goto error; | |
2296 | } | |
2297 | ||
2298 | /* Quiescent wait after starting trace */ | |
2299 | kernel_wait_quiescent(kernel_tracer_fd); | |
2300 | } | |
2301 | ||
2302 | /* TODO: Start all UST traces */ | |
2303 | ||
2304 | ret = LTTCOMM_OK; | |
2305 | ||
2306 | error: | |
2307 | return ret; | |
2308 | } | |
2309 | ||
2310 | /* | |
2311 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2312 | */ | |
2313 | static int cmd_stop_trace(struct ltt_session *session) | |
2314 | { | |
2315 | int ret; | |
2316 | struct ltt_kernel_channel *kchan; | |
2317 | struct ltt_kernel_session *ksession; | |
2318 | ||
2319 | /* Short cut */ | |
2320 | ksession = session->kernel_session; | |
2321 | ||
2322 | /* Kernel tracer */ | |
2323 | if (ksession != NULL) { | |
2324 | DBG("Stop kernel tracing"); | |
2325 | ||
2326 | /* Flush all buffers before stopping */ | |
2327 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
2328 | if (ret < 0) { | |
2329 | ERR("Kernel metadata flush failed"); | |
2330 | } | |
f34daff7 | 2331 | |
54d01ffb DG |
2332 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
2333 | ret = kernel_flush_buffer(kchan); | |
0d0c377a | 2334 | if (ret < 0) { |
54d01ffb | 2335 | ERR("Kernel flush buffer error"); |
7d29a247 | 2336 | } |
54d01ffb | 2337 | } |
19e70852 | 2338 | |
54d01ffb DG |
2339 | ret = kernel_stop_session(ksession); |
2340 | if (ret < 0) { | |
2341 | ret = LTTCOMM_KERN_STOP_FAIL; | |
19e70852 | 2342 | goto error; |
f3ed775e | 2343 | } |
54d01ffb DG |
2344 | |
2345 | kernel_wait_quiescent(kernel_tracer_fd); | |
894be886 | 2346 | } |
54d01ffb DG |
2347 | |
2348 | /* TODO : User-space tracer */ | |
2349 | ||
2350 | ret = LTTCOMM_OK; | |
2351 | ||
2352 | error: | |
2353 | return ret; | |
2354 | } | |
2355 | ||
2356 | /* | |
2357 | * Command LTTNG_CREATE_SESSION processed by the client thread. | |
2358 | */ | |
2359 | static int cmd_create_session(char *name, char *path) | |
2360 | { | |
2361 | int ret; | |
2362 | ||
2363 | ret = session_create(name, path); | |
2364 | if (ret != LTTCOMM_OK) { | |
2365 | goto error; | |
2366 | } | |
2367 | ||
2368 | ret = LTTCOMM_OK; | |
2369 | ||
2370 | error: | |
2371 | return ret; | |
2372 | } | |
2373 | ||
2374 | /* | |
2375 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
2376 | */ | |
2377 | static int cmd_destroy_session(struct ltt_session *session, char *name) | |
2378 | { | |
2379 | int ret; | |
2380 | ||
2381 | /* Clean kernel session teardown */ | |
2382 | teardown_kernel_session(session); | |
2383 | ||
2384 | /* | |
2385 | * Must notify the kernel thread here to update it's poll setin order | |
2386 | * to remove the channel(s)' fd just destroyed. | |
2387 | */ | |
2388 | ret = notify_thread_pipe(kernel_poll_pipe[1]); | |
2389 | if (ret < 0) { | |
2390 | perror("write kernel poll pipe"); | |
2391 | } | |
2392 | ||
271933a4 | 2393 | ret = session_destroy(session); |
54d01ffb DG |
2394 | |
2395 | return ret; | |
2396 | } | |
2397 | ||
2398 | /* | |
2399 | * Command LTTNG_CALIBRATE processed by the client thread. | |
2400 | */ | |
2401 | static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate) | |
2402 | { | |
2403 | int ret; | |
2404 | ||
2405 | switch (domain) { | |
2406 | case LTTNG_DOMAIN_KERNEL: | |
33a2b854 | 2407 | { |
54d01ffb | 2408 | struct lttng_kernel_calibrate kcalibrate; |
33a2b854 | 2409 | |
54d01ffb DG |
2410 | kcalibrate.type = calibrate->type; |
2411 | ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate); | |
33a2b854 | 2412 | if (ret < 0) { |
54d01ffb DG |
2413 | ret = LTTCOMM_KERN_ENABLE_FAIL; |
2414 | goto error; | |
33a2b854 | 2415 | } |
54d01ffb DG |
2416 | break; |
2417 | } | |
2418 | default: | |
2419 | /* TODO: Userspace tracing */ | |
2420 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2421 | goto error; | |
2422 | } | |
33a2b854 | 2423 | |
54d01ffb | 2424 | ret = LTTCOMM_OK; |
33a2b854 | 2425 | |
54d01ffb DG |
2426 | error: |
2427 | return ret; | |
2428 | } | |
7d29a247 | 2429 | |
54d01ffb DG |
2430 | /* |
2431 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
2432 | */ | |
2433 | static int cmd_register_consumer(struct ltt_session *session, int domain, | |
2434 | char *sock_path) | |
2435 | { | |
2436 | int ret, sock; | |
b389abbe | 2437 | |
54d01ffb DG |
2438 | switch (domain) { |
2439 | case LTTNG_DOMAIN_KERNEL: | |
2440 | /* Can't register a consumer if there is already one */ | |
2441 | if (session->kernel_session->consumer_fd != 0) { | |
2442 | ret = LTTCOMM_KERN_CONSUMER_FAIL; | |
2443 | goto error; | |
2444 | } | |
2445 | ||
2446 | sock = lttcomm_connect_unix_sock(sock_path); | |
2447 | if (sock < 0) { | |
2448 | ret = LTTCOMM_CONNECT_FAIL; | |
2449 | goto error; | |
2450 | } | |
2451 | ||
2452 | session->kernel_session->consumer_fd = sock; | |
2453 | break; | |
2454 | default: | |
2455 | /* TODO: Userspace tracing */ | |
2456 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2457 | goto error; | |
2458 | } | |
2459 | ||
2460 | ret = LTTCOMM_OK; | |
2461 | ||
2462 | error: | |
2463 | return ret; | |
2464 | } | |
2465 | ||
2466 | /* | |
2467 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
2468 | */ | |
2469 | static ssize_t cmd_list_domains(struct ltt_session *session, | |
2470 | struct lttng_domain **domains) | |
2471 | { | |
2472 | int ret; | |
2473 | ssize_t nb_dom = 0; | |
2474 | ||
2475 | if (session->kernel_session != NULL) { | |
2476 | nb_dom++; | |
2477 | } | |
2478 | ||
2479 | nb_dom += session->ust_session_list.count; | |
2480 | ||
2481 | *domains = malloc(nb_dom * sizeof(struct lttng_domain)); | |
2482 | if (*domains == NULL) { | |
2483 | ret = -LTTCOMM_FATAL; | |
2484 | goto error; | |
2485 | } | |
2486 | ||
2487 | (*domains)[0].type = LTTNG_DOMAIN_KERNEL; | |
2488 | ||
2489 | /* TODO: User-space tracer domain support */ | |
2490 | ||
2491 | return nb_dom; | |
2492 | ||
2493 | error: | |
2494 | return ret; | |
2495 | } | |
2496 | ||
2497 | /* | |
2498 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
2499 | */ | |
2500 | static ssize_t cmd_list_channels(struct ltt_session *session, | |
2501 | struct lttng_channel **channels) | |
2502 | { | |
2503 | int ret; | |
2504 | ssize_t nb_chan = 0; | |
2505 | ||
2506 | if (session->kernel_session != NULL) { | |
2507 | nb_chan += session->kernel_session->channel_count; | |
2508 | } | |
2509 | ||
2510 | *channels = malloc(nb_chan * sizeof(struct lttng_channel)); | |
2511 | if (*channels == NULL) { | |
2512 | ret = -LTTCOMM_FATAL; | |
2513 | goto error; | |
2514 | } | |
2515 | ||
2516 | list_lttng_channels(session, *channels); | |
2517 | ||
2518 | return nb_chan; | |
2519 | ||
2520 | error: | |
2521 | return ret; | |
2522 | } | |
2523 | ||
2524 | /* | |
2525 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
2526 | */ | |
2527 | static ssize_t cmd_list_events(struct ltt_session *session, | |
2528 | char *channel_name, struct lttng_event **events) | |
2529 | { | |
2530 | int ret; | |
2531 | ssize_t nb_event = 0; | |
2532 | struct ltt_kernel_channel *kchan = NULL; | |
2533 | ||
2534 | if (session->kernel_session != NULL) { | |
2535 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2536 | session->kernel_session); | |
2537 | if (kchan == NULL) { | |
2538 | ret = -LTTCOMM_KERN_CHAN_NOT_FOUND; | |
2539 | goto error; | |
2540 | } | |
2541 | nb_event += kchan->event_count; | |
2542 | } | |
2543 | ||
2544 | *events = malloc(nb_event * sizeof(struct lttng_event)); | |
2545 | if (*events == NULL) { | |
2546 | ret = -LTTCOMM_FATAL; | |
2547 | goto error; | |
2548 | } | |
2549 | ||
2550 | list_lttng_events(kchan, *events); | |
2551 | ||
2552 | /* TODO: User-space tracer support */ | |
2553 | ||
2554 | return nb_event; | |
2555 | ||
2556 | error: | |
2557 | return ret; | |
2558 | } | |
2559 | ||
2560 | /* | |
2561 | * Process the command requested by the lttng client within the command | |
2562 | * context structure. This function make sure that the return structure (llm) | |
2563 | * is set and ready for transmission before returning. | |
2564 | * | |
2565 | * Return any error encountered or 0 for success. | |
2566 | */ | |
2567 | static int process_client_msg(struct command_ctx *cmd_ctx) | |
2568 | { | |
2569 | int ret = LTTCOMM_OK; | |
44d3bd01 | 2570 | int need_tracing_session = 1; |
54d01ffb DG |
2571 | |
2572 | DBG("Processing client command %d", cmd_ctx->lsm->cmd_type); | |
2573 | ||
2574 | /* | |
2575 | * Check for command that don't needs to allocate a returned payload. We do | |
44d3bd01 | 2576 | * this here so we don't have to make the call for no payload at each |
54d01ffb DG |
2577 | * command. |
2578 | */ | |
2579 | switch(cmd_ctx->lsm->cmd_type) { | |
2580 | case LTTNG_LIST_SESSIONS: | |
2581 | case LTTNG_LIST_TRACEPOINTS: | |
2582 | case LTTNG_LIST_DOMAINS: | |
2583 | case LTTNG_LIST_CHANNELS: | |
2584 | case LTTNG_LIST_EVENTS: | |
2585 | break; | |
2586 | default: | |
2587 | /* Setup lttng message with no payload */ | |
2588 | ret = setup_lttng_msg(cmd_ctx, 0); | |
2589 | if (ret < 0) { | |
2590 | /* This label does not try to unlock the session */ | |
2591 | goto init_setup_error; | |
2592 | } | |
2593 | } | |
2594 | ||
2595 | /* Commands that DO NOT need a session. */ | |
2596 | switch (cmd_ctx->lsm->cmd_type) { | |
2597 | case LTTNG_CALIBRATE: | |
2598 | case LTTNG_CREATE_SESSION: | |
2599 | case LTTNG_LIST_SESSIONS: | |
2600 | case LTTNG_LIST_TRACEPOINTS: | |
44d3bd01 | 2601 | need_tracing_session = 0; |
54d01ffb DG |
2602 | break; |
2603 | default: | |
2604 | DBG("Getting session %s by name", cmd_ctx->lsm->session.name); | |
74babd95 | 2605 | session_lock_list(); |
54d01ffb | 2606 | cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name); |
74babd95 | 2607 | session_unlock_list(); |
54d01ffb DG |
2608 | if (cmd_ctx->session == NULL) { |
2609 | if (cmd_ctx->lsm->session.name != NULL) { | |
2610 | ret = LTTCOMM_SESS_NOT_FOUND; | |
2611 | } else { | |
2612 | /* If no session name specified */ | |
2613 | ret = LTTCOMM_SELECT_SESS; | |
2614 | } | |
2615 | goto error; | |
2616 | } else { | |
2617 | /* Acquire lock for the session */ | |
2618 | session_lock(cmd_ctx->session); | |
2619 | } | |
2620 | break; | |
2621 | } | |
b389abbe | 2622 | |
54d01ffb DG |
2623 | /* |
2624 | * Check domain type for specific "pre-action". | |
2625 | */ | |
2626 | switch (cmd_ctx->lsm->domain.type) { | |
2627 | case LTTNG_DOMAIN_KERNEL: | |
2628 | /* Kernel tracer check */ | |
2629 | if (kernel_tracer_fd == 0) { | |
2630 | /* Basically, load kernel tracer modules */ | |
2631 | init_kernel_tracer(); | |
2632 | if (kernel_tracer_fd == 0) { | |
2633 | ret = LTTCOMM_KERN_NA; | |
2634 | goto error; | |
2635 | } | |
2636 | } | |
5eb91c98 | 2637 | |
54d01ffb | 2638 | /* Need a session for kernel command */ |
44d3bd01 | 2639 | if (need_tracing_session) { |
54d01ffb DG |
2640 | if (cmd_ctx->session->kernel_session == NULL) { |
2641 | ret = create_kernel_session(cmd_ctx->session); | |
5eb91c98 | 2642 | if (ret < 0) { |
54d01ffb | 2643 | ret = LTTCOMM_KERN_SESS_FAIL; |
5eb91c98 DG |
2644 | goto error; |
2645 | } | |
b389abbe | 2646 | } |
7d29a247 | 2647 | |
54d01ffb | 2648 | /* Start the kernel consumer daemon */ |
3bd1e081 MD |
2649 | pthread_mutex_lock(&kconsumer_data.pid_mutex); |
2650 | if (kconsumer_data.pid == 0 && | |
54d01ffb | 2651 | cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) { |
3bd1e081 MD |
2652 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
2653 | ret = start_consumerd(&kconsumer_data); | |
7d29a247 | 2654 | if (ret < 0) { |
54d01ffb DG |
2655 | ret = LTTCOMM_KERN_CONSUMER_FAIL; |
2656 | goto error; | |
950131af | 2657 | } |
33a2b854 | 2658 | } |
3bd1e081 | 2659 | pthread_mutex_unlock(&kconsumer_data.pid_mutex); |
0d0c377a | 2660 | } |
54d01ffb | 2661 | break; |
44d3bd01 DG |
2662 | case LTTNG_DOMAIN_UST_PID: |
2663 | { | |
2664 | struct ltt_ust_session *usess; | |
2665 | ||
2666 | if (need_tracing_session) { | |
2667 | usess = trace_ust_get_session_by_pid( | |
2668 | &cmd_ctx->session->ust_session_list, | |
2669 | cmd_ctx->lsm->domain.attr.pid); | |
2670 | if (usess == NULL) { | |
2671 | ret = create_ust_session(cmd_ctx->session, | |
2672 | &cmd_ctx->lsm->domain); | |
2673 | if (ret != LTTCOMM_OK) { | |
2674 | goto error; | |
2675 | } | |
2676 | } | |
2677 | } | |
2678 | break; | |
2679 | } | |
54d01ffb DG |
2680 | default: |
2681 | /* TODO Userspace tracer */ | |
2682 | break; | |
2683 | } | |
33a2b854 | 2684 | |
54d01ffb DG |
2685 | /* Process by command type */ |
2686 | switch (cmd_ctx->lsm->cmd_type) { | |
2687 | case LTTNG_ADD_CONTEXT: | |
2688 | { | |
2689 | ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2690 | cmd_ctx->lsm->u.context.channel_name, | |
2691 | cmd_ctx->lsm->u.context.event_name, | |
2692 | &cmd_ctx->lsm->u.context.ctx); | |
2693 | break; | |
2694 | } | |
2695 | case LTTNG_DISABLE_CHANNEL: | |
2696 | { | |
2697 | ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2698 | cmd_ctx->lsm->u.disable.channel_name); | |
2699 | break; | |
2700 | } | |
2701 | case LTTNG_DISABLE_EVENT: | |
2702 | { | |
2703 | ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2704 | cmd_ctx->lsm->u.disable.channel_name, | |
2705 | cmd_ctx->lsm->u.disable.name); | |
33a2b854 DG |
2706 | ret = LTTCOMM_OK; |
2707 | break; | |
2708 | } | |
54d01ffb DG |
2709 | case LTTNG_DISABLE_ALL_EVENT: |
2710 | { | |
2711 | DBG("Disabling all kernel event"); | |
2712 | ||
2713 | ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2714 | cmd_ctx->lsm->u.disable.channel_name); | |
2715 | break; | |
2716 | } | |
2717 | case LTTNG_ENABLE_CHANNEL: | |
2718 | { | |
44d3bd01 | 2719 | ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain, |
54d01ffb DG |
2720 | &cmd_ctx->lsm->u.channel.chan); |
2721 | break; | |
2722 | } | |
2723 | case LTTNG_ENABLE_EVENT: | |
2724 | { | |
2725 | ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
2726 | cmd_ctx->lsm->u.enable.channel_name, | |
2727 | &cmd_ctx->lsm->u.enable.event); | |
2728 | break; | |
2729 | } | |
2730 | case LTTNG_ENABLE_ALL_EVENT: | |
2731 | { | |
2732 | DBG("Enabling all kernel event"); | |
2733 | ||
2734 | ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type, | |
8c9ae521 DG |
2735 | cmd_ctx->lsm->u.enable.channel_name, |
2736 | cmd_ctx->lsm->u.enable.event.type); | |
54d01ffb DG |
2737 | break; |
2738 | } | |
052da939 | 2739 | case LTTNG_LIST_TRACEPOINTS: |
2ef84c95 | 2740 | { |
9f19cc17 | 2741 | struct lttng_event *events; |
54d01ffb | 2742 | ssize_t nb_events; |
052da939 | 2743 | |
54d01ffb DG |
2744 | nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events); |
2745 | if (nb_events < 0) { | |
2746 | ret = -nb_events; | |
2747 | goto error; | |
2ef84c95 DG |
2748 | } |
2749 | ||
2750 | /* | |
2751 | * Setup lttng message with payload size set to the event list size in | |
2752 | * bytes and then copy list into the llm payload. | |
2753 | */ | |
052da939 | 2754 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 2755 | if (ret < 0) { |
052da939 | 2756 | free(events); |
2ef84c95 DG |
2757 | goto setup_error; |
2758 | } | |
2759 | ||
2760 | /* Copy event list into message payload */ | |
9f19cc17 | 2761 | memcpy(cmd_ctx->llm->payload, events, |
052da939 | 2762 | sizeof(struct lttng_event) * nb_events); |
2ef84c95 | 2763 | |
9f19cc17 | 2764 | free(events); |
2ef84c95 DG |
2765 | |
2766 | ret = LTTCOMM_OK; | |
2767 | break; | |
2768 | } | |
f3ed775e | 2769 | case LTTNG_START_TRACE: |
8c0faa1d | 2770 | { |
54d01ffb | 2771 | ret = cmd_start_trace(cmd_ctx->session); |
8c0faa1d DG |
2772 | break; |
2773 | } | |
f3ed775e | 2774 | case LTTNG_STOP_TRACE: |
8c0faa1d | 2775 | { |
54d01ffb | 2776 | ret = cmd_stop_trace(cmd_ctx->session); |
8c0faa1d DG |
2777 | break; |
2778 | } | |
5e16da05 MD |
2779 | case LTTNG_CREATE_SESSION: |
2780 | { | |
54d01ffb DG |
2781 | ret = cmd_create_session(cmd_ctx->lsm->session.name, |
2782 | cmd_ctx->lsm->session.path); | |
5e16da05 MD |
2783 | break; |
2784 | } | |
2785 | case LTTNG_DESTROY_SESSION: | |
2786 | { | |
54d01ffb DG |
2787 | ret = cmd_destroy_session(cmd_ctx->session, |
2788 | cmd_ctx->lsm->session.name); | |
5461b305 | 2789 | break; |
5e16da05 | 2790 | } |
9f19cc17 | 2791 | case LTTNG_LIST_DOMAINS: |
5e16da05 | 2792 | { |
54d01ffb DG |
2793 | ssize_t nb_dom; |
2794 | struct lttng_domain *domains; | |
5461b305 | 2795 | |
54d01ffb DG |
2796 | nb_dom = cmd_list_domains(cmd_ctx->session, &domains); |
2797 | if (nb_dom < 0) { | |
2798 | ret = -nb_dom; | |
2799 | goto error; | |
ce3d728c | 2800 | } |
520ff687 | 2801 | |
54d01ffb | 2802 | ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain)); |
5461b305 DG |
2803 | if (ret < 0) { |
2804 | goto setup_error; | |
520ff687 | 2805 | } |
57167058 | 2806 | |
54d01ffb DG |
2807 | /* Copy event list into message payload */ |
2808 | memcpy(cmd_ctx->llm->payload, domains, | |
2809 | nb_dom * sizeof(struct lttng_domain)); | |
2810 | ||
2811 | free(domains); | |
5e16da05 | 2812 | |
5461b305 | 2813 | ret = LTTCOMM_OK; |
5e16da05 MD |
2814 | break; |
2815 | } | |
9f19cc17 | 2816 | case LTTNG_LIST_CHANNELS: |
5e16da05 | 2817 | { |
54d01ffb DG |
2818 | size_t nb_chan; |
2819 | struct lttng_channel *channels; | |
2820 | ||
2821 | nb_chan = cmd_list_channels(cmd_ctx->session, &channels); | |
2822 | if (nb_chan < 0) { | |
2823 | ret = -nb_chan; | |
2824 | goto error; | |
5461b305 | 2825 | } |
ca95a216 | 2826 | |
54d01ffb | 2827 | ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel)); |
5461b305 DG |
2828 | if (ret < 0) { |
2829 | goto setup_error; | |
2830 | } | |
9f19cc17 | 2831 | |
54d01ffb DG |
2832 | /* Copy event list into message payload */ |
2833 | memcpy(cmd_ctx->llm->payload, channels, | |
2834 | nb_chan * sizeof(struct lttng_channel)); | |
2835 | ||
2836 | free(channels); | |
9f19cc17 DG |
2837 | |
2838 | ret = LTTCOMM_OK; | |
5461b305 | 2839 | break; |
5e16da05 | 2840 | } |
9f19cc17 | 2841 | case LTTNG_LIST_EVENTS: |
5e16da05 | 2842 | { |
54d01ffb | 2843 | size_t nb_event; |
684d34d2 | 2844 | struct lttng_event *events = NULL; |
9f19cc17 | 2845 | |
54d01ffb DG |
2846 | nb_event = cmd_list_events(cmd_ctx->session, |
2847 | cmd_ctx->lsm->u.list.channel_name, &events); | |
2848 | if (nb_event < 0) { | |
2849 | ret = -nb_event; | |
2850 | goto error; | |
5461b305 | 2851 | } |
ca95a216 | 2852 | |
54d01ffb | 2853 | ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event)); |
5461b305 DG |
2854 | if (ret < 0) { |
2855 | goto setup_error; | |
2856 | } | |
9f19cc17 | 2857 | |
54d01ffb DG |
2858 | /* Copy event list into message payload */ |
2859 | memcpy(cmd_ctx->llm->payload, events, | |
2860 | nb_event * sizeof(struct lttng_event)); | |
9f19cc17 | 2861 | |
54d01ffb | 2862 | free(events); |
9f19cc17 DG |
2863 | |
2864 | ret = LTTCOMM_OK; | |
5461b305 | 2865 | break; |
5e16da05 MD |
2866 | } |
2867 | case LTTNG_LIST_SESSIONS: | |
2868 | { | |
54d01ffb | 2869 | session_lock_list(); |
5461b305 | 2870 | |
6c9cc2ab | 2871 | if (session_list_ptr->count == 0) { |
f3ed775e | 2872 | ret = LTTCOMM_NO_SESSION; |
54d01ffb | 2873 | session_unlock_list(); |
5461b305 | 2874 | goto error; |
57167058 | 2875 | } |
5e16da05 | 2876 | |
6c9cc2ab DG |
2877 | ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * |
2878 | session_list_ptr->count); | |
5461b305 | 2879 | if (ret < 0) { |
54d01ffb | 2880 | session_unlock_list(); |
5461b305 | 2881 | goto setup_error; |
e065084a | 2882 | } |
5e16da05 | 2883 | |
6c9cc2ab DG |
2884 | /* Filled the session array */ |
2885 | list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload)); | |
2886 | ||
54d01ffb | 2887 | session_unlock_list(); |
5e16da05 | 2888 | |
5461b305 | 2889 | ret = LTTCOMM_OK; |
5e16da05 MD |
2890 | break; |
2891 | } | |
d0254c7c MD |
2892 | case LTTNG_CALIBRATE: |
2893 | { | |
54d01ffb DG |
2894 | ret = cmd_calibrate(cmd_ctx->lsm->domain.type, |
2895 | &cmd_ctx->lsm->u.calibrate); | |
d0254c7c MD |
2896 | break; |
2897 | } | |
d9800920 DG |
2898 | case LTTNG_REGISTER_CONSUMER: |
2899 | { | |
54d01ffb DG |
2900 | ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type, |
2901 | cmd_ctx->lsm->u.reg.path); | |
d9800920 DG |
2902 | break; |
2903 | } | |
5e16da05 | 2904 | default: |
5e16da05 | 2905 | ret = LTTCOMM_UND; |
5461b305 | 2906 | break; |
fac6795d DG |
2907 | } |
2908 | ||
5461b305 | 2909 | error: |
5461b305 DG |
2910 | if (cmd_ctx->llm == NULL) { |
2911 | DBG("Missing llm structure. Allocating one."); | |
894be886 | 2912 | if (setup_lttng_msg(cmd_ctx, 0) < 0) { |
5461b305 DG |
2913 | goto setup_error; |
2914 | } | |
2915 | } | |
54d01ffb | 2916 | /* Set return code */ |
5461b305 | 2917 | cmd_ctx->llm->ret_code = ret; |
5461b305 | 2918 | setup_error: |
b5541356 | 2919 | if (cmd_ctx->session) { |
54d01ffb | 2920 | session_unlock(cmd_ctx->session); |
b5541356 | 2921 | } |
54d01ffb | 2922 | init_setup_error: |
8028d920 | 2923 | return ret; |
fac6795d DG |
2924 | } |
2925 | ||
1d4b027a | 2926 | /* |
d063d709 DG |
2927 | * This thread manage all clients request using the unix client socket for |
2928 | * communication. | |
1d4b027a DG |
2929 | */ |
2930 | static void *thread_manage_clients(void *data) | |
2931 | { | |
5eb91c98 DG |
2932 | int sock = 0, ret, i, pollfd; |
2933 | uint32_t revents, nb_fd; | |
273ea72c | 2934 | struct command_ctx *cmd_ctx = NULL; |
5eb91c98 | 2935 | struct lttng_poll_event events; |
1d4b027a DG |
2936 | |
2937 | DBG("[thread] Manage client started"); | |
2938 | ||
2939 | ret = lttcomm_listen_unix_sock(client_sock); | |
2940 | if (ret < 0) { | |
2941 | goto error; | |
2942 | } | |
2943 | ||
5eb91c98 DG |
2944 | /* |
2945 | * Pass 2 as size here for the thread quit pipe and client_sock. Nothing | |
2946 | * more will be added to this poll set. | |
2947 | */ | |
2948 | ret = create_thread_poll_set(&events, 2); | |
2949 | if (ret < 0) { | |
2950 | goto error; | |
2951 | } | |
273ea72c | 2952 | |
5eb91c98 DG |
2953 | /* Add the application registration socket */ |
2954 | ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI); | |
2955 | if (ret < 0) { | |
2956 | goto error; | |
2957 | } | |
273ea72c | 2958 | |
bbd973c2 DG |
2959 | /* |
2960 | * Notify parent pid that we are ready to accept command for client side. | |
1d4b027a DG |
2961 | */ |
2962 | if (opt_sig_parent) { | |
2963 | kill(ppid, SIGCHLD); | |
2964 | } | |
2965 | ||
2966 | while (1) { | |
1d4b027a | 2967 | DBG("Accepting client command ..."); |
273ea72c | 2968 | |
5eb91c98 DG |
2969 | nb_fd = LTTNG_POLL_GETNB(&events); |
2970 | ||
273ea72c | 2971 | /* Inifinite blocking call, waiting for transmission */ |
5eb91c98 | 2972 | ret = lttng_poll_wait(&events, -1); |
273ea72c | 2973 | if (ret < 0) { |
273ea72c DG |
2974 | goto error; |
2975 | } | |
2976 | ||
5eb91c98 DG |
2977 | for (i = 0; i < nb_fd; i++) { |
2978 | /* Fetch once the poll data */ | |
2979 | revents = LTTNG_POLL_GETEV(&events, i); | |
2980 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
2981 | ||
2982 | /* Thread quit pipe has been closed. Killing thread. */ | |
2983 | ret = check_thread_quit_pipe(pollfd, revents); | |
2984 | if (ret) { | |
2985 | goto error; | |
2986 | } | |
2987 | ||
2988 | /* Event on the registration socket */ | |
2989 | if (pollfd == client_sock) { | |
2990 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
2991 | ERR("Client socket poll error"); | |
2992 | goto error; | |
2993 | } | |
2994 | } | |
2995 | } | |
2996 | ||
2997 | DBG("Wait for client response"); | |
2998 | ||
2999 | sock = lttcomm_accept_unix_sock(client_sock); | |
3000 | if (sock < 0) { | |
273ea72c | 3001 | goto error; |
273ea72c DG |
3002 | } |
3003 | ||
5eb91c98 DG |
3004 | /* Allocate context command to process the client request */ |
3005 | cmd_ctx = malloc(sizeof(struct command_ctx)); | |
3006 | if (cmd_ctx == NULL) { | |
3007 | perror("malloc cmd_ctx"); | |
1d4b027a | 3008 | goto error; |
5eb91c98 | 3009 | } |
1d4b027a | 3010 | |
5eb91c98 DG |
3011 | /* Allocate data buffer for reception */ |
3012 | cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg)); | |
3013 | if (cmd_ctx->lsm == NULL) { | |
3014 | perror("malloc cmd_ctx->lsm"); | |
3015 | goto error; | |
3016 | } | |
1d4b027a | 3017 | |
5eb91c98 DG |
3018 | cmd_ctx->llm = NULL; |
3019 | cmd_ctx->session = NULL; | |
1d4b027a | 3020 | |
5eb91c98 DG |
3021 | /* |
3022 | * Data is received from the lttng client. The struct | |
3023 | * lttcomm_session_msg (lsm) contains the command and data request of | |
3024 | * the client. | |
3025 | */ | |
3026 | DBG("Receiving data from client ..."); | |
3027 | ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, | |
3028 | sizeof(struct lttcomm_session_msg)); | |
3029 | if (ret <= 0) { | |
3030 | DBG("Nothing recv() from client... continuing"); | |
3031 | close(sock); | |
3032 | free(cmd_ctx); | |
3033 | continue; | |
3034 | } | |
1d4b027a | 3035 | |
5eb91c98 DG |
3036 | // TODO: Validate cmd_ctx including sanity check for |
3037 | // security purpose. | |
f7776ea7 | 3038 | |
5eb91c98 DG |
3039 | /* |
3040 | * This function dispatch the work to the kernel or userspace tracer | |
3041 | * libs and fill the lttcomm_lttng_msg data structure of all the needed | |
3042 | * informations for the client. The command context struct contains | |
3043 | * everything this function may needs. | |
3044 | */ | |
3045 | ret = process_client_msg(cmd_ctx); | |
3046 | if (ret < 0) { | |
bbd973c2 | 3047 | /* |
5eb91c98 DG |
3048 | * TODO: Inform client somehow of the fatal error. At |
3049 | * this point, ret < 0 means that a malloc failed | |
3050 | * (ENOMEM). Error detected but still accept command. | |
bbd973c2 | 3051 | */ |
bbd973c2 | 3052 | clean_command_ctx(&cmd_ctx); |
5eb91c98 DG |
3053 | continue; |
3054 | } | |
d6e4fca4 | 3055 | |
54d01ffb DG |
3056 | DBG("Sending response (size: %d, retcode: %s)", |
3057 | cmd_ctx->lttng_msg_size, | |
9a745bc7 | 3058 | lttng_strerror(-cmd_ctx->llm->ret_code)); |
54d01ffb | 3059 | ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size); |
5eb91c98 DG |
3060 | if (ret < 0) { |
3061 | ERR("Failed to send data back to client"); | |
bbd973c2 | 3062 | } |
1d4b027a | 3063 | |
5eb91c98 DG |
3064 | clean_command_ctx(&cmd_ctx); |
3065 | ||
3066 | /* End of transmission */ | |
273ea72c DG |
3067 | close(sock); |
3068 | } | |
3069 | ||
5eb91c98 DG |
3070 | error: |
3071 | DBG("Client thread dying"); | |
273ea72c | 3072 | unlink(client_unix_sock_path); |
5eb91c98 DG |
3073 | close(client_sock); |
3074 | close(sock); | |
273ea72c | 3075 | |
5eb91c98 | 3076 | lttng_poll_clean(&events); |
a2fb29a5 | 3077 | clean_command_ctx(&cmd_ctx); |
1d4b027a DG |
3078 | return NULL; |
3079 | } | |
3080 | ||
3081 | ||
fac6795d DG |
3082 | /* |
3083 | * usage function on stderr | |
3084 | */ | |
3085 | static void usage(void) | |
3086 | { | |
b716ce68 | 3087 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); |
d6f42150 DG |
3088 | fprintf(stderr, " -h, --help Display this usage.\n"); |
3089 | fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n"); | |
3090 | fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n"); | |
3091 | fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n"); | |
3092 | fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n"); | |
3bd1e081 MD |
3093 | fprintf(stderr, " --ustconsumerd-err-sock PATH Specify path for the UST consumer error socket\n"); |
3094 | fprintf(stderr, " --ustconsumerd-cmd-sock PATH Specify path for the UST consumer command socket\n"); | |
d6f42150 DG |
3095 | fprintf(stderr, " -d, --daemonize Start as a daemon.\n"); |
3096 | fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n"); | |
3097 | fprintf(stderr, " -V, --version Show version number.\n"); | |
3098 | fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n"); | |
3099 | fprintf(stderr, " -q, --quiet No output at all.\n"); | |
3100 | fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n"); | |
3bd1e081 | 3101 | fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n"); |
fac6795d DG |
3102 | } |
3103 | ||
3104 | /* | |
3105 | * daemon argument parsing | |
3106 | */ | |
3107 | static int parse_args(int argc, char **argv) | |
3108 | { | |
3109 | int c; | |
3110 | ||
3111 | static struct option long_options[] = { | |
3112 | { "client-sock", 1, 0, 'c' }, | |
3113 | { "apps-sock", 1, 0, 'a' }, | |
3bd1e081 MD |
3114 | { "kconsumerd-cmd-sock", 1, 0, 'C' }, |
3115 | { "kconsumerd-err-sock", 1, 0, 'E' }, | |
3116 | { "ustconsumerd-cmd-sock", 1, 0, 'D' }, | |
3117 | { "ustconsumerd-err-sock", 1, 0, 'F' }, | |
fac6795d | 3118 | { "daemonize", 0, 0, 'd' }, |
5b8719f5 | 3119 | { "sig-parent", 0, 0, 'S' }, |
fac6795d DG |
3120 | { "help", 0, 0, 'h' }, |
3121 | { "group", 1, 0, 'g' }, | |
3122 | { "version", 0, 0, 'V' }, | |
75462a81 | 3123 | { "quiet", 0, 0, 'q' }, |
3f9947db | 3124 | { "verbose", 0, 0, 'v' }, |
3bd1e081 | 3125 | { "verbose-consumer", 0, 0, 'Z' }, |
fac6795d DG |
3126 | { NULL, 0, 0, 0 } |
3127 | }; | |
3128 | ||
3129 | while (1) { | |
3130 | int option_index = 0; | |
3bd1e081 | 3131 | c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:C:E:D:F:Z", |
54d01ffb | 3132 | long_options, &option_index); |
fac6795d DG |
3133 | if (c == -1) { |
3134 | break; | |
3135 | } | |
3136 | ||
3137 | switch (c) { | |
3138 | case 0: | |
3139 | fprintf(stderr, "option %s", long_options[option_index].name); | |
3140 | if (optarg) { | |
3141 | fprintf(stderr, " with arg %s\n", optarg); | |
3142 | } | |
3143 | break; | |
b716ce68 | 3144 | case 'c': |
fac6795d DG |
3145 | snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg); |
3146 | break; | |
3147 | case 'a': | |
3148 | snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg); | |
3149 | break; | |
3150 | case 'd': | |
3151 | opt_daemon = 1; | |
3152 | break; | |
3153 | case 'g': | |
3154 | opt_tracing_group = strdup(optarg); | |
3155 | break; | |
3156 | case 'h': | |
3157 | usage(); | |
3158 | exit(EXIT_FAILURE); | |
3159 | case 'V': | |
3160 | fprintf(stdout, "%s\n", VERSION); | |
3161 | exit(EXIT_SUCCESS); | |
5b8719f5 DG |
3162 | case 'S': |
3163 | opt_sig_parent = 1; | |
3164 | break; | |
d6f42150 | 3165 | case 'E': |
3bd1e081 | 3166 | snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); |
d6f42150 DG |
3167 | break; |
3168 | case 'C': | |
3bd1e081 MD |
3169 | snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); |
3170 | break; | |
3171 | case 'F': | |
3172 | snprintf(ustconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg); | |
3173 | break; | |
3174 | case 'D': | |
3175 | snprintf(ustconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg); | |
d6f42150 | 3176 | break; |
75462a81 DG |
3177 | case 'q': |
3178 | opt_quiet = 1; | |
3179 | break; | |
3f9947db | 3180 | case 'v': |
53086306 DG |
3181 | /* Verbose level can increase using multiple -v */ |
3182 | opt_verbose += 1; | |
3f9947db | 3183 | break; |
31f73cc9 | 3184 | case 'Z': |
3bd1e081 | 3185 | opt_verbose_consumer += 1; |
31f73cc9 | 3186 | break; |
fac6795d DG |
3187 | default: |
3188 | /* Unknown option or other error. | |
3189 | * Error is printed by getopt, just return */ | |
3190 | return -1; | |
3191 | } | |
3192 | } | |
3193 | ||
3194 | return 0; | |
3195 | } | |
3196 | ||
3197 | /* | |
d063d709 | 3198 | * Creates the two needed socket by the daemon. |
d6f42150 DG |
3199 | * apps_sock - The communication socket for all UST apps. |
3200 | * client_sock - The communication of the cli tool (lttng). | |
fac6795d | 3201 | */ |
cf3af59e | 3202 | static int init_daemon_socket(void) |
fac6795d DG |
3203 | { |
3204 | int ret = 0; | |
3205 | mode_t old_umask; | |
3206 | ||
3207 | old_umask = umask(0); | |
3208 | ||
3209 | /* Create client tool unix socket */ | |
d6f42150 DG |
3210 | client_sock = lttcomm_create_unix_sock(client_unix_sock_path); |
3211 | if (client_sock < 0) { | |
3212 | ERR("Create unix sock failed: %s", client_unix_sock_path); | |
fac6795d DG |
3213 | ret = -1; |
3214 | goto end; | |
3215 | } | |
3216 | ||
3217 | /* File permission MUST be 660 */ | |
3218 | ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
3219 | if (ret < 0) { | |
d6f42150 | 3220 | ERR("Set file permissions failed: %s", client_unix_sock_path); |
fac6795d DG |
3221 | perror("chmod"); |
3222 | goto end; | |
3223 | } | |
3224 | ||
3225 | /* Create the application unix socket */ | |
d6f42150 DG |
3226 | apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path); |
3227 | if (apps_sock < 0) { | |
3228 | ERR("Create unix sock failed: %s", apps_unix_sock_path); | |
fac6795d DG |
3229 | ret = -1; |
3230 | goto end; | |
3231 | } | |
3232 | ||
d6f42150 | 3233 | /* File permission MUST be 666 */ |
54d01ffb DG |
3234 | ret = chmod(apps_unix_sock_path, |
3235 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
fac6795d | 3236 | if (ret < 0) { |
d6f42150 | 3237 | ERR("Set file permissions failed: %s", apps_unix_sock_path); |
fac6795d DG |
3238 | perror("chmod"); |
3239 | goto end; | |
3240 | } | |
3241 | ||
3242 | end: | |
3243 | umask(old_umask); | |
3244 | return ret; | |
3245 | } | |
3246 | ||
3247 | /* | |
54d01ffb DG |
3248 | * Check if the global socket is available, and if a daemon is answering at the |
3249 | * other side. If yes, error is returned. | |
fac6795d | 3250 | */ |
cf3af59e | 3251 | static int check_existing_daemon(void) |
fac6795d | 3252 | { |
7d8234d9 | 3253 | if (access(client_unix_sock_path, F_OK) < 0 && |
099e26bd | 3254 | access(apps_unix_sock_path, F_OK) < 0) { |
7d8234d9 | 3255 | return 0; |
099e26bd | 3256 | } |
54d01ffb | 3257 | |
7d8234d9 | 3258 | /* Is there anybody out there ? */ |
099e26bd | 3259 | if (lttng_session_daemon_alive()) { |
7d8234d9 | 3260 | return -EEXIST; |
099e26bd | 3261 | } else { |
7d8234d9 | 3262 | return 0; |
099e26bd | 3263 | } |
fac6795d DG |
3264 | } |
3265 | ||
fac6795d | 3266 | /* |
d063d709 | 3267 | * Set the tracing group gid onto the client socket. |
5e16da05 | 3268 | * |
d063d709 DG |
3269 | * Race window between mkdir and chown is OK because we are going from more |
3270 | * permissive (root.root) to les permissive (root.tracing). | |
fac6795d | 3271 | */ |
d6f42150 | 3272 | static int set_permissions(void) |
fac6795d DG |
3273 | { |
3274 | int ret; | |
996b65c8 | 3275 | gid_t gid; |
fac6795d | 3276 | |
996b65c8 MD |
3277 | gid = allowed_group(); |
3278 | if (gid < 0) { | |
a463f419 DG |
3279 | if (is_root) { |
3280 | WARN("No tracing group detected"); | |
3281 | ret = 0; | |
3282 | } else { | |
3283 | ERR("Missing tracing group. Aborting execution."); | |
3284 | ret = -1; | |
3285 | } | |
fac6795d DG |
3286 | goto end; |
3287 | } | |
3288 | ||
d6f42150 | 3289 | /* Set lttng run dir */ |
996b65c8 | 3290 | ret = chown(LTTNG_RUNDIR, 0, gid); |
d6f42150 DG |
3291 | if (ret < 0) { |
3292 | ERR("Unable to set group on " LTTNG_RUNDIR); | |
3293 | perror("chown"); | |
3294 | } | |
3295 | ||
3296 | /* lttng client socket path */ | |
996b65c8 | 3297 | ret = chown(client_unix_sock_path, 0, gid); |
fac6795d | 3298 | if (ret < 0) { |
d6f42150 DG |
3299 | ERR("Unable to set group on %s", client_unix_sock_path); |
3300 | perror("chown"); | |
3301 | } | |
3302 | ||
3bd1e081 MD |
3303 | /* kconsumer error socket path */ |
3304 | ret = chown(kconsumer_data.err_unix_sock_path, 0, gid); | |
d6f42150 | 3305 | if (ret < 0) { |
3bd1e081 MD |
3306 | ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path); |
3307 | perror("chown"); | |
3308 | } | |
3309 | ||
3310 | /* ustconsumer error socket path */ | |
3311 | ret = chown(ustconsumer_data.err_unix_sock_path, 0, gid); | |
3312 | if (ret < 0) { | |
3313 | ERR("Unable to set group on %s", ustconsumer_data.err_unix_sock_path); | |
fac6795d DG |
3314 | perror("chown"); |
3315 | } | |
3316 | ||
d6f42150 | 3317 | DBG("All permissions are set"); |
e07ae692 | 3318 | |
fac6795d DG |
3319 | end: |
3320 | return ret; | |
3321 | } | |
3322 | ||
7a485870 | 3323 | /* |
d063d709 | 3324 | * Create the pipe used to wake up the kernel thread. |
7a485870 DG |
3325 | */ |
3326 | static int create_kernel_poll_pipe(void) | |
3327 | { | |
3328 | return pipe2(kernel_poll_pipe, O_CLOEXEC); | |
3329 | } | |
3330 | ||
099e26bd DG |
3331 | /* |
3332 | * Create the application command pipe to wake thread_manage_apps. | |
3333 | */ | |
3334 | static int create_apps_cmd_pipe(void) | |
3335 | { | |
3336 | return pipe2(apps_cmd_pipe, O_CLOEXEC); | |
3337 | } | |
3338 | ||
d6f42150 | 3339 | /* |
d063d709 | 3340 | * Create the lttng run directory needed for all global sockets and pipe. |
d6f42150 DG |
3341 | */ |
3342 | static int create_lttng_rundir(void) | |
3343 | { | |
3344 | int ret; | |
3345 | ||
3346 | ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG ); | |
3347 | if (ret < 0) { | |
b1f11e69 DG |
3348 | if (errno != EEXIST) { |
3349 | ERR("Unable to create " LTTNG_RUNDIR); | |
3350 | goto error; | |
3351 | } else { | |
3352 | ret = 0; | |
3353 | } | |
d6f42150 DG |
3354 | } |
3355 | ||
3356 | error: | |
3357 | return ret; | |
3358 | } | |
3359 | ||
3360 | /* | |
d063d709 DG |
3361 | * Setup sockets and directory needed by the kconsumerd communication with the |
3362 | * session daemon. | |
d6f42150 | 3363 | */ |
3bd1e081 | 3364 | static int set_consumer_sockets(struct consumer_data *consumer_data) |
d6f42150 DG |
3365 | { |
3366 | int ret; | |
3bd1e081 MD |
3367 | const char *path = consumer_data->type == LTTNG_CONSUMER_KERNEL ? |
3368 | KCONSUMERD_PATH : USTCONSUMERD_PATH; | |
d6f42150 | 3369 | |
3bd1e081 MD |
3370 | if (strlen(consumer_data->err_unix_sock_path) == 0) { |
3371 | snprintf(consumer_data->err_unix_sock_path, PATH_MAX, | |
3372 | consumer_data->type == LTTNG_CONSUMER_KERNEL ? | |
3373 | KCONSUMERD_ERR_SOCK_PATH : | |
3374 | USTCONSUMERD_ERR_SOCK_PATH); | |
d6f42150 DG |
3375 | } |
3376 | ||
3bd1e081 MD |
3377 | if (strlen(consumer_data->cmd_unix_sock_path) == 0) { |
3378 | snprintf(consumer_data->cmd_unix_sock_path, PATH_MAX, | |
3379 | consumer_data->type == LTTNG_CONSUMER_KERNEL ? | |
3380 | KCONSUMERD_CMD_SOCK_PATH : | |
3381 | USTCONSUMERD_CMD_SOCK_PATH); | |
d6f42150 DG |
3382 | } |
3383 | ||
3bd1e081 | 3384 | ret = mkdir(path, S_IRWXU | S_IRWXG); |
d6f42150 | 3385 | if (ret < 0) { |
6beb2242 | 3386 | if (errno != EEXIST) { |
3bd1e081 | 3387 | ERR("Failed to create %s", path); |
6beb2242 DG |
3388 | goto error; |
3389 | } | |
3390 | ret = 0; | |
d6f42150 DG |
3391 | } |
3392 | ||
3393 | /* Create the kconsumerd error unix socket */ | |
3bd1e081 MD |
3394 | consumer_data->err_sock = |
3395 | lttcomm_create_unix_sock(consumer_data->err_unix_sock_path); | |
3396 | if (consumer_data->err_sock < 0) { | |
3397 | ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path); | |
d6f42150 DG |
3398 | ret = -1; |
3399 | goto error; | |
3400 | } | |
3401 | ||
3402 | /* File permission MUST be 660 */ | |
3bd1e081 | 3403 | ret = chmod(consumer_data->err_unix_sock_path, |
54d01ffb | 3404 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); |
d6f42150 | 3405 | if (ret < 0) { |
3bd1e081 | 3406 | ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path); |
d6f42150 DG |
3407 | perror("chmod"); |
3408 | goto error; | |
3409 | } | |
3410 | ||
3411 | error: | |
3412 | return ret; | |
3413 | } | |
3414 | ||
fac6795d | 3415 | /* |
d063d709 | 3416 | * Signal handler for the daemon |
cf3af59e | 3417 | * |
54d01ffb DG |
3418 | * Simply stop all worker threads, leaving main() return gracefully after |
3419 | * joining all threads and calling cleanup(). | |
fac6795d DG |
3420 | */ |
3421 | static void sighandler(int sig) | |
3422 | { | |
3423 | switch (sig) { | |
cf3af59e MD |
3424 | case SIGPIPE: |
3425 | DBG("SIGPIPE catched"); | |
3426 | return; | |
3427 | case SIGINT: | |
3428 | DBG("SIGINT catched"); | |
3429 | stop_threads(); | |
3430 | break; | |
3431 | case SIGTERM: | |
3432 | DBG("SIGTERM catched"); | |
3433 | stop_threads(); | |
3434 | break; | |
3435 | default: | |
3436 | break; | |
fac6795d | 3437 | } |
fac6795d DG |
3438 | } |
3439 | ||
3440 | /* | |
d063d709 | 3441 | * Setup signal handler for : |
1d4b027a | 3442 | * SIGINT, SIGTERM, SIGPIPE |
fac6795d | 3443 | */ |
1d4b027a | 3444 | static int set_signal_handler(void) |
fac6795d | 3445 | { |
1d4b027a DG |
3446 | int ret = 0; |
3447 | struct sigaction sa; | |
3448 | sigset_t sigset; | |
fac6795d | 3449 | |
1d4b027a DG |
3450 | if ((ret = sigemptyset(&sigset)) < 0) { |
3451 | perror("sigemptyset"); | |
3452 | return ret; | |
3453 | } | |
d6f42150 | 3454 | |
1d4b027a DG |
3455 | sa.sa_handler = sighandler; |
3456 | sa.sa_mask = sigset; | |
3457 | sa.sa_flags = 0; | |
3458 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
3459 | perror("sigaction"); | |
3460 | return ret; | |
d6f42150 DG |
3461 | } |
3462 | ||
1d4b027a DG |
3463 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { |
3464 | perror("sigaction"); | |
3465 | return ret; | |
d6f42150 | 3466 | } |
aaf26714 | 3467 | |
1d4b027a DG |
3468 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { |
3469 | perror("sigaction"); | |
3470 | return ret; | |
8c0faa1d DG |
3471 | } |
3472 | ||
1d4b027a DG |
3473 | DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT"); |
3474 | ||
3475 | return ret; | |
fac6795d DG |
3476 | } |
3477 | ||
f3ed775e | 3478 | /* |
d063d709 DG |
3479 | * Set open files limit to unlimited. This daemon can open a large number of |
3480 | * file descriptors in order to consumer multiple kernel traces. | |
f3ed775e DG |
3481 | */ |
3482 | static void set_ulimit(void) | |
3483 | { | |
3484 | int ret; | |
3485 | struct rlimit lim; | |
3486 | ||
a88df331 | 3487 | /* The kernel does not allowed an infinite limit for open files */ |
f3ed775e DG |
3488 | lim.rlim_cur = 65535; |
3489 | lim.rlim_max = 65535; | |
3490 | ||
3491 | ret = setrlimit(RLIMIT_NOFILE, &lim); | |
3492 | if (ret < 0) { | |
3493 | perror("failed to set open files limit"); | |
3494 | } | |
3495 | } | |
3496 | ||
fac6795d DG |
3497 | /* |
3498 | * main | |
3499 | */ | |
3500 | int main(int argc, char **argv) | |
3501 | { | |
fac6795d DG |
3502 | int ret = 0; |
3503 | void *status; | |
b082db07 | 3504 | const char *home_path; |
fac6795d | 3505 | |
273ea72c | 3506 | /* Create thread quit pipe */ |
cf3af59e MD |
3507 | if ((ret = init_thread_quit_pipe()) < 0) { |
3508 | goto error; | |
273ea72c DG |
3509 | } |
3510 | ||
fac6795d DG |
3511 | /* Parse arguments */ |
3512 | progname = argv[0]; | |
3513 | if ((ret = parse_args(argc, argv) < 0)) { | |
cf3af59e | 3514 | goto error; |
fac6795d DG |
3515 | } |
3516 | ||
3517 | /* Daemonize */ | |
3518 | if (opt_daemon) { | |
53094c05 DG |
3519 | ret = daemon(0, 0); |
3520 | if (ret < 0) { | |
3521 | perror("daemon"); | |
cf3af59e | 3522 | goto error; |
53094c05 | 3523 | } |
fac6795d DG |
3524 | } |
3525 | ||
3526 | /* Check if daemon is UID = 0 */ | |
3527 | is_root = !getuid(); | |
3528 | ||
fac6795d | 3529 | if (is_root) { |
d6f42150 DG |
3530 | ret = create_lttng_rundir(); |
3531 | if (ret < 0) { | |
cf3af59e | 3532 | goto error; |
d6f42150 DG |
3533 | } |
3534 | ||
fac6795d | 3535 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 DG |
3536 | snprintf(apps_unix_sock_path, PATH_MAX, |
3537 | DEFAULT_GLOBAL_APPS_UNIX_SOCK); | |
fac6795d DG |
3538 | } |
3539 | ||
3540 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 DG |
3541 | snprintf(client_unix_sock_path, PATH_MAX, |
3542 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK); | |
3543 | } | |
0fdd1e2c DG |
3544 | |
3545 | /* Set global SHM for ust */ | |
3546 | if (strlen(wait_shm_path) == 0) { | |
3547 | snprintf(wait_shm_path, PATH_MAX, | |
3548 | DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH); | |
3549 | } | |
fac6795d | 3550 | } else { |
b082db07 DG |
3551 | home_path = get_home_dir(); |
3552 | if (home_path == NULL) { | |
273ea72c DG |
3553 | /* TODO: Add --socket PATH option */ |
3554 | ERR("Can't get HOME directory for sockets creation."); | |
cf3af59e MD |
3555 | ret = -EPERM; |
3556 | goto error; | |
b082db07 DG |
3557 | } |
3558 | ||
fac6795d | 3559 | if (strlen(apps_unix_sock_path) == 0) { |
d6f42150 | 3560 | snprintf(apps_unix_sock_path, PATH_MAX, |
b082db07 | 3561 | DEFAULT_HOME_APPS_UNIX_SOCK, home_path); |
fac6795d DG |
3562 | } |
3563 | ||
3564 | /* Set the cli tool unix socket path */ | |
3565 | if (strlen(client_unix_sock_path) == 0) { | |
d6f42150 | 3566 | snprintf(client_unix_sock_path, PATH_MAX, |
b082db07 | 3567 | DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path); |
fac6795d | 3568 | } |
0fdd1e2c DG |
3569 | |
3570 | /* Set global SHM for ust */ | |
3571 | if (strlen(wait_shm_path) == 0) { | |
3572 | snprintf(wait_shm_path, PATH_MAX, | |
3573 | DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid()); | |
3574 | } | |
fac6795d DG |
3575 | } |
3576 | ||
847177cd DG |
3577 | DBG("Client socket path %s", client_unix_sock_path); |
3578 | DBG("Application socket path %s", apps_unix_sock_path); | |
3579 | ||
273ea72c | 3580 | /* |
7d8234d9 | 3581 | * See if daemon already exist. |
fac6795d | 3582 | */ |
7d8234d9 | 3583 | if ((ret = check_existing_daemon()) < 0) { |
75462a81 | 3584 | ERR("Already running daemon.\n"); |
273ea72c | 3585 | /* |
cf3af59e MD |
3586 | * We do not goto exit because we must not cleanup() |
3587 | * because a daemon is already running. | |
ab118b20 | 3588 | */ |
cf3af59e | 3589 | goto error; |
a88df331 DG |
3590 | } |
3591 | ||
54d01ffb | 3592 | /* After this point, we can safely call cleanup() with "goto exit" */ |
a88df331 DG |
3593 | |
3594 | /* | |
3595 | * These actions must be executed as root. We do that *after* setting up | |
3596 | * the sockets path because we MUST make the check for another daemon using | |
3597 | * those paths *before* trying to set the kernel consumer sockets and init | |
3598 | * kernel tracer. | |
3599 | */ | |
3600 | if (is_root) { | |
3bd1e081 MD |
3601 | ret = set_consumer_sockets(&kconsumer_data); |
3602 | if (ret < 0) { | |
3603 | goto exit; | |
3604 | } | |
3605 | ret = set_consumer_sockets(&ustconsumer_data); | |
a88df331 | 3606 | if (ret < 0) { |
cf3af59e | 3607 | goto exit; |
a88df331 | 3608 | } |
a88df331 DG |
3609 | /* Setup kernel tracer */ |
3610 | init_kernel_tracer(); | |
3611 | ||
3612 | /* Set ulimit for open files */ | |
3613 | set_ulimit(); | |
fac6795d DG |
3614 | } |
3615 | ||
cf3af59e MD |
3616 | if ((ret = set_signal_handler()) < 0) { |
3617 | goto exit; | |
fac6795d DG |
3618 | } |
3619 | ||
d6f42150 | 3620 | /* Setup the needed unix socket */ |
cf3af59e MD |
3621 | if ((ret = init_daemon_socket()) < 0) { |
3622 | goto exit; | |
fac6795d DG |
3623 | } |
3624 | ||
3625 | /* Set credentials to socket */ | |
cf3af59e MD |
3626 | if (is_root && ((ret = set_permissions()) < 0)) { |
3627 | goto exit; | |
fac6795d DG |
3628 | } |
3629 | ||
5b8719f5 DG |
3630 | /* Get parent pid if -S, --sig-parent is specified. */ |
3631 | if (opt_sig_parent) { | |
3632 | ppid = getppid(); | |
3633 | } | |
3634 | ||
7a485870 | 3635 | /* Setup the kernel pipe for waking up the kernel thread */ |
cf3af59e MD |
3636 | if ((ret = create_kernel_poll_pipe()) < 0) { |
3637 | goto exit; | |
7a485870 DG |
3638 | } |
3639 | ||
099e26bd DG |
3640 | /* Setup the thread apps communication pipe. */ |
3641 | if ((ret = create_apps_cmd_pipe()) < 0) { | |
3642 | goto exit; | |
3643 | } | |
3644 | ||
3645 | /* Init UST command queue. */ | |
3646 | cds_wfq_init(&ust_cmd_queue.queue); | |
3647 | ||
273ea72c | 3648 | /* |
54d01ffb DG |
3649 | * Get session list pointer. This pointer MUST NOT be free(). This list is |
3650 | * statically declared in session.c | |
273ea72c | 3651 | */ |
54d01ffb | 3652 | session_list_ptr = session_get_list(); |
b5541356 | 3653 | |
5eb91c98 DG |
3654 | /* Set up max poll set size */ |
3655 | lttng_poll_set_max_size(); | |
3656 | ||
cf3af59e | 3657 | /* Create thread to manage the client socket */ |
099e26bd DG |
3658 | ret = pthread_create(&client_thread, NULL, |
3659 | thread_manage_clients, (void *) NULL); | |
cf3af59e | 3660 | if (ret != 0) { |
099e26bd | 3661 | perror("pthread_create clients"); |
cf3af59e MD |
3662 | goto exit_client; |
3663 | } | |
fac6795d | 3664 | |
099e26bd DG |
3665 | /* Create thread to dispatch registration */ |
3666 | ret = pthread_create(&dispatch_thread, NULL, | |
3667 | thread_dispatch_ust_registration, (void *) NULL); | |
3668 | if (ret != 0) { | |
3669 | perror("pthread_create dispatch"); | |
3670 | goto exit_dispatch; | |
3671 | } | |
3672 | ||
3673 | /* Create thread to manage application registration. */ | |
3674 | ret = pthread_create(®_apps_thread, NULL, | |
3675 | thread_registration_apps, (void *) NULL); | |
3676 | if (ret != 0) { | |
3677 | perror("pthread_create registration"); | |
3678 | goto exit_reg_apps; | |
3679 | } | |
3680 | ||
cf3af59e | 3681 | /* Create thread to manage application socket */ |
54d01ffb DG |
3682 | ret = pthread_create(&apps_thread, NULL, |
3683 | thread_manage_apps, (void *) NULL); | |
cf3af59e | 3684 | if (ret != 0) { |
099e26bd | 3685 | perror("pthread_create apps"); |
cf3af59e MD |
3686 | goto exit_apps; |
3687 | } | |
fac6795d | 3688 | |
cf3af59e | 3689 | /* Create kernel thread to manage kernel event */ |
54d01ffb DG |
3690 | ret = pthread_create(&kernel_thread, NULL, |
3691 | thread_manage_kernel, (void *) NULL); | |
cf3af59e | 3692 | if (ret != 0) { |
099e26bd | 3693 | perror("pthread_create kernel"); |
cf3af59e MD |
3694 | goto exit_kernel; |
3695 | } | |
7a485870 | 3696 | |
cf3af59e MD |
3697 | ret = pthread_join(kernel_thread, &status); |
3698 | if (ret != 0) { | |
3699 | perror("pthread_join"); | |
3700 | goto error; /* join error, exit without cleanup */ | |
fac6795d DG |
3701 | } |
3702 | ||
cf3af59e MD |
3703 | exit_kernel: |
3704 | ret = pthread_join(apps_thread, &status); | |
3705 | if (ret != 0) { | |
3706 | perror("pthread_join"); | |
3707 | goto error; /* join error, exit without cleanup */ | |
3708 | } | |
fac6795d | 3709 | |
cf3af59e | 3710 | exit_apps: |
099e26bd DG |
3711 | ret = pthread_join(reg_apps_thread, &status); |
3712 | if (ret != 0) { | |
3713 | perror("pthread_join"); | |
3714 | goto error; /* join error, exit without cleanup */ | |
3715 | } | |
3716 | ||
3717 | exit_reg_apps: | |
3718 | ret = pthread_join(dispatch_thread, &status); | |
3719 | if (ret != 0) { | |
3720 | perror("pthread_join"); | |
3721 | goto error; /* join error, exit without cleanup */ | |
3722 | } | |
3723 | ||
3724 | exit_dispatch: | |
cf3af59e MD |
3725 | ret = pthread_join(client_thread, &status); |
3726 | if (ret != 0) { | |
3727 | perror("pthread_join"); | |
3728 | goto error; /* join error, exit without cleanup */ | |
3729 | } | |
3730 | ||
3bd1e081 | 3731 | ret = join_consumer_thread(&kconsumer_data); |
cf3af59e | 3732 | if (ret != 0) { |
3bd1e081 | 3733 | perror("join_consumer"); |
cf3af59e MD |
3734 | goto error; /* join error, exit without cleanup */ |
3735 | } | |
a88df331 | 3736 | |
cf3af59e | 3737 | exit_client: |
a88df331 | 3738 | exit: |
cf3af59e MD |
3739 | /* |
3740 | * cleanup() is called when no other thread is running. | |
3741 | */ | |
3742 | cleanup(); | |
3743 | if (!ret) | |
3744 | exit(EXIT_SUCCESS); | |
3745 | error: | |
5e16da05 | 3746 | exit(EXIT_FAILURE); |
fac6795d | 3747 | } |