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