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