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