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
;
97 static struct lttng_kernel_tracer_version kernel_tracer_version
;
98 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
101 * Consumer daemon specific control data. Every value not initialized here is
102 * set to 0 by the static definition.
104 static struct consumer_data kconsumer_data
= {
105 .type
= LTTNG_CONSUMER_KERNEL
,
108 .channel_monitor_pipe
= -1,
109 .channel_rotate_pipe
= -1,
110 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 .lock
= PTHREAD_MUTEX_INITIALIZER
,
112 .cond
= PTHREAD_COND_INITIALIZER
,
113 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
115 static struct consumer_data ustconsumer64_data
= {
116 .type
= LTTNG_CONSUMER64_UST
,
119 .channel_monitor_pipe
= -1,
120 .channel_rotate_pipe
= -1,
121 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
122 .lock
= PTHREAD_MUTEX_INITIALIZER
,
123 .cond
= PTHREAD_COND_INITIALIZER
,
124 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
126 static struct consumer_data ustconsumer32_data
= {
127 .type
= LTTNG_CONSUMER32_UST
,
130 .channel_monitor_pipe
= -1,
131 .channel_rotate_pipe
= -1,
132 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
133 .lock
= PTHREAD_MUTEX_INITIALIZER
,
134 .cond
= PTHREAD_COND_INITIALIZER
,
135 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
138 /* Command line options */
139 static const struct option long_options
[] = {
140 { "client-sock", required_argument
, 0, 'c' },
141 { "apps-sock", required_argument
, 0, 'a' },
142 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
143 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
144 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
145 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
148 { "consumerd32-path", required_argument
, 0, '\0' },
149 { "consumerd32-libdir", required_argument
, 0, '\0' },
150 { "consumerd64-path", required_argument
, 0, '\0' },
151 { "consumerd64-libdir", required_argument
, 0, '\0' },
152 { "daemonize", no_argument
, 0, 'd' },
153 { "background", no_argument
, 0, 'b' },
154 { "sig-parent", no_argument
, 0, 'S' },
155 { "help", no_argument
, 0, 'h' },
156 { "group", required_argument
, 0, 'g' },
157 { "version", no_argument
, 0, 'V' },
158 { "quiet", no_argument
, 0, 'q' },
159 { "verbose", no_argument
, 0, 'v' },
160 { "verbose-consumer", no_argument
, 0, '\0' },
161 { "no-kernel", no_argument
, 0, '\0' },
162 { "pidfile", required_argument
, 0, 'p' },
163 { "agent-tcp-port", required_argument
, 0, '\0' },
164 { "config", required_argument
, 0, 'f' },
165 { "load", required_argument
, 0, 'l' },
166 { "kmod-probes", required_argument
, 0, '\0' },
167 { "extra-kmod-probes", required_argument
, 0, '\0' },
171 struct sessiond_config config
;
173 /* Command line options to ignore from configuration file */
174 static const char *config_ignore_options
[] = { "help", "version", "config" };
176 /* Shared between threads */
177 static int dispatch_thread_exit
;
179 /* Sockets and FDs */
180 static int client_sock
= -1;
181 static int apps_sock
= -1;
182 int kernel_tracer_fd
= -1;
183 static int kernel_poll_pipe
[2] = { -1, -1 };
186 * Quit pipe for all threads. This permits a single cancellation point
187 * for all threads when receiving an event on the pipe.
189 static int thread_quit_pipe
[2] = { -1, -1 };
192 * This pipe is used to inform the thread managing application communication
193 * that a command is queued and ready to be processed.
195 static int apps_cmd_pipe
[2] = { -1, -1 };
197 int apps_cmd_notify_pipe
[2] = { -1, -1 };
199 /* Pthread, Mutexes and Semaphores */
200 static pthread_t apps_thread
;
201 static pthread_t apps_notify_thread
;
202 static pthread_t reg_apps_thread
;
203 static pthread_t client_thread
;
204 static pthread_t kernel_thread
;
205 static pthread_t dispatch_thread
;
206 static pthread_t health_thread
;
207 static pthread_t ht_cleanup_thread
;
208 static pthread_t agent_reg_thread
;
209 static pthread_t load_session_thread
;
210 static pthread_t notification_thread
;
213 * UST registration command queue. This queue is tied with a futex and uses a N
214 * wakers / 1 waiter implemented and detailed in futex.c/.h
216 * The thread_registration_apps and thread_dispatch_ust_registration uses this
217 * queue along with the wait/wake scheme. The thread_manage_apps receives down
218 * the line new application socket and monitors it for any I/O error or clean
219 * close that triggers an unregistration of the application.
221 static struct ust_cmd_queue ust_cmd_queue
;
224 * Pointer initialized before thread creation.
226 * This points to the tracing session list containing the session count and a
227 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
228 * MUST NOT be taken if you call a public function in session.c.
230 * The lock is nested inside the structure: session_list_ptr->lock. Please use
231 * session_lock_list and session_unlock_list for lock acquisition.
233 static struct ltt_session_list
*session_list_ptr
;
235 int ust_consumerd64_fd
= -1;
236 int ust_consumerd32_fd
= -1;
238 static const char *module_proc_lttng
= "/proc/lttng";
241 * Consumer daemon state which is changed when spawning it, killing it or in
242 * case of a fatal error.
244 enum consumerd_state
{
245 CONSUMER_STARTED
= 1,
246 CONSUMER_STOPPED
= 2,
251 * This consumer daemon state is used to validate if a client command will be
252 * able to reach the consumer. If not, the client is informed. For instance,
253 * doing a "lttng start" when the consumer state is set to ERROR will return an
254 * error to the client.
256 * The following example shows a possible race condition of this scheme:
258 * consumer thread error happens
260 * client cmd checks state -> still OK
261 * consumer thread exit, sets error
262 * client cmd try to talk to consumer
265 * However, since the consumer is a different daemon, we have no way of making
266 * sure the command will reach it safely even with this state flag. This is why
267 * we consider that up to the state validation during command processing, the
268 * command is safe. After that, we can not guarantee the correctness of the
269 * client request vis-a-vis the consumer.
271 static enum consumerd_state ust_consumerd_state
;
272 static enum consumerd_state kernel_consumerd_state
;
274 /* Set in main() with the current page size. */
277 /* Application health monitoring */
278 struct health_app
*health_sessiond
;
280 /* Am I root or not. */
281 int is_root
; /* Set to 1 if the daemon is running as root */
283 const char * const config_section_name
= "sessiond";
285 /* Load session thread information to operate. */
286 struct load_session_thread_data
*load_info
;
288 /* Notification thread handle. */
289 struct notification_thread_handle
*notification_thread_handle
;
291 /* Global hash tables */
292 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
295 * Whether sessiond is ready for commands/notification channel/health check
297 * NR_LTTNG_SESSIOND_READY must match the number of calls to
298 * sessiond_notify_ready().
300 #define NR_LTTNG_SESSIOND_READY 4
301 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
303 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
305 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
308 /* Notify parents that we are ready for cmd and health check */
310 void sessiond_notify_ready(void)
312 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
314 * Notify parent pid that we are ready to accept command
315 * for client side. This ppid is the one from the
316 * external process that spawned us.
318 if (config
.sig_parent
) {
323 * Notify the parent of the fork() process that we are
326 if (config
.daemonize
|| config
.background
) {
327 kill(child_ppid
, SIGUSR1
);
333 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
340 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
346 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
358 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
360 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
362 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
366 * Init thread quit pipe.
368 * Return -1 on error or 0 if all pipes are created.
370 static int __init_thread_quit_pipe(int *a_pipe
)
376 PERROR("thread quit pipe");
380 for (i
= 0; i
< 2; i
++) {
381 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
392 static int init_thread_quit_pipe(void)
394 return __init_thread_quit_pipe(thread_quit_pipe
);
398 * Stop all threads by closing the thread quit pipe.
400 static void stop_threads(void)
404 /* Stopping all threads */
405 DBG("Terminating all threads");
406 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
408 ERR("write error on thread quit pipe");
411 /* Dispatch thread */
412 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
413 futex_nto1_wake(&ust_cmd_queue
.futex
);
417 * Close every consumer sockets.
419 static void close_consumer_sockets(void)
423 if (kconsumer_data
.err_sock
>= 0) {
424 ret
= close(kconsumer_data
.err_sock
);
426 PERROR("kernel consumer err_sock close");
429 if (ustconsumer32_data
.err_sock
>= 0) {
430 ret
= close(ustconsumer32_data
.err_sock
);
432 PERROR("UST consumerd32 err_sock close");
435 if (ustconsumer64_data
.err_sock
>= 0) {
436 ret
= close(ustconsumer64_data
.err_sock
);
438 PERROR("UST consumerd64 err_sock close");
441 if (kconsumer_data
.cmd_sock
>= 0) {
442 ret
= close(kconsumer_data
.cmd_sock
);
444 PERROR("kernel consumer cmd_sock close");
447 if (ustconsumer32_data
.cmd_sock
>= 0) {
448 ret
= close(ustconsumer32_data
.cmd_sock
);
450 PERROR("UST consumerd32 cmd_sock close");
453 if (ustconsumer64_data
.cmd_sock
>= 0) {
454 ret
= close(ustconsumer64_data
.cmd_sock
);
456 PERROR("UST consumerd64 cmd_sock close");
459 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
460 ret
= close(kconsumer_data
.channel_monitor_pipe
);
462 PERROR("kernel consumer channel monitor pipe close");
465 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
466 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
468 PERROR("UST consumerd32 channel monitor pipe close");
471 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
472 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
474 PERROR("UST consumerd64 channel monitor pipe close");
477 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
478 ret
= close(kconsumer_data
.channel_rotate_pipe
);
480 PERROR("kernel consumer channel rotate pipe close");
483 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
484 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
486 PERROR("UST consumerd32 channel rotate pipe close");
489 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
490 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
492 PERROR("UST consumerd64 channel rotate pipe close");
498 * Wait on consumer process termination.
500 * Need to be called with the consumer data lock held or from a context
501 * ensuring no concurrent access to data (e.g: cleanup).
503 static void wait_consumer(struct consumer_data
*consumer_data
)
508 if (consumer_data
->pid
<= 0) {
512 DBG("Waiting for complete teardown of consumerd (PID: %d)",
514 ret
= waitpid(consumer_data
->pid
, &status
, 0);
516 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
517 } else if (!WIFEXITED(status
)) {
518 ERR("consumerd termination with error: %d",
521 consumer_data
->pid
= 0;
525 * Cleanup the session daemon's data structures.
527 static void sessiond_cleanup(void)
530 struct ltt_session
*sess
, *stmp
;
532 DBG("Cleanup sessiond");
535 * Close the thread quit pipe. It has already done its job,
536 * since we are now called.
538 utils_close_pipe(thread_quit_pipe
);
541 * If config.pid_file_path.value is undefined, the default file will be
542 * wiped when removing the rundir.
544 if (config
.pid_file_path
.value
) {
545 ret
= remove(config
.pid_file_path
.value
);
547 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
551 DBG("Removing sessiond and consumerd content of directory %s",
552 config
.rundir
.value
);
555 DBG("Removing %s", config
.pid_file_path
.value
);
556 (void) unlink(config
.pid_file_path
.value
);
558 DBG("Removing %s", config
.agent_port_file_path
.value
);
559 (void) unlink(config
.agent_port_file_path
.value
);
562 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
563 (void) unlink(kconsumer_data
.err_unix_sock_path
);
565 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
566 (void) rmdir(config
.kconsumerd_path
.value
);
568 /* ust consumerd 32 */
569 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
570 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
572 DBG("Removing directory %s", config
.consumerd32_path
.value
);
573 (void) rmdir(config
.consumerd32_path
.value
);
575 /* ust consumerd 64 */
576 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
577 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
579 DBG("Removing directory %s", config
.consumerd64_path
.value
);
580 (void) rmdir(config
.consumerd64_path
.value
);
582 DBG("Cleaning up all sessions");
584 /* Destroy session list mutex */
585 if (session_list_ptr
!= NULL
) {
586 pthread_mutex_destroy(&session_list_ptr
->lock
);
588 /* Cleanup ALL session */
589 cds_list_for_each_entry_safe(sess
, stmp
,
590 &session_list_ptr
->head
, list
) {
591 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
595 wait_consumer(&kconsumer_data
);
596 wait_consumer(&ustconsumer64_data
);
597 wait_consumer(&ustconsumer32_data
);
599 DBG("Cleaning up all agent apps");
600 agent_app_ht_clean();
602 DBG("Closing all UST sockets");
603 ust_app_clean_list();
604 buffer_reg_destroy_registries();
606 if (is_root
&& !config
.no_kernel
) {
607 DBG2("Closing kernel fd");
608 if (kernel_tracer_fd
>= 0) {
609 ret
= close(kernel_tracer_fd
);
614 DBG("Unloading kernel modules");
615 modprobe_remove_lttng_all();
619 close_consumer_sockets();
622 load_session_destroy_data(load_info
);
627 * Cleanup lock file by deleting it and finaly closing it which will
628 * release the file system lock.
630 if (lockfile_fd
>= 0) {
631 ret
= remove(config
.lock_file_path
.value
);
633 PERROR("remove lock file");
635 ret
= close(lockfile_fd
);
637 PERROR("close lock file");
642 * We do NOT rmdir rundir because there are other processes
643 * using it, for instance lttng-relayd, which can start in
644 * parallel with this teardown.
649 * Cleanup the daemon's option data structures.
651 static void sessiond_cleanup_options(void)
653 DBG("Cleaning up options");
655 sessiond_config_fini(&config
);
657 run_as_destroy_worker();
661 * Send data on a unix socket using the liblttsessiondcomm API.
663 * Return lttcomm error code.
665 static int send_unix_sock(int sock
, void *buf
, size_t len
)
667 /* Check valid length */
672 return lttcomm_send_unix_sock(sock
, buf
, len
);
676 * Free memory of a command context structure.
678 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
680 DBG("Clean command context structure");
682 if ((*cmd_ctx
)->llm
) {
683 free((*cmd_ctx
)->llm
);
685 if ((*cmd_ctx
)->lsm
) {
686 free((*cmd_ctx
)->lsm
);
694 * Notify UST applications using the shm mmap futex.
696 static int notify_ust_apps(int active
)
700 DBG("Notifying applications of session daemon state: %d", active
);
702 /* See shm.c for this call implying mmap, shm and futex calls */
703 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
704 if (wait_shm_mmap
== NULL
) {
708 /* Wake waiting process */
709 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
711 /* Apps notified successfully */
719 * Setup the outgoing data buffer for the response (llm) by allocating the
720 * right amount of memory and copying the original information from the lsm
723 * Return 0 on success, negative value on error.
725 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
726 const void *payload_buf
, size_t payload_len
,
727 const void *cmd_header_buf
, size_t cmd_header_len
)
730 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
731 const size_t cmd_header_offset
= header_len
;
732 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
733 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
735 cmd_ctx
->llm
= zmalloc(total_msg_size
);
737 if (cmd_ctx
->llm
== NULL
) {
743 /* Copy common data */
744 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
745 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
746 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
747 cmd_ctx
->llm
->data_size
= payload_len
;
748 cmd_ctx
->lttng_msg_size
= total_msg_size
;
750 /* Copy command header */
751 if (cmd_header_len
) {
752 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
758 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
767 * Version of setup_lttng_msg() without command header.
769 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
770 void *payload_buf
, size_t payload_len
)
772 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
775 * Update the kernel poll set of all channel fd available over all tracing
776 * session. Add the wakeup pipe at the end of the set.
778 static int update_kernel_poll(struct lttng_poll_event
*events
)
781 struct ltt_session
*session
;
782 struct ltt_kernel_channel
*channel
;
784 DBG("Updating kernel poll set");
787 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
788 session_lock(session
);
789 if (session
->kernel_session
== NULL
) {
790 session_unlock(session
);
794 cds_list_for_each_entry(channel
,
795 &session
->kernel_session
->channel_list
.head
, list
) {
796 /* Add channel fd to the kernel poll set */
797 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
799 session_unlock(session
);
802 DBG("Channel fd %d added to kernel set", channel
->fd
);
804 session_unlock(session
);
806 session_unlock_list();
811 session_unlock_list();
816 * Find the channel fd from 'fd' over all tracing session. When found, check
817 * for new channel stream and send those stream fds to the kernel consumer.
819 * Useful for CPU hotplug feature.
821 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
824 struct ltt_session
*session
;
825 struct ltt_kernel_session
*ksess
;
826 struct ltt_kernel_channel
*channel
;
828 DBG("Updating kernel streams for channel fd %d", fd
);
831 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
832 session_lock(session
);
833 if (session
->kernel_session
== NULL
) {
834 session_unlock(session
);
837 ksess
= session
->kernel_session
;
839 cds_list_for_each_entry(channel
,
840 &ksess
->channel_list
.head
, list
) {
841 struct lttng_ht_iter iter
;
842 struct consumer_socket
*socket
;
844 if (channel
->fd
!= fd
) {
847 DBG("Channel found, updating kernel streams");
848 ret
= kernel_open_channel_stream(channel
);
852 /* Update the stream global counter */
853 ksess
->stream_count_global
+= ret
;
856 * Have we already sent fds to the consumer? If yes, it
857 * means that tracing is started so it is safe to send
858 * our updated stream fds.
860 if (ksess
->consumer_fds_sent
!= 1
861 || ksess
->consumer
== NULL
) {
867 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
868 &iter
.iter
, socket
, node
.node
) {
869 pthread_mutex_lock(socket
->lock
);
870 ret
= kernel_consumer_send_channel_stream(socket
,
872 session
->output_traces
? 1 : 0);
873 pthread_mutex_unlock(socket
->lock
);
881 session_unlock(session
);
883 session_unlock_list();
887 session_unlock(session
);
888 session_unlock_list();
893 * For each tracing session, update newly registered apps. The session list
894 * lock MUST be acquired before calling this.
896 static void update_ust_app(int app_sock
)
898 struct ltt_session
*sess
, *stmp
;
900 /* Consumer is in an ERROR state. Stop any application update. */
901 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
902 /* Stop the update process since the consumer is dead. */
906 /* For all tracing session(s) */
907 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
911 if (!sess
->ust_session
) {
916 assert(app_sock
>= 0);
917 app
= ust_app_find_by_sock(app_sock
);
920 * Application can be unregistered before so
921 * this is possible hence simply stopping the
924 DBG3("UST app update failed to find app sock %d",
928 ust_app_global_update(sess
->ust_session
, app
);
932 session_unlock(sess
);
937 * This thread manage event coming from the kernel.
939 * Features supported in this thread:
942 static void *thread_manage_kernel(void *data
)
944 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
945 uint32_t revents
, nb_fd
;
947 struct lttng_poll_event events
;
949 DBG("[thread] Thread manage kernel started");
951 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
954 * This first step of the while is to clean this structure which could free
955 * non NULL pointers so initialize it before the loop.
957 lttng_poll_init(&events
);
959 if (testpoint(sessiond_thread_manage_kernel
)) {
960 goto error_testpoint
;
963 health_code_update();
965 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
966 goto error_testpoint
;
970 health_code_update();
972 if (update_poll_flag
== 1) {
973 /* Clean events object. We are about to populate it again. */
974 lttng_poll_clean(&events
);
976 ret
= sessiond_set_thread_pollset(&events
, 2);
978 goto error_poll_create
;
981 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
986 /* This will add the available kernel channel if any. */
987 ret
= update_kernel_poll(&events
);
991 update_poll_flag
= 0;
994 DBG("Thread kernel polling");
996 /* Poll infinite value of time */
999 ret
= lttng_poll_wait(&events
, -1);
1000 DBG("Thread kernel return from poll on %d fds",
1001 LTTNG_POLL_GETNB(&events
));
1005 * Restart interrupted system call.
1007 if (errno
== EINTR
) {
1011 } else if (ret
== 0) {
1012 /* Should not happen since timeout is infinite */
1013 ERR("Return value of poll is 0 with an infinite timeout.\n"
1014 "This should not have happened! Continuing...");
1020 for (i
= 0; i
< nb_fd
; i
++) {
1021 /* Fetch once the poll data */
1022 revents
= LTTNG_POLL_GETEV(&events
, i
);
1023 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1025 health_code_update();
1028 /* No activity for this FD (poll implementation). */
1032 /* Thread quit pipe has been closed. Killing thread. */
1033 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1039 /* Check for data on kernel pipe */
1040 if (revents
& LPOLLIN
) {
1041 if (pollfd
== kernel_poll_pipe
[0]) {
1042 (void) lttng_read(kernel_poll_pipe
[0],
1045 * Ret value is useless here, if this pipe gets any actions an
1046 * update is required anyway.
1048 update_poll_flag
= 1;
1052 * New CPU detected by the kernel. Adding kernel stream to
1053 * kernel session and updating the kernel consumer
1055 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1061 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1062 update_poll_flag
= 1;
1065 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1073 lttng_poll_clean(&events
);
1076 utils_close_pipe(kernel_poll_pipe
);
1077 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1080 ERR("Health error occurred in %s", __func__
);
1081 WARN("Kernel thread died unexpectedly. "
1082 "Kernel tracing can continue but CPU hotplug is disabled.");
1084 health_unregister(health_sessiond
);
1085 DBG("Kernel thread dying");
1090 * Signal pthread condition of the consumer data that the thread.
1092 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1094 pthread_mutex_lock(&data
->cond_mutex
);
1097 * The state is set before signaling. It can be any value, it's the waiter
1098 * job to correctly interpret this condition variable associated to the
1099 * consumer pthread_cond.
1101 * A value of 0 means that the corresponding thread of the consumer data
1102 * was not started. 1 indicates that the thread has started and is ready
1103 * for action. A negative value means that there was an error during the
1106 data
->consumer_thread_is_ready
= state
;
1107 (void) pthread_cond_signal(&data
->cond
);
1109 pthread_mutex_unlock(&data
->cond_mutex
);
1113 * This thread manage the consumer error sent back to the session daemon.
1115 static void *thread_manage_consumer(void *data
)
1117 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1118 uint32_t revents
, nb_fd
;
1119 enum lttcomm_return_code code
;
1120 struct lttng_poll_event events
;
1121 struct consumer_data
*consumer_data
= data
;
1122 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1124 DBG("[thread] Manage consumer started");
1126 rcu_register_thread();
1127 rcu_thread_online();
1129 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1131 health_code_update();
1134 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1135 * metadata_sock. Nothing more will be added to this poll set.
1137 ret
= sessiond_set_thread_pollset(&events
, 3);
1143 * The error socket here is already in a listening state which was done
1144 * just before spawning this thread to avoid a race between the consumer
1145 * daemon exec trying to connect and the listen() call.
1147 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1152 health_code_update();
1154 /* Infinite blocking call, waiting for transmission */
1156 health_poll_entry();
1158 if (testpoint(sessiond_thread_manage_consumer
)) {
1162 ret
= lttng_poll_wait(&events
, -1);
1166 * Restart interrupted system call.
1168 if (errno
== EINTR
) {
1176 for (i
= 0; i
< nb_fd
; i
++) {
1177 /* Fetch once the poll data */
1178 revents
= LTTNG_POLL_GETEV(&events
, i
);
1179 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1181 health_code_update();
1184 /* No activity for this FD (poll implementation). */
1188 /* Thread quit pipe has been closed. Killing thread. */
1189 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1195 /* Event on the registration socket */
1196 if (pollfd
== consumer_data
->err_sock
) {
1197 if (revents
& LPOLLIN
) {
1199 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1200 ERR("consumer err socket poll error");
1203 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1209 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1215 * Set the CLOEXEC flag. Return code is useless because either way, the
1218 (void) utils_set_fd_cloexec(sock
);
1220 health_code_update();
1222 DBG2("Receiving code from consumer err_sock");
1224 /* Getting status code from kconsumerd */
1225 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1226 sizeof(enum lttcomm_return_code
));
1231 health_code_update();
1232 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1233 ERR("consumer error when waiting for SOCK_READY : %s",
1234 lttcomm_get_readable_code(-code
));
1238 /* Connect both command and metadata sockets. */
1239 consumer_data
->cmd_sock
=
1240 lttcomm_connect_unix_sock(
1241 consumer_data
->cmd_unix_sock_path
);
1242 consumer_data
->metadata_fd
=
1243 lttcomm_connect_unix_sock(
1244 consumer_data
->cmd_unix_sock_path
);
1245 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1246 PERROR("consumer connect cmd socket");
1247 /* On error, signal condition and quit. */
1248 signal_consumer_condition(consumer_data
, -1);
1252 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1254 /* Create metadata socket lock. */
1255 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1256 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1257 PERROR("zmalloc pthread mutex");
1260 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1262 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1263 DBG("Consumer metadata socket ready (fd: %d)",
1264 consumer_data
->metadata_fd
);
1267 * Remove the consumerd error sock since we've established a connection.
1269 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1274 /* Add new accepted error socket. */
1275 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1280 /* Add metadata socket that is successfully connected. */
1281 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1282 LPOLLIN
| LPOLLRDHUP
);
1287 health_code_update();
1290 * Transfer the write-end of the channel monitoring and rotate pipe
1291 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1292 * SET_CHANNEL_ROTATE_PIPE commands.
1294 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1295 if (!cmd_socket_wrapper
) {
1298 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1300 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1301 consumer_data
->channel_monitor_pipe
);
1306 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1307 consumer_data
->channel_rotate_pipe
);
1312 /* Discard the socket wrapper as it is no longer needed. */
1313 consumer_destroy_socket(cmd_socket_wrapper
);
1314 cmd_socket_wrapper
= NULL
;
1316 /* The thread is completely initialized, signal that it is ready. */
1317 signal_consumer_condition(consumer_data
, 1);
1319 /* Infinite blocking call, waiting for transmission */
1322 health_code_update();
1324 /* Exit the thread because the thread quit pipe has been triggered. */
1326 /* Not a health error. */
1331 health_poll_entry();
1332 ret
= lttng_poll_wait(&events
, -1);
1336 * Restart interrupted system call.
1338 if (errno
== EINTR
) {
1346 for (i
= 0; i
< nb_fd
; i
++) {
1347 /* Fetch once the poll data */
1348 revents
= LTTNG_POLL_GETEV(&events
, i
);
1349 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1351 health_code_update();
1354 /* No activity for this FD (poll implementation). */
1359 * Thread quit pipe has been triggered, flag that we should stop
1360 * but continue the current loop to handle potential data from
1363 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1365 if (pollfd
== sock
) {
1366 /* Event on the consumerd socket */
1367 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1368 && !(revents
& LPOLLIN
)) {
1369 ERR("consumer err socket second poll error");
1372 health_code_update();
1373 /* Wait for any kconsumerd error */
1374 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1375 sizeof(enum lttcomm_return_code
));
1377 ERR("consumer closed the command socket");
1381 ERR("consumer return code : %s",
1382 lttcomm_get_readable_code(-code
));
1385 } else if (pollfd
== consumer_data
->metadata_fd
) {
1386 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1387 && !(revents
& LPOLLIN
)) {
1388 ERR("consumer err metadata socket second poll error");
1391 /* UST metadata requests */
1392 ret
= ust_consumer_metadata_request(
1393 &consumer_data
->metadata_sock
);
1395 ERR("Handling metadata request");
1399 /* No need for an else branch all FDs are tested prior. */
1401 health_code_update();
1407 * We lock here because we are about to close the sockets and some other
1408 * thread might be using them so get exclusive access which will abort all
1409 * other consumer command by other threads.
1411 pthread_mutex_lock(&consumer_data
->lock
);
1413 /* Immediately set the consumerd state to stopped */
1414 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1415 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1416 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1417 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1418 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1420 /* Code flow error... */
1424 if (consumer_data
->err_sock
>= 0) {
1425 ret
= close(consumer_data
->err_sock
);
1429 consumer_data
->err_sock
= -1;
1431 if (consumer_data
->cmd_sock
>= 0) {
1432 ret
= close(consumer_data
->cmd_sock
);
1436 consumer_data
->cmd_sock
= -1;
1438 if (consumer_data
->metadata_sock
.fd_ptr
&&
1439 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1440 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1452 unlink(consumer_data
->err_unix_sock_path
);
1453 unlink(consumer_data
->cmd_unix_sock_path
);
1454 pthread_mutex_unlock(&consumer_data
->lock
);
1456 /* Cleanup metadata socket mutex. */
1457 if (consumer_data
->metadata_sock
.lock
) {
1458 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1459 free(consumer_data
->metadata_sock
.lock
);
1461 lttng_poll_clean(&events
);
1463 if (cmd_socket_wrapper
) {
1464 consumer_destroy_socket(cmd_socket_wrapper
);
1469 ERR("Health error occurred in %s", __func__
);
1471 health_unregister(health_sessiond
);
1472 DBG("consumer thread cleanup completed");
1474 rcu_thread_offline();
1475 rcu_unregister_thread();
1481 * This thread receives application command sockets (FDs) on the
1482 * apps_cmd_pipe and waits (polls) on them until they are closed
1483 * or an error occurs.
1485 * At that point, it flushes the data (tracing and metadata) associated
1486 * with this application and tears down ust app sessions and other
1487 * associated data structures through ust_app_unregister().
1489 * Note that this thread never sends commands to the applications
1490 * through the command sockets; it merely listens for hang-ups
1491 * and errors on those sockets and cleans-up as they occur.
1493 static void *thread_manage_apps(void *data
)
1495 int i
, ret
, pollfd
, err
= -1;
1497 uint32_t revents
, nb_fd
;
1498 struct lttng_poll_event events
;
1500 DBG("[thread] Manage application started");
1502 rcu_register_thread();
1503 rcu_thread_online();
1505 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1507 if (testpoint(sessiond_thread_manage_apps
)) {
1508 goto error_testpoint
;
1511 health_code_update();
1513 ret
= sessiond_set_thread_pollset(&events
, 2);
1515 goto error_poll_create
;
1518 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1523 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1527 health_code_update();
1530 DBG("Apps thread polling");
1532 /* Inifinite blocking call, waiting for transmission */
1534 health_poll_entry();
1535 ret
= lttng_poll_wait(&events
, -1);
1536 DBG("Apps thread return from poll on %d fds",
1537 LTTNG_POLL_GETNB(&events
));
1541 * Restart interrupted system call.
1543 if (errno
== EINTR
) {
1551 for (i
= 0; i
< nb_fd
; i
++) {
1552 /* Fetch once the poll data */
1553 revents
= LTTNG_POLL_GETEV(&events
, i
);
1554 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1556 health_code_update();
1559 /* No activity for this FD (poll implementation). */
1563 /* Thread quit pipe has been closed. Killing thread. */
1564 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1570 /* Inspect the apps cmd pipe */
1571 if (pollfd
== apps_cmd_pipe
[0]) {
1572 if (revents
& LPOLLIN
) {
1576 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1577 if (size_ret
< sizeof(sock
)) {
1578 PERROR("read apps cmd pipe");
1582 health_code_update();
1585 * Since this is a command socket (write then read),
1586 * we only monitor the error events of the socket.
1588 ret
= lttng_poll_add(&events
, sock
,
1589 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1594 DBG("Apps with sock %d added to poll set", sock
);
1595 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1596 ERR("Apps command pipe error");
1599 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1604 * At this point, we know that a registered application made
1605 * the event at poll_wait.
1607 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1608 /* Removing from the poll set */
1609 ret
= lttng_poll_del(&events
, pollfd
);
1614 /* Socket closed on remote end. */
1615 ust_app_unregister(pollfd
);
1617 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1622 health_code_update();
1628 lttng_poll_clean(&events
);
1631 utils_close_pipe(apps_cmd_pipe
);
1632 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1635 * We don't clean the UST app hash table here since already registered
1636 * applications can still be controlled so let them be until the session
1637 * daemon dies or the applications stop.
1642 ERR("Health error occurred in %s", __func__
);
1644 health_unregister(health_sessiond
);
1645 DBG("Application communication apps thread cleanup complete");
1646 rcu_thread_offline();
1647 rcu_unregister_thread();
1652 * Send a socket to a thread This is called from the dispatch UST registration
1653 * thread once all sockets are set for the application.
1655 * The sock value can be invalid, we don't really care, the thread will handle
1656 * it and make the necessary cleanup if so.
1658 * On success, return 0 else a negative value being the errno message of the
1661 static int send_socket_to_thread(int fd
, int sock
)
1666 * It's possible that the FD is set as invalid with -1 concurrently just
1667 * before calling this function being a shutdown state of the thread.
1674 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1675 if (ret
< sizeof(sock
)) {
1676 PERROR("write apps pipe %d", fd
);
1683 /* All good. Don't send back the write positive ret value. */
1690 * Sanitize the wait queue of the dispatch registration thread meaning removing
1691 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1692 * notify socket is never received.
1694 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1696 int ret
, nb_fd
= 0, i
;
1697 unsigned int fd_added
= 0;
1698 struct lttng_poll_event events
;
1699 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1703 lttng_poll_init(&events
);
1705 /* Just skip everything for an empty queue. */
1706 if (!wait_queue
->count
) {
1710 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1715 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1716 &wait_queue
->head
, head
) {
1717 assert(wait_node
->app
);
1718 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1719 LPOLLHUP
| LPOLLERR
);
1732 * Poll but don't block so we can quickly identify the faulty events and
1733 * clean them afterwards from the wait queue.
1735 ret
= lttng_poll_wait(&events
, 0);
1741 for (i
= 0; i
< nb_fd
; i
++) {
1742 /* Get faulty FD. */
1743 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1744 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1747 /* No activity for this FD (poll implementation). */
1751 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1752 &wait_queue
->head
, head
) {
1753 if (pollfd
== wait_node
->app
->sock
&&
1754 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1755 cds_list_del(&wait_node
->head
);
1756 wait_queue
->count
--;
1757 ust_app_destroy(wait_node
->app
);
1760 * Silence warning of use-after-free in
1761 * cds_list_for_each_entry_safe which uses
1762 * __typeof__(*wait_node).
1767 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1774 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1778 lttng_poll_clean(&events
);
1782 lttng_poll_clean(&events
);
1784 ERR("Unable to sanitize wait queue");
1789 * Dispatch request from the registration threads to the application
1790 * communication thread.
1792 static void *thread_dispatch_ust_registration(void *data
)
1795 struct cds_wfcq_node
*node
;
1796 struct ust_command
*ust_cmd
= NULL
;
1797 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1798 struct ust_reg_wait_queue wait_queue
= {
1802 rcu_register_thread();
1804 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1806 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1807 goto error_testpoint
;
1810 health_code_update();
1812 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1814 DBG("[thread] Dispatch UST command started");
1817 health_code_update();
1819 /* Atomically prepare the queue futex */
1820 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1822 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1827 struct ust_app
*app
= NULL
;
1831 * Make sure we don't have node(s) that have hung up before receiving
1832 * the notify socket. This is to clean the list in order to avoid
1833 * memory leaks from notify socket that are never seen.
1835 sanitize_wait_queue(&wait_queue
);
1837 health_code_update();
1838 /* Dequeue command for registration */
1839 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1841 DBG("Woken up but nothing in the UST command queue");
1842 /* Continue thread execution */
1846 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1848 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1849 " gid:%d sock:%d name:%s (version %d.%d)",
1850 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1851 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1852 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1853 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1855 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1856 wait_node
= zmalloc(sizeof(*wait_node
));
1858 PERROR("zmalloc wait_node dispatch");
1859 ret
= close(ust_cmd
->sock
);
1861 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1863 lttng_fd_put(LTTNG_FD_APPS
, 1);
1867 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1869 /* Create application object if socket is CMD. */
1870 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1872 if (!wait_node
->app
) {
1873 ret
= close(ust_cmd
->sock
);
1875 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1877 lttng_fd_put(LTTNG_FD_APPS
, 1);
1883 * Add application to the wait queue so we can set the notify
1884 * socket before putting this object in the global ht.
1886 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1891 * We have to continue here since we don't have the notify
1892 * socket and the application MUST be added to the hash table
1893 * only at that moment.
1898 * Look for the application in the local wait queue and set the
1899 * notify socket if found.
1901 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1902 &wait_queue
.head
, head
) {
1903 health_code_update();
1904 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1905 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1906 cds_list_del(&wait_node
->head
);
1908 app
= wait_node
->app
;
1910 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1916 * With no application at this stage the received socket is
1917 * basically useless so close it before we free the cmd data
1918 * structure for good.
1921 ret
= close(ust_cmd
->sock
);
1923 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1925 lttng_fd_put(LTTNG_FD_APPS
, 1);
1932 * @session_lock_list
1934 * Lock the global session list so from the register up to the
1935 * registration done message, no thread can see the application
1936 * and change its state.
1938 session_lock_list();
1942 * Add application to the global hash table. This needs to be
1943 * done before the update to the UST registry can locate the
1948 /* Set app version. This call will print an error if needed. */
1949 (void) ust_app_version(app
);
1951 /* Send notify socket through the notify pipe. */
1952 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1956 session_unlock_list();
1958 * No notify thread, stop the UST tracing. However, this is
1959 * not an internal error of the this thread thus setting
1960 * the health error code to a normal exit.
1967 * Update newly registered application with the tracing
1968 * registry info already enabled information.
1970 update_ust_app(app
->sock
);
1973 * Don't care about return value. Let the manage apps threads
1974 * handle app unregistration upon socket close.
1976 (void) ust_app_register_done(app
);
1979 * Even if the application socket has been closed, send the app
1980 * to the thread and unregistration will take place at that
1983 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1986 session_unlock_list();
1988 * No apps. thread, stop the UST tracing. However, this is
1989 * not an internal error of the this thread thus setting
1990 * the health error code to a normal exit.
1997 session_unlock_list();
1999 } while (node
!= NULL
);
2001 health_poll_entry();
2002 /* Futex wait on queue. Blocking call on futex() */
2003 futex_nto1_wait(&ust_cmd_queue
.futex
);
2006 /* Normal exit, no error */
2010 /* Clean up wait queue. */
2011 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2012 &wait_queue
.head
, head
) {
2013 cds_list_del(&wait_node
->head
);
2018 /* Empty command queue. */
2020 /* Dequeue command for registration */
2021 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2025 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2026 ret
= close(ust_cmd
->sock
);
2028 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2030 lttng_fd_put(LTTNG_FD_APPS
, 1);
2035 DBG("Dispatch thread dying");
2038 ERR("Health error occurred in %s", __func__
);
2040 health_unregister(health_sessiond
);
2041 rcu_unregister_thread();
2046 * This thread manage application registration.
2048 static void *thread_registration_apps(void *data
)
2050 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2051 uint32_t revents
, nb_fd
;
2052 struct lttng_poll_event events
;
2054 * Get allocated in this thread, enqueued to a global queue, dequeued and
2055 * freed in the manage apps thread.
2057 struct ust_command
*ust_cmd
= NULL
;
2059 DBG("[thread] Manage application registration started");
2061 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2063 if (testpoint(sessiond_thread_registration_apps
)) {
2064 goto error_testpoint
;
2067 ret
= lttcomm_listen_unix_sock(apps_sock
);
2073 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2074 * more will be added to this poll set.
2076 ret
= sessiond_set_thread_pollset(&events
, 2);
2078 goto error_create_poll
;
2081 /* Add the application registration socket */
2082 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2084 goto error_poll_add
;
2087 /* Notify all applications to register */
2088 ret
= notify_ust_apps(1);
2090 ERR("Failed to notify applications or create the wait shared memory.\n"
2091 "Execution continues but there might be problem for already\n"
2092 "running applications that wishes to register.");
2096 DBG("Accepting application registration");
2098 /* Inifinite blocking call, waiting for transmission */
2100 health_poll_entry();
2101 ret
= lttng_poll_wait(&events
, -1);
2105 * Restart interrupted system call.
2107 if (errno
== EINTR
) {
2115 for (i
= 0; i
< nb_fd
; i
++) {
2116 health_code_update();
2118 /* Fetch once the poll data */
2119 revents
= LTTNG_POLL_GETEV(&events
, i
);
2120 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2123 /* No activity for this FD (poll implementation). */
2127 /* Thread quit pipe has been closed. Killing thread. */
2128 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2134 /* Event on the registration socket */
2135 if (pollfd
== apps_sock
) {
2136 if (revents
& LPOLLIN
) {
2137 sock
= lttcomm_accept_unix_sock(apps_sock
);
2143 * Set socket timeout for both receiving and ending.
2144 * app_socket_timeout is in seconds, whereas
2145 * lttcomm_setsockopt_rcv_timeout and
2146 * lttcomm_setsockopt_snd_timeout expect msec as
2149 if (config
.app_socket_timeout
>= 0) {
2150 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2151 config
.app_socket_timeout
* 1000);
2152 (void) lttcomm_setsockopt_snd_timeout(sock
,
2153 config
.app_socket_timeout
* 1000);
2157 * Set the CLOEXEC flag. Return code is useless because
2158 * either way, the show must go on.
2160 (void) utils_set_fd_cloexec(sock
);
2162 /* Create UST registration command for enqueuing */
2163 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2164 if (ust_cmd
== NULL
) {
2165 PERROR("ust command zmalloc");
2174 * Using message-based transmissions to ensure we don't
2175 * have to deal with partially received messages.
2177 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2179 ERR("Exhausted file descriptors allowed for applications.");
2189 health_code_update();
2190 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2193 /* Close socket of the application. */
2198 lttng_fd_put(LTTNG_FD_APPS
, 1);
2202 health_code_update();
2204 ust_cmd
->sock
= sock
;
2207 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2208 " gid:%d sock:%d name:%s (version %d.%d)",
2209 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2210 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2211 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2212 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2215 * Lock free enqueue the registration request. The red pill
2216 * has been taken! This apps will be part of the *system*.
2218 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2221 * Wake the registration queue futex. Implicit memory
2222 * barrier with the exchange in cds_wfcq_enqueue.
2224 futex_nto1_wake(&ust_cmd_queue
.futex
);
2225 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2226 ERR("Register apps socket poll error");
2229 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2238 /* Notify that the registration thread is gone */
2241 if (apps_sock
>= 0) {
2242 ret
= close(apps_sock
);
2252 lttng_fd_put(LTTNG_FD_APPS
, 1);
2254 unlink(config
.apps_unix_sock_path
.value
);
2257 lttng_poll_clean(&events
);
2261 DBG("UST Registration thread cleanup complete");
2264 ERR("Health error occurred in %s", __func__
);
2266 health_unregister(health_sessiond
);
2272 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2273 * exec or it will fails.
2275 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2278 struct timespec timeout
;
2281 * Make sure we set the readiness flag to 0 because we are NOT ready.
2282 * This access to consumer_thread_is_ready does not need to be
2283 * protected by consumer_data.cond_mutex (yet) since the consumer
2284 * management thread has not been started at this point.
2286 consumer_data
->consumer_thread_is_ready
= 0;
2288 /* Setup pthread condition */
2289 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2292 PERROR("pthread_condattr_init consumer data");
2297 * Set the monotonic clock in order to make sure we DO NOT jump in time
2298 * between the clock_gettime() call and the timedwait call. See bug #324
2299 * for a more details and how we noticed it.
2301 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2304 PERROR("pthread_condattr_setclock consumer data");
2308 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2311 PERROR("pthread_cond_init consumer data");
2315 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2316 thread_manage_consumer
, consumer_data
);
2319 PERROR("pthread_create consumer");
2324 /* We are about to wait on a pthread condition */
2325 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2327 /* Get time for sem_timedwait absolute timeout */
2328 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2330 * Set the timeout for the condition timed wait even if the clock gettime
2331 * call fails since we might loop on that call and we want to avoid to
2332 * increment the timeout too many times.
2334 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2337 * The following loop COULD be skipped in some conditions so this is why we
2338 * set ret to 0 in order to make sure at least one round of the loop is
2344 * Loop until the condition is reached or when a timeout is reached. Note
2345 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2346 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2347 * possible. This loop does not take any chances and works with both of
2350 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2351 if (clock_ret
< 0) {
2352 PERROR("clock_gettime spawn consumer");
2353 /* Infinite wait for the consumerd thread to be ready */
2354 ret
= pthread_cond_wait(&consumer_data
->cond
,
2355 &consumer_data
->cond_mutex
);
2357 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2358 &consumer_data
->cond_mutex
, &timeout
);
2362 /* Release the pthread condition */
2363 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2367 if (ret
== ETIMEDOUT
) {
2371 * Call has timed out so we kill the kconsumerd_thread and return
2374 ERR("Condition timed out. The consumer thread was never ready."
2376 pth_ret
= pthread_cancel(consumer_data
->thread
);
2378 PERROR("pthread_cancel consumer thread");
2381 PERROR("pthread_cond_wait failed consumer thread");
2383 /* Caller is expecting a negative value on failure. */
2388 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2389 if (consumer_data
->pid
== 0) {
2390 ERR("Consumerd did not start");
2391 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2394 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2403 * Join consumer thread
2405 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2409 /* Consumer pid must be a real one. */
2410 if (consumer_data
->pid
> 0) {
2412 ret
= kill(consumer_data
->pid
, SIGTERM
);
2414 PERROR("Error killing consumer daemon");
2417 return pthread_join(consumer_data
->thread
, &status
);
2424 * Fork and exec a consumer daemon (consumerd).
2426 * Return pid if successful else -1.
2428 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2432 const char *consumer_to_use
;
2433 const char *verbosity
;
2436 DBG("Spawning consumerd");
2443 if (config
.verbose_consumer
) {
2444 verbosity
= "--verbose";
2445 } else if (lttng_opt_quiet
) {
2446 verbosity
= "--quiet";
2451 switch (consumer_data
->type
) {
2452 case LTTNG_CONSUMER_KERNEL
:
2454 * Find out which consumerd to execute. We will first try the
2455 * 64-bit path, then the sessiond's installation directory, and
2456 * fallback on the 32-bit one,
2458 DBG3("Looking for a kernel consumer at these locations:");
2459 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2460 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2461 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2462 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2463 DBG3("Found location #1");
2464 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2465 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2466 DBG3("Found location #2");
2467 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2468 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2469 DBG3("Found location #3");
2470 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2472 DBG("Could not find any valid consumerd executable");
2476 DBG("Using kernel consumer at: %s", consumer_to_use
);
2477 (void) execl(consumer_to_use
,
2478 "lttng-consumerd", verbosity
, "-k",
2479 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2480 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2481 "--group", config
.tracing_group_name
.value
,
2484 case LTTNG_CONSUMER64_UST
:
2486 if (config
.consumerd64_lib_dir
.value
) {
2491 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2495 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2496 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2501 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2502 if (tmp
[0] != '\0') {
2503 strcat(tmpnew
, ":");
2504 strcat(tmpnew
, tmp
);
2506 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2513 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2514 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2515 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2516 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2517 "--group", config
.tracing_group_name
.value
,
2521 case LTTNG_CONSUMER32_UST
:
2523 if (config
.consumerd32_lib_dir
.value
) {
2528 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2532 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2533 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2538 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2539 if (tmp
[0] != '\0') {
2540 strcat(tmpnew
, ":");
2541 strcat(tmpnew
, tmp
);
2543 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2550 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2551 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2552 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2553 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2554 "--group", config
.tracing_group_name
.value
,
2559 ERR("unknown consumer type");
2563 PERROR("Consumer execl()");
2565 /* Reaching this point, we got a failure on our execl(). */
2567 } else if (pid
> 0) {
2570 PERROR("start consumer fork");
2578 * Spawn the consumerd daemon and session daemon thread.
2580 static int start_consumerd(struct consumer_data
*consumer_data
)
2585 * Set the listen() state on the socket since there is a possible race
2586 * between the exec() of the consumer daemon and this call if place in the
2587 * consumer thread. See bug #366 for more details.
2589 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2594 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2595 if (consumer_data
->pid
!= 0) {
2596 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2600 ret
= spawn_consumerd(consumer_data
);
2602 ERR("Spawning consumerd failed");
2603 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2607 /* Setting up the consumer_data pid */
2608 consumer_data
->pid
= ret
;
2609 DBG2("Consumer pid %d", consumer_data
->pid
);
2610 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2612 DBG2("Spawning consumer control thread");
2613 ret
= spawn_consumer_thread(consumer_data
);
2615 ERR("Fatal error spawning consumer control thread");
2623 /* Cleanup already created sockets on error. */
2624 if (consumer_data
->err_sock
>= 0) {
2627 err
= close(consumer_data
->err_sock
);
2629 PERROR("close consumer data error socket");
2636 * Setup necessary data for kernel tracer action.
2638 static int init_kernel_tracer(void)
2642 /* Modprobe lttng kernel modules */
2643 ret
= modprobe_lttng_control();
2648 /* Open debugfs lttng */
2649 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2650 if (kernel_tracer_fd
< 0) {
2651 DBG("Failed to open %s", module_proc_lttng
);
2655 /* Validate kernel version */
2656 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2657 &kernel_tracer_abi_version
);
2662 ret
= modprobe_lttng_data();
2667 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2674 WARN("Kernel tracer does not support buffer monitoring. "
2675 "The monitoring timer of channels in the kernel domain "
2676 "will be set to 0 (disabled).");
2679 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2683 modprobe_remove_lttng_control();
2684 ret
= close(kernel_tracer_fd
);
2688 kernel_tracer_fd
= -1;
2689 return LTTNG_ERR_KERN_VERSION
;
2692 ret
= close(kernel_tracer_fd
);
2698 modprobe_remove_lttng_control();
2701 WARN("No kernel tracer available");
2702 kernel_tracer_fd
= -1;
2704 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2706 return LTTNG_ERR_KERN_NA
;
2712 * Copy consumer output from the tracing session to the domain session. The
2713 * function also applies the right modification on a per domain basis for the
2714 * trace files destination directory.
2716 * Should *NOT* be called with RCU read-side lock held.
2718 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2721 const char *dir_name
;
2722 struct consumer_output
*consumer
;
2725 assert(session
->consumer
);
2728 case LTTNG_DOMAIN_KERNEL
:
2729 DBG3("Copying tracing session consumer output in kernel session");
2731 * XXX: We should audit the session creation and what this function
2732 * does "extra" in order to avoid a destroy since this function is used
2733 * in the domain session creation (kernel and ust) only. Same for UST
2736 if (session
->kernel_session
->consumer
) {
2737 consumer_output_put(session
->kernel_session
->consumer
);
2739 session
->kernel_session
->consumer
=
2740 consumer_copy_output(session
->consumer
);
2741 /* Ease our life a bit for the next part */
2742 consumer
= session
->kernel_session
->consumer
;
2743 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2745 case LTTNG_DOMAIN_JUL
:
2746 case LTTNG_DOMAIN_LOG4J
:
2747 case LTTNG_DOMAIN_PYTHON
:
2748 case LTTNG_DOMAIN_UST
:
2749 DBG3("Copying tracing session consumer output in UST session");
2750 if (session
->ust_session
->consumer
) {
2751 consumer_output_put(session
->ust_session
->consumer
);
2753 session
->ust_session
->consumer
=
2754 consumer_copy_output(session
->consumer
);
2755 /* Ease our life a bit for the next part */
2756 consumer
= session
->ust_session
->consumer
;
2757 dir_name
= DEFAULT_UST_TRACE_DIR
;
2760 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2764 /* Append correct directory to subdir */
2765 strncat(consumer
->subdir
, dir_name
,
2766 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2767 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2776 * Create an UST session and add it to the session ust list.
2778 * Should *NOT* be called with RCU read-side lock held.
2780 static int create_ust_session(struct ltt_session
*session
,
2781 struct lttng_domain
*domain
)
2784 struct ltt_ust_session
*lus
= NULL
;
2788 assert(session
->consumer
);
2790 switch (domain
->type
) {
2791 case LTTNG_DOMAIN_JUL
:
2792 case LTTNG_DOMAIN_LOG4J
:
2793 case LTTNG_DOMAIN_PYTHON
:
2794 case LTTNG_DOMAIN_UST
:
2797 ERR("Unknown UST domain on create session %d", domain
->type
);
2798 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2802 DBG("Creating UST session");
2804 lus
= trace_ust_create_session(session
->id
);
2806 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2810 lus
->uid
= session
->uid
;
2811 lus
->gid
= session
->gid
;
2812 lus
->output_traces
= session
->output_traces
;
2813 lus
->snapshot_mode
= session
->snapshot_mode
;
2814 lus
->live_timer_interval
= session
->live_timer
;
2815 session
->ust_session
= lus
;
2816 if (session
->shm_path
[0]) {
2817 strncpy(lus
->root_shm_path
, session
->shm_path
,
2818 sizeof(lus
->root_shm_path
));
2819 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2820 strncpy(lus
->shm_path
, session
->shm_path
,
2821 sizeof(lus
->shm_path
));
2822 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2823 strncat(lus
->shm_path
, "/ust",
2824 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2826 /* Copy session output to the newly created UST session */
2827 ret
= copy_session_consumer(domain
->type
, session
);
2828 if (ret
!= LTTNG_OK
) {
2836 session
->ust_session
= NULL
;
2841 * Create a kernel tracer session then create the default channel.
2843 static int create_kernel_session(struct ltt_session
*session
)
2847 DBG("Creating kernel session");
2849 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2851 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2855 /* Code flow safety */
2856 assert(session
->kernel_session
);
2858 /* Copy session output to the newly created Kernel session */
2859 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2860 if (ret
!= LTTNG_OK
) {
2864 session
->kernel_session
->uid
= session
->uid
;
2865 session
->kernel_session
->gid
= session
->gid
;
2866 session
->kernel_session
->output_traces
= session
->output_traces
;
2867 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2872 trace_kernel_destroy_session(session
->kernel_session
);
2873 session
->kernel_session
= NULL
;
2878 * Count number of session permitted by uid/gid.
2880 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2883 struct ltt_session
*session
;
2885 DBG("Counting number of available session for UID %d GID %d",
2887 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2889 * Only list the sessions the user can control.
2891 if (!session_access_ok(session
, uid
, gid
)) {
2900 * Process the command requested by the lttng client within the command
2901 * context structure. This function make sure that the return structure (llm)
2902 * is set and ready for transmission before returning.
2904 * Return any error encountered or 0 for success.
2906 * "sock" is only used for special-case var. len data.
2908 * Should *NOT* be called with RCU read-side lock held.
2910 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2914 int need_tracing_session
= 1;
2917 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2919 assert(!rcu_read_ongoing());
2923 switch (cmd_ctx
->lsm
->cmd_type
) {
2924 case LTTNG_CREATE_SESSION
:
2925 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2926 case LTTNG_CREATE_SESSION_LIVE
:
2927 case LTTNG_DESTROY_SESSION
:
2928 case LTTNG_LIST_SESSIONS
:
2929 case LTTNG_LIST_DOMAINS
:
2930 case LTTNG_START_TRACE
:
2931 case LTTNG_STOP_TRACE
:
2932 case LTTNG_DATA_PENDING
:
2933 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2934 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2935 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2936 case LTTNG_SNAPSHOT_RECORD
:
2937 case LTTNG_SAVE_SESSION
:
2938 case LTTNG_SET_SESSION_SHM_PATH
:
2939 case LTTNG_REGENERATE_METADATA
:
2940 case LTTNG_REGENERATE_STATEDUMP
:
2941 case LTTNG_REGISTER_TRIGGER
:
2942 case LTTNG_UNREGISTER_TRIGGER
:
2949 if (config
.no_kernel
&& need_domain
2950 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2952 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2954 ret
= LTTNG_ERR_KERN_NA
;
2959 /* Deny register consumer if we already have a spawned consumer. */
2960 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2961 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2962 if (kconsumer_data
.pid
> 0) {
2963 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2964 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2967 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2971 * Check for command that don't needs to allocate a returned payload. We do
2972 * this here so we don't have to make the call for no payload at each
2975 switch(cmd_ctx
->lsm
->cmd_type
) {
2976 case LTTNG_LIST_SESSIONS
:
2977 case LTTNG_LIST_TRACEPOINTS
:
2978 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2979 case LTTNG_LIST_DOMAINS
:
2980 case LTTNG_LIST_CHANNELS
:
2981 case LTTNG_LIST_EVENTS
:
2982 case LTTNG_LIST_SYSCALLS
:
2983 case LTTNG_LIST_TRACKER_PIDS
:
2984 case LTTNG_DATA_PENDING
:
2987 /* Setup lttng message with no payload */
2988 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2990 /* This label does not try to unlock the session */
2991 goto init_setup_error
;
2995 /* Commands that DO NOT need a session. */
2996 switch (cmd_ctx
->lsm
->cmd_type
) {
2997 case LTTNG_CREATE_SESSION
:
2998 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2999 case LTTNG_CREATE_SESSION_LIVE
:
3000 case LTTNG_LIST_SESSIONS
:
3001 case LTTNG_LIST_TRACEPOINTS
:
3002 case LTTNG_LIST_SYSCALLS
:
3003 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3004 case LTTNG_SAVE_SESSION
:
3005 case LTTNG_REGISTER_TRIGGER
:
3006 case LTTNG_UNREGISTER_TRIGGER
:
3007 need_tracing_session
= 0;
3010 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3012 * We keep the session list lock across _all_ commands
3013 * for now, because the per-session lock does not
3014 * handle teardown properly.
3016 session_lock_list();
3017 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3018 if (cmd_ctx
->session
== NULL
) {
3019 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3022 /* Acquire lock for the session */
3023 session_lock(cmd_ctx
->session
);
3029 * Commands that need a valid session but should NOT create one if none
3030 * exists. Instead of creating one and destroying it when the command is
3031 * handled, process that right before so we save some round trip in useless
3034 switch (cmd_ctx
->lsm
->cmd_type
) {
3035 case LTTNG_DISABLE_CHANNEL
:
3036 case LTTNG_DISABLE_EVENT
:
3037 switch (cmd_ctx
->lsm
->domain
.type
) {
3038 case LTTNG_DOMAIN_KERNEL
:
3039 if (!cmd_ctx
->session
->kernel_session
) {
3040 ret
= LTTNG_ERR_NO_CHANNEL
;
3044 case LTTNG_DOMAIN_JUL
:
3045 case LTTNG_DOMAIN_LOG4J
:
3046 case LTTNG_DOMAIN_PYTHON
:
3047 case LTTNG_DOMAIN_UST
:
3048 if (!cmd_ctx
->session
->ust_session
) {
3049 ret
= LTTNG_ERR_NO_CHANNEL
;
3054 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3066 * Check domain type for specific "pre-action".
3068 switch (cmd_ctx
->lsm
->domain
.type
) {
3069 case LTTNG_DOMAIN_KERNEL
:
3071 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3075 /* Kernel tracer check */
3076 if (kernel_tracer_fd
== -1) {
3077 /* Basically, load kernel tracer modules */
3078 ret
= init_kernel_tracer();
3084 /* Consumer is in an ERROR state. Report back to client */
3085 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3086 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3090 /* Need a session for kernel command */
3091 if (need_tracing_session
) {
3092 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3093 ret
= create_kernel_session(cmd_ctx
->session
);
3095 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3100 /* Start the kernel consumer daemon */
3101 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3102 if (kconsumer_data
.pid
== 0 &&
3103 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3104 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3105 ret
= start_consumerd(&kconsumer_data
);
3107 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3110 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3112 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3116 * The consumer was just spawned so we need to add the socket to
3117 * the consumer output of the session if exist.
3119 ret
= consumer_create_socket(&kconsumer_data
,
3120 cmd_ctx
->session
->kernel_session
->consumer
);
3127 case LTTNG_DOMAIN_JUL
:
3128 case LTTNG_DOMAIN_LOG4J
:
3129 case LTTNG_DOMAIN_PYTHON
:
3130 case LTTNG_DOMAIN_UST
:
3132 if (!ust_app_supported()) {
3133 ret
= LTTNG_ERR_NO_UST
;
3136 /* Consumer is in an ERROR state. Report back to client */
3137 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3138 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3142 if (need_tracing_session
) {
3143 /* Create UST session if none exist. */
3144 if (cmd_ctx
->session
->ust_session
== NULL
) {
3145 ret
= create_ust_session(cmd_ctx
->session
,
3146 &cmd_ctx
->lsm
->domain
);
3147 if (ret
!= LTTNG_OK
) {
3152 /* Start the UST consumer daemons */
3154 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3155 if (config
.consumerd64_bin_path
.value
&&
3156 ustconsumer64_data
.pid
== 0 &&
3157 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3158 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3159 ret
= start_consumerd(&ustconsumer64_data
);
3161 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3162 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3166 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3167 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3169 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3173 * Setup socket for consumer 64 bit. No need for atomic access
3174 * since it was set above and can ONLY be set in this thread.
3176 ret
= consumer_create_socket(&ustconsumer64_data
,
3177 cmd_ctx
->session
->ust_session
->consumer
);
3183 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3184 if (config
.consumerd32_bin_path
.value
&&
3185 ustconsumer32_data
.pid
== 0 &&
3186 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3187 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3188 ret
= start_consumerd(&ustconsumer32_data
);
3190 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3191 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3195 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3196 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3198 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3202 * Setup socket for consumer 64 bit. No need for atomic access
3203 * since it was set above and can ONLY be set in this thread.
3205 ret
= consumer_create_socket(&ustconsumer32_data
,
3206 cmd_ctx
->session
->ust_session
->consumer
);
3218 /* Validate consumer daemon state when start/stop trace command */
3219 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3220 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3221 switch (cmd_ctx
->lsm
->domain
.type
) {
3222 case LTTNG_DOMAIN_NONE
:
3224 case LTTNG_DOMAIN_JUL
:
3225 case LTTNG_DOMAIN_LOG4J
:
3226 case LTTNG_DOMAIN_PYTHON
:
3227 case LTTNG_DOMAIN_UST
:
3228 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3229 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3233 case LTTNG_DOMAIN_KERNEL
:
3234 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3235 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3240 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3246 * Check that the UID or GID match that of the tracing session.
3247 * The root user can interact with all sessions.
3249 if (need_tracing_session
) {
3250 if (!session_access_ok(cmd_ctx
->session
,
3251 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3252 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3253 ret
= LTTNG_ERR_EPERM
;
3259 * Send relayd information to consumer as soon as we have a domain and a
3262 if (cmd_ctx
->session
&& need_domain
) {
3264 * Setup relayd if not done yet. If the relayd information was already
3265 * sent to the consumer, this call will gracefully return.
3267 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3268 if (ret
!= LTTNG_OK
) {
3273 /* Process by command type */
3274 switch (cmd_ctx
->lsm
->cmd_type
) {
3275 case LTTNG_ADD_CONTEXT
:
3278 * An LTTNG_ADD_CONTEXT command might have a supplementary
3279 * payload if the context being added is an application context.
3281 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3282 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3283 char *provider_name
= NULL
, *context_name
= NULL
;
3284 size_t provider_name_len
=
3285 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3286 size_t context_name_len
=
3287 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3289 if (provider_name_len
== 0 || context_name_len
== 0) {
3291 * Application provider and context names MUST
3294 ret
= -LTTNG_ERR_INVALID
;
3298 provider_name
= zmalloc(provider_name_len
+ 1);
3299 if (!provider_name
) {
3300 ret
= -LTTNG_ERR_NOMEM
;
3303 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3306 context_name
= zmalloc(context_name_len
+ 1);
3307 if (!context_name
) {
3308 ret
= -LTTNG_ERR_NOMEM
;
3309 goto error_add_context
;
3311 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3314 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3317 goto error_add_context
;
3320 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3323 goto error_add_context
;
3328 * cmd_add_context assumes ownership of the provider and context
3331 ret
= cmd_add_context(cmd_ctx
->session
,
3332 cmd_ctx
->lsm
->domain
.type
,
3333 cmd_ctx
->lsm
->u
.context
.channel_name
,
3334 &cmd_ctx
->lsm
->u
.context
.ctx
,
3335 kernel_poll_pipe
[1]);
3337 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3338 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3340 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3341 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3347 case LTTNG_DISABLE_CHANNEL
:
3349 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3350 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3353 case LTTNG_DISABLE_EVENT
:
3357 * FIXME: handle filter; for now we just receive the filter's
3358 * bytecode along with the filter expression which are sent by
3359 * liblttng-ctl and discard them.
3361 * This fixes an issue where the client may block while sending
3362 * the filter payload and encounter an error because the session
3363 * daemon closes the socket without ever handling this data.
3365 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3366 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3369 char data
[LTTNG_FILTER_MAX_LEN
];
3371 DBG("Discarding disable event command payload of size %zu", count
);
3373 ret
= lttcomm_recv_unix_sock(sock
, data
,
3374 count
> sizeof(data
) ? sizeof(data
) : count
);
3379 count
-= (size_t) ret
;
3382 /* FIXME: passing packed structure to non-packed pointer */
3383 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3384 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3385 &cmd_ctx
->lsm
->u
.disable
.event
);
3388 case LTTNG_ENABLE_CHANNEL
:
3390 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3391 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3392 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3393 &cmd_ctx
->lsm
->u
.channel
.chan
,
3394 kernel_poll_pipe
[1]);
3397 case LTTNG_TRACK_PID
:
3399 ret
= cmd_track_pid(cmd_ctx
->session
,
3400 cmd_ctx
->lsm
->domain
.type
,
3401 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3404 case LTTNG_UNTRACK_PID
:
3406 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3407 cmd_ctx
->lsm
->domain
.type
,
3408 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3411 case LTTNG_ENABLE_EVENT
:
3413 struct lttng_event_exclusion
*exclusion
= NULL
;
3414 struct lttng_filter_bytecode
*bytecode
= NULL
;
3415 char *filter_expression
= NULL
;
3417 /* Handle exclusion events and receive it from the client. */
3418 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3419 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3421 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3422 (count
* LTTNG_SYMBOL_NAME_LEN
));
3424 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3428 DBG("Receiving var len exclusion event list from client ...");
3429 exclusion
->count
= count
;
3430 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3431 count
* LTTNG_SYMBOL_NAME_LEN
);
3433 DBG("Nothing recv() from client var len data... continuing");
3436 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3441 /* Get filter expression from client. */
3442 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3443 size_t expression_len
=
3444 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3446 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3447 ret
= LTTNG_ERR_FILTER_INVAL
;
3452 filter_expression
= zmalloc(expression_len
);
3453 if (!filter_expression
) {
3455 ret
= LTTNG_ERR_FILTER_NOMEM
;
3459 /* Receive var. len. data */
3460 DBG("Receiving var len filter's expression from client ...");
3461 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3464 DBG("Nothing recv() from client car len data... continuing");
3466 free(filter_expression
);
3468 ret
= LTTNG_ERR_FILTER_INVAL
;
3473 /* Handle filter and get bytecode from client. */
3474 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3475 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3477 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3478 ret
= LTTNG_ERR_FILTER_INVAL
;
3479 free(filter_expression
);
3484 bytecode
= zmalloc(bytecode_len
);
3486 free(filter_expression
);
3488 ret
= LTTNG_ERR_FILTER_NOMEM
;
3492 /* Receive var. len. data */
3493 DBG("Receiving var len filter's bytecode from client ...");
3494 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3496 DBG("Nothing recv() from client car len data... continuing");
3498 free(filter_expression
);
3501 ret
= LTTNG_ERR_FILTER_INVAL
;
3505 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3506 free(filter_expression
);
3509 ret
= LTTNG_ERR_FILTER_INVAL
;
3514 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3515 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3516 &cmd_ctx
->lsm
->u
.enable
.event
,
3517 filter_expression
, bytecode
, exclusion
,
3518 kernel_poll_pipe
[1]);
3521 case LTTNG_LIST_TRACEPOINTS
:
3523 struct lttng_event
*events
;
3526 session_lock_list();
3527 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3528 session_unlock_list();
3529 if (nb_events
< 0) {
3530 /* Return value is a negative lttng_error_code. */
3536 * Setup lttng message with payload size set to the event list size in
3537 * bytes and then copy list into the llm payload.
3539 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3540 sizeof(struct lttng_event
) * nb_events
);
3550 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3552 struct lttng_event_field
*fields
;
3555 session_lock_list();
3556 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3558 session_unlock_list();
3559 if (nb_fields
< 0) {
3560 /* Return value is a negative lttng_error_code. */
3566 * Setup lttng message with payload size set to the event list size in
3567 * bytes and then copy list into the llm payload.
3569 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3570 sizeof(struct lttng_event_field
) * nb_fields
);
3580 case LTTNG_LIST_SYSCALLS
:
3582 struct lttng_event
*events
;
3585 nb_events
= cmd_list_syscalls(&events
);
3586 if (nb_events
< 0) {
3587 /* Return value is a negative lttng_error_code. */
3593 * Setup lttng message with payload size set to the event list size in
3594 * bytes and then copy list into the llm payload.
3596 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3597 sizeof(struct lttng_event
) * nb_events
);
3607 case LTTNG_LIST_TRACKER_PIDS
:
3609 int32_t *pids
= NULL
;
3612 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3613 cmd_ctx
->lsm
->domain
.type
, &pids
);
3615 /* Return value is a negative lttng_error_code. */
3621 * Setup lttng message with payload size set to the event list size in
3622 * bytes and then copy list into the llm payload.
3624 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3625 sizeof(int32_t) * nr_pids
);
3635 case LTTNG_SET_CONSUMER_URI
:
3638 struct lttng_uri
*uris
;
3640 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3641 len
= nb_uri
* sizeof(struct lttng_uri
);
3644 ret
= LTTNG_ERR_INVALID
;
3648 uris
= zmalloc(len
);
3650 ret
= LTTNG_ERR_FATAL
;
3654 /* Receive variable len data */
3655 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3656 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3658 DBG("No URIs received from client... continuing");
3660 ret
= LTTNG_ERR_SESSION_FAIL
;
3665 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3667 if (ret
!= LTTNG_OK
) {
3674 case LTTNG_START_TRACE
:
3676 ret
= cmd_start_trace(cmd_ctx
->session
);
3679 case LTTNG_STOP_TRACE
:
3681 ret
= cmd_stop_trace(cmd_ctx
->session
);
3684 case LTTNG_CREATE_SESSION
:
3687 struct lttng_uri
*uris
= NULL
;
3689 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3690 len
= nb_uri
* sizeof(struct lttng_uri
);
3693 uris
= zmalloc(len
);
3695 ret
= LTTNG_ERR_FATAL
;
3699 /* Receive variable len data */
3700 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3701 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3703 DBG("No URIs received from client... continuing");
3705 ret
= LTTNG_ERR_SESSION_FAIL
;
3710 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3711 DBG("Creating session with ONE network URI is a bad call");
3712 ret
= LTTNG_ERR_SESSION_FAIL
;
3718 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3719 &cmd_ctx
->creds
, 0);
3725 case LTTNG_DESTROY_SESSION
:
3727 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3729 /* Set session to NULL so we do not unlock it after free. */
3730 cmd_ctx
->session
= NULL
;
3733 case LTTNG_LIST_DOMAINS
:
3736 struct lttng_domain
*domains
= NULL
;
3738 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3740 /* Return value is a negative lttng_error_code. */
3745 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3746 nb_dom
* sizeof(struct lttng_domain
));
3756 case LTTNG_LIST_CHANNELS
:
3758 ssize_t payload_size
;
3759 struct lttng_channel
*channels
= NULL
;
3761 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3762 cmd_ctx
->session
, &channels
);
3763 if (payload_size
< 0) {
3764 /* Return value is a negative lttng_error_code. */
3765 ret
= -payload_size
;
3769 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3780 case LTTNG_LIST_EVENTS
:
3783 struct lttng_event
*events
= NULL
;
3784 struct lttcomm_event_command_header cmd_header
;
3787 memset(&cmd_header
, 0, sizeof(cmd_header
));
3788 /* Extended infos are included at the end of events */
3789 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3790 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3791 &events
, &total_size
);
3794 /* Return value is a negative lttng_error_code. */
3799 cmd_header
.nb_events
= nb_event
;
3800 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3801 &cmd_header
, sizeof(cmd_header
));
3811 case LTTNG_LIST_SESSIONS
:
3813 unsigned int nr_sessions
;
3814 void *sessions_payload
;
3817 session_lock_list();
3818 nr_sessions
= lttng_sessions_count(
3819 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3820 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3821 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3822 sessions_payload
= zmalloc(payload_len
);
3824 if (!sessions_payload
) {
3825 session_unlock_list();
3830 cmd_list_lttng_sessions(sessions_payload
,
3831 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3832 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3833 session_unlock_list();
3835 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3837 free(sessions_payload
);
3846 case LTTNG_REGISTER_CONSUMER
:
3848 struct consumer_data
*cdata
;
3850 switch (cmd_ctx
->lsm
->domain
.type
) {
3851 case LTTNG_DOMAIN_KERNEL
:
3852 cdata
= &kconsumer_data
;
3855 ret
= LTTNG_ERR_UND
;
3859 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3860 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3863 case LTTNG_DATA_PENDING
:
3866 uint8_t pending_ret_byte
;
3868 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3873 * This function may returns 0 or 1 to indicate whether or not
3874 * there is data pending. In case of error, it should return an
3875 * LTTNG_ERR code. However, some code paths may still return
3876 * a nondescript error code, which we handle by returning an
3879 if (pending_ret
== 0 || pending_ret
== 1) {
3881 * ret will be set to LTTNG_OK at the end of
3884 } else if (pending_ret
< 0) {
3885 ret
= LTTNG_ERR_UNK
;
3892 pending_ret_byte
= (uint8_t) pending_ret
;
3894 /* 1 byte to return whether or not data is pending */
3895 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3896 &pending_ret_byte
, 1);
3905 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3907 struct lttcomm_lttng_output_id reply
;
3909 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3910 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3911 if (ret
!= LTTNG_OK
) {
3915 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3921 /* Copy output list into message payload */
3925 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3927 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3928 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3931 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3934 struct lttng_snapshot_output
*outputs
= NULL
;
3936 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3937 if (nb_output
< 0) {
3942 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3943 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3944 nb_output
* sizeof(struct lttng_snapshot_output
));
3954 case LTTNG_SNAPSHOT_RECORD
:
3956 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3957 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3958 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3961 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3964 struct lttng_uri
*uris
= NULL
;
3966 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3967 len
= nb_uri
* sizeof(struct lttng_uri
);
3970 uris
= zmalloc(len
);
3972 ret
= LTTNG_ERR_FATAL
;
3976 /* Receive variable len data */
3977 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3978 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3980 DBG("No URIs received from client... continuing");
3982 ret
= LTTNG_ERR_SESSION_FAIL
;
3987 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3988 DBG("Creating session with ONE network URI is a bad call");
3989 ret
= LTTNG_ERR_SESSION_FAIL
;
3995 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3996 nb_uri
, &cmd_ctx
->creds
);
4000 case LTTNG_CREATE_SESSION_LIVE
:
4003 struct lttng_uri
*uris
= NULL
;
4005 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4006 len
= nb_uri
* sizeof(struct lttng_uri
);
4009 uris
= zmalloc(len
);
4011 ret
= LTTNG_ERR_FATAL
;
4015 /* Receive variable len data */
4016 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4017 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4019 DBG("No URIs received from client... continuing");
4021 ret
= LTTNG_ERR_SESSION_FAIL
;
4026 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4027 DBG("Creating session with ONE network URI is a bad call");
4028 ret
= LTTNG_ERR_SESSION_FAIL
;
4034 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4035 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4039 case LTTNG_SAVE_SESSION
:
4041 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4045 case LTTNG_SET_SESSION_SHM_PATH
:
4047 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4048 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4051 case LTTNG_REGENERATE_METADATA
:
4053 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4056 case LTTNG_REGENERATE_STATEDUMP
:
4058 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4061 case LTTNG_REGISTER_TRIGGER
:
4063 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4064 notification_thread_handle
);
4067 case LTTNG_UNREGISTER_TRIGGER
:
4069 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4070 notification_thread_handle
);
4074 ret
= LTTNG_ERR_UND
;
4079 if (cmd_ctx
->llm
== NULL
) {
4080 DBG("Missing llm structure. Allocating one.");
4081 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4085 /* Set return code */
4086 cmd_ctx
->llm
->ret_code
= ret
;
4088 if (cmd_ctx
->session
) {
4089 session_unlock(cmd_ctx
->session
);
4091 if (need_tracing_session
) {
4092 session_unlock_list();
4095 assert(!rcu_read_ongoing());
4100 * Thread managing health check socket.
4102 static void *thread_manage_health(void *data
)
4104 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4105 uint32_t revents
, nb_fd
;
4106 struct lttng_poll_event events
;
4107 struct health_comm_msg msg
;
4108 struct health_comm_reply reply
;
4110 DBG("[thread] Manage health check started");
4112 rcu_register_thread();
4114 /* We might hit an error path before this is created. */
4115 lttng_poll_init(&events
);
4117 /* Create unix socket */
4118 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4120 ERR("Unable to create health check Unix socket");
4125 /* lttng health client socket path permissions */
4126 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4127 utils_get_group_id(config
.tracing_group_name
.value
));
4129 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4134 ret
= chmod(config
.health_unix_sock_path
.value
,
4135 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4137 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4144 * Set the CLOEXEC flag. Return code is useless because either way, the
4147 (void) utils_set_fd_cloexec(sock
);
4149 ret
= lttcomm_listen_unix_sock(sock
);
4155 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4156 * more will be added to this poll set.
4158 ret
= sessiond_set_thread_pollset(&events
, 2);
4163 /* Add the application registration socket */
4164 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4169 sessiond_notify_ready();
4172 DBG("Health check ready");
4174 /* Inifinite blocking call, waiting for transmission */
4176 ret
= lttng_poll_wait(&events
, -1);
4179 * Restart interrupted system call.
4181 if (errno
== EINTR
) {
4189 for (i
= 0; i
< nb_fd
; i
++) {
4190 /* Fetch once the poll data */
4191 revents
= LTTNG_POLL_GETEV(&events
, i
);
4192 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4195 /* No activity for this FD (poll implementation). */
4199 /* Thread quit pipe has been closed. Killing thread. */
4200 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4206 /* Event on the registration socket */
4207 if (pollfd
== sock
) {
4208 if (revents
& LPOLLIN
) {
4210 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4211 ERR("Health socket poll error");
4214 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4220 new_sock
= lttcomm_accept_unix_sock(sock
);
4226 * Set the CLOEXEC flag. Return code is useless because either way, the
4229 (void) utils_set_fd_cloexec(new_sock
);
4231 DBG("Receiving data from client for health...");
4232 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4234 DBG("Nothing recv() from client... continuing");
4235 ret
= close(new_sock
);
4242 rcu_thread_online();
4244 memset(&reply
, 0, sizeof(reply
));
4245 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4247 * health_check_state returns 0 if health is
4250 if (!health_check_state(health_sessiond
, i
)) {
4251 reply
.ret_code
|= 1ULL << i
;
4255 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4257 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4259 ERR("Failed to send health data back to client");
4262 /* End of transmission */
4263 ret
= close(new_sock
);
4272 ERR("Health error occurred in %s", __func__
);
4274 DBG("Health check thread dying");
4275 unlink(config
.health_unix_sock_path
.value
);
4283 lttng_poll_clean(&events
);
4285 rcu_unregister_thread();
4290 * This thread manage all clients request using the unix client socket for
4293 static void *thread_manage_clients(void *data
)
4295 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4297 uint32_t revents
, nb_fd
;
4298 struct command_ctx
*cmd_ctx
= NULL
;
4299 struct lttng_poll_event events
;
4301 DBG("[thread] Manage client started");
4303 rcu_register_thread();
4305 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4307 health_code_update();
4309 ret
= lttcomm_listen_unix_sock(client_sock
);
4315 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4316 * more will be added to this poll set.
4318 ret
= sessiond_set_thread_pollset(&events
, 2);
4320 goto error_create_poll
;
4323 /* Add the application registration socket */
4324 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4329 sessiond_notify_ready();
4330 ret
= sem_post(&load_info
->message_thread_ready
);
4332 PERROR("sem_post message_thread_ready");
4336 /* This testpoint is after we signal readiness to the parent. */
4337 if (testpoint(sessiond_thread_manage_clients
)) {
4341 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4345 health_code_update();
4348 DBG("Accepting client command ...");
4350 /* Inifinite blocking call, waiting for transmission */
4352 health_poll_entry();
4353 ret
= lttng_poll_wait(&events
, -1);
4357 * Restart interrupted system call.
4359 if (errno
== EINTR
) {
4367 for (i
= 0; i
< nb_fd
; i
++) {
4368 /* Fetch once the poll data */
4369 revents
= LTTNG_POLL_GETEV(&events
, i
);
4370 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4372 health_code_update();
4375 /* No activity for this FD (poll implementation). */
4379 /* Thread quit pipe has been closed. Killing thread. */
4380 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4386 /* Event on the registration socket */
4387 if (pollfd
== client_sock
) {
4388 if (revents
& LPOLLIN
) {
4390 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4391 ERR("Client socket poll error");
4394 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4400 DBG("Wait for client response");
4402 health_code_update();
4404 sock
= lttcomm_accept_unix_sock(client_sock
);
4410 * Set the CLOEXEC flag. Return code is useless because either way, the
4413 (void) utils_set_fd_cloexec(sock
);
4415 /* Set socket option for credentials retrieval */
4416 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4421 /* Allocate context command to process the client request */
4422 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4423 if (cmd_ctx
== NULL
) {
4424 PERROR("zmalloc cmd_ctx");
4428 /* Allocate data buffer for reception */
4429 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4430 if (cmd_ctx
->lsm
== NULL
) {
4431 PERROR("zmalloc cmd_ctx->lsm");
4435 cmd_ctx
->llm
= NULL
;
4436 cmd_ctx
->session
= NULL
;
4438 health_code_update();
4441 * Data is received from the lttng client. The struct
4442 * lttcomm_session_msg (lsm) contains the command and data request of
4445 DBG("Receiving data from client ...");
4446 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4447 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4449 DBG("Nothing recv() from client... continuing");
4455 clean_command_ctx(&cmd_ctx
);
4459 health_code_update();
4461 // TODO: Validate cmd_ctx including sanity check for
4462 // security purpose.
4464 rcu_thread_online();
4466 * This function dispatch the work to the kernel or userspace tracer
4467 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4468 * informations for the client. The command context struct contains
4469 * everything this function may needs.
4471 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4472 rcu_thread_offline();
4480 * TODO: Inform client somehow of the fatal error. At
4481 * this point, ret < 0 means that a zmalloc failed
4482 * (ENOMEM). Error detected but still accept
4483 * command, unless a socket error has been
4486 clean_command_ctx(&cmd_ctx
);
4490 health_code_update();
4492 DBG("Sending response (size: %d, retcode: %s (%d))",
4493 cmd_ctx
->lttng_msg_size
,
4494 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4495 cmd_ctx
->llm
->ret_code
);
4496 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4498 ERR("Failed to send data back to client");
4501 /* End of transmission */
4508 clean_command_ctx(&cmd_ctx
);
4510 health_code_update();
4522 lttng_poll_clean(&events
);
4523 clean_command_ctx(&cmd_ctx
);
4527 unlink(config
.client_unix_sock_path
.value
);
4528 if (client_sock
>= 0) {
4529 ret
= close(client_sock
);
4537 ERR("Health error occurred in %s", __func__
);
4540 health_unregister(health_sessiond
);
4542 DBG("Client thread dying");
4544 rcu_unregister_thread();
4547 * Since we are creating the consumer threads, we own them, so we need
4548 * to join them before our thread exits.
4550 ret
= join_consumer_thread(&kconsumer_data
);
4553 PERROR("join_consumer");
4556 ret
= join_consumer_thread(&ustconsumer32_data
);
4559 PERROR("join_consumer ust32");
4562 ret
= join_consumer_thread(&ustconsumer64_data
);
4565 PERROR("join_consumer ust64");
4570 static int string_match(const char *str1
, const char *str2
)
4572 return (str1
&& str2
) && !strcmp(str1
, str2
);
4576 * Take an option from the getopt output and set it in the right variable to be
4579 * Return 0 on success else a negative value.
4581 static int set_option(int opt
, const char *arg
, const char *optname
)
4585 if (string_match(optname
, "client-sock") || opt
== 'c') {
4586 if (!arg
|| *arg
== '\0') {
4590 if (lttng_is_setuid_setgid()) {
4591 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4592 "-c, --client-sock");
4594 config_string_set(&config
.client_unix_sock_path
,
4596 if (!config
.client_unix_sock_path
.value
) {
4601 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4602 if (!arg
|| *arg
== '\0') {
4606 if (lttng_is_setuid_setgid()) {
4607 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4610 config_string_set(&config
.apps_unix_sock_path
,
4612 if (!config
.apps_unix_sock_path
.value
) {
4617 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4618 config
.daemonize
= true;
4619 } else if (string_match(optname
, "background") || opt
== 'b') {
4620 config
.background
= true;
4621 } else if (string_match(optname
, "group") || opt
== 'g') {
4622 if (!arg
|| *arg
== '\0') {
4626 if (lttng_is_setuid_setgid()) {
4627 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4630 config_string_set(&config
.tracing_group_name
,
4632 if (!config
.tracing_group_name
.value
) {
4637 } else if (string_match(optname
, "help") || opt
== 'h') {
4638 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4640 ERR("Cannot show --help for `lttng-sessiond`");
4643 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4644 } else if (string_match(optname
, "version") || opt
== 'V') {
4645 fprintf(stdout
, "%s\n", VERSION
);
4647 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4648 config
.sig_parent
= true;
4649 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4650 if (!arg
|| *arg
== '\0') {
4654 if (lttng_is_setuid_setgid()) {
4655 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4656 "--kconsumerd-err-sock");
4658 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4660 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4665 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4666 if (!arg
|| *arg
== '\0') {
4670 if (lttng_is_setuid_setgid()) {
4671 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4672 "--kconsumerd-cmd-sock");
4674 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4676 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4681 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4682 if (!arg
|| *arg
== '\0') {
4686 if (lttng_is_setuid_setgid()) {
4687 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4688 "--ustconsumerd64-err-sock");
4690 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4692 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4697 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4698 if (!arg
|| *arg
== '\0') {
4702 if (lttng_is_setuid_setgid()) {
4703 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4704 "--ustconsumerd64-cmd-sock");
4706 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4708 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4713 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4714 if (!arg
|| *arg
== '\0') {
4718 if (lttng_is_setuid_setgid()) {
4719 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4720 "--ustconsumerd32-err-sock");
4722 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4724 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4729 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4730 if (!arg
|| *arg
== '\0') {
4734 if (lttng_is_setuid_setgid()) {
4735 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4736 "--ustconsumerd32-cmd-sock");
4738 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4740 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4745 } else if (string_match(optname
, "no-kernel")) {
4746 config
.no_kernel
= true;
4747 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4748 lttng_opt_quiet
= true;
4749 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4750 /* Verbose level can increase using multiple -v */
4752 /* Value obtained from config file */
4753 config
.verbose
= config_parse_value(arg
);
4755 /* -v used on command line */
4758 /* Clamp value to [0, 3] */
4759 config
.verbose
= config
.verbose
< 0 ? 0 :
4760 (config
.verbose
<= 3 ? config
.verbose
: 3);
4761 } else if (string_match(optname
, "verbose-consumer")) {
4763 config
.verbose_consumer
= config_parse_value(arg
);
4765 config
.verbose_consumer
++;
4767 } else if (string_match(optname
, "consumerd32-path")) {
4768 if (!arg
|| *arg
== '\0') {
4772 if (lttng_is_setuid_setgid()) {
4773 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4774 "--consumerd32-path");
4776 config_string_set(&config
.consumerd32_bin_path
,
4778 if (!config
.consumerd32_bin_path
.value
) {
4783 } else if (string_match(optname
, "consumerd32-libdir")) {
4784 if (!arg
|| *arg
== '\0') {
4788 if (lttng_is_setuid_setgid()) {
4789 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4790 "--consumerd32-libdir");
4792 config_string_set(&config
.consumerd32_lib_dir
,
4794 if (!config
.consumerd32_lib_dir
.value
) {
4799 } else if (string_match(optname
, "consumerd64-path")) {
4800 if (!arg
|| *arg
== '\0') {
4804 if (lttng_is_setuid_setgid()) {
4805 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4806 "--consumerd64-path");
4808 config_string_set(&config
.consumerd64_bin_path
,
4810 if (!config
.consumerd64_bin_path
.value
) {
4815 } else if (string_match(optname
, "consumerd64-libdir")) {
4816 if (!arg
|| *arg
== '\0') {
4820 if (lttng_is_setuid_setgid()) {
4821 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4822 "--consumerd64-libdir");
4824 config_string_set(&config
.consumerd64_lib_dir
,
4826 if (!config
.consumerd64_lib_dir
.value
) {
4831 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4832 if (!arg
|| *arg
== '\0') {
4836 if (lttng_is_setuid_setgid()) {
4837 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4840 config_string_set(&config
.pid_file_path
, strdup(arg
));
4841 if (!config
.pid_file_path
.value
) {
4846 } else if (string_match(optname
, "agent-tcp-port")) {
4847 if (!arg
|| *arg
== '\0') {
4851 if (lttng_is_setuid_setgid()) {
4852 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4853 "--agent-tcp-port");
4858 v
= strtoul(arg
, NULL
, 0);
4859 if (errno
!= 0 || !isdigit(arg
[0])) {
4860 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4863 if (v
== 0 || v
>= 65535) {
4864 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4867 config
.agent_tcp_port
= (uint32_t) v
;
4868 DBG3("Agent TCP port set to non default: %u", config
.agent_tcp_port
);
4870 } else if (string_match(optname
, "load") || opt
== 'l') {
4871 if (!arg
|| *arg
== '\0') {
4875 if (lttng_is_setuid_setgid()) {
4876 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4879 config_string_set(&config
.load_session_path
, strdup(arg
));
4880 if (!config
.load_session_path
.value
) {
4885 } else if (string_match(optname
, "kmod-probes")) {
4886 if (!arg
|| *arg
== '\0') {
4890 if (lttng_is_setuid_setgid()) {
4891 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4894 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4895 if (!config
.kmod_probes_list
.value
) {
4900 } else if (string_match(optname
, "extra-kmod-probes")) {
4901 if (!arg
|| *arg
== '\0') {
4905 if (lttng_is_setuid_setgid()) {
4906 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4907 "--extra-kmod-probes");
4909 config_string_set(&config
.kmod_extra_probes_list
,
4911 if (!config
.kmod_extra_probes_list
.value
) {
4916 } else if (string_match(optname
, "config") || opt
== 'f') {
4917 /* This is handled in set_options() thus silent skip. */
4920 /* Unknown option or other error.
4921 * Error is printed by getopt, just return */
4926 if (ret
== -EINVAL
) {
4927 const char *opt_name
= "unknown";
4930 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4932 if (opt
== long_options
[i
].val
) {
4933 opt_name
= long_options
[i
].name
;
4938 WARN("Invalid argument provided for option \"%s\", using default value.",
4946 * config_entry_handler_cb used to handle options read from a config file.
4947 * See config_entry_handler_cb comment in common/config/session-config.h for the
4948 * return value conventions.
4950 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4954 if (!entry
|| !entry
->name
|| !entry
->value
) {
4959 /* Check if the option is to be ignored */
4960 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4961 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4966 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4969 /* Ignore if not fully matched. */
4970 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4975 * If the option takes no argument on the command line, we have to
4976 * check if the value is "true". We support non-zero numeric values,
4979 if (!long_options
[i
].has_arg
) {
4980 ret
= config_parse_value(entry
->value
);
4983 WARN("Invalid configuration value \"%s\" for option %s",
4984 entry
->value
, entry
->name
);
4986 /* False, skip boolean config option. */
4991 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4995 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5002 * daemon configuration loading and argument parsing
5004 static int set_options(int argc
, char **argv
)
5006 int ret
= 0, c
= 0, option_index
= 0;
5007 int orig_optopt
= optopt
, orig_optind
= optind
;
5009 const char *config_path
= NULL
;
5011 optstring
= utils_generate_optstring(long_options
,
5012 sizeof(long_options
) / sizeof(struct option
));
5018 /* Check for the --config option */
5019 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5020 &option_index
)) != -1) {
5024 } else if (c
!= 'f') {
5025 /* if not equal to --config option. */
5029 if (lttng_is_setuid_setgid()) {
5030 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5033 config_path
= utils_expand_path(optarg
);
5035 ERR("Failed to resolve path: %s", optarg
);
5040 ret
= config_get_section_entries(config_path
, config_section_name
,
5041 config_entry_handler
, NULL
);
5044 ERR("Invalid configuration option at line %i", ret
);
5050 /* Reset getopt's global state */
5051 optopt
= orig_optopt
;
5052 optind
= orig_optind
;
5056 * getopt_long() will not set option_index if it encounters a
5059 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5066 * Pass NULL as the long option name if popt left the index
5069 ret
= set_option(c
, optarg
,
5070 option_index
< 0 ? NULL
:
5071 long_options
[option_index
].name
);
5083 * Creates the two needed socket by the daemon.
5084 * apps_sock - The communication socket for all UST apps.
5085 * client_sock - The communication of the cli tool (lttng).
5087 static int init_daemon_socket(void)
5092 old_umask
= umask(0);
5094 /* Create client tool unix socket */
5095 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5096 if (client_sock
< 0) {
5097 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5102 /* Set the cloexec flag */
5103 ret
= utils_set_fd_cloexec(client_sock
);
5105 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5106 "Continuing but note that the consumer daemon will have a "
5107 "reference to this socket on exec()", client_sock
);
5110 /* File permission MUST be 660 */
5111 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5113 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5118 /* Create the application unix socket */
5119 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5120 if (apps_sock
< 0) {
5121 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5126 /* Set the cloexec flag */
5127 ret
= utils_set_fd_cloexec(apps_sock
);
5129 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5130 "Continuing but note that the consumer daemon will have a "
5131 "reference to this socket on exec()", apps_sock
);
5134 /* File permission MUST be 666 */
5135 ret
= chmod(config
.apps_unix_sock_path
.value
,
5136 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5138 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5143 DBG3("Session daemon client socket %d and application socket %d created",
5144 client_sock
, apps_sock
);
5152 * Check if the global socket is available, and if a daemon is answering at the
5153 * other side. If yes, error is returned.
5155 static int check_existing_daemon(void)
5157 /* Is there anybody out there ? */
5158 if (lttng_session_daemon_alive()) {
5166 * Set the tracing group gid onto the client socket.
5168 * Race window between mkdir and chown is OK because we are going from more
5169 * permissive (root.root) to less permissive (root.tracing).
5171 static int set_permissions(char *rundir
)
5176 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5178 /* Set lttng run dir */
5179 ret
= chown(rundir
, 0, gid
);
5181 ERR("Unable to set group on %s", rundir
);
5186 * Ensure all applications and tracing group can search the run
5187 * dir. Allow everyone to read the directory, since it does not
5188 * buy us anything to hide its content.
5190 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5192 ERR("Unable to set permissions on %s", rundir
);
5196 /* lttng client socket path */
5197 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5199 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5203 /* kconsumer error socket path */
5204 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5206 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5210 /* 64-bit ustconsumer error socket path */
5211 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5213 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5217 /* 32-bit ustconsumer compat32 error socket path */
5218 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5220 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5224 DBG("All permissions are set");
5230 * Create the lttng run directory needed for all global sockets and pipe.
5232 static int create_lttng_rundir(void)
5236 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5238 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5240 if (errno
!= EEXIST
) {
5241 ERR("Unable to create %s", config
.rundir
.value
);
5253 * Setup sockets and directory needed by the consumerds' communication with the
5256 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5261 switch (consumer_data
->type
) {
5262 case LTTNG_CONSUMER_KERNEL
:
5263 path
= config
.kconsumerd_path
.value
;
5265 case LTTNG_CONSUMER64_UST
:
5266 path
= config
.consumerd64_path
.value
;
5268 case LTTNG_CONSUMER32_UST
:
5269 path
= config
.consumerd32_path
.value
;
5272 ERR("Consumer type unknown");
5278 DBG2("Creating consumer directory: %s", path
);
5280 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5281 if (ret
< 0 && errno
!= EEXIST
) {
5283 ERR("Failed to create %s", path
);
5287 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5289 ERR("Unable to set group on %s", path
);
5295 /* Create the consumerd error unix socket */
5296 consumer_data
->err_sock
=
5297 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5298 if (consumer_data
->err_sock
< 0) {
5299 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5305 * Set the CLOEXEC flag. Return code is useless because either way, the
5308 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5310 PERROR("utils_set_fd_cloexec");
5311 /* continue anyway */
5314 /* File permission MUST be 660 */
5315 ret
= chmod(consumer_data
->err_unix_sock_path
,
5316 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5318 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5328 * Signal handler for the daemon
5330 * Simply stop all worker threads, leaving main() return gracefully after
5331 * joining all threads and calling cleanup().
5333 static void sighandler(int sig
)
5337 DBG("SIGINT caught");
5341 DBG("SIGTERM caught");
5345 CMM_STORE_SHARED(recv_child_signal
, 1);
5353 * Setup signal handler for :
5354 * SIGINT, SIGTERM, SIGPIPE
5356 static int set_signal_handler(void)
5359 struct sigaction sa
;
5362 if ((ret
= sigemptyset(&sigset
)) < 0) {
5363 PERROR("sigemptyset");
5367 sa
.sa_mask
= sigset
;
5370 sa
.sa_handler
= sighandler
;
5371 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5372 PERROR("sigaction");
5376 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5377 PERROR("sigaction");
5381 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5382 PERROR("sigaction");
5386 sa
.sa_handler
= SIG_IGN
;
5387 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5388 PERROR("sigaction");
5392 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5398 * Set open files limit to unlimited. This daemon can open a large number of
5399 * file descriptors in order to consume multiple kernel traces.
5401 static void set_ulimit(void)
5406 /* The kernel does not allow an infinite limit for open files */
5407 lim
.rlim_cur
= 65535;
5408 lim
.rlim_max
= 65535;
5410 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5412 PERROR("failed to set open files limit");
5416 static int write_pidfile(void)
5418 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5422 * Create lockfile using the rundir and return its fd.
5424 static int create_lockfile(void)
5426 return utils_create_lock_file(config
.lock_file_path
.value
);
5430 * Write agent TCP port using the rundir.
5432 static int write_agent_port(void)
5434 return utils_create_pid_file(config
.agent_tcp_port
,
5435 config
.agent_port_file_path
.value
);
5438 static int set_clock_plugin_env(void)
5441 char *env_value
= NULL
;
5443 if (!config
.lttng_ust_clock_plugin
.value
) {
5447 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5448 config
.lttng_ust_clock_plugin
.value
);
5454 ret
= putenv(env_value
);
5457 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5461 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5462 config
.lttng_ust_clock_plugin
.value
);
5470 int main(int argc
, char **argv
)
5472 int ret
= 0, retval
= 0;
5474 const char *env_app_timeout
;
5475 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5476 *ust64_channel_monitor_pipe
= NULL
,
5477 *kernel_channel_monitor_pipe
= NULL
;
5478 bool notification_thread_running
= false;
5479 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5480 *ust64_channel_rotate_pipe
= NULL
,
5481 *kernel_channel_rotate_pipe
= NULL
;
5483 init_kernel_workarounds();
5485 rcu_register_thread();
5487 if (set_signal_handler()) {
5489 goto exit_set_signal_handler
;
5492 page_size
= sysconf(_SC_PAGESIZE
);
5493 if (page_size
< 0) {
5494 PERROR("sysconf _SC_PAGESIZE");
5495 page_size
= LONG_MAX
;
5496 WARN("Fallback page size to %ld", page_size
);
5499 ret
= sessiond_config_init(&config
);
5502 goto exit_set_signal_handler
;
5506 * Parse arguments and load the daemon configuration file.
5508 * We have an exit_options exit path to free memory reserved by
5509 * set_options. This is needed because the rest of sessiond_cleanup()
5510 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5511 * depends on set_options.
5514 if (set_options(argc
, argv
)) {
5519 /* Init config from environment variables. */
5520 sessiond_config_apply_env_config(&config
);
5523 * Resolve all paths received as arguments, configuration option, or
5524 * through environment variable as absolute paths. This is necessary
5525 * since daemonizing causes the sessiond's current working directory
5528 ret
= sessiond_config_resolve_paths(&config
);
5534 lttng_opt_verbose
= config
.verbose
;
5535 lttng_opt_quiet
= config
.quiet
;
5536 kconsumer_data
.err_unix_sock_path
=
5537 config
.kconsumerd_err_unix_sock_path
.value
;
5538 kconsumer_data
.cmd_unix_sock_path
=
5539 config
.kconsumerd_cmd_unix_sock_path
.value
;
5540 ustconsumer32_data
.err_unix_sock_path
=
5541 config
.consumerd32_err_unix_sock_path
.value
;
5542 ustconsumer32_data
.cmd_unix_sock_path
=
5543 config
.consumerd32_cmd_unix_sock_path
.value
;
5544 ustconsumer64_data
.err_unix_sock_path
=
5545 config
.consumerd64_err_unix_sock_path
.value
;
5546 ustconsumer64_data
.cmd_unix_sock_path
=
5547 config
.consumerd64_cmd_unix_sock_path
.value
;
5548 set_clock_plugin_env();
5550 sessiond_config_log(&config
);
5553 if (config
.daemonize
|| config
.background
) {
5556 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5557 !config
.background
);
5564 * We are in the child. Make sure all other file descriptors are
5565 * closed, in case we are called with more opened file
5566 * descriptors than the standard ones.
5568 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5573 if (run_as_create_worker(argv
[0]) < 0) {
5574 goto exit_create_run_as_worker_cleanup
;
5578 * Starting from here, we can create threads. This needs to be after
5579 * lttng_daemonize due to RCU.
5583 * Initialize the health check subsystem. This call should set the
5584 * appropriate time values.
5586 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5587 if (!health_sessiond
) {
5588 PERROR("health_app_create error");
5590 goto exit_health_sessiond_cleanup
;
5593 /* Create thread to clean up RCU hash tables */
5594 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5596 goto exit_ht_cleanup
;
5599 /* Create thread quit pipe */
5600 if (init_thread_quit_pipe()) {
5602 goto exit_init_data
;
5605 /* Check if daemon is UID = 0 */
5606 is_root
= !getuid();
5608 if (create_lttng_rundir()) {
5610 goto exit_init_data
;
5614 /* Create global run dir with root access */
5616 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5617 if (!kernel_channel_monitor_pipe
) {
5618 ERR("Failed to create kernel consumer channel monitor pipe");
5620 goto exit_init_data
;
5622 kconsumer_data
.channel_monitor_pipe
=
5623 lttng_pipe_release_writefd(
5624 kernel_channel_monitor_pipe
);
5625 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5627 goto exit_init_data
;
5629 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5630 if (!kernel_channel_rotate_pipe
) {
5631 ERR("Failed to create kernel consumer channel rotate pipe");
5633 goto exit_init_data
;
5635 kconsumer_data
.channel_rotate_pipe
=
5636 lttng_pipe_release_writefd(
5637 kernel_channel_rotate_pipe
);
5638 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5640 goto exit_init_data
;
5644 lockfile_fd
= create_lockfile();
5645 if (lockfile_fd
< 0) {
5647 goto exit_init_data
;
5650 /* Set consumer initial state */
5651 kernel_consumerd_state
= CONSUMER_STOPPED
;
5652 ust_consumerd_state
= CONSUMER_STOPPED
;
5654 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5655 if (!ust32_channel_monitor_pipe
) {
5656 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5658 goto exit_init_data
;
5660 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5661 ust32_channel_monitor_pipe
);
5662 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5664 goto exit_init_data
;
5666 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
5667 if (!ust32_channel_rotate_pipe
) {
5668 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
5670 goto exit_init_data
;
5672 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5673 ust32_channel_rotate_pipe
);
5674 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
5676 goto exit_init_data
;
5680 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5681 if (!ust64_channel_monitor_pipe
) {
5682 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5684 goto exit_init_data
;
5686 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5687 ust64_channel_monitor_pipe
);
5688 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5690 goto exit_init_data
;
5692 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
5693 if (!ust64_channel_rotate_pipe
) {
5694 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
5696 goto exit_init_data
;
5698 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5699 ust64_channel_rotate_pipe
);
5700 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
5702 goto exit_init_data
;
5706 * See if daemon already exist.
5708 if (check_existing_daemon()) {
5709 ERR("Already running daemon.\n");
5711 * We do not goto exit because we must not cleanup()
5712 * because a daemon is already running.
5715 goto exit_init_data
;
5719 * Init UST app hash table. Alloc hash table before this point since
5720 * cleanup() can get called after that point.
5722 if (ust_app_ht_alloc()) {
5723 ERR("Failed to allocate UST app hash table");
5725 goto exit_init_data
;
5729 * Initialize agent app hash table. We allocate the hash table here
5730 * since cleanup() can get called after this point.
5732 if (agent_app_ht_alloc()) {
5733 ERR("Failed to allocate Agent app hash table");
5735 goto exit_init_data
;
5739 * These actions must be executed as root. We do that *after* setting up
5740 * the sockets path because we MUST make the check for another daemon using
5741 * those paths *before* trying to set the kernel consumer sockets and init
5745 if (set_consumer_sockets(&kconsumer_data
)) {
5747 goto exit_init_data
;
5750 /* Setup kernel tracer */
5751 if (!config
.no_kernel
) {
5752 init_kernel_tracer();
5753 if (kernel_tracer_fd
>= 0) {
5754 ret
= syscall_init_table();
5756 ERR("Unable to populate syscall table. "
5757 "Syscall tracing won't work "
5758 "for this session daemon.");
5763 /* Set ulimit for open files */
5766 /* init lttng_fd tracking must be done after set_ulimit. */
5769 if (set_consumer_sockets(&ustconsumer64_data
)) {
5771 goto exit_init_data
;
5774 if (set_consumer_sockets(&ustconsumer32_data
)) {
5776 goto exit_init_data
;
5779 /* Setup the needed unix socket */
5780 if (init_daemon_socket()) {
5782 goto exit_init_data
;
5785 /* Set credentials to socket */
5786 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5788 goto exit_init_data
;
5791 /* Get parent pid if -S, --sig-parent is specified. */
5792 if (config
.sig_parent
) {
5796 /* Setup the kernel pipe for waking up the kernel thread */
5797 if (is_root
&& !config
.no_kernel
) {
5798 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5800 goto exit_init_data
;
5804 /* Setup the thread apps communication pipe. */
5805 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5807 goto exit_init_data
;
5810 /* Setup the thread apps notify communication pipe. */
5811 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5813 goto exit_init_data
;
5816 /* Initialize global buffer per UID and PID registry. */
5817 buffer_reg_init_uid_registry();
5818 buffer_reg_init_pid_registry();
5820 /* Init UST command queue. */
5821 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5824 * Get session list pointer. This pointer MUST NOT be free'd. This list
5825 * is statically declared in session.c
5827 session_list_ptr
= session_get_list();
5831 /* Check for the application socket timeout env variable. */
5832 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5833 if (env_app_timeout
) {
5834 config
.app_socket_timeout
= atoi(env_app_timeout
);
5836 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5839 ret
= write_pidfile();
5841 ERR("Error in write_pidfile");
5843 goto exit_init_data
;
5845 ret
= write_agent_port();
5847 ERR("Error in write_agent_port");
5849 goto exit_init_data
;
5852 /* Initialize communication library */
5854 /* Initialize TCP timeout values */
5855 lttcomm_inet_init();
5857 if (load_session_init_data(&load_info
) < 0) {
5859 goto exit_init_data
;
5861 load_info
->path
= config
.load_session_path
.value
;
5863 /* Create health-check thread. */
5864 ret
= pthread_create(&health_thread
, default_pthread_attr(),
5865 thread_manage_health
, (void *) NULL
);
5868 PERROR("pthread_create health");
5873 /* notification_thread_data acquires the pipes' read side. */
5874 notification_thread_handle
= notification_thread_handle_create(
5875 ust32_channel_monitor_pipe
,
5876 ust64_channel_monitor_pipe
,
5877 kernel_channel_monitor_pipe
);
5878 if (!notification_thread_handle
) {
5880 ERR("Failed to create notification thread shared data");
5882 goto exit_notification
;
5885 /* Create notification thread. */
5886 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
5887 thread_notification
, notification_thread_handle
);
5890 PERROR("pthread_create notification");
5893 goto exit_notification
;
5895 notification_thread_running
= true;
5897 /* Create thread to manage the client socket */
5898 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5899 thread_manage_clients
, (void *) NULL
);
5902 PERROR("pthread_create clients");
5908 /* Create thread to dispatch registration */
5909 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5910 thread_dispatch_ust_registration
, (void *) NULL
);
5913 PERROR("pthread_create dispatch");
5919 /* Create thread to manage application registration. */
5920 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5921 thread_registration_apps
, (void *) NULL
);
5924 PERROR("pthread_create registration");
5930 /* Create thread to manage application socket */
5931 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5932 thread_manage_apps
, (void *) NULL
);
5935 PERROR("pthread_create apps");
5941 /* Create thread to manage application notify socket */
5942 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5943 ust_thread_manage_notify
, (void *) NULL
);
5946 PERROR("pthread_create notify");
5949 goto exit_apps_notify
;
5952 /* Create agent registration thread. */
5953 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5954 agent_thread_manage_registration
, (void *) NULL
);
5957 PERROR("pthread_create agent");
5960 goto exit_agent_reg
;
5963 /* Don't start this thread if kernel tracing is not requested nor root */
5964 if (is_root
&& !config
.no_kernel
) {
5965 /* Create kernel thread to manage kernel event */
5966 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5967 thread_manage_kernel
, (void *) NULL
);
5970 PERROR("pthread_create kernel");
5977 /* Create session loading thread. */
5978 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5979 thread_load_session
, load_info
);
5982 PERROR("pthread_create load_session_thread");
5985 goto exit_load_session
;
5989 * This is where we start awaiting program completion (e.g. through
5990 * signal that asks threads to teardown).
5993 ret
= pthread_join(load_session_thread
, &status
);
5996 PERROR("pthread_join load_session_thread");
6001 if (is_root
&& !config
.no_kernel
) {
6002 ret
= pthread_join(kernel_thread
, &status
);
6005 PERROR("pthread_join");
6011 ret
= pthread_join(agent_reg_thread
, &status
);
6014 PERROR("pthread_join agent");
6019 ret
= pthread_join(apps_notify_thread
, &status
);
6022 PERROR("pthread_join apps notify");
6027 ret
= pthread_join(apps_thread
, &status
);
6030 PERROR("pthread_join apps");
6035 ret
= pthread_join(reg_apps_thread
, &status
);
6038 PERROR("pthread_join");
6044 * Join dispatch thread after joining reg_apps_thread to ensure
6045 * we don't leak applications in the queue.
6047 ret
= pthread_join(dispatch_thread
, &status
);
6050 PERROR("pthread_join");
6055 ret
= pthread_join(client_thread
, &status
);
6058 PERROR("pthread_join");
6064 ret
= pthread_join(health_thread
, &status
);
6067 PERROR("pthread_join health thread");
6074 * Wait for all pending call_rcu work to complete before tearing
6075 * down data structures. call_rcu worker may be trying to
6076 * perform lookups in those structures.
6080 * sessiond_cleanup() is called when no other thread is running, except
6081 * the ht_cleanup thread, which is needed to destroy the hash tables.
6083 rcu_thread_online();
6087 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6088 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6089 * the queue is empty before shutting down the clean-up thread.
6094 * The teardown of the notification system is performed after the
6095 * session daemon's teardown in order to allow it to be notified
6096 * of the active session and channels at the moment of the teardown.
6098 if (notification_thread_handle
) {
6099 if (notification_thread_running
) {
6100 notification_thread_command_quit(
6101 notification_thread_handle
);
6102 ret
= pthread_join(notification_thread
, &status
);
6105 PERROR("pthread_join notification thread");
6109 notification_thread_handle_destroy(notification_thread_handle
);
6112 rcu_thread_offline();
6113 rcu_unregister_thread();
6115 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6119 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6120 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6121 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6122 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6123 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6124 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6127 health_app_destroy(health_sessiond
);
6128 exit_health_sessiond_cleanup
:
6129 exit_create_run_as_worker_cleanup
:
6132 sessiond_cleanup_options();
6134 exit_set_signal_handler
: