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