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