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