Use lib common daemonize
[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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <getopt.h>
21 #include <grp.h>
22 #include <limits.h>
23 #include <paths.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <inttypes.h>
30 #include <sys/mman.h>
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <urcu/uatomic.h>
38 #include <unistd.h>
39 #include <config.h>
40
41 #include <common/common.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
48 #include <common/daemonize.h>
49
50 #include "lttng-sessiond.h"
51 #include "buffer-registry.h"
52 #include "channel.h"
53 #include "cmd.h"
54 #include "consumer.h"
55 #include "context.h"
56 #include "event.h"
57 #include "kernel.h"
58 #include "kernel-consumer.h"
59 #include "modprobe.h"
60 #include "shm.h"
61 #include "ust-ctl.h"
62 #include "ust-consumer.h"
63 #include "utils.h"
64 #include "fd-limit.h"
65 #include "health-sessiond.h"
66 #include "testpoint.h"
67 #include "ust-thread.h"
68 #include "jul-thread.h"
69
70 #define CONSUMERD_FILE "lttng-consumerd"
71
72 const char *progname;
73 static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
74 static const char *opt_pidfile;
75 static int opt_sig_parent;
76 static int opt_verbose_consumer;
77 static int opt_daemon;
78 static int opt_no_kernel;
79 static int is_root; /* Set to 1 if the daemon is running as root */
80 static pid_t ppid; /* Parent PID for --sig-parent option */
81 static pid_t child_ppid; /* Internal parent PID use with daemonize. */
82 static char *rundir;
83
84 /* Set to 1 when a SIGUSR1 signal is received. */
85 static int recv_child_signal;
86
87 /*
88 * Consumer daemon specific control data. Every value not initialized here is
89 * set to 0 by the static definition.
90 */
91 static struct consumer_data kconsumer_data = {
92 .type = LTTNG_CONSUMER_KERNEL,
93 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
94 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
95 .err_sock = -1,
96 .cmd_sock = -1,
97 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
98 .lock = PTHREAD_MUTEX_INITIALIZER,
99 .cond = PTHREAD_COND_INITIALIZER,
100 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
101 };
102 static struct consumer_data ustconsumer64_data = {
103 .type = LTTNG_CONSUMER64_UST,
104 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
105 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
106 .err_sock = -1,
107 .cmd_sock = -1,
108 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
109 .lock = PTHREAD_MUTEX_INITIALIZER,
110 .cond = PTHREAD_COND_INITIALIZER,
111 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
112 };
113 static struct consumer_data ustconsumer32_data = {
114 .type = LTTNG_CONSUMER32_UST,
115 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
116 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
117 .err_sock = -1,
118 .cmd_sock = -1,
119 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
120 .lock = PTHREAD_MUTEX_INITIALIZER,
121 .cond = PTHREAD_COND_INITIALIZER,
122 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
123 };
124
125 /* Shared between threads */
126 static int dispatch_thread_exit;
127
128 /* Global application Unix socket path */
129 static char apps_unix_sock_path[PATH_MAX];
130 /* Global client Unix socket path */
131 static char client_unix_sock_path[PATH_MAX];
132 /* global wait shm path for UST */
133 static char wait_shm_path[PATH_MAX];
134 /* Global health check unix path */
135 static char health_unix_sock_path[PATH_MAX];
136
137 /* Sockets and FDs */
138 static int client_sock = -1;
139 static int apps_sock = -1;
140 int kernel_tracer_fd = -1;
141 static int kernel_poll_pipe[2] = { -1, -1 };
142
143 /*
144 * Quit pipe for all threads. This permits a single cancellation point
145 * for all threads when receiving an event on the pipe.
146 */
147 static int thread_quit_pipe[2] = { -1, -1 };
148
149 /*
150 * This pipe is used to inform the thread managing application communication
151 * that a command is queued and ready to be processed.
152 */
153 static int apps_cmd_pipe[2] = { -1, -1 };
154
155 int apps_cmd_notify_pipe[2] = { -1, -1 };
156
157 /* Pthread, Mutexes and Semaphores */
158 static pthread_t apps_thread;
159 static pthread_t apps_notify_thread;
160 static pthread_t reg_apps_thread;
161 static pthread_t client_thread;
162 static pthread_t kernel_thread;
163 static pthread_t dispatch_thread;
164 static pthread_t health_thread;
165 static pthread_t ht_cleanup_thread;
166 static pthread_t jul_reg_thread;
167
168 /*
169 * UST registration command queue. This queue is tied with a futex and uses a N
170 * wakers / 1 waiter implemented and detailed in futex.c/.h
171 *
172 * The thread_manage_apps and thread_dispatch_ust_registration interact with
173 * this queue and the wait/wake scheme.
174 */
175 static struct ust_cmd_queue ust_cmd_queue;
176
177 /*
178 * Pointer initialized before thread creation.
179 *
180 * This points to the tracing session list containing the session count and a
181 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
182 * MUST NOT be taken if you call a public function in session.c.
183 *
184 * The lock is nested inside the structure: session_list_ptr->lock. Please use
185 * session_lock_list and session_unlock_list for lock acquisition.
186 */
187 static struct ltt_session_list *session_list_ptr;
188
189 int ust_consumerd64_fd = -1;
190 int ust_consumerd32_fd = -1;
191
192 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
193 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
194 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
195 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
196
197 static const char *module_proc_lttng = "/proc/lttng";
198
199 /*
200 * Consumer daemon state which is changed when spawning it, killing it or in
201 * case of a fatal error.
202 */
203 enum consumerd_state {
204 CONSUMER_STARTED = 1,
205 CONSUMER_STOPPED = 2,
206 CONSUMER_ERROR = 3,
207 };
208
209 /*
210 * This consumer daemon state is used to validate if a client command will be
211 * able to reach the consumer. If not, the client is informed. For instance,
212 * doing a "lttng start" when the consumer state is set to ERROR will return an
213 * error to the client.
214 *
215 * The following example shows a possible race condition of this scheme:
216 *
217 * consumer thread error happens
218 * client cmd arrives
219 * client cmd checks state -> still OK
220 * consumer thread exit, sets error
221 * client cmd try to talk to consumer
222 * ...
223 *
224 * However, since the consumer is a different daemon, we have no way of making
225 * sure the command will reach it safely even with this state flag. This is why
226 * we consider that up to the state validation during command processing, the
227 * command is safe. After that, we can not guarantee the correctness of the
228 * client request vis-a-vis the consumer.
229 */
230 static enum consumerd_state ust_consumerd_state;
231 static enum consumerd_state kernel_consumerd_state;
232
233 /*
234 * Socket timeout for receiving and sending in seconds.
235 */
236 static int app_socket_timeout;
237
238 /* Set in main() with the current page size. */
239 long page_size;
240
241 /* Application health monitoring */
242 struct health_app *health_sessiond;
243
244 /* JUL TCP port for registration. Used by the JUL thread. */
245 unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT;
246
247 /*
248 * Whether sessiond is ready for commands/health check requests.
249 * NR_LTTNG_SESSIOND_READY must match the number of calls to
250 * lttng_sessiond_notify_ready().
251 */
252 #define NR_LTTNG_SESSIOND_READY 2
253 int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
254
255 /* Notify parents that we are ready for cmd and health check */
256 static
257 void lttng_sessiond_notify_ready(void)
258 {
259 if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
260 /*
261 * Notify parent pid that we are ready to accept command
262 * for client side. This ppid is the one from the
263 * external process that spawned us.
264 */
265 if (opt_sig_parent) {
266 kill(ppid, SIGUSR1);
267 }
268
269 /*
270 * Notify the parent of the fork() process that we are
271 * ready.
272 */
273 if (opt_daemon) {
274 kill(child_ppid, SIGUSR1);
275 }
276 }
277 }
278
279 static
280 void setup_consumerd_path(void)
281 {
282 const char *bin, *libdir;
283
284 /*
285 * Allow INSTALL_BIN_PATH to be used as a target path for the
286 * native architecture size consumer if CONFIG_CONSUMER*_PATH
287 * has not been defined.
288 */
289 #if (CAA_BITS_PER_LONG == 32)
290 if (!consumerd32_bin[0]) {
291 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
292 }
293 if (!consumerd32_libdir[0]) {
294 consumerd32_libdir = INSTALL_LIB_PATH;
295 }
296 #elif (CAA_BITS_PER_LONG == 64)
297 if (!consumerd64_bin[0]) {
298 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
299 }
300 if (!consumerd64_libdir[0]) {
301 consumerd64_libdir = INSTALL_LIB_PATH;
302 }
303 #else
304 #error "Unknown bitness"
305 #endif
306
307 /*
308 * runtime env. var. overrides the build default.
309 */
310 bin = getenv("LTTNG_CONSUMERD32_BIN");
311 if (bin) {
312 consumerd32_bin = bin;
313 }
314 bin = getenv("LTTNG_CONSUMERD64_BIN");
315 if (bin) {
316 consumerd64_bin = bin;
317 }
318 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
319 if (libdir) {
320 consumerd32_libdir = libdir;
321 }
322 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
323 if (libdir) {
324 consumerd64_libdir = libdir;
325 }
326 }
327
328 /*
329 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
330 */
331 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
332 {
333 int ret;
334
335 assert(events);
336
337 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
338 if (ret < 0) {
339 goto error;
340 }
341
342 /* Add quit pipe */
343 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
344 if (ret < 0) {
345 goto error;
346 }
347
348 return 0;
349
350 error:
351 return ret;
352 }
353
354 /*
355 * Check if the thread quit pipe was triggered.
356 *
357 * Return 1 if it was triggered else 0;
358 */
359 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
360 {
361 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
362 return 1;
363 }
364
365 return 0;
366 }
367
368 /*
369 * Init thread quit pipe.
370 *
371 * Return -1 on error or 0 if all pipes are created.
372 */
373 static int init_thread_quit_pipe(void)
374 {
375 int ret, i;
376
377 ret = pipe(thread_quit_pipe);
378 if (ret < 0) {
379 PERROR("thread quit pipe");
380 goto error;
381 }
382
383 for (i = 0; i < 2; i++) {
384 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
385 if (ret < 0) {
386 PERROR("fcntl");
387 goto error;
388 }
389 }
390
391 error:
392 return ret;
393 }
394
395 /*
396 * Stop all threads by closing the thread quit pipe.
397 */
398 static void stop_threads(void)
399 {
400 int ret;
401
402 /* Stopping all threads */
403 DBG("Terminating all threads");
404 ret = notify_thread_pipe(thread_quit_pipe[1]);
405 if (ret < 0) {
406 ERR("write error on thread quit pipe");
407 }
408
409 /* Dispatch thread */
410 CMM_STORE_SHARED(dispatch_thread_exit, 1);
411 futex_nto1_wake(&ust_cmd_queue.futex);
412 }
413
414 /*
415 * Close every consumer sockets.
416 */
417 static void close_consumer_sockets(void)
418 {
419 int ret;
420
421 if (kconsumer_data.err_sock >= 0) {
422 ret = close(kconsumer_data.err_sock);
423 if (ret < 0) {
424 PERROR("kernel consumer err_sock close");
425 }
426 }
427 if (ustconsumer32_data.err_sock >= 0) {
428 ret = close(ustconsumer32_data.err_sock);
429 if (ret < 0) {
430 PERROR("UST consumerd32 err_sock close");
431 }
432 }
433 if (ustconsumer64_data.err_sock >= 0) {
434 ret = close(ustconsumer64_data.err_sock);
435 if (ret < 0) {
436 PERROR("UST consumerd64 err_sock close");
437 }
438 }
439 if (kconsumer_data.cmd_sock >= 0) {
440 ret = close(kconsumer_data.cmd_sock);
441 if (ret < 0) {
442 PERROR("kernel consumer cmd_sock close");
443 }
444 }
445 if (ustconsumer32_data.cmd_sock >= 0) {
446 ret = close(ustconsumer32_data.cmd_sock);
447 if (ret < 0) {
448 PERROR("UST consumerd32 cmd_sock close");
449 }
450 }
451 if (ustconsumer64_data.cmd_sock >= 0) {
452 ret = close(ustconsumer64_data.cmd_sock);
453 if (ret < 0) {
454 PERROR("UST consumerd64 cmd_sock close");
455 }
456 }
457 }
458
459 /*
460 * Cleanup the daemon
461 */
462 static void cleanup(void)
463 {
464 int ret;
465 struct ltt_session *sess, *stmp;
466 char path[PATH_MAX];
467
468 DBG("Cleaning up");
469
470 /*
471 * Close the thread quit pipe. It has already done its job,
472 * since we are now called.
473 */
474 utils_close_pipe(thread_quit_pipe);
475
476 /*
477 * If opt_pidfile is undefined, the default file will be wiped when
478 * removing the rundir.
479 */
480 if (opt_pidfile) {
481 ret = remove(opt_pidfile);
482 if (ret < 0) {
483 PERROR("remove pidfile %s", opt_pidfile);
484 }
485 }
486
487 DBG("Removing sessiond and consumerd content of directory %s", rundir);
488
489 /* sessiond */
490 snprintf(path, PATH_MAX,
491 "%s/%s",
492 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
493 DBG("Removing %s", path);
494 (void) unlink(path);
495
496 snprintf(path, PATH_MAX, "%s/%s", rundir,
497 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE);
498 DBG("Removing %s", path);
499 (void) unlink(path);
500
501 /* kconsumerd */
502 snprintf(path, PATH_MAX,
503 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
504 rundir);
505 DBG("Removing %s", path);
506 (void) unlink(path);
507
508 snprintf(path, PATH_MAX,
509 DEFAULT_KCONSUMERD_PATH,
510 rundir);
511 DBG("Removing directory %s", path);
512 (void) rmdir(path);
513
514 /* ust consumerd 32 */
515 snprintf(path, PATH_MAX,
516 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
517 rundir);
518 DBG("Removing %s", path);
519 (void) unlink(path);
520
521 snprintf(path, PATH_MAX,
522 DEFAULT_USTCONSUMERD32_PATH,
523 rundir);
524 DBG("Removing directory %s", path);
525 (void) rmdir(path);
526
527 /* ust consumerd 64 */
528 snprintf(path, PATH_MAX,
529 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
530 rundir);
531 DBG("Removing %s", path);
532 (void) unlink(path);
533
534 snprintf(path, PATH_MAX,
535 DEFAULT_USTCONSUMERD64_PATH,
536 rundir);
537 DBG("Removing directory %s", path);
538 (void) rmdir(path);
539
540 /*
541 * We do NOT rmdir rundir because there are other processes
542 * using it, for instance lttng-relayd, which can start in
543 * parallel with this teardown.
544 */
545
546 free(rundir);
547
548 DBG("Cleaning up all sessions");
549
550 /* Destroy session list mutex */
551 if (session_list_ptr != NULL) {
552 pthread_mutex_destroy(&session_list_ptr->lock);
553
554 /* Cleanup ALL session */
555 cds_list_for_each_entry_safe(sess, stmp,
556 &session_list_ptr->head, list) {
557 cmd_destroy_session(sess, kernel_poll_pipe[1]);
558 }
559 }
560
561 DBG("Closing all UST sockets");
562 ust_app_clean_list();
563 buffer_reg_destroy_registries();
564
565 if (is_root && !opt_no_kernel) {
566 DBG2("Closing kernel fd");
567 if (kernel_tracer_fd >= 0) {
568 ret = close(kernel_tracer_fd);
569 if (ret) {
570 PERROR("close");
571 }
572 }
573 DBG("Unloading kernel modules");
574 modprobe_remove_lttng_all();
575 }
576
577 close_consumer_sockets();
578
579 /* <fun> */
580 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
581 "Matthew, BEET driven development works!%c[%dm",
582 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
583 /* </fun> */
584 }
585
586 /*
587 * Send data on a unix socket using the liblttsessiondcomm API.
588 *
589 * Return lttcomm error code.
590 */
591 static int send_unix_sock(int sock, void *buf, size_t len)
592 {
593 /* Check valid length */
594 if (len == 0) {
595 return -1;
596 }
597
598 return lttcomm_send_unix_sock(sock, buf, len);
599 }
600
601 /*
602 * Free memory of a command context structure.
603 */
604 static void clean_command_ctx(struct command_ctx **cmd_ctx)
605 {
606 DBG("Clean command context structure");
607 if (*cmd_ctx) {
608 if ((*cmd_ctx)->llm) {
609 free((*cmd_ctx)->llm);
610 }
611 if ((*cmd_ctx)->lsm) {
612 free((*cmd_ctx)->lsm);
613 }
614 free(*cmd_ctx);
615 *cmd_ctx = NULL;
616 }
617 }
618
619 /*
620 * Notify UST applications using the shm mmap futex.
621 */
622 static int notify_ust_apps(int active)
623 {
624 char *wait_shm_mmap;
625
626 DBG("Notifying applications of session daemon state: %d", active);
627
628 /* See shm.c for this call implying mmap, shm and futex calls */
629 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
630 if (wait_shm_mmap == NULL) {
631 goto error;
632 }
633
634 /* Wake waiting process */
635 futex_wait_update((int32_t *) wait_shm_mmap, active);
636
637 /* Apps notified successfully */
638 return 0;
639
640 error:
641 return -1;
642 }
643
644 /*
645 * Setup the outgoing data buffer for the response (llm) by allocating the
646 * right amount of memory and copying the original information from the lsm
647 * structure.
648 *
649 * Return total size of the buffer pointed by buf.
650 */
651 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
652 {
653 int ret, buf_size;
654
655 buf_size = size;
656
657 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
658 if (cmd_ctx->llm == NULL) {
659 PERROR("zmalloc");
660 ret = -ENOMEM;
661 goto error;
662 }
663
664 /* Copy common data */
665 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
666 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
667
668 cmd_ctx->llm->data_size = size;
669 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
670
671 return buf_size;
672
673 error:
674 return ret;
675 }
676
677 /*
678 * Update the kernel poll set of all channel fd available over all tracing
679 * session. Add the wakeup pipe at the end of the set.
680 */
681 static int update_kernel_poll(struct lttng_poll_event *events)
682 {
683 int ret;
684 struct ltt_session *session;
685 struct ltt_kernel_channel *channel;
686
687 DBG("Updating kernel poll set");
688
689 session_lock_list();
690 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
691 session_lock(session);
692 if (session->kernel_session == NULL) {
693 session_unlock(session);
694 continue;
695 }
696
697 cds_list_for_each_entry(channel,
698 &session->kernel_session->channel_list.head, list) {
699 /* Add channel fd to the kernel poll set */
700 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
701 if (ret < 0) {
702 session_unlock(session);
703 goto error;
704 }
705 DBG("Channel fd %d added to kernel set", channel->fd);
706 }
707 session_unlock(session);
708 }
709 session_unlock_list();
710
711 return 0;
712
713 error:
714 session_unlock_list();
715 return -1;
716 }
717
718 /*
719 * Find the channel fd from 'fd' over all tracing session. When found, check
720 * for new channel stream and send those stream fds to the kernel consumer.
721 *
722 * Useful for CPU hotplug feature.
723 */
724 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
725 {
726 int ret = 0;
727 struct ltt_session *session;
728 struct ltt_kernel_session *ksess;
729 struct ltt_kernel_channel *channel;
730
731 DBG("Updating kernel streams for channel fd %d", fd);
732
733 session_lock_list();
734 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
735 session_lock(session);
736 if (session->kernel_session == NULL) {
737 session_unlock(session);
738 continue;
739 }
740 ksess = session->kernel_session;
741
742 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
743 if (channel->fd == fd) {
744 DBG("Channel found, updating kernel streams");
745 ret = kernel_open_channel_stream(channel);
746 if (ret < 0) {
747 goto error;
748 }
749 /* Update the stream global counter */
750 ksess->stream_count_global += ret;
751
752 /*
753 * Have we already sent fds to the consumer? If yes, it means
754 * that tracing is started so it is safe to send our updated
755 * stream fds.
756 */
757 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
758 struct lttng_ht_iter iter;
759 struct consumer_socket *socket;
760
761 rcu_read_lock();
762 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
763 &iter.iter, socket, node.node) {
764 pthread_mutex_lock(socket->lock);
765 ret = kernel_consumer_send_channel_stream(socket,
766 channel, ksess,
767 session->output_traces ? 1 : 0);
768 pthread_mutex_unlock(socket->lock);
769 if (ret < 0) {
770 rcu_read_unlock();
771 goto error;
772 }
773 }
774 rcu_read_unlock();
775 }
776 goto error;
777 }
778 }
779 session_unlock(session);
780 }
781 session_unlock_list();
782 return ret;
783
784 error:
785 session_unlock(session);
786 session_unlock_list();
787 return ret;
788 }
789
790 /*
791 * For each tracing session, update newly registered apps. The session list
792 * lock MUST be acquired before calling this.
793 */
794 static void update_ust_app(int app_sock)
795 {
796 struct ltt_session *sess, *stmp;
797
798 /* Consumer is in an ERROR state. Stop any application update. */
799 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
800 /* Stop the update process since the consumer is dead. */
801 return;
802 }
803
804 /* For all tracing session(s) */
805 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
806 session_lock(sess);
807 if (sess->ust_session) {
808 ust_app_global_update(sess->ust_session, app_sock);
809 }
810 session_unlock(sess);
811 }
812 }
813
814 /*
815 * This thread manage event coming from the kernel.
816 *
817 * Features supported in this thread:
818 * -) CPU Hotplug
819 */
820 static void *thread_manage_kernel(void *data)
821 {
822 int ret, i, pollfd, update_poll_flag = 1, err = -1;
823 uint32_t revents, nb_fd;
824 char tmp;
825 struct lttng_poll_event events;
826
827 DBG("[thread] Thread manage kernel started");
828
829 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
830
831 /*
832 * This first step of the while is to clean this structure which could free
833 * non NULL pointers so initialize it before the loop.
834 */
835 lttng_poll_init(&events);
836
837 if (testpoint(sessiond_thread_manage_kernel)) {
838 goto error_testpoint;
839 }
840
841 health_code_update();
842
843 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
844 goto error_testpoint;
845 }
846
847 while (1) {
848 health_code_update();
849
850 if (update_poll_flag == 1) {
851 /* Clean events object. We are about to populate it again. */
852 lttng_poll_clean(&events);
853
854 ret = sessiond_set_thread_pollset(&events, 2);
855 if (ret < 0) {
856 goto error_poll_create;
857 }
858
859 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
860 if (ret < 0) {
861 goto error;
862 }
863
864 /* This will add the available kernel channel if any. */
865 ret = update_kernel_poll(&events);
866 if (ret < 0) {
867 goto error;
868 }
869 update_poll_flag = 0;
870 }
871
872 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
873
874 /* Poll infinite value of time */
875 restart:
876 health_poll_entry();
877 ret = lttng_poll_wait(&events, -1);
878 health_poll_exit();
879 if (ret < 0) {
880 /*
881 * Restart interrupted system call.
882 */
883 if (errno == EINTR) {
884 goto restart;
885 }
886 goto error;
887 } else if (ret == 0) {
888 /* Should not happen since timeout is infinite */
889 ERR("Return value of poll is 0 with an infinite timeout.\n"
890 "This should not have happened! Continuing...");
891 continue;
892 }
893
894 nb_fd = ret;
895
896 for (i = 0; i < nb_fd; i++) {
897 /* Fetch once the poll data */
898 revents = LTTNG_POLL_GETEV(&events, i);
899 pollfd = LTTNG_POLL_GETFD(&events, i);
900
901 health_code_update();
902
903 /* Thread quit pipe has been closed. Killing thread. */
904 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
905 if (ret) {
906 err = 0;
907 goto exit;
908 }
909
910 /* Check for data on kernel pipe */
911 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
912 (void) lttng_read(kernel_poll_pipe[0],
913 &tmp, 1);
914 /*
915 * Ret value is useless here, if this pipe gets any actions an
916 * update is required anyway.
917 */
918 update_poll_flag = 1;
919 continue;
920 } else {
921 /*
922 * New CPU detected by the kernel. Adding kernel stream to
923 * kernel session and updating the kernel consumer
924 */
925 if (revents & LPOLLIN) {
926 ret = update_kernel_stream(&kconsumer_data, pollfd);
927 if (ret < 0) {
928 continue;
929 }
930 break;
931 /*
932 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
933 * and unregister kernel stream at this point.
934 */
935 }
936 }
937 }
938 }
939
940 exit:
941 error:
942 lttng_poll_clean(&events);
943 error_poll_create:
944 error_testpoint:
945 utils_close_pipe(kernel_poll_pipe);
946 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
947 if (err) {
948 health_error();
949 ERR("Health error occurred in %s", __func__);
950 WARN("Kernel thread died unexpectedly. "
951 "Kernel tracing can continue but CPU hotplug is disabled.");
952 }
953 health_unregister(health_sessiond);
954 DBG("Kernel thread dying");
955 return NULL;
956 }
957
958 /*
959 * Signal pthread condition of the consumer data that the thread.
960 */
961 static void signal_consumer_condition(struct consumer_data *data, int state)
962 {
963 pthread_mutex_lock(&data->cond_mutex);
964
965 /*
966 * The state is set before signaling. It can be any value, it's the waiter
967 * job to correctly interpret this condition variable associated to the
968 * consumer pthread_cond.
969 *
970 * A value of 0 means that the corresponding thread of the consumer data
971 * was not started. 1 indicates that the thread has started and is ready
972 * for action. A negative value means that there was an error during the
973 * thread bootstrap.
974 */
975 data->consumer_thread_is_ready = state;
976 (void) pthread_cond_signal(&data->cond);
977
978 pthread_mutex_unlock(&data->cond_mutex);
979 }
980
981 /*
982 * This thread manage the consumer error sent back to the session daemon.
983 */
984 static void *thread_manage_consumer(void *data)
985 {
986 int sock = -1, i, ret, pollfd, err = -1;
987 uint32_t revents, nb_fd;
988 enum lttcomm_return_code code;
989 struct lttng_poll_event events;
990 struct consumer_data *consumer_data = data;
991
992 DBG("[thread] Manage consumer started");
993
994 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
995
996 health_code_update();
997
998 /*
999 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1000 * metadata_sock. Nothing more will be added to this poll set.
1001 */
1002 ret = sessiond_set_thread_pollset(&events, 3);
1003 if (ret < 0) {
1004 goto error_poll;
1005 }
1006
1007 /*
1008 * The error socket here is already in a listening state which was done
1009 * just before spawning this thread to avoid a race between the consumer
1010 * daemon exec trying to connect and the listen() call.
1011 */
1012 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
1013 if (ret < 0) {
1014 goto error;
1015 }
1016
1017 health_code_update();
1018
1019 /* Infinite blocking call, waiting for transmission */
1020 restart:
1021 health_poll_entry();
1022
1023 if (testpoint(sessiond_thread_manage_consumer)) {
1024 goto error;
1025 }
1026
1027 ret = lttng_poll_wait(&events, -1);
1028 health_poll_exit();
1029 if (ret < 0) {
1030 /*
1031 * Restart interrupted system call.
1032 */
1033 if (errno == EINTR) {
1034 goto restart;
1035 }
1036 goto error;
1037 }
1038
1039 nb_fd = ret;
1040
1041 for (i = 0; i < nb_fd; i++) {
1042 /* Fetch once the poll data */
1043 revents = LTTNG_POLL_GETEV(&events, i);
1044 pollfd = LTTNG_POLL_GETFD(&events, i);
1045
1046 health_code_update();
1047
1048 /* Thread quit pipe has been closed. Killing thread. */
1049 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1050 if (ret) {
1051 err = 0;
1052 goto exit;
1053 }
1054
1055 /* Event on the registration socket */
1056 if (pollfd == consumer_data->err_sock) {
1057 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1058 ERR("consumer err socket poll error");
1059 goto error;
1060 }
1061 }
1062 }
1063
1064 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1065 if (sock < 0) {
1066 goto error;
1067 }
1068
1069 /*
1070 * Set the CLOEXEC flag. Return code is useless because either way, the
1071 * show must go on.
1072 */
1073 (void) utils_set_fd_cloexec(sock);
1074
1075 health_code_update();
1076
1077 DBG2("Receiving code from consumer err_sock");
1078
1079 /* Getting status code from kconsumerd */
1080 ret = lttcomm_recv_unix_sock(sock, &code,
1081 sizeof(enum lttcomm_return_code));
1082 if (ret <= 0) {
1083 goto error;
1084 }
1085
1086 health_code_update();
1087
1088 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1089 /* Connect both socket, command and metadata. */
1090 consumer_data->cmd_sock =
1091 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1092 consumer_data->metadata_fd =
1093 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1094 if (consumer_data->cmd_sock < 0
1095 || consumer_data->metadata_fd < 0) {
1096 PERROR("consumer connect cmd socket");
1097 /* On error, signal condition and quit. */
1098 signal_consumer_condition(consumer_data, -1);
1099 goto error;
1100 }
1101 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1102 /* Create metadata socket lock. */
1103 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1104 if (consumer_data->metadata_sock.lock == NULL) {
1105 PERROR("zmalloc pthread mutex");
1106 ret = -1;
1107 goto error;
1108 }
1109 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1110
1111 signal_consumer_condition(consumer_data, 1);
1112 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1113 DBG("Consumer metadata socket ready (fd: %d)",
1114 consumer_data->metadata_fd);
1115 } else {
1116 ERR("consumer error when waiting for SOCK_READY : %s",
1117 lttcomm_get_readable_code(-code));
1118 goto error;
1119 }
1120
1121 /* Remove the consumerd error sock since we've established a connexion */
1122 ret = lttng_poll_del(&events, consumer_data->err_sock);
1123 if (ret < 0) {
1124 goto error;
1125 }
1126
1127 /* Add new accepted error socket. */
1128 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1129 if (ret < 0) {
1130 goto error;
1131 }
1132
1133 /* Add metadata socket that is successfully connected. */
1134 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1135 LPOLLIN | LPOLLRDHUP);
1136 if (ret < 0) {
1137 goto error;
1138 }
1139
1140 health_code_update();
1141
1142 /* Infinite blocking call, waiting for transmission */
1143 restart_poll:
1144 while (1) {
1145 health_poll_entry();
1146 ret = lttng_poll_wait(&events, -1);
1147 health_poll_exit();
1148 if (ret < 0) {
1149 /*
1150 * Restart interrupted system call.
1151 */
1152 if (errno == EINTR) {
1153 goto restart_poll;
1154 }
1155 goto error;
1156 }
1157
1158 nb_fd = ret;
1159
1160 for (i = 0; i < nb_fd; i++) {
1161 /* Fetch once the poll data */
1162 revents = LTTNG_POLL_GETEV(&events, i);
1163 pollfd = LTTNG_POLL_GETFD(&events, i);
1164
1165 health_code_update();
1166
1167 /* Thread quit pipe has been closed. Killing thread. */
1168 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1169 if (ret) {
1170 err = 0;
1171 goto exit;
1172 }
1173
1174 if (pollfd == sock) {
1175 /* Event on the consumerd socket */
1176 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1177 ERR("consumer err socket second poll error");
1178 goto error;
1179 }
1180 health_code_update();
1181 /* Wait for any kconsumerd error */
1182 ret = lttcomm_recv_unix_sock(sock, &code,
1183 sizeof(enum lttcomm_return_code));
1184 if (ret <= 0) {
1185 ERR("consumer closed the command socket");
1186 goto error;
1187 }
1188
1189 ERR("consumer return code : %s",
1190 lttcomm_get_readable_code(-code));
1191
1192 goto exit;
1193 } else if (pollfd == consumer_data->metadata_fd) {
1194 /* UST metadata requests */
1195 ret = ust_consumer_metadata_request(
1196 &consumer_data->metadata_sock);
1197 if (ret < 0) {
1198 ERR("Handling metadata request");
1199 goto error;
1200 }
1201 break;
1202 } else {
1203 ERR("Unknown pollfd");
1204 goto error;
1205 }
1206 }
1207 health_code_update();
1208 }
1209
1210 exit:
1211 error:
1212 /*
1213 * We lock here because we are about to close the sockets and some other
1214 * thread might be using them so get exclusive access which will abort all
1215 * other consumer command by other threads.
1216 */
1217 pthread_mutex_lock(&consumer_data->lock);
1218
1219 /* Immediately set the consumerd state to stopped */
1220 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1221 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1222 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1223 consumer_data->type == LTTNG_CONSUMER32_UST) {
1224 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1225 } else {
1226 /* Code flow error... */
1227 assert(0);
1228 }
1229
1230 if (consumer_data->err_sock >= 0) {
1231 ret = close(consumer_data->err_sock);
1232 if (ret) {
1233 PERROR("close");
1234 }
1235 consumer_data->err_sock = -1;
1236 }
1237 if (consumer_data->cmd_sock >= 0) {
1238 ret = close(consumer_data->cmd_sock);
1239 if (ret) {
1240 PERROR("close");
1241 }
1242 consumer_data->cmd_sock = -1;
1243 }
1244 if (*consumer_data->metadata_sock.fd_ptr >= 0) {
1245 ret = close(*consumer_data->metadata_sock.fd_ptr);
1246 if (ret) {
1247 PERROR("close");
1248 }
1249 }
1250
1251 if (sock >= 0) {
1252 ret = close(sock);
1253 if (ret) {
1254 PERROR("close");
1255 }
1256 }
1257
1258 unlink(consumer_data->err_unix_sock_path);
1259 unlink(consumer_data->cmd_unix_sock_path);
1260 consumer_data->pid = 0;
1261 pthread_mutex_unlock(&consumer_data->lock);
1262
1263 /* Cleanup metadata socket mutex. */
1264 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1265 free(consumer_data->metadata_sock.lock);
1266
1267 lttng_poll_clean(&events);
1268 error_poll:
1269 if (err) {
1270 health_error();
1271 ERR("Health error occurred in %s", __func__);
1272 }
1273 health_unregister(health_sessiond);
1274 DBG("consumer thread cleanup completed");
1275
1276 return NULL;
1277 }
1278
1279 /*
1280 * This thread manage application communication.
1281 */
1282 static void *thread_manage_apps(void *data)
1283 {
1284 int i, ret, pollfd, err = -1;
1285 ssize_t size_ret;
1286 uint32_t revents, nb_fd;
1287 struct lttng_poll_event events;
1288
1289 DBG("[thread] Manage application started");
1290
1291 rcu_register_thread();
1292 rcu_thread_online();
1293
1294 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1295
1296 if (testpoint(sessiond_thread_manage_apps)) {
1297 goto error_testpoint;
1298 }
1299
1300 health_code_update();
1301
1302 ret = sessiond_set_thread_pollset(&events, 2);
1303 if (ret < 0) {
1304 goto error_poll_create;
1305 }
1306
1307 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1308 if (ret < 0) {
1309 goto error;
1310 }
1311
1312 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1313 goto error;
1314 }
1315
1316 health_code_update();
1317
1318 while (1) {
1319 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
1320
1321 /* Inifinite blocking call, waiting for transmission */
1322 restart:
1323 health_poll_entry();
1324 ret = lttng_poll_wait(&events, -1);
1325 health_poll_exit();
1326 if (ret < 0) {
1327 /*
1328 * Restart interrupted system call.
1329 */
1330 if (errno == EINTR) {
1331 goto restart;
1332 }
1333 goto error;
1334 }
1335
1336 nb_fd = ret;
1337
1338 for (i = 0; i < nb_fd; i++) {
1339 /* Fetch once the poll data */
1340 revents = LTTNG_POLL_GETEV(&events, i);
1341 pollfd = LTTNG_POLL_GETFD(&events, i);
1342
1343 health_code_update();
1344
1345 /* Thread quit pipe has been closed. Killing thread. */
1346 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1347 if (ret) {
1348 err = 0;
1349 goto exit;
1350 }
1351
1352 /* Inspect the apps cmd pipe */
1353 if (pollfd == apps_cmd_pipe[0]) {
1354 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1355 ERR("Apps command pipe error");
1356 goto error;
1357 } else if (revents & LPOLLIN) {
1358 int sock;
1359
1360 /* Empty pipe */
1361 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1362 if (size_ret < sizeof(sock)) {
1363 PERROR("read apps cmd pipe");
1364 goto error;
1365 }
1366
1367 health_code_update();
1368
1369 /*
1370 * We only monitor the error events of the socket. This
1371 * thread does not handle any incoming data from UST
1372 * (POLLIN).
1373 */
1374 ret = lttng_poll_add(&events, sock,
1375 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1376 if (ret < 0) {
1377 goto error;
1378 }
1379
1380 DBG("Apps with sock %d added to poll set", sock);
1381 }
1382 } else {
1383 /*
1384 * At this point, we know that a registered application made
1385 * the event at poll_wait.
1386 */
1387 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1388 /* Removing from the poll set */
1389 ret = lttng_poll_del(&events, pollfd);
1390 if (ret < 0) {
1391 goto error;
1392 }
1393
1394 /* Socket closed on remote end. */
1395 ust_app_unregister(pollfd);
1396 }
1397 }
1398
1399 health_code_update();
1400 }
1401 }
1402
1403 exit:
1404 error:
1405 lttng_poll_clean(&events);
1406 error_poll_create:
1407 error_testpoint:
1408 utils_close_pipe(apps_cmd_pipe);
1409 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1410
1411 /*
1412 * We don't clean the UST app hash table here since already registered
1413 * applications can still be controlled so let them be until the session
1414 * daemon dies or the applications stop.
1415 */
1416
1417 if (err) {
1418 health_error();
1419 ERR("Health error occurred in %s", __func__);
1420 }
1421 health_unregister(health_sessiond);
1422 DBG("Application communication apps thread cleanup complete");
1423 rcu_thread_offline();
1424 rcu_unregister_thread();
1425 return NULL;
1426 }
1427
1428 /*
1429 * Send a socket to a thread This is called from the dispatch UST registration
1430 * thread once all sockets are set for the application.
1431 *
1432 * The sock value can be invalid, we don't really care, the thread will handle
1433 * it and make the necessary cleanup if so.
1434 *
1435 * On success, return 0 else a negative value being the errno message of the
1436 * write().
1437 */
1438 static int send_socket_to_thread(int fd, int sock)
1439 {
1440 ssize_t ret;
1441
1442 /*
1443 * It's possible that the FD is set as invalid with -1 concurrently just
1444 * before calling this function being a shutdown state of the thread.
1445 */
1446 if (fd < 0) {
1447 ret = -EBADF;
1448 goto error;
1449 }
1450
1451 ret = lttng_write(fd, &sock, sizeof(sock));
1452 if (ret < sizeof(sock)) {
1453 PERROR("write apps pipe %d", fd);
1454 if (ret < 0) {
1455 ret = -errno;
1456 }
1457 goto error;
1458 }
1459
1460 /* All good. Don't send back the write positive ret value. */
1461 ret = 0;
1462 error:
1463 return (int) ret;
1464 }
1465
1466 /*
1467 * Sanitize the wait queue of the dispatch registration thread meaning removing
1468 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1469 * notify socket is never received.
1470 */
1471 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1472 {
1473 int ret, nb_fd = 0, i;
1474 unsigned int fd_added = 0;
1475 struct lttng_poll_event events;
1476 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1477
1478 assert(wait_queue);
1479
1480 lttng_poll_init(&events);
1481
1482 /* Just skip everything for an empty queue. */
1483 if (!wait_queue->count) {
1484 goto end;
1485 }
1486
1487 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1488 if (ret < 0) {
1489 goto error_create;
1490 }
1491
1492 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1493 &wait_queue->head, head) {
1494 assert(wait_node->app);
1495 ret = lttng_poll_add(&events, wait_node->app->sock,
1496 LPOLLHUP | LPOLLERR);
1497 if (ret < 0) {
1498 goto error;
1499 }
1500
1501 fd_added = 1;
1502 }
1503
1504 if (!fd_added) {
1505 goto end;
1506 }
1507
1508 /*
1509 * Poll but don't block so we can quickly identify the faulty events and
1510 * clean them afterwards from the wait queue.
1511 */
1512 ret = lttng_poll_wait(&events, 0);
1513 if (ret < 0) {
1514 goto error;
1515 }
1516 nb_fd = ret;
1517
1518 for (i = 0; i < nb_fd; i++) {
1519 /* Get faulty FD. */
1520 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1521 int pollfd = LTTNG_POLL_GETFD(&events, i);
1522
1523 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1524 &wait_queue->head, head) {
1525 if (pollfd == wait_node->app->sock &&
1526 (revents & (LPOLLHUP | LPOLLERR))) {
1527 cds_list_del(&wait_node->head);
1528 wait_queue->count--;
1529 ust_app_destroy(wait_node->app);
1530 free(wait_node);
1531 break;
1532 }
1533 }
1534 }
1535
1536 if (nb_fd > 0) {
1537 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1538 }
1539
1540 end:
1541 lttng_poll_clean(&events);
1542 return;
1543
1544 error:
1545 lttng_poll_clean(&events);
1546 error_create:
1547 ERR("Unable to sanitize wait queue");
1548 return;
1549 }
1550
1551 /*
1552 * Dispatch request from the registration threads to the application
1553 * communication thread.
1554 */
1555 static void *thread_dispatch_ust_registration(void *data)
1556 {
1557 int ret, err = -1;
1558 struct cds_wfq_node *node;
1559 struct ust_command *ust_cmd = NULL;
1560 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1561 struct ust_reg_wait_queue wait_queue = {
1562 .count = 0,
1563 };
1564
1565 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1566
1567 health_code_update();
1568
1569 CDS_INIT_LIST_HEAD(&wait_queue.head);
1570
1571 DBG("[thread] Dispatch UST command started");
1572
1573 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1574 health_code_update();
1575
1576 /* Atomically prepare the queue futex */
1577 futex_nto1_prepare(&ust_cmd_queue.futex);
1578
1579 do {
1580 struct ust_app *app = NULL;
1581 ust_cmd = NULL;
1582
1583 /*
1584 * Make sure we don't have node(s) that have hung up before receiving
1585 * the notify socket. This is to clean the list in order to avoid
1586 * memory leaks from notify socket that are never seen.
1587 */
1588 sanitize_wait_queue(&wait_queue);
1589
1590 health_code_update();
1591 /* Dequeue command for registration */
1592 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1593 if (node == NULL) {
1594 DBG("Woken up but nothing in the UST command queue");
1595 /* Continue thread execution */
1596 break;
1597 }
1598
1599 ust_cmd = caa_container_of(node, struct ust_command, node);
1600
1601 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1602 " gid:%d sock:%d name:%s (version %d.%d)",
1603 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1604 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1605 ust_cmd->sock, ust_cmd->reg_msg.name,
1606 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1607
1608 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1609 wait_node = zmalloc(sizeof(*wait_node));
1610 if (!wait_node) {
1611 PERROR("zmalloc wait_node dispatch");
1612 ret = close(ust_cmd->sock);
1613 if (ret < 0) {
1614 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1615 }
1616 lttng_fd_put(LTTNG_FD_APPS, 1);
1617 free(ust_cmd);
1618 goto error;
1619 }
1620 CDS_INIT_LIST_HEAD(&wait_node->head);
1621
1622 /* Create application object if socket is CMD. */
1623 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1624 ust_cmd->sock);
1625 if (!wait_node->app) {
1626 ret = close(ust_cmd->sock);
1627 if (ret < 0) {
1628 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1629 }
1630 lttng_fd_put(LTTNG_FD_APPS, 1);
1631 free(wait_node);
1632 free(ust_cmd);
1633 continue;
1634 }
1635 /*
1636 * Add application to the wait queue so we can set the notify
1637 * socket before putting this object in the global ht.
1638 */
1639 cds_list_add(&wait_node->head, &wait_queue.head);
1640 wait_queue.count++;
1641
1642 free(ust_cmd);
1643 /*
1644 * We have to continue here since we don't have the notify
1645 * socket and the application MUST be added to the hash table
1646 * only at that moment.
1647 */
1648 continue;
1649 } else {
1650 /*
1651 * Look for the application in the local wait queue and set the
1652 * notify socket if found.
1653 */
1654 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1655 &wait_queue.head, head) {
1656 health_code_update();
1657 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1658 wait_node->app->notify_sock = ust_cmd->sock;
1659 cds_list_del(&wait_node->head);
1660 wait_queue.count--;
1661 app = wait_node->app;
1662 free(wait_node);
1663 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1664 break;
1665 }
1666 }
1667
1668 /*
1669 * With no application at this stage the received socket is
1670 * basically useless so close it before we free the cmd data
1671 * structure for good.
1672 */
1673 if (!app) {
1674 ret = close(ust_cmd->sock);
1675 if (ret < 0) {
1676 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1677 }
1678 lttng_fd_put(LTTNG_FD_APPS, 1);
1679 }
1680 free(ust_cmd);
1681 }
1682
1683 if (app) {
1684 /*
1685 * @session_lock_list
1686 *
1687 * Lock the global session list so from the register up to the
1688 * registration done message, no thread can see the application
1689 * and change its state.
1690 */
1691 session_lock_list();
1692 rcu_read_lock();
1693
1694 /*
1695 * Add application to the global hash table. This needs to be
1696 * done before the update to the UST registry can locate the
1697 * application.
1698 */
1699 ust_app_add(app);
1700
1701 /* Set app version. This call will print an error if needed. */
1702 (void) ust_app_version(app);
1703
1704 /* Send notify socket through the notify pipe. */
1705 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1706 app->notify_sock);
1707 if (ret < 0) {
1708 rcu_read_unlock();
1709 session_unlock_list();
1710 /*
1711 * No notify thread, stop the UST tracing. However, this is
1712 * not an internal error of the this thread thus setting
1713 * the health error code to a normal exit.
1714 */
1715 err = 0;
1716 goto error;
1717 }
1718
1719 /*
1720 * Update newly registered application with the tracing
1721 * registry info already enabled information.
1722 */
1723 update_ust_app(app->sock);
1724
1725 /*
1726 * Don't care about return value. Let the manage apps threads
1727 * handle app unregistration upon socket close.
1728 */
1729 (void) ust_app_register_done(app->sock);
1730
1731 /*
1732 * Even if the application socket has been closed, send the app
1733 * to the thread and unregistration will take place at that
1734 * place.
1735 */
1736 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
1737 if (ret < 0) {
1738 rcu_read_unlock();
1739 session_unlock_list();
1740 /*
1741 * No apps. thread, stop the UST tracing. However, this is
1742 * not an internal error of the this thread thus setting
1743 * the health error code to a normal exit.
1744 */
1745 err = 0;
1746 goto error;
1747 }
1748
1749 rcu_read_unlock();
1750 session_unlock_list();
1751 }
1752 } while (node != NULL);
1753
1754 health_poll_entry();
1755 /* Futex wait on queue. Blocking call on futex() */
1756 futex_nto1_wait(&ust_cmd_queue.futex);
1757 health_poll_exit();
1758 }
1759 /* Normal exit, no error */
1760 err = 0;
1761
1762 error:
1763 /* Clean up wait queue. */
1764 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1765 &wait_queue.head, head) {
1766 cds_list_del(&wait_node->head);
1767 wait_queue.count--;
1768 free(wait_node);
1769 }
1770
1771 DBG("Dispatch thread dying");
1772 if (err) {
1773 health_error();
1774 ERR("Health error occurred in %s", __func__);
1775 }
1776 health_unregister(health_sessiond);
1777 return NULL;
1778 }
1779
1780 /*
1781 * This thread manage application registration.
1782 */
1783 static void *thread_registration_apps(void *data)
1784 {
1785 int sock = -1, i, ret, pollfd, err = -1;
1786 uint32_t revents, nb_fd;
1787 struct lttng_poll_event events;
1788 /*
1789 * Get allocated in this thread, enqueued to a global queue, dequeued and
1790 * freed in the manage apps thread.
1791 */
1792 struct ust_command *ust_cmd = NULL;
1793
1794 DBG("[thread] Manage application registration started");
1795
1796 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
1797
1798 if (testpoint(sessiond_thread_registration_apps)) {
1799 goto error_testpoint;
1800 }
1801
1802 ret = lttcomm_listen_unix_sock(apps_sock);
1803 if (ret < 0) {
1804 goto error_listen;
1805 }
1806
1807 /*
1808 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1809 * more will be added to this poll set.
1810 */
1811 ret = sessiond_set_thread_pollset(&events, 2);
1812 if (ret < 0) {
1813 goto error_create_poll;
1814 }
1815
1816 /* Add the application registration socket */
1817 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1818 if (ret < 0) {
1819 goto error_poll_add;
1820 }
1821
1822 /* Notify all applications to register */
1823 ret = notify_ust_apps(1);
1824 if (ret < 0) {
1825 ERR("Failed to notify applications or create the wait shared memory.\n"
1826 "Execution continues but there might be problem for already\n"
1827 "running applications that wishes to register.");
1828 }
1829
1830 while (1) {
1831 DBG("Accepting application registration");
1832
1833 /* Inifinite blocking call, waiting for transmission */
1834 restart:
1835 health_poll_entry();
1836 ret = lttng_poll_wait(&events, -1);
1837 health_poll_exit();
1838 if (ret < 0) {
1839 /*
1840 * Restart interrupted system call.
1841 */
1842 if (errno == EINTR) {
1843 goto restart;
1844 }
1845 goto error;
1846 }
1847
1848 nb_fd = ret;
1849
1850 for (i = 0; i < nb_fd; i++) {
1851 health_code_update();
1852
1853 /* Fetch once the poll data */
1854 revents = LTTNG_POLL_GETEV(&events, i);
1855 pollfd = LTTNG_POLL_GETFD(&events, i);
1856
1857 /* Thread quit pipe has been closed. Killing thread. */
1858 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1859 if (ret) {
1860 err = 0;
1861 goto exit;
1862 }
1863
1864 /* Event on the registration socket */
1865 if (pollfd == apps_sock) {
1866 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1867 ERR("Register apps socket poll error");
1868 goto error;
1869 } else if (revents & LPOLLIN) {
1870 sock = lttcomm_accept_unix_sock(apps_sock);
1871 if (sock < 0) {
1872 goto error;
1873 }
1874
1875 /*
1876 * Set socket timeout for both receiving and ending.
1877 * app_socket_timeout is in seconds, whereas
1878 * lttcomm_setsockopt_rcv_timeout and
1879 * lttcomm_setsockopt_snd_timeout expect msec as
1880 * parameter.
1881 */
1882 (void) lttcomm_setsockopt_rcv_timeout(sock,
1883 app_socket_timeout * 1000);
1884 (void) lttcomm_setsockopt_snd_timeout(sock,
1885 app_socket_timeout * 1000);
1886
1887 /*
1888 * Set the CLOEXEC flag. Return code is useless because
1889 * either way, the show must go on.
1890 */
1891 (void) utils_set_fd_cloexec(sock);
1892
1893 /* Create UST registration command for enqueuing */
1894 ust_cmd = zmalloc(sizeof(struct ust_command));
1895 if (ust_cmd == NULL) {
1896 PERROR("ust command zmalloc");
1897 goto error;
1898 }
1899
1900 /*
1901 * Using message-based transmissions to ensure we don't
1902 * have to deal with partially received messages.
1903 */
1904 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1905 if (ret < 0) {
1906 ERR("Exhausted file descriptors allowed for applications.");
1907 free(ust_cmd);
1908 ret = close(sock);
1909 if (ret) {
1910 PERROR("close");
1911 }
1912 sock = -1;
1913 continue;
1914 }
1915
1916 health_code_update();
1917 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
1918 if (ret < 0) {
1919 free(ust_cmd);
1920 /* Close socket of the application. */
1921 ret = close(sock);
1922 if (ret) {
1923 PERROR("close");
1924 }
1925 lttng_fd_put(LTTNG_FD_APPS, 1);
1926 sock = -1;
1927 continue;
1928 }
1929 health_code_update();
1930
1931 ust_cmd->sock = sock;
1932 sock = -1;
1933
1934 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1935 " gid:%d sock:%d name:%s (version %d.%d)",
1936 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1937 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1938 ust_cmd->sock, ust_cmd->reg_msg.name,
1939 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1940
1941 /*
1942 * Lock free enqueue the registration request. The red pill
1943 * has been taken! This apps will be part of the *system*.
1944 */
1945 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1946
1947 /*
1948 * Wake the registration queue futex. Implicit memory
1949 * barrier with the exchange in cds_wfq_enqueue.
1950 */
1951 futex_nto1_wake(&ust_cmd_queue.futex);
1952 }
1953 }
1954 }
1955 }
1956
1957 exit:
1958 error:
1959 if (err) {
1960 health_error();
1961 ERR("Health error occurred in %s", __func__);
1962 }
1963
1964 /* Notify that the registration thread is gone */
1965 notify_ust_apps(0);
1966
1967 if (apps_sock >= 0) {
1968 ret = close(apps_sock);
1969 if (ret) {
1970 PERROR("close");
1971 }
1972 }
1973 if (sock >= 0) {
1974 ret = close(sock);
1975 if (ret) {
1976 PERROR("close");
1977 }
1978 lttng_fd_put(LTTNG_FD_APPS, 1);
1979 }
1980 unlink(apps_unix_sock_path);
1981
1982 error_poll_add:
1983 lttng_poll_clean(&events);
1984 error_listen:
1985 error_create_poll:
1986 error_testpoint:
1987 DBG("UST Registration thread cleanup complete");
1988 health_unregister(health_sessiond);
1989
1990 return NULL;
1991 }
1992
1993 /*
1994 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1995 * exec or it will fails.
1996 */
1997 static int spawn_consumer_thread(struct consumer_data *consumer_data)
1998 {
1999 int ret, clock_ret;
2000 struct timespec timeout;
2001
2002 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2003 consumer_data->consumer_thread_is_ready = 0;
2004
2005 /* Setup pthread condition */
2006 ret = pthread_condattr_init(&consumer_data->condattr);
2007 if (ret != 0) {
2008 errno = ret;
2009 PERROR("pthread_condattr_init consumer data");
2010 goto error;
2011 }
2012
2013 /*
2014 * Set the monotonic clock in order to make sure we DO NOT jump in time
2015 * between the clock_gettime() call and the timedwait call. See bug #324
2016 * for a more details and how we noticed it.
2017 */
2018 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2019 if (ret != 0) {
2020 errno = ret;
2021 PERROR("pthread_condattr_setclock consumer data");
2022 goto error;
2023 }
2024
2025 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2026 if (ret != 0) {
2027 errno = ret;
2028 PERROR("pthread_cond_init consumer data");
2029 goto error;
2030 }
2031
2032 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2033 consumer_data);
2034 if (ret != 0) {
2035 PERROR("pthread_create consumer");
2036 ret = -1;
2037 goto error;
2038 }
2039
2040 /* We are about to wait on a pthread condition */
2041 pthread_mutex_lock(&consumer_data->cond_mutex);
2042
2043 /* Get time for sem_timedwait absolute timeout */
2044 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2045 /*
2046 * Set the timeout for the condition timed wait even if the clock gettime
2047 * call fails since we might loop on that call and we want to avoid to
2048 * increment the timeout too many times.
2049 */
2050 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2051
2052 /*
2053 * The following loop COULD be skipped in some conditions so this is why we
2054 * set ret to 0 in order to make sure at least one round of the loop is
2055 * done.
2056 */
2057 ret = 0;
2058
2059 /*
2060 * Loop until the condition is reached or when a timeout is reached. Note
2061 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2062 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2063 * possible. This loop does not take any chances and works with both of
2064 * them.
2065 */
2066 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2067 if (clock_ret < 0) {
2068 PERROR("clock_gettime spawn consumer");
2069 /* Infinite wait for the consumerd thread to be ready */
2070 ret = pthread_cond_wait(&consumer_data->cond,
2071 &consumer_data->cond_mutex);
2072 } else {
2073 ret = pthread_cond_timedwait(&consumer_data->cond,
2074 &consumer_data->cond_mutex, &timeout);
2075 }
2076 }
2077
2078 /* Release the pthread condition */
2079 pthread_mutex_unlock(&consumer_data->cond_mutex);
2080
2081 if (ret != 0) {
2082 errno = ret;
2083 if (ret == ETIMEDOUT) {
2084 /*
2085 * Call has timed out so we kill the kconsumerd_thread and return
2086 * an error.
2087 */
2088 ERR("Condition timed out. The consumer thread was never ready."
2089 " Killing it");
2090 ret = pthread_cancel(consumer_data->thread);
2091 if (ret < 0) {
2092 PERROR("pthread_cancel consumer thread");
2093 }
2094 } else {
2095 PERROR("pthread_cond_wait failed consumer thread");
2096 }
2097 goto error;
2098 }
2099
2100 pthread_mutex_lock(&consumer_data->pid_mutex);
2101 if (consumer_data->pid == 0) {
2102 ERR("Consumerd did not start");
2103 pthread_mutex_unlock(&consumer_data->pid_mutex);
2104 goto error;
2105 }
2106 pthread_mutex_unlock(&consumer_data->pid_mutex);
2107
2108 return 0;
2109
2110 error:
2111 return ret;
2112 }
2113
2114 /*
2115 * Join consumer thread
2116 */
2117 static int join_consumer_thread(struct consumer_data *consumer_data)
2118 {
2119 void *status;
2120
2121 /* Consumer pid must be a real one. */
2122 if (consumer_data->pid > 0) {
2123 int ret;
2124 ret = kill(consumer_data->pid, SIGTERM);
2125 if (ret) {
2126 ERR("Error killing consumer daemon");
2127 return ret;
2128 }
2129 return pthread_join(consumer_data->thread, &status);
2130 } else {
2131 return 0;
2132 }
2133 }
2134
2135 /*
2136 * Fork and exec a consumer daemon (consumerd).
2137 *
2138 * Return pid if successful else -1.
2139 */
2140 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2141 {
2142 int ret;
2143 pid_t pid;
2144 const char *consumer_to_use;
2145 const char *verbosity;
2146 struct stat st;
2147
2148 DBG("Spawning consumerd");
2149
2150 pid = fork();
2151 if (pid == 0) {
2152 /*
2153 * Exec consumerd.
2154 */
2155 if (opt_verbose_consumer) {
2156 verbosity = "--verbose";
2157 } else {
2158 verbosity = "--quiet";
2159 }
2160 switch (consumer_data->type) {
2161 case LTTNG_CONSUMER_KERNEL:
2162 /*
2163 * Find out which consumerd to execute. We will first try the
2164 * 64-bit path, then the sessiond's installation directory, and
2165 * fallback on the 32-bit one,
2166 */
2167 DBG3("Looking for a kernel consumer at these locations:");
2168 DBG3(" 1) %s", consumerd64_bin);
2169 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2170 DBG3(" 3) %s", consumerd32_bin);
2171 if (stat(consumerd64_bin, &st) == 0) {
2172 DBG3("Found location #1");
2173 consumer_to_use = consumerd64_bin;
2174 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
2175 DBG3("Found location #2");
2176 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
2177 } else if (stat(consumerd32_bin, &st) == 0) {
2178 DBG3("Found location #3");
2179 consumer_to_use = consumerd32_bin;
2180 } else {
2181 DBG("Could not find any valid consumerd executable");
2182 break;
2183 }
2184 DBG("Using kernel consumer at: %s", consumer_to_use);
2185 execl(consumer_to_use,
2186 "lttng-consumerd", verbosity, "-k",
2187 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2188 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2189 "--group", tracing_group_name,
2190 NULL);
2191 break;
2192 case LTTNG_CONSUMER64_UST:
2193 {
2194 char *tmpnew = NULL;
2195
2196 if (consumerd64_libdir[0] != '\0') {
2197 char *tmp;
2198 size_t tmplen;
2199
2200 tmp = getenv("LD_LIBRARY_PATH");
2201 if (!tmp) {
2202 tmp = "";
2203 }
2204 tmplen = strlen("LD_LIBRARY_PATH=")
2205 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2206 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2207 if (!tmpnew) {
2208 ret = -ENOMEM;
2209 goto error;
2210 }
2211 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2212 strcat(tmpnew, consumerd64_libdir);
2213 if (tmp[0] != '\0') {
2214 strcat(tmpnew, ":");
2215 strcat(tmpnew, tmp);
2216 }
2217 ret = putenv(tmpnew);
2218 if (ret) {
2219 ret = -errno;
2220 free(tmpnew);
2221 goto error;
2222 }
2223 }
2224 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
2225 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
2226 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2227 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2228 "--group", tracing_group_name,
2229 NULL);
2230 if (consumerd64_libdir[0] != '\0') {
2231 free(tmpnew);
2232 }
2233 if (ret) {
2234 goto error;
2235 }
2236 break;
2237 }
2238 case LTTNG_CONSUMER32_UST:
2239 {
2240 char *tmpnew = NULL;
2241
2242 if (consumerd32_libdir[0] != '\0') {
2243 char *tmp;
2244 size_t tmplen;
2245
2246 tmp = getenv("LD_LIBRARY_PATH");
2247 if (!tmp) {
2248 tmp = "";
2249 }
2250 tmplen = strlen("LD_LIBRARY_PATH=")
2251 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2252 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2253 if (!tmpnew) {
2254 ret = -ENOMEM;
2255 goto error;
2256 }
2257 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2258 strcat(tmpnew, consumerd32_libdir);
2259 if (tmp[0] != '\0') {
2260 strcat(tmpnew, ":");
2261 strcat(tmpnew, tmp);
2262 }
2263 ret = putenv(tmpnew);
2264 if (ret) {
2265 ret = -errno;
2266 free(tmpnew);
2267 goto error;
2268 }
2269 }
2270 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
2271 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
2272 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2273 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2274 "--group", tracing_group_name,
2275 NULL);
2276 if (consumerd32_libdir[0] != '\0') {
2277 free(tmpnew);
2278 }
2279 if (ret) {
2280 goto error;
2281 }
2282 break;
2283 }
2284 default:
2285 PERROR("unknown consumer type");
2286 exit(EXIT_FAILURE);
2287 }
2288 if (errno != 0) {
2289 PERROR("kernel start consumer exec");
2290 }
2291 exit(EXIT_FAILURE);
2292 } else if (pid > 0) {
2293 ret = pid;
2294 } else {
2295 PERROR("start consumer fork");
2296 ret = -errno;
2297 }
2298 error:
2299 return ret;
2300 }
2301
2302 /*
2303 * Spawn the consumerd daemon and session daemon thread.
2304 */
2305 static int start_consumerd(struct consumer_data *consumer_data)
2306 {
2307 int ret;
2308
2309 /*
2310 * Set the listen() state on the socket since there is a possible race
2311 * between the exec() of the consumer daemon and this call if place in the
2312 * consumer thread. See bug #366 for more details.
2313 */
2314 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2315 if (ret < 0) {
2316 goto error;
2317 }
2318
2319 pthread_mutex_lock(&consumer_data->pid_mutex);
2320 if (consumer_data->pid != 0) {
2321 pthread_mutex_unlock(&consumer_data->pid_mutex);
2322 goto end;
2323 }
2324
2325 ret = spawn_consumerd(consumer_data);
2326 if (ret < 0) {
2327 ERR("Spawning consumerd failed");
2328 pthread_mutex_unlock(&consumer_data->pid_mutex);
2329 goto error;
2330 }
2331
2332 /* Setting up the consumer_data pid */
2333 consumer_data->pid = ret;
2334 DBG2("Consumer pid %d", consumer_data->pid);
2335 pthread_mutex_unlock(&consumer_data->pid_mutex);
2336
2337 DBG2("Spawning consumer control thread");
2338 ret = spawn_consumer_thread(consumer_data);
2339 if (ret < 0) {
2340 ERR("Fatal error spawning consumer control thread");
2341 goto error;
2342 }
2343
2344 end:
2345 return 0;
2346
2347 error:
2348 /* Cleanup already created sockets on error. */
2349 if (consumer_data->err_sock >= 0) {
2350 int err;
2351
2352 err = close(consumer_data->err_sock);
2353 if (err < 0) {
2354 PERROR("close consumer data error socket");
2355 }
2356 }
2357 return ret;
2358 }
2359
2360 /*
2361 * Setup necessary data for kernel tracer action.
2362 */
2363 static int init_kernel_tracer(void)
2364 {
2365 int ret;
2366
2367 /* Modprobe lttng kernel modules */
2368 ret = modprobe_lttng_control();
2369 if (ret < 0) {
2370 goto error;
2371 }
2372
2373 /* Open debugfs lttng */
2374 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2375 if (kernel_tracer_fd < 0) {
2376 DBG("Failed to open %s", module_proc_lttng);
2377 ret = -1;
2378 goto error_open;
2379 }
2380
2381 /* Validate kernel version */
2382 ret = kernel_validate_version(kernel_tracer_fd);
2383 if (ret < 0) {
2384 goto error_version;
2385 }
2386
2387 ret = modprobe_lttng_data();
2388 if (ret < 0) {
2389 goto error_modules;
2390 }
2391
2392 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2393 return 0;
2394
2395 error_version:
2396 modprobe_remove_lttng_control();
2397 ret = close(kernel_tracer_fd);
2398 if (ret) {
2399 PERROR("close");
2400 }
2401 kernel_tracer_fd = -1;
2402 return LTTNG_ERR_KERN_VERSION;
2403
2404 error_modules:
2405 ret = close(kernel_tracer_fd);
2406 if (ret) {
2407 PERROR("close");
2408 }
2409
2410 error_open:
2411 modprobe_remove_lttng_control();
2412
2413 error:
2414 WARN("No kernel tracer available");
2415 kernel_tracer_fd = -1;
2416 if (!is_root) {
2417 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2418 } else {
2419 return LTTNG_ERR_KERN_NA;
2420 }
2421 }
2422
2423
2424 /*
2425 * Copy consumer output from the tracing session to the domain session. The
2426 * function also applies the right modification on a per domain basis for the
2427 * trace files destination directory.
2428 *
2429 * Should *NOT* be called with RCU read-side lock held.
2430 */
2431 static int copy_session_consumer(int domain, struct ltt_session *session)
2432 {
2433 int ret;
2434 const char *dir_name;
2435 struct consumer_output *consumer;
2436
2437 assert(session);
2438 assert(session->consumer);
2439
2440 switch (domain) {
2441 case LTTNG_DOMAIN_KERNEL:
2442 DBG3("Copying tracing session consumer output in kernel session");
2443 /*
2444 * XXX: We should audit the session creation and what this function
2445 * does "extra" in order to avoid a destroy since this function is used
2446 * in the domain session creation (kernel and ust) only. Same for UST
2447 * domain.
2448 */
2449 if (session->kernel_session->consumer) {
2450 consumer_destroy_output(session->kernel_session->consumer);
2451 }
2452 session->kernel_session->consumer =
2453 consumer_copy_output(session->consumer);
2454 /* Ease our life a bit for the next part */
2455 consumer = session->kernel_session->consumer;
2456 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2457 break;
2458 case LTTNG_DOMAIN_JUL:
2459 case LTTNG_DOMAIN_UST:
2460 DBG3("Copying tracing session consumer output in UST session");
2461 if (session->ust_session->consumer) {
2462 consumer_destroy_output(session->ust_session->consumer);
2463 }
2464 session->ust_session->consumer =
2465 consumer_copy_output(session->consumer);
2466 /* Ease our life a bit for the next part */
2467 consumer = session->ust_session->consumer;
2468 dir_name = DEFAULT_UST_TRACE_DIR;
2469 break;
2470 default:
2471 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2472 goto error;
2473 }
2474
2475 /* Append correct directory to subdir */
2476 strncat(consumer->subdir, dir_name,
2477 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2478 DBG3("Copy session consumer subdir %s", consumer->subdir);
2479
2480 ret = LTTNG_OK;
2481
2482 error:
2483 return ret;
2484 }
2485
2486 /*
2487 * Create an UST session and add it to the session ust list.
2488 *
2489 * Should *NOT* be called with RCU read-side lock held.
2490 */
2491 static int create_ust_session(struct ltt_session *session,
2492 struct lttng_domain *domain)
2493 {
2494 int ret;
2495 struct ltt_ust_session *lus = NULL;
2496
2497 assert(session);
2498 assert(domain);
2499 assert(session->consumer);
2500
2501 switch (domain->type) {
2502 case LTTNG_DOMAIN_JUL:
2503 case LTTNG_DOMAIN_UST:
2504 break;
2505 default:
2506 ERR("Unknown UST domain on create session %d", domain->type);
2507 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2508 goto error;
2509 }
2510
2511 DBG("Creating UST session");
2512
2513 lus = trace_ust_create_session(session->id);
2514 if (lus == NULL) {
2515 ret = LTTNG_ERR_UST_SESS_FAIL;
2516 goto error;
2517 }
2518
2519 lus->uid = session->uid;
2520 lus->gid = session->gid;
2521 lus->output_traces = session->output_traces;
2522 lus->snapshot_mode = session->snapshot_mode;
2523 lus->live_timer_interval = session->live_timer;
2524 session->ust_session = lus;
2525
2526 /* Copy session output to the newly created UST session */
2527 ret = copy_session_consumer(domain->type, session);
2528 if (ret != LTTNG_OK) {
2529 goto error;
2530 }
2531
2532 return LTTNG_OK;
2533
2534 error:
2535 free(lus);
2536 session->ust_session = NULL;
2537 return ret;
2538 }
2539
2540 /*
2541 * Create a kernel tracer session then create the default channel.
2542 */
2543 static int create_kernel_session(struct ltt_session *session)
2544 {
2545 int ret;
2546
2547 DBG("Creating kernel session");
2548
2549 ret = kernel_create_session(session, kernel_tracer_fd);
2550 if (ret < 0) {
2551 ret = LTTNG_ERR_KERN_SESS_FAIL;
2552 goto error;
2553 }
2554
2555 /* Code flow safety */
2556 assert(session->kernel_session);
2557
2558 /* Copy session output to the newly created Kernel session */
2559 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2560 if (ret != LTTNG_OK) {
2561 goto error;
2562 }
2563
2564 /* Create directory(ies) on local filesystem. */
2565 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2566 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2567 ret = run_as_mkdir_recursive(
2568 session->kernel_session->consumer->dst.trace_path,
2569 S_IRWXU | S_IRWXG, session->uid, session->gid);
2570 if (ret < 0) {
2571 if (ret != -EEXIST) {
2572 ERR("Trace directory creation error");
2573 goto error;
2574 }
2575 }
2576 }
2577
2578 session->kernel_session->uid = session->uid;
2579 session->kernel_session->gid = session->gid;
2580 session->kernel_session->output_traces = session->output_traces;
2581 session->kernel_session->snapshot_mode = session->snapshot_mode;
2582
2583 return LTTNG_OK;
2584
2585 error:
2586 trace_kernel_destroy_session(session->kernel_session);
2587 session->kernel_session = NULL;
2588 return ret;
2589 }
2590
2591 /*
2592 * Count number of session permitted by uid/gid.
2593 */
2594 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2595 {
2596 unsigned int i = 0;
2597 struct ltt_session *session;
2598
2599 DBG("Counting number of available session for UID %d GID %d",
2600 uid, gid);
2601 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2602 /*
2603 * Only list the sessions the user can control.
2604 */
2605 if (!session_access_ok(session, uid, gid)) {
2606 continue;
2607 }
2608 i++;
2609 }
2610 return i;
2611 }
2612
2613 /*
2614 * Process the command requested by the lttng client within the command
2615 * context structure. This function make sure that the return structure (llm)
2616 * is set and ready for transmission before returning.
2617 *
2618 * Return any error encountered or 0 for success.
2619 *
2620 * "sock" is only used for special-case var. len data.
2621 *
2622 * Should *NOT* be called with RCU read-side lock held.
2623 */
2624 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2625 int *sock_error)
2626 {
2627 int ret = LTTNG_OK;
2628 int need_tracing_session = 1;
2629 int need_domain;
2630
2631 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2632
2633 *sock_error = 0;
2634
2635 switch (cmd_ctx->lsm->cmd_type) {
2636 case LTTNG_CREATE_SESSION:
2637 case LTTNG_CREATE_SESSION_SNAPSHOT:
2638 case LTTNG_CREATE_SESSION_LIVE:
2639 case LTTNG_DESTROY_SESSION:
2640 case LTTNG_LIST_SESSIONS:
2641 case LTTNG_LIST_DOMAINS:
2642 case LTTNG_START_TRACE:
2643 case LTTNG_STOP_TRACE:
2644 case LTTNG_DATA_PENDING:
2645 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2646 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2647 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2648 case LTTNG_SNAPSHOT_RECORD:
2649 need_domain = 0;
2650 break;
2651 default:
2652 need_domain = 1;
2653 }
2654
2655 if (opt_no_kernel && need_domain
2656 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2657 if (!is_root) {
2658 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2659 } else {
2660 ret = LTTNG_ERR_KERN_NA;
2661 }
2662 goto error;
2663 }
2664
2665 /* Deny register consumer if we already have a spawned consumer. */
2666 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2667 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2668 if (kconsumer_data.pid > 0) {
2669 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2670 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2671 goto error;
2672 }
2673 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2674 }
2675
2676 /*
2677 * Check for command that don't needs to allocate a returned payload. We do
2678 * this here so we don't have to make the call for no payload at each
2679 * command.
2680 */
2681 switch(cmd_ctx->lsm->cmd_type) {
2682 case LTTNG_LIST_SESSIONS:
2683 case LTTNG_LIST_TRACEPOINTS:
2684 case LTTNG_LIST_TRACEPOINT_FIELDS:
2685 case LTTNG_LIST_DOMAINS:
2686 case LTTNG_LIST_CHANNELS:
2687 case LTTNG_LIST_EVENTS:
2688 break;
2689 default:
2690 /* Setup lttng message with no payload */
2691 ret = setup_lttng_msg(cmd_ctx, 0);
2692 if (ret < 0) {
2693 /* This label does not try to unlock the session */
2694 goto init_setup_error;
2695 }
2696 }
2697
2698 /* Commands that DO NOT need a session. */
2699 switch (cmd_ctx->lsm->cmd_type) {
2700 case LTTNG_CREATE_SESSION:
2701 case LTTNG_CREATE_SESSION_SNAPSHOT:
2702 case LTTNG_CREATE_SESSION_LIVE:
2703 case LTTNG_CALIBRATE:
2704 case LTTNG_LIST_SESSIONS:
2705 case LTTNG_LIST_TRACEPOINTS:
2706 case LTTNG_LIST_TRACEPOINT_FIELDS:
2707 need_tracing_session = 0;
2708 break;
2709 default:
2710 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2711 /*
2712 * We keep the session list lock across _all_ commands
2713 * for now, because the per-session lock does not
2714 * handle teardown properly.
2715 */
2716 session_lock_list();
2717 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2718 if (cmd_ctx->session == NULL) {
2719 ret = LTTNG_ERR_SESS_NOT_FOUND;
2720 goto error;
2721 } else {
2722 /* Acquire lock for the session */
2723 session_lock(cmd_ctx->session);
2724 }
2725 break;
2726 }
2727
2728 if (!need_domain) {
2729 goto skip_domain;
2730 }
2731
2732 /*
2733 * Check domain type for specific "pre-action".
2734 */
2735 switch (cmd_ctx->lsm->domain.type) {
2736 case LTTNG_DOMAIN_KERNEL:
2737 if (!is_root) {
2738 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2739 goto error;
2740 }
2741
2742 /* Kernel tracer check */
2743 if (kernel_tracer_fd == -1) {
2744 /* Basically, load kernel tracer modules */
2745 ret = init_kernel_tracer();
2746 if (ret != 0) {
2747 goto error;
2748 }
2749 }
2750
2751 /* Consumer is in an ERROR state. Report back to client */
2752 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
2753 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2754 goto error;
2755 }
2756
2757 /* Need a session for kernel command */
2758 if (need_tracing_session) {
2759 if (cmd_ctx->session->kernel_session == NULL) {
2760 ret = create_kernel_session(cmd_ctx->session);
2761 if (ret < 0) {
2762 ret = LTTNG_ERR_KERN_SESS_FAIL;
2763 goto error;
2764 }
2765 }
2766
2767 /* Start the kernel consumer daemon */
2768 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2769 if (kconsumer_data.pid == 0 &&
2770 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2771 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2772 ret = start_consumerd(&kconsumer_data);
2773 if (ret < 0) {
2774 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2775 goto error;
2776 }
2777 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
2778 } else {
2779 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2780 }
2781
2782 /*
2783 * The consumer was just spawned so we need to add the socket to
2784 * the consumer output of the session if exist.
2785 */
2786 ret = consumer_create_socket(&kconsumer_data,
2787 cmd_ctx->session->kernel_session->consumer);
2788 if (ret < 0) {
2789 goto error;
2790 }
2791 }
2792
2793 break;
2794 case LTTNG_DOMAIN_JUL:
2795 case LTTNG_DOMAIN_UST:
2796 {
2797 if (!ust_app_supported()) {
2798 ret = LTTNG_ERR_NO_UST;
2799 goto error;
2800 }
2801 /* Consumer is in an ERROR state. Report back to client */
2802 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
2803 ret = LTTNG_ERR_NO_USTCONSUMERD;
2804 goto error;
2805 }
2806
2807 if (need_tracing_session) {
2808 /* Create UST session if none exist. */
2809 if (cmd_ctx->session->ust_session == NULL) {
2810 ret = create_ust_session(cmd_ctx->session,
2811 &cmd_ctx->lsm->domain);
2812 if (ret != LTTNG_OK) {
2813 goto error;
2814 }
2815 }
2816
2817 /* Start the UST consumer daemons */
2818 /* 64-bit */
2819 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
2820 if (consumerd64_bin[0] != '\0' &&
2821 ustconsumer64_data.pid == 0 &&
2822 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2823 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2824 ret = start_consumerd(&ustconsumer64_data);
2825 if (ret < 0) {
2826 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
2827 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2828 goto error;
2829 }
2830
2831 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
2832 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2833 } else {
2834 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2835 }
2836
2837 /*
2838 * Setup socket for consumer 64 bit. No need for atomic access
2839 * since it was set above and can ONLY be set in this thread.
2840 */
2841 ret = consumer_create_socket(&ustconsumer64_data,
2842 cmd_ctx->session->ust_session->consumer);
2843 if (ret < 0) {
2844 goto error;
2845 }
2846
2847 /* 32-bit */
2848 if (consumerd32_bin[0] != '\0' &&
2849 ustconsumer32_data.pid == 0 &&
2850 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2851 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2852 ret = start_consumerd(&ustconsumer32_data);
2853 if (ret < 0) {
2854 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
2855 uatomic_set(&ust_consumerd32_fd, -EINVAL);
2856 goto error;
2857 }
2858
2859 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
2860 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2861 } else {
2862 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2863 }
2864
2865 /*
2866 * Setup socket for consumer 64 bit. No need for atomic access
2867 * since it was set above and can ONLY be set in this thread.
2868 */
2869 ret = consumer_create_socket(&ustconsumer32_data,
2870 cmd_ctx->session->ust_session->consumer);
2871 if (ret < 0) {
2872 goto error;
2873 }
2874 }
2875 break;
2876 }
2877 default:
2878 break;
2879 }
2880 skip_domain:
2881
2882 /* Validate consumer daemon state when start/stop trace command */
2883 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2884 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2885 switch (cmd_ctx->lsm->domain.type) {
2886 case LTTNG_DOMAIN_JUL:
2887 case LTTNG_DOMAIN_UST:
2888 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
2889 ret = LTTNG_ERR_NO_USTCONSUMERD;
2890 goto error;
2891 }
2892 break;
2893 case LTTNG_DOMAIN_KERNEL:
2894 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
2895 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2896 goto error;
2897 }
2898 break;
2899 }
2900 }
2901
2902 /*
2903 * Check that the UID or GID match that of the tracing session.
2904 * The root user can interact with all sessions.
2905 */
2906 if (need_tracing_session) {
2907 if (!session_access_ok(cmd_ctx->session,
2908 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2909 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
2910 ret = LTTNG_ERR_EPERM;
2911 goto error;
2912 }
2913 }
2914
2915 /*
2916 * Send relayd information to consumer as soon as we have a domain and a
2917 * session defined.
2918 */
2919 if (cmd_ctx->session && need_domain) {
2920 /*
2921 * Setup relayd if not done yet. If the relayd information was already
2922 * sent to the consumer, this call will gracefully return.
2923 */
2924 ret = cmd_setup_relayd(cmd_ctx->session);
2925 if (ret != LTTNG_OK) {
2926 goto error;
2927 }
2928 }
2929
2930 /* Process by command type */
2931 switch (cmd_ctx->lsm->cmd_type) {
2932 case LTTNG_ADD_CONTEXT:
2933 {
2934 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2935 cmd_ctx->lsm->u.context.channel_name,
2936 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
2937 break;
2938 }
2939 case LTTNG_DISABLE_CHANNEL:
2940 {
2941 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2942 cmd_ctx->lsm->u.disable.channel_name);
2943 break;
2944 }
2945 case LTTNG_DISABLE_EVENT:
2946 {
2947 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2948 cmd_ctx->lsm->u.disable.channel_name,
2949 cmd_ctx->lsm->u.disable.name);
2950 break;
2951 }
2952 case LTTNG_DISABLE_ALL_EVENT:
2953 {
2954 DBG("Disabling all events");
2955
2956 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2957 cmd_ctx->lsm->u.disable.channel_name);
2958 break;
2959 }
2960 case LTTNG_ENABLE_CHANNEL:
2961 {
2962 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2963 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
2964 break;
2965 }
2966 case LTTNG_ENABLE_EVENT:
2967 {
2968 struct lttng_event_exclusion *exclusion = NULL;
2969 struct lttng_filter_bytecode *bytecode = NULL;
2970
2971 /* Handle exclusion events and receive it from the client. */
2972 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
2973 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
2974
2975 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
2976 (count * LTTNG_SYMBOL_NAME_LEN));
2977 if (!exclusion) {
2978 ret = LTTNG_ERR_EXCLUSION_NOMEM;
2979 goto error;
2980 }
2981
2982 DBG("Receiving var len exclusion event list from client ...");
2983 exclusion->count = count;
2984 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
2985 count * LTTNG_SYMBOL_NAME_LEN);
2986 if (ret <= 0) {
2987 DBG("Nothing recv() from client var len data... continuing");
2988 *sock_error = 1;
2989 free(exclusion);
2990 ret = LTTNG_ERR_EXCLUSION_INVAL;
2991 goto error;
2992 }
2993 }
2994
2995 /* Handle filter and get bytecode from client. */
2996 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
2997 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
2998
2999 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3000 ret = LTTNG_ERR_FILTER_INVAL;
3001 free(exclusion);
3002 goto error;
3003 }
3004
3005 bytecode = zmalloc(bytecode_len);
3006 if (!bytecode) {
3007 free(exclusion);
3008 ret = LTTNG_ERR_FILTER_NOMEM;
3009 goto error;
3010 }
3011
3012 /* Receive var. len. data */
3013 DBG("Receiving var len filter's bytecode from client ...");
3014 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3015 if (ret <= 0) {
3016 DBG("Nothing recv() from client car len data... continuing");
3017 *sock_error = 1;
3018 free(bytecode);
3019 free(exclusion);
3020 ret = LTTNG_ERR_FILTER_INVAL;
3021 goto error;
3022 }
3023
3024 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3025 free(bytecode);
3026 free(exclusion);
3027 ret = LTTNG_ERR_FILTER_INVAL;
3028 goto error;
3029 }
3030 }
3031
3032 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3033 cmd_ctx->lsm->u.enable.channel_name,
3034 &cmd_ctx->lsm->u.enable.event, bytecode, exclusion,
3035 kernel_poll_pipe[1]);
3036 break;
3037 }
3038 case LTTNG_ENABLE_ALL_EVENT:
3039 {
3040 DBG("Enabling all events");
3041
3042 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
3043 cmd_ctx->lsm->u.enable.channel_name,
3044 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
3045 break;
3046 }
3047 case LTTNG_LIST_TRACEPOINTS:
3048 {
3049 struct lttng_event *events;
3050 ssize_t nb_events;
3051
3052 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3053 if (nb_events < 0) {
3054 /* Return value is a negative lttng_error_code. */
3055 ret = -nb_events;
3056 goto error;
3057 }
3058
3059 /*
3060 * Setup lttng message with payload size set to the event list size in
3061 * bytes and then copy list into the llm payload.
3062 */
3063 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3064 if (ret < 0) {
3065 free(events);
3066 goto setup_error;
3067 }
3068
3069 /* Copy event list into message payload */
3070 memcpy(cmd_ctx->llm->payload, events,
3071 sizeof(struct lttng_event) * nb_events);
3072
3073 free(events);
3074
3075 ret = LTTNG_OK;
3076 break;
3077 }
3078 case LTTNG_LIST_TRACEPOINT_FIELDS:
3079 {
3080 struct lttng_event_field *fields;
3081 ssize_t nb_fields;
3082
3083 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3084 &fields);
3085 if (nb_fields < 0) {
3086 /* Return value is a negative lttng_error_code. */
3087 ret = -nb_fields;
3088 goto error;
3089 }
3090
3091 /*
3092 * Setup lttng message with payload size set to the event list size in
3093 * bytes and then copy list into the llm payload.
3094 */
3095 ret = setup_lttng_msg(cmd_ctx,
3096 sizeof(struct lttng_event_field) * nb_fields);
3097 if (ret < 0) {
3098 free(fields);
3099 goto setup_error;
3100 }
3101
3102 /* Copy event list into message payload */
3103 memcpy(cmd_ctx->llm->payload, fields,
3104 sizeof(struct lttng_event_field) * nb_fields);
3105
3106 free(fields);
3107
3108 ret = LTTNG_OK;
3109 break;
3110 }
3111 case LTTNG_SET_CONSUMER_URI:
3112 {
3113 size_t nb_uri, len;
3114 struct lttng_uri *uris;
3115
3116 nb_uri = cmd_ctx->lsm->u.uri.size;
3117 len = nb_uri * sizeof(struct lttng_uri);
3118
3119 if (nb_uri == 0) {
3120 ret = LTTNG_ERR_INVALID;
3121 goto error;
3122 }
3123
3124 uris = zmalloc(len);
3125 if (uris == NULL) {
3126 ret = LTTNG_ERR_FATAL;
3127 goto error;
3128 }
3129
3130 /* Receive variable len data */
3131 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3132 ret = lttcomm_recv_unix_sock(sock, uris, len);
3133 if (ret <= 0) {
3134 DBG("No URIs received from client... continuing");
3135 *sock_error = 1;
3136 ret = LTTNG_ERR_SESSION_FAIL;
3137 free(uris);
3138 goto error;
3139 }
3140
3141 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3142 nb_uri, uris);
3143 if (ret != LTTNG_OK) {
3144 free(uris);
3145 goto error;
3146 }
3147
3148 /*
3149 * XXX: 0 means that this URI should be applied on the session. Should
3150 * be a DOMAIN enuam.
3151 */
3152 if (cmd_ctx->lsm->domain.type == 0) {
3153 /* Add the URI for the UST session if a consumer is present. */
3154 if (cmd_ctx->session->ust_session &&
3155 cmd_ctx->session->ust_session->consumer) {
3156 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
3157 nb_uri, uris);
3158 } else if (cmd_ctx->session->kernel_session &&
3159 cmd_ctx->session->kernel_session->consumer) {
3160 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
3161 cmd_ctx->session, nb_uri, uris);
3162 }
3163 }
3164
3165 free(uris);
3166
3167 break;
3168 }
3169 case LTTNG_START_TRACE:
3170 {
3171 ret = cmd_start_trace(cmd_ctx->session);
3172 break;
3173 }
3174 case LTTNG_STOP_TRACE:
3175 {
3176 ret = cmd_stop_trace(cmd_ctx->session);
3177 break;
3178 }
3179 case LTTNG_CREATE_SESSION:
3180 {
3181 size_t nb_uri, len;
3182 struct lttng_uri *uris = NULL;
3183
3184 nb_uri = cmd_ctx->lsm->u.uri.size;
3185 len = nb_uri * sizeof(struct lttng_uri);
3186
3187 if (nb_uri > 0) {
3188 uris = zmalloc(len);
3189 if (uris == NULL) {
3190 ret = LTTNG_ERR_FATAL;
3191 goto error;
3192 }
3193
3194 /* Receive variable len data */
3195 DBG("Waiting for %zu URIs from client ...", nb_uri);
3196 ret = lttcomm_recv_unix_sock(sock, uris, len);
3197 if (ret <= 0) {
3198 DBG("No URIs received from client... continuing");
3199 *sock_error = 1;
3200 ret = LTTNG_ERR_SESSION_FAIL;
3201 free(uris);
3202 goto error;
3203 }
3204
3205 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3206 DBG("Creating session with ONE network URI is a bad call");
3207 ret = LTTNG_ERR_SESSION_FAIL;
3208 free(uris);
3209 goto error;
3210 }
3211 }
3212
3213 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
3214 &cmd_ctx->creds, 0);
3215
3216 free(uris);
3217
3218 break;
3219 }
3220 case LTTNG_DESTROY_SESSION:
3221 {
3222 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
3223
3224 /* Set session to NULL so we do not unlock it after free. */
3225 cmd_ctx->session = NULL;
3226 break;
3227 }
3228 case LTTNG_LIST_DOMAINS:
3229 {
3230 ssize_t nb_dom;
3231 struct lttng_domain *domains;
3232
3233 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3234 if (nb_dom < 0) {
3235 /* Return value is a negative lttng_error_code. */
3236 ret = -nb_dom;
3237 goto error;
3238 }
3239
3240 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
3241 if (ret < 0) {
3242 free(domains);
3243 goto setup_error;
3244 }
3245
3246 /* Copy event list into message payload */
3247 memcpy(cmd_ctx->llm->payload, domains,
3248 nb_dom * sizeof(struct lttng_domain));
3249
3250 free(domains);
3251
3252 ret = LTTNG_OK;
3253 break;
3254 }
3255 case LTTNG_LIST_CHANNELS:
3256 {
3257 int nb_chan;
3258 struct lttng_channel *channels;
3259
3260 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3261 cmd_ctx->session, &channels);
3262 if (nb_chan < 0) {
3263 /* Return value is a negative lttng_error_code. */
3264 ret = -nb_chan;
3265 goto error;
3266 }
3267
3268 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
3269 if (ret < 0) {
3270 free(channels);
3271 goto setup_error;
3272 }
3273
3274 /* Copy event list into message payload */
3275 memcpy(cmd_ctx->llm->payload, channels,
3276 nb_chan * sizeof(struct lttng_channel));
3277
3278 free(channels);
3279
3280 ret = LTTNG_OK;
3281 break;
3282 }
3283 case LTTNG_LIST_EVENTS:
3284 {
3285 ssize_t nb_event;
3286 struct lttng_event *events = NULL;
3287
3288 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3289 cmd_ctx->lsm->u.list.channel_name, &events);
3290 if (nb_event < 0) {
3291 /* Return value is a negative lttng_error_code. */
3292 ret = -nb_event;
3293 goto error;
3294 }
3295
3296 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
3297 if (ret < 0) {
3298 free(events);
3299 goto setup_error;
3300 }
3301
3302 /* Copy event list into message payload */
3303 memcpy(cmd_ctx->llm->payload, events,
3304 nb_event * sizeof(struct lttng_event));
3305
3306 free(events);
3307
3308 ret = LTTNG_OK;
3309 break;
3310 }
3311 case LTTNG_LIST_SESSIONS:
3312 {
3313 unsigned int nr_sessions;
3314
3315 session_lock_list();
3316 nr_sessions = lttng_sessions_count(
3317 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3318 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3319
3320 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
3321 if (ret < 0) {
3322 session_unlock_list();
3323 goto setup_error;
3324 }
3325
3326 /* Filled the session array */
3327 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
3328 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3329 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3330
3331 session_unlock_list();
3332
3333 ret = LTTNG_OK;
3334 break;
3335 }
3336 case LTTNG_CALIBRATE:
3337 {
3338 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3339 &cmd_ctx->lsm->u.calibrate);
3340 break;
3341 }
3342 case LTTNG_REGISTER_CONSUMER:
3343 {
3344 struct consumer_data *cdata;
3345
3346 switch (cmd_ctx->lsm->domain.type) {
3347 case LTTNG_DOMAIN_KERNEL:
3348 cdata = &kconsumer_data;
3349 break;
3350 default:
3351 ret = LTTNG_ERR_UND;
3352 goto error;
3353 }
3354
3355 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3356 cmd_ctx->lsm->u.reg.path, cdata);
3357 break;
3358 }
3359 case LTTNG_DATA_PENDING:
3360 {
3361 ret = cmd_data_pending(cmd_ctx->session);
3362 break;
3363 }
3364 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3365 {
3366 struct lttcomm_lttng_output_id reply;
3367
3368 ret = cmd_snapshot_add_output(cmd_ctx->session,
3369 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3370 if (ret != LTTNG_OK) {
3371 goto error;
3372 }
3373
3374 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3375 if (ret < 0) {
3376 goto setup_error;
3377 }
3378
3379 /* Copy output list into message payload */
3380 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3381 ret = LTTNG_OK;
3382 break;
3383 }
3384 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3385 {
3386 ret = cmd_snapshot_del_output(cmd_ctx->session,
3387 &cmd_ctx->lsm->u.snapshot_output.output);
3388 break;
3389 }
3390 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3391 {
3392 ssize_t nb_output;
3393 struct lttng_snapshot_output *outputs = NULL;
3394
3395 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3396 if (nb_output < 0) {
3397 ret = -nb_output;
3398 goto error;
3399 }
3400
3401 ret = setup_lttng_msg(cmd_ctx,
3402 nb_output * sizeof(struct lttng_snapshot_output));
3403 if (ret < 0) {
3404 free(outputs);
3405 goto setup_error;
3406 }
3407
3408 if (outputs) {
3409 /* Copy output list into message payload */
3410 memcpy(cmd_ctx->llm->payload, outputs,
3411 nb_output * sizeof(struct lttng_snapshot_output));
3412 free(outputs);
3413 }
3414
3415 ret = LTTNG_OK;
3416 break;
3417 }
3418 case LTTNG_SNAPSHOT_RECORD:
3419 {
3420 ret = cmd_snapshot_record(cmd_ctx->session,
3421 &cmd_ctx->lsm->u.snapshot_record.output,
3422 cmd_ctx->lsm->u.snapshot_record.wait);
3423 break;
3424 }
3425 case LTTNG_CREATE_SESSION_SNAPSHOT:
3426 {
3427 size_t nb_uri, len;
3428 struct lttng_uri *uris = NULL;
3429
3430 nb_uri = cmd_ctx->lsm->u.uri.size;
3431 len = nb_uri * sizeof(struct lttng_uri);
3432
3433 if (nb_uri > 0) {
3434 uris = zmalloc(len);
3435 if (uris == NULL) {
3436 ret = LTTNG_ERR_FATAL;
3437 goto error;
3438 }
3439
3440 /* Receive variable len data */
3441 DBG("Waiting for %zu URIs from client ...", nb_uri);
3442 ret = lttcomm_recv_unix_sock(sock, uris, len);
3443 if (ret <= 0) {
3444 DBG("No URIs received from client... continuing");
3445 *sock_error = 1;
3446 ret = LTTNG_ERR_SESSION_FAIL;
3447 free(uris);
3448 goto error;
3449 }
3450
3451 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3452 DBG("Creating session with ONE network URI is a bad call");
3453 ret = LTTNG_ERR_SESSION_FAIL;
3454 free(uris);
3455 goto error;
3456 }
3457 }
3458
3459 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3460 nb_uri, &cmd_ctx->creds);
3461 free(uris);
3462 break;
3463 }
3464 case LTTNG_CREATE_SESSION_LIVE:
3465 {
3466 size_t nb_uri, len;
3467 struct lttng_uri *uris = NULL;
3468
3469 nb_uri = cmd_ctx->lsm->u.uri.size;
3470 len = nb_uri * sizeof(struct lttng_uri);
3471
3472 if (nb_uri > 0) {
3473 uris = zmalloc(len);
3474 if (uris == NULL) {
3475 ret = LTTNG_ERR_FATAL;
3476 goto error;
3477 }
3478
3479 /* Receive variable len data */
3480 DBG("Waiting for %zu URIs from client ...", nb_uri);
3481 ret = lttcomm_recv_unix_sock(sock, uris, len);
3482 if (ret <= 0) {
3483 DBG("No URIs received from client... continuing");
3484 *sock_error = 1;
3485 ret = LTTNG_ERR_SESSION_FAIL;
3486 free(uris);
3487 goto error;
3488 }
3489
3490 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3491 DBG("Creating session with ONE network URI is a bad call");
3492 ret = LTTNG_ERR_SESSION_FAIL;
3493 free(uris);
3494 goto error;
3495 }
3496 }
3497
3498 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3499 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3500 free(uris);
3501 break;
3502 }
3503 default:
3504 ret = LTTNG_ERR_UND;
3505 break;
3506 }
3507
3508 error:
3509 if (cmd_ctx->llm == NULL) {
3510 DBG("Missing llm structure. Allocating one.");
3511 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3512 goto setup_error;
3513 }
3514 }
3515 /* Set return code */
3516 cmd_ctx->llm->ret_code = ret;
3517 setup_error:
3518 if (cmd_ctx->session) {
3519 session_unlock(cmd_ctx->session);
3520 }
3521 if (need_tracing_session) {
3522 session_unlock_list();
3523 }
3524 init_setup_error:
3525 return ret;
3526 }
3527
3528 /*
3529 * Thread managing health check socket.
3530 */
3531 static void *thread_manage_health(void *data)
3532 {
3533 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3534 uint32_t revents, nb_fd;
3535 struct lttng_poll_event events;
3536 struct health_comm_msg msg;
3537 struct health_comm_reply reply;
3538
3539 DBG("[thread] Manage health check started");
3540
3541 rcu_register_thread();
3542
3543 /* We might hit an error path before this is created. */
3544 lttng_poll_init(&events);
3545
3546 /* Create unix socket */
3547 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3548 if (sock < 0) {
3549 ERR("Unable to create health check Unix socket");
3550 ret = -1;
3551 goto error;
3552 }
3553
3554 if (is_root) {
3555 /* lttng health client socket path permissions */
3556 ret = chown(health_unix_sock_path, 0,
3557 utils_get_group_id(tracing_group_name));
3558 if (ret < 0) {
3559 ERR("Unable to set group on %s", health_unix_sock_path);
3560 PERROR("chown");
3561 ret = -1;
3562 goto error;
3563 }
3564
3565 ret = chmod(health_unix_sock_path,
3566 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3567 if (ret < 0) {
3568 ERR("Unable to set permissions on %s", health_unix_sock_path);
3569 PERROR("chmod");
3570 ret = -1;
3571 goto error;
3572 }
3573 }
3574
3575 /*
3576 * Set the CLOEXEC flag. Return code is useless because either way, the
3577 * show must go on.
3578 */
3579 (void) utils_set_fd_cloexec(sock);
3580
3581 ret = lttcomm_listen_unix_sock(sock);
3582 if (ret < 0) {
3583 goto error;
3584 }
3585
3586 /*
3587 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3588 * more will be added to this poll set.
3589 */
3590 ret = sessiond_set_thread_pollset(&events, 2);
3591 if (ret < 0) {
3592 goto error;
3593 }
3594
3595 /* Add the application registration socket */
3596 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3597 if (ret < 0) {
3598 goto error;
3599 }
3600
3601 lttng_sessiond_notify_ready();
3602
3603 while (1) {
3604 DBG("Health check ready");
3605
3606 /* Inifinite blocking call, waiting for transmission */
3607 restart:
3608 ret = lttng_poll_wait(&events, -1);
3609 if (ret < 0) {
3610 /*
3611 * Restart interrupted system call.
3612 */
3613 if (errno == EINTR) {
3614 goto restart;
3615 }
3616 goto error;
3617 }
3618
3619 nb_fd = ret;
3620
3621 for (i = 0; i < nb_fd; i++) {
3622 /* Fetch once the poll data */
3623 revents = LTTNG_POLL_GETEV(&events, i);
3624 pollfd = LTTNG_POLL_GETFD(&events, i);
3625
3626 /* Thread quit pipe has been closed. Killing thread. */
3627 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3628 if (ret) {
3629 err = 0;
3630 goto exit;
3631 }
3632
3633 /* Event on the registration socket */
3634 if (pollfd == sock) {
3635 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3636 ERR("Health socket poll error");
3637 goto error;
3638 }
3639 }
3640 }
3641
3642 new_sock = lttcomm_accept_unix_sock(sock);
3643 if (new_sock < 0) {
3644 goto error;
3645 }
3646
3647 /*
3648 * Set the CLOEXEC flag. Return code is useless because either way, the
3649 * show must go on.
3650 */
3651 (void) utils_set_fd_cloexec(new_sock);
3652
3653 DBG("Receiving data from client for health...");
3654 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3655 if (ret <= 0) {
3656 DBG("Nothing recv() from client... continuing");
3657 ret = close(new_sock);
3658 if (ret) {
3659 PERROR("close");
3660 }
3661 new_sock = -1;
3662 continue;
3663 }
3664
3665 rcu_thread_online();
3666
3667 reply.ret_code = 0;
3668 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
3669 /*
3670 * health_check_state returns 0 if health is
3671 * bad.
3672 */
3673 if (!health_check_state(health_sessiond, i)) {
3674 reply.ret_code |= 1ULL << i;
3675 }
3676 }
3677
3678 DBG2("Health check return value %" PRIx64, reply.ret_code);
3679
3680 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3681 if (ret < 0) {
3682 ERR("Failed to send health data back to client");
3683 }
3684
3685 /* End of transmission */
3686 ret = close(new_sock);
3687 if (ret) {
3688 PERROR("close");
3689 }
3690 new_sock = -1;
3691 }
3692
3693 exit:
3694 error:
3695 if (err) {
3696 ERR("Health error occurred in %s", __func__);
3697 }
3698 DBG("Health check thread dying");
3699 unlink(health_unix_sock_path);
3700 if (sock >= 0) {
3701 ret = close(sock);
3702 if (ret) {
3703 PERROR("close");
3704 }
3705 }
3706
3707 lttng_poll_clean(&events);
3708
3709 rcu_unregister_thread();
3710 return NULL;
3711 }
3712
3713 /*
3714 * This thread manage all clients request using the unix client socket for
3715 * communication.
3716 */
3717 static void *thread_manage_clients(void *data)
3718 {
3719 int sock = -1, ret, i, pollfd, err = -1;
3720 int sock_error;
3721 uint32_t revents, nb_fd;
3722 struct command_ctx *cmd_ctx = NULL;
3723 struct lttng_poll_event events;
3724
3725 DBG("[thread] Manage client started");
3726
3727 rcu_register_thread();
3728
3729 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
3730
3731 health_code_update();
3732
3733 ret = lttcomm_listen_unix_sock(client_sock);
3734 if (ret < 0) {
3735 goto error_listen;
3736 }
3737
3738 /*
3739 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3740 * more will be added to this poll set.
3741 */
3742 ret = sessiond_set_thread_pollset(&events, 2);
3743 if (ret < 0) {
3744 goto error_create_poll;
3745 }
3746
3747 /* Add the application registration socket */
3748 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3749 if (ret < 0) {
3750 goto error;
3751 }
3752
3753 lttng_sessiond_notify_ready();
3754
3755 /* This testpoint is after we signal readiness to the parent. */
3756 if (testpoint(sessiond_thread_manage_clients)) {
3757 goto error;
3758 }
3759
3760 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
3761 goto error;
3762 }
3763
3764 health_code_update();
3765
3766 while (1) {
3767 DBG("Accepting client command ...");
3768
3769 /* Inifinite blocking call, waiting for transmission */
3770 restart:
3771 health_poll_entry();
3772 ret = lttng_poll_wait(&events, -1);
3773 health_poll_exit();
3774 if (ret < 0) {
3775 /*
3776 * Restart interrupted system call.
3777 */
3778 if (errno == EINTR) {
3779 goto restart;
3780 }
3781 goto error;
3782 }
3783
3784 nb_fd = ret;
3785
3786 for (i = 0; i < nb_fd; i++) {
3787 /* Fetch once the poll data */
3788 revents = LTTNG_POLL_GETEV(&events, i);
3789 pollfd = LTTNG_POLL_GETFD(&events, i);
3790
3791 health_code_update();
3792
3793 /* Thread quit pipe has been closed. Killing thread. */
3794 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3795 if (ret) {
3796 err = 0;
3797 goto exit;
3798 }
3799
3800 /* Event on the registration socket */
3801 if (pollfd == client_sock) {
3802 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3803 ERR("Client socket poll error");
3804 goto error;
3805 }
3806 }
3807 }
3808
3809 DBG("Wait for client response");
3810
3811 health_code_update();
3812
3813 sock = lttcomm_accept_unix_sock(client_sock);
3814 if (sock < 0) {
3815 goto error;
3816 }
3817
3818 /*
3819 * Set the CLOEXEC flag. Return code is useless because either way, the
3820 * show must go on.
3821 */
3822 (void) utils_set_fd_cloexec(sock);
3823
3824 /* Set socket option for credentials retrieval */
3825 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3826 if (ret < 0) {
3827 goto error;
3828 }
3829
3830 /* Allocate context command to process the client request */
3831 cmd_ctx = zmalloc(sizeof(struct command_ctx));
3832 if (cmd_ctx == NULL) {
3833 PERROR("zmalloc cmd_ctx");
3834 goto error;
3835 }
3836
3837 /* Allocate data buffer for reception */
3838 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
3839 if (cmd_ctx->lsm == NULL) {
3840 PERROR("zmalloc cmd_ctx->lsm");
3841 goto error;
3842 }
3843
3844 cmd_ctx->llm = NULL;
3845 cmd_ctx->session = NULL;
3846
3847 health_code_update();
3848
3849 /*
3850 * Data is received from the lttng client. The struct
3851 * lttcomm_session_msg (lsm) contains the command and data request of
3852 * the client.
3853 */
3854 DBG("Receiving data from client ...");
3855 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3856 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
3857 if (ret <= 0) {
3858 DBG("Nothing recv() from client... continuing");
3859 ret = close(sock);
3860 if (ret) {
3861 PERROR("close");
3862 }
3863 sock = -1;
3864 clean_command_ctx(&cmd_ctx);
3865 continue;
3866 }
3867
3868 health_code_update();
3869
3870 // TODO: Validate cmd_ctx including sanity check for
3871 // security purpose.
3872
3873 rcu_thread_online();
3874 /*
3875 * This function dispatch the work to the kernel or userspace tracer
3876 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3877 * informations for the client. The command context struct contains
3878 * everything this function may needs.
3879 */
3880 ret = process_client_msg(cmd_ctx, sock, &sock_error);
3881 rcu_thread_offline();
3882 if (ret < 0) {
3883 ret = close(sock);
3884 if (ret) {
3885 PERROR("close");
3886 }
3887 sock = -1;
3888 /*
3889 * TODO: Inform client somehow of the fatal error. At
3890 * this point, ret < 0 means that a zmalloc failed
3891 * (ENOMEM). Error detected but still accept
3892 * command, unless a socket error has been
3893 * detected.
3894 */
3895 clean_command_ctx(&cmd_ctx);
3896 continue;
3897 }
3898
3899 health_code_update();
3900
3901 DBG("Sending response (size: %d, retcode: %s)",
3902 cmd_ctx->lttng_msg_size,
3903 lttng_strerror(-cmd_ctx->llm->ret_code));
3904 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
3905 if (ret < 0) {
3906 ERR("Failed to send data back to client");
3907 }
3908
3909 /* End of transmission */
3910 ret = close(sock);
3911 if (ret) {
3912 PERROR("close");
3913 }
3914 sock = -1;
3915
3916 clean_command_ctx(&cmd_ctx);
3917
3918 health_code_update();
3919 }
3920
3921 exit:
3922 error:
3923 if (sock >= 0) {
3924 ret = close(sock);
3925 if (ret) {
3926 PERROR("close");
3927 }
3928 }
3929
3930 lttng_poll_clean(&events);
3931 clean_command_ctx(&cmd_ctx);
3932
3933 error_listen:
3934 error_create_poll:
3935 unlink(client_unix_sock_path);
3936 if (client_sock >= 0) {
3937 ret = close(client_sock);
3938 if (ret) {
3939 PERROR("close");
3940 }
3941 }
3942
3943 if (err) {
3944 health_error();
3945 ERR("Health error occurred in %s", __func__);
3946 }
3947
3948 health_unregister(health_sessiond);
3949
3950 DBG("Client thread dying");
3951
3952 rcu_unregister_thread();
3953 return NULL;
3954 }
3955
3956
3957 /*
3958 * usage function on stderr
3959 */
3960 static void usage(void)
3961 {
3962 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
3963 fprintf(stderr, " -h, --help Display this usage.\n");
3964 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3965 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3966 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3967 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3968 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3969 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3970 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3971 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3972 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3973 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3974 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3975 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3976 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
3977 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3978 fprintf(stderr, " -V, --version Show version number.\n");
3979 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
3980 fprintf(stderr, " -q, --quiet No output at all.\n");
3981 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3982 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3983 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3984 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
3985 fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n");
3986 }
3987
3988 /*
3989 * daemon argument parsing
3990 */
3991 static int parse_args(int argc, char **argv)
3992 {
3993 int c;
3994
3995 static struct option long_options[] = {
3996 { "client-sock", 1, 0, 'c' },
3997 { "apps-sock", 1, 0, 'a' },
3998 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3999 { "kconsumerd-err-sock", 1, 0, 'E' },
4000 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
4001 { "ustconsumerd32-err-sock", 1, 0, 'H' },
4002 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
4003 { "ustconsumerd64-err-sock", 1, 0, 'F' },
4004 { "consumerd32-path", 1, 0, 'u' },
4005 { "consumerd32-libdir", 1, 0, 'U' },
4006 { "consumerd64-path", 1, 0, 't' },
4007 { "consumerd64-libdir", 1, 0, 'T' },
4008 { "daemonize", 0, 0, 'd' },
4009 { "sig-parent", 0, 0, 'S' },
4010 { "help", 0, 0, 'h' },
4011 { "group", 1, 0, 'g' },
4012 { "version", 0, 0, 'V' },
4013 { "quiet", 0, 0, 'q' },
4014 { "verbose", 0, 0, 'v' },
4015 { "verbose-consumer", 0, 0, 'Z' },
4016 { "no-kernel", 0, 0, 'N' },
4017 { "pidfile", 1, 0, 'p' },
4018 { "jul-tcp-port", 1, 0, 'J' },
4019 { NULL, 0, 0, 0 }
4020 };
4021
4022 while (1) {
4023 int option_index = 0;
4024 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:",
4025 long_options, &option_index);
4026 if (c == -1) {
4027 break;
4028 }
4029
4030 switch (c) {
4031 case 0:
4032 fprintf(stderr, "option %s", long_options[option_index].name);
4033 if (optarg) {
4034 fprintf(stderr, " with arg %s\n", optarg);
4035 }
4036 break;
4037 case 'c':
4038 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
4039 break;
4040 case 'a':
4041 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
4042 break;
4043 case 'd':
4044 opt_daemon = 1;
4045 break;
4046 case 'g':
4047 tracing_group_name = optarg;
4048 break;
4049 case 'h':
4050 usage();
4051 exit(EXIT_FAILURE);
4052 case 'V':
4053 fprintf(stdout, "%s\n", VERSION);
4054 exit(EXIT_SUCCESS);
4055 case 'S':
4056 opt_sig_parent = 1;
4057 break;
4058 case 'E':
4059 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4060 break;
4061 case 'C':
4062 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4063 break;
4064 case 'F':
4065 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4066 break;
4067 case 'D':
4068 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4069 break;
4070 case 'H':
4071 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4072 break;
4073 case 'G':
4074 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4075 break;
4076 case 'N':
4077 opt_no_kernel = 1;
4078 break;
4079 case 'q':
4080 lttng_opt_quiet = 1;
4081 break;
4082 case 'v':
4083 /* Verbose level can increase using multiple -v */
4084 lttng_opt_verbose += 1;
4085 break;
4086 case 'Z':
4087 opt_verbose_consumer += 1;
4088 break;
4089 case 'u':
4090 consumerd32_bin= optarg;
4091 break;
4092 case 'U':
4093 consumerd32_libdir = optarg;
4094 break;
4095 case 't':
4096 consumerd64_bin = optarg;
4097 break;
4098 case 'T':
4099 consumerd64_libdir = optarg;
4100 break;
4101 case 'p':
4102 opt_pidfile = optarg;
4103 break;
4104 case 'J': /* JUL TCP port. */
4105 {
4106 unsigned long v;
4107
4108 errno = 0;
4109 v = strtoul(optarg, NULL, 0);
4110 if (errno != 0 || !isdigit(optarg[0])) {
4111 ERR("Wrong value in --jul-tcp-port parameter: %s", optarg);
4112 return -1;
4113 }
4114 if (v == 0 || v >= 65535) {
4115 ERR("Port overflow in --jul-tcp-port parameter: %s", optarg);
4116 return -1;
4117 }
4118 jul_tcp_port = (uint32_t) v;
4119 DBG3("JUL TCP port set to non default: %u", jul_tcp_port);
4120 break;
4121 }
4122 default:
4123 /* Unknown option or other error.
4124 * Error is printed by getopt, just return */
4125 return -1;
4126 }
4127 }
4128
4129 return 0;
4130 }
4131
4132 /*
4133 * Creates the two needed socket by the daemon.
4134 * apps_sock - The communication socket for all UST apps.
4135 * client_sock - The communication of the cli tool (lttng).
4136 */
4137 static int init_daemon_socket(void)
4138 {
4139 int ret = 0;
4140 mode_t old_umask;
4141
4142 old_umask = umask(0);
4143
4144 /* Create client tool unix socket */
4145 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4146 if (client_sock < 0) {
4147 ERR("Create unix sock failed: %s", client_unix_sock_path);
4148 ret = -1;
4149 goto end;
4150 }
4151
4152 /* Set the cloexec flag */
4153 ret = utils_set_fd_cloexec(client_sock);
4154 if (ret < 0) {
4155 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4156 "Continuing but note that the consumer daemon will have a "
4157 "reference to this socket on exec()", client_sock);
4158 }
4159
4160 /* File permission MUST be 660 */
4161 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4162 if (ret < 0) {
4163 ERR("Set file permissions failed: %s", client_unix_sock_path);
4164 PERROR("chmod");
4165 goto end;
4166 }
4167
4168 /* Create the application unix socket */
4169 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4170 if (apps_sock < 0) {
4171 ERR("Create unix sock failed: %s", apps_unix_sock_path);
4172 ret = -1;
4173 goto end;
4174 }
4175
4176 /* Set the cloexec flag */
4177 ret = utils_set_fd_cloexec(apps_sock);
4178 if (ret < 0) {
4179 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4180 "Continuing but note that the consumer daemon will have a "
4181 "reference to this socket on exec()", apps_sock);
4182 }
4183
4184 /* File permission MUST be 666 */
4185 ret = chmod(apps_unix_sock_path,
4186 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
4187 if (ret < 0) {
4188 ERR("Set file permissions failed: %s", apps_unix_sock_path);
4189 PERROR("chmod");
4190 goto end;
4191 }
4192
4193 DBG3("Session daemon client socket %d and application socket %d created",
4194 client_sock, apps_sock);
4195
4196 end:
4197 umask(old_umask);
4198 return ret;
4199 }
4200
4201 /*
4202 * Check if the global socket is available, and if a daemon is answering at the
4203 * other side. If yes, error is returned.
4204 */
4205 static int check_existing_daemon(void)
4206 {
4207 /* Is there anybody out there ? */
4208 if (lttng_session_daemon_alive()) {
4209 return -EEXIST;
4210 }
4211
4212 return 0;
4213 }
4214
4215 /*
4216 * Set the tracing group gid onto the client socket.
4217 *
4218 * Race window between mkdir and chown is OK because we are going from more
4219 * permissive (root.root) to less permissive (root.tracing).
4220 */
4221 static int set_permissions(char *rundir)
4222 {
4223 int ret;
4224 gid_t gid;
4225
4226 gid = utils_get_group_id(tracing_group_name);
4227
4228 /* Set lttng run dir */
4229 ret = chown(rundir, 0, gid);
4230 if (ret < 0) {
4231 ERR("Unable to set group on %s", rundir);
4232 PERROR("chown");
4233 }
4234
4235 /*
4236 * Ensure all applications and tracing group can search the run
4237 * dir. Allow everyone to read the directory, since it does not
4238 * buy us anything to hide its content.
4239 */
4240 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
4241 if (ret < 0) {
4242 ERR("Unable to set permissions on %s", rundir);
4243 PERROR("chmod");
4244 }
4245
4246 /* lttng client socket path */
4247 ret = chown(client_unix_sock_path, 0, gid);
4248 if (ret < 0) {
4249 ERR("Unable to set group on %s", client_unix_sock_path);
4250 PERROR("chown");
4251 }
4252
4253 /* kconsumer error socket path */
4254 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
4255 if (ret < 0) {
4256 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
4257 PERROR("chown");
4258 }
4259
4260 /* 64-bit ustconsumer error socket path */
4261 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
4262 if (ret < 0) {
4263 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
4264 PERROR("chown");
4265 }
4266
4267 /* 32-bit ustconsumer compat32 error socket path */
4268 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
4269 if (ret < 0) {
4270 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
4271 PERROR("chown");
4272 }
4273
4274 DBG("All permissions are set");
4275
4276 return ret;
4277 }
4278
4279 /*
4280 * Create the lttng run directory needed for all global sockets and pipe.
4281 */
4282 static int create_lttng_rundir(const char *rundir)
4283 {
4284 int ret;
4285
4286 DBG3("Creating LTTng run directory: %s", rundir);
4287
4288 ret = mkdir(rundir, S_IRWXU);
4289 if (ret < 0) {
4290 if (errno != EEXIST) {
4291 ERR("Unable to create %s", rundir);
4292 goto error;
4293 } else {
4294 ret = 0;
4295 }
4296 }
4297
4298 error:
4299 return ret;
4300 }
4301
4302 /*
4303 * Setup sockets and directory needed by the kconsumerd communication with the
4304 * session daemon.
4305 */
4306 static int set_consumer_sockets(struct consumer_data *consumer_data,
4307 const char *rundir)
4308 {
4309 int ret;
4310 char path[PATH_MAX];
4311
4312 switch (consumer_data->type) {
4313 case LTTNG_CONSUMER_KERNEL:
4314 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
4315 break;
4316 case LTTNG_CONSUMER64_UST:
4317 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
4318 break;
4319 case LTTNG_CONSUMER32_UST:
4320 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
4321 break;
4322 default:
4323 ERR("Consumer type unknown");
4324 ret = -EINVAL;
4325 goto error;
4326 }
4327
4328 DBG2("Creating consumer directory: %s", path);
4329
4330 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
4331 if (ret < 0) {
4332 if (errno != EEXIST) {
4333 PERROR("mkdir");
4334 ERR("Failed to create %s", path);
4335 goto error;
4336 }
4337 ret = -1;
4338 }
4339 if (is_root) {
4340 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
4341 if (ret < 0) {
4342 ERR("Unable to set group on %s", path);
4343 PERROR("chown");
4344 goto error;
4345 }
4346 }
4347
4348 /* Create the kconsumerd error unix socket */
4349 consumer_data->err_sock =
4350 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
4351 if (consumer_data->err_sock < 0) {
4352 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
4353 ret = -1;
4354 goto error;
4355 }
4356
4357 /*
4358 * Set the CLOEXEC flag. Return code is useless because either way, the
4359 * show must go on.
4360 */
4361 ret = utils_set_fd_cloexec(consumer_data->err_sock);
4362 if (ret < 0) {
4363 PERROR("utils_set_fd_cloexec");
4364 /* continue anyway */
4365 }
4366
4367 /* File permission MUST be 660 */
4368 ret = chmod(consumer_data->err_unix_sock_path,
4369 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4370 if (ret < 0) {
4371 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
4372 PERROR("chmod");
4373 goto error;
4374 }
4375
4376 error:
4377 return ret;
4378 }
4379
4380 /*
4381 * Signal handler for the daemon
4382 *
4383 * Simply stop all worker threads, leaving main() return gracefully after
4384 * joining all threads and calling cleanup().
4385 */
4386 static void sighandler(int sig)
4387 {
4388 switch (sig) {
4389 case SIGPIPE:
4390 DBG("SIGPIPE caught");
4391 return;
4392 case SIGINT:
4393 DBG("SIGINT caught");
4394 stop_threads();
4395 break;
4396 case SIGTERM:
4397 DBG("SIGTERM caught");
4398 stop_threads();
4399 break;
4400 case SIGUSR1:
4401 CMM_STORE_SHARED(recv_child_signal, 1);
4402 break;
4403 default:
4404 break;
4405 }
4406 }
4407
4408 /*
4409 * Setup signal handler for :
4410 * SIGINT, SIGTERM, SIGPIPE
4411 */
4412 static int set_signal_handler(void)
4413 {
4414 int ret = 0;
4415 struct sigaction sa;
4416 sigset_t sigset;
4417
4418 if ((ret = sigemptyset(&sigset)) < 0) {
4419 PERROR("sigemptyset");
4420 return ret;
4421 }
4422
4423 sa.sa_handler = sighandler;
4424 sa.sa_mask = sigset;
4425 sa.sa_flags = 0;
4426 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
4427 PERROR("sigaction");
4428 return ret;
4429 }
4430
4431 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
4432 PERROR("sigaction");
4433 return ret;
4434 }
4435
4436 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
4437 PERROR("sigaction");
4438 return ret;
4439 }
4440
4441 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
4442 PERROR("sigaction");
4443 return ret;
4444 }
4445
4446 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4447
4448 return ret;
4449 }
4450
4451 /*
4452 * Set open files limit to unlimited. This daemon can open a large number of
4453 * file descriptors in order to consumer multiple kernel traces.
4454 */
4455 static void set_ulimit(void)
4456 {
4457 int ret;
4458 struct rlimit lim;
4459
4460 /* The kernel does not allowed an infinite limit for open files */
4461 lim.rlim_cur = 65535;
4462 lim.rlim_max = 65535;
4463
4464 ret = setrlimit(RLIMIT_NOFILE, &lim);
4465 if (ret < 0) {
4466 PERROR("failed to set open files limit");
4467 }
4468 }
4469
4470 /*
4471 * Write pidfile using the rundir and opt_pidfile.
4472 */
4473 static void write_pidfile(void)
4474 {
4475 int ret;
4476 char pidfile_path[PATH_MAX];
4477
4478 assert(rundir);
4479
4480 if (opt_pidfile) {
4481 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
4482 } else {
4483 /* Build pidfile path from rundir and opt_pidfile. */
4484 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
4485 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
4486 if (ret < 0) {
4487 PERROR("snprintf pidfile path");
4488 goto error;
4489 }
4490 }
4491
4492 /*
4493 * Create pid file in rundir. Return value is of no importance. The
4494 * execution will continue even though we are not able to write the file.
4495 */
4496 (void) utils_create_pid_file(getpid(), pidfile_path);
4497
4498 error:
4499 return;
4500 }
4501
4502 /*
4503 * Write JUL TCP port using the rundir.
4504 */
4505 static void write_julport(void)
4506 {
4507 int ret;
4508 char path[PATH_MAX];
4509
4510 assert(rundir);
4511
4512 ret = snprintf(path, sizeof(path), "%s/"
4513 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir);
4514 if (ret < 0) {
4515 PERROR("snprintf julport path");
4516 goto error;
4517 }
4518
4519 /*
4520 * Create TCP JUL port file in rundir. Return value is of no importance.
4521 * The execution will continue even though we are not able to write the
4522 * file.
4523 */
4524 (void) utils_create_pid_file(jul_tcp_port, path);
4525
4526 error:
4527 return;
4528 }
4529
4530 /*
4531 * main
4532 */
4533 int main(int argc, char **argv)
4534 {
4535 int ret = 0;
4536 void *status;
4537 const char *home_path, *env_app_timeout;
4538
4539 init_kernel_workarounds();
4540
4541 rcu_register_thread();
4542
4543 if ((ret = set_signal_handler()) < 0) {
4544 goto error;
4545 }
4546
4547 setup_consumerd_path();
4548
4549 page_size = sysconf(_SC_PAGESIZE);
4550 if (page_size < 0) {
4551 PERROR("sysconf _SC_PAGESIZE");
4552 page_size = LONG_MAX;
4553 WARN("Fallback page size to %ld", page_size);
4554 }
4555
4556 /* Parse arguments */
4557 progname = argv[0];
4558 if ((ret = parse_args(argc, argv)) < 0) {
4559 goto error;
4560 }
4561
4562 /* Daemonize */
4563 if (opt_daemon) {
4564 int i;
4565
4566 ret = lttng_daemonize(&child_ppid, &recv_child_signal, 1);
4567 if (ret < 0) {
4568 goto error;
4569 }
4570
4571 /*
4572 * We are in the child. Make sure all other file descriptors are
4573 * closed, in case we are called with more opened file descriptors than
4574 * the standard ones.
4575 */
4576 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4577 (void) close(i);
4578 }
4579 }
4580
4581 /* Create thread quit pipe */
4582 if ((ret = init_thread_quit_pipe()) < 0) {
4583 goto error;
4584 }
4585
4586 /* Check if daemon is UID = 0 */
4587 is_root = !getuid();
4588
4589 if (is_root) {
4590 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4591
4592 /* Create global run dir with root access */
4593 ret = create_lttng_rundir(rundir);
4594 if (ret < 0) {
4595 goto error;
4596 }
4597
4598 if (strlen(apps_unix_sock_path) == 0) {
4599 snprintf(apps_unix_sock_path, PATH_MAX,
4600 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4601 }
4602
4603 if (strlen(client_unix_sock_path) == 0) {
4604 snprintf(client_unix_sock_path, PATH_MAX,
4605 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4606 }
4607
4608 /* Set global SHM for ust */
4609 if (strlen(wait_shm_path) == 0) {
4610 snprintf(wait_shm_path, PATH_MAX,
4611 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4612 }
4613
4614 if (strlen(health_unix_sock_path) == 0) {
4615 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4616 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4617 }
4618
4619 /* Setup kernel consumerd path */
4620 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
4621 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4622 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
4623 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4624
4625 DBG2("Kernel consumer err path: %s",
4626 kconsumer_data.err_unix_sock_path);
4627 DBG2("Kernel consumer cmd path: %s",
4628 kconsumer_data.cmd_unix_sock_path);
4629 } else {
4630 home_path = utils_get_home_dir();
4631 if (home_path == NULL) {
4632 /* TODO: Add --socket PATH option */
4633 ERR("Can't get HOME directory for sockets creation.");
4634 ret = -EPERM;
4635 goto error;
4636 }
4637
4638 /*
4639 * Create rundir from home path. This will create something like
4640 * $HOME/.lttng
4641 */
4642 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
4643 if (ret < 0) {
4644 ret = -ENOMEM;
4645 goto error;
4646 }
4647
4648 ret = create_lttng_rundir(rundir);
4649 if (ret < 0) {
4650 goto error;
4651 }
4652
4653 if (strlen(apps_unix_sock_path) == 0) {
4654 snprintf(apps_unix_sock_path, PATH_MAX,
4655 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
4656 }
4657
4658 /* Set the cli tool unix socket path */
4659 if (strlen(client_unix_sock_path) == 0) {
4660 snprintf(client_unix_sock_path, PATH_MAX,
4661 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
4662 }
4663
4664 /* Set global SHM for ust */
4665 if (strlen(wait_shm_path) == 0) {
4666 snprintf(wait_shm_path, PATH_MAX,
4667 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
4668 }
4669
4670 /* Set health check Unix path */
4671 if (strlen(health_unix_sock_path) == 0) {
4672 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4673 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4674 }
4675 }
4676
4677 /* Set consumer initial state */
4678 kernel_consumerd_state = CONSUMER_STOPPED;
4679 ust_consumerd_state = CONSUMER_STOPPED;
4680
4681 DBG("Client socket path %s", client_unix_sock_path);
4682 DBG("Application socket path %s", apps_unix_sock_path);
4683 DBG("Application wait path %s", wait_shm_path);
4684 DBG("LTTng run directory path: %s", rundir);
4685
4686 /* 32 bits consumerd path setup */
4687 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
4688 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4689 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
4690 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4691
4692 DBG2("UST consumer 32 bits err path: %s",
4693 ustconsumer32_data.err_unix_sock_path);
4694 DBG2("UST consumer 32 bits cmd path: %s",
4695 ustconsumer32_data.cmd_unix_sock_path);
4696
4697 /* 64 bits consumerd path setup */
4698 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
4699 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4700 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
4701 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4702
4703 DBG2("UST consumer 64 bits err path: %s",
4704 ustconsumer64_data.err_unix_sock_path);
4705 DBG2("UST consumer 64 bits cmd path: %s",
4706 ustconsumer64_data.cmd_unix_sock_path);
4707
4708 /*
4709 * See if daemon already exist.
4710 */
4711 if ((ret = check_existing_daemon()) < 0) {
4712 ERR("Already running daemon.\n");
4713 /*
4714 * We do not goto exit because we must not cleanup()
4715 * because a daemon is already running.
4716 */
4717 goto error;
4718 }
4719
4720 /*
4721 * Init UST app hash table. Alloc hash table before this point since
4722 * cleanup() can get called after that point.
4723 */
4724 ust_app_ht_alloc();
4725
4726 /* Initialize JUL domain subsystem. */
4727 if ((ret = jul_init()) < 0) {
4728 /* ENOMEM at this point. */
4729 goto error;
4730 }
4731
4732 /* After this point, we can safely call cleanup() with "goto exit" */
4733
4734 /*
4735 * These actions must be executed as root. We do that *after* setting up
4736 * the sockets path because we MUST make the check for another daemon using
4737 * those paths *before* trying to set the kernel consumer sockets and init
4738 * kernel tracer.
4739 */
4740 if (is_root) {
4741 ret = set_consumer_sockets(&kconsumer_data, rundir);
4742 if (ret < 0) {
4743 goto exit;
4744 }
4745
4746 /* Setup kernel tracer */
4747 if (!opt_no_kernel) {
4748 init_kernel_tracer();
4749 }
4750
4751 /* Set ulimit for open files */
4752 set_ulimit();
4753 }
4754 /* init lttng_fd tracking must be done after set_ulimit. */
4755 lttng_fd_init();
4756
4757 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4758 if (ret < 0) {
4759 goto exit;
4760 }
4761
4762 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4763 if (ret < 0) {
4764 goto exit;
4765 }
4766
4767 /* Setup the needed unix socket */
4768 if ((ret = init_daemon_socket()) < 0) {
4769 goto exit;
4770 }
4771
4772 /* Set credentials to socket */
4773 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
4774 goto exit;
4775 }
4776
4777 /* Get parent pid if -S, --sig-parent is specified. */
4778 if (opt_sig_parent) {
4779 ppid = getppid();
4780 }
4781
4782 /* Setup the kernel pipe for waking up the kernel thread */
4783 if (is_root && !opt_no_kernel) {
4784 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4785 goto exit;
4786 }
4787 }
4788
4789 /* Setup the thread ht_cleanup communication pipe. */
4790 if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) {
4791 goto exit;
4792 }
4793
4794 /* Setup the thread apps communication pipe. */
4795 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
4796 goto exit;
4797 }
4798
4799 /* Setup the thread apps notify communication pipe. */
4800 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
4801 goto exit;
4802 }
4803
4804 /* Initialize global buffer per UID and PID registry. */
4805 buffer_reg_init_uid_registry();
4806 buffer_reg_init_pid_registry();
4807
4808 /* Init UST command queue. */
4809 cds_wfq_init(&ust_cmd_queue.queue);
4810
4811 /*
4812 * Get session list pointer. This pointer MUST NOT be free(). This list is
4813 * statically declared in session.c
4814 */
4815 session_list_ptr = session_get_list();
4816
4817 /* Set up max poll set size */
4818 lttng_poll_set_max_size();
4819
4820 cmd_init();
4821
4822 /* Check for the application socket timeout env variable. */
4823 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
4824 if (env_app_timeout) {
4825 app_socket_timeout = atoi(env_app_timeout);
4826 } else {
4827 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
4828 }
4829
4830 write_pidfile();
4831 write_julport();
4832
4833 /* Initialize communication library */
4834 lttcomm_init();
4835 /* This is to get the TCP timeout value. */
4836 lttcomm_inet_init();
4837
4838 /*
4839 * Initialize the health check subsystem. This call should set the
4840 * appropriate time values.
4841 */
4842 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
4843 if (!health_sessiond) {
4844 PERROR("health_app_create error");
4845 goto exit_health_sessiond_cleanup;
4846 }
4847
4848 /* Create thread to clean up RCU hash tables */
4849 ret = pthread_create(&ht_cleanup_thread, NULL,
4850 thread_ht_cleanup, (void *) NULL);
4851 if (ret != 0) {
4852 PERROR("pthread_create ht_cleanup");
4853 goto exit_ht_cleanup;
4854 }
4855
4856 /* Create health-check thread */
4857 ret = pthread_create(&health_thread, NULL,
4858 thread_manage_health, (void *) NULL);
4859 if (ret != 0) {
4860 PERROR("pthread_create health");
4861 goto exit_health;
4862 }
4863
4864 /* Create thread to manage the client socket */
4865 ret = pthread_create(&client_thread, NULL,
4866 thread_manage_clients, (void *) NULL);
4867 if (ret != 0) {
4868 PERROR("pthread_create clients");
4869 goto exit_client;
4870 }
4871
4872 /* Create thread to dispatch registration */
4873 ret = pthread_create(&dispatch_thread, NULL,
4874 thread_dispatch_ust_registration, (void *) NULL);
4875 if (ret != 0) {
4876 PERROR("pthread_create dispatch");
4877 goto exit_dispatch;
4878 }
4879
4880 /* Create thread to manage application registration. */
4881 ret = pthread_create(&reg_apps_thread, NULL,
4882 thread_registration_apps, (void *) NULL);
4883 if (ret != 0) {
4884 PERROR("pthread_create registration");
4885 goto exit_reg_apps;
4886 }
4887
4888 /* Create thread to manage application socket */
4889 ret = pthread_create(&apps_thread, NULL,
4890 thread_manage_apps, (void *) NULL);
4891 if (ret != 0) {
4892 PERROR("pthread_create apps");
4893 goto exit_apps;
4894 }
4895
4896 /* Create thread to manage application notify socket */
4897 ret = pthread_create(&apps_notify_thread, NULL,
4898 ust_thread_manage_notify, (void *) NULL);
4899 if (ret != 0) {
4900 PERROR("pthread_create apps");
4901 goto exit_apps_notify;
4902 }
4903
4904 /* Create JUL registration thread. */
4905 ret = pthread_create(&jul_reg_thread, NULL,
4906 jul_thread_manage_registration, (void *) NULL);
4907 if (ret != 0) {
4908 PERROR("pthread_create apps");
4909 goto exit_jul_reg;
4910 }
4911
4912 /* Don't start this thread if kernel tracing is not requested nor root */
4913 if (is_root && !opt_no_kernel) {
4914 /* Create kernel thread to manage kernel event */
4915 ret = pthread_create(&kernel_thread, NULL,
4916 thread_manage_kernel, (void *) NULL);
4917 if (ret != 0) {
4918 PERROR("pthread_create kernel");
4919 goto exit_kernel;
4920 }
4921
4922 ret = pthread_join(kernel_thread, &status);
4923 if (ret != 0) {
4924 PERROR("pthread_join");
4925 goto error; /* join error, exit without cleanup */
4926 }
4927 }
4928
4929 exit_kernel:
4930 ret = pthread_join(jul_reg_thread, &status);
4931 if (ret != 0) {
4932 PERROR("pthread_join JUL");
4933 goto error; /* join error, exit without cleanup */
4934 }
4935
4936 exit_jul_reg:
4937 ret = pthread_join(apps_notify_thread, &status);
4938 if (ret != 0) {
4939 PERROR("pthread_join apps notify");
4940 goto error; /* join error, exit without cleanup */
4941 }
4942
4943 exit_apps_notify:
4944 ret = pthread_join(apps_thread, &status);
4945 if (ret != 0) {
4946 PERROR("pthread_join apps");
4947 goto error; /* join error, exit without cleanup */
4948 }
4949
4950
4951 exit_apps:
4952 ret = pthread_join(reg_apps_thread, &status);
4953 if (ret != 0) {
4954 PERROR("pthread_join");
4955 goto error; /* join error, exit without cleanup */
4956 }
4957
4958 exit_reg_apps:
4959 ret = pthread_join(dispatch_thread, &status);
4960 if (ret != 0) {
4961 PERROR("pthread_join");
4962 goto error; /* join error, exit without cleanup */
4963 }
4964
4965 exit_dispatch:
4966 ret = pthread_join(client_thread, &status);
4967 if (ret != 0) {
4968 PERROR("pthread_join");
4969 goto error; /* join error, exit without cleanup */
4970 }
4971
4972 ret = join_consumer_thread(&kconsumer_data);
4973 if (ret != 0) {
4974 PERROR("join_consumer");
4975 goto error; /* join error, exit without cleanup */
4976 }
4977
4978 ret = join_consumer_thread(&ustconsumer32_data);
4979 if (ret != 0) {
4980 PERROR("join_consumer ust32");
4981 goto error; /* join error, exit without cleanup */
4982 }
4983
4984 ret = join_consumer_thread(&ustconsumer64_data);
4985 if (ret != 0) {
4986 PERROR("join_consumer ust64");
4987 goto error; /* join error, exit without cleanup */
4988 }
4989
4990 exit_client:
4991 ret = pthread_join(health_thread, &status);
4992 if (ret != 0) {
4993 PERROR("pthread_join health thread");
4994 goto error; /* join error, exit without cleanup */
4995 }
4996
4997 exit_health:
4998 ret = pthread_join(ht_cleanup_thread, &status);
4999 if (ret != 0) {
5000 PERROR("pthread_join ht cleanup thread");
5001 goto error; /* join error, exit without cleanup */
5002 }
5003 exit_ht_cleanup:
5004 health_app_destroy(health_sessiond);
5005 exit_health_sessiond_cleanup:
5006 exit:
5007 /*
5008 * cleanup() is called when no other thread is running.
5009 */
5010 rcu_thread_online();
5011 cleanup();
5012 rcu_thread_offline();
5013 rcu_unregister_thread();
5014 if (!ret) {
5015 exit(EXIT_SUCCESS);
5016 }
5017 error:
5018 exit(EXIT_FAILURE);
5019 }
This page took 0.154634 seconds and 5 git commands to generate.