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