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