0625c11939b958f8b9d88ed3ce48a88a3074d703
[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 int lttng_sessiond_ready = 4;
336
337 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
338 {
339 return (fd == thread_quit_pipe[0] && (events & LPOLLIN)) ? 1 : 0;
340 }
341
342 /* Notify parents that we are ready for cmd and health check */
343 LTTNG_HIDDEN
344 void sessiond_signal_parents(void)
345 {
346 /*
347 * Notify parent pid that we are ready to accept command
348 * for client side. This ppid is the one from the
349 * external process that spawned us.
350 */
351 if (config.sig_parent) {
352 kill(ppid, SIGUSR1);
353 }
354
355 /*
356 * Notify the parent of the fork() process that we are
357 * ready.
358 */
359 if (config.daemonize || config.background) {
360 kill(child_ppid, SIGUSR1);
361 }
362 }
363
364 LTTNG_HIDDEN
365 void sessiond_notify_ready(void)
366 {
367 /*
368 * The _return variant is used since the implied memory barriers are
369 * required.
370 */
371 (void) uatomic_sub_return(&lttng_sessiond_ready, 1);
372 }
373
374 static
375 int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
376 int *a_pipe)
377 {
378 int ret;
379
380 assert(events);
381
382 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
383 if (ret < 0) {
384 goto error;
385 }
386
387 /* Add quit pipe */
388 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
389 if (ret < 0) {
390 goto error;
391 }
392
393 return 0;
394
395 error:
396 return ret;
397 }
398
399 /*
400 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
401 */
402 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
403 {
404 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
405 }
406
407 /*
408 * Init thread quit pipe.
409 *
410 * Return -1 on error or 0 if all pipes are created.
411 */
412 static int __init_thread_quit_pipe(int *a_pipe)
413 {
414 int ret, i;
415
416 ret = pipe(a_pipe);
417 if (ret < 0) {
418 PERROR("thread quit pipe");
419 goto error;
420 }
421
422 for (i = 0; i < 2; i++) {
423 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
424 if (ret < 0) {
425 PERROR("fcntl");
426 goto error;
427 }
428 }
429
430 error:
431 return ret;
432 }
433
434 static int init_thread_quit_pipe(void)
435 {
436 return __init_thread_quit_pipe(thread_quit_pipe);
437 }
438
439 /*
440 * Stop all threads by closing the thread quit pipe.
441 */
442 static void stop_threads(void)
443 {
444 int ret;
445
446 /* Stopping all threads */
447 DBG("Terminating all threads");
448 ret = notify_thread_pipe(thread_quit_pipe[1]);
449 if (ret < 0) {
450 ERR("write error on thread quit pipe");
451 }
452
453 /* Dispatch thread */
454 CMM_STORE_SHARED(dispatch_thread_exit, 1);
455 futex_nto1_wake(&ust_cmd_queue.futex);
456 }
457
458 /*
459 * Close every consumer sockets.
460 */
461 static void close_consumer_sockets(void)
462 {
463 int ret;
464
465 if (kconsumer_data.err_sock >= 0) {
466 ret = close(kconsumer_data.err_sock);
467 if (ret < 0) {
468 PERROR("kernel consumer err_sock close");
469 }
470 }
471 if (ustconsumer32_data.err_sock >= 0) {
472 ret = close(ustconsumer32_data.err_sock);
473 if (ret < 0) {
474 PERROR("UST consumerd32 err_sock close");
475 }
476 }
477 if (ustconsumer64_data.err_sock >= 0) {
478 ret = close(ustconsumer64_data.err_sock);
479 if (ret < 0) {
480 PERROR("UST consumerd64 err_sock close");
481 }
482 }
483 if (kconsumer_data.cmd_sock >= 0) {
484 ret = close(kconsumer_data.cmd_sock);
485 if (ret < 0) {
486 PERROR("kernel consumer cmd_sock close");
487 }
488 }
489 if (ustconsumer32_data.cmd_sock >= 0) {
490 ret = close(ustconsumer32_data.cmd_sock);
491 if (ret < 0) {
492 PERROR("UST consumerd32 cmd_sock close");
493 }
494 }
495 if (ustconsumer64_data.cmd_sock >= 0) {
496 ret = close(ustconsumer64_data.cmd_sock);
497 if (ret < 0) {
498 PERROR("UST consumerd64 cmd_sock close");
499 }
500 }
501 if (kconsumer_data.channel_monitor_pipe >= 0) {
502 ret = close(kconsumer_data.channel_monitor_pipe);
503 if (ret < 0) {
504 PERROR("kernel consumer channel monitor pipe close");
505 }
506 }
507 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
508 ret = close(ustconsumer32_data.channel_monitor_pipe);
509 if (ret < 0) {
510 PERROR("UST consumerd32 channel monitor pipe close");
511 }
512 }
513 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
514 ret = close(ustconsumer64_data.channel_monitor_pipe);
515 if (ret < 0) {
516 PERROR("UST consumerd64 channel monitor pipe close");
517 }
518 }
519 if (kconsumer_data.channel_rotate_pipe >= 0) {
520 ret = close(kconsumer_data.channel_rotate_pipe);
521 if (ret < 0) {
522 PERROR("kernel consumer channel rotate pipe close");
523 }
524 }
525 if (ustconsumer32_data.channel_rotate_pipe >= 0) {
526 ret = close(ustconsumer32_data.channel_rotate_pipe);
527 if (ret < 0) {
528 PERROR("UST consumerd32 channel rotate pipe close");
529 }
530 }
531 if (ustconsumer64_data.channel_rotate_pipe >= 0) {
532 ret = close(ustconsumer64_data.channel_rotate_pipe);
533 if (ret < 0) {
534 PERROR("UST consumerd64 channel rotate pipe close");
535 }
536 }
537 }
538
539 /*
540 * Wait on consumer process termination.
541 *
542 * Need to be called with the consumer data lock held or from a context
543 * ensuring no concurrent access to data (e.g: cleanup).
544 */
545 static void wait_consumer(struct consumer_data *consumer_data)
546 {
547 pid_t ret;
548 int status;
549
550 if (consumer_data->pid <= 0) {
551 return;
552 }
553
554 DBG("Waiting for complete teardown of consumerd (PID: %d)",
555 consumer_data->pid);
556 ret = waitpid(consumer_data->pid, &status, 0);
557 if (ret == -1) {
558 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
559 } else if (!WIFEXITED(status)) {
560 ERR("consumerd termination with error: %d",
561 WEXITSTATUS(ret));
562 }
563 consumer_data->pid = 0;
564 }
565
566 /*
567 * Cleanup the session daemon's data structures.
568 */
569 static void sessiond_cleanup(void)
570 {
571 int ret;
572 struct ltt_session *sess, *stmp;
573
574 DBG("Cleanup sessiond");
575
576 /*
577 * Close the thread quit pipe. It has already done its job,
578 * since we are now called.
579 */
580 utils_close_pipe(thread_quit_pipe);
581
582 /*
583 * If config.pid_file_path.value is undefined, the default file will be
584 * wiped when removing the rundir.
585 */
586 if (config.pid_file_path.value) {
587 ret = remove(config.pid_file_path.value);
588 if (ret < 0) {
589 PERROR("remove pidfile %s", config.pid_file_path.value);
590 }
591 }
592
593 DBG("Removing sessiond and consumerd content of directory %s",
594 config.rundir.value);
595
596 /* sessiond */
597 DBG("Removing %s", config.pid_file_path.value);
598 (void) unlink(config.pid_file_path.value);
599
600 DBG("Removing %s", config.agent_port_file_path.value);
601 (void) unlink(config.agent_port_file_path.value);
602
603 /* kconsumerd */
604 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
605 (void) unlink(kconsumer_data.err_unix_sock_path);
606
607 DBG("Removing directory %s", config.kconsumerd_path.value);
608 (void) rmdir(config.kconsumerd_path.value);
609
610 /* ust consumerd 32 */
611 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
612 (void) unlink(config.consumerd32_err_unix_sock_path.value);
613
614 DBG("Removing directory %s", config.consumerd32_path.value);
615 (void) rmdir(config.consumerd32_path.value);
616
617 /* ust consumerd 64 */
618 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
619 (void) unlink(config.consumerd64_err_unix_sock_path.value);
620
621 DBG("Removing directory %s", config.consumerd64_path.value);
622 (void) rmdir(config.consumerd64_path.value);
623
624 DBG("Cleaning up all sessions");
625
626 /* Destroy session list mutex */
627 if (session_list_ptr != NULL) {
628 pthread_mutex_destroy(&session_list_ptr->lock);
629
630 /* Cleanup ALL session */
631 cds_list_for_each_entry_safe(sess, stmp,
632 &session_list_ptr->head, list) {
633 cmd_destroy_session(sess, kernel_poll_pipe[1],
634 notification_thread_handle);
635 }
636 }
637
638 wait_consumer(&kconsumer_data);
639 wait_consumer(&ustconsumer64_data);
640 wait_consumer(&ustconsumer32_data);
641
642 DBG("Cleaning up all agent apps");
643 agent_app_ht_clean();
644
645 DBG("Closing all UST sockets");
646 ust_app_clean_list();
647 buffer_reg_destroy_registries();
648
649 if (is_root && !config.no_kernel) {
650 DBG2("Closing kernel fd");
651 if (kernel_tracer_fd >= 0) {
652 ret = close(kernel_tracer_fd);
653 if (ret) {
654 PERROR("close");
655 }
656 }
657 DBG("Unloading kernel modules");
658 modprobe_remove_lttng_all();
659 free(syscall_table);
660 }
661
662 close_consumer_sockets();
663
664 if (load_info) {
665 load_session_destroy_data(load_info);
666 free(load_info);
667 }
668
669 /*
670 * We do NOT rmdir rundir because there are other processes
671 * using it, for instance lttng-relayd, which can start in
672 * parallel with this teardown.
673 */
674 }
675
676 /*
677 * Cleanup the daemon's option data structures.
678 */
679 static void sessiond_cleanup_options(void)
680 {
681 DBG("Cleaning up options");
682
683 sessiond_config_fini(&config);
684
685 run_as_destroy_worker();
686 }
687
688 /*
689 * Send data on a unix socket using the liblttsessiondcomm API.
690 *
691 * Return lttcomm error code.
692 */
693 static int send_unix_sock(int sock, void *buf, size_t len)
694 {
695 /* Check valid length */
696 if (len == 0) {
697 return -1;
698 }
699
700 return lttcomm_send_unix_sock(sock, buf, len);
701 }
702
703 /*
704 * Free memory of a command context structure.
705 */
706 static void clean_command_ctx(struct command_ctx **cmd_ctx)
707 {
708 DBG("Clean command context structure");
709 if (*cmd_ctx) {
710 if ((*cmd_ctx)->llm) {
711 free((*cmd_ctx)->llm);
712 }
713 if ((*cmd_ctx)->lsm) {
714 free((*cmd_ctx)->lsm);
715 }
716 free(*cmd_ctx);
717 *cmd_ctx = NULL;
718 }
719 }
720
721 /*
722 * Notify UST applications using the shm mmap futex.
723 */
724 static int notify_ust_apps(int active)
725 {
726 char *wait_shm_mmap;
727
728 DBG("Notifying applications of session daemon state: %d", active);
729
730 /* See shm.c for this call implying mmap, shm and futex calls */
731 wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root);
732 if (wait_shm_mmap == NULL) {
733 goto error;
734 }
735
736 /* Wake waiting process */
737 futex_wait_update((int32_t *) wait_shm_mmap, active);
738
739 /* Apps notified successfully */
740 return 0;
741
742 error:
743 return -1;
744 }
745
746 /*
747 * Setup the outgoing data buffer for the response (llm) by allocating the
748 * right amount of memory and copying the original information from the lsm
749 * structure.
750 *
751 * Return 0 on success, negative value on error.
752 */
753 static int setup_lttng_msg(struct command_ctx *cmd_ctx,
754 const void *payload_buf, size_t payload_len,
755 const void *cmd_header_buf, size_t cmd_header_len)
756 {
757 int ret = 0;
758 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
759 const size_t cmd_header_offset = header_len;
760 const size_t payload_offset = cmd_header_offset + cmd_header_len;
761 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
762
763 cmd_ctx->llm = zmalloc(total_msg_size);
764
765 if (cmd_ctx->llm == NULL) {
766 PERROR("zmalloc");
767 ret = -ENOMEM;
768 goto end;
769 }
770
771 /* Copy common data */
772 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
773 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
774 cmd_ctx->llm->cmd_header_size = cmd_header_len;
775 cmd_ctx->llm->data_size = payload_len;
776 cmd_ctx->lttng_msg_size = total_msg_size;
777
778 /* Copy command header */
779 if (cmd_header_len) {
780 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
781 cmd_header_len);
782 }
783
784 /* Copy payload */
785 if (payload_len) {
786 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
787 payload_len);
788 }
789
790 end:
791 return ret;
792 }
793
794 /*
795 * Version of setup_lttng_msg() without command header.
796 */
797 static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
798 void *payload_buf, size_t payload_len)
799 {
800 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
801 }
802 /*
803 * Update the kernel poll set of all channel fd available over all tracing
804 * session. Add the wakeup pipe at the end of the set.
805 */
806 static int update_kernel_poll(struct lttng_poll_event *events)
807 {
808 int ret;
809 struct ltt_session *session;
810 struct ltt_kernel_channel *channel;
811
812 DBG("Updating kernel poll set");
813
814 session_lock_list();
815 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
816 session_lock(session);
817 if (session->kernel_session == NULL) {
818 session_unlock(session);
819 continue;
820 }
821
822 cds_list_for_each_entry(channel,
823 &session->kernel_session->channel_list.head, list) {
824 /* Add channel fd to the kernel poll set */
825 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
826 if (ret < 0) {
827 session_unlock(session);
828 goto error;
829 }
830 DBG("Channel fd %d added to kernel set", channel->fd);
831 }
832 session_unlock(session);
833 }
834 session_unlock_list();
835
836 return 0;
837
838 error:
839 session_unlock_list();
840 return -1;
841 }
842
843 /*
844 * Find the channel fd from 'fd' over all tracing session. When found, check
845 * for new channel stream and send those stream fds to the kernel consumer.
846 *
847 * Useful for CPU hotplug feature.
848 */
849 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
850 {
851 int ret = 0;
852 struct ltt_session *session;
853 struct ltt_kernel_session *ksess;
854 struct ltt_kernel_channel *channel;
855
856 DBG("Updating kernel streams for channel fd %d", fd);
857
858 session_lock_list();
859 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
860 session_lock(session);
861 if (session->kernel_session == NULL) {
862 session_unlock(session);
863 continue;
864 }
865 ksess = session->kernel_session;
866
867 cds_list_for_each_entry(channel,
868 &ksess->channel_list.head, list) {
869 struct lttng_ht_iter iter;
870 struct consumer_socket *socket;
871
872 if (channel->fd != fd) {
873 continue;
874 }
875 DBG("Channel found, updating kernel streams");
876 ret = kernel_open_channel_stream(channel);
877 if (ret < 0) {
878 goto error;
879 }
880 /* Update the stream global counter */
881 ksess->stream_count_global += ret;
882
883 /*
884 * Have we already sent fds to the consumer? If yes, it
885 * means that tracing is started so it is safe to send
886 * our updated stream fds.
887 */
888 if (ksess->consumer_fds_sent != 1
889 || ksess->consumer == NULL) {
890 ret = -1;
891 goto error;
892 }
893
894 rcu_read_lock();
895 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
896 &iter.iter, socket, node.node) {
897 pthread_mutex_lock(socket->lock);
898 ret = kernel_consumer_send_channel_streams(socket,
899 channel, ksess,
900 session->output_traces ? 1 : 0);
901 pthread_mutex_unlock(socket->lock);
902 if (ret < 0) {
903 rcu_read_unlock();
904 goto error;
905 }
906 }
907 rcu_read_unlock();
908 }
909 session_unlock(session);
910 }
911 session_unlock_list();
912 return ret;
913
914 error:
915 session_unlock(session);
916 session_unlock_list();
917 return ret;
918 }
919
920 /*
921 * For each tracing session, update newly registered apps. The session list
922 * lock MUST be acquired before calling this.
923 */
924 static void update_ust_app(int app_sock)
925 {
926 struct ltt_session *sess, *stmp;
927
928 /* Consumer is in an ERROR state. Stop any application update. */
929 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
930 /* Stop the update process since the consumer is dead. */
931 return;
932 }
933
934 /* For all tracing session(s) */
935 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
936 struct ust_app *app;
937
938 session_lock(sess);
939 if (!sess->ust_session) {
940 goto unlock_session;
941 }
942
943 rcu_read_lock();
944 assert(app_sock >= 0);
945 app = ust_app_find_by_sock(app_sock);
946 if (app == NULL) {
947 /*
948 * Application can be unregistered before so
949 * this is possible hence simply stopping the
950 * update.
951 */
952 DBG3("UST app update failed to find app sock %d",
953 app_sock);
954 goto unlock_rcu;
955 }
956 ust_app_global_update(sess->ust_session, app);
957 unlock_rcu:
958 rcu_read_unlock();
959 unlock_session:
960 session_unlock(sess);
961 }
962 }
963
964 /*
965 * This thread manage event coming from the kernel.
966 *
967 * Features supported in this thread:
968 * -) CPU Hotplug
969 */
970 static void *thread_manage_kernel(void *data)
971 {
972 int ret, i, pollfd, update_poll_flag = 1, err = -1;
973 uint32_t revents, nb_fd;
974 char tmp;
975 struct lttng_poll_event events;
976
977 DBG("[thread] Thread manage kernel started");
978
979 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
980
981 /*
982 * This first step of the while is to clean this structure which could free
983 * non NULL pointers so initialize it before the loop.
984 */
985 lttng_poll_init(&events);
986
987 if (testpoint(sessiond_thread_manage_kernel)) {
988 goto error_testpoint;
989 }
990
991 health_code_update();
992
993 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
994 goto error_testpoint;
995 }
996
997 while (1) {
998 health_code_update();
999
1000 if (update_poll_flag == 1) {
1001 /* Clean events object. We are about to populate it again. */
1002 lttng_poll_clean(&events);
1003
1004 ret = sessiond_set_thread_pollset(&events, 2);
1005 if (ret < 0) {
1006 goto error_poll_create;
1007 }
1008
1009 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
1010 if (ret < 0) {
1011 goto error;
1012 }
1013
1014 /* This will add the available kernel channel if any. */
1015 ret = update_kernel_poll(&events);
1016 if (ret < 0) {
1017 goto error;
1018 }
1019 update_poll_flag = 0;
1020 }
1021
1022 DBG("Thread kernel polling");
1023
1024 /* Poll infinite value of time */
1025 restart:
1026 health_poll_entry();
1027 ret = lttng_poll_wait(&events, -1);
1028 DBG("Thread kernel return from poll on %d fds",
1029 LTTNG_POLL_GETNB(&events));
1030 health_poll_exit();
1031 if (ret < 0) {
1032 /*
1033 * Restart interrupted system call.
1034 */
1035 if (errno == EINTR) {
1036 goto restart;
1037 }
1038 goto error;
1039 } else if (ret == 0) {
1040 /* Should not happen since timeout is infinite */
1041 ERR("Return value of poll is 0 with an infinite timeout.\n"
1042 "This should not have happened! Continuing...");
1043 continue;
1044 }
1045
1046 nb_fd = ret;
1047
1048 for (i = 0; i < nb_fd; i++) {
1049 /* Fetch once the poll data */
1050 revents = LTTNG_POLL_GETEV(&events, i);
1051 pollfd = LTTNG_POLL_GETFD(&events, i);
1052
1053 health_code_update();
1054
1055 if (!revents) {
1056 /* No activity for this FD (poll implementation). */
1057 continue;
1058 }
1059
1060 /* Thread quit pipe has been closed. Killing thread. */
1061 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1062 if (ret) {
1063 err = 0;
1064 goto exit;
1065 }
1066
1067 /* Check for data on kernel pipe */
1068 if (revents & LPOLLIN) {
1069 if (pollfd == kernel_poll_pipe[0]) {
1070 (void) lttng_read(kernel_poll_pipe[0],
1071 &tmp, 1);
1072 /*
1073 * Ret value is useless here, if this pipe gets any actions an
1074 * update is required anyway.
1075 */
1076 update_poll_flag = 1;
1077 continue;
1078 } else {
1079 /*
1080 * New CPU detected by the kernel. Adding kernel stream to
1081 * kernel session and updating the kernel consumer
1082 */
1083 ret = update_kernel_stream(&kconsumer_data, pollfd);
1084 if (ret < 0) {
1085 continue;
1086 }
1087 break;
1088 }
1089 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1090 update_poll_flag = 1;
1091 continue;
1092 } else {
1093 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1094 goto error;
1095 }
1096 }
1097 }
1098
1099 exit:
1100 error:
1101 lttng_poll_clean(&events);
1102 error_poll_create:
1103 error_testpoint:
1104 utils_close_pipe(kernel_poll_pipe);
1105 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
1106 if (err) {
1107 health_error();
1108 ERR("Health error occurred in %s", __func__);
1109 WARN("Kernel thread died unexpectedly. "
1110 "Kernel tracing can continue but CPU hotplug is disabled.");
1111 }
1112 health_unregister(health_sessiond);
1113 DBG("Kernel thread dying");
1114 return NULL;
1115 }
1116
1117 /*
1118 * Signal pthread condition of the consumer data that the thread.
1119 */
1120 static void signal_consumer_condition(struct consumer_data *data, int state)
1121 {
1122 pthread_mutex_lock(&data->cond_mutex);
1123
1124 /*
1125 * The state is set before signaling. It can be any value, it's the waiter
1126 * job to correctly interpret this condition variable associated to the
1127 * consumer pthread_cond.
1128 *
1129 * A value of 0 means that the corresponding thread of the consumer data
1130 * was not started. 1 indicates that the thread has started and is ready
1131 * for action. A negative value means that there was an error during the
1132 * thread bootstrap.
1133 */
1134 data->consumer_thread_is_ready = state;
1135 (void) pthread_cond_signal(&data->cond);
1136
1137 pthread_mutex_unlock(&data->cond_mutex);
1138 }
1139
1140 /*
1141 * This thread manage the consumer error sent back to the session daemon.
1142 */
1143 static void *thread_manage_consumer(void *data)
1144 {
1145 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
1146 uint32_t revents, nb_fd;
1147 enum lttcomm_return_code code;
1148 struct lttng_poll_event events;
1149 struct consumer_data *consumer_data = data;
1150 struct consumer_socket *cmd_socket_wrapper = NULL;
1151
1152 DBG("[thread] Manage consumer started");
1153
1154 rcu_register_thread();
1155 rcu_thread_online();
1156
1157 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
1158
1159 health_code_update();
1160
1161 /*
1162 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1163 * metadata_sock. Nothing more will be added to this poll set.
1164 */
1165 ret = sessiond_set_thread_pollset(&events, 3);
1166 if (ret < 0) {
1167 goto error_poll;
1168 }
1169
1170 /*
1171 * The error socket here is already in a listening state which was done
1172 * just before spawning this thread to avoid a race between the consumer
1173 * daemon exec trying to connect and the listen() call.
1174 */
1175 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
1176 if (ret < 0) {
1177 goto error;
1178 }
1179
1180 health_code_update();
1181
1182 /* Infinite blocking call, waiting for transmission */
1183 restart:
1184 health_poll_entry();
1185
1186 if (testpoint(sessiond_thread_manage_consumer)) {
1187 goto error;
1188 }
1189
1190 ret = lttng_poll_wait(&events, -1);
1191 health_poll_exit();
1192 if (ret < 0) {
1193 /*
1194 * Restart interrupted system call.
1195 */
1196 if (errno == EINTR) {
1197 goto restart;
1198 }
1199 goto error;
1200 }
1201
1202 nb_fd = ret;
1203
1204 for (i = 0; i < nb_fd; i++) {
1205 /* Fetch once the poll data */
1206 revents = LTTNG_POLL_GETEV(&events, i);
1207 pollfd = LTTNG_POLL_GETFD(&events, i);
1208
1209 health_code_update();
1210
1211 if (!revents) {
1212 /* No activity for this FD (poll implementation). */
1213 continue;
1214 }
1215
1216 /* Thread quit pipe has been closed. Killing thread. */
1217 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1218 if (ret) {
1219 err = 0;
1220 goto exit;
1221 }
1222
1223 /* Event on the registration socket */
1224 if (pollfd == consumer_data->err_sock) {
1225 if (revents & LPOLLIN) {
1226 continue;
1227 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1228 ERR("consumer err socket poll error");
1229 goto error;
1230 } else {
1231 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1232 goto error;
1233 }
1234 }
1235 }
1236
1237 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1238 if (sock < 0) {
1239 goto error;
1240 }
1241
1242 /*
1243 * Set the CLOEXEC flag. Return code is useless because either way, the
1244 * show must go on.
1245 */
1246 (void) utils_set_fd_cloexec(sock);
1247
1248 health_code_update();
1249
1250 DBG2("Receiving code from consumer err_sock");
1251
1252 /* Getting status code from kconsumerd */
1253 ret = lttcomm_recv_unix_sock(sock, &code,
1254 sizeof(enum lttcomm_return_code));
1255 if (ret <= 0) {
1256 goto error;
1257 }
1258
1259 health_code_update();
1260 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1261 ERR("consumer error when waiting for SOCK_READY : %s",
1262 lttcomm_get_readable_code(-code));
1263 goto error;
1264 }
1265
1266 /* Connect both command and metadata sockets. */
1267 consumer_data->cmd_sock =
1268 lttcomm_connect_unix_sock(
1269 consumer_data->cmd_unix_sock_path);
1270 consumer_data->metadata_fd =
1271 lttcomm_connect_unix_sock(
1272 consumer_data->cmd_unix_sock_path);
1273 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
1274 PERROR("consumer connect cmd socket");
1275 /* On error, signal condition and quit. */
1276 signal_consumer_condition(consumer_data, -1);
1277 goto error;
1278 }
1279
1280 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1281
1282 /* Create metadata socket lock. */
1283 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1284 if (consumer_data->metadata_sock.lock == NULL) {
1285 PERROR("zmalloc pthread mutex");
1286 goto error;
1287 }
1288 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1289
1290 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1291 DBG("Consumer metadata socket ready (fd: %d)",
1292 consumer_data->metadata_fd);
1293
1294 /*
1295 * Remove the consumerd error sock since we've established a connection.
1296 */
1297 ret = lttng_poll_del(&events, consumer_data->err_sock);
1298 if (ret < 0) {
1299 goto error;
1300 }
1301
1302 /* Add new accepted error socket. */
1303 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1304 if (ret < 0) {
1305 goto error;
1306 }
1307
1308 /* Add metadata socket that is successfully connected. */
1309 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1310 LPOLLIN | LPOLLRDHUP);
1311 if (ret < 0) {
1312 goto error;
1313 }
1314
1315 health_code_update();
1316
1317 /*
1318 * Transfer the write-end of the channel monitoring and rotate pipe
1319 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1320 * SET_CHANNEL_ROTATE_PIPE commands.
1321 */
1322 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
1323 if (!cmd_socket_wrapper) {
1324 goto error;
1325 }
1326 cmd_socket_wrapper->lock = &consumer_data->lock;
1327
1328 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
1329 consumer_data->channel_monitor_pipe);
1330 if (ret) {
1331 goto error;
1332 }
1333
1334 ret = consumer_send_channel_rotate_pipe(cmd_socket_wrapper,
1335 consumer_data->channel_rotate_pipe);
1336 if (ret) {
1337 goto error;
1338 }
1339
1340 /* Discard the socket wrapper as it is no longer needed. */
1341 consumer_destroy_socket(cmd_socket_wrapper);
1342 cmd_socket_wrapper = NULL;
1343
1344 /* The thread is completely initialized, signal that it is ready. */
1345 signal_consumer_condition(consumer_data, 1);
1346
1347 /* Infinite blocking call, waiting for transmission */
1348 restart_poll:
1349 while (1) {
1350 health_code_update();
1351
1352 /* Exit the thread because the thread quit pipe has been triggered. */
1353 if (should_quit) {
1354 /* Not a health error. */
1355 err = 0;
1356 goto exit;
1357 }
1358
1359 health_poll_entry();
1360 ret = lttng_poll_wait(&events, -1);
1361 health_poll_exit();
1362 if (ret < 0) {
1363 /*
1364 * Restart interrupted system call.
1365 */
1366 if (errno == EINTR) {
1367 goto restart_poll;
1368 }
1369 goto error;
1370 }
1371
1372 nb_fd = ret;
1373
1374 for (i = 0; i < nb_fd; i++) {
1375 /* Fetch once the poll data */
1376 revents = LTTNG_POLL_GETEV(&events, i);
1377 pollfd = LTTNG_POLL_GETFD(&events, i);
1378
1379 health_code_update();
1380
1381 if (!revents) {
1382 /* No activity for this FD (poll implementation). */
1383 continue;
1384 }
1385
1386 /*
1387 * Thread quit pipe has been triggered, flag that we should stop
1388 * but continue the current loop to handle potential data from
1389 * consumer.
1390 */
1391 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
1392
1393 if (pollfd == sock) {
1394 /* Event on the consumerd socket */
1395 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1396 && !(revents & LPOLLIN)) {
1397 ERR("consumer err socket second poll error");
1398 goto error;
1399 }
1400 health_code_update();
1401 /* Wait for any kconsumerd error */
1402 ret = lttcomm_recv_unix_sock(sock, &code,
1403 sizeof(enum lttcomm_return_code));
1404 if (ret <= 0) {
1405 ERR("consumer closed the command socket");
1406 goto error;
1407 }
1408
1409 ERR("consumer return code : %s",
1410 lttcomm_get_readable_code(-code));
1411
1412 goto exit;
1413 } else if (pollfd == consumer_data->metadata_fd) {
1414 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1415 && !(revents & LPOLLIN)) {
1416 ERR("consumer err metadata socket second poll error");
1417 goto error;
1418 }
1419 /* UST metadata requests */
1420 ret = ust_consumer_metadata_request(
1421 &consumer_data->metadata_sock);
1422 if (ret < 0) {
1423 ERR("Handling metadata request");
1424 goto error;
1425 }
1426 }
1427 /* No need for an else branch all FDs are tested prior. */
1428 }
1429 health_code_update();
1430 }
1431
1432 exit:
1433 error:
1434 /*
1435 * We lock here because we are about to close the sockets and some other
1436 * thread might be using them so get exclusive access which will abort all
1437 * other consumer command by other threads.
1438 */
1439 pthread_mutex_lock(&consumer_data->lock);
1440
1441 /* Immediately set the consumerd state to stopped */
1442 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1443 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1444 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1445 consumer_data->type == LTTNG_CONSUMER32_UST) {
1446 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1447 } else {
1448 /* Code flow error... */
1449 assert(0);
1450 }
1451
1452 if (consumer_data->err_sock >= 0) {
1453 ret = close(consumer_data->err_sock);
1454 if (ret) {
1455 PERROR("close");
1456 }
1457 consumer_data->err_sock = -1;
1458 }
1459 if (consumer_data->cmd_sock >= 0) {
1460 ret = close(consumer_data->cmd_sock);
1461 if (ret) {
1462 PERROR("close");
1463 }
1464 consumer_data->cmd_sock = -1;
1465 }
1466 if (consumer_data->metadata_sock.fd_ptr &&
1467 *consumer_data->metadata_sock.fd_ptr >= 0) {
1468 ret = close(*consumer_data->metadata_sock.fd_ptr);
1469 if (ret) {
1470 PERROR("close");
1471 }
1472 }
1473 if (sock >= 0) {
1474 ret = close(sock);
1475 if (ret) {
1476 PERROR("close");
1477 }
1478 }
1479
1480 unlink(consumer_data->err_unix_sock_path);
1481 unlink(consumer_data->cmd_unix_sock_path);
1482 pthread_mutex_unlock(&consumer_data->lock);
1483
1484 /* Cleanup metadata socket mutex. */
1485 if (consumer_data->metadata_sock.lock) {
1486 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1487 free(consumer_data->metadata_sock.lock);
1488 }
1489 lttng_poll_clean(&events);
1490
1491 if (cmd_socket_wrapper) {
1492 consumer_destroy_socket(cmd_socket_wrapper);
1493 }
1494 error_poll:
1495 if (err) {
1496 health_error();
1497 ERR("Health error occurred in %s", __func__);
1498 }
1499 health_unregister(health_sessiond);
1500 DBG("consumer thread cleanup completed");
1501
1502 rcu_thread_offline();
1503 rcu_unregister_thread();
1504
1505 return NULL;
1506 }
1507
1508 /*
1509 * This thread receives application command sockets (FDs) on the
1510 * apps_cmd_pipe and waits (polls) on them until they are closed
1511 * or an error occurs.
1512 *
1513 * At that point, it flushes the data (tracing and metadata) associated
1514 * with this application and tears down ust app sessions and other
1515 * associated data structures through ust_app_unregister().
1516 *
1517 * Note that this thread never sends commands to the applications
1518 * through the command sockets; it merely listens for hang-ups
1519 * and errors on those sockets and cleans-up as they occur.
1520 */
1521 static void *thread_manage_apps(void *data)
1522 {
1523 int i, ret, pollfd, err = -1;
1524 ssize_t size_ret;
1525 uint32_t revents, nb_fd;
1526 struct lttng_poll_event events;
1527
1528 DBG("[thread] Manage application started");
1529
1530 rcu_register_thread();
1531 rcu_thread_online();
1532
1533 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1534
1535 if (testpoint(sessiond_thread_manage_apps)) {
1536 goto error_testpoint;
1537 }
1538
1539 health_code_update();
1540
1541 ret = sessiond_set_thread_pollset(&events, 2);
1542 if (ret < 0) {
1543 goto error_poll_create;
1544 }
1545
1546 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1547 if (ret < 0) {
1548 goto error;
1549 }
1550
1551 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1552 goto error;
1553 }
1554
1555 health_code_update();
1556
1557 while (1) {
1558 DBG("Apps thread polling");
1559
1560 /* Inifinite blocking call, waiting for transmission */
1561 restart:
1562 health_poll_entry();
1563 ret = lttng_poll_wait(&events, -1);
1564 DBG("Apps thread return from poll on %d fds",
1565 LTTNG_POLL_GETNB(&events));
1566 health_poll_exit();
1567 if (ret < 0) {
1568 /*
1569 * Restart interrupted system call.
1570 */
1571 if (errno == EINTR) {
1572 goto restart;
1573 }
1574 goto error;
1575 }
1576
1577 nb_fd = ret;
1578
1579 for (i = 0; i < nb_fd; i++) {
1580 /* Fetch once the poll data */
1581 revents = LTTNG_POLL_GETEV(&events, i);
1582 pollfd = LTTNG_POLL_GETFD(&events, i);
1583
1584 health_code_update();
1585
1586 if (!revents) {
1587 /* No activity for this FD (poll implementation). */
1588 continue;
1589 }
1590
1591 /* Thread quit pipe has been closed. Killing thread. */
1592 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1593 if (ret) {
1594 err = 0;
1595 goto exit;
1596 }
1597
1598 /* Inspect the apps cmd pipe */
1599 if (pollfd == apps_cmd_pipe[0]) {
1600 if (revents & LPOLLIN) {
1601 int sock;
1602
1603 /* Empty pipe */
1604 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1605 if (size_ret < sizeof(sock)) {
1606 PERROR("read apps cmd pipe");
1607 goto error;
1608 }
1609
1610 health_code_update();
1611
1612 /*
1613 * Since this is a command socket (write then read),
1614 * we only monitor the error events of the socket.
1615 */
1616 ret = lttng_poll_add(&events, sock,
1617 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1618 if (ret < 0) {
1619 goto error;
1620 }
1621
1622 DBG("Apps with sock %d added to poll set", sock);
1623 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1624 ERR("Apps command pipe error");
1625 goto error;
1626 } else {
1627 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1628 goto error;
1629 }
1630 } else {
1631 /*
1632 * At this point, we know that a registered application made
1633 * the event at poll_wait.
1634 */
1635 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1636 /* Removing from the poll set */
1637 ret = lttng_poll_del(&events, pollfd);
1638 if (ret < 0) {
1639 goto error;
1640 }
1641
1642 /* Socket closed on remote end. */
1643 ust_app_unregister(pollfd);
1644 } else {
1645 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1646 goto error;
1647 }
1648 }
1649
1650 health_code_update();
1651 }
1652 }
1653
1654 exit:
1655 error:
1656 lttng_poll_clean(&events);
1657 error_poll_create:
1658 error_testpoint:
1659 utils_close_pipe(apps_cmd_pipe);
1660 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1661
1662 /*
1663 * We don't clean the UST app hash table here since already registered
1664 * applications can still be controlled so let them be until the session
1665 * daemon dies or the applications stop.
1666 */
1667
1668 if (err) {
1669 health_error();
1670 ERR("Health error occurred in %s", __func__);
1671 }
1672 health_unregister(health_sessiond);
1673 DBG("Application communication apps thread cleanup complete");
1674 rcu_thread_offline();
1675 rcu_unregister_thread();
1676 return NULL;
1677 }
1678
1679 /*
1680 * Send a socket to a thread This is called from the dispatch UST registration
1681 * thread once all sockets are set for the application.
1682 *
1683 * The sock value can be invalid, we don't really care, the thread will handle
1684 * it and make the necessary cleanup if so.
1685 *
1686 * On success, return 0 else a negative value being the errno message of the
1687 * write().
1688 */
1689 static int send_socket_to_thread(int fd, int sock)
1690 {
1691 ssize_t ret;
1692
1693 /*
1694 * It's possible that the FD is set as invalid with -1 concurrently just
1695 * before calling this function being a shutdown state of the thread.
1696 */
1697 if (fd < 0) {
1698 ret = -EBADF;
1699 goto error;
1700 }
1701
1702 ret = lttng_write(fd, &sock, sizeof(sock));
1703 if (ret < sizeof(sock)) {
1704 PERROR("write apps pipe %d", fd);
1705 if (ret < 0) {
1706 ret = -errno;
1707 }
1708 goto error;
1709 }
1710
1711 /* All good. Don't send back the write positive ret value. */
1712 ret = 0;
1713 error:
1714 return (int) ret;
1715 }
1716
1717 /*
1718 * Sanitize the wait queue of the dispatch registration thread meaning removing
1719 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1720 * notify socket is never received.
1721 */
1722 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1723 {
1724 int ret, nb_fd = 0, i;
1725 unsigned int fd_added = 0;
1726 struct lttng_poll_event events;
1727 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1728
1729 assert(wait_queue);
1730
1731 lttng_poll_init(&events);
1732
1733 /* Just skip everything for an empty queue. */
1734 if (!wait_queue->count) {
1735 goto end;
1736 }
1737
1738 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1739 if (ret < 0) {
1740 goto error_create;
1741 }
1742
1743 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1744 &wait_queue->head, head) {
1745 assert(wait_node->app);
1746 ret = lttng_poll_add(&events, wait_node->app->sock,
1747 LPOLLHUP | LPOLLERR);
1748 if (ret < 0) {
1749 goto error;
1750 }
1751
1752 fd_added = 1;
1753 }
1754
1755 if (!fd_added) {
1756 goto end;
1757 }
1758
1759 /*
1760 * Poll but don't block so we can quickly identify the faulty events and
1761 * clean them afterwards from the wait queue.
1762 */
1763 ret = lttng_poll_wait(&events, 0);
1764 if (ret < 0) {
1765 goto error;
1766 }
1767 nb_fd = ret;
1768
1769 for (i = 0; i < nb_fd; i++) {
1770 /* Get faulty FD. */
1771 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1772 int pollfd = LTTNG_POLL_GETFD(&events, i);
1773
1774 if (!revents) {
1775 /* No activity for this FD (poll implementation). */
1776 continue;
1777 }
1778
1779 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1780 &wait_queue->head, head) {
1781 if (pollfd == wait_node->app->sock &&
1782 (revents & (LPOLLHUP | LPOLLERR))) {
1783 cds_list_del(&wait_node->head);
1784 wait_queue->count--;
1785 ust_app_destroy(wait_node->app);
1786 free(wait_node);
1787 /*
1788 * Silence warning of use-after-free in
1789 * cds_list_for_each_entry_safe which uses
1790 * __typeof__(*wait_node).
1791 */
1792 wait_node = NULL;
1793 break;
1794 } else {
1795 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1796 goto error;
1797 }
1798 }
1799 }
1800
1801 if (nb_fd > 0) {
1802 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1803 }
1804
1805 end:
1806 lttng_poll_clean(&events);
1807 return;
1808
1809 error:
1810 lttng_poll_clean(&events);
1811 error_create:
1812 ERR("Unable to sanitize wait queue");
1813 return;
1814 }
1815
1816 /*
1817 * Dispatch request from the registration threads to the application
1818 * communication thread.
1819 */
1820 static void *thread_dispatch_ust_registration(void *data)
1821 {
1822 int ret, err = -1;
1823 struct cds_wfcq_node *node;
1824 struct ust_command *ust_cmd = NULL;
1825 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1826 struct ust_reg_wait_queue wait_queue = {
1827 .count = 0,
1828 };
1829
1830 rcu_register_thread();
1831
1832 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1833
1834 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1835 goto error_testpoint;
1836 }
1837
1838 health_code_update();
1839
1840 CDS_INIT_LIST_HEAD(&wait_queue.head);
1841
1842 DBG("[thread] Dispatch UST command started");
1843
1844 for (;;) {
1845 health_code_update();
1846
1847 /* Atomically prepare the queue futex */
1848 futex_nto1_prepare(&ust_cmd_queue.futex);
1849
1850 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1851 break;
1852 }
1853
1854 do {
1855 struct ust_app *app = NULL;
1856 ust_cmd = NULL;
1857
1858 /*
1859 * Make sure we don't have node(s) that have hung up before receiving
1860 * the notify socket. This is to clean the list in order to avoid
1861 * memory leaks from notify socket that are never seen.
1862 */
1863 sanitize_wait_queue(&wait_queue);
1864
1865 health_code_update();
1866 /* Dequeue command for registration */
1867 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1868 if (node == NULL) {
1869 DBG("Woken up but nothing in the UST command queue");
1870 /* Continue thread execution */
1871 break;
1872 }
1873
1874 ust_cmd = caa_container_of(node, struct ust_command, node);
1875
1876 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1877 " gid:%d sock:%d name:%s (version %d.%d)",
1878 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1879 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1880 ust_cmd->sock, ust_cmd->reg_msg.name,
1881 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1882
1883 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1884 wait_node = zmalloc(sizeof(*wait_node));
1885 if (!wait_node) {
1886 PERROR("zmalloc wait_node dispatch");
1887 ret = close(ust_cmd->sock);
1888 if (ret < 0) {
1889 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1890 }
1891 lttng_fd_put(LTTNG_FD_APPS, 1);
1892 free(ust_cmd);
1893 goto error;
1894 }
1895 CDS_INIT_LIST_HEAD(&wait_node->head);
1896
1897 /* Create application object if socket is CMD. */
1898 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1899 ust_cmd->sock);
1900 if (!wait_node->app) {
1901 ret = close(ust_cmd->sock);
1902 if (ret < 0) {
1903 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1904 }
1905 lttng_fd_put(LTTNG_FD_APPS, 1);
1906 free(wait_node);
1907 free(ust_cmd);
1908 continue;
1909 }
1910 /*
1911 * Add application to the wait queue so we can set the notify
1912 * socket before putting this object in the global ht.
1913 */
1914 cds_list_add(&wait_node->head, &wait_queue.head);
1915 wait_queue.count++;
1916
1917 free(ust_cmd);
1918 /*
1919 * We have to continue here since we don't have the notify
1920 * socket and the application MUST be added to the hash table
1921 * only at that moment.
1922 */
1923 continue;
1924 } else {
1925 /*
1926 * Look for the application in the local wait queue and set the
1927 * notify socket if found.
1928 */
1929 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1930 &wait_queue.head, head) {
1931 health_code_update();
1932 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1933 wait_node->app->notify_sock = ust_cmd->sock;
1934 cds_list_del(&wait_node->head);
1935 wait_queue.count--;
1936 app = wait_node->app;
1937 free(wait_node);
1938 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1939 break;
1940 }
1941 }
1942
1943 /*
1944 * With no application at this stage the received socket is
1945 * basically useless so close it before we free the cmd data
1946 * structure for good.
1947 */
1948 if (!app) {
1949 ret = close(ust_cmd->sock);
1950 if (ret < 0) {
1951 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1952 }
1953 lttng_fd_put(LTTNG_FD_APPS, 1);
1954 }
1955 free(ust_cmd);
1956 }
1957
1958 if (app) {
1959 /*
1960 * @session_lock_list
1961 *
1962 * Lock the global session list so from the register up to the
1963 * registration done message, no thread can see the application
1964 * and change its state.
1965 */
1966 session_lock_list();
1967 rcu_read_lock();
1968
1969 /*
1970 * Add application to the global hash table. This needs to be
1971 * done before the update to the UST registry can locate the
1972 * application.
1973 */
1974 ust_app_add(app);
1975
1976 /* Set app version. This call will print an error if needed. */
1977 (void) ust_app_version(app);
1978
1979 /* Send notify socket through the notify pipe. */
1980 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1981 app->notify_sock);
1982 if (ret < 0) {
1983 rcu_read_unlock();
1984 session_unlock_list();
1985 /*
1986 * No notify thread, stop the UST tracing. However, this is
1987 * not an internal error of the this thread thus setting
1988 * the health error code to a normal exit.
1989 */
1990 err = 0;
1991 goto error;
1992 }
1993
1994 /*
1995 * Update newly registered application with the tracing
1996 * registry info already enabled information.
1997 */
1998 update_ust_app(app->sock);
1999
2000 /*
2001 * Don't care about return value. Let the manage apps threads
2002 * handle app unregistration upon socket close.
2003 */
2004 (void) ust_app_register_done(app);
2005
2006 /*
2007 * Even if the application socket has been closed, send the app
2008 * to the thread and unregistration will take place at that
2009 * place.
2010 */
2011 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
2012 if (ret < 0) {
2013 rcu_read_unlock();
2014 session_unlock_list();
2015 /*
2016 * No apps. thread, stop the UST tracing. However, this is
2017 * not an internal error of the this thread thus setting
2018 * the health error code to a normal exit.
2019 */
2020 err = 0;
2021 goto error;
2022 }
2023
2024 rcu_read_unlock();
2025 session_unlock_list();
2026 }
2027 } while (node != NULL);
2028
2029 health_poll_entry();
2030 /* Futex wait on queue. Blocking call on futex() */
2031 futex_nto1_wait(&ust_cmd_queue.futex);
2032 health_poll_exit();
2033 }
2034 /* Normal exit, no error */
2035 err = 0;
2036
2037 error:
2038 /* Clean up wait queue. */
2039 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
2040 &wait_queue.head, head) {
2041 cds_list_del(&wait_node->head);
2042 wait_queue.count--;
2043 free(wait_node);
2044 }
2045
2046 /* Empty command queue. */
2047 for (;;) {
2048 /* Dequeue command for registration */
2049 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
2050 if (node == NULL) {
2051 break;
2052 }
2053 ust_cmd = caa_container_of(node, struct ust_command, node);
2054 ret = close(ust_cmd->sock);
2055 if (ret < 0) {
2056 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
2057 }
2058 lttng_fd_put(LTTNG_FD_APPS, 1);
2059 free(ust_cmd);
2060 }
2061
2062 error_testpoint:
2063 DBG("Dispatch thread dying");
2064 if (err) {
2065 health_error();
2066 ERR("Health error occurred in %s", __func__);
2067 }
2068 health_unregister(health_sessiond);
2069 rcu_unregister_thread();
2070 return NULL;
2071 }
2072
2073 /*
2074 * This thread manage application registration.
2075 */
2076 static void *thread_registration_apps(void *data)
2077 {
2078 int sock = -1, i, ret, pollfd, err = -1;
2079 uint32_t revents, nb_fd;
2080 struct lttng_poll_event events;
2081 /*
2082 * Get allocated in this thread, enqueued to a global queue, dequeued and
2083 * freed in the manage apps thread.
2084 */
2085 struct ust_command *ust_cmd = NULL;
2086
2087 DBG("[thread] Manage application registration started");
2088
2089 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
2090
2091 if (testpoint(sessiond_thread_registration_apps)) {
2092 goto error_testpoint;
2093 }
2094
2095 ret = lttcomm_listen_unix_sock(apps_sock);
2096 if (ret < 0) {
2097 goto error_listen;
2098 }
2099
2100 /*
2101 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2102 * more will be added to this poll set.
2103 */
2104 ret = sessiond_set_thread_pollset(&events, 2);
2105 if (ret < 0) {
2106 goto error_create_poll;
2107 }
2108
2109 /* Add the application registration socket */
2110 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
2111 if (ret < 0) {
2112 goto error_poll_add;
2113 }
2114
2115 /* Notify all applications to register */
2116 ret = notify_ust_apps(1);
2117 if (ret < 0) {
2118 ERR("Failed to notify applications or create the wait shared memory.\n"
2119 "Execution continues but there might be problem for already\n"
2120 "running applications that wishes to register.");
2121 }
2122
2123 while (1) {
2124 DBG("Accepting application registration");
2125
2126 /* Inifinite blocking call, waiting for transmission */
2127 restart:
2128 health_poll_entry();
2129 ret = lttng_poll_wait(&events, -1);
2130 health_poll_exit();
2131 if (ret < 0) {
2132 /*
2133 * Restart interrupted system call.
2134 */
2135 if (errno == EINTR) {
2136 goto restart;
2137 }
2138 goto error;
2139 }
2140
2141 nb_fd = ret;
2142
2143 for (i = 0; i < nb_fd; i++) {
2144 health_code_update();
2145
2146 /* Fetch once the poll data */
2147 revents = LTTNG_POLL_GETEV(&events, i);
2148 pollfd = LTTNG_POLL_GETFD(&events, i);
2149
2150 if (!revents) {
2151 /* No activity for this FD (poll implementation). */
2152 continue;
2153 }
2154
2155 /* Thread quit pipe has been closed. Killing thread. */
2156 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
2157 if (ret) {
2158 err = 0;
2159 goto exit;
2160 }
2161
2162 /* Event on the registration socket */
2163 if (pollfd == apps_sock) {
2164 if (revents & LPOLLIN) {
2165 sock = lttcomm_accept_unix_sock(apps_sock);
2166 if (sock < 0) {
2167 goto error;
2168 }
2169
2170 /*
2171 * Set socket timeout for both receiving and ending.
2172 * app_socket_timeout is in seconds, whereas
2173 * lttcomm_setsockopt_rcv_timeout and
2174 * lttcomm_setsockopt_snd_timeout expect msec as
2175 * parameter.
2176 */
2177 if (config.app_socket_timeout >= 0) {
2178 (void) lttcomm_setsockopt_rcv_timeout(sock,
2179 config.app_socket_timeout * 1000);
2180 (void) lttcomm_setsockopt_snd_timeout(sock,
2181 config.app_socket_timeout * 1000);
2182 }
2183
2184 /*
2185 * Set the CLOEXEC flag. Return code is useless because
2186 * either way, the show must go on.
2187 */
2188 (void) utils_set_fd_cloexec(sock);
2189
2190 /* Create UST registration command for enqueuing */
2191 ust_cmd = zmalloc(sizeof(struct ust_command));
2192 if (ust_cmd == NULL) {
2193 PERROR("ust command zmalloc");
2194 ret = close(sock);
2195 if (ret) {
2196 PERROR("close");
2197 }
2198 goto error;
2199 }
2200
2201 /*
2202 * Using message-based transmissions to ensure we don't
2203 * have to deal with partially received messages.
2204 */
2205 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2206 if (ret < 0) {
2207 ERR("Exhausted file descriptors allowed for applications.");
2208 free(ust_cmd);
2209 ret = close(sock);
2210 if (ret) {
2211 PERROR("close");
2212 }
2213 sock = -1;
2214 continue;
2215 }
2216
2217 health_code_update();
2218 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2219 if (ret < 0) {
2220 free(ust_cmd);
2221 /* Close socket of the application. */
2222 ret = close(sock);
2223 if (ret) {
2224 PERROR("close");
2225 }
2226 lttng_fd_put(LTTNG_FD_APPS, 1);
2227 sock = -1;
2228 continue;
2229 }
2230 health_code_update();
2231
2232 ust_cmd->sock = sock;
2233 sock = -1;
2234
2235 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2236 " gid:%d sock:%d name:%s (version %d.%d)",
2237 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2238 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2239 ust_cmd->sock, ust_cmd->reg_msg.name,
2240 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
2241
2242 /*
2243 * Lock free enqueue the registration request. The red pill
2244 * has been taken! This apps will be part of the *system*.
2245 */
2246 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
2247
2248 /*
2249 * Wake the registration queue futex. Implicit memory
2250 * barrier with the exchange in cds_wfcq_enqueue.
2251 */
2252 futex_nto1_wake(&ust_cmd_queue.futex);
2253 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2254 ERR("Register apps socket poll error");
2255 goto error;
2256 } else {
2257 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2258 goto error;
2259 }
2260 }
2261 }
2262 }
2263
2264 exit:
2265 error:
2266 /* Notify that the registration thread is gone */
2267 notify_ust_apps(0);
2268
2269 if (apps_sock >= 0) {
2270 ret = close(apps_sock);
2271 if (ret) {
2272 PERROR("close");
2273 }
2274 }
2275 if (sock >= 0) {
2276 ret = close(sock);
2277 if (ret) {
2278 PERROR("close");
2279 }
2280 lttng_fd_put(LTTNG_FD_APPS, 1);
2281 }
2282 unlink(config.apps_unix_sock_path.value);
2283
2284 error_poll_add:
2285 lttng_poll_clean(&events);
2286 error_listen:
2287 error_create_poll:
2288 error_testpoint:
2289 DBG("UST Registration thread cleanup complete");
2290 if (err) {
2291 health_error();
2292 ERR("Health error occurred in %s", __func__);
2293 }
2294 health_unregister(health_sessiond);
2295
2296 return NULL;
2297 }
2298
2299 /*
2300 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2301 * exec or it will fails.
2302 */
2303 static int spawn_consumer_thread(struct consumer_data *consumer_data)
2304 {
2305 int ret, clock_ret;
2306 struct timespec timeout;
2307
2308 /*
2309 * Make sure we set the readiness flag to 0 because we are NOT ready.
2310 * This access to consumer_thread_is_ready does not need to be
2311 * protected by consumer_data.cond_mutex (yet) since the consumer
2312 * management thread has not been started at this point.
2313 */
2314 consumer_data->consumer_thread_is_ready = 0;
2315
2316 /* Setup pthread condition */
2317 ret = pthread_condattr_init(&consumer_data->condattr);
2318 if (ret) {
2319 errno = ret;
2320 PERROR("pthread_condattr_init consumer data");
2321 goto error;
2322 }
2323
2324 /*
2325 * Set the monotonic clock in order to make sure we DO NOT jump in time
2326 * between the clock_gettime() call and the timedwait call. See bug #324
2327 * for a more details and how we noticed it.
2328 */
2329 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2330 if (ret) {
2331 errno = ret;
2332 PERROR("pthread_condattr_setclock consumer data");
2333 goto error;
2334 }
2335
2336 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2337 if (ret) {
2338 errno = ret;
2339 PERROR("pthread_cond_init consumer data");
2340 goto error;
2341 }
2342
2343 ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
2344 thread_manage_consumer, consumer_data);
2345 if (ret) {
2346 errno = ret;
2347 PERROR("pthread_create consumer");
2348 ret = -1;
2349 goto error;
2350 }
2351
2352 /* We are about to wait on a pthread condition */
2353 pthread_mutex_lock(&consumer_data->cond_mutex);
2354
2355 /* Get time for sem_timedwait absolute timeout */
2356 clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
2357 /*
2358 * Set the timeout for the condition timed wait even if the clock gettime
2359 * call fails since we might loop on that call and we want to avoid to
2360 * increment the timeout too many times.
2361 */
2362 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2363
2364 /*
2365 * The following loop COULD be skipped in some conditions so this is why we
2366 * set ret to 0 in order to make sure at least one round of the loop is
2367 * done.
2368 */
2369 ret = 0;
2370
2371 /*
2372 * Loop until the condition is reached or when a timeout is reached. Note
2373 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2374 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2375 * possible. This loop does not take any chances and works with both of
2376 * them.
2377 */
2378 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2379 if (clock_ret < 0) {
2380 PERROR("clock_gettime spawn consumer");
2381 /* Infinite wait for the consumerd thread to be ready */
2382 ret = pthread_cond_wait(&consumer_data->cond,
2383 &consumer_data->cond_mutex);
2384 } else {
2385 ret = pthread_cond_timedwait(&consumer_data->cond,
2386 &consumer_data->cond_mutex, &timeout);
2387 }
2388 }
2389
2390 /* Release the pthread condition */
2391 pthread_mutex_unlock(&consumer_data->cond_mutex);
2392
2393 if (ret != 0) {
2394 errno = ret;
2395 if (ret == ETIMEDOUT) {
2396 int pth_ret;
2397
2398 /*
2399 * Call has timed out so we kill the kconsumerd_thread and return
2400 * an error.
2401 */
2402 ERR("Condition timed out. The consumer thread was never ready."
2403 " Killing it");
2404 pth_ret = pthread_cancel(consumer_data->thread);
2405 if (pth_ret < 0) {
2406 PERROR("pthread_cancel consumer thread");
2407 }
2408 } else {
2409 PERROR("pthread_cond_wait failed consumer thread");
2410 }
2411 /* Caller is expecting a negative value on failure. */
2412 ret = -1;
2413 goto error;
2414 }
2415
2416 pthread_mutex_lock(&consumer_data->pid_mutex);
2417 if (consumer_data->pid == 0) {
2418 ERR("Consumerd did not start");
2419 pthread_mutex_unlock(&consumer_data->pid_mutex);
2420 goto error;
2421 }
2422 pthread_mutex_unlock(&consumer_data->pid_mutex);
2423
2424 return 0;
2425
2426 error:
2427 return ret;
2428 }
2429
2430 /*
2431 * Join consumer thread
2432 */
2433 static int join_consumer_thread(struct consumer_data *consumer_data)
2434 {
2435 void *status;
2436
2437 /* Consumer pid must be a real one. */
2438 if (consumer_data->pid > 0) {
2439 int ret;
2440 ret = kill(consumer_data->pid, SIGTERM);
2441 if (ret) {
2442 PERROR("Error killing consumer daemon");
2443 return ret;
2444 }
2445 return pthread_join(consumer_data->thread, &status);
2446 } else {
2447 return 0;
2448 }
2449 }
2450
2451 /*
2452 * Fork and exec a consumer daemon (consumerd).
2453 *
2454 * Return pid if successful else -1.
2455 */
2456 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2457 {
2458 int ret;
2459 pid_t pid;
2460 const char *consumer_to_use;
2461 const char *verbosity;
2462 struct stat st;
2463
2464 DBG("Spawning consumerd");
2465
2466 pid = fork();
2467 if (pid == 0) {
2468 /*
2469 * Exec consumerd.
2470 */
2471 if (config.verbose_consumer) {
2472 verbosity = "--verbose";
2473 } else if (lttng_opt_quiet) {
2474 verbosity = "--quiet";
2475 } else {
2476 verbosity = "";
2477 }
2478
2479 switch (consumer_data->type) {
2480 case LTTNG_CONSUMER_KERNEL:
2481 /*
2482 * Find out which consumerd to execute. We will first try the
2483 * 64-bit path, then the sessiond's installation directory, and
2484 * fallback on the 32-bit one,
2485 */
2486 DBG3("Looking for a kernel consumer at these locations:");
2487 DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL");
2488 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE);
2489 DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL");
2490 if (stat(config.consumerd64_bin_path.value, &st) == 0) {
2491 DBG3("Found location #1");
2492 consumer_to_use = config.consumerd64_bin_path.value;
2493 } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) {
2494 DBG3("Found location #2");
2495 consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE;
2496 } else if (stat(config.consumerd32_bin_path.value, &st) == 0) {
2497 DBG3("Found location #3");
2498 consumer_to_use = config.consumerd32_bin_path.value;
2499 } else {
2500 DBG("Could not find any valid consumerd executable");
2501 ret = -EINVAL;
2502 goto error;
2503 }
2504 DBG("Using kernel consumer at: %s", consumer_to_use);
2505 (void) execl(consumer_to_use,
2506 "lttng-consumerd", verbosity, "-k",
2507 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2508 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2509 "--group", config.tracing_group_name.value,
2510 NULL);
2511 break;
2512 case LTTNG_CONSUMER64_UST:
2513 {
2514 if (config.consumerd64_lib_dir.value) {
2515 char *tmp;
2516 size_t tmplen;
2517 char *tmpnew;
2518
2519 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2520 if (!tmp) {
2521 tmp = "";
2522 }
2523 tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
2524 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2525 if (!tmpnew) {
2526 ret = -ENOMEM;
2527 goto error;
2528 }
2529 strcat(tmpnew, config.consumerd64_lib_dir.value);
2530 if (tmp[0] != '\0') {
2531 strcat(tmpnew, ":");
2532 strcat(tmpnew, tmp);
2533 }
2534 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
2535 free(tmpnew);
2536 if (ret) {
2537 ret = -errno;
2538 goto error;
2539 }
2540 }
2541 DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value);
2542 (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u",
2543 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2544 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2545 "--group", config.tracing_group_name.value,
2546 NULL);
2547 break;
2548 }
2549 case LTTNG_CONSUMER32_UST:
2550 {
2551 if (config.consumerd32_lib_dir.value) {
2552 char *tmp;
2553 size_t tmplen;
2554 char *tmpnew;
2555
2556 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2557 if (!tmp) {
2558 tmp = "";
2559 }
2560 tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
2561 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2562 if (!tmpnew) {
2563 ret = -ENOMEM;
2564 goto error;
2565 }
2566 strcat(tmpnew, config.consumerd32_lib_dir.value);
2567 if (tmp[0] != '\0') {
2568 strcat(tmpnew, ":");
2569 strcat(tmpnew, tmp);
2570 }
2571 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
2572 free(tmpnew);
2573 if (ret) {
2574 ret = -errno;
2575 goto error;
2576 }
2577 }
2578 DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value);
2579 (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u",
2580 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2581 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2582 "--group", config.tracing_group_name.value,
2583 NULL);
2584 break;
2585 }
2586 default:
2587 ERR("unknown consumer type");
2588 errno = 0;
2589 }
2590 if (errno != 0) {
2591 PERROR("Consumer execl()");
2592 }
2593 /* Reaching this point, we got a failure on our execl(). */
2594 exit(EXIT_FAILURE);
2595 } else if (pid > 0) {
2596 ret = pid;
2597 } else {
2598 PERROR("start consumer fork");
2599 ret = -errno;
2600 }
2601 error:
2602 return ret;
2603 }
2604
2605 /*
2606 * Spawn the consumerd daemon and session daemon thread.
2607 */
2608 static int start_consumerd(struct consumer_data *consumer_data)
2609 {
2610 int ret;
2611
2612 /*
2613 * Set the listen() state on the socket since there is a possible race
2614 * between the exec() of the consumer daemon and this call if place in the
2615 * consumer thread. See bug #366 for more details.
2616 */
2617 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2618 if (ret < 0) {
2619 goto error;
2620 }
2621
2622 pthread_mutex_lock(&consumer_data->pid_mutex);
2623 if (consumer_data->pid != 0) {
2624 pthread_mutex_unlock(&consumer_data->pid_mutex);
2625 goto end;
2626 }
2627
2628 ret = spawn_consumerd(consumer_data);
2629 if (ret < 0) {
2630 ERR("Spawning consumerd failed");
2631 pthread_mutex_unlock(&consumer_data->pid_mutex);
2632 goto error;
2633 }
2634
2635 /* Setting up the consumer_data pid */
2636 consumer_data->pid = ret;
2637 DBG2("Consumer pid %d", consumer_data->pid);
2638 pthread_mutex_unlock(&consumer_data->pid_mutex);
2639
2640 DBG2("Spawning consumer control thread");
2641 ret = spawn_consumer_thread(consumer_data);
2642 if (ret < 0) {
2643 ERR("Fatal error spawning consumer control thread");
2644 goto error;
2645 }
2646
2647 end:
2648 return 0;
2649
2650 error:
2651 /* Cleanup already created sockets on error. */
2652 if (consumer_data->err_sock >= 0) {
2653 int err;
2654
2655 err = close(consumer_data->err_sock);
2656 if (err < 0) {
2657 PERROR("close consumer data error socket");
2658 }
2659 }
2660 return ret;
2661 }
2662
2663 /*
2664 * Setup necessary data for kernel tracer action.
2665 */
2666 static int init_kernel_tracer(void)
2667 {
2668 int ret;
2669
2670 /* Modprobe lttng kernel modules */
2671 ret = modprobe_lttng_control();
2672 if (ret < 0) {
2673 goto error;
2674 }
2675
2676 /* Open debugfs lttng */
2677 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2678 if (kernel_tracer_fd < 0) {
2679 DBG("Failed to open %s", module_proc_lttng);
2680 goto error_open;
2681 }
2682
2683 /* Validate kernel version */
2684 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
2685 &kernel_tracer_abi_version);
2686 if (ret < 0) {
2687 goto error_version;
2688 }
2689
2690 ret = modprobe_lttng_data();
2691 if (ret < 0) {
2692 goto error_modules;
2693 }
2694
2695 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
2696 kernel_tracer_fd);
2697 if (ret < 0) {
2698 goto error_modules;
2699 }
2700
2701 if (ret < 1) {
2702 WARN("Kernel tracer does not support buffer monitoring. "
2703 "The monitoring timer of channels in the kernel domain "
2704 "will be set to 0 (disabled).");
2705 }
2706
2707 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2708 return 0;
2709
2710 error_version:
2711 modprobe_remove_lttng_control();
2712 ret = close(kernel_tracer_fd);
2713 if (ret) {
2714 PERROR("close");
2715 }
2716 kernel_tracer_fd = -1;
2717 return LTTNG_ERR_KERN_VERSION;
2718
2719 error_modules:
2720 ret = close(kernel_tracer_fd);
2721 if (ret) {
2722 PERROR("close");
2723 }
2724
2725 error_open:
2726 modprobe_remove_lttng_control();
2727
2728 error:
2729 WARN("No kernel tracer available");
2730 kernel_tracer_fd = -1;
2731 if (!is_root) {
2732 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2733 } else {
2734 return LTTNG_ERR_KERN_NA;
2735 }
2736 }
2737
2738
2739 /*
2740 * Copy consumer output from the tracing session to the domain session. The
2741 * function also applies the right modification on a per domain basis for the
2742 * trace files destination directory.
2743 *
2744 * Should *NOT* be called with RCU read-side lock held.
2745 */
2746 static int copy_session_consumer(int domain, struct ltt_session *session)
2747 {
2748 int ret;
2749 const char *dir_name;
2750 struct consumer_output *consumer;
2751
2752 assert(session);
2753 assert(session->consumer);
2754
2755 switch (domain) {
2756 case LTTNG_DOMAIN_KERNEL:
2757 DBG3("Copying tracing session consumer output in kernel session");
2758 /*
2759 * XXX: We should audit the session creation and what this function
2760 * does "extra" in order to avoid a destroy since this function is used
2761 * in the domain session creation (kernel and ust) only. Same for UST
2762 * domain.
2763 */
2764 if (session->kernel_session->consumer) {
2765 consumer_output_put(session->kernel_session->consumer);
2766 }
2767 session->kernel_session->consumer =
2768 consumer_copy_output(session->consumer);
2769 /* Ease our life a bit for the next part */
2770 consumer = session->kernel_session->consumer;
2771 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2772 break;
2773 case LTTNG_DOMAIN_JUL:
2774 case LTTNG_DOMAIN_LOG4J:
2775 case LTTNG_DOMAIN_PYTHON:
2776 case LTTNG_DOMAIN_UST:
2777 DBG3("Copying tracing session consumer output in UST session");
2778 if (session->ust_session->consumer) {
2779 consumer_output_put(session->ust_session->consumer);
2780 }
2781 session->ust_session->consumer =
2782 consumer_copy_output(session->consumer);
2783 /* Ease our life a bit for the next part */
2784 consumer = session->ust_session->consumer;
2785 dir_name = DEFAULT_UST_TRACE_DIR;
2786 break;
2787 default:
2788 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2789 goto error;
2790 }
2791
2792 /* Append correct directory to subdir */
2793 strncat(consumer->subdir, dir_name,
2794 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2795 DBG3("Copy session consumer subdir %s", consumer->subdir);
2796
2797 ret = LTTNG_OK;
2798
2799 error:
2800 return ret;
2801 }
2802
2803 /*
2804 * Create an UST session and add it to the session ust list.
2805 *
2806 * Should *NOT* be called with RCU read-side lock held.
2807 */
2808 static int create_ust_session(struct ltt_session *session,
2809 struct lttng_domain *domain)
2810 {
2811 int ret;
2812 struct ltt_ust_session *lus = NULL;
2813
2814 assert(session);
2815 assert(domain);
2816 assert(session->consumer);
2817
2818 switch (domain->type) {
2819 case LTTNG_DOMAIN_JUL:
2820 case LTTNG_DOMAIN_LOG4J:
2821 case LTTNG_DOMAIN_PYTHON:
2822 case LTTNG_DOMAIN_UST:
2823 break;
2824 default:
2825 ERR("Unknown UST domain on create session %d", domain->type);
2826 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2827 goto error;
2828 }
2829
2830 DBG("Creating UST session");
2831
2832 lus = trace_ust_create_session(session->id);
2833 if (lus == NULL) {
2834 ret = LTTNG_ERR_UST_SESS_FAIL;
2835 goto error;
2836 }
2837
2838 lus->uid = session->uid;
2839 lus->gid = session->gid;
2840 lus->output_traces = session->output_traces;
2841 lus->snapshot_mode = session->snapshot_mode;
2842 lus->live_timer_interval = session->live_timer;
2843 session->ust_session = lus;
2844 if (session->shm_path[0]) {
2845 strncpy(lus->root_shm_path, session->shm_path,
2846 sizeof(lus->root_shm_path));
2847 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
2848 strncpy(lus->shm_path, session->shm_path,
2849 sizeof(lus->shm_path));
2850 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2851 strncat(lus->shm_path, "/ust",
2852 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2853 }
2854 /* Copy session output to the newly created UST session */
2855 ret = copy_session_consumer(domain->type, session);
2856 if (ret != LTTNG_OK) {
2857 goto error;
2858 }
2859
2860 return LTTNG_OK;
2861
2862 error:
2863 free(lus);
2864 session->ust_session = NULL;
2865 return ret;
2866 }
2867
2868 /*
2869 * Create a kernel tracer session then create the default channel.
2870 */
2871 static int create_kernel_session(struct ltt_session *session)
2872 {
2873 int ret;
2874
2875 DBG("Creating kernel session");
2876
2877 ret = kernel_create_session(session, kernel_tracer_fd);
2878 if (ret < 0) {
2879 ret = LTTNG_ERR_KERN_SESS_FAIL;
2880 goto error;
2881 }
2882
2883 /* Code flow safety */
2884 assert(session->kernel_session);
2885
2886 /* Copy session output to the newly created Kernel session */
2887 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2888 if (ret != LTTNG_OK) {
2889 goto error;
2890 }
2891
2892 session->kernel_session->uid = session->uid;
2893 session->kernel_session->gid = session->gid;
2894 session->kernel_session->output_traces = session->output_traces;
2895 session->kernel_session->snapshot_mode = session->snapshot_mode;
2896
2897 return LTTNG_OK;
2898
2899 error:
2900 trace_kernel_destroy_session(session->kernel_session);
2901 session->kernel_session = NULL;
2902 return ret;
2903 }
2904
2905 /*
2906 * Count number of session permitted by uid/gid.
2907 */
2908 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2909 {
2910 unsigned int i = 0;
2911 struct ltt_session *session;
2912
2913 DBG("Counting number of available session for UID %d GID %d",
2914 uid, gid);
2915 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2916 /*
2917 * Only list the sessions the user can control.
2918 */
2919 if (!session_access_ok(session, uid, gid)) {
2920 continue;
2921 }
2922 i++;
2923 }
2924 return i;
2925 }
2926
2927 /*
2928 * Check if the current kernel tracer supports the session rotation feature.
2929 * Return 1 if it does, 0 otherwise.
2930 */
2931 static int check_rotate_compatible(void)
2932 {
2933 int ret = 1;
2934
2935 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
2936 DBG("Kernel tracer version is not compatible with the rotation feature");
2937 ret = 0;
2938 }
2939
2940 return ret;
2941 }
2942
2943 /*
2944 * Process the command requested by the lttng client within the command
2945 * context structure. This function make sure that the return structure (llm)
2946 * is set and ready for transmission before returning.
2947 *
2948 * Return any error encountered or 0 for success.
2949 *
2950 * "sock" is only used for special-case var. len data.
2951 *
2952 * Should *NOT* be called with RCU read-side lock held.
2953 */
2954 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2955 int *sock_error)
2956 {
2957 int ret = LTTNG_OK;
2958 int need_tracing_session = 1;
2959 int need_domain;
2960
2961 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2962
2963 assert(!rcu_read_ongoing());
2964
2965 *sock_error = 0;
2966
2967 switch (cmd_ctx->lsm->cmd_type) {
2968 case LTTNG_CREATE_SESSION:
2969 case LTTNG_CREATE_SESSION_SNAPSHOT:
2970 case LTTNG_CREATE_SESSION_LIVE:
2971 case LTTNG_DESTROY_SESSION:
2972 case LTTNG_LIST_SESSIONS:
2973 case LTTNG_LIST_DOMAINS:
2974 case LTTNG_START_TRACE:
2975 case LTTNG_STOP_TRACE:
2976 case LTTNG_DATA_PENDING:
2977 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2978 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2979 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2980 case LTTNG_SNAPSHOT_RECORD:
2981 case LTTNG_SAVE_SESSION:
2982 case LTTNG_SET_SESSION_SHM_PATH:
2983 case LTTNG_REGENERATE_METADATA:
2984 case LTTNG_REGENERATE_STATEDUMP:
2985 case LTTNG_REGISTER_TRIGGER:
2986 case LTTNG_UNREGISTER_TRIGGER:
2987 case LTTNG_ROTATE_SESSION:
2988 case LTTNG_ROTATION_GET_INFO:
2989 case LTTNG_SESSION_GET_CURRENT_OUTPUT:
2990 case LTTNG_ROTATION_SET_SCHEDULE:
2991 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD:
2992 case LTTNG_ROTATION_SCHEDULE_GET_SIZE:
2993 need_domain = 0;
2994 break;
2995 default:
2996 need_domain = 1;
2997 }
2998
2999 if (config.no_kernel && need_domain
3000 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
3001 if (!is_root) {
3002 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3003 } else {
3004 ret = LTTNG_ERR_KERN_NA;
3005 }
3006 goto error;
3007 }
3008
3009 /* Deny register consumer if we already have a spawned consumer. */
3010 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
3011 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3012 if (kconsumer_data.pid > 0) {
3013 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3014 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3015 goto error;
3016 }
3017 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3018 }
3019
3020 /*
3021 * Check for command that don't needs to allocate a returned payload. We do
3022 * this here so we don't have to make the call for no payload at each
3023 * command.
3024 */
3025 switch(cmd_ctx->lsm->cmd_type) {
3026 case LTTNG_LIST_SESSIONS:
3027 case LTTNG_LIST_TRACEPOINTS:
3028 case LTTNG_LIST_TRACEPOINT_FIELDS:
3029 case LTTNG_LIST_DOMAINS:
3030 case LTTNG_LIST_CHANNELS:
3031 case LTTNG_LIST_EVENTS:
3032 case LTTNG_LIST_SYSCALLS:
3033 case LTTNG_LIST_TRACKER_PIDS:
3034 case LTTNG_DATA_PENDING:
3035 case LTTNG_ROTATE_SESSION:
3036 case LTTNG_ROTATION_GET_INFO:
3037 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD:
3038 case LTTNG_ROTATION_SCHEDULE_GET_SIZE:
3039 break;
3040 default:
3041 /* Setup lttng message with no payload */
3042 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
3043 if (ret < 0) {
3044 /* This label does not try to unlock the session */
3045 goto init_setup_error;
3046 }
3047 }
3048
3049 /* Commands that DO NOT need a session. */
3050 switch (cmd_ctx->lsm->cmd_type) {
3051 case LTTNG_CREATE_SESSION:
3052 case LTTNG_CREATE_SESSION_SNAPSHOT:
3053 case LTTNG_CREATE_SESSION_LIVE:
3054 case LTTNG_LIST_SESSIONS:
3055 case LTTNG_LIST_TRACEPOINTS:
3056 case LTTNG_LIST_SYSCALLS:
3057 case LTTNG_LIST_TRACEPOINT_FIELDS:
3058 case LTTNG_SAVE_SESSION:
3059 case LTTNG_REGISTER_TRIGGER:
3060 case LTTNG_UNREGISTER_TRIGGER:
3061 need_tracing_session = 0;
3062 break;
3063 default:
3064 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
3065 /*
3066 * We keep the session list lock across _all_ commands
3067 * for now, because the per-session lock does not
3068 * handle teardown properly.
3069 */
3070 session_lock_list();
3071 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
3072 if (cmd_ctx->session == NULL) {
3073 ret = LTTNG_ERR_SESS_NOT_FOUND;
3074 goto error;
3075 } else {
3076 /* Acquire lock for the session */
3077 session_lock(cmd_ctx->session);
3078 }
3079 break;
3080 }
3081
3082 /*
3083 * Commands that need a valid session but should NOT create one if none
3084 * exists. Instead of creating one and destroying it when the command is
3085 * handled, process that right before so we save some round trip in useless
3086 * code path.
3087 */
3088 switch (cmd_ctx->lsm->cmd_type) {
3089 case LTTNG_DISABLE_CHANNEL:
3090 case LTTNG_DISABLE_EVENT:
3091 switch (cmd_ctx->lsm->domain.type) {
3092 case LTTNG_DOMAIN_KERNEL:
3093 if (!cmd_ctx->session->kernel_session) {
3094 ret = LTTNG_ERR_NO_CHANNEL;
3095 goto error;
3096 }
3097 break;
3098 case LTTNG_DOMAIN_JUL:
3099 case LTTNG_DOMAIN_LOG4J:
3100 case LTTNG_DOMAIN_PYTHON:
3101 case LTTNG_DOMAIN_UST:
3102 if (!cmd_ctx->session->ust_session) {
3103 ret = LTTNG_ERR_NO_CHANNEL;
3104 goto error;
3105 }
3106 break;
3107 default:
3108 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3109 goto error;
3110 }
3111 default:
3112 break;
3113 }
3114
3115 if (!need_domain) {
3116 goto skip_domain;
3117 }
3118
3119 /*
3120 * Check domain type for specific "pre-action".
3121 */
3122 switch (cmd_ctx->lsm->domain.type) {
3123 case LTTNG_DOMAIN_KERNEL:
3124 if (!is_root) {
3125 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3126 goto error;
3127 }
3128
3129 /* Kernel tracer check */
3130 if (kernel_tracer_fd == -1) {
3131 /* Basically, load kernel tracer modules */
3132 ret = init_kernel_tracer();
3133 if (ret != 0) {
3134 goto error;
3135 }
3136 }
3137
3138 /* Consumer is in an ERROR state. Report back to client */
3139 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
3140 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3141 goto error;
3142 }
3143
3144 /* Need a session for kernel command */
3145 if (need_tracing_session) {
3146 if (cmd_ctx->session->kernel_session == NULL) {
3147 ret = create_kernel_session(cmd_ctx->session);
3148 if (ret < 0) {
3149 ret = LTTNG_ERR_KERN_SESS_FAIL;
3150 goto error;
3151 }
3152 }
3153
3154 /* Start the kernel consumer daemon */
3155 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3156 if (kconsumer_data.pid == 0 &&
3157 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3158 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3159 ret = start_consumerd(&kconsumer_data);
3160 if (ret < 0) {
3161 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3162 goto error;
3163 }
3164 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3165 } else {
3166 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3167 }
3168
3169 /*
3170 * The consumer was just spawned so we need to add the socket to
3171 * the consumer output of the session if exist.
3172 */
3173 ret = consumer_create_socket(&kconsumer_data,
3174 cmd_ctx->session->kernel_session->consumer);
3175 if (ret < 0) {
3176 goto error;
3177 }
3178 }
3179
3180 break;
3181 case LTTNG_DOMAIN_JUL:
3182 case LTTNG_DOMAIN_LOG4J:
3183 case LTTNG_DOMAIN_PYTHON:
3184 case LTTNG_DOMAIN_UST:
3185 {
3186 if (!ust_app_supported()) {
3187 ret = LTTNG_ERR_NO_UST;
3188 goto error;
3189 }
3190 /* Consumer is in an ERROR state. Report back to client */
3191 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
3192 ret = LTTNG_ERR_NO_USTCONSUMERD;
3193 goto error;
3194 }
3195
3196 if (need_tracing_session) {
3197 /* Create UST session if none exist. */
3198 if (cmd_ctx->session->ust_session == NULL) {
3199 ret = create_ust_session(cmd_ctx->session,
3200 &cmd_ctx->lsm->domain);
3201 if (ret != LTTNG_OK) {
3202 goto error;
3203 }
3204 }
3205
3206 /* Start the UST consumer daemons */
3207 /* 64-bit */
3208 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3209 if (config.consumerd64_bin_path.value &&
3210 ustconsumer64_data.pid == 0 &&
3211 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3212 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3213 ret = start_consumerd(&ustconsumer64_data);
3214 if (ret < 0) {
3215 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
3216 uatomic_set(&ust_consumerd64_fd, -EINVAL);
3217 goto error;
3218 }
3219
3220 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
3221 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3222 } else {
3223 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3224 }
3225
3226 /*
3227 * Setup socket for consumer 64 bit. No need for atomic access
3228 * since it was set above and can ONLY be set in this thread.
3229 */
3230 ret = consumer_create_socket(&ustconsumer64_data,
3231 cmd_ctx->session->ust_session->consumer);
3232 if (ret < 0) {
3233 goto error;
3234 }
3235
3236 /* 32-bit */
3237 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
3238 if (config.consumerd32_bin_path.value &&
3239 ustconsumer32_data.pid == 0 &&
3240 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3241 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3242 ret = start_consumerd(&ustconsumer32_data);
3243 if (ret < 0) {
3244 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
3245 uatomic_set(&ust_consumerd32_fd, -EINVAL);
3246 goto error;
3247 }
3248
3249 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
3250 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3251 } else {
3252 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3253 }
3254
3255 /*
3256 * Setup socket for consumer 64 bit. No need for atomic access
3257 * since it was set above and can ONLY be set in this thread.
3258 */
3259 ret = consumer_create_socket(&ustconsumer32_data,
3260 cmd_ctx->session->ust_session->consumer);
3261 if (ret < 0) {
3262 goto error;
3263 }
3264 }
3265 break;
3266 }
3267 default:
3268 break;
3269 }
3270 skip_domain:
3271
3272 /* Validate consumer daemon state when start/stop trace command */
3273 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3274 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3275 switch (cmd_ctx->lsm->domain.type) {
3276 case LTTNG_DOMAIN_NONE:
3277 break;
3278 case LTTNG_DOMAIN_JUL:
3279 case LTTNG_DOMAIN_LOG4J:
3280 case LTTNG_DOMAIN_PYTHON:
3281 case LTTNG_DOMAIN_UST:
3282 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
3283 ret = LTTNG_ERR_NO_USTCONSUMERD;
3284 goto error;
3285 }
3286 break;
3287 case LTTNG_DOMAIN_KERNEL:
3288 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
3289 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3290 goto error;
3291 }
3292 break;
3293 default:
3294 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3295 goto error;
3296 }
3297 }
3298
3299 /*
3300 * Check that the UID or GID match that of the tracing session.
3301 * The root user can interact with all sessions.
3302 */
3303 if (need_tracing_session) {
3304 if (!session_access_ok(cmd_ctx->session,
3305 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3306 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
3307 ret = LTTNG_ERR_EPERM;
3308 goto error;
3309 }
3310 }
3311
3312 /*
3313 * Send relayd information to consumer as soon as we have a domain and a
3314 * session defined.
3315 */
3316 if (cmd_ctx->session && need_domain) {
3317 /*
3318 * Setup relayd if not done yet. If the relayd information was already
3319 * sent to the consumer, this call will gracefully return.
3320 */
3321 ret = cmd_setup_relayd(cmd_ctx->session);
3322 if (ret != LTTNG_OK) {
3323 goto error;
3324 }
3325 }
3326
3327 /* Process by command type */
3328 switch (cmd_ctx->lsm->cmd_type) {
3329 case LTTNG_ADD_CONTEXT:
3330 {
3331 /*
3332 * An LTTNG_ADD_CONTEXT command might have a supplementary
3333 * payload if the context being added is an application context.
3334 */
3335 if (cmd_ctx->lsm->u.context.ctx.ctx ==
3336 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
3337 char *provider_name = NULL, *context_name = NULL;
3338 size_t provider_name_len =
3339 cmd_ctx->lsm->u.context.provider_name_len;
3340 size_t context_name_len =
3341 cmd_ctx->lsm->u.context.context_name_len;
3342
3343 if (provider_name_len == 0 || context_name_len == 0) {
3344 /*
3345 * Application provider and context names MUST
3346 * be provided.
3347 */
3348 ret = -LTTNG_ERR_INVALID;
3349 goto error;
3350 }
3351
3352 provider_name = zmalloc(provider_name_len + 1);
3353 if (!provider_name) {
3354 ret = -LTTNG_ERR_NOMEM;
3355 goto error;
3356 }
3357 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
3358 provider_name;
3359
3360 context_name = zmalloc(context_name_len + 1);
3361 if (!context_name) {
3362 ret = -LTTNG_ERR_NOMEM;
3363 goto error_add_context;
3364 }
3365 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
3366 context_name;
3367
3368 ret = lttcomm_recv_unix_sock(sock, provider_name,
3369 provider_name_len);
3370 if (ret < 0) {
3371 goto error_add_context;
3372 }
3373
3374 ret = lttcomm_recv_unix_sock(sock, context_name,
3375 context_name_len);
3376 if (ret < 0) {
3377 goto error_add_context;
3378 }
3379 }
3380
3381 /*
3382 * cmd_add_context assumes ownership of the provider and context
3383 * names.
3384 */
3385 ret = cmd_add_context(cmd_ctx->session,
3386 cmd_ctx->lsm->domain.type,
3387 cmd_ctx->lsm->u.context.channel_name,
3388 &cmd_ctx->lsm->u.context.ctx,
3389 kernel_poll_pipe[1]);
3390
3391 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
3392 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
3393 error_add_context:
3394 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
3395 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
3396 if (ret < 0) {
3397 goto error;
3398 }
3399 break;
3400 }
3401 case LTTNG_DISABLE_CHANNEL:
3402 {
3403 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3404 cmd_ctx->lsm->u.disable.channel_name);
3405 break;
3406 }
3407 case LTTNG_DISABLE_EVENT:
3408 {
3409
3410 /*
3411 * FIXME: handle filter; for now we just receive the filter's
3412 * bytecode along with the filter expression which are sent by
3413 * liblttng-ctl and discard them.
3414 *
3415 * This fixes an issue where the client may block while sending
3416 * the filter payload and encounter an error because the session
3417 * daemon closes the socket without ever handling this data.
3418 */
3419 size_t count = cmd_ctx->lsm->u.disable.expression_len +
3420 cmd_ctx->lsm->u.disable.bytecode_len;
3421
3422 if (count) {
3423 char data[LTTNG_FILTER_MAX_LEN];
3424
3425 DBG("Discarding disable event command payload of size %zu", count);
3426 while (count) {
3427 ret = lttcomm_recv_unix_sock(sock, data,
3428 count > sizeof(data) ? sizeof(data) : count);
3429 if (ret < 0) {
3430 goto error;
3431 }
3432
3433 count -= (size_t) ret;
3434 }
3435 }
3436 /* FIXME: passing packed structure to non-packed pointer */
3437 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3438 cmd_ctx->lsm->u.disable.channel_name,
3439 &cmd_ctx->lsm->u.disable.event);
3440 break;
3441 }
3442 case LTTNG_ENABLE_CHANNEL:
3443 {
3444 cmd_ctx->lsm->u.channel.chan.attr.extended.ptr =
3445 (struct lttng_channel_extended *) &cmd_ctx->lsm->u.channel.extended;
3446 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
3447 &cmd_ctx->lsm->u.channel.chan,
3448 kernel_poll_pipe[1]);
3449 break;
3450 }
3451 case LTTNG_TRACK_PID:
3452 {
3453 ret = cmd_track_pid(cmd_ctx->session,
3454 cmd_ctx->lsm->domain.type,
3455 cmd_ctx->lsm->u.pid_tracker.pid);
3456 break;
3457 }
3458 case LTTNG_UNTRACK_PID:
3459 {
3460 ret = cmd_untrack_pid(cmd_ctx->session,
3461 cmd_ctx->lsm->domain.type,
3462 cmd_ctx->lsm->u.pid_tracker.pid);
3463 break;
3464 }
3465 case LTTNG_ENABLE_EVENT:
3466 {
3467 struct lttng_event_exclusion *exclusion = NULL;
3468 struct lttng_filter_bytecode *bytecode = NULL;
3469 char *filter_expression = NULL;
3470
3471 /* Handle exclusion events and receive it from the client. */
3472 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3473 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3474
3475 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3476 (count * LTTNG_SYMBOL_NAME_LEN));
3477 if (!exclusion) {
3478 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3479 goto error;
3480 }
3481
3482 DBG("Receiving var len exclusion event list from client ...");
3483 exclusion->count = count;
3484 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3485 count * LTTNG_SYMBOL_NAME_LEN);
3486 if (ret <= 0) {
3487 DBG("Nothing recv() from client var len data... continuing");
3488 *sock_error = 1;
3489 free(exclusion);
3490 ret = LTTNG_ERR_EXCLUSION_INVAL;
3491 goto error;
3492 }
3493 }
3494
3495 /* Get filter expression from client. */
3496 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3497 size_t expression_len =
3498 cmd_ctx->lsm->u.enable.expression_len;
3499
3500 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3501 ret = LTTNG_ERR_FILTER_INVAL;
3502 free(exclusion);
3503 goto error;
3504 }
3505
3506 filter_expression = zmalloc(expression_len);
3507 if (!filter_expression) {
3508 free(exclusion);
3509 ret = LTTNG_ERR_FILTER_NOMEM;
3510 goto error;
3511 }
3512
3513 /* Receive var. len. data */
3514 DBG("Receiving var len filter's expression from client ...");
3515 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3516 expression_len);
3517 if (ret <= 0) {
3518 DBG("Nothing recv() from client car len data... continuing");
3519 *sock_error = 1;
3520 free(filter_expression);
3521 free(exclusion);
3522 ret = LTTNG_ERR_FILTER_INVAL;
3523 goto error;
3524 }
3525 }
3526
3527 /* Handle filter and get bytecode from client. */
3528 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3529 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3530
3531 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3532 ret = LTTNG_ERR_FILTER_INVAL;
3533 free(filter_expression);
3534 free(exclusion);
3535 goto error;
3536 }
3537
3538 bytecode = zmalloc(bytecode_len);
3539 if (!bytecode) {
3540 free(filter_expression);
3541 free(exclusion);
3542 ret = LTTNG_ERR_FILTER_NOMEM;
3543 goto error;
3544 }
3545
3546 /* Receive var. len. data */
3547 DBG("Receiving var len filter's bytecode from client ...");
3548 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3549 if (ret <= 0) {
3550 DBG("Nothing recv() from client car len data... continuing");
3551 *sock_error = 1;
3552 free(filter_expression);
3553 free(bytecode);
3554 free(exclusion);
3555 ret = LTTNG_ERR_FILTER_INVAL;
3556 goto error;
3557 }
3558
3559 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3560 free(filter_expression);
3561 free(bytecode);
3562 free(exclusion);
3563 ret = LTTNG_ERR_FILTER_INVAL;
3564 goto error;
3565 }
3566 }
3567
3568 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3569 cmd_ctx->lsm->u.enable.channel_name,
3570 &cmd_ctx->lsm->u.enable.event,
3571 filter_expression, bytecode, exclusion,
3572 kernel_poll_pipe[1]);
3573 break;
3574 }
3575 case LTTNG_LIST_TRACEPOINTS:
3576 {
3577 struct lttng_event *events;
3578 ssize_t nb_events;
3579
3580 session_lock_list();
3581 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3582 session_unlock_list();
3583 if (nb_events < 0) {
3584 /* Return value is a negative lttng_error_code. */
3585 ret = -nb_events;
3586 goto error;
3587 }
3588
3589 /*
3590 * Setup lttng message with payload size set to the event list size in
3591 * bytes and then copy list into the llm payload.
3592 */
3593 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3594 sizeof(struct lttng_event) * nb_events);
3595 free(events);
3596
3597 if (ret < 0) {
3598 goto setup_error;
3599 }
3600
3601 ret = LTTNG_OK;
3602 break;
3603 }
3604 case LTTNG_LIST_TRACEPOINT_FIELDS:
3605 {
3606 struct lttng_event_field *fields;
3607 ssize_t nb_fields;
3608
3609 session_lock_list();
3610 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3611 &fields);
3612 session_unlock_list();
3613 if (nb_fields < 0) {
3614 /* Return value is a negative lttng_error_code. */
3615 ret = -nb_fields;
3616 goto error;
3617 }
3618
3619 /*
3620 * Setup lttng message with payload size set to the event list size in
3621 * bytes and then copy list into the llm payload.
3622 */
3623 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
3624 sizeof(struct lttng_event_field) * nb_fields);
3625 free(fields);
3626
3627 if (ret < 0) {
3628 goto setup_error;
3629 }
3630
3631 ret = LTTNG_OK;
3632 break;
3633 }
3634 case LTTNG_LIST_SYSCALLS:
3635 {
3636 struct lttng_event *events;
3637 ssize_t nb_events;
3638
3639 nb_events = cmd_list_syscalls(&events);
3640 if (nb_events < 0) {
3641 /* Return value is a negative lttng_error_code. */
3642 ret = -nb_events;
3643 goto error;
3644 }
3645
3646 /*
3647 * Setup lttng message with payload size set to the event list size in
3648 * bytes and then copy list into the llm payload.
3649 */
3650 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3651 sizeof(struct lttng_event) * nb_events);
3652 free(events);
3653
3654 if (ret < 0) {
3655 goto setup_error;
3656 }
3657
3658 ret = LTTNG_OK;
3659 break;
3660 }
3661 case LTTNG_LIST_TRACKER_PIDS:
3662 {
3663 int32_t *pids = NULL;
3664 ssize_t nr_pids;
3665
3666 nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
3667 cmd_ctx->lsm->domain.type, &pids);
3668 if (nr_pids < 0) {
3669 /* Return value is a negative lttng_error_code. */
3670 ret = -nr_pids;
3671 goto error;
3672 }
3673
3674 /*
3675 * Setup lttng message with payload size set to the event list size in
3676 * bytes and then copy list into the llm payload.
3677 */
3678 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids,
3679 sizeof(int32_t) * nr_pids);
3680 free(pids);
3681
3682 if (ret < 0) {
3683 goto setup_error;
3684 }
3685
3686 ret = LTTNG_OK;
3687 break;
3688 }
3689 case LTTNG_SET_CONSUMER_URI:
3690 {
3691 size_t nb_uri, len;
3692 struct lttng_uri *uris;
3693
3694 nb_uri = cmd_ctx->lsm->u.uri.size;
3695 len = nb_uri * sizeof(struct lttng_uri);
3696
3697 if (nb_uri == 0) {
3698 ret = LTTNG_ERR_INVALID;
3699 goto error;
3700 }
3701
3702 uris = zmalloc(len);
3703 if (uris == NULL) {
3704 ret = LTTNG_ERR_FATAL;
3705 goto error;
3706 }
3707
3708 /* Receive variable len data */
3709 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3710 ret = lttcomm_recv_unix_sock(sock, uris, len);
3711 if (ret <= 0) {
3712 DBG("No URIs received from client... continuing");
3713 *sock_error = 1;
3714 ret = LTTNG_ERR_SESSION_FAIL;
3715 free(uris);
3716 goto error;
3717 }
3718
3719 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3720 free(uris);
3721 if (ret != LTTNG_OK) {
3722 goto error;