2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
25 #include <semaphore.h>
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <urcu/futex.h>
41 #include <lttng-consumerd.h>
42 #include <lttng-sessiond-comm.h>
43 #include <lttng/lttng-consumer.h>
48 #include "compat/poll.h"
52 #include "hashtable.h"
53 #include "kernel-ctl.h"
54 #include "lttng-sessiond.h"
60 struct consumer_data
{
61 enum lttng_consumer_type type
;
63 pthread_t thread
; /* Worker thread interacting with the consumer */
66 /* Mutex to control consumerd pid assignation */
67 pthread_mutex_t pid_mutex
;
73 /* consumer error and command Unix socket path */
74 char err_unix_sock_path
[PATH_MAX
];
75 char cmd_unix_sock_path
[PATH_MAX
];
78 #include "benchmark.h"
81 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
82 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
83 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
84 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
87 int opt_verbose
; /* Not static for lttngerr.h */
88 int opt_verbose_consumer
; /* Not static for lttngerr.h */
89 int opt_quiet
; /* Not static for lttngerr.h */
92 const char *opt_tracing_group
;
93 static int opt_sig_parent
;
94 static int opt_daemon
;
95 static int is_root
; /* Set to 1 if the daemon is running as root */
96 static pid_t ppid
; /* Parent PID for --sig-parent option */
98 /* Consumer daemon specific control data */
99 static struct consumer_data kconsumer_data
= {
100 .type
= LTTNG_CONSUMER_KERNEL
,
102 static struct consumer_data ustconsumer_data
= {
103 .type
= LTTNG_CONSUMER_UST
,
106 static int dispatch_thread_exit
;
108 /* Global application Unix socket path */
109 static char apps_unix_sock_path
[PATH_MAX
];
110 /* Global client Unix socket path */
111 static char client_unix_sock_path
[PATH_MAX
];
112 /* global wait shm path for UST */
113 static char wait_shm_path
[PATH_MAX
];
115 /* Sockets and FDs */
116 static int client_sock
;
117 static int apps_sock
;
118 static int kernel_tracer_fd
;
119 static int kernel_poll_pipe
[2];
122 * Quit pipe for all threads. This permits a single cancellation point
123 * for all threads when receiving an event on the pipe.
125 static int thread_quit_pipe
[2];
128 * This pipe is used to inform the thread managing application communication
129 * that a command is queued and ready to be processed.
131 static int apps_cmd_pipe
[2];
133 /* Pthread, Mutexes and Semaphores */
134 static pthread_t apps_thread
;
135 static pthread_t reg_apps_thread
;
136 static pthread_t client_thread
;
137 static pthread_t kernel_thread
;
138 static pthread_t dispatch_thread
;
142 * UST registration command queue. This queue is tied with a futex and uses a N
143 * wakers / 1 waiter implemented and detailed in futex.c/.h
145 * The thread_manage_apps and thread_dispatch_ust_registration interact with
146 * this queue and the wait/wake scheme.
148 static struct ust_cmd_queue ust_cmd_queue
;
151 * Pointer initialized before thread creation.
153 * This points to the tracing session list containing the session count and a
154 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
155 * MUST NOT be taken if you call a public function in session.c.
157 * The lock is nested inside the structure: session_list_ptr->lock. Please use
158 * session_lock_list and session_unlock_list for lock acquisition.
160 static struct ltt_session_list
*session_list_ptr
;
163 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
165 static int create_thread_poll_set(struct lttng_poll_event
*events
,
170 if (events
== NULL
|| size
== 0) {
175 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
181 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
193 * Check if the thread quit pipe was triggered.
195 * Return 1 if it was triggered else 0;
197 static int check_thread_quit_pipe(int fd
, uint32_t events
)
199 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
207 * Remove modules in reverse load order.
209 static int modprobe_remove_kernel_modules(void)
214 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
215 ret
= snprintf(modprobe
, sizeof(modprobe
),
216 "/sbin/modprobe -r -q %s",
217 kernel_modules_list
[i
].name
);
219 perror("snprintf modprobe -r");
222 modprobe
[sizeof(modprobe
) - 1] = '\0';
223 ret
= system(modprobe
);
225 ERR("Unable to launch modprobe -r for module %s",
226 kernel_modules_list
[i
].name
);
227 } else if (kernel_modules_list
[i
].required
228 && WEXITSTATUS(ret
) != 0) {
229 ERR("Unable to remove module %s",
230 kernel_modules_list
[i
].name
);
232 DBG("Modprobe removal successful %s",
233 kernel_modules_list
[i
].name
);
242 * Return group ID of the tracing group or -1 if not found.
244 static gid_t
allowed_group(void)
248 if (opt_tracing_group
) {
249 grp
= getgrnam(opt_tracing_group
);
251 grp
= getgrnam(default_tracing_group
);
261 * Init thread quit pipe.
263 * Return -1 on error or 0 if all pipes are created.
265 static int init_thread_quit_pipe(void)
269 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
271 perror("thread quit pipe");
280 * Complete teardown of a kernel session. This free all data structure related
281 * to a kernel session and update counter.
283 static void teardown_kernel_session(struct ltt_session
*session
)
285 if (session
->kernel_session
!= NULL
) {
286 DBG("Tearing down kernel session");
289 * If a custom kernel consumer was registered, close the socket before
290 * tearing down the complete kernel session structure
292 if (session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
293 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
296 trace_kernel_destroy_session(session
->kernel_session
);
297 /* Extra precaution */
298 session
->kernel_session
= NULL
;
303 * Complete teardown of all UST sessions. This will free everything on his path
304 * and destroy the core essence of all ust sessions :)
306 static void teardown_ust_session(struct ltt_session
*session
)
308 DBG("Tearing down UST session(s)");
310 trace_ust_destroy_session(session
->ust_session
);
314 * Stop all threads by closing the thread quit pipe.
316 static void stop_threads(void)
320 /* Stopping all threads */
321 DBG("Terminating all threads");
322 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
324 ERR("write error on thread quit pipe");
327 /* Dispatch thread */
328 dispatch_thread_exit
= 1;
329 futex_nto1_wake(&ust_cmd_queue
.futex
);
335 static void cleanup(void)
339 struct ltt_session
*sess
, *stmp
;
344 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
345 "Matthew, BEET driven development works!%c[%dm",
346 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
350 DBG("Removing %s directory", LTTNG_RUNDIR
);
351 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
353 ERR("asprintf failed. Something is really wrong!");
356 /* Remove lttng run directory */
359 ERR("Unable to clean " LTTNG_RUNDIR
);
363 DBG("Cleaning up all session");
365 /* Destroy session list mutex */
366 if (session_list_ptr
!= NULL
) {
367 pthread_mutex_destroy(&session_list_ptr
->lock
);
369 /* Cleanup ALL session */
370 cds_list_for_each_entry_safe(sess
, stmp
,
371 &session_list_ptr
->head
, list
) {
372 teardown_kernel_session(sess
);
373 teardown_ust_session(sess
);
378 DBG("Closing all UST sockets");
379 ust_app_clean_list();
381 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
383 DBG("Closing kernel fd");
384 close(kernel_tracer_fd
);
387 DBG("Unloading kernel modules");
388 modprobe_remove_kernel_modules();
391 close(thread_quit_pipe
[0]);
392 close(thread_quit_pipe
[1]);
394 /* OUTPUT BENCHMARK RESULTS */
397 if (getenv("BENCH_UST_NOTIFY")) {
398 bench_print_ust_notification();
401 if (getenv("BENCH_UST_REGISTER")) {
402 bench_print_ust_register();
403 bench_print_ust_unregister();
406 if (getenv("BENCH_BOOT_PROCESS")) {
407 bench_print_boot_process();
415 * Send data on a unix socket using the liblttsessiondcomm API.
417 * Return lttcomm error code.
419 static int send_unix_sock(int sock
, void *buf
, size_t len
)
421 /* Check valid length */
426 return lttcomm_send_unix_sock(sock
, buf
, len
);
430 * Free memory of a command context structure.
432 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
434 DBG("Clean command context structure");
436 if ((*cmd_ctx
)->llm
) {
437 free((*cmd_ctx
)->llm
);
439 if ((*cmd_ctx
)->lsm
) {
440 free((*cmd_ctx
)->lsm
);
448 * Send all stream fds of kernel channel to the consumer.
450 static int send_kconsumer_channel_streams(struct consumer_data
*consumer_data
,
451 int sock
, struct ltt_kernel_channel
*channel
)
454 struct ltt_kernel_stream
*stream
;
455 struct lttcomm_consumer_msg lkm
;
457 DBG("Sending streams of channel %s to kernel consumer",
458 channel
->channel
->name
);
461 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
462 lkm
.u
.channel
.channel_key
= channel
->fd
;
463 lkm
.u
.channel
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
464 lkm
.u
.channel
.mmap_len
= 0; /* for kernel */
465 DBG("Sending channel %d to consumer", lkm
.u
.channel
.channel_key
);
466 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
468 perror("send consumer channel");
473 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
477 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
478 lkm
.u
.stream
.channel_key
= channel
->fd
;
479 lkm
.u
.stream
.stream_key
= stream
->fd
;
480 lkm
.u
.stream
.state
= stream
->state
;
481 lkm
.u
.stream
.output
= channel
->channel
->attr
.output
;
482 lkm
.u
.stream
.mmap_len
= 0; /* for kernel */
483 strncpy(lkm
.u
.stream
.path_name
, stream
->pathname
, PATH_MAX
- 1);
484 lkm
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
485 DBG("Sending stream %d to consumer", lkm
.u
.stream
.stream_key
);
486 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
488 perror("send consumer stream");
491 ret
= lttcomm_send_fds_unix_sock(sock
, &stream
->fd
, 1);
493 perror("send consumer stream ancillary data");
498 DBG("consumer channel streams sent");
507 * Send all stream fds of the kernel session to the consumer.
509 static int send_kconsumer_session_streams(struct consumer_data
*consumer_data
,
510 struct ltt_kernel_session
*session
)
513 struct ltt_kernel_channel
*chan
;
514 struct lttcomm_consumer_msg lkm
;
515 int sock
= session
->consumer_fd
;
517 DBG("Sending metadata stream fd");
519 /* Extra protection. It's NOT supposed to be set to 0 at this point */
520 if (session
->consumer_fd
== 0) {
521 session
->consumer_fd
= consumer_data
->cmd_sock
;
524 if (session
->metadata_stream_fd
!= 0) {
525 /* Send metadata channel fd */
526 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_CHANNEL
;
527 lkm
.u
.channel
.channel_key
= session
->metadata
->fd
;
528 lkm
.u
.channel
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
529 lkm
.u
.channel
.mmap_len
= 0; /* for kernel */
530 DBG("Sending metadata channel %d to consumer", lkm
.u
.stream
.stream_key
);
531 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
533 perror("send consumer channel");
537 /* Send metadata stream fd */
538 lkm
.cmd_type
= LTTNG_CONSUMER_ADD_STREAM
;
539 lkm
.u
.stream
.channel_key
= session
->metadata
->fd
;
540 lkm
.u
.stream
.stream_key
= session
->metadata_stream_fd
;
541 lkm
.u
.stream
.state
= LTTNG_CONSUMER_ACTIVE_STREAM
;
542 lkm
.u
.stream
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
543 lkm
.u
.stream
.mmap_len
= 0; /* for kernel */
544 strncpy(lkm
.u
.stream
.path_name
, session
->metadata
->pathname
, PATH_MAX
- 1);
545 lkm
.u
.stream
.path_name
[PATH_MAX
- 1] = '\0';
546 DBG("Sending metadata stream %d to consumer", lkm
.u
.stream
.stream_key
);
547 ret
= lttcomm_send_unix_sock(sock
, &lkm
, sizeof(lkm
));
549 perror("send consumer stream");
552 ret
= lttcomm_send_fds_unix_sock(sock
, &session
->metadata_stream_fd
, 1);
554 perror("send consumer stream");
559 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
560 ret
= send_kconsumer_channel_streams(consumer_data
, sock
, chan
);
566 DBG("consumer fds (metadata and channel streams) sent");
575 * Notify UST applications using the shm mmap futex.
577 static int notify_ust_apps(int active
)
581 DBG("Notifying applications of session daemon state: %d", active
);
583 tracepoint(ust_notify_apps_start
);
585 /* See shm.c for this call implying mmap, shm and futex calls */
586 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
587 if (wait_shm_mmap
== NULL
) {
591 /* Wake waiting process */
592 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
594 tracepoint(ust_notify_apps_stop
);
596 /* Apps notified successfully */
604 * Setup the outgoing data buffer for the response (llm) by allocating the
605 * right amount of memory and copying the original information from the lsm
608 * Return total size of the buffer pointed by buf.
610 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
616 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
617 if (cmd_ctx
->llm
== NULL
) {
623 /* Copy common data */
624 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
625 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
627 cmd_ctx
->llm
->data_size
= size
;
628 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
637 * Update the kernel poll set of all channel fd available over all tracing
638 * session. Add the wakeup pipe at the end of the set.
640 static int update_kernel_poll(struct lttng_poll_event
*events
)
643 struct ltt_session
*session
;
644 struct ltt_kernel_channel
*channel
;
646 DBG("Updating kernel poll set");
649 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
650 session_lock(session
);
651 if (session
->kernel_session
== NULL
) {
652 session_unlock(session
);
656 cds_list_for_each_entry(channel
,
657 &session
->kernel_session
->channel_list
.head
, list
) {
658 /* Add channel fd to the kernel poll set */
659 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
661 session_unlock(session
);
664 DBG("Channel fd %d added to kernel set", channel
->fd
);
666 session_unlock(session
);
668 session_unlock_list();
673 session_unlock_list();
678 * Find the channel fd from 'fd' over all tracing session. When found, check
679 * for new channel stream and send those stream fds to the kernel consumer.
681 * Useful for CPU hotplug feature.
683 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
686 struct ltt_session
*session
;
687 struct ltt_kernel_channel
*channel
;
689 DBG("Updating kernel streams for channel fd %d", fd
);
692 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
693 session_lock(session
);
694 if (session
->kernel_session
== NULL
) {
695 session_unlock(session
);
699 /* This is not suppose to be 0 but this is an extra security check */
700 if (session
->kernel_session
->consumer_fd
== 0) {
701 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
704 cds_list_for_each_entry(channel
,
705 &session
->kernel_session
->channel_list
.head
, list
) {
706 if (channel
->fd
== fd
) {
707 DBG("Channel found, updating kernel streams");
708 ret
= kernel_open_channel_stream(channel
);
714 * Have we already sent fds to the consumer? If yes, it means
715 * that tracing is started so it is safe to send our updated
718 if (session
->kernel_session
->consumer_fds_sent
== 1) {
719 ret
= send_kconsumer_channel_streams(consumer_data
,
720 session
->kernel_session
->consumer_fd
, channel
);
728 session_unlock(session
);
730 session_unlock_list();
734 session_unlock(session
);
735 session_unlock_list();
740 * This thread manage event coming from the kernel.
742 * Features supported in this thread:
745 static void *thread_manage_kernel(void *data
)
747 int ret
, i
, pollfd
, update_poll_flag
= 1;
748 uint32_t revents
, nb_fd
;
750 struct lttng_poll_event events
;
752 tracepoint(sessiond_th_kern_start
);
754 DBG("Thread manage kernel started");
756 ret
= create_thread_poll_set(&events
, 2);
761 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
767 if (update_poll_flag
== 1) {
769 * Reset number of fd in the poll set. Always 2 since there is the thread
770 * quit pipe and the kernel pipe.
774 ret
= update_kernel_poll(&events
);
778 update_poll_flag
= 0;
781 nb_fd
= LTTNG_POLL_GETNB(&events
);
783 DBG("Thread kernel polling on %d fds", nb_fd
);
785 /* Zeroed the poll events */
786 lttng_poll_reset(&events
);
788 tracepoint(sessiond_th_kern_poll
);
790 /* Poll infinite value of time */
791 ret
= lttng_poll_wait(&events
, -1);
794 } else if (ret
== 0) {
795 /* Should not happen since timeout is infinite */
796 ERR("Return value of poll is 0 with an infinite timeout.\n"
797 "This should not have happened! Continuing...");
801 for (i
= 0; i
< nb_fd
; i
++) {
802 /* Fetch once the poll data */
803 revents
= LTTNG_POLL_GETEV(&events
, i
);
804 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
806 /* Thread quit pipe has been closed. Killing thread. */
807 ret
= check_thread_quit_pipe(pollfd
, revents
);
812 /* Check for data on kernel pipe */
813 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
814 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
815 update_poll_flag
= 1;
819 * New CPU detected by the kernel. Adding kernel stream to
820 * kernel session and updating the kernel consumer
822 if (revents
& LPOLLIN
) {
823 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
829 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
830 * and unregister kernel stream at this point.
838 DBG("Kernel thread dying");
839 close(kernel_poll_pipe
[0]);
840 close(kernel_poll_pipe
[1]);
842 lttng_poll_clean(&events
);
848 * This thread manage the consumer error sent back to the session daemon.
850 static void *thread_manage_consumer(void *data
)
852 int sock
= 0, i
, ret
, pollfd
;
853 uint32_t revents
, nb_fd
;
854 enum lttcomm_return_code code
;
855 struct lttng_poll_event events
;
856 struct consumer_data
*consumer_data
= data
;
858 tracepoint(sessiond_th_kcon_start
);
860 DBG("[thread] Manage consumer started");
862 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
868 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
869 * Nothing more will be added to this poll set.
871 ret
= create_thread_poll_set(&events
, 2);
876 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
881 nb_fd
= LTTNG_POLL_GETNB(&events
);
883 tracepoint(sessiond_th_kcon_poll
);
885 /* Inifinite blocking call, waiting for transmission */
886 ret
= lttng_poll_wait(&events
, -1);
891 for (i
= 0; i
< nb_fd
; i
++) {
892 /* Fetch once the poll data */
893 revents
= LTTNG_POLL_GETEV(&events
, i
);
894 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
896 /* Thread quit pipe has been closed. Killing thread. */
897 ret
= check_thread_quit_pipe(pollfd
, revents
);
902 /* Event on the registration socket */
903 if (pollfd
== consumer_data
->err_sock
) {
904 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
905 ERR("consumer err socket poll error");
911 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
916 DBG2("Receiving code from consumer err_sock");
918 /* Getting status code from kconsumerd */
919 ret
= lttcomm_recv_unix_sock(sock
, &code
,
920 sizeof(enum lttcomm_return_code
));
925 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
926 consumer_data
->cmd_sock
=
927 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
928 if (consumer_data
->cmd_sock
< 0) {
929 sem_post(&consumer_data
->sem
);
930 PERROR("consumer connect");
933 /* Signal condition to tell that the kconsumerd is ready */
934 sem_post(&consumer_data
->sem
);
935 DBG("consumer command socket ready");
937 ERR("consumer error when waiting for SOCK_READY : %s",
938 lttcomm_get_readable_code(-code
));
942 /* Remove the kconsumerd error sock since we've established a connexion */
943 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
948 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
953 /* Update number of fd */
954 nb_fd
= LTTNG_POLL_GETNB(&events
);
956 /* Inifinite blocking call, waiting for transmission */
957 ret
= lttng_poll_wait(&events
, -1);
962 for (i
= 0; i
< nb_fd
; i
++) {
963 /* Fetch once the poll data */
964 revents
= LTTNG_POLL_GETEV(&events
, i
);
965 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
967 /* Thread quit pipe has been closed. Killing thread. */
968 ret
= check_thread_quit_pipe(pollfd
, revents
);
973 /* Event on the kconsumerd socket */
974 if (pollfd
== sock
) {
975 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
976 ERR("consumer err socket second poll error");
982 /* Wait for any kconsumerd error */
983 ret
= lttcomm_recv_unix_sock(sock
, &code
,
984 sizeof(enum lttcomm_return_code
));
986 ERR("consumer closed the command socket");
990 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
993 DBG("consumer thread dying");
994 close(consumer_data
->err_sock
);
995 close(consumer_data
->cmd_sock
);
998 unlink(consumer_data
->err_unix_sock_path
);
999 unlink(consumer_data
->cmd_unix_sock_path
);
1000 consumer_data
->pid
= 0;
1002 lttng_poll_clean(&events
);
1008 * This thread manage application communication.
1010 static void *thread_manage_apps(void *data
)
1013 uint32_t revents
, nb_fd
;
1014 struct ust_command ust_cmd
;
1015 struct lttng_poll_event events
;
1017 tracepoint(sessiond_th_apps_start
);
1019 DBG("[thread] Manage application started");
1021 rcu_register_thread();
1022 rcu_thread_online();
1024 ret
= create_thread_poll_set(&events
, 2);
1029 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1035 /* Zeroed the events structure */
1036 lttng_poll_reset(&events
);
1038 nb_fd
= LTTNG_POLL_GETNB(&events
);
1040 DBG("Apps thread polling on %d fds", nb_fd
);
1042 tracepoint(sessiond_th_apps_poll
);
1044 /* Inifinite blocking call, waiting for transmission */
1045 ret
= lttng_poll_wait(&events
, -1);
1050 for (i
= 0; i
< nb_fd
; i
++) {
1051 /* Fetch once the poll data */
1052 revents
= LTTNG_POLL_GETEV(&events
, i
);
1053 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1055 /* Thread quit pipe has been closed. Killing thread. */
1056 ret
= check_thread_quit_pipe(pollfd
, revents
);
1061 /* Inspect the apps cmd pipe */
1062 if (pollfd
== apps_cmd_pipe
[0]) {
1063 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1064 ERR("Apps command pipe error");
1066 } else if (revents
& LPOLLIN
) {
1067 tracepoint(ust_register_read_start
);
1069 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1070 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1071 perror("read apps cmd pipe");
1074 tracepoint(ust_register_read_stop
);
1076 tracepoint(ust_register_add_start
);
1077 /* Register applicaton to the session daemon */
1078 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1081 /* Only critical ENOMEM error can be returned here */
1084 tracepoint(ust_register_add_stop
);
1086 tracepoint(ust_register_done_start
);
1087 ret
= ustctl_register_done(ust_cmd
.sock
);
1090 * If the registration is not possible, we simply
1091 * unregister the apps and continue
1093 ust_app_unregister(ust_cmd
.sock
);
1096 * We just need here to monitor the close of the UST
1097 * socket and poll set monitor those by default.
1099 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, 0);
1104 DBG("Apps with sock %d added to poll set",
1107 tracepoint(ust_register_done_stop
);
1112 * At this point, we know that a registered application made
1113 * the event at poll_wait.
1115 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1116 /* Removing from the poll set */
1117 ret
= lttng_poll_del(&events
, pollfd
);
1123 ust_app_unregister(pollfd
);
1131 DBG("Application communication apps dying");
1132 close(apps_cmd_pipe
[0]);
1133 close(apps_cmd_pipe
[1]);
1135 lttng_poll_clean(&events
);
1137 rcu_thread_offline();
1138 rcu_unregister_thread();
1143 * Dispatch request from the registration threads to the application
1144 * communication thread.
1146 static void *thread_dispatch_ust_registration(void *data
)
1149 struct cds_wfq_node
*node
;
1150 struct ust_command
*ust_cmd
= NULL
;
1152 tracepoint(sessiond_th_dispatch_start
);
1154 DBG("[thread] Dispatch UST command started");
1156 while (!dispatch_thread_exit
) {
1157 /* Atomically prepare the queue futex */
1158 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1161 tracepoint(sessiond_th_dispatch_block
);
1163 /* Dequeue command for registration */
1164 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1166 DBG("Woken up but nothing in the UST command queue");
1167 /* Continue thread execution */
1171 tracepoint(ust_dispatch_register_start
);
1173 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1175 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1176 " gid:%d sock:%d name:%s (version %d.%d)",
1177 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1178 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1179 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1180 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1182 * Inform apps thread of the new application registration. This
1183 * call is blocking so we can be assured that the data will be read
1184 * at some point in time or wait to the end of the world :)
1186 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1187 sizeof(struct ust_command
));
1189 perror("write apps cmd pipe");
1190 if (errno
== EBADF
) {
1192 * We can't inform the application thread to process
1193 * registration. We will exit or else application
1194 * registration will not occur and tracing will never
1201 } while (node
!= NULL
);
1203 tracepoint(ust_dispatch_register_stop
);
1205 /* Futex wait on queue. Blocking call on futex() */
1206 futex_nto1_wait(&ust_cmd_queue
.futex
);
1210 DBG("Dispatch thread dying");
1215 * This thread manage application registration.
1217 static void *thread_registration_apps(void *data
)
1219 int sock
= 0, i
, ret
, pollfd
;
1220 uint32_t revents
, nb_fd
;
1221 struct lttng_poll_event events
;
1223 * Get allocated in this thread, enqueued to a global queue, dequeued and
1224 * freed in the manage apps thread.
1226 struct ust_command
*ust_cmd
= NULL
;
1228 tracepoint(sessiond_th_reg_start
);
1230 DBG("[thread] Manage application registration started");
1232 ret
= lttcomm_listen_unix_sock(apps_sock
);
1238 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1239 * more will be added to this poll set.
1241 ret
= create_thread_poll_set(&events
, 2);
1246 /* Add the application registration socket */
1247 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1252 /* Notify all applications to register */
1253 ret
= notify_ust_apps(1);
1255 ERR("Failed to notify applications or create the wait shared memory.\n"
1256 "Execution continues but there might be problem for already\n"
1257 "running applications that wishes to register.");
1261 DBG("Accepting application registration");
1263 tracepoint(sessiond_th_reg_poll
);
1265 nb_fd
= LTTNG_POLL_GETNB(&events
);
1267 /* Inifinite blocking call, waiting for transmission */
1268 ret
= lttng_poll_wait(&events
, -1);
1273 for (i
= 0; i
< nb_fd
; i
++) {
1274 /* Fetch once the poll data */
1275 revents
= LTTNG_POLL_GETEV(&events
, i
);
1276 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1278 /* Thread quit pipe has been closed. Killing thread. */
1279 ret
= check_thread_quit_pipe(pollfd
, revents
);
1284 /* Event on the registration socket */
1285 if (pollfd
== apps_sock
) {
1286 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1287 ERR("Register apps socket poll error");
1289 } else if (revents
& LPOLLIN
) {
1290 /* Registration starts here. Recording cycles */
1291 tracepoint(ust_register_start
);
1293 sock
= lttcomm_accept_unix_sock(apps_sock
);
1298 /* Create UST registration command for enqueuing */
1299 ust_cmd
= malloc(sizeof(struct ust_command
));
1300 if (ust_cmd
== NULL
) {
1301 perror("ust command malloc");
1306 * Using message-based transmissions to ensure we don't
1307 * have to deal with partially received messages.
1309 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1310 sizeof(struct ust_register_msg
));
1311 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1313 perror("lttcomm_recv_unix_sock register apps");
1315 ERR("Wrong size received on apps register");
1322 ust_cmd
->sock
= sock
;
1324 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1325 " gid:%d sock:%d name:%s (version %d.%d)",
1326 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1327 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1328 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1329 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1332 * Lock free enqueue the registration request. The red pill
1333 * has been taken! This apps will be part of the *system*.
1335 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1338 * Wake the registration queue futex. Implicit memory
1339 * barrier with the exchange in cds_wfq_enqueue.
1341 futex_nto1_wake(&ust_cmd_queue
.futex
);
1343 tracepoint(ust_register_stop
);
1350 DBG("UST Registration thread dying");
1352 /* Notify that the registration thread is gone */
1357 unlink(apps_unix_sock_path
);
1359 lttng_poll_clean(&events
);
1365 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1366 * exec or it will fails.
1368 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1371 struct timespec timeout
;
1373 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1374 timeout
.tv_nsec
= 0;
1376 /* Setup semaphore */
1377 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1379 PERROR("sem_init consumer semaphore");
1383 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1384 thread_manage_consumer
, consumer_data
);
1386 PERROR("pthread_create consumer");
1391 /* Get time for sem_timedwait absolute timeout */
1392 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1394 PERROR("clock_gettime spawn consumer");
1395 /* Infinite wait for the kconsumerd thread to be ready */
1396 ret
= sem_wait(&consumer_data
->sem
);
1398 /* Normal timeout if the gettime was successful */
1399 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1400 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1404 if (errno
== ETIMEDOUT
) {
1406 * Call has timed out so we kill the kconsumerd_thread and return
1409 ERR("The consumer thread was never ready. Killing it");
1410 ret
= pthread_cancel(consumer_data
->thread
);
1412 PERROR("pthread_cancel consumer thread");
1415 PERROR("semaphore wait failed consumer thread");
1420 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1421 if (consumer_data
->pid
== 0) {
1422 ERR("Kconsumerd did not start");
1423 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1426 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1435 * Join consumer thread
1437 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1442 if (consumer_data
->pid
!= 0) {
1443 ret
= kill(consumer_data
->pid
, SIGTERM
);
1445 ERR("Error killing consumer daemon");
1448 return pthread_join(consumer_data
->thread
, &status
);
1455 * Fork and exec a consumer daemon (consumerd).
1457 * Return pid if successful else -1.
1459 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1463 const char *verbosity
;
1465 DBG("Spawning consumerd");
1472 if (opt_verbose
> 1 || opt_verbose_consumer
) {
1473 verbosity
= "--verbose";
1475 verbosity
= "--quiet";
1477 switch (consumer_data
->type
) {
1478 case LTTNG_CONSUMER_KERNEL
:
1479 execl(INSTALL_BIN_PATH
"/lttng-consumerd",
1480 "lttng-consumerd", verbosity
, "-k", NULL
);
1482 case LTTNG_CONSUMER_UST
:
1483 execl(INSTALL_BIN_PATH
"/lttng-consumerd",
1484 "lttng-consumerd", verbosity
, "-u", NULL
);
1487 perror("unknown consumer type");
1491 perror("kernel start consumer exec");
1494 } else if (pid
> 0) {
1497 perror("start consumer fork");
1504 * Spawn the consumerd daemon and session daemon thread.
1506 static int start_consumerd(struct consumer_data
*consumer_data
)
1510 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1511 if (consumer_data
->pid
!= 0) {
1512 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1516 ret
= spawn_consumerd(consumer_data
);
1518 ERR("Spawning consumerd failed");
1519 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1523 /* Setting up the consumer_data pid */
1524 consumer_data
->pid
= ret
;
1525 DBG2("Consumer pid %d", consumer_data
->pid
);
1526 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1528 DBG2("Spawning consumer control thread");
1529 ret
= spawn_consumer_thread(consumer_data
);
1531 ERR("Fatal error spawning consumer control thread");
1543 * modprobe_kernel_modules
1545 static int modprobe_kernel_modules(void)
1550 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1551 ret
= snprintf(modprobe
, sizeof(modprobe
),
1552 "/sbin/modprobe %s%s",
1553 kernel_modules_list
[i
].required
? "" : "-q ",
1554 kernel_modules_list
[i
].name
);
1556 perror("snprintf modprobe");
1559 modprobe
[sizeof(modprobe
) - 1] = '\0';
1560 ret
= system(modprobe
);
1562 ERR("Unable to launch modprobe for module %s",
1563 kernel_modules_list
[i
].name
);
1564 } else if (kernel_modules_list
[i
].required
1565 && WEXITSTATUS(ret
) != 0) {
1566 ERR("Unable to load module %s",
1567 kernel_modules_list
[i
].name
);
1569 DBG("Modprobe successfully %s",
1570 kernel_modules_list
[i
].name
);
1581 static int mount_debugfs(char *path
)
1584 char *type
= "debugfs";
1586 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1588 PERROR("Cannot create debugfs path");
1592 ret
= mount(type
, path
, type
, 0, NULL
);
1594 PERROR("Cannot mount debugfs");
1598 DBG("Mounted debugfs successfully at %s", path
);
1605 * Setup necessary data for kernel tracer action.
1607 static void init_kernel_tracer(void)
1610 char *proc_mounts
= "/proc/mounts";
1612 char *debugfs_path
= NULL
, *lttng_path
= NULL
;
1615 /* Detect debugfs */
1616 fp
= fopen(proc_mounts
, "r");
1618 ERR("Unable to probe %s", proc_mounts
);
1622 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1623 if (strstr(line
, "debugfs") != NULL
) {
1624 /* Remove first string */
1626 /* Dup string here so we can reuse line later on */
1627 debugfs_path
= strdup(strtok(NULL
, " "));
1628 DBG("Got debugfs path : %s", debugfs_path
);
1635 /* Mount debugfs if needded */
1636 if (debugfs_path
== NULL
) {
1637 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1639 perror("asprintf debugfs path");
1642 ret
= mount_debugfs(debugfs_path
);
1644 perror("Cannot mount debugfs");
1649 /* Modprobe lttng kernel modules */
1650 ret
= modprobe_kernel_modules();
1655 /* Setup lttng kernel path */
1656 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1658 perror("asprintf lttng path");
1662 /* Open debugfs lttng */
1663 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1664 if (kernel_tracer_fd
< 0) {
1665 DBG("Failed to open %s", lttng_path
);
1671 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1681 WARN("No kernel tracer available");
1682 kernel_tracer_fd
= 0;
1687 * Init tracing by creating trace directory and sending fds kernel consumer.
1689 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1693 if (session
->consumer_fds_sent
== 0) {
1695 * Assign default kernel consumer socket if no consumer assigned to the
1696 * kernel session. At this point, it's NOT suppose to be 0 but this is
1697 * an extra security check.
1699 if (session
->consumer_fd
== 0) {
1700 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1703 ret
= send_kconsumer_session_streams(&kconsumer_data
, session
);
1705 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1709 session
->consumer_fds_sent
= 1;
1717 * Create an UST session and add it to the session ust list.
1719 static int create_ust_session(struct ltt_session
*session
,
1720 struct lttng_domain
*domain
)
1724 struct ltt_ust_session
*lus
= NULL
;
1726 switch (domain
->type
) {
1727 case LTTNG_DOMAIN_UST
:
1730 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
1734 DBG("Creating UST session");
1736 session_lock_list();
1737 uid
= session_list_ptr
->count
;
1738 session_unlock_list();
1740 lus
= trace_ust_create_session(session
->path
, uid
, domain
);
1742 ret
= LTTCOMM_UST_SESS_FAIL
;
1746 ret
= mkdir_recursive(lus
->pathname
, S_IRWXU
| S_IRWXG
,
1747 geteuid(), allowed_group());
1749 if (ret
!= -EEXIST
) {
1750 ERR("Trace directory creation error");
1751 ret
= LTTCOMM_UST_SESS_FAIL
;
1756 /* The domain type dictate different actions on session creation */
1757 switch (domain
->type
) {
1758 case LTTNG_DOMAIN_UST
:
1759 /* No ustctl for the global UST domain */
1764 session
->ust_session
= lus
;
1774 * Create a kernel tracer session then create the default channel.
1776 static int create_kernel_session(struct ltt_session
*session
)
1780 DBG("Creating kernel session");
1782 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1784 ret
= LTTCOMM_KERN_SESS_FAIL
;
1788 /* Set kernel consumer socket fd */
1789 if (kconsumer_data
.cmd_sock
) {
1790 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1793 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1794 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1796 if (ret
!= -EEXIST
) {
1797 ERR("Trace directory creation error");
1807 * Using the session list, filled a lttng_session array to send back to the
1808 * client for session listing.
1810 * The session list lock MUST be acquired before calling this function. Use
1811 * session_lock_list() and session_unlock_list().
1813 static void list_lttng_sessions(struct lttng_session
*sessions
)
1816 struct ltt_session
*session
;
1818 DBG("Getting all available session");
1820 * Iterate over session list and append data after the control struct in
1823 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1824 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1825 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1826 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1827 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1833 * Fill lttng_channel array of all channels.
1835 static void list_lttng_channels(struct ltt_session
*session
,
1836 struct lttng_channel
*channels
)
1839 struct ltt_kernel_channel
*kchan
;
1841 DBG("Listing channels for session %s", session
->name
);
1843 /* Kernel channels */
1844 if (session
->kernel_session
!= NULL
) {
1845 cds_list_for_each_entry(kchan
,
1846 &session
->kernel_session
->channel_list
.head
, list
) {
1847 /* Copy lttng_channel struct to array */
1848 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1849 channels
[i
].enabled
= kchan
->enabled
;
1854 /* TODO: Missing UST listing */
1858 * Fill lttng_event array of all events in the channel.
1860 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1861 struct lttng_event
*events
)
1864 * TODO: This is ONLY kernel. Need UST support.
1867 struct ltt_kernel_event
*event
;
1869 DBG("Listing events for channel %s", kchan
->channel
->name
);
1871 /* Kernel channels */
1872 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1873 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1874 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1875 events
[i
].enabled
= event
->enabled
;
1876 switch (event
->event
->instrumentation
) {
1877 case LTTNG_KERNEL_TRACEPOINT
:
1878 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1880 case LTTNG_KERNEL_KPROBE
:
1881 case LTTNG_KERNEL_KRETPROBE
:
1882 events
[i
].type
= LTTNG_EVENT_PROBE
;
1883 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1884 sizeof(struct lttng_kernel_kprobe
));
1886 case LTTNG_KERNEL_FUNCTION
:
1887 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1888 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1889 sizeof(struct lttng_kernel_function
));
1891 case LTTNG_KERNEL_NOOP
:
1892 events
[i
].type
= LTTNG_EVENT_NOOP
;
1894 case LTTNG_KERNEL_SYSCALL
:
1895 events
[i
].type
= LTTNG_EVENT_SYSCALL
;
1897 case LTTNG_KERNEL_ALL
:
1906 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1908 static int cmd_disable_channel(struct ltt_session
*session
,
1909 int domain
, char *channel_name
)
1914 case LTTNG_DOMAIN_KERNEL
:
1915 ret
= channel_kernel_disable(session
->kernel_session
,
1917 if (ret
!= LTTCOMM_OK
) {
1921 kernel_wait_quiescent(kernel_tracer_fd
);
1923 case LTTNG_DOMAIN_UST_PID
:
1926 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
1937 * Copy channel from attributes and set it in the application channel list.
1940 static int copy_ust_channel_to_app(struct ltt_ust_session *usess,
1941 struct lttng_channel *attr, struct ust_app *app)
1944 struct ltt_ust_channel *uchan, *new_chan;
1946 uchan = trace_ust_get_channel_by_key(usess->channels, attr->name);
1947 if (uchan == NULL) {
1948 ret = LTTCOMM_FATAL;
1952 new_chan = trace_ust_create_channel(attr, usess->path);
1953 if (new_chan == NULL) {
1954 PERROR("malloc ltt_ust_channel");
1955 ret = LTTCOMM_FATAL;
1959 ret = channel_ust_copy(new_chan, uchan);
1961 ret = LTTCOMM_FATAL;
1971 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1973 static int cmd_enable_channel(struct ltt_session
*session
,
1974 struct lttng_domain
*domain
, struct lttng_channel
*attr
)
1977 struct ltt_ust_session
*usess
= session
->ust_session
;
1979 DBG("Enabling channel %s for session %s", session
->name
, attr
->name
);
1981 switch (domain
->type
) {
1982 case LTTNG_DOMAIN_KERNEL
:
1984 struct ltt_kernel_channel
*kchan
;
1986 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1987 session
->kernel_session
);
1988 if (kchan
== NULL
) {
1989 ret
= channel_kernel_create(session
->kernel_session
,
1990 attr
, kernel_poll_pipe
[1]);
1992 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1995 if (ret
!= LTTCOMM_OK
) {
1999 kernel_wait_quiescent(kernel_tracer_fd
);
2002 case LTTNG_DOMAIN_UST
:
2004 struct ltt_ust_channel
*uchan
;
2006 DBG2("Enabling channel for LTTNG_DOMAIN_UST");
2008 /* Get channel in global UST domain HT */
2009 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2011 if (uchan
== NULL
) {
2012 uchan
= trace_ust_create_channel(attr
, usess
->pathname
);
2013 if (uchan
== NULL
) {
2014 ret
= LTTCOMM_UST_CHAN_FAIL
;
2018 hashtable_add_unique(usess
->domain_global
.channels
, &uchan
->node
);
2020 DBG2("UST channel %s added to global domain HT", attr
->name
);
2022 ret
= LTTCOMM_UST_CHAN_EXIST
;
2026 ret
= ust_app_add_channel(usess
, uchan
);
2027 if (ret
!= LTTCOMM_OK
) {
2033 case LTTNG_DOMAIN_UST_PID
:
2037 struct ltt_ust_channel *uchan;
2038 struct ltt_ust_session *usess;
2039 struct ust_app *app;
2041 usess = trace_ust_get_session_by_pid(&session->ust_session_list,
2043 if (usess == NULL) {
2044 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2048 app = ust_app_get_by_pid(domain->attr.pid);
2050 ret = LTTCOMM_APP_NOT_FOUND;
2055 uchan = trace_ust_get_channel_by_name(attr->name, usess);
2056 if (uchan == NULL) {
2057 ret = channel_ust_create(usess, attr, sock);
2059 ret = channel_ust_enable(usess, uchan, sock);
2062 if (ret != LTTCOMM_OK) {
2066 ret = copy_ust_channel_to_app(usess, attr, app);
2067 if (ret != LTTCOMM_OK) {
2071 DBG("UST channel %s created for app sock %d with pid %d",
2072 attr->name, app->sock, domain->attr.pid);
2074 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2078 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2089 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2091 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2092 char *channel_name
, char *event_name
)
2097 case LTTNG_DOMAIN_KERNEL
:
2099 struct ltt_kernel_channel
*kchan
;
2101 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2102 session
->kernel_session
);
2103 if (kchan
== NULL
) {
2104 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2108 ret
= event_kernel_disable_tracepoint(session
->kernel_session
, kchan
, event_name
);
2109 if (ret
!= LTTCOMM_OK
) {
2113 kernel_wait_quiescent(kernel_tracer_fd
);
2116 case LTTNG_DOMAIN_UST
:
2117 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2118 case LTTNG_DOMAIN_UST_PID
:
2119 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2121 /* TODO: Other UST domains */
2122 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2133 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2135 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2139 struct ltt_kernel_channel
*kchan
;
2142 case LTTNG_DOMAIN_KERNEL
:
2143 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2144 session
->kernel_session
);
2145 if (kchan
== NULL
) {
2146 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2150 ret
= event_kernel_disable_all(session
->kernel_session
, kchan
);
2151 if (ret
!= LTTCOMM_OK
) {
2155 kernel_wait_quiescent(kernel_tracer_fd
);
2158 /* TODO: Userspace tracing */
2159 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2170 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2172 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2173 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2178 case LTTNG_DOMAIN_KERNEL
:
2179 /* Add kernel context to kernel tracer */
2180 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2181 event_name
, channel_name
);
2182 if (ret
!= LTTCOMM_OK
) {
2186 case LTTNG_DOMAIN_UST
:
2189 struct ltt_ust_session *usess;
2191 cds_list_for_each_entry(usess, &session->ust_session_list.head, list) {
2192 ret = context_ust_add(usess, ctx,
2193 event_name, channel_name, domain);
2194 if (ret != LTTCOMM_OK) {
2202 /* TODO: UST other domains */
2203 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2214 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2216 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2217 char *channel_name
, struct lttng_event
*event
)
2220 struct lttng_channel
*attr
;
2221 struct ltt_ust_session
*usess
= session
->ust_session
;
2224 case LTTNG_DOMAIN_KERNEL
:
2226 struct ltt_kernel_channel
*kchan
;
2228 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2229 session
->kernel_session
);
2230 if (kchan
== NULL
) {
2231 attr
= channel_new_default_attr(domain
);
2233 ret
= LTTCOMM_FATAL
;
2236 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2238 /* This call will notify the kernel thread */
2239 ret
= channel_kernel_create(session
->kernel_session
,
2240 attr
, kernel_poll_pipe
[1]);
2241 if (ret
!= LTTCOMM_OK
) {
2246 /* Get the newly created kernel channel pointer */
2247 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2248 session
->kernel_session
);
2249 if (kchan
== NULL
) {
2250 /* This sould not happen... */
2251 ret
= LTTCOMM_FATAL
;
2255 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
2257 if (ret
!= LTTCOMM_OK
) {
2261 kernel_wait_quiescent(kernel_tracer_fd
);
2264 case LTTNG_DOMAIN_UST
:
2266 struct ltt_ust_channel
*uchan
;
2267 struct ltt_ust_event
*uevent
;
2269 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2271 if (uchan
== NULL
) {
2272 /* TODO: Create default channel */
2273 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2277 uevent
= trace_ust_find_event_by_name(uchan
->events
, event
->name
);
2278 if (uevent
== NULL
) {
2279 uevent
= trace_ust_create_event(event
);
2280 if (uevent
== NULL
) {
2281 ret
= LTTCOMM_FATAL
;
2286 ret
= ust_app_add_event(usess
, uchan
, uevent
);
2288 ret
= LTTCOMM_UST_ENABLE_FAIL
;
2293 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2294 case LTTNG_DOMAIN_UST_PID
:
2295 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2297 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2308 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2310 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2311 char *channel_name
, int event_type
)
2314 struct ltt_kernel_channel
*kchan
;
2317 case LTTNG_DOMAIN_KERNEL
:
2318 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2319 session
->kernel_session
);
2320 if (kchan
== NULL
) {
2321 /* This call will notify the kernel thread */
2322 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2323 kernel_poll_pipe
[1]);
2324 if (ret
!= LTTCOMM_OK
) {
2329 /* Get the newly created kernel channel pointer */
2330 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2331 session
->kernel_session
);
2332 if (kchan
== NULL
) {
2333 /* This sould not happen... */
2334 ret
= LTTCOMM_FATAL
;
2338 switch (event_type
) {
2339 case LTTNG_KERNEL_SYSCALL
:
2340 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2341 kchan
, kernel_tracer_fd
);
2343 case LTTNG_KERNEL_TRACEPOINT
:
2345 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2346 * events already registered to the channel.
2348 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
2349 kchan
, kernel_tracer_fd
);
2351 case LTTNG_KERNEL_ALL
:
2352 /* Enable syscalls and tracepoints */
2353 ret
= event_kernel_enable_all(session
->kernel_session
,
2354 kchan
, kernel_tracer_fd
);
2357 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2360 if (ret
!= LTTCOMM_OK
) {
2364 kernel_wait_quiescent(kernel_tracer_fd
);
2367 /* TODO: Userspace tracing */
2368 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2379 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2381 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
2384 ssize_t nb_events
= 0;
2387 case LTTNG_DOMAIN_KERNEL
:
2388 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2389 if (nb_events
< 0) {
2390 ret
= LTTCOMM_KERN_LIST_FAIL
;
2395 /* TODO: Userspace listing */
2396 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2403 /* Return negative value to differentiate return code */
2408 * Command LTTNG_START_TRACE processed by the client thread.
2410 static int cmd_start_trace(struct ltt_session
*session
)
2413 struct ltt_kernel_session
*ksession
;
2414 struct ltt_ust_session
*usess
= session
->ust_session
;
2417 ksession
= session
->kernel_session
;
2419 /* Kernel tracing */
2420 if (ksession
!= NULL
) {
2421 struct ltt_kernel_channel
*kchan
;
2423 /* Open kernel metadata */
2424 if (ksession
->metadata
== NULL
) {
2425 ret
= kernel_open_metadata(ksession
, ksession
->trace_path
);
2427 ret
= LTTCOMM_KERN_META_FAIL
;
2432 /* Open kernel metadata stream */
2433 if (ksession
->metadata_stream_fd
== 0) {
2434 ret
= kernel_open_metadata_stream(ksession
);
2436 ERR("Kernel create metadata stream failed");
2437 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2442 /* For each channel */
2443 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2444 if (kchan
->stream_count
== 0) {
2445 ret
= kernel_open_channel_stream(kchan
);
2447 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2450 /* Update the stream global counter */
2451 ksession
->stream_count_global
+= ret
;
2455 /* Setup kernel consumer socket and send fds to it */
2456 ret
= init_kernel_tracing(ksession
);
2458 ret
= LTTCOMM_KERN_START_FAIL
;
2462 /* This start the kernel tracing */
2463 ret
= kernel_start_session(ksession
);
2465 ret
= LTTCOMM_KERN_START_FAIL
;
2469 /* Quiescent wait after starting trace */
2470 kernel_wait_quiescent(kernel_tracer_fd
);
2473 ret
= ust_app_start_trace(usess
);
2475 ret
= LTTCOMM_UST_START_FAIL
;
2486 * Command LTTNG_STOP_TRACE processed by the client thread.
2488 static int cmd_stop_trace(struct ltt_session
*session
)
2491 struct ltt_kernel_channel
*kchan
;
2492 struct ltt_kernel_session
*ksession
;
2493 //struct ltt_ust_session *usess;
2494 //struct ltt_ust_channel *ustchan;
2497 ksession
= session
->kernel_session
;
2500 if (ksession
!= NULL
) {
2501 DBG("Stop kernel tracing");
2503 /* Flush all buffers before stopping */
2504 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2506 ERR("Kernel metadata flush failed");
2509 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2510 ret
= kernel_flush_buffer(kchan
);
2512 ERR("Kernel flush buffer error");
2516 ret
= kernel_stop_session(ksession
);
2518 ret
= LTTCOMM_KERN_STOP_FAIL
;
2522 kernel_wait_quiescent(kernel_tracer_fd
);
2526 /* Stop each UST session */
2527 DBG("Stop UST tracing");
2528 cds_list_for_each_entry(usess
, &session
->ust_session_list
.head
, list
) {
2529 /* Flush all buffers before stopping */
2530 ret
= ustctl_flush_buffer(usess
->sock
, usess
->metadata
->obj
);
2532 ERR("UST metadata flush failed");
2535 cds_list_for_each_entry(ustchan
, &usess
->channels
.head
, list
) {
2536 ret
= ustctl_flush_buffer(usess
->sock
, ustchan
->obj
);
2538 ERR("UST flush buffer error");
2542 ret
= ustctl_stop_session(usess
->sock
, usess
->handle
);
2544 ret
= LTTCOMM_KERN_STOP_FAIL
;
2548 ustctl_wait_quiescent(usess
->sock
);
2559 * Command LTTNG_CREATE_SESSION processed by the client thread.
2561 static int cmd_create_session(char *name
, char *path
)
2565 ret
= session_create(name
, path
);
2566 if (ret
!= LTTCOMM_OK
) {
2577 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2579 static int cmd_destroy_session(struct ltt_session
*session
, char *name
)
2583 /* Clean kernel session teardown */
2584 teardown_kernel_session(session
);
2587 * Must notify the kernel thread here to update it's poll setin order
2588 * to remove the channel(s)' fd just destroyed.
2590 ret
= notify_thread_pipe(kernel_poll_pipe
[1]);
2592 perror("write kernel poll pipe");
2595 ret
= session_destroy(session
);
2601 * Command LTTNG_CALIBRATE processed by the client thread.
2603 static int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2608 case LTTNG_DOMAIN_KERNEL
:
2610 struct lttng_kernel_calibrate kcalibrate
;
2612 kcalibrate
.type
= calibrate
->type
;
2613 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2615 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2621 /* TODO: Userspace tracing */
2622 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2633 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2635 static int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2641 case LTTNG_DOMAIN_KERNEL
:
2642 /* Can't register a consumer if there is already one */
2643 if (session
->kernel_session
->consumer_fd
!= 0) {
2644 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
2648 sock
= lttcomm_connect_unix_sock(sock_path
);
2650 ret
= LTTCOMM_CONNECT_FAIL
;
2654 session
->kernel_session
->consumer_fd
= sock
;
2657 /* TODO: Userspace tracing */
2658 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2669 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2671 static ssize_t
cmd_list_domains(struct ltt_session
*session
,
2672 struct lttng_domain
**domains
)
2677 if (session
->kernel_session
!= NULL
) {
2681 /* TODO: User-space tracer domain support */
2683 *domains
= malloc(nb_dom
* sizeof(struct lttng_domain
));
2684 if (*domains
== NULL
) {
2685 ret
= -LTTCOMM_FATAL
;
2689 (*domains
)[0].type
= LTTNG_DOMAIN_KERNEL
;
2698 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2700 static ssize_t
cmd_list_channels(struct ltt_session
*session
,
2701 struct lttng_channel
**channels
)
2704 ssize_t nb_chan
= 0;
2706 if (session
->kernel_session
!= NULL
) {
2707 nb_chan
+= session
->kernel_session
->channel_count
;
2710 *channels
= malloc(nb_chan
* sizeof(struct lttng_channel
));
2711 if (*channels
== NULL
) {
2712 ret
= -LTTCOMM_FATAL
;
2716 list_lttng_channels(session
, *channels
);
2718 /* TODO UST support */
2727 * Command LTTNG_LIST_EVENTS processed by the client thread.
2729 static ssize_t
cmd_list_events(struct ltt_session
*session
,
2730 char *channel_name
, struct lttng_event
**events
)
2733 ssize_t nb_event
= 0;
2734 struct ltt_kernel_channel
*kchan
= NULL
;
2736 if (session
->kernel_session
!= NULL
) {
2737 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2738 session
->kernel_session
);
2739 if (kchan
== NULL
) {
2740 ret
= -LTTCOMM_KERN_CHAN_NOT_FOUND
;
2743 nb_event
+= kchan
->event_count
;
2746 *events
= malloc(nb_event
* sizeof(struct lttng_event
));
2747 if (*events
== NULL
) {
2748 ret
= -LTTCOMM_FATAL
;
2752 list_lttng_events(kchan
, *events
);
2754 /* TODO: User-space tracer support */
2763 * Process the command requested by the lttng client within the command
2764 * context structure. This function make sure that the return structure (llm)
2765 * is set and ready for transmission before returning.
2767 * Return any error encountered or 0 for success.
2769 static int process_client_msg(struct command_ctx
*cmd_ctx
)
2771 int ret
= LTTCOMM_OK
;
2772 int need_tracing_session
= 1;
2774 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2777 * Check for command that don't needs to allocate a returned payload. We do
2778 * this here so we don't have to make the call for no payload at each
2781 switch(cmd_ctx
->lsm
->cmd_type
) {
2782 case LTTNG_LIST_SESSIONS
:
2783 case LTTNG_LIST_TRACEPOINTS
:
2784 case LTTNG_LIST_DOMAINS
:
2785 case LTTNG_LIST_CHANNELS
:
2786 case LTTNG_LIST_EVENTS
:
2789 /* Setup lttng message with no payload */
2790 ret
= setup_lttng_msg(cmd_ctx
, 0);
2792 /* This label does not try to unlock the session */
2793 goto init_setup_error
;
2797 /* Commands that DO NOT need a session. */
2798 switch (cmd_ctx
->lsm
->cmd_type
) {
2799 case LTTNG_CALIBRATE
:
2800 case LTTNG_CREATE_SESSION
:
2801 case LTTNG_LIST_SESSIONS
:
2802 case LTTNG_LIST_TRACEPOINTS
:
2803 need_tracing_session
= 0;
2806 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2807 session_lock_list();
2808 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2809 session_unlock_list();
2810 if (cmd_ctx
->session
== NULL
) {
2811 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
2812 ret
= LTTCOMM_SESS_NOT_FOUND
;
2814 /* If no session name specified */
2815 ret
= LTTCOMM_SELECT_SESS
;
2819 /* Acquire lock for the session */
2820 session_lock(cmd_ctx
->session
);
2826 * Check domain type for specific "pre-action".
2828 switch (cmd_ctx
->lsm
->domain
.type
) {
2829 case LTTNG_DOMAIN_KERNEL
:
2830 /* Kernel tracer check */
2831 if (kernel_tracer_fd
== 0) {
2832 /* Basically, load kernel tracer modules */
2833 init_kernel_tracer();
2834 if (kernel_tracer_fd
== 0) {
2835 ret
= LTTCOMM_KERN_NA
;
2840 /* Need a session for kernel command */
2841 if (need_tracing_session
) {
2842 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2843 ret
= create_kernel_session(cmd_ctx
->session
);
2845 ret
= LTTCOMM_KERN_SESS_FAIL
;
2850 /* Start the kernel consumer daemon */
2851 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2852 if (kconsumer_data
.pid
== 0 &&
2853 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2854 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2855 ret
= start_consumerd(&kconsumer_data
);
2857 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
2861 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2864 case LTTNG_DOMAIN_UST
:
2866 if (need_tracing_session
) {
2867 if (cmd_ctx
->session
->ust_session
== NULL
) {
2868 ret
= create_ust_session(cmd_ctx
->session
,
2869 &cmd_ctx
->lsm
->domain
);
2870 if (ret
!= LTTCOMM_OK
) {
2874 /* Start the kernel consumer daemon */
2875 pthread_mutex_lock(&ustconsumer_data
.pid_mutex
);
2876 if (ustconsumer_data
.pid
== 0 &&
2877 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2878 pthread_mutex_unlock(&ustconsumer_data
.pid_mutex
);
2879 ret
= start_consumerd(&ustconsumer_data
);
2881 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
2885 cmd_ctx
->session
->ust_session
->consumer_fd
=
2886 ustconsumer_data
.cmd_sock
;
2888 pthread_mutex_unlock(&ustconsumer_data
.pid_mutex
);
2896 /* Process by command type */
2897 switch (cmd_ctx
->lsm
->cmd_type
) {
2898 case LTTNG_ADD_CONTEXT
:
2900 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2901 cmd_ctx
->lsm
->u
.context
.channel_name
,
2902 cmd_ctx
->lsm
->u
.context
.event_name
,
2903 &cmd_ctx
->lsm
->u
.context
.ctx
);
2906 case LTTNG_DISABLE_CHANNEL
:
2908 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2909 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2912 case LTTNG_DISABLE_EVENT
:
2914 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2915 cmd_ctx
->lsm
->u
.disable
.channel_name
,
2916 cmd_ctx
->lsm
->u
.disable
.name
);
2920 case LTTNG_DISABLE_ALL_EVENT
:
2922 DBG("Disabling all events");
2924 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2925 cmd_ctx
->lsm
->u
.disable
.channel_name
);
2928 case LTTNG_ENABLE_CHANNEL
:
2930 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
2931 &cmd_ctx
->lsm
->u
.channel
.chan
);
2934 case LTTNG_ENABLE_EVENT
:
2936 ret
= cmd_enable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2937 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2938 &cmd_ctx
->lsm
->u
.enable
.event
);
2941 case LTTNG_ENABLE_ALL_EVENT
:
2943 DBG("Enabling all events");
2945 ret
= cmd_enable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2946 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2947 cmd_ctx
->lsm
->u
.enable
.event
.type
);
2950 case LTTNG_LIST_TRACEPOINTS
:
2952 struct lttng_event
*events
;
2955 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
2956 if (nb_events
< 0) {
2962 * Setup lttng message with payload size set to the event list size in
2963 * bytes and then copy list into the llm payload.
2965 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2971 /* Copy event list into message payload */
2972 memcpy(cmd_ctx
->llm
->payload
, events
,
2973 sizeof(struct lttng_event
) * nb_events
);
2980 case LTTNG_START_TRACE
:
2982 ret
= cmd_start_trace(cmd_ctx
->session
);
2985 case LTTNG_STOP_TRACE
:
2987 ret
= cmd_stop_trace(cmd_ctx
->session
);
2990 case LTTNG_CREATE_SESSION
:
2992 tracepoint(create_session_start
);
2993 ret
= cmd_create_session(cmd_ctx
->lsm
->session
.name
,
2994 cmd_ctx
->lsm
->session
.path
);
2995 tracepoint(create_session_end
);
2998 case LTTNG_DESTROY_SESSION
:
3000 tracepoint(destroy_session_start
);
3001 ret
= cmd_destroy_session(cmd_ctx
->session
,
3002 cmd_ctx
->lsm
->session
.name
);
3003 tracepoint(destroy_session_end
);
3006 case LTTNG_LIST_DOMAINS
:
3009 struct lttng_domain
*domains
;
3011 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3017 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3022 /* Copy event list into message payload */
3023 memcpy(cmd_ctx
->llm
->payload
, domains
,
3024 nb_dom
* sizeof(struct lttng_domain
));
3031 case LTTNG_LIST_CHANNELS
:
3034 struct lttng_channel
*channels
;
3036 nb_chan
= cmd_list_channels(cmd_ctx
->session
, &channels
);
3042 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3047 /* Copy event list into message payload */
3048 memcpy(cmd_ctx
->llm
->payload
, channels
,
3049 nb_chan
* sizeof(struct lttng_channel
));
3056 case LTTNG_LIST_EVENTS
:
3059 struct lttng_event
*events
= NULL
;
3061 nb_event
= cmd_list_events(cmd_ctx
->session
,
3062 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3068 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3073 /* Copy event list into message payload */
3074 memcpy(cmd_ctx
->llm
->payload
, events
,
3075 nb_event
* sizeof(struct lttng_event
));
3082 case LTTNG_LIST_SESSIONS
:
3084 session_lock_list();
3086 if (session_list_ptr
->count
== 0) {
3087 ret
= LTTCOMM_NO_SESSION
;
3088 session_unlock_list();
3092 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
3093 session_list_ptr
->count
);
3095 session_unlock_list();
3099 /* Filled the session array */
3100 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
3102 session_unlock_list();
3107 case LTTNG_CALIBRATE
:
3109 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3110 &cmd_ctx
->lsm
->u
.calibrate
);
3113 case LTTNG_REGISTER_CONSUMER
:
3115 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3116 cmd_ctx
->lsm
->u
.reg
.path
);
3125 if (cmd_ctx
->llm
== NULL
) {
3126 DBG("Missing llm structure. Allocating one.");
3127 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3131 /* Set return code */
3132 cmd_ctx
->llm
->ret_code
= ret
;
3134 if (cmd_ctx
->session
) {
3135 session_unlock(cmd_ctx
->session
);
3142 * This thread manage all clients request using the unix client socket for
3145 static void *thread_manage_clients(void *data
)
3147 int sock
= 0, ret
, i
, pollfd
;
3148 uint32_t revents
, nb_fd
;
3149 struct command_ctx
*cmd_ctx
= NULL
;
3150 struct lttng_poll_event events
;
3152 tracepoint(sessiond_th_cli_start
);
3154 DBG("[thread] Manage client started");
3156 rcu_register_thread();
3158 ret
= lttcomm_listen_unix_sock(client_sock
);
3164 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3165 * more will be added to this poll set.
3167 ret
= create_thread_poll_set(&events
, 2);
3172 /* Add the application registration socket */
3173 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3179 * Notify parent pid that we are ready to accept command for client side.
3181 if (opt_sig_parent
) {
3182 kill(ppid
, SIGCHLD
);
3186 DBG("Accepting client command ...");
3188 tracepoint(sessiond_th_cli_poll
);
3190 nb_fd
= LTTNG_POLL_GETNB(&events
);
3192 /* Inifinite blocking call, waiting for transmission */
3193 ret
= lttng_poll_wait(&events
, -1);
3198 for (i
= 0; i
< nb_fd
; i
++) {
3199 /* Fetch once the poll data */
3200 revents
= LTTNG_POLL_GETEV(&events
, i
);
3201 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3203 /* Thread quit pipe has been closed. Killing thread. */
3204 ret
= check_thread_quit_pipe(pollfd
, revents
);
3209 /* Event on the registration socket */
3210 if (pollfd
== client_sock
) {
3211 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3212 ERR("Client socket poll error");
3218 DBG("Wait for client response");
3220 sock
= lttcomm_accept_unix_sock(client_sock
);
3225 /* Allocate context command to process the client request */
3226 cmd_ctx
= malloc(sizeof(struct command_ctx
));
3227 if (cmd_ctx
== NULL
) {
3228 perror("malloc cmd_ctx");
3232 /* Allocate data buffer for reception */
3233 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
3234 if (cmd_ctx
->lsm
== NULL
) {
3235 perror("malloc cmd_ctx->lsm");
3239 cmd_ctx
->llm
= NULL
;
3240 cmd_ctx
->session
= NULL
;
3243 * Data is received from the lttng client. The struct
3244 * lttcomm_session_msg (lsm) contains the command and data request of
3247 DBG("Receiving data from client ...");
3248 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
,
3249 sizeof(struct lttcomm_session_msg
));
3251 DBG("Nothing recv() from client... continuing");
3257 // TODO: Validate cmd_ctx including sanity check for
3258 // security purpose.
3260 rcu_thread_online();
3262 * This function dispatch the work to the kernel or userspace tracer
3263 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3264 * informations for the client. The command context struct contains
3265 * everything this function may needs.
3267 ret
= process_client_msg(cmd_ctx
);
3268 rcu_thread_offline();
3271 * TODO: Inform client somehow of the fatal error. At
3272 * this point, ret < 0 means that a malloc failed
3273 * (ENOMEM). Error detected but still accept command.
3275 clean_command_ctx(&cmd_ctx
);
3279 DBG("Sending response (size: %d, retcode: %s)",
3280 cmd_ctx
->lttng_msg_size
,
3281 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3282 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3284 ERR("Failed to send data back to client");
3287 clean_command_ctx(&cmd_ctx
);
3289 /* End of transmission */
3294 DBG("Client thread dying");
3295 unlink(client_unix_sock_path
);
3299 lttng_poll_clean(&events
);
3300 clean_command_ctx(&cmd_ctx
);
3302 rcu_unregister_thread();
3308 * usage function on stderr
3310 static void usage(void)
3312 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
3313 fprintf(stderr
, " -h, --help Display this usage.\n");
3314 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
3315 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3316 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3317 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3318 fprintf(stderr
, " --ustconsumerd-err-sock PATH Specify path for the UST consumer error socket\n");
3319 fprintf(stderr
, " --ustconsumerd-cmd-sock PATH Specify path for the UST consumer command socket\n");
3320 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
3321 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3322 fprintf(stderr
, " -V, --version Show version number.\n");
3323 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3324 fprintf(stderr
, " -q, --quiet No output at all.\n");
3325 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3326 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3330 * daemon argument parsing
3332 static int parse_args(int argc
, char **argv
)
3336 static struct option long_options
[] = {
3337 { "client-sock", 1, 0, 'c' },
3338 { "apps-sock", 1, 0, 'a' },
3339 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3340 { "kconsumerd-err-sock", 1, 0, 'E' },
3341 { "ustconsumerd-cmd-sock", 1, 0, 'D' },
3342 { "ustconsumerd-err-sock", 1, 0, 'F' },
3343 { "daemonize", 0, 0, 'd' },
3344 { "sig-parent", 0, 0, 'S' },
3345 { "help", 0, 0, 'h' },
3346 { "group", 1, 0, 'g' },
3347 { "version", 0, 0, 'V' },
3348 { "quiet", 0, 0, 'q' },
3349 { "verbose", 0, 0, 'v' },
3350 { "verbose-consumer", 0, 0, 'Z' },
3355 int option_index
= 0;
3356 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:C:E:D:F:Z",
3357 long_options
, &option_index
);
3364 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
3366 fprintf(stderr
, " with arg %s\n", optarg
);
3370 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3373 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3379 opt_tracing_group
= strdup(optarg
);
3385 fprintf(stdout
, "%s\n", VERSION
);
3391 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3394 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3397 snprintf(ustconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3400 snprintf(ustconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
3406 /* Verbose level can increase using multiple -v */
3410 opt_verbose_consumer
+= 1;
3413 /* Unknown option or other error.
3414 * Error is printed by getopt, just return */
3423 * Creates the two needed socket by the daemon.
3424 * apps_sock - The communication socket for all UST apps.
3425 * client_sock - The communication of the cli tool (lttng).
3427 static int init_daemon_socket(void)
3432 old_umask
= umask(0);
3434 /* Create client tool unix socket */
3435 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
3436 if (client_sock
< 0) {
3437 ERR("Create unix sock failed: %s", client_unix_sock_path
);
3442 /* File permission MUST be 660 */
3443 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3445 ERR("Set file permissions failed: %s", client_unix_sock_path
);
3450 /* Create the application unix socket */
3451 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3452 if (apps_sock
< 0) {
3453 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3458 /* File permission MUST be 666 */
3459 ret
= chmod(apps_unix_sock_path
,
3460 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3462 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3473 * Check if the global socket is available, and if a daemon is answering at the
3474 * other side. If yes, error is returned.
3476 static int check_existing_daemon(void)
3478 if (access(client_unix_sock_path
, F_OK
) < 0 &&
3479 access(apps_unix_sock_path
, F_OK
) < 0) {
3483 /* Is there anybody out there ? */
3484 if (lttng_session_daemon_alive()) {
3492 * Set the tracing group gid onto the client socket.
3494 * Race window between mkdir and chown is OK because we are going from more
3495 * permissive (root.root) to les permissive (root.tracing).
3497 static int set_permissions(void)
3502 gid
= allowed_group();
3505 WARN("No tracing group detected");
3508 ERR("Missing tracing group. Aborting execution.");
3514 /* Set lttng run dir */
3515 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
3517 ERR("Unable to set group on " LTTNG_RUNDIR
);
3521 /* lttng client socket path */
3522 ret
= chown(client_unix_sock_path
, 0, gid
);
3524 ERR("Unable to set group on %s", client_unix_sock_path
);
3528 /* kconsumer error socket path */
3529 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, gid
);
3531 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
3535 /* ustconsumer error socket path */
3536 ret
= chown(ustconsumer_data
.err_unix_sock_path
, 0, gid
);
3538 ERR("Unable to set group on %s", ustconsumer_data
.err_unix_sock_path
);
3542 DBG("All permissions are set");
3549 * Create the pipe used to wake up the kernel thread.
3551 static int create_kernel_poll_pipe(void)
3553 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3557 * Create the application command pipe to wake thread_manage_apps.
3559 static int create_apps_cmd_pipe(void)
3561 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3565 * Create the lttng run directory needed for all global sockets and pipe.
3567 static int create_lttng_rundir(void)
3571 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3573 if (errno
!= EEXIST
) {
3574 ERR("Unable to create " LTTNG_RUNDIR
);
3586 * Setup sockets and directory needed by the kconsumerd communication with the
3589 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
3592 const char *path
= consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3593 KCONSUMERD_PATH
: USTCONSUMERD_PATH
;
3595 if (strlen(consumer_data
->err_unix_sock_path
) == 0) {
3596 snprintf(consumer_data
->err_unix_sock_path
, PATH_MAX
,
3597 consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3598 KCONSUMERD_ERR_SOCK_PATH
:
3599 USTCONSUMERD_ERR_SOCK_PATH
);
3602 if (strlen(consumer_data
->cmd_unix_sock_path
) == 0) {
3603 snprintf(consumer_data
->cmd_unix_sock_path
, PATH_MAX
,
3604 consumer_data
->type
== LTTNG_CONSUMER_KERNEL
?
3605 KCONSUMERD_CMD_SOCK_PATH
:
3606 USTCONSUMERD_CMD_SOCK_PATH
);
3609 ret
= mkdir(path
, S_IRWXU
| S_IRWXG
);
3611 if (errno
!= EEXIST
) {
3612 ERR("Failed to create %s", path
);
3618 /* Create the kconsumerd error unix socket */
3619 consumer_data
->err_sock
=
3620 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
3621 if (consumer_data
->err_sock
< 0) {
3622 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
3627 /* File permission MUST be 660 */
3628 ret
= chmod(consumer_data
->err_unix_sock_path
,
3629 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3631 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
3641 * Signal handler for the daemon
3643 * Simply stop all worker threads, leaving main() return gracefully after
3644 * joining all threads and calling cleanup().
3646 static void sighandler(int sig
)
3650 DBG("SIGPIPE catched");
3653 DBG("SIGINT catched");
3657 DBG("SIGTERM catched");
3666 * Setup signal handler for :
3667 * SIGINT, SIGTERM, SIGPIPE
3669 static int set_signal_handler(void)
3672 struct sigaction sa
;
3675 if ((ret
= sigemptyset(&sigset
)) < 0) {
3676 perror("sigemptyset");
3680 sa
.sa_handler
= sighandler
;
3681 sa
.sa_mask
= sigset
;
3683 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3684 perror("sigaction");
3688 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3689 perror("sigaction");
3693 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3694 perror("sigaction");
3698 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3704 * Set open files limit to unlimited. This daemon can open a large number of
3705 * file descriptors in order to consumer multiple kernel traces.
3707 static void set_ulimit(void)
3712 /* The kernel does not allowed an infinite limit for open files */
3713 lim
.rlim_cur
= 65535;
3714 lim
.rlim_max
= 65535;
3716 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3718 perror("failed to set open files limit");
3725 int main(int argc
, char **argv
)
3729 const char *home_path
;
3731 tracepoint(sessiond_boot_start
);
3732 rcu_register_thread();
3734 /* Create thread quit pipe */
3735 if ((ret
= init_thread_quit_pipe()) < 0) {
3739 /* Parse arguments */
3741 if ((ret
= parse_args(argc
, argv
) < 0)) {
3754 /* Check if daemon is UID = 0 */
3755 is_root
= !getuid();
3758 ret
= create_lttng_rundir();
3763 if (strlen(apps_unix_sock_path
) == 0) {
3764 snprintf(apps_unix_sock_path
, PATH_MAX
,
3765 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3768 if (strlen(client_unix_sock_path
) == 0) {
3769 snprintf(client_unix_sock_path
, PATH_MAX
,
3770 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3773 /* Set global SHM for ust */
3774 if (strlen(wait_shm_path
) == 0) {
3775 snprintf(wait_shm_path
, PATH_MAX
,
3776 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3779 home_path
= get_home_dir();
3780 if (home_path
== NULL
) {
3781 /* TODO: Add --socket PATH option */
3782 ERR("Can't get HOME directory for sockets creation.");
3787 if (strlen(apps_unix_sock_path
) == 0) {
3788 snprintf(apps_unix_sock_path
, PATH_MAX
,
3789 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3792 /* Set the cli tool unix socket path */
3793 if (strlen(client_unix_sock_path
) == 0) {
3794 snprintf(client_unix_sock_path
, PATH_MAX
,
3795 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3798 /* Set global SHM for ust */
3799 if (strlen(wait_shm_path
) == 0) {
3800 snprintf(wait_shm_path
, PATH_MAX
,
3801 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3805 DBG("Client socket path %s", client_unix_sock_path
);
3806 DBG("Application socket path %s", apps_unix_sock_path
);
3809 * See if daemon already exist.
3811 if ((ret
= check_existing_daemon()) < 0) {
3812 ERR("Already running daemon.\n");
3814 * We do not goto exit because we must not cleanup()
3815 * because a daemon is already running.
3820 /* After this point, we can safely call cleanup() with "goto exit" */
3823 * These actions must be executed as root. We do that *after* setting up
3824 * the sockets path because we MUST make the check for another daemon using
3825 * those paths *before* trying to set the kernel consumer sockets and init
3829 ret
= set_consumer_sockets(&kconsumer_data
);
3834 ret
= set_consumer_sockets(&ustconsumer_data
);
3838 /* Setup kernel tracer */
3839 init_kernel_tracer();
3841 /* Set ulimit for open files */
3845 if ((ret
= set_signal_handler()) < 0) {
3849 /* Setup the needed unix socket */
3850 if ((ret
= init_daemon_socket()) < 0) {
3854 /* Set credentials to socket */
3855 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3859 /* Get parent pid if -S, --sig-parent is specified. */
3860 if (opt_sig_parent
) {
3864 /* Setup the kernel pipe for waking up the kernel thread */
3865 if ((ret
= create_kernel_poll_pipe()) < 0) {
3869 /* Setup the thread apps communication pipe. */
3870 if ((ret
= create_apps_cmd_pipe()) < 0) {
3874 /* Init UST command queue. */
3875 cds_wfq_init(&ust_cmd_queue
.queue
);
3877 /* Init UST app hash table */
3881 * Get session list pointer. This pointer MUST NOT be free(). This list is
3882 * statically declared in session.c
3884 session_list_ptr
= session_get_list();
3886 /* Set up max poll set size */
3887 lttng_poll_set_max_size();
3889 /* Create thread to manage the client socket */
3890 ret
= pthread_create(&client_thread
, NULL
,
3891 thread_manage_clients
, (void *) NULL
);
3893 perror("pthread_create clients");
3897 /* Create thread to dispatch registration */
3898 ret
= pthread_create(&dispatch_thread
, NULL
,
3899 thread_dispatch_ust_registration
, (void *) NULL
);
3901 perror("pthread_create dispatch");
3905 /* Create thread to manage application registration. */
3906 ret
= pthread_create(®_apps_thread
, NULL
,
3907 thread_registration_apps
, (void *) NULL
);
3909 perror("pthread_create registration");
3913 /* Create thread to manage application socket */
3914 ret
= pthread_create(&apps_thread
, NULL
,
3915 thread_manage_apps
, (void *) NULL
);
3917 perror("pthread_create apps");
3921 /* Create kernel thread to manage kernel event */
3922 ret
= pthread_create(&kernel_thread
, NULL
,
3923 thread_manage_kernel
, (void *) NULL
);
3925 perror("pthread_create kernel");
3929 tracepoint(sessiond_boot_end
);
3931 ret
= pthread_join(kernel_thread
, &status
);
3933 perror("pthread_join");
3934 goto error
; /* join error, exit without cleanup */
3938 ret
= pthread_join(apps_thread
, &status
);
3940 perror("pthread_join");
3941 goto error
; /* join error, exit without cleanup */
3945 ret
= pthread_join(reg_apps_thread
, &status
);
3947 perror("pthread_join");
3948 goto error
; /* join error, exit without cleanup */
3952 ret
= pthread_join(dispatch_thread
, &status
);
3954 perror("pthread_join");
3955 goto error
; /* join error, exit without cleanup */
3959 ret
= pthread_join(client_thread
, &status
);
3961 perror("pthread_join");
3962 goto error
; /* join error, exit without cleanup */
3965 ret
= join_consumer_thread(&kconsumer_data
);
3967 perror("join_consumer");
3968 goto error
; /* join error, exit without cleanup */
3974 * cleanup() is called when no other thread is running.
3976 rcu_thread_online();
3978 rcu_thread_offline();
3979 rcu_unregister_thread();