Network streaming support
[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;