Fix: relayd and sessiond: dispatch thread exit is shared across threads
[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_reg;
220 struct health_state health_thread_kernel;
221
222 static
223 void setup_consumerd_path(void)
224 {
225 const char *bin, *libdir;
226
227 /*
228 * Allow INSTALL_BIN_PATH to be used as a target path for the
229 * native architecture size consumer if CONFIG_CONSUMER*_PATH
230 * has not been defined.
231 */
232 #if (CAA_BITS_PER_LONG == 32)
233 if (!consumerd32_bin[0]) {
234 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
235 }
236 if (!consumerd32_libdir[0]) {
237 consumerd32_libdir = INSTALL_LIB_PATH;
238 }
239 #elif (CAA_BITS_PER_LONG == 64)
240 if (!consumerd64_bin[0]) {
241 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
242 }
243 if (!consumerd64_libdir[0]) {
244 consumerd64_libdir = INSTALL_LIB_PATH;
245 }
246 #else
247 #error "Unknown bitness"
248 #endif
249
250 /*
251 * runtime env. var. overrides the build default.
252 */
253 bin = getenv("LTTNG_CONSUMERD32_BIN");
254 if (bin) {
255 consumerd32_bin = bin;
256 }
257 bin = getenv("LTTNG_CONSUMERD64_BIN");
258 if (bin) {
259 consumerd64_bin = bin;
260 }
261 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
262 if (libdir) {
263 consumerd32_libdir = libdir;
264 }
265 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
266 if (libdir) {
267 consumerd64_libdir = libdir;
268 }
269 }
270
271 /*
272 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
273 */
274 static int create_thread_poll_set(struct lttng_poll_event *events,
275 unsigned int size)
276 {
277 int ret;
278
279 if (events == NULL || size == 0) {
280 ret = -1;
281 goto error;
282 }
283
284 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
285 if (ret < 0) {
286 goto error;
287 }
288
289 /* Add quit pipe */
290 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
291 if (ret < 0) {
292 goto error;
293 }
294
295 return 0;
296
297 error:
298 return ret;
299 }
300
301 /*
302 * Check if the thread quit pipe was triggered.
303 *
304 * Return 1 if it was triggered else 0;
305 */
306 static int check_thread_quit_pipe(int fd, uint32_t events)
307 {
308 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
309 return 1;
310 }
311
312 return 0;
313 }
314
315 /*
316 * Return group ID of the tracing group or -1 if not found.
317 */
318 static gid_t allowed_group(void)
319 {
320 struct group *grp;
321
322 if (opt_tracing_group) {
323 grp = getgrnam(opt_tracing_group);
324 } else {
325 grp = getgrnam(default_tracing_group);
326 }
327 if (!grp) {
328 return -1;
329 } else {
330 return grp->gr_gid;
331 }
332 }
333
334 /*
335 * Init thread quit pipe.
336 *
337 * Return -1 on error or 0 if all pipes are created.
338 */
339 static int init_thread_quit_pipe(void)
340 {
341 int ret, i;
342
343 ret = pipe(thread_quit_pipe);
344 if (ret < 0) {
345 PERROR("thread quit pipe");
346 goto error;
347 }
348
349 for (i = 0; i < 2; i++) {
350 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
351 if (ret < 0) {
352 PERROR("fcntl");
353 goto error;
354 }
355 }
356
357 error:
358 return ret;
359 }
360
361 /*
362 * Complete teardown of a kernel session. This free all data structure related
363 * to a kernel session and update counter.
364 */
365 static void teardown_kernel_session(struct ltt_session *session)
366 {
367 if (!session->kernel_session) {
368 DBG3("No kernel session when tearing down session");
369 return;
370 }
371
372 DBG("Tearing down kernel session");
373
374 /*
375 * If a custom kernel consumer was registered, close the socket before
376 * tearing down the complete kernel session structure
377 */
378 if (kconsumer_data.cmd_sock >= 0 &&
379 session->kernel_session->consumer_fd != kconsumer_data.cmd_sock) {
380 lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
381 }
382
383 trace_kernel_destroy_session(session->kernel_session);
384 }
385
386 /*
387 * Complete teardown of all UST sessions. This will free everything on his path
388 * and destroy the core essence of all ust sessions :)
389 */
390 static void teardown_ust_session(struct ltt_session *session)
391 {
392 int ret;
393
394 if (!session->ust_session) {
395 DBG3("No UST session when tearing down session");
396 return;
397 }
398
399 DBG("Tearing down UST session(s)");
400
401 ret = ust_app_destroy_trace_all(session->ust_session);
402 if (ret) {
403 ERR("Error in ust_app_destroy_trace_all");
404 }
405
406 trace_ust_destroy_session(session->ust_session);
407 }
408
409 /*
410 * Stop all threads by closing the thread quit pipe.
411 */
412 static void stop_threads(void)
413 {
414 int ret;
415
416 /* Stopping all threads */
417 DBG("Terminating all threads");
418 ret = notify_thread_pipe(thread_quit_pipe[1]);
419 if (ret < 0) {
420 ERR("write error on thread quit pipe");
421 }
422
423 /* Dispatch thread */
424 CMM_STORE_SHARED(dispatch_thread_exit, 1);
425 futex_nto1_wake(&ust_cmd_queue.futex);
426 }
427
428 /*
429 * Cleanup the daemon
430 */
431 static void cleanup(void)
432 {
433 int ret;
434 char *cmd;
435 struct ltt_session *sess, *stmp;
436
437 DBG("Cleaning up");
438
439 DBG("Removing %s directory", rundir);
440 ret = asprintf(&cmd, "rm -rf %s", rundir);
441 if (ret < 0) {
442 ERR("asprintf failed. Something is really wrong!");
443 }
444
445 /* Remove lttng run directory */
446 ret = system(cmd);
447 if (ret < 0) {
448 ERR("Unable to clean %s", rundir);
449 }
450 free(cmd);
451
452 DBG("Cleaning up all sessions");
453
454 /* Destroy session list mutex */
455 if (session_list_ptr != NULL) {
456 pthread_mutex_destroy(&session_list_ptr->lock);
457
458 /* Cleanup ALL session */
459 cds_list_for_each_entry_safe(sess, stmp,
460 &session_list_ptr->head, list) {
461 teardown_kernel_session(sess);
462 teardown_ust_session(sess);
463 free(sess);
464 }
465 }
466
467 DBG("Closing all UST sockets");
468 ust_app_clean_list();
469
470 pthread_mutex_destroy(&kconsumer_data.pid_mutex);
471
472 if (is_root && !opt_no_kernel) {
473 DBG2("Closing kernel fd");
474 if (kernel_tracer_fd >= 0) {
475 ret = close(kernel_tracer_fd);
476 if (ret) {
477 PERROR("close");
478 }
479 }
480 DBG("Unloading kernel modules");
481 modprobe_remove_lttng_all();
482 }
483 utils_close_pipe(kernel_poll_pipe);
484 utils_close_pipe(thread_quit_pipe);
485 utils_close_pipe(apps_cmd_pipe);
486
487 /* <fun> */
488 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
489 "Matthew, BEET driven development works!%c[%dm",
490 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
491 /* </fun> */
492 }
493
494 /*
495 * Send data on a unix socket using the liblttsessiondcomm API.
496 *
497 * Return lttcomm error code.
498 */
499 static int send_unix_sock(int sock, void *buf, size_t len)
500 {
501 /* Check valid length */
502 if (len <= 0) {
503 return -1;
504 }
505
506 return lttcomm_send_unix_sock(sock, buf, len);
507 }
508
509 /*
510 * Free memory of a command context structure.
511 */
512 static void clean_command_ctx(struct command_ctx **cmd_ctx)
513 {
514 DBG("Clean command context structure");
515 if (*cmd_ctx) {
516 if ((*cmd_ctx)->llm) {
517 free((*cmd_ctx)->llm);
518 }
519 if ((*cmd_ctx)->lsm) {
520 free((*cmd_ctx)->lsm);
521 }
522 free(*cmd_ctx);
523 *cmd_ctx = NULL;
524 }
525 }
526
527 /*
528 * Notify UST applications using the shm mmap futex.
529 */
530 static int notify_ust_apps(int active)
531 {
532 char *wait_shm_mmap;
533
534 DBG("Notifying applications of session daemon state: %d", active);
535
536 /* See shm.c for this call implying mmap, shm and futex calls */
537 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
538 if (wait_shm_mmap == NULL) {
539 goto error;
540 }
541
542 /* Wake waiting process */
543 futex_wait_update((int32_t *) wait_shm_mmap, active);
544
545 /* Apps notified successfully */
546 return 0;
547
548 error:
549 return -1;
550 }
551
552 /*
553 * Setup the outgoing data buffer for the response (llm) by allocating the
554 * right amount of memory and copying the original information from the lsm
555 * structure.
556 *
557 * Return total size of the buffer pointed by buf.
558 */
559 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
560 {
561 int ret, buf_size;
562
563 buf_size = size;
564
565 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
566 if (cmd_ctx->llm == NULL) {
567 PERROR("zmalloc");
568 ret = -ENOMEM;
569 goto error;
570 }
571
572 /* Copy common data */
573 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
574 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
575
576 cmd_ctx->llm->data_size = size;
577 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
578
579 return buf_size;
580
581 error:
582 return ret;
583 }
584
585 /*
586 * Update the kernel poll set of all channel fd available over all tracing
587 * session. Add the wakeup pipe at the end of the set.
588 */
589 static int update_kernel_poll(struct lttng_poll_event *events)
590 {
591 int ret;
592 struct ltt_session *session;
593 struct ltt_kernel_channel *channel;
594
595 DBG("Updating kernel poll set");
596
597 session_lock_list();
598 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
599 session_lock(session);
600 if (session->kernel_session == NULL) {
601 session_unlock(session);
602 continue;
603 }
604
605 cds_list_for_each_entry(channel,
606 &session->kernel_session->channel_list.head, list) {
607 /* Add channel fd to the kernel poll set */
608 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
609 if (ret < 0) {
610 session_unlock(session);
611 goto error;
612 }
613 DBG("Channel fd %d added to kernel set", channel->fd);
614 }
615 session_unlock(session);
616 }
617 session_unlock_list();
618
619 return 0;
620
621 error:
622 session_unlock_list();
623 return -1;
624 }
625
626 /*
627 * Find the channel fd from 'fd' over all tracing session. When found, check
628 * for new channel stream and send those stream fds to the kernel consumer.
629 *
630 * Useful for CPU hotplug feature.
631 */
632 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
633 {
634 int ret = 0;
635 struct ltt_session *session;
636 struct ltt_kernel_channel *channel;
637
638 DBG("Updating kernel streams for channel fd %d", fd);
639
640 session_lock_list();
641 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
642 session_lock(session);
643 if (session->kernel_session == NULL) {
644 session_unlock(session);
645 continue;
646 }
647
648 /* This is not suppose to be -1 but this is an extra security check */
649 if (session->kernel_session->consumer_fd < 0) {
650 session->kernel_session->consumer_fd = consumer_data->cmd_sock;
651 }
652
653 cds_list_for_each_entry(channel,
654 &session->kernel_session->channel_list.head, list) {
655 if (channel->fd == fd) {
656 DBG("Channel found, updating kernel streams");
657 ret = kernel_open_channel_stream(channel);
658 if (ret < 0) {
659 goto error;
660 }
661
662 /*
663 * Have we already sent fds to the consumer? If yes, it means
664 * that tracing is started so it is safe to send our updated
665 * stream fds.
666 */
667 if (session->kernel_session->consumer_fds_sent == 1 &&
668 session->kernel_session->consumer != NULL) {
669 ret = kernel_consumer_send_channel_stream(
670 session->kernel_session->consumer_fd, channel,
671 session->kernel_session);
672 if (ret < 0) {
673 goto error;
674 }
675 }
676 goto error;
677 }
678 }
679 session_unlock(session);
680 }
681 session_unlock_list();
682 return ret;
683
684 error:
685 session_unlock(session);
686 session_unlock_list();
687 return ret;
688 }
689
690 /*
691 * For each tracing session, update newly registered apps.
692 */
693 static void update_ust_app(int app_sock)
694 {
695 struct ltt_session *sess, *stmp;
696
697 session_lock_list();
698
699 /* For all tracing session(s) */
700 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
701 session_lock(sess);
702 if (sess->ust_session) {
703 ust_app_global_update(sess->ust_session, app_sock);
704 }
705 session_unlock(sess);
706 }
707
708 session_unlock_list();
709 }
710
711 /*
712 * This thread manage event coming from the kernel.
713 *
714 * Features supported in this thread:
715 * -) CPU Hotplug
716 */
717 static void *thread_manage_kernel(void *data)
718 {
719 int ret, i, pollfd, update_poll_flag = 1;
720 uint32_t revents, nb_fd;
721 char tmp;
722 struct lttng_poll_event events;
723
724 DBG("Thread manage kernel started");
725
726 health_code_update(&health_thread_kernel);
727
728 ret = create_thread_poll_set(&events, 2);
729 if (ret < 0) {
730 goto error_poll_create;
731 }
732
733 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
734 if (ret < 0) {
735 goto error;
736 }
737
738 while (1) {
739 health_code_update(&health_thread_kernel);
740
741 if (update_poll_flag == 1) {
742 /*
743 * Reset number of fd in the poll set. Always 2 since there is the thread
744 * quit pipe and the kernel pipe.
745 */
746 events.nb_fd = 2;
747
748 ret = update_kernel_poll(&events);
749 if (ret < 0) {
750 goto error;
751 }
752 update_poll_flag = 0;
753 }
754
755 nb_fd = LTTNG_POLL_GETNB(&events);
756
757 DBG("Thread kernel polling on %d fds", nb_fd);
758
759 /* Zeroed the poll events */
760 lttng_poll_reset(&events);
761
762 /* Poll infinite value of time */
763 restart:
764 health_poll_update(&health_thread_kernel);
765 ret = lttng_poll_wait(&events, -1);
766 health_poll_update(&health_thread_kernel);
767 if (ret < 0) {
768 /*
769 * Restart interrupted system call.
770 */
771 if (errno == EINTR) {
772 goto restart;
773 }
774 goto error;
775 } else if (ret == 0) {
776 /* Should not happen since timeout is infinite */
777 ERR("Return value of poll is 0 with an infinite timeout.\n"
778 "This should not have happened! Continuing...");
779 continue;
780 }
781
782 for (i = 0; i < nb_fd; i++) {
783 /* Fetch once the poll data */
784 revents = LTTNG_POLL_GETEV(&events, i);
785 pollfd = LTTNG_POLL_GETFD(&events, i);
786
787 health_code_update(&health_thread_kernel);
788
789 /* Thread quit pipe has been closed. Killing thread. */
790 ret = check_thread_quit_pipe(pollfd, revents);
791 if (ret) {
792 goto error;
793 }
794
795 /* Check for data on kernel pipe */
796 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
797 ret = read(kernel_poll_pipe[0], &tmp, 1);
798 update_poll_flag = 1;
799 continue;
800 } else {
801 /*
802 * New CPU detected by the kernel. Adding kernel stream to
803 * kernel session and updating the kernel consumer
804 */
805 if (revents & LPOLLIN) {
806 ret = update_kernel_stream(&kconsumer_data, pollfd);
807 if (ret < 0) {
808 continue;
809 }
810 break;
811 /*
812 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
813 * and unregister kernel stream at this point.
814 */
815 }
816 }
817 }
818 }
819
820 error:
821 lttng_poll_clean(&events);
822 error_poll_create:
823 health_reset(&health_thread_kernel);
824 DBG("Kernel thread dying");
825 return NULL;
826 }
827
828 /*
829 * This thread manage the consumer error sent back to the session daemon.
830 */
831 static void *thread_manage_consumer(void *data)
832 {
833 int sock = -1, i, ret, pollfd;
834 uint32_t revents, nb_fd;
835 enum lttcomm_return_code code;
836 struct lttng_poll_event events;
837 struct consumer_data *consumer_data = data;
838
839 DBG("[thread] Manage consumer started");
840
841 health_code_update(&consumer_data->health);
842
843 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
844 if (ret < 0) {
845 goto error_listen;
846 }
847
848 /*
849 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
850 * Nothing more will be added to this poll set.
851 */
852 ret = create_thread_poll_set(&events, 2);
853 if (ret < 0) {
854 goto error_poll;
855 }
856
857 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
858 if (ret < 0) {
859 goto error;
860 }
861
862 nb_fd = LTTNG_POLL_GETNB(&events);
863
864 health_code_update(&consumer_data->health);
865
866 /* Inifinite blocking call, waiting for transmission */
867 restart:
868 health_poll_update(&consumer_data->health);
869 ret = lttng_poll_wait(&events, -1);
870 health_poll_update(&consumer_data->health);
871 if (ret < 0) {
872 /*
873 * Restart interrupted system call.
874 */
875 if (errno == EINTR) {
876 goto restart;
877 }
878 goto error;
879 }
880
881 for (i = 0; i < nb_fd; i++) {
882 /* Fetch once the poll data */
883 revents = LTTNG_POLL_GETEV(&events, i);
884 pollfd = LTTNG_POLL_GETFD(&events, i);
885
886 health_code_update(&consumer_data->health);
887
888 /* Thread quit pipe has been closed. Killing thread. */
889 ret = check_thread_quit_pipe(pollfd, revents);
890 if (ret) {
891 goto error;
892 }
893
894 /* Event on the registration socket */
895 if (pollfd == consumer_data->err_sock) {
896 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
897 ERR("consumer err socket poll error");
898 goto error;
899 }
900 }
901 }
902
903 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
904 if (sock < 0) {
905 goto error;
906 }
907
908 health_code_update(&consumer_data->health);
909
910 DBG2("Receiving code from consumer err_sock");
911
912 /* Getting status code from kconsumerd */
913 ret = lttcomm_recv_unix_sock(sock, &code,
914 sizeof(enum lttcomm_return_code));
915 if (ret <= 0) {
916 goto error;
917 }
918
919 health_code_update(&consumer_data->health);
920
921 if (code == CONSUMERD_COMMAND_SOCK_READY) {
922 consumer_data->cmd_sock =
923 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
924 if (consumer_data->cmd_sock < 0) {
925 sem_post(&consumer_data->sem);
926 PERROR("consumer connect");
927 goto error;
928 }
929 /* Signal condition to tell that the kconsumerd is ready */
930 sem_post(&consumer_data->sem);
931 DBG("consumer command socket ready");
932 } else {
933 ERR("consumer error when waiting for SOCK_READY : %s",
934 lttcomm_get_readable_code(-code));
935 goto error;
936 }
937
938 /* Remove the kconsumerd error sock since we've established a connexion */
939 ret = lttng_poll_del(&events, consumer_data->err_sock);
940 if (ret < 0) {
941 goto error;
942 }
943
944 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
945 if (ret < 0) {
946 goto error;
947 }
948
949 health_code_update(&consumer_data->health);
950
951 /* Update number of fd */
952 nb_fd = LTTNG_POLL_GETNB(&events);
953
954 /* Inifinite blocking call, waiting for transmission */
955 restart_poll:
956 health_poll_update(&consumer_data->health);
957 ret = lttng_poll_wait(&events, -1);
958 health_poll_update(&consumer_data->health);
959 if (ret < 0) {
960 /*
961 * Restart interrupted system call.
962 */
963 if (errno == EINTR) {
964 goto restart_poll;
965 }
966 goto error;
967 }
968
969 for (i = 0; i < nb_fd; i++) {
970 /* Fetch once the poll data */
971 revents = LTTNG_POLL_GETEV(&events, i);
972 pollfd = LTTNG_POLL_GETFD(&events, i);
973
974 health_code_update(&consumer_data->health);
975
976 /* Thread quit pipe has been closed. Killing thread. */
977 ret = check_thread_quit_pipe(pollfd, revents);
978 if (ret) {
979 goto error;
980 }
981
982 /* Event on the kconsumerd socket */
983 if (pollfd == sock) {
984 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
985 ERR("consumer err socket second poll error");
986 goto error;
987 }
988 }
989 }
990
991 health_code_update(&consumer_data->health);
992
993 /* Wait for any kconsumerd error */
994 ret = lttcomm_recv_unix_sock(sock, &code,
995 sizeof(enum lttcomm_return_code));
996 if (ret <= 0) {
997 ERR("consumer closed the command socket");
998 goto error;
999 }
1000
1001 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
1002
1003 error:
1004 /* Immediately set the consumerd state to stopped */
1005 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1006 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1007 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1008 consumer_data->type == LTTNG_CONSUMER32_UST) {
1009 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1010 } else {
1011 /* Code flow error... */
1012 assert(0);
1013 }
1014
1015 if (consumer_data->err_sock >= 0) {
1016 ret = close(consumer_data->err_sock);
1017 if (ret) {
1018 PERROR("close");
1019 }
1020 }
1021 if (consumer_data->cmd_sock >= 0) {
1022 ret = close(consumer_data->cmd_sock);
1023 if (ret) {
1024 PERROR("close");
1025 }
1026 }
1027 if (sock >= 0) {
1028 ret = close(sock);
1029 if (ret) {
1030 PERROR("close");
1031 }
1032 }
1033
1034 unlink(consumer_data->err_unix_sock_path);
1035 unlink(consumer_data->cmd_unix_sock_path);
1036 consumer_data->pid = 0;
1037
1038 lttng_poll_clean(&events);
1039 error_poll:
1040 error_listen:
1041 health_reset(&consumer_data->health);
1042 DBG("consumer thread cleanup completed");
1043
1044 return NULL;
1045 }
1046
1047 /*
1048 * This thread manage application communication.
1049 */
1050 static void *thread_manage_apps(void *data)
1051 {
1052 int i, ret, pollfd;
1053 uint32_t revents, nb_fd;
1054 struct ust_command ust_cmd;
1055 struct lttng_poll_event events;
1056
1057 DBG("[thread] Manage application started");
1058
1059 rcu_register_thread();
1060 rcu_thread_online();
1061
1062 health_code_update(&health_thread_app_reg);
1063
1064 ret = create_thread_poll_set(&events, 2);
1065 if (ret < 0) {
1066 goto error_poll_create;
1067 }
1068
1069 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1070 if (ret < 0) {
1071 goto error;
1072 }
1073
1074 health_code_update(&health_thread_app_reg);
1075
1076 while (1) {
1077 /* Zeroed the events structure */
1078 lttng_poll_reset(&events);
1079
1080 nb_fd = LTTNG_POLL_GETNB(&events);
1081
1082 DBG("Apps thread polling on %d fds", nb_fd);
1083
1084 /* Inifinite blocking call, waiting for transmission */
1085 restart:
1086 health_poll_update(&health_thread_app_reg);
1087 ret = lttng_poll_wait(&events, -1);
1088 health_poll_update(&health_thread_app_reg);
1089 if (ret < 0) {
1090 /*
1091 * Restart interrupted system call.
1092 */
1093 if (errno == EINTR) {
1094 goto restart;
1095 }
1096 goto error;
1097 }
1098
1099 for (i = 0; i < nb_fd; i++) {
1100 /* Fetch once the poll data */
1101 revents = LTTNG_POLL_GETEV(&events, i);
1102 pollfd = LTTNG_POLL_GETFD(&events, i);
1103
1104 health_code_update(&health_thread_app_reg);
1105
1106 /* Thread quit pipe has been closed. Killing thread. */
1107 ret = check_thread_quit_pipe(pollfd, revents);
1108 if (ret) {
1109 goto error;
1110 }
1111
1112 /* Inspect the apps cmd pipe */
1113 if (pollfd == apps_cmd_pipe[0]) {
1114 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1115 ERR("Apps command pipe error");
1116 goto error;
1117 } else if (revents & LPOLLIN) {
1118 /* Empty pipe */
1119 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1120 if (ret < 0 || ret < sizeof(ust_cmd)) {
1121 PERROR("read apps cmd pipe");
1122 goto error;
1123 }
1124
1125 health_code_update(&health_thread_app_reg);
1126
1127 /* Register applicaton to the session daemon */
1128 ret = ust_app_register(&ust_cmd.reg_msg,
1129 ust_cmd.sock);
1130 if (ret == -ENOMEM) {
1131 goto error;
1132 } else if (ret < 0) {
1133 break;
1134 }
1135
1136 health_code_update(&health_thread_app_reg);
1137
1138 /*
1139 * Validate UST version compatibility.
1140 */
1141 ret = ust_app_validate_version(ust_cmd.sock);
1142 if (ret >= 0) {
1143 /*
1144 * Add channel(s) and event(s) to newly registered apps
1145 * from lttng global UST domain.
1146 */
1147 update_ust_app(ust_cmd.sock);
1148 }
1149
1150 health_code_update(&health_thread_app_reg);
1151
1152 ret = ust_app_register_done(ust_cmd.sock);
1153 if (ret < 0) {
1154 /*
1155 * If the registration is not possible, we simply
1156 * unregister the apps and continue
1157 */
1158 ust_app_unregister(ust_cmd.sock);
1159 } else {
1160 /*
1161 * We just need here to monitor the close of the UST
1162 * socket and poll set monitor those by default.
1163 * Listen on POLLIN (even if we never expect any
1164 * data) to ensure that hangup wakes us.
1165 */
1166 ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN);
1167 if (ret < 0) {
1168 goto error;
1169 }
1170
1171 DBG("Apps with sock %d added to poll set",
1172 ust_cmd.sock);
1173 }
1174
1175 health_code_update(&health_thread_app_reg);
1176
1177 break;
1178 }
1179 } else {
1180 /*
1181 * At this point, we know that a registered application made
1182 * the event at poll_wait.
1183 */
1184 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1185 /* Removing from the poll set */
1186 ret = lttng_poll_del(&events, pollfd);
1187 if (ret < 0) {
1188 goto error;
1189 }
1190
1191 /* Socket closed on remote end. */
1192 ust_app_unregister(pollfd);
1193 break;
1194 }
1195 }
1196
1197 health_code_update(&health_thread_app_reg);
1198 }
1199 }
1200
1201 error:
1202 lttng_poll_clean(&events);
1203 error_poll_create:
1204 health_reset(&health_thread_app_reg);
1205 DBG("Application communication apps thread cleanup complete");
1206 rcu_thread_offline();
1207 rcu_unregister_thread();
1208 return NULL;
1209 }
1210
1211 /*
1212 * Dispatch request from the registration threads to the application
1213 * communication thread.
1214 */
1215 static void *thread_dispatch_ust_registration(void *data)
1216 {
1217 int ret;
1218 struct cds_wfq_node *node;
1219 struct ust_command *ust_cmd = NULL;
1220
1221 DBG("[thread] Dispatch UST command started");
1222
1223 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1224 /* Atomically prepare the queue futex */
1225 futex_nto1_prepare(&ust_cmd_queue.futex);
1226
1227 do {
1228 /* Dequeue command for registration */
1229 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1230 if (node == NULL) {
1231 DBG("Woken up but nothing in the UST command queue");
1232 /* Continue thread execution */
1233 break;
1234 }
1235
1236 ust_cmd = caa_container_of(node, struct ust_command, node);
1237
1238 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1239 " gid:%d sock:%d name:%s (version %d.%d)",
1240 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1241 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1242 ust_cmd->sock, ust_cmd->reg_msg.name,
1243 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1244 /*
1245 * Inform apps thread of the new application registration. This
1246 * call is blocking so we can be assured that the data will be read
1247 * at some point in time or wait to the end of the world :)
1248 */
1249 ret = write(apps_cmd_pipe[1], ust_cmd,
1250 sizeof(struct ust_command));
1251 if (ret < 0) {
1252 PERROR("write apps cmd pipe");
1253 if (errno == EBADF) {
1254 /*
1255 * We can't inform the application thread to process
1256 * registration. We will exit or else application
1257 * registration will not occur and tracing will never
1258 * start.
1259 */
1260 goto error;
1261 }
1262 }
1263 free(ust_cmd);
1264 } while (node != NULL);
1265
1266 /* Futex wait on queue. Blocking call on futex() */
1267 futex_nto1_wait(&ust_cmd_queue.futex);
1268 }
1269
1270 error:
1271 DBG("Dispatch thread dying");
1272 return NULL;
1273 }
1274
1275 /*
1276 * This thread manage application registration.
1277 */
1278 static void *thread_registration_apps(void *data)
1279 {
1280 int sock = -1, i, ret, pollfd;
1281 uint32_t revents, nb_fd;
1282 struct lttng_poll_event events;
1283 /*
1284 * Get allocated in this thread, enqueued to a global queue, dequeued and
1285 * freed in the manage apps thread.
1286 */
1287 struct ust_command *ust_cmd = NULL;
1288
1289 DBG("[thread] Manage application registration started");
1290
1291 ret = lttcomm_listen_unix_sock(apps_sock);
1292 if (ret < 0) {
1293 goto error_listen;
1294 }
1295
1296 /*
1297 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1298 * more will be added to this poll set.
1299 */
1300 ret = create_thread_poll_set(&events, 2);
1301 if (ret < 0) {
1302 goto error_create_poll;
1303 }
1304
1305 /* Add the application registration socket */
1306 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1307 if (ret < 0) {
1308 goto error_poll_add;
1309 }
1310
1311 /* Notify all applications to register */
1312 ret = notify_ust_apps(1);
1313 if (ret < 0) {
1314 ERR("Failed to notify applications or create the wait shared memory.\n"
1315 "Execution continues but there might be problem for already\n"
1316 "running applications that wishes to register.");
1317 }
1318
1319 while (1) {
1320 DBG("Accepting application registration");
1321
1322 nb_fd = LTTNG_POLL_GETNB(&events);
1323
1324 /* Inifinite blocking call, waiting for transmission */
1325 restart:
1326 ret = lttng_poll_wait(&events, -1);
1327 if (ret < 0) {
1328 /*
1329 * Restart interrupted system call.
1330 */
1331 if (errno == EINTR) {
1332 goto restart;
1333 }
1334 goto error;
1335 }
1336
1337 for (i = 0; i < nb_fd; i++) {
1338 /* Fetch once the poll data */
1339 revents = LTTNG_POLL_GETEV(&events, i);
1340 pollfd = LTTNG_POLL_GETFD(&events, i);
1341
1342 /* Thread quit pipe has been closed. Killing thread. */
1343 ret = check_thread_quit_pipe(pollfd, revents);
1344 if (ret) {
1345 goto error;
1346 }
1347
1348 /* Event on the registration socket */
1349 if (pollfd == apps_sock) {
1350 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1351 ERR("Register apps socket poll error");
1352 goto error;
1353 } else if (revents & LPOLLIN) {
1354 sock = lttcomm_accept_unix_sock(apps_sock);
1355 if (sock < 0) {
1356 goto error;
1357 }
1358
1359 /* Create UST registration command for enqueuing */
1360 ust_cmd = zmalloc(sizeof(struct ust_command));
1361 if (ust_cmd == NULL) {
1362 PERROR("ust command zmalloc");
1363 goto error;
1364 }
1365
1366 /*
1367 * Using message-based transmissions to ensure we don't
1368 * have to deal with partially received messages.
1369 */
1370 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1371 if (ret < 0) {
1372 ERR("Exhausted file descriptors allowed for applications.");
1373 free(ust_cmd);
1374 ret = close(sock);
1375 if (ret) {
1376 PERROR("close");
1377 }
1378 sock = -1;
1379 continue;
1380 }
1381 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1382 sizeof(struct ust_register_msg));
1383 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1384 if (ret < 0) {
1385 PERROR("lttcomm_recv_unix_sock register apps");
1386 } else {
1387 ERR("Wrong size received on apps register");
1388 }
1389 free(ust_cmd);
1390 ret = close(sock);
1391 if (ret) {
1392 PERROR("close");
1393 }
1394 lttng_fd_put(LTTNG_FD_APPS, 1);
1395 sock = -1;
1396 continue;
1397 }
1398
1399 ust_cmd->sock = sock;
1400 sock = -1;
1401
1402 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1403 " gid:%d sock:%d name:%s (version %d.%d)",
1404 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1405 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1406 ust_cmd->sock, ust_cmd->reg_msg.name,
1407 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1408
1409 /*
1410 * Lock free enqueue the registration request. The red pill
1411 * has been taken! This apps will be part of the *system*.
1412 */
1413 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1414
1415 /*
1416 * Wake the registration queue futex. Implicit memory
1417 * barrier with the exchange in cds_wfq_enqueue.
1418 */
1419 futex_nto1_wake(&ust_cmd_queue.futex);
1420 }
1421 }
1422 }
1423 }
1424
1425 error:
1426 /* Notify that the registration thread is gone */
1427 notify_ust_apps(0);
1428
1429 if (apps_sock >= 0) {
1430 ret = close(apps_sock);
1431 if (ret) {
1432 PERROR("close");
1433 }
1434 }
1435 if (sock >= 0) {
1436 ret = close(sock);
1437 if (ret) {
1438 PERROR("close");
1439 }
1440 lttng_fd_put(LTTNG_FD_APPS, 1);
1441 }
1442 unlink(apps_unix_sock_path);
1443
1444 error_poll_add:
1445 lttng_poll_clean(&events);
1446 error_listen:
1447 error_create_poll:
1448 DBG("UST Registration thread cleanup complete");
1449
1450 return NULL;
1451 }
1452
1453 /*
1454 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1455 * exec or it will fails.
1456 */
1457 static int spawn_consumer_thread(struct consumer_data *consumer_data)
1458 {
1459 int ret;
1460 struct timespec timeout;
1461
1462 timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT;
1463 timeout.tv_nsec = 0;
1464
1465 /* Setup semaphore */
1466 ret = sem_init(&consumer_data->sem, 0, 0);
1467 if (ret < 0) {
1468 PERROR("sem_init consumer semaphore");
1469 goto error;
1470 }
1471
1472 ret = pthread_create(&consumer_data->thread, NULL,
1473 thread_manage_consumer, consumer_data);
1474 if (ret != 0) {
1475 PERROR("pthread_create consumer");
1476 ret = -1;
1477 goto error;
1478 }
1479
1480 /* Get time for sem_timedwait absolute timeout */
1481 ret = clock_gettime(CLOCK_REALTIME, &timeout);
1482 if (ret < 0) {
1483 PERROR("clock_gettime spawn consumer");
1484 /* Infinite wait for the kconsumerd thread to be ready */
1485 ret = sem_wait(&consumer_data->sem);
1486 } else {
1487 /* Normal timeout if the gettime was successful */
1488 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
1489 ret = sem_timedwait(&consumer_data->sem, &timeout);
1490 }
1491
1492 if (ret < 0) {
1493 if (errno == ETIMEDOUT) {
1494 /*
1495 * Call has timed out so we kill the kconsumerd_thread and return
1496 * an error.
1497 */
1498 ERR("The consumer thread was never ready. Killing it");
1499 ret = pthread_cancel(consumer_data->thread);
1500 if (ret < 0) {
1501 PERROR("pthread_cancel consumer thread");
1502 }
1503 } else {
1504 PERROR("semaphore wait failed consumer thread");
1505 }
1506 goto error;
1507 }
1508
1509 pthread_mutex_lock(&consumer_data->pid_mutex);
1510 if (consumer_data->pid == 0) {
1511 ERR("Kconsumerd did not start");
1512 pthread_mutex_unlock(&consumer_data->pid_mutex);
1513 goto error;
1514 }
1515 pthread_mutex_unlock(&consumer_data->pid_mutex);
1516
1517 return 0;
1518
1519 error:
1520 return ret;
1521 }
1522
1523 /*
1524 * Join consumer thread
1525 */
1526 static int join_consumer_thread(struct consumer_data *consumer_data)
1527 {
1528 void *status;
1529 int ret;
1530
1531 if (consumer_data->pid != 0) {
1532 ret = kill(consumer_data->pid, SIGTERM);
1533 if (ret) {
1534 ERR("Error killing consumer daemon");
1535 return ret;
1536 }
1537 return pthread_join(consumer_data->thread, &status);
1538 } else {
1539 return 0;
1540 }
1541 }
1542
1543 /*
1544 * Fork and exec a consumer daemon (consumerd).
1545 *
1546 * Return pid if successful else -1.
1547 */
1548 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
1549 {
1550 int ret;
1551 pid_t pid;
1552 const char *consumer_to_use;
1553 const char *verbosity;
1554 struct stat st;
1555
1556 DBG("Spawning consumerd");
1557
1558 pid = fork();
1559 if (pid == 0) {
1560 /*
1561 * Exec consumerd.
1562 */
1563 if (opt_verbose_consumer) {
1564 verbosity = "--verbose";
1565 } else {
1566 verbosity = "--quiet";
1567 }
1568 switch (consumer_data->type) {
1569 case LTTNG_CONSUMER_KERNEL:
1570 /*
1571 * Find out which consumerd to execute. We will first try the
1572 * 64-bit path, then the sessiond's installation directory, and
1573 * fallback on the 32-bit one,
1574 */
1575 DBG3("Looking for a kernel consumer at these locations:");
1576 DBG3(" 1) %s", consumerd64_bin);
1577 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1578 DBG3(" 3) %s", consumerd32_bin);
1579 if (stat(consumerd64_bin, &st) == 0) {
1580 DBG3("Found location #1");
1581 consumer_to_use = consumerd64_bin;
1582 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
1583 DBG3("Found location #2");
1584 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
1585 } else if (stat(consumerd32_bin, &st) == 0) {
1586 DBG3("Found location #3");
1587 consumer_to_use = consumerd32_bin;
1588 } else {
1589 DBG("Could not find any valid consumerd executable");
1590 break;
1591 }
1592 DBG("Using kernel consumer at: %s", consumer_to_use);
1593 execl(consumer_to_use,
1594 "lttng-consumerd", verbosity, "-k",
1595 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1596 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1597 NULL);
1598 break;
1599 case LTTNG_CONSUMER64_UST:
1600 {
1601 char *tmpnew = NULL;
1602
1603 if (consumerd64_libdir[0] != '\0') {
1604 char *tmp;
1605 size_t tmplen;
1606
1607 tmp = getenv("LD_LIBRARY_PATH");
1608 if (!tmp) {
1609 tmp = "";
1610 }
1611 tmplen = strlen("LD_LIBRARY_PATH=")
1612 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1613 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1614 if (!tmpnew) {
1615 ret = -ENOMEM;
1616 goto error;
1617 }
1618 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1619 strcat(tmpnew, consumerd64_libdir);
1620 if (tmp[0] != '\0') {
1621 strcat(tmpnew, ":");
1622 strcat(tmpnew, tmp);
1623 }
1624 ret = putenv(tmpnew);
1625 if (ret) {
1626 ret = -errno;
1627 goto error;
1628 }
1629 }
1630 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
1631 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
1632 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1633 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1634 NULL);
1635 if (consumerd64_libdir[0] != '\0') {
1636 free(tmpnew);
1637 }
1638 if (ret) {
1639 goto error;
1640 }
1641 break;
1642 }
1643 case LTTNG_CONSUMER32_UST:
1644 {
1645 char *tmpnew = NULL;
1646
1647 if (consumerd32_libdir[0] != '\0') {
1648 char *tmp;
1649 size_t tmplen;
1650
1651 tmp = getenv("LD_LIBRARY_PATH");
1652 if (!tmp) {
1653 tmp = "";
1654 }
1655 tmplen = strlen("LD_LIBRARY_PATH=")
1656 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1657 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1658 if (!tmpnew) {
1659 ret = -ENOMEM;
1660 goto error;
1661 }
1662 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1663 strcat(tmpnew, consumerd32_libdir);
1664 if (tmp[0] != '\0') {
1665 strcat(tmpnew, ":");
1666 strcat(tmpnew, tmp);
1667 }
1668 ret = putenv(tmpnew);
1669 if (ret) {
1670 ret = -errno;
1671 goto error;
1672 }
1673 }
1674 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
1675 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
1676 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1677 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1678 NULL);
1679 if (consumerd32_libdir[0] != '\0') {
1680 free(tmpnew);
1681 }
1682 if (ret) {
1683 goto error;
1684 }
1685 break;
1686 }
1687 default:
1688 PERROR("unknown consumer type");
1689 exit(EXIT_FAILURE);
1690 }
1691 if (errno != 0) {
1692 PERROR("kernel start consumer exec");
1693 }
1694 exit(EXIT_FAILURE);
1695 } else if (pid > 0) {
1696 ret = pid;
1697 } else {
1698 PERROR("start consumer fork");
1699 ret = -errno;
1700 }
1701 error:
1702 return ret;
1703 }
1704
1705 /*
1706 * Spawn the consumerd daemon and session daemon thread.
1707 */
1708 static int start_consumerd(struct consumer_data *consumer_data)
1709 {
1710 int ret;
1711
1712 pthread_mutex_lock(&consumer_data->pid_mutex);
1713 if (consumer_data->pid != 0) {
1714 pthread_mutex_unlock(&consumer_data->pid_mutex);
1715 goto end;
1716 }
1717
1718 ret = spawn_consumerd(consumer_data);
1719 if (ret < 0) {
1720 ERR("Spawning consumerd failed");
1721 pthread_mutex_unlock(&consumer_data->pid_mutex);
1722 goto error;
1723 }
1724
1725 /* Setting up the consumer_data pid */
1726 consumer_data->pid = ret;
1727 DBG2("Consumer pid %d", consumer_data->pid);
1728 pthread_mutex_unlock(&consumer_data->pid_mutex);
1729
1730 DBG2("Spawning consumer control thread");
1731 ret = spawn_consumer_thread(consumer_data);
1732 if (ret < 0) {
1733 ERR("Fatal error spawning consumer control thread");
1734 goto error;
1735 }
1736
1737 end:
1738 return 0;
1739
1740 error:
1741 return ret;
1742 }
1743
1744 /*
1745 * Compute health status of each consumer.
1746 */
1747 static int check_consumer_health(void)
1748 {
1749 int ret;
1750
1751 ret =
1752 health_check_state(&kconsumer_data.health) &
1753 health_check_state(&ustconsumer32_data.health) &
1754 health_check_state(&ustconsumer64_data.health);
1755
1756 DBG3("Health consumer check %d", ret);
1757
1758 return ret;
1759 }
1760
1761 /*
1762 * Check version of the lttng-modules.
1763 */
1764 static int validate_lttng_modules_version(void)
1765 {
1766 return kernel_validate_version(kernel_tracer_fd);
1767 }
1768
1769 /*
1770 * Setup necessary data for kernel tracer action.
1771 */
1772 static int init_kernel_tracer(void)
1773 {
1774 int ret;
1775
1776 /* Modprobe lttng kernel modules */
1777 ret = modprobe_lttng_control();
1778 if (ret < 0) {
1779 goto error;
1780 }
1781
1782 /* Open debugfs lttng */
1783 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1784 if (kernel_tracer_fd < 0) {
1785 DBG("Failed to open %s", module_proc_lttng);
1786 ret = -1;
1787 goto error_open;
1788 }
1789
1790 /* Validate kernel version */
1791 ret = validate_lttng_modules_version();
1792 if (ret < 0) {
1793 goto error_version;
1794 }
1795
1796 ret = modprobe_lttng_data();
1797 if (ret < 0) {
1798 goto error_modules;
1799 }
1800
1801 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1802 return 0;
1803
1804 error_version:
1805 modprobe_remove_lttng_control();
1806 ret = close(kernel_tracer_fd);
1807 if (ret) {
1808 PERROR("close");
1809 }
1810 kernel_tracer_fd = -1;
1811 return LTTCOMM_KERN_VERSION;
1812
1813 error_modules:
1814 ret = close(kernel_tracer_fd);
1815 if (ret) {
1816 PERROR("close");
1817 }
1818
1819 error_open:
1820 modprobe_remove_lttng_control();
1821
1822 error:
1823 WARN("No kernel tracer available");
1824 kernel_tracer_fd = -1;
1825 if (!is_root) {
1826 return LTTCOMM_NEED_ROOT_SESSIOND;
1827 } else {
1828 return LTTCOMM_KERN_NA;
1829 }
1830 }
1831
1832 /*
1833 * Init tracing by creating trace directory and sending fds kernel consumer.
1834 */
1835 static int init_kernel_tracing(struct ltt_kernel_session *session)
1836 {
1837 int ret = 0;
1838
1839 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
1840 /*
1841 * Assign default kernel consumer socket if no consumer assigned to the
1842 * kernel session. At this point, it's NOT supposed to be -1 but this is
1843 * an extra security check.
1844 */
1845 if (session->consumer_fd < 0) {
1846 session->consumer_fd = kconsumer_data.cmd_sock;
1847 }
1848
1849 ret = kernel_consumer_send_session(session->consumer_fd, session);
1850 if (ret < 0) {
1851 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1852 goto error;
1853 }
1854 }
1855
1856 error:
1857 return ret;
1858 }
1859
1860 /*
1861 * Create a socket to the relayd using the URI.
1862 *
1863 * On success, the relayd_sock pointer is set to the created socket.
1864 * Else, it is untouched and an lttcomm error code is returned.
1865 */
1866 static int create_connect_relayd(struct consumer_output *output,
1867 const char *session_name, struct lttng_uri *uri,
1868 struct lttcomm_sock **relayd_sock)
1869 {
1870 int ret;
1871 struct lttcomm_sock *sock;
1872
1873 /* Create socket object from URI */
1874 sock = lttcomm_alloc_sock_from_uri(uri);
1875 if (sock == NULL) {
1876 ret = LTTCOMM_FATAL;
1877 goto error;
1878 }
1879
1880 ret = lttcomm_create_sock(sock);
1881 if (ret < 0) {
1882 ret = LTTCOMM_FATAL;
1883 goto error;
1884 }
1885
1886 /* Connect to relayd so we can proceed with a session creation. */
1887 ret = relayd_connect(sock);
1888 if (ret < 0) {
1889 ERR("Unable to reach lttng-relayd");
1890 ret = LTTCOMM_RELAYD_SESSION_FAIL;
1891 goto free_sock;
1892 }
1893
1894 /* Create socket for control stream. */
1895 if (uri->stype == LTTNG_STREAM_CONTROL) {
1896 DBG3("Creating relayd stream socket from URI");
1897
1898 /* Check relayd version */
1899 ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0);
1900 if (ret < 0) {
1901 ret = LTTCOMM_RELAYD_VERSION_FAIL;
1902 goto close_sock;
1903 }
1904 } else if (uri->stype == LTTNG_STREAM_DATA) {
1905 DBG3("Creating relayd data socket from URI");
1906 } else {
1907 /* Command is not valid */
1908 ERR("Relayd invalid stream type: %d", uri->stype);
1909 ret = LTTCOMM_INVALID;
1910 goto close_sock;
1911 }
1912
1913 *relayd_sock = sock;
1914
1915 return LTTCOMM_OK;
1916
1917 close_sock:
1918 if (sock) {
1919 (void) relayd_close(sock);
1920 }
1921 free_sock:
1922 if (sock) {
1923 lttcomm_destroy_sock(sock);
1924 }
1925 error:
1926 return ret;
1927 }
1928
1929 /*
1930 * Connect to the relayd using URI and send the socket to the right consumer.
1931 */
1932 static int send_socket_relayd_consumer(int domain, struct ltt_session *session,
1933 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
1934 int consumer_fd)
1935 {
1936 int ret;
1937 struct lttcomm_sock *sock = NULL;
1938
1939 /* Set the network sequence index if not set. */
1940 if (consumer->net_seq_index == -1) {
1941 /*
1942 * Increment net_seq_idx because we are about to transfer the
1943 * new relayd socket to the consumer.
1944 */
1945 uatomic_inc(&relayd_net_seq_idx);
1946 /* Assign unique key so the consumer can match streams */
1947 consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx);
1948 }
1949
1950 /* Connect to relayd and make version check if uri is the control. */
1951 ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
1952 if (ret != LTTCOMM_OK) {
1953 goto close_sock;
1954 }
1955
1956 /* If the control socket is connected, network session is ready */
1957 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
1958 session->net_handle = 1;
1959 }
1960
1961 /* Send relayd socket to consumer. */
1962 ret = consumer_send_relayd_socket(consumer_fd, sock,
1963 consumer, relayd_uri->stype);
1964 if (ret < 0) {
1965 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
1966 goto close_sock;
1967 }
1968
1969 ret = LTTCOMM_OK;
1970
1971 /*
1972 * Close socket which was dup on the consumer side. The session daemon does
1973 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1974 */
1975
1976 close_sock:
1977 if (sock) {
1978 (void) relayd_close(sock);
1979 lttcomm_destroy_sock(sock);
1980 }
1981
1982 return ret;
1983 }
1984
1985 /*
1986 * Send both relayd sockets to a specific consumer and domain. This is a
1987 * helper function to facilitate sending the information to the consumer for a
1988 * session.
1989 */
1990 static int send_sockets_relayd_consumer(int domain,
1991 struct ltt_session *session, struct consumer_output *consumer, int fd)
1992 {
1993 int ret;
1994
1995 /* Sending control relayd socket. */
1996 ret = send_socket_relayd_consumer(domain, session,
1997 &consumer->dst.net.control, consumer, fd);
1998 if (ret != LTTCOMM_OK) {
1999 goto error;
2000 }
2001
2002 /* Sending data relayd socket. */
2003 ret = send_socket_relayd_consumer(domain, session,
2004 &consumer->dst.net.data, consumer, fd);
2005 if (ret != LTTCOMM_OK) {
2006 goto error;
2007 }
2008
2009 error:
2010 return ret;
2011 }
2012
2013 /*
2014 * Setup relayd connections for a tracing session. First creates the socket to
2015 * the relayd and send them to the right domain consumer. Consumer type MUST be
2016 * network.
2017 */
2018 static int setup_relayd(struct ltt_session *session)
2019 {
2020 int ret = LTTCOMM_OK;
2021 struct ltt_ust_session *usess;
2022 struct ltt_kernel_session *ksess;
2023
2024 assert(session);
2025
2026 usess = session->ust_session;
2027 ksess = session->kernel_session;
2028
2029 DBG2("Setting relayd for session %s", session->name);
2030
2031 if (usess && usess->consumer->sock == -1 &&
2032 usess->consumer->type == CONSUMER_DST_NET &&
2033 usess->consumer->enabled) {
2034 /* Setup relayd for 64 bits consumer */
2035 if (ust_consumerd64_fd >= 0) {
2036 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
2037 usess->consumer, ust_consumerd64_fd);
2038 if (ret != LTTCOMM_OK) {
2039 goto error;
2040 }
2041 }
2042
2043 /* Setup relayd for 32 bits consumer */
2044 if (ust_consumerd32_fd >= 0) {
2045 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
2046 usess->consumer, ust_consumerd32_fd);
2047 if (ret != LTTCOMM_OK) {
2048 goto error;
2049 }
2050 }
2051 } else if (ksess && ksess->consumer->sock == -1 &&
2052 ksess->consumer->type == CONSUMER_DST_NET &&
2053 ksess->consumer->enabled) {
2054 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session,
2055 ksess->consumer, ksess->consumer_fd);
2056 if (ret != LTTCOMM_OK) {
2057 goto error;
2058 }
2059 }
2060
2061 error:
2062 return ret;
2063 }
2064
2065 /*
2066 * Copy consumer output from the tracing session to the domain session. The
2067 * function also applies the right modification on a per domain basis for the
2068 * trace files destination directory.
2069 */
2070 static int copy_session_consumer(int domain, struct ltt_session *session)
2071 {
2072 int ret;
2073 const char *dir_name;
2074 struct consumer_output *consumer;
2075
2076 switch (domain) {
2077 case LTTNG_DOMAIN_KERNEL:
2078 DBG3("Copying tracing session consumer output in kernel session");
2079 session->kernel_session->consumer =
2080 consumer_copy_output(session->consumer);
2081 /* Ease our life a bit for the next part */
2082 consumer = session->kernel_session->consumer;
2083 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2084 break;
2085 case LTTNG_DOMAIN_UST:
2086 DBG3("Copying tracing session consumer output in UST session");
2087 session->ust_session->consumer =
2088 consumer_copy_output(session->consumer);
2089 /* Ease our life a bit for the next part */
2090 consumer = session->ust_session->consumer;
2091 dir_name = DEFAULT_UST_TRACE_DIR;
2092 break;
2093 default:
2094 ret = LTTCOMM_UNKNOWN_DOMAIN;
2095 goto error;
2096 }
2097
2098 /* Append correct directory to subdir */
2099 strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
2100 DBG3("Copy session consumer subdir %s", consumer->subdir);
2101
2102 /* Add default trace directory name */
2103 if (consumer->type == CONSUMER_DST_LOCAL) {
2104 strncat(consumer->dst.trace_path, dir_name,
2105 sizeof(consumer->dst.trace_path));
2106 }
2107
2108 ret = LTTCOMM_OK;
2109
2110 error:
2111 return ret;
2112 }
2113
2114 /*
2115 * Create an UST session and add it to the session ust list.
2116 */
2117 static int create_ust_session(struct ltt_session *session,
2118 struct lttng_domain *domain)
2119 {
2120 int ret;
2121 struct ltt_ust_session *lus = NULL;
2122
2123 assert(session);
2124 assert(session->consumer);
2125
2126 switch (domain->type) {
2127 case LTTNG_DOMAIN_UST:
2128 break;
2129 default:
2130 ERR("Unknown UST domain on create session %d", domain->type);
2131 ret = LTTCOMM_UNKNOWN_DOMAIN;
2132 goto error;
2133 }
2134
2135 DBG("Creating UST session");
2136
2137 lus = trace_ust_create_session(session->path, session->id, domain);
2138 if (lus == NULL) {
2139 ret = LTTCOMM_UST_SESS_FAIL;
2140 goto error;
2141 }
2142
2143 if (session->consumer->type == CONSUMER_DST_LOCAL) {
2144 ret = run_as_mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
2145 session->uid, session->gid);
2146 if (ret < 0) {
2147 if (ret != -EEXIST) {
2148 ERR("Trace directory creation error");
2149 ret = LTTCOMM_UST_SESS_FAIL;
2150 goto error;
2151 }
2152 }
2153 }
2154
2155 lus->uid = session->uid;
2156 lus->gid = session->gid;
2157 session->ust_session = lus;
2158
2159 /* Copy session output to the newly created UST session */
2160 ret = copy_session_consumer(domain->type, session);
2161 if (ret != LTTCOMM_OK) {
2162 goto error;
2163 }
2164
2165 return LTTCOMM_OK;
2166
2167 error:
2168 free(lus);
2169 session->ust_session = NULL;
2170 return ret;
2171 }
2172
2173 /*
2174 * Create a kernel tracer session then create the default channel.
2175 */
2176 static int create_kernel_session(struct ltt_session *session)
2177 {
2178 int ret;
2179
2180 DBG("Creating kernel session");
2181
2182 ret = kernel_create_session(session, kernel_tracer_fd);
2183 if (ret < 0) {
2184 ret = LTTCOMM_KERN_SESS_FAIL;
2185 goto error;
2186 }
2187
2188 /* Set kernel consumer socket fd */
2189 if (kconsumer_data.cmd_sock >= 0) {
2190 session->kernel_session->consumer_fd = kconsumer_data.cmd_sock;
2191 }
2192
2193 /* Copy session output to the newly created Kernel session */
2194 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2195 if (ret != LTTCOMM_OK) {
2196 goto error;
2197 }
2198
2199 /* Create directory(ies) on local filesystem. */
2200 if (session->consumer->type == CONSUMER_DST_LOCAL) {
2201 ret = run_as_mkdir_recursive(
2202 session->kernel_session->consumer->dst.trace_path,
2203 S_IRWXU | S_IRWXG, session->uid, session->gid);
2204 if (ret < 0) {
2205 if (ret != -EEXIST) {
2206 ERR("Trace directory creation error");
2207 goto error;
2208 }
2209 }
2210 }
2211
2212 session->kernel_session->uid = session->uid;
2213 session->kernel_session->gid = session->gid;
2214
2215 return LTTCOMM_OK;
2216
2217 error:
2218 trace_kernel_destroy_session(session->kernel_session);
2219 session->kernel_session = NULL;
2220 return ret;
2221 }
2222
2223 /*
2224 * Check if the UID or GID match the session. Root user has access to all
2225 * sessions.
2226 */
2227 static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
2228 {
2229 if (uid != session->uid && gid != session->gid && uid != 0) {
2230 return 0;
2231 } else {
2232 return 1;
2233 }
2234 }
2235
2236 /*
2237 * Count number of session permitted by uid/gid.
2238 */
2239 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2240 {
2241 unsigned int i = 0;
2242 struct ltt_session *session;
2243
2244 DBG("Counting number of available session for UID %d GID %d",
2245 uid, gid);
2246 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2247 /*
2248 * Only list the sessions the user can control.
2249 */
2250 if (!session_access_ok(session, uid, gid)) {
2251 continue;
2252 }
2253 i++;
2254 }
2255 return i;
2256 }
2257
2258 /*
2259 * Using the session list, filled a lttng_session array to send back to the
2260 * client for session listing.
2261 *
2262 * The session list lock MUST be acquired before calling this function. Use
2263 * session_lock_list() and session_unlock_list().
2264 */
2265 static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2266 gid_t gid)
2267 {
2268 unsigned int i = 0;
2269 struct ltt_session *session;
2270
2271 DBG("Getting all available session for UID %d GID %d",
2272 uid, gid);
2273 /*
2274 * Iterate over session list and append data after the control struct in
2275 * the buffer.
2276 */
2277 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2278 /*
2279 * Only list the sessions the user can control.
2280 */
2281 if (!session_access_ok(session, uid, gid)) {
2282 continue;
2283 }
2284 strncpy(sessions[i].path, session->path, PATH_MAX);
2285 sessions[i].path[PATH_MAX - 1] = '\0';
2286 strncpy(sessions[i].name, session->name, NAME_MAX);
2287 sessions[i].name[NAME_MAX - 1] = '\0';
2288 sessions[i].enabled = session->enabled;
2289 i++;
2290 }
2291 }
2292
2293 /*
2294 * Fill lttng_channel array of all channels.
2295 */
2296 static void list_lttng_channels(int domain, struct ltt_session *session,
2297 struct lttng_channel *channels)
2298 {
2299 int i = 0;
2300 struct ltt_kernel_channel *kchan;
2301
2302 DBG("Listing channels for session %s", session->name);
2303
2304 switch (domain) {
2305 case LTTNG_DOMAIN_KERNEL:
2306 /* Kernel channels */
2307 if (session->kernel_session != NULL) {
2308 cds_list_for_each_entry(kchan,
2309 &session->kernel_session->channel_list.head, list) {
2310 /* Copy lttng_channel struct to array */
2311 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
2312 channels[i].enabled = kchan->enabled;
2313 i++;
2314 }
2315 }
2316 break;
2317 case LTTNG_DOMAIN_UST:
2318 {
2319 struct lttng_ht_iter iter;
2320 struct ltt_ust_channel *uchan;
2321
2322 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
2323 &iter.iter, uchan, node.node) {
2324 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
2325 channels[i].attr.overwrite = uchan->attr.overwrite;
2326 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
2327 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
2328 channels[i].attr.switch_timer_interval =
2329 uchan->attr.switch_timer_interval;
2330 channels[i].attr.read_timer_interval =
2331 uchan->attr.read_timer_interval;
2332 channels[i].enabled = uchan->enabled;
2333 switch (uchan->attr.output) {
2334 case LTTNG_UST_MMAP:
2335 default:
2336 channels[i].attr.output = LTTNG_EVENT_MMAP;
2337 break;
2338 }
2339 i++;
2340 }
2341 break;
2342 }
2343 default:
2344 break;
2345 }
2346 }
2347
2348 /*
2349 * Create a list of ust global domain events.
2350 */
2351 static int list_lttng_ust_global_events(char *channel_name,
2352 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
2353 {
2354 int i = 0, ret = 0;
2355 unsigned int nb_event = 0;
2356 struct lttng_ht_iter iter;
2357 struct lttng_ht_node_str *node;
2358 struct ltt_ust_channel *uchan;
2359 struct ltt_ust_event *uevent;
2360 struct lttng_event *tmp;
2361
2362 DBG("Listing UST global events for channel %s", channel_name);
2363
2364 rcu_read_lock();
2365
2366 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
2367 node = lttng_ht_iter_get_node_str(&iter);
2368 if (node == NULL) {
2369 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
2370 goto error;
2371 }
2372
2373 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
2374
2375 nb_event += lttng_ht_get_count(uchan->events);
2376
2377 if (nb_event == 0) {
2378 ret = nb_event;
2379 goto error;
2380 }
2381
2382 DBG3("Listing UST global %d events", nb_event);
2383
2384 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
2385 if (tmp == NULL) {
2386 ret = -LTTCOMM_FATAL;
2387 goto error;
2388 }
2389
2390 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
2391 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
2392 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2393 tmp[i].enabled = uevent->enabled;
2394 switch (uevent->attr.instrumentation) {
2395 case LTTNG_UST_TRACEPOINT:
2396 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
2397 break;
2398 case LTTNG_UST_PROBE:
2399 tmp[i].type = LTTNG_EVENT_PROBE;
2400 break;
2401 case LTTNG_UST_FUNCTION:
2402 tmp[i].type = LTTNG_EVENT_FUNCTION;
2403 break;
2404 }
2405 tmp[i].loglevel = uevent->attr.loglevel;
2406 switch (uevent->attr.loglevel_type) {
2407 case LTTNG_UST_LOGLEVEL_ALL:
2408 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2409 break;
2410 case LTTNG_UST_LOGLEVEL_RANGE:
2411 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
2412 break;
2413 case LTTNG_UST_LOGLEVEL_SINGLE:
2414 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
2415 break;
2416 }
2417 if (uevent->filter) {
2418 tmp[i].filter = 1;
2419 }
2420 i++;
2421 }
2422
2423 ret = nb_event;
2424 *events = tmp;
2425
2426 error:
2427 rcu_read_unlock();
2428 return ret;
2429 }
2430
2431 /*
2432 * Fill lttng_event array of all kernel events in the channel.
2433 */
2434 static int list_lttng_kernel_events(char *channel_name,
2435 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
2436 {
2437 int i = 0, ret;
2438 unsigned int nb_event;
2439 struct ltt_kernel_event *event;
2440 struct ltt_kernel_channel *kchan;
2441
2442 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
2443 if (kchan == NULL) {
2444 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2445 goto error;
2446 }
2447
2448 nb_event = kchan->event_count;
2449
2450 DBG("Listing events for channel %s", kchan->channel->name);
2451
2452 if (nb_event == 0) {
2453 ret = nb_event;
2454 goto error;
2455 }
2456
2457 *events = zmalloc(nb_event * sizeof(struct lttng_event));
2458 if (*events == NULL) {
2459 ret = LTTCOMM_FATAL;
2460 goto error;
2461 }
2462
2463 /* Kernel channels */
2464 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
2465 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
2466 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2467 (*events)[i].enabled = event->enabled;
2468 switch (event->event->instrumentation) {
2469 case LTTNG_KERNEL_TRACEPOINT:
2470 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
2471 break;
2472 case LTTNG_KERNEL_KPROBE:
2473 case LTTNG_KERNEL_KRETPROBE:
2474 (*events)[i].type = LTTNG_EVENT_PROBE;
2475 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
2476 sizeof(struct lttng_kernel_kprobe));
2477 break;
2478 case LTTNG_KERNEL_FUNCTION:
2479 (*events)[i].type = LTTNG_EVENT_FUNCTION;
2480 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
2481 sizeof(struct lttng_kernel_function));
2482 break;
2483 case LTTNG_KERNEL_NOOP:
2484 (*events)[i].type = LTTNG_EVENT_NOOP;
2485 break;
2486 case LTTNG_KERNEL_SYSCALL:
2487 (*events)[i].type = LTTNG_EVENT_SYSCALL;
2488 break;
2489 case LTTNG_KERNEL_ALL:
2490 assert(0);
2491 break;
2492 }
2493 i++;
2494 }
2495
2496 return nb_event;
2497
2498 error:
2499 return ret;
2500 }
2501
2502 /*
2503 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2504 */
2505 static int cmd_disable_channel(struct ltt_session *session,
2506 int domain, char *channel_name)
2507 {
2508 int ret;
2509 struct ltt_ust_session *usess;
2510
2511 usess = session->ust_session;
2512
2513 switch (domain) {
2514 case LTTNG_DOMAIN_KERNEL:
2515 {
2516 ret = channel_kernel_disable(session->kernel_session,
2517 channel_name);
2518 if (ret != LTTCOMM_OK) {
2519 goto error;
2520 }
2521
2522 kernel_wait_quiescent(kernel_tracer_fd);
2523 break;
2524 }
2525 case LTTNG_DOMAIN_UST:
2526 {
2527 struct ltt_ust_channel *uchan;
2528 struct lttng_ht *chan_ht;
2529
2530 chan_ht = usess->domain_global.channels;
2531
2532 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
2533 if (uchan == NULL) {
2534 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2535 goto error;
2536 }
2537
2538 ret = channel_ust_disable(usess, domain, uchan);
2539 if (ret != LTTCOMM_OK) {
2540 goto error;
2541 }
2542 break;
2543 }
2544 #if 0
2545 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2546 case LTTNG_DOMAIN_UST_EXEC_NAME:
2547 case LTTNG_DOMAIN_UST_PID:
2548 #endif
2549 default:
2550 ret = LTTCOMM_UNKNOWN_DOMAIN;
2551 goto error;
2552 }
2553
2554 ret = LTTCOMM_OK;
2555
2556 error:
2557 return ret;
2558 }
2559
2560 /*
2561 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2562 */
2563 static int cmd_enable_channel(struct ltt_session *session,
2564 int domain, struct lttng_channel *attr)
2565 {
2566 int ret;
2567 struct ltt_ust_session *usess = session->ust_session;
2568 struct lttng_ht *chan_ht;
2569
2570 DBG("Enabling channel %s for session %s", attr->name, session->name);
2571
2572 switch (domain) {
2573 case LTTNG_DOMAIN_KERNEL:
2574 {
2575 struct ltt_kernel_channel *kchan;
2576
2577 kchan = trace_kernel_get_channel_by_name(attr->name,
2578 session->kernel_session);
2579 if (kchan == NULL) {
2580 ret = channel_kernel_create(session->kernel_session,
2581 attr, kernel_poll_pipe[1]);
2582 } else {
2583 ret = channel_kernel_enable(session->kernel_session, kchan);
2584 }
2585
2586 if (ret != LTTCOMM_OK) {
2587 goto error;
2588 }
2589
2590 kernel_wait_quiescent(kernel_tracer_fd);
2591 break;
2592 }
2593 case LTTNG_DOMAIN_UST:
2594 {
2595 struct ltt_ust_channel *uchan;
2596
2597 chan_ht = usess->domain_global.channels;
2598
2599 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
2600 if (uchan == NULL) {
2601 ret = channel_ust_create(usess, domain, attr);
2602 } else {
2603 ret = channel_ust_enable(usess, domain, uchan);
2604 }
2605 break;
2606 }
2607 #if 0
2608 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2609 case LTTNG_DOMAIN_UST_EXEC_NAME:
2610 case LTTNG_DOMAIN_UST_PID:
2611 #endif
2612 default:
2613 ret = LTTCOMM_UNKNOWN_DOMAIN;
2614 goto error;
2615 }
2616
2617 error:
2618 return ret;
2619 }
2620
2621 /*
2622 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2623 */
2624 static int cmd_disable_event(struct ltt_session *session, int domain,
2625 char *channel_name, char *event_name)
2626 {
2627 int ret;
2628
2629 switch (domain) {
2630 case LTTNG_DOMAIN_KERNEL:
2631 {
2632 struct ltt_kernel_channel *kchan;
2633 struct ltt_kernel_session *ksess;
2634
2635 ksess = session->kernel_session;
2636
2637 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
2638 if (kchan == NULL) {
2639 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2640 goto error;
2641 }
2642
2643 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
2644 if (ret != LTTCOMM_OK) {
2645 goto error;
2646 }
2647
2648 kernel_wait_quiescent(kernel_tracer_fd);
2649 break;
2650 }
2651 case LTTNG_DOMAIN_UST:
2652 {
2653 struct ltt_ust_channel *uchan;
2654 struct ltt_ust_session *usess;
2655
2656 usess = session->ust_session;
2657
2658 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2659 channel_name);
2660 if (uchan == NULL) {
2661 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2662 goto error;
2663 }
2664
2665 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
2666 if (ret != LTTCOMM_OK) {
2667 goto error;
2668 }
2669
2670 DBG3("Disable UST event %s in channel %s completed", event_name,
2671 channel_name);
2672 break;
2673 }
2674 #if 0
2675 case LTTNG_DOMAIN_UST_EXEC_NAME:
2676 case LTTNG_DOMAIN_UST_PID:
2677 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2678 #endif
2679 default:
2680 ret = LTTCOMM_UND;
2681 goto error;
2682 }
2683
2684 ret = LTTCOMM_OK;
2685
2686 error:
2687 return ret;
2688 }
2689
2690 /*
2691 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2692 */
2693 static int cmd_disable_event_all(struct ltt_session *session, int domain,
2694 char *channel_name)
2695 {
2696 int ret;
2697
2698 switch (domain) {
2699 case LTTNG_DOMAIN_KERNEL:
2700 {
2701 struct ltt_kernel_session *ksess;
2702 struct ltt_kernel_channel *kchan;
2703
2704 ksess = session->kernel_session;
2705
2706 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
2707 if (kchan == NULL) {
2708 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2709 goto error;
2710 }
2711
2712 ret = event_kernel_disable_all(ksess, kchan);
2713 if (ret != LTTCOMM_OK) {
2714 goto error;
2715 }
2716
2717 kernel_wait_quiescent(kernel_tracer_fd);
2718 break;
2719 }
2720 case LTTNG_DOMAIN_UST:
2721 {
2722 struct ltt_ust_session *usess;
2723 struct ltt_ust_channel *uchan;
2724
2725 usess = session->ust_session;
2726
2727 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2728 channel_name);
2729 if (uchan == NULL) {
2730 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2731 goto error;
2732 }
2733
2734 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
2735 if (ret != 0) {
2736 goto error;
2737 }
2738
2739 DBG3("Disable all UST events in channel %s completed", channel_name);
2740
2741 break;
2742 }
2743 #if 0
2744 case LTTNG_DOMAIN_UST_EXEC_NAME:
2745 case LTTNG_DOMAIN_UST_PID:
2746 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2747 #endif
2748 default:
2749 ret = LTTCOMM_UND;
2750 goto error;
2751 }
2752
2753 ret = LTTCOMM_OK;
2754
2755 error:
2756 return ret;
2757 }
2758
2759 /*
2760 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2761 */
2762 static int cmd_add_context(struct ltt_session *session, int domain,
2763 char *channel_name, char *event_name, struct lttng_event_context *ctx)
2764 {
2765 int ret;
2766
2767 switch (domain) {
2768 case LTTNG_DOMAIN_KERNEL:
2769 /* Add kernel context to kernel tracer */
2770 ret = context_kernel_add(session->kernel_session, ctx,
2771 event_name, channel_name);
2772 if (ret != LTTCOMM_OK) {
2773 goto error;
2774 }
2775 break;
2776 case LTTNG_DOMAIN_UST:
2777 {
2778 struct ltt_ust_session *usess = session->ust_session;
2779
2780 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
2781 if (ret != LTTCOMM_OK) {
2782 goto error;
2783 }
2784 break;
2785 }
2786 #if 0
2787 case LTTNG_DOMAIN_UST_EXEC_NAME:
2788 case LTTNG_DOMAIN_UST_PID:
2789 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2790 #endif
2791 default:
2792 ret = LTTCOMM_UND;
2793 goto error;
2794 }
2795
2796 ret = LTTCOMM_OK;
2797
2798 error:
2799 return ret;
2800 }
2801
2802 /*
2803 * Command LTTNG_SET_FILTER processed by the client thread.
2804 */
2805 static int cmd_set_filter(struct ltt_session *session, int domain,
2806 char *channel_name, char *event_name,
2807 struct lttng_filter_bytecode *bytecode)
2808 {
2809 int ret;
2810
2811 switch (domain) {
2812 case LTTNG_DOMAIN_KERNEL:
2813 ret = LTTCOMM_FATAL;
2814 break;
2815 case LTTNG_DOMAIN_UST:
2816 {
2817 struct ltt_ust_session *usess = session->ust_session;
2818
2819 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
2820 if (ret != LTTCOMM_OK) {
2821 goto error;
2822 }
2823 break;
2824 }
2825 #if 0
2826 case LTTNG_DOMAIN_UST_EXEC_NAME:
2827 case LTTNG_DOMAIN_UST_PID:
2828 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2829 #endif
2830 default:
2831 ret = LTTCOMM_UND;
2832 goto error;
2833 }
2834
2835 ret = LTTCOMM_OK;
2836
2837 error:
2838 return ret;
2839
2840 }
2841
2842 /*
2843 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2844 */
2845 static int cmd_enable_event(struct ltt_session *session, int domain,
2846 char *channel_name, struct lttng_event *event)
2847 {
2848 int ret;
2849 struct lttng_channel *attr;
2850 struct ltt_ust_session *usess = session->ust_session;
2851
2852 switch (domain) {
2853 case LTTNG_DOMAIN_KERNEL:
2854 {
2855 struct ltt_kernel_channel *kchan;
2856
2857 kchan = trace_kernel_get_channel_by_name(channel_name,
2858 session->kernel_session);
2859 if (kchan == NULL) {
2860 attr = channel_new_default_attr(domain);
2861 if (attr == NULL) {
2862 ret = LTTCOMM_FATAL;
2863 goto error;
2864 }
2865 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2866
2867 /* This call will notify the kernel thread */
2868 ret = channel_kernel_create(session->kernel_session,
2869 attr, kernel_poll_pipe[1]);
2870 if (ret != LTTCOMM_OK) {
2871 free(attr);
2872 goto error;
2873 }
2874 free(attr);
2875 }
2876
2877 /* Get the newly created kernel channel pointer */
2878 kchan = trace_kernel_get_channel_by_name(channel_name,
2879 session->kernel_session);
2880 if (kchan == NULL) {
2881 /* This sould not happen... */
2882 ret = LTTCOMM_FATAL;
2883 goto error;
2884 }
2885
2886 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
2887 event);
2888 if (ret != LTTCOMM_OK) {
2889 goto error;
2890 }
2891
2892 kernel_wait_quiescent(kernel_tracer_fd);
2893 break;
2894 }
2895 case LTTNG_DOMAIN_UST:
2896 {
2897 struct lttng_channel *attr;
2898 struct ltt_ust_channel *uchan;
2899
2900 /* Get channel from global UST domain */
2901 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2902 channel_name);
2903 if (uchan == NULL) {
2904 /* Create default channel */
2905 attr = channel_new_default_attr(domain);
2906 if (attr == NULL) {
2907 ret = LTTCOMM_FATAL;
2908 goto error;
2909 }
2910 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2911 attr->name[NAME_MAX - 1] = '\0';
2912
2913 ret = channel_ust_create(usess, domain, attr);
2914 if (ret != LTTCOMM_OK) {
2915 free(attr);
2916 goto error;
2917 }
2918 free(attr);
2919
2920 /* Get the newly created channel reference back */
2921 uchan = trace_ust_find_channel_by_name(
2922 usess->domain_global.channels, channel_name);
2923 if (uchan == NULL) {
2924 /* Something is really wrong */
2925 ret = LTTCOMM_FATAL;
2926 goto error;
2927 }
2928 }
2929
2930 /* At this point, the session and channel exist on the tracer */
2931 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
2932 if (ret != LTTCOMM_OK) {
2933 goto error;
2934 }
2935 break;
2936 }
2937 #if 0
2938 case LTTNG_DOMAIN_UST_EXEC_NAME:
2939 case LTTNG_DOMAIN_UST_PID:
2940 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2941 #endif
2942 default:
2943 ret = LTTCOMM_UND;
2944 goto error;
2945 }
2946
2947 ret = LTTCOMM_OK;
2948
2949 error:
2950 return ret;
2951 }
2952
2953 /*
2954 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2955 */
2956 static int cmd_enable_event_all(struct ltt_session *session, int domain,
2957 char *channel_name, int event_type)
2958 {
2959 int ret;
2960 struct ltt_kernel_channel *kchan;
2961
2962 switch (domain) {
2963 case LTTNG_DOMAIN_KERNEL:
2964 kchan = trace_kernel_get_channel_by_name(channel_name,
2965 session->kernel_session);
2966 if (kchan == NULL) {
2967 /* This call will notify the kernel thread */
2968 ret = channel_kernel_create(session->kernel_session, NULL,
2969 kernel_poll_pipe[1]);
2970 if (ret != LTTCOMM_OK) {
2971 goto error;
2972 }
2973
2974 /* Get the newly created kernel channel pointer */
2975 kchan = trace_kernel_get_channel_by_name(channel_name,
2976 session->kernel_session);
2977 if (kchan == NULL) {
2978 /* This sould not happen... */
2979 ret = LTTCOMM_FATAL;
2980 goto error;
2981 }
2982
2983 }
2984
2985 switch (event_type) {
2986 case LTTNG_EVENT_SYSCALL:
2987 ret = event_kernel_enable_all_syscalls(session->kernel_session,
2988 kchan, kernel_tracer_fd);
2989 break;
2990 case LTTNG_EVENT_TRACEPOINT:
2991 /*
2992 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2993 * events already registered to the channel.
2994 */
2995 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
2996 kchan, kernel_tracer_fd);
2997 break;
2998 case LTTNG_EVENT_ALL:
2999 /* Enable syscalls and tracepoints */
3000 ret = event_kernel_enable_all(session->kernel_session,
3001 kchan, kernel_tracer_fd);
3002 break;
3003 default:
3004 ret = LTTCOMM_KERN_ENABLE_FAIL;
3005 goto error;
3006 }
3007
3008 /* Manage return value */
3009 if (ret != LTTCOMM_OK) {
3010 goto error;
3011 }
3012
3013 kernel_wait_quiescent(kernel_tracer_fd);
3014 break;
3015 case LTTNG_DOMAIN_UST:
3016 {
3017 struct lttng_channel *attr;
3018 struct ltt_ust_channel *uchan;
3019 struct ltt_ust_session *usess = session->ust_session;
3020
3021 /* Get channel from global UST domain */
3022 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3023 channel_name);
3024 if (uchan == NULL) {
3025 /* Create default channel */
3026 attr = channel_new_default_attr(domain);
3027 if (attr == NULL) {
3028 ret = LTTCOMM_FATAL;
3029 goto error;
3030 }
3031 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3032 attr->name[NAME_MAX - 1] = '\0';
3033
3034 /* Use the internal command enable channel */
3035 ret = channel_ust_create(usess, domain, attr);
3036 if (ret != LTTCOMM_OK) {
3037 free(attr);
3038 goto error;
3039 }
3040 free(attr);
3041
3042 /* Get the newly created channel reference back */
3043 uchan = trace_ust_find_channel_by_name(
3044 usess->domain_global.channels, channel_name);
3045 if (uchan == NULL) {
3046 /* Something is really wrong */
3047 ret = LTTCOMM_FATAL;
3048 goto error;
3049 }
3050 }
3051
3052 /* At this point, the session and channel exist on the tracer */
3053
3054 switch (event_type) {
3055 case LTTNG_EVENT_ALL:
3056 case LTTNG_EVENT_TRACEPOINT:
3057 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
3058 if (ret != LTTCOMM_OK) {
3059 goto error;
3060 }
3061 break;
3062 default:
3063 ret = LTTCOMM_UST_ENABLE_FAIL;
3064 goto error;
3065 }
3066
3067 /* Manage return value */
3068 if (ret != LTTCOMM_OK) {
3069 goto error;
3070 }
3071
3072 break;
3073 }
3074 #if 0
3075 case LTTNG_DOMAIN_UST_EXEC_NAME:
3076 case LTTNG_DOMAIN_UST_PID:
3077 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
3078 #endif
3079 default:
3080 ret = LTTCOMM_UND;
3081 goto error;
3082 }
3083
3084 ret = LTTCOMM_OK;
3085
3086 error:
3087 return ret;
3088 }
3089
3090 /*
3091 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3092 */
3093 static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
3094 {
3095 int ret;
3096 ssize_t nb_events = 0;
3097
3098 switch (domain) {
3099 case LTTNG_DOMAIN_KERNEL:
3100 nb_events = kernel_list_events(kernel_tracer_fd, events);
3101 if (nb_events < 0) {
3102 ret = LTTCOMM_KERN_LIST_FAIL;
3103 goto error;
3104 }
3105 break;
3106 case LTTNG_DOMAIN_UST:
3107 nb_events = ust_app_list_events(events);
3108 if (nb_events < 0) {
3109 ret = LTTCOMM_UST_LIST_FAIL;
3110 goto error;
3111 }
3112 break;
3113 default:
3114 ret = LTTCOMM_UND;
3115 goto error;
3116 }
3117
3118 return nb_events;
3119
3120 error:
3121 /* Return negative value to differentiate return code */
3122 return -ret;
3123 }
3124
3125 /*
3126 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3127 */
3128 static ssize_t cmd_list_tracepoint_fields(int domain,
3129 struct lttng_event_field **fields)
3130 {
3131 int ret;
3132 ssize_t nb_fields = 0;
3133
3134 switch (domain) {
3135 case LTTNG_DOMAIN_UST:
3136 nb_fields = ust_app_list_event_fields(fields);
3137 if (nb_fields < 0) {
3138 ret = LTTCOMM_UST_LIST_FAIL;
3139 goto error;
3140 }
3141 break;
3142 case LTTNG_DOMAIN_KERNEL:
3143 default: /* fall-through */
3144 ret = LTTCOMM_UND;
3145 goto error;
3146 }
3147
3148 return nb_fields;
3149
3150 error:
3151 /* Return negative value to differentiate return code */
3152 return -ret;
3153 }
3154
3155 /*
3156 * Command LTTNG_START_TRACE processed by the client thread.
3157 */
3158 static int cmd_start_trace(struct ltt_session *session)
3159 {
3160 int ret;
3161 struct ltt_kernel_session *ksession;
3162 struct ltt_ust_session *usess;
3163 struct ltt_kernel_channel *kchan;
3164
3165 /* Ease our life a bit ;) */
3166 ksession = session->kernel_session;
3167 usess = session->ust_session;
3168
3169 if (session->enabled) {
3170 /* Already started. */
3171 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3172 goto error;
3173 }
3174
3175 session->enabled = 1;
3176
3177 ret = setup_relayd(session);
3178 if (ret != LTTCOMM_OK) {
3179 ERR("Error setting up relayd for session %s", session->name);
3180 goto error;
3181 }
3182
3183 /* Kernel tracing */
3184 if (ksession != NULL) {
3185 /* Open kernel metadata */
3186 if (ksession->metadata == NULL) {
3187 ret = kernel_open_metadata(ksession,
3188 ksession->consumer->dst.trace_path);
3189 if (ret < 0) {
3190 ret = LTTCOMM_KERN_META_FAIL;
3191 goto error;
3192 }
3193 }
3194
3195 /* Open kernel metadata stream */
3196 if (ksession->metadata_stream_fd < 0) {
3197 ret = kernel_open_metadata_stream(ksession);
3198 if (ret < 0) {
3199 ERR("Kernel create metadata stream failed");
3200 ret = LTTCOMM_KERN_STREAM_FAIL;
3201 goto error;
3202 }
3203 }
3204
3205 /* For each channel */
3206 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3207 if (kchan->stream_count == 0) {
3208 ret = kernel_open_channel_stream(kchan);
3209 if (ret < 0) {
3210 ret = LTTCOMM_KERN_STREAM_FAIL;
3211 goto error;
3212 }
3213 /* Update the stream global counter */
3214 ksession->stream_count_global += ret;
3215 }
3216 }
3217
3218 /* Setup kernel consumer socket and send fds to it */
3219 ret = init_kernel_tracing(ksession);
3220 if (ret < 0) {
3221 ret = LTTCOMM_KERN_START_FAIL;
3222 goto error;
3223 }
3224
3225 /* This start the kernel tracing */
3226 ret = kernel_start_session(ksession);
3227 if (ret < 0) {
3228 ret = LTTCOMM_KERN_START_FAIL;
3229 goto error;
3230 }
3231
3232 /* Quiescent wait after starting trace */
3233 kernel_wait_quiescent(kernel_tracer_fd);
3234 }
3235
3236 /* Flag session that trace should start automatically */
3237 if (usess) {
3238 usess->start_trace = 1;
3239
3240 ret = ust_app_start_trace_all(usess);
3241 if (ret < 0) {
3242 ret = LTTCOMM_UST_START_FAIL;
3243 goto error;
3244 }
3245 }
3246
3247 ret = LTTCOMM_OK;
3248
3249 error:
3250 return ret;
3251 }
3252
3253 /*
3254 * Command LTTNG_STOP_TRACE processed by the client thread.
3255 */
3256 static int cmd_stop_trace(struct ltt_session *session)
3257 {
3258 int ret;
3259 struct ltt_kernel_channel *kchan;
3260 struct ltt_kernel_session *ksession;
3261 struct ltt_ust_session *usess;
3262
3263 /* Short cut */
3264 ksession = session->kernel_session;
3265 usess = session->ust_session;
3266
3267 if (!session->enabled) {
3268 ret = LTTCOMM_TRACE_ALREADY_STOPPED;
3269 goto error;
3270 }
3271
3272 session->enabled = 0;
3273
3274 /* Kernel tracer */
3275