Fix: app cmd leak on sessiond exit
[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 #include "agent.h"
77
78 #define CONSUMERD_FILE "lttng-consumerd"
79
80 const char *progname;
81 static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
82 static int tracing_group_name_override;
83 static char *opt_pidfile;
84 static int opt_sig_parent;
85 static int opt_verbose_consumer;
86 static int opt_daemon, opt_background;
87 static int opt_no_kernel;
88 static char *opt_load_session_path;
89 static pid_t ppid; /* Parent PID for --sig-parent option */
90 static pid_t child_ppid; /* Internal parent PID use with daemonize. */
91 static char *rundir;
92 static int lockfile_fd = -1;
93
94 /* Set to 1 when a SIGUSR1 signal is received. */
95 static int recv_child_signal;
96
97 /*
98 * Consumer daemon specific control data. Every value not initialized here is
99 * set to 0 by the static definition.
100 */
101 static struct consumer_data kconsumer_data = {
102 .type = LTTNG_CONSUMER_KERNEL,
103 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
104 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
105 .err_sock = -1,
106 .cmd_sock = -1,
107 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
108 .lock = PTHREAD_MUTEX_INITIALIZER,
109 .cond = PTHREAD_COND_INITIALIZER,
110 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
111 };
112 static struct consumer_data ustconsumer64_data = {
113 .type = LTTNG_CONSUMER64_UST,
114 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
115 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
116 .err_sock = -1,
117 .cmd_sock = -1,
118 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
119 .lock = PTHREAD_MUTEX_INITIALIZER,
120 .cond = PTHREAD_COND_INITIALIZER,
121 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
122 };
123 static struct consumer_data ustconsumer32_data = {
124 .type = LTTNG_CONSUMER32_UST,
125 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
126 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
127 .err_sock = -1,
128 .cmd_sock = -1,
129 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
130 .lock = PTHREAD_MUTEX_INITIALIZER,
131 .cond = PTHREAD_COND_INITIALIZER,
132 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
133 };
134
135 /* Command line options */
136 static const struct option long_options[] = {
137 { "client-sock", required_argument, 0, 'c' },
138 { "apps-sock", required_argument, 0, 'a' },
139 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
140 { "kconsumerd-err-sock", required_argument, 0, '\0' },
141 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
142 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
143 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
144 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
145 { "consumerd32-path", required_argument, 0, '\0' },
146 { "consumerd32-libdir", required_argument, 0, '\0' },
147 { "consumerd64-path", required_argument, 0, '\0' },
148 { "consumerd64-libdir", required_argument, 0, '\0' },
149 { "daemonize", no_argument, 0, 'd' },
150 { "background", no_argument, 0, 'b' },
151 { "sig-parent", no_argument, 0, 'S' },
152 { "help", no_argument, 0, 'h' },
153 { "group", required_argument, 0, 'g' },
154 { "version", no_argument, 0, 'V' },
155 { "quiet", no_argument, 0, 'q' },
156 { "verbose", no_argument, 0, 'v' },
157 { "verbose-consumer", no_argument, 0, '\0' },
158 { "no-kernel", no_argument, 0, '\0' },
159 { "pidfile", required_argument, 0, 'p' },
160 { "agent-tcp-port", required_argument, 0, '\0' },
161 { "config", required_argument, 0, 'f' },
162 { "load", required_argument, 0, 'l' },
163 { "kmod-probes", required_argument, 0, '\0' },
164 { "extra-kmod-probes", required_argument, 0, '\0' },
165 { NULL, 0, 0, 0 }
166 };
167
168 /* Command line options to ignore from configuration file */
169 static const char *config_ignore_options[] = { "help", "version", "config" };
170
171 /* Shared between threads */
172 static int dispatch_thread_exit;
173
174 /* Global application Unix socket path */
175 static char apps_unix_sock_path[PATH_MAX];
176 /* Global client Unix socket path */
177 static char client_unix_sock_path[PATH_MAX];
178 /* global wait shm path for UST */
179 static char wait_shm_path[PATH_MAX];
180 /* Global health check unix path */
181 static char health_unix_sock_path[PATH_MAX];
182
183 /* Sockets and FDs */
184 static int client_sock = -1;
185 static int apps_sock = -1;
186 int kernel_tracer_fd = -1;
187 static int kernel_poll_pipe[2] = { -1, -1 };
188
189 /*
190 * Quit pipe for all threads. This permits a single cancellation point
191 * for all threads when receiving an event on the pipe.
192 */
193 static int thread_quit_pipe[2] = { -1, -1 };
194 static int ht_cleanup_quit_pipe[2] = { -1, -1 };
195
196 /*
197 * This pipe is used to inform the thread managing application communication
198 * that a command is queued and ready to be processed.
199 */
200 static int apps_cmd_pipe[2] = { -1, -1 };
201
202 int apps_cmd_notify_pipe[2] = { -1, -1 };
203
204 /* Pthread, Mutexes and Semaphores */
205 static pthread_t apps_thread;
206 static pthread_t apps_notify_thread;
207 static pthread_t reg_apps_thread;
208 static pthread_t client_thread;
209 static pthread_t kernel_thread;
210 static pthread_t dispatch_thread;
211 static pthread_t health_thread;
212 static pthread_t ht_cleanup_thread;
213 static pthread_t agent_reg_thread;
214 static pthread_t load_session_thread;
215
216 /*
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
219 *
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
224 */
225 static struct ust_cmd_queue ust_cmd_queue;
226
227 /*
228 * Pointer initialized before thread creation.
229 *
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
233 *
234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
235 * session_lock_list and session_unlock_list for lock acquisition.
236 */
237 static struct ltt_session_list *session_list_ptr;
238
239 int ust_consumerd64_fd = -1;
240 int ust_consumerd32_fd = -1;
241
242 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
243 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
244 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
245 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
246 static int consumerd32_bin_override;
247 static int consumerd64_bin_override;
248 static int consumerd32_libdir_override;
249 static int consumerd64_libdir_override;
250
251 static const char *module_proc_lttng = "/proc/lttng";
252
253 /*
254 * Consumer daemon state which is changed when spawning it, killing it or in
255 * case of a fatal error.
256 */
257 enum consumerd_state {
258 CONSUMER_STARTED = 1,
259 CONSUMER_STOPPED = 2,
260 CONSUMER_ERROR = 3,
261 };
262
263 /*
264 * This consumer daemon state is used to validate if a client command will be
265 * able to reach the consumer. If not, the client is informed. For instance,
266 * doing a "lttng start" when the consumer state is set to ERROR will return an
267 * error to the client.
268 *
269 * The following example shows a possible race condition of this scheme:
270 *
271 * consumer thread error happens
272 * client cmd arrives
273 * client cmd checks state -> still OK
274 * consumer thread exit, sets error
275 * client cmd try to talk to consumer
276 * ...
277 *
278 * However, since the consumer is a different daemon, we have no way of making
279 * sure the command will reach it safely even with this state flag. This is why
280 * we consider that up to the state validation during command processing, the
281 * command is safe. After that, we can not guarantee the correctness of the
282 * client request vis-a-vis the consumer.
283 */
284 static enum consumerd_state ust_consumerd_state;
285 static enum consumerd_state kernel_consumerd_state;
286
287 /*
288 * Socket timeout for receiving and sending in seconds.
289 */
290 static int app_socket_timeout;
291
292 /* Set in main() with the current page size. */
293 long page_size;
294
295 /* Application health monitoring */
296 struct health_app *health_sessiond;
297
298 /* Agent TCP port for registration. Used by the agent thread. */
299 unsigned int agent_tcp_port = DEFAULT_AGENT_TCP_PORT;
300
301 /* Am I root or not. */
302 int is_root; /* Set to 1 if the daemon is running as root */
303
304 const char * const config_section_name = "sessiond";
305
306 /* Load session thread information to operate. */
307 struct load_session_thread_data *load_info;
308
309 /* Global hash tables */
310 struct lttng_ht *agent_apps_ht_by_sock = NULL;
311
312 /*
313 * Whether sessiond is ready for commands/health check requests.
314 * NR_LTTNG_SESSIOND_READY must match the number of calls to
315 * sessiond_notify_ready().
316 */
317 #define NR_LTTNG_SESSIOND_READY 3
318 int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
319
320 /* Notify parents that we are ready for cmd and health check */
321 LTTNG_HIDDEN
322 void sessiond_notify_ready(void)
323 {
324 if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
325 /*
326 * Notify parent pid that we are ready to accept command
327 * for client side. This ppid is the one from the
328 * external process that spawned us.
329 */
330 if (opt_sig_parent) {
331 kill(ppid, SIGUSR1);
332 }
333
334 /*
335 * Notify the parent of the fork() process that we are
336 * ready.
337 */
338 if (opt_daemon || opt_background) {
339 kill(child_ppid, SIGUSR1);
340 }
341 }
342 }
343
344 static
345 void setup_consumerd_path(void)
346 {
347 const char *bin, *libdir;
348
349 /*
350 * Allow INSTALL_BIN_PATH to be used as a target path for the
351 * native architecture size consumer if CONFIG_CONSUMER*_PATH
352 * has not been defined.
353 */
354 #if (CAA_BITS_PER_LONG == 32)
355 if (!consumerd32_bin[0]) {
356 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
357 }
358 if (!consumerd32_libdir[0]) {
359 consumerd32_libdir = INSTALL_LIB_PATH;
360 }
361 #elif (CAA_BITS_PER_LONG == 64)
362 if (!consumerd64_bin[0]) {
363 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
364 }
365 if (!consumerd64_libdir[0]) {
366 consumerd64_libdir = INSTALL_LIB_PATH;
367 }
368 #else
369 #error "Unknown bitness"
370 #endif
371
372 /*
373 * runtime env. var. overrides the build default.
374 */
375 bin = lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
376 if (bin) {
377 consumerd32_bin = bin;
378 }
379 bin = lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
380 if (bin) {
381 consumerd64_bin = bin;
382 }
383 libdir = lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
384 if (libdir) {
385 consumerd32_libdir = libdir;
386 }
387 libdir = lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
388 if (libdir) {
389 consumerd64_libdir = libdir;
390 }
391 }
392
393 static
394 int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
395 int *a_pipe)
396 {
397 int ret;
398
399 assert(events);
400
401 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
402 if (ret < 0) {
403 goto error;
404 }
405
406 /* Add quit pipe */
407 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
408 if (ret < 0) {
409 goto error;
410 }
411
412 return 0;
413
414 error:
415 return ret;
416 }
417
418 /*
419 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
420 */
421 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
422 {
423 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
424 }
425
426 /*
427 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
428 */
429 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event *events,
430 size_t size)
431 {
432 return __sessiond_set_thread_pollset(events, size,
433 ht_cleanup_quit_pipe);
434 }
435
436 static
437 int __sessiond_check_thread_quit_pipe(int fd, uint32_t events, int a_pipe)
438 {
439 if (fd == a_pipe && (events & LPOLLIN)) {
440 return 1;
441 }
442 return 0;
443 }
444
445 /*
446 * Check if the thread quit pipe was triggered.
447 *
448 * Return 1 if it was triggered else 0;
449 */
450 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
451 {
452 return __sessiond_check_thread_quit_pipe(fd, events,
453 thread_quit_pipe[0]);
454 }
455
456 /*
457 * Check if the ht_cleanup thread quit pipe was triggered.
458 *
459 * Return 1 if it was triggered else 0;
460 */
461 int sessiond_check_ht_cleanup_quit(int fd, uint32_t events)
462 {
463 return __sessiond_check_thread_quit_pipe(fd, events,
464 ht_cleanup_quit_pipe[0]);
465 }
466
467 /*
468 * Init thread quit pipe.
469 *
470 * Return -1 on error or 0 if all pipes are created.
471 */
472 static int __init_thread_quit_pipe(int *a_pipe)
473 {
474 int ret, i;
475
476 ret = pipe(a_pipe);
477 if (ret < 0) {
478 PERROR("thread quit pipe");
479 goto error;
480 }
481
482 for (i = 0; i < 2; i++) {
483 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
484 if (ret < 0) {
485 PERROR("fcntl");
486 goto error;
487 }
488 }
489
490 error:
491 return ret;
492 }
493
494 static int init_thread_quit_pipe(void)
495 {
496 return __init_thread_quit_pipe(thread_quit_pipe);
497 }
498
499 static int init_ht_cleanup_quit_pipe(void)
500 {
501 return __init_thread_quit_pipe(ht_cleanup_quit_pipe);
502 }
503
504 /*
505 * Stop all threads by closing the thread quit pipe.
506 */
507 static void stop_threads(void)
508 {
509 int ret;
510
511 /* Stopping all threads */
512 DBG("Terminating all threads");
513 ret = notify_thread_pipe(thread_quit_pipe[1]);
514 if (ret < 0) {
515 ERR("write error on thread quit pipe");
516 }
517
518 /* Dispatch thread */
519 CMM_STORE_SHARED(dispatch_thread_exit, 1);
520 futex_nto1_wake(&ust_cmd_queue.futex);
521 }
522
523 /*
524 * Close every consumer sockets.
525 */
526 static void close_consumer_sockets(void)
527 {
528 int ret;
529
530 if (kconsumer_data.err_sock >= 0) {
531 ret = close(kconsumer_data.err_sock);
532 if (ret < 0) {
533 PERROR("kernel consumer err_sock close");
534 }
535 }
536 if (ustconsumer32_data.err_sock >= 0) {
537 ret = close(ustconsumer32_data.err_sock);
538 if (ret < 0) {
539 PERROR("UST consumerd32 err_sock close");
540 }
541 }
542 if (ustconsumer64_data.err_sock >= 0) {
543 ret = close(ustconsumer64_data.err_sock);
544 if (ret < 0) {
545 PERROR("UST consumerd64 err_sock close");
546 }
547 }
548 if (kconsumer_data.cmd_sock >= 0) {
549 ret = close(kconsumer_data.cmd_sock);
550 if (ret < 0) {
551 PERROR("kernel consumer cmd_sock close");
552 }
553 }
554 if (ustconsumer32_data.cmd_sock >= 0) {
555 ret = close(ustconsumer32_data.cmd_sock);
556 if (ret < 0) {
557 PERROR("UST consumerd32 cmd_sock close");
558 }
559 }
560 if (ustconsumer64_data.cmd_sock >= 0) {
561 ret = close(ustconsumer64_data.cmd_sock);
562 if (ret < 0) {
563 PERROR("UST consumerd64 cmd_sock close");
564 }
565 }
566 }
567
568 /*
569 * Generate the full lock file path using the rundir.
570 *
571 * Return the snprintf() return value thus a negative value is an error.
572 */
573 static int generate_lock_file_path(char *path, size_t len)
574 {
575 int ret;
576
577 assert(path);
578 assert(rundir);
579
580 /* Build lockfile path from rundir. */
581 ret = snprintf(path, len, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE, rundir);
582 if (ret < 0) {
583 PERROR("snprintf lockfile path");
584 }
585
586 return ret;
587 }
588
589 /*
590 * Cleanup the session daemon's data structures.
591 */
592 static void sessiond_cleanup(void)
593 {
594 int ret;
595 struct ltt_session *sess, *stmp;
596 char path[PATH_MAX];
597
598 DBG("Cleanup sessiond");
599
600 /*
601 * Close the thread quit pipe. It has already done its job,
602 * since we are now called.
603 */
604 utils_close_pipe(thread_quit_pipe);
605
606 /*
607 * If opt_pidfile is undefined, the default file will be wiped when
608 * removing the rundir.
609 */
610 if (opt_pidfile) {
611 ret = remove(opt_pidfile);
612 if (ret < 0) {
613 PERROR("remove pidfile %s", opt_pidfile);
614 }
615 }
616
617 DBG("Removing sessiond and consumerd content of directory %s", rundir);
618
619 /* sessiond */
620 snprintf(path, PATH_MAX,
621 "%s/%s",
622 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
623 DBG("Removing %s", path);
624 (void) unlink(path);
625
626 snprintf(path, PATH_MAX, "%s/%s", rundir,
627 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE);
628 DBG("Removing %s", path);
629 (void) unlink(path);
630
631 /* kconsumerd */
632 snprintf(path, PATH_MAX,
633 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
634 rundir);
635 DBG("Removing %s", path);
636 (void) unlink(path);
637
638 snprintf(path, PATH_MAX,
639 DEFAULT_KCONSUMERD_PATH,
640 rundir);
641 DBG("Removing directory %s", path);
642 (void) rmdir(path);
643
644 /* ust consumerd 32 */
645 snprintf(path, PATH_MAX,
646 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
647 rundir);
648 DBG("Removing %s", path);
649 (void) unlink(path);
650
651 snprintf(path, PATH_MAX,
652 DEFAULT_USTCONSUMERD32_PATH,
653 rundir);
654 DBG("Removing directory %s", path);
655 (void) rmdir(path);
656
657 /* ust consumerd 64 */
658 snprintf(path, PATH_MAX,
659 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
660 rundir);
661 DBG("Removing %s", path);
662 (void) unlink(path);
663
664 snprintf(path, PATH_MAX,
665 DEFAULT_USTCONSUMERD64_PATH,
666 rundir);
667 DBG("Removing directory %s", path);
668 (void) rmdir(path);
669
670 DBG("Cleaning up all sessions");
671
672 /* Destroy session list mutex */
673 if (session_list_ptr != NULL) {
674 pthread_mutex_destroy(&session_list_ptr->lock);
675
676 /* Cleanup ALL session */
677 cds_list_for_each_entry_safe(sess, stmp,
678 &session_list_ptr->head, list) {
679 cmd_destroy_session(sess, kernel_poll_pipe[1]);
680 }
681 }
682
683 DBG("Cleaning up all agent apps");
684 agent_app_ht_clean();
685
686 DBG("Closing all UST sockets");
687 ust_app_clean_list();
688 buffer_reg_destroy_registries();
689
690 if (is_root && !opt_no_kernel) {
691 DBG2("Closing kernel fd");
692 if (kernel_tracer_fd >= 0) {
693 ret = close(kernel_tracer_fd);
694 if (ret) {
695 PERROR("close");
696 }
697 }
698 DBG("Unloading kernel modules");
699 modprobe_remove_lttng_all();
700 free(syscall_table);
701 }
702
703 close_consumer_sockets();
704
705 if (load_info) {
706 load_session_destroy_data(load_info);
707 free(load_info);
708 }
709
710 /*
711 * Cleanup lock file by deleting it and finaly closing it which will
712 * release the file system lock.
713 */
714 if (lockfile_fd >= 0) {
715 char lockfile_path[PATH_MAX];
716
717 ret = generate_lock_file_path(lockfile_path,
718 sizeof(lockfile_path));
719 if (ret > 0) {
720 ret = remove(lockfile_path);
721 if (ret < 0) {
722 PERROR("remove lock file");
723 }
724 ret = close(lockfile_fd);
725 if (ret < 0) {
726 PERROR("close lock file");
727 }
728 }
729 }
730
731 /*
732 * We do NOT rmdir rundir because there are other processes
733 * using it, for instance lttng-relayd, which can start in
734 * parallel with this teardown.
735 */
736
737 free(rundir);
738 }
739
740 /*
741 * Cleanup the daemon's option data structures.
742 */
743 static void sessiond_cleanup_options(void)
744 {
745 DBG("Cleaning up options");
746
747 /*
748 * If the override option is set, the pointer points to a *non* const
749 * thus freeing it even though the variable type is set to const.
750 */
751 if (tracing_group_name_override) {
752 free((void *) tracing_group_name);
753 }
754 if (consumerd32_bin_override) {
755 free((void *) consumerd32_bin);
756 }
757 if (consumerd64_bin_override) {
758 free((void *) consumerd64_bin);
759 }
760 if (consumerd32_libdir_override) {
761 free((void *) consumerd32_libdir);
762 }
763 if (consumerd64_libdir_override) {
764 free((void *) consumerd64_libdir);
765 }
766
767 free(opt_pidfile);
768 free(opt_load_session_path);
769 free(kmod_probes_list);
770 free(kmod_extra_probes_list);
771
772 /* <fun> */
773 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
774 "Matthew, BEET driven development works!%c[%dm",
775 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
776 /* </fun> */
777 }
778
779 /*
780 * Send data on a unix socket using the liblttsessiondcomm API.
781 *
782 * Return lttcomm error code.
783 */
784 static int send_unix_sock(int sock, void *buf, size_t len)
785 {
786 /* Check valid length */
787 if (len == 0) {
788 return -1;
789 }
790
791 return lttcomm_send_unix_sock(sock, buf, len);
792 }
793
794 /*
795 * Free memory of a command context structure.
796 */
797 static void clean_command_ctx(struct command_ctx **cmd_ctx)
798 {
799 DBG("Clean command context structure");
800 if (*cmd_ctx) {
801 if ((*cmd_ctx)->llm) {
802 free((*cmd_ctx)->llm);
803 }
804 if ((*cmd_ctx)->lsm) {
805 free((*cmd_ctx)->lsm);
806 }
807 free(*cmd_ctx);
808 *cmd_ctx = NULL;
809 }
810 }
811
812 /*
813 * Notify UST applications using the shm mmap futex.
814 */
815 static int notify_ust_apps(int active)
816 {
817 char *wait_shm_mmap;
818
819 DBG("Notifying applications of session daemon state: %d", active);
820
821 /* See shm.c for this call implying mmap, shm and futex calls */
822 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
823 if (wait_shm_mmap == NULL) {
824 goto error;
825 }
826
827 /* Wake waiting process */
828 futex_wait_update((int32_t *) wait_shm_mmap, active);
829
830 /* Apps notified successfully */
831 return 0;
832
833 error:
834 return -1;
835 }
836
837 /*
838 * Setup the outgoing data buffer for the response (llm) by allocating the
839 * right amount of memory and copying the original information from the lsm
840 * structure.
841 *
842 * Return total size of the buffer pointed by buf.
843 */
844 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
845 {
846 int ret, buf_size;
847
848 buf_size = size;
849
850 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
851 if (cmd_ctx->llm == NULL) {
852 PERROR("zmalloc");
853 ret = -ENOMEM;
854 goto error;
855 }
856
857 /* Copy common data */
858 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
859 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
860
861 cmd_ctx->llm->data_size = size;
862 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
863
864 return buf_size;
865
866 error:
867 return ret;
868 }
869
870 /*
871 * Update the kernel poll set of all channel fd available over all tracing
872 * session. Add the wakeup pipe at the end of the set.
873 */
874 static int update_kernel_poll(struct lttng_poll_event *events)
875 {
876 int ret;
877 struct ltt_session *session;
878 struct ltt_kernel_channel *channel;
879
880 DBG("Updating kernel poll set");
881
882 session_lock_list();
883 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
884 session_lock(session);
885 if (session->kernel_session == NULL) {
886 session_unlock(session);
887 continue;
888 }
889
890 cds_list_for_each_entry(channel,
891 &session->kernel_session->channel_list.head, list) {
892 /* Add channel fd to the kernel poll set */
893 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
894 if (ret < 0) {
895 session_unlock(session);
896 goto error;
897 }
898 DBG("Channel fd %d added to kernel set", channel->fd);
899 }
900 session_unlock(session);
901 }
902 session_unlock_list();
903
904 return 0;
905
906 error:
907 session_unlock_list();
908 return -1;
909 }
910
911 /*
912 * Find the channel fd from 'fd' over all tracing session. When found, check
913 * for new channel stream and send those stream fds to the kernel consumer.
914 *
915 * Useful for CPU hotplug feature.
916 */
917 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
918 {
919 int ret = 0;
920 struct ltt_session *session;
921 struct ltt_kernel_session *ksess;
922 struct ltt_kernel_channel *channel;
923
924 DBG("Updating kernel streams for channel fd %d", fd);
925
926 session_lock_list();
927 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
928 session_lock(session);
929 if (session->kernel_session == NULL) {
930 session_unlock(session);
931 continue;
932 }
933 ksess = session->kernel_session;
934
935 cds_list_for_each_entry(channel,
936 &ksess->channel_list.head, list) {
937 struct lttng_ht_iter iter;
938 struct consumer_socket *socket;
939
940 if (channel->fd != fd) {
941 continue;
942 }
943 DBG("Channel found, updating kernel streams");
944 ret = kernel_open_channel_stream(channel);
945 if (ret < 0) {
946 goto error;
947 }
948 /* Update the stream global counter */
949 ksess->stream_count_global += ret;
950
951 /*
952 * Have we already sent fds to the consumer? If yes, it
953 * means that tracing is started so it is safe to send
954 * our updated stream fds.
955 */
956 if (ksess->consumer_fds_sent != 1
957 || ksess->consumer == NULL) {
958 ret = -1;
959 goto error;
960 }
961
962 rcu_read_lock();
963 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
964 &iter.iter, socket, node.node) {
965 pthread_mutex_lock(socket->lock);
966 ret = kernel_consumer_send_channel_stream(socket,
967 channel, ksess,
968 session->output_traces ? 1 : 0);
969 pthread_mutex_unlock(socket->lock);
970 if (ret < 0) {
971 rcu_read_unlock();
972 goto error;
973 }
974 }
975 rcu_read_unlock();
976 }
977 session_unlock(session);
978 }
979 session_unlock_list();
980 return ret;
981
982 error:
983 session_unlock(session);
984 session_unlock_list();
985 return ret;
986 }
987
988 /*
989 * For each tracing session, update newly registered apps. The session list
990 * lock MUST be acquired before calling this.
991 */
992 static void update_ust_app(int app_sock)
993 {
994 struct ltt_session *sess, *stmp;
995
996 /* Consumer is in an ERROR state. Stop any application update. */
997 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
998 /* Stop the update process since the consumer is dead. */
999 return;
1000 }
1001
1002 /* For all tracing session(s) */
1003 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
1004 struct ust_app *app;
1005
1006 session_lock(sess);
1007 if (!sess->ust_session) {
1008 goto unlock_session;
1009 }
1010
1011 rcu_read_lock();
1012 assert(app_sock >= 0);
1013 app = ust_app_find_by_sock(app_sock);
1014 if (app == NULL) {
1015 /*
1016 * Application can be unregistered before so
1017 * this is possible hence simply stopping the
1018 * update.
1019 */
1020 DBG3("UST app update failed to find app sock %d",
1021 app_sock);
1022 goto unlock_rcu;
1023 }
1024 ust_app_global_update(sess->ust_session, app);
1025 unlock_rcu:
1026 rcu_read_unlock();
1027 unlock_session:
1028 session_unlock(sess);
1029 }
1030 }
1031
1032 /*
1033 * This thread manage event coming from the kernel.
1034 *
1035 * Features supported in this thread:
1036 * -) CPU Hotplug
1037 */
1038 static void *thread_manage_kernel(void *data)
1039 {
1040 int ret, i, pollfd, update_poll_flag = 1, err = -1;
1041 uint32_t revents, nb_fd;
1042 char tmp;
1043 struct lttng_poll_event events;
1044
1045 DBG("[thread] Thread manage kernel started");
1046
1047 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
1048
1049 /*
1050 * This first step of the while is to clean this structure which could free
1051 * non NULL pointers so initialize it before the loop.
1052 */
1053 lttng_poll_init(&events);
1054
1055 if (testpoint(sessiond_thread_manage_kernel)) {
1056 goto error_testpoint;
1057 }
1058
1059 health_code_update();
1060
1061 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
1062 goto error_testpoint;
1063 }
1064
1065 while (1) {
1066 health_code_update();
1067
1068 if (update_poll_flag == 1) {
1069 /* Clean events object. We are about to populate it again. */
1070 lttng_poll_clean(&events);
1071
1072 ret = sessiond_set_thread_pollset(&events, 2);
1073 if (ret < 0) {
1074 goto error_poll_create;
1075 }
1076
1077 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
1078 if (ret < 0) {
1079 goto error;
1080 }
1081
1082 /* This will add the available kernel channel if any. */
1083 ret = update_kernel_poll(&events);
1084 if (ret < 0) {
1085 goto error;
1086 }
1087 update_poll_flag = 0;
1088 }
1089
1090 DBG("Thread kernel polling");
1091
1092 /* Poll infinite value of time */
1093 restart:
1094 health_poll_entry();
1095 ret = lttng_poll_wait(&events, -1);
1096 DBG("Thread kernel return from poll on %d fds",
1097 LTTNG_POLL_GETNB(&events));
1098 health_poll_exit();
1099 if (ret < 0) {
1100 /*
1101 * Restart interrupted system call.
1102 */
1103 if (errno == EINTR) {
1104 goto restart;
1105 }
1106 goto error;
1107 } else if (ret == 0) {
1108 /* Should not happen since timeout is infinite */
1109 ERR("Return value of poll is 0 with an infinite timeout.\n"
1110 "This should not have happened! Continuing...");
1111 continue;
1112 }
1113
1114 nb_fd = ret;
1115
1116 for (i = 0; i < nb_fd; i++) {
1117 /* Fetch once the poll data */
1118 revents = LTTNG_POLL_GETEV(&events, i);
1119 pollfd = LTTNG_POLL_GETFD(&events, i);
1120
1121 health_code_update();
1122
1123 if (!revents) {
1124 /* No activity for this FD (poll implementation). */
1125 continue;
1126 }
1127
1128 /* Thread quit pipe has been closed. Killing thread. */
1129 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1130 if (ret) {
1131 err = 0;
1132 goto exit;
1133 }
1134
1135 /* Check for data on kernel pipe */
1136 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
1137 (void) lttng_read(kernel_poll_pipe[0],
1138 &tmp, 1);
1139 /*
1140 * Ret value is useless here, if this pipe gets any actions an
1141 * update is required anyway.
1142 */
1143 update_poll_flag = 1;
1144 continue;
1145 } else {
1146 /*
1147 * New CPU detected by the kernel. Adding kernel stream to
1148 * kernel session and updating the kernel consumer
1149 */
1150 if (revents & LPOLLIN) {
1151 ret = update_kernel_stream(&kconsumer_data, pollfd);
1152 if (ret < 0) {
1153 continue;
1154 }
1155 break;
1156 /*
1157 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1158 * and unregister kernel stream at this point.
1159 */
1160 }
1161 }
1162 }
1163 }
1164
1165 exit:
1166 error:
1167 lttng_poll_clean(&events);
1168 error_poll_create:
1169 error_testpoint:
1170 utils_close_pipe(kernel_poll_pipe);
1171 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
1172 if (err) {
1173 health_error();
1174 ERR("Health error occurred in %s", __func__);
1175 WARN("Kernel thread died unexpectedly. "
1176 "Kernel tracing can continue but CPU hotplug is disabled.");
1177 }
1178 health_unregister(health_sessiond);
1179 DBG("Kernel thread dying");
1180 return NULL;
1181 }
1182
1183 /*
1184 * Signal pthread condition of the consumer data that the thread.
1185 */
1186 static void signal_consumer_condition(struct consumer_data *data, int state)
1187 {
1188 pthread_mutex_lock(&data->cond_mutex);
1189
1190 /*
1191 * The state is set before signaling. It can be any value, it's the waiter
1192 * job to correctly interpret this condition variable associated to the
1193 * consumer pthread_cond.
1194 *
1195 * A value of 0 means that the corresponding thread of the consumer data
1196 * was not started. 1 indicates that the thread has started and is ready
1197 * for action. A negative value means that there was an error during the
1198 * thread bootstrap.
1199 */
1200 data->consumer_thread_is_ready = state;
1201 (void) pthread_cond_signal(&data->cond);
1202
1203 pthread_mutex_unlock(&data->cond_mutex);
1204 }
1205
1206 /*
1207 * This thread manage the consumer error sent back to the session daemon.
1208 */
1209 static void *thread_manage_consumer(void *data)
1210 {
1211 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
1212 uint32_t revents, nb_fd;
1213 enum lttcomm_return_code code;
1214 struct lttng_poll_event events;
1215 struct consumer_data *consumer_data = data;
1216
1217 DBG("[thread] Manage consumer started");
1218
1219 rcu_register_thread();
1220 rcu_thread_online();
1221
1222 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
1223
1224 health_code_update();
1225
1226 /*
1227 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1228 * metadata_sock. Nothing more will be added to this poll set.
1229 */
1230 ret = sessiond_set_thread_pollset(&events, 3);
1231 if (ret < 0) {
1232 goto error_poll;
1233 }
1234
1235 /*
1236 * The error socket here is already in a listening state which was done
1237 * just before spawning this thread to avoid a race between the consumer
1238 * daemon exec trying to connect and the listen() call.
1239 */
1240 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
1241 if (ret < 0) {
1242 goto error;
1243 }
1244
1245 health_code_update();
1246
1247 /* Infinite blocking call, waiting for transmission */
1248 restart:
1249 health_poll_entry();
1250
1251 if (testpoint(sessiond_thread_manage_consumer)) {
1252 goto error;
1253 }
1254
1255 ret = lttng_poll_wait(&events, -1);
1256 health_poll_exit();
1257 if (ret < 0) {
1258 /*
1259 * Restart interrupted system call.
1260 */
1261 if (errno == EINTR) {
1262 goto restart;
1263 }
1264 goto error;
1265 }
1266
1267 nb_fd = ret;
1268
1269 for (i = 0; i < nb_fd; i++) {
1270 /* Fetch once the poll data */
1271 revents = LTTNG_POLL_GETEV(&events, i);
1272 pollfd = LTTNG_POLL_GETFD(&events, i);
1273
1274 health_code_update();
1275
1276 if (!revents) {
1277 /* No activity for this FD (poll implementation). */
1278 continue;
1279 }
1280
1281 /* Thread quit pipe has been closed. Killing thread. */
1282 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1283 if (ret) {
1284 err = 0;
1285 goto exit;
1286 }
1287
1288 /* Event on the registration socket */
1289 if (pollfd == consumer_data->err_sock) {
1290 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1291 ERR("consumer err socket poll error");
1292 goto error;
1293 }
1294 }
1295 }
1296
1297 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1298 if (sock < 0) {
1299 goto error;
1300 }
1301
1302 /*
1303 * Set the CLOEXEC flag. Return code is useless because either way, the
1304 * show must go on.
1305 */
1306 (void) utils_set_fd_cloexec(sock);
1307
1308 health_code_update();
1309
1310 DBG2("Receiving code from consumer err_sock");
1311
1312 /* Getting status code from kconsumerd */
1313 ret = lttcomm_recv_unix_sock(sock, &code,
1314 sizeof(enum lttcomm_return_code));
1315 if (ret <= 0) {
1316 goto error;
1317 }
1318
1319 health_code_update();
1320 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1321 /* Connect both socket, command and metadata. */
1322 consumer_data->cmd_sock =
1323 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1324 consumer_data->metadata_fd =
1325 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1326 if (consumer_data->cmd_sock < 0
1327 || consumer_data->metadata_fd < 0) {
1328 PERROR("consumer connect cmd socket");
1329 /* On error, signal condition and quit. */
1330 signal_consumer_condition(consumer_data, -1);
1331 goto error;
1332 }
1333 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1334 /* Create metadata socket lock. */
1335 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1336 if (consumer_data->metadata_sock.lock == NULL) {
1337 PERROR("zmalloc pthread mutex");
1338 ret = -1;
1339 goto error;
1340 }
1341 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1342
1343 signal_consumer_condition(consumer_data, 1);
1344 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1345 DBG("Consumer metadata socket ready (fd: %d)",
1346 consumer_data->metadata_fd);
1347 } else {
1348 ERR("consumer error when waiting for SOCK_READY : %s",
1349 lttcomm_get_readable_code(-code));
1350 goto error;
1351 }
1352
1353 /* Remove the consumerd error sock since we've established a connexion */
1354 ret = lttng_poll_del(&events, consumer_data->err_sock);
1355 if (ret < 0) {
1356 goto error;
1357 }
1358
1359 /* Add new accepted error socket. */
1360 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1361 if (ret < 0) {
1362 goto error;
1363 }
1364
1365 /* Add metadata socket that is successfully connected. */
1366 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1367 LPOLLIN | LPOLLRDHUP);
1368 if (ret < 0) {
1369 goto error;
1370 }
1371
1372 health_code_update();
1373
1374 /* Infinite blocking call, waiting for transmission */
1375 restart_poll:
1376 while (1) {
1377 health_code_update();
1378
1379 /* Exit the thread because the thread quit pipe has been triggered. */
1380 if (should_quit) {
1381 /* Not a health error. */
1382 err = 0;
1383 goto exit;
1384 }
1385
1386 health_poll_entry();
1387 ret = lttng_poll_wait(&events, -1);
1388 health_poll_exit();
1389 if (ret < 0) {
1390 /*
1391 * Restart interrupted system call.
1392 */
1393 if (errno == EINTR) {
1394 goto restart_poll;
1395 }
1396 goto error;
1397 }
1398
1399 nb_fd = ret;
1400
1401 for (i = 0; i < nb_fd; i++) {
1402 /* Fetch once the poll data */
1403 revents = LTTNG_POLL_GETEV(&events, i);
1404 pollfd = LTTNG_POLL_GETFD(&events, i);
1405
1406 health_code_update();
1407
1408 if (!revents) {
1409 /* No activity for this FD (poll implementation). */
1410 continue;
1411 }
1412
1413 /*
1414 * Thread quit pipe has been triggered, flag that we should stop
1415 * but continue the current loop to handle potential data from
1416 * consumer.
1417 */
1418 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
1419
1420 if (pollfd == sock) {
1421 /* Event on the consumerd socket */
1422 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1423 ERR("consumer err socket second poll error");
1424 goto error;
1425 }
1426 health_code_update();
1427 /* Wait for any kconsumerd error */
1428 ret = lttcomm_recv_unix_sock(sock, &code,
1429 sizeof(enum lttcomm_return_code));
1430 if (ret <= 0) {
1431 ERR("consumer closed the command socket");
1432 goto error;
1433 }
1434
1435 ERR("consumer return code : %s",
1436 lttcomm_get_readable_code(-code));
1437
1438 goto exit;
1439 } else if (pollfd == consumer_data->metadata_fd) {
1440 /* UST metadata requests */
1441 ret = ust_consumer_metadata_request(
1442 &consumer_data->metadata_sock);
1443 if (ret < 0) {
1444 ERR("Handling metadata request");
1445 goto error;
1446 }
1447 }
1448 /* No need for an else branch all FDs are tested prior. */
1449 }
1450 health_code_update();
1451 }
1452
1453 exit:
1454 error:
1455 /*
1456 * We lock here because we are about to close the sockets and some other
1457 * thread might be using them so get exclusive access which will abort all
1458 * other consumer command by other threads.
1459 */
1460 pthread_mutex_lock(&consumer_data->lock);
1461
1462 /* Immediately set the consumerd state to stopped */
1463 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1464 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1465 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1466 consumer_data->type == LTTNG_CONSUMER32_UST) {
1467 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1468 } else {
1469 /* Code flow error... */
1470 assert(0);
1471 }
1472
1473 if (consumer_data->err_sock >= 0) {
1474 ret = close(consumer_data->err_sock);
1475 if (ret) {
1476 PERROR("close");
1477 }
1478 consumer_data->err_sock = -1;
1479 }
1480 if (consumer_data->cmd_sock >= 0) {
1481 ret = close(consumer_data->cmd_sock);
1482 if (ret) {
1483 PERROR("close");
1484 }
1485 consumer_data->cmd_sock = -1;
1486 }
1487 if (consumer_data->metadata_sock.fd_ptr &&
1488 *consumer_data->metadata_sock.fd_ptr >= 0) {
1489 ret = close(*consumer_data->metadata_sock.fd_ptr);
1490 if (ret) {
1491 PERROR("close");
1492 }
1493 }
1494 if (sock >= 0) {
1495 ret = close(sock);
1496 if (ret) {
1497 PERROR("close");
1498 }
1499 }
1500
1501 unlink(consumer_data->err_unix_sock_path);
1502 unlink(consumer_data->cmd_unix_sock_path);
1503 consumer_data->pid = 0;
1504 pthread_mutex_unlock(&consumer_data->lock);
1505
1506 /* Cleanup metadata socket mutex. */
1507 if (consumer_data->metadata_sock.lock) {
1508 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1509 free(consumer_data->metadata_sock.lock);
1510 }
1511 lttng_poll_clean(&events);
1512 error_poll:
1513 if (err) {
1514 health_error();
1515 ERR("Health error occurred in %s", __func__);
1516 }
1517 health_unregister(health_sessiond);
1518 DBG("consumer thread cleanup completed");
1519
1520 rcu_thread_offline();
1521 rcu_unregister_thread();
1522
1523 return NULL;
1524 }
1525
1526 /*
1527 * This thread manage application communication.
1528 */
1529 static void *thread_manage_apps(void *data)
1530 {
1531 int i, ret, pollfd, err = -1;
1532 ssize_t size_ret;
1533 uint32_t revents, nb_fd;
1534 struct lttng_poll_event events;
1535
1536 DBG("[thread] Manage application started");
1537
1538 rcu_register_thread();
1539 rcu_thread_online();
1540
1541 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1542
1543 if (testpoint(sessiond_thread_manage_apps)) {
1544 goto error_testpoint;
1545 }
1546
1547 health_code_update();
1548
1549 ret = sessiond_set_thread_pollset(&events, 2);
1550 if (ret < 0) {
1551 goto error_poll_create;
1552 }
1553
1554 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1555 if (ret < 0) {
1556 goto error;
1557 }
1558
1559 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1560 goto error;
1561 }
1562
1563 health_code_update();
1564
1565 while (1) {
1566 DBG("Apps thread polling");
1567
1568 /* Inifinite blocking call, waiting for transmission */
1569 restart:
1570 health_poll_entry();
1571 ret = lttng_poll_wait(&events, -1);
1572 DBG("Apps thread return from poll on %d fds",
1573 LTTNG_POLL_GETNB(&events));
1574 health_poll_exit();
1575 if (ret < 0) {
1576 /*
1577 * Restart interrupted system call.
1578 */
1579 if (errno == EINTR) {
1580 goto restart;
1581 }
1582 goto error;
1583 }
1584
1585 nb_fd = ret;
1586
1587 for (i = 0; i < nb_fd; i++) {
1588 /* Fetch once the poll data */
1589 revents = LTTNG_POLL_GETEV(&events, i);
1590 pollfd = LTTNG_POLL_GETFD(&events, i);
1591
1592 health_code_update();
1593
1594 if (!revents) {
1595 /* No activity for this FD (poll implementation). */
1596 continue;
1597 }
1598
1599 /* Thread quit pipe has been closed. Killing thread. */
1600 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1601 if (ret) {
1602 err = 0;
1603 goto exit;
1604 }
1605
1606 /* Inspect the apps cmd pipe */
1607 if (pollfd == apps_cmd_pipe[0]) {
1608 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1609 ERR("Apps command pipe error");
1610 goto error;
1611 } else if (revents & LPOLLIN) {
1612 int sock;
1613
1614 /* Empty pipe */
1615 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1616 if (size_ret < sizeof(sock)) {
1617 PERROR("read apps cmd pipe");
1618 goto error;
1619 }
1620
1621 health_code_update();
1622
1623 /*
1624 * We only monitor the error events of the socket. This
1625 * thread does not handle any incoming data from UST
1626 * (POLLIN).
1627 */
1628 ret = lttng_poll_add(&events, sock,
1629 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1630 if (ret < 0) {
1631 goto error;
1632 }
1633
1634 DBG("Apps with sock %d added to poll set", sock);
1635 }
1636 } else {
1637 /*
1638 * At this point, we know that a registered application made
1639 * the event at poll_wait.
1640 */
1641 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1642 /* Removing from the poll set */
1643 ret = lttng_poll_del(&events, pollfd);
1644 if (ret < 0) {
1645 goto error;
1646 }
1647
1648 /* Socket closed on remote end. */
1649 ust_app_unregister(pollfd);
1650 }
1651 }
1652
1653 health_code_update();
1654 }
1655 }
1656
1657 exit:
1658 error:
1659 lttng_poll_clean(&events);
1660 error_poll_create:
1661 error_testpoint:
1662 utils_close_pipe(apps_cmd_pipe);
1663 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1664
1665 /*
1666 * We don't clean the UST app hash table here since already registered
1667 * applications can still be controlled so let them be until the session
1668 * daemon dies or the applications stop.
1669 */
1670
1671 if (err) {
1672 health_error();
1673 ERR("Health error occurred in %s", __func__);
1674 }
1675 health_unregister(health_sessiond);
1676 DBG("Application communication apps thread cleanup complete");
1677 rcu_thread_offline();
1678 rcu_unregister_thread();
1679 return NULL;
1680 }
1681
1682 /*
1683 * Send a socket to a thread This is called from the dispatch UST registration
1684 * thread once all sockets are set for the application.
1685 *
1686 * The sock value can be invalid, we don't really care, the thread will handle
1687 * it and make the necessary cleanup if so.
1688 *
1689 * On success, return 0 else a negative value being the errno message of the
1690 * write().
1691 */
1692 static int send_socket_to_thread(int fd, int sock)
1693 {
1694 ssize_t ret;
1695
1696 /*
1697 * It's possible that the FD is set as invalid with -1 concurrently just
1698 * before calling this function being a shutdown state of the thread.
1699 */
1700 if (fd < 0) {
1701 ret = -EBADF;
1702 goto error;
1703 }
1704
1705 ret = lttng_write(fd, &sock, sizeof(sock));
1706 if (ret < sizeof(sock)) {
1707 PERROR("write apps pipe %d", fd);
1708 if (ret < 0) {
1709 ret = -errno;
1710 }
1711 goto error;
1712 }
1713
1714 /* All good. Don't send back the write positive ret value. */
1715 ret = 0;
1716 error:
1717 return (int) ret;
1718 }
1719
1720 /*
1721 * Sanitize the wait queue of the dispatch registration thread meaning removing
1722 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1723 * notify socket is never received.
1724 */
1725 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1726 {
1727 int ret, nb_fd = 0, i;
1728 unsigned int fd_added = 0;
1729 struct lttng_poll_event events;
1730 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1731
1732 assert(wait_queue);
1733
1734 lttng_poll_init(&events);
1735
1736 /* Just skip everything for an empty queue. */
1737 if (!wait_queue->count) {
1738 goto end;
1739 }
1740
1741 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1742 if (ret < 0) {
1743 goto error_create;
1744 }
1745
1746 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1747 &wait_queue->head, head) {
1748 assert(wait_node->app);
1749 ret = lttng_poll_add(&events, wait_node->app->sock,
1750 LPOLLHUP | LPOLLERR);
1751 if (ret < 0) {
1752 goto error;
1753 }
1754
1755 fd_added = 1;
1756 }
1757
1758 if (!fd_added) {
1759 goto end;
1760 }
1761
1762 /*
1763 * Poll but don't block so we can quickly identify the faulty events and
1764 * clean them afterwards from the wait queue.
1765 */
1766 ret = lttng_poll_wait(&events, 0);
1767 if (ret < 0) {
1768 goto error;
1769 }
1770 nb_fd = ret;
1771
1772 for (i = 0; i < nb_fd; i++) {
1773 /* Get faulty FD. */
1774 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1775 int pollfd = LTTNG_POLL_GETFD(&events, i);
1776
1777 if (!revents) {
1778 /* No activity for this FD (poll implementation). */
1779 continue;
1780 }
1781
1782 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1783 &wait_queue->head, head) {
1784 if (pollfd == wait_node->app->sock &&
1785 (revents & (LPOLLHUP | LPOLLERR))) {
1786 cds_list_del(&wait_node->head);
1787 wait_queue->count--;
1788 ust_app_destroy(wait_node->app);
1789 free(wait_node);
1790 break;
1791 }
1792 }
1793 }
1794
1795 if (nb_fd > 0) {
1796 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1797 }
1798
1799 end:
1800 lttng_poll_clean(&events);
1801 return;
1802
1803 error:
1804 lttng_poll_clean(&events);
1805 error_create:
1806 ERR("Unable to sanitize wait queue");
1807 return;
1808 }
1809
1810 /*
1811 * Dispatch request from the registration threads to the application
1812 * communication thread.
1813 */
1814 static void *thread_dispatch_ust_registration(void *data)
1815 {
1816 int ret, err = -1;
1817 struct cds_wfcq_node *node;
1818 struct ust_command *ust_cmd = NULL;
1819 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1820 struct ust_reg_wait_queue wait_queue = {
1821 .count = 0,
1822 };
1823
1824 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1825
1826 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1827 goto error_testpoint;
1828 }
1829
1830 health_code_update();
1831
1832 CDS_INIT_LIST_HEAD(&wait_queue.head);
1833
1834 DBG("[thread] Dispatch UST command started");
1835
1836 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1837 health_code_update();
1838
1839 /* Atomically prepare the queue futex */
1840 futex_nto1_prepare(&ust_cmd_queue.futex);
1841
1842 do {
1843 struct ust_app *app = NULL;
1844 ust_cmd = NULL;
1845
1846 /*
1847 * Make sure we don't have node(s) that have hung up before receiving
1848 * the notify socket. This is to clean the list in order to avoid
1849 * memory leaks from notify socket that are never seen.
1850 */
1851 sanitize_wait_queue(&wait_queue);
1852
1853 health_code_update();
1854 /* Dequeue command for registration */
1855 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1856 if (node == NULL) {
1857 DBG("Woken up but nothing in the UST command queue");
1858 /* Continue thread execution */
1859 break;
1860 }
1861
1862 ust_cmd = caa_container_of(node, struct ust_command, node);
1863
1864 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1865 " gid:%d sock:%d name:%s (version %d.%d)",
1866 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1867 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1868 ust_cmd->sock, ust_cmd->reg_msg.name,
1869 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1870
1871 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1872 wait_node = zmalloc(sizeof(*wait_node));
1873 if (!wait_node) {
1874 PERROR("zmalloc wait_node dispatch");
1875 ret = close(ust_cmd->sock);
1876 if (ret < 0) {
1877 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1878 }
1879 lttng_fd_put(LTTNG_FD_APPS, 1);
1880 free(ust_cmd);
1881 goto error;
1882 }
1883 CDS_INIT_LIST_HEAD(&wait_node->head);
1884
1885 /* Create application object if socket is CMD. */
1886 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1887 ust_cmd->sock);
1888 if (!wait_node->app) {
1889 ret = close(ust_cmd->sock);
1890 if (ret < 0) {
1891 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1892 }
1893 lttng_fd_put(LTTNG_FD_APPS, 1);
1894 free(wait_node);
1895 free(ust_cmd);
1896 continue;
1897 }
1898 /*
1899 * Add application to the wait queue so we can set the notify
1900 * socket before putting this object in the global ht.
1901 */
1902 cds_list_add(&wait_node->head, &wait_queue.head);
1903 wait_queue.count++;
1904
1905 free(ust_cmd);
1906 /*
1907 * We have to continue here since we don't have the notify
1908 * socket and the application MUST be added to the hash table
1909 * only at that moment.
1910 */
1911 continue;
1912 } else {
1913 /*
1914 * Look for the application in the local wait queue and set the
1915 * notify socket if found.
1916 */
1917 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1918 &wait_queue.head, head) {
1919 health_code_update();
1920 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1921 wait_node->app->notify_sock = ust_cmd->sock;
1922 cds_list_del(&wait_node->head);
1923 wait_queue.count--;
1924 app = wait_node->app;
1925 free(wait_node);
1926 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1927 break;
1928 }
1929 }
1930
1931 /*
1932 * With no application at this stage the received socket is
1933 * basically useless so close it before we free the cmd data
1934 * structure for good.
1935 */
1936 if (!app) {
1937 ret = close(ust_cmd->sock);
1938 if (ret < 0) {
1939 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1940 }
1941 lttng_fd_put(LTTNG_FD_APPS, 1);
1942 }
1943 free(ust_cmd);
1944 }
1945
1946 if (app) {
1947 /*
1948 * @session_lock_list
1949 *
1950 * Lock the global session list so from the register up to the
1951 * registration done message, no thread can see the application
1952 * and change its state.
1953 */
1954 session_lock_list();
1955 rcu_read_lock();
1956
1957 /*
1958 * Add application to the global hash table. This needs to be
1959 * done before the update to the UST registry can locate the
1960 * application.
1961 */
1962 ust_app_add(app);
1963
1964 /* Set app version. This call will print an error if needed. */
1965 (void) ust_app_version(app);
1966
1967 /* Send notify socket through the notify pipe. */
1968 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1969 app->notify_sock);
1970 if (ret < 0) {
1971 rcu_read_unlock();
1972 session_unlock_list();
1973 /*
1974 * No notify thread, stop the UST tracing. However, this is
1975 * not an internal error of the this thread thus setting
1976 * the health error code to a normal exit.
1977 */
1978 err = 0;
1979 goto error;
1980 }
1981
1982 /*
1983 * Update newly registered application with the tracing
1984 * registry info already enabled information.
1985 */
1986 update_ust_app(app->sock);
1987
1988 /*
1989 * Don't care about return value. Let the manage apps threads
1990 * handle app unregistration upon socket close.
1991 */
1992 (void) ust_app_register_done(app->sock);
1993
1994 /*
1995 * Even if the application socket has been closed, send the app
1996 * to the thread and unregistration will take place at that
1997 * place.
1998 */
1999 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
2000 if (ret < 0) {
2001 rcu_read_unlock();
2002 session_unlock_list();
2003 /*
2004 * No apps. thread, stop the UST tracing. However, this is
2005 * not an internal error of the this thread thus setting
2006 * the health error code to a normal exit.
2007 */
2008 err = 0;
2009 goto error;
2010 }
2011
2012 rcu_read_unlock();
2013 session_unlock_list();
2014 }
2015 } while (node != NULL);
2016
2017 health_poll_entry();
2018 /* Futex wait on queue. Blocking call on futex() */
2019 futex_nto1_wait(&ust_cmd_queue.futex);
2020 health_poll_exit();
2021 }
2022 /* Normal exit, no error */
2023 err = 0;
2024
2025 error:
2026 /* Clean up wait queue. */
2027 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
2028 &wait_queue.head, head) {
2029 cds_list_del(&wait_node->head);
2030 wait_queue.count--;
2031 free(wait_node);
2032 }
2033
2034 /* Empty command queue. */
2035 for (;;) {
2036 /* Dequeue command for registration */
2037 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
2038 if (node == NULL) {
2039 break;
2040 }
2041 ust_cmd = caa_container_of(node, struct ust_command, node);
2042 ret = close(ust_cmd->sock);
2043 if (ret < 0) {
2044 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
2045 }
2046 lttng_fd_put(LTTNG_FD_APPS, 1);
2047 free(ust_cmd);
2048 }
2049
2050 error_testpoint:
2051 DBG("Dispatch thread dying");
2052 if (err) {
2053 health_error();
2054 ERR("Health error occurred in %s", __func__);
2055 }
2056 health_unregister(health_sessiond);
2057 return NULL;
2058 }
2059
2060 /*
2061 * This thread manage application registration.
2062 */
2063 static void *thread_registration_apps(void *data)
2064 {
2065 int sock = -1, i, ret, pollfd, err = -1;
2066 uint32_t revents, nb_fd;
2067 struct lttng_poll_event events;
2068 /*
2069 * Get allocated in this thread, enqueued to a global queue, dequeued and
2070 * freed in the manage apps thread.
2071 */
2072 struct ust_command *ust_cmd = NULL;
2073
2074 DBG("[thread] Manage application registration started");
2075
2076 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
2077
2078 if (testpoint(sessiond_thread_registration_apps)) {
2079 goto error_testpoint;
2080 }
2081
2082 ret = lttcomm_listen_unix_sock(apps_sock);
2083 if (ret < 0) {
2084 goto error_listen;
2085 }
2086
2087 /*
2088 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2089 * more will be added to this poll set.
2090 */
2091 ret = sessiond_set_thread_pollset(&events, 2);
2092 if (ret < 0) {
2093 goto error_create_poll;
2094 }
2095
2096 /* Add the application registration socket */
2097 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
2098 if (ret < 0) {
2099 goto error_poll_add;
2100 }
2101
2102 /* Notify all applications to register */
2103 ret = notify_ust_apps(1);
2104 if (ret < 0) {
2105 ERR("Failed to notify applications or create the wait shared memory.\n"
2106 "Execution continues but there might be problem for already\n"
2107 "running applications that wishes to register.");
2108 }
2109
2110 while (1) {
2111 DBG("Accepting application registration");
2112
2113 /* Inifinite blocking call, waiting for transmission */
2114 restart:
2115 health_poll_entry();
2116 ret = lttng_poll_wait(&events, -1);
2117 health_poll_exit();
2118 if (ret < 0) {
2119 /*
2120 * Restart interrupted system call.
2121 */
2122 if (errno == EINTR) {
2123 goto restart;
2124 }
2125 goto error;
2126 }
2127
2128 nb_fd = ret;
2129
2130 for (i = 0; i < nb_fd; i++) {
2131 health_code_update();
2132
2133 /* Fetch once the poll data */
2134 revents = LTTNG_POLL_GETEV(&events, i);
2135 pollfd = LTTNG_POLL_GETFD(&events, i);
2136
2137 if (!revents) {
2138 /* No activity for this FD (poll implementation). */
2139 continue;
2140 }
2141
2142 /* Thread quit pipe has been closed. Killing thread. */
2143 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
2144 if (ret) {
2145 err = 0;
2146 goto exit;
2147 }
2148
2149 /* Event on the registration socket */
2150 if (pollfd == apps_sock) {
2151 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2152 ERR("Register apps socket poll error");
2153 goto error;
2154 } else if (revents & LPOLLIN) {
2155 sock = lttcomm_accept_unix_sock(apps_sock);
2156 if (sock < 0) {
2157 goto error;
2158 }
2159
2160 /*
2161 * Set socket timeout for both receiving and ending.
2162 * app_socket_timeout is in seconds, whereas
2163 * lttcomm_setsockopt_rcv_timeout and
2164 * lttcomm_setsockopt_snd_timeout expect msec as
2165 * parameter.
2166 */
2167 (void) lttcomm_setsockopt_rcv_timeout(sock,
2168 app_socket_timeout * 1000);
2169 (void) lttcomm_setsockopt_snd_timeout(sock,
2170 app_socket_timeout * 1000);
2171
2172 /*
2173 * Set the CLOEXEC flag. Return code is useless because
2174 * either way, the show must go on.
2175 */
2176 (void) utils_set_fd_cloexec(sock);
2177
2178 /* Create UST registration command for enqueuing */
2179 ust_cmd = zmalloc(sizeof(struct ust_command));
2180 if (ust_cmd == NULL) {
2181 PERROR("ust command zmalloc");
2182 ret = close(sock);
2183 if (ret) {
2184 PERROR("close");
2185 }
2186 goto error;
2187 }
2188
2189 /*
2190 * Using message-based transmissions to ensure we don't
2191 * have to deal with partially received messages.
2192 */
2193 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2194 if (ret < 0) {
2195 ERR("Exhausted file descriptors allowed for applications.");
2196 free(ust_cmd);
2197 ret = close(sock);
2198 if (ret) {
2199 PERROR("close");
2200 }
2201 sock = -1;
2202 continue;
2203 }
2204
2205 health_code_update();
2206 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2207 if (ret < 0) {
2208 free(ust_cmd);
2209 /* Close socket of the application. */
2210 ret = close(sock);
2211 if (ret) {
2212 PERROR("close");
2213 }
2214 lttng_fd_put(LTTNG_FD_APPS, 1);
2215 sock = -1;
2216 continue;
2217 }
2218 health_code_update();
2219
2220 ust_cmd->sock = sock;
2221 sock = -1;
2222
2223 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2224 " gid:%d sock:%d name:%s (version %d.%d)",
2225 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2226 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2227 ust_cmd->sock, ust_cmd->reg_msg.name,
2228 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
2229
2230 /*
2231 * Lock free enqueue the registration request. The red pill
2232 * has been taken! This apps will be part of the *system*.
2233 */
2234 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
2235
2236 /*
2237 * Wake the registration queue futex. Implicit memory
2238 * barrier with the exchange in cds_wfcq_enqueue.
2239 */
2240 futex_nto1_wake(&ust_cmd_queue.futex);
2241 }
2242 }
2243 }
2244 }
2245
2246 exit:
2247 error:
2248 /* Notify that the registration thread is gone */
2249 notify_ust_apps(0);
2250
2251 if (apps_sock >= 0) {
2252 ret = close(apps_sock);
2253 if (ret) {
2254 PERROR("close");
2255 }
2256 }
2257 if (sock >= 0) {
2258 ret = close(sock);
2259 if (ret) {
2260 PERROR("close");
2261 }
2262 lttng_fd_put(LTTNG_FD_APPS, 1);
2263 }
2264 unlink(apps_unix_sock_path);
2265
2266 error_poll_add:
2267 lttng_poll_clean(&events);
2268 error_listen:
2269 error_create_poll:
2270 error_testpoint:
2271 DBG("UST Registration thread cleanup complete");
2272 if (err) {
2273 health_error();
2274 ERR("Health error occurred in %s", __func__);
2275 }
2276 health_unregister(health_sessiond);
2277
2278 return NULL;
2279 }
2280
2281 /*
2282 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2283 * exec or it will fails.
2284 */
2285 static int spawn_consumer_thread(struct consumer_data *consumer_data)
2286 {
2287 int ret, clock_ret;
2288 struct timespec timeout;
2289
2290 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2291 consumer_data->consumer_thread_is_ready = 0;
2292
2293 /* Setup pthread condition */
2294 ret = pthread_condattr_init(&consumer_data->condattr);
2295 if (ret) {
2296 errno = ret;
2297 PERROR("pthread_condattr_init consumer data");
2298 goto error;
2299 }
2300
2301 /*
2302 * Set the monotonic clock in order to make sure we DO NOT jump in time
2303 * between the clock_gettime() call and the timedwait call. See bug #324
2304 * for a more details and how we noticed it.
2305 */
2306 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2307 if (ret) {
2308 errno = ret;
2309 PERROR("pthread_condattr_setclock consumer data");
2310 goto error;
2311 }
2312
2313 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2314 if (ret) {
2315 errno = ret;
2316 PERROR("pthread_cond_init consumer data");
2317 goto error;
2318 }
2319
2320 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2321 consumer_data);
2322 if (ret) {
2323 errno = ret;
2324 PERROR("pthread_create consumer");
2325 ret = -1;
2326 goto error;
2327 }
2328
2329 /* We are about to wait on a pthread condition */
2330 pthread_mutex_lock(&consumer_data->cond_mutex);
2331
2332 /* Get time for sem_timedwait absolute timeout */
2333 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2334 /*
2335 * Set the timeout for the condition timed wait even if the clock gettime
2336 * call fails since we might loop on that call and we want to avoid to
2337 * increment the timeout too many times.
2338 */
2339 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2340
2341 /*
2342 * The following loop COULD be skipped in some conditions so this is why we
2343 * set ret to 0 in order to make sure at least one round of the loop is
2344 * done.
2345 */
2346 ret = 0;
2347
2348 /*
2349 * Loop until the condition is reached or when a timeout is reached. Note
2350 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2351 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2352 * possible. This loop does not take any chances and works with both of
2353 * them.
2354 */
2355 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2356 if (clock_ret < 0) {
2357 PERROR("clock_gettime spawn consumer");
2358 /* Infinite wait for the consumerd thread to be ready */
2359 ret = pthread_cond_wait(&consumer_data->cond,
2360 &consumer_data->cond_mutex);
2361 } else {
2362 ret = pthread_cond_timedwait(&consumer_data->cond,
2363 &consumer_data->cond_mutex, &timeout);
2364 }
2365 }
2366
2367 /* Release the pthread condition */
2368 pthread_mutex_unlock(&consumer_data->cond_mutex);
2369
2370 if (ret != 0) {
2371 errno = ret;
2372 if (ret == ETIMEDOUT) {
2373 int pth_ret;
2374
2375 /*
2376 * Call has timed out so we kill the kconsumerd_thread and return
2377 * an error.
2378 */
2379 ERR("Condition timed out. The consumer thread was never ready."
2380 " Killing it");
2381 pth_ret = pthread_cancel(consumer_data->thread);
2382 if (pth_ret < 0) {
2383 PERROR("pthread_cancel consumer thread");
2384 }
2385 } else {
2386 PERROR("pthread_cond_wait failed consumer thread");
2387 }
2388 /* Caller is expecting a negative value on failure. */
2389 ret = -1;
2390 goto error;
2391 }
2392
2393 pthread_mutex_lock(&consumer_data->pid_mutex);
2394 if (consumer_data->pid == 0) {
2395 ERR("Consumerd did not start");
2396 pthread_mutex_unlock(&consumer_data->pid_mutex);
2397 goto error;
2398 }
2399 pthread_mutex_unlock(&consumer_data->pid_mutex);
2400
2401 return 0;
2402
2403 error:
2404 return ret;
2405 }
2406
2407 /*
2408 * Join consumer thread
2409 */
2410 static int join_consumer_thread(struct consumer_data *consumer_data)
2411 {
2412 void *status;
2413
2414 /* Consumer pid must be a real one. */
2415 if (consumer_data->pid > 0) {
2416 int ret;
2417 ret = kill(consumer_data->pid, SIGTERM);
2418 if (ret) {
2419 PERROR("Error killing consumer daemon");
2420 return ret;
2421 }
2422 return pthread_join(consumer_data->thread, &status);
2423 } else {
2424 return 0;
2425 }
2426 }
2427
2428 /*
2429 * Fork and exec a consumer daemon (consumerd).
2430 *
2431 * Return pid if successful else -1.
2432 */
2433 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2434 {
2435 int ret;
2436 pid_t pid;
2437 const char *consumer_to_use;
2438 const char *verbosity;
2439 struct stat st;
2440
2441 DBG("Spawning consumerd");
2442
2443 pid = fork();
2444 if (pid == 0) {
2445 /*
2446 * Exec consumerd.
2447 */
2448 if (opt_verbose_consumer) {
2449 verbosity = "--verbose";
2450 } else if (lttng_opt_quiet) {
2451 verbosity = "--quiet";
2452 } else {
2453 verbosity = "";
2454 }
2455
2456 switch (consumer_data->type) {
2457 case LTTNG_CONSUMER_KERNEL:
2458 /*
2459 * Find out which consumerd to execute. We will first try the
2460 * 64-bit path, then the sessiond's installation directory, and
2461 * fallback on the 32-bit one,
2462 */
2463 DBG3("Looking for a kernel consumer at these locations:");
2464 DBG3(" 1) %s", consumerd64_bin);
2465 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2466 DBG3(" 3) %s", consumerd32_bin);
2467 if (stat(consumerd64_bin, &st) == 0) {
2468 DBG3("Found location #1");
2469 consumer_to_use = consumerd64_bin;
2470 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
2471 DBG3("Found location #2");
2472 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
2473 } else if (stat(consumerd32_bin, &st) == 0) {
2474 DBG3("Found location #3");
2475 consumer_to_use = consumerd32_bin;
2476 } else {
2477 DBG("Could not find any valid consumerd executable");
2478 ret = -EINVAL;
2479 break;
2480 }
2481 DBG("Using kernel consumer at: %s", consumer_to_use);
2482 ret = execl(consumer_to_use,
2483 "lttng-consumerd", verbosity, "-k",
2484 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2485 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2486 "--group", tracing_group_name,
2487 NULL);
2488 break;
2489 case LTTNG_CONSUMER64_UST:
2490 {
2491 char *tmpnew = NULL;
2492
2493 if (consumerd64_libdir[0] != '\0') {
2494 char *tmp;
2495 size_t tmplen;
2496
2497 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2498 if (!tmp) {
2499 tmp = "";
2500 }
2501 tmplen = strlen("LD_LIBRARY_PATH=")
2502 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2503 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2504 if (!tmpnew) {
2505 ret = -ENOMEM;
2506 goto error;
2507 }
2508 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2509 strcat(tmpnew, consumerd64_libdir);
2510 if (tmp[0] != '\0') {
2511 strcat(tmpnew, ":");
2512 strcat(tmpnew, tmp);
2513 }
2514 ret = putenv(tmpnew);
2515 if (ret) {
2516 ret = -errno;
2517 free(tmpnew);
2518 goto error;
2519 }
2520 }
2521 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
2522 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
2523 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2524 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2525 "--group", tracing_group_name,
2526 NULL);
2527 if (consumerd64_libdir[0] != '\0') {
2528 free(tmpnew);
2529 }
2530 break;
2531 }
2532 case LTTNG_CONSUMER32_UST:
2533 {
2534 char *tmpnew = NULL;
2535
2536 if (consumerd32_libdir[0] != '\0') {
2537 char *tmp;
2538 size_t tmplen;
2539
2540 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2541 if (!tmp) {
2542 tmp = "";
2543 }
2544 tmplen = strlen("LD_LIBRARY_PATH=")
2545 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2546 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2547 if (!tmpnew) {
2548 ret = -ENOMEM;
2549 goto error;
2550 }
2551 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2552 strcat(tmpnew, consumerd32_libdir);
2553 if (tmp[0] != '\0') {
2554 strcat(tmpnew, ":");
2555 strcat(tmpnew, tmp);
2556 }
2557 ret = putenv(tmpnew);
2558 if (ret) {
2559 ret = -errno;
2560 free(tmpnew);
2561 goto error;
2562 }
2563 }
2564 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
2565 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
2566 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2567 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2568 "--group", tracing_group_name,
2569 NULL);
2570 if (consumerd32_libdir[0] != '\0') {
2571 free(tmpnew);
2572 }
2573 break;
2574 }
2575 default:
2576 PERROR("unknown consumer type");
2577 exit(EXIT_FAILURE);
2578 }
2579 if (errno != 0) {
2580 PERROR("Consumer execl()");
2581 }
2582 /* Reaching this point, we got a failure on our execl(). */
2583 exit(EXIT_FAILURE);
2584 } else if (pid > 0) {
2585 ret = pid;
2586 } else {
2587 PERROR("start consumer fork");
2588 ret = -errno;
2589 }
2590 error:
2591 return ret;
2592 }
2593
2594 /*
2595 * Spawn the consumerd daemon and session daemon thread.
2596 */
2597 static int start_consumerd(struct consumer_data *consumer_data)
2598 {
2599 int ret;
2600
2601 /*
2602 * Set the listen() state on the socket since there is a possible race
2603 * between the exec() of the consumer daemon and this call if place in the
2604 * consumer thread. See bug #366 for more details.
2605 */
2606 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2607 if (ret < 0) {
2608 goto error;
2609 }
2610
2611 pthread_mutex_lock(&consumer_data->pid_mutex);
2612 if (consumer_data->pid != 0) {
2613 pthread_mutex_unlock(&consumer_data->pid_mutex);
2614 goto end;
2615 }
2616
2617 ret = spawn_consumerd(consumer_data);
2618 if (ret < 0) {
2619 ERR("Spawning consumerd failed");
2620 pthread_mutex_unlock(&consumer_data->pid_mutex);
2621 goto error;
2622 }
2623
2624 /* Setting up the consumer_data pid */
2625 consumer_data->pid = ret;
2626 DBG2("Consumer pid %d", consumer_data->pid);
2627 pthread_mutex_unlock(&consumer_data->pid_mutex);
2628
2629 DBG2("Spawning consumer control thread");
2630 ret = spawn_consumer_thread(consumer_data);
2631 if (ret < 0) {
2632 ERR("Fatal error spawning consumer control thread");
2633 goto error;
2634 }
2635
2636 end:
2637 return 0;
2638
2639 error:
2640 /* Cleanup already created sockets on error. */
2641 if (consumer_data->err_sock >= 0) {
2642 int err;
2643
2644 err = close(consumer_data->err_sock);
2645 if (err < 0) {
2646 PERROR("close consumer data error socket");
2647 }
2648 }
2649 return ret;
2650 }
2651
2652 /*
2653 * Setup necessary data for kernel tracer action.
2654 */
2655 static int init_kernel_tracer(void)
2656 {
2657 int ret;
2658
2659 /* Modprobe lttng kernel modules */
2660 ret = modprobe_lttng_control();
2661 if (ret < 0) {
2662 goto error;
2663 }
2664
2665 /* Open debugfs lttng */
2666 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2667 if (kernel_tracer_fd < 0) {
2668 DBG("Failed to open %s", module_proc_lttng);
2669 ret = -1;
2670 goto error_open;
2671 }
2672
2673 /* Validate kernel version */
2674 ret = kernel_validate_version(kernel_tracer_fd);
2675 if (ret < 0) {
2676 goto error_version;
2677 }
2678
2679 ret = modprobe_lttng_data();
2680 if (ret < 0) {
2681 goto error_modules;
2682 }
2683
2684 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2685 return 0;
2686
2687 error_version:
2688 modprobe_remove_lttng_control();
2689 ret = close(kernel_tracer_fd);
2690 if (ret) {
2691 PERROR("close");
2692 }
2693 kernel_tracer_fd = -1;
2694 return LTTNG_ERR_KERN_VERSION;
2695
2696 error_modules:
2697 ret = close(kernel_tracer_fd);
2698 if (ret) {
2699 PERROR("close");
2700 }
2701
2702 error_open:
2703 modprobe_remove_lttng_control();
2704
2705 error:
2706 WARN("No kernel tracer available");
2707 kernel_tracer_fd = -1;
2708 if (!is_root) {
2709 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2710 } else {
2711 return LTTNG_ERR_KERN_NA;
2712 }
2713 }
2714
2715
2716 /*
2717 * Copy consumer output from the tracing session to the domain session. The
2718 * function also applies the right modification on a per domain basis for the
2719 * trace files destination directory.
2720 *
2721 * Should *NOT* be called with RCU read-side lock held.
2722 */
2723 static int copy_session_consumer(int domain, struct ltt_session *session)
2724 {
2725 int ret;
2726 const char *dir_name;
2727 struct consumer_output *consumer;
2728
2729 assert(session);
2730 assert(session->consumer);
2731
2732 switch (domain) {
2733 case LTTNG_DOMAIN_KERNEL:
2734 DBG3("Copying tracing session consumer output in kernel session");
2735 /*
2736 * XXX: We should audit the session creation and what this function
2737 * does "extra" in order to avoid a destroy since this function is used
2738 * in the domain session creation (kernel and ust) only. Same for UST
2739 * domain.
2740 */
2741 if (session->kernel_session->consumer) {
2742 consumer_output_put(session->kernel_session->consumer);
2743 }
2744 session->kernel_session->consumer =
2745 consumer_copy_output(session->consumer);
2746 /* Ease our life a bit for the next part */
2747 consumer = session->kernel_session->consumer;
2748 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2749 break;
2750 case LTTNG_DOMAIN_JUL:
2751 case LTTNG_DOMAIN_LOG4J:
2752 case LTTNG_DOMAIN_PYTHON:
2753 case LTTNG_DOMAIN_UST:
2754 DBG3("Copying tracing session consumer output in UST session");
2755 if (session->ust_session->consumer) {
2756 consumer_output_put(session->ust_session->consumer);
2757 }
2758 session->ust_session->consumer =
2759 consumer_copy_output(session->consumer);
2760 /* Ease our life a bit for the next part */
2761 consumer = session->ust_session->consumer;
2762 dir_name = DEFAULT_UST_TRACE_DIR;
2763 break;
2764 default:
2765 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2766 goto error;
2767 }
2768
2769 /* Append correct directory to subdir */
2770 strncat(consumer->subdir, dir_name,
2771 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2772 DBG3("Copy session consumer subdir %s", consumer->subdir);
2773
2774 ret = LTTNG_OK;
2775
2776 error:
2777 return ret;
2778 }
2779
2780 /*
2781 * Create an UST session and add it to the session ust list.
2782 *
2783 * Should *NOT* be called with RCU read-side lock held.
2784 */
2785 static int create_ust_session(struct ltt_session *session,
2786 struct lttng_domain *domain)
2787 {
2788 int ret;
2789 struct ltt_ust_session *lus = NULL;
2790
2791 assert(session);
2792 assert(domain);
2793 assert(session->consumer);
2794
2795 switch (domain->type) {
2796 case LTTNG_DOMAIN_JUL:
2797 case LTTNG_DOMAIN_LOG4J:
2798 case LTTNG_DOMAIN_PYTHON:
2799 case LTTNG_DOMAIN_UST:
2800 break;
2801 default:
2802 ERR("Unknown UST domain on create session %d", domain->type);
2803 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2804 goto error;
2805 }
2806
2807 DBG("Creating UST session");
2808
2809 lus = trace_ust_create_session(session->id);
2810 if (lus == NULL) {
2811 ret = LTTNG_ERR_UST_SESS_FAIL;
2812 goto error;
2813 }
2814
2815 lus->uid = session->uid;
2816 lus->gid = session->gid;
2817 lus->output_traces = session->output_traces;
2818 lus->snapshot_mode = session->snapshot_mode;
2819 lus->live_timer_interval = session->live_timer;
2820 session->ust_session = lus;
2821 if (session->shm_path[0]) {
2822 strncpy(lus->root_shm_path, session->shm_path,
2823 sizeof(lus->root_shm_path));
2824 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
2825 strncpy(lus->shm_path, session->shm_path,
2826 sizeof(lus->shm_path));
2827 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2828 strncat(lus->shm_path, "/ust",
2829 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2830 }
2831 /* Copy session output to the newly created UST session */
2832 ret = copy_session_consumer(domain->type, session);
2833 if (ret != LTTNG_OK) {
2834 goto error;
2835 }
2836
2837 return LTTNG_OK;
2838
2839 error:
2840 free(lus);
2841 session->ust_session = NULL;
2842 return ret;
2843 }
2844
2845 /*
2846 * Create a kernel tracer session then create the default channel.
2847 */
2848 static int create_kernel_session(struct ltt_session *session)
2849 {
2850 int ret;
2851
2852 DBG("Creating kernel session");
2853
2854 ret = kernel_create_session(session, kernel_tracer_fd);
2855 if (ret < 0) {
2856 ret = LTTNG_ERR_KERN_SESS_FAIL;
2857 goto error;
2858 }
2859
2860 /* Code flow safety */
2861 assert(session->kernel_session);
2862
2863 /* Copy session output to the newly created Kernel session */
2864 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2865 if (ret != LTTNG_OK) {
2866 goto error;
2867 }
2868
2869 /* Create directory(ies) on local filesystem. */
2870 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2871 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2872 ret = run_as_mkdir_recursive(
2873 session->kernel_session->consumer->dst.trace_path,
2874 S_IRWXU | S_IRWXG, session->uid, session->gid);
2875 if (ret < 0) {
2876 if (errno != EEXIST) {
2877 ERR("Trace directory creation error");
2878 goto error;
2879 }
2880 }
2881 }
2882
2883 session->kernel_session->uid = session->uid;
2884 session->kernel_session->gid = session->gid;
2885 session->kernel_session->output_traces = session->output_traces;
2886 session->kernel_session->snapshot_mode = session->snapshot_mode;
2887
2888 return LTTNG_OK;
2889
2890 error:
2891 trace_kernel_destroy_session(session->kernel_session);
2892 session->kernel_session = NULL;
2893 return ret;
2894 }
2895
2896 /*
2897 * Count number of session permitted by uid/gid.
2898 */
2899 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2900 {
2901 unsigned int i = 0;
2902 struct ltt_session *session;
2903
2904 DBG("Counting number of available session for UID %d GID %d",
2905 uid, gid);
2906 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2907 /*
2908 * Only list the sessions the user can control.
2909 */
2910 if (!session_access_ok(session, uid, gid)) {
2911 continue;
2912 }
2913 i++;
2914 }
2915 return i;
2916 }
2917
2918 /*
2919 * Process the command requested by the lttng client within the command
2920 * context structure. This function make sure that the return structure (llm)
2921 * is set and ready for transmission before returning.
2922 *
2923 * Return any error encountered or 0 for success.
2924 *
2925 * "sock" is only used for special-case var. len data.
2926 *
2927 * Should *NOT* be called with RCU read-side lock held.
2928 */
2929 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2930 int *sock_error)
2931 {
2932 int ret = LTTNG_OK;
2933 int need_tracing_session = 1;
2934 int need_domain;
2935
2936 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2937
2938 *sock_error = 0;
2939
2940 switch (cmd_ctx->lsm->cmd_type) {
2941 case LTTNG_CREATE_SESSION:
2942 case LTTNG_CREATE_SESSION_SNAPSHOT:
2943 case LTTNG_CREATE_SESSION_LIVE:
2944 case LTTNG_DESTROY_SESSION:
2945 case LTTNG_LIST_SESSIONS:
2946 case LTTNG_LIST_DOMAINS:
2947 case LTTNG_START_TRACE:
2948 case LTTNG_STOP_TRACE:
2949 case LTTNG_DATA_PENDING:
2950 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2951 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2952 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2953 case LTTNG_SNAPSHOT_RECORD:
2954 case LTTNG_SAVE_SESSION:
2955 case LTTNG_SET_SESSION_SHM_PATH:
2956 need_domain = 0;
2957 break;
2958 default:
2959 need_domain = 1;
2960 }
2961
2962 if (opt_no_kernel && need_domain
2963 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2964 if (!is_root) {
2965 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2966 } else {
2967 ret = LTTNG_ERR_KERN_NA;
2968 }
2969 goto error;
2970 }
2971
2972 /* Deny register consumer if we already have a spawned consumer. */
2973 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2974 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2975 if (kconsumer_data.pid > 0) {
2976 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2977 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2978 goto error;
2979 }
2980 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2981 }
2982
2983 /*
2984 * Check for command that don't needs to allocate a returned payload. We do
2985 * this here so we don't have to make the call for no payload at each
2986 * command.
2987 */
2988 switch(cmd_ctx->lsm->cmd_type) {
2989 case LTTNG_LIST_SESSIONS:
2990 case LTTNG_LIST_TRACEPOINTS:
2991 case LTTNG_LIST_TRACEPOINT_FIELDS:
2992 case LTTNG_LIST_DOMAINS:
2993 case LTTNG_LIST_CHANNELS:
2994 case LTTNG_LIST_EVENTS:
2995 case LTTNG_LIST_SYSCALLS:
2996 case LTTNG_LIST_TRACKER_PIDS:
2997 break;
2998 default:
2999 /* Setup lttng message with no payload */
3000 ret = setup_lttng_msg(cmd_ctx, 0);
3001 if (ret < 0) {
3002 /* This label does not try to unlock the session */
3003 goto init_setup_error;
3004 }
3005 }
3006
3007 /* Commands that DO NOT need a session. */
3008 switch (cmd_ctx->lsm->cmd_type) {
3009 case LTTNG_CREATE_SESSION:
3010 case LTTNG_CREATE_SESSION_SNAPSHOT:
3011 case LTTNG_CREATE_SESSION_LIVE:
3012 case LTTNG_CALIBRATE:
3013 case LTTNG_LIST_SESSIONS:
3014 case LTTNG_LIST_TRACEPOINTS:
3015 case LTTNG_LIST_SYSCALLS:
3016 case LTTNG_LIST_TRACEPOINT_FIELDS:
3017 case LTTNG_SAVE_SESSION:
3018 need_tracing_session = 0;
3019 break;
3020 default:
3021 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
3022 /*
3023 * We keep the session list lock across _all_ commands
3024 * for now, because the per-session lock does not
3025 * handle teardown properly.
3026 */
3027 session_lock_list();
3028 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
3029 if (cmd_ctx->session == NULL) {
3030 ret = LTTNG_ERR_SESS_NOT_FOUND;
3031 goto error;
3032 } else {
3033 /* Acquire lock for the session */
3034 session_lock(cmd_ctx->session);
3035 }
3036 break;
3037 }
3038
3039 /*
3040 * Commands that need a valid session but should NOT create one if none
3041 * exists. Instead of creating one and destroying it when the command is
3042 * handled, process that right before so we save some round trip in useless
3043 * code path.
3044 */
3045 switch (cmd_ctx->lsm->cmd_type) {
3046 case LTTNG_DISABLE_CHANNEL:
3047 case LTTNG_DISABLE_EVENT:
3048 switch (cmd_ctx->lsm->domain.type) {
3049 case LTTNG_DOMAIN_KERNEL:
3050 if (!cmd_ctx->session->kernel_session) {
3051 ret = LTTNG_ERR_NO_CHANNEL;
3052 goto error;
3053 }
3054 break;
3055 case LTTNG_DOMAIN_JUL:
3056 case LTTNG_DOMAIN_LOG4J:
3057 case LTTNG_DOMAIN_PYTHON:
3058 case LTTNG_DOMAIN_UST:
3059 if (!cmd_ctx->session->ust_session) {
3060 ret = LTTNG_ERR_NO_CHANNEL;
3061 goto error;
3062 }
3063 break;
3064 default:
3065 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3066 goto error;
3067 }
3068 default:
3069 break;
3070 }
3071
3072 if (!need_domain) {
3073 goto skip_domain;
3074 }
3075
3076 /*
3077 * Check domain type for specific "pre-action".
3078 */
3079 switch (cmd_ctx->lsm->domain.type) {
3080 case LTTNG_DOMAIN_KERNEL:
3081 if (!is_root) {
3082 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3083 goto error;
3084 }
3085
3086 /* Kernel tracer check */
3087 if (kernel_tracer_fd == -1) {
3088 /* Basically, load kernel tracer modules */
3089 ret = init_kernel_tracer();
3090 if (ret != 0) {
3091 goto error;
3092 }
3093 }
3094
3095 /* Consumer is in an ERROR state. Report back to client */
3096 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
3097 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3098 goto error;
3099 }
3100
3101 /* Need a session for kernel command */
3102 if (need_tracing_session) {
3103 if (cmd_ctx->session->kernel_session == NULL) {
3104 ret = create_kernel_session(cmd_ctx->session);
3105 if (ret < 0) {
3106 ret = LTTNG_ERR_KERN_SESS_FAIL;
3107 goto error;
3108 }
3109 }
3110
3111 /* Start the kernel consumer daemon */
3112 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3113 if (kconsumer_data.pid == 0 &&
3114 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3115 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3116 ret = start_consumerd(&kconsumer_data);
3117 if (ret < 0) {
3118 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3119 goto error;
3120 }
3121 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3122 } else {
3123 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3124 }
3125
3126 /*
3127 * The consumer was just spawned so we need to add the socket to
3128 * the consumer output of the session if exist.
3129 */
3130 ret = consumer_create_socket(&kconsumer_data,
3131 cmd_ctx->session->kernel_session->consumer);
3132 if (ret < 0) {
3133 goto error;
3134 }
3135 }
3136
3137 break;
3138 case LTTNG_DOMAIN_JUL:
3139 case LTTNG_DOMAIN_LOG4J:
3140 case LTTNG_DOMAIN_PYTHON:
3141 case LTTNG_DOMAIN_UST:
3142 {
3143 if (!ust_app_supported()) {
3144 ret = LTTNG_ERR_NO_UST;
3145 goto error;
3146 }
3147 /* Consumer is in an ERROR state. Report back to client */
3148 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
3149 ret = LTTNG_ERR_NO_USTCONSUMERD;
3150 goto error;
3151 }
3152
3153 if (need_tracing_session) {
3154 /* Create UST session if none exist. */
3155 if (cmd_ctx->session->ust_session == NULL) {
3156 ret = create_ust_session(cmd_ctx->session,
3157 &cmd_ctx->lsm->domain);
3158 if (ret != LTTNG_OK) {
3159 goto error;
3160 }
3161 }
3162
3163 /* Start the UST consumer daemons */
3164 /* 64-bit */
3165 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3166 if (consumerd64_bin[0] != '\0' &&
3167 ustconsumer64_data.pid == 0 &&
3168 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3169 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3170 ret = start_consumerd(&ustconsumer64_data);
3171 if (ret < 0) {
3172 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
3173 uatomic_set(&ust_consumerd64_fd, -EINVAL);
3174 goto error;
3175 }
3176
3177 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
3178 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3179 } else {
3180 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3181 }
3182
3183 /*
3184 * Setup socket for consumer 64 bit. No need for atomic access
3185 * since it was set above and can ONLY be set in this thread.
3186 */
3187 ret = consumer_create_socket(&ustconsumer64_data,
3188 cmd_ctx->session->ust_session->consumer);
3189 if (ret < 0) {
3190 goto error;
3191 }
3192
3193 /* 32-bit */
3194 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
3195 if (consumerd32_bin[0] != '\0' &&
3196 ustconsumer32_data.pid == 0 &&
3197 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3198 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3199 ret = start_consumerd(&ustconsumer32_data);
3200 if (ret < 0) {
3201 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
3202 uatomic_set(&ust_consumerd32_fd, -EINVAL);
3203 goto error;
3204 }
3205
3206 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
3207 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3208 } else {
3209 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3210 }
3211
3212 /*
3213 * Setup socket for consumer 64 bit. No need for atomic access
3214 * since it was set above and can ONLY be set in this thread.
3215 */
3216 ret = consumer_create_socket(&ustconsumer32_data,
3217 cmd_ctx->session->ust_session->consumer);
3218 if (ret < 0) {
3219 goto error;
3220 }
3221 }
3222 break;
3223 }
3224 default:
3225 break;
3226 }
3227 skip_domain:
3228
3229 /* Validate consumer daemon state when start/stop trace command */
3230 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3231 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3232 switch (cmd_ctx->lsm->domain.type) {
3233 case LTTNG_DOMAIN_JUL:
3234 case LTTNG_DOMAIN_LOG4J:
3235 case LTTNG_DOMAIN_PYTHON: