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