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