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 * The initialization of the session daemon is done in multiple phases.
291 * While all threads are launched near-simultaneously, only some of them
292 * are needed to ensure the session daemon can start to respond to client
295 * There are two important guarantees that we wish to offer with respect
296 * to the initialisation of the session daemon:
297 * - When the daemonize/background launcher process exits, the sessiond
298 * is fully able to respond to client requests,
299 * - Auto-loaded sessions are visible to clients.
301 * In order to achieve this, a number of support threads have to be launched
302 * to allow the "client" thread to function properly. Moreover, since the
303 * "load session" thread needs the client thread, we must provide a way
304 * for the "load session" thread to know that the "client" thread is up
307 * Hence, the support threads decrement the lttng_sessiond_ready counter
308 * while the "client" threads waits for it to reach 0. Once the "client" thread
309 * unblocks, it posts the message_thread_ready semaphore which allows the
310 * "load session" thread to progress.
312 * This implies that the "load session" thread is the last to be initialized
313 * and will explicitly call sessiond_signal_parents(), which signals the parents
314 * that the session daemon is fully initialized.
316 * The three (3) support threads are:
318 * - notification_thread
321 int lttng_sessiond_ready
= 3;
323 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
325 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
328 /* Notify parents that we are ready for cmd and health check */
330 void sessiond_signal_parents(void)
333 * Notify parent pid that we are ready to accept command
334 * for client side. This ppid is the one from the
335 * external process that spawned us.
337 if (config
.sig_parent
) {
342 * Notify the parent of the fork() process that we are
345 if (config
.daemonize
|| config
.background
) {
346 kill(child_ppid
, SIGUSR1
);
351 void sessiond_notify_ready(void)
354 * The _return variant is used since the implied memory barriers are
357 (void) uatomic_sub_return(<tng_sessiond_ready
, 1);
361 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
368 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
374 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
386 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
388 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
390 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
394 * Init thread quit pipe.
396 * Return -1 on error or 0 if all pipes are created.
398 static int __init_thread_quit_pipe(int *a_pipe
)
404 PERROR("thread quit pipe");
408 for (i
= 0; i
< 2; i
++) {
409 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
420 static int init_thread_quit_pipe(void)
422 return __init_thread_quit_pipe(thread_quit_pipe
);
426 * Stop all threads by closing the thread quit pipe.
428 static void stop_threads(void)
432 /* Stopping all threads */
433 DBG("Terminating all threads");
434 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
436 ERR("write error on thread quit pipe");
439 /* Dispatch thread */
440 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
441 futex_nto1_wake(&ust_cmd_queue
.futex
);
445 * Close every consumer sockets.
447 static void close_consumer_sockets(void)
451 if (kconsumer_data
.err_sock
>= 0) {
452 ret
= close(kconsumer_data
.err_sock
);
454 PERROR("kernel consumer err_sock close");
457 if (ustconsumer32_data
.err_sock
>= 0) {
458 ret
= close(ustconsumer32_data
.err_sock
);
460 PERROR("UST consumerd32 err_sock close");
463 if (ustconsumer64_data
.err_sock
>= 0) {
464 ret
= close(ustconsumer64_data
.err_sock
);
466 PERROR("UST consumerd64 err_sock close");
469 if (kconsumer_data
.cmd_sock
>= 0) {
470 ret
= close(kconsumer_data
.cmd_sock
);
472 PERROR("kernel consumer cmd_sock close");
475 if (ustconsumer32_data
.cmd_sock
>= 0) {
476 ret
= close(ustconsumer32_data
.cmd_sock
);
478 PERROR("UST consumerd32 cmd_sock close");
481 if (ustconsumer64_data
.cmd_sock
>= 0) {
482 ret
= close(ustconsumer64_data
.cmd_sock
);
484 PERROR("UST consumerd64 cmd_sock close");
487 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
488 ret
= close(kconsumer_data
.channel_monitor_pipe
);
490 PERROR("kernel consumer channel monitor pipe close");
493 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
494 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
496 PERROR("UST consumerd32 channel monitor pipe close");
499 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
500 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
502 PERROR("UST consumerd64 channel monitor pipe close");
508 * Wait on consumer process termination.
510 * Need to be called with the consumer data lock held or from a context
511 * ensuring no concurrent access to data (e.g: cleanup).
513 static void wait_consumer(struct consumer_data
*consumer_data
)
518 if (consumer_data
->pid
<= 0) {
522 DBG("Waiting for complete teardown of consumerd (PID: %d)",
524 ret
= waitpid(consumer_data
->pid
, &status
, 0);
526 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
527 } else if (!WIFEXITED(status
)) {
528 ERR("consumerd termination with error: %d",
531 consumer_data
->pid
= 0;
535 * Cleanup the session daemon's data structures.
537 static void sessiond_cleanup(void)
540 struct ltt_session
*sess
, *stmp
;
542 DBG("Cleanup sessiond");
545 * Close the thread quit pipe. It has already done its job,
546 * since we are now called.
548 utils_close_pipe(thread_quit_pipe
);
551 * If config.pid_file_path.value is undefined, the default file will be
552 * wiped when removing the rundir.
554 if (config
.pid_file_path
.value
) {
555 ret
= remove(config
.pid_file_path
.value
);
557 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
561 DBG("Removing sessiond and consumerd content of directory %s",
562 config
.rundir
.value
);
565 DBG("Removing %s", config
.pid_file_path
.value
);
566 (void) unlink(config
.pid_file_path
.value
);
568 DBG("Removing %s", config
.agent_port_file_path
.value
);
569 (void) unlink(config
.agent_port_file_path
.value
);
572 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
573 (void) unlink(kconsumer_data
.err_unix_sock_path
);
575 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
576 (void) rmdir(config
.kconsumerd_path
.value
);
578 /* ust consumerd 32 */
579 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
580 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
582 DBG("Removing directory %s", config
.consumerd32_path
.value
);
583 (void) rmdir(config
.consumerd32_path
.value
);
585 /* ust consumerd 64 */
586 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
587 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
589 DBG("Removing directory %s", config
.consumerd64_path
.value
);
590 (void) rmdir(config
.consumerd64_path
.value
);
592 DBG("Cleaning up all sessions");
594 /* Destroy session list mutex */
595 if (session_list_ptr
!= NULL
) {
596 pthread_mutex_destroy(&session_list_ptr
->lock
);
598 /* Cleanup ALL session */
599 cds_list_for_each_entry_safe(sess
, stmp
,
600 &session_list_ptr
->head
, list
) {
601 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
605 wait_consumer(&kconsumer_data
);
606 wait_consumer(&ustconsumer64_data
);
607 wait_consumer(&ustconsumer32_data
);
609 DBG("Cleaning up all agent apps");
610 agent_app_ht_clean();
612 DBG("Closing all UST sockets");
613 ust_app_clean_list();
614 buffer_reg_destroy_registries();
616 if (is_root
&& !config
.no_kernel
) {
617 DBG2("Closing kernel fd");
618 if (kernel_tracer_fd
>= 0) {
619 ret
= close(kernel_tracer_fd
);
624 DBG("Unloading kernel modules");
625 modprobe_remove_lttng_all();
629 close_consumer_sockets();
632 load_session_destroy_data(load_info
);
637 * We do NOT rmdir rundir because there are other processes
638 * using it, for instance lttng-relayd, which can start in
639 * parallel with this teardown.
644 * Cleanup the daemon's option data structures.
646 static void sessiond_cleanup_options(void)
648 DBG("Cleaning up options");
650 sessiond_config_fini(&config
);
652 run_as_destroy_worker();
656 * Send data on a unix socket using the liblttsessiondcomm API.
658 * Return lttcomm error code.
660 static int send_unix_sock(int sock
, void *buf
, size_t len
)
662 /* Check valid length */
667 return lttcomm_send_unix_sock(sock
, buf
, len
);
671 * Free memory of a command context structure.
673 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
675 DBG("Clean command context structure");
677 if ((*cmd_ctx
)->llm
) {
678 free((*cmd_ctx
)->llm
);
680 if ((*cmd_ctx
)->lsm
) {
681 free((*cmd_ctx
)->lsm
);
689 * Notify UST applications using the shm mmap futex.
691 static int notify_ust_apps(int active
)
695 DBG("Notifying applications of session daemon state: %d", active
);
697 /* See shm.c for this call implying mmap, shm and futex calls */
698 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
699 if (wait_shm_mmap
== NULL
) {
703 /* Wake waiting process */
704 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
706 /* Apps notified successfully */
714 * Setup the outgoing data buffer for the response (llm) by allocating the
715 * right amount of memory and copying the original information from the lsm
718 * Return 0 on success, negative value on error.
720 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
721 const void *payload_buf
, size_t payload_len
,
722 const void *cmd_header_buf
, size_t cmd_header_len
)
725 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
726 const size_t cmd_header_offset
= header_len
;
727 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
728 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
730 cmd_ctx
->llm
= zmalloc(total_msg_size
);
732 if (cmd_ctx
->llm
== NULL
) {
738 /* Copy common data */
739 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
740 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
741 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
742 cmd_ctx
->llm
->data_size
= payload_len
;
743 cmd_ctx
->lttng_msg_size
= total_msg_size
;
745 /* Copy command header */
746 if (cmd_header_len
) {
747 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
753 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
762 * Version of setup_lttng_msg() without command header.
764 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
765 void *payload_buf
, size_t payload_len
)
767 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
770 * Update the kernel poll set of all channel fd available over all tracing
771 * session. Add the wakeup pipe at the end of the set.
773 static int update_kernel_poll(struct lttng_poll_event
*events
)
776 struct ltt_session
*session
;
777 struct ltt_kernel_channel
*channel
;
779 DBG("Updating kernel poll set");
782 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
783 session_lock(session
);
784 if (session
->kernel_session
== NULL
) {
785 session_unlock(session
);
789 cds_list_for_each_entry(channel
,
790 &session
->kernel_session
->channel_list
.head
, list
) {
791 /* Add channel fd to the kernel poll set */
792 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
794 session_unlock(session
);
797 DBG("Channel fd %d added to kernel set", channel
->fd
);
799 session_unlock(session
);
801 session_unlock_list();
806 session_unlock_list();
811 * Find the channel fd from 'fd' over all tracing session. When found, check
812 * for new channel stream and send those stream fds to the kernel consumer.
814 * Useful for CPU hotplug feature.
816 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
819 struct ltt_session
*session
;
820 struct ltt_kernel_session
*ksess
;
821 struct ltt_kernel_channel
*channel
;
823 DBG("Updating kernel streams for channel fd %d", fd
);
826 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
827 session_lock(session
);
828 if (session
->kernel_session
== NULL
) {
829 session_unlock(session
);
832 ksess
= session
->kernel_session
;
834 cds_list_for_each_entry(channel
,
835 &ksess
->channel_list
.head
, list
) {
836 struct lttng_ht_iter iter
;
837 struct consumer_socket
*socket
;
839 if (channel
->fd
!= fd
) {
842 DBG("Channel found, updating kernel streams");
843 ret
= kernel_open_channel_stream(channel
);
847 /* Update the stream global counter */
848 ksess
->stream_count_global
+= ret
;
851 * Have we already sent fds to the consumer? If yes, it
852 * means that tracing is started so it is safe to send
853 * our updated stream fds.
855 if (ksess
->consumer_fds_sent
!= 1
856 || ksess
->consumer
== NULL
) {
862 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
863 &iter
.iter
, socket
, node
.node
) {
864 pthread_mutex_lock(socket
->lock
);
865 ret
= kernel_consumer_send_channel_stream(socket
,
867 session
->output_traces
? 1 : 0);
868 pthread_mutex_unlock(socket
->lock
);
876 session_unlock(session
);
878 session_unlock_list();
882 session_unlock(session
);
883 session_unlock_list();
888 * For each tracing session, update newly registered apps. The session list
889 * lock MUST be acquired before calling this.
891 static void update_ust_app(int app_sock
)
893 struct ltt_session
*sess
, *stmp
;
895 /* Consumer is in an ERROR state. Stop any application update. */
896 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
897 /* Stop the update process since the consumer is dead. */
901 /* For all tracing session(s) */
902 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
906 if (!sess
->ust_session
) {
911 assert(app_sock
>= 0);
912 app
= ust_app_find_by_sock(app_sock
);
915 * Application can be unregistered before so
916 * this is possible hence simply stopping the
919 DBG3("UST app update failed to find app sock %d",
923 ust_app_global_update(sess
->ust_session
, app
);
927 session_unlock(sess
);
932 * This thread manage event coming from the kernel.
934 * Features supported in this thread:
937 static void *thread_manage_kernel(void *data
)
939 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
940 uint32_t revents
, nb_fd
;
942 struct lttng_poll_event events
;
944 DBG("[thread] Thread manage kernel started");
946 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
949 * This first step of the while is to clean this structure which could free
950 * non NULL pointers so initialize it before the loop.
952 lttng_poll_init(&events
);
954 if (testpoint(sessiond_thread_manage_kernel
)) {
955 goto error_testpoint
;
958 health_code_update();
960 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
961 goto error_testpoint
;
965 health_code_update();
967 if (update_poll_flag
== 1) {
968 /* Clean events object. We are about to populate it again. */
969 lttng_poll_clean(&events
);
971 ret
= sessiond_set_thread_pollset(&events
, 2);
973 goto error_poll_create
;
976 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
981 /* This will add the available kernel channel if any. */
982 ret
= update_kernel_poll(&events
);
986 update_poll_flag
= 0;
989 DBG("Thread kernel polling");
991 /* Poll infinite value of time */
994 ret
= lttng_poll_wait(&events
, -1);
995 DBG("Thread kernel return from poll on %d fds",
996 LTTNG_POLL_GETNB(&events
));
1000 * Restart interrupted system call.
1002 if (errno
== EINTR
) {
1006 } else if (ret
== 0) {
1007 /* Should not happen since timeout is infinite */
1008 ERR("Return value of poll is 0 with an infinite timeout.\n"
1009 "This should not have happened! Continuing...");
1015 for (i
= 0; i
< nb_fd
; i
++) {
1016 /* Fetch once the poll data */
1017 revents
= LTTNG_POLL_GETEV(&events
, i
);
1018 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1020 health_code_update();
1023 /* No activity for this FD (poll implementation). */
1027 /* Thread quit pipe has been closed. Killing thread. */
1028 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1034 /* Check for data on kernel pipe */
1035 if (revents
& LPOLLIN
) {
1036 if (pollfd
== kernel_poll_pipe
[0]) {
1037 (void) lttng_read(kernel_poll_pipe
[0],
1040 * Ret value is useless here, if this pipe gets any actions an
1041 * update is required anyway.
1043 update_poll_flag
= 1;
1047 * New CPU detected by the kernel. Adding kernel stream to
1048 * kernel session and updating the kernel consumer
1050 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1056 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1057 update_poll_flag
= 1;
1060 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1068 lttng_poll_clean(&events
);
1071 utils_close_pipe(kernel_poll_pipe
);
1072 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1075 ERR("Health error occurred in %s", __func__
);
1076 WARN("Kernel thread died unexpectedly. "
1077 "Kernel tracing can continue but CPU hotplug is disabled.");
1079 health_unregister(health_sessiond
);
1080 DBG("Kernel thread dying");
1085 * Signal pthread condition of the consumer data that the thread.
1087 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1089 pthread_mutex_lock(&data
->cond_mutex
);
1092 * The state is set before signaling. It can be any value, it's the waiter
1093 * job to correctly interpret this condition variable associated to the
1094 * consumer pthread_cond.
1096 * A value of 0 means that the corresponding thread of the consumer data
1097 * was not started. 1 indicates that the thread has started and is ready
1098 * for action. A negative value means that there was an error during the
1101 data
->consumer_thread_is_ready
= state
;
1102 (void) pthread_cond_signal(&data
->cond
);
1104 pthread_mutex_unlock(&data
->cond_mutex
);
1108 * This thread manage the consumer error sent back to the session daemon.
1110 static void *thread_manage_consumer(void *data
)
1112 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1113 uint32_t revents
, nb_fd
;
1114 enum lttcomm_return_code code
;
1115 struct lttng_poll_event events
;
1116 struct consumer_data
*consumer_data
= data
;
1117 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1119 DBG("[thread] Manage consumer started");
1121 rcu_register_thread();
1122 rcu_thread_online();
1124 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1126 health_code_update();
1129 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1130 * metadata_sock. Nothing more will be added to this poll set.
1132 ret
= sessiond_set_thread_pollset(&events
, 3);
1138 * The error socket here is already in a listening state which was done
1139 * just before spawning this thread to avoid a race between the consumer
1140 * daemon exec trying to connect and the listen() call.
1142 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1147 health_code_update();
1149 /* Infinite blocking call, waiting for transmission */
1151 health_poll_entry();
1153 if (testpoint(sessiond_thread_manage_consumer
)) {
1157 ret
= lttng_poll_wait(&events
, -1);
1161 * Restart interrupted system call.
1163 if (errno
== EINTR
) {
1171 for (i
= 0; i
< nb_fd
; i
++) {
1172 /* Fetch once the poll data */
1173 revents
= LTTNG_POLL_GETEV(&events
, i
);
1174 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1176 health_code_update();
1179 /* No activity for this FD (poll implementation). */
1183 /* Thread quit pipe has been closed. Killing thread. */
1184 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1190 /* Event on the registration socket */
1191 if (pollfd
== consumer_data
->err_sock
) {
1192 if (revents
& LPOLLIN
) {
1194 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1195 ERR("consumer err socket poll error");
1198 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1204 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1210 * Set the CLOEXEC flag. Return code is useless because either way, the
1213 (void) utils_set_fd_cloexec(sock
);
1215 health_code_update();
1217 DBG2("Receiving code from consumer err_sock");
1219 /* Getting status code from kconsumerd */
1220 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1221 sizeof(enum lttcomm_return_code
));
1226 health_code_update();
1227 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1228 ERR("consumer error when waiting for SOCK_READY : %s",
1229 lttcomm_get_readable_code(-code
));
1233 /* Connect both command and metadata sockets. */
1234 consumer_data
->cmd_sock
=
1235 lttcomm_connect_unix_sock(
1236 consumer_data
->cmd_unix_sock_path
);
1237 consumer_data
->metadata_fd
=
1238 lttcomm_connect_unix_sock(
1239 consumer_data
->cmd_unix_sock_path
);
1240 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1241 PERROR("consumer connect cmd socket");
1242 /* On error, signal condition and quit. */
1243 signal_consumer_condition(consumer_data
, -1);
1247 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1249 /* Create metadata socket lock. */
1250 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1251 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1252 PERROR("zmalloc pthread mutex");
1255 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1257 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1258 DBG("Consumer metadata socket ready (fd: %d)",
1259 consumer_data
->metadata_fd
);
1262 * Remove the consumerd error sock since we've established a connection.
1264 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1269 /* Add new accepted error socket. */
1270 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1275 /* Add metadata socket that is successfully connected. */
1276 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1277 LPOLLIN
| LPOLLRDHUP
);
1282 health_code_update();
1285 * Transfer the write-end of the channel monitoring pipe to the
1286 * by issuing a SET_CHANNEL_MONITOR_PIPE command.
1288 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1289 if (!cmd_socket_wrapper
) {
1292 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1294 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1295 consumer_data
->channel_monitor_pipe
);
1299 /* Discard the socket wrapper as it is no longer needed. */
1300 consumer_destroy_socket(cmd_socket_wrapper
);
1301 cmd_socket_wrapper
= NULL
;
1303 /* The thread is completely initialized, signal that it is ready. */
1304 signal_consumer_condition(consumer_data
, 1);
1306 /* Infinite blocking call, waiting for transmission */
1309 health_code_update();
1311 /* Exit the thread because the thread quit pipe has been triggered. */
1313 /* Not a health error. */
1318 health_poll_entry();
1319 ret
= lttng_poll_wait(&events
, -1);
1323 * Restart interrupted system call.
1325 if (errno
== EINTR
) {
1333 for (i
= 0; i
< nb_fd
; i
++) {
1334 /* Fetch once the poll data */
1335 revents
= LTTNG_POLL_GETEV(&events
, i
);
1336 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1338 health_code_update();
1341 /* No activity for this FD (poll implementation). */
1346 * Thread quit pipe has been triggered, flag that we should stop
1347 * but continue the current loop to handle potential data from
1350 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1352 if (pollfd
== sock
) {
1353 /* Event on the consumerd socket */
1354 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1355 && !(revents
& LPOLLIN
)) {
1356 ERR("consumer err socket second poll error");
1359 health_code_update();
1360 /* Wait for any kconsumerd error */
1361 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1362 sizeof(enum lttcomm_return_code
));
1364 ERR("consumer closed the command socket");
1368 ERR("consumer return code : %s",
1369 lttcomm_get_readable_code(-code
));
1372 } else if (pollfd
== consumer_data
->metadata_fd
) {
1373 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1374 && !(revents
& LPOLLIN
)) {
1375 ERR("consumer err metadata socket second poll error");
1378 /* UST metadata requests */
1379 ret
= ust_consumer_metadata_request(
1380 &consumer_data
->metadata_sock
);
1382 ERR("Handling metadata request");
1386 /* No need for an else branch all FDs are tested prior. */
1388 health_code_update();
1394 * We lock here because we are about to close the sockets and some other
1395 * thread might be using them so get exclusive access which will abort all
1396 * other consumer command by other threads.
1398 pthread_mutex_lock(&consumer_data
->lock
);
1400 /* Immediately set the consumerd state to stopped */
1401 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1402 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1403 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1404 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1405 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1407 /* Code flow error... */
1411 if (consumer_data
->err_sock
>= 0) {
1412 ret
= close(consumer_data
->err_sock
);
1416 consumer_data
->err_sock
= -1;
1418 if (consumer_data
->cmd_sock
>= 0) {
1419 ret
= close(consumer_data
->cmd_sock
);
1423 consumer_data
->cmd_sock
= -1;
1425 if (consumer_data
->metadata_sock
.fd_ptr
&&
1426 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1427 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1439 unlink(consumer_data
->err_unix_sock_path
);
1440 unlink(consumer_data
->cmd_unix_sock_path
);
1441 pthread_mutex_unlock(&consumer_data
->lock
);
1443 /* Cleanup metadata socket mutex. */
1444 if (consumer_data
->metadata_sock
.lock
) {
1445 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1446 free(consumer_data
->metadata_sock
.lock
);
1448 lttng_poll_clean(&events
);
1450 if (cmd_socket_wrapper
) {
1451 consumer_destroy_socket(cmd_socket_wrapper
);
1456 ERR("Health error occurred in %s", __func__
);
1458 health_unregister(health_sessiond
);
1459 DBG("consumer thread cleanup completed");
1461 rcu_thread_offline();
1462 rcu_unregister_thread();
1468 * This thread manage application communication.
1470 static void *thread_manage_apps(void *data
)
1472 int i
, ret
, pollfd
, err
= -1;
1474 uint32_t revents
, nb_fd
;
1475 struct lttng_poll_event events
;
1477 DBG("[thread] Manage application started");
1479 rcu_register_thread();
1480 rcu_thread_online();
1482 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1484 if (testpoint(sessiond_thread_manage_apps
)) {
1485 goto error_testpoint
;
1488 health_code_update();
1490 ret
= sessiond_set_thread_pollset(&events
, 2);
1492 goto error_poll_create
;
1495 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1500 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1504 health_code_update();
1507 DBG("Apps thread polling");
1509 /* Inifinite blocking call, waiting for transmission */
1511 health_poll_entry();
1512 ret
= lttng_poll_wait(&events
, -1);
1513 DBG("Apps thread return from poll on %d fds",
1514 LTTNG_POLL_GETNB(&events
));
1518 * Restart interrupted system call.
1520 if (errno
== EINTR
) {
1528 for (i
= 0; i
< nb_fd
; i
++) {
1529 /* Fetch once the poll data */
1530 revents
= LTTNG_POLL_GETEV(&events
, i
);
1531 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1533 health_code_update();
1536 /* No activity for this FD (poll implementation). */
1540 /* Thread quit pipe has been closed. Killing thread. */
1541 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1547 /* Inspect the apps cmd pipe */
1548 if (pollfd
== apps_cmd_pipe
[0]) {
1549 if (revents
& LPOLLIN
) {
1553 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1554 if (size_ret
< sizeof(sock
)) {
1555 PERROR("read apps cmd pipe");
1559 health_code_update();
1562 * Since this is a command socket (write then read),
1563 * we only monitor the error events of the socket.
1565 ret
= lttng_poll_add(&events
, sock
,
1566 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1571 DBG("Apps with sock %d added to poll set", sock
);
1572 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1573 ERR("Apps command pipe error");
1576 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1581 * At this point, we know that a registered application made
1582 * the event at poll_wait.
1584 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1585 /* Removing from the poll set */
1586 ret
= lttng_poll_del(&events
, pollfd
);
1591 /* Socket closed on remote end. */
1592 ust_app_unregister(pollfd
);
1594 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1599 health_code_update();
1605 lttng_poll_clean(&events
);
1608 utils_close_pipe(apps_cmd_pipe
);
1609 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1612 * We don't clean the UST app hash table here since already registered
1613 * applications can still be controlled so let them be until the session
1614 * daemon dies or the applications stop.
1619 ERR("Health error occurred in %s", __func__
);
1621 health_unregister(health_sessiond
);
1622 DBG("Application communication apps thread cleanup complete");
1623 rcu_thread_offline();
1624 rcu_unregister_thread();
1629 * Send a socket to a thread This is called from the dispatch UST registration
1630 * thread once all sockets are set for the application.
1632 * The sock value can be invalid, we don't really care, the thread will handle
1633 * it and make the necessary cleanup if so.
1635 * On success, return 0 else a negative value being the errno message of the
1638 static int send_socket_to_thread(int fd
, int sock
)
1643 * It's possible that the FD is set as invalid with -1 concurrently just
1644 * before calling this function being a shutdown state of the thread.
1651 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1652 if (ret
< sizeof(sock
)) {
1653 PERROR("write apps pipe %d", fd
);
1660 /* All good. Don't send back the write positive ret value. */
1667 * Sanitize the wait queue of the dispatch registration thread meaning removing
1668 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1669 * notify socket is never received.
1671 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1673 int ret
, nb_fd
= 0, i
;
1674 unsigned int fd_added
= 0;
1675 struct lttng_poll_event events
;
1676 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1680 lttng_poll_init(&events
);
1682 /* Just skip everything for an empty queue. */
1683 if (!wait_queue
->count
) {
1687 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1692 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1693 &wait_queue
->head
, head
) {
1694 assert(wait_node
->app
);
1695 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1696 LPOLLHUP
| LPOLLERR
);
1709 * Poll but don't block so we can quickly identify the faulty events and
1710 * clean them afterwards from the wait queue.
1712 ret
= lttng_poll_wait(&events
, 0);
1718 for (i
= 0; i
< nb_fd
; i
++) {
1719 /* Get faulty FD. */
1720 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1721 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1724 /* No activity for this FD (poll implementation). */
1728 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1729 &wait_queue
->head
, head
) {
1730 if (pollfd
== wait_node
->app
->sock
&&
1731 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1732 cds_list_del(&wait_node
->head
);
1733 wait_queue
->count
--;
1734 ust_app_destroy(wait_node
->app
);
1737 * Silence warning of use-after-free in
1738 * cds_list_for_each_entry_safe which uses
1739 * __typeof__(*wait_node).
1744 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1751 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1755 lttng_poll_clean(&events
);
1759 lttng_poll_clean(&events
);
1761 ERR("Unable to sanitize wait queue");
1766 * Dispatch request from the registration threads to the application
1767 * communication thread.
1769 static void *thread_dispatch_ust_registration(void *data
)
1772 struct cds_wfcq_node
*node
;
1773 struct ust_command
*ust_cmd
= NULL
;
1774 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1775 struct ust_reg_wait_queue wait_queue
= {
1779 rcu_register_thread();
1781 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1783 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1784 goto error_testpoint
;
1787 health_code_update();
1789 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1791 DBG("[thread] Dispatch UST command started");
1794 health_code_update();
1796 /* Atomically prepare the queue futex */
1797 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1799 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1804 struct ust_app
*app
= NULL
;
1808 * Make sure we don't have node(s) that have hung up before receiving
1809 * the notify socket. This is to clean the list in order to avoid
1810 * memory leaks from notify socket that are never seen.
1812 sanitize_wait_queue(&wait_queue
);
1814 health_code_update();
1815 /* Dequeue command for registration */
1816 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1818 DBG("Woken up but nothing in the UST command queue");
1819 /* Continue thread execution */
1823 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1825 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1826 " gid:%d sock:%d name:%s (version %d.%d)",
1827 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1828 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1829 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1830 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1832 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1833 wait_node
= zmalloc(sizeof(*wait_node
));
1835 PERROR("zmalloc wait_node dispatch");
1836 ret
= close(ust_cmd
->sock
);
1838 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1840 lttng_fd_put(LTTNG_FD_APPS
, 1);
1844 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1846 /* Create application object if socket is CMD. */
1847 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1849 if (!wait_node
->app
) {
1850 ret
= close(ust_cmd
->sock
);
1852 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1854 lttng_fd_put(LTTNG_FD_APPS
, 1);
1860 * Add application to the wait queue so we can set the notify
1861 * socket before putting this object in the global ht.
1863 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1868 * We have to continue here since we don't have the notify
1869 * socket and the application MUST be added to the hash table
1870 * only at that moment.
1875 * Look for the application in the local wait queue and set the
1876 * notify socket if found.
1878 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1879 &wait_queue
.head
, head
) {
1880 health_code_update();
1881 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1882 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1883 cds_list_del(&wait_node
->head
);
1885 app
= wait_node
->app
;
1887 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1893 * With no application at this stage the received socket is
1894 * basically useless so close it before we free the cmd data
1895 * structure for good.
1898 ret
= close(ust_cmd
->sock
);
1900 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1902 lttng_fd_put(LTTNG_FD_APPS
, 1);
1909 * @session_lock_list
1911 * Lock the global session list so from the register up to the
1912 * registration done message, no thread can see the application
1913 * and change its state.
1915 session_lock_list();
1919 * Add application to the global hash table. This needs to be
1920 * done before the update to the UST registry can locate the
1925 /* Set app version. This call will print an error if needed. */
1926 (void) ust_app_version(app
);
1928 /* Send notify socket through the notify pipe. */
1929 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1933 session_unlock_list();
1935 * No notify thread, stop the UST tracing. However, this is
1936 * not an internal error of the this thread thus setting
1937 * the health error code to a normal exit.
1944 * Update newly registered application with the tracing
1945 * registry info already enabled information.
1947 update_ust_app(app
->sock
);
1950 * Don't care about return value. Let the manage apps threads
1951 * handle app unregistration upon socket close.
1953 (void) ust_app_register_done(app
);
1956 * Even if the application socket has been closed, send the app
1957 * to the thread and unregistration will take place at that
1960 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1963 session_unlock_list();
1965 * No apps. thread, stop the UST tracing. However, this is
1966 * not an internal error of the this thread thus setting
1967 * the health error code to a normal exit.
1974 session_unlock_list();
1976 } while (node
!= NULL
);
1978 health_poll_entry();
1979 /* Futex wait on queue. Blocking call on futex() */
1980 futex_nto1_wait(&ust_cmd_queue
.futex
);
1983 /* Normal exit, no error */
1987 /* Clean up wait queue. */
1988 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1989 &wait_queue
.head
, head
) {
1990 cds_list_del(&wait_node
->head
);
1995 /* Empty command queue. */
1997 /* Dequeue command for registration */
1998 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2002 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2003 ret
= close(ust_cmd
->sock
);
2005 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2007 lttng_fd_put(LTTNG_FD_APPS
, 1);
2012 DBG("Dispatch thread dying");
2015 ERR("Health error occurred in %s", __func__
);
2017 health_unregister(health_sessiond
);
2018 rcu_unregister_thread();
2023 * This thread manage application registration.
2025 static void *thread_registration_apps(void *data
)
2027 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2028 uint32_t revents
, nb_fd
;
2029 struct lttng_poll_event events
;
2031 * Get allocated in this thread, enqueued to a global queue, dequeued and
2032 * freed in the manage apps thread.
2034 struct ust_command
*ust_cmd
= NULL
;
2036 DBG("[thread] Manage application registration started");
2038 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2040 if (testpoint(sessiond_thread_registration_apps
)) {
2041 goto error_testpoint
;
2044 ret
= lttcomm_listen_unix_sock(apps_sock
);
2050 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2051 * more will be added to this poll set.
2053 ret
= sessiond_set_thread_pollset(&events
, 2);
2055 goto error_create_poll
;
2058 /* Add the application registration socket */
2059 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2061 goto error_poll_add
;
2064 /* Notify all applications to register */
2065 ret
= notify_ust_apps(1);
2067 ERR("Failed to notify applications or create the wait shared memory.\n"
2068 "Execution continues but there might be problem for already\n"
2069 "running applications that wishes to register.");
2073 DBG("Accepting application registration");
2075 /* Inifinite blocking call, waiting for transmission */
2077 health_poll_entry();
2078 ret
= lttng_poll_wait(&events
, -1);
2082 * Restart interrupted system call.
2084 if (errno
== EINTR
) {
2092 for (i
= 0; i
< nb_fd
; i
++) {
2093 health_code_update();
2095 /* Fetch once the poll data */
2096 revents
= LTTNG_POLL_GETEV(&events
, i
);
2097 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2100 /* No activity for this FD (poll implementation). */
2104 /* Thread quit pipe has been closed. Killing thread. */
2105 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2111 /* Event on the registration socket */
2112 if (pollfd
== apps_sock
) {
2113 if (revents
& LPOLLIN
) {
2114 sock
= lttcomm_accept_unix_sock(apps_sock
);
2120 * Set socket timeout for both receiving and ending.
2121 * app_socket_timeout is in seconds, whereas
2122 * lttcomm_setsockopt_rcv_timeout and
2123 * lttcomm_setsockopt_snd_timeout expect msec as
2126 if (config
.app_socket_timeout
>= 0) {
2127 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2128 config
.app_socket_timeout
* 1000);
2129 (void) lttcomm_setsockopt_snd_timeout(sock
,
2130 config
.app_socket_timeout
* 1000);
2134 * Set the CLOEXEC flag. Return code is useless because
2135 * either way, the show must go on.
2137 (void) utils_set_fd_cloexec(sock
);
2139 /* Create UST registration command for enqueuing */
2140 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2141 if (ust_cmd
== NULL
) {
2142 PERROR("ust command zmalloc");
2151 * Using message-based transmissions to ensure we don't
2152 * have to deal with partially received messages.
2154 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2156 ERR("Exhausted file descriptors allowed for applications.");
2166 health_code_update();
2167 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2170 /* Close socket of the application. */
2175 lttng_fd_put(LTTNG_FD_APPS
, 1);
2179 health_code_update();
2181 ust_cmd
->sock
= sock
;
2184 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2185 " gid:%d sock:%d name:%s (version %d.%d)",
2186 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2187 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2188 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2189 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2192 * Lock free enqueue the registration request. The red pill
2193 * has been taken! This apps will be part of the *system*.
2195 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2198 * Wake the registration queue futex. Implicit memory
2199 * barrier with the exchange in cds_wfcq_enqueue.
2201 futex_nto1_wake(&ust_cmd_queue
.futex
);
2202 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2203 ERR("Register apps socket poll error");
2206 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2215 /* Notify that the registration thread is gone */
2218 if (apps_sock
>= 0) {
2219 ret
= close(apps_sock
);
2229 lttng_fd_put(LTTNG_FD_APPS
, 1);
2231 unlink(config
.apps_unix_sock_path
.value
);
2234 lttng_poll_clean(&events
);
2238 DBG("UST Registration thread cleanup complete");
2241 ERR("Health error occurred in %s", __func__
);
2243 health_unregister(health_sessiond
);
2249 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2250 * exec or it will fails.
2252 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2255 struct timespec timeout
;
2258 * Make sure we set the readiness flag to 0 because we are NOT ready.
2259 * This access to consumer_thread_is_ready does not need to be
2260 * protected by consumer_data.cond_mutex (yet) since the consumer
2261 * management thread has not been started at this point.
2263 consumer_data
->consumer_thread_is_ready
= 0;
2265 /* Setup pthread condition */
2266 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2269 PERROR("pthread_condattr_init consumer data");
2274 * Set the monotonic clock in order to make sure we DO NOT jump in time
2275 * between the clock_gettime() call and the timedwait call. See bug #324
2276 * for a more details and how we noticed it.
2278 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2281 PERROR("pthread_condattr_setclock consumer data");
2285 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2288 PERROR("pthread_cond_init consumer data");
2292 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2293 thread_manage_consumer
, consumer_data
);
2296 PERROR("pthread_create consumer");
2301 /* We are about to wait on a pthread condition */
2302 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2304 /* Get time for sem_timedwait absolute timeout */
2305 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2307 * Set the timeout for the condition timed wait even if the clock gettime
2308 * call fails since we might loop on that call and we want to avoid to
2309 * increment the timeout too many times.
2311 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2314 * The following loop COULD be skipped in some conditions so this is why we
2315 * set ret to 0 in order to make sure at least one round of the loop is
2321 * Loop until the condition is reached or when a timeout is reached. Note
2322 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2323 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2324 * possible. This loop does not take any chances and works with both of
2327 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2328 if (clock_ret
< 0) {
2329 PERROR("clock_gettime spawn consumer");
2330 /* Infinite wait for the consumerd thread to be ready */
2331 ret
= pthread_cond_wait(&consumer_data
->cond
,
2332 &consumer_data
->cond_mutex
);
2334 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2335 &consumer_data
->cond_mutex
, &timeout
);
2339 /* Release the pthread condition */
2340 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2344 if (ret
== ETIMEDOUT
) {
2348 * Call has timed out so we kill the kconsumerd_thread and return
2351 ERR("Condition timed out. The consumer thread was never ready."
2353 pth_ret
= pthread_cancel(consumer_data
->thread
);
2355 PERROR("pthread_cancel consumer thread");
2358 PERROR("pthread_cond_wait failed consumer thread");
2360 /* Caller is expecting a negative value on failure. */
2365 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2366 if (consumer_data
->pid
== 0) {
2367 ERR("Consumerd did not start");
2368 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2371 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2380 * Join consumer thread
2382 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2386 /* Consumer pid must be a real one. */
2387 if (consumer_data
->pid
> 0) {
2389 ret
= kill(consumer_data
->pid
, SIGTERM
);
2391 PERROR("Error killing consumer daemon");
2394 return pthread_join(consumer_data
->thread
, &status
);
2401 * Fork and exec a consumer daemon (consumerd).
2403 * Return pid if successful else -1.
2405 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2409 const char *consumer_to_use
;
2410 const char *verbosity
;
2413 DBG("Spawning consumerd");
2420 if (config
.verbose_consumer
) {
2421 verbosity
= "--verbose";
2422 } else if (lttng_opt_quiet
) {
2423 verbosity
= "--quiet";
2428 switch (consumer_data
->type
) {
2429 case LTTNG_CONSUMER_KERNEL
:
2431 * Find out which consumerd to execute. We will first try the
2432 * 64-bit path, then the sessiond's installation directory, and
2433 * fallback on the 32-bit one,
2435 DBG3("Looking for a kernel consumer at these locations:");
2436 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2437 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2438 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2439 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2440 DBG3("Found location #1");
2441 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2442 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2443 DBG3("Found location #2");
2444 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2445 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2446 DBG3("Found location #3");
2447 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2449 DBG("Could not find any valid consumerd executable");
2453 DBG("Using kernel consumer at: %s", consumer_to_use
);
2454 (void) execl(consumer_to_use
,
2455 "lttng-consumerd", verbosity
, "-k",
2456 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2457 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2458 "--group", config
.tracing_group_name
.value
,
2461 case LTTNG_CONSUMER64_UST
:
2463 char *tmpnew
= NULL
;
2465 if (config
.consumerd64_lib_dir
.value
) {
2469 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2473 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2474 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2479 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2480 if (tmp
[0] != '\0') {
2481 strcat(tmpnew
, ":");
2482 strcat(tmpnew
, tmp
);
2484 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2491 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2492 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2493 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2494 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2495 "--group", config
.tracing_group_name
.value
,
2500 case LTTNG_CONSUMER32_UST
:
2502 char *tmpnew
= NULL
;
2504 if (config
.consumerd32_lib_dir
.value
) {
2508 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2512 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2513 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2518 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2519 if (tmp
[0] != '\0') {
2520 strcat(tmpnew
, ":");
2521 strcat(tmpnew
, tmp
);
2523 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2530 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2531 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2532 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2533 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2534 "--group", config
.tracing_group_name
.value
,
2540 ERR("unknown consumer type");
2544 PERROR("Consumer execl()");
2546 /* Reaching this point, we got a failure on our execl(). */
2548 } else if (pid
> 0) {
2551 PERROR("start consumer fork");
2559 * Spawn the consumerd daemon and session daemon thread.
2561 static int start_consumerd(struct consumer_data
*consumer_data
)
2566 * Set the listen() state on the socket since there is a possible race
2567 * between the exec() of the consumer daemon and this call if place in the
2568 * consumer thread. See bug #366 for more details.
2570 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2575 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2576 if (consumer_data
->pid
!= 0) {
2577 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2581 ret
= spawn_consumerd(consumer_data
);
2583 ERR("Spawning consumerd failed");
2584 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2588 /* Setting up the consumer_data pid */
2589 consumer_data
->pid
= ret
;
2590 DBG2("Consumer pid %d", consumer_data
->pid
);
2591 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2593 DBG2("Spawning consumer control thread");
2594 ret
= spawn_consumer_thread(consumer_data
);
2596 ERR("Fatal error spawning consumer control thread");
2604 /* Cleanup already created sockets on error. */
2605 if (consumer_data
->err_sock
>= 0) {
2608 err
= close(consumer_data
->err_sock
);
2610 PERROR("close consumer data error socket");
2617 * Setup necessary data for kernel tracer action.
2619 static int init_kernel_tracer(void)
2623 /* Modprobe lttng kernel modules */
2624 ret
= modprobe_lttng_control();
2629 /* Open debugfs lttng */
2630 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2631 if (kernel_tracer_fd
< 0) {
2632 DBG("Failed to open %s", module_proc_lttng
);
2636 /* Validate kernel version */
2637 ret
= kernel_validate_version(kernel_tracer_fd
);
2642 ret
= modprobe_lttng_data();
2647 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2654 WARN("Kernel tracer does not support buffer monitoring. "
2655 "The monitoring timer of channels in the kernel domain "
2656 "will be set to 0 (disabled).");
2659 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2663 modprobe_remove_lttng_control();
2664 ret
= close(kernel_tracer_fd
);
2668 kernel_tracer_fd
= -1;
2669 return LTTNG_ERR_KERN_VERSION
;
2672 ret
= close(kernel_tracer_fd
);
2678 modprobe_remove_lttng_control();
2681 WARN("No kernel tracer available");
2682 kernel_tracer_fd
= -1;
2684 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2686 return LTTNG_ERR_KERN_NA
;
2692 * Copy consumer output from the tracing session to the domain session. The
2693 * function also applies the right modification on a per domain basis for the
2694 * trace files destination directory.
2696 * Should *NOT* be called with RCU read-side lock held.
2698 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2701 const char *dir_name
;
2702 struct consumer_output
*consumer
;
2705 assert(session
->consumer
);
2708 case LTTNG_DOMAIN_KERNEL
:
2709 DBG3("Copying tracing session consumer output in kernel session");
2711 * XXX: We should audit the session creation and what this function
2712 * does "extra" in order to avoid a destroy since this function is used
2713 * in the domain session creation (kernel and ust) only. Same for UST
2716 if (session
->kernel_session
->consumer
) {
2717 consumer_output_put(session
->kernel_session
->consumer
);
2719 session
->kernel_session
->consumer
=
2720 consumer_copy_output(session
->consumer
);
2721 /* Ease our life a bit for the next part */
2722 consumer
= session
->kernel_session
->consumer
;
2723 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2725 case LTTNG_DOMAIN_JUL
:
2726 case LTTNG_DOMAIN_LOG4J
:
2727 case LTTNG_DOMAIN_PYTHON
:
2728 case LTTNG_DOMAIN_UST
:
2729 DBG3("Copying tracing session consumer output in UST session");
2730 if (session
->ust_session
->consumer
) {
2731 consumer_output_put(session
->ust_session
->consumer
);
2733 session
->ust_session
->consumer
=
2734 consumer_copy_output(session
->consumer
);
2735 /* Ease our life a bit for the next part */
2736 consumer
= session
->ust_session
->consumer
;
2737 dir_name
= DEFAULT_UST_TRACE_DIR
;
2740 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2744 /* Append correct directory to subdir */
2745 strncat(consumer
->subdir
, dir_name
,
2746 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2747 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2756 * Create an UST session and add it to the session ust list.
2758 * Should *NOT* be called with RCU read-side lock held.
2760 static int create_ust_session(struct ltt_session
*session
,
2761 struct lttng_domain
*domain
)
2764 struct ltt_ust_session
*lus
= NULL
;
2768 assert(session
->consumer
);
2770 switch (domain
->type
) {
2771 case LTTNG_DOMAIN_JUL
:
2772 case LTTNG_DOMAIN_LOG4J
:
2773 case LTTNG_DOMAIN_PYTHON
:
2774 case LTTNG_DOMAIN_UST
:
2777 ERR("Unknown UST domain on create session %d", domain
->type
);
2778 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2782 DBG("Creating UST session");
2784 lus
= trace_ust_create_session(session
->id
);
2786 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2790 lus
->uid
= session
->uid
;
2791 lus
->gid
= session
->gid
;
2792 lus
->output_traces
= session
->output_traces
;
2793 lus
->snapshot_mode
= session
->snapshot_mode
;
2794 lus
->live_timer_interval
= session
->live_timer
;
2795 session
->ust_session
= lus
;
2796 if (session
->shm_path
[0]) {
2797 strncpy(lus
->root_shm_path
, session
->shm_path
,
2798 sizeof(lus
->root_shm_path
));
2799 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2800 strncpy(lus
->shm_path
, session
->shm_path
,
2801 sizeof(lus
->shm_path
));
2802 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2803 strncat(lus
->shm_path
, "/ust",
2804 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2806 /* Copy session output to the newly created UST session */
2807 ret
= copy_session_consumer(domain
->type
, session
);
2808 if (ret
!= LTTNG_OK
) {
2816 session
->ust_session
= NULL
;
2821 * Create a kernel tracer session then create the default channel.
2823 static int create_kernel_session(struct ltt_session
*session
)
2827 DBG("Creating kernel session");
2829 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2831 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2835 /* Code flow safety */
2836 assert(session
->kernel_session
);
2838 /* Copy session output to the newly created Kernel session */
2839 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2840 if (ret
!= LTTNG_OK
) {
2844 /* Create directory(ies) on local filesystem. */
2845 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2846 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2847 ret
= run_as_mkdir_recursive(
2848 session
->kernel_session
->consumer
->dst
.trace_path
,
2849 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2851 if (errno
!= EEXIST
) {
2852 ERR("Trace directory creation error");
2858 session
->kernel_session
->uid
= session
->uid
;
2859 session
->kernel_session
->gid
= session
->gid
;
2860 session
->kernel_session
->output_traces
= session
->output_traces
;
2861 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2866 trace_kernel_destroy_session(session
->kernel_session
);
2867 session
->kernel_session
= NULL
;
2872 * Count number of session permitted by uid/gid.
2874 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2877 struct ltt_session
*session
;
2879 DBG("Counting number of available session for UID %d GID %d",
2881 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2883 * Only list the sessions the user can control.
2885 if (!session_access_ok(session
, uid
, gid
)) {
2894 * Process the command requested by the lttng client within the command
2895 * context structure. This function make sure that the return structure (llm)
2896 * is set and ready for transmission before returning.
2898 * Return any error encountered or 0 for success.
2900 * "sock" is only used for special-case var. len data.
2902 * Should *NOT* be called with RCU read-side lock held.
2904 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2908 int need_tracing_session
= 1;
2911 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2913 assert(!rcu_read_ongoing());
2917 switch (cmd_ctx
->lsm
->cmd_type
) {
2918 case LTTNG_CREATE_SESSION
:
2919 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2920 case LTTNG_CREATE_SESSION_LIVE
:
2921 case LTTNG_DESTROY_SESSION
:
2922 case LTTNG_LIST_SESSIONS
:
2923 case LTTNG_LIST_DOMAINS
:
2924 case LTTNG_START_TRACE
:
2925 case LTTNG_STOP_TRACE
:
2926 case LTTNG_DATA_PENDING
:
2927 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2928 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2929 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2930 case LTTNG_SNAPSHOT_RECORD
:
2931 case LTTNG_SAVE_SESSION
:
2932 case LTTNG_SET_SESSION_SHM_PATH
:
2933 case LTTNG_REGENERATE_METADATA
:
2934 case LTTNG_REGENERATE_STATEDUMP
:
2935 case LTTNG_REGISTER_TRIGGER
:
2936 case LTTNG_UNREGISTER_TRIGGER
:
2943 if (config
.no_kernel
&& need_domain
2944 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2946 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2948 ret
= LTTNG_ERR_KERN_NA
;
2953 /* Deny register consumer if we already have a spawned consumer. */
2954 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2955 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2956 if (kconsumer_data
.pid
> 0) {
2957 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2958 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2961 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2965 * Check for command that don't needs to allocate a returned payload. We do
2966 * this here so we don't have to make the call for no payload at each
2969 switch(cmd_ctx
->lsm
->cmd_type
) {
2970 case LTTNG_LIST_SESSIONS
:
2971 case LTTNG_LIST_TRACEPOINTS
:
2972 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2973 case LTTNG_LIST_DOMAINS
:
2974 case LTTNG_LIST_CHANNELS
:
2975 case LTTNG_LIST_EVENTS
:
2976 case LTTNG_LIST_SYSCALLS
:
2977 case LTTNG_LIST_TRACKER_PIDS
:
2978 case LTTNG_DATA_PENDING
:
2981 /* Setup lttng message with no payload */
2982 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2984 /* This label does not try to unlock the session */
2985 goto init_setup_error
;
2989 /* Commands that DO NOT need a session. */
2990 switch (cmd_ctx
->lsm
->cmd_type
) {
2991 case LTTNG_CREATE_SESSION
:
2992 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2993 case LTTNG_CREATE_SESSION_LIVE
:
2994 case LTTNG_LIST_SESSIONS
:
2995 case LTTNG_LIST_TRACEPOINTS
:
2996 case LTTNG_LIST_SYSCALLS
:
2997 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2998 case LTTNG_SAVE_SESSION
:
2999 case LTTNG_REGISTER_TRIGGER
:
3000 case LTTNG_UNREGISTER_TRIGGER
:
3001 need_tracing_session
= 0;
3004 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3006 * We keep the session list lock across _all_ commands
3007 * for now, because the per-session lock does not
3008 * handle teardown properly.
3010 session_lock_list();
3011 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3012 if (cmd_ctx
->session
== NULL
) {
3013 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3016 /* Acquire lock for the session */
3017 session_lock(cmd_ctx
->session
);
3023 * Commands that need a valid session but should NOT create one if none
3024 * exists. Instead of creating one and destroying it when the command is
3025 * handled, process that right before so we save some round trip in useless
3028 switch (cmd_ctx
->lsm
->cmd_type
) {
3029 case LTTNG_DISABLE_CHANNEL
:
3030 case LTTNG_DISABLE_EVENT
:
3031 switch (cmd_ctx
->lsm
->domain
.type
) {
3032 case LTTNG_DOMAIN_KERNEL
:
3033 if (!cmd_ctx
->session
->kernel_session
) {
3034 ret
= LTTNG_ERR_NO_CHANNEL
;
3038 case LTTNG_DOMAIN_JUL
:
3039 case LTTNG_DOMAIN_LOG4J
:
3040 case LTTNG_DOMAIN_PYTHON
:
3041 case LTTNG_DOMAIN_UST
:
3042 if (!cmd_ctx
->session
->ust_session
) {
3043 ret
= LTTNG_ERR_NO_CHANNEL
;
3048 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3060 * Check domain type for specific "pre-action".
3062 switch (cmd_ctx
->lsm
->domain
.type
) {
3063 case LTTNG_DOMAIN_KERNEL
:
3065 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3069 /* Kernel tracer check */
3070 if (kernel_tracer_fd
== -1) {
3071 /* Basically, load kernel tracer modules */
3072 ret
= init_kernel_tracer();
3078 /* Consumer is in an ERROR state. Report back to client */
3079 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3080 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3084 /* Need a session for kernel command */
3085 if (need_tracing_session
) {
3086 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3087 ret
= create_kernel_session(cmd_ctx
->session
);
3089 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3094 /* Start the kernel consumer daemon */
3095 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3096 if (kconsumer_data
.pid
== 0 &&
3097 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3098 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3099 ret
= start_consumerd(&kconsumer_data
);
3101 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3104 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3106 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3110 * The consumer was just spawned so we need to add the socket to
3111 * the consumer output of the session if exist.
3113 ret
= consumer_create_socket(&kconsumer_data
,
3114 cmd_ctx
->session
->kernel_session
->consumer
);
3121 case LTTNG_DOMAIN_JUL
:
3122 case LTTNG_DOMAIN_LOG4J
:
3123 case LTTNG_DOMAIN_PYTHON
:
3124 case LTTNG_DOMAIN_UST
:
3126 if (!ust_app_supported()) {
3127 ret
= LTTNG_ERR_NO_UST
;
3130 /* Consumer is in an ERROR state. Report back to client */
3131 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3132 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3136 if (need_tracing_session
) {
3137 /* Create UST session if none exist. */
3138 if (cmd_ctx
->session
->ust_session
== NULL
) {
3139 ret
= create_ust_session(cmd_ctx
->session
,
3140 &cmd_ctx
->lsm
->domain
);
3141 if (ret
!= LTTNG_OK
) {
3146 /* Start the UST consumer daemons */
3148 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3149 if (config
.consumerd64_bin_path
.value
&&
3150 ustconsumer64_data
.pid
== 0 &&
3151 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3152 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3153 ret
= start_consumerd(&ustconsumer64_data
);
3155 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3156 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3160 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3161 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3163 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3167 * Setup socket for consumer 64 bit. No need for atomic access
3168 * since it was set above and can ONLY be set in this thread.
3170 ret
= consumer_create_socket(&ustconsumer64_data
,
3171 cmd_ctx
->session
->ust_session
->consumer
);
3177 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3178 if (config
.consumerd32_bin_path
.value
&&
3179 ustconsumer32_data
.pid
== 0 &&
3180 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3181 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3182 ret
= start_consumerd(&ustconsumer32_data
);
3184 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3185 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3189 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3190 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3192 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3196 * Setup socket for consumer 64 bit. No need for atomic access
3197 * since it was set above and can ONLY be set in this thread.
3199 ret
= consumer_create_socket(&ustconsumer32_data
,
3200 cmd_ctx
->session
->ust_session
->consumer
);
3212 /* Validate consumer daemon state when start/stop trace command */
3213 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3214 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3215 switch (cmd_ctx
->lsm
->domain
.type
) {
3216 case LTTNG_DOMAIN_NONE
:
3218 case LTTNG_DOMAIN_JUL
:
3219 case LTTNG_DOMAIN_LOG4J
:
3220 case LTTNG_DOMAIN_PYTHON
:
3221 case LTTNG_DOMAIN_UST
:
3222 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3223 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3227 case LTTNG_DOMAIN_KERNEL
:
3228 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3229 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3234 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3240 * Check that the UID or GID match that of the tracing session.
3241 * The root user can interact with all sessions.
3243 if (need_tracing_session
) {
3244 if (!session_access_ok(cmd_ctx
->session
,
3245 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3246 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3247 ret
= LTTNG_ERR_EPERM
;
3253 * Send relayd information to consumer as soon as we have a domain and a
3256 if (cmd_ctx
->session
&& need_domain
) {
3258 * Setup relayd if not done yet. If the relayd information was already
3259 * sent to the consumer, this call will gracefully return.
3261 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3262 if (ret
!= LTTNG_OK
) {
3267 /* Process by command type */
3268 switch (cmd_ctx
->lsm
->cmd_type
) {
3269 case LTTNG_ADD_CONTEXT
:
3272 * An LTTNG_ADD_CONTEXT command might have a supplementary
3273 * payload if the context being added is an application context.
3275 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3276 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3277 char *provider_name
= NULL
, *context_name
= NULL
;
3278 size_t provider_name_len
=
3279 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3280 size_t context_name_len
=
3281 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3283 if (provider_name_len
== 0 || context_name_len
== 0) {
3285 * Application provider and context names MUST
3288 ret
= -LTTNG_ERR_INVALID
;
3292 provider_name
= zmalloc(provider_name_len
+ 1);
3293 if (!provider_name
) {
3294 ret
= -LTTNG_ERR_NOMEM
;
3297 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3300 context_name
= zmalloc(context_name_len
+ 1);
3301 if (!context_name
) {
3302 ret
= -LTTNG_ERR_NOMEM
;
3303 goto error_add_context
;
3305 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3308 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3311 goto error_add_context
;
3314 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3317 goto error_add_context
;
3322 * cmd_add_context assumes ownership of the provider and context
3325 ret
= cmd_add_context(cmd_ctx
->session
,
3326 cmd_ctx
->lsm
->domain
.type
,
3327 cmd_ctx
->lsm
->u
.context
.channel_name
,
3328 &cmd_ctx
->lsm
->u
.context
.ctx
,
3329 kernel_poll_pipe
[1]);
3331 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3332 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3334 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3335 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3341 case LTTNG_DISABLE_CHANNEL
:
3343 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3344 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3347 case LTTNG_DISABLE_EVENT
:
3351 * FIXME: handle filter; for now we just receive the filter's
3352 * bytecode along with the filter expression which are sent by
3353 * liblttng-ctl and discard them.
3355 * This fixes an issue where the client may block while sending
3356 * the filter payload and encounter an error because the session
3357 * daemon closes the socket without ever handling this data.
3359 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3360 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3363 char data
[LTTNG_FILTER_MAX_LEN
];
3365 DBG("Discarding disable event command payload of size %zu", count
);
3367 ret
= lttcomm_recv_unix_sock(sock
, data
,
3368 count
> sizeof(data
) ? sizeof(data
) : count
);
3373 count
-= (size_t) ret
;
3376 /* FIXME: passing packed structure to non-packed pointer */
3377 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3378 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3379 &cmd_ctx
->lsm
->u
.disable
.event
);
3382 case LTTNG_ENABLE_CHANNEL
:
3384 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3385 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3386 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3387 &cmd_ctx
->lsm
->u
.channel
.chan
,
3388 kernel_poll_pipe
[1]);
3391 case LTTNG_TRACK_PID
:
3393 ret
= cmd_track_pid(cmd_ctx
->session
,
3394 cmd_ctx
->lsm
->domain
.type
,
3395 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3398 case LTTNG_UNTRACK_PID
:
3400 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3401 cmd_ctx
->lsm
->domain
.type
,
3402 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3405 case LTTNG_ENABLE_EVENT
:
3407 struct lttng_event_exclusion
*exclusion
= NULL
;
3408 struct lttng_filter_bytecode
*bytecode
= NULL
;
3409 char *filter_expression
= NULL
;
3411 /* Handle exclusion events and receive it from the client. */
3412 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3413 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3415 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3416 (count
* LTTNG_SYMBOL_NAME_LEN
));
3418 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3422 DBG("Receiving var len exclusion event list from client ...");
3423 exclusion
->count
= count
;
3424 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3425 count
* LTTNG_SYMBOL_NAME_LEN
);
3427 DBG("Nothing recv() from client var len data... continuing");
3430 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3435 /* Get filter expression from client. */
3436 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3437 size_t expression_len
=
3438 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3440 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3441 ret
= LTTNG_ERR_FILTER_INVAL
;
3446 filter_expression
= zmalloc(expression_len
);
3447 if (!filter_expression
) {
3449 ret
= LTTNG_ERR_FILTER_NOMEM
;
3453 /* Receive var. len. data */
3454 DBG("Receiving var len filter's expression from client ...");
3455 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3458 DBG("Nothing recv() from client car len data... continuing");
3460 free(filter_expression
);
3462 ret
= LTTNG_ERR_FILTER_INVAL
;
3467 /* Handle filter and get bytecode from client. */
3468 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3469 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3471 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3472 ret
= LTTNG_ERR_FILTER_INVAL
;
3473 free(filter_expression
);
3478 bytecode
= zmalloc(bytecode_len
);
3480 free(filter_expression
);
3482 ret
= LTTNG_ERR_FILTER_NOMEM
;
3486 /* Receive var. len. data */
3487 DBG("Receiving var len filter's bytecode from client ...");
3488 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3490 DBG("Nothing recv() from client car len data... continuing");
3492 free(filter_expression
);
3495 ret
= LTTNG_ERR_FILTER_INVAL
;
3499 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3500 free(filter_expression
);
3503 ret
= LTTNG_ERR_FILTER_INVAL
;
3508 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3509 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3510 &cmd_ctx
->lsm
->u
.enable
.event
,
3511 filter_expression
, bytecode
, exclusion
,
3512 kernel_poll_pipe
[1]);
3515 case LTTNG_LIST_TRACEPOINTS
:
3517 struct lttng_event
*events
;
3520 session_lock_list();
3521 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3522 session_unlock_list();
3523 if (nb_events
< 0) {
3524 /* Return value is a negative lttng_error_code. */
3530 * Setup lttng message with payload size set to the event list size in
3531 * bytes and then copy list into the llm payload.
3533 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3534 sizeof(struct lttng_event
) * nb_events
);
3544 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3546 struct lttng_event_field
*fields
;
3549 session_lock_list();
3550 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3552 session_unlock_list();
3553 if (nb_fields
< 0) {
3554 /* Return value is a negative lttng_error_code. */
3560 * Setup lttng message with payload size set to the event list size in
3561 * bytes and then copy list into the llm payload.
3563 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3564 sizeof(struct lttng_event_field
) * nb_fields
);
3574 case LTTNG_LIST_SYSCALLS
:
3576 struct lttng_event
*events
;
3579 nb_events
= cmd_list_syscalls(&events
);
3580 if (nb_events
< 0) {
3581 /* Return value is a negative lttng_error_code. */
3587 * Setup lttng message with payload size set to the event list size in
3588 * bytes and then copy list into the llm payload.
3590 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3591 sizeof(struct lttng_event
) * nb_events
);
3601 case LTTNG_LIST_TRACKER_PIDS
:
3603 int32_t *pids
= NULL
;
3606 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3607 cmd_ctx
->lsm
->domain
.type
, &pids
);
3609 /* Return value is a negative lttng_error_code. */
3615 * Setup lttng message with payload size set to the event list size in
3616 * bytes and then copy list into the llm payload.
3618 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3619 sizeof(int32_t) * nr_pids
);
3629 case LTTNG_SET_CONSUMER_URI
:
3632 struct lttng_uri
*uris
;
3634 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3635 len
= nb_uri
* sizeof(struct lttng_uri
);
3638 ret
= LTTNG_ERR_INVALID
;
3642 uris
= zmalloc(len
);
3644 ret
= LTTNG_ERR_FATAL
;
3648 /* Receive variable len data */
3649 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3650 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3652 DBG("No URIs received from client... continuing");
3654 ret
= LTTNG_ERR_SESSION_FAIL
;
3659 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3661 if (ret
!= LTTNG_OK
) {
3668 case LTTNG_START_TRACE
:
3670 ret
= cmd_start_trace(cmd_ctx
->session
);
3673 case LTTNG_STOP_TRACE
:
3675 ret
= cmd_stop_trace(cmd_ctx
->session
);
3678 case LTTNG_CREATE_SESSION
:
3681 struct lttng_uri
*uris
= NULL
;
3683 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3684 len
= nb_uri
* sizeof(struct lttng_uri
);
3687 uris
= zmalloc(len
);
3689 ret
= LTTNG_ERR_FATAL
;
3693 /* Receive variable len data */
3694 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3695 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3697 DBG("No URIs received from client... continuing");
3699 ret
= LTTNG_ERR_SESSION_FAIL
;
3704 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3705 DBG("Creating session with ONE network URI is a bad call");
3706 ret
= LTTNG_ERR_SESSION_FAIL
;
3712 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3713 &cmd_ctx
->creds
, 0);
3719 case LTTNG_DESTROY_SESSION
:
3721 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3723 /* Set session to NULL so we do not unlock it after free. */
3724 cmd_ctx
->session
= NULL
;
3727 case LTTNG_LIST_DOMAINS
:
3730 struct lttng_domain
*domains
= NULL
;
3732 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3734 /* Return value is a negative lttng_error_code. */
3739 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3740 nb_dom
* sizeof(struct lttng_domain
));
3750 case LTTNG_LIST_CHANNELS
:
3752 ssize_t payload_size
;
3753 struct lttng_channel
*channels
= NULL
;
3755 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3756 cmd_ctx
->session
, &channels
);
3757 if (payload_size
< 0) {
3758 /* Return value is a negative lttng_error_code. */
3759 ret
= -payload_size
;
3763 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3774 case LTTNG_LIST_EVENTS
:
3777 struct lttng_event
*events
= NULL
;
3778 struct lttcomm_event_command_header cmd_header
;
3781 memset(&cmd_header
, 0, sizeof(cmd_header
));
3782 /* Extended infos are included at the end of events */
3783 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3784 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3785 &events
, &total_size
);
3788 /* Return value is a negative lttng_error_code. */
3793 cmd_header
.nb_events
= nb_event
;
3794 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3795 &cmd_header
, sizeof(cmd_header
));
3805 case LTTNG_LIST_SESSIONS
:
3807 unsigned int nr_sessions
;
3808 void *sessions_payload
;
3811 session_lock_list();
3812 nr_sessions
= lttng_sessions_count(
3813 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3814 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3815 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3816 sessions_payload
= zmalloc(payload_len
);
3818 if (!sessions_payload
) {
3819 session_unlock_list();
3824 cmd_list_lttng_sessions(sessions_payload
,
3825 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3826 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3827 session_unlock_list();
3829 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3831 free(sessions_payload
);
3840 case LTTNG_REGISTER_CONSUMER
:
3842 struct consumer_data
*cdata
;
3844 switch (cmd_ctx
->lsm
->domain
.type
) {
3845 case LTTNG_DOMAIN_KERNEL
:
3846 cdata
= &kconsumer_data
;
3849 ret
= LTTNG_ERR_UND
;
3853 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3854 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3857 case LTTNG_DATA_PENDING
:
3860 uint8_t pending_ret_byte
;
3862 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3867 * This function may returns 0 or 1 to indicate whether or not
3868 * there is data pending. In case of error, it should return an
3869 * LTTNG_ERR code. However, some code paths may still return
3870 * a nondescript error code, which we handle by returning an
3873 if (pending_ret
== 0 || pending_ret
== 1) {
3875 * ret will be set to LTTNG_OK at the end of
3878 } else if (pending_ret
< 0) {
3879 ret
= LTTNG_ERR_UNK
;
3886 pending_ret_byte
= (uint8_t) pending_ret
;
3888 /* 1 byte to return whether or not data is pending */
3889 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3890 &pending_ret_byte
, 1);
3899 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3901 struct lttcomm_lttng_output_id reply
;
3903 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3904 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3905 if (ret
!= LTTNG_OK
) {
3909 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3915 /* Copy output list into message payload */
3919 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3921 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3922 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3925 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3928 struct lttng_snapshot_output
*outputs
= NULL
;
3930 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3931 if (nb_output
< 0) {
3936 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3937 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3938 nb_output
* sizeof(struct lttng_snapshot_output
));
3948 case LTTNG_SNAPSHOT_RECORD
:
3950 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3951 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3952 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3955 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3958 struct lttng_uri
*uris
= NULL
;
3960 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3961 len
= nb_uri
* sizeof(struct lttng_uri
);
3964 uris
= zmalloc(len
);
3966 ret
= LTTNG_ERR_FATAL
;
3970 /* Receive variable len data */
3971 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3972 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3974 DBG("No URIs received from client... continuing");
3976 ret
= LTTNG_ERR_SESSION_FAIL
;
3981 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3982 DBG("Creating session with ONE network URI is a bad call");
3983 ret
= LTTNG_ERR_SESSION_FAIL
;
3989 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3990 nb_uri
, &cmd_ctx
->creds
);
3994 case LTTNG_CREATE_SESSION_LIVE
:
3997 struct lttng_uri
*uris
= NULL
;
3999 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4000 len
= nb_uri
* sizeof(struct lttng_uri
);
4003 uris
= zmalloc(len
);
4005 ret
= LTTNG_ERR_FATAL
;
4009 /* Receive variable len data */
4010 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4011 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4013 DBG("No URIs received from client... continuing");
4015 ret
= LTTNG_ERR_SESSION_FAIL
;
4020 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4021 DBG("Creating session with ONE network URI is a bad call");
4022 ret
= LTTNG_ERR_SESSION_FAIL
;
4028 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4029 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4033 case LTTNG_SAVE_SESSION
:
4035 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4039 case LTTNG_SET_SESSION_SHM_PATH
:
4041 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4042 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4045 case LTTNG_REGENERATE_METADATA
:
4047 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4050 case LTTNG_REGENERATE_STATEDUMP
:
4052 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4055 case LTTNG_REGISTER_TRIGGER
:
4057 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4058 notification_thread_handle
);
4061 case LTTNG_UNREGISTER_TRIGGER
:
4063 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4064 notification_thread_handle
);
4068 ret
= LTTNG_ERR_UND
;
4073 if (cmd_ctx
->llm
== NULL
) {
4074 DBG("Missing llm structure. Allocating one.");
4075 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4079 /* Set return code */
4080 cmd_ctx
->llm
->ret_code
= ret
;
4082 if (cmd_ctx
->session
) {
4083 session_unlock(cmd_ctx
->session
);
4085 if (need_tracing_session
) {
4086 session_unlock_list();
4089 assert(!rcu_read_ongoing());
4094 * Thread managing health check socket.
4096 static void *thread_manage_health(void *data
)
4098 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4099 uint32_t revents
, nb_fd
;
4100 struct lttng_poll_event events
;
4101 struct health_comm_msg msg
;
4102 struct health_comm_reply reply
;
4104 DBG("[thread] Manage health check started");
4106 rcu_register_thread();
4108 /* We might hit an error path before this is created. */
4109 lttng_poll_init(&events
);
4111 /* Create unix socket */
4112 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4114 ERR("Unable to create health check Unix socket");
4119 /* lttng health client socket path permissions */
4120 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4121 utils_get_group_id(config
.tracing_group_name
.value
));
4123 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4128 ret
= chmod(config
.health_unix_sock_path
.value
,
4129 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4131 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4138 * Set the CLOEXEC flag. Return code is useless because either way, the
4141 (void) utils_set_fd_cloexec(sock
);
4143 ret
= lttcomm_listen_unix_sock(sock
);
4149 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4150 * more will be added to this poll set.
4152 ret
= sessiond_set_thread_pollset(&events
, 2);
4157 /* Add the application registration socket */
4158 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4163 sessiond_notify_ready();
4166 DBG("Health check ready");
4168 /* Inifinite blocking call, waiting for transmission */
4170 ret
= lttng_poll_wait(&events
, -1);
4173 * Restart interrupted system call.
4175 if (errno
== EINTR
) {
4183 for (i
= 0; i
< nb_fd
; i
++) {
4184 /* Fetch once the poll data */
4185 revents
= LTTNG_POLL_GETEV(&events
, i
);
4186 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4189 /* No activity for this FD (poll implementation). */
4193 /* Thread quit pipe has been closed. Killing thread. */
4194 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4200 /* Event on the registration socket */
4201 if (pollfd
== sock
) {
4202 if (revents
& LPOLLIN
) {
4204 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4205 ERR("Health socket poll error");
4208 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4214 new_sock
= lttcomm_accept_unix_sock(sock
);
4220 * Set the CLOEXEC flag. Return code is useless because either way, the
4223 (void) utils_set_fd_cloexec(new_sock
);
4225 DBG("Receiving data from client for health...");
4226 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4228 DBG("Nothing recv() from client... continuing");
4229 ret
= close(new_sock
);
4236 rcu_thread_online();
4238 memset(&reply
, 0, sizeof(reply
));
4239 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4241 * health_check_state returns 0 if health is
4244 if (!health_check_state(health_sessiond
, i
)) {
4245 reply
.ret_code
|= 1ULL << i
;
4249 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4251 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4253 ERR("Failed to send health data back to client");
4256 /* End of transmission */
4257 ret
= close(new_sock
);
4266 ERR("Health error occurred in %s", __func__
);
4268 DBG("Health check thread dying");
4269 unlink(config
.health_unix_sock_path
.value
);
4277 lttng_poll_clean(&events
);
4279 rcu_unregister_thread();
4284 * This thread manage all clients request using the unix client socket for
4287 static void *thread_manage_clients(void *data
)
4289 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4291 uint32_t revents
, nb_fd
;
4292 struct command_ctx
*cmd_ctx
= NULL
;
4293 struct lttng_poll_event events
;
4295 DBG("[thread] Manage client started");
4297 rcu_register_thread();
4299 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4301 health_code_update();
4303 ret
= lttcomm_listen_unix_sock(client_sock
);
4309 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4310 * more will be added to this poll set.
4312 ret
= sessiond_set_thread_pollset(&events
, 2);
4314 goto error_create_poll
;
4317 /* Add the application registration socket */
4318 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4323 ret
= sem_post(&load_info
->message_thread_ready
);
4325 PERROR("sem_post message_thread_ready");
4330 * Wait until all support threads are initialized before accepting
4333 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4335 struct timeval timeout
;
4338 FD_SET(thread_quit_pipe
[0], &read_fds
);
4339 memset(&timeout
, 0, sizeof(timeout
));
4340 timeout
.tv_usec
= 1000;
4343 * If a support thread failed to launch, it may signal that
4344 * we must exit and the sessiond would never be marked as
4347 * The timeout is set to 1ms, which serves as a way to
4348 * pace down this check.
4350 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4352 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4358 /* This testpoint is after we signal readiness to the parent. */
4359 if (testpoint(sessiond_thread_manage_clients
)) {
4363 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4367 health_code_update();
4370 DBG("Accepting client command ...");
4372 /* Inifinite blocking call, waiting for transmission */
4374 health_poll_entry();
4375 ret
= lttng_poll_wait(&events
, -1);
4379 * Restart interrupted system call.
4381 if (errno
== EINTR
) {
4389 for (i
= 0; i
< nb_fd
; i
++) {
4390 /* Fetch once the poll data */
4391 revents
= LTTNG_POLL_GETEV(&events
, i
);
4392 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4394 health_code_update();
4397 /* No activity for this FD (poll implementation). */
4401 /* Thread quit pipe has been closed. Killing thread. */
4402 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4408 /* Event on the registration socket */
4409 if (pollfd
== client_sock
) {
4410 if (revents
& LPOLLIN
) {
4412 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4413 ERR("Client socket poll error");
4416 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4422 DBG("Wait for client response");
4424 health_code_update();
4426 sock
= lttcomm_accept_unix_sock(client_sock
);
4432 * Set the CLOEXEC flag. Return code is useless because either way, the
4435 (void) utils_set_fd_cloexec(sock
);
4437 /* Set socket option for credentials retrieval */
4438 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4443 /* Allocate context command to process the client request */
4444 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4445 if (cmd_ctx
== NULL
) {
4446 PERROR("zmalloc cmd_ctx");
4450 /* Allocate data buffer for reception */
4451 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4452 if (cmd_ctx
->lsm
== NULL
) {
4453 PERROR("zmalloc cmd_ctx->lsm");
4457 cmd_ctx
->llm
= NULL
;
4458 cmd_ctx
->session
= NULL
;
4460 health_code_update();
4463 * Data is received from the lttng client. The struct
4464 * lttcomm_session_msg (lsm) contains the command and data request of
4467 DBG("Receiving data from client ...");
4468 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4469 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4471 DBG("Nothing recv() from client... continuing");
4477 clean_command_ctx(&cmd_ctx
);
4481 health_code_update();
4483 // TODO: Validate cmd_ctx including sanity check for
4484 // security purpose.
4486 rcu_thread_online();
4488 * This function dispatch the work to the kernel or userspace tracer
4489 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4490 * informations for the client. The command context struct contains
4491 * everything this function may needs.
4493 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4494 rcu_thread_offline();
4502 * TODO: Inform client somehow of the fatal error. At
4503 * this point, ret < 0 means that a zmalloc failed
4504 * (ENOMEM). Error detected but still accept
4505 * command, unless a socket error has been
4508 clean_command_ctx(&cmd_ctx
);
4512 health_code_update();
4514 DBG("Sending response (size: %d, retcode: %s (%d))",
4515 cmd_ctx
->lttng_msg_size
,
4516 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4517 cmd_ctx
->llm
->ret_code
);
4518 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4520 ERR("Failed to send data back to client");
4523 /* End of transmission */
4530 clean_command_ctx(&cmd_ctx
);
4532 health_code_update();
4544 lttng_poll_clean(&events
);
4545 clean_command_ctx(&cmd_ctx
);
4549 unlink(config
.client_unix_sock_path
.value
);
4550 if (client_sock
>= 0) {
4551 ret
= close(client_sock
);
4559 ERR("Health error occurred in %s", __func__
);
4562 health_unregister(health_sessiond
);
4564 DBG("Client thread dying");
4566 rcu_unregister_thread();
4569 * Since we are creating the consumer threads, we own them, so we need
4570 * to join them before our thread exits.
4572 ret
= join_consumer_thread(&kconsumer_data
);
4575 PERROR("join_consumer");
4578 ret
= join_consumer_thread(&ustconsumer32_data
);
4581 PERROR("join_consumer ust32");
4584 ret
= join_consumer_thread(&ustconsumer64_data
);
4587 PERROR("join_consumer ust64");
4592 static int string_match(const char *str1
, const char *str2
)
4594 return (str1
&& str2
) && !strcmp(str1
, str2
);
4598 * Take an option from the getopt output and set it in the right variable to be
4601 * Return 0 on success else a negative value.
4603 static int set_option(int opt
, const char *arg
, const char *optname
)
4607 if (string_match(optname
, "client-sock") || opt
== 'c') {
4608 if (!arg
|| *arg
== '\0') {
4612 if (lttng_is_setuid_setgid()) {
4613 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4614 "-c, --client-sock");
4616 config_string_set(&config
.client_unix_sock_path
,
4618 if (!config
.client_unix_sock_path
.value
) {
4623 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4624 if (!arg
|| *arg
== '\0') {
4628 if (lttng_is_setuid_setgid()) {
4629 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4632 config_string_set(&config
.apps_unix_sock_path
,
4634 if (!config
.apps_unix_sock_path
.value
) {
4639 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4640 config
.daemonize
= true;
4641 } else if (string_match(optname
, "background") || opt
== 'b') {
4642 config
.background
= true;
4643 } else if (string_match(optname
, "group") || opt
== 'g') {
4644 if (!arg
|| *arg
== '\0') {
4648 if (lttng_is_setuid_setgid()) {
4649 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4652 config_string_set(&config
.tracing_group_name
,
4654 if (!config
.tracing_group_name
.value
) {
4659 } else if (string_match(optname
, "help") || opt
== 'h') {
4660 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4662 ERR("Cannot show --help for `lttng-sessiond`");
4665 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4666 } else if (string_match(optname
, "version") || opt
== 'V') {
4667 fprintf(stdout
, "%s\n", VERSION
);
4669 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4670 config
.sig_parent
= true;
4671 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4672 if (!arg
|| *arg
== '\0') {
4676 if (lttng_is_setuid_setgid()) {
4677 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4678 "--kconsumerd-err-sock");
4680 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4682 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4687 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4688 if (!arg
|| *arg
== '\0') {
4692 if (lttng_is_setuid_setgid()) {
4693 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4694 "--kconsumerd-cmd-sock");
4696 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4698 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4703 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4704 if (!arg
|| *arg
== '\0') {
4708 if (lttng_is_setuid_setgid()) {
4709 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4710 "--ustconsumerd64-err-sock");
4712 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4714 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4719 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4720 if (!arg
|| *arg
== '\0') {
4724 if (lttng_is_setuid_setgid()) {
4725 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4726 "--ustconsumerd64-cmd-sock");
4728 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4730 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4735 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4736 if (!arg
|| *arg
== '\0') {
4740 if (lttng_is_setuid_setgid()) {
4741 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4742 "--ustconsumerd32-err-sock");
4744 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4746 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4751 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4752 if (!arg
|| *arg
== '\0') {
4756 if (lttng_is_setuid_setgid()) {
4757 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4758 "--ustconsumerd32-cmd-sock");
4760 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4762 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4767 } else if (string_match(optname
, "no-kernel")) {
4768 config
.no_kernel
= true;
4769 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4770 config
.quiet
= true;
4771 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4772 /* Verbose level can increase using multiple -v */
4774 /* Value obtained from config file */
4775 config
.verbose
= config_parse_value(arg
);
4777 /* -v used on command line */
4780 /* Clamp value to [0, 3] */
4781 config
.verbose
= config
.verbose
< 0 ? 0 :
4782 (config
.verbose
<= 3 ? config
.verbose
: 3);
4783 } else if (string_match(optname
, "verbose-consumer")) {
4785 config
.verbose_consumer
= config_parse_value(arg
);
4787 config
.verbose_consumer
++;
4789 } else if (string_match(optname
, "consumerd32-path")) {
4790 if (!arg
|| *arg
== '\0') {
4794 if (lttng_is_setuid_setgid()) {
4795 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4796 "--consumerd32-path");
4798 config_string_set(&config
.consumerd32_bin_path
,
4800 if (!config
.consumerd32_bin_path
.value
) {
4805 } else if (string_match(optname
, "consumerd32-libdir")) {
4806 if (!arg
|| *arg
== '\0') {
4810 if (lttng_is_setuid_setgid()) {
4811 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4812 "--consumerd32-libdir");
4814 config_string_set(&config
.consumerd32_lib_dir
,
4816 if (!config
.consumerd32_lib_dir
.value
) {
4821 } else if (string_match(optname
, "consumerd64-path")) {
4822 if (!arg
|| *arg
== '\0') {
4826 if (lttng_is_setuid_setgid()) {
4827 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4828 "--consumerd64-path");
4830 config_string_set(&config
.consumerd64_bin_path
,
4832 if (!config
.consumerd64_bin_path
.value
) {
4837 } else if (string_match(optname
, "consumerd64-libdir")) {
4838 if (!arg
|| *arg
== '\0') {
4842 if (lttng_is_setuid_setgid()) {
4843 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4844 "--consumerd64-libdir");
4846 config_string_set(&config
.consumerd64_lib_dir
,
4848 if (!config
.consumerd64_lib_dir
.value
) {
4853 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4854 if (!arg
|| *arg
== '\0') {
4858 if (lttng_is_setuid_setgid()) {
4859 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4862 config_string_set(&config
.pid_file_path
, strdup(arg
));
4863 if (!config
.pid_file_path
.value
) {
4868 } else if (string_match(optname
, "agent-tcp-port")) {
4869 if (!arg
|| *arg
== '\0') {
4873 if (lttng_is_setuid_setgid()) {
4874 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4875 "--agent-tcp-port");
4880 v
= strtoul(arg
, NULL
, 0);
4881 if (errno
!= 0 || !isdigit(arg
[0])) {
4882 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4885 if (v
== 0 || v
>= 65535) {
4886 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4889 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
4890 DBG3("Agent TCP port set to non default: %i", (int) v
);
4892 } else if (string_match(optname
, "load") || opt
== 'l') {
4893 if (!arg
|| *arg
== '\0') {
4897 if (lttng_is_setuid_setgid()) {
4898 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4901 config_string_set(&config
.load_session_path
, strdup(arg
));
4902 if (!config
.load_session_path
.value
) {
4907 } else if (string_match(optname
, "kmod-probes")) {
4908 if (!arg
|| *arg
== '\0') {
4912 if (lttng_is_setuid_setgid()) {
4913 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4916 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4917 if (!config
.kmod_probes_list
.value
) {
4922 } else if (string_match(optname
, "extra-kmod-probes")) {
4923 if (!arg
|| *arg
== '\0') {
4927 if (lttng_is_setuid_setgid()) {
4928 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4929 "--extra-kmod-probes");
4931 config_string_set(&config
.kmod_extra_probes_list
,
4933 if (!config
.kmod_extra_probes_list
.value
) {
4938 } else if (string_match(optname
, "config") || opt
== 'f') {
4939 /* This is handled in set_options() thus silent skip. */
4942 /* Unknown option or other error.
4943 * Error is printed by getopt, just return */
4948 if (ret
== -EINVAL
) {
4949 const char *opt_name
= "unknown";
4952 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4954 if (opt
== long_options
[i
].val
) {
4955 opt_name
= long_options
[i
].name
;
4960 WARN("Invalid argument provided for option \"%s\", using default value.",
4968 * config_entry_handler_cb used to handle options read from a config file.
4969 * See config_entry_handler_cb comment in common/config/session-config.h for the
4970 * return value conventions.
4972 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4976 if (!entry
|| !entry
->name
|| !entry
->value
) {
4981 /* Check if the option is to be ignored */
4982 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4983 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4988 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4991 /* Ignore if not fully matched. */
4992 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4997 * If the option takes no argument on the command line, we have to
4998 * check if the value is "true". We support non-zero numeric values,
5001 if (!long_options
[i
].has_arg
) {
5002 ret
= config_parse_value(entry
->value
);
5005 WARN("Invalid configuration value \"%s\" for option %s",
5006 entry
->value
, entry
->name
);
5008 /* False, skip boolean config option. */
5013 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5017 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5024 * daemon configuration loading and argument parsing
5026 static int set_options(int argc
, char **argv
)
5028 int ret
= 0, c
= 0, option_index
= 0;
5029 int orig_optopt
= optopt
, orig_optind
= optind
;
5031 const char *config_path
= NULL
;
5033 optstring
= utils_generate_optstring(long_options
,
5034 sizeof(long_options
) / sizeof(struct option
));
5040 /* Check for the --config option */
5041 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5042 &option_index
)) != -1) {
5046 } else if (c
!= 'f') {
5047 /* if not equal to --config option. */
5051 if (lttng_is_setuid_setgid()) {
5052 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5055 config_path
= utils_expand_path(optarg
);
5057 ERR("Failed to resolve path: %s", optarg
);
5062 ret
= config_get_section_entries(config_path
, config_section_name
,
5063 config_entry_handler
, NULL
);
5066 ERR("Invalid configuration option at line %i", ret
);
5072 /* Reset getopt's global state */
5073 optopt
= orig_optopt
;
5074 optind
= orig_optind
;
5078 * getopt_long() will not set option_index if it encounters a
5081 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5088 * Pass NULL as the long option name if popt left the index
5091 ret
= set_option(c
, optarg
,
5092 option_index
< 0 ? NULL
:
5093 long_options
[option_index
].name
);
5105 * Creates the two needed socket by the daemon.
5106 * apps_sock - The communication socket for all UST apps.
5107 * client_sock - The communication of the cli tool (lttng).
5109 static int init_daemon_socket(void)
5114 old_umask
= umask(0);
5116 /* Create client tool unix socket */
5117 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5118 if (client_sock
< 0) {
5119 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5124 /* Set the cloexec flag */
5125 ret
= utils_set_fd_cloexec(client_sock
);
5127 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5128 "Continuing but note that the consumer daemon will have a "
5129 "reference to this socket on exec()", client_sock
);
5132 /* File permission MUST be 660 */
5133 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5135 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5140 /* Create the application unix socket */
5141 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5142 if (apps_sock
< 0) {
5143 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5148 /* Set the cloexec flag */
5149 ret
= utils_set_fd_cloexec(apps_sock
);
5151 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5152 "Continuing but note that the consumer daemon will have a "
5153 "reference to this socket on exec()", apps_sock
);
5156 /* File permission MUST be 666 */
5157 ret
= chmod(config
.apps_unix_sock_path
.value
,
5158 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5160 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5165 DBG3("Session daemon client socket %d and application socket %d created",
5166 client_sock
, apps_sock
);
5174 * Create lockfile using the rundir and return its fd.
5176 static int create_lockfile(void)
5178 return utils_create_lock_file(config
.lock_file_path
.value
);
5182 * Check if the global socket is available, and if a daemon is answering at the
5183 * other side. If yes, error is returned.
5185 * Also attempts to create and hold the lock file.
5187 static int check_existing_daemon(void)
5191 /* Is there anybody out there ? */
5192 if (lttng_session_daemon_alive()) {
5197 lockfile_fd
= create_lockfile();
5198 if (lockfile_fd
< 0) {
5206 static void sessiond_cleanup_lock_file(void)
5211 * Cleanup lock file by deleting it and finaly closing it which will
5212 * release the file system lock.
5214 if (lockfile_fd
>= 0) {
5215 ret
= remove(config
.lock_file_path
.value
);
5217 PERROR("remove lock file");
5219 ret
= close(lockfile_fd
);
5221 PERROR("close lock file");
5227 * Set the tracing group gid onto the client socket.
5229 * Race window between mkdir and chown is OK because we are going from more
5230 * permissive (root.root) to less permissive (root.tracing).
5232 static int set_permissions(char *rundir
)
5237 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5239 /* Set lttng run dir */
5240 ret
= chown(rundir
, 0, gid
);
5242 ERR("Unable to set group on %s", rundir
);
5247 * Ensure all applications and tracing group can search the run
5248 * dir. Allow everyone to read the directory, since it does not
5249 * buy us anything to hide its content.
5251 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5253 ERR("Unable to set permissions on %s", rundir
);
5257 /* lttng client socket path */
5258 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5260 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5264 /* kconsumer error socket path */
5265 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5267 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5271 /* 64-bit ustconsumer error socket path */
5272 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5274 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5278 /* 32-bit ustconsumer compat32 error socket path */
5279 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5281 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5285 DBG("All permissions are set");
5291 * Create the lttng run directory needed for all global sockets and pipe.
5293 static int create_lttng_rundir(void)
5297 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5299 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5301 if (errno
!= EEXIST
) {
5302 ERR("Unable to create %s", config
.rundir
.value
);
5314 * Setup sockets and directory needed by the consumerds' communication with the
5317 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5322 switch (consumer_data
->type
) {
5323 case LTTNG_CONSUMER_KERNEL
:
5324 path
= config
.kconsumerd_path
.value
;
5326 case LTTNG_CONSUMER64_UST
:
5327 path
= config
.consumerd64_path
.value
;
5329 case LTTNG_CONSUMER32_UST
:
5330 path
= config
.consumerd32_path
.value
;
5333 ERR("Consumer type unknown");
5339 DBG2("Creating consumer directory: %s", path
);
5341 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5342 if (ret
< 0 && errno
!= EEXIST
) {
5344 ERR("Failed to create %s", path
);
5348 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5350 ERR("Unable to set group on %s", path
);
5356 /* Create the consumerd error unix socket */
5357 consumer_data
->err_sock
=
5358 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5359 if (consumer_data
->err_sock
< 0) {
5360 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5366 * Set the CLOEXEC flag. Return code is useless because either way, the
5369 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5371 PERROR("utils_set_fd_cloexec");
5372 /* continue anyway */
5375 /* File permission MUST be 660 */
5376 ret
= chmod(consumer_data
->err_unix_sock_path
,
5377 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5379 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5389 * Signal handler for the daemon
5391 * Simply stop all worker threads, leaving main() return gracefully after
5392 * joining all threads and calling cleanup().
5394 static void sighandler(int sig
)
5398 DBG("SIGINT caught");
5402 DBG("SIGTERM caught");
5406 CMM_STORE_SHARED(recv_child_signal
, 1);
5414 * Setup signal handler for :
5415 * SIGINT, SIGTERM, SIGPIPE
5417 static int set_signal_handler(void)
5420 struct sigaction sa
;
5423 if ((ret
= sigemptyset(&sigset
)) < 0) {
5424 PERROR("sigemptyset");
5428 sa
.sa_mask
= sigset
;
5431 sa
.sa_handler
= sighandler
;
5432 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5433 PERROR("sigaction");
5437 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5438 PERROR("sigaction");
5442 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5443 PERROR("sigaction");
5447 sa
.sa_handler
= SIG_IGN
;
5448 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5449 PERROR("sigaction");
5453 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5459 * Set open files limit to unlimited. This daemon can open a large number of
5460 * file descriptors in order to consume multiple kernel traces.
5462 static void set_ulimit(void)
5467 /* The kernel does not allow an infinite limit for open files */
5468 lim
.rlim_cur
= 65535;
5469 lim
.rlim_max
= 65535;
5471 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5473 PERROR("failed to set open files limit");
5477 static int write_pidfile(void)
5479 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5482 static int set_clock_plugin_env(void)
5485 char *env_value
= NULL
;
5487 if (!config
.lttng_ust_clock_plugin
.value
) {
5491 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5492 config
.lttng_ust_clock_plugin
.value
);
5498 ret
= putenv(env_value
);
5501 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5505 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5506 config
.lttng_ust_clock_plugin
.value
);
5514 int main(int argc
, char **argv
)
5516 int ret
= 0, retval
= 0;
5518 const char *env_app_timeout
;
5519 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5520 *ust64_channel_monitor_pipe
= NULL
,
5521 *kernel_channel_monitor_pipe
= NULL
;
5522 bool notification_thread_running
= false;
5524 init_kernel_workarounds();
5526 rcu_register_thread();
5528 if (set_signal_handler()) {
5530 goto exit_set_signal_handler
;
5533 page_size
= sysconf(_SC_PAGESIZE
);
5534 if (page_size
< 0) {
5535 PERROR("sysconf _SC_PAGESIZE");
5536 page_size
= LONG_MAX
;
5537 WARN("Fallback page size to %ld", page_size
);
5540 ret
= sessiond_config_init(&config
);
5543 goto exit_set_signal_handler
;
5547 * Parse arguments and load the daemon configuration file.
5549 * We have an exit_options exit path to free memory reserved by
5550 * set_options. This is needed because the rest of sessiond_cleanup()
5551 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5552 * depends on set_options.
5555 if (set_options(argc
, argv
)) {
5560 /* Init config from environment variables. */
5561 sessiond_config_apply_env_config(&config
);
5564 * Resolve all paths received as arguments, configuration option, or
5565 * through environment variable as absolute paths. This is necessary
5566 * since daemonizing causes the sessiond's current working directory
5569 ret
= sessiond_config_resolve_paths(&config
);
5575 lttng_opt_verbose
= config
.verbose
;
5576 lttng_opt_quiet
= config
.quiet
;
5577 kconsumer_data
.err_unix_sock_path
=
5578 config
.kconsumerd_err_unix_sock_path
.value
;
5579 kconsumer_data
.cmd_unix_sock_path
=
5580 config
.kconsumerd_cmd_unix_sock_path
.value
;
5581 ustconsumer32_data
.err_unix_sock_path
=
5582 config
.consumerd32_err_unix_sock_path
.value
;
5583 ustconsumer32_data
.cmd_unix_sock_path
=
5584 config
.consumerd32_cmd_unix_sock_path
.value
;
5585 ustconsumer64_data
.err_unix_sock_path
=
5586 config
.consumerd64_err_unix_sock_path
.value
;
5587 ustconsumer64_data
.cmd_unix_sock_path
=
5588 config
.consumerd64_cmd_unix_sock_path
.value
;
5589 set_clock_plugin_env();
5591 sessiond_config_log(&config
);
5593 if (create_lttng_rundir()) {
5598 /* Abort launch if a session daemon is already running. */
5599 if (check_existing_daemon()) {
5600 ERR("A session daemon is already running.");
5606 if (config
.daemonize
|| config
.background
) {
5609 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5610 !config
.background
);
5617 * We are in the child. Make sure all other file descriptors are
5618 * closed, in case we are called with more opened file
5619 * descriptors than the standard ones and the lock file.
5621 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5622 if (i
== lockfile_fd
) {
5629 if (run_as_create_worker(argv
[0]) < 0) {
5630 goto exit_create_run_as_worker_cleanup
;
5634 * Starting from here, we can create threads. This needs to be after
5635 * lttng_daemonize due to RCU.
5639 * Initialize the health check subsystem. This call should set the
5640 * appropriate time values.
5642 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5643 if (!health_sessiond
) {
5644 PERROR("health_app_create error");
5646 goto exit_health_sessiond_cleanup
;
5649 /* Create thread to clean up RCU hash tables */
5650 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5652 goto exit_ht_cleanup
;
5655 /* Create thread quit pipe */
5656 if (init_thread_quit_pipe()) {
5658 goto exit_init_data
;
5661 /* Check if daemon is UID = 0 */
5662 is_root
= !getuid();
5664 /* Create global run dir with root access */
5666 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5667 if (!kernel_channel_monitor_pipe
) {
5668 ERR("Failed to create kernel consumer channel monitor pipe");
5670 goto exit_init_data
;
5672 kconsumer_data
.channel_monitor_pipe
=
5673 lttng_pipe_release_writefd(
5674 kernel_channel_monitor_pipe
);
5675 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5677 goto exit_init_data
;
5681 /* Set consumer initial state */
5682 kernel_consumerd_state
= CONSUMER_STOPPED
;
5683 ust_consumerd_state
= CONSUMER_STOPPED
;
5685 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5686 if (!ust32_channel_monitor_pipe
) {
5687 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5689 goto exit_init_data
;
5691 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5692 ust32_channel_monitor_pipe
);
5693 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5695 goto exit_init_data
;
5698 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5699 if (!ust64_channel_monitor_pipe
) {
5700 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5702 goto exit_init_data
;
5704 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5705 ust64_channel_monitor_pipe
);
5706 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5708 goto exit_init_data
;
5712 * Init UST app hash table. Alloc hash table before this point since
5713 * cleanup() can get called after that point.
5715 if (ust_app_ht_alloc()) {
5716 ERR("Failed to allocate UST app hash table");
5718 goto exit_init_data
;
5722 * Initialize agent app hash table. We allocate the hash table here
5723 * since cleanup() can get called after this point.
5725 if (agent_app_ht_alloc()) {
5726 ERR("Failed to allocate Agent app hash table");
5728 goto exit_init_data
;
5732 * These actions must be executed as root. We do that *after* setting up
5733 * the sockets path because we MUST make the check for another daemon using
5734 * those paths *before* trying to set the kernel consumer sockets and init
5738 if (set_consumer_sockets(&kconsumer_data
)) {
5740 goto exit_init_data
;
5743 /* Setup kernel tracer */
5744 if (!config
.no_kernel
) {
5745 init_kernel_tracer();
5746 if (kernel_tracer_fd
>= 0) {
5747 ret
= syscall_init_table();
5749 ERR("Unable to populate syscall table. "
5750 "Syscall tracing won't work "
5751 "for this session daemon.");
5756 /* Set ulimit for open files */
5759 /* init lttng_fd tracking must be done after set_ulimit. */
5762 if (set_consumer_sockets(&ustconsumer64_data
)) {
5764 goto exit_init_data
;
5767 if (set_consumer_sockets(&ustconsumer32_data
)) {
5769 goto exit_init_data
;
5772 /* Setup the needed unix socket */
5773 if (init_daemon_socket()) {
5775 goto exit_init_data
;
5778 /* Set credentials to socket */
5779 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5781 goto exit_init_data
;
5784 /* Get parent pid if -S, --sig-parent is specified. */
5785 if (config
.sig_parent
) {
5789 /* Setup the kernel pipe for waking up the kernel thread */
5790 if (is_root
&& !config
.no_kernel
) {
5791 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5793 goto exit_init_data
;
5797 /* Setup the thread apps communication pipe. */
5798 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5800 goto exit_init_data
;
5803 /* Setup the thread apps notify communication pipe. */
5804 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5806 goto exit_init_data
;
5809 /* Initialize global buffer per UID and PID registry. */
5810 buffer_reg_init_uid_registry();
5811 buffer_reg_init_pid_registry();
5813 /* Init UST command queue. */
5814 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5817 * Get session list pointer. This pointer MUST NOT be free'd. This list
5818 * is statically declared in session.c
5820 session_list_ptr
= session_get_list();
5824 /* Check for the application socket timeout env variable. */
5825 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5826 if (env_app_timeout
) {
5827 config
.app_socket_timeout
= atoi(env_app_timeout
);
5829 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5832 ret
= write_pidfile();
5834 ERR("Error in write_pidfile");
5836 goto exit_init_data
;
5839 /* Initialize communication library */
5841 /* Initialize TCP timeout values */
5842 lttcomm_inet_init();
5844 if (load_session_init_data(&load_info
) < 0) {
5846 goto exit_init_data
;
5848 load_info
->path
= config
.load_session_path
.value
;
5850 /* Create health-check thread. */
5851 ret
= pthread_create(&health_thread
, default_pthread_attr(),
5852 thread_manage_health
, (void *) NULL
);
5855 PERROR("pthread_create health");
5860 /* notification_thread_data acquires the pipes' read side. */
5861 notification_thread_handle
= notification_thread_handle_create(
5862 ust32_channel_monitor_pipe
,
5863 ust64_channel_monitor_pipe
,
5864 kernel_channel_monitor_pipe
);
5865 if (!notification_thread_handle
) {
5867 ERR("Failed to create notification thread shared data");
5869 goto exit_notification
;
5872 /* Create notification thread. */
5873 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
5874 thread_notification
, notification_thread_handle
);
5877 PERROR("pthread_create notification");
5880 goto exit_notification
;
5882 notification_thread_running
= true;
5884 /* Create thread to manage the client socket */
5885 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5886 thread_manage_clients
, (void *) NULL
);
5889 PERROR("pthread_create clients");
5895 /* Create thread to dispatch registration */
5896 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5897 thread_dispatch_ust_registration
, (void *) NULL
);
5900 PERROR("pthread_create dispatch");
5906 /* Create thread to manage application registration. */
5907 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5908 thread_registration_apps
, (void *) NULL
);
5911 PERROR("pthread_create registration");
5917 /* Create thread to manage application socket */
5918 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5919 thread_manage_apps
, (void *) NULL
);
5922 PERROR("pthread_create apps");
5928 /* Create thread to manage application notify socket */
5929 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5930 ust_thread_manage_notify
, (void *) NULL
);
5933 PERROR("pthread_create notify");
5936 goto exit_apps_notify
;
5939 /* Create agent registration thread. */
5940 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5941 agent_thread_manage_registration
, (void *) NULL
);
5944 PERROR("pthread_create agent");
5947 goto exit_agent_reg
;
5950 /* Don't start this thread if kernel tracing is not requested nor root */
5951 if (is_root
&& !config
.no_kernel
) {
5952 /* Create kernel thread to manage kernel event */
5953 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5954 thread_manage_kernel
, (void *) NULL
);
5957 PERROR("pthread_create kernel");
5964 /* Create session loading thread. */
5965 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5966 thread_load_session
, load_info
);
5969 PERROR("pthread_create load_session_thread");
5972 goto exit_load_session
;
5976 * This is where we start awaiting program completion (e.g. through
5977 * signal that asks threads to teardown).
5980 ret
= pthread_join(load_session_thread
, &status
);
5983 PERROR("pthread_join load_session_thread");
5988 if (is_root
&& !config
.no_kernel
) {
5989 ret
= pthread_join(kernel_thread
, &status
);
5992 PERROR("pthread_join");
5998 ret
= pthread_join(agent_reg_thread
, &status
);
6001 PERROR("pthread_join agent");
6006 ret
= pthread_join(apps_notify_thread
, &status
);
6009 PERROR("pthread_join apps notify");
6014 ret
= pthread_join(apps_thread
, &status
);
6017 PERROR("pthread_join apps");
6022 ret
= pthread_join(reg_apps_thread
, &status
);
6025 PERROR("pthread_join");
6031 * Join dispatch thread after joining reg_apps_thread to ensure
6032 * we don't leak applications in the queue.
6034 ret
= pthread_join(dispatch_thread
, &status
);
6037 PERROR("pthread_join");
6042 ret
= pthread_join(client_thread
, &status
);
6045 PERROR("pthread_join");
6051 ret
= pthread_join(health_thread
, &status
);
6054 PERROR("pthread_join health thread");
6061 * Wait for all pending call_rcu work to complete before tearing
6062 * down data structures. call_rcu worker may be trying to
6063 * perform lookups in those structures.
6067 * sessiond_cleanup() is called when no other thread is running, except
6068 * the ht_cleanup thread, which is needed to destroy the hash tables.
6070 rcu_thread_online();
6074 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6075 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6076 * the queue is empty before shutting down the clean-up thread.
6081 * The teardown of the notification system is performed after the
6082 * session daemon's teardown in order to allow it to be notified
6083 * of the active session and channels at the moment of the teardown.
6085 if (notification_thread_handle
) {
6086 if (notification_thread_running
) {
6087 notification_thread_command_quit(
6088 notification_thread_handle
);
6089 ret
= pthread_join(notification_thread
, &status
);
6092 PERROR("pthread_join notification thread");
6096 notification_thread_handle_destroy(notification_thread_handle
);
6099 rcu_thread_offline();
6100 rcu_unregister_thread();
6102 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6106 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6107 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6108 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6111 health_app_destroy(health_sessiond
);
6112 exit_health_sessiond_cleanup
:
6113 exit_create_run_as_worker_cleanup
:
6116 sessiond_cleanup_lock_file();
6117 sessiond_cleanup_options();
6119 exit_set_signal_handler
: