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