2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
53 #include "lttng-sessiond.h"
54 #include "buffer-registry.h"
61 #include "kernel-consumer.h"
65 #include "ust-consumer.h"
68 #include "health-sessiond.h"
69 #include "testpoint.h"
70 #include "ust-thread.h"
71 #include "agent-thread.h"
73 #include "load-session-thread.h"
74 #include "notification-thread.h"
75 #include "notification-thread-commands.h"
78 #include "ht-cleanup.h"
79 #include "sessiond-config.h"
81 static const char *help_msg
=
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-sessiond.8.h>
90 static pid_t ppid
; /* Parent PID for --sig-parent option */
91 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
92 static int lockfile_fd
= -1;
94 /* Set to 1 when a SIGUSR1 signal is received. */
95 static int recv_child_signal
;
98 * Consumer daemon specific control data. Every value not initialized here is
99 * set to 0 by the static definition.
101 static struct consumer_data kconsumer_data
= {
102 .type
= LTTNG_CONSUMER_KERNEL
,
105 .channel_monitor_pipe
= -1,
106 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 .lock
= PTHREAD_MUTEX_INITIALIZER
,
108 .cond
= PTHREAD_COND_INITIALIZER
,
109 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 static struct consumer_data ustconsumer64_data
= {
112 .type
= LTTNG_CONSUMER64_UST
,
115 .channel_monitor_pipe
= -1,
116 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 .lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .cond
= PTHREAD_COND_INITIALIZER
,
119 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 static struct consumer_data ustconsumer32_data
= {
122 .type
= LTTNG_CONSUMER32_UST
,
125 .channel_monitor_pipe
= -1,
126 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
127 .lock
= PTHREAD_MUTEX_INITIALIZER
,
128 .cond
= PTHREAD_COND_INITIALIZER
,
129 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
132 /* Command line options */
133 static const struct option long_options
[] = {
134 { "client-sock", required_argument
, 0, 'c' },
135 { "apps-sock", required_argument
, 0, 'a' },
136 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
137 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
138 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
139 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
140 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
141 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
142 { "consumerd32-path", required_argument
, 0, '\0' },
143 { "consumerd32-libdir", required_argument
, 0, '\0' },
144 { "consumerd64-path", required_argument
, 0, '\0' },
145 { "consumerd64-libdir", required_argument
, 0, '\0' },
146 { "daemonize", no_argument
, 0, 'd' },
147 { "background", no_argument
, 0, 'b' },
148 { "sig-parent", no_argument
, 0, 'S' },
149 { "help", no_argument
, 0, 'h' },
150 { "group", required_argument
, 0, 'g' },
151 { "version", no_argument
, 0, 'V' },
152 { "quiet", no_argument
, 0, 'q' },
153 { "verbose", no_argument
, 0, 'v' },
154 { "verbose-consumer", no_argument
, 0, '\0' },
155 { "no-kernel", no_argument
, 0, '\0' },
156 { "pidfile", required_argument
, 0, 'p' },
157 { "agent-tcp-port", required_argument
, 0, '\0' },
158 { "config", required_argument
, 0, 'f' },
159 { "load", required_argument
, 0, 'l' },
160 { "kmod-probes", required_argument
, 0, '\0' },
161 { "extra-kmod-probes", required_argument
, 0, '\0' },
165 struct sessiond_config config
;
167 /* Command line options to ignore from configuration file */
168 static const char *config_ignore_options
[] = { "help", "version", "config" };
170 /* Shared between threads */
171 static int dispatch_thread_exit
;
173 /* Sockets and FDs */
174 static int client_sock
= -1;
175 static int apps_sock
= -1;
176 int kernel_tracer_fd
= -1;
177 static int kernel_poll_pipe
[2] = { -1, -1 };
180 * Quit pipe for all threads. This permits a single cancellation point
181 * for all threads when receiving an event on the pipe.
183 static int thread_quit_pipe
[2] = { -1, -1 };
186 * This pipe is used to inform the thread managing application communication
187 * that a command is queued and ready to be processed.
189 static int apps_cmd_pipe
[2] = { -1, -1 };
191 int apps_cmd_notify_pipe
[2] = { -1, -1 };
193 /* Pthread, Mutexes and Semaphores */
194 static pthread_t apps_thread
;
195 static pthread_t apps_notify_thread
;
196 static pthread_t reg_apps_thread
;
197 static pthread_t client_thread
;
198 static pthread_t kernel_thread
;
199 static pthread_t dispatch_thread
;
200 static pthread_t health_thread
;
201 static pthread_t ht_cleanup_thread
;
202 static pthread_t agent_reg_thread
;
203 static pthread_t load_session_thread
;
204 static pthread_t notification_thread
;
207 * UST registration command queue. This queue is tied with a futex and uses a N
208 * wakers / 1 waiter implemented and detailed in futex.c/.h
210 * The thread_registration_apps and thread_dispatch_ust_registration uses this
211 * queue along with the wait/wake scheme. The thread_manage_apps receives down
212 * the line new application socket and monitors it for any I/O error or clean
213 * close that triggers an unregistration of the application.
215 static struct ust_cmd_queue ust_cmd_queue
;
218 * Pointer initialized before thread creation.
220 * This points to the tracing session list containing the session count and a
221 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
222 * MUST NOT be taken if you call a public function in session.c.
224 * The lock is nested inside the structure: session_list_ptr->lock. Please use
225 * session_lock_list and session_unlock_list for lock acquisition.
227 static struct ltt_session_list
*session_list_ptr
;
229 int ust_consumerd64_fd
= -1;
230 int ust_consumerd32_fd
= -1;
232 static const char *module_proc_lttng
= "/proc/lttng";
235 * Consumer daemon state which is changed when spawning it, killing it or in
236 * case of a fatal error.
238 enum consumerd_state
{
239 CONSUMER_STARTED
= 1,
240 CONSUMER_STOPPED
= 2,
245 * This consumer daemon state is used to validate if a client command will be
246 * able to reach the consumer. If not, the client is informed. For instance,
247 * doing a "lttng start" when the consumer state is set to ERROR will return an
248 * error to the client.
250 * The following example shows a possible race condition of this scheme:
252 * consumer thread error happens
254 * client cmd checks state -> still OK
255 * consumer thread exit, sets error
256 * client cmd try to talk to consumer
259 * However, since the consumer is a different daemon, we have no way of making
260 * sure the command will reach it safely even with this state flag. This is why
261 * we consider that up to the state validation during command processing, the
262 * command is safe. After that, we can not guarantee the correctness of the
263 * client request vis-a-vis the consumer.
265 static enum consumerd_state ust_consumerd_state
;
266 static enum consumerd_state kernel_consumerd_state
;
268 /* Set in main() with the current page size. */
271 /* Application health monitoring */
272 struct health_app
*health_sessiond
;
274 /* Am I root or not. */
275 int is_root
; /* Set to 1 if the daemon is running as root */
277 const char * const config_section_name
= "sessiond";
279 /* Load session thread information to operate. */
280 struct load_session_thread_data
*load_info
;
282 /* Notification thread handle. */
283 struct notification_thread_handle
*notification_thread_handle
;
285 /* Global hash tables */
286 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
289 * Whether sessiond is ready for commands/notification channel/health check
291 * NR_LTTNG_SESSIOND_READY must match the number of calls to
292 * sessiond_notify_ready().
294 #define NR_LTTNG_SESSIOND_READY 4
295 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
297 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
299 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
302 /* Notify parents that we are ready for cmd and health check */
304 void sessiond_notify_ready(void)
306 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
308 * Notify parent pid that we are ready to accept command
309 * for client side. This ppid is the one from the
310 * external process that spawned us.
312 if (config
.sig_parent
) {
317 * Notify the parent of the fork() process that we are
320 if (config
.daemonize
|| config
.background
) {
321 kill(child_ppid
, SIGUSR1
);
327 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
334 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
340 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
352 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
354 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
356 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
360 * Init thread quit pipe.
362 * Return -1 on error or 0 if all pipes are created.
364 static int __init_thread_quit_pipe(int *a_pipe
)
370 PERROR("thread quit pipe");
374 for (i
= 0; i
< 2; i
++) {
375 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
386 static int init_thread_quit_pipe(void)
388 return __init_thread_quit_pipe(thread_quit_pipe
);
392 * Stop all threads by closing the thread quit pipe.
394 static void stop_threads(void)
398 /* Stopping all threads */
399 DBG("Terminating all threads");
400 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
402 ERR("write error on thread quit pipe");
405 /* Dispatch thread */
406 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
407 futex_nto1_wake(&ust_cmd_queue
.futex
);
411 * Close every consumer sockets.
413 static void close_consumer_sockets(void)
417 if (kconsumer_data
.err_sock
>= 0) {
418 ret
= close(kconsumer_data
.err_sock
);
420 PERROR("kernel consumer err_sock close");
423 if (ustconsumer32_data
.err_sock
>= 0) {
424 ret
= close(ustconsumer32_data
.err_sock
);
426 PERROR("UST consumerd32 err_sock close");
429 if (ustconsumer64_data
.err_sock
>= 0) {
430 ret
= close(ustconsumer64_data
.err_sock
);
432 PERROR("UST consumerd64 err_sock close");
435 if (kconsumer_data
.cmd_sock
>= 0) {
436 ret
= close(kconsumer_data
.cmd_sock
);
438 PERROR("kernel consumer cmd_sock close");
441 if (ustconsumer32_data
.cmd_sock
>= 0) {
442 ret
= close(ustconsumer32_data
.cmd_sock
);
444 PERROR("UST consumerd32 cmd_sock close");
447 if (ustconsumer64_data
.cmd_sock
>= 0) {
448 ret
= close(ustconsumer64_data
.cmd_sock
);
450 PERROR("UST consumerd64 cmd_sock close");
453 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
454 ret
= close(kconsumer_data
.channel_monitor_pipe
);
456 PERROR("kernel consumer channel monitor pipe close");
459 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
460 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
462 PERROR("UST consumerd32 channel monitor pipe close");
465 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
466 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
468 PERROR("UST consumerd64 channel monitor pipe close");
474 * Wait on consumer process termination.
476 * Need to be called with the consumer data lock held or from a context
477 * ensuring no concurrent access to data (e.g: cleanup).
479 static void wait_consumer(struct consumer_data
*consumer_data
)
484 if (consumer_data
->pid
<= 0) {
488 DBG("Waiting for complete teardown of consumerd (PID: %d)",
490 ret
= waitpid(consumer_data
->pid
, &status
, 0);
492 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
493 } else if (!WIFEXITED(status
)) {
494 ERR("consumerd termination with error: %d",
497 consumer_data
->pid
= 0;
501 * Cleanup the session daemon's data structures.
503 static void sessiond_cleanup(void)
506 struct ltt_session
*sess
, *stmp
;
508 DBG("Cleanup sessiond");
511 * Close the thread quit pipe. It has already done its job,
512 * since we are now called.
514 utils_close_pipe(thread_quit_pipe
);
517 * If config.pid_file_path.value is undefined, the default file will be
518 * wiped when removing the rundir.
520 if (config
.pid_file_path
.value
) {
521 ret
= remove(config
.pid_file_path
.value
);
523 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
527 DBG("Removing sessiond and consumerd content of directory %s",
528 config
.rundir
.value
);
531 DBG("Removing %s", config
.pid_file_path
.value
);
532 (void) unlink(config
.pid_file_path
.value
);
534 DBG("Removing %s", config
.agent_port_file_path
.value
);
535 (void) unlink(config
.agent_port_file_path
.value
);
538 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
539 (void) unlink(kconsumer_data
.err_unix_sock_path
);
541 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
542 (void) rmdir(config
.kconsumerd_path
.value
);
544 /* ust consumerd 32 */
545 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
546 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
548 DBG("Removing directory %s", config
.consumerd32_path
.value
);
549 (void) rmdir(config
.consumerd32_path
.value
);
551 /* ust consumerd 64 */
552 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
553 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
555 DBG("Removing directory %s", config
.consumerd64_path
.value
);
556 (void) rmdir(config
.consumerd64_path
.value
);
558 DBG("Cleaning up all sessions");
560 /* Destroy session list mutex */
561 if (session_list_ptr
!= NULL
) {
562 pthread_mutex_destroy(&session_list_ptr
->lock
);
564 /* Cleanup ALL session */
565 cds_list_for_each_entry_safe(sess
, stmp
,
566 &session_list_ptr
->head
, list
) {
567 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
571 wait_consumer(&kconsumer_data
);
572 wait_consumer(&ustconsumer64_data
);
573 wait_consumer(&ustconsumer32_data
);
575 DBG("Cleaning up all agent apps");
576 agent_app_ht_clean();
578 DBG("Closing all UST sockets");
579 ust_app_clean_list();
580 buffer_reg_destroy_registries();
582 if (is_root
&& !config
.no_kernel
) {
583 DBG2("Closing kernel fd");
584 if (kernel_tracer_fd
>= 0) {
585 ret
= close(kernel_tracer_fd
);
590 DBG("Unloading kernel modules");
591 modprobe_remove_lttng_all();
595 close_consumer_sockets();
598 load_session_destroy_data(load_info
);
603 * Cleanup lock file by deleting it and finaly closing it which will
604 * release the file system lock.
606 if (lockfile_fd
>= 0) {
607 ret
= remove(config
.lock_file_path
.value
);
609 PERROR("remove lock file");
611 ret
= close(lockfile_fd
);
613 PERROR("close lock file");
618 * We do NOT rmdir rundir because there are other processes
619 * using it, for instance lttng-relayd, which can start in
620 * parallel with this teardown.
625 * Cleanup the daemon's option data structures.
627 static void sessiond_cleanup_options(void)
629 DBG("Cleaning up options");
631 sessiond_config_fini(&config
);
633 run_as_destroy_worker();
637 * Send data on a unix socket using the liblttsessiondcomm API.
639 * Return lttcomm error code.
641 static int send_unix_sock(int sock
, void *buf
, size_t len
)
643 /* Check valid length */
648 return lttcomm_send_unix_sock(sock
, buf
, len
);
652 * Free memory of a command context structure.
654 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
656 DBG("Clean command context structure");
658 if ((*cmd_ctx
)->llm
) {
659 free((*cmd_ctx
)->llm
);
661 if ((*cmd_ctx
)->lsm
) {
662 free((*cmd_ctx
)->lsm
);
670 * Notify UST applications using the shm mmap futex.
672 static int notify_ust_apps(int active
)
676 DBG("Notifying applications of session daemon state: %d", active
);
678 /* See shm.c for this call implying mmap, shm and futex calls */
679 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
680 if (wait_shm_mmap
== NULL
) {
684 /* Wake waiting process */
685 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
687 /* Apps notified successfully */
695 * Setup the outgoing data buffer for the response (llm) by allocating the
696 * right amount of memory and copying the original information from the lsm
699 * Return 0 on success, negative value on error.
701 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
702 const void *payload_buf
, size_t payload_len
,
703 const void *cmd_header_buf
, size_t cmd_header_len
)
706 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
707 const size_t cmd_header_offset
= header_len
;
708 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
709 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
711 cmd_ctx
->llm
= zmalloc(total_msg_size
);
713 if (cmd_ctx
->llm
== NULL
) {
719 /* Copy common data */
720 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
721 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
722 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
723 cmd_ctx
->llm
->data_size
= payload_len
;
724 cmd_ctx
->lttng_msg_size
= total_msg_size
;
726 /* Copy command header */
727 if (cmd_header_len
) {
728 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
734 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
743 * Version of setup_lttng_msg() without command header.
745 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
746 void *payload_buf
, size_t payload_len
)
748 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
751 * Update the kernel poll set of all channel fd available over all tracing
752 * session. Add the wakeup pipe at the end of the set.
754 static int update_kernel_poll(struct lttng_poll_event
*events
)
757 struct ltt_session
*session
;
758 struct ltt_kernel_channel
*channel
;
760 DBG("Updating kernel poll set");
763 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
764 session_lock(session
);
765 if (session
->kernel_session
== NULL
) {
766 session_unlock(session
);
770 cds_list_for_each_entry(channel
,
771 &session
->kernel_session
->channel_list
.head
, list
) {
772 /* Add channel fd to the kernel poll set */
773 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
775 session_unlock(session
);
778 DBG("Channel fd %d added to kernel set", channel
->fd
);
780 session_unlock(session
);
782 session_unlock_list();
787 session_unlock_list();
792 * Find the channel fd from 'fd' over all tracing session. When found, check
793 * for new channel stream and send those stream fds to the kernel consumer.
795 * Useful for CPU hotplug feature.
797 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
800 struct ltt_session
*session
;
801 struct ltt_kernel_session
*ksess
;
802 struct ltt_kernel_channel
*channel
;
804 DBG("Updating kernel streams for channel fd %d", fd
);
807 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
808 session_lock(session
);
809 if (session
->kernel_session
== NULL
) {
810 session_unlock(session
);
813 ksess
= session
->kernel_session
;
815 cds_list_for_each_entry(channel
,
816 &ksess
->channel_list
.head
, list
) {
817 struct lttng_ht_iter iter
;
818 struct consumer_socket
*socket
;
820 if (channel
->fd
!= fd
) {
823 DBG("Channel found, updating kernel streams");
824 ret
= kernel_open_channel_stream(channel
);
828 /* Update the stream global counter */
829 ksess
->stream_count_global
+= ret
;
832 * Have we already sent fds to the consumer? If yes, it
833 * means that tracing is started so it is safe to send
834 * our updated stream fds.
836 if (ksess
->consumer_fds_sent
!= 1
837 || ksess
->consumer
== NULL
) {
843 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
844 &iter
.iter
, socket
, node
.node
) {
845 pthread_mutex_lock(socket
->lock
);
846 ret
= kernel_consumer_send_channel_stream(socket
,
848 session
->output_traces
? 1 : 0);
849 pthread_mutex_unlock(socket
->lock
);
857 session_unlock(session
);
859 session_unlock_list();
863 session_unlock(session
);
864 session_unlock_list();
869 * For each tracing session, update newly registered apps. The session list
870 * lock MUST be acquired before calling this.
872 static void update_ust_app(int app_sock
)
874 struct ltt_session
*sess
, *stmp
;
876 /* Consumer is in an ERROR state. Stop any application update. */
877 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
878 /* Stop the update process since the consumer is dead. */
882 /* For all tracing session(s) */
883 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
887 if (!sess
->ust_session
) {
892 assert(app_sock
>= 0);
893 app
= ust_app_find_by_sock(app_sock
);
896 * Application can be unregistered before so
897 * this is possible hence simply stopping the
900 DBG3("UST app update failed to find app sock %d",
904 ust_app_global_update(sess
->ust_session
, app
);
908 session_unlock(sess
);
913 * This thread manage event coming from the kernel.
915 * Features supported in this thread:
918 static void *thread_manage_kernel(void *data
)
920 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
921 uint32_t revents
, nb_fd
;
923 struct lttng_poll_event events
;
925 DBG("[thread] Thread manage kernel started");
927 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
930 * This first step of the while is to clean this structure which could free
931 * non NULL pointers so initialize it before the loop.
933 lttng_poll_init(&events
);
935 if (testpoint(sessiond_thread_manage_kernel
)) {
936 goto error_testpoint
;
939 health_code_update();
941 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
942 goto error_testpoint
;
946 health_code_update();
948 if (update_poll_flag
== 1) {
949 /* Clean events object. We are about to populate it again. */
950 lttng_poll_clean(&events
);
952 ret
= sessiond_set_thread_pollset(&events
, 2);
954 goto error_poll_create
;
957 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
962 /* This will add the available kernel channel if any. */
963 ret
= update_kernel_poll(&events
);
967 update_poll_flag
= 0;
970 DBG("Thread kernel polling");
972 /* Poll infinite value of time */
975 ret
= lttng_poll_wait(&events
, -1);
976 DBG("Thread kernel return from poll on %d fds",
977 LTTNG_POLL_GETNB(&events
));
981 * Restart interrupted system call.
983 if (errno
== EINTR
) {
987 } else if (ret
== 0) {
988 /* Should not happen since timeout is infinite */
989 ERR("Return value of poll is 0 with an infinite timeout.\n"
990 "This should not have happened! Continuing...");
996 for (i
= 0; i
< nb_fd
; i
++) {
997 /* Fetch once the poll data */
998 revents
= LTTNG_POLL_GETEV(&events
, i
);
999 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1001 health_code_update();
1004 /* No activity for this FD (poll implementation). */
1008 /* Thread quit pipe has been closed. Killing thread. */
1009 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1015 /* Check for data on kernel pipe */
1016 if (revents
& LPOLLIN
) {
1017 if (pollfd
== kernel_poll_pipe
[0]) {
1018 (void) lttng_read(kernel_poll_pipe
[0],
1021 * Ret value is useless here, if this pipe gets any actions an
1022 * update is required anyway.
1024 update_poll_flag
= 1;
1028 * New CPU detected by the kernel. Adding kernel stream to
1029 * kernel session and updating the kernel consumer
1031 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1037 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1038 update_poll_flag
= 1;
1041 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1049 lttng_poll_clean(&events
);
1052 utils_close_pipe(kernel_poll_pipe
);
1053 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1056 ERR("Health error occurred in %s", __func__
);
1057 WARN("Kernel thread died unexpectedly. "
1058 "Kernel tracing can continue but CPU hotplug is disabled.");
1060 health_unregister(health_sessiond
);
1061 DBG("Kernel thread dying");
1066 * Signal pthread condition of the consumer data that the thread.
1068 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1070 pthread_mutex_lock(&data
->cond_mutex
);
1073 * The state is set before signaling. It can be any value, it's the waiter
1074 * job to correctly interpret this condition variable associated to the
1075 * consumer pthread_cond.
1077 * A value of 0 means that the corresponding thread of the consumer data
1078 * was not started. 1 indicates that the thread has started and is ready
1079 * for action. A negative value means that there was an error during the
1082 data
->consumer_thread_is_ready
= state
;
1083 (void) pthread_cond_signal(&data
->cond
);
1085 pthread_mutex_unlock(&data
->cond_mutex
);
1089 * This thread manage the consumer error sent back to the session daemon.
1091 static void *thread_manage_consumer(void *data
)
1093 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1094 uint32_t revents
, nb_fd
;
1095 enum lttcomm_return_code code
;
1096 struct lttng_poll_event events
;
1097 struct consumer_data
*consumer_data
= data
;
1098 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1100 DBG("[thread] Manage consumer started");
1102 rcu_register_thread();
1103 rcu_thread_online();
1105 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1107 health_code_update();
1110 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1111 * metadata_sock. Nothing more will be added to this poll set.
1113 ret
= sessiond_set_thread_pollset(&events
, 3);
1119 * The error socket here is already in a listening state which was done
1120 * just before spawning this thread to avoid a race between the consumer
1121 * daemon exec trying to connect and the listen() call.
1123 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1128 health_code_update();
1130 /* Infinite blocking call, waiting for transmission */
1132 health_poll_entry();
1134 if (testpoint(sessiond_thread_manage_consumer
)) {
1138 ret
= lttng_poll_wait(&events
, -1);
1142 * Restart interrupted system call.
1144 if (errno
== EINTR
) {
1152 for (i
= 0; i
< nb_fd
; i
++) {
1153 /* Fetch once the poll data */
1154 revents
= LTTNG_POLL_GETEV(&events
, i
);
1155 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1157 health_code_update();
1160 /* No activity for this FD (poll implementation). */
1164 /* Thread quit pipe has been closed. Killing thread. */
1165 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1171 /* Event on the registration socket */
1172 if (pollfd
== consumer_data
->err_sock
) {
1173 if (revents
& LPOLLIN
) {
1175 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1176 ERR("consumer err socket poll error");
1179 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1185 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1191 * Set the CLOEXEC flag. Return code is useless because either way, the
1194 (void) utils_set_fd_cloexec(sock
);
1196 health_code_update();
1198 DBG2("Receiving code from consumer err_sock");
1200 /* Getting status code from kconsumerd */
1201 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1202 sizeof(enum lttcomm_return_code
));
1207 health_code_update();
1208 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1209 ERR("consumer error when waiting for SOCK_READY : %s",
1210 lttcomm_get_readable_code(-code
));
1214 /* Connect both command and metadata sockets. */
1215 consumer_data
->cmd_sock
=
1216 lttcomm_connect_unix_sock(
1217 consumer_data
->cmd_unix_sock_path
);
1218 consumer_data
->metadata_fd
=
1219 lttcomm_connect_unix_sock(
1220 consumer_data
->cmd_unix_sock_path
);
1221 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1222 PERROR("consumer connect cmd socket");
1223 /* On error, signal condition and quit. */
1224 signal_consumer_condition(consumer_data
, -1);
1228 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1230 /* Create metadata socket lock. */
1231 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1232 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1233 PERROR("zmalloc pthread mutex");
1236 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1238 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1239 DBG("Consumer metadata socket ready (fd: %d)",
1240 consumer_data
->metadata_fd
);
1243 * Remove the consumerd error sock since we've established a connection.
1245 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1250 /* Add new accepted error socket. */
1251 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1256 /* Add metadata socket that is successfully connected. */
1257 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1258 LPOLLIN
| LPOLLRDHUP
);
1263 health_code_update();
1266 * Transfer the write-end of the channel monitoring pipe to the
1267 * by issuing a SET_CHANNEL_MONITOR_PIPE command.
1269 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1270 if (!cmd_socket_wrapper
) {
1274 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1275 consumer_data
->channel_monitor_pipe
);
1279 /* Discard the socket wrapper as it is no longer needed. */
1280 consumer_destroy_socket(cmd_socket_wrapper
);
1281 cmd_socket_wrapper
= NULL
;
1283 /* The thread is completely initialized, signal that it is ready. */
1284 signal_consumer_condition(consumer_data
, 1);
1286 /* Infinite blocking call, waiting for transmission */
1289 health_code_update();
1291 /* Exit the thread because the thread quit pipe has been triggered. */
1293 /* Not a health error. */
1298 health_poll_entry();
1299 ret
= lttng_poll_wait(&events
, -1);
1303 * Restart interrupted system call.
1305 if (errno
== EINTR
) {
1313 for (i
= 0; i
< nb_fd
; i
++) {
1314 /* Fetch once the poll data */
1315 revents
= LTTNG_POLL_GETEV(&events
, i
);
1316 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1318 health_code_update();
1321 /* No activity for this FD (poll implementation). */
1326 * Thread quit pipe has been triggered, flag that we should stop
1327 * but continue the current loop to handle potential data from
1330 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1332 if (pollfd
== sock
) {
1333 /* Event on the consumerd socket */
1334 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1335 && !(revents
& LPOLLIN
)) {
1336 ERR("consumer err socket second poll error");
1339 health_code_update();
1340 /* Wait for any kconsumerd error */
1341 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1342 sizeof(enum lttcomm_return_code
));
1344 ERR("consumer closed the command socket");
1348 ERR("consumer return code : %s",
1349 lttcomm_get_readable_code(-code
));
1352 } else if (pollfd
== consumer_data
->metadata_fd
) {
1353 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1354 && !(revents
& LPOLLIN
)) {
1355 ERR("consumer err metadata socket second poll error");
1358 /* UST metadata requests */
1359 ret
= ust_consumer_metadata_request(
1360 &consumer_data
->metadata_sock
);
1362 ERR("Handling metadata request");
1366 /* No need for an else branch all FDs are tested prior. */
1368 health_code_update();
1374 * We lock here because we are about to close the sockets and some other
1375 * thread might be using them so get exclusive access which will abort all
1376 * other consumer command by other threads.
1378 pthread_mutex_lock(&consumer_data
->lock
);
1380 /* Immediately set the consumerd state to stopped */
1381 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1382 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1383 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1384 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1385 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1387 /* Code flow error... */
1391 if (consumer_data
->err_sock
>= 0) {
1392 ret
= close(consumer_data
->err_sock
);
1396 consumer_data
->err_sock
= -1;
1398 if (consumer_data
->cmd_sock
>= 0) {
1399 ret
= close(consumer_data
->cmd_sock
);
1403 consumer_data
->cmd_sock
= -1;
1405 if (consumer_data
->metadata_sock
.fd_ptr
&&
1406 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1407 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1419 unlink(consumer_data
->err_unix_sock_path
);
1420 unlink(consumer_data
->cmd_unix_sock_path
);
1421 pthread_mutex_unlock(&consumer_data
->lock
);
1423 /* Cleanup metadata socket mutex. */
1424 if (consumer_data
->metadata_sock
.lock
) {
1425 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1426 free(consumer_data
->metadata_sock
.lock
);
1428 lttng_poll_clean(&events
);
1430 if (cmd_socket_wrapper
) {
1431 consumer_destroy_socket(cmd_socket_wrapper
);
1436 ERR("Health error occurred in %s", __func__
);
1438 health_unregister(health_sessiond
);
1439 DBG("consumer thread cleanup completed");
1441 rcu_thread_offline();
1442 rcu_unregister_thread();
1448 * This thread manage application communication.
1450 static void *thread_manage_apps(void *data
)
1452 int i
, ret
, pollfd
, err
= -1;
1454 uint32_t revents
, nb_fd
;
1455 struct lttng_poll_event events
;
1457 DBG("[thread] Manage application started");
1459 rcu_register_thread();
1460 rcu_thread_online();
1462 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1464 if (testpoint(sessiond_thread_manage_apps
)) {
1465 goto error_testpoint
;
1468 health_code_update();
1470 ret
= sessiond_set_thread_pollset(&events
, 2);
1472 goto error_poll_create
;
1475 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1480 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1484 health_code_update();
1487 DBG("Apps thread polling");
1489 /* Inifinite blocking call, waiting for transmission */
1491 health_poll_entry();
1492 ret
= lttng_poll_wait(&events
, -1);
1493 DBG("Apps thread return from poll on %d fds",
1494 LTTNG_POLL_GETNB(&events
));
1498 * Restart interrupted system call.
1500 if (errno
== EINTR
) {
1508 for (i
= 0; i
< nb_fd
; i
++) {
1509 /* Fetch once the poll data */
1510 revents
= LTTNG_POLL_GETEV(&events
, i
);
1511 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1513 health_code_update();
1516 /* No activity for this FD (poll implementation). */
1520 /* Thread quit pipe has been closed. Killing thread. */
1521 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1527 /* Inspect the apps cmd pipe */
1528 if (pollfd
== apps_cmd_pipe
[0]) {
1529 if (revents
& LPOLLIN
) {
1533 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1534 if (size_ret
< sizeof(sock
)) {
1535 PERROR("read apps cmd pipe");
1539 health_code_update();
1542 * Since this is a command socket (write then read),
1543 * we only monitor the error events of the socket.
1545 ret
= lttng_poll_add(&events
, sock
,
1546 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1551 DBG("Apps with sock %d added to poll set", sock
);
1552 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1553 ERR("Apps command pipe error");
1556 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1561 * At this point, we know that a registered application made
1562 * the event at poll_wait.
1564 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1565 /* Removing from the poll set */
1566 ret
= lttng_poll_del(&events
, pollfd
);
1571 /* Socket closed on remote end. */
1572 ust_app_unregister(pollfd
);
1574 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1579 health_code_update();
1585 lttng_poll_clean(&events
);
1588 utils_close_pipe(apps_cmd_pipe
);
1589 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1592 * We don't clean the UST app hash table here since already registered
1593 * applications can still be controlled so let them be until the session
1594 * daemon dies or the applications stop.
1599 ERR("Health error occurred in %s", __func__
);
1601 health_unregister(health_sessiond
);
1602 DBG("Application communication apps thread cleanup complete");
1603 rcu_thread_offline();
1604 rcu_unregister_thread();
1609 * Send a socket to a thread This is called from the dispatch UST registration
1610 * thread once all sockets are set for the application.
1612 * The sock value can be invalid, we don't really care, the thread will handle
1613 * it and make the necessary cleanup if so.
1615 * On success, return 0 else a negative value being the errno message of the
1618 static int send_socket_to_thread(int fd
, int sock
)
1623 * It's possible that the FD is set as invalid with -1 concurrently just
1624 * before calling this function being a shutdown state of the thread.
1631 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1632 if (ret
< sizeof(sock
)) {
1633 PERROR("write apps pipe %d", fd
);
1640 /* All good. Don't send back the write positive ret value. */
1647 * Sanitize the wait queue of the dispatch registration thread meaning removing
1648 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1649 * notify socket is never received.
1651 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1653 int ret
, nb_fd
= 0, i
;
1654 unsigned int fd_added
= 0;
1655 struct lttng_poll_event events
;
1656 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1660 lttng_poll_init(&events
);
1662 /* Just skip everything for an empty queue. */
1663 if (!wait_queue
->count
) {
1667 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1672 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1673 &wait_queue
->head
, head
) {
1674 assert(wait_node
->app
);
1675 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1676 LPOLLHUP
| LPOLLERR
);
1689 * Poll but don't block so we can quickly identify the faulty events and
1690 * clean them afterwards from the wait queue.
1692 ret
= lttng_poll_wait(&events
, 0);
1698 for (i
= 0; i
< nb_fd
; i
++) {
1699 /* Get faulty FD. */
1700 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1701 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1704 /* No activity for this FD (poll implementation). */
1708 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1709 &wait_queue
->head
, head
) {
1710 if (pollfd
== wait_node
->app
->sock
&&
1711 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1712 cds_list_del(&wait_node
->head
);
1713 wait_queue
->count
--;
1714 ust_app_destroy(wait_node
->app
);
1717 * Silence warning of use-after-free in
1718 * cds_list_for_each_entry_safe which uses
1719 * __typeof__(*wait_node).
1724 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1731 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1735 lttng_poll_clean(&events
);
1739 lttng_poll_clean(&events
);
1741 ERR("Unable to sanitize wait queue");
1746 * Dispatch request from the registration threads to the application
1747 * communication thread.
1749 static void *thread_dispatch_ust_registration(void *data
)
1752 struct cds_wfcq_node
*node
;
1753 struct ust_command
*ust_cmd
= NULL
;
1754 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1755 struct ust_reg_wait_queue wait_queue
= {
1759 rcu_register_thread();
1761 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1763 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1764 goto error_testpoint
;
1767 health_code_update();
1769 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1771 DBG("[thread] Dispatch UST command started");
1774 health_code_update();
1776 /* Atomically prepare the queue futex */
1777 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1779 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1784 struct ust_app
*app
= NULL
;
1788 * Make sure we don't have node(s) that have hung up before receiving
1789 * the notify socket. This is to clean the list in order to avoid
1790 * memory leaks from notify socket that are never seen.
1792 sanitize_wait_queue(&wait_queue
);
1794 health_code_update();
1795 /* Dequeue command for registration */
1796 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1798 DBG("Woken up but nothing in the UST command queue");
1799 /* Continue thread execution */
1803 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1805 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1806 " gid:%d sock:%d name:%s (version %d.%d)",
1807 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1808 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1809 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1810 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1812 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1813 wait_node
= zmalloc(sizeof(*wait_node
));
1815 PERROR("zmalloc wait_node dispatch");
1816 ret
= close(ust_cmd
->sock
);
1818 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1820 lttng_fd_put(LTTNG_FD_APPS
, 1);
1824 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1826 /* Create application object if socket is CMD. */
1827 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1829 if (!wait_node
->app
) {
1830 ret
= close(ust_cmd
->sock
);
1832 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1834 lttng_fd_put(LTTNG_FD_APPS
, 1);
1840 * Add application to the wait queue so we can set the notify
1841 * socket before putting this object in the global ht.
1843 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1848 * We have to continue here since we don't have the notify
1849 * socket and the application MUST be added to the hash table
1850 * only at that moment.
1855 * Look for the application in the local wait queue and set the
1856 * notify socket if found.
1858 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1859 &wait_queue
.head
, head
) {
1860 health_code_update();
1861 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1862 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1863 cds_list_del(&wait_node
->head
);
1865 app
= wait_node
->app
;
1867 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1873 * With no application at this stage the received socket is
1874 * basically useless so close it before we free the cmd data
1875 * structure for good.
1878 ret
= close(ust_cmd
->sock
);
1880 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1882 lttng_fd_put(LTTNG_FD_APPS
, 1);
1889 * @session_lock_list
1891 * Lock the global session list so from the register up to the
1892 * registration done message, no thread can see the application
1893 * and change its state.
1895 session_lock_list();
1899 * Add application to the global hash table. This needs to be
1900 * done before the update to the UST registry can locate the
1905 /* Set app version. This call will print an error if needed. */
1906 (void) ust_app_version(app
);
1908 /* Send notify socket through the notify pipe. */
1909 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1913 session_unlock_list();
1915 * No notify thread, stop the UST tracing. However, this is
1916 * not an internal error of the this thread thus setting
1917 * the health error code to a normal exit.
1924 * Update newly registered application with the tracing
1925 * registry info already enabled information.
1927 update_ust_app(app
->sock
);
1930 * Don't care about return value. Let the manage apps threads
1931 * handle app unregistration upon socket close.
1933 (void) ust_app_register_done(app
);
1936 * Even if the application socket has been closed, send the app
1937 * to the thread and unregistration will take place at that
1940 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1943 session_unlock_list();
1945 * No apps. thread, stop the UST tracing. However, this is
1946 * not an internal error of the this thread thus setting
1947 * the health error code to a normal exit.
1954 session_unlock_list();
1956 } while (node
!= NULL
);
1958 health_poll_entry();
1959 /* Futex wait on queue. Blocking call on futex() */
1960 futex_nto1_wait(&ust_cmd_queue
.futex
);
1963 /* Normal exit, no error */
1967 /* Clean up wait queue. */
1968 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1969 &wait_queue
.head
, head
) {
1970 cds_list_del(&wait_node
->head
);
1975 /* Empty command queue. */
1977 /* Dequeue command for registration */
1978 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1982 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1983 ret
= close(ust_cmd
->sock
);
1985 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1987 lttng_fd_put(LTTNG_FD_APPS
, 1);
1992 DBG("Dispatch thread dying");
1995 ERR("Health error occurred in %s", __func__
);
1997 health_unregister(health_sessiond
);
1998 rcu_unregister_thread();
2003 * This thread manage application registration.
2005 static void *thread_registration_apps(void *data
)
2007 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2008 uint32_t revents
, nb_fd
;
2009 struct lttng_poll_event events
;
2011 * Get allocated in this thread, enqueued to a global queue, dequeued and
2012 * freed in the manage apps thread.
2014 struct ust_command
*ust_cmd
= NULL
;
2016 DBG("[thread] Manage application registration started");
2018 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2020 if (testpoint(sessiond_thread_registration_apps
)) {
2021 goto error_testpoint
;
2024 ret
= lttcomm_listen_unix_sock(apps_sock
);
2030 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2031 * more will be added to this poll set.
2033 ret
= sessiond_set_thread_pollset(&events
, 2);
2035 goto error_create_poll
;
2038 /* Add the application registration socket */
2039 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2041 goto error_poll_add
;
2044 /* Notify all applications to register */
2045 ret
= notify_ust_apps(1);
2047 ERR("Failed to notify applications or create the wait shared memory.\n"
2048 "Execution continues but there might be problem for already\n"
2049 "running applications that wishes to register.");
2053 DBG("Accepting application registration");
2055 /* Inifinite blocking call, waiting for transmission */
2057 health_poll_entry();
2058 ret
= lttng_poll_wait(&events
, -1);
2062 * Restart interrupted system call.
2064 if (errno
== EINTR
) {
2072 for (i
= 0; i
< nb_fd
; i
++) {
2073 health_code_update();
2075 /* Fetch once the poll data */
2076 revents
= LTTNG_POLL_GETEV(&events
, i
);
2077 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2080 /* No activity for this FD (poll implementation). */
2084 /* Thread quit pipe has been closed. Killing thread. */
2085 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2091 /* Event on the registration socket */
2092 if (pollfd
== apps_sock
) {
2093 if (revents
& LPOLLIN
) {
2094 sock
= lttcomm_accept_unix_sock(apps_sock
);
2100 * Set socket timeout for both receiving and ending.
2101 * app_socket_timeout is in seconds, whereas
2102 * lttcomm_setsockopt_rcv_timeout and
2103 * lttcomm_setsockopt_snd_timeout expect msec as
2106 if (config
.app_socket_timeout
>= 0) {
2107 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2108 config
.app_socket_timeout
* 1000);
2109 (void) lttcomm_setsockopt_snd_timeout(sock
,
2110 config
.app_socket_timeout
* 1000);
2114 * Set the CLOEXEC flag. Return code is useless because
2115 * either way, the show must go on.
2117 (void) utils_set_fd_cloexec(sock
);
2119 /* Create UST registration command for enqueuing */
2120 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2121 if (ust_cmd
== NULL
) {
2122 PERROR("ust command zmalloc");
2131 * Using message-based transmissions to ensure we don't
2132 * have to deal with partially received messages.
2134 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2136 ERR("Exhausted file descriptors allowed for applications.");
2146 health_code_update();
2147 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2150 /* Close socket of the application. */
2155 lttng_fd_put(LTTNG_FD_APPS
, 1);
2159 health_code_update();
2161 ust_cmd
->sock
= sock
;
2164 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2165 " gid:%d sock:%d name:%s (version %d.%d)",
2166 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2167 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2168 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2169 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2172 * Lock free enqueue the registration request. The red pill
2173 * has been taken! This apps will be part of the *system*.
2175 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2178 * Wake the registration queue futex. Implicit memory
2179 * barrier with the exchange in cds_wfcq_enqueue.
2181 futex_nto1_wake(&ust_cmd_queue
.futex
);
2182 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2183 ERR("Register apps socket poll error");
2186 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2195 /* Notify that the registration thread is gone */
2198 if (apps_sock
>= 0) {
2199 ret
= close(apps_sock
);
2209 lttng_fd_put(LTTNG_FD_APPS
, 1);
2211 unlink(config
.apps_unix_sock_path
.value
);
2214 lttng_poll_clean(&events
);
2218 DBG("UST Registration thread cleanup complete");
2221 ERR("Health error occurred in %s", __func__
);
2223 health_unregister(health_sessiond
);
2229 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2230 * exec or it will fails.
2232 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2235 struct timespec timeout
;
2238 * Make sure we set the readiness flag to 0 because we are NOT ready.
2239 * This access to consumer_thread_is_ready does not need to be
2240 * protected by consumer_data.cond_mutex (yet) since the consumer
2241 * management thread has not been started at this point.
2243 consumer_data
->consumer_thread_is_ready
= 0;
2245 /* Setup pthread condition */
2246 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2249 PERROR("pthread_condattr_init consumer data");
2254 * Set the monotonic clock in order to make sure we DO NOT jump in time
2255 * between the clock_gettime() call and the timedwait call. See bug #324
2256 * for a more details and how we noticed it.
2258 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2261 PERROR("pthread_condattr_setclock consumer data");
2265 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2268 PERROR("pthread_cond_init consumer data");
2272 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2273 thread_manage_consumer
, consumer_data
);
2276 PERROR("pthread_create consumer");
2281 /* We are about to wait on a pthread condition */
2282 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2284 /* Get time for sem_timedwait absolute timeout */
2285 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2287 * Set the timeout for the condition timed wait even if the clock gettime
2288 * call fails since we might loop on that call and we want to avoid to
2289 * increment the timeout too many times.
2291 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2294 * The following loop COULD be skipped in some conditions so this is why we
2295 * set ret to 0 in order to make sure at least one round of the loop is
2301 * Loop until the condition is reached or when a timeout is reached. Note
2302 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2303 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2304 * possible. This loop does not take any chances and works with both of
2307 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2308 if (clock_ret
< 0) {
2309 PERROR("clock_gettime spawn consumer");
2310 /* Infinite wait for the consumerd thread to be ready */
2311 ret
= pthread_cond_wait(&consumer_data
->cond
,
2312 &consumer_data
->cond_mutex
);
2314 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2315 &consumer_data
->cond_mutex
, &timeout
);
2319 /* Release the pthread condition */
2320 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2324 if (ret
== ETIMEDOUT
) {
2328 * Call has timed out so we kill the kconsumerd_thread and return
2331 ERR("Condition timed out. The consumer thread was never ready."
2333 pth_ret
= pthread_cancel(consumer_data
->thread
);
2335 PERROR("pthread_cancel consumer thread");
2338 PERROR("pthread_cond_wait failed consumer thread");
2340 /* Caller is expecting a negative value on failure. */
2345 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2346 if (consumer_data
->pid
== 0) {
2347 ERR("Consumerd did not start");
2348 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2351 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2360 * Join consumer thread
2362 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2366 /* Consumer pid must be a real one. */
2367 if (consumer_data
->pid
> 0) {
2369 ret
= kill(consumer_data
->pid
, SIGTERM
);
2371 PERROR("Error killing consumer daemon");
2374 return pthread_join(consumer_data
->thread
, &status
);
2381 * Fork and exec a consumer daemon (consumerd).
2383 * Return pid if successful else -1.
2385 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2389 const char *consumer_to_use
;
2390 const char *verbosity
;
2393 DBG("Spawning consumerd");
2400 if (config
.verbose_consumer
) {
2401 verbosity
= "--verbose";
2402 } else if (lttng_opt_quiet
) {
2403 verbosity
= "--quiet";
2408 switch (consumer_data
->type
) {
2409 case LTTNG_CONSUMER_KERNEL
:
2411 * Find out which consumerd to execute. We will first try the
2412 * 64-bit path, then the sessiond's installation directory, and
2413 * fallback on the 32-bit one,
2415 DBG3("Looking for a kernel consumer at these locations:");
2416 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2417 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2418 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2419 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2420 DBG3("Found location #1");
2421 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2422 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2423 DBG3("Found location #2");
2424 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2425 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2426 DBG3("Found location #3");
2427 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2429 DBG("Could not find any valid consumerd executable");
2433 DBG("Using kernel consumer at: %s", consumer_to_use
);
2434 (void) execl(consumer_to_use
,
2435 "lttng-consumerd", verbosity
, "-k",
2436 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2437 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2438 "--group", config
.tracing_group_name
.value
,
2441 case LTTNG_CONSUMER64_UST
:
2443 char *tmpnew
= NULL
;
2445 if (config
.consumerd64_lib_dir
.value
) {
2449 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2453 tmplen
= strlen("LD_LIBRARY_PATH=")
2454 + strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2455 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2460 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2461 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2462 if (tmp
[0] != '\0') {
2463 strcat(tmpnew
, ":");
2464 strcat(tmpnew
, tmp
);
2466 ret
= putenv(tmpnew
);
2473 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2474 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2475 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2476 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2477 "--group", config
.tracing_group_name
.value
,
2482 case LTTNG_CONSUMER32_UST
:
2484 char *tmpnew
= NULL
;
2486 if (config
.consumerd32_lib_dir
.value
) {
2490 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2494 tmplen
= strlen("LD_LIBRARY_PATH=")
2495 + strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2496 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2501 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2502 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2503 if (tmp
[0] != '\0') {
2504 strcat(tmpnew
, ":");
2505 strcat(tmpnew
, tmp
);
2507 ret
= putenv(tmpnew
);
2514 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2515 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2516 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2517 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2518 "--group", config
.tracing_group_name
.value
,
2524 ERR("unknown consumer type");
2528 PERROR("Consumer execl()");
2530 /* Reaching this point, we got a failure on our execl(). */
2532 } else if (pid
> 0) {
2535 PERROR("start consumer fork");
2543 * Spawn the consumerd daemon and session daemon thread.
2545 static int start_consumerd(struct consumer_data
*consumer_data
)
2550 * Set the listen() state on the socket since there is a possible race
2551 * between the exec() of the consumer daemon and this call if place in the
2552 * consumer thread. See bug #366 for more details.
2554 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2559 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2560 if (consumer_data
->pid
!= 0) {
2561 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2565 ret
= spawn_consumerd(consumer_data
);
2567 ERR("Spawning consumerd failed");
2568 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2572 /* Setting up the consumer_data pid */
2573 consumer_data
->pid
= ret
;
2574 DBG2("Consumer pid %d", consumer_data
->pid
);
2575 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2577 DBG2("Spawning consumer control thread");
2578 ret
= spawn_consumer_thread(consumer_data
);
2580 ERR("Fatal error spawning consumer control thread");
2588 /* Cleanup already created sockets on error. */
2589 if (consumer_data
->err_sock
>= 0) {
2592 err
= close(consumer_data
->err_sock
);
2594 PERROR("close consumer data error socket");
2601 * Setup necessary data for kernel tracer action.
2603 static int init_kernel_tracer(void)
2607 /* Modprobe lttng kernel modules */
2608 ret
= modprobe_lttng_control();
2613 /* Open debugfs lttng */
2614 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2615 if (kernel_tracer_fd
< 0) {
2616 DBG("Failed to open %s", module_proc_lttng
);
2620 /* Validate kernel version */
2621 ret
= kernel_validate_version(kernel_tracer_fd
);
2626 ret
= modprobe_lttng_data();
2631 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2638 WARN("Kernel tracer does not support buffer monitoring. "
2639 "The monitoring timer of channels in the kernel domain "
2640 "will be set to 0 (disabled).");
2643 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2647 modprobe_remove_lttng_control();
2648 ret
= close(kernel_tracer_fd
);
2652 kernel_tracer_fd
= -1;
2653 return LTTNG_ERR_KERN_VERSION
;
2656 ret
= close(kernel_tracer_fd
);
2662 modprobe_remove_lttng_control();
2665 WARN("No kernel tracer available");
2666 kernel_tracer_fd
= -1;
2668 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2670 return LTTNG_ERR_KERN_NA
;
2676 * Copy consumer output from the tracing session to the domain session. The
2677 * function also applies the right modification on a per domain basis for the
2678 * trace files destination directory.
2680 * Should *NOT* be called with RCU read-side lock held.
2682 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2685 const char *dir_name
;
2686 struct consumer_output
*consumer
;
2689 assert(session
->consumer
);
2692 case LTTNG_DOMAIN_KERNEL
:
2693 DBG3("Copying tracing session consumer output in kernel session");
2695 * XXX: We should audit the session creation and what this function
2696 * does "extra" in order to avoid a destroy since this function is used
2697 * in the domain session creation (kernel and ust) only. Same for UST
2700 if (session
->kernel_session
->consumer
) {
2701 consumer_output_put(session
->kernel_session
->consumer
);
2703 session
->kernel_session
->consumer
=
2704 consumer_copy_output(session
->consumer
);
2705 /* Ease our life a bit for the next part */
2706 consumer
= session
->kernel_session
->consumer
;
2707 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2709 case LTTNG_DOMAIN_JUL
:
2710 case LTTNG_DOMAIN_LOG4J
:
2711 case LTTNG_DOMAIN_PYTHON
:
2712 case LTTNG_DOMAIN_UST
:
2713 DBG3("Copying tracing session consumer output in UST session");
2714 if (session
->ust_session
->consumer
) {
2715 consumer_output_put(session
->ust_session
->consumer
);
2717 session
->ust_session
->consumer
=
2718 consumer_copy_output(session
->consumer
);
2719 /* Ease our life a bit for the next part */
2720 consumer
= session
->ust_session
->consumer
;
2721 dir_name
= DEFAULT_UST_TRACE_DIR
;
2724 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2728 /* Append correct directory to subdir */
2729 strncat(consumer
->subdir
, dir_name
,
2730 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2731 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2740 * Create an UST session and add it to the session ust list.
2742 * Should *NOT* be called with RCU read-side lock held.
2744 static int create_ust_session(struct ltt_session
*session
,
2745 struct lttng_domain
*domain
)
2748 struct ltt_ust_session
*lus
= NULL
;
2752 assert(session
->consumer
);
2754 switch (domain
->type
) {
2755 case LTTNG_DOMAIN_JUL
:
2756 case LTTNG_DOMAIN_LOG4J
:
2757 case LTTNG_DOMAIN_PYTHON
:
2758 case LTTNG_DOMAIN_UST
:
2761 ERR("Unknown UST domain on create session %d", domain
->type
);
2762 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2766 DBG("Creating UST session");
2768 lus
= trace_ust_create_session(session
->id
);
2770 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2774 lus
->uid
= session
->uid
;
2775 lus
->gid
= session
->gid
;
2776 lus
->output_traces
= session
->output_traces
;
2777 lus
->snapshot_mode
= session
->snapshot_mode
;
2778 lus
->live_timer_interval
= session
->live_timer
;
2779 session
->ust_session
= lus
;
2780 if (session
->shm_path
[0]) {
2781 strncpy(lus
->root_shm_path
, session
->shm_path
,
2782 sizeof(lus
->root_shm_path
));
2783 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2784 strncpy(lus
->shm_path
, session
->shm_path
,
2785 sizeof(lus
->shm_path
));
2786 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2787 strncat(lus
->shm_path
, "/ust",
2788 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2790 /* Copy session output to the newly created UST session */
2791 ret
= copy_session_consumer(domain
->type
, session
);
2792 if (ret
!= LTTNG_OK
) {
2800 session
->ust_session
= NULL
;
2805 * Create a kernel tracer session then create the default channel.
2807 static int create_kernel_session(struct ltt_session
*session
)
2811 DBG("Creating kernel session");
2813 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2815 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2819 /* Code flow safety */
2820 assert(session
->kernel_session
);
2822 /* Copy session output to the newly created Kernel session */
2823 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2824 if (ret
!= LTTNG_OK
) {
2828 /* Create directory(ies) on local filesystem. */
2829 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2830 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2831 ret
= run_as_mkdir_recursive(
2832 session
->kernel_session
->consumer
->dst
.trace_path
,
2833 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2835 if (errno
!= EEXIST
) {
2836 ERR("Trace directory creation error");
2842 session
->kernel_session
->uid
= session
->uid
;
2843 session
->kernel_session
->gid
= session
->gid
;
2844 session
->kernel_session
->output_traces
= session
->output_traces
;
2845 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2850 trace_kernel_destroy_session(session
->kernel_session
);
2851 session
->kernel_session
= NULL
;
2856 * Count number of session permitted by uid/gid.
2858 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2861 struct ltt_session
*session
;
2863 DBG("Counting number of available session for UID %d GID %d",
2865 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2867 * Only list the sessions the user can control.
2869 if (!session_access_ok(session
, uid
, gid
)) {
2878 * Process the command requested by the lttng client within the command
2879 * context structure. This function make sure that the return structure (llm)
2880 * is set and ready for transmission before returning.
2882 * Return any error encountered or 0 for success.
2884 * "sock" is only used for special-case var. len data.
2886 * Should *NOT* be called with RCU read-side lock held.
2888 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2892 int need_tracing_session
= 1;
2895 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2897 assert(!rcu_read_ongoing());
2901 switch (cmd_ctx
->lsm
->cmd_type
) {
2902 case LTTNG_CREATE_SESSION
:
2903 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2904 case LTTNG_CREATE_SESSION_LIVE
:
2905 case LTTNG_DESTROY_SESSION
:
2906 case LTTNG_LIST_SESSIONS
:
2907 case LTTNG_LIST_DOMAINS
:
2908 case LTTNG_START_TRACE
:
2909 case LTTNG_STOP_TRACE
:
2910 case LTTNG_DATA_PENDING
:
2911 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2912 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2913 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2914 case LTTNG_SNAPSHOT_RECORD
:
2915 case LTTNG_SAVE_SESSION
:
2916 case LTTNG_SET_SESSION_SHM_PATH
:
2917 case LTTNG_REGENERATE_METADATA
:
2918 case LTTNG_REGENERATE_STATEDUMP
:
2919 case LTTNG_REGISTER_TRIGGER
:
2920 case LTTNG_UNREGISTER_TRIGGER
:
2927 if (config
.no_kernel
&& need_domain
2928 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2930 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2932 ret
= LTTNG_ERR_KERN_NA
;
2937 /* Deny register consumer if we already have a spawned consumer. */
2938 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2939 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2940 if (kconsumer_data
.pid
> 0) {
2941 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2942 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2945 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2949 * Check for command that don't needs to allocate a returned payload. We do
2950 * this here so we don't have to make the call for no payload at each
2953 switch(cmd_ctx
->lsm
->cmd_type
) {
2954 case LTTNG_LIST_SESSIONS
:
2955 case LTTNG_LIST_TRACEPOINTS
:
2956 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2957 case LTTNG_LIST_DOMAINS
:
2958 case LTTNG_LIST_CHANNELS
:
2959 case LTTNG_LIST_EVENTS
:
2960 case LTTNG_LIST_SYSCALLS
:
2961 case LTTNG_LIST_TRACKER_PIDS
:
2962 case LTTNG_DATA_PENDING
:
2965 /* Setup lttng message with no payload */
2966 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2968 /* This label does not try to unlock the session */
2969 goto init_setup_error
;
2973 /* Commands that DO NOT need a session. */
2974 switch (cmd_ctx
->lsm
->cmd_type
) {
2975 case LTTNG_CREATE_SESSION
:
2976 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2977 case LTTNG_CREATE_SESSION_LIVE
:
2978 case LTTNG_LIST_SESSIONS
:
2979 case LTTNG_LIST_TRACEPOINTS
:
2980 case LTTNG_LIST_SYSCALLS
:
2981 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2982 case LTTNG_SAVE_SESSION
:
2983 case LTTNG_REGISTER_TRIGGER
:
2984 case LTTNG_UNREGISTER_TRIGGER
:
2985 need_tracing_session
= 0;
2988 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2990 * We keep the session list lock across _all_ commands
2991 * for now, because the per-session lock does not
2992 * handle teardown properly.
2994 session_lock_list();
2995 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2996 if (cmd_ctx
->session
== NULL
) {
2997 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3000 /* Acquire lock for the session */
3001 session_lock(cmd_ctx
->session
);
3007 * Commands that need a valid session but should NOT create one if none
3008 * exists. Instead of creating one and destroying it when the command is
3009 * handled, process that right before so we save some round trip in useless
3012 switch (cmd_ctx
->lsm
->cmd_type
) {
3013 case LTTNG_DISABLE_CHANNEL
:
3014 case LTTNG_DISABLE_EVENT
:
3015 switch (cmd_ctx
->lsm
->domain
.type
) {
3016 case LTTNG_DOMAIN_KERNEL
:
3017 if (!cmd_ctx
->session
->kernel_session
) {
3018 ret
= LTTNG_ERR_NO_CHANNEL
;
3022 case LTTNG_DOMAIN_JUL
:
3023 case LTTNG_DOMAIN_LOG4J
:
3024 case LTTNG_DOMAIN_PYTHON
:
3025 case LTTNG_DOMAIN_UST
:
3026 if (!cmd_ctx
->session
->ust_session
) {
3027 ret
= LTTNG_ERR_NO_CHANNEL
;
3032 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3044 * Check domain type for specific "pre-action".
3046 switch (cmd_ctx
->lsm
->domain
.type
) {
3047 case LTTNG_DOMAIN_KERNEL
:
3049 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3053 /* Kernel tracer check */
3054 if (kernel_tracer_fd
== -1) {
3055 /* Basically, load kernel tracer modules */
3056 ret
= init_kernel_tracer();
3062 /* Consumer is in an ERROR state. Report back to client */
3063 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3064 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3068 /* Need a session for kernel command */
3069 if (need_tracing_session
) {
3070 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3071 ret
= create_kernel_session(cmd_ctx
->session
);
3073 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3078 /* Start the kernel consumer daemon */
3079 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3080 if (kconsumer_data
.pid
== 0 &&
3081 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3082 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3083 ret
= start_consumerd(&kconsumer_data
);
3085 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3088 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3090 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3094 * The consumer was just spawned so we need to add the socket to
3095 * the consumer output of the session if exist.
3097 ret
= consumer_create_socket(&kconsumer_data
,
3098 cmd_ctx
->session
->kernel_session
->consumer
);
3105 case LTTNG_DOMAIN_JUL
:
3106 case LTTNG_DOMAIN_LOG4J
:
3107 case LTTNG_DOMAIN_PYTHON
:
3108 case LTTNG_DOMAIN_UST
:
3110 if (!ust_app_supported()) {
3111 ret
= LTTNG_ERR_NO_UST
;
3114 /* Consumer is in an ERROR state. Report back to client */
3115 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3116 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3120 if (need_tracing_session
) {
3121 /* Create UST session if none exist. */
3122 if (cmd_ctx
->session
->ust_session
== NULL
) {
3123 ret
= create_ust_session(cmd_ctx
->session
,
3124 &cmd_ctx
->lsm
->domain
);
3125 if (ret
!= LTTNG_OK
) {
3130 /* Start the UST consumer daemons */
3132 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3133 if (config
.consumerd64_bin_path
.value
&&
3134 ustconsumer64_data
.pid
== 0 &&
3135 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3136 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3137 ret
= start_consumerd(&ustconsumer64_data
);
3139 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3140 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3144 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3145 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3147 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3151 * Setup socket for consumer 64 bit. No need for atomic access
3152 * since it was set above and can ONLY be set in this thread.
3154 ret
= consumer_create_socket(&ustconsumer64_data
,
3155 cmd_ctx
->session
->ust_session
->consumer
);
3161 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3162 if (config
.consumerd32_bin_path
.value
&&
3163 ustconsumer32_data
.pid
== 0 &&
3164 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3165 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3166 ret
= start_consumerd(&ustconsumer32_data
);
3168 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3169 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3173 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3174 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3176 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3180 * Setup socket for consumer 64 bit. No need for atomic access
3181 * since it was set above and can ONLY be set in this thread.
3183 ret
= consumer_create_socket(&ustconsumer32_data
,
3184 cmd_ctx
->session
->ust_session
->consumer
);
3196 /* Validate consumer daemon state when start/stop trace command */
3197 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3198 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3199 switch (cmd_ctx
->lsm
->domain
.type
) {
3200 case LTTNG_DOMAIN_NONE
:
3202 case LTTNG_DOMAIN_JUL
:
3203 case LTTNG_DOMAIN_LOG4J
:
3204 case LTTNG_DOMAIN_PYTHON
:
3205 case LTTNG_DOMAIN_UST
:
3206 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3207 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3211 case LTTNG_DOMAIN_KERNEL
:
3212 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3213 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3218 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3224 * Check that the UID or GID match that of the tracing session.
3225 * The root user can interact with all sessions.
3227 if (need_tracing_session
) {
3228 if (!session_access_ok(cmd_ctx
->session
,
3229 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3230 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3231 ret
= LTTNG_ERR_EPERM
;
3237 * Send relayd information to consumer as soon as we have a domain and a
3240 if (cmd_ctx
->session
&& need_domain
) {
3242 * Setup relayd if not done yet. If the relayd information was already
3243 * sent to the consumer, this call will gracefully return.
3245 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3246 if (ret
!= LTTNG_OK
) {
3251 /* Process by command type */
3252 switch (cmd_ctx
->lsm
->cmd_type
) {
3253 case LTTNG_ADD_CONTEXT
:
3256 * An LTTNG_ADD_CONTEXT command might have a supplementary
3257 * payload if the context being added is an application context.
3259 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3260 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3261 char *provider_name
= NULL
, *context_name
= NULL
;
3262 size_t provider_name_len
=
3263 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3264 size_t context_name_len
=
3265 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3267 if (provider_name_len
== 0 || context_name_len
== 0) {
3269 * Application provider and context names MUST
3272 ret
= -LTTNG_ERR_INVALID
;
3276 provider_name
= zmalloc(provider_name_len
+ 1);
3277 if (!provider_name
) {
3278 ret
= -LTTNG_ERR_NOMEM
;
3281 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3284 context_name
= zmalloc(context_name_len
+ 1);
3285 if (!context_name
) {
3286 ret
= -LTTNG_ERR_NOMEM
;
3287 goto error_add_context
;
3289 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3292 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3295 goto error_add_context
;
3298 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3301 goto error_add_context
;
3306 * cmd_add_context assumes ownership of the provider and context
3309 ret
= cmd_add_context(cmd_ctx
->session
,
3310 cmd_ctx
->lsm
->domain
.type
,
3311 cmd_ctx
->lsm
->u
.context
.channel_name
,
3312 &cmd_ctx
->lsm
->u
.context
.ctx
,
3313 kernel_poll_pipe
[1]);
3315 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3316 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3318 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3319 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3325 case LTTNG_DISABLE_CHANNEL
:
3327 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3328 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3331 case LTTNG_DISABLE_EVENT
:
3335 * FIXME: handle filter; for now we just receive the filter's
3336 * bytecode along with the filter expression which are sent by
3337 * liblttng-ctl and discard them.
3339 * This fixes an issue where the client may block while sending
3340 * the filter payload and encounter an error because the session
3341 * daemon closes the socket without ever handling this data.
3343 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3344 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3347 char data
[LTTNG_FILTER_MAX_LEN
];
3349 DBG("Discarding disable event command payload of size %zu", count
);
3351 ret
= lttcomm_recv_unix_sock(sock
, data
,
3352 count
> sizeof(data
) ? sizeof(data
) : count
);
3357 count
-= (size_t) ret
;
3360 /* FIXME: passing packed structure to non-packed pointer */
3361 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3362 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3363 &cmd_ctx
->lsm
->u
.disable
.event
);
3366 case LTTNG_ENABLE_CHANNEL
:
3368 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3369 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3370 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3371 &cmd_ctx
->lsm
->u
.channel
.chan
,
3372 kernel_poll_pipe
[1]);
3375 case LTTNG_TRACK_PID
:
3377 ret
= cmd_track_pid(cmd_ctx
->session
,
3378 cmd_ctx
->lsm
->domain
.type
,
3379 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3382 case LTTNG_UNTRACK_PID
:
3384 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3385 cmd_ctx
->lsm
->domain
.type
,
3386 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3389 case LTTNG_ENABLE_EVENT
:
3391 struct lttng_event_exclusion
*exclusion
= NULL
;
3392 struct lttng_filter_bytecode
*bytecode
= NULL
;
3393 char *filter_expression
= NULL
;
3395 /* Handle exclusion events and receive it from the client. */
3396 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3397 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3399 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3400 (count
* LTTNG_SYMBOL_NAME_LEN
));
3402 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3406 DBG("Receiving var len exclusion event list from client ...");
3407 exclusion
->count
= count
;
3408 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3409 count
* LTTNG_SYMBOL_NAME_LEN
);
3411 DBG("Nothing recv() from client var len data... continuing");
3414 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3419 /* Get filter expression from client. */
3420 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3421 size_t expression_len
=
3422 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3424 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3425 ret
= LTTNG_ERR_FILTER_INVAL
;
3430 filter_expression
= zmalloc(expression_len
);
3431 if (!filter_expression
) {
3433 ret
= LTTNG_ERR_FILTER_NOMEM
;
3437 /* Receive var. len. data */
3438 DBG("Receiving var len filter's expression from client ...");
3439 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3442 DBG("Nothing recv() from client car len data... continuing");
3444 free(filter_expression
);
3446 ret
= LTTNG_ERR_FILTER_INVAL
;
3451 /* Handle filter and get bytecode from client. */
3452 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3453 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3455 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3456 ret
= LTTNG_ERR_FILTER_INVAL
;
3457 free(filter_expression
);
3462 bytecode
= zmalloc(bytecode_len
);
3464 free(filter_expression
);
3466 ret
= LTTNG_ERR_FILTER_NOMEM
;
3470 /* Receive var. len. data */
3471 DBG("Receiving var len filter's bytecode from client ...");
3472 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3474 DBG("Nothing recv() from client car len data... continuing");
3476 free(filter_expression
);
3479 ret
= LTTNG_ERR_FILTER_INVAL
;
3483 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3484 free(filter_expression
);
3487 ret
= LTTNG_ERR_FILTER_INVAL
;
3492 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3493 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3494 &cmd_ctx
->lsm
->u
.enable
.event
,
3495 filter_expression
, bytecode
, exclusion
,
3496 kernel_poll_pipe
[1]);
3499 case LTTNG_LIST_TRACEPOINTS
:
3501 struct lttng_event
*events
;
3504 session_lock_list();
3505 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3506 session_unlock_list();
3507 if (nb_events
< 0) {
3508 /* Return value is a negative lttng_error_code. */
3514 * Setup lttng message with payload size set to the event list size in
3515 * bytes and then copy list into the llm payload.
3517 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3518 sizeof(struct lttng_event
) * nb_events
);
3528 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3530 struct lttng_event_field
*fields
;
3533 session_lock_list();
3534 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3536 session_unlock_list();
3537 if (nb_fields
< 0) {
3538 /* Return value is a negative lttng_error_code. */
3544 * Setup lttng message with payload size set to the event list size in
3545 * bytes and then copy list into the llm payload.
3547 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3548 sizeof(struct lttng_event_field
) * nb_fields
);
3558 case LTTNG_LIST_SYSCALLS
:
3560 struct lttng_event
*events
;
3563 nb_events
= cmd_list_syscalls(&events
);
3564 if (nb_events
< 0) {
3565 /* Return value is a negative lttng_error_code. */
3571 * Setup lttng message with payload size set to the event list size in
3572 * bytes and then copy list into the llm payload.
3574 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3575 sizeof(struct lttng_event
) * nb_events
);
3585 case LTTNG_LIST_TRACKER_PIDS
:
3587 int32_t *pids
= NULL
;
3590 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3591 cmd_ctx
->lsm
->domain
.type
, &pids
);
3593 /* Return value is a negative lttng_error_code. */
3599 * Setup lttng message with payload size set to the event list size in
3600 * bytes and then copy list into the llm payload.
3602 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3603 sizeof(int32_t) * nr_pids
);
3613 case LTTNG_SET_CONSUMER_URI
:
3616 struct lttng_uri
*uris
;
3618 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3619 len
= nb_uri
* sizeof(struct lttng_uri
);
3622 ret
= LTTNG_ERR_INVALID
;
3626 uris
= zmalloc(len
);
3628 ret
= LTTNG_ERR_FATAL
;
3632 /* Receive variable len data */
3633 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3634 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3636 DBG("No URIs received from client... continuing");
3638 ret
= LTTNG_ERR_SESSION_FAIL
;
3643 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3645 if (ret
!= LTTNG_OK
) {
3652 case LTTNG_START_TRACE
:
3654 ret
= cmd_start_trace(cmd_ctx
->session
);
3657 case LTTNG_STOP_TRACE
:
3659 ret
= cmd_stop_trace(cmd_ctx
->session
);
3662 case LTTNG_CREATE_SESSION
:
3665 struct lttng_uri
*uris
= NULL
;
3667 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3668 len
= nb_uri
* sizeof(struct lttng_uri
);
3671 uris
= zmalloc(len
);
3673 ret
= LTTNG_ERR_FATAL
;
3677 /* Receive variable len data */
3678 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3679 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3681 DBG("No URIs received from client... continuing");
3683 ret
= LTTNG_ERR_SESSION_FAIL
;
3688 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3689 DBG("Creating session with ONE network URI is a bad call");
3690 ret
= LTTNG_ERR_SESSION_FAIL
;
3696 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3697 &cmd_ctx
->creds
, 0);
3703 case LTTNG_DESTROY_SESSION
:
3705 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3707 /* Set session to NULL so we do not unlock it after free. */
3708 cmd_ctx
->session
= NULL
;
3711 case LTTNG_LIST_DOMAINS
:
3714 struct lttng_domain
*domains
= NULL
;
3716 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3718 /* Return value is a negative lttng_error_code. */
3723 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3724 nb_dom
* sizeof(struct lttng_domain
));
3734 case LTTNG_LIST_CHANNELS
:
3736 ssize_t payload_size
;
3737 struct lttng_channel
*channels
= NULL
;
3739 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3740 cmd_ctx
->session
, &channels
);
3741 if (payload_size
< 0) {
3742 /* Return value is a negative lttng_error_code. */
3743 ret
= -payload_size
;
3747 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3758 case LTTNG_LIST_EVENTS
:
3761 struct lttng_event
*events
= NULL
;
3762 struct lttcomm_event_command_header cmd_header
;
3765 memset(&cmd_header
, 0, sizeof(cmd_header
));
3766 /* Extended infos are included at the end of events */
3767 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3768 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3769 &events
, &total_size
);
3772 /* Return value is a negative lttng_error_code. */
3777 cmd_header
.nb_events
= nb_event
;
3778 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3779 &cmd_header
, sizeof(cmd_header
));
3789 case LTTNG_LIST_SESSIONS
:
3791 unsigned int nr_sessions
;
3792 void *sessions_payload
;
3795 session_lock_list();
3796 nr_sessions
= lttng_sessions_count(
3797 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3798 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3799 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3800 sessions_payload
= zmalloc(payload_len
);
3802 if (!sessions_payload
) {
3803 session_unlock_list();
3808 cmd_list_lttng_sessions(sessions_payload
,
3809 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3810 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3811 session_unlock_list();
3813 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3815 free(sessions_payload
);
3824 case LTTNG_REGISTER_CONSUMER
:
3826 struct consumer_data
*cdata
;
3828 switch (cmd_ctx
->lsm
->domain
.type
) {
3829 case LTTNG_DOMAIN_KERNEL
:
3830 cdata
= &kconsumer_data
;
3833 ret
= LTTNG_ERR_UND
;
3837 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3838 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3841 case LTTNG_DATA_PENDING
:
3844 uint8_t pending_ret_byte
;
3846 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3851 * This function may returns 0 or 1 to indicate whether or not
3852 * there is data pending. In case of error, it should return an
3853 * LTTNG_ERR code. However, some code paths may still return
3854 * a nondescript error code, which we handle by returning an
3857 if (pending_ret
== 0 || pending_ret
== 1) {
3859 * ret will be set to LTTNG_OK at the end of
3862 } else if (pending_ret
< 0) {
3863 ret
= LTTNG_ERR_UNK
;
3870 pending_ret_byte
= (uint8_t) pending_ret
;
3872 /* 1 byte to return whether or not data is pending */
3873 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3874 &pending_ret_byte
, 1);
3883 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3885 struct lttcomm_lttng_output_id reply
;
3887 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3888 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3889 if (ret
!= LTTNG_OK
) {
3893 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3899 /* Copy output list into message payload */
3903 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3905 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3906 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3909 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3912 struct lttng_snapshot_output
*outputs
= NULL
;
3914 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3915 if (nb_output
< 0) {
3920 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3921 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3922 nb_output
* sizeof(struct lttng_snapshot_output
));
3932 case LTTNG_SNAPSHOT_RECORD
:
3934 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3935 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3936 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3939 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3942 struct lttng_uri
*uris
= NULL
;
3944 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3945 len
= nb_uri
* sizeof(struct lttng_uri
);
3948 uris
= zmalloc(len
);
3950 ret
= LTTNG_ERR_FATAL
;
3954 /* Receive variable len data */
3955 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3956 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3958 DBG("No URIs received from client... continuing");
3960 ret
= LTTNG_ERR_SESSION_FAIL
;
3965 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3966 DBG("Creating session with ONE network URI is a bad call");
3967 ret
= LTTNG_ERR_SESSION_FAIL
;
3973 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3974 nb_uri
, &cmd_ctx
->creds
);
3978 case LTTNG_CREATE_SESSION_LIVE
:
3981 struct lttng_uri
*uris
= NULL
;
3983 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3984 len
= nb_uri
* sizeof(struct lttng_uri
);
3987 uris
= zmalloc(len
);
3989 ret
= LTTNG_ERR_FATAL
;
3993 /* Receive variable len data */
3994 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3995 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3997 DBG("No URIs received from client... continuing");
3999 ret
= LTTNG_ERR_SESSION_FAIL
;
4004 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4005 DBG("Creating session with ONE network URI is a bad call");
4006 ret
= LTTNG_ERR_SESSION_FAIL
;
4012 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4013 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4017 case LTTNG_SAVE_SESSION
:
4019 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4023 case LTTNG_SET_SESSION_SHM_PATH
:
4025 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4026 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4029 case LTTNG_REGENERATE_METADATA
:
4031 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4034 case LTTNG_REGENERATE_STATEDUMP
:
4036 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4039 case LTTNG_REGISTER_TRIGGER
:
4041 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4042 notification_thread_handle
);
4045 case LTTNG_UNREGISTER_TRIGGER
:
4047 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4048 notification_thread_handle
);
4052 ret
= LTTNG_ERR_UND
;
4057 if (cmd_ctx
->llm
== NULL
) {
4058 DBG("Missing llm structure. Allocating one.");
4059 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4063 /* Set return code */
4064 cmd_ctx
->llm
->ret_code
= ret
;
4066 if (cmd_ctx
->session
) {
4067 session_unlock(cmd_ctx
->session
);
4069 if (need_tracing_session
) {
4070 session_unlock_list();
4073 assert(!rcu_read_ongoing());
4078 * Thread managing health check socket.
4080 static void *thread_manage_health(void *data
)
4082 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4083 uint32_t revents
, nb_fd
;
4084 struct lttng_poll_event events
;
4085 struct health_comm_msg msg
;
4086 struct health_comm_reply reply
;
4088 DBG("[thread] Manage health check started");
4090 rcu_register_thread();
4092 /* We might hit an error path before this is created. */
4093 lttng_poll_init(&events
);
4095 /* Create unix socket */
4096 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4098 ERR("Unable to create health check Unix socket");
4103 /* lttng health client socket path permissions */
4104 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4105 utils_get_group_id(config
.tracing_group_name
.value
));
4107 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4112 ret
= chmod(config
.health_unix_sock_path
.value
,
4113 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4115 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4122 * Set the CLOEXEC flag. Return code is useless because either way, the
4125 (void) utils_set_fd_cloexec(sock
);
4127 ret
= lttcomm_listen_unix_sock(sock
);
4133 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4134 * more will be added to this poll set.
4136 ret
= sessiond_set_thread_pollset(&events
, 2);
4141 /* Add the application registration socket */
4142 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4147 sessiond_notify_ready();
4150 DBG("Health check ready");
4152 /* Inifinite blocking call, waiting for transmission */
4154 ret
= lttng_poll_wait(&events
, -1);
4157 * Restart interrupted system call.
4159 if (errno
== EINTR
) {
4167 for (i
= 0; i
< nb_fd
; i
++) {
4168 /* Fetch once the poll data */
4169 revents
= LTTNG_POLL_GETEV(&events
, i
);
4170 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4173 /* No activity for this FD (poll implementation). */
4177 /* Thread quit pipe has been closed. Killing thread. */
4178 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4184 /* Event on the registration socket */
4185 if (pollfd
== sock
) {
4186 if (revents
& LPOLLIN
) {
4188 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4189 ERR("Health socket poll error");
4192 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4198 new_sock
= lttcomm_accept_unix_sock(sock
);
4204 * Set the CLOEXEC flag. Return code is useless because either way, the
4207 (void) utils_set_fd_cloexec(new_sock
);
4209 DBG("Receiving data from client for health...");
4210 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4212 DBG("Nothing recv() from client... continuing");
4213 ret
= close(new_sock
);
4220 rcu_thread_online();
4222 memset(&reply
, 0, sizeof(reply
));
4223 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4225 * health_check_state returns 0 if health is
4228 if (!health_check_state(health_sessiond
, i
)) {
4229 reply
.ret_code
|= 1ULL << i
;
4233 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4235 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4237 ERR("Failed to send health data back to client");
4240 /* End of transmission */
4241 ret
= close(new_sock
);
4250 ERR("Health error occurred in %s", __func__
);
4252 DBG("Health check thread dying");
4253 unlink(config
.health_unix_sock_path
.value
);
4261 lttng_poll_clean(&events
);
4263 rcu_unregister_thread();
4268 * This thread manage all clients request using the unix client socket for
4271 static void *thread_manage_clients(void *data
)
4273 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4275 uint32_t revents
, nb_fd
;
4276 struct command_ctx
*cmd_ctx
= NULL
;
4277 struct lttng_poll_event events
;
4279 DBG("[thread] Manage client started");
4281 rcu_register_thread();
4283 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4285 health_code_update();
4287 ret
= lttcomm_listen_unix_sock(client_sock
);
4293 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4294 * more will be added to this poll set.
4296 ret
= sessiond_set_thread_pollset(&events
, 2);
4298 goto error_create_poll
;
4301 /* Add the application registration socket */
4302 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4307 sessiond_notify_ready();
4308 ret
= sem_post(&load_info
->message_thread_ready
);
4310 PERROR("sem_post message_thread_ready");
4314 /* This testpoint is after we signal readiness to the parent. */
4315 if (testpoint(sessiond_thread_manage_clients
)) {
4319 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4323 health_code_update();
4326 DBG("Accepting client command ...");
4328 /* Inifinite blocking call, waiting for transmission */
4330 health_poll_entry();
4331 ret
= lttng_poll_wait(&events
, -1);
4335 * Restart interrupted system call.
4337 if (errno
== EINTR
) {
4345 for (i
= 0; i
< nb_fd
; i
++) {
4346 /* Fetch once the poll data */
4347 revents
= LTTNG_POLL_GETEV(&events
, i
);
4348 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4350 health_code_update();
4353 /* No activity for this FD (poll implementation). */
4357 /* Thread quit pipe has been closed. Killing thread. */
4358 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4364 /* Event on the registration socket */
4365 if (pollfd
== client_sock
) {
4366 if (revents
& LPOLLIN
) {
4368 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4369 ERR("Client socket poll error");
4372 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4378 DBG("Wait for client response");
4380 health_code_update();
4382 sock
= lttcomm_accept_unix_sock(client_sock
);
4388 * Set the CLOEXEC flag. Return code is useless because either way, the
4391 (void) utils_set_fd_cloexec(sock
);
4393 /* Set socket option for credentials retrieval */
4394 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4399 /* Allocate context command to process the client request */
4400 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4401 if (cmd_ctx
== NULL
) {
4402 PERROR("zmalloc cmd_ctx");
4406 /* Allocate data buffer for reception */
4407 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4408 if (cmd_ctx
->lsm
== NULL
) {
4409 PERROR("zmalloc cmd_ctx->lsm");
4413 cmd_ctx
->llm
= NULL
;
4414 cmd_ctx
->session
= NULL
;
4416 health_code_update();
4419 * Data is received from the lttng client. The struct
4420 * lttcomm_session_msg (lsm) contains the command and data request of
4423 DBG("Receiving data from client ...");
4424 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4425 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4427 DBG("Nothing recv() from client... continuing");
4433 clean_command_ctx(&cmd_ctx
);
4437 health_code_update();
4439 // TODO: Validate cmd_ctx including sanity check for
4440 // security purpose.
4442 rcu_thread_online();
4444 * This function dispatch the work to the kernel or userspace tracer
4445 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4446 * informations for the client. The command context struct contains
4447 * everything this function may needs.
4449 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4450 rcu_thread_offline();
4458 * TODO: Inform client somehow of the fatal error. At
4459 * this point, ret < 0 means that a zmalloc failed
4460 * (ENOMEM). Error detected but still accept
4461 * command, unless a socket error has been
4464 clean_command_ctx(&cmd_ctx
);
4468 health_code_update();
4470 DBG("Sending response (size: %d, retcode: %s (%d))",
4471 cmd_ctx
->lttng_msg_size
,
4472 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4473 cmd_ctx
->llm
->ret_code
);
4474 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4476 ERR("Failed to send data back to client");
4479 /* End of transmission */
4486 clean_command_ctx(&cmd_ctx
);
4488 health_code_update();
4500 lttng_poll_clean(&events
);
4501 clean_command_ctx(&cmd_ctx
);
4505 unlink(config
.client_unix_sock_path
.value
);
4506 if (client_sock
>= 0) {
4507 ret
= close(client_sock
);
4515 ERR("Health error occurred in %s", __func__
);
4518 health_unregister(health_sessiond
);
4520 DBG("Client thread dying");
4522 rcu_unregister_thread();
4525 * Since we are creating the consumer threads, we own them, so we need
4526 * to join them before our thread exits.
4528 ret
= join_consumer_thread(&kconsumer_data
);
4531 PERROR("join_consumer");
4534 ret
= join_consumer_thread(&ustconsumer32_data
);
4537 PERROR("join_consumer ust32");
4540 ret
= join_consumer_thread(&ustconsumer64_data
);
4543 PERROR("join_consumer ust64");
4548 static int string_match(const char *str1
, const char *str2
)
4550 return (str1
&& str2
) && !strcmp(str1
, str2
);
4554 * Take an option from the getopt output and set it in the right variable to be
4557 * Return 0 on success else a negative value.
4559 static int set_option(int opt
, const char *arg
, const char *optname
)
4563 if (string_match(optname
, "client-sock") || opt
== 'c') {
4564 if (!arg
|| *arg
== '\0') {
4568 if (lttng_is_setuid_setgid()) {
4569 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4570 "-c, --client-sock");
4572 config_string_set(&config
.client_unix_sock_path
,
4574 if (!config
.client_unix_sock_path
.value
) {
4579 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4580 if (!arg
|| *arg
== '\0') {
4584 if (lttng_is_setuid_setgid()) {
4585 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4588 config_string_set(&config
.apps_unix_sock_path
,
4590 if (!config
.apps_unix_sock_path
.value
) {
4595 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4596 config
.daemonize
= true;
4597 } else if (string_match(optname
, "background") || opt
== 'b') {
4598 config
.background
= true;
4599 } else if (string_match(optname
, "group") || opt
== 'g') {
4600 if (!arg
|| *arg
== '\0') {
4604 if (lttng_is_setuid_setgid()) {
4605 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4608 config_string_set(&config
.tracing_group_name
,
4610 if (!config
.tracing_group_name
.value
) {
4615 } else if (string_match(optname
, "help") || opt
== 'h') {
4616 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4618 ERR("Cannot show --help for `lttng-sessiond`");
4621 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4622 } else if (string_match(optname
, "version") || opt
== 'V') {
4623 fprintf(stdout
, "%s\n", VERSION
);
4625 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4626 config
.sig_parent
= true;
4627 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4628 if (!arg
|| *arg
== '\0') {
4632 if (lttng_is_setuid_setgid()) {
4633 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4634 "--kconsumerd-err-sock");
4636 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4638 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4643 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4644 if (!arg
|| *arg
== '\0') {
4648 if (lttng_is_setuid_setgid()) {
4649 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4650 "--kconsumerd-cmd-sock");
4652 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4654 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4659 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4660 if (!arg
|| *arg
== '\0') {
4664 if (lttng_is_setuid_setgid()) {
4665 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4666 "--ustconsumerd64-err-sock");
4668 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4670 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4675 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4676 if (!arg
|| *arg
== '\0') {
4680 if (lttng_is_setuid_setgid()) {
4681 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4682 "--ustconsumerd64-cmd-sock");
4684 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4686 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4691 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4692 if (!arg
|| *arg
== '\0') {
4696 if (lttng_is_setuid_setgid()) {
4697 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4698 "--ustconsumerd32-err-sock");
4700 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4702 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4707 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4708 if (!arg
|| *arg
== '\0') {
4712 if (lttng_is_setuid_setgid()) {
4713 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4714 "--ustconsumerd32-cmd-sock");
4716 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4718 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4723 } else if (string_match(optname
, "no-kernel")) {
4724 config
.no_kernel
= true;
4725 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4726 lttng_opt_quiet
= true;
4727 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4728 /* Verbose level can increase using multiple -v */
4730 /* Value obtained from config file */
4731 config
.verbose
= config_parse_value(arg
);
4733 /* -v used on command line */
4736 /* Clamp value to [0, 3] */
4737 config
.verbose
= config
.verbose
< 0 ? 0 :
4738 (config
.verbose
<= 3 ? config
.verbose
: 3);
4739 } else if (string_match(optname
, "verbose-consumer")) {
4741 config
.verbose_consumer
= config_parse_value(arg
);
4743 config
.verbose_consumer
++;
4745 } else if (string_match(optname
, "consumerd32-path")) {
4746 if (!arg
|| *arg
== '\0') {
4750 if (lttng_is_setuid_setgid()) {
4751 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4752 "--consumerd32-path");
4754 config_string_set(&config
.consumerd32_bin_path
,
4756 if (!config
.consumerd32_bin_path
.value
) {
4761 } else if (string_match(optname
, "consumerd32-libdir")) {
4762 if (!arg
|| *arg
== '\0') {
4766 if (lttng_is_setuid_setgid()) {
4767 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4768 "--consumerd32-libdir");
4770 config_string_set(&config
.consumerd32_lib_dir
,
4772 if (!config
.consumerd32_lib_dir
.value
) {
4777 } else if (string_match(optname
, "consumerd64-path")) {
4778 if (!arg
|| *arg
== '\0') {
4782 if (lttng_is_setuid_setgid()) {
4783 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4784 "--consumerd64-path");
4786 config_string_set(&config
.consumerd64_bin_path
,
4788 if (!config
.consumerd64_bin_path
.value
) {
4793 } else if (string_match(optname
, "consumerd64-libdir")) {
4794 if (!arg
|| *arg
== '\0') {
4798 if (lttng_is_setuid_setgid()) {
4799 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4800 "--consumerd64-libdir");
4802 config_string_set(&config
.consumerd64_lib_dir
,
4804 if (!config
.consumerd64_lib_dir
.value
) {
4809 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4810 if (!arg
|| *arg
== '\0') {
4814 if (lttng_is_setuid_setgid()) {
4815 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4818 config_string_set(&config
.pid_file_path
, strdup(arg
));
4819 if (!config
.pid_file_path
.value
) {
4824 } else if (string_match(optname
, "agent-tcp-port")) {
4825 if (!arg
|| *arg
== '\0') {
4829 if (lttng_is_setuid_setgid()) {
4830 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4831 "--agent-tcp-port");
4836 v
= strtoul(arg
, NULL
, 0);
4837 if (errno
!= 0 || !isdigit(arg
[0])) {
4838 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4841 if (v
== 0 || v
>= 65535) {
4842 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4845 config
.agent_tcp_port
= (uint32_t) v
;
4846 DBG3("Agent TCP port set to non default: %u", config
.agent_tcp_port
);
4848 } else if (string_match(optname
, "load") || opt
== 'l') {
4849 if (!arg
|| *arg
== '\0') {
4853 if (lttng_is_setuid_setgid()) {
4854 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4857 config_string_set(&config
.load_session_path
, strdup(arg
));
4858 if (!config
.load_session_path
.value
) {
4863 } else if (string_match(optname
, "kmod-probes")) {
4864 if (!arg
|| *arg
== '\0') {
4868 if (lttng_is_setuid_setgid()) {
4869 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4872 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4873 if (!config
.kmod_probes_list
.value
) {
4878 } else if (string_match(optname
, "extra-kmod-probes")) {
4879 if (!arg
|| *arg
== '\0') {
4883 if (lttng_is_setuid_setgid()) {
4884 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4885 "--extra-kmod-probes");
4887 config_string_set(&config
.kmod_extra_probes_list
,
4889 if (!config
.kmod_extra_probes_list
.value
) {
4894 } else if (string_match(optname
, "config") || opt
== 'f') {
4895 /* This is handled in set_options() thus silent skip. */
4898 /* Unknown option or other error.
4899 * Error is printed by getopt, just return */
4904 if (ret
== -EINVAL
) {
4905 const char *opt_name
= "unknown";
4908 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4910 if (opt
== long_options
[i
].val
) {
4911 opt_name
= long_options
[i
].name
;
4916 WARN("Invalid argument provided for option \"%s\", using default value.",
4924 * config_entry_handler_cb used to handle options read from a config file.
4925 * See config_entry_handler_cb comment in common/config/session-config.h for the
4926 * return value conventions.
4928 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4932 if (!entry
|| !entry
->name
|| !entry
->value
) {
4937 /* Check if the option is to be ignored */
4938 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4939 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4944 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4947 /* Ignore if not fully matched. */
4948 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4953 * If the option takes no argument on the command line, we have to
4954 * check if the value is "true". We support non-zero numeric values,
4957 if (!long_options
[i
].has_arg
) {
4958 ret
= config_parse_value(entry
->value
);
4961 WARN("Invalid configuration value \"%s\" for option %s",
4962 entry
->value
, entry
->name
);
4964 /* False, skip boolean config option. */
4969 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4973 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4980 * daemon configuration loading and argument parsing
4982 static int set_options(int argc
, char **argv
)
4984 int ret
= 0, c
= 0, option_index
= 0;
4985 int orig_optopt
= optopt
, orig_optind
= optind
;
4987 const char *config_path
= NULL
;
4989 optstring
= utils_generate_optstring(long_options
,
4990 sizeof(long_options
) / sizeof(struct option
));
4996 /* Check for the --config option */
4997 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4998 &option_index
)) != -1) {
5002 } else if (c
!= 'f') {
5003 /* if not equal to --config option. */
5007 if (lttng_is_setuid_setgid()) {
5008 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5011 config_path
= utils_expand_path(optarg
);
5013 ERR("Failed to resolve path: %s", optarg
);
5018 ret
= config_get_section_entries(config_path
, config_section_name
,
5019 config_entry_handler
, NULL
);
5022 ERR("Invalid configuration option at line %i", ret
);
5028 /* Reset getopt's global state */
5029 optopt
= orig_optopt
;
5030 optind
= orig_optind
;
5034 * getopt_long() will not set option_index if it encounters a
5037 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5044 * Pass NULL as the long option name if popt left the index
5047 ret
= set_option(c
, optarg
,
5048 option_index
< 0 ? NULL
:
5049 long_options
[option_index
].name
);
5061 * Creates the two needed socket by the daemon.
5062 * apps_sock - The communication socket for all UST apps.
5063 * client_sock - The communication of the cli tool (lttng).
5065 static int init_daemon_socket(void)
5070 old_umask
= umask(0);
5072 /* Create client tool unix socket */
5073 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5074 if (client_sock
< 0) {
5075 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5080 /* Set the cloexec flag */
5081 ret
= utils_set_fd_cloexec(client_sock
);
5083 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5084 "Continuing but note that the consumer daemon will have a "
5085 "reference to this socket on exec()", client_sock
);
5088 /* File permission MUST be 660 */
5089 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5091 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5096 /* Create the application unix socket */
5097 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5098 if (apps_sock
< 0) {
5099 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5104 /* Set the cloexec flag */
5105 ret
= utils_set_fd_cloexec(apps_sock
);
5107 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5108 "Continuing but note that the consumer daemon will have a "
5109 "reference to this socket on exec()", apps_sock
);
5112 /* File permission MUST be 666 */
5113 ret
= chmod(config
.apps_unix_sock_path
.value
,
5114 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5116 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5121 DBG3("Session daemon client socket %d and application socket %d created",
5122 client_sock
, apps_sock
);
5130 * Check if the global socket is available, and if a daemon is answering at the
5131 * other side. If yes, error is returned.
5133 static int check_existing_daemon(void)
5135 /* Is there anybody out there ? */
5136 if (lttng_session_daemon_alive()) {
5144 * Set the tracing group gid onto the client socket.
5146 * Race window between mkdir and chown is OK because we are going from more
5147 * permissive (root.root) to less permissive (root.tracing).
5149 static int set_permissions(char *rundir
)
5154 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5156 /* Set lttng run dir */
5157 ret
= chown(rundir
, 0, gid
);
5159 ERR("Unable to set group on %s", rundir
);
5164 * Ensure all applications and tracing group can search the run
5165 * dir. Allow everyone to read the directory, since it does not
5166 * buy us anything to hide its content.
5168 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5170 ERR("Unable to set permissions on %s", rundir
);
5174 /* lttng client socket path */
5175 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5177 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5181 /* kconsumer error socket path */
5182 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5184 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5188 /* 64-bit ustconsumer error socket path */
5189 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5191 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5195 /* 32-bit ustconsumer compat32 error socket path */
5196 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5198 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5202 DBG("All permissions are set");
5208 * Create the lttng run directory needed for all global sockets and pipe.
5210 static int create_lttng_rundir(void)
5214 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5216 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5218 if (errno
!= EEXIST
) {
5219 ERR("Unable to create %s", config
.rundir
.value
);
5231 * Setup sockets and directory needed by the consumerds' communication with the
5234 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5239 switch (consumer_data
->type
) {
5240 case LTTNG_CONSUMER_KERNEL
:
5241 path
= config
.kconsumerd_path
.value
;
5243 case LTTNG_CONSUMER64_UST
:
5244 path
= config
.consumerd64_path
.value
;
5246 case LTTNG_CONSUMER32_UST
:
5247 path
= config
.consumerd32_path
.value
;
5250 ERR("Consumer type unknown");
5256 DBG2("Creating consumer directory: %s", path
);
5258 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5259 if (ret
< 0 && errno
!= EEXIST
) {
5261 ERR("Failed to create %s", path
);
5265 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5267 ERR("Unable to set group on %s", path
);
5273 /* Create the consumerd error unix socket */
5274 consumer_data
->err_sock
=
5275 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5276 if (consumer_data
->err_sock
< 0) {
5277 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5283 * Set the CLOEXEC flag. Return code is useless because either way, the
5286 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5288 PERROR("utils_set_fd_cloexec");
5289 /* continue anyway */
5292 /* File permission MUST be 660 */
5293 ret
= chmod(consumer_data
->err_unix_sock_path
,
5294 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5296 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5306 * Signal handler for the daemon
5308 * Simply stop all worker threads, leaving main() return gracefully after
5309 * joining all threads and calling cleanup().
5311 static void sighandler(int sig
)
5315 DBG("SIGINT caught");
5319 DBG("SIGTERM caught");
5323 CMM_STORE_SHARED(recv_child_signal
, 1);
5331 * Setup signal handler for :
5332 * SIGINT, SIGTERM, SIGPIPE
5334 static int set_signal_handler(void)
5337 struct sigaction sa
;
5340 if ((ret
= sigemptyset(&sigset
)) < 0) {
5341 PERROR("sigemptyset");
5345 sa
.sa_mask
= sigset
;
5348 sa
.sa_handler
= sighandler
;
5349 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5350 PERROR("sigaction");
5354 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5355 PERROR("sigaction");
5359 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5360 PERROR("sigaction");
5364 sa
.sa_handler
= SIG_IGN
;
5365 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5366 PERROR("sigaction");
5370 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5376 * Set open files limit to unlimited. This daemon can open a large number of
5377 * file descriptors in order to consume multiple kernel traces.
5379 static void set_ulimit(void)
5384 /* The kernel does not allow an infinite limit for open files */
5385 lim
.rlim_cur
= 65535;
5386 lim
.rlim_max
= 65535;
5388 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5390 PERROR("failed to set open files limit");
5394 static int write_pidfile(void)
5396 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5400 * Create lockfile using the rundir and return its fd.
5402 static int create_lockfile(void)
5404 return utils_create_lock_file(config
.lock_file_path
.value
);
5408 * Write agent TCP port using the rundir.
5410 static int write_agent_port(void)
5412 return utils_create_pid_file(config
.agent_tcp_port
,
5413 config
.agent_port_file_path
.value
);
5416 static int set_clock_plugin_env(void)
5419 char *env_value
= NULL
;
5421 if (!config
.lttng_ust_clock_plugin
.value
) {
5425 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5426 config
.lttng_ust_clock_plugin
.value
);
5432 ret
= putenv(env_value
);
5435 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5439 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5440 config
.lttng_ust_clock_plugin
.value
);
5448 int main(int argc
, char **argv
)
5450 int ret
= 0, retval
= 0;
5452 const char *env_app_timeout
;
5453 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5454 *ust64_channel_monitor_pipe
= NULL
,
5455 *kernel_channel_monitor_pipe
= NULL
;
5456 bool notification_thread_running
= false;
5458 init_kernel_workarounds();
5460 rcu_register_thread();
5462 if (set_signal_handler()) {
5464 goto exit_set_signal_handler
;
5467 page_size
= sysconf(_SC_PAGESIZE
);
5468 if (page_size
< 0) {
5469 PERROR("sysconf _SC_PAGESIZE");
5470 page_size
= LONG_MAX
;
5471 WARN("Fallback page size to %ld", page_size
);
5474 ret
= sessiond_config_init(&config
);
5477 goto exit_set_signal_handler
;
5481 * Parse arguments and load the daemon configuration file.
5483 * We have an exit_options exit path to free memory reserved by
5484 * set_options. This is needed because the rest of sessiond_cleanup()
5485 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5486 * depends on set_options.
5489 if (set_options(argc
, argv
)) {
5494 /* Init config from environment variables. */
5495 sessiond_config_apply_env_config(&config
);
5498 * Resolve all paths received as arguments, configuration option, or
5499 * through environment variable as absolute paths. This is necessary
5500 * since daemonizing causes the sessiond's current working directory
5503 ret
= sessiond_config_resolve_paths(&config
);
5509 lttng_opt_verbose
= config
.verbose
;
5510 lttng_opt_quiet
= config
.quiet
;
5511 kconsumer_data
.err_unix_sock_path
=
5512 config
.kconsumerd_err_unix_sock_path
.value
;
5513 kconsumer_data
.cmd_unix_sock_path
=
5514 config
.kconsumerd_cmd_unix_sock_path
.value
;
5515 ustconsumer32_data
.err_unix_sock_path
=
5516 config
.consumerd32_err_unix_sock_path
.value
;
5517 ustconsumer32_data
.cmd_unix_sock_path
=
5518 config
.consumerd32_cmd_unix_sock_path
.value
;
5519 ustconsumer64_data
.err_unix_sock_path
=
5520 config
.consumerd64_err_unix_sock_path
.value
;
5521 ustconsumer64_data
.cmd_unix_sock_path
=
5522 config
.consumerd64_cmd_unix_sock_path
.value
;
5523 set_clock_plugin_env();
5525 sessiond_config_log(&config
);
5528 if (config
.daemonize
|| config
.background
) {
5531 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5532 !config
.background
);
5539 * We are in the child. Make sure all other file descriptors are
5540 * closed, in case we are called with more opened file
5541 * descriptors than the standard ones.
5543 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5548 if (run_as_create_worker(argv
[0]) < 0) {
5549 goto exit_create_run_as_worker_cleanup
;
5553 * Starting from here, we can create threads. This needs to be after
5554 * lttng_daemonize due to RCU.
5558 * Initialize the health check subsystem. This call should set the
5559 * appropriate time values.
5561 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5562 if (!health_sessiond
) {
5563 PERROR("health_app_create error");
5565 goto exit_health_sessiond_cleanup
;
5568 /* Create thread to clean up RCU hash tables */
5569 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5571 goto exit_ht_cleanup
;
5574 /* Create thread quit pipe */
5575 if (init_thread_quit_pipe()) {
5577 goto exit_init_data
;
5580 /* Check if daemon is UID = 0 */
5581 is_root
= !getuid();
5583 if (create_lttng_rundir()) {
5585 goto exit_init_data
;
5589 /* Create global run dir with root access */
5591 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5592 if (!kernel_channel_monitor_pipe
) {
5593 ERR("Failed to create kernel consumer channel monitor pipe");
5595 goto exit_init_data
;
5597 kconsumer_data
.channel_monitor_pipe
=
5598 lttng_pipe_release_writefd(
5599 kernel_channel_monitor_pipe
);
5600 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5602 goto exit_init_data
;
5606 lockfile_fd
= create_lockfile();
5607 if (lockfile_fd
< 0) {
5609 goto exit_init_data
;
5612 /* Set consumer initial state */
5613 kernel_consumerd_state
= CONSUMER_STOPPED
;
5614 ust_consumerd_state
= CONSUMER_STOPPED
;
5616 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5617 if (!ust32_channel_monitor_pipe
) {
5618 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5620 goto exit_init_data
;
5622 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5623 ust32_channel_monitor_pipe
);
5624 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5626 goto exit_init_data
;
5629 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5630 if (!ust64_channel_monitor_pipe
) {
5631 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5633 goto exit_init_data
;
5635 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5636 ust64_channel_monitor_pipe
);
5637 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5639 goto exit_init_data
;
5643 * See if daemon already exist.
5645 if (check_existing_daemon()) {
5646 ERR("Already running daemon.\n");
5648 * We do not goto exit because we must not cleanup()
5649 * because a daemon is already running.
5652 goto exit_init_data
;
5656 * Init UST app hash table. Alloc hash table before this point since
5657 * cleanup() can get called after that point.
5659 if (ust_app_ht_alloc()) {
5660 ERR("Failed to allocate UST app hash table");
5662 goto exit_init_data
;
5666 * Initialize agent app hash table. We allocate the hash table here
5667 * since cleanup() can get called after this point.
5669 if (agent_app_ht_alloc()) {
5670 ERR("Failed to allocate Agent app hash table");
5672 goto exit_init_data
;
5676 * These actions must be executed as root. We do that *after* setting up
5677 * the sockets path because we MUST make the check for another daemon using
5678 * those paths *before* trying to set the kernel consumer sockets and init
5682 if (set_consumer_sockets(&kconsumer_data
)) {
5684 goto exit_init_data
;
5687 /* Setup kernel tracer */
5688 if (!config
.no_kernel
) {
5689 init_kernel_tracer();
5690 if (kernel_tracer_fd
>= 0) {
5691 ret
= syscall_init_table();
5693 ERR("Unable to populate syscall table. "
5694 "Syscall tracing won't work "
5695 "for this session daemon.");
5700 /* Set ulimit for open files */
5703 /* init lttng_fd tracking must be done after set_ulimit. */
5706 if (set_consumer_sockets(&ustconsumer64_data
)) {
5708 goto exit_init_data
;
5711 if (set_consumer_sockets(&ustconsumer32_data
)) {
5713 goto exit_init_data
;
5716 /* Setup the needed unix socket */
5717 if (init_daemon_socket()) {
5719 goto exit_init_data
;
5722 /* Set credentials to socket */
5723 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5725 goto exit_init_data
;
5728 /* Get parent pid if -S, --sig-parent is specified. */
5729 if (config
.sig_parent
) {
5733 /* Setup the kernel pipe for waking up the kernel thread */
5734 if (is_root
&& !config
.no_kernel
) {
5735 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5737 goto exit_init_data
;
5741 /* Setup the thread apps communication pipe. */
5742 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5744 goto exit_init_data
;
5747 /* Setup the thread apps notify communication pipe. */
5748 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5750 goto exit_init_data
;
5753 /* Initialize global buffer per UID and PID registry. */
5754 buffer_reg_init_uid_registry();
5755 buffer_reg_init_pid_registry();
5757 /* Init UST command queue. */
5758 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5761 * Get session list pointer. This pointer MUST NOT be free'd. This list
5762 * is statically declared in session.c
5764 session_list_ptr
= session_get_list();
5768 /* Check for the application socket timeout env variable. */
5769 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5770 if (env_app_timeout
) {
5771 config
.app_socket_timeout
= atoi(env_app_timeout
);
5773 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5776 ret
= write_pidfile();
5778 ERR("Error in write_pidfile");
5780 goto exit_init_data
;
5782 ret
= write_agent_port();
5784 ERR("Error in write_agent_port");
5786 goto exit_init_data
;
5789 /* Initialize communication library */
5791 /* Initialize TCP timeout values */
5792 lttcomm_inet_init();
5794 if (load_session_init_data(&load_info
) < 0) {
5796 goto exit_init_data
;
5798 load_info
->path
= config
.load_session_path
.value
;
5800 /* Create health-check thread. */
5801 ret
= pthread_create(&health_thread
, default_pthread_attr(),
5802 thread_manage_health
, (void *) NULL
);
5805 PERROR("pthread_create health");
5810 /* notification_thread_data acquires the pipes' read side. */
5811 notification_thread_handle
= notification_thread_handle_create(
5812 ust32_channel_monitor_pipe
,
5813 ust64_channel_monitor_pipe
,
5814 kernel_channel_monitor_pipe
);
5815 if (!notification_thread_handle
) {
5817 ERR("Failed to create notification thread shared data");
5819 goto exit_notification
;
5822 /* Create notification thread. */
5823 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
5824 thread_notification
, notification_thread_handle
);
5827 PERROR("pthread_create notification");
5830 goto exit_notification
;
5832 notification_thread_running
= true;
5834 /* Create thread to manage the client socket */
5835 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5836 thread_manage_clients
, (void *) NULL
);
5839 PERROR("pthread_create clients");
5845 /* Create thread to dispatch registration */
5846 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5847 thread_dispatch_ust_registration
, (void *) NULL
);
5850 PERROR("pthread_create dispatch");
5856 /* Create thread to manage application registration. */
5857 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5858 thread_registration_apps
, (void *) NULL
);
5861 PERROR("pthread_create registration");
5867 /* Create thread to manage application socket */
5868 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5869 thread_manage_apps
, (void *) NULL
);
5872 PERROR("pthread_create apps");
5878 /* Create thread to manage application notify socket */
5879 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5880 ust_thread_manage_notify
, (void *) NULL
);
5883 PERROR("pthread_create notify");
5886 goto exit_apps_notify
;
5889 /* Create agent registration thread. */
5890 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5891 agent_thread_manage_registration
, (void *) NULL
);
5894 PERROR("pthread_create agent");
5897 goto exit_agent_reg
;
5900 /* Don't start this thread if kernel tracing is not requested nor root */
5901 if (is_root
&& !config
.no_kernel
) {
5902 /* Create kernel thread to manage kernel event */
5903 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5904 thread_manage_kernel
, (void *) NULL
);
5907 PERROR("pthread_create kernel");
5914 /* Create session loading thread. */
5915 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5916 thread_load_session
, load_info
);
5919 PERROR("pthread_create load_session_thread");
5922 goto exit_load_session
;
5926 * This is where we start awaiting program completion (e.g. through
5927 * signal that asks threads to teardown).
5930 ret
= pthread_join(load_session_thread
, &status
);
5933 PERROR("pthread_join load_session_thread");
5938 if (is_root
&& !config
.no_kernel
) {
5939 ret
= pthread_join(kernel_thread
, &status
);
5942 PERROR("pthread_join");
5948 ret
= pthread_join(agent_reg_thread
, &status
);
5951 PERROR("pthread_join agent");
5956 ret
= pthread_join(apps_notify_thread
, &status
);
5959 PERROR("pthread_join apps notify");
5964 ret
= pthread_join(apps_thread
, &status
);
5967 PERROR("pthread_join apps");
5972 ret
= pthread_join(reg_apps_thread
, &status
);
5975 PERROR("pthread_join");
5981 * Join dispatch thread after joining reg_apps_thread to ensure
5982 * we don't leak applications in the queue.
5984 ret
= pthread_join(dispatch_thread
, &status
);
5987 PERROR("pthread_join");
5992 ret
= pthread_join(client_thread
, &status
);
5995 PERROR("pthread_join");
6001 ret
= pthread_join(health_thread
, &status
);
6004 PERROR("pthread_join health thread");
6011 * Wait for all pending call_rcu work to complete before tearing
6012 * down data structures. call_rcu worker may be trying to
6013 * perform lookups in those structures.
6017 * sessiond_cleanup() is called when no other thread is running, except
6018 * the ht_cleanup thread, which is needed to destroy the hash tables.
6020 rcu_thread_online();
6024 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6025 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6026 * the queue is empty before shutting down the clean-up thread.
6031 * The teardown of the notification system is performed after the
6032 * session daemon's teardown in order to allow it to be notified
6033 * of the active session and channels at the moment of the teardown.
6035 if (notification_thread_handle
) {
6036 if (notification_thread_running
) {
6037 notification_thread_command_quit(
6038 notification_thread_handle
);
6039 ret
= pthread_join(notification_thread
, &status
);
6042 PERROR("pthread_join notification thread");
6046 notification_thread_handle_destroy(notification_thread_handle
);
6049 rcu_thread_offline();
6050 rcu_unregister_thread();
6052 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6056 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6057 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6058 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6061 health_app_destroy(health_sessiond
);
6062 exit_health_sessiond_cleanup
:
6063 exit_create_run_as_worker_cleanup
:
6066 sessiond_cleanup_options();
6068 exit_set_signal_handler
: