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