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