f6a051aa1cb52f8080cc9b2209a71044f052e860
[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 static int copy_session_consumer(int domain, struct ltt_session *session)
2164 {
2165 int ret;
2166 const char *dir_name;
2167 struct consumer_output *consumer;
2168
2169 assert(session);
2170 assert(session->consumer);
2171
2172 switch (domain) {
2173 case LTTNG_DOMAIN_KERNEL:
2174 DBG3("Copying tracing session consumer output in kernel session");
2175 /*
2176 * XXX: We should audit the session creation and what this function
2177 * does "extra" in order to avoid a destroy since this function is used
2178 * in the domain session creation (kernel and ust) only. Same for UST
2179 * domain.
2180 */
2181 if (session->kernel_session->consumer) {
2182 consumer_destroy_output(session->kernel_session->consumer);
2183 }
2184 session->kernel_session->consumer =
2185 consumer_copy_output(session->consumer);
2186 /* Ease our life a bit for the next part */
2187 consumer = session->kernel_session->consumer;
2188 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2189 break;
2190 case LTTNG_DOMAIN_UST:
2191 DBG3("Copying tracing session consumer output in UST session");
2192 if (session->ust_session->consumer) {
2193 consumer_destroy_output(session->ust_session->consumer);
2194 }
2195 session->ust_session->consumer =
2196 consumer_copy_output(session->consumer);
2197 /* Ease our life a bit for the next part */
2198 consumer = session->ust_session->consumer;
2199 dir_name = DEFAULT_UST_TRACE_DIR;
2200 break;
2201 default:
2202 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2203 goto error;
2204 }
2205
2206 /* Append correct directory to subdir */
2207 strncat(consumer->subdir, dir_name,
2208 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2209 DBG3("Copy session consumer subdir %s", consumer->subdir);
2210
2211 ret = LTTNG_OK;
2212
2213 error:
2214 return ret;
2215 }
2216
2217 /*
2218 * Create an UST session and add it to the session ust list.
2219 */
2220 static int create_ust_session(struct ltt_session *session,
2221 struct lttng_domain *domain)
2222 {
2223 int ret;
2224 struct ltt_ust_session *lus = NULL;
2225
2226 assert(session);
2227 assert(domain);
2228 assert(session->consumer);
2229
2230 switch (domain->type) {
2231 case LTTNG_DOMAIN_UST:
2232 break;
2233 default:
2234 ERR("Unknown UST domain on create session %d", domain->type);
2235 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2236 goto error;
2237 }
2238
2239 DBG("Creating UST session");
2240
2241 lus = trace_ust_create_session(session->id);
2242 if (lus == NULL) {
2243 ret = LTTNG_ERR_UST_SESS_FAIL;
2244 goto error;
2245 }
2246
2247 lus->uid = session->uid;
2248 lus->gid = session->gid;
2249 session->ust_session = lus;
2250
2251 /* Copy session output to the newly created UST session */
2252 ret = copy_session_consumer(domain->type, session);
2253 if (ret != LTTNG_OK) {
2254 goto error;
2255 }
2256
2257 return LTTNG_OK;
2258
2259 error:
2260 free(lus);
2261 session->ust_session = NULL;
2262 return ret;
2263 }
2264
2265 /*
2266 * Create a kernel tracer session then create the default channel.
2267 */
2268 static int create_kernel_session(struct ltt_session *session)
2269 {
2270 int ret;
2271
2272 DBG("Creating kernel session");
2273
2274 ret = kernel_create_session(session, kernel_tracer_fd);
2275 if (ret < 0) {
2276 ret = LTTNG_ERR_KERN_SESS_FAIL;
2277 goto error;
2278 }
2279
2280 /* Code flow safety */
2281 assert(session->kernel_session);
2282
2283 /* Copy session output to the newly created Kernel session */
2284 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2285 if (ret != LTTNG_OK) {
2286 goto error;
2287 }
2288
2289 /* Create directory(ies) on local filesystem. */
2290 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2291 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2292 ret = run_as_mkdir_recursive(
2293 session->kernel_session->consumer->dst.trace_path,
2294 S_IRWXU | S_IRWXG, session->uid, session->gid);
2295 if (ret < 0) {
2296 if (ret != -EEXIST) {
2297 ERR("Trace directory creation error");
2298 goto error;
2299 }
2300 }
2301 }
2302
2303 session->kernel_session->uid = session->uid;
2304 session->kernel_session->gid = session->gid;
2305
2306 return LTTNG_OK;
2307
2308 error:
2309 trace_kernel_destroy_session(session->kernel_session);
2310 session->kernel_session = NULL;
2311 return ret;
2312 }
2313
2314 /*
2315 * Count number of session permitted by uid/gid.
2316 */
2317 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2318 {
2319 unsigned int i = 0;
2320 struct ltt_session *session;
2321
2322 DBG("Counting number of available session for UID %d GID %d",
2323 uid, gid);
2324 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2325 /*
2326 * Only list the sessions the user can control.
2327 */
2328 if (!session_access_ok(session, uid, gid)) {
2329 continue;
2330 }
2331 i++;
2332 }
2333 return i;
2334 }
2335
2336 /*
2337 * Process the command requested by the lttng client within the command
2338 * context structure. This function make sure that the return structure (llm)
2339 * is set and ready for transmission before returning.
2340 *
2341 * Return any error encountered or 0 for success.
2342 *
2343 * "sock" is only used for special-case var. len data.
2344 */
2345 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2346 int *sock_error)
2347 {
2348 int ret = LTTNG_OK;
2349 int need_tracing_session = 1;
2350 int need_domain;
2351
2352 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2353
2354 *sock_error = 0;
2355
2356 switch (cmd_ctx->lsm->cmd_type) {
2357 case LTTNG_CREATE_SESSION:
2358 case LTTNG_DESTROY_SESSION:
2359 case LTTNG_LIST_SESSIONS:
2360 case LTTNG_LIST_DOMAINS:
2361 case LTTNG_START_TRACE:
2362 case LTTNG_STOP_TRACE:
2363 case LTTNG_DATA_PENDING:
2364 need_domain = 0;
2365 break;
2366 default:
2367 need_domain = 1;
2368 }
2369
2370 if (opt_no_kernel && need_domain
2371 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2372 if (!is_root) {
2373 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2374 } else {
2375 ret = LTTNG_ERR_KERN_NA;
2376 }
2377 goto error;
2378 }
2379
2380 /* Deny register consumer if we already have a spawned consumer. */
2381 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2382 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2383 if (kconsumer_data.pid > 0) {
2384 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2385 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2386 goto error;
2387 }
2388 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2389 }
2390
2391 /*
2392 * Check for command that don't needs to allocate a returned payload. We do
2393 * this here so we don't have to make the call for no payload at each
2394 * command.
2395 */
2396 switch(cmd_ctx->lsm->cmd_type) {
2397 case LTTNG_LIST_SESSIONS:
2398 case LTTNG_LIST_TRACEPOINTS:
2399 case LTTNG_LIST_TRACEPOINT_FIELDS:
2400 case LTTNG_LIST_DOMAINS:
2401 case LTTNG_LIST_CHANNELS:
2402 case LTTNG_LIST_EVENTS:
2403 break;
2404 default:
2405 /* Setup lttng message with no payload */
2406 ret = setup_lttng_msg(cmd_ctx, 0);
2407 if (ret < 0) {
2408 /* This label does not try to unlock the session */
2409 goto init_setup_error;
2410 }
2411 }
2412
2413 /* Commands that DO NOT need a session. */
2414 switch (cmd_ctx->lsm->cmd_type) {
2415 case LTTNG_CREATE_SESSION:
2416 case LTTNG_CALIBRATE:
2417 case LTTNG_LIST_SESSIONS:
2418 case LTTNG_LIST_TRACEPOINTS:
2419 case LTTNG_LIST_TRACEPOINT_FIELDS:
2420 need_tracing_session = 0;
2421 break;
2422 default:
2423 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2424 /*
2425 * We keep the session list lock across _all_ commands
2426 * for now, because the per-session lock does not
2427 * handle teardown properly.
2428 */
2429 session_lock_list();
2430 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2431 if (cmd_ctx->session == NULL) {
2432 if (cmd_ctx->lsm->session.name != NULL) {
2433 ret = LTTNG_ERR_SESS_NOT_FOUND;
2434 } else {
2435 /* If no session name specified */
2436 ret = LTTNG_ERR_SELECT_SESS;
2437 }
2438 goto error;
2439 } else {
2440 /* Acquire lock for the session */
2441 session_lock(cmd_ctx->session);
2442 }
2443 break;
2444 }
2445
2446 if (!need_domain) {
2447 goto skip_domain;
2448 }
2449
2450 /*
2451 * Check domain type for specific "pre-action".
2452 */
2453 switch (cmd_ctx->lsm->domain.type) {
2454 case LTTNG_DOMAIN_KERNEL:
2455 if (!is_root) {
2456 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2457 goto error;
2458 }
2459
2460 /* Kernel tracer check */
2461 if (kernel_tracer_fd == -1) {
2462 /* Basically, load kernel tracer modules */
2463 ret = init_kernel_tracer();
2464 if (ret != 0) {
2465 goto error;
2466 }
2467 }
2468
2469 /* Consumer is in an ERROR state. Report back to client */
2470 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
2471 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2472 goto error;
2473 }
2474
2475 /* Need a session for kernel command */
2476 if (need_tracing_session) {
2477 if (cmd_ctx->session->kernel_session == NULL) {
2478 ret = create_kernel_session(cmd_ctx->session);
2479 if (ret < 0) {
2480 ret = LTTNG_ERR_KERN_SESS_FAIL;
2481 goto error;
2482 }
2483 }
2484
2485 /* Start the kernel consumer daemon */
2486 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2487 if (kconsumer_data.pid == 0 &&
2488 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2489 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2490 ret = start_consumerd(&kconsumer_data);
2491 if (ret < 0) {
2492 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2493 goto error;
2494 }
2495 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
2496 } else {
2497 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2498 }
2499
2500 /*
2501 * The consumer was just spawned so we need to add the socket to
2502 * the consumer output of the session if exist.
2503 */
2504 ret = consumer_create_socket(&kconsumer_data,
2505 cmd_ctx->session->kernel_session->consumer);
2506 if (ret < 0) {
2507 goto error;
2508 }
2509 }
2510
2511 break;
2512 case LTTNG_DOMAIN_UST:
2513 {
2514 /* Consumer is in an ERROR state. Report back to client */
2515 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
2516 ret = LTTNG_ERR_NO_USTCONSUMERD;
2517 goto error;
2518 }
2519
2520 if (need_tracing_session) {
2521 /* Create UST session if none exist. */
2522 if (cmd_ctx->session->ust_session == NULL) {
2523 ret = create_ust_session(cmd_ctx->session,
2524 &cmd_ctx->lsm->domain);
2525 if (ret != LTTNG_OK) {
2526 goto error;
2527 }
2528 }
2529
2530 /* Start the UST consumer daemons */
2531 /* 64-bit */
2532 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
2533 if (consumerd64_bin[0] != '\0' &&
2534 ustconsumer64_data.pid == 0 &&
2535 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2536 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2537 ret = start_consumerd(&ustconsumer64_data);
2538 if (ret < 0) {
2539 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
2540 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2541 goto error;
2542 }
2543
2544 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
2545 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2546 } else {
2547 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2548 }
2549
2550 /*
2551 * Setup socket for consumer 64 bit. No need for atomic access
2552 * since it was set above and can ONLY be set in this thread.
2553 */
2554 ret = consumer_create_socket(&ustconsumer64_data,
2555 cmd_ctx->session->ust_session->consumer);
2556 if (ret < 0) {
2557 goto error;
2558 }
2559
2560 /* 32-bit */
2561 if (consumerd32_bin[0] != '\0' &&
2562 ustconsumer32_data.pid == 0 &&
2563 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2564 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2565 ret = start_consumerd(&ustconsumer32_data);
2566 if (ret < 0) {
2567 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
2568 uatomic_set(&ust_consumerd32_fd, -EINVAL);
2569 goto error;
2570 }
2571
2572 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
2573 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2574 } else {
2575 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2576 }
2577
2578 /*
2579 * Setup socket for consumer 64 bit. No need for atomic access
2580 * since it was set above and can ONLY be set in this thread.
2581 */
2582 ret = consumer_create_socket(&ustconsumer32_data,
2583 cmd_ctx->session->ust_session->consumer);
2584 if (ret < 0) {
2585 goto error;
2586 }
2587 }
2588 break;
2589 }
2590 default:
2591 break;
2592 }
2593 skip_domain:
2594
2595 /* Validate consumer daemon state when start/stop trace command */
2596 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2597 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2598 switch (cmd_ctx->lsm->domain.type) {
2599 case LTTNG_DOMAIN_UST:
2600 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
2601 ret = LTTNG_ERR_NO_USTCONSUMERD;
2602 goto error;
2603 }
2604 break;
2605 case LTTNG_DOMAIN_KERNEL:
2606 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
2607 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2608 goto error;
2609 }
2610 break;
2611 }
2612 }
2613
2614 /*
2615 * Check that the UID or GID match that of the tracing session.
2616 * The root user can interact with all sessions.
2617 */
2618 if (need_tracing_session) {
2619 if (!session_access_ok(cmd_ctx->session,
2620 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2621 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
2622 ret = LTTNG_ERR_EPERM;
2623 goto error;
2624 }
2625 }
2626
2627 /*
2628 * Send relayd information to consumer as soon as we have a domain and a
2629 * session defined.
2630 */
2631 if (cmd_ctx->session && need_domain) {
2632 /*
2633 * Setup relayd if not done yet. If the relayd information was already
2634 * sent to the consumer, this call will gracefully return.
2635 */
2636 ret = cmd_setup_relayd(cmd_ctx->session);
2637 if (ret != LTTNG_OK) {
2638 goto error;
2639 }
2640 }
2641
2642 /* Process by command type */
2643 switch (cmd_ctx->lsm->cmd_type) {
2644 case LTTNG_ADD_CONTEXT:
2645 {
2646 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2647 cmd_ctx->lsm->u.context.channel_name,
2648 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
2649 break;
2650 }
2651 case LTTNG_DISABLE_CHANNEL:
2652 {
2653 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2654 cmd_ctx->lsm->u.disable.channel_name);
2655 break;
2656 }
2657 case LTTNG_DISABLE_EVENT:
2658 {
2659 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2660 cmd_ctx->lsm->u.disable.channel_name,
2661 cmd_ctx->lsm->u.disable.name);
2662 break;
2663 }
2664 case LTTNG_DISABLE_ALL_EVENT:
2665 {
2666 DBG("Disabling all events");
2667
2668 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2669 cmd_ctx->lsm->u.disable.channel_name);
2670 break;
2671 }
2672 case LTTNG_ENABLE_CHANNEL:
2673 {
2674 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2675 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
2676 break;
2677 }
2678 case LTTNG_ENABLE_EVENT:
2679 {
2680 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
2681 cmd_ctx->lsm->u.enable.channel_name,
2682 &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]);
2683 break;
2684 }
2685 case LTTNG_ENABLE_ALL_EVENT:
2686 {
2687 DBG("Enabling all events");
2688
2689 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
2690 cmd_ctx->lsm->u.enable.channel_name,
2691 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
2692 break;
2693 }
2694 case LTTNG_LIST_TRACEPOINTS:
2695 {
2696 struct lttng_event *events;
2697 ssize_t nb_events;
2698
2699 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
2700 if (nb_events < 0) {
2701 /* Return value is a negative lttng_error_code. */
2702 ret = -nb_events;
2703 goto error;
2704 }
2705
2706 /*
2707 * Setup lttng message with payload size set to the event list size in
2708 * bytes and then copy list into the llm payload.
2709 */
2710 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2711 if (ret < 0) {
2712 free(events);
2713 goto setup_error;
2714 }
2715
2716 /* Copy event list into message payload */
2717 memcpy(cmd_ctx->llm->payload, events,
2718 sizeof(struct lttng_event) * nb_events);
2719
2720 free(events);
2721
2722 ret = LTTNG_OK;
2723 break;
2724 }
2725 case LTTNG_LIST_TRACEPOINT_FIELDS:
2726 {
2727 struct lttng_event_field *fields;
2728 ssize_t nb_fields;
2729
2730 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
2731 &fields);
2732 if (nb_fields < 0) {
2733 /* Return value is a negative lttng_error_code. */
2734 ret = -nb_fields;
2735 goto error;
2736 }
2737
2738 /*
2739 * Setup lttng message with payload size set to the event list size in
2740 * bytes and then copy list into the llm payload.
2741 */
2742 ret = setup_lttng_msg(cmd_ctx,
2743 sizeof(struct lttng_event_field) * nb_fields);
2744 if (ret < 0) {
2745 free(fields);
2746 goto setup_error;
2747 }
2748
2749 /* Copy event list into message payload */
2750 memcpy(cmd_ctx->llm->payload, fields,
2751 sizeof(struct lttng_event_field) * nb_fields);
2752
2753 free(fields);
2754
2755 ret = LTTNG_OK;
2756 break;
2757 }
2758 case LTTNG_SET_CONSUMER_URI:
2759 {
2760 size_t nb_uri, len;
2761 struct lttng_uri *uris;
2762
2763 nb_uri = cmd_ctx->lsm->u.uri.size;
2764 len = nb_uri * sizeof(struct lttng_uri);
2765
2766 if (nb_uri == 0) {
2767 ret = LTTNG_ERR_INVALID;
2768 goto error;
2769 }
2770
2771 uris = zmalloc(len);
2772 if (uris == NULL) {
2773 ret = LTTNG_ERR_FATAL;
2774 goto error;
2775 }
2776
2777 /* Receive variable len data */
2778 DBG("Receiving %zu URI(s) from client ...", nb_uri);
2779 ret = lttcomm_recv_unix_sock(sock, uris, len);
2780 if (ret <= 0) {
2781 DBG("No URIs received from client... continuing");
2782 *sock_error = 1;
2783 ret = LTTNG_ERR_SESSION_FAIL;
2784 free(uris);
2785 goto error;
2786 }
2787
2788 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
2789 nb_uri, uris);
2790 if (ret != LTTNG_OK) {
2791 free(uris);
2792 goto error;
2793 }
2794
2795 /*
2796 * XXX: 0 means that this URI should be applied on the session. Should
2797 * be a DOMAIN enuam.
2798 */
2799 if (cmd_ctx->lsm->domain.type == 0) {
2800 /* Add the URI for the UST session if a consumer is present. */
2801 if (cmd_ctx->session->ust_session &&
2802 cmd_ctx->session->ust_session->consumer) {
2803 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
2804 nb_uri, uris);
2805 } else if (cmd_ctx->session->kernel_session &&
2806 cmd_ctx->session->kernel_session->consumer) {
2807 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
2808 cmd_ctx->session, nb_uri, uris);
2809 }
2810 }
2811
2812 free(uris);
2813
2814 break;
2815 }
2816 case LTTNG_START_TRACE:
2817 {
2818 ret = cmd_start_trace(cmd_ctx->session);
2819 break;
2820 }
2821 case LTTNG_STOP_TRACE:
2822 {
2823 ret = cmd_stop_trace(cmd_ctx->session);
2824 break;
2825 }
2826 case LTTNG_CREATE_SESSION:
2827 {
2828 size_t nb_uri, len;
2829 struct lttng_uri *uris = NULL;
2830
2831 nb_uri = cmd_ctx->lsm->u.uri.size;
2832 len = nb_uri * sizeof(struct lttng_uri);
2833
2834 if (nb_uri > 0) {
2835 uris = zmalloc(len);
2836 if (uris == NULL) {
2837 ret = LTTNG_ERR_FATAL;
2838 goto error;
2839 }
2840
2841 /* Receive variable len data */
2842 DBG("Waiting for %zu URIs from client ...", nb_uri);
2843 ret = lttcomm_recv_unix_sock(sock, uris, len);
2844 if (ret <= 0) {
2845 DBG("No URIs received from client... continuing");
2846 *sock_error = 1;
2847 ret = LTTNG_ERR_SESSION_FAIL;
2848 free(uris);
2849 goto error;
2850 }
2851
2852 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
2853 DBG("Creating session with ONE network URI is a bad call");
2854 ret = LTTNG_ERR_SESSION_FAIL;
2855 free(uris);
2856 goto error;
2857 }
2858 }
2859
2860 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
2861 &cmd_ctx->creds);
2862
2863 free(uris);
2864
2865 break;
2866 }
2867 case LTTNG_DESTROY_SESSION:
2868 {
2869 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
2870
2871 /* Set session to NULL so we do not unlock it after free. */
2872 cmd_ctx->session = NULL;
2873 break;
2874 }
2875 case LTTNG_LIST_DOMAINS:
2876 {
2877 ssize_t nb_dom;
2878 struct lttng_domain *domains;
2879
2880 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
2881 if (nb_dom < 0) {
2882 /* Return value is a negative lttng_error_code. */
2883 ret = -nb_dom;
2884 goto error;
2885 }
2886
2887 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
2888 if (ret < 0) {
2889 goto setup_error;
2890 }
2891
2892 /* Copy event list into message payload */
2893 memcpy(cmd_ctx->llm->payload, domains,
2894 nb_dom * sizeof(struct lttng_domain));
2895
2896 free(domains);
2897
2898 ret = LTTNG_OK;
2899 break;
2900 }
2901 case LTTNG_LIST_CHANNELS:
2902 {
2903 int nb_chan;
2904 struct lttng_channel *channels;
2905
2906 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
2907 cmd_ctx->session, &channels);
2908 if (nb_chan < 0) {
2909 /* Return value is a negative lttng_error_code. */
2910 ret = -nb_chan;
2911 goto error;
2912 }
2913
2914 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
2915 if (ret < 0) {
2916 goto setup_error;
2917 }
2918
2919 /* Copy event list into message payload */
2920 memcpy(cmd_ctx->llm->payload, channels,
2921 nb_chan * sizeof(struct lttng_channel));
2922
2923 free(channels);
2924
2925 ret = LTTNG_OK;
2926 break;
2927 }
2928 case LTTNG_LIST_EVENTS:
2929 {
2930 ssize_t nb_event;
2931 struct lttng_event *events = NULL;
2932
2933 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
2934 cmd_ctx->lsm->u.list.channel_name, &events);
2935 if (nb_event < 0) {
2936 /* Return value is a negative lttng_error_code. */
2937 ret = -nb_event;
2938 goto error;
2939 }
2940
2941 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
2942 if (ret < 0) {
2943 goto setup_error;
2944 }
2945
2946 /* Copy event list into message payload */
2947 memcpy(cmd_ctx->llm->payload, events,
2948 nb_event * sizeof(struct lttng_event));
2949
2950 free(events);
2951
2952 ret = LTTNG_OK;
2953 break;
2954 }
2955 case LTTNG_LIST_SESSIONS:
2956 {
2957 unsigned int nr_sessions;
2958
2959 session_lock_list();
2960 nr_sessions = lttng_sessions_count(
2961 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2962 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
2963
2964 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
2965 if (ret < 0) {
2966 session_unlock_list();
2967 goto setup_error;
2968 }
2969
2970 /* Filled the session array */
2971 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
2972 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2973 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
2974
2975 session_unlock_list();
2976
2977 ret = LTTNG_OK;
2978 break;
2979 }
2980 case LTTNG_CALIBRATE:
2981 {
2982 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
2983 &cmd_ctx->lsm->u.calibrate);
2984 break;
2985 }
2986 case LTTNG_REGISTER_CONSUMER:
2987 {
2988 struct consumer_data *cdata;
2989
2990 switch (cmd_ctx->lsm->domain.type) {
2991 case LTTNG_DOMAIN_KERNEL:
2992 cdata = &kconsumer_data;
2993 break;
2994 default:
2995 ret = LTTNG_ERR_UND;
2996 goto error;
2997 }
2998
2999 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3000 cmd_ctx->lsm->u.reg.path, cdata);
3001 break;
3002 }
3003 case LTTNG_ENABLE_EVENT_WITH_FILTER:
3004 {
3005 struct lttng_filter_bytecode *bytecode;
3006
3007 if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) {
3008 ret = LTTNG_ERR_FILTER_INVAL;
3009 goto error;
3010 }
3011 if (cmd_ctx->lsm->u.enable.bytecode_len == 0) {
3012 ret = LTTNG_ERR_FILTER_INVAL;
3013 goto error;
3014 }
3015 bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len);
3016 if (!bytecode) {
3017 ret = LTTNG_ERR_FILTER_NOMEM;
3018 goto error;
3019 }
3020 /* Receive var. len. data */
3021 DBG("Receiving var len data from client ...");
3022 ret = lttcomm_recv_unix_sock(sock, bytecode,
3023 cmd_ctx->lsm->u.enable.bytecode_len);
3024 if (ret <= 0) {
3025 DBG("Nothing recv() from client var len data... continuing");
3026 *sock_error = 1;
3027 ret = LTTNG_ERR_FILTER_INVAL;
3028 goto error;
3029 }
3030
3031 if (bytecode->len + sizeof(*bytecode)
3032 != cmd_ctx->lsm->u.enable.bytecode_len) {
3033 free(bytecode);
3034 ret = LTTNG_ERR_FILTER_INVAL;
3035 goto error;
3036 }
3037
3038 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3039 cmd_ctx->lsm->u.enable.channel_name,
3040 &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]);
3041 break;
3042 }
3043 case LTTNG_DATA_PENDING:
3044 {
3045 ret = cmd_data_pending(cmd_ctx->session);
3046 break;
3047 }
3048 default:
3049 ret = LTTNG_ERR_UND;
3050 break;
3051 }
3052
3053 error:
3054 if (cmd_ctx->llm == NULL) {
3055 DBG("Missing llm structure. Allocating one.");
3056 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3057 goto setup_error;
3058 }
3059 }
3060 /* Set return code */
3061 cmd_ctx->llm->ret_code = ret;
3062 setup_error:
3063 if (cmd_ctx->session) {
3064 session_unlock(cmd_ctx->session);
3065 }
3066 if (need_tracing_session) {
3067 session_unlock_list();
3068 }
3069 init_setup_error:
3070 return ret;
3071 }
3072
3073 /*
3074 * Thread managing health check socket.
3075 */
3076 static void *thread_manage_health(void *data)
3077 {
3078 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3079 uint32_t revents, nb_fd;
3080 struct lttng_poll_event events;
3081 struct lttcomm_health_msg msg;
3082 struct lttcomm_health_data reply;
3083
3084 DBG("[thread] Manage health check started");
3085
3086 rcu_register_thread();
3087
3088 /* Create unix socket */
3089 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3090 if (sock < 0) {
3091 ERR("Unable to create health check Unix socket");
3092 ret = -1;
3093 goto error;
3094 }
3095
3096 /*
3097 * Set the CLOEXEC flag. Return code is useless because either way, the
3098 * show must go on.
3099 */
3100 (void) utils_set_fd_cloexec(sock);
3101
3102 ret = lttcomm_listen_unix_sock(sock);
3103 if (ret < 0) {
3104 goto error;
3105 }
3106
3107 /*
3108 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3109 * more will be added to this poll set.
3110 */
3111 ret = sessiond_set_thread_pollset(&events, 2);
3112 if (ret < 0) {
3113 goto error;
3114 }
3115
3116 /* Add the application registration socket */
3117 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3118 if (ret < 0) {
3119 goto error;
3120 }
3121
3122 while (1) {
3123 DBG("Health check ready");
3124
3125 /* Inifinite blocking call, waiting for transmission */
3126 restart:
3127 ret = lttng_poll_wait(&events, -1);
3128 if (ret < 0) {
3129 /*
3130 * Restart interrupted system call.
3131 */
3132 if (errno == EINTR) {
3133 goto restart;
3134 }
3135 goto error;
3136 }
3137
3138 nb_fd = ret;
3139
3140 for (i = 0; i < nb_fd; i++) {
3141 /* Fetch once the poll data */
3142 revents = LTTNG_POLL_GETEV(&events, i);
3143 pollfd = LTTNG_POLL_GETFD(&events, i);
3144
3145 /* Thread quit pipe has been closed. Killing thread. */
3146 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3147 if (ret) {
3148 err = 0;
3149 goto exit;
3150 }
3151
3152 /* Event on the registration socket */
3153 if (pollfd == sock) {
3154 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3155 ERR("Health socket poll error");
3156 goto error;
3157 }
3158 }
3159 }
3160
3161 new_sock = lttcomm_accept_unix_sock(sock);
3162 if (new_sock < 0) {
3163 goto error;
3164 }
3165
3166 /*
3167 * Set the CLOEXEC flag. Return code is useless because either way, the
3168 * show must go on.
3169 */
3170 (void) utils_set_fd_cloexec(new_sock);
3171
3172 DBG("Receiving data from client for health...");
3173 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3174 if (ret <= 0) {
3175 DBG("Nothing recv() from client... continuing");
3176 ret = close(new_sock);
3177 if (ret) {
3178 PERROR("close");
3179 }
3180 new_sock = -1;
3181 continue;
3182 }
3183
3184 rcu_thread_online();
3185
3186 switch (msg.component) {
3187 case LTTNG_HEALTH_CMD:
3188 reply.ret_code = health_check_state(HEALTH_TYPE_CMD);
3189 break;
3190 case LTTNG_HEALTH_APP_MANAGE:
3191 reply.ret_code = health_check_state(HEALTH_TYPE_APP_MANAGE);
3192 break;
3193 case LTTNG_HEALTH_APP_REG:
3194 reply.ret_code = health_check_state(HEALTH_TYPE_APP_REG);
3195 break;
3196 case LTTNG_HEALTH_KERNEL:
3197 reply.ret_code = health_check_state(HEALTH_TYPE_KERNEL);
3198 break;
3199 case LTTNG_HEALTH_CONSUMER:
3200 reply.ret_code = check_consumer_health();
3201 break;
3202 case LTTNG_HEALTH_ALL:
3203 reply.ret_code =
3204 health_check_state(HEALTH_TYPE_APP_MANAGE) &&
3205 health_check_state(HEALTH_TYPE_APP_REG) &&
3206 health_check_state(HEALTH_TYPE_CMD) &&
3207 health_check_state(HEALTH_TYPE_KERNEL) &&
3208 check_consumer_health();
3209 break;
3210 default:
3211 reply.ret_code = LTTNG_ERR_UND;
3212 break;
3213 }
3214
3215 /*
3216 * Flip ret value since 0 is a success and 1 indicates a bad health for
3217 * the client where in the sessiond it is the opposite. Again, this is
3218 * just to make things easier for us poor developer which enjoy a lot
3219 * lazyness.
3220 */
3221 if (reply.ret_code == 0 || reply.ret_code == 1) {
3222 reply.ret_code = !reply.ret_code;
3223 }
3224
3225 DBG2("Health check return value %d", reply.ret_code);
3226
3227 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3228 if (ret < 0) {
3229 ERR("Failed to send health data back to client");
3230 }
3231
3232 /* End of transmission */
3233 ret = close(new_sock);
3234 if (ret) {
3235 PERROR("close");
3236 }
3237 new_sock = -1;
3238 }
3239
3240 exit:
3241 error:
3242 if (err) {
3243 ERR("Health error occurred in %s", __func__);
3244 }
3245 DBG("Health check thread dying");
3246 unlink(health_unix_sock_path);
3247 if (sock >= 0) {
3248 ret = close(sock);
3249 if (ret) {
3250 PERROR("close");
3251 }
3252 }
3253 if (new_sock >= 0) {
3254 ret = close(new_sock);
3255 if (ret) {
3256 PERROR("close");
3257 }
3258 }
3259
3260 lttng_poll_clean(&events);
3261
3262 rcu_unregister_thread();
3263 return NULL;
3264 }
3265
3266 /*
3267 * This thread manage all clients request using the unix client socket for
3268 * communication.
3269 */
3270 static void *thread_manage_clients(void *data)
3271 {
3272 int sock = -1, ret, i, pollfd, err = -1;
3273 int sock_error;
3274 uint32_t revents, nb_fd;
3275 struct command_ctx *cmd_ctx = NULL;
3276 struct lttng_poll_event events;
3277
3278 DBG("[thread] Manage client started");
3279
3280 rcu_register_thread();
3281
3282 health_register(HEALTH_TYPE_CMD);
3283
3284 if (testpoint(thread_manage_clients)) {
3285 goto error_testpoint;
3286 }
3287
3288 health_code_update();
3289
3290 ret = lttcomm_listen_unix_sock(client_sock);
3291 if (ret < 0) {
3292 goto error_listen;
3293 }
3294
3295 /*
3296 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3297 * more will be added to this poll set.
3298 */
3299 ret = sessiond_set_thread_pollset(&events, 2);
3300 if (ret < 0) {
3301 goto error_create_poll;
3302 }
3303
3304 /* Add the application registration socket */
3305 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3306 if (ret < 0) {
3307 goto error;
3308 }
3309
3310 /*
3311 * Notify parent pid that we are ready to accept command for client side.
3312 */
3313 if (opt_sig_parent) {
3314 kill(ppid, SIGUSR1);
3315 }
3316
3317 if (testpoint(thread_manage_clients_before_loop)) {
3318 goto error;
3319 }
3320
3321 health_code_update();
3322
3323 while (1) {
3324 DBG("Accepting client command ...");
3325
3326 /* Inifinite blocking call, waiting for transmission */
3327 restart:
3328 health_poll_entry();
3329 ret = lttng_poll_wait(&events, -1);
3330 health_poll_exit();
3331 if (ret < 0) {
3332 /*
3333 * Restart interrupted system call.
3334 */
3335 if (errno == EINTR) {
3336 goto restart;
3337 }
3338 goto error;
3339 }
3340
3341 nb_fd = ret;
3342
3343 for (i = 0; i < nb_fd; i++) {
3344 /* Fetch once the poll data */
3345 revents = LTTNG_POLL_GETEV(&events, i);
3346 pollfd = LTTNG_POLL_GETFD(&events, i);
3347
3348 health_code_update();
3349
3350 /* Thread quit pipe has been closed. Killing thread. */
3351 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3352 if (ret) {
3353 err = 0;
3354 goto exit;
3355 }
3356
3357 /* Event on the registration socket */
3358 if (pollfd == client_sock) {
3359 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3360 ERR("Client socket poll error");
3361 goto error;
3362 }
3363 }
3364 }
3365
3366 DBG("Wait for client response");
3367
3368 health_code_update();
3369
3370 sock = lttcomm_accept_unix_sock(client_sock);
3371 if (sock < 0) {
3372 goto error;
3373 }
3374
3375 /*
3376 * Set the CLOEXEC flag. Return code is useless because either way, the
3377 * show must go on.
3378 */
3379 (void) utils_set_fd_cloexec(sock);
3380
3381 /* Set socket option for credentials retrieval */
3382 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3383 if (ret < 0) {
3384 goto error;
3385 }
3386
3387 /* Allocate context command to process the client request */
3388 cmd_ctx = zmalloc(sizeof(struct command_ctx));
3389 if (cmd_ctx == NULL) {
3390 PERROR("zmalloc cmd_ctx");
3391 goto error;
3392 }
3393
3394 /* Allocate data buffer for reception */
3395 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
3396 if (cmd_ctx->lsm == NULL) {
3397 PERROR("zmalloc cmd_ctx->lsm");
3398 goto error;
3399 }
3400
3401 cmd_ctx->llm = NULL;
3402 cmd_ctx->session = NULL;
3403
3404 health_code_update();
3405
3406 /*
3407 * Data is received from the lttng client. The struct
3408 * lttcomm_session_msg (lsm) contains the command and data request of
3409 * the client.
3410 */
3411 DBG("Receiving data from client ...");
3412 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3413 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
3414 if (ret <= 0) {
3415 DBG("Nothing recv() from client... continuing");
3416 ret = close(sock);
3417 if (ret) {
3418 PERROR("close");
3419 }
3420 sock = -1;
3421 clean_command_ctx(&cmd_ctx);
3422 continue;
3423 }
3424
3425 health_code_update();
3426
3427 // TODO: Validate cmd_ctx including sanity check for
3428 // security purpose.
3429
3430 rcu_thread_online();
3431 /*
3432 * This function dispatch the work to the kernel or userspace tracer
3433 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3434 * informations for the client. The command context struct contains
3435 * everything this function may needs.
3436 */
3437 ret = process_client_msg(cmd_ctx, sock, &sock_error);
3438 rcu_thread_offline();
3439 if (ret < 0) {
3440 if (sock_error) {
3441 ret = close(sock);
3442 if (ret) {
3443 PERROR("close");
3444 }
3445 sock = -1;
3446 }
3447 /*
3448 * TODO: Inform client somehow of the fatal error. At
3449 * this point, ret < 0 means that a zmalloc failed
3450 * (ENOMEM). Error detected but still accept
3451 * command, unless a socket error has been
3452 * detected.
3453 */
3454 clean_command_ctx(&cmd_ctx);
3455 continue;
3456 }
3457
3458 health_code_update();
3459
3460 DBG("Sending response (size: %d, retcode: %s)",
3461 cmd_ctx->lttng_msg_size,
3462 lttng_strerror(-cmd_ctx->llm->ret_code));
3463 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
3464 if (ret < 0) {
3465 ERR("Failed to send data back to client");
3466 }
3467
3468 /* End of transmission */
3469 ret = close(sock);
3470 if (ret) {
3471 PERROR("close");
3472 }
3473 sock = -1;
3474
3475 clean_command_ctx(&cmd_ctx);
3476
3477 health_code_update();
3478 }
3479
3480 exit:
3481 error:
3482 if (sock >= 0) {
3483 ret = close(sock);
3484 if (ret) {
3485 PERROR("close");
3486 }
3487 }
3488
3489 lttng_poll_clean(&events);
3490 clean_command_ctx(&cmd_ctx);
3491
3492 error_listen:
3493 error_create_poll:
3494 error_testpoint:
3495 unlink(client_unix_sock_path);
3496 if (client_sock >= 0) {
3497 ret = close(client_sock);
3498 if (ret) {
3499 PERROR("close");
3500 }
3501 }
3502
3503 if (err) {
3504 health_error();
3505 ERR("Health error occurred in %s", __func__);
3506 }
3507
3508 health_unregister();
3509
3510 DBG("Client thread dying");
3511
3512 rcu_unregister_thread();
3513 return NULL;
3514 }
3515
3516
3517 /*
3518 * usage function on stderr
3519 */
3520 static void usage(void)
3521 {
3522 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
3523 fprintf(stderr, " -h, --help Display this usage.\n");
3524 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3525 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3526 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3527 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3528 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3529 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3530 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3531 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3532 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3533 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3534 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3535 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3536 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
3537 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3538 fprintf(stderr, " -V, --version Show version number.\n");
3539 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3540 fprintf(stderr, " -q, --quiet No output at all.\n");
3541 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3542 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3543 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3544 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
3545 }
3546
3547 /*
3548 * daemon argument parsing
3549 */
3550 static int parse_args(int argc, char **argv)
3551 {
3552 int c;
3553
3554 static struct option long_options[] = {
3555 { "client-sock", 1, 0, 'c' },
3556 { "apps-sock", 1, 0, 'a' },
3557 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3558 { "kconsumerd-err-sock", 1, 0, 'E' },
3559 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3560 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3561 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3562 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3563 { "consumerd32-path", 1, 0, 'u' },
3564 { "consumerd32-libdir", 1, 0, 'U' },
3565 { "consumerd64-path", 1, 0, 't' },
3566 { "consumerd64-libdir", 1, 0, 'T' },
3567 { "daemonize", 0, 0, 'd' },
3568 { "sig-parent", 0, 0, 'S' },
3569 { "help", 0, 0, 'h' },
3570 { "group", 1, 0, 'g' },
3571 { "version", 0, 0, 'V' },
3572 { "quiet", 0, 0, 'q' },
3573 { "verbose", 0, 0, 'v' },
3574 { "verbose-consumer", 0, 0, 'Z' },
3575 { "no-kernel", 0, 0, 'N' },
3576 { "pidfile", 1, 0, 'p' },
3577 { NULL, 0, 0, 0 }
3578 };
3579
3580 while (1) {
3581 int option_index = 0;
3582 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
3583 long_options, &option_index);
3584 if (c == -1) {
3585 break;
3586 }
3587
3588 switch (c) {
3589 case 0:
3590 fprintf(stderr, "option %s", long_options[option_index].name);
3591 if (optarg) {
3592 fprintf(stderr, " with arg %s\n", optarg);
3593 }
3594 break;
3595 case 'c':
3596 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
3597 break;
3598 case 'a':
3599 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
3600 break;
3601 case 'd':
3602 opt_daemon = 1;
3603 break;
3604 case 'g':
3605 opt_tracing_group = optarg;
3606 break;
3607 case 'h':
3608 usage();
3609 exit(EXIT_FAILURE);
3610 case 'V':
3611 fprintf(stdout, "%s\n", VERSION);
3612 exit(EXIT_SUCCESS);
3613 case 'S':
3614 opt_sig_parent = 1;
3615 break;
3616 case 'E':
3617 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3618 break;
3619 case 'C':
3620 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3621 break;
3622 case 'F':
3623 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3624 break;
3625 case 'D':
3626 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3627 break;
3628 case 'H':
3629 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3630 break;
3631 case 'G':
3632 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3633 break;
3634 case 'N':
3635 opt_no_kernel = 1;
3636 break;
3637 case 'q':
3638 lttng_opt_quiet = 1;
3639 break;
3640 case 'v':
3641 /* Verbose level can increase using multiple -v */
3642 lttng_opt_verbose += 1;
3643 break;
3644 case 'Z':
3645 opt_verbose_consumer += 1;
3646 break;
3647 case 'u':
3648 consumerd32_bin= optarg;
3649 break;
3650 case 'U':
3651 consumerd32_libdir = optarg;
3652 break;
3653 case 't':
3654 consumerd64_bin = optarg;
3655 break;
3656 case 'T':
3657 consumerd64_libdir = optarg;
3658 break;
3659 case 'p':
3660 opt_pidfile = optarg;
3661 break;
3662 default:
3663 /* Unknown option or other error.
3664 * Error is printed by getopt, just return */
3665 return -1;
3666 }
3667 }
3668
3669 return 0;
3670 }
3671
3672 /*
3673 * Creates the two needed socket by the daemon.
3674 * apps_sock - The communication socket for all UST apps.
3675 * client_sock - The communication of the cli tool (lttng).
3676 */
3677 static int init_daemon_socket(void)
3678 {
3679 int ret = 0;
3680 mode_t old_umask;
3681
3682 old_umask = umask(0);
3683
3684 /* Create client tool unix socket */
3685 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
3686 if (client_sock < 0) {
3687 ERR("Create unix sock failed: %s", client_unix_sock_path);
3688 ret = -1;
3689 goto end;
3690 }
3691
3692 /* Set the cloexec flag */
3693 ret = utils_set_fd_cloexec(client_sock);
3694 if (ret < 0) {
3695 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
3696 "Continuing but note that the consumer daemon will have a "
3697 "reference to this socket on exec()", client_sock);
3698 }
3699
3700 /* File permission MUST be 660 */
3701 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3702 if (ret < 0) {
3703 ERR("Set file permissions failed: %s", client_unix_sock_path);
3704 PERROR("chmod");
3705 goto end;
3706 }
3707
3708 /* Create the application unix socket */
3709 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
3710 if (apps_sock < 0) {
3711 ERR("Create unix sock failed: %s", apps_unix_sock_path);
3712 ret = -1;
3713 goto end;
3714 }
3715
3716 /* Set the cloexec flag */
3717 ret = utils_set_fd_cloexec(apps_sock);
3718 if (ret < 0) {
3719 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
3720 "Continuing but note that the consumer daemon will have a "
3721 "reference to this socket on exec()", apps_sock);
3722 }
3723
3724 /* File permission MUST be 666 */
3725 ret = chmod(apps_unix_sock_path,
3726 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
3727 if (ret < 0) {
3728 ERR("Set file permissions failed: %s", apps_unix_sock_path);
3729 PERROR("chmod");
3730 goto end;
3731 }
3732
3733 DBG3("Session daemon client socket %d and application socket %d created",
3734 client_sock, apps_sock);
3735
3736 end:
3737 umask(old_umask);
3738 return ret;
3739 }
3740
3741 /*
3742 * Check if the global socket is available, and if a daemon is answering at the
3743 * other side. If yes, error is returned.
3744 */
3745 static int check_existing_daemon(void)
3746 {
3747 /* Is there anybody out there ? */
3748 if (lttng_session_daemon_alive()) {
3749 return -EEXIST;
3750 }
3751
3752 return 0;
3753 }
3754
3755 /*
3756 * Set the tracing group gid onto the client socket.
3757 *
3758 * Race window between mkdir and chown is OK because we are going from more
3759 * permissive (root.root) to less permissive (root.tracing).
3760 */
3761 static int set_permissions(char *rundir)
3762 {
3763 int ret;
3764 gid_t gid;
3765
3766 ret = allowed_group();
3767 if (ret < 0) {
3768 WARN("No tracing group detected");
3769 ret = 0;
3770 goto end;
3771 }
3772
3773 gid = ret;
3774
3775 /* Set lttng run dir */
3776 ret = chown(rundir, 0, gid);
3777 if (ret < 0) {
3778 ERR("Unable to set group on %s", rundir);
3779 PERROR("chown");
3780 }
3781
3782 /* Ensure tracing group can search the run dir */
3783 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
3784 if (ret < 0) {
3785 ERR("Unable to set permissions on %s", rundir);
3786 PERROR("chmod");
3787 }
3788
3789 /* lttng client socket path */
3790 ret = chown(client_unix_sock_path, 0, gid);
3791 if (ret < 0) {
3792 ERR("Unable to set group on %s", client_unix_sock_path);
3793 PERROR("chown");
3794 }
3795
3796 /* kconsumer error socket path */
3797 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
3798 if (ret < 0) {
3799 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
3800 PERROR("chown");
3801 }
3802
3803 /* 64-bit ustconsumer error socket path */
3804 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
3805 if (ret < 0) {
3806 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
3807 PERROR("chown");
3808 }
3809
3810 /* 32-bit ustconsumer compat32 error socket path */
3811 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3812 if (ret < 0) {
3813 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
3814 PERROR("chown");
3815 }
3816
3817 DBG("All permissions are set");
3818
3819 end:
3820 return ret;
3821 }
3822
3823 /*
3824 * Create the lttng run directory needed for all global sockets and pipe.
3825 */
3826 static int create_lttng_rundir(const char *rundir)
3827 {
3828 int ret;
3829
3830 DBG3("Creating LTTng run directory: %s", rundir);
3831
3832 ret = mkdir(rundir, S_IRWXU);
3833 if (ret < 0) {
3834 if (errno != EEXIST) {
3835 ERR("Unable to create %s", rundir);
3836 goto error;
3837 } else {
3838 ret = 0;
3839 }
3840 }
3841
3842 error:
3843 return ret;
3844 }
3845
3846 /*
3847 * Setup sockets and directory needed by the kconsumerd communication with the
3848 * session daemon.
3849 */
3850 static int set_consumer_sockets(struct consumer_data *consumer_data,
3851 const char *rundir)
3852 {
3853 int ret;
3854 char path[PATH_MAX];
3855
3856 switch (consumer_data->type) {
3857 case LTTNG_CONSUMER_KERNEL:
3858 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
3859 break;
3860 case LTTNG_CONSUMER64_UST:
3861 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
3862 break;
3863 case LTTNG_CONSUMER32_UST:
3864 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
3865 break;
3866 default:
3867 ERR("Consumer type unknown");
3868 ret = -EINVAL;
3869 goto error;
3870 }
3871
3872 DBG2("Creating consumer directory: %s", path);
3873
3874 ret = mkdir(path, S_IRWXU);
3875 if (ret < 0) {
3876 if (errno != EEXIST) {
3877 PERROR("mkdir");
3878 ERR("Failed to create %s", path);
3879 goto error;
3880 }
3881 ret = -1;
3882 }
3883
3884 /* Create the kconsumerd error unix socket */
3885 consumer_data->err_sock =
3886 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
3887 if (consumer_data->err_sock < 0) {
3888 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
3889 ret = -1;
3890 goto error;
3891 }
3892
3893 /* File permission MUST be 660 */
3894 ret = chmod(consumer_data->err_unix_sock_path,
3895 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3896 if (ret < 0) {
3897 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
3898 PERROR("chmod");
3899 goto error;
3900 }
3901
3902 error:
3903 return ret;
3904 }
3905
3906 /*
3907 * Signal handler for the daemon
3908 *
3909 * Simply stop all worker threads, leaving main() return gracefully after
3910 * joining all threads and calling cleanup().
3911 */
3912 static void sighandler(int sig)
3913 {
3914 switch (sig) {
3915 case SIGPIPE:
3916 DBG("SIGPIPE caught");
3917 return;
3918 case SIGINT:
3919 DBG("SIGINT caught");
3920 stop_threads();
3921 break;
3922 case SIGTERM:
3923 DBG("SIGTERM caught");
3924 stop_threads();
3925 break;
3926 default:
3927 break;
3928 }
3929 }
3930
3931 /*
3932 * Setup signal handler for :
3933 * SIGINT, SIGTERM, SIGPIPE
3934 */
3935 static int set_signal_handler(void)
3936 {
3937 int ret = 0;
3938 struct sigaction sa;
3939 sigset_t sigset;
3940
3941 if ((ret = sigemptyset(&sigset)) < 0) {
3942 PERROR("sigemptyset");
3943 return ret;
3944 }
3945
3946 sa.sa_handler = sighandler;
3947 sa.sa_mask = sigset;
3948 sa.sa_flags = 0;
3949 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3950 PERROR("sigaction");
3951 return ret;
3952 }
3953
3954 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3955 PERROR("sigaction");
3956 return ret;
3957 }
3958
3959 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3960 PERROR("sigaction");
3961 return ret;
3962 }
3963
3964 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3965
3966 return ret;
3967 }
3968
3969 /*
3970 * Set open files limit to unlimited. This daemon can open a large number of
3971 * file descriptors in order to consumer multiple kernel traces.
3972 */
3973 static void set_ulimit(void)
3974 {
3975 int ret;
3976 struct rlimit lim;
3977
3978 /* The kernel does not allowed an infinite limit for open files */
3979 lim.rlim_cur = 65535;
3980 lim.rlim_max = 65535;
3981
3982 ret = setrlimit(RLIMIT_NOFILE, &lim);
3983 if (ret < 0) {
3984 PERROR("failed to set open files limit");
3985 }
3986 }
3987
3988 /*
3989 * Write pidfile using the rundir and opt_pidfile.
3990 */
3991 static void write_pidfile(void)
3992 {
3993 int ret;
3994 char pidfile_path[PATH_MAX];
3995
3996 assert(rundir);
3997
3998 if (opt_pidfile) {
3999 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
4000 } else {
4001 /* Build pidfile path from rundir and opt_pidfile. */
4002 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
4003 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
4004 if (ret < 0) {
4005 PERROR("snprintf pidfile path");
4006 goto error;
4007 }
4008 }
4009
4010 /*
4011 * Create pid file in rundir. Return value is of no importance. The
4012 * execution will continue even though we are not able to write the file.
4013 */
4014 (void) utils_create_pid_file(getpid(), pidfile_path);
4015
4016 error:
4017 return;
4018 }
4019
4020 /*
4021 * main
4022 */
4023 int main(int argc, char **argv)
4024 {
4025 int ret = 0;
4026 void *status;
4027 const char *home_path, *env_app_timeout;
4028
4029 init_kernel_workarounds();
4030
4031 rcu_register_thread();
4032
4033 setup_consumerd_path();
4034
4035 /* Parse arguments */
4036 progname = argv[0];
4037 if ((ret = parse_args(argc, argv)) < 0) {
4038 goto error;
4039 }
4040
4041 /* Daemonize */
4042 if (opt_daemon) {
4043 int i;
4044
4045 /*
4046 * fork
4047 * child: setsid, close FD 0, 1, 2, chdir /
4048 * parent: exit (if fork is successful)
4049 */
4050 ret = daemon(0, 0);
4051 if (ret < 0) {
4052 PERROR("daemon");
4053 goto error;
4054 }
4055 /*
4056 * We are in the child. Make sure all other file
4057 * descriptors are closed, in case we are called with
4058 * more opened file descriptors than the standard ones.
4059 */
4060 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4061 (void) close(i);
4062 }
4063 }
4064
4065 /* Create thread quit pipe */
4066 if ((ret = init_thread_quit_pipe()) < 0) {
4067 goto error;
4068 }
4069
4070 /* Check if daemon is UID = 0 */
4071 is_root = !getuid();
4072
4073 if (is_root) {
4074 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4075
4076 /* Create global run dir with root access */
4077 ret = create_lttng_rundir(rundir);
4078 if (ret < 0) {
4079 goto error;
4080 }
4081
4082 if (strlen(apps_unix_sock_path) == 0) {
4083 snprintf(apps_unix_sock_path, PATH_MAX,
4084 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4085 }
4086
4087 if (strlen(client_unix_sock_path) == 0) {
4088 snprintf(client_unix_sock_path, PATH_MAX,
4089 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4090 }
4091
4092 /* Set global SHM for ust */
4093 if (strlen(wait_shm_path) == 0) {
4094 snprintf(wait_shm_path, PATH_MAX,
4095 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4096 }
4097
4098 if (strlen(health_unix_sock_path) == 0) {
4099 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4100 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4101 }
4102
4103 /* Setup kernel consumerd path */
4104 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
4105 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4106 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
4107 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4108
4109 DBG2("Kernel consumer err path: %s",
4110 kconsumer_data.err_unix_sock_path);
4111 DBG2("Kernel consumer cmd path: %s",
4112 kconsumer_data.cmd_unix_sock_path);
4113 } else {
4114 home_path = get_home_dir();
4115 if (home_path == NULL) {
4116 /* TODO: Add --socket PATH option */
4117 ERR("Can't get HOME directory for sockets creation.");
4118 ret = -EPERM;
4119 goto error;
4120 }
4121
4122 /*
4123 * Create rundir from home path. This will create something like
4124 * $HOME/.lttng
4125 */
4126 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
4127 if (ret < 0) {
4128 ret = -ENOMEM;
4129 goto error;
4130 }
4131
4132 ret = create_lttng_rundir(rundir);
4133 if (ret < 0) {
4134 goto error;
4135 }
4136
4137 if (strlen(apps_unix_sock_path) == 0) {
4138 snprintf(apps_unix_sock_path, PATH_MAX,
4139 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
4140 }
4141
4142 /* Set the cli tool unix socket path */
4143 if (strlen(client_unix_sock_path) == 0) {
4144 snprintf(client_unix_sock_path, PATH_MAX,
4145 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
4146 }
4147
4148 /* Set global SHM for ust */
4149 if (strlen(wait_shm_path) == 0) {
4150 snprintf(wait_shm_path, PATH_MAX,
4151 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
4152 }
4153
4154 /* Set health check Unix path */
4155 if (strlen(health_unix_sock_path) == 0) {
4156 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4157 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4158 }
4159 }
4160
4161 /* Set consumer initial state */
4162 kernel_consumerd_state = CONSUMER_STOPPED;
4163 ust_consumerd_state = CONSUMER_STOPPED;
4164
4165 DBG("Client socket path %s", client_unix_sock_path);
4166 DBG("Application socket path %s", apps_unix_sock_path);
4167 DBG("Application wait path %s", wait_shm_path);
4168 DBG("LTTng run directory path: %s", rundir);
4169
4170 /* 32 bits consumerd path setup */
4171 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
4172 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4173 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
4174 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4175
4176 DBG2("UST consumer 32 bits err path: %s",
4177 ustconsumer32_data.err_unix_sock_path);
4178 DBG2("UST consumer 32 bits cmd path: %s",
4179 ustconsumer32_data.cmd_unix_sock_path);
4180
4181 /* 64 bits consumerd path setup */
4182 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
4183 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4184 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
4185 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4186
4187 DBG2("UST consumer 64 bits err path: %s",
4188 ustconsumer64_data.err_unix_sock_path);
4189 DBG2("UST consumer 64 bits cmd path: %s",
4190 ustconsumer64_data.cmd_unix_sock_path);
4191
4192 /*
4193 * See if daemon already exist.
4194 */
4195 if ((ret = check_existing_daemon()) < 0) {
4196 ERR("Already running daemon.\n");
4197 /*
4198 * We do not goto exit because we must not cleanup()
4199 * because a daemon is already running.
4200 */
4201 goto error;
4202 }
4203
4204 /*
4205 * Init UST app hash table. Alloc hash table before this point since
4206 * cleanup() can get called after that point.
4207 */
4208 ust_app_ht_alloc();
4209
4210 /* After this point, we can safely call cleanup() with "goto exit" */
4211
4212 /*
4213 * These actions must be executed as root. We do that *after* setting up
4214 * the sockets path because we MUST make the check for another daemon using
4215 * those paths *before* trying to set the kernel consumer sockets and init
4216 * kernel tracer.
4217 */
4218 if (is_root) {
4219 ret = set_consumer_sockets(&kconsumer_data, rundir);
4220 if (ret < 0) {
4221 goto exit;
4222 }
4223
4224 /* Setup kernel tracer */
4225 if (!opt_no_kernel) {
4226 init_kernel_tracer();
4227 }
4228
4229 /* Set ulimit for open files */
4230 set_ulimit();
4231 }
4232 /* init lttng_fd tracking must be done after set_ulimit. */
4233 lttng_fd_init();
4234
4235 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4236 if (ret < 0) {
4237 goto exit;
4238 }
4239
4240 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4241 if (ret < 0) {
4242 goto exit;
4243 }
4244
4245 if ((ret = set_signal_handler()) < 0) {
4246 goto exit;
4247 }
4248
4249 /* Setup the needed unix socket */
4250 if ((ret = init_daemon_socket()) < 0) {
4251 goto exit;
4252 }
4253
4254 /* Set credentials to socket */
4255 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
4256 goto exit;
4257 }
4258
4259 /* Get parent pid if -S, --sig-parent is specified. */
4260 if (opt_sig_parent) {
4261 ppid = getppid();
4262 }
4263
4264 /* Setup the kernel pipe for waking up the kernel thread */
4265 if (is_root && !opt_no_kernel) {
4266 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4267 goto exit;
4268 }
4269 }
4270
4271 /* Setup the thread apps communication pipe. */
4272 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
4273 goto exit;
4274 }
4275
4276 /* Setup the thread apps notify communication pipe. */
4277 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
4278 goto exit;
4279 }
4280
4281 /* Initialize global buffer per UID and PID registry. */
4282 buffer_reg_init_uid_registry();
4283 buffer_reg_init_pid_registry();
4284
4285 /* Init UST command queue. */
4286 cds_wfq_init(&ust_cmd_queue.queue);
4287
4288 /*
4289 * Get session list pointer. This pointer MUST NOT be free(). This list is
4290 * statically declared in session.c
4291 */
4292 session_list_ptr = session_get_list();
4293
4294 /* Set up max poll set size */
4295 lttng_poll_set_max_size();
4296
4297 cmd_init();
4298
4299 /* Check for the application socket timeout env variable. */
4300 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
4301 if (env_app_timeout) {
4302 app_socket_timeout = atoi(env_app_timeout);
4303 } else {
4304 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
4305 }
4306
4307 write_pidfile();
4308
4309 /* Create thread to manage the client socket */
4310 ret = pthread_create(&health_thread, NULL,
4311 thread_manage_health, (void *) NULL);
4312 if (ret != 0) {
4313 PERROR("pthread_create health");
4314 goto exit_health;
4315 }
4316
4317 /* Create thread to manage the client socket */
4318 ret = pthread_create(&client_thread, NULL,
4319 thread_manage_clients, (void *) NULL);
4320 if (ret != 0) {
4321 PERROR("pthread_create clients");
4322 goto exit_client;
4323 }
4324
4325 /* Create thread to dispatch registration */
4326 ret = pthread_create(&dispatch_thread, NULL,
4327 thread_dispatch_ust_registration, (void *) NULL);
4328 if (ret != 0) {
4329 PERROR("pthread_create dispatch");
4330 goto exit_dispatch;
4331 }
4332
4333 /* Create thread to manage application registration. */
4334 ret = pthread_create(&reg_apps_thread, NULL,
4335 thread_registration_apps, (void *) NULL);
4336 if (ret != 0) {
4337 PERROR("pthread_create registration");
4338 goto exit_reg_apps;
4339 }
4340
4341 /* Create thread to manage application socket */
4342 ret = pthread_create(&apps_thread, NULL,
4343 thread_manage_apps, (void *) NULL);
4344 if (ret != 0) {
4345 PERROR("pthread_create apps");
4346 goto exit_apps;
4347 }
4348
4349 /* Create thread to manage application notify socket */
4350 ret = pthread_create(&apps_notify_thread, NULL,
4351 ust_thread_manage_notify, (void *) NULL);
4352 if (ret != 0) {
4353 PERROR("pthread_create apps");
4354 goto exit_apps;
4355 }
4356
4357 /* Don't start this thread if kernel tracing is not requested nor root */
4358 if (is_root && !opt_no_kernel) {
4359 /* Create kernel thread to manage kernel event */
4360 ret = pthread_create(&kernel_thread, NULL,
4361 thread_manage_kernel, (void *) NULL);
4362 if (ret != 0) {
4363 PERROR("pthread_create kernel");
4364 goto exit_kernel;
4365 }
4366
4367 ret = pthread_join(kernel_thread, &status);
4368 if (ret != 0) {
4369 PERROR("pthread_join");
4370 goto error; /* join error, exit without cleanup */
4371 }
4372 }
4373
4374 exit_kernel:
4375 ret = pthread_join(apps_thread, &status);
4376 if (ret != 0) {
4377 PERROR("pthread_join");
4378 goto error; /* join error, exit without cleanup */
4379 }
4380
4381 exit_apps:
4382 ret = pthread_join(reg_apps_thread, &status);
4383 if (ret != 0) {
4384 PERROR("pthread_join");
4385 goto error; /* join error, exit without cleanup */
4386 }
4387
4388 exit_reg_apps:
4389 ret = pthread_join(dispatch_thread, &status);
4390 if (ret != 0) {
4391 PERROR("pthread_join");
4392 goto error; /* join error, exit without cleanup */
4393 }
4394
4395 exit_dispatch:
4396 ret = pthread_join(client_thread, &status);
4397 if (ret != 0) {
4398 PERROR("pthread_join");
4399 goto error; /* join error, exit without cleanup */
4400 }
4401
4402 ret = join_consumer_thread(&kconsumer_data);
4403 if (ret != 0) {
4404 PERROR("join_consumer");
4405 goto error; /* join error, exit without cleanup */
4406 }
4407
4408 ret = join_consumer_thread(&ustconsumer32_data);
4409 if (ret != 0) {
4410 PERROR("join_consumer ust32");
4411 goto error; /* join error, exit without cleanup */
4412 }
4413
4414 ret = join_consumer_thread(&ustconsumer64_data);
4415 if (ret != 0) {
4416 PERROR("join_consumer ust64");
4417 goto error; /* join error, exit without cleanup */
4418 }
4419
4420 exit_client:
4421 ret = pthread_join(health_thread, &status);
4422 if (ret != 0) {
4423 PERROR("pthread_join health thread");
4424 goto error; /* join error, exit without cleanup */
4425 }
4426
4427 exit_health:
4428 exit:
4429 /*
4430 * cleanup() is called when no other thread is running.
4431 */
4432 rcu_thread_online();
4433 cleanup();
4434 rcu_thread_offline();
4435 rcu_unregister_thread();
4436 if (!ret) {
4437 exit(EXIT_SUCCESS);
4438 }
4439 error:
4440 exit(EXIT_FAILURE);
4441 }
This page took 0.182716 seconds and 4 git commands to generate.