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