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