Implement UST PID tracker
[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 break;
2964 default:
2965 /* Setup lttng message with no payload */
2966 ret = setup_lttng_msg(cmd_ctx, 0);
2967 if (ret < 0) {
2968 /* This label does not try to unlock the session */
2969 goto init_setup_error;
2970 }
2971 }
2972
2973 /* Commands that DO NOT need a session. */
2974 switch (cmd_ctx->lsm->cmd_type) {
2975 case LTTNG_CREATE_SESSION:
2976 case LTTNG_CREATE_SESSION_SNAPSHOT:
2977 case LTTNG_CREATE_SESSION_LIVE:
2978 case LTTNG_CALIBRATE:
2979 case LTTNG_LIST_SESSIONS:
2980 case LTTNG_LIST_TRACEPOINTS:
2981 case LTTNG_LIST_SYSCALLS:
2982 case LTTNG_LIST_TRACEPOINT_FIELDS:
2983 case LTTNG_SAVE_SESSION:
2984 need_tracing_session = 0;
2985 break;
2986 default:
2987 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2988 /*
2989 * We keep the session list lock across _all_ commands
2990 * for now, because the per-session lock does not
2991 * handle teardown properly.
2992 */
2993 session_lock_list();
2994 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2995 if (cmd_ctx->session == NULL) {
2996 ret = LTTNG_ERR_SESS_NOT_FOUND;
2997 goto error;
2998 } else {
2999 /* Acquire lock for the session */
3000 session_lock(cmd_ctx->session);
3001 }
3002 break;
3003 }
3004
3005 /*
3006 * Commands that need a valid session but should NOT create one if none
3007 * exists. Instead of creating one and destroying it when the command is
3008 * handled, process that right before so we save some round trip in useless
3009 * code path.
3010 */
3011 switch (cmd_ctx->lsm->cmd_type) {
3012 case LTTNG_DISABLE_CHANNEL:
3013 case LTTNG_DISABLE_EVENT:
3014 switch (cmd_ctx->lsm->domain.type) {
3015 case LTTNG_DOMAIN_KERNEL:
3016 if (!cmd_ctx->session->kernel_session) {
3017 ret = LTTNG_ERR_NO_CHANNEL;
3018 goto error;
3019 }
3020 break;
3021 case LTTNG_DOMAIN_JUL:
3022 case LTTNG_DOMAIN_LOG4J:
3023 case LTTNG_DOMAIN_PYTHON:
3024 case LTTNG_DOMAIN_UST:
3025 if (!cmd_ctx->session->ust_session) {
3026 ret = LTTNG_ERR_NO_CHANNEL;
3027 goto error;
3028 }
3029 break;
3030 default:
3031 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3032 goto error;
3033 }
3034 default:
3035 break;
3036 }
3037
3038 if (!need_domain) {
3039 goto skip_domain;
3040 }
3041
3042 /*
3043 * Check domain type for specific "pre-action".
3044 */
3045 switch (cmd_ctx->lsm->domain.type) {
3046 case LTTNG_DOMAIN_KERNEL:
3047 if (!is_root) {
3048 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3049 goto error;
3050 }
3051
3052 /* Kernel tracer check */
3053 if (kernel_tracer_fd == -1) {
3054 /* Basically, load kernel tracer modules */
3055 ret = init_kernel_tracer();
3056 if (ret != 0) {
3057 goto error;
3058 }
3059 }
3060
3061 /* Consumer is in an ERROR state. Report back to client */
3062 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
3063 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3064 goto error;
3065 }
3066
3067 /* Need a session for kernel command */
3068 if (need_tracing_session) {
3069 if (cmd_ctx->session->kernel_session == NULL) {
3070 ret = create_kernel_session(cmd_ctx->session);
3071 if (ret < 0) {
3072 ret = LTTNG_ERR_KERN_SESS_FAIL;
3073 goto error;
3074 }
3075 }
3076
3077 /* Start the kernel consumer daemon */
3078 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3079 if (kconsumer_data.pid == 0 &&
3080 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3081 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3082 ret = start_consumerd(&kconsumer_data);
3083 if (ret < 0) {
3084 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3085 goto error;
3086 }
3087 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3088 } else {
3089 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3090 }
3091
3092 /*
3093 * The consumer was just spawned so we need to add the socket to
3094 * the consumer output of the session if exist.
3095 */
3096 ret = consumer_create_socket(&kconsumer_data,
3097 cmd_ctx->session->kernel_session->consumer);
3098 if (ret < 0) {
3099 goto error;
3100 }
3101 }
3102
3103 break;
3104 case LTTNG_DOMAIN_JUL:
3105 case LTTNG_DOMAIN_LOG4J:
3106 case LTTNG_DOMAIN_PYTHON:
3107 case LTTNG_DOMAIN_UST:
3108 {
3109 if (!ust_app_supported()) {
3110 ret = LTTNG_ERR_NO_UST;
3111 goto error;
3112 }
3113 /* Consumer is in an ERROR state. Report back to client */
3114 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
3115 ret = LTTNG_ERR_NO_USTCONSUMERD;
3116 goto error;
3117 }
3118
3119 if (need_tracing_session) {
3120 /* Create UST session if none exist. */
3121 if (cmd_ctx->session->ust_session == NULL) {
3122 ret = create_ust_session(cmd_ctx->session,
3123 &cmd_ctx->lsm->domain);
3124 if (ret != LTTNG_OK) {
3125 goto error;
3126 }
3127 }
3128
3129 /* Start the UST consumer daemons */
3130 /* 64-bit */
3131 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3132 if (consumerd64_bin[0] != '\0' &&
3133 ustconsumer64_data.pid == 0 &&
3134 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3135 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3136 ret = start_consumerd(&ustconsumer64_data);
3137 if (ret < 0) {
3138 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
3139 uatomic_set(&ust_consumerd64_fd, -EINVAL);
3140 goto error;
3141 }
3142
3143 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
3144 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3145 } else {
3146 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3147 }
3148
3149 /*
3150 * Setup socket for consumer 64 bit. No need for atomic access
3151 * since it was set above and can ONLY be set in this thread.
3152 */
3153 ret = consumer_create_socket(&ustconsumer64_data,
3154 cmd_ctx->session->ust_session->consumer);
3155 if (ret < 0) {
3156 goto error;
3157 }
3158
3159 /* 32-bit */
3160 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
3161 if (consumerd32_bin[0] != '\0' &&
3162 ustconsumer32_data.pid == 0 &&
3163 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3164 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3165 ret = start_consumerd(&ustconsumer32_data);
3166 if (ret < 0) {
3167 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
3168 uatomic_set(&ust_consumerd32_fd, -EINVAL);
3169 goto error;
3170 }
3171
3172 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
3173 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3174 } else {
3175 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3176 }
3177
3178 /*
3179 * Setup socket for consumer 64 bit. No need for atomic access
3180 * since it was set above and can ONLY be set in this thread.
3181 */
3182 ret = consumer_create_socket(&ustconsumer32_data,
3183 cmd_ctx->session->ust_session->consumer);
3184 if (ret < 0) {
3185 goto error;
3186 }
3187 }
3188 break;
3189 }
3190 default:
3191 break;
3192 }
3193 skip_domain:
3194
3195 /* Validate consumer daemon state when start/stop trace command */
3196 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3197 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3198 switch (cmd_ctx->lsm->domain.type) {
3199 case LTTNG_DOMAIN_JUL:
3200 case LTTNG_DOMAIN_LOG4J:
3201 case LTTNG_DOMAIN_PYTHON:
3202 case LTTNG_DOMAIN_UST:
3203 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
3204 ret = LTTNG_ERR_NO_USTCONSUMERD;
3205 goto error;
3206 }
3207 break;
3208 case LTTNG_DOMAIN_KERNEL:
3209 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
3210 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3211 goto error;
3212 }
3213 break;
3214 }
3215 }
3216
3217 /*
3218 * Check that the UID or GID match that of the tracing session.
3219 * The root user can interact with all sessions.
3220 */
3221 if (need_tracing_session) {
3222 if (!session_access_ok(cmd_ctx->session,
3223 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3224 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
3225 ret = LTTNG_ERR_EPERM;
3226 goto error;
3227 }
3228 }
3229
3230 /*
3231 * Send relayd information to consumer as soon as we have a domain and a
3232 * session defined.
3233 */
3234 if (cmd_ctx->session && need_domain) {
3235 /*
3236 * Setup relayd if not done yet. If the relayd information was already
3237 * sent to the consumer, this call will gracefully return.
3238 */
3239 ret = cmd_setup_relayd(cmd_ctx->session);
3240 if (ret != LTTNG_OK) {
3241 goto error;
3242 }
3243 }
3244
3245 /* Process by command type */
3246 switch (cmd_ctx->lsm->cmd_type) {
3247 case LTTNG_ADD_CONTEXT:
3248 {
3249 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3250 cmd_ctx->lsm->u.context.channel_name,
3251 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
3252 break;
3253 }
3254 case LTTNG_DISABLE_CHANNEL:
3255 {
3256 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3257 cmd_ctx->lsm->u.disable.channel_name);
3258 break;
3259 }
3260 case LTTNG_DISABLE_EVENT:
3261 {
3262 /* FIXME: passing packed structure to non-packed pointer */
3263 /* TODO: handle filter */
3264 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3265 cmd_ctx->lsm->u.disable.channel_name,
3266 &cmd_ctx->lsm->u.disable.event);
3267 break;
3268 }
3269 case LTTNG_ENABLE_CHANNEL:
3270 {
3271 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
3272 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
3273 break;
3274 }
3275 case LTTNG_TRACK_PID:
3276 {
3277 ret = cmd_track_pid(cmd_ctx->session,
3278 cmd_ctx->lsm->domain.type,
3279 cmd_ctx->lsm->u.pid_tracker.pid);
3280 break;
3281 }
3282 case LTTNG_UNTRACK_PID:
3283 {
3284 ret = cmd_untrack_pid(cmd_ctx->session,
3285 cmd_ctx->lsm->domain.type,
3286 cmd_ctx->lsm->u.pid_tracker.pid);
3287 break;
3288 }
3289 case LTTNG_ENABLE_EVENT:
3290 {
3291 struct lttng_event_exclusion *exclusion = NULL;
3292 struct lttng_filter_bytecode *bytecode = NULL;
3293 char *filter_expression = NULL;
3294
3295 /* Handle exclusion events and receive it from the client. */
3296 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3297 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3298
3299 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3300 (count * LTTNG_SYMBOL_NAME_LEN));
3301 if (!exclusion) {
3302 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3303 goto error;
3304 }
3305
3306 DBG("Receiving var len exclusion event list from client ...");
3307 exclusion->count = count;
3308 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3309 count * LTTNG_SYMBOL_NAME_LEN);
3310 if (ret <= 0) {
3311 DBG("Nothing recv() from client var len data... continuing");
3312 *sock_error = 1;
3313 free(exclusion);
3314 ret = LTTNG_ERR_EXCLUSION_INVAL;
3315 goto error;
3316 }
3317 }
3318
3319 /* Get filter expression from client. */
3320 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3321 size_t expression_len =
3322 cmd_ctx->lsm->u.enable.expression_len;
3323
3324 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3325 ret = LTTNG_ERR_FILTER_INVAL;
3326 free(exclusion);
3327 goto error;
3328 }
3329
3330 filter_expression = zmalloc(expression_len);
3331 if (!filter_expression) {
3332 free(exclusion);
3333 ret = LTTNG_ERR_FILTER_NOMEM;
3334 goto error;
3335 }
3336
3337 /* Receive var. len. data */
3338 DBG("Receiving var len filter's expression from client ...");
3339 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3340 expression_len);
3341 if (ret <= 0) {
3342 DBG("Nothing recv() from client car len data... continuing");
3343 *sock_error = 1;
3344 free(filter_expression);
3345 free(exclusion);
3346 ret = LTTNG_ERR_FILTER_INVAL;
3347 goto error;
3348 }
3349 }
3350
3351 /* Handle filter and get bytecode from client. */
3352 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3353 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3354
3355 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3356 ret = LTTNG_ERR_FILTER_INVAL;
3357 free(filter_expression);
3358 free(exclusion);
3359 goto error;
3360 }
3361
3362 bytecode = zmalloc(bytecode_len);
3363 if (!bytecode) {
3364 free(filter_expression);
3365 free(exclusion);
3366 ret = LTTNG_ERR_FILTER_NOMEM;
3367 goto error;
3368 }
3369
3370 /* Receive var. len. data */
3371 DBG("Receiving var len filter's bytecode from client ...");
3372 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3373 if (ret <= 0) {
3374 DBG("Nothing recv() from client car len data... continuing");
3375 *sock_error = 1;
3376 free(filter_expression);
3377 free(bytecode);
3378 free(exclusion);
3379 ret = LTTNG_ERR_FILTER_INVAL;
3380 goto error;
3381 }
3382
3383 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3384 free(filter_expression);
3385 free(bytecode);
3386 free(exclusion);
3387 ret = LTTNG_ERR_FILTER_INVAL;
3388 goto error;
3389 }
3390 }
3391
3392 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3393 cmd_ctx->lsm->u.enable.channel_name,
3394 &cmd_ctx->lsm->u.enable.event,
3395 filter_expression, bytecode, exclusion,
3396 kernel_poll_pipe[1]);
3397 break;
3398 }
3399 case LTTNG_LIST_TRACEPOINTS:
3400 {
3401 struct lttng_event *events;
3402 ssize_t nb_events;
3403
3404 session_lock_list();
3405 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3406 session_unlock_list();
3407 if (nb_events < 0) {
3408 /* Return value is a negative lttng_error_code. */
3409 ret = -nb_events;
3410 goto error;
3411 }
3412
3413 /*
3414 * Setup lttng message with payload size set to the event list size in
3415 * bytes and then copy list into the llm payload.
3416 */
3417 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3418 if (ret < 0) {
3419 free(events);
3420 goto setup_error;
3421 }
3422
3423 /* Copy event list into message payload */
3424 memcpy(cmd_ctx->llm->payload, events,
3425 sizeof(struct lttng_event) * nb_events);
3426
3427 free(events);
3428
3429 ret = LTTNG_OK;
3430 break;
3431 }
3432 case LTTNG_LIST_TRACEPOINT_FIELDS:
3433 {
3434 struct lttng_event_field *fields;
3435 ssize_t nb_fields;
3436
3437 session_lock_list();
3438 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3439 &fields);
3440 session_unlock_list();
3441 if (nb_fields < 0) {
3442 /* Return value is a negative lttng_error_code. */
3443 ret = -nb_fields;
3444 goto error;
3445 }
3446
3447 /*
3448 * Setup lttng message with payload size set to the event list size in
3449 * bytes and then copy list into the llm payload.
3450 */
3451 ret = setup_lttng_msg(cmd_ctx,
3452 sizeof(struct lttng_event_field) * nb_fields);
3453 if (ret < 0) {
3454 free(fields);
3455 goto setup_error;
3456 }
3457
3458 /* Copy event list into message payload */
3459 memcpy(cmd_ctx->llm->payload, fields,
3460 sizeof(struct lttng_event_field) * nb_fields);
3461
3462 free(fields);
3463
3464 ret = LTTNG_OK;
3465 break;
3466 }
3467 case LTTNG_LIST_SYSCALLS:
3468 {
3469 struct lttng_event *events;
3470 ssize_t nb_events;
3471
3472 nb_events = cmd_list_syscalls(&events);
3473 if (nb_events < 0) {
3474 /* Return value is a negative lttng_error_code. */
3475 ret = -nb_events;
3476 goto error;
3477 }
3478
3479 /*
3480 * Setup lttng message with payload size set to the event list size in
3481 * bytes and then copy list into the llm payload.
3482 */
3483 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3484 if (ret < 0) {
3485 free(events);
3486 goto setup_error;
3487 }
3488
3489 /* Copy event list into message payload */
3490 memcpy(cmd_ctx->llm->payload, events,
3491 sizeof(struct lttng_event) * nb_events);
3492
3493 free(events);
3494
3495 ret = LTTNG_OK;
3496 break;
3497 }
3498 case LTTNG_SET_CONSUMER_URI:
3499 {
3500 size_t nb_uri, len;
3501 struct lttng_uri *uris;
3502
3503 nb_uri = cmd_ctx->lsm->u.uri.size;
3504 len = nb_uri * sizeof(struct lttng_uri);
3505
3506 if (nb_uri == 0) {
3507 ret = LTTNG_ERR_INVALID;
3508 goto error;
3509 }
3510
3511 uris = zmalloc(len);
3512 if (uris == NULL) {
3513 ret = LTTNG_ERR_FATAL;
3514 goto error;
3515 }
3516
3517 /* Receive variable len data */
3518 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3519 ret = lttcomm_recv_unix_sock(sock, uris, len);
3520 if (ret <= 0) {
3521 DBG("No URIs received from client... continuing");
3522 *sock_error = 1;
3523 ret = LTTNG_ERR_SESSION_FAIL;
3524 free(uris);
3525 goto error;
3526 }
3527
3528 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3529 free(uris);
3530 if (ret != LTTNG_OK) {
3531 goto error;
3532 }
3533
3534
3535 break;
3536 }
3537 case LTTNG_START_TRACE:
3538 {
3539 ret = cmd_start_trace(cmd_ctx->session);
3540 break;
3541 }
3542 case LTTNG_STOP_TRACE:
3543 {
3544 ret = cmd_stop_trace(cmd_ctx->session);
3545 break;
3546 }
3547 case LTTNG_CREATE_SESSION:
3548 {
3549 size_t nb_uri, len;
3550 struct lttng_uri *uris = NULL;
3551
3552 nb_uri = cmd_ctx->lsm->u.uri.size;
3553 len = nb_uri * sizeof(struct lttng_uri);
3554
3555 if (nb_uri > 0) {
3556 uris = zmalloc(len);
3557 if (uris == NULL) {
3558 ret = LTTNG_ERR_FATAL;
3559 goto error;
3560 }
3561
3562 /* Receive variable len data */
3563 DBG("Waiting for %zu URIs from client ...", nb_uri);
3564 ret = lttcomm_recv_unix_sock(sock, uris, len);
3565 if (ret <= 0) {
3566 DBG("No URIs received from client... continuing");
3567 *sock_error = 1;
3568 ret = LTTNG_ERR_SESSION_FAIL;
3569 free(uris);
3570 goto error;
3571 }
3572
3573 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3574 DBG("Creating session with ONE network URI is a bad call");
3575 ret = LTTNG_ERR_SESSION_FAIL;
3576 free(uris);
3577 goto error;
3578 }
3579 }
3580
3581 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
3582 &cmd_ctx->creds, 0);
3583
3584 free(uris);
3585
3586 break;
3587 }
3588 case LTTNG_DESTROY_SESSION:
3589 {
3590 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
3591
3592 /* Set session to NULL so we do not unlock it after free. */
3593 cmd_ctx->session = NULL;
3594 break;
3595 }
3596 case LTTNG_LIST_DOMAINS:
3597 {
3598 ssize_t nb_dom;
3599 struct lttng_domain *domains = NULL;
3600
3601 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3602 if (nb_dom < 0) {
3603 /* Return value is a negative lttng_error_code. */
3604 ret = -nb_dom;
3605 goto error;
3606 }
3607
3608 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
3609 if (ret < 0) {
3610 free(domains);
3611 goto setup_error;
3612 }
3613
3614 /* Copy event list into message payload */
3615 memcpy(cmd_ctx->llm->payload, domains,
3616 nb_dom * sizeof(struct lttng_domain));
3617
3618 free(domains);
3619
3620 ret = LTTNG_OK;
3621 break;
3622 }
3623 case LTTNG_LIST_CHANNELS:
3624 {
3625 int nb_chan;
3626 struct lttng_channel *channels = NULL;
3627
3628 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3629 cmd_ctx->session, &channels);
3630 if (nb_chan < 0) {
3631 /* Return value is a negative lttng_error_code. */
3632 ret = -nb_chan;
3633 goto error;
3634 }
3635
3636 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
3637 if (ret < 0) {
3638 free(channels);
3639 goto setup_error;
3640 }
3641
3642 /* Copy event list into message payload */
3643 memcpy(cmd_ctx->llm->payload, channels,
3644 nb_chan * sizeof(struct lttng_channel));
3645
3646 free(channels);
3647
3648 ret = LTTNG_OK;
3649 break;
3650 }
3651 case LTTNG_LIST_EVENTS:
3652 {
3653 ssize_t nb_event;
3654 struct lttng_event *events = NULL;
3655
3656 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3657 cmd_ctx->lsm->u.list.channel_name, &events);
3658 if (nb_event < 0) {
3659 /* Return value is a negative lttng_error_code. */
3660 ret = -nb_event;
3661 goto error;
3662 }
3663
3664 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
3665 if (ret < 0) {
3666 free(events);
3667 goto setup_error;
3668 }
3669
3670 /* Copy event list into message payload */
3671 memcpy(cmd_ctx->llm->payload, events,
3672 nb_event * sizeof(struct lttng_event));
3673
3674 free(events);
3675
3676 ret = LTTNG_OK;
3677 break;
3678 }
3679 case LTTNG_LIST_SESSIONS:
3680 {
3681 unsigned int nr_sessions;
3682
3683 session_lock_list();
3684 nr_sessions = lttng_sessions_count(
3685 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3686 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3687
3688 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
3689 if (ret < 0) {
3690 session_unlock_list();
3691 goto setup_error;
3692 }
3693
3694 /* Filled the session array */
3695 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
3696 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3697 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3698
3699 session_unlock_list();
3700
3701 ret = LTTNG_OK;
3702 break;
3703 }
3704 case LTTNG_CALIBRATE:
3705 {
3706 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3707 &cmd_ctx->lsm->u.calibrate);
3708 break;
3709 }
3710 case LTTNG_REGISTER_CONSUMER:
3711 {
3712 struct consumer_data *cdata;
3713
3714 switch (cmd_ctx->lsm->domain.type) {
3715 case LTTNG_DOMAIN_KERNEL:
3716 cdata = &kconsumer_data;
3717 break;
3718 default:
3719 ret = LTTNG_ERR_UND;
3720 goto error;
3721 }
3722
3723 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3724 cmd_ctx->lsm->u.reg.path, cdata);
3725 break;
3726 }
3727 case LTTNG_DATA_PENDING:
3728 {
3729 ret = cmd_data_pending(cmd_ctx->session);
3730 break;
3731 }
3732 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3733 {
3734 struct lttcomm_lttng_output_id reply;
3735
3736 ret = cmd_snapshot_add_output(cmd_ctx->session,
3737 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3738 if (ret != LTTNG_OK) {
3739 goto error;
3740 }
3741
3742 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3743 if (ret < 0) {
3744 goto setup_error;
3745 }
3746
3747 /* Copy output list into message payload */
3748 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3749 ret = LTTNG_OK;
3750 break;
3751 }
3752 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3753 {
3754 ret = cmd_snapshot_del_output(cmd_ctx->session,
3755 &cmd_ctx->lsm->u.snapshot_output.output);
3756 break;
3757 }
3758 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3759 {
3760 ssize_t nb_output;
3761 struct lttng_snapshot_output *outputs = NULL;
3762
3763 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3764 if (nb_output < 0) {
3765 ret = -nb_output;
3766 goto error;
3767 }
3768
3769 ret = setup_lttng_msg(cmd_ctx,
3770 nb_output * sizeof(struct lttng_snapshot_output));
3771 if (ret < 0) {
3772 free(outputs);
3773 goto setup_error;
3774 }
3775
3776 if (outputs) {
3777 /* Copy output list into message payload */
3778 memcpy(cmd_ctx->llm->payload, outputs,
3779 nb_output * sizeof(struct lttng_snapshot_output));
3780 free(outputs);
3781 }
3782
3783 ret = LTTNG_OK;
3784 break;
3785 }
3786 case LTTNG_SNAPSHOT_RECORD:
3787 {
3788 ret = cmd_snapshot_record(cmd_ctx->session,
3789 &cmd_ctx->lsm->u.snapshot_record.output,
3790 cmd_ctx->lsm->u.snapshot_record.wait);
3791 break;
3792 }
3793 case LTTNG_CREATE_SESSION_SNAPSHOT:
3794 {
3795 size_t nb_uri, len;
3796 struct lttng_uri *uris = NULL;
3797
3798 nb_uri = cmd_ctx->lsm->u.uri.size;
3799 len = nb_uri * sizeof(struct lttng_uri);
3800
3801 if (nb_uri > 0) {
3802 uris = zmalloc(len);
3803 if (uris == NULL) {
3804 ret = LTTNG_ERR_FATAL;
3805 goto error;
3806 }
3807
3808 /* Receive variable len data */
3809 DBG("Waiting for %zu URIs from client ...", nb_uri);
3810 ret = lttcomm_recv_unix_sock(sock, uris, len);
3811 if (ret <= 0) {
3812 DBG("No URIs received from client... continuing");
3813 *sock_error = 1;
3814 ret = LTTNG_ERR_SESSION_FAIL;
3815 free(uris);
3816 goto error;
3817 }
3818
3819 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3820 DBG("Creating session with ONE network URI is a bad call");
3821 ret = LTTNG_ERR_SESSION_FAIL;
3822 free(uris);
3823 goto error;
3824 }
3825 }
3826
3827 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3828 nb_uri, &cmd_ctx->creds);
3829 free(uris);
3830 break;
3831 }
3832 case LTTNG_CREATE_SESSION_LIVE:
3833 {
3834 size_t nb_uri, len;
3835 struct lttng_uri *uris = NULL;
3836
3837 nb_uri = cmd_ctx->lsm->u.uri.size;
3838 len = nb_uri * sizeof(struct lttng_uri);
3839
3840 if (nb_uri > 0) {
3841 uris = zmalloc(len);
3842 if (uris == NULL) {
3843 ret = LTTNG_ERR_FATAL;
3844 goto error;
3845 }
3846
3847 /* Receive variable len data */
3848 DBG("Waiting for %zu URIs from client ...", nb_uri);
3849 ret = lttcomm_recv_unix_sock(sock, uris, len);
3850 if (ret <= 0) {
3851 DBG("No URIs received from client... continuing");
3852 *sock_error = 1;
3853 ret = LTTNG_ERR_SESSION_FAIL;
3854 free(uris);
3855 goto error;
3856 }
3857
3858 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3859 DBG("Creating session with ONE network URI is a bad call");
3860 ret = LTTNG_ERR_SESSION_FAIL;
3861 free(uris);
3862 goto error;
3863 }
3864 }
3865
3866 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3867 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3868 free(uris);
3869 break;
3870 }
3871 case LTTNG_SAVE_SESSION:
3872 {
3873 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
3874 &cmd_ctx->creds);
3875 break;
3876 }
3877 case LTTNG_SET_SESSION_SHM_PATH:
3878 {
3879 ret = cmd_set_session_shm_path(cmd_ctx->session,
3880 cmd_ctx->lsm->u.set_shm_path.shm_path);
3881 break;
3882 }
3883 default:
3884 ret = LTTNG_ERR_UND;
3885 break;
3886 }
3887
3888 error:
3889 if (cmd_ctx->llm == NULL) {
3890 DBG("Missing llm structure. Allocating one.");
3891 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3892 goto setup_error;
3893 }
3894 }
3895 /* Set return code */
3896 cmd_ctx->llm->ret_code = ret;
3897 setup_error:
3898 if (cmd_ctx->session) {
3899 session_unlock(cmd_ctx->session);
3900 }
3901 if (need_tracing_session) {
3902 session_unlock_list();
3903 }
3904 init_setup_error:
3905 return ret;
3906 }
3907
3908 /*
3909 * Thread managing health check socket.
3910 */
3911 static void *thread_manage_health(void *data)
3912 {
3913 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3914 uint32_t revents, nb_fd;
3915 struct lttng_poll_event events;
3916 struct health_comm_msg msg;
3917 struct health_comm_reply reply;
3918
3919 DBG("[thread] Manage health check started");
3920
3921 rcu_register_thread();
3922
3923 /* We might hit an error path before this is created. */
3924 lttng_poll_init(&events);
3925
3926 /* Create unix socket */
3927 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3928 if (sock < 0) {
3929 ERR("Unable to create health check Unix socket");
3930 ret = -1;
3931 goto error;
3932 }
3933
3934 if (is_root) {
3935 /* lttng health client socket path permissions */
3936 ret = chown(health_unix_sock_path, 0,
3937 utils_get_group_id(tracing_group_name));
3938 if (ret < 0) {
3939 ERR("Unable to set group on %s", health_unix_sock_path);
3940 PERROR("chown");
3941 ret = -1;
3942 goto error;
3943 }
3944
3945 ret = chmod(health_unix_sock_path,
3946 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3947 if (ret < 0) {
3948 ERR("Unable to set permissions on %s", health_unix_sock_path);
3949 PERROR("chmod");
3950 ret = -1;
3951 goto error;
3952 }
3953 }
3954
3955 /*
3956 * Set the CLOEXEC flag. Return code is useless because either way, the
3957 * show must go on.
3958 */
3959 (void) utils_set_fd_cloexec(sock);
3960
3961 ret = lttcomm_listen_unix_sock(sock);
3962 if (ret < 0) {
3963 goto error;
3964 }
3965
3966 /*
3967 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3968 * more will be added to this poll set.
3969 */
3970 ret = sessiond_set_thread_pollset(&events, 2);
3971 if (ret < 0) {
3972 goto error;
3973 }
3974
3975 /* Add the application registration socket */
3976 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3977 if (ret < 0) {
3978 goto error;
3979 }
3980
3981 sessiond_notify_ready();
3982
3983 while (1) {
3984 DBG("Health check ready");
3985
3986 /* Inifinite blocking call, waiting for transmission */
3987 restart:
3988 ret = lttng_poll_wait(&events, -1);
3989 if (ret < 0) {
3990 /*
3991 * Restart interrupted system call.
3992 */
3993 if (errno == EINTR) {
3994 goto restart;
3995 }
3996 goto error;
3997 }
3998
3999 nb_fd = ret;
4000
4001 for (i = 0; i < nb_fd; i++) {
4002 /* Fetch once the poll data */
4003 revents = LTTNG_POLL_GETEV(&events, i);
4004 pollfd = LTTNG_POLL_GETFD(&events, i);
4005
4006 if (!revents) {
4007 /* No activity for this FD (poll implementation). */
4008 continue;
4009 }
4010
4011 /* Thread quit pipe has been closed. Killing thread. */
4012 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
4013 if (ret) {
4014 err = 0;
4015 goto exit;
4016 }
4017
4018 /* Event on the registration socket */
4019 if (pollfd == sock) {
4020 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4021 ERR("Health socket poll error");
4022 goto error;
4023 }
4024 }
4025 }
4026
4027 new_sock = lttcomm_accept_unix_sock(sock);
4028 if (new_sock < 0) {
4029 goto error;
4030 }
4031
4032 /*
4033 * Set the CLOEXEC flag. Return code is useless because either way, the
4034 * show must go on.
4035 */
4036 (void) utils_set_fd_cloexec(new_sock);
4037
4038 DBG("Receiving data from client for health...");
4039 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4040 if (ret <= 0) {
4041 DBG("Nothing recv() from client... continuing");
4042 ret = close(new_sock);
4043 if (ret) {
4044 PERROR("close");
4045 }
4046 new_sock = -1;
4047 continue;
4048 }
4049
4050 rcu_thread_online();
4051
4052 memset(&reply, 0, sizeof(reply));
4053 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
4054 /*
4055 * health_check_state returns 0 if health is
4056 * bad.
4057 */
4058 if (!health_check_state(health_sessiond, i)) {
4059 reply.ret_code |= 1ULL << i;
4060 }
4061 }
4062
4063 DBG2("Health check return value %" PRIx64, reply.ret_code);
4064
4065 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4066 if (ret < 0) {
4067 ERR("Failed to send health data back to client");
4068 }
4069
4070 /* End of transmission */
4071 ret = close(new_sock);
4072 if (ret) {
4073 PERROR("close");
4074 }
4075 new_sock = -1;
4076 }
4077
4078 exit:
4079 error:
4080 if (err) {
4081 ERR("Health error occurred in %s", __func__);
4082 }
4083 DBG("Health check thread dying");
4084 unlink(health_unix_sock_path);
4085 if (sock >= 0) {
4086 ret = close(sock);
4087 if (ret) {
4088 PERROR("close");
4089 }
4090 }
4091
4092 lttng_poll_clean(&events);
4093
4094 rcu_unregister_thread();
4095 return NULL;
4096 }
4097
4098 /*
4099 * This thread manage all clients request using the unix client socket for
4100 * communication.
4101 */
4102 static void *thread_manage_clients(void *data)
4103 {
4104 int sock = -1, ret, i, pollfd, err = -1;
4105 int sock_error;
4106 uint32_t revents, nb_fd;
4107 struct command_ctx *cmd_ctx = NULL;
4108 struct lttng_poll_event events;
4109
4110 DBG("[thread] Manage client started");
4111
4112 rcu_register_thread();
4113
4114 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
4115
4116 health_code_update();
4117
4118 ret = lttcomm_listen_unix_sock(client_sock);
4119 if (ret < 0) {
4120 goto error_listen;
4121 }
4122
4123 /*
4124 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4125 * more will be added to this poll set.
4126 */
4127 ret = sessiond_set_thread_pollset(&events, 2);
4128 if (ret < 0) {
4129 goto error_create_poll;
4130 }
4131
4132 /* Add the application registration socket */
4133 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4134 if (ret < 0) {
4135 goto error;
4136 }
4137
4138 sessiond_notify_ready();
4139 ret = sem_post(&load_info->message_thread_ready);
4140 if (ret) {
4141 PERROR("sem_post message_thread_ready");
4142 goto error;
4143 }
4144
4145 /* This testpoint is after we signal readiness to the parent. */
4146 if (testpoint(sessiond_thread_manage_clients)) {
4147 goto error;
4148 }
4149
4150 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
4151 goto error;
4152 }
4153
4154 health_code_update();
4155
4156 while (1) {
4157 DBG("Accepting client command ...");
4158
4159 /* Inifinite blocking call, waiting for transmission */
4160 restart:
4161 health_poll_entry();
4162 ret = lttng_poll_wait(&events, -1);
4163 health_poll_exit();
4164 if (ret < 0) {
4165 /*
4166 * Restart interrupted system call.
4167 */
4168 if (errno == EINTR) {
4169 goto restart;
4170 }
4171 goto error;
4172 }
4173
4174 nb_fd = ret;
4175
4176 for (i = 0; i < nb_fd; i++) {
4177 /* Fetch once the poll data */
4178 revents = LTTNG_POLL_GETEV(&events, i);
4179 pollfd = LTTNG_POLL_GETFD(&events, i);
4180
4181 health_code_update();
4182
4183 if (!revents) {
4184 /* No activity for this FD (poll implementation). */
4185 continue;
4186 }
4187
4188 /* Thread quit pipe has been closed. Killing thread. */
4189 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
4190 if (ret) {
4191 err = 0;
4192 goto exit;
4193 }
4194
4195 /* Event on the registration socket */
4196 if (pollfd == client_sock) {
4197 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4198 ERR("Client socket poll error");
4199 goto error;
4200 }
4201 }
4202 }
4203
4204 DBG("Wait for client response");
4205
4206 health_code_update();
4207
4208 sock = lttcomm_accept_unix_sock(client_sock);
4209 if (sock < 0) {
4210 goto error;
4211 }
4212
4213 /*
4214 * Set the CLOEXEC flag. Return code is useless because either way, the
4215 * show must go on.
4216 */
4217 (void) utils_set_fd_cloexec(sock);
4218
4219 /* Set socket option for credentials retrieval */
4220 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4221 if (ret < 0) {
4222 goto error;
4223 }
4224
4225 /* Allocate context command to process the client request */
4226 cmd_ctx = zmalloc(sizeof(struct command_ctx));
4227 if (cmd_ctx == NULL) {
4228 PERROR("zmalloc cmd_ctx");
4229 goto error;
4230 }
4231
4232 /* Allocate data buffer for reception */
4233 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
4234 if (cmd_ctx->lsm == NULL) {
4235 PERROR("zmalloc cmd_ctx->lsm");
4236 goto error;
4237 }
4238
4239 cmd_ctx->llm = NULL;
4240 cmd_ctx->session = NULL;
4241
4242 health_code_update();
4243
4244 /*
4245 * Data is received from the lttng client. The struct
4246 * lttcomm_session_msg (lsm) contains the command and data request of
4247 * the client.
4248 */
4249 DBG("Receiving data from client ...");
4250 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4251 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
4252 if (ret <= 0) {
4253 DBG("Nothing recv() from client... continuing");
4254 ret = close(sock);
4255 if (ret) {
4256 PERROR("close");
4257 }
4258 sock = -1;
4259 clean_command_ctx(&cmd_ctx);
4260 continue;
4261 }
4262
4263 health_code_update();
4264
4265 // TODO: Validate cmd_ctx including sanity check for
4266 // security purpose.
4267
4268 rcu_thread_online();
4269 /*
4270 * This function dispatch the work to the kernel or userspace tracer
4271 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4272 * informations for the client. The command context struct contains
4273 * everything this function may needs.
4274 */
4275 ret = process_client_msg(cmd_ctx, sock, &sock_error);
4276 rcu_thread_offline();
4277 if (ret < 0) {
4278 ret = close(sock);
4279 if (ret) {
4280 PERROR("close");
4281 }
4282 sock = -1;
4283 /*
4284 * TODO: Inform client somehow of the fatal error. At
4285 * this point, ret < 0 means that a zmalloc failed
4286 * (ENOMEM). Error detected but still accept
4287 * command, unless a socket error has been
4288 * detected.
4289 */
4290 clean_command_ctx(&cmd_ctx);
4291 continue;
4292 }
4293
4294 health_code_update();
4295
4296 DBG("Sending response (size: %d, retcode: %s)",
4297 cmd_ctx->lttng_msg_size,
4298 lttng_strerror(-cmd_ctx->llm->ret_code));
4299 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
4300 if (ret < 0) {
4301 ERR("Failed to send data back to client");
4302 }
4303
4304 /* End of transmission */
4305 ret = close(sock);
4306 if (ret) {
4307 PERROR("close");
4308 }
4309 sock = -1;
4310
4311 clean_command_ctx(&cmd_ctx);
4312
4313 health_code_update();
4314 }
4315
4316 exit:
4317 error:
4318 if (sock >= 0) {
4319 ret = close(sock);
4320 if (ret) {
4321 PERROR("close");
4322 }
4323 }
4324
4325 lttng_poll_clean(&events);
4326 clean_command_ctx(&cmd_ctx);
4327
4328 error_listen:
4329 error_create_poll:
4330 unlink(client_unix_sock_path);
4331 if (client_sock >= 0) {
4332 ret = close(client_sock);
4333 if (ret) {
4334 PERROR("close");
4335 }
4336 }
4337
4338 if (err) {
4339 health_error();
4340 ERR("Health error occurred in %s", __func__);
4341 }
4342
4343 health_unregister(health_sessiond);
4344
4345 DBG("Client thread dying");
4346
4347 rcu_unregister_thread();
4348
4349 /*
4350 * Since we are creating the consumer threads, we own them, so we need
4351 * to join them before our thread exits.
4352 */
4353 ret = join_consumer_thread(&kconsumer_data);
4354 if (ret) {
4355 errno = ret;
4356 PERROR("join_consumer");
4357 }
4358
4359 ret = join_consumer_thread(&ustconsumer32_data);
4360 if (ret) {
4361 errno = ret;
4362 PERROR("join_consumer ust32");
4363 }
4364
4365 ret = join_consumer_thread(&ustconsumer64_data);
4366 if (ret) {
4367 errno = ret;
4368 PERROR("join_consumer ust64");
4369 }
4370 return NULL;
4371 }
4372
4373
4374 /*
4375 * usage function on stderr
4376 */
4377 static void usage(void)
4378 {
4379 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
4380 fprintf(stderr, " -h, --help Display this usage.\n");
4381 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
4382 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4383 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4384 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4385 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4386 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4387 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4388 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4389 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4390 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4391 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4392 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4393 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
4394 fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n");
4395 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4396 fprintf(stderr, " -V, --version Show version number.\n");
4397 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4398 fprintf(stderr, " -q, --quiet No output at all.\n");
4399 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4400 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4401 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4402 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
4403 fprintf(stderr, " --agent-tcp-port Agent registration TCP port\n");
4404 fprintf(stderr, " -f --config PATH Load daemon configuration file\n");
4405 fprintf(stderr, " -l --load PATH Load session configuration\n");
4406 fprintf(stderr, " --kmod-probes Specify kernel module probes to load\n");
4407 fprintf(stderr, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4408 }
4409
4410 /*
4411 * Take an option from the getopt output and set it in the right variable to be
4412 * used later.
4413 *
4414 * Return 0 on success else a negative value.
4415 */
4416 static int set_option(int opt, const char *arg, const char *optname)
4417 {
4418 int ret = 0;
4419
4420 if (arg && arg[0] == '\0') {
4421 /*
4422 * This only happens if the value is read from daemon config
4423 * file. This means the option requires an argument and the
4424 * configuration file contains a line such as:
4425 * my_option =
4426 */
4427 ret = -EINVAL;
4428 goto end;
4429 }
4430
4431 switch (opt) {
4432 case 0:
4433 fprintf(stderr, "option %s", optname);
4434 if (arg) {
4435 fprintf(stderr, " with arg %s\n", arg);
4436 }
4437 break;
4438 case 'c':
4439 if (lttng_is_setuid_setgid()) {
4440 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4441 "-c, --client-sock");
4442 } else {
4443 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4444 }
4445 break;
4446 case 'a':
4447 if (lttng_is_setuid_setgid()) {
4448 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4449 "-a, --apps-sock");
4450 } else {
4451 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4452 }
4453 break;
4454 case 'd':
4455 opt_daemon = 1;
4456 break;
4457 case 'b':
4458 opt_background = 1;
4459 break;
4460 case 'g':
4461 if (lttng_is_setuid_setgid()) {
4462 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4463 "-g, --group");
4464 } else {
4465 /*
4466 * If the override option is set, the pointer points to a
4467 * *non* const thus freeing it even though the variable type is
4468 * set to const.
4469 */
4470 if (tracing_group_name_override) {
4471 free((void *) tracing_group_name);
4472 }
4473 tracing_group_name = strdup(arg);
4474 if (!tracing_group_name) {
4475 PERROR("strdup");
4476 ret = -ENOMEM;
4477 }
4478 tracing_group_name_override = 1;
4479 }
4480 break;
4481 case 'h':
4482 usage();
4483 exit(EXIT_SUCCESS);
4484 case 'V':
4485 fprintf(stdout, "%s\n", VERSION);
4486 exit(EXIT_SUCCESS);
4487 case 'S':
4488 opt_sig_parent = 1;
4489 break;
4490 case 'E':
4491 if (lttng_is_setuid_setgid()) {
4492 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4493 "--kconsumerd-err-sock");
4494 } else {
4495 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4496 }
4497 break;
4498 case 'C':
4499 if (lttng_is_setuid_setgid()) {
4500 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4501 "--kconsumerd-cmd-sock");
4502 } else {
4503 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4504 }
4505 break;
4506 case 'F':
4507 if (lttng_is_setuid_setgid()) {
4508 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4509 "--ustconsumerd64-err-sock");
4510 } else {
4511 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4512 }
4513 break;
4514 case 'D':
4515 if (lttng_is_setuid_setgid()) {
4516 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4517 "--ustconsumerd64-cmd-sock");
4518 } else {
4519 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4520 }
4521 break;
4522 case 'H':
4523 if (lttng_is_setuid_setgid()) {
4524 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4525 "--ustconsumerd32-err-sock");
4526 } else {
4527 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4528 }
4529 break;
4530 case 'G':
4531 if (lttng_is_setuid_setgid()) {
4532 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4533 "--ustconsumerd32-cmd-sock");
4534 } else {
4535 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4536 }
4537 break;
4538 case 'N':
4539 opt_no_kernel = 1;
4540 break;
4541 case 'q':
4542 lttng_opt_quiet = 1;
4543 break;
4544 case 'v':
4545 /* Verbose level can increase using multiple -v */
4546 if (arg) {
4547 /* Value obtained from config file */
4548 lttng_opt_verbose = config_parse_value(arg);
4549 } else {
4550 /* -v used on command line */
4551 lttng_opt_verbose++;
4552 }
4553 /* Clamp value to [0, 3] */
4554 lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 :
4555 (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3);
4556 break;
4557 case 'Z':
4558 if (arg) {
4559 opt_verbose_consumer = config_parse_value(arg);
4560 } else {
4561 opt_verbose_consumer += 1;
4562 }
4563 break;
4564 case 'u':
4565 if (lttng_is_setuid_setgid()) {
4566 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4567 "--consumerd32-path");
4568 } else {
4569 if (consumerd32_bin_override) {
4570 free((void *) consumerd32_bin);
4571 }
4572 consumerd32_bin = strdup(arg);
4573 if (!consumerd32_bin) {
4574 PERROR("strdup");
4575 ret = -ENOMEM;
4576 }
4577 consumerd32_bin_override = 1;
4578 }
4579 break;
4580 case 'U':
4581 if (lttng_is_setuid_setgid()) {
4582 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4583 "--consumerd32-libdir");
4584 } else {
4585 if (consumerd32_libdir_override) {
4586 free((void *) consumerd32_libdir);
4587 }
4588 consumerd32_libdir = strdup(arg);
4589 if (!consumerd32_libdir) {
4590 PERROR("strdup");
4591 ret = -ENOMEM;
4592 }
4593 consumerd32_libdir_override = 1;
4594 }
4595 break;
4596 case 't':
4597 if (lttng_is_setuid_setgid()) {
4598 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4599 "--consumerd64-path");
4600 } else {
4601 if (consumerd64_bin_override) {
4602 free((void *) consumerd64_bin);
4603 }
4604 consumerd64_bin = strdup(arg);
4605 if (!consumerd64_bin) {
4606 PERROR("strdup");
4607 ret = -ENOMEM;
4608 }
4609 consumerd64_bin_override = 1;
4610 }
4611 break;
4612 case 'T':
4613 if (lttng_is_setuid_setgid()) {
4614 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4615 "--consumerd64-libdir");
4616 } else {
4617 if (consumerd64_libdir_override) {
4618 free((void *) consumerd64_libdir);
4619 }
4620 consumerd64_libdir = strdup(arg);
4621 if (!consumerd64_libdir) {
4622 PERROR("strdup");
4623 ret = -ENOMEM;
4624 }
4625 consumerd64_libdir_override = 1;
4626 }
4627 break;
4628 case 'p':
4629 if (lttng_is_setuid_setgid()) {
4630 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4631 "-p, --pidfile");
4632 } else {
4633 free(opt_pidfile);
4634 opt_pidfile = strdup(arg);
4635 if (!opt_pidfile) {
4636 PERROR("strdup");
4637 ret = -ENOMEM;
4638 }
4639 }
4640 break;
4641 case 'J': /* Agent TCP port. */
4642 {
4643 if (lttng_is_setuid_setgid()) {
4644 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4645 "--agent-tcp-port");
4646 } else {
4647 unsigned long v;
4648
4649 if (!arg) {
4650 ret = -EINVAL;
4651 goto end;
4652 }
4653 errno = 0;
4654 v = strtoul(arg, NULL, 0);
4655 if (errno != 0 || !isdigit(arg[0])) {
4656 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4657 return -1;
4658 }
4659 if (v == 0 || v >= 65535) {
4660 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4661 return -1;
4662 }
4663 agent_tcp_port = (uint32_t) v;
4664 DBG3("Agent TCP port set to non default: %u", agent_tcp_port);
4665 }
4666 break;
4667 }
4668 case 'l':
4669 if (lttng_is_setuid_setgid()) {
4670 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4671 "-l, --load");
4672 } else {
4673 free(opt_load_session_path);
4674 opt_load_session_path = strdup(arg);
4675 if (!opt_load_session_path) {
4676 PERROR("strdup");
4677 ret = -ENOMEM;
4678 }
4679 }
4680 break;
4681 case 'P': /* probe modules list */
4682 if (lttng_is_setuid_setgid()) {
4683 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4684 "--kmod-probes");
4685 } else {
4686 free(kmod_probes_list);
4687 kmod_probes_list = strdup(arg);
4688 if (!kmod_probes_list) {
4689 PERROR("strdup");
4690 ret = -ENOMEM;
4691 }
4692 }
4693 break;
4694 case 'e':
4695 if (lttng_is_setuid_setgid()) {
4696 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4697 "--extra-kmod-probes");
4698 } else {
4699 free(kmod_extra_probes_list);
4700 kmod_extra_probes_list = strdup(arg);
4701 if (!kmod_extra_probes_list) {
4702 PERROR("strdup");
4703 ret = -ENOMEM;
4704 }
4705 }
4706 break;
4707 case 'f':
4708 /* This is handled in set_options() thus silent break. */
4709 break;
4710 default:
4711 /* Unknown option or other error.
4712 * Error is printed by getopt, just return */
4713 ret = -1;
4714 }
4715
4716 end:
4717 if (ret == -EINVAL) {
4718 const char *opt_name = "unknown";
4719 int i;
4720
4721 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
4722 i++) {
4723 if (opt == long_options[i].val) {
4724 opt_name = long_options[i].name;
4725 break;
4726 }
4727 }
4728
4729 WARN("Invalid argument provided for option \"%s\", using default value.",
4730 opt_name);
4731 }
4732
4733 return ret;
4734 }
4735
4736 /*
4737 * config_entry_handler_cb used to handle options read from a config file.
4738 * See config_entry_handler_cb comment in common/config/config.h for the
4739 * return value conventions.
4740 */
4741 static int config_entry_handler(const struct config_entry *entry, void *unused)
4742 {
4743 int ret = 0, i;
4744
4745 if (!entry || !entry->name || !entry->value) {
4746 ret = -EINVAL;
4747 goto end;
4748 }
4749
4750 /* Check if the option is to be ignored */
4751 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4752 if (!strcmp(entry->name, config_ignore_options[i])) {
4753 goto end;
4754 }
4755 }
4756
4757 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4758 i++) {
4759
4760 /* Ignore if not fully matched. */
4761 if (strcmp(entry->name, long_options[i].name)) {
4762 continue;
4763 }
4764
4765 /*
4766 * If the option takes no argument on the command line, we have to
4767 * check if the value is "true". We support non-zero numeric values,
4768 * true, on and yes.
4769 */
4770 if (!long_options[i].has_arg) {
4771 ret = config_parse_value(entry->value);
4772 if (ret <= 0) {
4773 if (ret) {
4774 WARN("Invalid configuration value \"%s\" for option %s",
4775 entry->value, entry->name);
4776 }
4777 /* False, skip boolean config option. */
4778 goto end;
4779 }
4780 }
4781
4782 ret = set_option(long_options[i].val, entry->value, entry->name);
4783 goto end;
4784 }
4785
4786 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4787
4788 end:
4789 return ret;
4790 }
4791
4792 /*
4793 * daemon configuration loading and argument parsing
4794 */
4795 static int set_options(int argc, char **argv)
4796 {
4797 int ret = 0, c = 0, option_index = 0;
4798 int orig_optopt = optopt, orig_optind = optind;
4799 char *optstring;
4800 const char *config_path = NULL;
4801
4802 optstring = utils_generate_optstring(long_options,
4803 sizeof(long_options) / sizeof(struct option));
4804 if (!optstring) {
4805 ret = -ENOMEM;
4806 goto end;
4807 }
4808
4809 /* Check for the --config option */
4810 while ((c = getopt_long(argc, argv, optstring, long_options,
4811 &option_index)) != -1) {
4812 if (c == '?') {
4813 ret = -EINVAL;
4814 goto end;
4815 } else if (c != 'f') {
4816 /* if not equal to --config option. */
4817 continue;
4818 }
4819
4820 if (lttng_is_setuid_setgid()) {
4821 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4822 "-f, --config");
4823 } else {
4824 config_path = utils_expand_path(optarg);
4825 if (!config_path) {
4826 ERR("Failed to resolve path: %s", optarg);
4827 }
4828 }
4829 }
4830
4831 ret = config_get_section_entries(config_path, config_section_name,
4832 config_entry_handler, NULL);
4833 if (ret) {
4834 if (ret > 0) {
4835 ERR("Invalid configuration option at line %i", ret);
4836 ret = -1;
4837 }
4838 goto end;
4839 }
4840
4841 /* Reset getopt's global state */
4842 optopt = orig_optopt;
4843 optind = orig_optind;
4844 while (1) {
4845 c = getopt_long(argc, argv, optstring, long_options, &option_index);
4846 if (c == -1) {
4847 break;
4848 }
4849
4850 ret = set_option(c, optarg, long_options[option_index].name);
4851 if (ret < 0) {
4852 break;
4853 }
4854 }
4855
4856 end:
4857 free(optstring);
4858 return ret;
4859 }
4860
4861 /*
4862 * Creates the two needed socket by the daemon.
4863 * apps_sock - The communication socket for all UST apps.
4864 * client_sock - The communication of the cli tool (lttng).
4865 */
4866 static int init_daemon_socket(void)
4867 {
4868 int ret = 0;
4869 mode_t old_umask;
4870
4871 old_umask = umask(0);
4872
4873 /* Create client tool unix socket */
4874 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4875 if (client_sock < 0) {
4876 ERR("Create unix sock failed: %s", client_unix_sock_path);
4877 ret = -1;
4878 goto end;
4879 }
4880
4881 /* Set the cloexec flag */
4882 ret = utils_set_fd_cloexec(client_sock);
4883 if (ret < 0) {
4884 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4885 "Continuing but note that the consumer daemon will have a "
4886 "reference to this socket on exec()", client_sock);
4887 }
4888
4889 /* File permission MUST be 660 */
4890 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4891 if (ret < 0) {
4892 ERR("Set file permissions failed: %s", client_unix_sock_path);
4893 PERROR("chmod");
4894 goto end;
4895 }
4896
4897 /* Create the application unix socket */
4898 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4899 if (apps_sock < 0) {
4900 ERR("Create unix sock failed: %s", apps_unix_sock_path);
4901 ret = -1;
4902 goto end;
4903 }
4904
4905 /* Set the cloexec flag */
4906 ret = utils_set_fd_cloexec(apps_sock);
4907 if (ret < 0) {
4908 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4909 "Continuing but note that the consumer daemon will have a "
4910 "reference to this socket on exec()", apps_sock);
4911 }
4912
4913 /* File permission MUST be 666 */
4914 ret = chmod(apps_unix_sock_path,
4915 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
4916 if (ret < 0) {
4917 ERR("Set file permissions failed: %s", apps_unix_sock_path);
4918 PERROR("chmod");
4919 goto end;
4920 }
4921
4922 DBG3("Session daemon client socket %d and application socket %d created",
4923 client_sock, apps_sock);
4924
4925 end:
4926 umask(old_umask);
4927 return ret;
4928 }
4929
4930 /*
4931 * Check if the global socket is available, and if a daemon is answering at the
4932 * other side. If yes, error is returned.
4933 */
4934 static int check_existing_daemon(void)
4935 {
4936 /* Is there anybody out there ? */
4937 if (lttng_session_daemon_alive()) {
4938 return -EEXIST;
4939 }
4940
4941 return 0;
4942 }
4943
4944 /*
4945 * Set the tracing group gid onto the client socket.
4946 *
4947 * Race window between mkdir and chown is OK because we are going from more
4948 * permissive (root.root) to less permissive (root.tracing).
4949 */
4950 static int set_permissions(char *rundir)
4951 {
4952 int ret;
4953 gid_t gid;
4954
4955 gid = utils_get_group_id(tracing_group_name);
4956
4957 /* Set lttng run dir */
4958 ret = chown(rundir, 0, gid);
4959 if (ret < 0) {
4960 ERR("Unable to set group on %s", rundir);
4961 PERROR("chown");
4962 }
4963
4964 /*
4965 * Ensure all applications and tracing group can search the run
4966 * dir. Allow everyone to read the directory, since it does not
4967 * buy us anything to hide its content.
4968 */
4969 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
4970 if (ret < 0) {
4971 ERR("Unable to set permissions on %s", rundir);
4972 PERROR("chmod");
4973 }
4974
4975 /* lttng client socket path */
4976 ret = chown(client_unix_sock_path, 0, gid);
4977 if (ret < 0) {
4978 ERR("Unable to set group on %s", client_unix_sock_path);
4979 PERROR("chown");
4980 }
4981
4982 /* kconsumer error socket path */
4983 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
4984 if (ret < 0) {
4985 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
4986 PERROR("chown");
4987 }
4988
4989 /* 64-bit ustconsumer error socket path */
4990 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
4991 if (ret < 0) {
4992 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
4993 PERROR("chown");
4994 }
4995
4996 /* 32-bit ustconsumer compat32 error socket path */
4997 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
4998 if (ret < 0) {
4999 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
5000 PERROR("chown");
5001 }
5002
5003 DBG("All permissions are set");
5004
5005 return ret;
5006 }
5007
5008 /*
5009 * Create the lttng run directory needed for all global sockets and pipe.
5010 */
5011 static int create_lttng_rundir(const char *rundir)
5012 {
5013 int ret;
5014
5015 DBG3("Creating LTTng run directory: %s", rundir);
5016
5017 ret = mkdir(rundir, S_IRWXU);
5018 if (ret < 0) {
5019 if (errno != EEXIST) {
5020 ERR("Unable to create %s", rundir);
5021 goto error;
5022 } else {
5023 ret = 0;
5024 }
5025 }
5026
5027 error:
5028 return ret;
5029 }
5030
5031 /*
5032 * Setup sockets and directory needed by the kconsumerd communication with the
5033 * session daemon.
5034 */
5035 static int set_consumer_sockets(struct consumer_data *consumer_data,
5036 const char *rundir)
5037 {
5038 int ret;
5039 char path[PATH_MAX];
5040
5041 switch (consumer_data->type) {
5042 case LTTNG_CONSUMER_KERNEL:
5043 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
5044 break;
5045 case LTTNG_CONSUMER64_UST:
5046 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
5047 break;
5048 case LTTNG_CONSUMER32_UST:
5049 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
5050 break;
5051 default:
5052 ERR("Consumer type unknown");
5053 ret = -EINVAL;
5054 goto error;
5055 }
5056
5057 DBG2("Creating consumer directory: %s", path);
5058
5059 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
5060 if (ret < 0) {
5061 if (errno != EEXIST) {
5062 PERROR("mkdir");
5063 ERR("Failed to create %s", path);
5064 goto error;
5065 }
5066 ret = -1;
5067 }
5068 if (is_root) {
5069 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
5070 if (ret < 0) {
5071 ERR("Unable to set group on %s", path);
5072 PERROR("chown");
5073 goto error;
5074 }
5075 }
5076
5077 /* Create the kconsumerd error unix socket */
5078 consumer_data->err_sock =
5079 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5080 if (consumer_data->err_sock < 0) {
5081 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
5082 ret = -1;
5083 goto error;
5084 }
5085
5086 /*
5087 * Set the CLOEXEC flag. Return code is useless because either way, the
5088 * show must go on.
5089 */
5090 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5091 if (ret < 0) {
5092 PERROR("utils_set_fd_cloexec");
5093 /* continue anyway */
5094 }
5095
5096 /* File permission MUST be 660 */
5097 ret = chmod(consumer_data->err_unix_sock_path,
5098 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5099 if (ret < 0) {
5100 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
5101 PERROR("chmod");
5102 goto error;
5103 }
5104
5105 error:
5106 return ret;
5107 }
5108
5109 /*
5110 * Signal handler for the daemon
5111 *
5112 * Simply stop all worker threads, leaving main() return gracefully after
5113 * joining all threads and calling cleanup().
5114 */
5115 static void sighandler(int sig)
5116 {
5117 switch (sig) {
5118 case SIGPIPE:
5119 DBG("SIGPIPE caught");
5120 return;
5121 case SIGINT:
5122 DBG("SIGINT caught");
5123 stop_threads();
5124 break;
5125 case SIGTERM:
5126 DBG("SIGTERM caught");
5127 stop_threads();
5128 break;
5129 case SIGUSR1:
5130 CMM_STORE_SHARED(recv_child_signal, 1);
5131 break;
5132 default:
5133 break;
5134 }
5135 }
5136
5137 /*
5138 * Setup signal handler for :
5139 * SIGINT, SIGTERM, SIGPIPE
5140 */
5141 static int set_signal_handler(void)
5142 {
5143 int ret = 0;
5144 struct sigaction sa;
5145 sigset_t sigset;
5146
5147 if ((ret = sigemptyset(&sigset)) < 0) {
5148 PERROR("sigemptyset");
5149 return ret;
5150 }
5151
5152 sa.sa_handler = sighandler;
5153 sa.sa_mask = sigset;
5154 sa.sa_flags = 0;
5155 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
5156 PERROR("sigaction");
5157 return ret;
5158 }
5159
5160 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
5161 PERROR("sigaction");
5162 return ret;
5163 }
5164
5165 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
5166 PERROR("sigaction");
5167 return ret;
5168 }
5169
5170 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
5171 PERROR("sigaction");
5172 return ret;
5173 }
5174
5175 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5176
5177 return ret;
5178 }
5179
5180 /*
5181 * Set open files limit to unlimited. This daemon can open a large number of
5182 * file descriptors in order to consumer multiple kernel traces.
5183 */
5184 static void set_ulimit(void)
5185 {
5186 int ret;
5187 struct rlimit lim;
5188
5189 /* The kernel does not allowed an infinite limit for open files */
5190 lim.rlim_cur = 65535;
5191 lim.rlim_max = 65535;
5192
5193 ret = setrlimit(RLIMIT_NOFILE, &lim);
5194 if (ret < 0) {
5195 PERROR("failed to set open files limit");
5196 }
5197 }
5198
5199 /*
5200 * Write pidfile using the rundir and opt_pidfile.
5201 */
5202 static int write_pidfile(void)
5203 {
5204 int ret;
5205 char pidfile_path[PATH_MAX];
5206
5207 assert(rundir);
5208
5209 if (opt_pidfile) {
5210 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
5211 } else {
5212 /* Build pidfile path from rundir and opt_pidfile. */
5213 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
5214 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
5215 if (ret < 0) {
5216 PERROR("snprintf pidfile path");
5217 goto error;
5218 }
5219 }
5220
5221 /*
5222 * Create pid file in rundir.
5223 */
5224 ret = utils_create_pid_file(getpid(), pidfile_path);
5225 error:
5226 return ret;
5227 }
5228
5229 /*
5230 * Create lockfile using the rundir and return its fd.
5231 */
5232 static int create_lockfile(void)
5233 {
5234 int ret;
5235 char lockfile_path[PATH_MAX];
5236
5237 ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path));
5238 if (ret < 0) {
5239 goto error;
5240 }
5241
5242 ret = utils_create_lock_file(lockfile_path);
5243 error:
5244 return ret;
5245 }
5246
5247 /*
5248 * Write agent TCP port using the rundir.
5249 */
5250 static int write_agent_port(void)
5251 {
5252 int ret;
5253 char path[PATH_MAX];
5254
5255 assert(rundir);
5256
5257 ret = snprintf(path, sizeof(path), "%s/"
5258 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE, rundir);
5259 if (ret < 0) {
5260 PERROR("snprintf agent port path");
5261 goto error;
5262 }
5263
5264 /*
5265 * Create TCP agent port file in rundir.
5266 */
5267 ret = utils_create_pid_file(agent_tcp_port, path);
5268
5269 error:
5270 return ret;
5271 }
5272
5273 /*
5274 * main
5275 */
5276 int main(int argc, char **argv)
5277 {
5278 int ret = 0, retval = 0;
5279 void *status;
5280 const char *home_path, *env_app_timeout;
5281
5282 init_kernel_workarounds();
5283
5284 rcu_register_thread();
5285
5286 if (set_signal_handler()) {
5287 retval = -1;
5288 goto exit_set_signal_handler;
5289 }
5290
5291 setup_consumerd_path();
5292
5293 page_size = sysconf(_SC_PAGESIZE);
5294 if (page_size < 0) {
5295 PERROR("sysconf _SC_PAGESIZE");
5296 page_size = LONG_MAX;
5297 WARN("Fallback page size to %ld", page_size);
5298 }
5299
5300 /*
5301 * Parse arguments and load the daemon configuration file.
5302 *
5303 * We have an exit_options exit path to free memory reserved by
5304 * set_options. This is needed because the rest of sessiond_cleanup()
5305 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5306 * depends on set_options.
5307 */
5308 progname = argv[0];
5309 if (set_options(argc, argv)) {
5310 retval = -1;
5311 goto exit_options;
5312 }
5313
5314 /* Daemonize */
5315 if (opt_daemon || opt_background) {
5316 int i;
5317
5318 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
5319 !opt_background);
5320 if (ret < 0) {
5321 retval = -1;
5322 goto exit_options;
5323 }
5324
5325 /*
5326 * We are in the child. Make sure all other file descriptors are
5327 * closed, in case we are called with more opened file
5328 * descriptors than the standard ones.
5329 */
5330 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5331 (void) close(i);
5332 }
5333 }
5334
5335 /*
5336 * Starting from here, we can create threads. This needs to be after
5337 * lttng_daemonize due to RCU.
5338 */
5339
5340 /*
5341 * Initialize the health check subsystem. This call should set the
5342 * appropriate time values.
5343 */
5344 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5345 if (!health_sessiond) {
5346 PERROR("health_app_create error");
5347 retval = -1;
5348 goto exit_health_sessiond_cleanup;
5349 }
5350
5351 if (init_ht_cleanup_quit_pipe()) {
5352 retval = -1;
5353 goto exit_ht_cleanup_quit_pipe;
5354 }
5355
5356 /* Setup the thread ht_cleanup communication pipe. */
5357 if (utils_create_pipe_cloexec(ht_cleanup_pipe)) {
5358 retval = -1;
5359 goto exit_ht_cleanup_pipe;
5360 }
5361
5362 /* Set up max poll set size */
5363 if (lttng_poll_set_max_size()) {
5364 retval = -1;
5365 goto exit_set_max_size;
5366 }
5367
5368 /* Create thread to clean up RCU hash tables */
5369 ret = pthread_create(&ht_cleanup_thread, NULL,
5370 thread_ht_cleanup, (void *) NULL);
5371 if (ret) {
5372 errno = ret;
5373 PERROR("pthread_create ht_cleanup");
5374 retval = -1;
5375 goto exit_ht_cleanup;
5376 }
5377
5378 /* Create thread quit pipe */
5379 if (init_thread_quit_pipe()) {
5380 retval = -1;
5381 goto exit_init_data;
5382 }
5383
5384 /* Check if daemon is UID = 0 */
5385 is_root = !getuid();
5386
5387 if (is_root) {
5388 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
5389 if (!rundir) {
5390 retval = -1;
5391 goto exit_init_data;
5392 }
5393
5394 /* Create global run dir with root access */
5395 if (create_lttng_rundir(rundir)) {
5396 retval = -1;
5397 goto exit_init_data;
5398 }
5399
5400 if (strlen(apps_unix_sock_path) == 0) {
5401 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5402 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
5403 if (ret < 0) {
5404 retval = -1;
5405 goto exit_init_data;
5406 }
5407 }
5408
5409 if (strlen(client_unix_sock_path) == 0) {
5410 ret = snprintf(client_unix_sock_path, PATH_MAX,
5411 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
5412 if (ret < 0) {
5413 retval = -1;
5414 goto exit_init_data;
5415 }
5416 }
5417
5418 /* Set global SHM for ust */
5419 if (strlen(wait_shm_path) == 0) {
5420 ret = snprintf(wait_shm_path, PATH_MAX,
5421 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
5422 if (ret < 0) {
5423 retval = -1;
5424 goto exit_init_data;
5425 }
5426 }
5427
5428 if (strlen(health_unix_sock_path) == 0) {
5429 ret = snprintf(health_unix_sock_path,
5430 sizeof(health_unix_sock_path),
5431 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
5432 if (ret < 0) {
5433 retval = -1;
5434 goto exit_init_data;
5435 }
5436 }
5437
5438 /* Setup kernel consumerd path */
5439 ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
5440 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
5441 if (ret < 0) {
5442 retval = -1;
5443 goto exit_init_data;
5444 }
5445 ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
5446 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
5447 if (ret < 0) {
5448 retval = -1;
5449 goto exit_init_data;
5450 }
5451
5452 DBG2("Kernel consumer err path: %s",
5453 kconsumer_data.err_unix_sock_path);
5454 DBG2("Kernel consumer cmd path: %s",
5455 kconsumer_data.cmd_unix_sock_path);
5456 } else {
5457 home_path = utils_get_home_dir();
5458 if (home_path == NULL) {
5459 /* TODO: Add --socket PATH option */
5460 ERR("Can't get HOME directory for sockets creation.");
5461 retval = -1;
5462 goto exit_init_data;
5463 }
5464
5465 /*
5466 * Create rundir from home path. This will create something like
5467 * $HOME/.lttng
5468 */
5469 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
5470 if (ret < 0) {
5471 retval = -1;
5472 goto exit_init_data;
5473 }
5474
5475 if (create_lttng_rundir(rundir)) {
5476 retval = -1;
5477 goto exit_init_data;
5478 }
5479
5480 if (strlen(apps_unix_sock_path) == 0) {
5481 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5482 DEFAULT_HOME_APPS_UNIX_SOCK,
5483 home_path);
5484 if (ret < 0) {
5485 retval = -1;
5486 goto exit_init_data;
5487 }
5488 }
5489
5490 /* Set the cli tool unix socket path */
5491 if (strlen(client_unix_sock_path) == 0) {
5492 ret = snprintf(client_unix_sock_path, PATH_MAX,
5493 DEFAULT_HOME_CLIENT_UNIX_SOCK,
5494 home_path);
5495 if (ret < 0) {
5496 retval = -1;
5497 goto exit_init_data;
5498 }
5499 }
5500
5501 /* Set global SHM for ust */
5502 if (strlen(wait_shm_path) == 0) {
5503 ret = snprintf(wait_shm_path, PATH_MAX,
5504 DEFAULT_HOME_APPS_WAIT_SHM_PATH,
5505 getuid());
5506 if (ret < 0) {
5507 retval = -1;
5508 goto exit_init_data;
5509 }
5510 }
5511
5512 /* Set health check Unix path */
5513 if (strlen(health_unix_sock_path) == 0) {
5514 ret = snprintf(health_unix_sock_path,
5515 sizeof(health_unix_sock_path),
5516 DEFAULT_HOME_HEALTH_UNIX_SOCK,
5517 home_path);
5518 if (ret < 0) {
5519 retval = -1;
5520 goto exit_init_data;
5521 }
5522 }
5523 }
5524
5525 lockfile_fd = create_lockfile();
5526 if (lockfile_fd < 0) {
5527 retval = -1;
5528 goto exit_init_data;
5529 }
5530
5531 /* Set consumer initial state */
5532 kernel_consumerd_state = CONSUMER_STOPPED;
5533 ust_consumerd_state = CONSUMER_STOPPED;
5534
5535 DBG("Client socket path %s", client_unix_sock_path);
5536 DBG("Application socket path %s", apps_unix_sock_path);
5537 DBG("Application wait path %s", wait_shm_path);
5538 DBG("LTTng run directory path: %s", rundir);
5539
5540 /* 32 bits consumerd path setup */
5541 ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
5542 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
5543 if (ret < 0) {
5544 PERROR("snprintf 32-bit consumer error socket path");
5545 retval = -1;
5546 goto exit_init_data;
5547 }
5548 ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
5549 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
5550 if (ret < 0) {
5551 PERROR("snprintf 32-bit consumer command socket path");
5552 retval = -1;
5553 goto exit_init_data;
5554 }
5555
5556 DBG2("UST consumer 32 bits err path: %s",
5557 ustconsumer32_data.err_unix_sock_path);
5558 DBG2("UST consumer 32 bits cmd path: %s",
5559 ustconsumer32_data.cmd_unix_sock_path);
5560
5561 /* 64 bits consumerd path setup */
5562 ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
5563 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
5564 if (ret < 0) {
5565 PERROR("snprintf 64-bit consumer error socket path");
5566 retval = -1;
5567 goto exit_init_data;
5568 }
5569 ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
5570 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
5571 if (ret < 0) {
5572 PERROR("snprintf 64-bit consumer command socket path");
5573 retval = -1;
5574 goto exit_init_data;
5575 }
5576
5577 DBG2("UST consumer 64 bits err path: %s",
5578 ustconsumer64_data.err_unix_sock_path);
5579 DBG2("UST consumer 64 bits cmd path: %s",
5580 ustconsumer64_data.cmd_unix_sock_path);
5581
5582 /*
5583 * See if daemon already exist.
5584 */
5585 if (check_existing_daemon()) {
5586 ERR("Already running daemon.\n");
5587 /*
5588 * We do not goto exit because we must not cleanup()
5589 * because a daemon is already running.
5590 */
5591 retval = -1;
5592 goto exit_init_data;
5593 }
5594
5595 /*
5596 * Init UST app hash table. Alloc hash table before this point since
5597 * cleanup() can get called after that point.
5598 */
5599 if (ust_app_ht_alloc()) {
5600 retval = -1;
5601 goto exit_init_data;
5602 }
5603
5604 /* Initialize agent domain subsystem. */
5605 if (agent_setup()) {
5606 /* ENOMEM at this point. */
5607 retval = -1;
5608 goto exit_init_data;
5609 }
5610
5611 /*
5612 * These actions must be executed as root. We do that *after* setting up
5613 * the sockets path because we MUST make the check for another daemon using
5614 * those paths *before* trying to set the kernel consumer sockets and init
5615 * kernel tracer.
5616 */
5617 if (is_root) {
5618 if (set_consumer_sockets(&kconsumer_data, rundir)) {
5619 retval = -1;
5620 goto exit_init_data;
5621 }
5622
5623 /* Setup kernel tracer */
5624 if (!opt_no_kernel) {
5625 init_kernel_tracer();
5626 if (kernel_tracer_fd >= 0) {
5627 ret = syscall_init_table();
5628 if (ret < 0) {
5629 ERR("Unable to populate syscall table. "
5630 "Syscall tracing won't work "
5631 "for this session daemon.");
5632 }
5633 }
5634 }
5635
5636 /* Set ulimit for open files */
5637 set_ulimit();
5638 }
5639 /* init lttng_fd tracking must be done after set_ulimit. */
5640 lttng_fd_init();
5641
5642 if (set_consumer_sockets(&ustconsumer64_data, rundir)) {
5643 retval = -1;
5644 goto exit_init_data;
5645 }
5646
5647 if (set_consumer_sockets(&ustconsumer32_data, rundir)) {
5648 retval = -1;
5649 goto exit_init_data;
5650 }
5651
5652 /* Setup the needed unix socket */
5653 if (init_daemon_socket()) {
5654 retval = -1;
5655 goto exit_init_data;
5656 }
5657
5658 /* Set credentials to socket */
5659 if (is_root && set_permissions(rundir)) {
5660 retval = -1;
5661 goto exit_init_data;
5662 }
5663
5664 /* Get parent pid if -S, --sig-parent is specified. */
5665 if (opt_sig_parent) {
5666 ppid = getppid();
5667 }
5668
5669 /* Setup the kernel pipe for waking up the kernel thread */
5670 if (is_root && !opt_no_kernel) {
5671 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
5672 retval = -1;
5673 goto exit_init_data;
5674 }
5675 }
5676
5677 /* Setup the thread apps communication pipe. */
5678 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
5679 retval = -1;
5680 goto exit_init_data;
5681 }
5682
5683 /* Setup the thread apps notify communication pipe. */
5684 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
5685 retval = -1;
5686 goto exit_init_data;
5687 }
5688
5689 /* Initialize global buffer per UID and PID registry. */
5690 buffer_reg_init_uid_registry();
5691 buffer_reg_init_pid_registry();
5692
5693 /* Init UST command queue. */
5694 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
5695
5696 /*
5697 * Get session list pointer. This pointer MUST NOT be free'd. This list
5698 * is statically declared in session.c
5699 */
5700 session_list_ptr = session_get_list();
5701
5702 cmd_init();
5703
5704 /* Check for the application socket timeout env variable. */
5705 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5706 if (env_app_timeout) {
5707 app_socket_timeout = atoi(env_app_timeout);
5708 } else {
5709 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5710 }
5711
5712 ret = write_pidfile();
5713 if (ret) {
5714 ERR("Error in write_pidfile");
5715 retval = -1;
5716 goto exit_init_data;
5717 }
5718 ret = write_agent_port();
5719 if (ret) {
5720 ERR("Error in write_agent_port");
5721 retval = -1;
5722 goto exit_init_data;
5723 }
5724
5725 /* Initialize communication library */
5726 lttcomm_init();
5727 /* Initialize TCP timeout values */
5728 lttcomm_inet_init();
5729
5730 if (load_session_init_data(&load_info) < 0) {
5731 retval = -1;
5732 goto exit_init_data;
5733 }
5734 load_info->path = opt_load_session_path;
5735
5736 /* Create health-check thread */
5737 ret = pthread_create(&health_thread, NULL,
5738 thread_manage_health, (void *) NULL);
5739 if (ret) {
5740 errno = ret;
5741 PERROR("pthread_create health");
5742 retval = -1;
5743 goto exit_health;
5744 }
5745
5746 /* Create thread to manage the client socket */
5747 ret = pthread_create(&client_thread, NULL,
5748 thread_manage_clients, (void *) NULL);
5749 if (ret) {
5750 errno = ret;
5751 PERROR("pthread_create clients");
5752 retval = -1;
5753 goto exit_client;
5754 }
5755
5756 /* Create thread to dispatch registration */
5757 ret = pthread_create(&dispatch_thread, NULL,
5758 thread_dispatch_ust_registration, (void *) NULL);
5759 if (ret) {
5760 errno = ret;
5761 PERROR("pthread_create dispatch");
5762 retval = -1;
5763 goto exit_dispatch;
5764 }
5765
5766 /* Create thread to manage application registration. */
5767 ret = pthread_create(&reg_apps_thread, NULL,
5768 thread_registration_apps, (void *) NULL);
5769 if (ret) {
5770 errno = ret;
5771 PERROR("pthread_create registration");
5772 retval = -1;
5773 goto exit_reg_apps;
5774 }
5775
5776 /* Create thread to manage application socket */
5777 ret = pthread_create(&apps_thread, NULL,
5778 thread_manage_apps, (void *) NULL);
5779 if (ret) {
5780 errno = ret;
5781 PERROR("pthread_create apps");
5782 retval = -1;
5783 goto exit_apps;
5784 }
5785
5786 /* Create thread to manage application notify socket */
5787 ret = pthread_create(&apps_notify_thread, NULL,
5788 ust_thread_manage_notify, (void *) NULL);
5789 if (ret) {
5790 errno = ret;
5791 PERROR("pthread_create notify");
5792 retval = -1;
5793 goto exit_apps_notify;
5794 }
5795
5796 /* Create agent registration thread. */
5797 ret = pthread_create(&agent_reg_thread, NULL,
5798 agent_thread_manage_registration, (void *) NULL);
5799 if (ret) {
5800 errno = ret;
5801 PERROR("pthread_create agent");
5802 retval = -1;
5803 goto exit_agent_reg;
5804 }
5805
5806 /* Don't start this thread if kernel tracing is not requested nor root */
5807 if (is_root && !opt_no_kernel) {
5808 /* Create kernel thread to manage kernel event */
5809 ret = pthread_create(&kernel_thread, NULL,
5810 thread_manage_kernel, (void *) NULL);
5811 if (ret) {
5812 errno = ret;
5813 PERROR("pthread_create kernel");
5814 retval = -1;
5815 goto exit_kernel;
5816 }
5817 }
5818
5819 /* Create session loading thread. */
5820 ret = pthread_create(&load_session_thread, NULL, thread_load_session,
5821 load_info);
5822 if (ret) {
5823 errno = ret;
5824 PERROR("pthread_create load_session_thread");
5825 retval = -1;
5826 goto exit_load_session;
5827 }
5828
5829 /*
5830 * This is where we start awaiting program completion (e.g. through
5831 * signal that asks threads to teardown).
5832 */
5833
5834 ret = pthread_join(load_session_thread, &status);
5835 if (ret) {
5836 errno = ret;
5837 PERROR("pthread_join load_session_thread");
5838 retval = -1;
5839 }
5840 exit_load_session:
5841
5842 if (is_root && !opt_no_kernel) {
5843 ret = pthread_join(kernel_thread, &status);
5844 if (ret) {
5845 errno = ret;
5846 PERROR("pthread_join");
5847 retval = -1;
5848 }
5849 }
5850 exit_kernel:
5851
5852 ret = pthread_join(agent_reg_thread, &status);
5853 if (ret) {
5854 errno = ret;
5855 PERROR("pthread_join agent");
5856 retval = -1;
5857 }
5858 exit_agent_reg:
5859
5860 ret = pthread_join(apps_notify_thread, &status);
5861 if (ret) {
5862 errno = ret;
5863 PERROR("pthread_join apps notify");
5864 retval = -1;
5865 }
5866 exit_apps_notify:
5867
5868 ret = pthread_join(apps_thread, &status);
5869 if (ret) {
5870 errno = ret;
5871 PERROR("pthread_join apps");
5872 retval = -1;
5873 }
5874 exit_apps:
5875
5876 ret = pthread_join(reg_apps_thread, &status);
5877 if (ret) {
5878 errno = ret;
5879 PERROR("pthread_join");
5880 retval = -1;
5881 }
5882 exit_reg_apps:
5883
5884 ret = pthread_join(dispatch_thread, &status);
5885 if (ret) {
5886 errno = ret;
5887 PERROR("pthread_join");
5888 retval = -1;
5889 }
5890 exit_dispatch:
5891
5892 ret = pthread_join(client_thread, &status);
5893 if (ret) {
5894 errno = ret;
5895 PERROR("pthread_join");
5896 retval = -1;
5897 }
5898 exit_client:
5899
5900 ret = pthread_join(health_thread, &status);
5901 if (ret) {
5902 errno = ret;
5903 PERROR("pthread_join health thread");
5904 retval = -1;
5905 }
5906 exit_health:
5907
5908 exit_init_data:
5909 /*
5910 * sessiond_cleanup() is called when no other thread is running, except
5911 * the ht_cleanup thread, which is needed to destroy the hash tables.
5912 */
5913 rcu_thread_online();
5914 sessiond_cleanup();
5915 rcu_thread_offline();
5916 rcu_unregister_thread();
5917
5918 ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]);
5919 if (ret < 0) {
5920 ERR("write error on ht_cleanup quit pipe");
5921 retval = -1;
5922 }
5923
5924 ret = pthread_join(ht_cleanup_thread, &status);
5925 if (ret) {
5926 errno = ret;
5927 PERROR("pthread_join ht cleanup thread");
5928 retval = -1;
5929 }
5930 exit_ht_cleanup:
5931 exit_set_max_size:
5932
5933 utils_close_pipe(ht_cleanup_pipe);
5934 exit_ht_cleanup_pipe:
5935
5936 /*
5937 * Close the ht_cleanup quit pipe.
5938 */
5939 utils_close_pipe(ht_cleanup_quit_pipe);
5940 exit_ht_cleanup_quit_pipe:
5941
5942 health_app_destroy(health_sessiond);
5943 exit_health_sessiond_cleanup:
5944
5945 exit_options:
5946 sessiond_cleanup_options();
5947
5948 exit_set_signal_handler:
5949 if (!retval) {
5950 exit(EXIT_SUCCESS);
5951 } else {
5952 exit(EXIT_FAILURE);
5953 }
5954 }
This page took 0.205646 seconds and 4 git commands to generate.