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