Fix: Multiple health monitoring fixes
[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 <semaphore.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.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/poll.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
48
49 #include "lttng-sessiond.h"
50 #include "channel.h"
51 #include "consumer.h"
52 #include "context.h"
53 #include "event.h"
54 #include "kernel.h"
55 #include "kernel-consumer.h"
56 #include "modprobe.h"
57 #include "shm.h"
58 #include "ust-ctl.h"
59 #include "ust-consumer.h"
60 #include "utils.h"
61 #include "fd-limit.h"
62 #include "filter.h"
63 #include "health.h"
64
65 #define CONSUMERD_FILE "lttng-consumerd"
66
67 /* Const values */
68 const char default_home_dir[] = DEFAULT_HOME_DIR;
69 const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
70 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
71 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
72
73 const char *progname;
74 const char *opt_tracing_group;
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 /* Consumer daemon specific control data */
84 static struct consumer_data kconsumer_data = {
85 .type = LTTNG_CONSUMER_KERNEL,
86 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
87 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
88 .err_sock = -1,
89 .cmd_sock = -1,
90 };
91 static struct consumer_data ustconsumer64_data = {
92 .type = LTTNG_CONSUMER64_UST,
93 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
94 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
95 .err_sock = -1,
96 .cmd_sock = -1,
97 };
98 static struct consumer_data ustconsumer32_data = {
99 .type = LTTNG_CONSUMER32_UST,
100 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
101 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
102 .err_sock = -1,
103 .cmd_sock = -1,
104 };
105
106 /* Shared between threads */
107 static int dispatch_thread_exit;
108
109 /* Global application Unix socket path */
110 static char apps_unix_sock_path[PATH_MAX];
111 /* Global client Unix socket path */
112 static char client_unix_sock_path[PATH_MAX];
113 /* global wait shm path for UST */
114 static char wait_shm_path[PATH_MAX];
115 /* Global health check unix path */
116 static char health_unix_sock_path[PATH_MAX];
117
118 /* Sockets and FDs */
119 static int client_sock = -1;
120 static int apps_sock = -1;
121 static int kernel_tracer_fd = -1;
122 static int kernel_poll_pipe[2] = { -1, -1 };
123
124 /*
125 * Quit pipe for all threads. This permits a single cancellation point
126 * for all threads when receiving an event on the pipe.
127 */
128 static int thread_quit_pipe[2] = { -1, -1 };
129
130 /*
131 * This pipe is used to inform the thread managing application communication
132 * that a command is queued and ready to be processed.
133 */
134 static int apps_cmd_pipe[2] = { -1, -1 };
135
136 /* Pthread, Mutexes and Semaphores */
137 static pthread_t apps_thread;
138 static pthread_t reg_apps_thread;
139 static pthread_t client_thread;
140 static pthread_t kernel_thread;
141 static pthread_t dispatch_thread;
142 static pthread_t health_thread;
143
144 /*
145 * UST registration command queue. This queue is tied with a futex and uses a N
146 * wakers / 1 waiter implemented and detailed in futex.c/.h
147 *
148 * The thread_manage_apps and thread_dispatch_ust_registration interact with
149 * this queue and the wait/wake scheme.
150 */
151 static struct ust_cmd_queue ust_cmd_queue;
152
153 /*
154 * Pointer initialized before thread creation.
155 *
156 * This points to the tracing session list containing the session count and a
157 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
158 * MUST NOT be taken if you call a public function in session.c.
159 *
160 * The lock is nested inside the structure: session_list_ptr->lock. Please use
161 * session_lock_list and session_unlock_list for lock acquisition.
162 */
163 static struct ltt_session_list *session_list_ptr;
164
165 int ust_consumerd64_fd = -1;
166 int ust_consumerd32_fd = -1;
167
168 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
169 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
170 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
171 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
172
173 /*
174 * Consumer daemon state which is changed when spawning it, killing it or in
175 * case of a fatal error.
176 */
177 enum consumerd_state {
178 CONSUMER_STARTED = 1,
179 CONSUMER_STOPPED = 2,
180 CONSUMER_ERROR = 3,
181 };
182
183 /*
184 * This consumer daemon state is used to validate if a client command will be
185 * able to reach the consumer. If not, the client is informed. For instance,
186 * doing a "lttng start" when the consumer state is set to ERROR will return an
187 * error to the client.
188 *
189 * The following example shows a possible race condition of this scheme:
190 *
191 * consumer thread error happens
192 * client cmd arrives
193 * client cmd checks state -> still OK
194 * consumer thread exit, sets error
195 * client cmd try to talk to consumer
196 * ...
197 *
198 * However, since the consumer is a different daemon, we have no way of making
199 * sure the command will reach it safely even with this state flag. This is why
200 * we consider that up to the state validation during command processing, the
201 * command is safe. After that, we can not guarantee the correctness of the
202 * client request vis-a-vis the consumer.
203 */
204 static enum consumerd_state ust_consumerd_state;
205 static enum consumerd_state kernel_consumerd_state;
206
207 /*
208 * Used to keep a unique index for each relayd socket created where this value
209 * is associated with streams on the consumer so it can match the right relayd
210 * to send to.
211 *
212 * This value should be incremented atomically for safety purposes and future
213 * possible concurrent access.
214 */
215 static unsigned int relayd_net_seq_idx;
216
217 /* Used for the health monitoring of the session daemon. See health.h */
218 struct health_state health_thread_cmd;
219 struct health_state health_thread_app_manage;
220 struct health_state health_thread_app_reg;
221 struct health_state health_thread_kernel;
222
223 static
224 void setup_consumerd_path(void)
225 {
226 const char *bin, *libdir;
227
228 /*
229 * Allow INSTALL_BIN_PATH to be used as a target path for the
230 * native architecture size consumer if CONFIG_CONSUMER*_PATH
231 * has not been defined.
232 */
233 #if (CAA_BITS_PER_LONG == 32)
234 if (!consumerd32_bin[0]) {
235 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
236 }
237 if (!consumerd32_libdir[0]) {
238 consumerd32_libdir = INSTALL_LIB_PATH;
239 }
240 #elif (CAA_BITS_PER_LONG == 64)
241 if (!consumerd64_bin[0]) {
242 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
243 }
244 if (!consumerd64_libdir[0]) {
245 consumerd64_libdir = INSTALL_LIB_PATH;
246 }
247 #else
248 #error "Unknown bitness"
249 #endif
250
251 /*
252 * runtime env. var. overrides the build default.
253 */
254 bin = getenv("LTTNG_CONSUMERD32_BIN");
255 if (bin) {
256 consumerd32_bin = bin;
257 }
258 bin = getenv("LTTNG_CONSUMERD64_BIN");
259 if (bin) {
260 consumerd64_bin = bin;
261 }
262 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
263 if (libdir) {
264 consumerd32_libdir = libdir;
265 }
266 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
267 if (libdir) {
268 consumerd64_libdir = libdir;
269 }
270 }
271
272 /*
273 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
274 */
275 static int create_thread_poll_set(struct lttng_poll_event *events,
276 unsigned int size)
277 {
278 int ret;
279
280 if (events == NULL || size == 0) {
281 ret = -1;
282 goto error;
283 }
284
285 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
286 if (ret < 0) {
287 goto error;
288 }
289
290 /* Add quit pipe */
291 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
292 if (ret < 0) {
293 goto error;
294 }
295
296 return 0;
297
298 error:
299 return ret;
300 }
301
302 /*
303 * Check if the thread quit pipe was triggered.
304 *
305 * Return 1 if it was triggered else 0;
306 */
307 static int check_thread_quit_pipe(int fd, uint32_t events)
308 {
309 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
310 return 1;
311 }
312
313 return 0;
314 }
315
316 /*
317 * Return group ID of the tracing group or -1 if not found.
318 */
319 static gid_t allowed_group(void)
320 {
321 struct group *grp;
322
323 if (opt_tracing_group) {
324 grp = getgrnam(opt_tracing_group);
325 } else {
326 grp = getgrnam(default_tracing_group);
327 }
328 if (!grp) {
329 return -1;
330 } else {
331 return grp->gr_gid;
332 }
333 }
334
335 /*
336 * Init thread quit pipe.
337 *
338 * Return -1 on error or 0 if all pipes are created.
339 */
340 static int init_thread_quit_pipe(void)
341 {
342 int ret, i;
343
344 ret = pipe(thread_quit_pipe);
345 if (ret < 0) {
346 PERROR("thread quit pipe");
347 goto error;
348 }
349
350 for (i = 0; i < 2; i++) {
351 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
352 if (ret < 0) {
353 PERROR("fcntl");
354 goto error;
355 }
356 }
357
358 error:
359 return ret;
360 }
361
362 /*
363 * Complete teardown of a kernel session. This free all data structure related
364 * to a kernel session and update counter.
365 */
366 static void teardown_kernel_session(struct ltt_session *session)
367 {
368 if (!session->kernel_session) {
369 DBG3("No kernel session when tearing down session");
370 return;
371 }
372
373 DBG("Tearing down kernel session");
374
375 /*
376 * If a custom kernel consumer was registered, close the socket before
377 * tearing down the complete kernel session structure
378 */
379 if (kconsumer_data.cmd_sock >= 0 &&
380 session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
381 lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
382 }
383
384 trace_kernel_destroy_session(session->kernel_session);
385 }
386
387 /*
388 * Complete teardown of all UST sessions. This will free everything on his path
389 * and destroy the core essence of all ust sessions :)
390 */
391 static void teardown_ust_session(struct ltt_session *session)
392 {
393 int ret;
394
395 if (!session->ust_session) {
396 DBG3("No UST session when tearing down session");
397 return;
398 }
399
400 DBG("Tearing down UST session(s)");
401
402 ret = ust_app_destroy_trace_all(session->ust_session);
403 if (ret) {
404 ERR("Error in ust_app_destroy_trace_all");
405 }
406
407 trace_ust_destroy_session(session->ust_session);
408 }
409
410 /*
411 * Stop all threads by closing the thread quit pipe.
412 */
413 static void stop_threads(void)
414 {
415 int ret;
416
417 /* Stopping all threads */
418 DBG("Terminating all threads");
419 ret = notify_thread_pipe(thread_quit_pipe[1]);
420 if (ret < 0) {
421 ERR("write error on thread quit pipe");
422 }
423
424 /* Dispatch thread */
425 CMM_STORE_SHARED(dispatch_thread_exit, 1);
426 futex_nto1_wake(&ust_cmd_queue.futex);
427 }
428
429 /*
430 * Cleanup the daemon
431 */
432 static void cleanup(void)
433 {
434 int ret;
435 char *cmd;
436 struct ltt_session *sess, *stmp;
437
438 DBG("Cleaning up");
439
440 DBG("Removing %s directory", rundir);
441 ret = asprintf(&cmd, "rm -rf %s", rundir);
442 if (ret < 0) {
443 ERR("asprintf failed. Something is really wrong!");
444 }
445
446 /* Remove lttng run directory */
447 ret = system(cmd);
448 if (ret < 0) {
449 ERR("Unable to clean %s", rundir);
450 }
451 free(cmd);
452
453 DBG("Cleaning up all sessions");
454
455 /* Destroy session list mutex */
456 if (session_list_ptr != NULL) {
457 pthread_mutex_destroy(&session_list_ptr->lock);
458
459 /* Cleanup ALL session */
460 cds_list_for_each_entry_safe(sess, stmp,
461 &session_list_ptr->head, list) {
462 teardown_kernel_session(sess);
463 teardown_ust_session(sess);
464 free(sess);
465 }
466 }
467
468 DBG("Closing all UST sockets");
469 ust_app_clean_list();
470
471 pthread_mutex_destroy(&kconsumer_data.pid_mutex);
472
473 if (is_root && !opt_no_kernel) {
474 DBG2("Closing kernel fd");
475 if (kernel_tracer_fd >= 0) {
476 ret = close(kernel_tracer_fd);
477 if (ret) {
478 PERROR("close");
479 }
480 }
481 DBG("Unloading kernel modules");
482 modprobe_remove_lttng_all();
483 }
484 utils_close_pipe(kernel_poll_pipe);
485 utils_close_pipe(thread_quit_pipe);
486 utils_close_pipe(apps_cmd_pipe);
487
488 /* <fun> */
489 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
490 "Matthew, BEET driven development works!%c[%dm",
491 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
492 /* </fun> */
493 }
494
495 /*
496 * Send data on a unix socket using the liblttsessiondcomm API.
497 *
498 * Return lttcomm error code.
499 */
500 static int send_unix_sock(int sock, void *buf, size_t len)
501 {
502 /* Check valid length */
503 if (len <= 0) {
504 return -1;
505 }
506
507 return lttcomm_send_unix_sock(sock, buf, len);
508 }
509
510 /*
511 * Free memory of a command context structure.
512 */
513 static void clean_command_ctx(struct command_ctx **cmd_ctx)
514 {
515 DBG("Clean command context structure");
516 if (*cmd_ctx) {
517 if ((*cmd_ctx)->llm) {
518 free((*cmd_ctx)->llm);
519 }
520 if ((*cmd_ctx)->lsm) {
521 free((*cmd_ctx)->lsm);
522 }
523 free(*cmd_ctx);
524 *cmd_ctx = NULL;
525 }
526 }
527
528 /*
529 * Notify UST applications using the shm mmap futex.
530 */
531 static int notify_ust_apps(int active)
532 {
533 char *wait_shm_mmap;
534
535 DBG("Notifying applications of session daemon state: %d", active);
536
537 /* See shm.c for this call implying mmap, shm and futex calls */
538 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
539 if (wait_shm_mmap == NULL) {
540 goto error;
541 }
542
543 /* Wake waiting process */
544 futex_wait_update((int32_t *) wait_shm_mmap, active);
545
546 /* Apps notified successfully */
547 return 0;
548
549 error:
550 return -1;
551 }
552
553 /*
554 * Setup the outgoing data buffer for the response (llm) by allocating the
555 * right amount of memory and copying the original information from the lsm
556 * structure.
557 *
558 * Return total size of the buffer pointed by buf.
559 */
560 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
561 {
562 int ret, buf_size;
563
564 buf_size = size;
565
566 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
567 if (cmd_ctx->llm == NULL) {
568 PERROR("zmalloc");
569 ret = -ENOMEM;
570 goto error;
571 }
572
573 /* Copy common data */
574 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
575 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
576
577 cmd_ctx->llm->data_size = size;
578 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
579
580 return buf_size;
581
582 error:
583 return ret;
584 }
585
586 /*
587 * Update the kernel poll set of all channel fd available over all tracing
588 * session. Add the wakeup pipe at the end of the set.
589 */
590 static int update_kernel_poll(struct lttng_poll_event *events)
591 {
592 int ret;
593 struct ltt_session *session;
594 struct ltt_kernel_channel *channel;
595
596 DBG("Updating kernel poll set");
597
598 session_lock_list();
599 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
600 session_lock(session);
601 if (session->kernel_session == NULL) {
602 session_unlock(session);
603 continue;
604 }
605
606 cds_list_for_each_entry(channel,
607 &session->kernel_session->channel_list.head, list) {
608 /* Add channel fd to the kernel poll set */
609 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
610 if (ret < 0) {
611 session_unlock(session);
612 goto error;
613 }
614 DBG("Channel fd %d added to kernel set", channel->fd);
615 }
616 session_unlock(session);
617 }
618 session_unlock_list();
619
620 return 0;
621
622 error:
623 session_unlock_list();
624 return -1;
625 }
626
627 /*
628 * Find the channel fd from 'fd' over all tracing session. When found, check
629 * for new channel stream and send those stream fds to the kernel consumer.
630 *
631 * Useful for CPU hotplug feature.
632 */
633 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
634 {
635 int ret = 0;
636 struct ltt_session *session;
637 struct ltt_kernel_channel *channel;
638
639 DBG("Updating kernel streams for channel fd %d", fd);
640
641 session_lock_list();
642 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
643 session_lock(session);
644 if (session->kernel_session == NULL) {
645 session_unlock(session);
646 continue;
647 }
648
649 /* This is not suppose to be -1 but this is an extra security check */
650 if (session->kernel_session->consumer_fd < 0) {
651 session->kernel_session->consumer_fd = consumer_data->cmd_sock;
652 }
653
654 cds_list_for_each_entry(channel,
655 &session->kernel_session->channel_list.head, list) {
656 if (channel->fd == fd) {
657 DBG("Channel found, updating kernel streams");
658 ret = kernel_open_channel_stream(channel);
659 if (ret < 0) {
660 goto error;
661 }
662
663 /*
664 * Have we already sent fds to the consumer? If yes, it means
665 * that tracing is started so it is safe to send our updated
666 * stream fds.
667 */
668 if (session->kernel_session->consumer_fds_sent == 1 &&
669 session->kernel_session->consumer != NULL) {
670 ret = kernel_consumer_send_channel_stream(
671 session->kernel_session->consumer_fd, channel,
672 session->kernel_session);
673 if (ret < 0) {
674 goto error;
675 }
676 }
677 goto error;
678 }
679 }
680 session_unlock(session);
681 }
682 session_unlock_list();
683 return ret;
684
685 error:
686 session_unlock(session);
687 session_unlock_list();
688 return ret;
689 }
690
691 /*
692 * For each tracing session, update newly registered apps.
693 */
694 static void update_ust_app(int app_sock)
695 {
696 struct ltt_session *sess, *stmp;
697
698 session_lock_list();
699
700 /* For all tracing session(s) */
701 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
702 session_lock(sess);
703 if (sess->ust_session) {
704 ust_app_global_update(sess->ust_session, app_sock);
705 }
706 session_unlock(sess);
707 }
708
709 session_unlock_list();
710 }
711
712 /*
713 * This thread manage event coming from the kernel.
714 *
715 * Features supported in this thread:
716 * -) CPU Hotplug
717 */
718 static void *thread_manage_kernel(void *data)
719 {
720 int ret, i, pollfd, update_poll_flag = 1, err = -1;
721 uint32_t revents, nb_fd;
722 char tmp;
723 struct lttng_poll_event events;
724
725 DBG("Thread manage kernel started");
726
727 health_code_update(&health_thread_kernel);
728
729 ret = create_thread_poll_set(&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 while (1) {
740 health_code_update(&health_thread_kernel);
741
742 if (update_poll_flag == 1) {
743 /*
744 * Reset number of fd in the poll set. Always 2 since there is the thread
745 * quit pipe and the kernel pipe.
746 */
747 events.nb_fd = 2;
748
749 ret = update_kernel_poll(&events);
750 if (ret < 0) {
751 goto error;
752 }
753 update_poll_flag = 0;
754 }
755
756 nb_fd = LTTNG_POLL_GETNB(&events);
757
758 DBG("Thread kernel polling on %d fds", nb_fd);
759
760 /* Zeroed the poll events */
761 lttng_poll_reset(&events);
762
763 /* Poll infinite value of time */
764 restart:
765 health_poll_update(&health_thread_kernel);
766 ret = lttng_poll_wait(&events, -1);
767 health_poll_update(&health_thread_kernel);
768 if (ret < 0) {
769 /*
770 * Restart interrupted system call.
771 */
772 if (errno == EINTR) {
773 goto restart;
774 }
775 goto error;
776 } else if (ret == 0) {
777 /* Should not happen since timeout is infinite */
778 ERR("Return value of poll is 0 with an infinite timeout.\n"
779 "This should not have happened! Continuing...");
780 continue;
781 }
782
783 for (i = 0; i < nb_fd; i++) {
784 /* Fetch once the poll data */
785 revents = LTTNG_POLL_GETEV(&events, i);
786 pollfd = LTTNG_POLL_GETFD(&events, i);
787
788 health_code_update(&health_thread_kernel);
789
790 /* Thread quit pipe has been closed. Killing thread. */
791 ret = check_thread_quit_pipe(pollfd, revents);
792 if (ret) {
793 err = 0;
794 goto exit;
795 }
796
797 /* Check for data on kernel pipe */
798 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
799 ret = read(kernel_poll_pipe[0], &tmp, 1);
800 update_poll_flag = 1;
801 continue;
802 } else {
803 /*
804 * New CPU detected by the kernel. Adding kernel stream to
805 * kernel session and updating the kernel consumer
806 */
807 if (revents & LPOLLIN) {
808 ret = update_kernel_stream(&kconsumer_data, pollfd);
809 if (ret < 0) {
810 continue;
811 }
812 break;
813 /*
814 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
815 * and unregister kernel stream at this point.
816 */
817 }
818 }
819 }
820 }
821
822 exit:
823 error:
824 lttng_poll_clean(&events);
825 error_poll_create:
826 if (err) {
827 health_error(&health_thread_kernel);
828 ERR("Health error occurred in %s", __func__);
829 }
830 health_exit(&health_thread_kernel);
831 DBG("Kernel thread dying");
832 return NULL;
833 }
834
835 /*
836 * This thread manage the consumer error sent back to the session daemon.
837 */
838 static void *thread_manage_consumer(void *data)
839 {
840 int sock = -1, i, ret, pollfd, err = -1;
841 uint32_t revents, nb_fd;
842 enum lttcomm_return_code code;
843 struct lttng_poll_event events;
844 struct consumer_data *consumer_data = data;
845
846 DBG("[thread] Manage consumer started");
847
848 health_code_update(&consumer_data->health);
849
850 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
851 if (ret < 0) {
852 goto error_listen;
853 }
854
855 /*
856 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
857 * Nothing more will be added to this poll set.
858 */
859 ret = create_thread_poll_set(&events, 2);
860 if (ret < 0) {
861 goto error_poll;
862 }
863
864 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
865 if (ret < 0) {
866 goto error;
867 }
868
869 nb_fd = LTTNG_POLL_GETNB(&events);
870
871 health_code_update(&consumer_data->health);
872
873 /* Inifinite blocking call, waiting for transmission */
874 restart:
875 health_poll_update(&consumer_data->health);
876 ret = lttng_poll_wait(&events, -1);
877 health_poll_update(&consumer_data->health);
878 if (ret < 0) {
879 /*
880 * Restart interrupted system call.
881 */
882 if (errno == EINTR) {
883 goto restart;
884 }
885 goto error;
886 }
887
888 for (i = 0; i < nb_fd; i++) {
889 /* Fetch once the poll data */
890 revents = LTTNG_POLL_GETEV(&events, i);
891 pollfd = LTTNG_POLL_GETFD(&events, i);
892
893 health_code_update(&consumer_data->health);
894
895 /* Thread quit pipe has been closed. Killing thread. */
896 ret = check_thread_quit_pipe(pollfd, revents);
897 if (ret) {
898 err = 0;
899 goto exit;
900 }
901
902 /* Event on the registration socket */
903 if (pollfd == consumer_data->err_sock) {
904 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
905 ERR("consumer err socket poll error");
906 goto error;
907 }
908 }
909 }
910
911 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
912 if (sock < 0) {
913 goto error;
914 }
915
916 health_code_update(&consumer_data->health);
917
918 DBG2("Receiving code from consumer err_sock");
919
920 /* Getting status code from kconsumerd */
921 ret = lttcomm_recv_unix_sock(sock, &code,
922 sizeof(enum lttcomm_return_code));
923 if (ret <= 0) {
924 goto error;
925 }
926
927 health_code_update(&consumer_data->health);
928
929 if (code == CONSUMERD_COMMAND_SOCK_READY) {
930 consumer_data->cmd_sock =
931 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
932 if (consumer_data->cmd_sock < 0) {
933 sem_post(&consumer_data->sem);
934 PERROR("consumer connect");
935 goto error;
936 }
937 /* Signal condition to tell that the kconsumerd is ready */
938 sem_post(&consumer_data->sem);
939 DBG("consumer command socket ready");
940 } else {
941 ERR("consumer error when waiting for SOCK_READY : %s",
942 lttcomm_get_readable_code(-code));
943 goto error;
944 }
945
946 /* Remove the kconsumerd error sock since we've established a connexion */
947 ret = lttng_poll_del(&events, consumer_data->err_sock);
948 if (ret < 0) {
949 goto error;
950 }
951
952 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
953 if (ret < 0) {
954 goto error;
955 }
956
957 health_code_update(&consumer_data->health);
958
959 /* Update number of fd */
960 nb_fd = LTTNG_POLL_GETNB(&events);
961
962 /* Inifinite blocking call, waiting for transmission */
963 restart_poll:
964 health_poll_update(&consumer_data->health);
965 ret = lttng_poll_wait(&events, -1);
966 health_poll_update(&consumer_data->health);
967 if (ret < 0) {
968 /*
969 * Restart interrupted system call.
970 */
971 if (errno == EINTR) {
972 goto restart_poll;
973 }
974 goto error;
975 }
976
977 for (i = 0; i < nb_fd; i++) {
978 /* Fetch once the poll data */
979 revents = LTTNG_POLL_GETEV(&events, i);
980 pollfd = LTTNG_POLL_GETFD(&events, i);
981
982 health_code_update(&consumer_data->health);
983
984 /* Thread quit pipe has been closed. Killing thread. */
985 ret = check_thread_quit_pipe(pollfd, revents);
986 if (ret) {
987 err = 0;
988 goto exit;
989 }
990
991 /* Event on the kconsumerd socket */
992 if (pollfd == sock) {
993 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
994 ERR("consumer err socket second poll error");
995 goto error;
996 }
997 }
998 }
999
1000 health_code_update(&consumer_data->health);
1001
1002 /* Wait for any kconsumerd error */
1003 ret = lttcomm_recv_unix_sock(sock, &code,
1004 sizeof(enum lttcomm_return_code));
1005 if (ret <= 0) {
1006 ERR("consumer closed the command socket");
1007 goto error;
1008 }
1009
1010 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
1011
1012 exit:
1013 error:
1014 /* Immediately set the consumerd state to stopped */
1015 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1016 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1017 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1018 consumer_data->type == LTTNG_CONSUMER32_UST) {
1019 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1020 } else {
1021 /* Code flow error... */
1022 assert(0);
1023 }
1024
1025 if (consumer_data->err_sock >= 0) {
1026 ret = close(consumer_data->err_sock);
1027 if (ret) {
1028 PERROR("close");
1029 }
1030 }
1031 if (consumer_data->cmd_sock >= 0) {
1032 ret = close(consumer_data->cmd_sock);
1033 if (ret) {
1034 PERROR("close");
1035 }
1036 }
1037 if (sock >= 0) {
1038 ret = close(sock);
1039 if (ret) {
1040 PERROR("close");
1041 }
1042 }
1043
1044 unlink(consumer_data->err_unix_sock_path);
1045 unlink(consumer_data->cmd_unix_sock_path);
1046 consumer_data->pid = 0;
1047
1048 lttng_poll_clean(&events);
1049 error_poll:
1050 error_listen:
1051 if (err) {
1052 health_error(&consumer_data->health);
1053 ERR("Health error occurred in %s", __func__);
1054 }
1055 health_exit(&consumer_data->health);
1056 DBG("consumer thread cleanup completed");
1057
1058 return NULL;
1059 }
1060
1061 /*
1062 * This thread manage application communication.
1063 */
1064 static void *thread_manage_apps(void *data)
1065 {
1066 int i, ret, pollfd, err = -1;
1067 uint32_t revents, nb_fd;
1068 struct ust_command ust_cmd;
1069 struct lttng_poll_event events;
1070
1071 DBG("[thread] Manage application started");
1072
1073 rcu_register_thread();
1074 rcu_thread_online();
1075
1076 health_code_update(&health_thread_app_manage);
1077
1078 ret = create_thread_poll_set(&events, 2);
1079 if (ret < 0) {
1080 goto error_poll_create;
1081 }
1082
1083 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1084 if (ret < 0) {
1085 goto error;
1086 }
1087
1088 health_code_update(&health_thread_app_manage);
1089
1090 while (1) {
1091 /* Zeroed the events structure */
1092 lttng_poll_reset(&events);
1093
1094 nb_fd = LTTNG_POLL_GETNB(&events);
1095
1096 DBG("Apps thread polling on %d fds", nb_fd);
1097
1098 /* Inifinite blocking call, waiting for transmission */
1099 restart:
1100 health_poll_update(&health_thread_app_manage);
1101 ret = lttng_poll_wait(&events, -1);
1102 health_poll_update(&health_thread_app_manage);
1103 if (ret < 0) {
1104 /*
1105 * Restart interrupted system call.
1106 */
1107 if (errno == EINTR) {
1108 goto restart;
1109 }
1110 goto error;
1111 }
1112
1113 for (i = 0; i < nb_fd; i++) {
1114 /* Fetch once the poll data */
1115 revents = LTTNG_POLL_GETEV(&events, i);
1116 pollfd = LTTNG_POLL_GETFD(&events, i);
1117
1118 health_code_update(&health_thread_app_manage);
1119
1120 /* Thread quit pipe has been closed. Killing thread. */
1121 ret = check_thread_quit_pipe(pollfd, revents);
1122 if (ret) {
1123 err = 0;
1124 goto exit;
1125 }
1126
1127 /* Inspect the apps cmd pipe */
1128 if (pollfd == apps_cmd_pipe[0]) {
1129 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1130 ERR("Apps command pipe error");
1131 goto error;
1132 } else if (revents & LPOLLIN) {
1133 /* Empty pipe */
1134 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1135 if (ret < 0 || ret < sizeof(ust_cmd)) {
1136 PERROR("read apps cmd pipe");
1137 goto error;
1138 }
1139
1140 health_code_update(&health_thread_app_manage);
1141
1142 /* Register applicaton to the session daemon */
1143 ret = ust_app_register(&ust_cmd.reg_msg,
1144 ust_cmd.sock);
1145 if (ret == -ENOMEM) {
1146 goto error;
1147 } else if (ret < 0) {
1148 break;
1149 }
1150
1151 health_code_update(&health_thread_app_manage);
1152
1153 /*
1154 * Validate UST version compatibility.
1155 */
1156 ret = ust_app_validate_version(ust_cmd.sock);
1157 if (ret >= 0) {
1158 /*
1159 * Add channel(s) and event(s) to newly registered apps
1160 * from lttng global UST domain.
1161 */
1162 update_ust_app(ust_cmd.sock);
1163 }
1164
1165 health_code_update(&health_thread_app_manage);
1166
1167 ret = ust_app_register_done(ust_cmd.sock);
1168 if (ret < 0) {
1169 /*
1170 * If the registration is not possible, we simply
1171 * unregister the apps and continue
1172 */
1173 ust_app_unregister(ust_cmd.sock);
1174 } else {
1175 /*
1176 * We just need here to monitor the close of the UST
1177 * socket and poll set monitor those by default.
1178 * Listen on POLLIN (even if we never expect any
1179 * data) to ensure that hangup wakes us.
1180 */
1181 ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN);
1182 if (ret < 0) {
1183 goto error;
1184 }
1185
1186 DBG("Apps with sock %d added to poll set",
1187 ust_cmd.sock);
1188 }
1189
1190 health_code_update(&health_thread_app_manage);
1191
1192 break;
1193 }
1194 } else {
1195 /*
1196 * At this point, we know that a registered application made
1197 * the event at poll_wait.
1198 */
1199 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1200 /* Removing from the poll set */
1201 ret = lttng_poll_del(&events, pollfd);
1202 if (ret < 0) {
1203 goto error;
1204 }
1205
1206 /* Socket closed on remote end. */
1207 ust_app_unregister(pollfd);
1208 break;
1209 }
1210 }
1211
1212 health_code_update(&health_thread_app_manage);
1213 }
1214 }
1215
1216 exit:
1217 error:
1218 lttng_poll_clean(&events);
1219 error_poll_create:
1220 if (err) {
1221 health_error(&health_thread_app_manage);
1222 ERR("Health error occurred in %s", __func__);
1223 }
1224 health_exit(&health_thread_app_manage);
1225 DBG("Application communication apps thread cleanup complete");
1226 rcu_thread_offline();
1227 rcu_unregister_thread();
1228 return NULL;
1229 }
1230
1231 /*
1232 * Dispatch request from the registration threads to the application
1233 * communication thread.
1234 */
1235 static void *thread_dispatch_ust_registration(void *data)
1236 {
1237 int ret;
1238 struct cds_wfq_node *node;
1239 struct ust_command *ust_cmd = NULL;
1240
1241 DBG("[thread] Dispatch UST command started");
1242
1243 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1244 /* Atomically prepare the queue futex */
1245 futex_nto1_prepare(&ust_cmd_queue.futex);
1246
1247 do {
1248 /* Dequeue command for registration */
1249 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1250 if (node == NULL) {
1251 DBG("Woken up but nothing in the UST command queue");
1252 /* Continue thread execution */
1253 break;
1254 }
1255
1256 ust_cmd = caa_container_of(node, struct ust_command, node);
1257
1258 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1259 " gid:%d sock:%d name:%s (version %d.%d)",
1260 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1261 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1262 ust_cmd->sock, ust_cmd->reg_msg.name,
1263 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1264 /*
1265 * Inform apps thread of the new application registration. This
1266 * call is blocking so we can be assured that the data will be read
1267 * at some point in time or wait to the end of the world :)
1268 */
1269 ret = write(apps_cmd_pipe[1], ust_cmd,
1270 sizeof(struct ust_command));
1271 if (ret < 0) {
1272 PERROR("write apps cmd pipe");
1273 if (errno == EBADF) {
1274 /*
1275 * We can't inform the application thread to process
1276 * registration. We will exit or else application
1277 * registration will not occur and tracing will never
1278 * start.
1279 */
1280 goto error;
1281 }
1282 }
1283 free(ust_cmd);
1284 } while (node != NULL);
1285
1286 /* Futex wait on queue. Blocking call on futex() */
1287 futex_nto1_wait(&ust_cmd_queue.futex);
1288 }
1289
1290 error:
1291 DBG("Dispatch thread dying");
1292 return NULL;
1293 }
1294
1295 /*
1296 * This thread manage application registration.
1297 */
1298 static void *thread_registration_apps(void *data)
1299 {
1300 int sock = -1, i, ret, pollfd, err = -1;
1301 uint32_t revents, nb_fd;
1302 struct lttng_poll_event events;
1303 /*
1304 * Get allocated in this thread, enqueued to a global queue, dequeued and
1305 * freed in the manage apps thread.
1306 */
1307 struct ust_command *ust_cmd = NULL;
1308
1309 DBG("[thread] Manage application registration started");
1310
1311 ret = lttcomm_listen_unix_sock(apps_sock);
1312 if (ret < 0) {
1313 goto error_listen;
1314 }
1315
1316 /*
1317 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1318 * more will be added to this poll set.
1319 */
1320 ret = create_thread_poll_set(&events, 2);
1321 if (ret < 0) {
1322 goto error_create_poll;
1323 }
1324
1325 /* Add the application registration socket */
1326 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1327 if (ret < 0) {
1328 goto error_poll_add;
1329 }
1330
1331 /* Notify all applications to register */
1332 ret = notify_ust_apps(1);
1333 if (ret < 0) {
1334 ERR("Failed to notify applications or create the wait shared memory.\n"
1335 "Execution continues but there might be problem for already\n"
1336 "running applications that wishes to register.");
1337 }
1338
1339 while (1) {
1340 DBG("Accepting application registration");
1341
1342 nb_fd = LTTNG_POLL_GETNB(&events);
1343
1344 /* Inifinite blocking call, waiting for transmission */
1345 restart:
1346 health_poll_update(&health_thread_app_reg);
1347 ret = lttng_poll_wait(&events, -1);
1348 health_poll_update(&health_thread_app_reg);
1349 if (ret < 0) {
1350 /*
1351 * Restart interrupted system call.
1352 */
1353 if (errno == EINTR) {
1354 goto restart;
1355 }
1356 goto error;
1357 }
1358
1359 for (i = 0; i < nb_fd; i++) {
1360 health_code_update(&health_thread_app_reg);
1361
1362 /* Fetch once the poll data */
1363 revents = LTTNG_POLL_GETEV(&events, i);
1364 pollfd = LTTNG_POLL_GETFD(&events, i);
1365
1366 /* Thread quit pipe has been closed. Killing thread. */
1367 ret = check_thread_quit_pipe(pollfd, revents);
1368 if (ret) {
1369 err = 0;
1370 goto exit;
1371 }
1372
1373 /* Event on the registration socket */
1374 if (pollfd == apps_sock) {
1375 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1376 ERR("Register apps socket poll error");
1377 goto error;
1378 } else if (revents & LPOLLIN) {
1379 sock = lttcomm_accept_unix_sock(apps_sock);
1380 if (sock < 0) {
1381 goto error;
1382 }
1383
1384 /* Create UST registration command for enqueuing */
1385 ust_cmd = zmalloc(sizeof(struct ust_command));
1386 if (ust_cmd == NULL) {
1387 PERROR("ust command zmalloc");
1388 goto error;
1389 }
1390
1391 /*
1392 * Using message-based transmissions to ensure we don't
1393 * have to deal with partially received messages.
1394 */
1395 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1396 if (ret < 0) {
1397 ERR("Exhausted file descriptors allowed for applications.");
1398 free(ust_cmd);
1399 ret = close(sock);
1400 if (ret) {
1401 PERROR("close");
1402 }
1403 sock = -1;
1404 continue;
1405 }
1406 health_code_update(&health_thread_app_reg);
1407 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1408 sizeof(struct ust_register_msg));
1409 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1410 if (ret < 0) {
1411 PERROR("lttcomm_recv_unix_sock register apps");
1412 } else {
1413 ERR("Wrong size received on apps register");
1414 }
1415 free(ust_cmd);
1416 ret = close(sock);
1417 if (ret) {
1418 PERROR("close");
1419 }
1420 lttng_fd_put(LTTNG_FD_APPS, 1);
1421 sock = -1;
1422 continue;
1423 }
1424 health_code_update(&health_thread_app_reg);
1425
1426 ust_cmd->sock = sock;
1427 sock = -1;
1428
1429 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1430 " gid:%d sock:%d name:%s (version %d.%d)",
1431 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1432 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1433 ust_cmd->sock, ust_cmd->reg_msg.name,
1434 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1435
1436 /*
1437 * Lock free enqueue the registration request. The red pill
1438 * has been taken! This apps will be part of the *system*.
1439 */
1440 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1441
1442 /*
1443 * Wake the registration queue futex. Implicit memory
1444 * barrier with the exchange in cds_wfq_enqueue.
1445 */
1446 futex_nto1_wake(&ust_cmd_queue.futex);
1447 }
1448 }
1449 }
1450 }
1451
1452 exit:
1453 error:
1454 if (err) {
1455 health_error(&health_thread_app_reg);
1456 ERR("Health error occurred in %s", __func__);
1457 }
1458 health_exit(&health_thread_app_reg);
1459
1460 /* Notify that the registration thread is gone */
1461 notify_ust_apps(0);
1462
1463 if (apps_sock >= 0) {
1464 ret = close(apps_sock);
1465 if (ret) {
1466 PERROR("close");
1467 }
1468 }
1469 if (sock >= 0) {
1470 ret = close(sock);
1471 if (ret) {
1472 PERROR("close");
1473 }
1474 lttng_fd_put(LTTNG_FD_APPS, 1);
1475 }
1476 unlink(apps_unix_sock_path);
1477
1478 error_poll_add:
1479 lttng_poll_clean(&events);
1480 error_listen:
1481 error_create_poll:
1482 DBG("UST Registration thread cleanup complete");
1483
1484 return NULL;
1485 }
1486
1487 /*
1488 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1489 * exec or it will fails.
1490 */
1491 static int spawn_consumer_thread(struct consumer_data *consumer_data)
1492 {
1493 int ret;
1494 struct timespec timeout;
1495
1496 timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT;
1497 timeout.tv_nsec = 0;
1498
1499 /* Setup semaphore */
1500 ret = sem_init(&consumer_data->sem, 0, 0);
1501 if (ret < 0) {
1502 PERROR("sem_init consumer semaphore");
1503 goto error;
1504 }
1505
1506 ret = pthread_create(&consumer_data->thread, NULL,
1507 thread_manage_consumer, consumer_data);
1508 if (ret != 0) {
1509 PERROR("pthread_create consumer");
1510 ret = -1;
1511 goto error;
1512 }
1513
1514 /* Get time for sem_timedwait absolute timeout */
1515 ret = clock_gettime(CLOCK_REALTIME, &timeout);
1516 if (ret < 0) {
1517 PERROR("clock_gettime spawn consumer");
1518 /* Infinite wait for the kconsumerd thread to be ready */
1519 ret = sem_wait(&consumer_data->sem);
1520 } else {
1521 /* Normal timeout if the gettime was successful */
1522 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
1523 ret = sem_timedwait(&consumer_data->sem, &timeout);
1524 }
1525
1526 if (ret < 0) {
1527 if (errno == ETIMEDOUT) {
1528 /*
1529 * Call has timed out so we kill the kconsumerd_thread and return
1530 * an error.
1531 */
1532 ERR("The consumer thread was never ready. Killing it");
1533 ret = pthread_cancel(consumer_data->thread);
1534 if (ret < 0) {
1535 PERROR("pthread_cancel consumer thread");
1536 }
1537 } else {
1538 PERROR("semaphore wait failed consumer thread");
1539 }
1540 goto error;
1541 }
1542
1543 pthread_mutex_lock(&consumer_data->pid_mutex);
1544 if (consumer_data->pid == 0) {
1545 ERR("Kconsumerd did not start");
1546 pthread_mutex_unlock(&consumer_data->pid_mutex);
1547 goto error;
1548 }
1549 pthread_mutex_unlock(&consumer_data->pid_mutex);
1550
1551 return 0;
1552
1553 error:
1554 return ret;
1555 }
1556
1557 /*
1558 * Join consumer thread
1559 */
1560 static int join_consumer_thread(struct consumer_data *consumer_data)
1561 {
1562 void *status;
1563 int ret;
1564
1565 if (consumer_data->pid != 0) {
1566 ret = kill(consumer_data->pid, SIGTERM);
1567 if (ret) {
1568 ERR("Error killing consumer daemon");
1569 return ret;
1570 }
1571 return pthread_join(consumer_data->thread, &status);
1572 } else {
1573 return 0;
1574 }
1575 }
1576
1577 /*
1578 * Fork and exec a consumer daemon (consumerd).
1579 *
1580 * Return pid if successful else -1.
1581 */
1582 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
1583 {
1584 int ret;
1585 pid_t pid;
1586 const char *consumer_to_use;
1587 const char *verbosity;
1588 struct stat st;
1589
1590 DBG("Spawning consumerd");
1591
1592 pid = fork();
1593 if (pid == 0) {
1594 /*
1595 * Exec consumerd.
1596 */
1597 if (opt_verbose_consumer) {
1598 verbosity = "--verbose";
1599 } else {
1600 verbosity = "--quiet";
1601 }
1602 switch (consumer_data->type) {
1603 case LTTNG_CONSUMER_KERNEL:
1604 /*
1605 * Find out which consumerd to execute. We will first try the
1606 * 64-bit path, then the sessiond's installation directory, and
1607 * fallback on the 32-bit one,
1608 */
1609 DBG3("Looking for a kernel consumer at these locations:");
1610 DBG3(" 1) %s", consumerd64_bin);
1611 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1612 DBG3(" 3) %s", consumerd32_bin);
1613 if (stat(consumerd64_bin, &st) == 0) {
1614 DBG3("Found location #1");
1615 consumer_to_use = consumerd64_bin;
1616 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
1617 DBG3("Found location #2");
1618 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
1619 } else if (stat(consumerd32_bin, &st) == 0) {
1620 DBG3("Found location #3");
1621 consumer_to_use = consumerd32_bin;
1622 } else {
1623 DBG("Could not find any valid consumerd executable");
1624 break;
1625 }
1626 DBG("Using kernel consumer at: %s", consumer_to_use);
1627 execl(consumer_to_use,
1628 "lttng-consumerd", verbosity, "-k",
1629 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1630 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1631 NULL);
1632 break;
1633 case LTTNG_CONSUMER64_UST:
1634 {
1635 char *tmpnew = NULL;
1636
1637 if (consumerd64_libdir[0] != '\0') {
1638 char *tmp;
1639 size_t tmplen;
1640
1641 tmp = getenv("LD_LIBRARY_PATH");
1642 if (!tmp) {
1643 tmp = "";
1644 }
1645 tmplen = strlen("LD_LIBRARY_PATH=")
1646 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1647 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1648 if (!tmpnew) {
1649 ret = -ENOMEM;
1650 goto error;
1651 }
1652 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1653 strcat(tmpnew, consumerd64_libdir);
1654 if (tmp[0] != '\0') {
1655 strcat(tmpnew, ":");
1656 strcat(tmpnew, tmp);
1657 }
1658 ret = putenv(tmpnew);
1659 if (ret) {
1660 ret = -errno;
1661 goto error;
1662 }
1663 }
1664 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
1665 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
1666 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1667 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1668 NULL);
1669 if (consumerd64_libdir[0] != '\0') {
1670 free(tmpnew);
1671 }
1672 if (ret) {
1673 goto error;
1674 }
1675 break;
1676 }
1677 case LTTNG_CONSUMER32_UST:
1678 {
1679 char *tmpnew = NULL;
1680
1681 if (consumerd32_libdir[0] != '\0') {
1682 char *tmp;
1683 size_t tmplen;
1684
1685 tmp = getenv("LD_LIBRARY_PATH");
1686 if (!tmp) {
1687 tmp = "";
1688 }
1689 tmplen = strlen("LD_LIBRARY_PATH=")
1690 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1691 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1692 if (!tmpnew) {
1693 ret = -ENOMEM;
1694 goto error;
1695 }
1696 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1697 strcat(tmpnew, consumerd32_libdir);
1698 if (tmp[0] != '\0') {
1699 strcat(tmpnew, ":");
1700 strcat(tmpnew, tmp);
1701 }
1702 ret = putenv(tmpnew);
1703 if (ret) {
1704 ret = -errno;
1705 goto error;
1706 }
1707 }
1708 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
1709 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
1710 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1711 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1712 NULL);
1713 if (consumerd32_libdir[0] != '\0') {
1714 free(tmpnew);
1715 }
1716 if (ret) {
1717 goto error;
1718 }
1719 break;
1720 }
1721 default:
1722 PERROR("unknown consumer type");
1723 exit(EXIT_FAILURE);
1724 }
1725 if (errno != 0) {
1726 PERROR("kernel start consumer exec");
1727 }
1728 exit(EXIT_FAILURE);
1729 } else if (pid > 0) {
1730 ret = pid;
1731 } else {
1732 PERROR("start consumer fork");
1733 ret = -errno;
1734 }
1735 error:
1736 return ret;
1737 }
1738
1739 /*
1740 * Spawn the consumerd daemon and session daemon thread.
1741 */
1742 static int start_consumerd(struct consumer_data *consumer_data)
1743 {
1744 int ret;
1745
1746 pthread_mutex_lock(&consumer_data->pid_mutex);
1747 if (consumer_data->pid != 0) {
1748 pthread_mutex_unlock(&consumer_data->pid_mutex);
1749 goto end;
1750 }
1751
1752 ret = spawn_consumerd(consumer_data);
1753 if (ret < 0) {
1754 ERR("Spawning consumerd failed");
1755 pthread_mutex_unlock(&consumer_data->pid_mutex);
1756 goto error;
1757 }
1758
1759 /* Setting up the consumer_data pid */
1760 consumer_data->pid = ret;
1761 DBG2("Consumer pid %d", consumer_data->pid);
1762 pthread_mutex_unlock(&consumer_data->pid_mutex);
1763
1764 DBG2("Spawning consumer control thread");
1765 ret = spawn_consumer_thread(consumer_data);
1766 if (ret < 0) {
1767 ERR("Fatal error spawning consumer control thread");
1768 goto error;
1769 }
1770
1771 end:
1772 return 0;
1773
1774 error:
1775 return ret;
1776 }
1777
1778 /*
1779 * Compute health status of each consumer. If one of them is zero (bad
1780 * state), we return 0.
1781 */
1782 static int check_consumer_health(void)
1783 {
1784 int ret;
1785
1786 ret = health_check_state(&kconsumer_data.health) &&
1787 health_check_state(&ustconsumer32_data.health) &&
1788 health_check_state(&ustconsumer64_data.health);
1789
1790 DBG3("Health consumer check %d", ret);
1791
1792 return ret;
1793 }
1794
1795 /*
1796 * Check version of the lttng-modules.
1797 */
1798 static int validate_lttng_modules_version(void)
1799 {
1800 return kernel_validate_version(kernel_tracer_fd);
1801 }
1802
1803 /*
1804 * Setup necessary data for kernel tracer action.
1805 */
1806 static int init_kernel_tracer(void)
1807 {
1808 int ret;
1809
1810 /* Modprobe lttng kernel modules */
1811 ret = modprobe_lttng_control();
1812 if (ret < 0) {
1813 goto error;
1814 }
1815
1816 /* Open debugfs lttng */
1817 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1818 if (kernel_tracer_fd < 0) {
1819 DBG("Failed to open %s", module_proc_lttng);
1820 ret = -1;
1821 goto error_open;
1822 }
1823
1824 /* Validate kernel version */
1825 ret = validate_lttng_modules_version();
1826 if (ret < 0) {
1827 goto error_version;
1828 }
1829
1830 ret = modprobe_lttng_data();
1831 if (ret < 0) {
1832 goto error_modules;
1833 }
1834
1835 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1836 return 0;
1837
1838 error_version:
1839 modprobe_remove_lttng_control();
1840 ret = close(kernel_tracer_fd);
1841 if (ret) {
1842 PERROR("close");
1843 }
1844 kernel_tracer_fd = -1;
1845 return LTTCOMM_KERN_VERSION;
1846
1847 error_modules:
1848 ret = close(kernel_tracer_fd);
1849 if (ret) {
1850 PERROR("close");
1851 }
1852
1853 error_open:
1854 modprobe_remove_lttng_control();
1855
1856 error:
1857 WARN("No kernel tracer available");
1858 kernel_tracer_fd = -1;
1859 if (!is_root) {
1860 return LTTCOMM_NEED_ROOT_SESSIOND;
1861 } else {
1862 return LTTCOMM_KERN_NA;
1863 }
1864 }
1865
1866 /*
1867 * Init tracing by creating trace directory and sending fds kernel consumer.
1868 */
1869 static int init_kernel_tracing(struct ltt_kernel_session *session)
1870 {
1871 int ret = 0;
1872
1873 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
1874 /*
1875 * Assign default kernel consumer socket if no consumer assigned to the
1876 * kernel session. At this point, it's NOT supposed to be -1 but this is
1877 * an extra security check.
1878 */
1879 if (session->consumer_fd < 0) {
1880 session->consumer_fd = kconsumer_data.cmd_sock;
1881 }
1882
1883 ret = kernel_consumer_send_session(session->consumer_fd, session);
1884 if (ret < 0) {
1885 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1886 goto error;
1887 }
1888 }
1889
1890 error:
1891 return ret;
1892 }
1893
1894 /*
1895 * Create a socket to the relayd using the URI.
1896 *
1897 * On success, the relayd_sock pointer is set to the created socket.
1898 * Else, it is untouched and an lttcomm error code is returned.
1899 */
1900 static int create_connect_relayd(struct consumer_output *output,
1901 const char *session_name, struct lttng_uri *uri,
1902 struct lttcomm_sock **relayd_sock)
1903 {
1904 int ret;
1905 struct lttcomm_sock *sock;
1906
1907 /* Create socket object from URI */
1908 sock = lttcomm_alloc_sock_from_uri(uri);
1909 if (sock == NULL) {
1910 ret = LTTCOMM_FATAL;
1911 goto error;
1912 }
1913
1914 ret = lttcomm_create_sock(sock);
1915 if (ret < 0) {
1916 ret = LTTCOMM_FATAL;
1917 goto error;
1918 }
1919
1920 /* Connect to relayd so we can proceed with a session creation. */
1921 ret = relayd_connect(sock);
1922 if (ret < 0) {
1923 ERR("Unable to reach lttng-relayd");
1924 ret = LTTCOMM_RELAYD_SESSION_FAIL;
1925 goto free_sock;
1926 }
1927
1928 /* Create socket for control stream. */
1929 if (uri->stype == LTTNG_STREAM_CONTROL) {
1930 DBG3("Creating relayd stream socket from URI");
1931
1932 /* Check relayd version */
1933 ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0);
1934 if (ret < 0) {
1935 ret = LTTCOMM_RELAYD_VERSION_FAIL;
1936 goto close_sock;
1937 }
1938 } else if (uri->stype == LTTNG_STREAM_DATA) {
1939 DBG3("Creating relayd data socket from URI");
1940 } else {
1941 /* Command is not valid */
1942 ERR("Relayd invalid stream type: %d", uri->stype);
1943 ret = LTTCOMM_INVALID;
1944 goto close_sock;
1945 }
1946
1947 *relayd_sock = sock;
1948
1949 return LTTCOMM_OK;
1950
1951 close_sock:
1952 if (sock) {
1953 (void) relayd_close(sock);
1954 }
1955 free_sock:
1956 if (sock) {
1957 lttcomm_destroy_sock(sock);
1958 }
1959 error:
1960 return ret;
1961 }
1962
1963 /*
1964 * Connect to the relayd using URI and send the socket to the right consumer.
1965 */
1966 static int send_socket_relayd_consumer(int domain, struct ltt_session *session,
1967 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
1968 int consumer_fd)
1969 {
1970 int ret;
1971 struct lttcomm_sock *sock = NULL;
1972
1973 /* Set the network sequence index if not set. */
1974 if (consumer->net_seq_index == -1) {
1975 /*
1976 * Increment net_seq_idx because we are about to transfer the
1977 * new relayd socket to the consumer.
1978 */
1979 uatomic_inc(&relayd_net_seq_idx);
1980 /* Assign unique key so the consumer can match streams */
1981 consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx);
1982 }
1983
1984 /* Connect to relayd and make version check if uri is the control. */
1985 ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
1986 if (ret != LTTCOMM_OK) {
1987 goto close_sock;
1988 }
1989
1990 /* If the control socket is connected, network session is ready */
1991 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
1992 session->net_handle = 1;
1993 }
1994
1995 /* Send relayd socket to consumer. */
1996 ret = consumer_send_relayd_socket(consumer_fd, sock,
1997 consumer, relayd_uri->stype);
1998 if (ret < 0) {
1999 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2000 goto close_sock;
2001 }
2002
2003 ret = LTTCOMM_OK;
2004
2005 /*
2006 * Close socket which was dup on the consumer side. The session daemon does
2007 * NOT keep track of the relayd socket(s) once transfer to the consumer.
2008 */
2009
2010 close_sock:
2011 if (sock) {
2012 (void) relayd_close(sock);
2013 lttcomm_destroy_sock(sock);
2014 }
2015
2016 return ret;
2017 }
2018
2019 /*
2020 * Send both relayd sockets to a specific consumer and domain. This is a
2021 * helper function to facilitate sending the information to the consumer for a
2022 * session.
2023 */
2024 static int send_sockets_relayd_consumer(int domain,
2025 struct ltt_session *session, struct consumer_output *consumer, int fd)
2026 {
2027 int ret;
2028
2029 /* Sending control relayd socket. */
2030 ret = send_socket_relayd_consumer(domain, session,
2031 &consumer->dst.net.control, consumer, fd);
2032 if (ret != LTTCOMM_OK) {
2033 goto error;
2034 }
2035
2036 /* Sending data relayd socket. */
2037 ret = send_socket_relayd_consumer(domain, session,
2038 &consumer->dst.net.data, consumer, fd);
2039 if (ret != LTTCOMM_OK) {
2040 goto error;
2041 }
2042
2043 error:
2044 return ret;
2045 }
2046
2047 /*
2048 * Setup relayd connections for a tracing session. First creates the socket to
2049 * the relayd and send them to the right domain consumer. Consumer type MUST be
2050 * network.
2051 */
2052 static int setup_relayd(struct ltt_session *session)
2053 {
2054 int ret = LTTCOMM_OK;
2055 struct ltt_ust_session *usess;
2056 struct ltt_kernel_session *ksess;
2057
2058 assert(session);
2059
2060 usess = session->ust_session;
2061 ksess = session->kernel_session;
2062
2063 DBG2("Setting relayd for session %s", session->name);
2064
2065 if (usess && usess->consumer->sock == -1 &&
2066 usess->consumer->type == CONSUMER_DST_NET &&
2067 usess->consumer->enabled) {
2068 /* Setup relayd for 64 bits consumer */
2069 if (ust_consumerd64_fd >= 0) {
2070 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
2071 usess->consumer, ust_consumerd64_fd);
2072 if (ret != LTTCOMM_OK) {
2073 goto error;
2074 }
2075 }
2076
2077 /* Setup relayd for 32 bits consumer */
2078 if (ust_consumerd32_fd >= 0) {
2079 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
2080 usess->consumer, ust_consumerd32_fd);
2081 if (ret != LTTCOMM_OK) {
2082 goto error;
2083 }
2084 }
2085 } else if (ksess && ksess->consumer->sock == -1 &&
2086 ksess->consumer->type == CONSUMER_DST_NET &&
2087 ksess->consumer->enabled) {
2088 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session,
2089 ksess->consumer, ksess->consumer_fd);
2090 if (ret != LTTCOMM_OK) {
2091 goto error;
2092 }
2093 }
2094
2095 error:
2096 return ret;
2097 }
2098
2099 /*
2100 * Copy consumer output from the tracing session to the domain session. The
2101 * function also applies the right modification on a per domain basis for the
2102 * trace files destination directory.
2103 */
2104 static int copy_session_consumer(int domain, struct ltt_session *session)
2105 {
2106 int ret;
2107 const char *dir_name;
2108 struct consumer_output *consumer;
2109
2110 switch (domain) {
2111 case LTTNG_DOMAIN_KERNEL:
2112 DBG3("Copying tracing session consumer output in kernel session");
2113 session->kernel_session->consumer =
2114 consumer_copy_output(session->consumer);
2115 /* Ease our life a bit for the next part */
2116 consumer = session->kernel_session->consumer;
2117 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2118 break;
2119 case LTTNG_DOMAIN_UST:
2120 DBG3("Copying tracing session consumer output in UST session");
2121 session->ust_session->consumer =
2122 consumer_copy_output(session->consumer);
2123 /* Ease our life a bit for the next part */
2124 consumer = session->ust_session->consumer;
2125 dir_name = DEFAULT_UST_TRACE_DIR;
2126 break;
2127 default:
2128 ret = LTTCOMM_UNKNOWN_DOMAIN;
2129 goto error;
2130 }
2131
2132 /* Append correct directory to subdir */
2133 strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
2134 DBG3("Copy session consumer subdir %s", consumer->subdir);
2135
2136 /* Add default trace directory name */
2137 if (consumer->type == CONSUMER_DST_LOCAL) {
2138 strncat(consumer->dst.trace_path, dir_name,
2139 sizeof(consumer->dst.trace_path));
2140 }
2141
2142 ret = LTTCOMM_OK;
2143
2144 error:
2145 return ret;
2146 }
2147
2148 /*
2149 * Create an UST session and add it to the session ust list.
2150 */
2151 static int create_ust_session(struct ltt_session *session,
2152 struct lttng_domain *domain)
2153 {
2154 int ret;
2155 struct ltt_ust_session *lus = NULL;
2156
2157 assert(session);
2158 assert(session->consumer);
2159
2160 switch (domain->type) {
2161 case LTTNG_DOMAIN_UST:
2162 break;
2163 default:
2164 ERR("Unknown UST domain on create session %d", domain->type);
2165 ret = LTTCOMM_UNKNOWN_DOMAIN;
2166 goto error;
2167 }
2168
2169 DBG("Creating UST session");
2170
2171 lus = trace_ust_create_session(session->path, session->id, domain);
2172 if (lus == NULL) {
2173 ret = LTTCOMM_UST_SESS_FAIL;
2174 goto error;
2175 }
2176
2177 if (session->consumer->type == CONSUMER_DST_LOCAL) {
2178 ret = run_as_mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
2179 session->uid, session->gid);
2180 if (ret < 0) {
2181 if (ret != -EEXIST) {
2182 ERR("Trace directory creation error");
2183 ret = LTTCOMM_UST_SESS_FAIL;
2184 goto error;
2185 }
2186 }
2187 }
2188
2189 lus->uid = session->uid;
2190 lus->gid = session->gid;
2191 session->ust_session = lus;
2192
2193 /* Copy session output to the newly created UST session */
2194 ret = copy_session_consumer(domain->type, session);
2195 if (ret != LTTCOMM_OK) {
2196 goto error;
2197 }
2198
2199 return LTTCOMM_OK;
2200
2201 error:
2202 free(lus);
2203 session->ust_session = NULL;
2204 return ret;
2205 }
2206
2207 /*
2208 * Create a kernel tracer session then create the default channel.
2209 */
2210 static int create_kernel_session(struct ltt_session *session)
2211 {
2212 int ret;
2213
2214 DBG("Creating kernel session");
2215
2216 ret = kernel_create_session(session, kernel_tracer_fd);
2217 if (ret < 0) {
2218 ret = LTTCOMM_KERN_SESS_FAIL;
2219 goto error;
2220 }
2221
2222 /* Set kernel consumer socket fd */
2223 if (kconsumer_data.cmd_sock >= 0) {
2224 session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
2225 }
2226
2227 /* Copy session output to the newly created Kernel session */
2228 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2229 if (ret != LTTCOMM_OK) {
2230 goto error;
2231 }
2232
2233 /* Create directory(ies) on local filesystem. */
2234 if (session->consumer->type == CONSUMER_DST_LOCAL) {
2235 ret = run_as_mkdir_recursive(
2236 session->kernel_session->consumer->dst.trace_path,
2237 S_IRWXU | S_IRWXG, session->uid, session->gid);
2238 if (ret < 0) {
2239 if (ret != -EEXIST) {
2240 ERR("Trace directory creation error");
2241 goto error;
2242 }
2243 }
2244 }
2245
2246 session->kernel_session->uid = session->uid;
2247 session->kernel_session->gid = session->gid;
2248
2249 return LTTCOMM_OK;
2250
2251 error:
2252 trace_kernel_destroy_session(session->kernel_session);
2253 session->kernel_session = NULL;
2254 return ret;
2255 }
2256
2257 /*
2258 * Check if the UID or GID match the session. Root user has access to all
2259 * sessions.
2260 */
2261 static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
2262 {
2263 if (uid != session->uid && gid != session->gid && uid != 0) {
2264 return 0;
2265 } else {
2266 return 1;
2267 }
2268 }
2269
2270 /*
2271 * Count number of session permitted by uid/gid.
2272 */
2273 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2274 {
2275 unsigned int i = 0;
2276 struct ltt_session *session;
2277
2278 DBG("Counting number of available session for UID %d GID %d",
2279 uid, gid);
2280 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2281 /*
2282 * Only list the sessions the user can control.
2283 */
2284 if (!session_access_ok(session, uid, gid)) {
2285 continue;
2286 }
2287 i++;
2288 }
2289 return i;
2290 }
2291
2292 /*
2293 * Using the session list, filled a lttng_session array to send back to the
2294 * client for session listing.
2295 *
2296 * The session list lock MUST be acquired before calling this function. Use
2297 * session_lock_list() and session_unlock_list().
2298 */
2299 static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2300 gid_t gid)
2301 {
2302 unsigned int i = 0;
2303 struct ltt_session *session;
2304
2305 DBG("Getting all available session for UID %d GID %d",
2306 uid, gid);
2307 /*
2308 * Iterate over session list and append data after the control struct in
2309 * the buffer.
2310 */
2311 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2312 /*
2313 * Only list the sessions the user can control.
2314 */
2315 if (!session_access_ok(session, uid, gid)) {
2316 continue;
2317 }
2318 strncpy(sessions[i].path, session->path, PATH_MAX);
2319 sessions[i].path[PATH_MAX - 1] = '\0';
2320 strncpy(sessions[i].name, session->name, NAME_MAX);
2321 sessions[i].name[NAME_MAX - 1] = '\0';
2322 sessions[i].enabled = session->enabled;
2323 i++;
2324 }
2325 }
2326
2327 /*
2328 * Fill lttng_channel array of all channels.
2329 */
2330 static void list_lttng_channels(int domain, struct ltt_session *session,
2331 struct lttng_channel *channels)
2332 {
2333 int i = 0;
2334 struct ltt_kernel_channel *kchan;
2335
2336 DBG("Listing channels for session %s", session->name);
2337
2338 switch (domain) {
2339 case LTTNG_DOMAIN_KERNEL:
2340 /* Kernel channels */
2341 if (session->kernel_session != NULL) {
2342 cds_list_for_each_entry(kchan,
2343 &session->kernel_session->channel_list.head, list) {
2344 /* Copy lttng_channel struct to array */
2345 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
2346 channels[i].enabled = kchan->enabled;
2347 i++;
2348 }
2349 }
2350 break;
2351 case LTTNG_DOMAIN_UST:
2352 {
2353 struct lttng_ht_iter iter;
2354 struct ltt_ust_channel *uchan;
2355
2356 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
2357 &iter.iter, uchan, node.node) {
2358 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
2359 channels[i].attr.overwrite = uchan->attr.overwrite;
2360 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
2361 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
2362 channels[i].attr.switch_timer_interval =
2363 uchan->attr.switch_timer_interval;
2364 channels[i].attr.read_timer_interval =
2365 uchan->attr.read_timer_interval;
2366 channels[i].enabled = uchan->enabled;
2367 switch (uchan->attr.output) {
2368 case LTTNG_UST_MMAP:
2369 default:
2370 channels[i].attr.output = LTTNG_EVENT_MMAP;
2371 break;
2372 }
2373 i++;
2374 }
2375 break;
2376 }
2377 default:
2378 break;
2379 }
2380 }
2381
2382 /*
2383 * Create a list of ust global domain events.
2384 */
2385 static int list_lttng_ust_global_events(char *channel_name,
2386 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
2387 {
2388 int i = 0, ret = 0;
2389 unsigned int nb_event = 0;
2390 struct lttng_ht_iter iter;
2391 struct lttng_ht_node_str *node;
2392 struct ltt_ust_channel *uchan;
2393 struct ltt_ust_event *uevent;
2394 struct lttng_event *tmp;
2395
2396 DBG("Listing UST global events for channel %s", channel_name);
2397
2398 rcu_read_lock();
2399
2400 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
2401 node = lttng_ht_iter_get_node_str(&iter);
2402 if (node == NULL) {
2403 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
2404 goto error;
2405 }
2406
2407 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
2408
2409 nb_event += lttng_ht_get_count(uchan->events);
2410
2411 if (nb_event == 0) {
2412 ret = nb_event;
2413 goto error;
2414 }
2415
2416 DBG3("Listing UST global %d events", nb_event);
2417
2418 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
2419 if (tmp == NULL) {
2420 ret = -LTTCOMM_FATAL;
2421 goto error;
2422 }
2423
2424 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
2425 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
2426 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2427 tmp[i].enabled = uevent->enabled;
2428 switch (uevent->attr.instrumentation) {
2429 case LTTNG_UST_TRACEPOINT:
2430 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
2431 break;
2432 case LTTNG_UST_PROBE:
2433 tmp[i].type = LTTNG_EVENT_PROBE;
2434 break;
2435 case LTTNG_UST_FUNCTION:
2436 tmp[i].type = LTTNG_EVENT_FUNCTION;
2437 break;
2438 }
2439 tmp[i].loglevel = uevent->attr.loglevel;
2440 switch (uevent->attr.loglevel_type) {
2441 case LTTNG_UST_LOGLEVEL_ALL:
2442 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2443 break;
2444 case LTTNG_UST_LOGLEVEL_RANGE:
2445 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
2446 break;
2447 case LTTNG_UST_LOGLEVEL_SINGLE:
2448 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
2449 break;
2450 }
2451 if (uevent->filter) {
2452 tmp[i].filter = 1;
2453 }
2454 i++;
2455 }
2456
2457 ret = nb_event;
2458 *events = tmp;
2459
2460 error:
2461 rcu_read_unlock();
2462 return ret;
2463 }
2464
2465 /*
2466 * Fill lttng_event array of all kernel events in the channel.
2467 */
2468 static int list_lttng_kernel_events(char *channel_name,
2469 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
2470 {
2471 int i = 0, ret;
2472 unsigned int nb_event;
2473 struct ltt_kernel_event *event;
2474 struct ltt_kernel_channel *kchan;
2475
2476 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
2477 if (kchan == NULL) {
2478 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2479 goto error;
2480 }
2481
2482 nb_event = kchan->event_count;
2483
2484 DBG("Listing events for channel %s", kchan->channel->name);
2485
2486 if (nb_event == 0) {
2487 ret = nb_event;
2488 goto error;
2489 }
2490
2491 *events = zmalloc(nb_event * sizeof(struct lttng_event));
2492 if (*events == NULL) {
2493 ret = LTTCOMM_FATAL;
2494 goto error;
2495 }
2496
2497 /* Kernel channels */
2498 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
2499 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
2500 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2501 (*events)[i].enabled = event->enabled;
2502 switch (event->event->instrumentation) {
2503 case LTTNG_KERNEL_TRACEPOINT:
2504 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
2505 break;
2506 case LTTNG_KERNEL_KPROBE:
2507 case LTTNG_KERNEL_KRETPROBE:
2508 (*events)[i].type = LTTNG_EVENT_PROBE;
2509 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
2510 sizeof(struct lttng_kernel_kprobe));
2511 break;
2512 case LTTNG_KERNEL_FUNCTION:
2513 (*events)[i].type = LTTNG_EVENT_FUNCTION;
2514 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
2515 sizeof(struct lttng_kernel_function));
2516 break;
2517 case LTTNG_KERNEL_NOOP:
2518 (*events)[i].type = LTTNG_EVENT_NOOP;
2519 break;
2520 case LTTNG_KERNEL_SYSCALL:
2521 (*events)[i].type = LTTNG_EVENT_SYSCALL;
2522 break;
2523 case LTTNG_KERNEL_ALL:
2524 assert(0);
2525 break;
2526 }
2527 i++;
2528 }
2529
2530 return nb_event;
2531
2532 error:
2533 return ret;
2534 }
2535
2536 /*
2537 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2538 */
2539 static int cmd_disable_channel(struct ltt_session *session,
2540 int domain, char *channel_name)
2541 {
2542 int ret;
2543 struct ltt_ust_session *usess;
2544
2545 usess = session->ust_session;
2546
2547 switch (domain) {
2548 case LTTNG_DOMAIN_KERNEL:
2549 {
2550 ret = channel_kernel_disable(session->kernel_session,
2551 channel_name);
2552 if (ret != LTTCOMM_OK) {
2553 goto error;
2554 }
2555
2556 kernel_wait_quiescent(kernel_tracer_fd);
2557 break;
2558 }
2559 case LTTNG_DOMAIN_UST:
2560 {
2561 struct ltt_ust_channel *uchan;
2562 struct lttng_ht *chan_ht;
2563
2564 chan_ht = usess->domain_global.channels;
2565
2566 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
2567 if (uchan == NULL) {
2568 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2569 goto error;
2570 }
2571
2572 ret = channel_ust_disable(usess, domain, uchan);
2573 if (ret != LTTCOMM_OK) {
2574 goto error;
2575 }
2576 break;
2577 }
2578 #if 0
2579 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2580 case LTTNG_DOMAIN_UST_EXEC_NAME:
2581 case LTTNG_DOMAIN_UST_PID:
2582 #endif
2583 default:
2584 ret = LTTCOMM_UNKNOWN_DOMAIN;
2585 goto error;
2586 }
2587
2588 ret = LTTCOMM_OK;
2589
2590 error:
2591 return ret;
2592 }
2593
2594 /*
2595 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2596 */
2597 static int cmd_enable_channel(struct ltt_session *session,
2598 int domain, struct lttng_channel *attr)
2599 {
2600 int ret;
2601 struct ltt_ust_session *usess = session->ust_session;
2602 struct lttng_ht *chan_ht;
2603
2604 DBG("Enabling channel %s for session %s", attr->name, session->name);
2605
2606 switch (domain) {
2607 case LTTNG_DOMAIN_KERNEL:
2608 {
2609 struct ltt_kernel_channel *kchan;
2610
2611 kchan = trace_kernel_get_channel_by_name(attr->name,
2612 session->kernel_session);
2613 if (kchan == NULL) {
2614 ret = channel_kernel_create(session->kernel_session,
2615 attr, kernel_poll_pipe[1]);
2616 } else {
2617 ret = channel_kernel_enable(session->kernel_session, kchan);
2618 }
2619
2620 if (ret != LTTCOMM_OK) {
2621 goto error;
2622 }
2623
2624 kernel_wait_quiescent(kernel_tracer_fd);
2625 break;
2626 }
2627 case LTTNG_DOMAIN_UST:
2628 {
2629 struct ltt_ust_channel *uchan;
2630
2631 chan_ht = usess->domain_global.channels;
2632
2633 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
2634 if (uchan == NULL) {
2635 ret = channel_ust_create(usess, domain, attr);
2636 } else {
2637 ret = channel_ust_enable(usess, domain, uchan);
2638 }
2639 break;
2640 }
2641 #if 0
2642 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2643 case LTTNG_DOMAIN_UST_EXEC_NAME:
2644 case LTTNG_DOMAIN_UST_PID:
2645 #endif
2646 default:
2647 ret = LTTCOMM_UNKNOWN_DOMAIN;
2648 goto error;
2649 }
2650
2651 error:
2652 return ret;
2653 }
2654
2655 /*
2656 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2657 */
2658 static int cmd_disable_event(struct ltt_session *session, int domain,
2659 char *channel_name, char *event_name)
2660 {
2661 int ret;
2662
2663 switch (domain) {
2664 case LTTNG_DOMAIN_KERNEL:
2665 {
2666 struct ltt_kernel_channel *kchan;
2667 struct ltt_kernel_session *ksess;
2668
2669 ksess = session->kernel_session;
2670
2671 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
2672 if (kchan == NULL) {
2673 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2674 goto error;
2675 }
2676
2677 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
2678 if (ret != LTTCOMM_OK) {
2679 goto error;
2680 }
2681
2682 kernel_wait_quiescent(kernel_tracer_fd);
2683 break;
2684 }
2685 case LTTNG_DOMAIN_UST:
2686 {
2687 struct ltt_ust_channel *uchan;
2688 struct ltt_ust_session *usess;
2689
2690 usess = session->ust_session;
2691
2692 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2693 channel_name);
2694 if (uchan == NULL) {
2695 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2696 goto error;
2697 }
2698
2699 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
2700 if (ret != LTTCOMM_OK) {
2701 goto error;
2702 }
2703
2704 DBG3("Disable UST event %s in channel %s completed", event_name,
2705 channel_name);
2706 break;
2707 }
2708 #if 0
2709 case LTTNG_DOMAIN_UST_EXEC_NAME:
2710 case LTTNG_DOMAIN_UST_PID:
2711 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2712 #endif
2713 default:
2714 ret = LTTCOMM_UND;
2715 goto error;
2716 }
2717
2718 ret = LTTCOMM_OK;
2719
2720 error:
2721 return ret;
2722 }
2723
2724 /*
2725 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2726 */
2727 static int cmd_disable_event_all(struct ltt_session *session, int domain,
2728 char *channel_name)
2729 {
2730 int ret;
2731
2732 switch (domain) {
2733 case LTTNG_DOMAIN_KERNEL:
2734 {
2735 struct ltt_kernel_session *ksess;
2736 struct ltt_kernel_channel *kchan;
2737
2738 ksess = session->kernel_session;
2739
2740 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
2741 if (kchan == NULL) {
2742 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2743 goto error;
2744 }
2745
2746 ret = event_kernel_disable_all(ksess, kchan);
2747 if (ret != LTTCOMM_OK) {
2748 goto error;
2749 }
2750
2751 kernel_wait_quiescent(kernel_tracer_fd);
2752 break;
2753 }
2754 case LTTNG_DOMAIN_UST:
2755 {
2756 struct ltt_ust_session *usess;
2757 struct ltt_ust_channel *uchan;
2758
2759 usess = session->ust_session;
2760
2761 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2762 channel_name);
2763 if (uchan == NULL) {
2764 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2765 goto error;
2766 }
2767
2768 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
2769 if (ret != 0) {
2770 goto error;
2771 }
2772
2773 DBG3("Disable all UST events in channel %s completed", channel_name);
2774
2775 break;
2776 }
2777 #if 0
2778 case LTTNG_DOMAIN_UST_EXEC_NAME:
2779 case LTTNG_DOMAIN_UST_PID:
2780 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2781 #endif
2782 default:
2783 ret = LTTCOMM_UND;
2784 goto error;
2785 }
2786
2787 ret = LTTCOMM_OK;
2788
2789 error:
2790 return ret;
2791 }
2792
2793 /*
2794 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2795 */
2796 static int cmd_add_context(struct ltt_session *session, int domain,
2797 char *channel_name, char *event_name, struct lttng_event_context *ctx)
2798 {
2799 int ret;
2800
2801 switch (domain) {
2802 case LTTNG_DOMAIN_KERNEL:
2803 /* Add kernel context to kernel tracer */
2804 ret = context_kernel_add(session->kernel_session, ctx,
2805 event_name, channel_name);
2806 if (ret != LTTCOMM_OK) {
2807 goto error;
2808 }
2809 break;
2810 case LTTNG_DOMAIN_UST:
2811 {
2812 struct ltt_ust_session *usess = session->ust_session;
2813
2814 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
2815 if (ret != LTTCOMM_OK) {
2816 goto error;
2817 }
2818 break;
2819 }
2820 #if 0
2821 case LTTNG_DOMAIN_UST_EXEC_NAME:
2822 case LTTNG_DOMAIN_UST_PID:
2823 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2824 #endif
2825 default:
2826 ret = LTTCOMM_UND;
2827 goto error;
2828 }
2829
2830 ret = LTTCOMM_OK;
2831
2832 error:
2833 return ret;
2834 }
2835
2836 /*
2837 * Command LTTNG_SET_FILTER processed by the client thread.
2838 */
2839 static int cmd_set_filter(struct ltt_session *session, int domain,
2840 char *channel_name, char *event_name,
2841 struct lttng_filter_bytecode *bytecode)
2842 {
2843 int ret;
2844
2845 switch (domain) {
2846 case LTTNG_DOMAIN_KERNEL:
2847 ret = LTTCOMM_FATAL;
2848 break;
2849 case LTTNG_DOMAIN_UST:
2850 {
2851 struct ltt_ust_session *usess = session->ust_session;
2852
2853 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
2854 if (ret != LTTCOMM_OK) {
2855 goto error;
2856 }
2857 break;
2858 }
2859 #if 0
2860 case LTTNG_DOMAIN_UST_EXEC_NAME:
2861 case LTTNG_DOMAIN_UST_PID:
2862 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2863 #endif
2864 default:
2865 ret = LTTCOMM_UND;
2866 goto error;
2867 }
2868
2869 ret = LTTCOMM_OK;
2870
2871 error:
2872 return ret;
2873
2874 }
2875
2876 /*
2877 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2878 */
2879 static int cmd_enable_event(struct ltt_session *session, int domain,
2880 char *channel_name, struct lttng_event *event)
2881 {
2882 int ret;
2883 struct lttng_channel *attr;
2884 struct ltt_ust_session *usess = session->ust_session;
2885
2886 switch (domain) {
2887 case LTTNG_DOMAIN_KERNEL:
2888 {
2889 struct ltt_kernel_channel *kchan;
2890
2891 kchan = trace_kernel_get_channel_by_name(channel_name,
2892 session->kernel_session);
2893 if (kchan == NULL) {
2894 attr = channel_new_default_attr(domain);
2895 if (attr == NULL) {
2896 ret = LTTCOMM_FATAL;
2897 goto error;
2898 }
2899 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2900
2901 /* This call will notify the kernel thread */
2902 ret = channel_kernel_create(session->kernel_session,
2903 attr, kernel_poll_pipe[1]);
2904 if (ret != LTTCOMM_OK) {
2905 free(attr);
2906 goto error;
2907 }
2908 free(attr);
2909 }
2910
2911 /* Get the newly created kernel channel pointer */
2912 kchan = trace_kernel_get_channel_by_name(channel_name,
2913 session->kernel_session);
2914 if (kchan == NULL) {
2915 /* This sould not happen... */
2916 ret = LTTCOMM_FATAL;
2917 goto error;
2918 }
2919
2920 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
2921 event);
2922 if (ret != LTTCOMM_OK) {
2923 goto error;
2924 }
2925
2926 kernel_wait_quiescent(kernel_tracer_fd);
2927 break;
2928 }
2929 case LTTNG_DOMAIN_UST:
2930 {
2931 struct lttng_channel *attr;
2932 struct ltt_ust_channel *uchan;
2933
2934 /* Get channel from global UST domain */
2935 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2936 channel_name);
2937 if (uchan == NULL) {
2938 /* Create default channel */
2939 attr = channel_new_default_attr(domain);
2940 if (attr == NULL) {
2941 ret = LTTCOMM_FATAL;
2942 goto error;
2943 }
2944 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2945 attr->name[NAME_MAX - 1] = '\0';
2946
2947 ret = channel_ust_create(usess, domain, attr);
2948 if (ret != LTTCOMM_OK) {
2949 free(attr);
2950 goto error;
2951 }
2952 free(attr);
2953
2954 /* Get the newly created channel reference back */
2955 uchan = trace_ust_find_channel_by_name(
2956 usess->domain_global.channels, channel_name);
2957 if (uchan == NULL) {
2958 /* Something is really wrong */
2959 ret = LTTCOMM_FATAL;
2960 goto error;
2961 }
2962 }
2963
2964 /* At this point, the session and channel exist on the tracer */
2965 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
2966 if (ret != LTTCOMM_OK) {
2967 goto error;
2968 }
2969 break;
2970 }
2971 #if 0
2972 case LTTNG_DOMAIN_UST_EXEC_NAME:
2973 case LTTNG_DOMAIN_UST_PID:
2974 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2975 #endif
2976 default:
2977 ret = LTTCOMM_UND;
2978 goto error;
2979 }
2980
2981 ret = LTTCOMM_OK;
2982
2983 error:
2984 return ret;
2985 }
2986
2987 /*
2988 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2989 */
2990 static int cmd_enable_event_all(struct ltt_session *session, int domain,
2991 char *channel_name, int event_type)
2992 {
2993 int ret;
2994 struct ltt_kernel_channel *kchan;
2995
2996 switch (domain) {
2997 case LTTNG_DOMAIN_KERNEL:
2998 kchan = trace_kernel_get_channel_by_name(channel_name,
2999 session->kernel_session);
3000 if (kchan == NULL) {
3001 /* This call will notify the kernel thread */
3002 ret = channel_kernel_create(session->kernel_session, NULL,
3003 kernel_poll_pipe[1]);
3004 if (ret != LTTCOMM_OK) {
3005 goto error;
3006 }
3007
3008 /* Get the newly created kernel channel pointer */
3009 kchan = trace_kernel_get_channel_by_name(channel_name,
3010 session->kernel_session);
3011 if (kchan == NULL) {
3012 /* This sould not happen... */
3013 ret = LTTCOMM_FATAL;
3014 goto error;
3015 }
3016
3017 }
3018
3019 switch (event_type) {
3020 case LTTNG_EVENT_SYSCALL:
3021 ret = event_kernel_enable_all_syscalls(session->kernel_session,
3022 kchan, kernel_tracer_fd);
3023 break;
3024 case LTTNG_EVENT_TRACEPOINT:
3025 /*
3026 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3027 * events already registered to the channel.
3028 */
3029 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
3030 kchan, kernel_tracer_fd);
3031 break;
3032 case LTTNG_EVENT_ALL:
3033 /* Enable syscalls and tracepoints */
3034 ret = event_kernel_enable_all(session->kernel_session,
3035 kchan, kernel_tracer_fd);
3036 break;
3037 default:
3038 ret = LTTCOMM_KERN_ENABLE_FAIL;
3039 goto error;
3040 }
3041
3042 /* Manage return value */
3043 if (ret != LTTCOMM_OK) {
3044 goto error;
3045 }
3046
3047 kernel_wait_quiescent(kernel_tracer_fd);
3048 break;
3049 case LTTNG_DOMAIN_UST:
3050 {
3051 struct lttng_channel *attr;
3052 struct ltt_ust_channel *uchan;
3053 struct ltt_ust_session *usess = session->ust_session;
3054
3055 /* Get channel from global UST domain */
3056 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3057 channel_name);
3058 if (uchan == NULL) {
3059 /* Create default channel */
3060 attr = channel_new_default_attr(domain);
3061 if (attr == NULL) {
3062 ret = LTTCOMM_FATAL;
3063 goto error;
3064 }
3065 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3066 attr->name[NAME_MAX - 1] = '\0';
3067
3068 /* Use the internal command enable channel */
3069 ret = channel_ust_create(usess, domain, attr);
3070 if (ret != LTTCOMM_OK) {
3071 free(attr);
3072 goto error;
3073 }
3074 free(attr);
3075
3076 /* Get the newly created channel reference back */
3077 uchan = trace_ust_find_channel_by_name(
3078 usess->domain_global.channels, channel_name);
3079 if (uchan == NULL) {
3080 /* Something is really wrong */
3081 ret = LTTCOMM_FATAL;
3082 goto error;
3083 }
3084 }
3085
3086 /* At this point, the session and channel exist on the tracer */
3087
3088 switch (event_type) {
3089 case LTTNG_EVENT_ALL:
3090 case LTTNG_EVENT_TRACEPOINT:
3091 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
3092 if (ret != LTTCOMM_OK) {
3093 goto error;
3094 }
3095 break;
3096 default:
3097 ret = LTTCOMM_UST_ENABLE_FAIL;
3098 goto error;
3099 }
3100
3101 /* Manage return value */
3102 if (ret != LTTCOMM_OK) {
3103 goto error;
3104 }
3105
3106 break;
3107 }
3108 #if 0
3109 case LTTNG_DOMAIN_UST_EXEC_NAME:
3110 case LTTNG_DOMAIN_UST_PID:
3111 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
3112 #endif
3113 default:
3114 ret = LTTCOMM_UND;
3115 goto error;
3116 }
3117
3118 ret = LTTCOMM_OK;
3119
3120 error:
3121 return ret;
3122 }
3123
3124 /*
3125 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3126 */
3127 static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
3128 {
3129 int ret;
3130 ssize_t nb_events = 0;
3131
3132 switch (domain) {
3133 case LTTNG_DOMAIN_KERNEL:
3134 nb_events = kernel_list_events(kernel_tracer_fd, events);
3135 if (nb_events < 0) {
3136 ret = LTTCOMM_KERN_LIST_FAIL;
3137 goto error;
3138 }
3139 break;
3140 case LTTNG_DOMAIN_UST:
3141 nb_events = ust_app_list_events(events);
3142 if (nb_events < 0) {
3143 ret = LTTCOMM_UST_LIST_FAIL;
3144 goto error;
3145 }
3146 break;
3147 default:
3148 ret = LTTCOMM_UND;
3149 goto error;
3150 }
3151
3152 return nb_events;
3153
3154 error:
3155 /* Return negative value to differentiate return code */
3156 return -ret;
3157 }
3158
3159 /*
3160 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3161 */
3162 static ssize_t cmd_list_tracepoint_fields(int domain,
3163 struct lttng_event_field **fields)
3164 {
3165 int ret;
3166 ssize_t nb_fields = 0;
3167
3168 switch (domain) {
3169 case LTTNG_DOMAIN_UST:
3170 nb_fields = ust_app_list_event_fields(fields);
3171 if (nb_fields < 0) {
3172 ret = LTTCOMM_UST_LIST_FAIL;
3173 goto error;
3174 }
3175 break;
3176 case LTTNG_DOMAIN_KERNEL:
3177 default: /* fall-through */
3178 ret = LTTCOMM_UND;
3179 goto error;
3180 }
3181
3182 return nb_fields;
3183
3184 error:
3185 /* Return negative value to differentiate return code */
3186 return -ret;
3187 }
3188
3189 /*
3190 * Command LTTNG_START_TRACE processed by the client thread.
3191 */
3192 static int cmd_start_trace(struct ltt_session *session)
3193 {
3194 int ret;
3195 struct ltt_kernel_session *ksession;
3196 struct ltt_ust_session *usess;
3197 struct ltt_kernel_channel *kchan;
3198
3199 /* Ease our life a bit ;) */
3200 ksession = session->kernel_session;
3201 usess = session->ust_session;
3202
3203 if (session->enabled) {
3204 /* Already started. */
3205 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3206 goto error;
3207 }
3208
3209 session->enabled = 1;
3210
3211 ret = setup_relayd(session);
3212 if (ret != LTTCOMM_OK) {
3213 ERR("Error setting up relayd for session %s", session->name);
3214 goto error;
3215 }
3216
3217 /* Kernel tracing */
3218 if (ksession != NULL) {
3219 /* Open kernel metadata */
3220 if (ksession->metadata == NULL) {
3221 ret = kernel_open_metadata(ksession,
3222 ksession->consumer->dst.trace_path);
3223 if (ret < 0) {
3224 ret = LTTCOMM_KERN_META_FAIL;
3225 goto error;
3226 }
3227 }
3228
3229 /* Open kernel metadata stream */
3230 if (ksession->metadata_stream_fd < 0) {
3231 ret = kernel_open_metadata_stream(ksession);
3232 if (ret < 0) {
3233 ERR("Kernel create metadata stream failed");
3234 ret = LTTCOMM_KERN_STREAM_FAIL;
3235 goto error;
3236 }
3237 }
3238
3239 /* For each channel */
3240 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3241 if (kchan->stream_count == 0) {
3242 ret = kernel_open_channel_stream(kchan);
3243 if (ret < 0) {
3244 ret = LTTCOMM_KERN_STREAM_FAIL;
3245 goto error;
3246 }
3247 /* Update the stream global counter */
3248 ksession->stream_count_global += ret;
3249 }
3250 }
3251
3252 /* Setup kernel consumer socket and send fds to it */
3253 ret = init_kernel_tracing(ksession);
3254 if (ret < 0) {
3255 ret = LTTCOMM_KERN_START_FAIL;
3256 goto error;
3257 }
3258
3259 /* This start the kernel tracing */
3260 ret = kernel_start_session(ksession);
3261 if (ret < 0) {
3262 ret = LTTCOMM_KERN_START_FAIL;
3263 goto error;
3264 }
3265
3266 /* Quiescent wait after starting trace */
3267 kernel_wait_quiescent(kernel_tracer_fd);
3268 }
3269
3270 /* Flag session that trace should start automatically */
3271 if (usess) {
3272 usess->start_trace = 1;
3273
3274 ret = ust_app_start_trace_all(usess);
3275 if (ret < 0) {
3276 ret = LTTCOMM_UST_START_FAIL;
3277 goto error;
3278 }
3279 }
3280
3281 ret = LTTCOMM_OK;
3282
3283 error:
3284 return ret;
3285 }
3286
3287 /*
3288 * Command LTTNG_STOP_TRACE processed by the client thread.
3289 */
3290 static int cmd_stop_trace(struct ltt_session *session)
3291 {
3292 int ret;
3293 struct ltt_kernel_channel *kchan;
3294 struct ltt_kernel_session *ksession;
3295 struct ltt_ust_session *usess;
3296
3297 /* Short cut */
3298 ksession = session->kernel_session;
3299 usess = session->ust_session;
3300
3301 if (!session->enabled) {
3302 ret = LTTCOMM_TRACE_ALREADY_STOPPED;
3303 goto error;
3304 }
3305
3306 session->enabled = 0;
3307
3308 /* Kernel tracer */
3309 if (ksession != NULL) {
3310 DBG("Stop kernel tracing");
3311
3312 /* Flush metadata if exist */
3313 if (ksession->metadata_stream_fd >= 0) {
3314 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
3315 if (ret < 0) {
3316 ERR("Kernel metadata flush failed");
3317 }
3318 }
3319
3320 /* Flush all buffers before stopping */
3321 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3322 ret = kernel_flush_buffer(kchan);
3323 if (ret < 0) {
3324 ERR("Kernel flush buffer error");
3325 }
3326 }
3327
3328 ret = kernel_stop_session(ksession);
3329 if (ret < 0) {
3330 ret = LTTCOMM_KERN_STOP_FAIL;
3331 goto error;
3332 }
3333
3334 kernel_wait_quiescent(kernel_tracer_fd);
3335 }
3336
3337 if (usess) {
3338 usess->start_trace = 0;
3339
3340 ret = ust_app_stop_trace_all(usess);
3341 if (ret < 0) {
3342 ret = LTTCOMM_UST_STOP_FAIL;
3343 goto error;
3344 }
3345 }
3346
3347 ret = LTTCOMM_OK;
3348
3349 error:
3350 return ret;
3351 }
3352
3353 /*
3354 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3355 */
3356 static int cmd_create_session_uri(char *name, struct lttng_uri *ctrl_uri,
3357 struct lttng_uri *data_uri, unsigned int enable_consumer,
3358 lttng_sock_cred *creds)
3359 {
3360 int ret;
3361 char *path = NULL;
3362 struct ltt_session *session;
3363 struct consumer_output *consumer;
3364
3365 /* Verify if the session already exist */
3366 session = session_find_by_name(name);
3367 if (session != NULL) {
3368 ret = LTTCOMM_EXIST_SESS;
3369 goto error;
3370 }
3371
3372 /* TODO: validate URIs */
3373
3374 /* Create default consumer output */
3375 consumer = consumer_create_output(CONSUMER_DST_LOCAL);
3376 if (consumer == NULL) {
3377 ret = LTTCOMM_FATAL;
3378 goto error;
3379 }
3380 strncpy(consumer->subdir, ctrl_uri->subdir, sizeof(consumer->subdir));
3381 DBG2("Consumer subdir set to %s", consumer->subdir);
3382
3383 switch (ctrl_uri->dtype) {
3384 case LTTNG_DST_IPV4:
3385 case LTTNG_DST_IPV6:
3386 /* Set control URI into consumer output object */
3387 ret = consumer_set_network_uri(consumer, ctrl_uri);
3388 if (ret < 0) {
3389 ret = LTTCOMM_FATAL;
3390 goto error;
3391 }
3392
3393 /* Set data URI into consumer output object */
3394 ret = consumer_set_network_uri(consumer, data_uri);
3395 if (ret < 0) {
3396 ret = LTTCOMM_FATAL;
3397 goto error;
3398 }
3399
3400 /* Empty path since the session is network */
3401 path = "";
3402 break;
3403 case LTTNG_DST_PATH:
3404 /* Very volatile pointer. Only used for the create session. */
3405 path = ctrl_uri->dst.path;
3406 strncpy(consumer->dst.trace_path, path,
3407 sizeof(consumer->dst.trace_path));
3408 break;
3409 }
3410
3411 /* Set if the consumer is enabled or not */
3412 consumer->enabled = enable_consumer;
3413
3414 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
3415 LTTNG_SOCK_GET_GID_CRED(creds));
3416 if (ret != LTTCOMM_OK) {
3417 goto consumer_error;
3418 }
3419
3420 /* Get the newly created session pointer back */
3421 session = session_find_by_name(name);
3422 assert(session);
3423
3424 /* Assign consumer to session */
3425 session->consumer = consumer;
3426
3427 return LTTCOMM_OK;
3428
3429 consumer_error:
3430 consumer_destroy_output(consumer);
3431 error:
3432 return ret;
3433 }
3434
3435 /*
3436 * Command LTTNG_CREATE_SESSION processed by the client thread.
3437 */
3438 static int cmd_create_session(char *name, char *path, lttng_sock_cred *creds)
3439 {
3440 int ret;
3441 struct lttng_uri uri;
3442
3443 /* Zeroed temporary URI */
3444 memset(&uri, 0, sizeof(uri));
3445
3446 uri.dtype = LTTNG_DST_PATH;
3447 uri.utype = LTTNG_URI_DST;
3448 strncpy(uri.dst.path, path, sizeof(uri.dst.path));
3449
3450 /* TODO: Strip date-time from path and put it in uri's subdir */
3451
3452 ret = cmd_create_session_uri(name, &uri, NULL, 1, creds);
3453 if (ret != LTTCOMM_OK) {
3454 goto error;
3455 }
3456
3457 error:
3458 return ret;
3459 }
3460
3461 /*
3462 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3463 */
3464 static int cmd_destroy_session(struct ltt_session *session, char *name)
3465 {
3466 int ret;
3467
3468 /* Clean kernel session teardown */
3469 teardown_kernel_session(session);
3470 /* UST session teardown */
3471 teardown_ust_session(session);
3472
3473 /*
3474 * Must notify the kernel thread here to update it's poll setin order
3475 * to remove the channel(s)' fd just destroyed.
3476 */
3477 ret = notify_thread_pipe(kernel_poll_pipe[1]);
3478 if (ret < 0) {
3479 PERROR("write kernel poll pipe");
3480 }
3481
3482 ret = session_destroy(session);
3483
3484 return ret;
3485 }
3486
3487 /*
3488 * Command LTTNG_CALIBRATE processed by the client thread.
3489 */
3490 static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
3491 {
3492 int ret;
3493
3494 switch (domain) {
3495 case LTTNG_DOMAIN_KERNEL:
3496 {
3497 struct lttng_kernel_calibrate kcalibrate;
3498
3499 kcalibrate.type = calibrate->type;
3500 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
3501 if (ret < 0) {
3502 ret = LTTCOMM_KERN_ENABLE_FAIL;
3503 goto error;
3504 }
3505 break;
3506 }
3507 case LTTNG_DOMAIN_UST:
3508 {
3509 struct lttng_ust_calibrate ucalibrate;
3510
3511 ucalibrate.type = calibrate->type;
3512 ret = ust_app_calibrate_glb(&ucalibrate);
3513 if (ret < 0) {
3514 ret = LTTCOMM_UST_CALIBRATE_FAIL;
3515 goto error;
3516 }
3517 break;
3518 }
3519 default:
3520 ret = LTTCOMM_UND;
3521 goto error;
3522 }
3523
3524 ret = LTTCOMM_OK;
3525
3526 error:
3527 return ret;
3528 }
3529
3530 /*
3531 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3532 */
3533 static int cmd_register_consumer(struct ltt_session *session, int domain,
3534 char *sock_path)
3535 {
3536 int ret, sock;
3537
3538 switch (domain) {
3539 case LTTNG_DOMAIN_KERNEL:
3540 /* Can't register a consumer if there is already one */
3541 if (session->kernel_session->consumer_fds_sent != 0) {
3542 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3543 goto error;
3544 }
3545
3546 sock = lttcomm_connect_unix_sock(sock_path);
3547 if (sock < 0) {
3548 ret = LTTCOMM_CONNECT_FAIL;
3549 goto error;
3550 }
3551
3552 session->kernel_session->consumer_fd = sock;
3553 break;
3554 default:
3555 /* TODO: Userspace tracing */
3556 ret = LTTCOMM_UND;
3557 goto error;
3558 }
3559
3560 ret = LTTCOMM_OK;
3561
3562 error:
3563 return ret;
3564 }
3565
3566 /*
3567 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3568 */
3569 static ssize_t cmd_list_domains(struct ltt_session *session,
3570 struct lttng_domain **domains)
3571 {
3572 int ret, index = 0;
3573 ssize_t nb_dom = 0;
3574
3575 if (session->kernel_session != NULL) {
3576 DBG3("Listing domains found kernel domain");
3577 nb_dom++;
3578 }
3579
3580 if (session->ust_session != NULL) {
3581 DBG3("Listing domains found UST global domain");
3582 nb_dom++;
3583 }
3584
3585 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3586 if (*domains == NULL) {
3587 ret = -LTTCOMM_FATAL;
3588 goto error;
3589 }
3590
3591 if (session->kernel_session != NULL) {
3592 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3593 index++;
3594 }
3595
3596 if (session->ust_session != NULL) {
3597 (*domains)[index].type = LTTNG_DOMAIN_UST;
3598 index++;
3599 }
3600
3601 return nb_dom;
3602
3603 error:
3604 return ret;
3605 }
3606
3607 /*
3608 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3609 */
3610 static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
3611 struct lttng_channel **channels)
3612 {
3613 int ret;
3614 ssize_t nb_chan = 0;
3615
3616 switch (domain) {
3617 case LTTNG_DOMAIN_KERNEL:
3618 if (session->kernel_session != NULL) {
3619 nb_chan = session->kernel_session->channel_count;
3620 }
3621 DBG3("Number of kernel channels %zd", nb_chan);
3622 break;
3623 case LTTNG_DOMAIN_UST:
3624 if (session->ust_session != NULL) {
3625 nb_chan = lttng_ht_get_count(
3626 session->ust_session->domain_global.channels);
3627 }
3628 DBG3("Number of UST global channels %zd", nb_chan);
3629 break;
3630 default:
3631 *channels = NULL;
3632 ret = -LTTCOMM_UND;
3633 goto error;
3634 }
3635
3636 if (nb_chan > 0) {
3637 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
3638 if (*channels == NULL) {
3639 ret = -LTTCOMM_FATAL;
3640 goto error;
3641 }
3642
3643 list_lttng_channels(domain, session, *channels);
3644 } else {
3645 *channels = NULL;
3646 }
3647
3648 return nb_chan;
3649
3650 error:
3651 return ret;
3652 }
3653
3654 /*
3655 * Command LTTNG_LIST_EVENTS processed by the client thread.
3656 */
3657 static ssize_t cmd_list_events(int domain, struct ltt_session *session,
3658 char *channel_name, struct lttng_event **events)
3659 {
3660 int ret = 0;
3661 ssize_t nb_event = 0;
3662
3663 switch (domain) {
3664 case LTTNG_DOMAIN_KERNEL:
3665 if (session->kernel_session != NULL) {
3666 nb_event = list_lttng_kernel_events(channel_name,
3667 session->kernel_session, events);
3668 }
3669 break;
3670 case LTTNG_DOMAIN_UST:
3671 {
3672 if (session->ust_session != NULL) {
3673 nb_event = list_lttng_ust_global_events(channel_name,
3674 &session->ust_session->domain_global, events);
3675 }
3676 break;
3677 }
3678 default:
3679 ret = -LTTCOMM_UND;
3680 goto error;
3681 }
3682
3683 ret = nb_event;
3684
3685 error:
3686 return ret;
3687 }
3688
3689 /*
3690 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3691 */
3692 static int cmd_set_consumer_uri(int domain, struct ltt_session *session,
3693 struct lttng_uri *uri)
3694 {
3695 int ret;
3696 struct ltt_kernel_session *ksess = session->kernel_session;
3697 struct ltt_ust_session *usess = session->ust_session;
3698 struct consumer_output *consumer;
3699
3700 /* Can't enable consumer after session started. */
3701 if (session->enabled) {
3702 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3703 goto error;
3704 }
3705
3706 switch (domain) {
3707 case LTTNG_DOMAIN_KERNEL:
3708 /* Code flow error if we don't have a kernel session here. */
3709 assert(ksess);
3710
3711 /* Create consumer output if none exists */
3712 consumer = ksess->tmp_consumer;
3713 if (consumer == NULL) {
3714 consumer = consumer_copy_output(ksess->consumer);
3715 if (consumer == NULL) {
3716 ret = LTTCOMM_FATAL;
3717 goto error;
3718 }
3719 /* Reassign new pointer */
3720 ksess->tmp_consumer = consumer;
3721 }
3722
3723 switch (uri->dtype) {
3724 case LTTNG_DST_IPV4:
3725 case LTTNG_DST_IPV6:
3726 DBG2("Setting network URI for kernel session %s", session->name);
3727
3728 /* Set URI into consumer output object */
3729 ret = consumer_set_network_uri(consumer, uri);
3730 if (ret < 0) {
3731 ret = LTTCOMM_FATAL;
3732 goto error;
3733 }
3734
3735 /* On a new subdir, reappend the default trace dir. */
3736 if (strlen(uri->subdir) != 0) {
3737 strncat(consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
3738 sizeof(consumer->subdir));
3739 }
3740
3741 ret = send_socket_relayd_consumer(domain, session, uri, consumer,
3742 ksess->consumer_fd);
3743 if (ret != LTTCOMM_OK) {
3744 goto error;
3745 }
3746 break;
3747 case LTTNG_DST_PATH:
3748 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
3749 memset(consumer->dst.trace_path, 0,
3750 sizeof(consumer->dst.trace_path));
3751 strncpy(consumer->dst.trace_path, uri->dst.path,
3752 sizeof(consumer->dst.trace_path));
3753 /* Append default kernel trace dir */
3754 strncat(consumer->dst.trace_path, DEFAULT_KERNEL_TRACE_DIR,
3755 sizeof(consumer->dst.trace_path));
3756 break;
3757 }
3758
3759 /* All good! */
3760 break;
3761 case LTTNG_DOMAIN_UST:
3762 /* Code flow error if we don't have a kernel session here. */
3763 assert(usess);
3764
3765 /* Create consumer output if none exists */
3766 consumer = usess->tmp_consumer;
3767 if (consumer == NULL) {
3768 consumer = consumer_copy_output(usess->consumer);
3769 if (consumer == NULL) {
3770 ret = LTTCOMM_FATAL;
3771 goto error;
3772 }
3773 /* Reassign new pointer */
3774 usess->tmp_consumer = consumer;
3775 }
3776
3777 switch (uri->dtype) {
3778 case LTTNG_DST_IPV4:
3779 case LTTNG_DST_IPV6:
3780 {
3781 DBG2("Setting network URI for UST session %s", session->name);
3782
3783 /* Set URI into consumer object */
3784 ret = consumer_set_network_uri(consumer, uri);
3785 if (ret < 0) {
3786 ret = LTTCOMM_FATAL;
3787 goto error;
3788 }
3789
3790 /* On a new subdir, reappend the default trace dir. */
3791 if (strlen(uri->subdir) != 0) {
3792 strncat(consumer->subdir, DEFAULT_UST_TRACE_DIR,
3793 sizeof(consumer->subdir));
3794 }
3795
3796 if (ust_consumerd64_fd >= 0) {
3797 ret = send_socket_relayd_consumer(domain, session, uri,
3798 consumer, ust_consumerd64_fd);
3799 if (ret != LTTCOMM_OK) {
3800 goto error;
3801 }
3802 }
3803
3804 if (ust_consumerd32_fd >= 0) {
3805 ret = send_socket_relayd_consumer(domain, session, uri,
3806 consumer, ust_consumerd32_fd);
3807 if (ret != LTTCOMM_OK) {
3808 goto error;
3809 }
3810 }
3811
3812 break;
3813 }
3814 case LTTNG_DST_PATH:
3815 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
3816 memset(consumer->dst.trace_path, 0,
3817 sizeof(consumer->dst.trace_path));
3818 strncpy(consumer->dst.trace_path, uri->dst.path,
3819 sizeof(consumer->dst.trace_path));
3820 /* Append default UST trace dir */
3821 strncat(consumer->dst.trace_path, DEFAULT_UST_TRACE_DIR,
3822 sizeof(consumer->dst.trace_path));
3823 break;
3824 }
3825 break;
3826 }
3827
3828 /* All good! */
3829 ret = LTTCOMM_OK;
3830
3831 error:
3832 return ret;
3833 }
3834
3835 /*
3836 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3837 */
3838 static int cmd_disable_consumer(int domain, struct ltt_session *session)
3839 {
3840 int ret;
3841 struct ltt_kernel_session *ksess = session->kernel_session;
3842 struct ltt_ust_session *usess = session->ust_session;
3843 struct consumer_output *consumer;
3844
3845 if (session->enabled) {
3846 /* Can't disable consumer on an already started session */
3847 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3848 goto error;
3849 }
3850
3851 switch (domain) {
3852 case LTTNG_DOMAIN_KERNEL:
3853 /* Code flow error if we don't have a kernel session here. */
3854 assert(ksess);
3855
3856 DBG("Disabling kernel consumer");
3857 consumer = ksess->consumer;
3858
3859 break;
3860 case LTTNG_DOMAIN_UST:
3861 /* Code flow error if we don't have a UST session here. */
3862 assert(usess);
3863
3864 DBG("Disabling UST consumer");
3865 consumer = usess->consumer;
3866
3867 break;
3868 default:
3869 ret = LTTCOMM_UNKNOWN_DOMAIN;
3870 goto error;
3871 }
3872
3873 assert(consumer);
3874 consumer->enabled = 0;
3875
3876 /* Success at this point */
3877 ret = LTTCOMM_OK;
3878
3879 error:
3880 return ret;
3881 }
3882
3883 /*
3884 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3885 */
3886 static int cmd_enable_consumer(int domain, struct ltt_session *session)
3887 {
3888 int ret;
3889 struct ltt_kernel_session *ksess = session->kernel_session;
3890 struct ltt_ust_session *usess = session->ust_session;
3891 struct consumer_output *tmp_out;
3892
3893 /* Can't enable consumer after session started. */
3894 if (session->enabled) {
3895 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3896 goto error;
3897 }
3898
3899 switch (domain) {
3900 case LTTNG_DOMAIN_KERNEL:
3901 /* Code flow error if we don't have a kernel session here. */
3902 assert(ksess);
3903
3904 /*
3905 * Check if we have already sent fds to the consumer. In that case,
3906 * the enable-consumer command can't be used because a start trace
3907 * had previously occured.
3908 */
3909 if (ksess->consumer_fds_sent) {
3910 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3911 goto error;
3912 }
3913
3914 tmp_out = ksess->tmp_consumer;
3915 if (tmp_out == NULL) {
3916 /* No temp. consumer output exists. Using the current one. */
3917 DBG3("No temporary consumer. Using default");
3918 ret = LTTCOMM_OK;
3919 goto error;
3920 }
3921
3922 switch (tmp_out->type) {
3923 case CONSUMER_DST_LOCAL:
3924 DBG2("Consumer output is local. Creating directory(ies)");
3925
3926 /* Create directory(ies) */
3927 ret = run_as_mkdir_recursive(tmp_out->dst.trace_path,
3928 S_IRWXU | S_IRWXG, session->uid, session->gid);
3929 if (ret < 0) {
3930 if (ret != -EEXIST) {
3931 ERR("Trace directory creation error");
3932 ret = LTTCOMM_FATAL;
3933 goto error;
3934 }
3935 }
3936 break;
3937 case CONSUMER_DST_NET:
3938 DBG2("Consumer output is network. Validating URIs");
3939 /* Validate if we have both control and data path set. */
3940 if (!tmp_out->dst.net.control_isset) {
3941 ret = LTTCOMM_URI_CTRL_MISS;
3942 goto error;
3943 }
3944
3945 if (!tmp_out->dst.net.data_isset) {
3946 ret = LTTCOMM_URI_DATA_MISS;
3947 goto error;
3948 }
3949
3950 /* Check established network session state */
3951 if (session->net_handle == 0) {
3952 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3953 ERR("Session network handle is not set on enable-consumer");
3954 goto error;
3955 }
3956
3957 /* Append default kernel trace dir to subdir */
3958 strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
3959 sizeof(ksess->consumer->subdir));
3960
3961 break;
3962 }
3963
3964 /*
3965 * @session-lock
3966 * This is race free for now since the session lock is acquired before
3967 * ending up in this function. No other threads can access this kernel
3968 * session without this lock hence freeing the consumer output object
3969 * is valid.
3970 */
3971 consumer_destroy_output(ksess->consumer);
3972 ksess->consumer = tmp_out;
3973 ksess->tmp_consumer = NULL;
3974
3975 break;
3976 case LTTNG_DOMAIN_UST:
3977 /* Code flow error if we don't have a UST session here. */
3978 assert(usess);
3979
3980 /*
3981 * Check if we have already sent fds to the consumer. In that case,
3982 * the enable-consumer command can't be used because a start trace
3983 * had previously occured.
3984 */
3985 if (usess->start_trace) {
3986 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3987 goto error;
3988 }
3989
3990 tmp_out = usess->tmp_consumer;
3991 if (tmp_out == NULL) {
3992 /* No temp. consumer output exists. Using the current one. */
3993 DBG3("No temporary consumer. Using default");
3994 ret = LTTCOMM_OK;
3995 goto error;
3996 }
3997
3998 switch (tmp_out->type) {
3999 case CONSUMER_DST_LOCAL:
4000 DBG2("Consumer output is local. Creating directory(ies)");
4001
4002 /* Create directory(ies) */
4003 ret = run_as_mkdir_recursive(tmp_out->dst.trace_path,
4004 S_IRWXU | S_IRWXG, session->uid, session->gid);
4005 if (ret < 0) {
4006 if (ret != -EEXIST) {
4007 ERR("Trace directory creation error");
4008 ret = LTTCOMM_FATAL;
4009 goto error;
4010 }
4011 }
4012 break;
4013 case CONSUMER_DST_NET:
4014 DBG2("Consumer output is network. Validating URIs");
4015 /* Validate if we have both control and data path set. */
4016 if (!tmp_out->dst.net.control_isset) {
4017 ret = LTTCOMM_URI_CTRL_MISS;
4018 goto error;
4019 }
4020
4021 if (!tmp_out->dst.net.data_isset) {
4022 ret = LTTCOMM_URI_DATA_MISS;
4023 goto error;
4024 }
4025
4026 /* Check established network session state */
4027 if (session->net_handle == 0) {
4028 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4029 DBG2("Session network handle is not set on enable-consumer");
4030 goto error;
4031 }
4032
4033 if (tmp_out->net_seq_index == -1) {
4034 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4035 DBG2("Network index is not set on the consumer");
4036 goto error;
4037 }
4038
4039 /* Append default kernel trace dir to subdir */
4040 strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
4041 sizeof(usess->consumer->subdir));
4042
4043 break;
4044 }
4045
4046 /*
4047 * @session-lock
4048 * This is race free for now since the session lock is acquired before
4049 * ending up in this function. No other threads can access this kernel
4050 * session without this lock hence freeing the consumer output object
4051 * is valid.
4052 */
4053 consumer_destroy_output(usess->consumer);
4054 usess->consumer = tmp_out;
4055 usess->tmp_consumer = NULL;
4056
4057 break;
4058 }
4059
4060 /* Success at this point */
4061 ret = LTTCOMM_OK;
4062
4063 error:
4064 return ret;
4065 }
4066
4067 /*
4068 * Process the command requested by the lttng client within the command
4069 * context structure. This function make sure that the return structure (llm)
4070 * is set and ready for transmission before returning.
4071 *
4072 * Return any error encountered or 0 for success.
4073 *
4074 * "sock" is only used for special-case var. len data.
4075 */
4076 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
4077 int *sock_error)
4078 {
4079 int ret = LTTCOMM_OK;
4080 int need_tracing_session = 1;
4081 int need_domain;
4082
4083 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
4084
4085 *sock_error = 0;
4086
4087 switch (cmd_ctx->lsm->cmd_type) {
4088 case LTTNG_CREATE_SESSION:
4089 case LTTNG_CREATE_SESSION_URI:
4090 case LTTNG_DESTROY_SESSION:
4091 case LTTNG_LIST_SESSIONS:
4092 case LTTNG_LIST_DOMAINS:
4093 case LTTNG_START_TRACE:
4094 case LTTNG_STOP_TRACE:
4095 need_domain = 0;
4096 break;
4097 default:
4098 need_domain = 1;
4099 }
4100
4101 if (opt_no_kernel && need_domain
4102 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
4103 if (!is_root) {
4104 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4105 } else {
4106 ret = LTTCOMM_KERN_NA;
4107 }
4108 goto error;
4109 }
4110
4111 /*
4112 * Check for command that don't needs to allocate a returned payload. We do
4113 * this here so we don't have to make the call for no payload at each
4114 * command.
4115 */
4116 switch(cmd_ctx->lsm->cmd_type) {
4117 case LTTNG_LIST_SESSIONS:
4118 case LTTNG_LIST_TRACEPOINTS:
4119 case LTTNG_LIST_TRACEPOINT_FIELDS:
4120 case LTTNG_LIST_DOMAINS:
4121 case LTTNG_LIST_CHANNELS:
4122 case LTTNG_LIST_EVENTS:
4123 break;
4124 default:
4125 /* Setup lttng message with no payload */
4126 ret = setup_lttng_msg(cmd_ctx, 0);
4127 if (ret < 0) {
4128 /* This label does not try to unlock the session */
4129 goto init_setup_error;
4130 }
4131 }
4132
4133 /* Commands that DO NOT need a session. */
4134 switch (cmd_ctx->lsm->cmd_type) {
4135 case LTTNG_CREATE_SESSION:
4136 case LTTNG_CREATE_SESSION_URI:
4137 case LTTNG_CALIBRATE:
4138 case LTTNG_LIST_SESSIONS:
4139 case LTTNG_LIST_TRACEPOINTS:
4140 case LTTNG_LIST_TRACEPOINT_FIELDS:
4141 need_tracing_session = 0;
4142 break;
4143 default:
4144 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
4145 /*
4146 * We keep the session list lock across _all_ commands
4147 * for now, because the per-session lock does not
4148 * handle teardown properly.
4149 */
4150 session_lock_list();
4151 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
4152 if (cmd_ctx->session == NULL) {
4153 if (cmd_ctx->lsm->session.name != NULL) {
4154 ret = LTTCOMM_SESS_NOT_FOUND;
4155 } else {
4156 /* If no session name specified */
4157 ret = LTTCOMM_SELECT_SESS;
4158 }
4159 goto error;
4160 } else {
4161 /* Acquire lock for the session */
4162 session_lock(cmd_ctx->session);
4163 }
4164 break;
4165 }
4166
4167 if (!need_domain) {
4168 goto skip_domain;
4169 }
4170 /*
4171 * Check domain type for specific "pre-action".
4172 */
4173 switch (cmd_ctx->lsm->domain.type) {
4174 case LTTNG_DOMAIN_KERNEL:
4175 if (!is_root) {
4176 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4177 goto error;
4178 }
4179
4180 /* Kernel tracer check */
4181 if (kernel_tracer_fd == -1) {
4182 /* Basically, load kernel tracer modules */
4183 ret = init_kernel_tracer();
4184 if (ret != 0) {
4185 goto error;
4186 }
4187 }
4188
4189 /* Consumer is in an ERROR state. Report back to client */
4190 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
4191 ret = LTTCOMM_NO_KERNCONSUMERD;
4192 goto error;
4193 }
4194
4195 /* Need a session for kernel command */
4196 if (need_tracing_session) {
4197 if (cmd_ctx->session->kernel_session == NULL) {
4198 ret = create_kernel_session(cmd_ctx->session);
4199 if (ret < 0) {
4200 ret = LTTCOMM_KERN_SESS_FAIL;
4201 goto error;
4202 }
4203 }
4204
4205 /* Start the kernel consumer daemon */
4206 pthread_mutex_lock(&kconsumer_data.pid_mutex);
4207 if (kconsumer_data.pid == 0 &&
4208 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4209 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4210 ret = start_consumerd(&kconsumer_data);
4211 if (ret < 0) {
4212 ret = LTTCOMM_KERN_CONSUMER_FAIL;
4213 goto error;
4214 }
4215 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
4216
4217 /* Set consumer fd of the session */
4218 cmd_ctx->session->kernel_session->consumer_fd =
4219 kconsumer_data.cmd_sock;
4220 } else {
4221 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4222 }
4223 }
4224
4225 break;
4226 case LTTNG_DOMAIN_UST:
4227 {
4228 /* Consumer is in an ERROR state. Report back to client */
4229 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
4230 ret = LTTCOMM_NO_USTCONSUMERD;
4231 goto error;
4232 }
4233
4234 if (need_tracing_session) {
4235 if (cmd_ctx->session->ust_session == NULL) {
4236 ret = create_ust_session(cmd_ctx->session,
4237 &cmd_ctx->lsm->domain);
4238 if (ret != LTTCOMM_OK) {
4239 goto error;
4240 }
4241 }
4242
4243 /* Start the UST consumer daemons */
4244 /* 64-bit */
4245 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
4246 if (consumerd64_bin[0] != '\0' &&
4247 ustconsumer64_data.pid == 0 &&
4248 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4249 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4250 ret = start_consumerd(&ustconsumer64_data);
4251 if (ret < 0) {
4252 ret = LTTCOMM_UST_CONSUMER64_FAIL;
4253 ust_consumerd64_fd = -EINVAL;
4254 goto error;
4255 }
4256
4257 ust_consumerd64_fd = ustconsumer64_data.cmd_sock;
4258 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
4259 } else {
4260 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4261 }
4262 /* 32-bit */
4263 if (consumerd32_bin[0] != '\0' &&
4264 ustconsumer32_data.pid == 0 &&
4265 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4266 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4267 ret = start_consumerd(&ustconsumer32_data);
4268 if (ret < 0) {
4269 ret = LTTCOMM_UST_CONSUMER32_FAIL;
4270 ust_consumerd32_fd = -EINVAL;
4271 goto error;
4272 }
4273
4274 ust_consumerd32_fd = ustconsumer32_data.cmd_sock;
4275 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
4276 } else {
4277 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4278 }
4279 }
4280 break;
4281 }
4282 default:
4283 break;
4284 }
4285 skip_domain:
4286
4287 /* Validate consumer daemon state when start/stop trace command */
4288 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
4289 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
4290 switch (cmd_ctx->lsm->domain.type) {
4291 case LTTNG_DOMAIN_UST:
4292 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
4293 ret = LTTCOMM_NO_USTCONSUMERD;
4294 goto error;
4295 }
4296 break;
4297 case LTTNG_DOMAIN_KERNEL:
4298 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
4299 ret = LTTCOMM_NO_KERNCONSUMERD;
4300 goto error;
4301 }
4302 break;
4303 }
4304 }
4305
4306 /*
4307 * Check that the UID or GID match that of the tracing session.
4308 * The root user can interact with all sessions.
4309 */
4310 if (need_tracing_session) {
4311 if (!session_access_ok(cmd_ctx->session,
4312 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4313 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
4314 ret = LTTCOMM_EPERM;
4315 goto error;
4316 }
4317 }
4318
4319 /* Process by command type */
4320 switch (cmd_ctx->lsm->cmd_type) {
4321 case LTTNG_ADD_CONTEXT:
4322 {
4323 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4324 cmd_ctx->lsm->u.context.channel_name,
4325 cmd_ctx->lsm->u.context.event_name,
4326 &cmd_ctx->lsm->u.context.ctx);
4327 break;
4328 }
4329 case LTTNG_DISABLE_CHANNEL:
4330 {
4331 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4332 cmd_ctx->lsm->u.disable.channel_name);
4333 break;
4334 }
4335 case LTTNG_DISABLE_EVENT:
4336 {
4337 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4338 cmd_ctx->lsm->u.disable.channel_name,
4339 cmd_ctx->lsm->u.disable.name);
4340 break;
4341 }
4342 case LTTNG_DISABLE_ALL_EVENT:
4343 {
4344 DBG("Disabling all events");
4345
4346 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4347 cmd_ctx->lsm->u.disable.channel_name);
4348 break;
4349 }
4350 case LTTNG_DISABLE_CONSUMER:
4351 {
4352 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4353 break;
4354 }
4355 case LTTNG_ENABLE_CHANNEL:
4356 {
4357 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4358 &cmd_ctx->lsm->u.channel.chan);
4359 break;
4360 }
4361 case LTTNG_ENABLE_CONSUMER:
4362 {
4363 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4364 break;
4365 }
4366 case LTTNG_ENABLE_EVENT:
4367 {
4368 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4369 cmd_ctx->lsm->u.enable.channel_name,
4370 &cmd_ctx->lsm->u.enable.event);
4371 break;
4372 }
4373 case LTTNG_ENABLE_ALL_EVENT:
4374 {
4375 DBG("Enabling all events");
4376
4377 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4378 cmd_ctx->lsm->u.enable.channel_name,
4379 cmd_ctx->lsm->u.enable.event.type);
4380 break;
4381 }
4382 case LTTNG_LIST_TRACEPOINTS:
4383 {
4384 struct lttng_event *events;
4385 ssize_t nb_events;
4386
4387 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
4388 if (nb_events < 0) {
4389 ret = -nb_events;
4390 goto error;
4391 }
4392
4393 /*
4394 * Setup lttng message with payload size set to the event list size in
4395 * bytes and then copy list into the llm payload.
4396 */
4397 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
4398 if (ret < 0) {
4399 free(events);
4400 goto setup_error;
4401 }
4402
4403 /* Copy event list into message payload */
4404 memcpy(cmd_ctx->llm->payload, events,
4405 sizeof(struct lttng_event) * nb_events);
4406
4407 free(events);
4408
4409 ret = LTTCOMM_OK;
4410 break;
4411 }
4412 case LTTNG_LIST_TRACEPOINT_FIELDS:
4413 {
4414 struct lttng_event_field *fields;
4415 ssize_t nb_fields;
4416
4417 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, &fields);
4418 if (nb_fields < 0) {
4419 ret = -nb_fields;
4420 goto error;
4421 }
4422
4423 /*
4424 * Setup lttng message with payload size set to the event list size in
4425 * bytes and then copy list into the llm payload.
4426 */
4427 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event_field) * nb_fields);
4428 if (ret < 0) {
4429 free(fields);
4430 goto setup_error;
4431 }
4432
4433 /* Copy event list into message payload */
4434 memcpy(cmd_ctx->llm->payload, fields,
4435 sizeof(struct lttng_event_field) * nb_fields);
4436
4437 free(fields);
4438
4439 ret = LTTCOMM_OK;
4440 break;
4441 }
4442 case LTTNG_SET_CONSUMER_URI:
4443 {
4444 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
4445 &cmd_ctx->lsm->u.uri);
4446 break;
4447 }
4448 case LTTNG_START_TRACE:
4449 {
4450 ret = cmd_start_trace(cmd_ctx->session);
4451 break;
4452 }
4453 case LTTNG_STOP_TRACE:
4454 {
4455 ret = cmd_stop_trace(cmd_ctx->session);
4456 break;
4457 }
4458 case LTTNG_CREATE_SESSION:
4459 {
4460 ret = cmd_create_session(cmd_ctx->lsm->session.name,
4461 cmd_ctx->lsm->session.path, &cmd_ctx->creds);
4462 break;
4463 }
4464 case LTTNG_CREATE_SESSION_URI:
4465 {
4466 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name,
4467 &cmd_ctx->lsm->u.create_uri.ctrl_uri,
4468 &cmd_ctx->lsm->u.create_uri.data_uri,
4469 cmd_ctx->lsm->u.create_uri.enable_consumer, &cmd_ctx->creds);
4470 break;
4471 }
4472 case LTTNG_DESTROY_SESSION:
4473 {
4474 ret = cmd_destroy_session(cmd_ctx->session,
4475 cmd_ctx->lsm->session.name);
4476 /*
4477 * Set session to NULL so we do not unlock it after
4478 * free.
4479 */
4480 cmd_ctx->session = NULL;
4481 break;
4482 }
4483 case LTTNG_LIST_DOMAINS:
4484 {
4485 ssize_t nb_dom;
4486 struct lttng_domain *domains;
4487
4488 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
4489 if (nb_dom < 0) {
4490 ret = -nb_dom;
4491 goto error;
4492 }
4493
4494 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
4495 if (ret < 0) {
4496 goto setup_error;
4497 }
4498
4499 /* Copy event list into message payload */
4500 memcpy(cmd_ctx->llm->payload, domains,
4501 nb_dom * sizeof(struct lttng_domain));
4502
4503 free(domains);
4504
4505 ret = LTTCOMM_OK;
4506 break;
4507 }
4508 case LTTNG_LIST_CHANNELS:
4509 {
4510 int nb_chan;
4511 struct lttng_channel *channels;
4512
4513 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
4514 cmd_ctx->session, &channels);
4515 if (nb_chan < 0) {
4516 ret = -nb_chan;
4517 goto error;
4518 }
4519
4520 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
4521 if (ret < 0) {
4522 goto setup_error;
4523 }
4524
4525 /* Copy event list into message payload */
4526 memcpy(cmd_ctx->llm->payload, channels,
4527 nb_chan * sizeof(struct lttng_channel));
4528
4529 free(channels);
4530
4531 ret = LTTCOMM_OK;
4532 break;
4533 }
4534 case LTTNG_LIST_EVENTS:
4535 {
4536 ssize_t nb_event;
4537 struct lttng_event *events = NULL;
4538
4539 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
4540 cmd_ctx->lsm->u.list.channel_name, &events);
4541 if (nb_event < 0) {
4542 ret = -nb_event;
4543 goto error;
4544 }
4545
4546 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
4547 if (ret < 0) {
4548 goto setup_error;
4549 }
4550
4551 /* Copy event list into message payload */
4552 memcpy(cmd_ctx->llm->payload, events,
4553 nb_event * sizeof(struct lttng_event));
4554
4555 free(events);
4556
4557 ret = LTTCOMM_OK;
4558 break;
4559 }
4560 case LTTNG_LIST_SESSIONS:
4561 {
4562 unsigned int nr_sessions;
4563
4564 session_lock_list();
4565 nr_sessions = lttng_sessions_count(
4566 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4567 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
4568
4569 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
4570 if (ret < 0) {
4571 session_unlock_list();
4572 goto setup_error;
4573 }
4574
4575 /* Filled the session array */
4576 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
4577 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4578 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
4579
4580 session_unlock_list();
4581
4582 ret = LTTCOMM_OK;
4583 break;
4584 }
4585 case LTTNG_CALIBRATE:
4586 {
4587 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
4588 &cmd_ctx->lsm->u.calibrate);
4589 break;
4590 }
4591 case LTTNG_REGISTER_CONSUMER:
4592 {
4593 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4594 cmd_ctx->lsm->u.reg.path);
4595 break;
4596 }
4597 case LTTNG_SET_FILTER:
4598 {
4599 struct lttng_filter_bytecode *bytecode;
4600
4601 if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) {
4602 ret = LTTCOMM_FILTER_INVAL;
4603 goto error;
4604 }
4605 bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len);
4606 if (!bytecode) {
4607 ret = LTTCOMM_FILTER_NOMEM;
4608 goto error;
4609 }
4610 /* Receive var. len. data */
4611 DBG("Receiving var len data from client ...");
4612 ret = lttcomm_recv_unix_sock(sock, bytecode,
4613 cmd_ctx->lsm->u.filter.bytecode_len);
4614 if (ret <= 0) {
4615 DBG("Nothing recv() from client var len data... continuing");
4616 *sock_error = 1;
4617 ret = LTTCOMM_FILTER_INVAL;
4618 goto error;
4619 }
4620
4621 if (bytecode->len + sizeof(*bytecode)
4622 != cmd_ctx->lsm->u.filter.bytecode_len) {
4623 free(bytecode);
4624 ret = LTTCOMM_FILTER_INVAL;
4625 goto error;
4626 }
4627
4628 ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4629 cmd_ctx->lsm->u.filter.channel_name,
4630 cmd_ctx->lsm->u.filter.event_name,
4631 bytecode);
4632 break;
4633 }
4634 default:
4635 ret = LTTCOMM_UND;
4636 break;
4637 }
4638
4639 error:
4640 if (cmd_ctx->llm == NULL) {
4641 DBG("Missing llm structure. Allocating one.");
4642 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
4643 goto setup_error;
4644 }
4645 }
4646 /* Set return code */
4647 cmd_ctx->llm->ret_code = ret;
4648 setup_error:
4649 if (cmd_ctx->session) {
4650 session_unlock(cmd_ctx->session);
4651 }
4652 if (need_tracing_session) {
4653 session_unlock_list();
4654 }
4655 init_setup_error:
4656 return ret;
4657 }
4658
4659 /*
4660 * Thread managing health check socket.
4661 */
4662 static void *thread_manage_health(void *data)
4663 {
4664 int sock = -1, new_sock, ret, i, pollfd, err = -1;
4665 uint32_t revents, nb_fd;
4666 struct lttng_poll_event events;
4667 struct lttcomm_health_msg msg;
4668 struct lttcomm_health_data reply;
4669
4670 DBG("[thread] Manage health check started");
4671
4672 rcu_register_thread();
4673
4674 /* Create unix socket */
4675 sock = lttcomm_create_unix_sock(health_unix_sock_path);
4676 if (sock < 0) {
4677 ERR("Unable to create health check Unix socket");
4678 ret = -1;
4679 goto error;
4680 }
4681
4682 ret = lttcomm_listen_unix_sock(sock);
4683 if (ret < 0) {
4684 goto error;
4685 }
4686
4687 /*
4688 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4689 * more will be added to this poll set.
4690 */
4691 ret = create_thread_poll_set(&events, 2);
4692 if (ret < 0) {
4693 goto error;
4694 }
4695
4696 /* Add the application registration socket */
4697 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
4698 if (ret < 0) {
4699 goto error;
4700 }
4701
4702 while (1) {
4703 DBG("Health check ready");
4704
4705 nb_fd = LTTNG_POLL_GETNB(&events);
4706
4707 /* Inifinite blocking call, waiting for transmission */
4708 restart:
4709 ret = lttng_poll_wait(&events, -1);
4710 if (ret < 0) {
4711 /*
4712 * Restart interrupted system call.
4713 */
4714 if (errno == EINTR) {
4715 goto restart;
4716 }
4717 goto error;
4718 }
4719
4720 for (i = 0; i < nb_fd; i++) {
4721 /* Fetch once the poll data */
4722 revents = LTTNG_POLL_GETEV(&events, i);
4723 pollfd = LTTNG_POLL_GETFD(&events, i);
4724
4725 /* Thread quit pipe has been closed. Killing thread. */
4726 ret = check_thread_quit_pipe(pollfd, revents);
4727 if (ret) {
4728 err = 0;
4729 goto exit;
4730 }
4731
4732 /* Event on the registration socket */
4733 if (pollfd == sock) {
4734 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4735 ERR("Health socket poll error");
4736 goto error;
4737 }
4738 }
4739 }
4740
4741 new_sock = lttcomm_accept_unix_sock(sock);
4742 if (new_sock < 0) {
4743 goto error;
4744 }
4745
4746 DBG("Receiving data from client for health...");
4747 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4748 if (ret <= 0) {
4749 DBG("Nothing recv() from client... continuing");
4750 ret = close(new_sock);
4751 if (ret) {
4752 PERROR("close");
4753 }
4754 new_sock = -1;
4755 continue;
4756 }
4757
4758 rcu_thread_online();
4759
4760 switch (msg.component) {
4761 case LTTNG_HEALTH_CMD:
4762 reply.ret_code = health_check_state(&health_thread_cmd);
4763 break;
4764 case LTTNG_HEALTH_APP_MANAGE:
4765 reply.ret_code = health_check_state(&health_thread_app_manage);
4766 break;
4767 case LTTNG_HEALTH_APP_REG:
4768 reply.ret_code = health_check_state(&health_thread_app_reg);
4769 break;
4770 case LTTNG_HEALTH_KERNEL:
4771 reply.ret_code = health_check_state(&health_thread_kernel);
4772 break;
4773 case LTTNG_HEALTH_CONSUMER:
4774 reply.ret_code = check_consumer_health();
4775 break;
4776 case LTTNG_HEALTH_ALL:
4777 reply.ret_code =
4778 health_check_state(&health_thread_app_manage) &&
4779 health_check_state(&health_thread_app_reg) &&
4780 health_check_state(&health_thread_cmd) &&
4781 health_check_state(&health_thread_kernel) &&
4782 check_consumer_health();
4783 break;
4784 default:
4785 reply.ret_code = LTTCOMM_UND;
4786 break;
4787 }
4788
4789 /*
4790 * Flip ret value since 0 is a success and 1 indicates a bad health for
4791 * the client where in the sessiond it is the opposite. Again, this is
4792 * just to make things easier for us poor developer which enjoy a lot
4793 * lazyness.
4794 */
4795 if (reply.ret_code == 0 || reply.ret_code == 1) {
4796 reply.ret_code = !reply.ret_code;
4797 }
4798
4799 DBG2("Health check return value %d", reply.ret_code);
4800
4801 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4802 if (ret < 0) {
4803 ERR("Failed to send health data back to client");
4804 }
4805
4806 /* End of transmission */
4807 ret = close(new_sock);
4808 if (ret) {
4809 PERROR("close");
4810 }
4811 new_sock = -1;
4812 }
4813
4814 exit:
4815 error:
4816 if (err) {
4817 ERR("Health error occurred in %s", __func__);
4818 }
4819 DBG("Health check thread dying");
4820 unlink(health_unix_sock_path);
4821 if (sock >= 0) {
4822 ret = close(sock);
4823 if (ret) {
4824 PERROR("close");
4825 }
4826 }
4827 if (new_sock >= 0) {
4828 ret = close(new_sock);
4829 if (ret) {
4830 PERROR("close");
4831 }
4832 }
4833
4834 lttng_poll_clean(&events);
4835
4836 rcu_unregister_thread();
4837 return NULL;
4838 }
4839
4840 /*
4841 * This thread manage all clients request using the unix client socket for
4842 * communication.
4843 */
4844 static void *thread_manage_clients(void *data)
4845 {
4846 int sock = -1, ret, i, pollfd, err = -1;
4847 int sock_error;
4848 uint32_t revents, nb_fd;
4849 struct command_ctx *cmd_ctx = NULL;
4850 struct lttng_poll_event events;
4851
4852 DBG("[thread] Manage client started");
4853
4854 rcu_register_thread();
4855
4856 health_code_update(&health_thread_cmd);
4857
4858 ret = lttcomm_listen_unix_sock(client_sock);
4859 if (ret < 0) {
4860 goto error;
4861 }
4862
4863 /*
4864 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4865 * more will be added to this poll set.
4866 */
4867 ret = create_thread_poll_set(&events, 2);
4868 if (ret < 0) {
4869 goto error;
4870 }
4871
4872 /* Add the application registration socket */
4873 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4874 if (ret < 0) {
4875 goto error;
4876 }
4877
4878 /*
4879 * Notify parent pid that we are ready to accept command for client side.
4880 */
4881 if (opt_sig_parent) {
4882 kill(ppid, SIGUSR1);
4883 }
4884
4885 health_code_update(&health_thread_cmd);
4886
4887 while (1) {
4888 DBG("Accepting client command ...");
4889
4890 nb_fd = LTTNG_POLL_GETNB(&events);
4891
4892 /* Inifinite blocking call, waiting for transmission */
4893 restart:
4894 health_poll_update(&health_thread_cmd);
4895 ret = lttng_poll_wait(&events, -1);
4896 health_poll_update(&health_thread_cmd);
4897 if (ret < 0) {
4898 /*
4899 * Restart interrupted system call.
4900 */
4901 if (errno == EINTR) {
4902 goto restart;
4903 }
4904 goto error;
4905 }
4906
4907 for (i = 0; i < nb_fd; i++) {
4908 /* Fetch once the poll data */
4909 revents = LTTNG_POLL_GETEV(&events, i);
4910 pollfd = LTTNG_POLL_GETFD(&events, i);
4911
4912 health_code_update(&health_thread_cmd);
4913
4914 /* Thread quit pipe has been closed. Killing thread. */
4915 ret = check_thread_quit_pipe(pollfd, revents);
4916 if (ret) {
4917 err = 0;
4918 goto exit;
4919 }
4920
4921 /* Event on the registration socket */
4922 if (pollfd == client_sock) {
4923 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4924 ERR("Client socket poll error");
4925 goto error;
4926 }
4927 }
4928 }
4929
4930 DBG("Wait for client response");
4931
4932 health_code_update(&health_thread_cmd);
4933
4934 sock = lttcomm_accept_unix_sock(client_sock);
4935 if (sock < 0) {
4936 goto error;
4937 }
4938
4939 /* Set socket option for credentials retrieval */
4940 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4941 if (ret < 0) {
4942 goto error;
4943 }
4944
4945 /* Allocate context command to process the client request */
4946 cmd_ctx = zmalloc(sizeof(struct command_ctx));
4947 if (cmd_ctx == NULL) {
4948 PERROR("zmalloc cmd_ctx");
4949 goto error;
4950 }
4951
4952 /* Allocate data buffer for reception */
4953 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
4954 if (cmd_ctx->lsm == NULL) {
4955 PERROR("zmalloc cmd_ctx->lsm");
4956 goto error;
4957 }
4958
4959 cmd_ctx->llm = NULL;
4960 cmd_ctx->session = NULL;
4961
4962 health_code_update(&health_thread_cmd);
4963
4964 /*
4965 * Data is received from the lttng client. The struct
4966 * lttcomm_session_msg (lsm) contains the command and data request of
4967 * the client.
4968 */
4969 DBG("Receiving data from client ...");
4970 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4971 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
4972 if (ret <= 0) {
4973 DBG("Nothing recv() from client... continuing");
4974 ret = close(sock);
4975 if (ret) {
4976 PERROR("close");
4977 }
4978 sock = -1;
4979 clean_command_ctx(&cmd_ctx);
4980 continue;
4981 }
4982
4983 health_code_update(&health_thread_cmd);
4984
4985 // TODO: Validate cmd_ctx including sanity check for
4986 // security purpose.
4987
4988 rcu_thread_online();
4989 /*
4990 * This function dispatch the work to the kernel or userspace tracer
4991 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4992 * informations for the client. The command context struct contains
4993 * everything this function may needs.
4994 */
4995 ret = process_client_msg(cmd_ctx, sock, &sock_error);
4996 rcu_thread_offline();
4997 if (ret < 0) {
4998 if (sock_error) {
4999 ret = close(sock);
5000 if (ret) {
5001 PERROR("close");
5002 }
5003 sock = -1;
5004 }
5005 /*
5006 * TODO: Inform client somehow of the fatal error. At
5007 * this point, ret < 0 means that a zmalloc failed
5008 * (ENOMEM). Error detected but still accept
5009 * command, unless a socket error has been
5010 * detected.
5011 */
5012 clean_command_ctx(&cmd_ctx);
5013 continue;
5014 }
5015
5016 health_code_update(&health_thread_cmd);
5017
5018 DBG("Sending response (size: %d, retcode: %s)",
5019 cmd_ctx->lttng_msg_size,
5020 lttng_strerror(-cmd_ctx->llm->ret_code));
5021 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5022 if (ret < 0) {
5023 ERR("Failed to send data back to client");
5024 }
5025
5026 /* End of transmission */
5027 ret = close(sock);
5028 if (ret) {
5029 PERROR("close");
5030 }
5031 sock = -1;
5032
5033 clean_command_ctx(&cmd_ctx);
5034
5035 health_code_update(&health_thread_cmd);
5036 }
5037
5038 exit:
5039 error:
5040 if (err) {
5041 health_error(&health_thread_cmd);
5042 ERR("Health error occurred in %s", __func__);
5043 }
5044 health_exit(&health_thread_cmd);
5045
5046 DBG("Client thread dying");
5047 unlink(client_unix_sock_path);
5048 if (client_sock >= 0) {
5049 ret = close(client_sock);
5050 if (ret) {
5051 PERROR("close");
5052 }
5053 }
5054 if (sock >= 0) {
5055 ret = close(sock);
5056 if (ret) {
5057 PERROR("close");
5058 }
5059 }
5060
5061 lttng_poll_clean(&events);
5062 clean_command_ctx(&cmd_ctx);
5063
5064 rcu_unregister_thread();
5065 return NULL;
5066 }
5067
5068
5069 /*
5070 * usage function on stderr
5071 */
5072 static void usage(void)
5073 {
5074 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
5075 fprintf(stderr, " -h, --help Display this usage.\n");
5076 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
5077 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5078 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5079 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
5080 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5081 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5082 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5083 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
5084 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5085 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5086 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5087 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
5088 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
5089 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5090 fprintf(stderr, " -V, --version Show version number.\n");
5091 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5092 fprintf(stderr, " -q, --quiet No output at all.\n");
5093 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
5094 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
5095 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
5096 }
5097
5098 /*
5099 * daemon argument parsing
5100 */
5101 static int parse_args(int argc, char **argv)
5102 {
5103 int c;
5104
5105 static struct option long_options[] = {
5106 { "client-sock", 1, 0, 'c' },
5107 { "apps-sock", 1, 0, 'a' },
5108 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5109 { "kconsumerd-err-sock", 1, 0, 'E' },
5110 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5111 { "ustconsumerd32-err-sock", 1, 0, 'H' },
5112 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5113 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5114 { "consumerd32-path", 1, 0, 'u' },
5115 { "consumerd32-libdir", 1, 0, 'U' },
5116 { "consumerd64-path", 1, 0, 't' },
5117 { "consumerd64-libdir", 1, 0, 'T' },
5118 { "daemonize", 0, 0, 'd' },
5119 { "sig-parent", 0, 0, 'S' },
5120 { "help", 0, 0, 'h' },
5121 { "group", 1, 0, 'g' },
5122 { "version", 0, 0, 'V' },
5123 { "quiet", 0, 0, 'q' },
5124 { "verbose", 0, 0, 'v' },
5125 { "verbose-consumer", 0, 0, 'Z' },
5126 { "no-kernel", 0, 0, 'N' },
5127 { NULL, 0, 0, 0 }
5128 };
5129
5130 while (1) {
5131 int option_index = 0;
5132 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
5133 long_options, &option_index);
5134 if (c == -1) {
5135 break;
5136 }
5137
5138 switch (c) {
5139 case 0:
5140 fprintf(stderr, "option %s", long_options[option_index].name);
5141 if (optarg) {
5142 fprintf(stderr, " with arg %s\n", optarg);
5143 }
5144 break;
5145 case 'c':
5146 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
5147 break;
5148 case 'a':
5149 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
5150 break;
5151 case 'd':
5152 opt_daemon = 1;
5153 break;
5154 case 'g':
5155 opt_tracing_group = optarg;
5156 break;
5157 case 'h':
5158 usage();
5159 exit(EXIT_FAILURE);
5160 case 'V':
5161 fprintf(stdout, "%s\n", VERSION);
5162 exit(EXIT_SUCCESS);
5163 case 'S':
5164 opt_sig_parent = 1;
5165 break;
5166 case 'E':
5167 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5168 break;
5169 case 'C':
5170 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5171 break;
5172 case 'F':
5173 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5174 break;
5175 case 'D':
5176 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5177 break;
5178 case 'H':
5179 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5180 break;
5181 case 'G':
5182 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5183 break;
5184 case 'N':
5185 opt_no_kernel = 1;
5186 break;
5187 case 'q':
5188 lttng_opt_quiet = 1;
5189 break;
5190 case 'v':
5191 /* Verbose level can increase using multiple -v */
5192 lttng_opt_verbose += 1;
5193 break;
5194 case 'Z':
5195 opt_verbose_consumer += 1;
5196 break;
5197 case 'u':
5198 consumerd32_bin= optarg;
5199 break;
5200 case 'U':
5201 consumerd32_libdir = optarg;
5202 break;
5203 case 't':
5204 consumerd64_bin = optarg;
5205 break;
5206 case 'T':
5207 consumerd64_libdir = optarg;
5208 break;
5209 default:
5210 /* Unknown option or other error.
5211 * Error is printed by getopt, just return */
5212 return -1;
5213 }
5214 }
5215
5216 return 0;
5217 }
5218
5219 /*
5220 * Creates the two needed socket by the daemon.
5221 * apps_sock - The communication socket for all UST apps.
5222 * client_sock - The communication of the cli tool (lttng).
5223 */
5224 static int init_daemon_socket(void)
5225 {
5226 int ret = 0;
5227 mode_t old_umask;
5228
5229 old_umask = umask(0);
5230
5231 /* Create client tool unix socket */
5232 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5233 if (client_sock < 0) {
5234 ERR("Create unix sock failed: %s", client_unix_sock_path);
5235 ret = -1;
5236 goto end;
5237 }
5238
5239 /* File permission MUST be 660 */
5240 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5241 if (ret < 0) {
5242 ERR("Set file permissions failed: %s", client_unix_sock_path);
5243 PERROR("chmod");
5244 goto end;
5245 }
5246
5247 /* Create the application unix socket */
5248 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5249 if (apps_sock < 0) {
5250 ERR("Create unix sock failed: %s", apps_unix_sock_path);
5251 ret = -1;
5252 goto end;
5253 }
5254
5255 /* File permission MUST be 666 */
5256 ret = chmod(apps_unix_sock_path,
5257 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
5258 if (ret < 0) {
5259 ERR("Set file permissions failed: %s", apps_unix_sock_path);
5260 PERROR("chmod");
5261 goto end;
5262 }
5263
5264 end:
5265 umask(old_umask);
5266 return ret;
5267 }
5268
5269 /*
5270 * Check if the global socket is available, and if a daemon is answering at the
5271 * other side. If yes, error is returned.
5272 */
5273 static int check_existing_daemon(void)
5274 {
5275 /* Is there anybody out there ? */
5276 if (lttng_session_daemon_alive()) {
5277 return -EEXIST;
5278 }
5279
5280 return 0;
5281 }
5282
5283 /*
5284 * Set the tracing group gid onto the client socket.
5285 *
5286 * Race window between mkdir and chown is OK because we are going from more
5287 * permissive (root.root) to less permissive (root.tracing).
5288 */
5289 static int set_permissions(char *rundir)
5290 {
5291 int ret;
5292 gid_t gid;
5293
5294 ret = allowed_group();
5295 if (ret < 0) {
5296 WARN("No tracing group detected");
5297 ret = 0;
5298 goto end;
5299 }
5300
5301 gid = ret;
5302
5303 /* Set lttng run dir */
5304 ret = chown(rundir, 0, gid);
5305 if (ret < 0) {
5306 ERR("Unable to set group on %s", rundir);
5307 PERROR("chown");
5308 }
5309
5310 /* Ensure tracing group can search the run dir */
5311 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
5312 if (ret < 0) {
5313 ERR("Unable to set permissions on %s", rundir);
5314 PERROR("chmod");
5315 }
5316
5317 /* lttng client socket path */
5318 ret = chown(client_unix_sock_path, 0, gid);
5319 if (ret < 0) {
5320 ERR("Unable to set group on %s", client_unix_sock_path);
5321 PERROR("chown");
5322 }
5323
5324 /* kconsumer error socket path */
5325 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
5326 if (ret < 0) {
5327 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
5328 PERROR("chown");
5329 }
5330
5331 /* 64-bit ustconsumer error socket path */
5332 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
5333 if (ret < 0) {
5334 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
5335 PERROR("chown");
5336 }
5337
5338 /* 32-bit ustconsumer compat32 error socket path */
5339 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
5340 if (ret < 0) {
5341 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
5342 PERROR("chown");
5343 }
5344
5345 DBG("All permissions are set");
5346
5347 end:
5348 return ret;
5349 }
5350
5351 /*
5352 * Create the lttng run directory needed for all global sockets and pipe.
5353 */
5354 static int create_lttng_rundir(const char *rundir)
5355 {
5356 int ret;
5357
5358 DBG3("Creating LTTng run directory: %s", rundir);
5359
5360 ret = mkdir(rundir, S_IRWXU);
5361 if (ret < 0) {
5362 if (errno != EEXIST) {
5363 ERR("Unable to create %s", rundir);
5364 goto error;
5365 } else {
5366 ret = 0;
5367 }
5368 }
5369
5370 error:
5371 return ret;
5372 }
5373
5374 /*
5375 * Setup sockets and directory needed by the kconsumerd communication with the
5376 * session daemon.
5377 */
5378 static int set_consumer_sockets(struct consumer_data *consumer_data,
5379 const char *rundir)
5380 {
5381 int ret;
5382 char path[PATH_MAX];
5383
5384 switch (consumer_data->type) {
5385 case LTTNG_CONSUMER_KERNEL:
5386 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
5387 break;
5388 case LTTNG_CONSUMER64_UST:
5389 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
5390 break;
5391 case LTTNG_CONSUMER32_UST:
5392 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
5393 break;
5394 default:
5395 ERR("Consumer type unknown");
5396 ret = -EINVAL;
5397 goto error;
5398 }
5399
5400 DBG2("Creating consumer directory: %s", path);
5401
5402 ret = mkdir(path, S_IRWXU);
5403 if (ret < 0) {
5404 if (errno != EEXIST) {
5405 PERROR("mkdir");
5406 ERR("Failed to create %s", path);
5407 goto error;
5408 }
5409 ret = -1;
5410 }
5411
5412 /* Create the kconsumerd error unix socket */
5413 consumer_data->err_sock =
5414 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5415 if (consumer_data->err_sock < 0) {
5416 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
5417 ret = -1;
5418 goto error;
5419 }
5420
5421 /* File permission MUST be 660 */
5422 ret = chmod(consumer_data->err_unix_sock_path,
5423 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5424 if (ret < 0) {
5425 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
5426 PERROR("chmod");
5427 goto error;
5428 }
5429
5430 error:
5431 return ret;
5432 }
5433
5434 /*
5435 * Signal handler for the daemon
5436 *
5437 * Simply stop all worker threads, leaving main() return gracefully after
5438 * joining all threads and calling cleanup().
5439 */
5440 static void sighandler(int sig)
5441 {
5442 switch (sig) {
5443 case SIGPIPE:
5444 DBG("SIGPIPE caught");
5445 return;
5446 case SIGINT:
5447 DBG("SIGINT caught");
5448 stop_threads();
5449 break;
5450 case SIGTERM:
5451 DBG("SIGTERM caught");
5452 stop_threads();
5453 break;
5454 default:
5455 break;
5456 }
5457 }
5458
5459 /*
5460 * Setup signal handler for :
5461 * SIGINT, SIGTERM, SIGPIPE
5462 */
5463 static int set_signal_handler(void)
5464 {
5465 int ret = 0;
5466 struct sigaction sa;
5467 sigset_t sigset;
5468
5469 if ((ret = sigemptyset(&sigset)) < 0) {
5470 PERROR("sigemptyset");
5471 return ret;
5472 }
5473
5474 sa.sa_handler = sighandler;
5475 sa.sa_mask = sigset;
5476 sa.sa_flags = 0;
5477 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
5478 PERROR("sigaction");
5479 return ret;
5480 }
5481
5482 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
5483 PERROR("sigaction");
5484 return ret;
5485 }
5486
5487 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
5488 PERROR("sigaction");
5489 return ret;
5490 }
5491
5492 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5493
5494 return ret;
5495 }
5496
5497 /*
5498 * Set open files limit to unlimited. This daemon can open a large number of
5499 * file descriptors in order to consumer multiple kernel traces.
5500 */
5501 static void set_ulimit(void)
5502 {
5503 int ret;
5504 struct rlimit lim;
5505
5506 /* The kernel does not allowed an infinite limit for open files */
5507 lim.rlim_cur = 65535;
5508 lim.rlim_max = 65535;
5509
5510 ret = setrlimit(RLIMIT_NOFILE, &lim);
5511 if (ret < 0) {
5512 PERROR("failed to set open files limit");
5513 }
5514 }
5515
5516 /*
5517 * main
5518 */
5519 int main(int argc, char **argv)
5520 {
5521 int ret = 0;
5522 void *status;
5523 const char *home_path;
5524
5525 init_kernel_workarounds();
5526
5527 rcu_register_thread();
5528
5529 setup_consumerd_path();
5530
5531 /* Parse arguments */
5532 progname = argv[0];
5533 if ((ret = parse_args(argc, argv) < 0)) {
5534 goto error;
5535 }
5536
5537 /* Daemonize */
5538 if (opt_daemon) {
5539 int i;
5540
5541 /*
5542 * fork
5543 * child: setsid, close FD 0, 1, 2, chdir /
5544 * parent: exit (if fork is successful)
5545 */
5546 ret = daemon(0, 0);
5547 if (ret < 0) {
5548 PERROR("daemon");
5549 goto error;
5550 }
5551 /*
5552 * We are in the child. Make sure all other file
5553 * descriptors are closed, in case we are called with
5554 * more opened file descriptors than the standard ones.
5555 */
5556 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5557 (void) close(i);
5558 }
5559 }
5560
5561 /* Create thread quit pipe */
5562 if ((ret = init_thread_quit_pipe()) < 0) {
5563 goto error;
5564 }
5565
5566 /* Check if daemon is UID = 0 */
5567 is_root = !getuid();
5568
5569 if (is_root) {
5570 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
5571
5572 /* Create global run dir with root access */
5573 ret = create_lttng_rundir(rundir);
5574 if (ret < 0) {
5575 goto error;
5576 }
5577
5578 if (strlen(apps_unix_sock_path) == 0) {
5579 snprintf(apps_unix_sock_path, PATH_MAX,
5580 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
5581 }
5582
5583 if (strlen(client_unix_sock_path) == 0) {
5584 snprintf(client_unix_sock_path, PATH_MAX,
5585 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
5586 }
5587
5588 /* Set global SHM for ust */
5589 if (strlen(wait_shm_path) == 0) {
5590 snprintf(wait_shm_path, PATH_MAX,
5591 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
5592 }
5593
5594 if (strlen(health_unix_sock_path) == 0) {
5595 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5596 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
5597 }
5598
5599 /* Setup kernel consumerd path */
5600 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
5601 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
5602 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
5603 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
5604
5605 DBG2("Kernel consumer err path: %s",
5606 kconsumer_data.err_unix_sock_path);
5607 DBG2("Kernel consumer cmd path: %s",
5608 kconsumer_data.cmd_unix_sock_path);
5609 } else {
5610 home_path = get_home_dir();
5611 if (home_path == NULL) {
5612 /* TODO: Add --socket PATH option */
5613 ERR("Can't get HOME directory for sockets creation.");
5614 ret = -EPERM;
5615 goto error;
5616 }
5617
5618 /*
5619 * Create rundir from home path. This will create something like
5620 * $HOME/.lttng
5621 */
5622 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
5623 if (ret < 0) {
5624 ret = -ENOMEM;
5625 goto error;
5626 }
5627
5628 ret = create_lttng_rundir(rundir);
5629 if (ret < 0) {
5630 goto error;
5631 }
5632
5633 if (strlen(apps_unix_sock_path) == 0) {
5634 snprintf(apps_unix_sock_path, PATH_MAX,
5635 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
5636 }
5637
5638 /* Set the cli tool unix socket path */
5639 if (strlen(client_unix_sock_path) == 0) {
5640 snprintf(client_unix_sock_path, PATH_MAX,
5641 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
5642 }
5643
5644 /* Set global SHM for ust */
5645 if (strlen(wait_shm_path) == 0) {
5646 snprintf(wait_shm_path, PATH_MAX,
5647 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
5648 }
5649
5650 /* Set health check Unix path */
5651 if (strlen(health_unix_sock_path) == 0) {
5652 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5653 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
5654 }
5655 }
5656
5657 /* Set consumer initial state */
5658 kernel_consumerd_state = CONSUMER_STOPPED;
5659 ust_consumerd_state = CONSUMER_STOPPED;
5660
5661 DBG("Client socket path %s", client_unix_sock_path);
5662 DBG("Application socket path %s", apps_unix_sock_path);
5663 DBG("LTTng run directory path: %s", rundir);
5664
5665 /* 32 bits consumerd path setup */
5666 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
5667 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
5668 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
5669 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
5670
5671 DBG2("UST consumer 32 bits err path: %s",
5672 ustconsumer32_data.err_unix_sock_path);
5673 DBG2("UST consumer 32 bits cmd path: %s",
5674 ustconsumer32_data.cmd_unix_sock_path);
5675
5676 /* 64 bits consumerd path setup */
5677 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
5678 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
5679 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
5680 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
5681
5682 DBG2("UST consumer 64 bits err path: %s",
5683 ustconsumer64_data.err_unix_sock_path);
5684 DBG2("UST consumer 64 bits cmd path: %s",
5685 ustconsumer64_data.cmd_unix_sock_path);
5686
5687 /*
5688 * See if daemon already exist.
5689 */
5690 if ((ret = check_existing_daemon()) < 0) {
5691 ERR("Already running daemon.\n");
5692 /*
5693 * We do not goto exit because we must not cleanup()
5694 * because a daemon is already running.
5695 */
5696 goto error;
5697 }
5698
5699 /*
5700 * Init UST app hash table. Alloc hash table before this point since
5701 * cleanup() can get called after that point.
5702 */
5703 ust_app_ht_alloc();
5704
5705 /* After this point, we can safely call cleanup() with "goto exit" */
5706
5707 /*
5708 * These actions must be executed as root. We do that *after* setting up
5709 * the sockets path because we MUST make the check for another daemon using
5710 * those paths *before* trying to set the kernel consumer sockets and init
5711 * kernel tracer.
5712 */
5713 if (is_root) {
5714 ret = set_consumer_sockets(&kconsumer_data, rundir);
5715 if (ret < 0) {
5716 goto exit;
5717 }
5718
5719 /* Setup kernel tracer */
5720 if (!opt_no_kernel) {
5721 init_kernel_tracer();
5722 }
5723
5724 /* Set ulimit for open files */
5725 set_ulimit();
5726 }
5727 /* init lttng_fd tracking must be done after set_ulimit. */
5728 lttng_fd_init();
5729
5730 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
5731 if (ret < 0) {
5732 goto exit;
5733 }
5734
5735 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
5736 if (ret < 0) {
5737 goto exit;
5738 }
5739
5740 if ((ret = set_signal_handler()) < 0) {
5741 goto exit;
5742 }
5743
5744 /* Setup the needed unix socket */
5745 if ((ret = init_daemon_socket()) < 0) {
5746 goto exit;
5747 }
5748
5749 /* Set credentials to socket */
5750 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
5751 goto exit;
5752 }
5753
5754 /* Get parent pid if -S, --sig-parent is specified. */
5755 if (opt_sig_parent) {
5756 ppid = getppid();
5757 }
5758
5759 /* Setup the kernel pipe for waking up the kernel thread */
5760 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
5761 goto exit;
5762 }
5763
5764 /* Setup the thread apps communication pipe. */
5765 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
5766 goto exit;
5767 }
5768
5769 /* Init UST command queue. */
5770 cds_wfq_init(&ust_cmd_queue.queue);
5771
5772 /*
5773 * Get session list pointer. This pointer MUST NOT be free(). This list is
5774 * statically declared in session.c
5775 */
5776 session_list_ptr = session_get_list();
5777
5778 /* Set up max poll set size */
5779 lttng_poll_set_max_size();
5780
5781 /*
5782 * Set network sequence index to 1 for streams to match a relayd socket on
5783 * the consumer side.
5784 */
5785 uatomic_set(&relayd_net_seq_idx, 1);
5786
5787 /* Init all health thread counters. */
5788 health_init(&health_thread_cmd);
5789 health_init(&health_thread_kernel);
5790 health_init(&health_thread_app_manage);
5791 health_init(&health_thread_app_reg);
5792
5793 /*
5794 * Init health counters of the consumer thread. We do a quick hack here to
5795 * the state of the consumer health is fine even if the thread is not
5796 * started. This is simply to ease our life and has no cost what so ever.
5797 */
5798 health_init(&kconsumer_data.health);
5799 health_poll_update(&kconsumer_data.health);
5800 health_init(&ustconsumer32_data.health);
5801 health_poll_update(&ustconsumer32_data.health);
5802 health_init(&ustconsumer64_data.health);
5803 health_poll_update(&ustconsumer64_data.health);
5804
5805 /* Create thread to manage the client socket */
5806 ret = pthread_create(&health_thread, NULL,
5807 thread_manage_health, (void *) NULL);
5808 if (ret != 0) {
5809 PERROR("pthread_create health");
5810 goto exit_health;
5811 }
5812
5813 /* Create thread to manage the client socket */
5814 ret = pthread_create(&client_thread, NULL,
5815 thread_manage_clients, (void *) NULL);
5816 if (ret != 0) {
5817 PERROR("pthread_create clients");
5818 goto exit_client;
5819 }
5820
5821 /* Create thread to dispatch registration */
5822 ret = pthread_create(&dispatch_thread, NULL,
5823 thread_dispatch_ust_registration, (void *) NULL);
5824 if (ret != 0) {
5825 PERROR("pthread_create dispatch");
5826 goto exit_dispatch;
5827 }
5828
5829 /* Create thread to manage application registration. */
5830 ret = pthread_create(&reg_apps_thread, NULL,
5831 thread_registration_apps, (void *) NULL);
5832 if (ret != 0) {
5833 PERROR("pthread_create registration");
5834 goto exit_reg_apps;
5835 }
5836
5837 /* Create thread to manage application socket */
5838 ret = pthread_create(&apps_thread, NULL,
5839 thread_manage_apps, (void *) NULL);
5840 if (ret != 0) {
5841 PERROR("pthread_create apps");
5842 goto exit_apps;
5843 }
5844
5845 /* Create kernel thread to manage kernel event */
5846 ret = pthread_create(&kernel_thread, NULL,
5847 thread_manage_kernel, (void *) NULL);
5848 if (ret != 0) {
5849 PERROR("pthread_create kernel");
5850 goto exit_kernel;
5851 }
5852
5853 ret = pthread_join(kernel_thread, &status);
5854 if (ret != 0) {
5855 PERROR("pthread_join");
5856 goto error; /* join error, exit without cleanup */
5857 }
5858
5859 exit_kernel:
5860 ret = pthread_join(apps_thread, &status);
5861 if (ret != 0) {
5862 PERROR("pthread_join");
5863 goto error; /* join error, exit without cleanup */
5864 }
5865
5866 exit_apps:
5867 ret = pthread_join(reg_apps_thread, &status);
5868 if (ret != 0) {
5869 PERROR("pthread_join");
5870 goto error; /* join error, exit without cleanup */
5871 }
5872
5873 exit_reg_apps:
5874 ret = pthread_join(dispatch_thread, &status);
5875 if (ret != 0) {
5876 PERROR("pthread_join");
5877 goto error; /* join error, exit without cleanup */
5878 }
5879
5880 exit_dispatch:
5881 ret = pthread_join(client_thread, &status);
5882 if (ret != 0) {
5883 PERROR("pthread_join");
5884 goto error; /* join error, exit without cleanup */
5885 }
5886
5887 ret = join_consumer_thread(&kconsumer_data);
5888 if (ret != 0) {
5889 PERROR("join_consumer");
5890 goto error; /* join error, exit without cleanup */
5891 }
5892
5893 exit_client:
5894 exit_health:
5895 exit:
5896 /*
5897 * cleanup() is called when no other thread is running.
5898 */
5899 rcu_thread_online();
5900 cleanup();
5901 rcu_thread_offline();
5902 rcu_unregister_thread();
5903 if (!ret) {
5904 exit(EXIT_SUCCESS);
5905 }
5906 error:
5907 exit(EXIT_FAILURE);
5908 }
This page took 0.195401 seconds and 4 git commands to generate.