Implement PID tracker content listing
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _GNU_SOURCE
21 #define _LGPL_SOURCE
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <paths.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <inttypes.h>
32 #include <sys/mman.h>
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <urcu/uatomic.h>
40 #include <unistd.h>
41 #include <config.h>
42
43 #include <common/common.h>
44 #include <common/compat/socket.h>
45 #include <common/compat/getenv.h>
46 #include <common/defaults.h>
47 #include <common/kernel-consumer/kernel-consumer.h>
48 #include <common/futex.h>
49 #include <common/relayd/relayd.h>
50 #include <common/utils.h>
51 #include <common/daemonize.h>
52 #include <common/config/config.h>
53
54 #include "lttng-sessiond.h"
55 #include "buffer-registry.h"
56 #include "channel.h"
57 #include "cmd.h"
58 #include "consumer.h"
59 #include "context.h"
60 #include "event.h"
61 #include "kernel.h"
62 #include "kernel-consumer.h"
63 #include "modprobe.h"
64 #include "shm.h"
65 #include "ust-ctl.h"
66 #include "ust-consumer.h"
67 #include "utils.h"
68 #include "fd-limit.h"
69 #include "health-sessiond.h"
70 #include "testpoint.h"
71 #include "ust-thread.h"
72 #include "agent-thread.h"
73 #include "save.h"
74 #include "load-session-thread.h"
75 #include "syscall.h"
76
77 #define CONSUMERD_FILE "lttng-consumerd"
78
79 const char *progname;
80 static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
81 static int tracing_group_name_override;
82 static char *opt_pidfile;
83 static int opt_sig_parent;
84 static int opt_verbose_consumer;
85 static int opt_daemon, opt_background;
86 static int opt_no_kernel;
87 static char *opt_load_session_path;
88 static pid_t ppid; /* Parent PID for --sig-parent option */
89 static pid_t child_ppid; /* Internal parent PID use with daemonize. */
90 static char *rundir;
91 static int lockfile_fd = -1;
92
93 /* Set to 1 when a SIGUSR1 signal is received. */
94 static int recv_child_signal;
95
96 /*
97 * Consumer daemon specific control data. Every value not initialized here is
98 * set to 0 by the static definition.
99 */
100 static struct consumer_data kconsumer_data = {
101 .type = LTTNG_CONSUMER_KERNEL,
102 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
103 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
104 .err_sock = -1,
105 .cmd_sock = -1,
106 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
107 .lock = PTHREAD_MUTEX_INITIALIZER,
108 .cond = PTHREAD_COND_INITIALIZER,
109 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
110 };
111 static struct consumer_data ustconsumer64_data = {
112 .type = LTTNG_CONSUMER64_UST,
113 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
114 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
115 .err_sock = -1,
116 .cmd_sock = -1,
117 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
118 .lock = PTHREAD_MUTEX_INITIALIZER,
119 .cond = PTHREAD_COND_INITIALIZER,
120 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
121 };
122 static struct consumer_data ustconsumer32_data = {
123 .type = LTTNG_CONSUMER32_UST,
124 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
125 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
126 .err_sock = -1,
127 .cmd_sock = -1,
128 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
129 .lock = PTHREAD_MUTEX_INITIALIZER,
130 .cond = PTHREAD_COND_INITIALIZER,
131 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
132 };
133
134 /* Command line options */
135 static const struct option long_options[] = {
136 { "client-sock", 1, 0, 'c' },
137 { "apps-sock", 1, 0, 'a' },
138 { "kconsumerd-cmd-sock", 1, 0, 'C' },
139 { "kconsumerd-err-sock", 1, 0, 'E' },
140 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
141 { "ustconsumerd32-err-sock", 1, 0, 'H' },
142 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
143 { "ustconsumerd64-err-sock", 1, 0, 'F' },
144 { "consumerd32-path", 1, 0, 'u' },
145 { "consumerd32-libdir", 1, 0, 'U' },
146 { "consumerd64-path", 1, 0, 't' },
147 { "consumerd64-libdir", 1, 0, 'T' },
148 { "daemonize", 0, 0, 'd' },
149 { "background", 0, 0, 'b' },
150 { "sig-parent", 0, 0, 'S' },
151 { "help", 0, 0, 'h' },
152 { "group", 1, 0, 'g' },
153 { "version", 0, 0, 'V' },
154 { "quiet", 0, 0, 'q' },
155 { "verbose", 0, 0, 'v' },
156 { "verbose-consumer", 0, 0, 'Z' },
157 { "no-kernel", 0, 0, 'N' },
158 { "pidfile", 1, 0, 'p' },
159 { "agent-tcp-port", 1, 0, 'J' },
160 { "config", 1, 0, 'f' },
161 { "load", 1, 0, 'l' },
162 { "kmod-probes", 1, 0, 'P' },
163 { "extra-kmod-probes", 1, 0, 'e' },
164 { NULL, 0, 0, 0 }
165 };
166
167 /* Command line options to ignore from configuration file */
168 static const char *config_ignore_options[] = { "help", "version", "config" };
169
170 /* Shared between threads */
171 static int dispatch_thread_exit;
172
173 /* Global application Unix socket path */
174 static char apps_unix_sock_path[PATH_MAX];
175 /* Global client Unix socket path */
176 static char client_unix_sock_path[PATH_MAX];
177 /* global wait shm path for UST */
178 static char wait_shm_path[PATH_MAX];
179 /* Global health check unix path */
180 static char health_unix_sock_path[PATH_MAX];
181
182 /* Sockets and FDs */
183 static int client_sock = -1;
184 static int apps_sock = -1;
185 int kernel_tracer_fd = -1;
186 static int kernel_poll_pipe[2] = { -1, -1 };
187
188 /*
189 * Quit pipe for all threads. This permits a single cancellation point
190 * for all threads when receiving an event on the pipe.
191 */
192 static int thread_quit_pipe[2] = { -1, -1 };
193 static int ht_cleanup_quit_pipe[2] = { -1, -1 };
194
195 /*
196 * This pipe is used to inform the thread managing application communication
197 * that a command is queued and ready to be processed.
198 */
199 static int apps_cmd_pipe[2] = { -1, -1 };
200
201 int apps_cmd_notify_pipe[2] = { -1, -1 };
202
203 /* Pthread, Mutexes and Semaphores */
204 static pthread_t apps_thread;
205 static pthread_t apps_notify_thread;
206 static pthread_t reg_apps_thread;
207 static pthread_t client_thread;
208 static pthread_t kernel_thread;
209 static pthread_t dispatch_thread;
210 static pthread_t health_thread;
211 static pthread_t ht_cleanup_thread;
212 static pthread_t agent_reg_thread;
213 static pthread_t load_session_thread;
214
215 /*
216 * UST registration command queue. This queue is tied with a futex and uses a N
217 * wakers / 1 waiter implemented and detailed in futex.c/.h
218 *
219 * The thread_registration_apps and thread_dispatch_ust_registration uses this
220 * queue along with the wait/wake scheme. The thread_manage_apps receives down
221 * the line new application socket and monitors it for any I/O error or clean
222 * close that triggers an unregistration of the application.
223 */
224 static struct ust_cmd_queue ust_cmd_queue;
225
226 /*
227 * Pointer initialized before thread creation.
228 *
229 * This points to the tracing session list containing the session count and a
230 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
231 * MUST NOT be taken if you call a public function in session.c.
232 *
233 * The lock is nested inside the structure: session_list_ptr->lock. Please use
234 * session_lock_list and session_unlock_list for lock acquisition.
235 */
236 static struct ltt_session_list *session_list_ptr;
237
238 int ust_consumerd64_fd = -1;
239 int ust_consumerd32_fd = -1;
240
241 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
242 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
243 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
244 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
245 static int consumerd32_bin_override;
246 static int consumerd64_bin_override;
247 static int consumerd32_libdir_override;
248 static int consumerd64_libdir_override;
249
250 static const char *module_proc_lttng = "/proc/lttng";
251
252 /*
253 * Consumer daemon state which is changed when spawning it, killing it or in
254 * case of a fatal error.
255 */
256 enum consumerd_state {
257 CONSUMER_STARTED = 1,
258 CONSUMER_STOPPED = 2,
259 CONSUMER_ERROR = 3,
260 };
261
262 /*
263 * This consumer daemon state is used to validate if a client command will be
264 * able to reach the consumer. If not, the client is informed. For instance,
265 * doing a "lttng start" when the consumer state is set to ERROR will return an
266 * error to the client.
267 *
268 * The following example shows a possible race condition of this scheme:
269 *
270 * consumer thread error happens
271 * client cmd arrives
272 * client cmd checks state -> still OK
273 * consumer thread exit, sets error
274 * client cmd try to talk to consumer
275 * ...
276 *
277 * However, since the consumer is a different daemon, we have no way of making
278 * sure the command will reach it safely even with this state flag. This is why
279 * we consider that up to the state validation during command processing, the
280 * command is safe. After that, we can not guarantee the correctness of the
281 * client request vis-a-vis the consumer.
282 */
283 static enum consumerd_state ust_consumerd_state;
284 static enum consumerd_state kernel_consumerd_state;
285
286 /*
287 * Socket timeout for receiving and sending in seconds.
288 */
289 static int app_socket_timeout;
290
291 /* Set in main() with the current page size. */
292 long page_size;
293
294 /* Application health monitoring */
295 struct health_app *health_sessiond;
296
297 /* Agent TCP port for registration. Used by the agent thread. */
298 unsigned int agent_tcp_port = DEFAULT_AGENT_TCP_PORT;
299
300 /* Am I root or not. */
301 int is_root; /* Set to 1 if the daemon is running as root */
302
303 const char * const config_section_name = "sessiond";
304
305 /* Load session thread information to operate. */
306 struct load_session_thread_data *load_info;
307
308 /*
309 * Whether sessiond is ready for commands/health check requests.
310 * NR_LTTNG_SESSIOND_READY must match the number of calls to
311 * sessiond_notify_ready().
312 */
313 #define NR_LTTNG_SESSIOND_READY 3
314 int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
315
316 /* Notify parents that we are ready for cmd and health check */
317 LTTNG_HIDDEN
318 void sessiond_notify_ready(void)
319 {
320 if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
321 /*
322 * Notify parent pid that we are ready to accept command
323 * for client side. This ppid is the one from the
324 * external process that spawned us.
325 */
326 if (opt_sig_parent) {
327 kill(ppid, SIGUSR1);
328 }
329
330 /*
331 * Notify the parent of the fork() process that we are
332 * ready.
333 */
334 if (opt_daemon || opt_background) {
335 kill(child_ppid, SIGUSR1);
336 }
337 }
338 }
339
340 static
341 void setup_consumerd_path(void)
342 {
343 const char *bin, *libdir;
344
345 /*
346 * Allow INSTALL_BIN_PATH to be used as a target path for the
347 * native architecture size consumer if CONFIG_CONSUMER*_PATH
348 * has not been defined.
349 */
350 #if (CAA_BITS_PER_LONG == 32)
351 if (!consumerd32_bin[0]) {
352 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
353 }
354 if (!consumerd32_libdir[0]) {
355 consumerd32_libdir = INSTALL_LIB_PATH;
356 }
357 #elif (CAA_BITS_PER_LONG == 64)
358 if (!consumerd64_bin[0]) {
359 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
360 }
361 if (!consumerd64_libdir[0]) {
362 consumerd64_libdir = INSTALL_LIB_PATH;
363 }
364 #else
365 #error "Unknown bitness"
366 #endif
367
368 /*
369 * runtime env. var. overrides the build default.
370 */
371 bin = lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
372 if (bin) {
373 consumerd32_bin = bin;
374 }
375 bin = lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
376 if (bin) {
377 consumerd64_bin = bin;
378 }
379 libdir = lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
380 if (libdir) {
381 consumerd32_libdir = libdir;
382 }
383 libdir = lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
384 if (libdir) {
385 consumerd64_libdir = libdir;
386 }
387 }
388
389 static
390 int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
391 int *a_pipe)
392 {
393 int ret;
394
395 assert(events);
396
397 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
398 if (ret < 0) {
399 goto error;
400 }
401
402 /* Add quit pipe */
403 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
404 if (ret < 0) {
405 goto error;
406 }
407
408 return 0;
409
410 error:
411 return ret;
412 }
413
414 /*
415 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
416 */
417 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
418 {
419 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
420 }
421
422 /*
423 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
424 */
425 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event *events,
426 size_t size)
427 {
428 return __sessiond_set_thread_pollset(events, size,
429 ht_cleanup_quit_pipe);
430 }
431
432 static
433 int __sessiond_check_thread_quit_pipe(int fd, uint32_t events, int a_pipe)
434 {
435 if (fd == a_pipe && (events & LPOLLIN)) {
436 return 1;
437 }
438 return 0;
439 }
440
441 /*
442 * Check if the thread quit pipe was triggered.
443 *
444 * Return 1 if it was triggered else 0;
445 */
446 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
447 {
448 return __sessiond_check_thread_quit_pipe(fd, events,
449 thread_quit_pipe[0]);
450 }
451
452 /*
453 * Check if the ht_cleanup thread quit pipe was triggered.
454 *
455 * Return 1 if it was triggered else 0;
456 */
457 int sessiond_check_ht_cleanup_quit(int fd, uint32_t events)
458 {
459 return __sessiond_check_thread_quit_pipe(fd, events,
460 ht_cleanup_quit_pipe[0]);
461 }
462
463 /*
464 * Init thread quit pipe.
465 *
466 * Return -1 on error or 0 if all pipes are created.
467 */
468 static int __init_thread_quit_pipe(int *a_pipe)
469 {
470 int ret, i;
471
472 ret = pipe(a_pipe);
473 if (ret < 0) {
474 PERROR("thread quit pipe");
475 goto error;
476 }
477
478 for (i = 0; i < 2; i++) {
479 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
480 if (ret < 0) {
481 PERROR("fcntl");
482 goto error;
483 }
484 }
485
486 error:
487 return ret;
488 }
489
490 static int init_thread_quit_pipe(void)
491 {
492 return __init_thread_quit_pipe(thread_quit_pipe);
493 }
494
495 static int init_ht_cleanup_quit_pipe(void)
496 {
497 return __init_thread_quit_pipe(ht_cleanup_quit_pipe);
498 }
499
500 /*
501 * Stop all threads by closing the thread quit pipe.
502 */
503 static void stop_threads(void)
504 {
505 int ret;
506
507 /* Stopping all threads */
508 DBG("Terminating all threads");
509 ret = notify_thread_pipe(thread_quit_pipe[1]);
510 if (ret < 0) {
511 ERR("write error on thread quit pipe");
512 }
513
514 /* Dispatch thread */
515 CMM_STORE_SHARED(dispatch_thread_exit, 1);
516 futex_nto1_wake(&ust_cmd_queue.futex);
517 }
518
519 /*
520 * Close every consumer sockets.
521 */
522 static void close_consumer_sockets(void)
523 {
524 int ret;
525
526 if (kconsumer_data.err_sock >= 0) {
527 ret = close(kconsumer_data.err_sock);
528 if (ret < 0) {
529 PERROR("kernel consumer err_sock close");
530 }
531 }
532 if (ustconsumer32_data.err_sock >= 0) {
533 ret = close(ustconsumer32_data.err_sock);
534 if (ret < 0) {
535 PERROR("UST consumerd32 err_sock close");
536 }
537 }
538 if (ustconsumer64_data.err_sock >= 0) {
539 ret = close(ustconsumer64_data.err_sock);
540 if (ret < 0) {
541 PERROR("UST consumerd64 err_sock close");
542 }
543 }
544 if (kconsumer_data.cmd_sock >= 0) {
545 ret = close(kconsumer_data.cmd_sock);
546 if (ret < 0) {
547 PERROR("kernel consumer cmd_sock close");
548 }
549 }
550 if (ustconsumer32_data.cmd_sock >= 0) {
551 ret = close(ustconsumer32_data.cmd_sock);
552 if (ret < 0) {
553 PERROR("UST consumerd32 cmd_sock close");
554 }
555 }
556 if (ustconsumer64_data.cmd_sock >= 0) {
557 ret = close(ustconsumer64_data.cmd_sock);
558 if (ret < 0) {
559 PERROR("UST consumerd64 cmd_sock close");
560 }
561 }
562 }
563
564 /*
565 * Generate the full lock file path using the rundir.
566 *
567 * Return the snprintf() return value thus a negative value is an error.
568 */
569 static int generate_lock_file_path(char *path, size_t len)
570 {
571 int ret;
572
573 assert(path);
574 assert(rundir);
575
576 /* Build lockfile path from rundir. */
577 ret = snprintf(path, len, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE, rundir);
578 if (ret < 0) {
579 PERROR("snprintf lockfile path");
580 }
581
582 return ret;
583 }
584
585 /*
586 * Cleanup the session daemon's data structures.
587 */
588 static void sessiond_cleanup(void)
589 {
590 int ret;
591 struct ltt_session *sess, *stmp;
592 char path[PATH_MAX];
593
594 DBG("Cleanup sessiond");
595
596 /*
597 * Close the thread quit pipe. It has already done its job,
598 * since we are now called.
599 */
600 utils_close_pipe(thread_quit_pipe);
601
602 /*
603 * If opt_pidfile is undefined, the default file will be wiped when
604 * removing the rundir.
605 */
606 if (opt_pidfile) {
607 ret = remove(opt_pidfile);
608 if (ret < 0) {
609 PERROR("remove pidfile %s", opt_pidfile);
610 }
611 }
612
613 DBG("Removing sessiond and consumerd content of directory %s", rundir);
614
615 /* sessiond */
616 snprintf(path, PATH_MAX,
617 "%s/%s",
618 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
619 DBG("Removing %s", path);
620 (void) unlink(path);
621
622 snprintf(path, PATH_MAX, "%s/%s", rundir,
623 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE);
624 DBG("Removing %s", path);
625 (void) unlink(path);
626
627 /* kconsumerd */
628 snprintf(path, PATH_MAX,
629 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
630 rundir);
631 DBG("Removing %s", path);
632 (void) unlink(path);
633
634 snprintf(path, PATH_MAX,
635 DEFAULT_KCONSUMERD_PATH,
636 rundir);
637 DBG("Removing directory %s", path);
638 (void) rmdir(path);
639
640 /* ust consumerd 32 */
641 snprintf(path, PATH_MAX,
642 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
643 rundir);
644 DBG("Removing %s", path);
645 (void) unlink(path);
646
647 snprintf(path, PATH_MAX,
648 DEFAULT_USTCONSUMERD32_PATH,
649 rundir);
650 DBG("Removing directory %s", path);
651 (void) rmdir(path);
652
653 /* ust consumerd 64 */
654 snprintf(path, PATH_MAX,
655 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
656 rundir);
657 DBG("Removing %s", path);
658 (void) unlink(path);
659
660 snprintf(path, PATH_MAX,
661 DEFAULT_USTCONSUMERD64_PATH,
662 rundir);
663 DBG("Removing directory %s", path);
664 (void) rmdir(path);
665
666 DBG("Cleaning up all sessions");
667
668 /* Destroy session list mutex */
669 if (session_list_ptr != NULL) {
670 pthread_mutex_destroy(&session_list_ptr->lock);
671
672 /* Cleanup ALL session */
673 cds_list_for_each_entry_safe(sess, stmp,
674 &session_list_ptr->head, list) {
675 cmd_destroy_session(sess, kernel_poll_pipe[1]);
676 }
677 }
678
679 DBG("Closing all UST sockets");
680 ust_app_clean_list();
681 buffer_reg_destroy_registries();
682
683 if (is_root && !opt_no_kernel) {
684 DBG2("Closing kernel fd");
685 if (kernel_tracer_fd >= 0) {
686 ret = close(kernel_tracer_fd);
687 if (ret) {
688 PERROR("close");
689 }
690 }
691 DBG("Unloading kernel modules");
692 modprobe_remove_lttng_all();
693 free(syscall_table);
694 }
695
696 close_consumer_sockets();
697
698 if (load_info) {
699 load_session_destroy_data(load_info);
700 free(load_info);
701 }
702
703 /*
704 * Cleanup lock file by deleting it and finaly closing it which will
705 * release the file system lock.
706 */
707 if (lockfile_fd >= 0) {
708 char lockfile_path[PATH_MAX];
709
710 ret = generate_lock_file_path(lockfile_path,
711 sizeof(lockfile_path));
712 if (ret > 0) {
713 ret = remove(lockfile_path);
714 if (ret < 0) {
715 PERROR("remove lock file");
716 }
717 ret = close(lockfile_fd);
718 if (ret < 0) {
719 PERROR("close lock file");
720 }
721 }
722 }
723
724 /*
725 * We do NOT rmdir rundir because there are other processes
726 * using it, for instance lttng-relayd, which can start in
727 * parallel with this teardown.
728 */
729
730 free(rundir);
731 }
732
733 /*
734 * Cleanup the daemon's option data structures.
735 */
736 static void sessiond_cleanup_options(void)
737 {
738 DBG("Cleaning up options");
739
740 /*
741 * If the override option is set, the pointer points to a *non* const
742 * thus freeing it even though the variable type is set to const.
743 */
744 if (tracing_group_name_override) {
745 free((void *) tracing_group_name);
746 }
747 if (consumerd32_bin_override) {
748 free((void *) consumerd32_bin);
749 }
750 if (consumerd64_bin_override) {
751 free((void *) consumerd64_bin);
752 }
753 if (consumerd32_libdir_override) {
754 free((void *) consumerd32_libdir);
755 }
756 if (consumerd64_libdir_override) {
757 free((void *) consumerd64_libdir);
758 }
759
760 free(opt_pidfile);
761 free(opt_load_session_path);
762 free(kmod_probes_list);
763 free(kmod_extra_probes_list);
764
765 /* <fun> */
766 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
767 "Matthew, BEET driven development works!%c[%dm",
768 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
769 /* </fun> */
770 }
771
772 /*
773 * Send data on a unix socket using the liblttsessiondcomm API.
774 *
775 * Return lttcomm error code.
776 */
777 static int send_unix_sock(int sock, void *buf, size_t len)
778 {
779 /* Check valid length */
780 if (len == 0) {
781 return -1;
782 }
783
784 return lttcomm_send_unix_sock(sock, buf, len);
785 }
786
787 /*
788 * Free memory of a command context structure.
789 */
790 static void clean_command_ctx(struct command_ctx **cmd_ctx)
791 {
792 DBG("Clean command context structure");
793 if (*cmd_ctx) {
794 if ((*cmd_ctx)->llm) {
795 free((*cmd_ctx)->llm);
796 }
797 if ((*cmd_ctx)->lsm) {
798 free((*cmd_ctx)->lsm);
799 }
800 free(*cmd_ctx);
801 *cmd_ctx = NULL;
802 }
803 }
804
805 /*
806 * Notify UST applications using the shm mmap futex.
807 */
808 static int notify_ust_apps(int active)
809 {
810 char *wait_shm_mmap;
811
812 DBG("Notifying applications of session daemon state: %d", active);
813
814 /* See shm.c for this call implying mmap, shm and futex calls */
815 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
816 if (wait_shm_mmap == NULL) {
817 goto error;
818 }
819
820 /* Wake waiting process */
821 futex_wait_update((int32_t *) wait_shm_mmap, active);
822
823 /* Apps notified successfully */
824 return 0;
825
826 error:
827 return -1;
828 }
829
830 /*
831 * Setup the outgoing data buffer for the response (llm) by allocating the
832 * right amount of memory and copying the original information from the lsm
833 * structure.
834 *
835 * Return total size of the buffer pointed by buf.
836 */
837 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
838 {
839 int ret, buf_size;
840
841 buf_size = size;
842
843 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
844 if (cmd_ctx->llm == NULL) {
845 PERROR("zmalloc");
846 ret = -ENOMEM;
847 goto error;
848 }
849
850 /* Copy common data */
851 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
852 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
853
854 cmd_ctx->llm->data_size = size;
855 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
856
857 return buf_size;
858
859 error:
860 return ret;
861 }
862
863 /*
864 * Update the kernel poll set of all channel fd available over all tracing
865 * session. Add the wakeup pipe at the end of the set.
866 */
867 static int update_kernel_poll(struct lttng_poll_event *events)
868 {
869 int ret;
870 struct ltt_session *session;
871 struct ltt_kernel_channel *channel;
872
873 DBG("Updating kernel poll set");
874
875 session_lock_list();
876 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
877 session_lock(session);
878 if (session->kernel_session == NULL) {
879 session_unlock(session);
880 continue;
881 }
882
883 cds_list_for_each_entry(channel,
884 &session->kernel_session->channel_list.head, list) {
885 /* Add channel fd to the kernel poll set */
886 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
887 if (ret < 0) {
888 session_unlock(session);
889 goto error;
890 }
891 DBG("Channel fd %d added to kernel set", channel->fd);
892 }
893 session_unlock(session);
894 }
895 session_unlock_list();
896
897 return 0;
898
899 error:
900 session_unlock_list();
901 return -1;
902 }
903
904 /*
905 * Find the channel fd from 'fd' over all tracing session. When found, check
906 * for new channel stream and send those stream fds to the kernel consumer.
907 *
908 * Useful for CPU hotplug feature.
909 */
910 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
911 {
912 int ret = 0;
913 struct ltt_session *session;
914 struct ltt_kernel_session *ksess;
915 struct ltt_kernel_channel *channel;
916
917 DBG("Updating kernel streams for channel fd %d", fd);
918
919 session_lock_list();
920 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
921 session_lock(session);
922 if (session->kernel_session == NULL) {
923 session_unlock(session);
924 continue;
925 }
926 ksess = session->kernel_session;
927
928 cds_list_for_each_entry(channel,
929 &ksess->channel_list.head, list) {
930 struct lttng_ht_iter iter;
931 struct consumer_socket *socket;
932
933 if (channel->fd != fd) {
934 continue;
935 }
936 DBG("Channel found, updating kernel streams");
937 ret = kernel_open_channel_stream(channel);
938 if (ret < 0) {
939 goto error;
940 }
941 /* Update the stream global counter */
942 ksess->stream_count_global += ret;
943
944 /*
945 * Have we already sent fds to the consumer? If yes, it
946 * means that tracing is started so it is safe to send
947 * our updated stream fds.
948 */
949 if (ksess->consumer_fds_sent != 1
950 || ksess->consumer == NULL) {
951 ret = -1;
952 goto error;
953 }
954
955 rcu_read_lock();
956 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
957 &iter.iter, socket, node.node) {
958 pthread_mutex_lock(socket->lock);
959 ret = kernel_consumer_send_channel_stream(socket,
960 channel, ksess,
961 session->output_traces ? 1 : 0);
962 pthread_mutex_unlock(socket->lock);
963 if (ret < 0) {
964 rcu_read_unlock();
965 goto error;
966 }
967 }
968 rcu_read_unlock();
969 }
970 session_unlock(session);
971 }
972 session_unlock_list();
973 return ret;
974
975 error:
976 session_unlock(session);
977 session_unlock_list();
978 return ret;
979 }
980
981 /*
982 * For each tracing session, update newly registered apps. The session list
983 * lock MUST be acquired before calling this.
984 */
985 static void update_ust_app(int app_sock)
986 {
987 struct ltt_session *sess, *stmp;
988
989 /* Consumer is in an ERROR state. Stop any application update. */
990 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
991 /* Stop the update process since the consumer is dead. */
992 return;
993 }
994
995 /* For all tracing session(s) */
996 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
997 struct ust_app *app;
998
999 session_lock(sess);
1000 if (!sess->ust_session) {
1001 goto unlock_session;
1002 }
1003
1004 rcu_read_lock();
1005 assert(app_sock >= 0);
1006 app = ust_app_find_by_sock(app_sock);
1007 if (app == NULL) {
1008 /*
1009 * Application can be unregistered before so
1010 * this is possible hence simply stopping the
1011 * update.
1012 */
1013 DBG3("UST app update failed to find app sock %d",
1014 app_sock);
1015 goto unlock_rcu;
1016 }
1017 ust_app_global_update(sess->ust_session, app);
1018 unlock_rcu:
1019 rcu_read_unlock();
1020 unlock_session:
1021 session_unlock(sess);
1022 }
1023 }
1024
1025 /*
1026 * This thread manage event coming from the kernel.
1027 *
1028 * Features supported in this thread:
1029 * -) CPU Hotplug
1030 */
1031 static void *thread_manage_kernel(void *data)
1032 {
1033 int ret, i, pollfd, update_poll_flag = 1, err = -1;
1034 uint32_t revents, nb_fd;
1035 char tmp;
1036 struct lttng_poll_event events;
1037
1038 DBG("[thread] Thread manage kernel started");
1039
1040 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
1041
1042 /*
1043 * This first step of the while is to clean this structure which could free
1044 * non NULL pointers so initialize it before the loop.
1045 */
1046 lttng_poll_init(&events);
1047
1048 if (testpoint(sessiond_thread_manage_kernel)) {
1049 goto error_testpoint;
1050 }
1051
1052 health_code_update();
1053
1054 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
1055 goto error_testpoint;
1056 }
1057
1058 while (1) {
1059 health_code_update();
1060
1061 if (update_poll_flag == 1) {
1062 /* Clean events object. We are about to populate it again. */
1063 lttng_poll_clean(&events);
1064
1065 ret = sessiond_set_thread_pollset(&events, 2);
1066 if (ret < 0) {
1067 goto error_poll_create;
1068 }
1069
1070 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
1071 if (ret < 0) {
1072 goto error;
1073 }
1074
1075 /* This will add the available kernel channel if any. */
1076 ret = update_kernel_poll(&events);
1077 if (ret < 0) {
1078 goto error;
1079 }
1080 update_poll_flag = 0;
1081 }
1082
1083 DBG("Thread kernel polling");
1084
1085 /* Poll infinite value of time */
1086 restart:
1087 health_poll_entry();
1088 ret = lttng_poll_wait(&events, -1);
1089 DBG("Thread kernel return from poll on %d fds",
1090 LTTNG_POLL_GETNB(&events));
1091 health_poll_exit();
1092 if (ret < 0) {
1093 /*
1094 * Restart interrupted system call.
1095 */
1096 if (errno == EINTR) {
1097 goto restart;
1098 }
1099 goto error;
1100 } else if (ret == 0) {
1101 /* Should not happen since timeout is infinite */
1102 ERR("Return value of poll is 0 with an infinite timeout.\n"
1103 "This should not have happened! Continuing...");
1104 continue;
1105 }
1106
1107 nb_fd = ret;
1108
1109 for (i = 0; i < nb_fd; i++) {
1110 /* Fetch once the poll data */
1111 revents = LTTNG_POLL_GETEV(&events, i);
1112 pollfd = LTTNG_POLL_GETFD(&events, i);
1113
1114 health_code_update();
1115
1116 if (!revents) {
1117 /* No activity for this FD (poll implementation). */
1118 continue;
1119 }
1120
1121 /* Thread quit pipe has been closed. Killing thread. */
1122 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1123 if (ret) {
1124 err = 0;
1125 goto exit;
1126 }
1127
1128 /* Check for data on kernel pipe */
1129 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
1130 (void) lttng_read(kernel_poll_pipe[0],
1131 &tmp, 1);
1132 /*
1133 * Ret value is useless here, if this pipe gets any actions an
1134 * update is required anyway.
1135 */
1136 update_poll_flag = 1;
1137 continue;
1138 } else {
1139 /*
1140 * New CPU detected by the kernel. Adding kernel stream to
1141 * kernel session and updating the kernel consumer
1142 */
1143 if (revents & LPOLLIN) {
1144 ret = update_kernel_stream(&kconsumer_data, pollfd);
1145 if (ret < 0) {
1146 continue;
1147 }
1148 break;
1149 /*
1150 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1151 * and unregister kernel stream at this point.
1152 */
1153 }
1154 }
1155 }
1156 }
1157
1158 exit:
1159 error:
1160 lttng_poll_clean(&events);
1161 error_poll_create:
1162 error_testpoint:
1163 utils_close_pipe(kernel_poll_pipe);
1164 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
1165 if (err) {
1166 health_error();
1167 ERR("Health error occurred in %s", __func__);
1168 WARN("Kernel thread died unexpectedly. "
1169 "Kernel tracing can continue but CPU hotplug is disabled.");
1170 }
1171 health_unregister(health_sessiond);
1172 DBG("Kernel thread dying");
1173 return NULL;
1174 }
1175
1176 /*
1177 * Signal pthread condition of the consumer data that the thread.
1178 */
1179 static void signal_consumer_condition(struct consumer_data *data, int state)
1180 {
1181 pthread_mutex_lock(&data->cond_mutex);
1182
1183 /*
1184 * The state is set before signaling. It can be any value, it's the waiter
1185 * job to correctly interpret this condition variable associated to the
1186 * consumer pthread_cond.
1187 *
1188 * A value of 0 means that the corresponding thread of the consumer data
1189 * was not started. 1 indicates that the thread has started and is ready
1190 * for action. A negative value means that there was an error during the
1191 * thread bootstrap.
1192 */
1193 data->consumer_thread_is_ready = state;
1194 (void) pthread_cond_signal(&data->cond);
1195
1196 pthread_mutex_unlock(&data->cond_mutex);
1197 }
1198
1199 /*
1200 * This thread manage the consumer error sent back to the session daemon.
1201 */
1202 static void *thread_manage_consumer(void *data)
1203 {
1204 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
1205 uint32_t revents, nb_fd;
1206 enum lttcomm_return_code code;
1207 struct lttng_poll_event events;
1208 struct consumer_data *consumer_data = data;
1209
1210 DBG("[thread] Manage consumer started");
1211
1212 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
1213
1214 health_code_update();
1215
1216 /*
1217 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1218 * metadata_sock. Nothing more will be added to this poll set.
1219 */
1220 ret = sessiond_set_thread_pollset(&events, 3);
1221 if (ret < 0) {
1222 goto error_poll;
1223 }
1224
1225 /*
1226 * The error socket here is already in a listening state which was done
1227 * just before spawning this thread to avoid a race between the consumer
1228 * daemon exec trying to connect and the listen() call.
1229 */
1230 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
1231 if (ret < 0) {
1232 goto error;
1233 }
1234
1235 health_code_update();
1236
1237 /* Infinite blocking call, waiting for transmission */
1238 restart:
1239 health_poll_entry();
1240
1241 if (testpoint(sessiond_thread_manage_consumer)) {
1242 goto error;
1243 }
1244
1245 ret = lttng_poll_wait(&events, -1);
1246 health_poll_exit();
1247 if (ret < 0) {
1248 /*
1249 * Restart interrupted system call.
1250 */
1251 if (errno == EINTR) {
1252 goto restart;
1253 }
1254 goto error;
1255 }
1256
1257 nb_fd = ret;
1258
1259 for (i = 0; i < nb_fd; i++) {
1260 /* Fetch once the poll data */
1261 revents = LTTNG_POLL_GETEV(&events, i);
1262 pollfd = LTTNG_POLL_GETFD(&events, i);
1263
1264 health_code_update();
1265
1266 if (!revents) {
1267 /* No activity for this FD (poll implementation). */
1268 continue;
1269 }
1270
1271 /* Thread quit pipe has been closed. Killing thread. */
1272 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1273 if (ret) {
1274 err = 0;
1275 goto exit;
1276 }
1277
1278 /* Event on the registration socket */
1279 if (pollfd == consumer_data->err_sock) {
1280 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1281 ERR("consumer err socket poll error");
1282 goto error;
1283 }
1284 }
1285 }
1286
1287 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1288 if (sock < 0) {
1289 goto error;
1290 }
1291
1292 /*
1293 * Set the CLOEXEC flag. Return code is useless because either way, the
1294 * show must go on.
1295 */
1296 (void) utils_set_fd_cloexec(sock);
1297
1298 health_code_update();
1299
1300 DBG2("Receiving code from consumer err_sock");
1301
1302 /* Getting status code from kconsumerd */
1303 ret = lttcomm_recv_unix_sock(sock, &code,
1304 sizeof(enum lttcomm_return_code));
1305 if (ret <= 0) {
1306 goto error;
1307 }
1308
1309 health_code_update();
1310 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1311 /* Connect both socket, command and metadata. */
1312 consumer_data->cmd_sock =
1313 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1314 consumer_data->metadata_fd =
1315 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1316 if (consumer_data->cmd_sock < 0
1317 || consumer_data->metadata_fd < 0) {
1318 PERROR("consumer connect cmd socket");
1319 /* On error, signal condition and quit. */
1320 signal_consumer_condition(consumer_data, -1);
1321 goto error;
1322 }
1323 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1324 /* Create metadata socket lock. */
1325 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1326 if (consumer_data->metadata_sock.lock == NULL) {
1327 PERROR("zmalloc pthread mutex");
1328 ret = -1;
1329 goto error;
1330 }
1331 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1332
1333 signal_consumer_condition(consumer_data, 1);
1334 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1335 DBG("Consumer metadata socket ready (fd: %d)",
1336 consumer_data->metadata_fd);
1337 } else {
1338 ERR("consumer error when waiting for SOCK_READY : %s",
1339 lttcomm_get_readable_code(-code));
1340 goto error;
1341 }
1342
1343 /* Remove the consumerd error sock since we've established a connexion */
1344 ret = lttng_poll_del(&events, consumer_data->err_sock);
1345 if (ret < 0) {
1346 goto error;
1347 }
1348
1349 /* Add new accepted error socket. */
1350 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1351 if (ret < 0) {
1352 goto error;
1353 }
1354
1355 /* Add metadata socket that is successfully connected. */
1356 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1357 LPOLLIN | LPOLLRDHUP);
1358 if (ret < 0) {
1359 goto error;
1360 }
1361
1362 health_code_update();
1363
1364 /* Infinite blocking call, waiting for transmission */
1365 restart_poll:
1366 while (1) {
1367 health_code_update();
1368
1369 /* Exit the thread because the thread quit pipe has been triggered. */
1370 if (should_quit) {
1371 /* Not a health error. */
1372 err = 0;
1373 goto exit;
1374 }
1375
1376 health_poll_entry();
1377 ret = lttng_poll_wait(&events, -1);
1378 health_poll_exit();
1379 if (ret < 0) {
1380 /*
1381 * Restart interrupted system call.
1382 */
1383 if (errno == EINTR) {
1384 goto restart_poll;
1385 }
1386 goto error;
1387 }
1388
1389 nb_fd = ret;
1390
1391 for (i = 0; i < nb_fd; i++) {
1392 /* Fetch once the poll data */
1393 revents = LTTNG_POLL_GETEV(&events, i);
1394 pollfd = LTTNG_POLL_GETFD(&events, i);
1395
1396 health_code_update();
1397
1398 if (!revents) {
1399 /* No activity for this FD (poll implementation). */
1400 continue;
1401 }
1402
1403 /*
1404 * Thread quit pipe has been triggered, flag that we should stop
1405 * but continue the current loop to handle potential data from
1406 * consumer.
1407 */
1408 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
1409
1410 if (pollfd == sock) {
1411 /* Event on the consumerd socket */
1412 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1413 ERR("consumer err socket second poll error");
1414 goto error;
1415 }
1416 health_code_update();
1417 /* Wait for any kconsumerd error */
1418 ret = lttcomm_recv_unix_sock(sock, &code,
1419 sizeof(enum lttcomm_return_code));
1420 if (ret <= 0) {
1421 ERR("consumer closed the command socket");
1422 goto error;
1423 }
1424
1425 ERR("consumer return code : %s",
1426 lttcomm_get_readable_code(-code));
1427
1428 goto exit;
1429 } else if (pollfd == consumer_data->metadata_fd) {
1430 /* UST metadata requests */
1431 ret = ust_consumer_metadata_request(
1432 &consumer_data->metadata_sock);
1433 if (ret < 0) {
1434 ERR("Handling metadata request");
1435 goto error;
1436 }
1437 }
1438 /* No need for an else branch all FDs are tested prior. */
1439 }
1440 health_code_update();
1441 }
1442
1443 exit:
1444 error:
1445 /*
1446 * We lock here because we are about to close the sockets and some other
1447 * thread might be using them so get exclusive access which will abort all
1448 * other consumer command by other threads.
1449 */
1450 pthread_mutex_lock(&consumer_data->lock);
1451
1452 /* Immediately set the consumerd state to stopped */
1453 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1454 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1455 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1456 consumer_data->type == LTTNG_CONSUMER32_UST) {
1457 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1458 } else {
1459 /* Code flow error... */
1460 assert(0);
1461 }
1462
1463 if (consumer_data->err_sock >= 0) {
1464 ret = close(consumer_data->err_sock);
1465 if (ret) {
1466 PERROR("close");
1467 }
1468 consumer_data->err_sock = -1;
1469 }
1470 if (consumer_data->cmd_sock >= 0) {
1471 ret = close(consumer_data->cmd_sock);
1472 if (ret) {
1473 PERROR("close");
1474 }
1475 consumer_data->cmd_sock = -1;
1476 }
1477 if (consumer_data->metadata_sock.fd_ptr &&
1478 *consumer_data->metadata_sock.fd_ptr >= 0) {
1479 ret = close(*consumer_data->metadata_sock.fd_ptr);
1480 if (ret) {
1481 PERROR("close");
1482 }
1483 }
1484 if (sock >= 0) {
1485 ret = close(sock);
1486 if (ret) {
1487 PERROR("close");
1488 }
1489 }
1490
1491 unlink(consumer_data->err_unix_sock_path);
1492 unlink(consumer_data->cmd_unix_sock_path);
1493 consumer_data->pid = 0;
1494 pthread_mutex_unlock(&consumer_data->lock);
1495
1496 /* Cleanup metadata socket mutex. */
1497 if (consumer_data->metadata_sock.lock) {
1498 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1499 free(consumer_data->metadata_sock.lock);
1500 }
1501 lttng_poll_clean(&events);
1502 error_poll:
1503 if (err) {
1504 health_error();
1505 ERR("Health error occurred in %s", __func__);
1506 }
1507 health_unregister(health_sessiond);
1508 DBG("consumer thread cleanup completed");
1509
1510 return NULL;
1511 }
1512
1513 /*
1514 * This thread manage application communication.
1515 */
1516 static void *thread_manage_apps(void *data)
1517 {
1518 int i, ret, pollfd, err = -1;
1519 ssize_t size_ret;
1520 uint32_t revents, nb_fd;
1521 struct lttng_poll_event events;
1522
1523 DBG("[thread] Manage application started");
1524
1525 rcu_register_thread();
1526 rcu_thread_online();
1527
1528 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1529
1530 if (testpoint(sessiond_thread_manage_apps)) {
1531 goto error_testpoint;
1532 }
1533
1534 health_code_update();
1535
1536 ret = sessiond_set_thread_pollset(&events, 2);
1537 if (ret < 0) {
1538 goto error_poll_create;
1539 }
1540
1541 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1542 if (ret < 0) {
1543 goto error;
1544 }
1545
1546 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1547 goto error;
1548 }
1549
1550 health_code_update();
1551
1552 while (1) {
1553 DBG("Apps thread polling");
1554
1555 /* Inifinite blocking call, waiting for transmission */
1556 restart:
1557 health_poll_entry();
1558 ret = lttng_poll_wait(&events, -1);
1559 DBG("Apps thread return from poll on %d fds",
1560 LTTNG_POLL_GETNB(&events));
1561 health_poll_exit();
1562 if (ret < 0) {
1563 /*
1564 * Restart interrupted system call.
1565 */
1566 if (errno == EINTR) {
1567 goto restart;
1568 }
1569 goto error;
1570 }
1571
1572 nb_fd = ret;
1573
1574 for (i = 0; i < nb_fd; i++) {
1575 /* Fetch once the poll data */
1576 revents = LTTNG_POLL_GETEV(&events, i);
1577 pollfd = LTTNG_POLL_GETFD(&events, i);
1578
1579 health_code_update();
1580
1581 if (!revents) {
1582 /* No activity for this FD (poll implementation). */
1583 continue;
1584 }
1585
1586 /* Thread quit pipe has been closed. Killing thread. */
1587 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1588 if (ret) {
1589 err = 0;
1590 goto exit;
1591 }
1592
1593 /* Inspect the apps cmd pipe */
1594 if (pollfd == apps_cmd_pipe[0]) {
1595 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1596 ERR("Apps command pipe error");
1597 goto error;
1598 } else if (revents & LPOLLIN) {
1599 int sock;
1600
1601 /* Empty pipe */
1602 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1603 if (size_ret < sizeof(sock)) {
1604 PERROR("read apps cmd pipe");
1605 goto error;
1606 }
1607
1608 health_code_update();
1609
1610 /*
1611 * We only monitor the error events of the socket. This
1612 * thread does not handle any incoming data from UST
1613 * (POLLIN).
1614 */
1615 ret = lttng_poll_add(&events, sock,
1616 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1617 if (ret < 0) {
1618 goto error;
1619 }
1620
1621 DBG("Apps with sock %d added to poll set", sock);
1622 }
1623 } else {
1624 /*
1625 * At this point, we know that a registered application made
1626 * the event at poll_wait.
1627 */
1628 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1629 /* Removing from the poll set */
1630 ret = lttng_poll_del(&events, pollfd);
1631 if (ret < 0) {
1632 goto error;
1633 }
1634
1635 /* Socket closed on remote end. */
1636 ust_app_unregister(pollfd);
1637 }
1638 }
1639
1640 health_code_update();
1641 }
1642 }
1643
1644 exit:
1645 error:
1646 lttng_poll_clean(&events);
1647 error_poll_create:
1648 error_testpoint:
1649 utils_close_pipe(apps_cmd_pipe);
1650 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1651
1652 /*
1653 * We don't clean the UST app hash table here since already registered
1654 * applications can still be controlled so let them be until the session
1655 * daemon dies or the applications stop.
1656 */
1657
1658 if (err) {
1659 health_error();
1660 ERR("Health error occurred in %s", __func__);
1661 }
1662 health_unregister(health_sessiond);
1663 DBG("Application communication apps thread cleanup complete");
1664 rcu_thread_offline();
1665 rcu_unregister_thread();
1666 return NULL;
1667 }
1668
1669 /*
1670 * Send a socket to a thread This is called from the dispatch UST registration
1671 * thread once all sockets are set for the application.
1672 *
1673 * The sock value can be invalid, we don't really care, the thread will handle
1674 * it and make the necessary cleanup if so.
1675 *
1676 * On success, return 0 else a negative value being the errno message of the
1677 * write().
1678 */
1679 static int send_socket_to_thread(int fd, int sock)
1680 {
1681 ssize_t ret;
1682
1683 /*
1684 * It's possible that the FD is set as invalid with -1 concurrently just
1685 * before calling this function being a shutdown state of the thread.
1686 */
1687 if (fd < 0) {
1688 ret = -EBADF;
1689 goto error;
1690 }
1691
1692 ret = lttng_write(fd, &sock, sizeof(sock));
1693 if (ret < sizeof(sock)) {
1694 PERROR("write apps pipe %d", fd);
1695 if (ret < 0) {
1696 ret = -errno;
1697 }
1698 goto error;
1699 }
1700
1701 /* All good. Don't send back the write positive ret value. */
1702 ret = 0;
1703 error:
1704 return (int) ret;
1705 }
1706
1707 /*
1708 * Sanitize the wait queue of the dispatch registration thread meaning removing
1709 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1710 * notify socket is never received.
1711 */
1712 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1713 {
1714 int ret, nb_fd = 0, i;
1715 unsigned int fd_added = 0;
1716 struct lttng_poll_event events;
1717 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1718
1719 assert(wait_queue);
1720
1721 lttng_poll_init(&events);
1722
1723 /* Just skip everything for an empty queue. */
1724 if (!wait_queue->count) {
1725 goto end;
1726 }
1727
1728 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1729 if (ret < 0) {
1730 goto error_create;
1731 }
1732
1733 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1734 &wait_queue->head, head) {
1735 assert(wait_node->app);
1736 ret = lttng_poll_add(&events, wait_node->app->sock,
1737 LPOLLHUP | LPOLLERR);
1738 if (ret < 0) {
1739 goto error;
1740 }
1741
1742 fd_added = 1;
1743 }
1744
1745 if (!fd_added) {
1746 goto end;
1747 }
1748
1749 /*
1750 * Poll but don't block so we can quickly identify the faulty events and
1751 * clean them afterwards from the wait queue.
1752 */
1753 ret = lttng_poll_wait(&events, 0);
1754 if (ret < 0) {
1755 goto error;
1756 }
1757 nb_fd = ret;
1758
1759 for (i = 0; i < nb_fd; i++) {
1760 /* Get faulty FD. */
1761 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1762 int pollfd = LTTNG_POLL_GETFD(&events, i);
1763
1764 if (!revents) {
1765 /* No activity for this FD (poll implementation). */
1766 continue;
1767 }
1768
1769 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1770 &wait_queue->head, head) {
1771 if (pollfd == wait_node->app->sock &&
1772 (revents & (LPOLLHUP | LPOLLERR))) {
1773 cds_list_del(&wait_node->head);
1774 wait_queue->count--;
1775 ust_app_destroy(wait_node->app);
1776 free(wait_node);
1777 break;
1778 }
1779 }
1780 }
1781
1782 if (nb_fd > 0) {
1783 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1784 }
1785
1786 end:
1787 lttng_poll_clean(&events);
1788 return;
1789
1790 error:
1791 lttng_poll_clean(&events);
1792 error_create:
1793 ERR("Unable to sanitize wait queue");
1794 return;
1795 }
1796
1797 /*
1798 * Dispatch request from the registration threads to the application
1799 * communication thread.
1800 */
1801 static void *thread_dispatch_ust_registration(void *data)
1802 {
1803 int ret, err = -1;
1804 struct cds_wfcq_node *node;
1805 struct ust_command *ust_cmd = NULL;
1806 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1807 struct ust_reg_wait_queue wait_queue = {
1808 .count = 0,
1809 };
1810
1811 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1812
1813 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1814 goto error_testpoint;
1815 }
1816
1817 health_code_update();
1818
1819 CDS_INIT_LIST_HEAD(&wait_queue.head);
1820
1821 DBG("[thread] Dispatch UST command started");
1822
1823 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1824 health_code_update();
1825
1826 /* Atomically prepare the queue futex */
1827 futex_nto1_prepare(&ust_cmd_queue.futex);
1828
1829 do {
1830 struct ust_app *app = NULL;
1831 ust_cmd = NULL;
1832
1833 /*
1834 * Make sure we don't have node(s) that have hung up before receiving
1835 * the notify socket. This is to clean the list in order to avoid
1836 * memory leaks from notify socket that are never seen.
1837 */
1838 sanitize_wait_queue(&wait_queue);
1839
1840 health_code_update();
1841 /* Dequeue command for registration */
1842 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1843 if (node == NULL) {
1844 DBG("Woken up but nothing in the UST command queue");
1845 /* Continue thread execution */
1846 break;
1847 }
1848
1849 ust_cmd = caa_container_of(node, struct ust_command, node);
1850
1851 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1852 " gid:%d sock:%d name:%s (version %d.%d)",
1853 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1854 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1855 ust_cmd->sock, ust_cmd->reg_msg.name,
1856 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1857
1858 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1859 wait_node = zmalloc(sizeof(*wait_node));
1860 if (!wait_node) {
1861 PERROR("zmalloc wait_node dispatch");
1862 ret = close(ust_cmd->sock);
1863 if (ret < 0) {
1864 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1865 }
1866 lttng_fd_put(LTTNG_FD_APPS, 1);
1867 free(ust_cmd);
1868 goto error;
1869 }
1870 CDS_INIT_LIST_HEAD(&wait_node->head);
1871
1872 /* Create application object if socket is CMD. */
1873 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1874 ust_cmd->sock);
1875 if (!wait_node->app) {
1876 ret = close(ust_cmd->sock);
1877 if (ret < 0) {
1878 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1879 }
1880 lttng_fd_put(LTTNG_FD_APPS, 1);
1881 free(wait_node);
1882 free(ust_cmd);
1883 continue;
1884 }
1885 /*
1886 * Add application to the wait queue so we can set the notify
1887 * socket before putting this object in the global ht.
1888 */
1889 cds_list_add(&wait_node->head, &wait_queue.head);
1890 wait_queue.count++;
1891
1892 free(ust_cmd);
1893 /*
1894 * We have to continue here since we don't have the notify
1895 * socket and the application MUST be added to the hash table
1896 * only at that moment.
1897 */
1898 continue;
1899 } else {
1900 /*
1901 * Look for the application in the local wait queue and set the
1902 * notify socket if found.
1903 */
1904 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1905 &wait_queue.head, head) {
1906 health_code_update();
1907 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1908 wait_node->app->notify_sock = ust_cmd->sock;
1909 cds_list_del(&wait_node->head);
1910 wait_queue.count--;
1911 app = wait_node->app;
1912 free(wait_node);
1913 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1914 break;
1915 }
1916 }
1917
1918 /*
1919 * With no application at this stage the received socket is
1920 * basically useless so close it before we free the cmd data
1921 * structure for good.
1922 */
1923 if (!app) {
1924 ret = close(ust_cmd->sock);
1925 if (ret < 0) {
1926 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1927 }
1928 lttng_fd_put(LTTNG_FD_APPS, 1);
1929 }
1930 free(ust_cmd);
1931 }
1932
1933 if (app) {
1934 /*
1935 * @session_lock_list
1936 *
1937 * Lock the global session list so from the register up to the
1938 * registration done message, no thread can see the application
1939 * and change its state.
1940 */
1941 session_lock_list();
1942 rcu_read_lock();
1943
1944 /*
1945 * Add application to the global hash table. This needs to be
1946 * done before the update to the UST registry can locate the
1947 * application.
1948 */
1949 ust_app_add(app);
1950
1951 /* Set app version. This call will print an error if needed. */
1952 (void) ust_app_version(app);
1953
1954 /* Send notify socket through the notify pipe. */
1955 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1956 app->notify_sock);
1957 if (ret < 0) {
1958 rcu_read_unlock();
1959 session_unlock_list();
1960 /*
1961 * No notify thread, stop the UST tracing. However, this is
1962 * not an internal error of the this thread thus setting
1963 * the health error code to a normal exit.
1964 */
1965 err = 0;
1966 goto error;
1967 }
1968
1969 /*
1970 * Update newly registered application with the tracing
1971 * registry info already enabled information.
1972 */
1973 update_ust_app(app->sock);
1974
1975 /*
1976 * Don't care about return value. Let the manage apps threads
1977 * handle app unregistration upon socket close.
1978 */
1979 (void) ust_app_register_done(app->sock);
1980
1981 /*
1982 * Even if the application socket has been closed, send the app
1983 * to the thread and unregistration will take place at that
1984 * place.
1985 */
1986 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
1987 if (ret < 0) {
1988 rcu_read_unlock();
1989 session_unlock_list();
1990 /*
1991 * No apps. thread, stop the UST tracing. However, this is
1992 * not an internal error of the this thread thus setting
1993 * the health error code to a normal exit.
1994 */
1995 err = 0;
1996 goto error;
1997 }
1998
1999 rcu_read_unlock();
2000 session_unlock_list();
2001 }
2002 } while (node != NULL);
2003
2004 health_poll_entry();
2005 /* Futex wait on queue. Blocking call on futex() */
2006 futex_nto1_wait(&ust_cmd_queue.futex);
2007 health_poll_exit();
2008 }
2009 /* Normal exit, no error */
2010 err = 0;
2011
2012 error:
2013 /* Clean up wait queue. */
2014 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
2015 &wait_queue.head, head) {
2016 cds_list_del(&wait_node->head);
2017 wait_queue.count--;
2018 free(wait_node);
2019 }
2020
2021 error_testpoint:
2022 DBG("Dispatch thread dying");
2023 if (err) {
2024 health_error();
2025 ERR("Health error occurred in %s", __func__);
2026 }
2027 health_unregister(health_sessiond);
2028 return NULL;
2029 }
2030
2031 /*
2032 * This thread manage application registration.
2033 */
2034 static void *thread_registration_apps(void *data)
2035 {
2036 int sock = -1, i, ret, pollfd, err = -1;
2037 uint32_t revents, nb_fd;
2038 struct lttng_poll_event events;
2039 /*
2040 * Get allocated in this thread, enqueued to a global queue, dequeued and
2041 * freed in the manage apps thread.
2042 */
2043 struct ust_command *ust_cmd = NULL;
2044
2045 DBG("[thread] Manage application registration started");
2046
2047 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
2048
2049 if (testpoint(sessiond_thread_registration_apps)) {
2050 goto error_testpoint;
2051 }
2052
2053 ret = lttcomm_listen_unix_sock(apps_sock);
2054 if (ret < 0) {
2055 goto error_listen;
2056 }
2057
2058 /*
2059 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2060 * more will be added to this poll set.
2061 */
2062 ret = sessiond_set_thread_pollset(&events, 2);
2063 if (ret < 0) {
2064 goto error_create_poll;
2065 }
2066
2067 /* Add the application registration socket */
2068 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
2069 if (ret < 0) {
2070 goto error_poll_add;
2071 }
2072
2073 /* Notify all applications to register */
2074 ret = notify_ust_apps(1);
2075 if (ret < 0) {
2076 ERR("Failed to notify applications or create the wait shared memory.\n"
2077 "Execution continues but there might be problem for already\n"
2078 "running applications that wishes to register.");
2079 }
2080
2081 while (1) {
2082 DBG("Accepting application registration");
2083
2084 /* Inifinite blocking call, waiting for transmission */
2085 restart:
2086 health_poll_entry();
2087 ret = lttng_poll_wait(&events, -1);
2088 health_poll_exit();
2089 if (ret < 0) {
2090 /*
2091 * Restart interrupted system call.
2092 */
2093 if (errno == EINTR) {
2094 goto restart;
2095 }
2096 goto error;
2097 }
2098
2099 nb_fd = ret;
2100
2101 for (i = 0; i < nb_fd; i++) {
2102 health_code_update();
2103
2104 /* Fetch once the poll data */
2105 revents = LTTNG_POLL_GETEV(&events, i);
2106 pollfd = LTTNG_POLL_GETFD(&events, i);
2107
2108 if (!revents) {
2109 /* No activity for this FD (poll implementation). */
2110 continue;
2111 }
2112
2113 /* Thread quit pipe has been closed. Killing thread. */
2114 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
2115 if (ret) {
2116 err = 0;
2117 goto exit;
2118 }
2119
2120 /* Event on the registration socket */
2121 if (pollfd == apps_sock) {
2122 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2123 ERR("Register apps socket poll error");
2124 goto error;
2125 } else if (revents & LPOLLIN) {
2126 sock = lttcomm_accept_unix_sock(apps_sock);
2127 if (sock < 0) {
2128 goto error;
2129 }
2130
2131 /*
2132 * Set socket timeout for both receiving and ending.
2133 * app_socket_timeout is in seconds, whereas
2134 * lttcomm_setsockopt_rcv_timeout and
2135 * lttcomm_setsockopt_snd_timeout expect msec as
2136 * parameter.
2137 */
2138 (void) lttcomm_setsockopt_rcv_timeout(sock,
2139 app_socket_timeout * 1000);
2140 (void) lttcomm_setsockopt_snd_timeout(sock,
2141 app_socket_timeout * 1000);
2142
2143 /*
2144 * Set the CLOEXEC flag. Return code is useless because
2145 * either way, the show must go on.
2146 */
2147 (void) utils_set_fd_cloexec(sock);
2148
2149 /* Create UST registration command for enqueuing */
2150 ust_cmd = zmalloc(sizeof(struct ust_command));
2151 if (ust_cmd == NULL) {
2152 PERROR("ust command zmalloc");
2153 goto error;
2154 }
2155
2156 /*
2157 * Using message-based transmissions to ensure we don't
2158 * have to deal with partially received messages.
2159 */
2160 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2161 if (ret < 0) {
2162 ERR("Exhausted file descriptors allowed for applications.");
2163 free(ust_cmd);
2164 ret = close(sock);
2165 if (ret) {
2166 PERROR("close");
2167 }
2168 sock = -1;
2169 continue;
2170 }
2171
2172 health_code_update();
2173 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2174 if (ret < 0) {
2175 free(ust_cmd);
2176 /* Close socket of the application. */
2177 ret = close(sock);
2178 if (ret) {
2179 PERROR("close");
2180 }
2181 lttng_fd_put(LTTNG_FD_APPS, 1);
2182 sock = -1;
2183 continue;
2184 }
2185 health_code_update();
2186
2187 ust_cmd->sock = sock;
2188 sock = -1;
2189
2190 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2191 " gid:%d sock:%d name:%s (version %d.%d)",
2192 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2193 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2194 ust_cmd->sock, ust_cmd->reg_msg.name,
2195 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
2196
2197 /*
2198 * Lock free enqueue the registration request. The red pill
2199 * has been taken! This apps will be part of the *system*.
2200 */
2201 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
2202
2203 /*
2204 * Wake the registration queue futex. Implicit memory
2205 * barrier with the exchange in cds_wfcq_enqueue.
2206 */
2207 futex_nto1_wake(&ust_cmd_queue.futex);
2208 }
2209 }
2210 }
2211 }
2212
2213 exit:
2214 error:
2215 /* Notify that the registration thread is gone */
2216 notify_ust_apps(0);
2217
2218 if (apps_sock >= 0) {
2219 ret = close(apps_sock);
2220 if (ret) {
2221 PERROR("close");
2222 }
2223 }
2224 if (sock >= 0) {
2225 ret = close(sock);
2226 if (ret) {
2227 PERROR("close");
2228 }
2229 lttng_fd_put(LTTNG_FD_APPS, 1);
2230 }
2231 unlink(apps_unix_sock_path);
2232
2233 error_poll_add:
2234 lttng_poll_clean(&events);
2235 error_listen:
2236 error_create_poll:
2237 error_testpoint:
2238 DBG("UST Registration thread cleanup complete");
2239 if (err) {
2240 health_error();
2241 ERR("Health error occurred in %s", __func__);
2242 }
2243 health_unregister(health_sessiond);
2244
2245 return NULL;
2246 }
2247
2248 /*
2249 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2250 * exec or it will fails.
2251 */
2252 static int spawn_consumer_thread(struct consumer_data *consumer_data)
2253 {
2254 int ret, clock_ret;
2255 struct timespec timeout;
2256
2257 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2258 consumer_data->consumer_thread_is_ready = 0;
2259
2260 /* Setup pthread condition */
2261 ret = pthread_condattr_init(&consumer_data->condattr);
2262 if (ret) {
2263 errno = ret;
2264 PERROR("pthread_condattr_init consumer data");
2265 goto error;
2266 }
2267
2268 /*
2269 * Set the monotonic clock in order to make sure we DO NOT jump in time
2270 * between the clock_gettime() call and the timedwait call. See bug #324
2271 * for a more details and how we noticed it.
2272 */
2273 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2274 if (ret) {
2275 errno = ret;
2276 PERROR("pthread_condattr_setclock consumer data");
2277 goto error;
2278 }
2279
2280 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2281 if (ret) {
2282 errno = ret;
2283 PERROR("pthread_cond_init consumer data");
2284 goto error;
2285 }
2286
2287 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2288 consumer_data);
2289 if (ret) {
2290 errno = ret;
2291 PERROR("pthread_create consumer");
2292 ret = -1;
2293 goto error;
2294 }
2295
2296 /* We are about to wait on a pthread condition */
2297 pthread_mutex_lock(&consumer_data->cond_mutex);
2298
2299 /* Get time for sem_timedwait absolute timeout */
2300 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2301 /*
2302 * Set the timeout for the condition timed wait even if the clock gettime
2303 * call fails since we might loop on that call and we want to avoid to
2304 * increment the timeout too many times.
2305 */
2306 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2307
2308 /*
2309 * The following loop COULD be skipped in some conditions so this is why we
2310 * set ret to 0 in order to make sure at least one round of the loop is
2311 * done.
2312 */
2313 ret = 0;
2314
2315 /*
2316 * Loop until the condition is reached or when a timeout is reached. Note
2317 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2318 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2319 * possible. This loop does not take any chances and works with both of
2320 * them.
2321 */
2322 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2323 if (clock_ret < 0) {
2324 PERROR("clock_gettime spawn consumer");
2325 /* Infinite wait for the consumerd thread to be ready */
2326 ret = pthread_cond_wait(&consumer_data->cond,
2327 &consumer_data->cond_mutex);
2328 } else {
2329 ret = pthread_cond_timedwait(&consumer_data->cond,
2330 &consumer_data->cond_mutex, &timeout);
2331 }
2332 }
2333
2334 /* Release the pthread condition */
2335 pthread_mutex_unlock(&consumer_data->cond_mutex);
2336
2337 if (ret != 0) {
2338 errno = ret;
2339 if (ret == ETIMEDOUT) {
2340 int pth_ret;
2341
2342 /*
2343 * Call has timed out so we kill the kconsumerd_thread and return
2344 * an error.
2345 */
2346 ERR("Condition timed out. The consumer thread was never ready."
2347 " Killing it");
2348 pth_ret = pthread_cancel(consumer_data->thread);
2349 if (pth_ret < 0) {
2350 PERROR("pthread_cancel consumer thread");
2351 }
2352 } else {
2353 PERROR("pthread_cond_wait failed consumer thread");
2354 }
2355 /* Caller is expecting a negative value on failure. */
2356 ret = -1;
2357 goto error;
2358 }
2359
2360 pthread_mutex_lock(&consumer_data->pid_mutex);
2361 if (consumer_data->pid == 0) {
2362 ERR("Consumerd did not start");
2363 pthread_mutex_unlock(&consumer_data->pid_mutex);
2364 goto error;
2365 }
2366 pthread_mutex_unlock(&consumer_data->pid_mutex);
2367
2368 return 0;
2369
2370 error:
2371 return ret;
2372 }
2373
2374 /*
2375 * Join consumer thread
2376 */
2377 static int join_consumer_thread(struct consumer_data *consumer_data)
2378 {
2379 void *status;
2380
2381 /* Consumer pid must be a real one. */
2382 if (consumer_data->pid > 0) {
2383 int ret;
2384 ret = kill(consumer_data->pid, SIGTERM);
2385 if (ret) {
2386 PERROR("Error killing consumer daemon");
2387 return ret;
2388 }
2389 return pthread_join(consumer_data->thread, &status);
2390 } else {
2391 return 0;
2392 }
2393 }
2394
2395 /*
2396 * Fork and exec a consumer daemon (consumerd).
2397 *
2398 * Return pid if successful else -1.
2399 */
2400 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2401 {
2402 int ret;
2403 pid_t pid;
2404 const char *consumer_to_use;
2405 const char *verbosity;
2406 struct stat st;
2407
2408 DBG("Spawning consumerd");
2409
2410 pid = fork();
2411 if (pid == 0) {
2412 /*
2413 * Exec consumerd.
2414 */
2415 if (opt_verbose_consumer) {
2416 verbosity = "--verbose";
2417 } else if (lttng_opt_quiet) {
2418 verbosity = "--quiet";
2419 } else {
2420 verbosity = "";
2421 }
2422
2423 switch (consumer_data->type) {
2424 case LTTNG_CONSUMER_KERNEL:
2425 /*
2426 * Find out which consumerd to execute. We will first try the
2427 * 64-bit path, then the sessiond's installation directory, and
2428 * fallback on the 32-bit one,
2429 */
2430 DBG3("Looking for a kernel consumer at these locations:");
2431 DBG3(" 1) %s", consumerd64_bin);
2432 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2433 DBG3(" 3) %s", consumerd32_bin);
2434 if (stat(consumerd64_bin, &st) == 0) {
2435 DBG3("Found location #1");
2436 consumer_to_use = consumerd64_bin;
2437 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
2438 DBG3("Found location #2");
2439 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
2440 } else if (stat(consumerd32_bin, &st) == 0) {
2441 DBG3("Found location #3");
2442 consumer_to_use = consumerd32_bin;
2443 } else {
2444 DBG("Could not find any valid consumerd executable");
2445 ret = -EINVAL;
2446 break;
2447 }
2448 DBG("Using kernel consumer at: %s", consumer_to_use);
2449 ret = execl(consumer_to_use,
2450 "lttng-consumerd", verbosity, "-k",
2451 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2452 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2453 "--group", tracing_group_name,
2454 NULL);
2455 break;
2456 case LTTNG_CONSUMER64_UST:
2457 {
2458 char *tmpnew = NULL;
2459
2460 if (consumerd64_libdir[0] != '\0') {
2461 char *tmp;
2462 size_t tmplen;
2463
2464 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2465 if (!tmp) {
2466 tmp = "";
2467 }
2468 tmplen = strlen("LD_LIBRARY_PATH=")
2469 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2470 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2471 if (!tmpnew) {
2472 ret = -ENOMEM;
2473 goto error;
2474 }
2475 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2476 strcat(tmpnew, consumerd64_libdir);
2477 if (tmp[0] != '\0') {
2478 strcat(tmpnew, ":");
2479 strcat(tmpnew, tmp);
2480 }
2481 ret = putenv(tmpnew);
2482 if (ret) {
2483 ret = -errno;
2484 free(tmpnew);
2485 goto error;
2486 }
2487 }
2488 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
2489 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
2490 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2491 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2492 "--group", tracing_group_name,
2493 NULL);
2494 if (consumerd64_libdir[0] != '\0') {
2495 free(tmpnew);
2496 }
2497 break;
2498 }
2499 case LTTNG_CONSUMER32_UST:
2500 {
2501 char *tmpnew = NULL;
2502
2503 if (consumerd32_libdir[0] != '\0') {
2504 char *tmp;
2505 size_t tmplen;
2506
2507 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2508 if (!tmp) {
2509 tmp = "";
2510 }
2511 tmplen = strlen("LD_LIBRARY_PATH=")
2512 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2513 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2514 if (!tmpnew) {
2515 ret = -ENOMEM;
2516 goto error;
2517 }
2518 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2519 strcat(tmpnew, consumerd32_libdir);
2520 if (tmp[0] != '\0') {
2521 strcat(tmpnew, ":");
2522 strcat(tmpnew, tmp);
2523 }
2524 ret = putenv(tmpnew);
2525 if (ret) {
2526 ret = -errno;
2527 free(tmpnew);
2528 goto error;
2529 }
2530 }
2531 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
2532 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
2533 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2534 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2535 "--group", tracing_group_name,
2536 NULL);
2537 if (consumerd32_libdir[0] != '\0') {
2538 free(tmpnew);
2539 }
2540 break;
2541 }
2542 default:
2543 PERROR("unknown consumer type");
2544 exit(EXIT_FAILURE);
2545 }
2546 if (errno != 0) {
2547 PERROR("Consumer execl()");
2548 }
2549 /* Reaching this point, we got a failure on our execl(). */
2550 exit(EXIT_FAILURE);
2551 } else if (pid > 0) {
2552 ret = pid;
2553 } else {
2554 PERROR("start consumer fork");
2555 ret = -errno;
2556 }
2557 error:
2558 return ret;
2559 }
2560
2561 /*
2562 * Spawn the consumerd daemon and session daemon thread.
2563 */
2564 static int start_consumerd(struct consumer_data *consumer_data)
2565 {
2566 int ret;
2567
2568 /*
2569 * Set the listen() state on the socket since there is a possible race
2570 * between the exec() of the consumer daemon and this call if place in the
2571 * consumer thread. See bug #366 for more details.
2572 */
2573 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2574 if (ret < 0) {
2575 goto error;
2576 }
2577
2578 pthread_mutex_lock(&consumer_data->pid_mutex);
2579 if (consumer_data->pid != 0) {
2580 pthread_mutex_unlock(&consumer_data->pid_mutex);
2581 goto end;
2582 }
2583
2584 ret = spawn_consumerd(consumer_data);
2585 if (ret < 0) {
2586 ERR("Spawning consumerd failed");
2587 pthread_mutex_unlock(&consumer_data->pid_mutex);
2588 goto error;
2589 }
2590
2591 /* Setting up the consumer_data pid */
2592 consumer_data->pid = ret;
2593 DBG2("Consumer pid %d", consumer_data->pid);
2594 pthread_mutex_unlock(&consumer_data->pid_mutex);
2595
2596 DBG2("Spawning consumer control thread");
2597 ret = spawn_consumer_thread(consumer_data);
2598 if (ret < 0) {
2599 ERR("Fatal error spawning consumer control thread");
2600 goto error;
2601 }
2602
2603 end:
2604 return 0;
2605
2606 error:
2607 /* Cleanup already created sockets on error. */
2608 if (consumer_data->err_sock >= 0) {
2609 int err;
2610
2611 err = close(consumer_data->err_sock);
2612 if (err < 0) {
2613 PERROR("close consumer data error socket");
2614 }
2615 }
2616 return ret;
2617 }
2618
2619 /*
2620 * Setup necessary data for kernel tracer action.
2621 */
2622 static int init_kernel_tracer(void)
2623 {
2624 int ret;
2625
2626 /* Modprobe lttng kernel modules */
2627 ret = modprobe_lttng_control();
2628 if (ret < 0) {
2629 goto error;
2630 }
2631
2632 /* Open debugfs lttng */
2633 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2634 if (kernel_tracer_fd < 0) {
2635 DBG("Failed to open %s", module_proc_lttng);
2636 ret = -1;
2637 goto error_open;
2638 }
2639
2640 /* Validate kernel version */
2641 ret = kernel_validate_version(kernel_tracer_fd);
2642 if (ret < 0) {
2643 goto error_version;
2644 }
2645
2646 ret = modprobe_lttng_data();
2647 if (ret < 0) {
2648 goto error_modules;
2649 }
2650
2651 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2652 return 0;
2653
2654 error_version:
2655 modprobe_remove_lttng_control();
2656 ret = close(kernel_tracer_fd);
2657 if (ret) {
2658 PERROR("close");
2659 }
2660 kernel_tracer_fd = -1;
2661 return LTTNG_ERR_KERN_VERSION;
2662
2663 error_modules:
2664 ret = close(kernel_tracer_fd);
2665 if (ret) {
2666 PERROR("close");
2667 }
2668
2669 error_open:
2670 modprobe_remove_lttng_control();
2671
2672 error:
2673 WARN("No kernel tracer available");
2674 kernel_tracer_fd = -1;
2675 if (!is_root) {
2676 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2677 } else {
2678 return LTTNG_ERR_KERN_NA;
2679 }
2680 }
2681
2682
2683 /*
2684 * Copy consumer output from the tracing session to the domain session. The
2685 * function also applies the right modification on a per domain basis for the
2686 * trace files destination directory.
2687 *
2688 * Should *NOT* be called with RCU read-side lock held.
2689 */
2690 static int copy_session_consumer(int domain, struct ltt_session *session)
2691 {
2692 int ret;
2693 const char *dir_name;
2694 struct consumer_output *consumer;
2695
2696 assert(session);
2697 assert(session->consumer);
2698
2699 switch (domain) {
2700 case LTTNG_DOMAIN_KERNEL:
2701 DBG3("Copying tracing session consumer output in kernel session");
2702 /*
2703 * XXX: We should audit the session creation and what this function
2704 * does "extra" in order to avoid a destroy since this function is used
2705 * in the domain session creation (kernel and ust) only. Same for UST
2706 * domain.
2707 */
2708 if (session->kernel_session->consumer) {
2709 consumer_destroy_output(session->kernel_session->consumer);
2710 }
2711 session->kernel_session->consumer =
2712 consumer_copy_output(session->consumer);
2713 /* Ease our life a bit for the next part */
2714 consumer = session->kernel_session->consumer;
2715 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2716 break;
2717 case LTTNG_DOMAIN_JUL:
2718 case LTTNG_DOMAIN_LOG4J:
2719 case LTTNG_DOMAIN_PYTHON:
2720 case LTTNG_DOMAIN_UST:
2721 DBG3("Copying tracing session consumer output in UST session");
2722 if (session->ust_session->consumer) {
2723 consumer_destroy_output(session->ust_session->consumer);
2724 }
2725 session->ust_session->consumer =
2726 consumer_copy_output(session->consumer);
2727 /* Ease our life a bit for the next part */
2728 consumer = session->ust_session->consumer;
2729 dir_name = DEFAULT_UST_TRACE_DIR;
2730 break;
2731 default:
2732 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2733 goto error;
2734 }
2735
2736 /* Append correct directory to subdir */
2737 strncat(consumer->subdir, dir_name,
2738 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2739 DBG3("Copy session consumer subdir %s", consumer->subdir);
2740
2741 ret = LTTNG_OK;
2742
2743 error:
2744 return ret;
2745 }
2746
2747 /*
2748 * Create an UST session and add it to the session ust list.
2749 *
2750 * Should *NOT* be called with RCU read-side lock held.
2751 */
2752 static int create_ust_session(struct ltt_session *session,
2753 struct lttng_domain *domain)
2754 {
2755 int ret;
2756 struct ltt_ust_session *lus = NULL;
2757
2758 assert(session);
2759 assert(domain);
2760 assert(session->consumer);
2761
2762 switch (domain->type) {
2763 case LTTNG_DOMAIN_JUL:
2764 case LTTNG_DOMAIN_LOG4J:
2765 case LTTNG_DOMAIN_PYTHON:
2766 case LTTNG_DOMAIN_UST:
2767 break;
2768 default:
2769 ERR("Unknown UST domain on create session %d", domain->type);
2770 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2771 goto error;
2772 }
2773
2774 DBG("Creating UST session");
2775
2776 lus = trace_ust_create_session(session->id);
2777 if (lus == NULL) {
2778 ret = LTTNG_ERR_UST_SESS_FAIL;
2779 goto error;
2780 }
2781
2782 lus->uid = session->uid;
2783 lus->gid = session->gid;
2784 lus->output_traces = session->output_traces;
2785 lus->snapshot_mode = session->snapshot_mode;
2786 lus->live_timer_interval = session->live_timer;
2787 session->ust_session = lus;
2788 if (session->shm_path[0]) {
2789 strncpy(lus->root_shm_path, session->shm_path,
2790 sizeof(lus->root_shm_path));
2791 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
2792 strncpy(lus->shm_path, session->shm_path,
2793 sizeof(lus->shm_path));
2794 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2795 strncat(lus->shm_path, "/ust",
2796 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2797 }
2798 /* Copy session output to the newly created UST session */
2799 ret = copy_session_consumer(domain->type, session);
2800 if (ret != LTTNG_OK) {
2801 goto error;
2802 }
2803
2804 return LTTNG_OK;
2805
2806 error:
2807 free(lus);
2808 session->ust_session = NULL;
2809 return ret;
2810 }
2811
2812 /*
2813 * Create a kernel tracer session then create the default channel.
2814 */
2815 static int create_kernel_session(struct ltt_session *session)
2816 {
2817 int ret;
2818
2819 DBG("Creating kernel session");
2820
2821 ret = kernel_create_session(session, kernel_tracer_fd);
2822 if (ret < 0) {
2823 ret = LTTNG_ERR_KERN_SESS_FAIL;
2824 goto error;
2825 }
2826
2827 /* Code flow safety */
2828 assert(session->kernel_session);
2829
2830 /* Copy session output to the newly created Kernel session */
2831 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2832 if (ret != LTTNG_OK) {
2833 goto error;
2834 }
2835
2836 /* Create directory(ies) on local filesystem. */
2837 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2838 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2839 ret = run_as_mkdir_recursive(
2840 session->kernel_session->consumer->dst.trace_path,
2841 S_IRWXU | S_IRWXG, session->uid, session->gid);
2842 if (ret < 0) {
2843 if (ret != -EEXIST) {
2844 ERR("Trace directory creation error");
2845 goto error;
2846 }
2847 }
2848 }
2849
2850 session->kernel_session->uid = session->uid;
2851 session->kernel_session->gid = session->gid;
2852 session->kernel_session->output_traces = session->output_traces;
2853 session->kernel_session->snapshot_mode = session->snapshot_mode;
2854
2855 return LTTNG_OK;
2856
2857 error:
2858 trace_kernel_destroy_session(session->kernel_session);
2859 session->kernel_session = NULL;
2860 return ret;
2861 }
2862
2863 /*
2864 * Count number of session permitted by uid/gid.
2865 */
2866 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2867 {
2868 unsigned int i = 0;
2869 struct ltt_session *session;
2870
2871 DBG("Counting number of available session for UID %d GID %d",
2872 uid, gid);
2873 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2874 /*
2875 * Only list the sessions the user can control.
2876 */
2877 if (!session_access_ok(session, uid, gid)) {
2878 continue;
2879 }
2880 i++;
2881 }
2882 return i;
2883 }
2884
2885 /*
2886 * Process the command requested by the lttng client within the command
2887 * context structure. This function make sure that the return structure (llm)
2888 * is set and ready for transmission before returning.
2889 *
2890 * Return any error encountered or 0 for success.
2891 *
2892 * "sock" is only used for special-case var. len data.
2893 *
2894 * Should *NOT* be called with RCU read-side lock held.
2895 */
2896 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2897 int *sock_error)
2898 {
2899 int ret = LTTNG_OK;
2900 int need_tracing_session = 1;
2901 int need_domain;
2902
2903 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2904
2905 *sock_error = 0;
2906
2907 switch (cmd_ctx->lsm->cmd_type) {
2908 case LTTNG_CREATE_SESSION:
2909 case LTTNG_CREATE_SESSION_SNAPSHOT:
2910 case LTTNG_CREATE_SESSION_LIVE:
2911 case LTTNG_DESTROY_SESSION:
2912 case LTTNG_LIST_SESSIONS:
2913 case LTTNG_LIST_DOMAINS:
2914 case LTTNG_START_TRACE:
2915 case LTTNG_STOP_TRACE:
2916 case LTTNG_DATA_PENDING:
2917 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2918 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2919 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2920 case LTTNG_SNAPSHOT_RECORD:
2921 case LTTNG_SAVE_SESSION:
2922 case LTTNG_SET_SESSION_SHM_PATH:
2923 need_domain = 0;
2924 break;
2925 default:
2926 need_domain = 1;
2927 }
2928
2929 if (opt_no_kernel && need_domain
2930 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2931 if (!is_root) {
2932 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2933 } else {
2934 ret = LTTNG_ERR_KERN_NA;
2935 }
2936 goto error;
2937 }
2938
2939 /* Deny register consumer if we already have a spawned consumer. */
2940 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2941 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2942 if (kconsumer_data.pid > 0) {
2943 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2944 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2945 goto error;
2946 }
2947 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2948 }
2949
2950 /*
2951 * Check for command that don't needs to allocate a returned payload. We do
2952 * this here so we don't have to make the call for no payload at each
2953 * command.
2954 */
2955 switch(cmd_ctx->lsm->cmd_type) {
2956 case LTTNG_LIST_SESSIONS:
2957 case LTTNG_LIST_TRACEPOINTS:
2958 case LTTNG_LIST_TRACEPOINT_FIELDS:
2959 case LTTNG_LIST_DOMAINS:
2960 case LTTNG_LIST_CHANNELS:
2961 case LTTNG_LIST_EVENTS:
2962 case LTTNG_LIST_SYSCALLS:
2963 case LTTNG_LIST_TRACKER_PIDS:
2964 break;
2965 default:
2966 /* Setup lttng message with no payload */
2967 ret = setup_lttng_msg(cmd_ctx, 0);
2968 if (ret < 0) {
2969 /* This label does not try to unlock the session */
2970 goto init_setup_error;
2971 }
2972 }
2973
2974 /* Commands that DO NOT need a session. */
2975 switch (cmd_ctx->lsm->cmd_type) {
2976 case LTTNG_CREATE_SESSION:
2977 case LTTNG_CREATE_SESSION_SNAPSHOT:
2978 case LTTNG_CREATE_SESSION_LIVE:
2979 case LTTNG_CALIBRATE:
2980 case LTTNG_LIST_SESSIONS:
2981 case LTTNG_LIST_TRACEPOINTS:
2982 case LTTNG_LIST_SYSCALLS:
2983 case LTTNG_LIST_TRACEPOINT_FIELDS:
2984 case LTTNG_SAVE_SESSION:
2985 need_tracing_session = 0;
2986 break;
2987 default:
2988 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2989 /*
2990 * We keep the session list lock across _all_ commands
2991 * for now, because the per-session lock does not
2992 * handle teardown properly.
2993 */
2994 session_lock_list();
2995 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2996 if (cmd_ctx->session == NULL) {
2997 ret = LTTNG_ERR_SESS_NOT_FOUND;
2998 goto error;
2999 } else {
3000 /* Acquire lock for the session */
3001 session_lock(cmd_ctx->session);
3002 }
3003 break;
3004 }
3005
3006 /*
3007 * Commands that need a valid session but should NOT create one if none
3008 * exists. Instead of creating one and destroying it when the command is
3009 * handled, process that right before so we save some round trip in useless
3010 * code path.
3011 */
3012 switch (cmd_ctx->lsm->cmd_type) {
3013 case LTTNG_DISABLE_CHANNEL:
3014 case LTTNG_DISABLE_EVENT:
3015 switch (cmd_ctx->lsm->domain.type) {
3016 case LTTNG_DOMAIN_KERNEL:
3017 if (!cmd_ctx->session->kernel_session) {
3018 ret = LTTNG_ERR_NO_CHANNEL;
3019 goto error;
3020 }
3021 break;
3022 case LTTNG_DOMAIN_JUL:
3023 case LTTNG_DOMAIN_LOG4J:
3024 case LTTNG_DOMAIN_PYTHON:
3025 case LTTNG_DOMAIN_UST:
3026 if (!cmd_ctx->session->ust_session) {
3027 ret = LTTNG_ERR_NO_CHANNEL;
3028 goto error;
3029 }
3030 break;
3031 default:
3032 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3033 goto error;
3034 }
3035 default:
3036 break;
3037 }
3038
3039 if (!need_domain) {
3040 goto skip_domain;
3041 }
3042
3043 /*
3044 * Check domain type for specific "pre-action".
3045 */
3046 switch (cmd_ctx->lsm->domain.type) {
3047 case LTTNG_DOMAIN_KERNEL:
3048 if (!is_root) {
3049 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3050 goto error;
3051 }
3052
3053 /* Kernel tracer check */
3054 if (kernel_tracer_fd == -1) {
3055 /* Basically, load kernel tracer modules */
3056 ret = init_kernel_tracer();
3057 if (ret != 0) {
3058 goto error;
3059 }
3060 }
3061
3062 /* Consumer is in an ERROR state. Report back to client */
3063 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
3064 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3065 goto error;
3066 }
3067
3068 /* Need a session for kernel command */
3069 if (need_tracing_session) {
3070 if (cmd_ctx->session->kernel_session == NULL) {
3071 ret = create_kernel_session(cmd_ctx->session);
3072 if (ret < 0) {
3073 ret = LTTNG_ERR_KERN_SESS_FAIL;
3074 goto error;
3075 }
3076 }
3077
3078 /* Start the kernel consumer daemon */
3079 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3080 if (kconsumer_data.pid == 0 &&
3081 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3082 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3083 ret = start_consumerd(&kconsumer_data);
3084 if (ret < 0) {
3085 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3086 goto error;
3087 }
3088 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3089 } else {
3090 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3091 }
3092
3093 /*
3094 * The consumer was just spawned so we need to add the socket to
3095 * the consumer output of the session if exist.
3096 */
3097 ret = consumer_create_socket(&kconsumer_data,
3098 cmd_ctx->session->kernel_session->consumer);
3099 if (ret < 0) {
3100 goto error;
3101 }
3102 }
3103
3104 break;
3105 case LTTNG_DOMAIN_JUL:
3106 case LTTNG_DOMAIN_LOG4J:
3107 case LTTNG_DOMAIN_PYTHON:
3108 case LTTNG_DOMAIN_UST:
3109 {
3110 if (!ust_app_supported()) {
3111 ret = LTTNG_ERR_NO_UST;
3112 goto error;
3113 }
3114 /* Consumer is in an ERROR state. Report back to client */
3115 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
3116 ret = LTTNG_ERR_NO_USTCONSUMERD;
3117 goto error;
3118 }
3119
3120 if (need_tracing_session) {
3121 /* Create UST session if none exist. */
3122 if (cmd_ctx->session->ust_session == NULL) {
3123 ret = create_ust_session(cmd_ctx->session,
3124 &cmd_ctx->lsm->domain);
3125 if (ret != LTTNG_OK) {
3126 goto error;
3127 }
3128 }
3129
3130 /* Start the UST consumer daemons */
3131 /* 64-bit */
3132 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3133 if (consumerd64_bin[0] != '\0' &&
3134 ustconsumer64_data.pid == 0 &&
3135 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3136 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3137 ret = start_consumerd(&ustconsumer64_data);
3138 if (ret < 0) {
3139 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
3140 uatomic_set(&ust_consumerd64_fd, -EINVAL);
3141 goto error;
3142 }
3143
3144 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
3145 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3146 } else {
3147 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3148 }
3149
3150 /*
3151 * Setup socket for consumer 64 bit. No need for atomic access
3152 * since it was set above and can ONLY be set in this thread.
3153 */
3154 ret = consumer_create_socket(&ustconsumer64_data,
3155 cmd_ctx->session->ust_session->consumer);
3156 if (ret < 0) {
3157 goto error;
3158 }
3159
3160 /* 32-bit */
3161 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
3162 if (consumerd32_bin[0] != '\0' &&
3163 ustconsumer32_data.pid == 0 &&
3164 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3165 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3166 ret = start_consumerd(&ustconsumer32_data);
3167 if (ret < 0) {
3168 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
3169 uatomic_set(&ust_consumerd32_fd, -EINVAL);
3170 goto error;
3171 }
3172
3173 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
3174 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3175 } else {
3176 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3177 }
3178
3179 /*
3180 * Setup socket for consumer 64 bit. No need for atomic access
3181 * since it was set above and can ONLY be set in this thread.
3182 */
3183 ret = consumer_create_socket(&ustconsumer32_data,
3184 cmd_ctx->session->ust_session->consumer);
3185 if (ret < 0) {
3186 goto error;
3187 }
3188 }
3189 break;
3190 }
3191 default:
3192 break;
3193 }
3194 skip_domain:
3195
3196 /* Validate consumer daemon state when start/stop trace command */
3197 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3198 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3199 switch (cmd_ctx->lsm->domain.type) {
3200 case LTTNG_DOMAIN_JUL:
3201 case LTTNG_DOMAIN_LOG4J:
3202 case LTTNG_DOMAIN_PYTHON:
3203 case LTTNG_DOMAIN_UST:
3204 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
3205 ret = LTTNG_ERR_NO_USTCONSUMERD;
3206 goto error;
3207 }
3208 break;
3209 case LTTNG_DOMAIN_KERNEL:
3210 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
3211 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3212 goto error;
3213 }
3214 break;
3215 }
3216 }
3217
3218 /*
3219 * Check that the UID or GID match that of the tracing session.
3220 * The root user can interact with all sessions.
3221 */
3222 if (need_tracing_session) {
3223 if (!session_access_ok(cmd_ctx->session,
3224 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3225 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
3226 ret = LTTNG_ERR_EPERM;
3227 goto error;
3228 }
3229 }
3230
3231 /*
3232 * Send relayd information to consumer as soon as we have a domain and a
3233 * session defined.
3234 */
3235 if (cmd_ctx->session && need_domain) {
3236 /*
3237 * Setup relayd if not done yet. If the relayd information was already
3238 * sent to the consumer, this call will gracefully return.
3239 */
3240 ret = cmd_setup_relayd(cmd_ctx->session);
3241 if (ret != LTTNG_OK) {
3242 goto error;
3243 }
3244 }
3245
3246 /* Process by command type */
3247 switch (cmd_ctx->lsm->cmd_type) {
3248 case LTTNG_ADD_CONTEXT:
3249 {
3250 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3251 cmd_ctx->lsm->u.context.channel_name,
3252 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
3253 break;
3254 }
3255 case LTTNG_DISABLE_CHANNEL:
3256 {
3257 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3258 cmd_ctx->lsm->u.disable.channel_name);
3259 break;
3260 }
3261 case LTTNG_DISABLE_EVENT:
3262 {
3263 /* FIXME: passing packed structure to non-packed pointer */
3264 /* TODO: handle filter */
3265 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3266 cmd_ctx->lsm->u.disable.channel_name,
3267 &cmd_ctx->lsm->u.disable.event);
3268 break;
3269 }
3270 case LTTNG_ENABLE_CHANNEL:
3271 {
3272 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
3273 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
3274 break;
3275 }
3276 case LTTNG_TRACK_PID:
3277 {
3278 ret = cmd_track_pid(cmd_ctx->session,
3279 cmd_ctx->lsm->domain.type,
3280 cmd_ctx->lsm->u.pid_tracker.pid);
3281 break;
3282 }
3283 case LTTNG_UNTRACK_PID:
3284 {
3285 ret = cmd_untrack_pid(cmd_ctx->session,
3286 cmd_ctx->lsm->domain.type,
3287 cmd_ctx->lsm->u.pid_tracker.pid);
3288 break;
3289 }
3290 case LTTNG_ENABLE_EVENT:
3291 {
3292 struct lttng_event_exclusion *exclusion = NULL;
3293 struct lttng_filter_bytecode *bytecode = NULL;
3294 char *filter_expression = NULL;
3295
3296 /* Handle exclusion events and receive it from the client. */
3297 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3298 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3299
3300 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3301 (count * LTTNG_SYMBOL_NAME_LEN));
3302 if (!exclusion) {
3303 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3304 goto error;
3305 }
3306
3307 DBG("Receiving var len exclusion event list from client ...");
3308 exclusion->count = count;
3309 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3310 count * LTTNG_SYMBOL_NAME_LEN);
3311 if (ret <= 0) {
3312 DBG("Nothing recv() from client var len data... continuing");
3313 *sock_error = 1;
3314 free(exclusion);
3315 ret = LTTNG_ERR_EXCLUSION_INVAL;
3316 goto error;
3317 }
3318 }
3319
3320 /* Get filter expression from client. */
3321 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3322 size_t expression_len =
3323 cmd_ctx->lsm->u.enable.expression_len;
3324
3325 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3326 ret = LTTNG_ERR_FILTER_INVAL;
3327 free(exclusion);
3328 goto error;
3329 }
3330
3331 filter_expression = zmalloc(expression_len);
3332 if (!filter_expression) {
3333 free(exclusion);
3334 ret = LTTNG_ERR_FILTER_NOMEM;
3335 goto error;
3336 }
3337
3338 /* Receive var. len. data */
3339 DBG("Receiving var len filter's expression from client ...");
3340 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3341 expression_len);
3342 if (ret <= 0) {
3343 DBG("Nothing recv() from client car len data... continuing");
3344 *sock_error = 1;
3345 free(filter_expression);
3346 free(exclusion);
3347 ret = LTTNG_ERR_FILTER_INVAL;
3348 goto error;
3349 }
3350 }
3351
3352 /* Handle filter and get bytecode from client. */
3353 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3354 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3355
3356 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3357 ret = LTTNG_ERR_FILTER_INVAL;
3358 free(filter_expression);
3359 free(exclusion);
3360 goto error;
3361 }
3362
3363 bytecode = zmalloc(bytecode_len);
3364 if (!bytecode) {
3365 free(filter_expression);
3366 free(exclusion);
3367 ret = LTTNG_ERR_FILTER_NOMEM;
3368 goto error;
3369 }
3370
3371 /* Receive var. len. data */
3372 DBG("Receiving var len filter's bytecode from client ...");
3373 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3374 if (ret <= 0) {
3375 DBG("Nothing recv() from client car len data... continuing");
3376 *sock_error = 1;
3377 free(filter_expression);
3378 free(bytecode);
3379 free(exclusion);
3380 ret = LTTNG_ERR_FILTER_INVAL;
3381 goto error;
3382 }
3383
3384 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3385 free(filter_expression);
3386 free(bytecode);
3387 free(exclusion);
3388 ret = LTTNG_ERR_FILTER_INVAL;
3389 goto error;
3390 }
3391 }
3392
3393 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3394 cmd_ctx->lsm->u.enable.channel_name,
3395 &cmd_ctx->lsm->u.enable.event,
3396 filter_expression, bytecode, exclusion,
3397 kernel_poll_pipe[1]);
3398 break;
3399 }
3400 case LTTNG_LIST_TRACEPOINTS:
3401 {
3402 struct lttng_event *events;
3403 ssize_t nb_events;
3404
3405 session_lock_list();
3406 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3407 session_unlock_list();
3408 if (nb_events < 0) {
3409 /* Return value is a negative lttng_error_code. */
3410 ret = -nb_events;
3411 goto error;
3412 }
3413
3414 /*
3415 * Setup lttng message with payload size set to the event list size in
3416 * bytes and then copy list into the llm payload.
3417 */
3418 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3419 if (ret < 0) {
3420 free(events);
3421 goto setup_error;
3422 }
3423
3424 /* Copy event list into message payload */
3425 memcpy(cmd_ctx->llm->payload, events,
3426 sizeof(struct lttng_event) * nb_events);
3427
3428 free(events);
3429
3430 ret = LTTNG_OK;
3431 break;
3432 }
3433 case LTTNG_LIST_TRACEPOINT_FIELDS:
3434 {
3435 struct lttng_event_field *fields;
3436 ssize_t nb_fields;
3437
3438 session_lock_list();
3439 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3440 &fields);
3441 session_unlock_list();
3442 if (nb_fields < 0) {
3443 /* Return value is a negative lttng_error_code. */
3444 ret = -nb_fields;
3445 goto error;
3446 }
3447
3448 /*
3449 * Setup lttng message with payload size set to the event list size in
3450 * bytes and then copy list into the llm payload.
3451 */
3452 ret = setup_lttng_msg(cmd_ctx,
3453 sizeof(struct lttng_event_field) * nb_fields);
3454 if (ret < 0) {
3455 free(fields);
3456 goto setup_error;
3457 }
3458
3459 /* Copy event list into message payload */
3460 memcpy(cmd_ctx->llm->payload, fields,
3461 sizeof(struct lttng_event_field) * nb_fields);
3462
3463 free(fields);
3464
3465 ret = LTTNG_OK;
3466 break;
3467 }
3468 case LTTNG_LIST_SYSCALLS:
3469 {
3470 struct lttng_event *events;
3471 ssize_t nb_events;
3472
3473 nb_events = cmd_list_syscalls(&events);
3474 if (nb_events < 0) {
3475 /* Return value is a negative lttng_error_code. */
3476 ret = -nb_events;
3477 goto error;
3478 }
3479
3480 /*
3481 * Setup lttng message with payload size set to the event list size in
3482 * bytes and then copy list into the llm payload.
3483 */
3484 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3485 if (ret < 0) {
3486 free(events);
3487 goto setup_error;
3488 }
3489
3490 /* Copy event list into message payload */
3491 memcpy(cmd_ctx->llm->payload, events,
3492 sizeof(struct lttng_event) * nb_events);
3493
3494 free(events);
3495
3496 ret = LTTNG_OK;
3497 break;
3498 }
3499 case LTTNG_LIST_TRACKER_PIDS:
3500 {
3501 int32_t *pids = NULL;
3502 ssize_t nr_pids;
3503
3504 nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
3505 cmd_ctx->lsm->domain.type, &pids);
3506 if (nr_pids < 0) {
3507 /* Return value is a negative lttng_error_code. */
3508 ret = -nr_pids;
3509 goto error;
3510 }
3511
3512 /*
3513 * Setup lttng message with payload size set to the event list size in
3514 * bytes and then copy list into the llm payload.
3515 */
3516 ret = setup_lttng_msg(cmd_ctx, sizeof(int32_t) * nr_pids);
3517 if (ret < 0) {
3518 free(pids);
3519 goto setup_error;
3520 }
3521
3522 /* Copy event list into message payload */
3523 memcpy(cmd_ctx->llm->payload, pids,
3524 sizeof(int) * nr_pids);
3525
3526 free(pids);
3527
3528 ret = LTTNG_OK;
3529 break;
3530 }
3531 case LTTNG_SET_CONSUMER_URI:
3532 {
3533 size_t nb_uri, len;
3534 struct lttng_uri *uris;
3535
3536 nb_uri = cmd_ctx->lsm->u.uri.size;
3537 len = nb_uri * sizeof(struct lttng_uri);
3538
3539 if (nb_uri == 0) {
3540 ret = LTTNG_ERR_INVALID;
3541 goto error;
3542 }
3543
3544 uris = zmalloc(len);
3545 if (uris == NULL) {
3546 ret = LTTNG_ERR_FATAL;
3547 goto error;
3548 }
3549
3550 /* Receive variable len data */
3551 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3552 ret = lttcomm_recv_unix_sock(sock, uris, len);
3553 if (ret <= 0) {
3554 DBG("No URIs received from client... continuing");
3555 *sock_error = 1;
3556 ret = LTTNG_ERR_SESSION_FAIL;
3557 free(uris);
3558 goto error;
3559 }
3560
3561 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3562 free(uris);
3563 if (ret != LTTNG_OK) {
3564 goto error;
3565 }
3566
3567
3568 break;
3569 }
3570 case LTTNG_START_TRACE:
3571 {
3572 ret = cmd_start_trace(cmd_ctx->session);
3573 break;
3574 }
3575 case LTTNG_STOP_TRACE:
3576 {
3577 ret = cmd_stop_trace(cmd_ctx->session);
3578 break;
3579 }
3580 case LTTNG_CREATE_SESSION:
3581 {
3582 size_t nb_uri, len;
3583 struct lttng_uri *uris = NULL;
3584
3585 nb_uri = cmd_ctx->lsm->u.uri.size;
3586 len = nb_uri * sizeof(struct lttng_uri);
3587
3588 if (nb_uri > 0) {
3589 uris = zmalloc(len);
3590 if (uris == NULL) {
3591 ret = LTTNG_ERR_FATAL;
3592 goto error;
3593 }
3594
3595 /* Receive variable len data */
3596 DBG("Waiting for %zu URIs from client ...", nb_uri);
3597 ret = lttcomm_recv_unix_sock(sock, uris, len);
3598 if (ret <= 0) {
3599 DBG("No URIs received from client... continuing");
3600 *sock_error = 1;
3601 ret = LTTNG_ERR_SESSION_FAIL;
3602 free(uris);
3603 goto error;
3604 }
3605
3606 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3607 DBG("Creating session with ONE network URI is a bad call");
3608 ret = LTTNG_ERR_SESSION_FAIL;
3609 free(uris);
3610 goto error;
3611 }
3612 }
3613
3614 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
3615 &cmd_ctx->creds, 0);
3616
3617 free(uris);
3618
3619 break;
3620 }
3621 case LTTNG_DESTROY_SESSION:
3622 {
3623 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
3624
3625 /* Set session to NULL so we do not unlock it after free. */
3626 cmd_ctx->session = NULL;
3627 break;
3628 }
3629 case LTTNG_LIST_DOMAINS:
3630 {
3631 ssize_t nb_dom;
3632 struct lttng_domain *domains = NULL;
3633
3634 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3635 if (nb_dom < 0) {
3636 /* Return value is a negative lttng_error_code. */
3637 ret = -nb_dom;
3638 goto error;
3639 }
3640
3641 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
3642 if (ret < 0) {
3643 free(domains);
3644 goto setup_error;
3645 }
3646
3647 /* Copy event list into message payload */
3648 memcpy(cmd_ctx->llm->payload, domains,
3649 nb_dom * sizeof(struct lttng_domain));
3650
3651 free(domains);
3652
3653 ret = LTTNG_OK;
3654 break;
3655 }
3656 case LTTNG_LIST_CHANNELS:
3657 {
3658 int nb_chan;
3659 struct lttng_channel *channels = NULL;
3660
3661 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3662 cmd_ctx->session, &channels);
3663 if (nb_chan < 0) {
3664 /* Return value is a negative lttng_error_code. */
3665 ret = -nb_chan;
3666 goto error;
3667 }
3668
3669 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
3670 if (ret < 0) {
3671 free(channels);
3672 goto setup_error;
3673 }
3674
3675 /* Copy event list into message payload */
3676 memcpy(cmd_ctx->llm->payload, channels,
3677 nb_chan * sizeof(struct lttng_channel));
3678
3679 free(channels);
3680
3681 ret = LTTNG_OK;
3682 break;
3683 }
3684 case LTTNG_LIST_EVENTS:
3685 {
3686 ssize_t nb_event;
3687 struct lttng_event *events = NULL;
3688
3689 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3690 cmd_ctx->lsm->u.list.channel_name, &events);
3691 if (nb_event < 0) {
3692 /* Return value is a negative lttng_error_code. */
3693 ret = -nb_event;
3694 goto error;
3695 }
3696
3697 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
3698 if (ret < 0) {
3699 free(events);
3700 goto setup_error;
3701 }
3702
3703 /* Copy event list into message payload */
3704 memcpy(cmd_ctx->llm->payload, events,
3705 nb_event * sizeof(struct lttng_event));
3706
3707 free(events);
3708
3709 ret = LTTNG_OK;
3710 break;
3711 }
3712 case LTTNG_LIST_SESSIONS:
3713 {
3714 unsigned int nr_sessions;
3715
3716 session_lock_list();
3717 nr_sessions = lttng_sessions_count(
3718 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3719 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3720
3721 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
3722 if (ret < 0) {
3723 session_unlock_list();
3724 goto setup_error;
3725 }
3726
3727 /* Filled the session array */
3728 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
3729 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3730 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3731
3732 session_unlock_list();
3733
3734 ret = LTTNG_OK;
3735 break;
3736 }
3737 case LTTNG_CALIBRATE:
3738 {
3739 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3740 &cmd_ctx->lsm->u.calibrate);
3741 break;
3742 }
3743 case LTTNG_REGISTER_CONSUMER:
3744 {
3745 struct consumer_data *cdata;
3746
3747 switch (cmd_ctx->lsm->domain.type) {
3748 case LTTNG_DOMAIN_KERNEL:
3749 cdata = &kconsumer_data;
3750 break;
3751 default:
3752 ret = LTTNG_ERR_UND;
3753 goto error;
3754 }
3755
3756 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3757 cmd_ctx->lsm->u.reg.path, cdata);
3758 break;
3759 }
3760 case LTTNG_DATA_PENDING:
3761 {
3762 ret = cmd_data_pending(cmd_ctx->session);
3763 break;
3764 }
3765 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3766 {
3767 struct lttcomm_lttng_output_id reply;
3768
3769 ret = cmd_snapshot_add_output(cmd_ctx->session,
3770 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3771 if (ret != LTTNG_OK) {
3772 goto error;
3773 }
3774
3775 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3776 if (ret < 0) {
3777 goto setup_error;
3778 }
3779
3780 /* Copy output list into message payload */
3781 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3782 ret = LTTNG_OK;
3783 break;
3784 }
3785 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3786 {
3787 ret = cmd_snapshot_del_output(cmd_ctx->session,
3788 &cmd_ctx->lsm->u.snapshot_output.output);
3789 break;
3790 }
3791 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3792 {
3793 ssize_t nb_output;
3794 struct lttng_snapshot_output *outputs = NULL;
3795
3796 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3797 if (nb_output < 0) {
3798 ret = -nb_output;
3799 goto error;
3800 }
3801
3802 ret = setup_lttng_msg(cmd_ctx,
3803 nb_output * sizeof(struct lttng_snapshot_output));
3804 if (ret < 0) {
3805 free(outputs);
3806 goto setup_error;
3807 }
3808
3809 if (outputs) {
3810 /* Copy output list into message payload */
3811 memcpy(cmd_ctx->llm->payload, outputs,
3812 nb_output * sizeof(struct lttng_snapshot_output));
3813 free(outputs);
3814 }
3815
3816 ret = LTTNG_OK;
3817 break;
3818 }
3819 case LTTNG_SNAPSHOT_RECORD:
3820 {
3821 ret = cmd_snapshot_record(cmd_ctx->session,
3822 &cmd_ctx->lsm->u.snapshot_record.output,
3823 cmd_ctx->lsm->u.snapshot_record.wait);
3824 break;
3825 }
3826 case LTTNG_CREATE_SESSION_SNAPSHOT:
3827 {
3828 size_t nb_uri, len;
3829 struct lttng_uri *uris = NULL;
3830
3831 nb_uri = cmd_ctx->lsm->u.uri.size;
3832 len = nb_uri * sizeof(struct lttng_uri);
3833
3834 if (nb_uri > 0) {
3835 uris = zmalloc(len);
3836 if (uris == NULL) {
3837 ret = LTTNG_ERR_FATAL;
3838 goto error;
3839 }
3840
3841 /* Receive variable len data */
3842 DBG("Waiting for %zu URIs from client ...", nb_uri);
3843 ret = lttcomm_recv_unix_sock(sock, uris, len);
3844 if (ret <= 0) {
3845 DBG("No URIs received from client... continuing");
3846 *sock_error = 1;
3847 ret = LTTNG_ERR_SESSION_FAIL;
3848 free(uris);
3849 goto error;
3850 }
3851
3852 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3853 DBG("Creating session with ONE network URI is a bad call");
3854 ret = LTTNG_ERR_SESSION_FAIL;
3855 free(uris);
3856 goto error;
3857 }
3858 }
3859
3860 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3861 nb_uri, &cmd_ctx->creds);
3862 free(uris);
3863 break;
3864 }
3865 case LTTNG_CREATE_SESSION_LIVE:
3866 {
3867 size_t nb_uri, len;
3868 struct lttng_uri *uris = NULL;
3869
3870 nb_uri = cmd_ctx->lsm->u.uri.size;
3871 len = nb_uri * sizeof(struct lttng_uri);
3872
3873 if (nb_uri > 0) {
3874 uris = zmalloc(len);
3875 if (uris == NULL) {
3876 ret = LTTNG_ERR_FATAL;
3877 goto error;
3878 }
3879
3880 /* Receive variable len data */
3881 DBG("Waiting for %zu URIs from client ...", nb_uri);
3882 ret = lttcomm_recv_unix_sock(sock, uris, len);
3883 if (ret <= 0) {
3884 DBG("No URIs received from client... continuing");
3885 *sock_error = 1;
3886 ret = LTTNG_ERR_SESSION_FAIL;
3887 free(uris);
3888 goto error;
3889 }
3890
3891 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3892 DBG("Creating session with ONE network URI is a bad call");
3893 ret = LTTNG_ERR_SESSION_FAIL;
3894 free(uris);
3895 goto error;
3896 }
3897 }
3898
3899 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3900 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3901 free(uris);
3902 break;
3903 }
3904 case LTTNG_SAVE_SESSION:
3905 {
3906 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
3907 &cmd_ctx->creds);
3908 break;
3909 }
3910 case LTTNG_SET_SESSION_SHM_PATH:
3911 {
3912 ret = cmd_set_session_shm_path(cmd_ctx->session,
3913 cmd_ctx->lsm->u.set_shm_path.shm_path);
3914 break;
3915 }
3916 default:
3917 ret = LTTNG_ERR_UND;
3918 break;
3919 }
3920
3921 error:
3922 if (cmd_ctx->llm == NULL) {
3923 DBG("Missing llm structure. Allocating one.");
3924 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3925 goto setup_error;
3926 }
3927 }
3928 /* Set return code */
3929 cmd_ctx->llm->ret_code = ret;
3930 setup_error:
3931 if (cmd_ctx->session) {
3932 session_unlock(cmd_ctx->session);
3933 }
3934 if (need_tracing_session) {
3935 session_unlock_list();
3936 }
3937 init_setup_error:
3938 return ret;
3939 }
3940
3941 /*
3942 * Thread managing health check socket.
3943 */
3944 static void *thread_manage_health(void *data)
3945 {
3946 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3947 uint32_t revents, nb_fd;
3948 struct lttng_poll_event events;
3949 struct health_comm_msg msg;
3950 struct health_comm_reply reply;
3951
3952 DBG("[thread] Manage health check started");
3953
3954 rcu_register_thread();
3955
3956 /* We might hit an error path before this is created. */
3957 lttng_poll_init(&events);
3958
3959 /* Create unix socket */
3960 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3961 if (sock < 0) {
3962 ERR("Unable to create health check Unix socket");
3963 ret = -1;
3964 goto error;
3965 }
3966
3967 if (is_root) {
3968 /* lttng health client socket path permissions */
3969 ret = chown(health_unix_sock_path, 0,
3970 utils_get_group_id(tracing_group_name));
3971 if (ret < 0) {
3972 ERR("Unable to set group on %s", health_unix_sock_path);
3973 PERROR("chown");
3974 ret = -1;
3975 goto error;
3976 }
3977
3978 ret = chmod(health_unix_sock_path,
3979 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3980 if (ret < 0) {
3981 ERR("Unable to set permissions on %s", health_unix_sock_path);
3982 PERROR("chmod");
3983 ret = -1;
3984 goto error;
3985 }
3986 }
3987
3988 /*
3989 * Set the CLOEXEC flag. Return code is useless because either way, the
3990 * show must go on.
3991 */
3992 (void) utils_set_fd_cloexec(sock);
3993
3994 ret = lttcomm_listen_unix_sock(sock);
3995 if (ret < 0) {
3996 goto error;
3997 }
3998
3999 /*
4000 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4001 * more will be added to this poll set.
4002 */
4003 ret = sessiond_set_thread_pollset(&events, 2);
4004 if (ret < 0) {
4005 goto error;
4006 }
4007
4008 /* Add the application registration socket */
4009 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
4010 if (ret < 0) {
4011 goto error;
4012 }
4013
4014 sessiond_notify_ready();
4015
4016 while (1) {
4017 DBG("Health check ready");
4018
4019 /* Inifinite blocking call, waiting for transmission */
4020 restart:
4021 ret = lttng_poll_wait(&events, -1);
4022 if (ret < 0) {
4023 /*
4024 * Restart interrupted system call.
4025 */
4026 if (errno == EINTR) {
4027 goto restart;
4028 }
4029 goto error;
4030 }
4031
4032 nb_fd = ret;
4033
4034 for (i = 0; i < nb_fd; i++) {
4035 /* Fetch once the poll data */
4036 revents = LTTNG_POLL_GETEV(&events, i);
4037 pollfd = LTTNG_POLL_GETFD(&events, i);
4038
4039 if (!revents) {
4040 /* No activity for this FD (poll implementation). */
4041 continue;
4042 }
4043
4044 /* Thread quit pipe has been closed. Killing thread. */
4045 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
4046 if (ret) {
4047 err = 0;
4048 goto exit;
4049 }
4050
4051 /* Event on the registration socket */
4052 if (pollfd == sock) {
4053 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4054 ERR("Health socket poll error");
4055 goto error;
4056 }
4057 }
4058 }
4059
4060 new_sock = lttcomm_accept_unix_sock(sock);
4061 if (new_sock < 0) {
4062 goto error;
4063 }
4064
4065 /*
4066 * Set the CLOEXEC flag. Return code is useless because either way, the
4067 * show must go on.
4068 */
4069 (void) utils_set_fd_cloexec(new_sock);
4070
4071 DBG("Receiving data from client for health...");
4072 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4073 if (ret <= 0) {
4074 DBG("Nothing recv() from client... continuing");
4075 ret = close(new_sock);
4076 if (ret) {
4077 PERROR("close");
4078 }
4079 new_sock = -1;
4080 continue;
4081 }
4082
4083 rcu_thread_online();
4084
4085 memset(&reply, 0, sizeof(reply));
4086 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
4087 /*
4088 * health_check_state returns 0 if health is
4089 * bad.
4090 */
4091 if (!health_check_state(health_sessiond, i)) {
4092 reply.ret_code |= 1ULL << i;
4093 }
4094 }
4095
4096 DBG2("Health check return value %" PRIx64, reply.ret_code);
4097
4098 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4099 if (ret < 0) {
4100 ERR("Failed to send health data back to client");
4101 }
4102
4103 /* End of transmission */
4104 ret = close(new_sock);
4105 if (ret) {
4106 PERROR("close");
4107 }
4108 new_sock = -1;
4109 }
4110
4111 exit:
4112 error:
4113 if (err) {
4114 ERR("Health error occurred in %s", __func__);
4115 }
4116 DBG("Health check thread dying");
4117 unlink(health_unix_sock_path);
4118 if (sock >= 0) {
4119 ret = close(sock);
4120 if (ret) {
4121 PERROR("close");
4122 }
4123 }
4124
4125 lttng_poll_clean(&events);
4126
4127 rcu_unregister_thread();
4128 return NULL;
4129 }
4130
4131 /*
4132 * This thread manage all clients request using the unix client socket for
4133 * communication.
4134 */
4135 static void *thread_manage_clients(void *data)
4136 {
4137 int sock = -1, ret, i, pollfd, err = -1;
4138 int sock_error;
4139 uint32_t revents, nb_fd;
4140 struct command_ctx *cmd_ctx = NULL;
4141 struct lttng_poll_event events;
4142
4143 DBG("[thread] Manage client started");
4144
4145 rcu_register_thread();
4146
4147 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
4148
4149 health_code_update();
4150
4151 ret = lttcomm_listen_unix_sock(client_sock);
4152 if (ret < 0) {
4153 goto error_listen;
4154 }
4155
4156 /*
4157 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4158 * more will be added to this poll set.
4159 */
4160 ret = sessiond_set_thread_pollset(&events, 2);
4161 if (ret < 0) {
4162 goto error_create_poll;
4163 }
4164
4165 /* Add the application registration socket */
4166 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4167 if (ret < 0) {
4168 goto error;
4169 }
4170
4171 sessiond_notify_ready();
4172 ret = sem_post(&load_info->message_thread_ready);
4173 if (ret) {
4174 PERROR("sem_post message_thread_ready");
4175 goto error;
4176 }
4177
4178 /* This testpoint is after we signal readiness to the parent. */
4179 if (testpoint(sessiond_thread_manage_clients)) {
4180 goto error;
4181 }
4182
4183 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
4184 goto error;
4185 }
4186
4187 health_code_update();
4188
4189 while (1) {
4190 DBG("Accepting client command ...");
4191
4192 /* Inifinite blocking call, waiting for transmission */
4193 restart:
4194 health_poll_entry();
4195 ret = lttng_poll_wait(&events, -1);
4196 health_poll_exit();
4197 if (ret < 0) {
4198 /*
4199 * Restart interrupted system call.
4200 */
4201 if (errno == EINTR) {
4202 goto restart;
4203 }
4204 goto error;
4205 }
4206
4207 nb_fd = ret;
4208
4209 for (i = 0; i < nb_fd; i++) {
4210 /* Fetch once the poll data */
4211 revents = LTTNG_POLL_GETEV(&events, i);
4212 pollfd = LTTNG_POLL_GETFD(&events, i);
4213
4214 health_code_update();
4215
4216 if (!revents) {
4217 /* No activity for this FD (poll implementation). */
4218 continue;
4219 }
4220
4221 /* Thread quit pipe has been closed. Killing thread. */
4222 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
4223 if (ret) {
4224 err = 0;
4225 goto exit;
4226 }
4227
4228 /* Event on the registration socket */
4229 if (pollfd == client_sock) {
4230 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4231 ERR("Client socket poll error");
4232 goto error;
4233 }
4234 }
4235 }
4236
4237 DBG("Wait for client response");
4238
4239 health_code_update();
4240
4241 sock = lttcomm_accept_unix_sock(client_sock);
4242 if (sock < 0) {
4243 goto error;
4244 }
4245
4246 /*
4247 * Set the CLOEXEC flag. Return code is useless because either way, the
4248 * show must go on.
4249 */
4250 (void) utils_set_fd_cloexec(sock);
4251
4252 /* Set socket option for credentials retrieval */
4253 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4254 if (ret < 0) {
4255 goto error;
4256 }
4257
4258 /* Allocate context command to process the client request */
4259 cmd_ctx = zmalloc(sizeof(struct command_ctx));
4260 if (cmd_ctx == NULL) {
4261 PERROR("zmalloc cmd_ctx");
4262 goto error;
4263 }
4264
4265 /* Allocate data buffer for reception */
4266 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
4267 if (cmd_ctx->lsm == NULL) {
4268 PERROR("zmalloc cmd_ctx->lsm");
4269 goto error;
4270 }
4271
4272 cmd_ctx->llm = NULL;
4273 cmd_ctx->session = NULL;
4274
4275 health_code_update();
4276
4277 /*
4278 * Data is received from the lttng client. The struct
4279 * lttcomm_session_msg (lsm) contains the command and data request of
4280 * the client.
4281 */
4282 DBG("Receiving data from client ...");
4283 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4284 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
4285 if (ret <= 0) {
4286 DBG("Nothing recv() from client... continuing");
4287 ret = close(sock);
4288 if (ret) {
4289 PERROR("close");
4290 }
4291 sock = -1;
4292 clean_command_ctx(&cmd_ctx);
4293 continue;
4294 }
4295
4296 health_code_update();
4297
4298 // TODO: Validate cmd_ctx including sanity check for
4299 // security purpose.
4300
4301 rcu_thread_online();
4302 /*
4303 * This function dispatch the work to the kernel or userspace tracer
4304 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4305 * informations for the client. The command context struct contains
4306 * everything this function may needs.
4307 */
4308 ret = process_client_msg(cmd_ctx, sock, &sock_error);
4309 rcu_thread_offline();
4310 if (ret < 0) {
4311 ret = close(sock);
4312 if (ret) {
4313 PERROR("close");
4314 }
4315 sock = -1;
4316 /*
4317 * TODO: Inform client somehow of the fatal error. At
4318 * this point, ret < 0 means that a zmalloc failed
4319 * (ENOMEM). Error detected but still accept
4320 * command, unless a socket error has been
4321 * detected.
4322 */
4323 clean_command_ctx(&cmd_ctx);
4324 continue;
4325 }
4326
4327 health_code_update();
4328
4329 DBG("Sending response (size: %d, retcode: %s)",
4330 cmd_ctx->lttng_msg_size,
4331 lttng_strerror(-cmd_ctx->llm->ret_code));
4332 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
4333 if (ret < 0) {
4334 ERR("Failed to send data back to client");
4335 }
4336
4337 /* End of transmission */
4338 ret = close(sock);
4339 if (ret) {
4340 PERROR("close");
4341 }
4342 sock = -1;
4343
4344 clean_command_ctx(&cmd_ctx);
4345
4346 health_code_update();
4347 }
4348
4349 exit:
4350 error:
4351 if (sock >= 0) {
4352 ret = close(sock);
4353 if (ret) {
4354 PERROR("close");
4355 }
4356 }
4357
4358 lttng_poll_clean(&events);
4359 clean_command_ctx(&cmd_ctx);
4360
4361 error_listen:
4362 error_create_poll:
4363 unlink(client_unix_sock_path);
4364 if (client_sock >= 0) {
4365 ret = close(client_sock);
4366 if (ret) {
4367 PERROR("close");
4368 }
4369 }
4370
4371 if (err) {
4372 health_error();
4373 ERR("Health error occurred in %s", __func__);
4374 }
4375
4376 health_unregister(health_sessiond);
4377
4378 DBG("Client thread dying");
4379
4380 rcu_unregister_thread();
4381
4382 /*
4383 * Since we are creating the consumer threads, we own them, so we need
4384 * to join them before our thread exits.
4385 */
4386 ret = join_consumer_thread(&kconsumer_data);
4387 if (ret) {
4388 errno = ret;
4389 PERROR("join_consumer");
4390 }
4391
4392 ret = join_consumer_thread(&ustconsumer32_data);
4393 if (ret) {
4394 errno = ret;
4395 PERROR("join_consumer ust32");
4396 }
4397
4398 ret = join_consumer_thread(&ustconsumer64_data);
4399 if (ret) {
4400 errno = ret;
4401 PERROR("join_consumer ust64");
4402 }
4403 return NULL;
4404 }
4405
4406
4407 /*
4408 * usage function on stderr
4409 */
4410 static void usage(void)
4411 {
4412 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
4413 fprintf(stderr, " -h, --help Display this usage.\n");
4414 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
4415 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4416 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4417 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4418 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4419 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4420 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4421 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4422 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4423 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4424 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4425 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4426 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
4427 fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n");
4428 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4429 fprintf(stderr, " -V, --version Show version number.\n");
4430 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4431 fprintf(stderr, " -q, --quiet No output at all.\n");
4432 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4433 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4434 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4435 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
4436 fprintf(stderr, " --agent-tcp-port Agent registration TCP port\n");
4437 fprintf(stderr, " -f --config PATH Load daemon configuration file\n");
4438 fprintf(stderr, " -l --load PATH Load session configuration\n");
4439 fprintf(stderr, " --kmod-probes Specify kernel module probes to load\n");
4440 fprintf(stderr, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4441 }
4442
4443 /*
4444 * Take an option from the getopt output and set it in the right variable to be
4445 * used later.
4446 *
4447 * Return 0 on success else a negative value.
4448 */
4449 static int set_option(int opt, const char *arg, const char *optname)
4450 {
4451 int ret = 0;
4452
4453 if (arg && arg[0] == '\0') {
4454 /*
4455 * This only happens if the value is read from daemon config
4456 * file. This means the option requires an argument and the
4457 * configuration file contains a line such as:
4458 * my_option =
4459 */
4460 ret = -EINVAL;
4461 goto end;
4462 }
4463
4464 switch (opt) {
4465 case 0:
4466 fprintf(stderr, "option %s", optname);
4467 if (arg) {
4468 fprintf(stderr, " with arg %s\n", arg);
4469 }
4470 break;
4471 case 'c':
4472 if (lttng_is_setuid_setgid()) {
4473 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4474 "-c, --client-sock");
4475 } else {
4476 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4477 }
4478 break;
4479 case 'a':
4480 if (lttng_is_setuid_setgid()) {
4481 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4482 "-a, --apps-sock");
4483 } else {
4484 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4485 }
4486 break;
4487 case 'd':
4488 opt_daemon = 1;
4489 break;
4490 case 'b':
4491 opt_background = 1;
4492 break;
4493 case 'g':
4494 if (lttng_is_setuid_setgid()) {
4495 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4496 "-g, --group");
4497 } else {
4498 /*
4499 * If the override option is set, the pointer points to a
4500 * *non* const thus freeing it even though the variable type is
4501 * set to const.
4502 */
4503 if (tracing_group_name_override) {
4504 free((void *) tracing_group_name);
4505 }
4506 tracing_group_name = strdup(arg);
4507 if (!tracing_group_name) {
4508 PERROR("strdup");
4509 ret = -ENOMEM;
4510 }
4511 tracing_group_name_override = 1;
4512 }
4513 break;
4514 case 'h':
4515 usage();
4516 exit(EXIT_SUCCESS);
4517 case 'V':
4518 fprintf(stdout, "%s\n", VERSION);
4519 exit(EXIT_SUCCESS);
4520 case 'S':
4521 opt_sig_parent = 1;
4522 break;
4523 case 'E':
4524 if (lttng_is_setuid_setgid()) {
4525 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4526 "--kconsumerd-err-sock");
4527 } else {
4528 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4529 }
4530 break;
4531 case 'C':
4532 if (lttng_is_setuid_setgid()) {
4533 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4534 "--kconsumerd-cmd-sock");
4535 } else {
4536 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4537 }
4538 break;
4539 case 'F':
4540 if (lttng_is_setuid_setgid()) {
4541 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4542 "--ustconsumerd64-err-sock");
4543 } else {
4544 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4545 }
4546 break;
4547 case 'D':
4548 if (lttng_is_setuid_setgid()) {
4549 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4550 "--ustconsumerd64-cmd-sock");
4551 } else {
4552 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4553 }
4554 break;
4555 case 'H':
4556 if (lttng_is_setuid_setgid()) {
4557 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4558 "--ustconsumerd32-err-sock");
4559 } else {
4560 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4561 }
4562 break;
4563 case 'G':
4564 if (lttng_is_setuid_setgid()) {
4565 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4566 "--ustconsumerd32-cmd-sock");
4567 } else {
4568 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4569 }
4570 break;
4571 case 'N':
4572 opt_no_kernel = 1;
4573 break;
4574 case 'q':
4575 lttng_opt_quiet = 1;
4576 break;
4577 case 'v':
4578 /* Verbose level can increase using multiple -v */
4579 if (arg) {
4580 /* Value obtained from config file */
4581 lttng_opt_verbose = config_parse_value(arg);
4582 } else {
4583 /* -v used on command line */
4584 lttng_opt_verbose++;
4585 }
4586 /* Clamp value to [0, 3] */
4587 lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 :
4588 (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3);
4589 break;
4590 case 'Z':
4591 if (arg) {
4592 opt_verbose_consumer = config_parse_value(arg);
4593 } else {
4594 opt_verbose_consumer += 1;
4595 }
4596 break;
4597 case 'u':
4598 if (lttng_is_setuid_setgid()) {
4599 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4600 "--consumerd32-path");
4601 } else {
4602 if (consumerd32_bin_override) {
4603 free((void *) consumerd32_bin);
4604 }
4605 consumerd32_bin = strdup(arg);
4606 if (!consumerd32_bin) {
4607 PERROR("strdup");
4608 ret = -ENOMEM;
4609 }
4610 consumerd32_bin_override = 1;
4611 }
4612 break;
4613 case 'U':
4614 if (lttng_is_setuid_setgid()) {
4615 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4616 "--consumerd32-libdir");
4617 } else {
4618 if (consumerd32_libdir_override) {
4619 free((void *) consumerd32_libdir);
4620 }
4621 consumerd32_libdir = strdup(arg);
4622 if (!consumerd32_libdir) {
4623 PERROR("strdup");
4624 ret = -ENOMEM;
4625 }
4626 consumerd32_libdir_override = 1;
4627 }
4628 break;
4629 case 't':
4630 if (lttng_is_setuid_setgid()) {
4631 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4632 "--consumerd64-path");
4633 } else {
4634 if (consumerd64_bin_override) {
4635 free((void *) consumerd64_bin);
4636 }
4637 consumerd64_bin = strdup(arg);
4638 if (!consumerd64_bin) {
4639 PERROR("strdup");
4640 ret = -ENOMEM;
4641 }
4642 consumerd64_bin_override = 1;
4643 }
4644 break;
4645 case 'T':
4646 if (lttng_is_setuid_setgid()) {
4647 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4648 "--consumerd64-libdir");
4649 } else {
4650 if (consumerd64_libdir_override) {
4651 free((void *) consumerd64_libdir);
4652 }
4653 consumerd64_libdir = strdup(arg);
4654 if (!consumerd64_libdir) {
4655 PERROR("strdup");
4656 ret = -ENOMEM;
4657 }
4658 consumerd64_libdir_override = 1;
4659 }
4660 break;
4661 case 'p':
4662 if (lttng_is_setuid_setgid()) {
4663 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4664 "-p, --pidfile");
4665 } else {
4666 free(opt_pidfile);
4667 opt_pidfile = strdup(arg);
4668 if (!opt_pidfile) {
4669 PERROR("strdup");
4670 ret = -ENOMEM;
4671 }
4672 }
4673 break;
4674 case 'J': /* Agent TCP port. */
4675 {
4676 if (lttng_is_setuid_setgid()) {
4677 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4678 "--agent-tcp-port");
4679 } else {
4680 unsigned long v;
4681
4682 if (!arg) {
4683 ret = -EINVAL;
4684 goto end;
4685 }
4686 errno = 0;
4687 v = strtoul(arg, NULL, 0);
4688 if (errno != 0 || !isdigit(arg[0])) {
4689 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4690 return -1;
4691 }
4692 if (v == 0 || v >= 65535) {
4693 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4694 return -1;
4695 }
4696 agent_tcp_port = (uint32_t) v;
4697 DBG3("Agent TCP port set to non default: %u", agent_tcp_port);
4698 }
4699 break;
4700 }
4701 case 'l':
4702 if (lttng_is_setuid_setgid()) {
4703 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4704 "-l, --load");
4705 } else {
4706 free(opt_load_session_path);
4707 opt_load_session_path = strdup(arg);
4708 if (!opt_load_session_path) {
4709 PERROR("strdup");
4710 ret = -ENOMEM;
4711 }
4712 }
4713 break;
4714 case 'P': /* probe modules list */
4715 if (lttng_is_setuid_setgid()) {
4716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4717 "--kmod-probes");
4718 } else {
4719 free(kmod_probes_list);
4720 kmod_probes_list = strdup(arg);
4721 if (!kmod_probes_list) {
4722 PERROR("strdup");
4723 ret = -ENOMEM;
4724 }
4725 }
4726 break;
4727 case 'e':
4728 if (lttng_is_setuid_setgid()) {
4729 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4730 "--extra-kmod-probes");
4731 } else {
4732 free(kmod_extra_probes_list);
4733 kmod_extra_probes_list = strdup(arg);
4734 if (!kmod_extra_probes_list) {
4735 PERROR("strdup");
4736 ret = -ENOMEM;
4737 }
4738 }
4739 break;
4740 case 'f':
4741 /* This is handled in set_options() thus silent break. */
4742 break;
4743 default:
4744 /* Unknown option or other error.
4745 * Error is printed by getopt, just return */
4746 ret = -1;
4747 }
4748
4749 end:
4750 if (ret == -EINVAL) {
4751 const char *opt_name = "unknown";
4752 int i;
4753
4754 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
4755 i++) {
4756 if (opt == long_options[i].val) {
4757 opt_name = long_options[i].name;
4758 break;
4759 }
4760 }
4761
4762 WARN("Invalid argument provided for option \"%s\", using default value.",
4763 opt_name);
4764 }
4765
4766 return ret;
4767 }
4768
4769 /*
4770 * config_entry_handler_cb used to handle options read from a config file.
4771 * See config_entry_handler_cb comment in common/config/config.h for the
4772 * return value conventions.
4773 */
4774 static int config_entry_handler(const struct config_entry *entry, void *unused)
4775 {
4776 int ret = 0, i;
4777
4778 if (!entry || !entry->name || !entry->value) {
4779 ret = -EINVAL;
4780 goto end;
4781 }
4782
4783 /* Check if the option is to be ignored */
4784 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4785 if (!strcmp(entry->name, config_ignore_options[i])) {
4786 goto end;
4787 }
4788 }
4789
4790 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4791 i++) {
4792
4793 /* Ignore if not fully matched. */
4794 if (strcmp(entry->name, long_options[i].name)) {
4795 continue;
4796 }
4797
4798 /*
4799 * If the option takes no argument on the command line, we have to
4800 * check if the value is "true". We support non-zero numeric values,
4801 * true, on and yes.
4802 */
4803 if (!long_options[i].has_arg) {
4804 ret = config_parse_value(entry->value);
4805 if (ret <= 0) {
4806 if (ret) {
4807 WARN("Invalid configuration value \"%s\" for option %s",
4808 entry->value, entry->name);
4809 }
4810 /* False, skip boolean config option. */
4811 goto end;
4812 }
4813 }
4814
4815 ret = set_option(long_options[i].val, entry->value, entry->name);
4816 goto end;
4817 }
4818
4819 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4820
4821 end:
4822 return ret;
4823 }
4824
4825 /*
4826 * daemon configuration loading and argument parsing
4827 */
4828 static int set_options(int argc, char **argv)
4829 {
4830 int ret = 0, c = 0, option_index = 0;
4831 int orig_optopt = optopt, orig_optind = optind;
4832 char *optstring;
4833 const char *config_path = NULL;
4834
4835 optstring = utils_generate_optstring(long_options,
4836 sizeof(long_options) / sizeof(struct option));
4837 if (!optstring) {
4838 ret = -ENOMEM;
4839 goto end;
4840 }
4841
4842 /* Check for the --config option */
4843 while ((c = getopt_long(argc, argv, optstring, long_options,
4844 &option_index)) != -1) {
4845 if (c == '?') {
4846 ret = -EINVAL;
4847 goto end;
4848 } else if (c != 'f') {
4849 /* if not equal to --config option. */
4850 continue;
4851 }
4852
4853 if (lttng_is_setuid_setgid()) {
4854 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4855 "-f, --config");
4856 } else {
4857 config_path = utils_expand_path(optarg);
4858 if (!config_path) {
4859 ERR("Failed to resolve path: %s", optarg);
4860 }
4861 }
4862 }
4863
4864 ret = config_get_section_entries(config_path, config_section_name,
4865 config_entry_handler, NULL);
4866 if (ret) {
4867 if (ret > 0) {
4868 ERR("Invalid configuration option at line %i", ret);
4869 ret = -1;
4870 }
4871 goto end;
4872 }
4873
4874 /* Reset getopt's global state */
4875 optopt = orig_optopt;
4876 optind = orig_optind;
4877 while (1) {
4878 c = getopt_long(argc, argv, optstring, long_options, &option_index);
4879 if (c == -1) {
4880 break;
4881 }
4882
4883 ret = set_option(c, optarg, long_options[option_index].name);
4884 if (ret < 0) {
4885 break;
4886 }
4887 }
4888
4889 end:
4890 free(optstring);
4891 return ret;
4892 }
4893
4894 /*
4895 * Creates the two needed socket by the daemon.
4896 * apps_sock - The communication socket for all UST apps.
4897 * client_sock - The communication of the cli tool (lttng).
4898 */
4899 static int init_daemon_socket(void)
4900 {
4901 int ret = 0;
4902 mode_t old_umask;
4903
4904 old_umask = umask(0);
4905
4906 /* Create client tool unix socket */
4907 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4908 if (client_sock < 0) {
4909 ERR("Create unix sock failed: %s", client_unix_sock_path);
4910 ret = -1;
4911 goto end;
4912 }
4913
4914 /* Set the cloexec flag */
4915 ret = utils_set_fd_cloexec(client_sock);
4916 if (ret < 0) {
4917 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4918 "Continuing but note that the consumer daemon will have a "
4919 "reference to this socket on exec()", client_sock);
4920 }
4921
4922 /* File permission MUST be 660 */
4923 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4924 if (ret < 0) {
4925 ERR("Set file permissions failed: %s", client_unix_sock_path);
4926 PERROR("chmod");
4927 goto end;
4928 }
4929
4930 /* Create the application unix socket */
4931 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4932 if (apps_sock < 0) {
4933 ERR("Create unix sock failed: %s", apps_unix_sock_path);
4934 ret = -1;
4935 goto end;
4936 }
4937
4938 /* Set the cloexec flag */
4939 ret = utils_set_fd_cloexec(apps_sock);
4940 if (ret < 0) {
4941 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4942 "Continuing but note that the consumer daemon will have a "
4943 "reference to this socket on exec()", apps_sock);
4944 }
4945
4946 /* File permission MUST be 666 */
4947 ret = chmod(apps_unix_sock_path,
4948 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
4949 if (ret < 0) {
4950 ERR("Set file permissions failed: %s", apps_unix_sock_path);
4951 PERROR("chmod");
4952 goto end;
4953 }
4954
4955 DBG3("Session daemon client socket %d and application socket %d created",
4956 client_sock, apps_sock);
4957
4958 end:
4959 umask(old_umask);
4960 return ret;
4961 }
4962
4963 /*
4964 * Check if the global socket is available, and if a daemon is answering at the
4965 * other side. If yes, error is returned.
4966 */
4967 static int check_existing_daemon(void)
4968 {
4969 /* Is there anybody out there ? */
4970 if (lttng_session_daemon_alive()) {
4971 return -EEXIST;
4972 }
4973
4974 return 0;
4975 }
4976
4977 /*
4978 * Set the tracing group gid onto the client socket.
4979 *
4980 * Race window between mkdir and chown is OK because we are going from more
4981 * permissive (root.root) to less permissive (root.tracing).
4982 */
4983 static int set_permissions(char *rundir)
4984 {
4985 int ret;
4986 gid_t gid;
4987
4988 gid = utils_get_group_id(tracing_group_name);
4989
4990 /* Set lttng run dir */
4991 ret = chown(rundir, 0, gid);
4992 if (ret < 0) {
4993 ERR("Unable to set group on %s", rundir);
4994 PERROR("chown");
4995 }
4996
4997 /*
4998 * Ensure all applications and tracing group can search the run
4999 * dir. Allow everyone to read the directory, since it does not
5000 * buy us anything to hide its content.
5001 */
5002 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
5003 if (ret < 0) {
5004 ERR("Unable to set permissions on %s", rundir);
5005 PERROR("chmod");
5006 }
5007
5008 /* lttng client socket path */
5009 ret = chown(client_unix_sock_path, 0, gid);
5010 if (ret < 0) {
5011 ERR("Unable to set group on %s", client_unix_sock_path);
5012 PERROR("chown");
5013 }
5014
5015 /* kconsumer error socket path */
5016 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
5017 if (ret < 0) {
5018 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
5019 PERROR("chown");
5020 }
5021
5022 /* 64-bit ustconsumer error socket path */
5023 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
5024 if (ret < 0) {
5025 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
5026 PERROR("chown");
5027 }
5028
5029 /* 32-bit ustconsumer compat32 error socket path */
5030 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
5031 if (ret < 0) {
5032 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
5033 PERROR("chown");
5034 }
5035
5036 DBG("All permissions are set");
5037
5038 return ret;
5039 }
5040
5041 /*
5042 * Create the lttng run directory needed for all global sockets and pipe.
5043 */
5044 static int create_lttng_rundir(const char *rundir)
5045 {
5046 int ret;
5047
5048 DBG3("Creating LTTng run directory: %s", rundir);
5049
5050 ret = mkdir(rundir, S_IRWXU);
5051 if (ret < 0) {
5052 if (errno != EEXIST) {
5053 ERR("Unable to create %s", rundir);
5054 goto error;
5055 } else {
5056 ret = 0;
5057 }
5058 }
5059
5060 error:
5061 return ret;
5062 }
5063
5064 /*
5065 * Setup sockets and directory needed by the kconsumerd communication with the
5066 * session daemon.
5067 */
5068 static int set_consumer_sockets(struct consumer_data *consumer_data,
5069 const char *rundir)
5070 {
5071 int ret;
5072 char path[PATH_MAX];
5073
5074 switch (consumer_data->type) {
5075 case LTTNG_CONSUMER_KERNEL:
5076 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
5077 break;
5078 case LTTNG_CONSUMER64_UST:
5079 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
5080 break;
5081 case LTTNG_CONSUMER32_UST:
5082 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
5083 break;
5084 default:
5085 ERR("Consumer type unknown");
5086 ret = -EINVAL;
5087 goto error;
5088 }
5089
5090 DBG2("Creating consumer directory: %s", path);
5091
5092 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
5093 if (ret < 0) {
5094 if (errno != EEXIST) {
5095 PERROR("mkdir");
5096 ERR("Failed to create %s", path);
5097 goto error;
5098 }
5099 ret = -1;
5100 }
5101 if (is_root) {
5102 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
5103 if (ret < 0) {
5104 ERR("Unable to set group on %s", path);
5105 PERROR("chown");
5106 goto error;
5107 }
5108 }
5109
5110 /* Create the kconsumerd error unix socket */
5111 consumer_data->err_sock =
5112 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5113 if (consumer_data->err_sock < 0) {
5114 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
5115 ret = -1;
5116 goto error;
5117 }
5118
5119 /*
5120 * Set the CLOEXEC flag. Return code is useless because either way, the
5121 * show must go on.
5122 */
5123 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5124 if (ret < 0) {
5125 PERROR("utils_set_fd_cloexec");
5126 /* continue anyway */
5127 }
5128
5129 /* File permission MUST be 660 */
5130 ret = chmod(consumer_data->err_unix_sock_path,
5131 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5132 if (ret < 0) {
5133 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
5134 PERROR("chmod");
5135 goto error;
5136 }
5137
5138 error:
5139 return ret;
5140 }
5141
5142 /*
5143 * Signal handler for the daemon
5144 *
5145 * Simply stop all worker threads, leaving main() return gracefully after
5146 * joining all threads and calling cleanup().
5147 */
5148 static void sighandler(int sig)
5149 {
5150 switch (sig) {
5151 case SIGPIPE:
5152 DBG("SIGPIPE caught");
5153 return;
5154 case SIGINT:
5155 DBG("SIGINT caught");
5156 stop_threads();
5157 break;
5158 case SIGTERM:
5159 DBG("SIGTERM caught");
5160 stop_threads();
5161 break;
5162 case SIGUSR1:
5163 CMM_STORE_SHARED(recv_child_signal, 1);
5164 break;
5165 default:
5166 break;
5167 }
5168 }
5169
5170 /*
5171 * Setup signal handler for :
5172 * SIGINT, SIGTERM, SIGPIPE
5173 */
5174 static int set_signal_handler(void)
5175 {
5176 int ret = 0;
5177 struct sigaction sa;
5178 sigset_t sigset;
5179
5180 if ((ret = sigemptyset(&sigset)) < 0) {
5181 PERROR("sigemptyset");
5182 return ret;
5183 }
5184
5185 sa.sa_handler = sighandler;
5186 sa.sa_mask = sigset;
5187 sa.sa_flags = 0;
5188 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
5189 PERROR("sigaction");
5190 return ret;
5191 }
5192
5193 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
5194 PERROR("sigaction");
5195 return ret;
5196 }
5197
5198 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
5199 PERROR("sigaction");
5200 return ret;
5201 }
5202
5203 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
5204 PERROR("sigaction");
5205 return ret;
5206 }
5207
5208 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5209
5210 return ret;
5211 }
5212
5213 /*
5214 * Set open files limit to unlimited. This daemon can open a large number of
5215 * file descriptors in order to consumer multiple kernel traces.
5216 */
5217 static void set_ulimit(void)
5218 {
5219 int ret;
5220 struct rlimit lim;
5221
5222 /* The kernel does not allowed an infinite limit for open files */
5223 lim.rlim_cur = 65535;
5224 lim.rlim_max = 65535;
5225
5226 ret = setrlimit(RLIMIT_NOFILE, &lim);
5227 if (ret < 0) {
5228 PERROR("failed to set open files limit");
5229 }
5230 }
5231
5232 /*
5233 * Write pidfile using the rundir and opt_pidfile.
5234 */
5235 static int write_pidfile(void)
5236 {
5237 int ret;
5238 char pidfile_path[PATH_MAX];
5239
5240 assert(rundir);
5241
5242 if (opt_pidfile) {
5243 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
5244 } else {
5245 /* Build pidfile path from rundir and opt_pidfile. */
5246 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
5247 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
5248 if (ret < 0) {
5249 PERROR("snprintf pidfile path");
5250 goto error;
5251 }
5252 }
5253
5254 /*
5255 * Create pid file in rundir.
5256 */
5257 ret = utils_create_pid_file(getpid(), pidfile_path);
5258 error:
5259 return ret;
5260 }
5261
5262 /*
5263 * Create lockfile using the rundir and return its fd.
5264 */
5265 static int create_lockfile(void)
5266 {
5267 int ret;
5268 char lockfile_path[PATH_MAX];
5269
5270 ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path));
5271 if (ret < 0) {
5272 goto error;
5273 }
5274
5275 ret = utils_create_lock_file(lockfile_path);
5276 error:
5277 return ret;
5278 }
5279
5280 /*
5281 * Write agent TCP port using the rundir.
5282 */
5283 static int write_agent_port(void)
5284 {
5285 int ret;
5286 char path[PATH_MAX];
5287
5288 assert(rundir);
5289
5290 ret = snprintf(path, sizeof(path), "%s/"
5291 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE, rundir);
5292 if (ret < 0) {
5293 PERROR("snprintf agent port path");
5294 goto error;
5295 }
5296
5297 /*
5298 * Create TCP agent port file in rundir.
5299 */
5300 ret = utils_create_pid_file(agent_tcp_port, path);
5301
5302 error:
5303 return ret;
5304 }
5305
5306 /*
5307 * main
5308 */
5309 int main(int argc, char **argv)
5310 {
5311 int ret = 0, retval = 0;
5312 void *status;
5313 const char *home_path, *env_app_timeout;
5314
5315 init_kernel_workarounds();
5316
5317 rcu_register_thread();
5318
5319 if (set_signal_handler()) {
5320 retval = -1;
5321 goto exit_set_signal_handler;
5322 }
5323
5324 setup_consumerd_path();
5325
5326 page_size = sysconf(_SC_PAGESIZE);
5327 if (page_size < 0) {
5328 PERROR("sysconf _SC_PAGESIZE");
5329 page_size = LONG_MAX;
5330 WARN("Fallback page size to %ld", page_size);
5331 }
5332
5333 /*
5334 * Parse arguments and load the daemon configuration file.
5335 *
5336 * We have an exit_options exit path to free memory reserved by
5337 * set_options. This is needed because the rest of sessiond_cleanup()
5338 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5339 * depends on set_options.
5340 */
5341 progname = argv[0];
5342 if (set_options(argc, argv)) {
5343 retval = -1;
5344 goto exit_options;
5345 }
5346
5347 /* Daemonize */
5348 if (opt_daemon || opt_background) {
5349 int i;
5350
5351 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
5352 !opt_background);
5353 if (ret < 0) {
5354 retval = -1;
5355 goto exit_options;
5356 }
5357
5358 /*
5359 * We are in the child. Make sure all other file descriptors are
5360 * closed, in case we are called with more opened file
5361 * descriptors than the standard ones.
5362 */
5363 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5364 (void) close(i);
5365 }
5366 }
5367
5368 /*
5369 * Starting from here, we can create threads. This needs to be after
5370 * lttng_daemonize due to RCU.
5371 */
5372
5373 /*
5374 * Initialize the health check subsystem. This call should set the
5375 * appropriate time values.
5376 */
5377 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5378 if (!health_sessiond) {
5379 PERROR("health_app_create error");
5380 retval = -1;
5381 goto exit_health_sessiond_cleanup;
5382 }
5383
5384 if (init_ht_cleanup_quit_pipe()) {
5385 retval = -1;
5386 goto exit_ht_cleanup_quit_pipe;
5387 }
5388
5389 /* Setup the thread ht_cleanup communication pipe. */
5390 if (utils_create_pipe_cloexec(ht_cleanup_pipe)) {
5391 retval = -1;
5392 goto exit_ht_cleanup_pipe;
5393 }
5394
5395 /* Set up max poll set size */
5396 if (lttng_poll_set_max_size()) {
5397 retval = -1;
5398 goto exit_set_max_size;
5399 }
5400
5401 /* Create thread to clean up RCU hash tables */
5402 ret = pthread_create(&ht_cleanup_thread, NULL,
5403 thread_ht_cleanup, (void *) NULL);
5404 if (ret) {
5405 errno = ret;
5406 PERROR("pthread_create ht_cleanup");
5407 retval = -1;
5408 goto exit_ht_cleanup;
5409 }
5410
5411 /* Create thread quit pipe */
5412 if (init_thread_quit_pipe()) {
5413 retval = -1;
5414 goto exit_init_data;
5415 }
5416
5417 /* Check if daemon is UID = 0 */
5418 is_root = !getuid();
5419
5420 if (is_root) {
5421 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
5422 if (!rundir) {
5423 retval = -1;
5424 goto exit_init_data;
5425 }
5426
5427 /* Create global run dir with root access */
5428 if (create_lttng_rundir(rundir)) {
5429 retval = -1;
5430 goto exit_init_data;
5431 }
5432
5433 if (strlen(apps_unix_sock_path) == 0) {
5434 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5435 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
5436 if (ret < 0) {
5437 retval = -1;
5438 goto exit_init_data;
5439 }
5440 }
5441
5442 if (strlen(client_unix_sock_path) == 0) {
5443 ret = snprintf(client_unix_sock_path, PATH_MAX,
5444 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
5445 if (ret < 0) {
5446 retval = -1;
5447 goto exit_init_data;
5448 }
5449 }
5450
5451 /* Set global SHM for ust */
5452 if (strlen(wait_shm_path) == 0) {
5453 ret = snprintf(wait_shm_path, PATH_MAX,
5454 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
5455 if (ret < 0) {
5456 retval = -1;
5457 goto exit_init_data;
5458 }
5459 }
5460
5461 if (strlen(health_unix_sock_path) == 0) {
5462 ret = snprintf(health_unix_sock_path,
5463 sizeof(health_unix_sock_path),
5464 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
5465 if (ret < 0) {
5466 retval = -1;
5467 goto exit_init_data;
5468 }
5469 }
5470
5471 /* Setup kernel consumerd path */
5472 ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
5473 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
5474 if (ret < 0) {
5475 retval = -1;
5476 goto exit_init_data;
5477 }
5478 ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
5479 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
5480 if (ret < 0) {
5481 retval = -1;
5482 goto exit_init_data;
5483 }
5484
5485 DBG2("Kernel consumer err path: %s",
5486 kconsumer_data.err_unix_sock_path);
5487 DBG2("Kernel consumer cmd path: %s",
5488 kconsumer_data.cmd_unix_sock_path);
5489 } else {
5490 home_path = utils_get_home_dir();
5491 if (home_path == NULL) {
5492 /* TODO: Add --socket PATH option */
5493 ERR("Can't get HOME directory for sockets creation.");
5494 retval = -1;
5495 goto exit_init_data;
5496 }
5497
5498 /*
5499 * Create rundir from home path. This will create something like
5500 * $HOME/.lttng
5501 */
5502 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
5503 if (ret < 0) {
5504 retval = -1;
5505 goto exit_init_data;
5506 }
5507
5508 if (create_lttng_rundir(rundir)) {
5509 retval = -1;
5510 goto exit_init_data;
5511 }
5512
5513 if (strlen(apps_unix_sock_path) == 0) {
5514 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5515 DEFAULT_HOME_APPS_UNIX_SOCK,
5516 home_path);
5517 if (ret < 0) {
5518 retval = -1;
5519 goto exit_init_data;
5520 }
5521 }
5522
5523 /* Set the cli tool unix socket path */
5524 if (strlen(client_unix_sock_path) == 0) {
5525 ret = snprintf(client_unix_sock_path, PATH_MAX,
5526 DEFAULT_HOME_CLIENT_UNIX_SOCK,
5527 home_path);
5528 if (ret < 0) {
5529 retval = -1;
5530 goto exit_init_data;
5531 }
5532 }
5533
5534 /* Set global SHM for ust */
5535 if (strlen(wait_shm_path) == 0) {
5536 ret = snprintf(wait_shm_path, PATH_MAX,
5537 DEFAULT_HOME_APPS_WAIT_SHM_PATH,
5538 getuid());
5539 if (ret < 0) {
5540 retval = -1;
5541 goto exit_init_data;
5542 }
5543 }
5544
5545 /* Set health check Unix path */
5546 if (strlen(health_unix_sock_path) == 0) {
5547 ret = snprintf(health_unix_sock_path,
5548 sizeof(health_unix_sock_path),
5549 DEFAULT_HOME_HEALTH_UNIX_SOCK,
5550 home_path);
5551 if (ret < 0) {
5552 retval = -1;
5553 goto exit_init_data;
5554 }
5555 }
5556 }
5557
5558 lockfile_fd = create_lockfile();
5559 if (lockfile_fd < 0) {
5560 retval = -1;
5561 goto exit_init_data;
5562 }
5563
5564 /* Set consumer initial state */
5565 kernel_consumerd_state = CONSUMER_STOPPED;
5566 ust_consumerd_state = CONSUMER_STOPPED;
5567
5568 DBG("Client socket path %s", client_unix_sock_path);
5569 DBG("Application socket path %s", apps_unix_sock_path);
5570 DBG("Application wait path %s", wait_shm_path);
5571 DBG("LTTng run directory path: %s", rundir);
5572
5573 /* 32 bits consumerd path setup */
5574 ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
5575 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
5576 if (ret < 0) {
5577 PERROR("snprintf 32-bit consumer error socket path");
5578 retval = -1;
5579 goto exit_init_data;
5580 }
5581 ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
5582 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
5583 if (ret < 0) {
5584 PERROR("snprintf 32-bit consumer command socket path");
5585 retval = -1;
5586 goto exit_init_data;
5587 }
5588
5589 DBG2("UST consumer 32 bits err path: %s",
5590 ustconsumer32_data.err_unix_sock_path);
5591 DBG2("UST consumer 32 bits cmd path: %s",
5592 ustconsumer32_data.cmd_unix_sock_path);
5593
5594 /* 64 bits consumerd path setup */
5595 ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
5596 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
5597 if (ret < 0) {
5598 PERROR("snprintf 64-bit consumer error socket path");
5599 retval = -1;
5600 goto exit_init_data;
5601 }
5602 ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
5603 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
5604 if (ret < 0) {
5605 PERROR("snprintf 64-bit consumer command socket path");
5606 retval = -1;
5607 goto exit_init_data;
5608 }
5609
5610 DBG2("UST consumer 64 bits err path: %s",
5611 ustconsumer64_data.err_unix_sock_path);
5612 DBG2("UST consumer 64 bits cmd path: %s",
5613 ustconsumer64_data.cmd_unix_sock_path);
5614
5615 /*
5616 * See if daemon already exist.
5617 */
5618 if (check_existing_daemon()) {
5619 ERR("Already running daemon.\n");
5620 /*
5621 * We do not goto exit because we must not cleanup()
5622 * because a daemon is already running.
5623 */
5624 retval = -1;
5625 goto exit_init_data;
5626 }
5627
5628 /*
5629 * Init UST app hash table. Alloc hash table before this point since
5630 * cleanup() can get called after that point.
5631 */
5632 if (ust_app_ht_alloc()) {
5633 retval = -1;
5634 goto exit_init_data;
5635 }
5636
5637 /* Initialize agent domain subsystem. */
5638 if (agent_setup()) {
5639 /* ENOMEM at this point. */
5640 retval = -1;
5641 goto exit_init_data;
5642 }
5643
5644 /*
5645 * These actions must be executed as root. We do that *after* setting up
5646 * the sockets path because we MUST make the check for another daemon using
5647 * those paths *before* trying to set the kernel consumer sockets and init
5648 * kernel tracer.
5649 */
5650 if (is_root) {
5651 if (set_consumer_sockets(&kconsumer_data, rundir)) {
5652 retval = -1;
5653 goto exit_init_data;
5654 }
5655
5656 /* Setup kernel tracer */
5657 if (!opt_no_kernel) {
5658 init_kernel_tracer();
5659 if (kernel_tracer_fd >= 0) {
5660 ret = syscall_init_table();
5661 if (ret < 0) {
5662 ERR("Unable to populate syscall table. "
5663 "Syscall tracing won't work "
5664 "for this session daemon.");
5665 }
5666 }
5667 }
5668
5669 /* Set ulimit for open files */
5670 set_ulimit();
5671 }
5672 /* init lttng_fd tracking must be done after set_ulimit. */
5673 lttng_fd_init();
5674
5675 if (set_consumer_sockets(&ustconsumer64_data, rundir)) {
5676 retval = -1;
5677 goto exit_init_data;
5678 }
5679
5680 if (set_consumer_sockets(&ustconsumer32_data, rundir)) {
5681 retval = -1;
5682 goto exit_init_data;
5683 }
5684
5685 /* Setup the needed unix socket */
5686 if (init_daemon_socket()) {
5687 retval = -1;
5688 goto exit_init_data;
5689 }
5690
5691 /* Set credentials to socket */
5692 if (is_root && set_permissions(rundir)) {
5693 retval = -1;
5694 goto exit_init_data;
5695 }
5696
5697 /* Get parent pid if -S, --sig-parent is specified. */
5698 if (opt_sig_parent) {
5699 ppid = getppid();
5700 }
5701
5702 /* Setup the kernel pipe for waking up the kernel thread */
5703 if (is_root && !opt_no_kernel) {
5704 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
5705 retval = -1;
5706 goto exit_init_data;
5707 }
5708 }
5709
5710 /* Setup the thread apps communication pipe. */
5711 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
5712 retval = -1;
5713 goto exit_init_data;
5714 }
5715
5716 /* Setup the thread apps notify communication pipe. */
5717 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
5718 retval = -1;
5719 goto exit_init_data;
5720 }
5721
5722 /* Initialize global buffer per UID and PID registry. */
5723 buffer_reg_init_uid_registry();
5724 buffer_reg_init_pid_registry();
5725
5726 /* Init UST command queue. */
5727 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
5728
5729 /*
5730 * Get session list pointer. This pointer MUST NOT be free'd. This list
5731 * is statically declared in session.c
5732 */
5733 session_list_ptr = session_get_list();
5734
5735 cmd_init();
5736
5737 /* Check for the application socket timeout env variable. */
5738 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5739 if (env_app_timeout) {
5740 app_socket_timeout = atoi(env_app_timeout);
5741 } else {
5742 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5743 }
5744
5745 ret = write_pidfile();
5746 if (ret) {
5747 ERR("Error in write_pidfile");
5748 retval = -1;
5749 goto exit_init_data;
5750 }
5751 ret = write_agent_port();
5752 if (ret) {
5753 ERR("Error in write_agent_port");
5754 retval = -1;
5755 goto exit_init_data;
5756 }
5757
5758 /* Initialize communication library */
5759 lttcomm_init();
5760 /* Initialize TCP timeout values */
5761 lttcomm_inet_init();
5762
5763 if (load_session_init_data(&load_info) < 0) {
5764 retval = -1;
5765 goto exit_init_data;
5766 }
5767 load_info->path = opt_load_session_path;
5768
5769 /* Create health-check thread */
5770 ret = pthread_create(&health_thread, NULL,
5771 thread_manage_health, (void *) NULL);
5772 if (ret) {
5773 errno = ret;
5774 PERROR("pthread_create health");
5775 retval = -1;
5776 goto exit_health;
5777 }
5778
5779 /* Create thread to manage the client socket */
5780 ret = pthread_create(&client_thread, NULL,
5781 thread_manage_clients, (void *) NULL);
5782 if (ret) {
5783 errno = ret;
5784 PERROR("pthread_create clients");
5785 retval = -1;
5786 goto exit_client;
5787 }
5788
5789 /* Create thread to dispatch registration */
5790 ret = pthread_create(&dispatch_thread, NULL,
5791 thread_dispatch_ust_registration, (void *) NULL);
5792 if (ret) {
5793 errno = ret;
5794 PERROR("pthread_create dispatch");
5795 retval = -1;
5796 goto exit_dispatch;
5797 }
5798
5799 /* Create thread to manage application registration. */
5800 ret = pthread_create(&reg_apps_thread, NULL,
5801 thread_registration_apps, (void *) NULL);
5802 if (ret) {
5803 errno = ret;
5804 PERROR("pthread_create registration");
5805 retval = -1;
5806 goto exit_reg_apps;
5807 }
5808
5809 /* Create thread to manage application socket */
5810 ret = pthread_create(&apps_thread, NULL,
5811 thread_manage_apps, (void *) NULL);
5812 if (ret) {
5813 errno = ret;
5814 PERROR("pthread_create apps");
5815 retval = -1;
5816 goto exit_apps;
5817 }
5818
5819 /* Create thread to manage application notify socket */
5820 ret = pthread_create(&apps_notify_thread, NULL,
5821 ust_thread_manage_notify, (void *) NULL);
5822 if (ret) {
5823 errno = ret;
5824 PERROR("pthread_create notify");
5825 retval = -1;
5826 goto exit_apps_notify;
5827 }
5828
5829 /* Create agent registration thread. */
5830 ret = pthread_create(&agent_reg_thread, NULL,
5831 agent_thread_manage_registration, (void *) NULL);
5832 if (ret) {
5833 errno = ret;
5834 PERROR("pthread_create agent");
5835 retval = -1;
5836 goto exit_agent_reg;
5837 }
5838
5839 /* Don't start this thread if kernel tracing is not requested nor root */
5840 if (is_root && !opt_no_kernel) {
5841 /* Create kernel thread to manage kernel event */
5842 ret = pthread_create(&kernel_thread, NULL,
5843 thread_manage_kernel, (void *) NULL);
5844 if (ret) {
5845 errno = ret;
5846 PERROR("pthread_create kernel");
5847 retval = -1;
5848 goto exit_kernel;
5849 }
5850 }
5851
5852 /* Create session loading thread. */
5853 ret = pthread_create(&load_session_thread, NULL, thread_load_session,
5854 load_info);
5855 if (ret) {
5856 errno = ret;
5857 PERROR("pthread_create load_session_thread");
5858 retval = -1;
5859 goto exit_load_session;
5860 }
5861
5862 /*
5863 * This is where we start awaiting program completion (e.g. through
5864 * signal that asks threads to teardown).
5865 */
5866
5867 ret = pthread_join(load_session_thread, &status);
5868 if (ret) {
5869 errno = ret;
5870 PERROR("pthread_join load_session_thread");
5871 retval = -1;
5872 }
5873 exit_load_session:
5874
5875 if (is_root && !opt_no_kernel) {
5876 ret = pthread_join(kernel_thread, &status);
5877 if (ret) {
5878 errno = ret;
5879 PERROR("pthread_join");
5880 retval = -1;
5881 }
5882 }
5883 exit_kernel:
5884
5885 ret = pthread_join(agent_reg_thread, &status);
5886 if (ret) {
5887 errno = ret;
5888 PERROR("pthread_join agent");
5889 retval = -1;
5890 }
5891 exit_agent_reg:
5892
5893 ret = pthread_join(apps_notify_thread, &status);
5894 if (ret) {
5895 errno = ret;
5896 PERROR("pthread_join apps notify");
5897 retval = -1;
5898 }
5899 exit_apps_notify:
5900
5901 ret = pthread_join(apps_thread, &status);
5902 if (ret) {
5903 errno = ret;
5904 PERROR("pthread_join apps");
5905 retval = -1;
5906 }
5907 exit_apps:
5908
5909 ret = pthread_join(reg_apps_thread, &status);
5910 if (ret) {
5911 errno = ret;
5912 PERROR("pthread_join");
5913 retval = -1;
5914 }
5915 exit_reg_apps:
5916
5917 ret = pthread_join(dispatch_thread, &status);
5918 if (ret) {
5919 errno = ret;
5920 PERROR("pthread_join");
5921 retval = -1;
5922 }
5923 exit_dispatch:
5924
5925 ret = pthread_join(client_thread, &status);
5926 if (ret) {
5927 errno = ret;
5928 PERROR("pthread_join");
5929 retval = -1;
5930 }
5931 exit_client:
5932
5933 ret = pthread_join(health_thread, &status);
5934 if (ret) {
5935 errno = ret;
5936 PERROR("pthread_join health thread");
5937 retval = -1;
5938 }
5939 exit_health:
5940
5941 exit_init_data:
5942 /*
5943 * sessiond_cleanup() is called when no other thread is running, except
5944 * the ht_cleanup thread, which is needed to destroy the hash tables.
5945 */
5946 rcu_thread_online();
5947 sessiond_cleanup();
5948 rcu_thread_offline();
5949 rcu_unregister_thread();
5950
5951 ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]);
5952 if (ret < 0) {
5953 ERR("write error on ht_cleanup quit pipe");
5954 retval = -1;
5955 }
5956
5957 ret = pthread_join(ht_cleanup_thread, &status);
5958 if (ret) {
5959 errno = ret;
5960 PERROR("pthread_join ht cleanup thread");
5961 retval = -1;
5962 }
5963 exit_ht_cleanup:
5964 exit_set_max_size:
5965
5966 utils_close_pipe(ht_cleanup_pipe);
5967 exit_ht_cleanup_pipe:
5968
5969 /*
5970 * Close the ht_cleanup quit pipe.
5971 */
5972 utils_close_pipe(ht_cleanup_quit_pipe);
5973 exit_ht_cleanup_quit_pipe:
5974
5975 health_app_destroy(health_sessiond);
5976 exit_health_sessiond_cleanup:
5977
5978 exit_options:
5979 sessiond_cleanup_options();
5980
5981 exit_set_signal_handler:
5982 if (!retval) {
5983 exit(EXIT_SUCCESS);
5984 } else {
5985 exit(EXIT_FAILURE);
5986 }
5987 }
This page took 0.200836 seconds and 5 git commands to generate.