5664b0be1e86c46e4c2d4f4b1e42250fb30cee8e
[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 if (ksession != NULL) {
3276 DBG("Stop kernel tracing");
3277
3278 /* Flush metadata if exist */
3279 if (ksession->metadata_stream_fd >= 0) {
3280 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
3281 if (ret < 0) {
3282 ERR("Kernel metadata flush failed");
3283 }
3284 }
3285
3286 /* Flush all buffers before stopping */
3287 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3288 ret = kernel_flush_buffer(kchan);
3289 if (ret < 0) {
3290 ERR("Kernel flush buffer error");
3291 }
3292 }
3293
3294 ret = kernel_stop_session(ksession);
3295 if (ret < 0) {
3296 ret = LTTCOMM_KERN_STOP_FAIL;
3297 goto error;
3298 }
3299
3300 kernel_wait_quiescent(kernel_tracer_fd);
3301 }
3302
3303 if (usess) {
3304 usess->start_trace = 0;
3305
3306 ret = ust_app_stop_trace_all(usess);
3307 if (ret < 0) {
3308 ret = LTTCOMM_UST_STOP_FAIL;
3309 goto error;
3310 }
3311 }
3312
3313 ret = LTTCOMM_OK;
3314
3315 error:
3316 return ret;
3317 }
3318
3319 /*
3320 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3321 */
3322 static int cmd_create_session_uri(char *name, struct lttng_uri *ctrl_uri,
3323 struct lttng_uri *data_uri, unsigned int enable_consumer,
3324 lttng_sock_cred *creds)
3325 {
3326 int ret;
3327 char *path = NULL;
3328 struct ltt_session *session;
3329 struct consumer_output *consumer;
3330
3331 /* Verify if the session already exist */
3332 session = session_find_by_name(name);
3333 if (session != NULL) {
3334 ret = LTTCOMM_EXIST_SESS;
3335 goto error;
3336 }
3337
3338 /* TODO: validate URIs */
3339
3340 /* Create default consumer output */
3341 consumer = consumer_create_output(CONSUMER_DST_LOCAL);
3342 if (consumer == NULL) {
3343 ret = LTTCOMM_FATAL;
3344 goto error;
3345 }
3346 strncpy(consumer->subdir, ctrl_uri->subdir, sizeof(consumer->subdir));
3347 DBG2("Consumer subdir set to %s", consumer->subdir);
3348
3349 switch (ctrl_uri->dtype) {
3350 case LTTNG_DST_IPV4:
3351 case LTTNG_DST_IPV6:
3352 /* Set control URI into consumer output object */
3353 ret = consumer_set_network_uri(consumer, ctrl_uri);
3354 if (ret < 0) {
3355 ret = LTTCOMM_FATAL;
3356 goto error;
3357 }
3358
3359 /* Set data URI into consumer output object */
3360 ret = consumer_set_network_uri(consumer, data_uri);
3361 if (ret < 0) {
3362 ret = LTTCOMM_FATAL;
3363 goto error;
3364 }
3365
3366 /* Empty path since the session is network */
3367 path = "";
3368 break;
3369 case LTTNG_DST_PATH:
3370 /* Very volatile pointer. Only used for the create session. */
3371 path = ctrl_uri->dst.path;
3372 strncpy(consumer->dst.trace_path, path,
3373 sizeof(consumer->dst.trace_path));
3374 break;
3375 }
3376
3377 /* Set if the consumer is enabled or not */
3378 consumer->enabled = enable_consumer;
3379
3380 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
3381 LTTNG_SOCK_GET_GID_CRED(creds));
3382 if (ret != LTTCOMM_OK) {
3383 goto consumer_error;
3384 }
3385
3386 /* Get the newly created session pointer back */
3387 session = session_find_by_name(name);
3388 assert(session);
3389
3390 /* Assign consumer to session */
3391 session->consumer = consumer;
3392
3393 return LTTCOMM_OK;
3394
3395 consumer_error:
3396 consumer_destroy_output(consumer);
3397 error:
3398 return ret;
3399 }
3400
3401 /*
3402 * Command LTTNG_CREATE_SESSION processed by the client thread.
3403 */
3404 static int cmd_create_session(char *name, char *path, lttng_sock_cred *creds)
3405 {
3406 int ret;
3407 struct lttng_uri uri;
3408
3409 /* Zeroed temporary URI */
3410 memset(&uri, 0, sizeof(uri));
3411
3412 uri.dtype = LTTNG_DST_PATH;
3413 uri.utype = LTTNG_URI_DST;
3414 strncpy(uri.dst.path, path, sizeof(uri.dst.path));
3415
3416 /* TODO: Strip date-time from path and put it in uri's subdir */
3417
3418 ret = cmd_create_session_uri(name, &uri, NULL, 1, creds);
3419 if (ret != LTTCOMM_OK) {
3420 goto error;
3421 }
3422
3423 error:
3424 return ret;
3425 }
3426
3427 /*
3428 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3429 */
3430 static int cmd_destroy_session(struct ltt_session *session, char *name)
3431 {
3432 int ret;
3433
3434 /* Clean kernel session teardown */
3435 teardown_kernel_session(session);
3436 /* UST session teardown */
3437 teardown_ust_session(session);
3438
3439 /*
3440 * Must notify the kernel thread here to update it's poll setin order
3441 * to remove the channel(s)' fd just destroyed.
3442 */
3443 ret = notify_thread_pipe(kernel_poll_pipe[1]);
3444 if (ret < 0) {
3445 PERROR("write kernel poll pipe");
3446 }
3447
3448 ret = session_destroy(session);
3449
3450 return ret;
3451 }
3452
3453 /*
3454 * Command LTTNG_CALIBRATE processed by the client thread.
3455 */
3456 static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
3457 {
3458 int ret;
3459
3460 switch (domain) {
3461 case LTTNG_DOMAIN_KERNEL:
3462 {
3463 struct lttng_kernel_calibrate kcalibrate;
3464
3465 kcalibrate.type = calibrate->type;
3466 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
3467 if (ret < 0) {
3468 ret = LTTCOMM_KERN_ENABLE_FAIL;
3469 goto error;
3470 }
3471 break;
3472 }
3473 case LTTNG_DOMAIN_UST:
3474 {
3475 struct lttng_ust_calibrate ucalibrate;
3476
3477 ucalibrate.type = calibrate->type;
3478 ret = ust_app_calibrate_glb(&ucalibrate);
3479 if (ret < 0) {
3480 ret = LTTCOMM_UST_CALIBRATE_FAIL;
3481 goto error;
3482 }
3483 break;
3484 }
3485 default:
3486 ret = LTTCOMM_UND;
3487 goto error;
3488 }
3489
3490 ret = LTTCOMM_OK;
3491
3492 error:
3493 return ret;
3494 }
3495
3496 /*
3497 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3498 */
3499 static int cmd_register_consumer(struct ltt_session *session, int domain,
3500 char *sock_path)
3501 {
3502 int ret, sock;
3503
3504 switch (domain) {
3505 case LTTNG_DOMAIN_KERNEL:
3506 /* Can't register a consumer if there is already one */
3507 if (session->kernel_session->consumer_fds_sent != 0) {
3508 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3509 goto error;
3510 }
3511
3512 sock = lttcomm_connect_unix_sock(sock_path);
3513 if (sock < 0) {
3514 ret = LTTCOMM_CONNECT_FAIL;
3515 goto error;
3516 }
3517
3518 session->kernel_session->consumer_fd = sock;
3519 break;
3520 default:
3521 /* TODO: Userspace tracing */
3522 ret = LTTCOMM_UND;
3523 goto error;
3524 }
3525
3526 ret = LTTCOMM_OK;
3527
3528 error:
3529 return ret;
3530 }
3531
3532 /*
3533 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3534 */
3535 static ssize_t cmd_list_domains(struct ltt_session *session,
3536 struct lttng_domain **domains)
3537 {
3538 int ret, index = 0;
3539 ssize_t nb_dom = 0;
3540
3541 if (session->kernel_session != NULL) {
3542 DBG3("Listing domains found kernel domain");
3543 nb_dom++;
3544 }
3545
3546 if (session->ust_session != NULL) {
3547 DBG3("Listing domains found UST global domain");
3548 nb_dom++;
3549 }
3550
3551 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3552 if (*domains == NULL) {
3553 ret = -LTTCOMM_FATAL;
3554 goto error;
3555 }
3556
3557 if (session->kernel_session != NULL) {
3558 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3559 index++;
3560 }
3561
3562 if (session->ust_session != NULL) {
3563 (*domains)[index].type = LTTNG_DOMAIN_UST;
3564 index++;
3565 }
3566
3567 return nb_dom;
3568
3569 error:
3570 return ret;
3571 }
3572
3573 /*
3574 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3575 */
3576 static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
3577 struct lttng_channel **channels)
3578 {
3579 int ret;
3580 ssize_t nb_chan = 0;
3581
3582 switch (domain) {
3583 case LTTNG_DOMAIN_KERNEL:
3584 if (session->kernel_session != NULL) {
3585 nb_chan = session->kernel_session->channel_count;
3586 }
3587 DBG3("Number of kernel channels %zd", nb_chan);
3588 break;
3589 case LTTNG_DOMAIN_UST:
3590 if (session->ust_session != NULL) {
3591 nb_chan = lttng_ht_get_count(
3592 session->ust_session->domain_global.channels);
3593 }
3594 DBG3("Number of UST global channels %zd", nb_chan);
3595 break;
3596 default:
3597 *channels = NULL;
3598 ret = -LTTCOMM_UND;
3599 goto error;
3600 }
3601
3602 if (nb_chan > 0) {
3603 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
3604 if (*channels == NULL) {
3605 ret = -LTTCOMM_FATAL;
3606 goto error;
3607 }
3608
3609 list_lttng_channels(domain, session, *channels);
3610 } else {
3611 *channels = NULL;
3612 }
3613
3614 return nb_chan;
3615
3616 error:
3617 return ret;
3618 }
3619
3620 /*
3621 * Command LTTNG_LIST_EVENTS processed by the client thread.
3622 */
3623 static ssize_t cmd_list_events(int domain, struct ltt_session *session,
3624 char *channel_name, struct lttng_event **events)
3625 {
3626 int ret = 0;
3627 ssize_t nb_event = 0;
3628
3629 switch (domain) {
3630 case LTTNG_DOMAIN_KERNEL:
3631 if (session->kernel_session != NULL) {
3632 nb_event = list_lttng_kernel_events(channel_name,
3633 session->kernel_session, events);
3634 }
3635 break;
3636 case LTTNG_DOMAIN_UST:
3637 {
3638 if (session->ust_session != NULL) {
3639 nb_event = list_lttng_ust_global_events(channel_name,
3640 &session->ust_session->domain_global, events);
3641 }
3642 break;
3643 }
3644 default:
3645 ret = -LTTCOMM_UND;
3646 goto error;
3647 }
3648
3649 ret = nb_event;
3650
3651 error:
3652 return ret;
3653 }
3654
3655 /*
3656 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3657 */
3658 static int cmd_set_consumer_uri(int domain, struct ltt_session *session,
3659 struct lttng_uri *uri)
3660 {
3661 int ret;
3662 struct ltt_kernel_session *ksess = session->kernel_session;
3663 struct ltt_ust_session *usess = session->ust_session;
3664 struct consumer_output *consumer;
3665
3666 /* Can't enable consumer after session started. */
3667 if (session->enabled) {
3668 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3669 goto error;
3670 }
3671
3672 switch (domain) {
3673 case LTTNG_DOMAIN_KERNEL:
3674 /* Code flow error if we don't have a kernel session here. */
3675 assert(ksess);
3676
3677 /* Create consumer output if none exists */
3678 consumer = ksess->tmp_consumer;
3679 if (consumer == NULL) {
3680 consumer = consumer_copy_output(ksess->consumer);
3681 if (consumer == NULL) {
3682 ret = LTTCOMM_FATAL;
3683 goto error;
3684 }
3685 /* Reassign new pointer */
3686 ksess->tmp_consumer = consumer;
3687 }
3688
3689 switch (uri->dtype) {
3690 case LTTNG_DST_IPV4:
3691 case LTTNG_DST_IPV6:
3692 DBG2("Setting network URI for kernel session %s", session->name);
3693
3694 /* Set URI into consumer output object */
3695 ret = consumer_set_network_uri(consumer, uri);
3696 if (ret < 0) {
3697 ret = LTTCOMM_FATAL;
3698 goto error;
3699 }
3700
3701 /* On a new subdir, reappend the default trace dir. */
3702 if (strlen(uri->subdir) != 0) {
3703 strncat(consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
3704 sizeof(consumer->subdir));
3705 }
3706
3707 ret = send_socket_relayd_consumer(domain, session, uri, consumer,
3708 ksess->consumer_fd);
3709 if (ret != LTTCOMM_OK) {
3710 goto error;
3711 }
3712 break;
3713 case LTTNG_DST_PATH:
3714 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
3715 memset(consumer->dst.trace_path, 0,
3716 sizeof(consumer->dst.trace_path));
3717 strncpy(consumer->dst.trace_path, uri->dst.path,
3718 sizeof(consumer->dst.trace_path));
3719 /* Append default kernel trace dir */
3720 strncat(consumer->dst.trace_path, DEFAULT_KERNEL_TRACE_DIR,
3721 sizeof(consumer->dst.trace_path));
3722 break;
3723 }
3724
3725 /* All good! */
3726 break;
3727 case LTTNG_DOMAIN_UST:
3728 /* Code flow error if we don't have a kernel session here. */
3729 assert(usess);
3730
3731 /* Create consumer output if none exists */
3732 consumer = usess->tmp_consumer;
3733 if (consumer == NULL) {
3734 consumer = consumer_copy_output(usess->consumer);
3735 if (consumer == NULL) {
3736 ret = LTTCOMM_FATAL;
3737 goto error;
3738 }
3739 /* Reassign new pointer */
3740 usess->tmp_consumer = consumer;
3741 }
3742
3743 switch (uri->dtype) {
3744 case LTTNG_DST_IPV4:
3745 case LTTNG_DST_IPV6:
3746 {
3747 DBG2("Setting network URI for UST session %s", session->name);
3748
3749 /* Set URI into consumer object */
3750 ret = consumer_set_network_uri(consumer, uri);
3751 if (ret < 0) {
3752 ret = LTTCOMM_FATAL;
3753 goto error;
3754 }
3755
3756 /* On a new subdir, reappend the default trace dir. */
3757 if (strlen(uri->subdir) != 0) {
3758 strncat(consumer->subdir, DEFAULT_UST_TRACE_DIR,
3759 sizeof(consumer->subdir));
3760 }
3761
3762 if (ust_consumerd64_fd >= 0) {
3763 ret = send_socket_relayd_consumer(domain, session, uri,
3764 consumer, ust_consumerd64_fd);
3765 if (ret != LTTCOMM_OK) {
3766 goto error;
3767 }
3768 }
3769
3770 if (ust_consumerd32_fd >= 0) {
3771 ret = send_socket_relayd_consumer(domain, session, uri,
3772 consumer, ust_consumerd32_fd);
3773 if (ret != LTTCOMM_OK) {
3774 goto error;
3775 }
3776 }
3777
3778 break;
3779 }
3780 case LTTNG_DST_PATH:
3781 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
3782 memset(consumer->dst.trace_path, 0,
3783 sizeof(consumer->dst.trace_path));
3784 strncpy(consumer->dst.trace_path, uri->dst.path,
3785 sizeof(consumer->dst.trace_path));
3786 /* Append default UST trace dir */
3787 strncat(consumer->dst.trace_path, DEFAULT_UST_TRACE_DIR,
3788 sizeof(consumer->dst.trace_path));
3789 break;
3790 }
3791 break;
3792 }
3793
3794 /* All good! */
3795 ret = LTTCOMM_OK;
3796
3797 error:
3798 return ret;
3799 }
3800
3801 /*
3802 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3803 */
3804 static int cmd_disable_consumer(int domain, struct ltt_session *session)
3805 {
3806 int ret;
3807 struct ltt_kernel_session *ksess = session->kernel_session;
3808 struct ltt_ust_session *usess = session->ust_session;
3809 struct consumer_output *consumer;
3810
3811 if (session->enabled) {
3812 /* Can't disable consumer on an already started session */
3813 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3814 goto error;
3815 }
3816
3817 switch (domain) {
3818 case LTTNG_DOMAIN_KERNEL:
3819 /* Code flow error if we don't have a kernel session here. */
3820 assert(ksess);
3821
3822 DBG("Disabling kernel consumer");
3823 consumer = ksess->consumer;
3824
3825 break;
3826 case LTTNG_DOMAIN_UST:
3827 /* Code flow error if we don't have a UST session here. */
3828 assert(usess);
3829
3830 DBG("Disabling UST consumer");
3831 consumer = usess->consumer;
3832
3833 break;
3834 default:
3835 ret = LTTCOMM_UNKNOWN_DOMAIN;
3836 goto error;
3837 }
3838
3839 assert(consumer);
3840 consumer->enabled = 0;
3841
3842 /* Success at this point */
3843 ret = LTTCOMM_OK;
3844
3845 error:
3846 return ret;
3847 }
3848
3849 /*
3850 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3851 */
3852 static int cmd_enable_consumer(int domain, struct ltt_session *session)
3853 {
3854 int ret;
3855 struct ltt_kernel_session *ksess = session->kernel_session;
3856 struct ltt_ust_session *usess = session->ust_session;
3857 struct consumer_output *tmp_out;
3858
3859 /* Can't enable consumer after session started. */
3860 if (session->enabled) {
3861 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3862 goto error;
3863 }
3864
3865 switch (domain) {
3866 case LTTNG_DOMAIN_KERNEL:
3867 /* Code flow error if we don't have a kernel session here. */
3868 assert(ksess);
3869
3870 /*
3871 * Check if we have already sent fds to the consumer. In that case,
3872 * the enable-consumer command can't be used because a start trace
3873 * had previously occured.
3874 */
3875 if (ksess->consumer_fds_sent) {
3876 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3877 goto error;
3878 }
3879
3880 tmp_out = ksess->tmp_consumer;
3881 if (tmp_out == NULL) {
3882 /* No temp. consumer output exists. Using the current one. */
3883 DBG3("No temporary consumer. Using default");
3884 ret = LTTCOMM_OK;
3885 goto error;
3886 }
3887
3888 switch (tmp_out->type) {
3889 case CONSUMER_DST_LOCAL:
3890 DBG2("Consumer output is local. Creating directory(ies)");
3891
3892 /* Create directory(ies) */
3893 ret = run_as_mkdir_recursive(tmp_out->dst.trace_path,
3894 S_IRWXU | S_IRWXG, session->uid, session->gid);
3895 if (ret < 0) {
3896 if (ret != -EEXIST) {
3897 ERR("Trace directory creation error");
3898 ret = LTTCOMM_FATAL;
3899 goto error;
3900 }
3901 }
3902 break;
3903 case CONSUMER_DST_NET:
3904 DBG2("Consumer output is network. Validating URIs");
3905 /* Validate if we have both control and data path set. */
3906 if (!tmp_out->dst.net.control_isset) {
3907 ret = LTTCOMM_URI_CTRL_MISS;
3908 goto error;
3909 }
3910
3911 if (!tmp_out->dst.net.data_isset) {
3912 ret = LTTCOMM_URI_DATA_MISS;
3913 goto error;
3914 }
3915
3916 /* Check established network session state */
3917 if (session->net_handle == 0) {
3918 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3919 ERR("Session network handle is not set on enable-consumer");
3920 goto error;
3921 }
3922
3923 /* Append default kernel trace dir to subdir */
3924 strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
3925 sizeof(ksess->consumer->subdir));
3926
3927 break;
3928 }
3929
3930 /*
3931 * @session-lock
3932 * This is race free for now since the session lock is acquired before
3933 * ending up in this function. No other threads can access this kernel
3934 * session without this lock hence freeing the consumer output object
3935 * is valid.
3936 */
3937 consumer_destroy_output(ksess->consumer);
3938 ksess->consumer = tmp_out;
3939 ksess->tmp_consumer = NULL;
3940
3941 break;
3942 case LTTNG_DOMAIN_UST:
3943 /* Code flow error if we don't have a UST session here. */
3944 assert(usess);
3945
3946 /*
3947 * Check if we have already sent fds to the consumer. In that case,
3948 * the enable-consumer command can't be used because a start trace
3949 * had previously occured.
3950 */
3951 if (usess->start_trace) {
3952 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3953 goto error;
3954 }
3955
3956 tmp_out = usess->tmp_consumer;
3957 if (tmp_out == NULL) {
3958 /* No temp. consumer output exists. Using the current one. */
3959 DBG3("No temporary consumer. Using default");
3960 ret = LTTCOMM_OK;
3961 goto error;
3962 }
3963
3964 switch (tmp_out->type) {
3965 case CONSUMER_DST_LOCAL:
3966 DBG2("Consumer output is local. Creating directory(ies)");
3967
3968 /* Create directory(ies) */
3969 ret = run_as_mkdir_recursive(tmp_out->dst.trace_path,
3970 S_IRWXU | S_IRWXG, session->uid, session->gid);
3971 if (ret < 0) {
3972 if (ret != -EEXIST) {
3973 ERR("Trace directory creation error");
3974 ret = LTTCOMM_FATAL;
3975 goto error;
3976 }
3977 }
3978 break;
3979 case CONSUMER_DST_NET:
3980 DBG2("Consumer output is network. Validating URIs");
3981 /* Validate if we have both control and data path set. */
3982 if (!tmp_out->dst.net.control_isset) {
3983 ret = LTTCOMM_URI_CTRL_MISS;
3984 goto error;
3985 }
3986
3987 if (!tmp_out->dst.net.data_isset) {
3988 ret = LTTCOMM_URI_DATA_MISS;
3989 goto error;
3990 }
3991
3992 /* Check established network session state */
3993 if (session->net_handle == 0) {
3994 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
3995 DBG2("Session network handle is not set on enable-consumer");
3996 goto error;
3997 }
3998
3999 if (tmp_out->net_seq_index == -1) {
4000 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4001 DBG2("Network index is not set on the consumer");
4002 goto error;
4003 }
4004
4005 /* Append default kernel trace dir to subdir */
4006 strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
4007 sizeof(usess->consumer->subdir));
4008
4009 break;
4010 }
4011
4012 /*
4013 * @session-lock
4014 * This is race free for now since the session lock is acquired before
4015 * ending up in this function. No other threads can access this kernel
4016 * session without this lock hence freeing the consumer output object
4017 * is valid.
4018 */
4019 consumer_destroy_output(usess->consumer);
4020 usess->consumer = tmp_out;
4021 usess->tmp_consumer = NULL;
4022
4023 break;
4024 }
4025
4026 /* Success at this point */
4027 ret = LTTCOMM_OK;
4028
4029 error:
4030 return ret;
4031 }
4032
4033 /*
4034 * Process the command requested by the lttng client within the command
4035 * context structure. This function make sure that the return structure (llm)
4036 * is set and ready for transmission before returning.
4037 *
4038 * Return any error encountered or 0 for success.
4039 *
4040 * "sock" is only used for special-case var. len data.
4041 */
4042 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
4043 int *sock_error)
4044 {
4045 int ret = LTTCOMM_OK;
4046 int need_tracing_session = 1;
4047 int need_domain;
4048
4049 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
4050
4051 *sock_error = 0;
4052
4053 switch (cmd_ctx->lsm->cmd_type) {
4054 case LTTNG_CREATE_SESSION:
4055 case LTTNG_CREATE_SESSION_URI:
4056 case LTTNG_DESTROY_SESSION:
4057 case LTTNG_LIST_SESSIONS:
4058 case LTTNG_LIST_DOMAINS:
4059 case LTTNG_START_TRACE:
4060 case LTTNG_STOP_TRACE:
4061 need_domain = 0;
4062 break;
4063 default:
4064 need_domain = 1;
4065 }
4066
4067 if (opt_no_kernel && need_domain
4068 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
4069 if (!is_root) {
4070 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4071 } else {
4072 ret = LTTCOMM_KERN_NA;
4073 }
4074 goto error;
4075 }
4076
4077 /*
4078 * Check for command that don't needs to allocate a returned payload. We do
4079 * this here so we don't have to make the call for no payload at each
4080 * command.
4081 */
4082 switch(cmd_ctx->lsm->cmd_type) {
4083 case LTTNG_LIST_SESSIONS:
4084 case LTTNG_LIST_TRACEPOINTS:
4085 case LTTNG_LIST_TRACEPOINT_FIELDS:
4086 case LTTNG_LIST_DOMAINS:
4087 case LTTNG_LIST_CHANNELS:
4088 case LTTNG_LIST_EVENTS:
4089 break;
4090 default:
4091 /* Setup lttng message with no payload */
4092 ret = setup_lttng_msg(cmd_ctx, 0);
4093 if (ret < 0) {
4094 /* This label does not try to unlock the session */
4095 goto init_setup_error;
4096 }
4097 }
4098
4099 /* Commands that DO NOT need a session. */
4100 switch (cmd_ctx->lsm->cmd_type) {
4101 case LTTNG_CREATE_SESSION:
4102 case LTTNG_CREATE_SESSION_URI:
4103 case LTTNG_CALIBRATE:
4104 case LTTNG_LIST_SESSIONS:
4105 case LTTNG_LIST_TRACEPOINTS:
4106 case LTTNG_LIST_TRACEPOINT_FIELDS:
4107 need_tracing_session = 0;
4108 break;
4109 default:
4110 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
4111 /*
4112 * We keep the session list lock across _all_ commands
4113 * for now, because the per-session lock does not
4114 * handle teardown properly.
4115 */
4116 session_lock_list();
4117 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
4118 if (cmd_ctx->session == NULL) {
4119 if (cmd_ctx->lsm->session.name != NULL) {
4120 ret = LTTCOMM_SESS_NOT_FOUND;
4121 } else {
4122 /* If no session name specified */
4123 ret = LTTCOMM_SELECT_SESS;
4124 }
4125 goto error;
4126 } else {
4127 /* Acquire lock for the session */
4128 session_lock(cmd_ctx->session);
4129 }
4130 break;
4131 }
4132
4133 if (!need_domain) {
4134 goto skip_domain;
4135 }
4136 /*
4137 * Check domain type for specific "pre-action".
4138 */
4139 switch (cmd_ctx->lsm->domain.type) {
4140 case LTTNG_DOMAIN_KERNEL:
4141 if (!is_root) {
4142 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4143 goto error;
4144 }
4145
4146 /* Kernel tracer check */
4147 if (kernel_tracer_fd == -1) {
4148 /* Basically, load kernel tracer modules */
4149 ret = init_kernel_tracer();
4150 if (ret != 0) {
4151 goto error;
4152 }
4153 }
4154
4155 /* Consumer is in an ERROR state. Report back to client */
4156 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
4157 ret = LTTCOMM_NO_KERNCONSUMERD;
4158 goto error;
4159 }
4160
4161 /* Need a session for kernel command */
4162 if (need_tracing_session) {
4163 if (cmd_ctx->session->kernel_session == NULL) {
4164 ret = create_kernel_session(cmd_ctx->session);
4165 if (ret < 0) {
4166 ret = LTTCOMM_KERN_SESS_FAIL;
4167 goto error;
4168 }
4169 }
4170
4171 /* Start the kernel consumer daemon */
4172 pthread_mutex_lock(&kconsumer_data.pid_mutex);
4173 if (kconsumer_data.pid == 0 &&
4174 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4175 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4176 ret = start_consumerd(&kconsumer_data);
4177 if (ret < 0) {
4178 ret = LTTCOMM_KERN_CONSUMER_FAIL;
4179 goto error;
4180 }
4181 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
4182
4183 /* Set consumer fd of the session */
4184 cmd_ctx->session->kernel_session->consumer_fd =
4185 kconsumer_data.cmd_sock;
4186 } else {
4187 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4188 }
4189 }
4190
4191 break;
4192 case LTTNG_DOMAIN_UST:
4193 {
4194 /* Consumer is in an ERROR state. Report back to client */
4195 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
4196 ret = LTTCOMM_NO_USTCONSUMERD;
4197 goto error;
4198 }
4199
4200 if (need_tracing_session) {
4201 if (cmd_ctx->session->ust_session == NULL) {
4202 ret = create_ust_session(cmd_ctx->session,
4203 &cmd_ctx->lsm->domain);
4204 if (ret != LTTCOMM_OK) {
4205 goto error;
4206 }
4207 }
4208
4209 /* Start the UST consumer daemons */
4210 /* 64-bit */
4211 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
4212 if (consumerd64_bin[0] != '\0' &&
4213 ustconsumer64_data.pid == 0 &&
4214 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4215 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4216 ret = start_consumerd(&ustconsumer64_data);
4217 if (ret < 0) {
4218 ret = LTTCOMM_UST_CONSUMER64_FAIL;
4219 ust_consumerd64_fd = -EINVAL;
4220 goto error;
4221 }
4222
4223 ust_consumerd64_fd = ustconsumer64_data.cmd_sock;
4224 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
4225 } else {
4226 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4227 }
4228 /* 32-bit */
4229 if (consumerd32_bin[0] != '\0' &&
4230 ustconsumer32_data.pid == 0 &&
4231 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4232 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4233 ret = start_consumerd(&ustconsumer32_data);
4234 if (ret < 0) {
4235 ret = LTTCOMM_UST_CONSUMER32_FAIL;
4236 ust_consumerd32_fd = -EINVAL;
4237 goto error;
4238 }
4239
4240 ust_consumerd32_fd = ustconsumer32_data.cmd_sock;
4241 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
4242 } else {
4243 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4244 }
4245 }
4246 break;
4247 }
4248 default:
4249 break;
4250 }
4251 skip_domain:
4252
4253 /* Validate consumer daemon state when start/stop trace command */
4254 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
4255 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
4256 switch (cmd_ctx->lsm->domain.type) {
4257 case LTTNG_DOMAIN_UST:
4258 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
4259 ret = LTTCOMM_NO_USTCONSUMERD;
4260 goto error;
4261 }
4262 break;
4263 case LTTNG_DOMAIN_KERNEL:
4264 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
4265 ret = LTTCOMM_NO_KERNCONSUMERD;
4266 goto error;
4267 }
4268 break;
4269 }
4270 }
4271
4272 /*
4273 * Check that the UID or GID match that of the tracing session.
4274 * The root user can interact with all sessions.
4275 */
4276 if (need_tracing_session) {
4277 if (!session_access_ok(cmd_ctx->session,
4278 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4279 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
4280 ret = LTTCOMM_EPERM;
4281 goto error;
4282 }
4283 }
4284
4285 /* Process by command type */
4286 switch (cmd_ctx->lsm->cmd_type) {
4287 case LTTNG_ADD_CONTEXT:
4288 {
4289 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4290 cmd_ctx->lsm->u.context.channel_name,
4291 cmd_ctx->lsm->u.context.event_name,
4292 &cmd_ctx->lsm->u.context.ctx);
4293 break;
4294 }
4295 case LTTNG_DISABLE_CHANNEL:
4296 {
4297 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4298 cmd_ctx->lsm->u.disable.channel_name);
4299 break;
4300 }
4301 case LTTNG_DISABLE_EVENT:
4302 {
4303 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4304 cmd_ctx->lsm->u.disable.channel_name,
4305 cmd_ctx->lsm->u.disable.name);
4306 break;
4307 }
4308 case LTTNG_DISABLE_ALL_EVENT:
4309 {
4310 DBG("Disabling all events");
4311
4312 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4313 cmd_ctx->lsm->u.disable.channel_name);
4314 break;
4315 }
4316 case LTTNG_DISABLE_CONSUMER:
4317 {
4318 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4319 break;
4320 }
4321 case LTTNG_ENABLE_CHANNEL:
4322 {
4323 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4324 &cmd_ctx->lsm->u.channel.chan);
4325 break;
4326 }
4327 case LTTNG_ENABLE_CONSUMER:
4328 {
4329 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4330 break;
4331 }
4332 case LTTNG_ENABLE_EVENT:
4333 {
4334 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4335 cmd_ctx->lsm->u.enable.channel_name,
4336 &cmd_ctx->lsm->u.enable.event);
4337 break;
4338 }
4339 case LTTNG_ENABLE_ALL_EVENT:
4340 {
4341 DBG("Enabling all events");
4342
4343 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4344 cmd_ctx->lsm->u.enable.channel_name,
4345 cmd_ctx->lsm->u.enable.event.type);
4346 break;
4347 }
4348 case LTTNG_LIST_TRACEPOINTS:
4349 {
4350 struct lttng_event *events;
4351 ssize_t nb_events;
4352
4353 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
4354 if (nb_events < 0) {
4355 ret = -nb_events;
4356 goto error;
4357 }
4358
4359 /*
4360 * Setup lttng message with payload size set to the event list size in
4361 * bytes and then copy list into the llm payload.
4362 */
4363 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
4364 if (ret < 0) {
4365 free(events);
4366 goto setup_error;
4367 }
4368
4369 /* Copy event list into message payload */
4370 memcpy(cmd_ctx->llm->payload, events,
4371 sizeof(struct lttng_event) * nb_events);
4372
4373 free(events);
4374
4375 ret = LTTCOMM_OK;
4376 break;
4377 }
4378 case LTTNG_LIST_TRACEPOINT_FIELDS:
4379 {
4380 struct lttng_event_field *fields;
4381 ssize_t nb_fields;
4382
4383 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, &fields);
4384 if (nb_fields < 0) {
4385 ret = -nb_fields;
4386 goto error;
4387 }
4388
4389 /*
4390 * Setup lttng message with payload size set to the event list size in
4391 * bytes and then copy list into the llm payload.
4392 */
4393 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event_field) * nb_fields);
4394 if (ret < 0) {
4395 free(fields);
4396 goto setup_error;
4397 }
4398
4399 /* Copy event list into message payload */
4400 memcpy(cmd_ctx->llm->payload, fields,
4401 sizeof(struct lttng_event_field) * nb_fields);
4402
4403 free(fields);
4404
4405 ret = LTTCOMM_OK;
4406 break;
4407 }
4408 case LTTNG_SET_CONSUMER_URI:
4409 {
4410 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
4411 &cmd_ctx->lsm->u.uri);
4412 break;
4413 }
4414 case LTTNG_START_TRACE:
4415 {
4416 ret = cmd_start_trace(cmd_ctx->session);
4417 break;
4418 }
4419 case LTTNG_STOP_TRACE:
4420 {
4421 ret = cmd_stop_trace(cmd_ctx->session);
4422 break;
4423 }
4424 case LTTNG_CREATE_SESSION:
4425 {
4426 ret = cmd_create_session(cmd_ctx->lsm->session.name,
4427 cmd_ctx->lsm->session.path, &cmd_ctx->creds);
4428 break;
4429 }
4430 case LTTNG_CREATE_SESSION_URI:
4431 {
4432 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name,
4433 &cmd_ctx->lsm->u.create_uri.ctrl_uri,
4434 &cmd_ctx->lsm->u.create_uri.data_uri,
4435 cmd_ctx->lsm->u.create_uri.enable_consumer, &cmd_ctx->creds);
4436 break;
4437 }
4438 case LTTNG_DESTROY_SESSION:
4439 {
4440 ret = cmd_destroy_session(cmd_ctx->session,
4441 cmd_ctx->lsm->session.name);
4442 /*
4443 * Set session to NULL so we do not unlock it after
4444 * free.
4445 */
4446 cmd_ctx->session = NULL;
4447 break;
4448 }
4449 case LTTNG_LIST_DOMAINS:
4450 {
4451 ssize_t nb_dom;
4452 struct lttng_domain *domains;
4453
4454 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
4455 if (nb_dom < 0) {
4456 ret = -nb_dom;
4457 goto error;
4458 }
4459
4460 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
4461 if (ret < 0) {
4462 goto setup_error;
4463 }
4464
4465 /* Copy event list into message payload */
4466 memcpy(cmd_ctx->llm->payload, domains,
4467 nb_dom * sizeof(struct lttng_domain));
4468
4469 free(domains);
4470
4471 ret = LTTCOMM_OK;
4472 break;
4473 }
4474 case LTTNG_LIST_CHANNELS:
4475 {
4476 int nb_chan;
4477 struct lttng_channel *channels;
4478
4479 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
4480 cmd_ctx->session, &channels);
4481 if (nb_chan < 0) {
4482 ret = -nb_chan;
4483 goto error;
4484 }
4485
4486 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
4487 if (ret < 0) {
4488 goto setup_error;
4489 }
4490
4491 /* Copy event list into message payload */
4492 memcpy(cmd_ctx->llm->payload, channels,
4493 nb_chan * sizeof(struct lttng_channel));
4494
4495 free(channels);
4496
4497 ret = LTTCOMM_OK;
4498 break;
4499 }
4500 case LTTNG_LIST_EVENTS:
4501 {
4502 ssize_t nb_event;
4503 struct lttng_event *events = NULL;
4504
4505 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
4506 cmd_ctx->lsm->u.list.channel_name, &events);
4507 if (nb_event < 0) {
4508 ret = -nb_event;
4509 goto error;
4510 }
4511
4512 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
4513 if (ret < 0) {
4514 goto setup_error;
4515 }
4516
4517 /* Copy event list into message payload */
4518 memcpy(cmd_ctx->llm->payload, events,
4519 nb_event * sizeof(struct lttng_event));
4520
4521 free(events);
4522
4523 ret = LTTCOMM_OK;
4524 break;
4525 }
4526 case LTTNG_LIST_SESSIONS:
4527 {
4528 unsigned int nr_sessions;
4529
4530 session_lock_list();
4531 nr_sessions = lttng_sessions_count(
4532 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4533 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
4534
4535 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
4536 if (ret < 0) {
4537 session_unlock_list();
4538 goto setup_error;
4539 }
4540
4541 /* Filled the session array */
4542 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
4543 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4544 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
4545
4546 session_unlock_list();
4547
4548 ret = LTTCOMM_OK;
4549 break;
4550 }
4551 case LTTNG_CALIBRATE:
4552 {
4553 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
4554 &cmd_ctx->lsm->u.calibrate);
4555 break;
4556 }
4557 case LTTNG_REGISTER_CONSUMER:
4558 {
4559 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4560 cmd_ctx->lsm->u.reg.path);
4561 break;
4562 }
4563 case LTTNG_SET_FILTER:
4564 {
4565 struct lttng_filter_bytecode *bytecode;
4566
4567 if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) {
4568 ret = LTTCOMM_FILTER_INVAL;
4569 goto error;
4570 }
4571 bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len);
4572 if (!bytecode) {
4573 ret = LTTCOMM_FILTER_NOMEM;
4574 goto error;
4575 }
4576 /* Receive var. len. data */
4577 DBG("Receiving var len data from client ...");
4578 ret = lttcomm_recv_unix_sock(sock, bytecode,
4579 cmd_ctx->lsm->u.filter.bytecode_len);
4580 if (ret <= 0) {
4581 DBG("Nothing recv() from client var len data... continuing");
4582 *sock_error = 1;
4583 ret = LTTCOMM_FILTER_INVAL;
4584 goto error;
4585 }
4586
4587 if (bytecode->len + sizeof(*bytecode)
4588 != cmd_ctx->lsm->u.filter.bytecode_len) {
4589 free(bytecode);
4590 ret = LTTCOMM_FILTER_INVAL;
4591 goto error;
4592 }
4593
4594 ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4595 cmd_ctx->lsm->u.filter.channel_name,
4596 cmd_ctx->lsm->u.filter.event_name,
4597 bytecode);
4598 break;
4599 }
4600 default:
4601 ret = LTTCOMM_UND;
4602 break;
4603 }
4604
4605 error:
4606 if (cmd_ctx->llm == NULL) {
4607 DBG("Missing llm structure. Allocating one.");
4608 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
4609 goto setup_error;
4610 }
4611 }
4612 /* Set return code */
4613 cmd_ctx->llm->ret_code = ret;
4614 setup_error:
4615 if (cmd_ctx->session) {
4616 session_unlock(cmd_ctx->session);
4617 }
4618 if (need_tracing_session) {
4619 session_unlock_list();
4620 }
4621 init_setup_error:
4622 return ret;
4623 }
4624
4625 /*
4626 * Thread managing health check socket.
4627 */
4628 static void *thread_manage_health(void *data)
4629 {
4630 int sock = -1, new_sock, ret, i, pollfd;
4631 uint32_t revents, nb_fd;
4632 struct lttng_poll_event events;
4633 struct lttcomm_health_msg msg;
4634 struct lttcomm_health_data reply;
4635
4636 DBG("[thread] Manage health check started");
4637
4638 rcu_register_thread();
4639
4640 /* Create unix socket */
4641 sock = lttcomm_create_unix_sock(health_unix_sock_path);
4642 if (sock < 0) {
4643 ERR("Unable to create health check Unix socket");
4644 ret = -1;
4645 goto error;
4646 }
4647
4648 ret = lttcomm_listen_unix_sock(sock);
4649 if (ret < 0) {
4650 goto error;
4651 }
4652
4653 /*
4654 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4655 * more will be added to this poll set.
4656 */
4657 ret = create_thread_poll_set(&events, 2);
4658 if (ret < 0) {
4659 goto error;
4660 }
4661
4662 /* Add the application registration socket */
4663 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
4664 if (ret < 0) {
4665 goto error;
4666 }
4667
4668 while (1) {
4669 DBG("Health check ready");
4670
4671 nb_fd = LTTNG_POLL_GETNB(&events);
4672
4673 /* Inifinite blocking call, waiting for transmission */
4674 restart:
4675 ret = lttng_poll_wait(&events, -1);
4676 if (ret < 0) {
4677 /*
4678 * Restart interrupted system call.
4679 */
4680 if (errno == EINTR) {
4681 goto restart;
4682 }
4683 goto error;
4684 }
4685
4686 for (i = 0; i < nb_fd; i++) {
4687 /* Fetch once the poll data */
4688 revents = LTTNG_POLL_GETEV(&events, i);
4689 pollfd = LTTNG_POLL_GETFD(&events, i);
4690
4691 /* Thread quit pipe has been closed. Killing thread. */
4692 ret = check_thread_quit_pipe(pollfd, revents);
4693 if (ret) {
4694 goto error;
4695 }
4696
4697 /* Event on the registration socket */
4698 if (pollfd == sock) {
4699 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4700 ERR("Health socket poll error");
4701 goto error;
4702 }
4703 }
4704 }
4705
4706 new_sock = lttcomm_accept_unix_sock(sock);
4707 if (new_sock < 0) {
4708 goto error;
4709 }
4710
4711 DBG("Receiving data from client for health...");
4712 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4713 if (ret <= 0) {
4714 DBG("Nothing recv() from client... continuing");
4715 ret = close(new_sock);
4716 if (ret) {
4717 PERROR("close");
4718 }
4719 new_sock = -1;
4720 continue;
4721 }
4722
4723 rcu_thread_online();
4724
4725 switch (msg.component) {
4726 case LTTNG_HEALTH_CMD:
4727 reply.ret_code = health_check_state(&health_thread_cmd);
4728 break;
4729 case LTTNG_HEALTH_APP_REG:
4730 reply.ret_code = health_check_state(&health_thread_app_reg);
4731 break;
4732 case LTTNG_HEALTH_KERNEL:
4733 reply.ret_code = health_check_state(&health_thread_kernel);
4734 break;
4735 case LTTNG_HEALTH_CONSUMER:
4736 reply.ret_code = check_consumer_health();
4737 break;
4738 case LTTNG_HEALTH_ALL:
4739 ret = check_consumer_health();
4740
4741 reply.ret_code =
4742 health_check_state(&health_thread_app_reg) &
4743 health_check_state(&health_thread_cmd) &
4744 health_check_state(&health_thread_kernel) &
4745 ret;
4746 break;
4747 default:
4748 reply.ret_code = LTTCOMM_UND;
4749 break;
4750 }
4751
4752 /*
4753 * Flip ret value since 0 is a success and 1 indicates a bad health for
4754 * the client where in the sessiond it is the opposite. Again, this is
4755 * just to make things easier for us poor developer which enjoy a lot
4756 * lazyness.
4757 */
4758 if (reply.ret_code == 0 || reply.ret_code == 1) {
4759 reply.ret_code = !reply.ret_code;
4760 }
4761
4762 DBG2("Health check return value %d", reply.ret_code);
4763
4764 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4765 if (ret < 0) {
4766 ERR("Failed to send health data back to client");
4767 }
4768
4769 /* End of transmission */
4770 ret = close(new_sock);
4771 if (ret) {
4772 PERROR("close");
4773 }
4774 new_sock = -1;
4775 }
4776
4777 error:
4778 DBG("Health check thread dying");
4779 unlink(health_unix_sock_path);
4780 if (sock >= 0) {
4781 ret = close(sock);
4782 if (ret) {
4783 PERROR("close");
4784 }
4785 }
4786 if (new_sock >= 0) {
4787 ret = close(new_sock);
4788 if (ret) {
4789 PERROR("close");
4790 }
4791 }
4792
4793 lttng_poll_clean(&events);
4794
4795 rcu_unregister_thread();
4796 return NULL;
4797 }
4798
4799 /*
4800 * This thread manage all clients request using the unix client socket for
4801 * communication.
4802 */
4803 static void *thread_manage_clients(void *data)
4804 {
4805 int sock = -1, ret, i, pollfd;
4806 int sock_error;
4807 uint32_t revents, nb_fd;
4808 struct command_ctx *cmd_ctx = NULL;
4809 struct lttng_poll_event events;
4810
4811 DBG("[thread] Manage client started");
4812
4813 rcu_register_thread();
4814
4815 health_code_update(&health_thread_cmd);
4816
4817 ret = lttcomm_listen_unix_sock(client_sock);
4818 if (ret < 0) {
4819 goto error;
4820 }
4821
4822 /*
4823 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4824 * more will be added to this poll set.
4825 */
4826 ret = create_thread_poll_set(&events, 2);
4827 if (ret < 0) {
4828 goto error;
4829 }
4830
4831 /* Add the application registration socket */
4832 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4833 if (ret < 0) {
4834 goto error;
4835 }
4836
4837 /*
4838 * Notify parent pid that we are ready to accept command for client side.
4839 */
4840 if (opt_sig_parent) {
4841 kill(ppid, SIGUSR1);
4842 }
4843
4844 health_code_update(&health_thread_cmd);
4845
4846 while (1) {
4847 DBG("Accepting client command ...");
4848
4849 nb_fd = LTTNG_POLL_GETNB(&events);
4850
4851 /* Inifinite blocking call, waiting for transmission */
4852 restart:
4853 health_poll_update(&health_thread_cmd);
4854 ret = lttng_poll_wait(&events, -1);
4855 health_poll_update(&health_thread_cmd);
4856 if (ret < 0) {
4857 /*
4858 * Restart interrupted system call.
4859 */
4860 if (errno == EINTR) {
4861 goto restart;
4862 }
4863 goto error;
4864 }
4865
4866 for (i = 0; i < nb_fd; i++) {
4867 /* Fetch once the poll data */
4868 revents = LTTNG_POLL_GETEV(&events, i);
4869 pollfd = LTTNG_POLL_GETFD(&events, i);
4870
4871 health_code_update(&health_thread_cmd);
4872
4873 /* Thread quit pipe has been closed. Killing thread. */
4874 ret = check_thread_quit_pipe(pollfd, revents);
4875 if (ret) {
4876 goto error;
4877 }
4878
4879 /* Event on the registration socket */
4880 if (pollfd == client_sock) {
4881 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4882 ERR("Client socket poll error");
4883 goto error;
4884 }
4885 }
4886 }
4887
4888 DBG("Wait for client response");
4889
4890 health_code_update(&health_thread_cmd);
4891
4892 sock = lttcomm_accept_unix_sock(client_sock);
4893 if (sock < 0) {
4894 goto error;
4895 }
4896
4897 /* Set socket option for credentials retrieval */
4898 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4899 if (ret < 0) {
4900 goto error;
4901 }
4902
4903 /* Allocate context command to process the client request */
4904 cmd_ctx = zmalloc(sizeof(struct command_ctx));
4905 if (cmd_ctx == NULL) {
4906 PERROR("zmalloc cmd_ctx");
4907 goto error;
4908 }
4909
4910 /* Allocate data buffer for reception */
4911 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
4912 if (cmd_ctx->lsm == NULL) {
4913 PERROR("zmalloc cmd_ctx->lsm");
4914 goto error;
4915 }
4916
4917 cmd_ctx->llm = NULL;
4918 cmd_ctx->session = NULL;
4919
4920 health_code_update(&health_thread_cmd);
4921
4922 /*
4923 * Data is received from the lttng client. The struct
4924 * lttcomm_session_msg (lsm) contains the command and data request of
4925 * the client.
4926 */
4927 DBG("Receiving data from client ...");
4928 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4929 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
4930 if (ret <= 0) {
4931 DBG("Nothing recv() from client... continuing");
4932 ret = close(sock);
4933 if (ret) {
4934 PERROR("close");
4935 }
4936 sock = -1;
4937 clean_command_ctx(&cmd_ctx);
4938 continue;
4939 }
4940
4941 health_code_update(&health_thread_cmd);
4942
4943 // TODO: Validate cmd_ctx including sanity check for
4944 // security purpose.
4945
4946 rcu_thread_online();
4947 /*
4948 * This function dispatch the work to the kernel or userspace tracer
4949 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4950 * informations for the client. The command context struct contains
4951 * everything this function may needs.
4952 */
4953 ret = process_client_msg(cmd_ctx, sock, &sock_error);
4954 rcu_thread_offline();
4955 if (ret < 0) {
4956 if (sock_error) {
4957 ret = close(sock);
4958 if (ret) {
4959 PERROR("close");
4960 }
4961 sock = -1;
4962 }
4963 /*
4964 * TODO: Inform client somehow of the fatal error. At
4965 * this point, ret < 0 means that a zmalloc failed
4966 * (ENOMEM). Error detected but still accept
4967 * command, unless a socket error has been
4968 * detected.
4969 */
4970 clean_command_ctx(&cmd_ctx);
4971 continue;
4972 }
4973
4974 health_code_update(&health_thread_cmd);
4975
4976 DBG("Sending response (size: %d, retcode: %s)",
4977 cmd_ctx->lttng_msg_size,
4978 lttng_strerror(-cmd_ctx->llm->ret_code));
4979 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
4980 if (ret < 0) {
4981 ERR("Failed to send data back to client");
4982 }
4983
4984 /* End of transmission */
4985 ret = close(sock);
4986 if (ret) {
4987 PERROR("close");
4988 }
4989 sock = -1;
4990
4991 clean_command_ctx(&cmd_ctx);
4992
4993 health_code_update(&health_thread_cmd);
4994 }
4995
4996 error:
4997 health_reset(&health_thread_cmd);
4998
4999 DBG("Client thread dying");
5000 unlink(client_unix_sock_path);
5001 if (client_sock >= 0) {
5002 ret = close(client_sock);
5003 if (ret) {
5004 PERROR("close");
5005 }
5006 }
5007 if (sock >= 0) {
5008 ret = close(sock);
5009 if (ret) {
5010 PERROR("close");
5011 }
5012 }
5013
5014 lttng_poll_clean(&events);
5015 clean_command_ctx(&cmd_ctx);
5016
5017 rcu_unregister_thread();
5018 return NULL;
5019 }
5020
5021
5022 /*
5023 * usage function on stderr
5024 */
5025 static void usage(void)
5026 {
5027 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
5028 fprintf(stderr, " -h, --help Display this usage.\n");
5029 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
5030 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5031 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5032 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
5033 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5034 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5035 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5036 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
5037 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5038 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5039 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5040 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
5041 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
5042 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5043 fprintf(stderr, " -V, --version Show version number.\n");
5044 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5045 fprintf(stderr, " -q, --quiet No output at all.\n");
5046 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
5047 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
5048 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
5049 }
5050
5051 /*
5052 * daemon argument parsing
5053 */
5054 static int parse_args(int argc, char **argv)
5055 {
5056 int c;
5057
5058 static struct option long_options[] = {
5059 { "client-sock", 1, 0, 'c' },
5060 { "apps-sock", 1, 0, 'a' },
5061 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5062 { "kconsumerd-err-sock", 1, 0, 'E' },
5063 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5064 { "ustconsumerd32-err-sock", 1, 0, 'H' },
5065 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5066 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5067 { "consumerd32-path", 1, 0, 'u' },
5068 { "consumerd32-libdir", 1, 0, 'U' },
5069 { "consumerd64-path", 1, 0, 't' },
5070 { "consumerd64-libdir", 1, 0, 'T' },
5071 { "daemonize", 0, 0, 'd' },
5072 { "sig-parent", 0, 0, 'S' },
5073 { "help", 0, 0, 'h' },
5074 { "group", 1, 0, 'g' },
5075 { "version", 0, 0, 'V' },
5076 { "quiet", 0, 0, 'q' },
5077 { "verbose", 0, 0, 'v' },
5078 { "verbose-consumer", 0, 0, 'Z' },
5079 { "no-kernel", 0, 0, 'N' },
5080 { NULL, 0, 0, 0 }
5081 };
5082
5083 while (1) {
5084 int option_index = 0;
5085 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
5086 long_options, &option_index);
5087 if (c == -1) {
5088 break;
5089 }
5090
5091 switch (c) {
5092 case 0:
5093 fprintf(stderr, "option %s", long_options[option_index].name);
5094 if (optarg) {
5095 fprintf(stderr, " with arg %s\n", optarg);
5096 }
5097 break;
5098 case 'c':
5099 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
5100 break;
5101 case 'a':
5102 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
5103 break;
5104 case 'd':
5105 opt_daemon = 1;
5106 break;
5107 case 'g':
5108 opt_tracing_group = optarg;
5109 break;
5110 case 'h':
5111 usage();
5112 exit(EXIT_FAILURE);
5113 case 'V':
5114 fprintf(stdout, "%s\n", VERSION);
5115 exit(EXIT_SUCCESS);
5116 case 'S':
5117 opt_sig_parent = 1;
5118 break;
5119 case 'E':
5120 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5121 break;
5122 case 'C':
5123 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5124 break;
5125 case 'F':
5126 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5127 break;
5128 case 'D':
5129 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5130 break;
5131 case 'H':
5132 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5133 break;
5134 case 'G':
5135 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5136 break;
5137 case 'N':
5138 opt_no_kernel = 1;
5139 break;
5140 case 'q':
5141 lttng_opt_quiet = 1;
5142 break;
5143 case 'v':
5144 /* Verbose level can increase using multiple -v */
5145 lttng_opt_verbose += 1;
5146 break;
5147 case 'Z':
5148 opt_verbose_consumer += 1;
5149 break;
5150 case 'u':
5151 consumerd32_bin= optarg;
5152 break;
5153 case 'U':
5154 consumerd32_libdir = optarg;
5155 break;
5156 case 't':
5157 consumerd64_bin = optarg;
5158 break;
5159 case 'T':
5160 consumerd64_libdir = optarg;
5161 break;
5162 default:
5163 /* Unknown option or other error.
5164 * Error is printed by getopt, just return */
5165 return -1;
5166 }
5167 }
5168
5169 return 0;
5170 }
5171
5172 /*
5173 * Creates the two needed socket by the daemon.
5174 * apps_sock - The communication socket for all UST apps.
5175 * client_sock - The communication of the cli tool (lttng).
5176 */
5177 static int init_daemon_socket(void)
5178 {
5179 int ret = 0;
5180 mode_t old_umask;
5181
5182 old_umask = umask(0);
5183
5184 /* Create client tool unix socket */
5185 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5186 if (client_sock < 0) {
5187 ERR("Create unix sock failed: %s", client_unix_sock_path);
5188 ret = -1;
5189 goto end;
5190 }
5191
5192 /* File permission MUST be 660 */
5193 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5194 if (ret < 0) {
5195 ERR("Set file permissions failed: %s", client_unix_sock_path);
5196 PERROR("chmod");
5197 goto end;
5198 }
5199
5200 /* Create the application unix socket */
5201 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5202 if (apps_sock < 0) {
5203 ERR("Create unix sock failed: %s", apps_unix_sock_path);
5204 ret = -1;
5205 goto end;
5206 }
5207
5208 /* File permission MUST be 666 */
5209 ret = chmod(apps_unix_sock_path,
5210 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
5211 if (ret < 0) {
5212 ERR("Set file permissions failed: %s", apps_unix_sock_path);
5213 PERROR("chmod");
5214 goto end;
5215 }
5216
5217 end:
5218 umask(old_umask);
5219 return ret;
5220 }
5221
5222 /*
5223 * Check if the global socket is available, and if a daemon is answering at the
5224 * other side. If yes, error is returned.
5225 */
5226 static int check_existing_daemon(void)
5227 {
5228 /* Is there anybody out there ? */
5229 if (lttng_session_daemon_alive()) {
5230 return -EEXIST;
5231 }
5232
5233 return 0;
5234 }
5235
5236 /*
5237 * Set the tracing group gid onto the client socket.
5238 *
5239 * Race window between mkdir and chown is OK because we are going from more
5240 * permissive (root.root) to less permissive (root.tracing).
5241 */
5242 static int set_permissions(char *rundir)
5243 {
5244 int ret;
5245 gid_t gid;
5246
5247 ret = allowed_group();
5248 if (ret < 0) {
5249 WARN("No tracing group detected");
5250 ret = 0;
5251 goto end;
5252 }
5253
5254 gid = ret;
5255
5256 /* Set lttng run dir */
5257 ret = chown(rundir, 0, gid);
5258 if (ret < 0) {
5259 ERR("Unable to set group on %s", rundir);
5260 PERROR("chown");
5261 }
5262
5263 /* Ensure tracing group can search the run dir */
5264 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
5265 if (ret < 0) {
5266 ERR("Unable to set permissions on %s", rundir);
5267 PERROR("chmod");
5268 }
5269
5270 /* lttng client socket path */
5271 ret = chown(client_unix_sock_path, 0, gid);
5272 if (ret < 0) {
5273 ERR("Unable to set group on %s", client_unix_sock_path);
5274 PERROR("chown");
5275 }
5276
5277 /* kconsumer error socket path */
5278 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
5279 if (ret < 0) {
5280 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
5281 PERROR("chown");
5282 }
5283
5284 /* 64-bit ustconsumer error socket path */
5285 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
5286 if (ret < 0) {
5287 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
5288 PERROR("chown");
5289 }
5290
5291 /* 32-bit ustconsumer compat32 error socket path */
5292 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
5293 if (ret < 0) {
5294 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
5295 PERROR("chown");
5296 }
5297
5298 DBG("All permissions are set");
5299
5300 end:
5301 return ret;
5302 }
5303
5304 /*
5305 * Create the lttng run directory needed for all global sockets and pipe.
5306 */
5307 static int create_lttng_rundir(const char *rundir)
5308 {
5309 int ret;
5310
5311 DBG3("Creating LTTng run directory: %s", rundir);
5312
5313 ret = mkdir(rundir, S_IRWXU);
5314 if (ret < 0) {
5315 if (errno != EEXIST) {
5316 ERR("Unable to create %s", rundir);
5317 goto error;
5318 } else {
5319 ret = 0;
5320 }
5321 }
5322
5323 error:
5324 return ret;
5325 }
5326
5327 /*
5328 * Setup sockets and directory needed by the kconsumerd communication with the
5329 * session daemon.
5330 */
5331 static int set_consumer_sockets(struct consumer_data *consumer_data,
5332 const char *rundir)
5333 {
5334 int ret;
5335 char path[PATH_MAX];
5336
5337 switch (consumer_data->type) {
5338 case LTTNG_CONSUMER_KERNEL:
5339 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
5340 break;
5341 case LTTNG_CONSUMER64_UST:
5342 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
5343 break;
5344 case LTTNG_CONSUMER32_UST:
5345 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
5346 break;
5347 default:
5348 ERR("Consumer type unknown");
5349 ret = -EINVAL;
5350 goto error;
5351 }
5352
5353 DBG2("Creating consumer directory: %s", path);
5354
5355 ret = mkdir(path, S_IRWXU);
5356 if (ret < 0) {
5357 if (errno != EEXIST) {
5358 PERROR("mkdir");
5359 ERR("Failed to create %s", path);
5360 goto error;
5361 }
5362 ret = -1;
5363 }
5364
5365 /* Create the kconsumerd error unix socket */
5366 consumer_data->err_sock =
5367 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5368 if (consumer_data->err_sock < 0) {
5369 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
5370 ret = -1;
5371 goto error;
5372 }
5373
5374 /* File permission MUST be 660 */
5375 ret = chmod(consumer_data->err_unix_sock_path,
5376 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5377 if (ret < 0) {
5378 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
5379 PERROR("chmod");
5380 goto error;
5381 }
5382
5383 error:
5384 return ret;
5385 }
5386
5387 /*
5388 * Signal handler for the daemon
5389 *
5390 * Simply stop all worker threads, leaving main() return gracefully after
5391 * joining all threads and calling cleanup().
5392 */
5393 static void sighandler(int sig)
5394 {
5395 switch (sig) {
5396 case SIGPIPE:
5397 DBG("SIGPIPE caught");
5398 return;
5399 case SIGINT:
5400 DBG("SIGINT caught");
5401 stop_threads();
5402 break;
5403 case SIGTERM:
5404 DBG("SIGTERM caught");
5405 stop_threads();
5406 break;
5407 default:
5408 break;
5409 }
5410 }
5411
5412 /*
5413 * Setup signal handler for :
5414 * SIGINT, SIGTERM, SIGPIPE
5415 */
5416 static int set_signal_handler(void)
5417 {
5418 int ret = 0;
5419 struct sigaction sa;
5420 sigset_t sigset;
5421
5422 if ((ret = sigemptyset(&sigset)) < 0) {
5423 PERROR("sigemptyset");
5424 return ret;
5425 }
5426
5427 sa.sa_handler = sighandler;
5428 sa.sa_mask = sigset;
5429 sa.sa_flags = 0;
5430 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
5431 PERROR("sigaction");
5432 return ret;
5433 }
5434
5435 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
5436 PERROR("sigaction");
5437 return ret;
5438 }
5439
5440 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
5441 PERROR("sigaction");
5442 return ret;
5443 }
5444
5445 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5446
5447 return ret;
5448 }
5449
5450 /*
5451 * Set open files limit to unlimited. This daemon can open a large number of
5452 * file descriptors in order to consumer multiple kernel traces.
5453 */
5454 static void set_ulimit(void)
5455 {
5456 int ret;
5457 struct rlimit lim;
5458
5459 /* The kernel does not allowed an infinite limit for open files */
5460 lim.rlim_cur = 65535;
5461 lim.rlim_max = 65535;
5462
5463 ret = setrlimit(RLIMIT_NOFILE, &lim);
5464 if (ret < 0) {
5465 PERROR("failed to set open files limit");
5466 }
5467 }
5468
5469 /*
5470 * main
5471 */
5472 int main(int argc, char **argv)
5473 {
5474 int ret = 0;
5475 void *status;
5476 const char *home_path;
5477
5478 init_kernel_workarounds();
5479
5480 rcu_register_thread();
5481
5482 setup_consumerd_path();
5483
5484 /* Parse arguments */
5485 progname = argv[0];
5486 if ((ret = parse_args(argc, argv) < 0)) {
5487 goto error;
5488 }
5489
5490 /* Daemonize */
5491 if (opt_daemon) {
5492 int i;
5493
5494 /*
5495 * fork
5496 * child: setsid, close FD 0, 1, 2, chdir /
5497 * parent: exit (if fork is successful)
5498 */
5499 ret = daemon(0, 0);
5500 if (ret < 0) {
5501 PERROR("daemon");
5502 goto error;
5503 }
5504 /*
5505 * We are in the child. Make sure all other file
5506 * descriptors are closed, in case we are called with
5507 * more opened file descriptors than the standard ones.
5508 */
5509 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5510 (void) close(i);
5511 }
5512 }
5513
5514 /* Create thread quit pipe */
5515 if ((ret = init_thread_quit_pipe()) < 0) {
5516 goto error;
5517 }
5518
5519 /* Check if daemon is UID = 0 */
5520 is_root = !getuid();
5521
5522 if (is_root) {
5523 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
5524
5525 /* Create global run dir with root access */
5526 ret = create_lttng_rundir(rundir);
5527 if (ret < 0) {
5528 goto error;
5529 }
5530
5531 if (strlen(apps_unix_sock_path) == 0) {
5532 snprintf(apps_unix_sock_path, PATH_MAX,
5533 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
5534 }
5535
5536 if (strlen(client_unix_sock_path) == 0) {
5537 snprintf(client_unix_sock_path, PATH_MAX,
5538 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
5539 }
5540
5541 /* Set global SHM for ust */
5542 if (strlen(wait_shm_path) == 0) {
5543 snprintf(wait_shm_path, PATH_MAX,
5544 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
5545 }
5546
5547 if (strlen(health_unix_sock_path) == 0) {
5548 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5549 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
5550 }
5551
5552 /* Setup kernel consumerd path */
5553 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
5554 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
5555 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
5556 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
5557
5558 DBG2("Kernel consumer err path: %s",
5559 kconsumer_data.err_unix_sock_path);
5560 DBG2("Kernel consumer cmd path: %s",
5561 kconsumer_data.cmd_unix_sock_path);
5562 } else {
5563 home_path = get_home_dir();
5564 if (home_path == NULL) {
5565 /* TODO: Add --socket PATH option */
5566 ERR("Can't get HOME directory for sockets creation.");
5567 ret = -EPERM;
5568 goto error;
5569 }
5570
5571 /*
5572 * Create rundir from home path. This will create something like
5573 * $HOME/.lttng
5574 */
5575 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
5576 if (ret < 0) {
5577 ret = -ENOMEM;
5578 goto error;
5579 }
5580
5581 ret = create_lttng_rundir(rundir);
5582 if (ret < 0) {
5583 goto error;
5584 }
5585
5586 if (strlen(apps_unix_sock_path) == 0) {
5587 snprintf(apps_unix_sock_path, PATH_MAX,
5588 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
5589 }
5590
5591 /* Set the cli tool unix socket path */
5592 if (strlen(client_unix_sock_path) == 0) {
5593 snprintf(client_unix_sock_path, PATH_MAX,
5594 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
5595 }
5596
5597 /* Set global SHM for ust */
5598 if (strlen(wait_shm_path) == 0) {
5599 snprintf(wait_shm_path, PATH_MAX,
5600 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
5601 }
5602
5603 /* Set health check Unix path */
5604 if (strlen(health_unix_sock_path) == 0) {
5605 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5606 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
5607 }
5608 }
5609
5610 /* Set consumer initial state */
5611 kernel_consumerd_state = CONSUMER_STOPPED;
5612 ust_consumerd_state = CONSUMER_STOPPED;
5613
5614 DBG("Client socket path %s", client_unix_sock_path);
5615 DBG("Application socket path %s", apps_unix_sock_path);
5616 DBG("LTTng run directory path: %s", rundir);
5617
5618 /* 32 bits consumerd path setup */
5619 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
5620 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
5621 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
5622 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
5623
5624 DBG2("UST consumer 32 bits err path: %s",
5625 ustconsumer32_data.err_unix_sock_path);
5626 DBG2("UST consumer 32 bits cmd path: %s",
5627 ustconsumer32_data.cmd_unix_sock_path);
5628
5629 /* 64 bits consumerd path setup */
5630 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
5631 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
5632 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
5633 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
5634
5635 DBG2("UST consumer 64 bits err path: %s",
5636 ustconsumer64_data.err_unix_sock_path);
5637 DBG2("UST consumer 64 bits cmd path: %s",
5638 ustconsumer64_data.cmd_unix_sock_path);
5639
5640 /*
5641 * See if daemon already exist.
5642 */
5643 if ((ret = check_existing_daemon()) < 0) {
5644 ERR("Already running daemon.\n");
5645 /*
5646 * We do not goto exit because we must not cleanup()
5647 * because a daemon is already running.
5648 */
5649 goto error;
5650 }
5651
5652 /*
5653 * Init UST app hash table. Alloc hash table before this point since
5654 * cleanup() can get called after that point.
5655 */
5656 ust_app_ht_alloc();
5657
5658 /* After this point, we can safely call cleanup() with "goto exit" */
5659
5660 /*
5661 * These actions must be executed as root. We do that *after* setting up
5662 * the sockets path because we MUST make the check for another daemon using
5663 * those paths *before* trying to set the kernel consumer sockets and init
5664 * kernel tracer.
5665 */
5666 if (is_root) {
5667 ret = set_consumer_sockets(&kconsumer_data, rundir);
5668 if (ret < 0) {
5669 goto exit;
5670 }
5671
5672 /* Setup kernel tracer */
5673 if (!opt_no_kernel) {
5674 init_kernel_tracer();
5675 }
5676
5677 /* Set ulimit for open files */
5678 set_ulimit();
5679 }
5680 /* init lttng_fd tracking must be done after set_ulimit. */
5681 lttng_fd_init();
5682
5683 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
5684 if (ret < 0) {
5685 goto exit;
5686 }
5687
5688 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
5689 if (ret < 0) {
5690 goto exit;
5691 }
5692
5693 if ((ret = set_signal_handler()) < 0) {
5694 goto exit;
5695 }
5696
5697 /* Setup the needed unix socket */
5698 if ((ret = init_daemon_socket()) < 0) {
5699 goto exit;
5700 }
5701
5702 /* Set credentials to socket */
5703 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
5704 goto exit;
5705 }
5706
5707 /* Get parent pid if -S, --sig-parent is specified. */
5708 if (opt_sig_parent) {
5709 ppid = getppid();
5710 }
5711
5712 /* Setup the kernel pipe for waking up the kernel thread */
5713 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
5714 goto exit;
5715 }
5716
5717 /* Setup the thread apps communication pipe. */
5718 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
5719 goto exit;
5720 }
5721
5722 /* Init UST command queue. */
5723 cds_wfq_init(&ust_cmd_queue.queue);
5724
5725 /*
5726 * Get session list pointer. This pointer MUST NOT be free(). This list is
5727 * statically declared in session.c
5728 */
5729 session_list_ptr = session_get_list();
5730
5731 /* Set up max poll set size */
5732 lttng_poll_set_max_size();
5733
5734 /*
5735 * Set network sequence index to 1 for streams to match a relayd socket on
5736 * the consumer side.
5737 */
5738 uatomic_set(&relayd_net_seq_idx, 1);
5739
5740 /* Init all health thread counters. */
5741 health_init(&health_thread_cmd);
5742 health_init(&health_thread_kernel);
5743 health_init(&health_thread_app_reg);
5744
5745 /*
5746 * Init health counters of the consumer thread. We do a quick hack here to
5747 * the state of the consumer health is fine even if the thread is not
5748 * started. This is simply to ease our life and has no cost what so ever.
5749 */
5750 health_init(&kconsumer_data.health);
5751 health_poll_update(&kconsumer_data.health);
5752 health_init(&ustconsumer32_data.health);
5753 health_poll_update(&ustconsumer32_data.health);
5754 health_init(&ustconsumer64_data.health);
5755 health_poll_update(&ustconsumer64_data.health);
5756
5757 /* Create thread to manage the client socket */
5758 ret = pthread_create(&health_thread, NULL,
5759 thread_manage_health, (void *) NULL);
5760 if (ret != 0) {
5761 PERROR("pthread_create health");
5762 goto exit_health;
5763 }
5764
5765 /* Create thread to manage the client socket */
5766 ret = pthread_create(&client_thread, NULL,
5767 thread_manage_clients, (void *) NULL);
5768 if (ret != 0) {
5769 PERROR("pthread_create clients");
5770 goto exit_client;
5771 }
5772
5773 /* Create thread to dispatch registration */
5774 ret = pthread_create(&dispatch_thread, NULL,
5775 thread_dispatch_ust_registration, (void *) NULL);
5776 if (ret != 0) {
5777 PERROR("pthread_create dispatch");
5778 goto exit_dispatch;
5779 }
5780
5781 /* Create thread to manage application registration. */
5782 ret = pthread_create(&reg_apps_thread, NULL,
5783 thread_registration_apps, (void *) NULL);
5784 if (ret != 0) {
5785 PERROR("pthread_create registration");
5786 goto exit_reg_apps;
5787 }
5788
5789 /* Create thread to manage application socket */
5790 ret = pthread_create(&apps_thread, NULL,
5791 thread_manage_apps, (void *) NULL);
5792 if (ret != 0) {
5793 PERROR("pthread_create apps");
5794 goto exit_apps;
5795 }
5796
5797 /* Create kernel thread to manage kernel event */
5798 ret = pthread_create(&kernel_thread, NULL,
5799 thread_manage_kernel, (void *) NULL);
5800 if (ret != 0) {
5801 PERROR("pthread_create kernel");
5802 goto exit_kernel;
5803 }
5804
5805 ret = pthread_join(kernel_thread, &status);
5806 if (ret != 0) {
5807 PERROR("pthread_join");
5808 goto error; /* join error, exit without cleanup */
5809 }
5810
5811 exit_kernel:
5812 ret = pthread_join(apps_thread, &status);
5813 if (ret != 0) {
5814 PERROR("pthread_join");
5815 goto error; /* join error, exit without cleanup */
5816 }
5817
5818 exit_apps:
5819 ret = pthread_join(reg_apps_thread, &status);
5820 if (ret != 0) {
5821 PERROR("pthread_join");
5822 goto error; /* join error, exit without cleanup */
5823 }
5824
5825 exit_reg_apps:
5826 ret = pthread_join(dispatch_thread, &status);
5827 if (ret != 0) {
5828 PERROR("pthread_join");
5829 goto error; /* join error, exit without cleanup */
5830 }
5831
5832 exit_dispatch:
5833 ret = pthread_join(client_thread, &status);
5834 if (ret != 0) {
5835 PERROR("pthread_join");
5836 goto error; /* join error, exit without cleanup */
5837 }
5838
5839 ret = join_consumer_thread(&kconsumer_data);
5840 if (ret != 0) {
5841 PERROR("join_consumer");
5842 goto error; /* join error, exit without cleanup */
5843 }
5844
5845 exit_client:
5846 exit_health:
5847 exit:
5848 /*
5849 * cleanup() is called when no other thread is running.
5850 */
5851 rcu_thread_online();
5852 cleanup();
5853 rcu_thread_offline();
5854 rcu_unregister_thread();
5855 if (!ret) {
5856 exit(EXIT_SUCCESS);
5857 }
5858 error:
5859 exit(EXIT_FAILURE);
5860 }
This page took 0.25787 seconds and 3 git commands to generate.