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