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>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
64 #include "kernel-consumer.h"
68 #include "ust-consumer.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
86 static const char *help_msg
=
87 #ifdef LTTNG_EMBED_HELP
88 #include <lttng-sessiond.8.h>
95 static int lockfile_fd
= -1;
97 /* Set to 1 when a SIGUSR1 signal is received. */
98 static int recv_child_signal
;
100 static struct lttng_kernel_tracer_version kernel_tracer_version
;
101 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
104 * Consumer daemon specific control data. Every value not initialized here is
105 * set to 0 by the static definition.
107 static struct consumer_data kconsumer_data
= {
108 .type
= LTTNG_CONSUMER_KERNEL
,
111 .channel_monitor_pipe
= -1,
112 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 .lock
= PTHREAD_MUTEX_INITIALIZER
,
114 .cond
= PTHREAD_COND_INITIALIZER
,
115 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 static struct consumer_data ustconsumer64_data
= {
118 .type
= LTTNG_CONSUMER64_UST
,
121 .channel_monitor_pipe
= -1,
122 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
123 .lock
= PTHREAD_MUTEX_INITIALIZER
,
124 .cond
= PTHREAD_COND_INITIALIZER
,
125 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
127 static struct consumer_data ustconsumer32_data
= {
128 .type
= LTTNG_CONSUMER32_UST
,
131 .channel_monitor_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 /* Command line options to ignore from configuration file */
172 static const char *config_ignore_options
[] = { "help", "version", "config" };
174 /* Shared between threads */
175 static int dispatch_thread_exit
;
177 /* Sockets and FDs */
178 static int client_sock
= -1;
179 static int apps_sock
= -1;
182 * This pipe is used to inform the thread managing application communication
183 * that a command is queued and ready to be processed.
185 static int apps_cmd_pipe
[2] = { -1, -1 };
187 /* Pthread, Mutexes and Semaphores */
188 static pthread_t apps_thread
;
189 static pthread_t apps_notify_thread
;
190 static pthread_t reg_apps_thread
;
191 static pthread_t client_thread
;
192 static pthread_t kernel_thread
;
193 static pthread_t dispatch_thread
;
194 static pthread_t health_thread
;
195 static pthread_t ht_cleanup_thread
;
196 static pthread_t agent_reg_thread
;
197 static pthread_t load_session_thread
;
198 static pthread_t notification_thread
;
199 static pthread_t rotation_thread
;
200 static pthread_t timer_thread
;
203 * UST registration command queue. This queue is tied with a futex and uses a N
204 * wakers / 1 waiter implemented and detailed in futex.c/.h
206 * The thread_registration_apps and thread_dispatch_ust_registration uses this
207 * queue along with the wait/wake scheme. The thread_manage_apps receives down
208 * the line new application socket and monitors it for any I/O error or clean
209 * close that triggers an unregistration of the application.
211 static struct ust_cmd_queue ust_cmd_queue
;
213 static const char *module_proc_lttng
= "/proc/lttng";
216 * Consumer daemon state which is changed when spawning it, killing it or in
217 * case of a fatal error.
219 enum consumerd_state
{
220 CONSUMER_STARTED
= 1,
221 CONSUMER_STOPPED
= 2,
226 * This consumer daemon state is used to validate if a client command will be
227 * able to reach the consumer. If not, the client is informed. For instance,
228 * doing a "lttng start" when the consumer state is set to ERROR will return an
229 * error to the client.
231 * The following example shows a possible race condition of this scheme:
233 * consumer thread error happens
235 * client cmd checks state -> still OK
236 * consumer thread exit, sets error
237 * client cmd try to talk to consumer
240 * However, since the consumer is a different daemon, we have no way of making
241 * sure the command will reach it safely even with this state flag. This is why
242 * we consider that up to the state validation during command processing, the
243 * command is safe. After that, we can not guarantee the correctness of the
244 * client request vis-a-vis the consumer.
246 static enum consumerd_state ust_consumerd_state
;
247 static enum consumerd_state kernel_consumerd_state
;
249 /* Load session thread information to operate. */
250 static struct load_session_thread_data
*load_info
;
253 * Section name to look for in the daemon configuration file.
255 static const char * const config_section_name
= "sessiond";
257 /* Am I root or not. Set to 1 if the daemon is running as root */
260 /* Rotation thread handle. */
261 static struct rotation_thread_handle
*rotation_thread_handle
;
264 * Stop all threads by closing the thread quit pipe.
266 static void stop_threads(void)
270 /* Stopping all threads */
271 DBG("Terminating all threads");
272 ret
= sessiond_notify_quit_pipe();
274 ERR("write error on thread quit pipe");
277 /* Dispatch thread */
278 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
279 futex_nto1_wake(&ust_cmd_queue
.futex
);
283 * Close every consumer sockets.
285 static void close_consumer_sockets(void)
289 if (kconsumer_data
.err_sock
>= 0) {
290 ret
= close(kconsumer_data
.err_sock
);
292 PERROR("kernel consumer err_sock close");
295 if (ustconsumer32_data
.err_sock
>= 0) {
296 ret
= close(ustconsumer32_data
.err_sock
);
298 PERROR("UST consumerd32 err_sock close");
301 if (ustconsumer64_data
.err_sock
>= 0) {
302 ret
= close(ustconsumer64_data
.err_sock
);
304 PERROR("UST consumerd64 err_sock close");
307 if (kconsumer_data
.cmd_sock
>= 0) {
308 ret
= close(kconsumer_data
.cmd_sock
);
310 PERROR("kernel consumer cmd_sock close");
313 if (ustconsumer32_data
.cmd_sock
>= 0) {
314 ret
= close(ustconsumer32_data
.cmd_sock
);
316 PERROR("UST consumerd32 cmd_sock close");
319 if (ustconsumer64_data
.cmd_sock
>= 0) {
320 ret
= close(ustconsumer64_data
.cmd_sock
);
322 PERROR("UST consumerd64 cmd_sock close");
325 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
326 ret
= close(kconsumer_data
.channel_monitor_pipe
);
328 PERROR("kernel consumer channel monitor pipe close");
331 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
332 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
334 PERROR("UST consumerd32 channel monitor pipe close");
337 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
338 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
340 PERROR("UST consumerd64 channel monitor pipe close");
346 * Wait on consumer process termination.
348 * Need to be called with the consumer data lock held or from a context
349 * ensuring no concurrent access to data (e.g: cleanup).
351 static void wait_consumer(struct consumer_data
*consumer_data
)
356 if (consumer_data
->pid
<= 0) {
360 DBG("Waiting for complete teardown of consumerd (PID: %d)",
362 ret
= waitpid(consumer_data
->pid
, &status
, 0);
364 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
365 } else if (!WIFEXITED(status
)) {
366 ERR("consumerd termination with error: %d",
369 consumer_data
->pid
= 0;
373 * Cleanup the session daemon's data structures.
375 static void sessiond_cleanup(void)
378 struct ltt_session
*sess
, *stmp
;
379 struct ltt_session_list
*session_list
= session_get_list();
381 DBG("Cleanup sessiond");
384 * Close the thread quit pipe. It has already done its job,
385 * since we are now called.
387 sessiond_close_quit_pipe();
389 ret
= remove(config
.pid_file_path
.value
);
391 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
394 DBG("Removing sessiond and consumerd content of directory %s",
395 config
.rundir
.value
);
398 DBG("Removing %s", config
.pid_file_path
.value
);
399 (void) unlink(config
.pid_file_path
.value
);
401 DBG("Removing %s", config
.agent_port_file_path
.value
);
402 (void) unlink(config
.agent_port_file_path
.value
);
405 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
406 (void) unlink(kconsumer_data
.err_unix_sock_path
);
408 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
409 (void) rmdir(config
.kconsumerd_path
.value
);
411 /* ust consumerd 32 */
412 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
413 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
415 DBG("Removing directory %s", config
.consumerd32_path
.value
);
416 (void) rmdir(config
.consumerd32_path
.value
);
418 /* ust consumerd 64 */
419 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
420 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
422 DBG("Removing directory %s", config
.consumerd64_path
.value
);
423 (void) rmdir(config
.consumerd64_path
.value
);
425 DBG("Cleaning up all sessions");
427 /* Destroy session list mutex */
430 /* Cleanup ALL session */
431 cds_list_for_each_entry_safe(sess
, stmp
,
432 &session_list
->head
, list
) {
433 if (sess
->destroyed
) {
436 cmd_destroy_session(sess
,
437 notification_thread_handle
);
439 session_unlock_list();
440 pthread_mutex_destroy(&session_list
->lock
);
443 wait_consumer(&kconsumer_data
);
444 wait_consumer(&ustconsumer64_data
);
445 wait_consumer(&ustconsumer32_data
);
447 DBG("Cleaning up all agent apps");
448 agent_app_ht_clean();
450 DBG("Closing all UST sockets");
451 ust_app_clean_list();
452 buffer_reg_destroy_registries();
454 if (is_root
&& !config
.no_kernel
) {
455 DBG2("Closing kernel fd");
456 if (kernel_tracer_fd
>= 0) {
457 ret
= close(kernel_tracer_fd
);
462 DBG("Unloading kernel modules");
463 modprobe_remove_lttng_all();
467 close_consumer_sockets();
470 load_session_destroy_data(load_info
);
475 * We do NOT rmdir rundir because there are other processes
476 * using it, for instance lttng-relayd, which can start in
477 * parallel with this teardown.
482 * Cleanup the daemon's option data structures.
484 static void sessiond_cleanup_options(void)
486 DBG("Cleaning up options");
488 sessiond_config_fini(&config
);
490 run_as_destroy_worker();
494 * Send data on a unix socket using the liblttsessiondcomm API.
496 * Return lttcomm error code.
498 static int send_unix_sock(int sock
, void *buf
, size_t len
)
500 /* Check valid length */
505 return lttcomm_send_unix_sock(sock
, buf
, len
);
509 * Free memory of a command context structure.
511 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
513 DBG("Clean command context structure");
515 if ((*cmd_ctx
)->llm
) {
516 free((*cmd_ctx
)->llm
);
518 if ((*cmd_ctx
)->lsm
) {
519 free((*cmd_ctx
)->lsm
);
527 * Notify UST applications using the shm mmap futex.
529 static int notify_ust_apps(int active
)
533 DBG("Notifying applications of session daemon state: %d", active
);
535 /* See shm.c for this call implying mmap, shm and futex calls */
536 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
537 if (wait_shm_mmap
== NULL
) {
541 /* Wake waiting process */
542 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
544 /* Apps notified successfully */
552 * Setup the outgoing data buffer for the response (llm) by allocating the
553 * right amount of memory and copying the original information from the lsm
556 * Return 0 on success, negative value on error.
558 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
559 const void *payload_buf
, size_t payload_len
,
560 const void *cmd_header_buf
, size_t cmd_header_len
)
563 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
564 const size_t cmd_header_offset
= header_len
;
565 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
566 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
568 cmd_ctx
->llm
= zmalloc(total_msg_size
);
570 if (cmd_ctx
->llm
== NULL
) {
576 /* Copy common data */
577 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
578 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
579 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
580 cmd_ctx
->llm
->data_size
= payload_len
;
581 cmd_ctx
->lttng_msg_size
= total_msg_size
;
583 /* Copy command header */
584 if (cmd_header_len
) {
585 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
591 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
600 * Version of setup_lttng_msg() without command header.
602 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
603 void *payload_buf
, size_t payload_len
)
605 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
608 * Update the kernel poll set of all channel fd available over all tracing
609 * session. Add the wakeup pipe at the end of the set.
611 static int update_kernel_poll(struct lttng_poll_event
*events
)
614 struct ltt_kernel_channel
*channel
;
615 struct ltt_session
*session
;
616 const struct ltt_session_list
*session_list
= session_get_list();
618 DBG("Updating kernel poll set");
621 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
622 if (!session_get(session
)) {
625 session_lock(session
);
626 if (session
->kernel_session
== NULL
) {
627 session_unlock(session
);
628 session_put(session
);
632 cds_list_for_each_entry(channel
,
633 &session
->kernel_session
->channel_list
.head
, list
) {
634 /* Add channel fd to the kernel poll set */
635 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
637 session_unlock(session
);
638 session_put(session
);
641 DBG("Channel fd %d added to kernel set", channel
->fd
);
643 session_unlock(session
);
645 session_unlock_list();
650 session_unlock_list();
655 * Find the channel fd from 'fd' over all tracing session. When found, check
656 * for new channel stream and send those stream fds to the kernel consumer.
658 * Useful for CPU hotplug feature.
660 static int update_kernel_stream(int fd
)
663 struct ltt_session
*session
;
664 struct ltt_kernel_session
*ksess
;
665 struct ltt_kernel_channel
*channel
;
666 const struct ltt_session_list
*session_list
= session_get_list();
668 DBG("Updating kernel streams for channel fd %d", fd
);
671 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
672 if (!session_get(session
)) {
675 session_lock(session
);
676 if (session
->kernel_session
== NULL
) {
677 session_unlock(session
);
678 session_put(session
);
681 ksess
= session
->kernel_session
;
683 cds_list_for_each_entry(channel
,
684 &ksess
->channel_list
.head
, list
) {
685 struct lttng_ht_iter iter
;
686 struct consumer_socket
*socket
;
688 if (channel
->fd
!= fd
) {
691 DBG("Channel found, updating kernel streams");
692 ret
= kernel_open_channel_stream(channel
);
696 /* Update the stream global counter */
697 ksess
->stream_count_global
+= ret
;
700 * Have we already sent fds to the consumer? If yes, it
701 * means that tracing is started so it is safe to send
702 * our updated stream fds.
704 if (ksess
->consumer_fds_sent
!= 1
705 || ksess
->consumer
== NULL
) {
711 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
712 &iter
.iter
, socket
, node
.node
) {
713 pthread_mutex_lock(socket
->lock
);
714 ret
= kernel_consumer_send_channel_streams(socket
,
716 session
->output_traces
? 1 : 0);
717 pthread_mutex_unlock(socket
->lock
);
725 session_unlock(session
);
726 session_put(session
);
728 session_unlock_list();
732 session_unlock(session
);
733 session_put(session
);
734 session_unlock_list();
739 * For each tracing session, update newly registered apps. The session list
740 * lock MUST be acquired before calling this.
742 static void update_ust_app(int app_sock
)
744 struct ltt_session
*sess
, *stmp
;
745 const struct ltt_session_list
*session_list
= session_get_list();
747 /* Consumer is in an ERROR state. Stop any application update. */
748 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
749 /* Stop the update process since the consumer is dead. */
753 /* For all tracing session(s) */
754 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
757 if (!session_get(sess
)) {
761 if (!sess
->ust_session
) {
766 assert(app_sock
>= 0);
767 app
= ust_app_find_by_sock(app_sock
);
770 * Application can be unregistered before so
771 * this is possible hence simply stopping the
774 DBG3("UST app update failed to find app sock %d",
778 ust_app_global_update(sess
->ust_session
, app
);
782 session_unlock(sess
);
788 * This thread manage event coming from the kernel.
790 * Features supported in this thread:
793 static void *thread_manage_kernel(void *data
)
795 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
796 uint32_t revents
, nb_fd
;
798 struct lttng_poll_event events
;
800 DBG("[thread] Thread manage kernel started");
802 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
805 * This first step of the while is to clean this structure which could free
806 * non NULL pointers so initialize it before the loop.
808 lttng_poll_init(&events
);
810 if (testpoint(sessiond_thread_manage_kernel
)) {
811 goto error_testpoint
;
814 health_code_update();
816 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
817 goto error_testpoint
;
821 health_code_update();
823 if (update_poll_flag
== 1) {
824 /* Clean events object. We are about to populate it again. */
825 lttng_poll_clean(&events
);
827 ret
= sessiond_set_thread_pollset(&events
, 2);
829 goto error_poll_create
;
832 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
837 /* This will add the available kernel channel if any. */
838 ret
= update_kernel_poll(&events
);
842 update_poll_flag
= 0;
845 DBG("Thread kernel polling");
847 /* Poll infinite value of time */
850 ret
= lttng_poll_wait(&events
, -1);
851 DBG("Thread kernel return from poll on %d fds",
852 LTTNG_POLL_GETNB(&events
));
856 * Restart interrupted system call.
858 if (errno
== EINTR
) {
862 } else if (ret
== 0) {
863 /* Should not happen since timeout is infinite */
864 ERR("Return value of poll is 0 with an infinite timeout.\n"
865 "This should not have happened! Continuing...");
871 for (i
= 0; i
< nb_fd
; i
++) {
872 /* Fetch once the poll data */
873 revents
= LTTNG_POLL_GETEV(&events
, i
);
874 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
876 health_code_update();
879 /* No activity for this FD (poll implementation). */
883 /* Thread quit pipe has been closed. Killing thread. */
884 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
890 /* Check for data on kernel pipe */
891 if (revents
& LPOLLIN
) {
892 if (pollfd
== kernel_poll_pipe
[0]) {
893 (void) lttng_read(kernel_poll_pipe
[0],
896 * Ret value is useless here, if this pipe gets any actions an
897 * update is required anyway.
899 update_poll_flag
= 1;
903 * New CPU detected by the kernel. Adding kernel stream to
904 * kernel session and updating the kernel consumer
906 ret
= update_kernel_stream(pollfd
);
912 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
913 update_poll_flag
= 1;
916 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
924 lttng_poll_clean(&events
);
927 utils_close_pipe(kernel_poll_pipe
);
928 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
931 ERR("Health error occurred in %s", __func__
);
932 WARN("Kernel thread died unexpectedly. "
933 "Kernel tracing can continue but CPU hotplug is disabled.");
935 health_unregister(health_sessiond
);
936 DBG("Kernel thread dying");
941 * Signal pthread condition of the consumer data that the thread.
943 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
945 pthread_mutex_lock(&data
->cond_mutex
);
948 * The state is set before signaling. It can be any value, it's the waiter
949 * job to correctly interpret this condition variable associated to the
950 * consumer pthread_cond.
952 * A value of 0 means that the corresponding thread of the consumer data
953 * was not started. 1 indicates that the thread has started and is ready
954 * for action. A negative value means that there was an error during the
957 data
->consumer_thread_is_ready
= state
;
958 (void) pthread_cond_signal(&data
->cond
);
960 pthread_mutex_unlock(&data
->cond_mutex
);
964 * This thread manage the consumer error sent back to the session daemon.
966 static void *thread_manage_consumer(void *data
)
968 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
969 uint32_t revents
, nb_fd
;
970 enum lttcomm_return_code code
;
971 struct lttng_poll_event events
;
972 struct consumer_data
*consumer_data
= data
;
973 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
975 DBG("[thread] Manage consumer started");
977 rcu_register_thread();
980 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
982 health_code_update();
985 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
986 * metadata_sock. Nothing more will be added to this poll set.
988 ret
= sessiond_set_thread_pollset(&events
, 3);
994 * The error socket here is already in a listening state which was done
995 * just before spawning this thread to avoid a race between the consumer
996 * daemon exec trying to connect and the listen() call.
998 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1003 health_code_update();
1005 /* Infinite blocking call, waiting for transmission */
1007 health_poll_entry();
1009 if (testpoint(sessiond_thread_manage_consumer
)) {
1013 ret
= lttng_poll_wait(&events
, -1);
1017 * Restart interrupted system call.
1019 if (errno
== EINTR
) {
1027 for (i
= 0; i
< nb_fd
; i
++) {
1028 /* Fetch once the poll data */
1029 revents
= LTTNG_POLL_GETEV(&events
, i
);
1030 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1032 health_code_update();
1035 /* No activity for this FD (poll implementation). */
1039 /* Thread quit pipe has been closed. Killing thread. */
1040 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1046 /* Event on the registration socket */
1047 if (pollfd
== consumer_data
->err_sock
) {
1048 if (revents
& LPOLLIN
) {
1050 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1051 ERR("consumer err socket poll error");
1054 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1060 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1066 * Set the CLOEXEC flag. Return code is useless because either way, the
1069 (void) utils_set_fd_cloexec(sock
);
1071 health_code_update();
1073 DBG2("Receiving code from consumer err_sock");
1075 /* Getting status code from kconsumerd */
1076 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1077 sizeof(enum lttcomm_return_code
));
1082 health_code_update();
1083 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1084 ERR("consumer error when waiting for SOCK_READY : %s",
1085 lttcomm_get_readable_code(-code
));
1089 /* Connect both command and metadata sockets. */
1090 consumer_data
->cmd_sock
=
1091 lttcomm_connect_unix_sock(
1092 consumer_data
->cmd_unix_sock_path
);
1093 consumer_data
->metadata_fd
=
1094 lttcomm_connect_unix_sock(
1095 consumer_data
->cmd_unix_sock_path
);
1096 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1097 PERROR("consumer connect cmd socket");
1098 /* On error, signal condition and quit. */
1099 signal_consumer_condition(consumer_data
, -1);
1103 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1105 /* Create metadata socket lock. */
1106 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1107 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1108 PERROR("zmalloc pthread mutex");
1111 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1113 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1114 DBG("Consumer metadata socket ready (fd: %d)",
1115 consumer_data
->metadata_fd
);
1118 * Remove the consumerd error sock since we've established a connection.
1120 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1125 /* Add new accepted error socket. */
1126 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1131 /* Add metadata socket that is successfully connected. */
1132 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1133 LPOLLIN
| LPOLLRDHUP
);
1138 health_code_update();
1141 * Transfer the write-end of the channel monitoring and rotate pipe
1142 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1144 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1145 if (!cmd_socket_wrapper
) {
1148 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1150 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1151 consumer_data
->channel_monitor_pipe
);
1156 /* Discard the socket wrapper as it is no longer needed. */
1157 consumer_destroy_socket(cmd_socket_wrapper
);
1158 cmd_socket_wrapper
= NULL
;
1160 /* The thread is completely initialized, signal that it is ready. */
1161 signal_consumer_condition(consumer_data
, 1);
1163 /* Infinite blocking call, waiting for transmission */
1166 health_code_update();
1168 /* Exit the thread because the thread quit pipe has been triggered. */
1170 /* Not a health error. */
1175 health_poll_entry();
1176 ret
= lttng_poll_wait(&events
, -1);
1180 * Restart interrupted system call.
1182 if (errno
== EINTR
) {
1190 for (i
= 0; i
< nb_fd
; i
++) {
1191 /* Fetch once the poll data */
1192 revents
= LTTNG_POLL_GETEV(&events
, i
);
1193 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1195 health_code_update();
1198 /* No activity for this FD (poll implementation). */
1203 * Thread quit pipe has been triggered, flag that we should stop
1204 * but continue the current loop to handle potential data from
1207 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1209 if (pollfd
== sock
) {
1210 /* Event on the consumerd socket */
1211 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1212 && !(revents
& LPOLLIN
)) {
1213 ERR("consumer err socket second poll error");
1216 health_code_update();
1217 /* Wait for any kconsumerd error */
1218 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1219 sizeof(enum lttcomm_return_code
));
1221 ERR("consumer closed the command socket");
1225 ERR("consumer return code : %s",
1226 lttcomm_get_readable_code(-code
));
1229 } else if (pollfd
== consumer_data
->metadata_fd
) {
1230 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1231 && !(revents
& LPOLLIN
)) {
1232 ERR("consumer err metadata socket second poll error");
1235 /* UST metadata requests */
1236 ret
= ust_consumer_metadata_request(
1237 &consumer_data
->metadata_sock
);
1239 ERR("Handling metadata request");
1243 /* No need for an else branch all FDs are tested prior. */
1245 health_code_update();
1251 * We lock here because we are about to close the sockets and some other
1252 * thread might be using them so get exclusive access which will abort all
1253 * other consumer command by other threads.
1255 pthread_mutex_lock(&consumer_data
->lock
);
1257 /* Immediately set the consumerd state to stopped */
1258 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1259 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1260 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1261 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1262 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1264 /* Code flow error... */
1268 if (consumer_data
->err_sock
>= 0) {
1269 ret
= close(consumer_data
->err_sock
);
1273 consumer_data
->err_sock
= -1;
1275 if (consumer_data
->cmd_sock
>= 0) {
1276 ret
= close(consumer_data
->cmd_sock
);
1280 consumer_data
->cmd_sock
= -1;
1282 if (consumer_data
->metadata_sock
.fd_ptr
&&
1283 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1284 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1296 unlink(consumer_data
->err_unix_sock_path
);
1297 unlink(consumer_data
->cmd_unix_sock_path
);
1298 pthread_mutex_unlock(&consumer_data
->lock
);
1300 /* Cleanup metadata socket mutex. */
1301 if (consumer_data
->metadata_sock
.lock
) {
1302 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1303 free(consumer_data
->metadata_sock
.lock
);
1305 lttng_poll_clean(&events
);
1307 if (cmd_socket_wrapper
) {
1308 consumer_destroy_socket(cmd_socket_wrapper
);
1313 ERR("Health error occurred in %s", __func__
);
1315 health_unregister(health_sessiond
);
1316 DBG("consumer thread cleanup completed");
1318 rcu_thread_offline();
1319 rcu_unregister_thread();
1325 * This thread receives application command sockets (FDs) on the
1326 * apps_cmd_pipe and waits (polls) on them until they are closed
1327 * or an error occurs.
1329 * At that point, it flushes the data (tracing and metadata) associated
1330 * with this application and tears down ust app sessions and other
1331 * associated data structures through ust_app_unregister().
1333 * Note that this thread never sends commands to the applications
1334 * through the command sockets; it merely listens for hang-ups
1335 * and errors on those sockets and cleans-up as they occur.
1337 static void *thread_manage_apps(void *data
)
1339 int i
, ret
, pollfd
, err
= -1;
1341 uint32_t revents
, nb_fd
;
1342 struct lttng_poll_event events
;
1344 DBG("[thread] Manage application started");
1346 rcu_register_thread();
1347 rcu_thread_online();
1349 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1351 if (testpoint(sessiond_thread_manage_apps
)) {
1352 goto error_testpoint
;
1355 health_code_update();
1357 ret
= sessiond_set_thread_pollset(&events
, 2);
1359 goto error_poll_create
;
1362 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1367 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1371 health_code_update();
1374 DBG("Apps thread polling");
1376 /* Inifinite blocking call, waiting for transmission */
1378 health_poll_entry();
1379 ret
= lttng_poll_wait(&events
, -1);
1380 DBG("Apps thread return from poll on %d fds",
1381 LTTNG_POLL_GETNB(&events
));
1385 * Restart interrupted system call.
1387 if (errno
== EINTR
) {
1395 for (i
= 0; i
< nb_fd
; i
++) {
1396 /* Fetch once the poll data */
1397 revents
= LTTNG_POLL_GETEV(&events
, i
);
1398 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1400 health_code_update();
1403 /* No activity for this FD (poll implementation). */
1407 /* Thread quit pipe has been closed. Killing thread. */
1408 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1414 /* Inspect the apps cmd pipe */
1415 if (pollfd
== apps_cmd_pipe
[0]) {
1416 if (revents
& LPOLLIN
) {
1420 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1421 if (size_ret
< sizeof(sock
)) {
1422 PERROR("read apps cmd pipe");
1426 health_code_update();
1429 * Since this is a command socket (write then read),
1430 * we only monitor the error events of the socket.
1432 ret
= lttng_poll_add(&events
, sock
,
1433 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1438 DBG("Apps with sock %d added to poll set", sock
);
1439 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1440 ERR("Apps command pipe error");
1443 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1448 * At this point, we know that a registered application made
1449 * the event at poll_wait.
1451 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1452 /* Removing from the poll set */
1453 ret
= lttng_poll_del(&events
, pollfd
);
1458 /* Socket closed on remote end. */
1459 ust_app_unregister(pollfd
);
1461 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1466 health_code_update();
1472 lttng_poll_clean(&events
);
1475 utils_close_pipe(apps_cmd_pipe
);
1476 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1479 * We don't clean the UST app hash table here since already registered
1480 * applications can still be controlled so let them be until the session
1481 * daemon dies or the applications stop.
1486 ERR("Health error occurred in %s", __func__
);
1488 health_unregister(health_sessiond
);
1489 DBG("Application communication apps thread cleanup complete");
1490 rcu_thread_offline();
1491 rcu_unregister_thread();
1496 * Send a socket to a thread This is called from the dispatch UST registration
1497 * thread once all sockets are set for the application.
1499 * The sock value can be invalid, we don't really care, the thread will handle
1500 * it and make the necessary cleanup if so.
1502 * On success, return 0 else a negative value being the errno message of the
1505 static int send_socket_to_thread(int fd
, int sock
)
1510 * It's possible that the FD is set as invalid with -1 concurrently just
1511 * before calling this function being a shutdown state of the thread.
1518 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1519 if (ret
< sizeof(sock
)) {
1520 PERROR("write apps pipe %d", fd
);
1527 /* All good. Don't send back the write positive ret value. */
1534 * Sanitize the wait queue of the dispatch registration thread meaning removing
1535 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1536 * notify socket is never received.
1538 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1540 int ret
, nb_fd
= 0, i
;
1541 unsigned int fd_added
= 0;
1542 struct lttng_poll_event events
;
1543 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1547 lttng_poll_init(&events
);
1549 /* Just skip everything for an empty queue. */
1550 if (!wait_queue
->count
) {
1554 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1559 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1560 &wait_queue
->head
, head
) {
1561 assert(wait_node
->app
);
1562 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1563 LPOLLHUP
| LPOLLERR
);
1576 * Poll but don't block so we can quickly identify the faulty events and
1577 * clean them afterwards from the wait queue.
1579 ret
= lttng_poll_wait(&events
, 0);
1585 for (i
= 0; i
< nb_fd
; i
++) {
1586 /* Get faulty FD. */
1587 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1588 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1591 /* No activity for this FD (poll implementation). */
1595 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1596 &wait_queue
->head
, head
) {
1597 if (pollfd
== wait_node
->app
->sock
&&
1598 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1599 cds_list_del(&wait_node
->head
);
1600 wait_queue
->count
--;
1601 ust_app_destroy(wait_node
->app
);
1604 * Silence warning of use-after-free in
1605 * cds_list_for_each_entry_safe which uses
1606 * __typeof__(*wait_node).
1611 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1618 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1622 lttng_poll_clean(&events
);
1626 lttng_poll_clean(&events
);
1628 ERR("Unable to sanitize wait queue");
1633 * Dispatch request from the registration threads to the application
1634 * communication thread.
1636 static void *thread_dispatch_ust_registration(void *data
)
1639 struct cds_wfcq_node
*node
;
1640 struct ust_command
*ust_cmd
= NULL
;
1641 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1642 struct ust_reg_wait_queue wait_queue
= {
1646 rcu_register_thread();
1648 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1650 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1651 goto error_testpoint
;
1654 health_code_update();
1656 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1658 DBG("[thread] Dispatch UST command started");
1661 health_code_update();
1663 /* Atomically prepare the queue futex */
1664 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1666 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1671 struct ust_app
*app
= NULL
;
1675 * Make sure we don't have node(s) that have hung up before receiving
1676 * the notify socket. This is to clean the list in order to avoid
1677 * memory leaks from notify socket that are never seen.
1679 sanitize_wait_queue(&wait_queue
);
1681 health_code_update();
1682 /* Dequeue command for registration */
1683 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1685 DBG("Woken up but nothing in the UST command queue");
1686 /* Continue thread execution */
1690 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1692 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1693 " gid:%d sock:%d name:%s (version %d.%d)",
1694 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1695 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1696 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1697 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1699 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1700 wait_node
= zmalloc(sizeof(*wait_node
));
1702 PERROR("zmalloc wait_node dispatch");
1703 ret
= close(ust_cmd
->sock
);
1705 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1707 lttng_fd_put(LTTNG_FD_APPS
, 1);
1711 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1713 /* Create application object if socket is CMD. */
1714 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1716 if (!wait_node
->app
) {
1717 ret
= close(ust_cmd
->sock
);
1719 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1721 lttng_fd_put(LTTNG_FD_APPS
, 1);
1727 * Add application to the wait queue so we can set the notify
1728 * socket before putting this object in the global ht.
1730 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1735 * We have to continue here since we don't have the notify
1736 * socket and the application MUST be added to the hash table
1737 * only at that moment.
1742 * Look for the application in the local wait queue and set the
1743 * notify socket if found.
1745 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1746 &wait_queue
.head
, head
) {
1747 health_code_update();
1748 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1749 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1750 cds_list_del(&wait_node
->head
);
1752 app
= wait_node
->app
;
1754 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1760 * With no application at this stage the received socket is
1761 * basically useless so close it before we free the cmd data
1762 * structure for good.
1765 ret
= close(ust_cmd
->sock
);
1767 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1769 lttng_fd_put(LTTNG_FD_APPS
, 1);
1776 * @session_lock_list
1778 * Lock the global session list so from the register up to the
1779 * registration done message, no thread can see the application
1780 * and change its state.
1782 session_lock_list();
1786 * Add application to the global hash table. This needs to be
1787 * done before the update to the UST registry can locate the
1792 /* Set app version. This call will print an error if needed. */
1793 (void) ust_app_version(app
);
1795 /* Send notify socket through the notify pipe. */
1796 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1800 session_unlock_list();
1802 * No notify thread, stop the UST tracing. However, this is
1803 * not an internal error of the this thread thus setting
1804 * the health error code to a normal exit.
1811 * Update newly registered application with the tracing
1812 * registry info already enabled information.
1814 update_ust_app(app
->sock
);
1817 * Don't care about return value. Let the manage apps threads
1818 * handle app unregistration upon socket close.
1820 (void) ust_app_register_done(app
);
1823 * Even if the application socket has been closed, send the app
1824 * to the thread and unregistration will take place at that
1827 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1830 session_unlock_list();
1832 * No apps. thread, stop the UST tracing. However, this is
1833 * not an internal error of the this thread thus setting
1834 * the health error code to a normal exit.
1841 session_unlock_list();
1843 } while (node
!= NULL
);
1845 health_poll_entry();
1846 /* Futex wait on queue. Blocking call on futex() */
1847 futex_nto1_wait(&ust_cmd_queue
.futex
);
1850 /* Normal exit, no error */
1854 /* Clean up wait queue. */
1855 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1856 &wait_queue
.head
, head
) {
1857 cds_list_del(&wait_node
->head
);
1862 /* Empty command queue. */
1864 /* Dequeue command for registration */
1865 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1869 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1870 ret
= close(ust_cmd
->sock
);
1872 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1874 lttng_fd_put(LTTNG_FD_APPS
, 1);
1879 DBG("Dispatch thread dying");
1882 ERR("Health error occurred in %s", __func__
);
1884 health_unregister(health_sessiond
);
1885 rcu_unregister_thread();
1890 * This thread manage application registration.
1892 static void *thread_registration_apps(void *data
)
1894 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1895 uint32_t revents
, nb_fd
;
1896 struct lttng_poll_event events
;
1898 * Get allocated in this thread, enqueued to a global queue, dequeued and
1899 * freed in the manage apps thread.
1901 struct ust_command
*ust_cmd
= NULL
;
1903 DBG("[thread] Manage application registration started");
1905 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1907 if (testpoint(sessiond_thread_registration_apps
)) {
1908 goto error_testpoint
;
1911 ret
= lttcomm_listen_unix_sock(apps_sock
);
1917 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1918 * more will be added to this poll set.
1920 ret
= sessiond_set_thread_pollset(&events
, 2);
1922 goto error_create_poll
;
1925 /* Add the application registration socket */
1926 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1928 goto error_poll_add
;
1931 /* Notify all applications to register */
1932 ret
= notify_ust_apps(1);
1934 ERR("Failed to notify applications or create the wait shared memory.\n"
1935 "Execution continues but there might be problem for already\n"
1936 "running applications that wishes to register.");
1940 DBG("Accepting application registration");
1942 /* Inifinite blocking call, waiting for transmission */
1944 health_poll_entry();
1945 ret
= lttng_poll_wait(&events
, -1);
1949 * Restart interrupted system call.
1951 if (errno
== EINTR
) {
1959 for (i
= 0; i
< nb_fd
; i
++) {
1960 health_code_update();
1962 /* Fetch once the poll data */
1963 revents
= LTTNG_POLL_GETEV(&events
, i
);
1964 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1967 /* No activity for this FD (poll implementation). */
1971 /* Thread quit pipe has been closed. Killing thread. */
1972 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1978 /* Event on the registration socket */
1979 if (pollfd
== apps_sock
) {
1980 if (revents
& LPOLLIN
) {
1981 sock
= lttcomm_accept_unix_sock(apps_sock
);
1987 * Set socket timeout for both receiving and ending.
1988 * app_socket_timeout is in seconds, whereas
1989 * lttcomm_setsockopt_rcv_timeout and
1990 * lttcomm_setsockopt_snd_timeout expect msec as
1993 if (config
.app_socket_timeout
>= 0) {
1994 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1995 config
.app_socket_timeout
* 1000);
1996 (void) lttcomm_setsockopt_snd_timeout(sock
,
1997 config
.app_socket_timeout
* 1000);
2001 * Set the CLOEXEC flag. Return code is useless because
2002 * either way, the show must go on.
2004 (void) utils_set_fd_cloexec(sock
);
2006 /* Create UST registration command for enqueuing */
2007 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2008 if (ust_cmd
== NULL
) {
2009 PERROR("ust command zmalloc");
2018 * Using message-based transmissions to ensure we don't
2019 * have to deal with partially received messages.
2021 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2023 ERR("Exhausted file descriptors allowed for applications.");
2033 health_code_update();
2034 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2037 /* Close socket of the application. */
2042 lttng_fd_put(LTTNG_FD_APPS
, 1);
2046 health_code_update();
2048 ust_cmd
->sock
= sock
;
2051 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2052 " gid:%d sock:%d name:%s (version %d.%d)",
2053 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2054 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2055 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2056 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2059 * Lock free enqueue the registration request. The red pill
2060 * has been taken! This apps will be part of the *system*.
2062 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2065 * Wake the registration queue futex. Implicit memory
2066 * barrier with the exchange in cds_wfcq_enqueue.
2068 futex_nto1_wake(&ust_cmd_queue
.futex
);
2069 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2070 ERR("Register apps socket poll error");
2073 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2082 /* Notify that the registration thread is gone */
2085 if (apps_sock
>= 0) {
2086 ret
= close(apps_sock
);
2096 lttng_fd_put(LTTNG_FD_APPS
, 1);
2098 unlink(config
.apps_unix_sock_path
.value
);
2101 lttng_poll_clean(&events
);
2105 DBG("UST Registration thread cleanup complete");
2108 ERR("Health error occurred in %s", __func__
);
2110 health_unregister(health_sessiond
);
2116 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2117 * exec or it will fails.
2119 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2122 struct timespec timeout
;
2125 * Make sure we set the readiness flag to 0 because we are NOT ready.
2126 * This access to consumer_thread_is_ready does not need to be
2127 * protected by consumer_data.cond_mutex (yet) since the consumer
2128 * management thread has not been started at this point.
2130 consumer_data
->consumer_thread_is_ready
= 0;
2132 /* Setup pthread condition */
2133 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2136 PERROR("pthread_condattr_init consumer data");
2141 * Set the monotonic clock in order to make sure we DO NOT jump in time
2142 * between the clock_gettime() call and the timedwait call. See bug #324
2143 * for a more details and how we noticed it.
2145 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2148 PERROR("pthread_condattr_setclock consumer data");
2152 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2155 PERROR("pthread_cond_init consumer data");
2159 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2160 thread_manage_consumer
, consumer_data
);
2163 PERROR("pthread_create consumer");
2168 /* We are about to wait on a pthread condition */
2169 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2171 /* Get time for sem_timedwait absolute timeout */
2172 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2174 * Set the timeout for the condition timed wait even if the clock gettime
2175 * call fails since we might loop on that call and we want to avoid to
2176 * increment the timeout too many times.
2178 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2181 * The following loop COULD be skipped in some conditions so this is why we
2182 * set ret to 0 in order to make sure at least one round of the loop is
2188 * Loop until the condition is reached or when a timeout is reached. Note
2189 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2190 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2191 * possible. This loop does not take any chances and works with both of
2194 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2195 if (clock_ret
< 0) {
2196 PERROR("clock_gettime spawn consumer");
2197 /* Infinite wait for the consumerd thread to be ready */
2198 ret
= pthread_cond_wait(&consumer_data
->cond
,
2199 &consumer_data
->cond_mutex
);
2201 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2202 &consumer_data
->cond_mutex
, &timeout
);
2206 /* Release the pthread condition */
2207 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2211 if (ret
== ETIMEDOUT
) {
2215 * Call has timed out so we kill the kconsumerd_thread and return
2218 ERR("Condition timed out. The consumer thread was never ready."
2220 pth_ret
= pthread_cancel(consumer_data
->thread
);
2222 PERROR("pthread_cancel consumer thread");
2225 PERROR("pthread_cond_wait failed consumer thread");
2227 /* Caller is expecting a negative value on failure. */
2232 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2233 if (consumer_data
->pid
== 0) {
2234 ERR("Consumerd did not start");
2235 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2238 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2247 * Join consumer thread
2249 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2253 /* Consumer pid must be a real one. */
2254 if (consumer_data
->pid
> 0) {
2256 ret
= kill(consumer_data
->pid
, SIGTERM
);
2258 PERROR("Error killing consumer daemon");
2261 return pthread_join(consumer_data
->thread
, &status
);
2268 * Fork and exec a consumer daemon (consumerd).
2270 * Return pid if successful else -1.
2272 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2276 const char *consumer_to_use
;
2277 const char *verbosity
;
2280 DBG("Spawning consumerd");
2287 if (config
.verbose_consumer
) {
2288 verbosity
= "--verbose";
2289 } else if (lttng_opt_quiet
) {
2290 verbosity
= "--quiet";
2295 switch (consumer_data
->type
) {
2296 case LTTNG_CONSUMER_KERNEL
:
2298 * Find out which consumerd to execute. We will first try the
2299 * 64-bit path, then the sessiond's installation directory, and
2300 * fallback on the 32-bit one,
2302 DBG3("Looking for a kernel consumer at these locations:");
2303 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2304 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2305 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2306 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2307 DBG3("Found location #1");
2308 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2309 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2310 DBG3("Found location #2");
2311 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2312 } else if (config
.consumerd32_bin_path
.value
&&
2313 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2314 DBG3("Found location #3");
2315 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2317 DBG("Could not find any valid consumerd executable");
2321 DBG("Using kernel consumer at: %s", consumer_to_use
);
2322 (void) execl(consumer_to_use
,
2323 "lttng-consumerd", verbosity
, "-k",
2324 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2325 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2326 "--group", config
.tracing_group_name
.value
,
2329 case LTTNG_CONSUMER64_UST
:
2331 if (config
.consumerd64_lib_dir
.value
) {
2336 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2340 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2341 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2346 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2347 if (tmp
[0] != '\0') {
2348 strcat(tmpnew
, ":");
2349 strcat(tmpnew
, tmp
);
2351 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2358 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2359 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2360 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2361 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2362 "--group", config
.tracing_group_name
.value
,
2366 case LTTNG_CONSUMER32_UST
:
2368 if (config
.consumerd32_lib_dir
.value
) {
2373 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2377 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2378 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2383 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2384 if (tmp
[0] != '\0') {
2385 strcat(tmpnew
, ":");
2386 strcat(tmpnew
, tmp
);
2388 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2395 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2396 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2397 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2398 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2399 "--group", config
.tracing_group_name
.value
,
2404 ERR("unknown consumer type");
2408 PERROR("Consumer execl()");
2410 /* Reaching this point, we got a failure on our execl(). */
2412 } else if (pid
> 0) {
2415 PERROR("start consumer fork");
2423 * Spawn the consumerd daemon and session daemon thread.
2425 static int start_consumerd(struct consumer_data
*consumer_data
)
2430 * Set the listen() state on the socket since there is a possible race
2431 * between the exec() of the consumer daemon and this call if place in the
2432 * consumer thread. See bug #366 for more details.
2434 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2439 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2440 if (consumer_data
->pid
!= 0) {
2441 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2445 ret
= spawn_consumerd(consumer_data
);
2447 ERR("Spawning consumerd failed");
2448 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2452 /* Setting up the consumer_data pid */
2453 consumer_data
->pid
= ret
;
2454 DBG2("Consumer pid %d", consumer_data
->pid
);
2455 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2457 DBG2("Spawning consumer control thread");
2458 ret
= spawn_consumer_thread(consumer_data
);
2460 ERR("Fatal error spawning consumer control thread");
2468 /* Cleanup already created sockets on error. */
2469 if (consumer_data
->err_sock
>= 0) {
2472 err
= close(consumer_data
->err_sock
);
2474 PERROR("close consumer data error socket");
2481 * Setup necessary data for kernel tracer action.
2483 static int init_kernel_tracer(void)
2487 /* Modprobe lttng kernel modules */
2488 ret
= modprobe_lttng_control();
2493 /* Open debugfs lttng */
2494 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2495 if (kernel_tracer_fd
< 0) {
2496 DBG("Failed to open %s", module_proc_lttng
);
2500 /* Validate kernel version */
2501 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2502 &kernel_tracer_abi_version
);
2507 ret
= modprobe_lttng_data();
2512 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2519 WARN("Kernel tracer does not support buffer monitoring. "
2520 "The monitoring timer of channels in the kernel domain "
2521 "will be set to 0 (disabled).");
2524 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2528 modprobe_remove_lttng_control();
2529 ret
= close(kernel_tracer_fd
);
2533 kernel_tracer_fd
= -1;
2534 return LTTNG_ERR_KERN_VERSION
;
2537 ret
= close(kernel_tracer_fd
);
2543 modprobe_remove_lttng_control();
2546 WARN("No kernel tracer available");
2547 kernel_tracer_fd
= -1;
2549 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2551 return LTTNG_ERR_KERN_NA
;
2557 * Copy consumer output from the tracing session to the domain session. The
2558 * function also applies the right modification on a per domain basis for the
2559 * trace files destination directory.
2561 * Should *NOT* be called with RCU read-side lock held.
2563 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2566 const char *dir_name
;
2567 struct consumer_output
*consumer
;
2570 assert(session
->consumer
);
2573 case LTTNG_DOMAIN_KERNEL
:
2574 DBG3("Copying tracing session consumer output in kernel session");
2576 * XXX: We should audit the session creation and what this function
2577 * does "extra" in order to avoid a destroy since this function is used
2578 * in the domain session creation (kernel and ust) only. Same for UST
2581 if (session
->kernel_session
->consumer
) {
2582 consumer_output_put(session
->kernel_session
->consumer
);
2584 session
->kernel_session
->consumer
=
2585 consumer_copy_output(session
->consumer
);
2586 /* Ease our life a bit for the next part */
2587 consumer
= session
->kernel_session
->consumer
;
2588 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2590 case LTTNG_DOMAIN_JUL
:
2591 case LTTNG_DOMAIN_LOG4J
:
2592 case LTTNG_DOMAIN_PYTHON
:
2593 case LTTNG_DOMAIN_UST
:
2594 DBG3("Copying tracing session consumer output in UST session");
2595 if (session
->ust_session
->consumer
) {
2596 consumer_output_put(session
->ust_session
->consumer
);
2598 session
->ust_session
->consumer
=
2599 consumer_copy_output(session
->consumer
);
2600 /* Ease our life a bit for the next part */
2601 consumer
= session
->ust_session
->consumer
;
2602 dir_name
= DEFAULT_UST_TRACE_DIR
;
2605 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2609 /* Append correct directory to subdir */
2610 strncat(consumer
->subdir
, dir_name
,
2611 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2612 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2621 * Create an UST session and add it to the session ust list.
2623 * Should *NOT* be called with RCU read-side lock held.
2625 static int create_ust_session(struct ltt_session
*session
,
2626 struct lttng_domain
*domain
)
2629 struct ltt_ust_session
*lus
= NULL
;
2633 assert(session
->consumer
);
2635 switch (domain
->type
) {
2636 case LTTNG_DOMAIN_JUL
:
2637 case LTTNG_DOMAIN_LOG4J
:
2638 case LTTNG_DOMAIN_PYTHON
:
2639 case LTTNG_DOMAIN_UST
:
2642 ERR("Unknown UST domain on create session %d", domain
->type
);
2643 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2647 DBG("Creating UST session");
2649 lus
= trace_ust_create_session(session
->id
);
2651 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2655 lus
->uid
= session
->uid
;
2656 lus
->gid
= session
->gid
;
2657 lus
->output_traces
= session
->output_traces
;
2658 lus
->snapshot_mode
= session
->snapshot_mode
;
2659 lus
->live_timer_interval
= session
->live_timer
;
2660 session
->ust_session
= lus
;
2661 if (session
->shm_path
[0]) {
2662 strncpy(lus
->root_shm_path
, session
->shm_path
,
2663 sizeof(lus
->root_shm_path
));
2664 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2665 strncpy(lus
->shm_path
, session
->shm_path
,
2666 sizeof(lus
->shm_path
));
2667 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2668 strncat(lus
->shm_path
, "/ust",
2669 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2671 /* Copy session output to the newly created UST session */
2672 ret
= copy_session_consumer(domain
->type
, session
);
2673 if (ret
!= LTTNG_OK
) {
2681 session
->ust_session
= NULL
;
2686 * Create a kernel tracer session then create the default channel.
2688 static int create_kernel_session(struct ltt_session
*session
)
2692 DBG("Creating kernel session");
2694 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2696 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2700 /* Code flow safety */
2701 assert(session
->kernel_session
);
2703 /* Copy session output to the newly created Kernel session */
2704 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2705 if (ret
!= LTTNG_OK
) {
2709 session
->kernel_session
->uid
= session
->uid
;
2710 session
->kernel_session
->gid
= session
->gid
;
2711 session
->kernel_session
->output_traces
= session
->output_traces
;
2712 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2717 trace_kernel_destroy_session(session
->kernel_session
);
2718 session
->kernel_session
= NULL
;
2723 * Count number of session permitted by uid/gid.
2725 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2728 struct ltt_session
*session
;
2729 const struct ltt_session_list
*session_list
= session_get_list();
2731 DBG("Counting number of available session for UID %d GID %d",
2733 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2734 if (!session_get(session
)) {
2737 session_lock(session
);
2738 /* Only count the sessions the user can control. */
2739 if (session_access_ok(session
, uid
, gid
) &&
2740 !session
->destroyed
) {
2743 session_unlock(session
);
2744 session_put(session
);
2749 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2750 int *sock_error
, struct lttng_event
*event
)
2753 struct lttng_userspace_probe_location
*probe_location
;
2754 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2755 struct lttng_dynamic_buffer probe_location_buffer
;
2756 struct lttng_buffer_view buffer_view
;
2759 * Create a buffer to store the serialized version of the probe
2762 lttng_dynamic_buffer_init(&probe_location_buffer
);
2763 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2764 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2766 ret
= LTTNG_ERR_NOMEM
;
2771 * Receive the probe location.
2773 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2774 probe_location_buffer
.size
);
2776 DBG("Nothing recv() from client var len data... continuing");
2778 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2779 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2783 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2784 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2787 * Extract the probe location from the serialized version.
2789 ret
= lttng_userspace_probe_location_create_from_buffer(
2790 &buffer_view
, &probe_location
);
2792 WARN("Failed to create a userspace probe location from the received buffer");
2793 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2794 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2799 * Receive the file descriptor to the target binary from the client.
2801 DBG("Receiving userspace probe target FD from client ...");
2802 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2804 DBG("Nothing recv() from client userspace probe fd... continuing");
2806 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2811 * Set the file descriptor received from the client through the unix
2812 * socket in the probe location.
2814 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2816 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2821 * From the kernel tracer's perspective, all userspace probe event types
2822 * are all the same: a file and an offset.
2824 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2825 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2826 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2827 probe_location
, fd
);
2829 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2830 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2831 probe_location
, fd
);
2834 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2839 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2843 /* Attach the probe location to the event. */
2844 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2846 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2850 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2856 * Check if the current kernel tracer supports the session rotation feature.
2857 * Return 1 if it does, 0 otherwise.
2859 static int check_rotate_compatible(void)
2863 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2864 DBG("Kernel tracer version is not compatible with the rotation feature");
2872 * Process the command requested by the lttng client within the command
2873 * context structure. This function make sure that the return structure (llm)
2874 * is set and ready for transmission before returning.
2876 * Return any error encountered or 0 for success.
2878 * "sock" is only used for special-case var. len data.
2880 * Should *NOT* be called with RCU read-side lock held.
2882 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2886 int need_tracing_session
= 1;
2889 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2891 assert(!rcu_read_ongoing());
2895 switch (cmd_ctx
->lsm
->cmd_type
) {
2896 case LTTNG_CREATE_SESSION
:
2897 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2898 case LTTNG_CREATE_SESSION_LIVE
:
2899 case LTTNG_DESTROY_SESSION
:
2900 case LTTNG_LIST_SESSIONS
:
2901 case LTTNG_LIST_DOMAINS
:
2902 case LTTNG_START_TRACE
:
2903 case LTTNG_STOP_TRACE
:
2904 case LTTNG_DATA_PENDING
:
2905 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2906 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2907 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2908 case LTTNG_SNAPSHOT_RECORD
:
2909 case LTTNG_SAVE_SESSION
:
2910 case LTTNG_SET_SESSION_SHM_PATH
:
2911 case LTTNG_REGENERATE_METADATA
:
2912 case LTTNG_REGENERATE_STATEDUMP
:
2913 case LTTNG_REGISTER_TRIGGER
:
2914 case LTTNG_UNREGISTER_TRIGGER
:
2915 case LTTNG_ROTATE_SESSION
:
2916 case LTTNG_ROTATION_GET_INFO
:
2917 case LTTNG_ROTATION_SET_SCHEDULE
:
2918 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2925 if (config
.no_kernel
&& need_domain
2926 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2928 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2930 ret
= LTTNG_ERR_KERN_NA
;
2935 /* Deny register consumer if we already have a spawned consumer. */
2936 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2937 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2938 if (kconsumer_data
.pid
> 0) {
2939 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2940 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2943 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2947 * Check for command that don't needs to allocate a returned payload. We do
2948 * this here so we don't have to make the call for no payload at each
2951 switch(cmd_ctx
->lsm
->cmd_type
) {
2952 case LTTNG_LIST_SESSIONS
:
2953 case LTTNG_LIST_TRACEPOINTS
:
2954 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2955 case LTTNG_LIST_DOMAINS
:
2956 case LTTNG_LIST_CHANNELS
:
2957 case LTTNG_LIST_EVENTS
:
2958 case LTTNG_LIST_SYSCALLS
:
2959 case LTTNG_LIST_TRACKER_PIDS
:
2960 case LTTNG_DATA_PENDING
:
2961 case LTTNG_ROTATE_SESSION
:
2962 case LTTNG_ROTATION_GET_INFO
:
2963 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2966 /* Setup lttng message with no payload */
2967 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2969 /* This label does not try to unlock the session */
2970 goto init_setup_error
;
2974 /* Commands that DO NOT need a session. */
2975 switch (cmd_ctx
->lsm
->cmd_type
) {
2976 case LTTNG_CREATE_SESSION
:
2977 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2978 case LTTNG_CREATE_SESSION_LIVE
:
2979 case LTTNG_LIST_SESSIONS
:
2980 case LTTNG_LIST_TRACEPOINTS
:
2981 case LTTNG_LIST_SYSCALLS
:
2982 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2983 case LTTNG_SAVE_SESSION
:
2984 case LTTNG_REGISTER_TRIGGER
:
2985 case LTTNG_UNREGISTER_TRIGGER
:
2986 need_tracing_session
= 0;
2989 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2991 * We keep the session list lock across _all_ commands
2992 * for now, because the per-session lock does not
2993 * handle teardown properly.
2995 session_lock_list();
2996 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2997 if (cmd_ctx
->session
== NULL
) {
2998 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3001 /* Acquire lock for the session */
3002 session_lock(cmd_ctx
->session
);
3008 * Commands that need a valid session but should NOT create one if none
3009 * exists. Instead of creating one and destroying it when the command is
3010 * handled, process that right before so we save some round trip in useless
3013 switch (cmd_ctx
->lsm
->cmd_type
) {
3014 case LTTNG_DISABLE_CHANNEL
:
3015 case LTTNG_DISABLE_EVENT
:
3016 switch (cmd_ctx
->lsm
->domain
.type
) {
3017 case LTTNG_DOMAIN_KERNEL
:
3018 if (!cmd_ctx
->session
->kernel_session
) {
3019 ret
= LTTNG_ERR_NO_CHANNEL
;
3023 case LTTNG_DOMAIN_JUL
:
3024 case LTTNG_DOMAIN_LOG4J
:
3025 case LTTNG_DOMAIN_PYTHON
:
3026 case LTTNG_DOMAIN_UST
:
3027 if (!cmd_ctx
->session
->ust_session
) {
3028 ret
= LTTNG_ERR_NO_CHANNEL
;
3033 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3045 * Check domain type for specific "pre-action".
3047 switch (cmd_ctx
->lsm
->domain
.type
) {
3048 case LTTNG_DOMAIN_KERNEL
:
3050 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3054 /* Kernel tracer check */
3055 if (kernel_tracer_fd
== -1) {
3056 /* Basically, load kernel tracer modules */
3057 ret
= init_kernel_tracer();
3063 /* Consumer is in an ERROR state. Report back to client */
3064 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3065 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3069 /* Need a session for kernel command */
3070 if (need_tracing_session
) {
3071 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3072 ret
= create_kernel_session(cmd_ctx
->session
);
3074 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3079 /* Start the kernel consumer daemon */
3080 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3081 if (kconsumer_data
.pid
== 0 &&
3082 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3083 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3084 ret
= start_consumerd(&kconsumer_data
);
3086 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3089 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3091 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3095 * The consumer was just spawned so we need to add the socket to
3096 * the consumer output of the session if exist.
3098 ret
= consumer_create_socket(&kconsumer_data
,
3099 cmd_ctx
->session
->kernel_session
->consumer
);
3106 case LTTNG_DOMAIN_JUL
:
3107 case LTTNG_DOMAIN_LOG4J
:
3108 case LTTNG_DOMAIN_PYTHON
:
3109 case LTTNG_DOMAIN_UST
:
3111 if (!ust_app_supported()) {
3112 ret
= LTTNG_ERR_NO_UST
;
3115 /* Consumer is in an ERROR state. Report back to client */
3116 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3117 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3121 if (need_tracing_session
) {
3122 /* Create UST session if none exist. */
3123 if (cmd_ctx
->session
->ust_session
== NULL
) {
3124 ret
= create_ust_session(cmd_ctx
->session
,
3125 &cmd_ctx
->lsm
->domain
);
3126 if (ret
!= LTTNG_OK
) {
3131 /* Start the UST consumer daemons */
3133 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3134 if (config
.consumerd64_bin_path
.value
&&
3135 ustconsumer64_data
.pid
== 0 &&
3136 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3137 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3138 ret
= start_consumerd(&ustconsumer64_data
);
3140 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3141 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3145 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3146 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3148 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3152 * Setup socket for consumer 64 bit. No need for atomic access
3153 * since it was set above and can ONLY be set in this thread.
3155 ret
= consumer_create_socket(&ustconsumer64_data
,
3156 cmd_ctx
->session
->ust_session
->consumer
);
3162 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3163 if (config
.consumerd32_bin_path
.value
&&
3164 ustconsumer32_data
.pid
== 0 &&
3165 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3166 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3167 ret
= start_consumerd(&ustconsumer32_data
);
3169 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3170 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3174 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3175 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3177 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3181 * Setup socket for consumer 32 bit. No need for atomic access
3182 * since it was set above and can ONLY be set in this thread.
3184 ret
= consumer_create_socket(&ustconsumer32_data
,
3185 cmd_ctx
->session
->ust_session
->consumer
);
3197 /* Validate consumer daemon state when start/stop trace command */
3198 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3199 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3200 switch (cmd_ctx
->lsm
->domain
.type
) {
3201 case LTTNG_DOMAIN_NONE
:
3203 case LTTNG_DOMAIN_JUL
:
3204 case LTTNG_DOMAIN_LOG4J
:
3205 case LTTNG_DOMAIN_PYTHON
:
3206 case LTTNG_DOMAIN_UST
:
3207 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3208 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3212 case LTTNG_DOMAIN_KERNEL
:
3213 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3214 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3219 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3225 * Check that the UID or GID match that of the tracing session.
3226 * The root user can interact with all sessions.
3228 if (need_tracing_session
) {
3229 if (!session_access_ok(cmd_ctx
->session
,
3230 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3231 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3232 cmd_ctx
->session
->destroyed
) {
3233 ret
= LTTNG_ERR_EPERM
;
3239 * Send relayd information to consumer as soon as we have a domain and a
3242 if (cmd_ctx
->session
&& need_domain
) {
3244 * Setup relayd if not done yet. If the relayd information was already
3245 * sent to the consumer, this call will gracefully return.
3247 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3248 if (ret
!= LTTNG_OK
) {
3253 /* Process by command type */
3254 switch (cmd_ctx
->lsm
->cmd_type
) {
3255 case LTTNG_ADD_CONTEXT
:
3258 * An LTTNG_ADD_CONTEXT command might have a supplementary
3259 * payload if the context being added is an application context.
3261 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3262 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3263 char *provider_name
= NULL
, *context_name
= NULL
;
3264 size_t provider_name_len
=
3265 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3266 size_t context_name_len
=
3267 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3269 if (provider_name_len
== 0 || context_name_len
== 0) {
3271 * Application provider and context names MUST
3274 ret
= -LTTNG_ERR_INVALID
;
3278 provider_name
= zmalloc(provider_name_len
+ 1);
3279 if (!provider_name
) {
3280 ret
= -LTTNG_ERR_NOMEM
;
3283 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3286 context_name
= zmalloc(context_name_len
+ 1);
3287 if (!context_name
) {
3288 ret
= -LTTNG_ERR_NOMEM
;
3289 goto error_add_context
;
3291 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3294 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3297 goto error_add_context
;
3300 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3303 goto error_add_context
;
3308 * cmd_add_context assumes ownership of the provider and context
3311 ret
= cmd_add_context(cmd_ctx
->session
,
3312 cmd_ctx
->lsm
->domain
.type
,
3313 cmd_ctx
->lsm
->u
.context
.channel_name
,
3314 &cmd_ctx
->lsm
->u
.context
.ctx
,
3315 kernel_poll_pipe
[1]);
3317 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3318 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3320 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3321 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3327 case LTTNG_DISABLE_CHANNEL
:
3329 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3330 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3333 case LTTNG_DISABLE_EVENT
:
3337 * FIXME: handle filter; for now we just receive the filter's
3338 * bytecode along with the filter expression which are sent by
3339 * liblttng-ctl and discard them.
3341 * This fixes an issue where the client may block while sending
3342 * the filter payload and encounter an error because the session
3343 * daemon closes the socket without ever handling this data.
3345 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3346 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3349 char data
[LTTNG_FILTER_MAX_LEN
];
3351 DBG("Discarding disable event command payload of size %zu", count
);
3353 ret
= lttcomm_recv_unix_sock(sock
, data
,
3354 count
> sizeof(data
) ? sizeof(data
) : count
);
3359 count
-= (size_t) ret
;
3362 /* FIXME: passing packed structure to non-packed pointer */
3363 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3364 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3365 &cmd_ctx
->lsm
->u
.disable
.event
);
3368 case LTTNG_ENABLE_CHANNEL
:
3370 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3371 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3372 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3373 &cmd_ctx
->lsm
->u
.channel
.chan
,
3374 kernel_poll_pipe
[1]);
3377 case LTTNG_TRACK_PID
:
3379 ret
= cmd_track_pid(cmd_ctx
->session
,
3380 cmd_ctx
->lsm
->domain
.type
,
3381 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3384 case LTTNG_UNTRACK_PID
:
3386 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3387 cmd_ctx
->lsm
->domain
.type
,
3388 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3391 case LTTNG_ENABLE_EVENT
:
3393 struct lttng_event
*ev
= NULL
;
3394 struct lttng_event_exclusion
*exclusion
= NULL
;
3395 struct lttng_filter_bytecode
*bytecode
= NULL
;
3396 char *filter_expression
= NULL
;
3398 /* Handle exclusion events and receive it from the client. */
3399 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3400 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3402 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3403 (count
* LTTNG_SYMBOL_NAME_LEN
));
3405 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3409 DBG("Receiving var len exclusion event list from client ...");
3410 exclusion
->count
= count
;
3411 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3412 count
* LTTNG_SYMBOL_NAME_LEN
);
3414 DBG("Nothing recv() from client var len data... continuing");
3417 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3422 /* Get filter expression from client. */
3423 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3424 size_t expression_len
=
3425 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3427 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3428 ret
= LTTNG_ERR_FILTER_INVAL
;
3433 filter_expression
= zmalloc(expression_len
);
3434 if (!filter_expression
) {
3436 ret
= LTTNG_ERR_FILTER_NOMEM
;
3440 /* Receive var. len. data */
3441 DBG("Receiving var len filter's expression from client ...");
3442 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3445 DBG("Nothing recv() from client var len data... continuing");
3447 free(filter_expression
);
3449 ret
= LTTNG_ERR_FILTER_INVAL
;
3454 /* Handle filter and get bytecode from client. */
3455 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3456 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3458 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3459 ret
= LTTNG_ERR_FILTER_INVAL
;
3460 free(filter_expression
);
3465 bytecode
= zmalloc(bytecode_len
);
3467 free(filter_expression
);
3469 ret
= LTTNG_ERR_FILTER_NOMEM
;
3473 /* Receive var. len. data */
3474 DBG("Receiving var len filter's bytecode from client ...");
3475 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3477 DBG("Nothing recv() from client var len data... continuing");
3479 free(filter_expression
);
3482 ret
= LTTNG_ERR_FILTER_INVAL
;
3486 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3487 free(filter_expression
);
3490 ret
= LTTNG_ERR_FILTER_INVAL
;
3495 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3497 DBG("Failed to copy event: %s",
3498 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3499 free(filter_expression
);
3502 ret
= LTTNG_ERR_NOMEM
;
3507 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3508 /* Expect a userspace probe description. */
3509 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3511 free(filter_expression
);
3514 lttng_event_destroy(ev
);
3519 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3520 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3522 filter_expression
, bytecode
, exclusion
,
3523 kernel_poll_pipe
[1]);
3524 lttng_event_destroy(ev
);
3527 case LTTNG_LIST_TRACEPOINTS
:
3529 struct lttng_event
*events
;
3532 session_lock_list();
3533 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3534 session_unlock_list();
3535 if (nb_events
< 0) {
3536 /* Return value is a negative lttng_error_code. */
3542 * Setup lttng message with payload size set to the event list size in
3543 * bytes and then copy list into the llm payload.
3545 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3546 sizeof(struct lttng_event
) * nb_events
);
3556 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3558 struct lttng_event_field
*fields
;
3561 session_lock_list();
3562 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3564 session_unlock_list();
3565 if (nb_fields
< 0) {
3566 /* Return value is a negative lttng_error_code. */
3572 * Setup lttng message with payload size set to the event list size in
3573 * bytes and then copy list into the llm payload.
3575 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3576 sizeof(struct lttng_event_field
) * nb_fields
);
3586 case LTTNG_LIST_SYSCALLS
:
3588 struct lttng_event
*events
;
3591 nb_events
= cmd_list_syscalls(&events
);
3592 if (nb_events
< 0) {
3593 /* Return value is a negative lttng_error_code. */
3599 * Setup lttng message with payload size set to the event list size in
3600 * bytes and then copy list into the llm payload.
3602 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3603 sizeof(struct lttng_event
) * nb_events
);
3613 case LTTNG_LIST_TRACKER_PIDS
:
3615 int32_t *pids
= NULL
;
3618 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3619 cmd_ctx
->lsm
->domain
.type
, &pids
);
3621 /* Return value is a negative lttng_error_code. */
3627 * Setup lttng message with payload size set to the event list size in
3628 * bytes and then copy list into the llm payload.
3630 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3631 sizeof(int32_t) * nr_pids
);
3641 case LTTNG_SET_CONSUMER_URI
:
3644 struct lttng_uri
*uris
;
3646 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3647 len
= nb_uri
* sizeof(struct lttng_uri
);
3650 ret
= LTTNG_ERR_INVALID
;
3654 uris
= zmalloc(len
);
3656 ret
= LTTNG_ERR_FATAL
;
3660 /* Receive variable len data */
3661 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3662 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3664 DBG("No URIs received from client... continuing");
3666 ret
= LTTNG_ERR_SESSION_FAIL
;
3671 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3673 if (ret
!= LTTNG_OK
) {
3680 case LTTNG_START_TRACE
:
3683 * On the first start, if we have a kernel session and we have
3684 * enabled time or size-based rotations, we have to make sure
3685 * the kernel tracer supports it.
3687 if (!cmd_ctx
->session
->has_been_started
&& \
3688 cmd_ctx
->session
->kernel_session
&& \
3689 (cmd_ctx
->session
->rotate_timer_period
|| \
3690 cmd_ctx
->session
->rotate_size
) && \
3691 !check_rotate_compatible()) {
3692 DBG("Kernel tracer version is not compatible with the rotation feature");
3693 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3696 ret
= cmd_start_trace(cmd_ctx
->session
);
3699 case LTTNG_STOP_TRACE
:
3701 ret
= cmd_stop_trace(cmd_ctx
->session
);
3704 case LTTNG_CREATE_SESSION
:
3707 struct lttng_uri
*uris
= NULL
;
3709 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3710 len
= nb_uri
* sizeof(struct lttng_uri
);
3713 uris
= zmalloc(len
);
3715 ret
= LTTNG_ERR_FATAL
;
3719 /* Receive variable len data */
3720 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3721 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3723 DBG("No URIs received from client... continuing");
3725 ret
= LTTNG_ERR_SESSION_FAIL
;
3730 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3731 DBG("Creating session with ONE network URI is a bad call");
3732 ret
= LTTNG_ERR_SESSION_FAIL
;
3738 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3739 &cmd_ctx
->creds
, 0);
3745 case LTTNG_DESTROY_SESSION
:
3747 ret
= cmd_destroy_session(cmd_ctx
->session
,
3748 notification_thread_handle
);
3751 case LTTNG_LIST_DOMAINS
:
3754 struct lttng_domain
*domains
= NULL
;
3756 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3758 /* Return value is a negative lttng_error_code. */
3763 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3764 nb_dom
* sizeof(struct lttng_domain
));
3774 case LTTNG_LIST_CHANNELS
:
3776 ssize_t payload_size
;
3777 struct lttng_channel
*channels
= NULL
;
3779 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3780 cmd_ctx
->session
, &channels
);
3781 if (payload_size
< 0) {
3782 /* Return value is a negative lttng_error_code. */
3783 ret
= -payload_size
;
3787 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3798 case LTTNG_LIST_EVENTS
:
3801 struct lttng_event
*events
= NULL
;
3802 struct lttcomm_event_command_header cmd_header
;
3805 memset(&cmd_header
, 0, sizeof(cmd_header
));
3806 /* Extended infos are included at the end of events */
3807 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3808 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3809 &events
, &total_size
);
3812 /* Return value is a negative lttng_error_code. */
3817 cmd_header
.nb_events
= nb_event
;
3818 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3819 &cmd_header
, sizeof(cmd_header
));
3829 case LTTNG_LIST_SESSIONS
:
3831 unsigned int nr_sessions
;
3832 void *sessions_payload
;
3835 session_lock_list();
3836 nr_sessions
= lttng_sessions_count(
3837 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3838 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3839 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3840 sessions_payload
= zmalloc(payload_len
);
3842 if (!sessions_payload
) {
3843 session_unlock_list();
3848 cmd_list_lttng_sessions(sessions_payload
,
3849 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3850 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3851 session_unlock_list();
3853 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3855 free(sessions_payload
);
3864 case LTTNG_REGISTER_CONSUMER
:
3866 struct consumer_data
*cdata
;
3868 switch (cmd_ctx
->lsm
->domain
.type
) {
3869 case LTTNG_DOMAIN_KERNEL
:
3870 cdata
= &kconsumer_data
;
3873 ret
= LTTNG_ERR_UND
;
3877 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3878 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3881 case LTTNG_DATA_PENDING
:
3884 uint8_t pending_ret_byte
;
3886 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3891 * This function may returns 0 or 1 to indicate whether or not
3892 * there is data pending. In case of error, it should return an
3893 * LTTNG_ERR code. However, some code paths may still return
3894 * a nondescript error code, which we handle by returning an
3897 if (pending_ret
== 0 || pending_ret
== 1) {
3899 * ret will be set to LTTNG_OK at the end of
3902 } else if (pending_ret
< 0) {
3903 ret
= LTTNG_ERR_UNK
;
3910 pending_ret_byte
= (uint8_t) pending_ret
;
3912 /* 1 byte to return whether or not data is pending */
3913 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3914 &pending_ret_byte
, 1);
3923 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3925 struct lttcomm_lttng_output_id reply
;
3927 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3928 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3929 if (ret
!= LTTNG_OK
) {
3933 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3939 /* Copy output list into message payload */
3943 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3945 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3946 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3949 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3952 struct lttng_snapshot_output
*outputs
= NULL
;
3954 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3955 if (nb_output
< 0) {
3960 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3961 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3962 nb_output
* sizeof(struct lttng_snapshot_output
));
3972 case LTTNG_SNAPSHOT_RECORD
:
3974 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3975 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3976 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3979 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3982 struct lttng_uri
*uris
= NULL
;
3984 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3985 len
= nb_uri
* sizeof(struct lttng_uri
);
3988 uris
= zmalloc(len
);
3990 ret
= LTTNG_ERR_FATAL
;
3994 /* Receive variable len data */
3995 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3996 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3998 DBG("No URIs received from client... continuing");
4000 ret
= LTTNG_ERR_SESSION_FAIL
;
4005 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4006 DBG("Creating session with ONE network URI is a bad call");
4007 ret
= LTTNG_ERR_SESSION_FAIL
;
4013 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4014 nb_uri
, &cmd_ctx
->creds
);
4018 case LTTNG_CREATE_SESSION_LIVE
:
4021 struct lttng_uri
*uris
= NULL
;
4023 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4024 len
= nb_uri
* sizeof(struct lttng_uri
);
4027 uris
= zmalloc(len
);
4029 ret
= LTTNG_ERR_FATAL
;
4033 /* Receive variable len data */
4034 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4035 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4037 DBG("No URIs received from client... continuing");
4039 ret
= LTTNG_ERR_SESSION_FAIL
;
4044 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4045 DBG("Creating session with ONE network URI is a bad call");
4046 ret
= LTTNG_ERR_SESSION_FAIL
;
4052 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4053 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4057 case LTTNG_SAVE_SESSION
:
4059 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4063 case LTTNG_SET_SESSION_SHM_PATH
:
4065 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4066 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4069 case LTTNG_REGENERATE_METADATA
:
4071 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4074 case LTTNG_REGENERATE_STATEDUMP
:
4076 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4079 case LTTNG_REGISTER_TRIGGER
:
4081 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4082 notification_thread_handle
);
4085 case LTTNG_UNREGISTER_TRIGGER
:
4087 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4088 notification_thread_handle
);
4091 case LTTNG_ROTATE_SESSION
:
4093 struct lttng_rotate_session_return rotate_return
;
4095 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4097 memset(&rotate_return
, 0, sizeof(rotate_return
));
4098 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4099 DBG("Kernel tracer version is not compatible with the rotation feature");
4100 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4104 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4110 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4111 sizeof(rotate_return
));
4120 case LTTNG_ROTATION_GET_INFO
:
4122 struct lttng_rotation_get_info_return get_info_return
;
4124 memset(&get_info_return
, 0, sizeof(get_info_return
));
4125 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4126 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4132 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4133 sizeof(get_info_return
));
4142 case LTTNG_ROTATION_SET_SCHEDULE
:
4145 enum lttng_rotation_schedule_type schedule_type
;
4148 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4149 DBG("Kernel tracer version does not support session rotations");
4150 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4154 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4155 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4156 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4158 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4162 notification_thread_handle
);
4163 if (ret
!= LTTNG_OK
) {
4169 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4171 struct lttng_session_list_schedules_return schedules
= {
4172 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4173 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4174 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4175 .size
.value
= cmd_ctx
->session
->rotate_size
,
4178 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4189 ret
= LTTNG_ERR_UND
;
4194 if (cmd_ctx
->llm
== NULL
) {
4195 DBG("Missing llm structure. Allocating one.");
4196 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4200 /* Set return code */
4201 cmd_ctx
->llm
->ret_code
= ret
;
4203 if (cmd_ctx
->session
) {
4204 session_unlock(cmd_ctx
->session
);
4205 session_put(cmd_ctx
->session
);
4207 if (need_tracing_session
) {
4208 session_unlock_list();
4211 assert(!rcu_read_ongoing());
4216 * Thread managing health check socket.
4218 static void *thread_manage_health(void *data
)
4220 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4221 uint32_t revents
, nb_fd
;
4222 struct lttng_poll_event events
;
4223 struct health_comm_msg msg
;
4224 struct health_comm_reply reply
;
4226 DBG("[thread] Manage health check started");
4228 rcu_register_thread();
4230 /* We might hit an error path before this is created. */
4231 lttng_poll_init(&events
);
4233 /* Create unix socket */
4234 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4236 ERR("Unable to create health check Unix socket");
4241 /* lttng health client socket path permissions */
4242 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4243 utils_get_group_id(config
.tracing_group_name
.value
));
4245 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4250 ret
= chmod(config
.health_unix_sock_path
.value
,
4251 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4253 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4260 * Set the CLOEXEC flag. Return code is useless because either way, the
4263 (void) utils_set_fd_cloexec(sock
);
4265 ret
= lttcomm_listen_unix_sock(sock
);
4271 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4272 * more will be added to this poll set.
4274 ret
= sessiond_set_thread_pollset(&events
, 2);
4279 /* Add the application registration socket */
4280 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4285 sessiond_notify_ready();
4288 DBG("Health check ready");
4290 /* Inifinite blocking call, waiting for transmission */
4292 ret
= lttng_poll_wait(&events
, -1);
4295 * Restart interrupted system call.
4297 if (errno
== EINTR
) {
4305 for (i
= 0; i
< nb_fd
; i
++) {
4306 /* Fetch once the poll data */
4307 revents
= LTTNG_POLL_GETEV(&events
, i
);
4308 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4311 /* No activity for this FD (poll implementation). */
4315 /* Thread quit pipe has been closed. Killing thread. */
4316 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4322 /* Event on the registration socket */
4323 if (pollfd
== sock
) {
4324 if (revents
& LPOLLIN
) {
4326 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4327 ERR("Health socket poll error");
4330 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4336 new_sock
= lttcomm_accept_unix_sock(sock
);
4342 * Set the CLOEXEC flag. Return code is useless because either way, the
4345 (void) utils_set_fd_cloexec(new_sock
);
4347 DBG("Receiving data from client for health...");
4348 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4350 DBG("Nothing recv() from client... continuing");
4351 ret
= close(new_sock
);
4358 rcu_thread_online();
4360 memset(&reply
, 0, sizeof(reply
));
4361 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4363 * health_check_state returns 0 if health is
4366 if (!health_check_state(health_sessiond
, i
)) {
4367 reply
.ret_code
|= 1ULL << i
;
4371 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4373 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4375 ERR("Failed to send health data back to client");
4378 /* End of transmission */
4379 ret
= close(new_sock
);
4388 ERR("Health error occurred in %s", __func__
);
4390 DBG("Health check thread dying");
4391 unlink(config
.health_unix_sock_path
.value
);
4399 lttng_poll_clean(&events
);
4401 rcu_unregister_thread();
4406 * This thread manage all clients request using the unix client socket for
4409 static void *thread_manage_clients(void *data
)
4411 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4413 uint32_t revents
, nb_fd
;
4414 struct command_ctx
*cmd_ctx
= NULL
;
4415 struct lttng_poll_event events
;
4417 DBG("[thread] Manage client started");
4419 rcu_register_thread();
4421 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4423 health_code_update();
4425 ret
= lttcomm_listen_unix_sock(client_sock
);
4431 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4432 * more will be added to this poll set.
4434 ret
= sessiond_set_thread_pollset(&events
, 2);
4436 goto error_create_poll
;
4439 /* Add the application registration socket */
4440 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4445 ret
= sem_post(&load_info
->message_thread_ready
);
4447 PERROR("sem_post message_thread_ready");
4452 * Wait until all support threads are initialized before accepting
4455 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4457 * If a support thread failed to launch, it may signal that
4458 * we must exit and the sessiond would never be marked as
4461 * The timeout is set to 1ms, which serves as a way to
4462 * pace down this check.
4464 ret
= sessiond_wait_for_quit_pipe(1000);
4470 * This barrier is paired with the one in sessiond_notify_ready() to
4471 * ensure that loads accessing data initialized by the other threads,
4472 * on which this thread was waiting, are not performed before this point.
4474 * Note that this could be a 'read' memory barrier, but a full barrier
4475 * is used in case the code changes. The performance implications of
4476 * this choice are minimal since this is a slow path.
4480 /* This testpoint is after we signal readiness to the parent. */
4481 if (testpoint(sessiond_thread_manage_clients
)) {
4485 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4489 health_code_update();
4492 const struct cmd_completion_handler
*cmd_completion_handler
;
4494 DBG("Accepting client command ...");
4496 /* Inifinite blocking call, waiting for transmission */
4498 health_poll_entry();
4499 ret
= lttng_poll_wait(&events
, -1);
4503 * Restart interrupted system call.
4505 if (errno
== EINTR
) {
4513 for (i
= 0; i
< nb_fd
; i
++) {
4514 /* Fetch once the poll data */
4515 revents
= LTTNG_POLL_GETEV(&events
, i
);
4516 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4518 health_code_update();
4521 /* No activity for this FD (poll implementation). */
4525 /* Thread quit pipe has been closed. Killing thread. */
4526 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4532 /* Event on the registration socket */
4533 if (pollfd
== client_sock
) {
4534 if (revents
& LPOLLIN
) {
4536 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4537 ERR("Client socket poll error");
4540 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4546 DBG("Wait for client response");
4548 health_code_update();
4550 sock
= lttcomm_accept_unix_sock(client_sock
);
4556 * Set the CLOEXEC flag. Return code is useless because either way, the
4559 (void) utils_set_fd_cloexec(sock
);
4561 /* Set socket option for credentials retrieval */
4562 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4567 /* Allocate context command to process the client request */
4568 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4569 if (cmd_ctx
== NULL
) {
4570 PERROR("zmalloc cmd_ctx");
4574 /* Allocate data buffer for reception */
4575 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4576 if (cmd_ctx
->lsm
== NULL
) {
4577 PERROR("zmalloc cmd_ctx->lsm");
4581 cmd_ctx
->llm
= NULL
;
4582 cmd_ctx
->session
= NULL
;
4584 health_code_update();
4587 * Data is received from the lttng client. The struct
4588 * lttcomm_session_msg (lsm) contains the command and data request of
4591 DBG("Receiving data from client ...");
4592 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4593 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4595 DBG("Nothing recv() from client... continuing");
4601 clean_command_ctx(&cmd_ctx
);
4605 health_code_update();
4607 // TODO: Validate cmd_ctx including sanity check for
4608 // security purpose.
4610 rcu_thread_online();
4612 * This function dispatch the work to the kernel or userspace tracer
4613 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4614 * informations for the client. The command context struct contains
4615 * everything this function may needs.
4617 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4618 rcu_thread_offline();
4626 * TODO: Inform client somehow of the fatal error. At
4627 * this point, ret < 0 means that a zmalloc failed
4628 * (ENOMEM). Error detected but still accept
4629 * command, unless a socket error has been
4632 clean_command_ctx(&cmd_ctx
);
4636 cmd_completion_handler
= cmd_pop_completion_handler();
4637 if (cmd_completion_handler
) {
4638 enum lttng_error_code completion_code
;
4640 completion_code
= cmd_completion_handler
->run(
4641 cmd_completion_handler
->data
);
4642 if (completion_code
!= LTTNG_OK
) {
4643 clean_command_ctx(&cmd_ctx
);
4648 health_code_update();
4650 DBG("Sending response (size: %d, retcode: %s (%d))",
4651 cmd_ctx
->lttng_msg_size
,
4652 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4653 cmd_ctx
->llm
->ret_code
);
4654 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4656 ERR("Failed to send data back to client");
4659 /* End of transmission */
4666 clean_command_ctx(&cmd_ctx
);
4668 health_code_update();
4680 lttng_poll_clean(&events
);
4681 clean_command_ctx(&cmd_ctx
);
4685 unlink(config
.client_unix_sock_path
.value
);
4686 if (client_sock
>= 0) {
4687 ret
= close(client_sock
);
4695 ERR("Health error occurred in %s", __func__
);
4698 health_unregister(health_sessiond
);
4700 DBG("Client thread dying");
4702 rcu_unregister_thread();
4705 * Since we are creating the consumer threads, we own them, so we need
4706 * to join them before our thread exits.
4708 ret
= join_consumer_thread(&kconsumer_data
);
4711 PERROR("join_consumer");
4714 ret
= join_consumer_thread(&ustconsumer32_data
);
4717 PERROR("join_consumer ust32");
4720 ret
= join_consumer_thread(&ustconsumer64_data
);
4723 PERROR("join_consumer ust64");
4728 static int string_match(const char *str1
, const char *str2
)
4730 return (str1
&& str2
) && !strcmp(str1
, str2
);
4734 * Take an option from the getopt output and set it in the right variable to be
4737 * Return 0 on success else a negative value.
4739 static int set_option(int opt
, const char *arg
, const char *optname
)
4743 if (string_match(optname
, "client-sock") || opt
== 'c') {
4744 if (!arg
|| *arg
== '\0') {
4748 if (lttng_is_setuid_setgid()) {
4749 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4750 "-c, --client-sock");
4752 config_string_set(&config
.client_unix_sock_path
,
4754 if (!config
.client_unix_sock_path
.value
) {
4759 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4760 if (!arg
|| *arg
== '\0') {
4764 if (lttng_is_setuid_setgid()) {
4765 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4768 config_string_set(&config
.apps_unix_sock_path
,
4770 if (!config
.apps_unix_sock_path
.value
) {
4775 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4776 config
.daemonize
= true;
4777 } else if (string_match(optname
, "background") || opt
== 'b') {
4778 config
.background
= true;
4779 } else if (string_match(optname
, "group") || opt
== 'g') {
4780 if (!arg
|| *arg
== '\0') {
4784 if (lttng_is_setuid_setgid()) {
4785 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4788 config_string_set(&config
.tracing_group_name
,
4790 if (!config
.tracing_group_name
.value
) {
4795 } else if (string_match(optname
, "help") || opt
== 'h') {
4796 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4798 ERR("Cannot show --help for `lttng-sessiond`");
4801 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4802 } else if (string_match(optname
, "version") || opt
== 'V') {
4803 fprintf(stdout
, "%s\n", VERSION
);
4805 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4806 config
.sig_parent
= true;
4807 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4808 if (!arg
|| *arg
== '\0') {
4812 if (lttng_is_setuid_setgid()) {
4813 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4814 "--kconsumerd-err-sock");
4816 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4818 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4823 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4824 if (!arg
|| *arg
== '\0') {
4828 if (lttng_is_setuid_setgid()) {
4829 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4830 "--kconsumerd-cmd-sock");
4832 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4834 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4839 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4840 if (!arg
|| *arg
== '\0') {
4844 if (lttng_is_setuid_setgid()) {
4845 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4846 "--ustconsumerd64-err-sock");
4848 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4850 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4855 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4856 if (!arg
|| *arg
== '\0') {
4860 if (lttng_is_setuid_setgid()) {
4861 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4862 "--ustconsumerd64-cmd-sock");
4864 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4866 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4871 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4872 if (!arg
|| *arg
== '\0') {
4876 if (lttng_is_setuid_setgid()) {
4877 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4878 "--ustconsumerd32-err-sock");
4880 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4882 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4887 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4888 if (!arg
|| *arg
== '\0') {
4892 if (lttng_is_setuid_setgid()) {
4893 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4894 "--ustconsumerd32-cmd-sock");
4896 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4898 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4903 } else if (string_match(optname
, "no-kernel")) {
4904 config
.no_kernel
= true;
4905 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4906 config
.quiet
= true;
4907 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4908 /* Verbose level can increase using multiple -v */
4910 /* Value obtained from config file */
4911 config
.verbose
= config_parse_value(arg
);
4913 /* -v used on command line */
4916 /* Clamp value to [0, 3] */
4917 config
.verbose
= config
.verbose
< 0 ? 0 :
4918 (config
.verbose
<= 3 ? config
.verbose
: 3);
4919 } else if (string_match(optname
, "verbose-consumer")) {
4921 config
.verbose_consumer
= config_parse_value(arg
);
4923 config
.verbose_consumer
++;
4925 } else if (string_match(optname
, "consumerd32-path")) {
4926 if (!arg
|| *arg
== '\0') {
4930 if (lttng_is_setuid_setgid()) {
4931 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4932 "--consumerd32-path");
4934 config_string_set(&config
.consumerd32_bin_path
,
4936 if (!config
.consumerd32_bin_path
.value
) {
4941 } else if (string_match(optname
, "consumerd32-libdir")) {
4942 if (!arg
|| *arg
== '\0') {
4946 if (lttng_is_setuid_setgid()) {
4947 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4948 "--consumerd32-libdir");
4950 config_string_set(&config
.consumerd32_lib_dir
,
4952 if (!config
.consumerd32_lib_dir
.value
) {
4957 } else if (string_match(optname
, "consumerd64-path")) {
4958 if (!arg
|| *arg
== '\0') {
4962 if (lttng_is_setuid_setgid()) {
4963 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4964 "--consumerd64-path");
4966 config_string_set(&config
.consumerd64_bin_path
,
4968 if (!config
.consumerd64_bin_path
.value
) {
4973 } else if (string_match(optname
, "consumerd64-libdir")) {
4974 if (!arg
|| *arg
== '\0') {
4978 if (lttng_is_setuid_setgid()) {
4979 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4980 "--consumerd64-libdir");
4982 config_string_set(&config
.consumerd64_lib_dir
,
4984 if (!config
.consumerd64_lib_dir
.value
) {
4989 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4990 if (!arg
|| *arg
== '\0') {
4994 if (lttng_is_setuid_setgid()) {
4995 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4998 config_string_set(&config
.pid_file_path
, strdup(arg
));
4999 if (!config
.pid_file_path
.value
) {
5004 } else if (string_match(optname
, "agent-tcp-port")) {
5005 if (!arg
|| *arg
== '\0') {
5009 if (lttng_is_setuid_setgid()) {
5010 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5011 "--agent-tcp-port");
5016 v
= strtoul(arg
, NULL
, 0);
5017 if (errno
!= 0 || !isdigit(arg
[0])) {
5018 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5021 if (v
== 0 || v
>= 65535) {
5022 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5025 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5026 DBG3("Agent TCP port set to non default: %i", (int) v
);
5028 } else if (string_match(optname
, "load") || opt
== 'l') {
5029 if (!arg
|| *arg
== '\0') {
5033 if (lttng_is_setuid_setgid()) {
5034 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5037 config_string_set(&config
.load_session_path
, strdup(arg
));
5038 if (!config
.load_session_path
.value
) {
5043 } else if (string_match(optname
, "kmod-probes")) {
5044 if (!arg
|| *arg
== '\0') {
5048 if (lttng_is_setuid_setgid()) {
5049 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5052 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5053 if (!config
.kmod_probes_list
.value
) {
5058 } else if (string_match(optname
, "extra-kmod-probes")) {
5059 if (!arg
|| *arg
== '\0') {
5063 if (lttng_is_setuid_setgid()) {
5064 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5065 "--extra-kmod-probes");
5067 config_string_set(&config
.kmod_extra_probes_list
,
5069 if (!config
.kmod_extra_probes_list
.value
) {
5074 } else if (string_match(optname
, "config") || opt
== 'f') {
5075 /* This is handled in set_options() thus silent skip. */
5078 /* Unknown option or other error.
5079 * Error is printed by getopt, just return */
5084 if (ret
== -EINVAL
) {
5085 const char *opt_name
= "unknown";
5088 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5090 if (opt
== long_options
[i
].val
) {
5091 opt_name
= long_options
[i
].name
;
5096 WARN("Invalid argument provided for option \"%s\", using default value.",
5104 * config_entry_handler_cb used to handle options read from a config file.
5105 * See config_entry_handler_cb comment in common/config/session-config.h for the
5106 * return value conventions.
5108 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5112 if (!entry
|| !entry
->name
|| !entry
->value
) {
5117 /* Check if the option is to be ignored */
5118 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5119 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5124 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5127 /* Ignore if not fully matched. */
5128 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5133 * If the option takes no argument on the command line, we have to
5134 * check if the value is "true". We support non-zero numeric values,
5137 if (!long_options
[i
].has_arg
) {
5138 ret
= config_parse_value(entry
->value
);
5141 WARN("Invalid configuration value \"%s\" for option %s",
5142 entry
->value
, entry
->name
);
5144 /* False, skip boolean config option. */
5149 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5153 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5160 * daemon configuration loading and argument parsing
5162 static int set_options(int argc
, char **argv
)
5164 int ret
= 0, c
= 0, option_index
= 0;
5165 int orig_optopt
= optopt
, orig_optind
= optind
;
5167 const char *config_path
= NULL
;
5169 optstring
= utils_generate_optstring(long_options
,
5170 sizeof(long_options
) / sizeof(struct option
));
5176 /* Check for the --config option */
5177 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5178 &option_index
)) != -1) {
5182 } else if (c
!= 'f') {
5183 /* if not equal to --config option. */
5187 if (lttng_is_setuid_setgid()) {
5188 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5191 config_path
= utils_expand_path(optarg
);
5193 ERR("Failed to resolve path: %s", optarg
);
5198 ret
= config_get_section_entries(config_path
, config_section_name
,
5199 config_entry_handler
, NULL
);
5202 ERR("Invalid configuration option at line %i", ret
);
5208 /* Reset getopt's global state */
5209 optopt
= orig_optopt
;
5210 optind
= orig_optind
;
5214 * getopt_long() will not set option_index if it encounters a
5217 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5224 * Pass NULL as the long option name if popt left the index
5227 ret
= set_option(c
, optarg
,
5228 option_index
< 0 ? NULL
:
5229 long_options
[option_index
].name
);
5241 * Creates the two needed socket by the daemon.
5242 * apps_sock - The communication socket for all UST apps.
5243 * client_sock - The communication of the cli tool (lttng).
5245 static int init_daemon_socket(void)
5250 old_umask
= umask(0);
5252 /* Create client tool unix socket */
5253 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5254 if (client_sock
< 0) {
5255 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5260 /* Set the cloexec flag */
5261 ret
= utils_set_fd_cloexec(client_sock
);
5263 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5264 "Continuing but note that the consumer daemon will have a "
5265 "reference to this socket on exec()", client_sock
);
5268 /* File permission MUST be 660 */
5269 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5271 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5276 /* Create the application unix socket */
5277 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5278 if (apps_sock
< 0) {
5279 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5284 /* Set the cloexec flag */
5285 ret
= utils_set_fd_cloexec(apps_sock
);
5287 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5288 "Continuing but note that the consumer daemon will have a "
5289 "reference to this socket on exec()", apps_sock
);
5292 /* File permission MUST be 666 */
5293 ret
= chmod(config
.apps_unix_sock_path
.value
,
5294 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5296 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5301 DBG3("Session daemon client socket %d and application socket %d created",
5302 client_sock
, apps_sock
);
5310 * Create lockfile using the rundir and return its fd.
5312 static int create_lockfile(void)
5314 return utils_create_lock_file(config
.lock_file_path
.value
);
5318 * Check if the global socket is available, and if a daemon is answering at the
5319 * other side. If yes, error is returned.
5321 * Also attempts to create and hold the lock file.
5323 static int check_existing_daemon(void)
5327 /* Is there anybody out there ? */
5328 if (lttng_session_daemon_alive()) {
5333 lockfile_fd
= create_lockfile();
5334 if (lockfile_fd
< 0) {
5342 static void sessiond_cleanup_lock_file(void)
5347 * Cleanup lock file by deleting it and finaly closing it which will
5348 * release the file system lock.
5350 if (lockfile_fd
>= 0) {
5351 ret
= remove(config
.lock_file_path
.value
);
5353 PERROR("remove lock file");
5355 ret
= close(lockfile_fd
);
5357 PERROR("close lock file");
5363 * Set the tracing group gid onto the client socket.
5365 * Race window between mkdir and chown is OK because we are going from more
5366 * permissive (root.root) to less permissive (root.tracing).
5368 static int set_permissions(char *rundir
)
5373 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5375 /* Set lttng run dir */
5376 ret
= chown(rundir
, 0, gid
);
5378 ERR("Unable to set group on %s", rundir
);
5383 * Ensure all applications and tracing group can search the run
5384 * dir. Allow everyone to read the directory, since it does not
5385 * buy us anything to hide its content.
5387 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5389 ERR("Unable to set permissions on %s", rundir
);
5393 /* lttng client socket path */
5394 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5396 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5400 /* kconsumer error socket path */
5401 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5403 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5407 /* 64-bit ustconsumer error socket path */
5408 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5410 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5414 /* 32-bit ustconsumer compat32 error socket path */
5415 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5417 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5421 DBG("All permissions are set");
5427 * Create the lttng run directory needed for all global sockets and pipe.
5429 static int create_lttng_rundir(void)
5433 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5435 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5437 if (errno
!= EEXIST
) {
5438 ERR("Unable to create %s", config
.rundir
.value
);
5450 * Setup sockets and directory needed by the consumerds' communication with the
5453 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5458 switch (consumer_data
->type
) {
5459 case LTTNG_CONSUMER_KERNEL
:
5460 path
= config
.kconsumerd_path
.value
;
5462 case LTTNG_CONSUMER64_UST
:
5463 path
= config
.consumerd64_path
.value
;
5465 case LTTNG_CONSUMER32_UST
:
5466 path
= config
.consumerd32_path
.value
;
5469 ERR("Consumer type unknown");
5475 DBG2("Creating consumer directory: %s", path
);
5477 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5478 if (ret
< 0 && errno
!= EEXIST
) {
5480 ERR("Failed to create %s", path
);
5484 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5486 ERR("Unable to set group on %s", path
);
5492 /* Create the consumerd error unix socket */
5493 consumer_data
->err_sock
=
5494 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5495 if (consumer_data
->err_sock
< 0) {
5496 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5502 * Set the CLOEXEC flag. Return code is useless because either way, the
5505 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5507 PERROR("utils_set_fd_cloexec");
5508 /* continue anyway */
5511 /* File permission MUST be 660 */
5512 ret
= chmod(consumer_data
->err_unix_sock_path
,
5513 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5515 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5525 * Signal handler for the daemon
5527 * Simply stop all worker threads, leaving main() return gracefully after
5528 * joining all threads and calling cleanup().
5530 static void sighandler(int sig
)
5534 DBG("SIGINT caught");
5538 DBG("SIGTERM caught");
5542 CMM_STORE_SHARED(recv_child_signal
, 1);
5550 * Setup signal handler for :
5551 * SIGINT, SIGTERM, SIGPIPE
5553 static int set_signal_handler(void)
5556 struct sigaction sa
;
5559 if ((ret
= sigemptyset(&sigset
)) < 0) {
5560 PERROR("sigemptyset");
5564 sa
.sa_mask
= sigset
;
5567 sa
.sa_handler
= sighandler
;
5568 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5569 PERROR("sigaction");
5573 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5574 PERROR("sigaction");
5578 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5579 PERROR("sigaction");
5583 sa
.sa_handler
= SIG_IGN
;
5584 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5585 PERROR("sigaction");
5589 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5595 * Set open files limit to unlimited. This daemon can open a large number of
5596 * file descriptors in order to consume multiple kernel traces.
5598 static void set_ulimit(void)
5603 /* The kernel does not allow an infinite limit for open files */
5604 lim
.rlim_cur
= 65535;
5605 lim
.rlim_max
= 65535;
5607 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5609 PERROR("failed to set open files limit");
5613 static int write_pidfile(void)
5615 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5618 static int set_clock_plugin_env(void)
5621 char *env_value
= NULL
;
5623 if (!config
.lttng_ust_clock_plugin
.value
) {
5627 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5628 config
.lttng_ust_clock_plugin
.value
);
5634 ret
= putenv(env_value
);
5637 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5641 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5642 config
.lttng_ust_clock_plugin
.value
);
5650 int main(int argc
, char **argv
)
5652 int ret
= 0, retval
= 0;
5654 const char *env_app_timeout
;
5655 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5656 *ust64_channel_monitor_pipe
= NULL
,
5657 *kernel_channel_monitor_pipe
= NULL
;
5658 bool notification_thread_launched
= false;
5659 bool rotation_thread_launched
= false;
5660 bool timer_thread_launched
= false;
5661 struct timer_thread_parameters timer_thread_ctx
;
5662 /* Queue of rotation jobs populated by the sessiond-timer. */
5663 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5664 sem_t notification_thread_ready
;
5666 init_kernel_workarounds();
5668 rcu_register_thread();
5670 if (set_signal_handler()) {
5672 goto exit_set_signal_handler
;
5675 if (timer_signal_init()) {
5677 goto exit_set_signal_handler
;
5680 page_size
= sysconf(_SC_PAGESIZE
);
5681 if (page_size
< 0) {
5682 PERROR("sysconf _SC_PAGESIZE");
5683 page_size
= LONG_MAX
;
5684 WARN("Fallback page size to %ld", page_size
);
5687 ret
= sessiond_config_init(&config
);
5690 goto exit_set_signal_handler
;
5694 * Init config from environment variables.
5695 * Command line option override env configuration per-doc. Do env first.
5697 sessiond_config_apply_env_config(&config
);
5700 * Parse arguments and load the daemon configuration file.
5702 * We have an exit_options exit path to free memory reserved by
5703 * set_options. This is needed because the rest of sessiond_cleanup()
5704 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5705 * depends on set_options.
5708 if (set_options(argc
, argv
)) {
5714 * Resolve all paths received as arguments, configuration option, or
5715 * through environment variable as absolute paths. This is necessary
5716 * since daemonizing causes the sessiond's current working directory
5719 ret
= sessiond_config_resolve_paths(&config
);
5725 lttng_opt_verbose
= config
.verbose
;
5726 lttng_opt_quiet
= config
.quiet
;
5727 kconsumer_data
.err_unix_sock_path
=
5728 config
.kconsumerd_err_unix_sock_path
.value
;
5729 kconsumer_data
.cmd_unix_sock_path
=
5730 config
.kconsumerd_cmd_unix_sock_path
.value
;
5731 ustconsumer32_data
.err_unix_sock_path
=
5732 config
.consumerd32_err_unix_sock_path
.value
;
5733 ustconsumer32_data
.cmd_unix_sock_path
=
5734 config
.consumerd32_cmd_unix_sock_path
.value
;
5735 ustconsumer64_data
.err_unix_sock_path
=
5736 config
.consumerd64_err_unix_sock_path
.value
;
5737 ustconsumer64_data
.cmd_unix_sock_path
=
5738 config
.consumerd64_cmd_unix_sock_path
.value
;
5739 set_clock_plugin_env();
5741 sessiond_config_log(&config
);
5743 if (create_lttng_rundir()) {
5748 /* Abort launch if a session daemon is already running. */
5749 if (check_existing_daemon()) {
5750 ERR("A session daemon is already running.");
5756 if (config
.daemonize
|| config
.background
) {
5759 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5760 !config
.background
);
5767 * We are in the child. Make sure all other file descriptors are
5768 * closed, in case we are called with more opened file
5769 * descriptors than the standard ones and the lock file.
5771 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5772 if (i
== lockfile_fd
) {
5779 if (run_as_create_worker(argv
[0]) < 0) {
5780 goto exit_create_run_as_worker_cleanup
;
5784 * Starting from here, we can create threads. This needs to be after
5785 * lttng_daemonize due to RCU.
5789 * Initialize the health check subsystem. This call should set the
5790 * appropriate time values.
5792 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5793 if (!health_sessiond
) {
5794 PERROR("health_app_create error");
5796 goto exit_health_sessiond_cleanup
;
5799 /* Create thread to clean up RCU hash tables */
5800 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5802 goto exit_ht_cleanup
;
5805 /* Create thread quit pipe */
5806 if (sessiond_init_thread_quit_pipe()) {
5808 goto exit_init_data
;
5811 /* Check if daemon is UID = 0 */
5812 is_root
= !getuid();
5814 /* Create global run dir with root access */
5816 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5817 if (!kernel_channel_monitor_pipe
) {
5818 ERR("Failed to create kernel consumer channel monitor pipe");
5820 goto exit_init_data
;
5822 kconsumer_data
.channel_monitor_pipe
=
5823 lttng_pipe_release_writefd(
5824 kernel_channel_monitor_pipe
);
5825 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5827 goto exit_init_data
;
5831 /* Set consumer initial state */
5832 kernel_consumerd_state
= CONSUMER_STOPPED
;
5833 ust_consumerd_state
= CONSUMER_STOPPED
;
5835 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5836 if (!ust32_channel_monitor_pipe
) {
5837 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5839 goto exit_init_data
;
5841 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5842 ust32_channel_monitor_pipe
);
5843 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5845 goto exit_init_data
;
5849 * The rotation_thread_timer_queue structure is shared between the
5850 * sessiond timer thread and the rotation thread. The main thread keeps
5851 * its ownership and destroys it when both threads have been joined.
5853 rotation_timer_queue
= rotation_thread_timer_queue_create();
5854 if (!rotation_timer_queue
) {
5856 goto exit_init_data
;
5858 timer_thread_ctx
.rotation_thread_job_queue
= rotation_timer_queue
;
5860 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5861 if (!ust64_channel_monitor_pipe
) {
5862 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5864 goto exit_init_data
;
5866 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5867 ust64_channel_monitor_pipe
);
5868 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5870 goto exit_init_data
;
5874 * Init UST app hash table. Alloc hash table before this point since
5875 * cleanup() can get called after that point.
5877 if (ust_app_ht_alloc()) {
5878 ERR("Failed to allocate UST app hash table");
5880 goto exit_init_data
;
5884 * Initialize agent app hash table. We allocate the hash table here
5885 * since cleanup() can get called after this point.
5887 if (agent_app_ht_alloc()) {
5888 ERR("Failed to allocate Agent app hash table");
5890 goto exit_init_data
;
5894 * These actions must be executed as root. We do that *after* setting up
5895 * the sockets path because we MUST make the check for another daemon using
5896 * those paths *before* trying to set the kernel consumer sockets and init
5900 if (set_consumer_sockets(&kconsumer_data
)) {
5902 goto exit_init_data
;
5905 /* Setup kernel tracer */
5906 if (!config
.no_kernel
) {
5907 init_kernel_tracer();
5908 if (kernel_tracer_fd
>= 0) {
5909 ret
= syscall_init_table();
5911 ERR("Unable to populate syscall table. "
5912 "Syscall tracing won't work "
5913 "for this session daemon.");
5918 /* Set ulimit for open files */
5921 /* init lttng_fd tracking must be done after set_ulimit. */
5924 if (set_consumer_sockets(&ustconsumer64_data
)) {
5926 goto exit_init_data
;
5929 if (set_consumer_sockets(&ustconsumer32_data
)) {
5931 goto exit_init_data
;
5934 /* Setup the needed unix socket */
5935 if (init_daemon_socket()) {
5937 goto exit_init_data
;
5940 /* Set credentials to socket */
5941 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5943 goto exit_init_data
;
5946 /* Get parent pid if -S, --sig-parent is specified. */
5947 if (config
.sig_parent
) {
5951 /* Setup the kernel pipe for waking up the kernel thread */
5952 if (is_root
&& !config
.no_kernel
) {
5953 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5955 goto exit_init_data
;
5959 /* Setup the thread apps communication pipe. */
5960 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5962 goto exit_init_data
;
5965 /* Setup the thread apps notify communication pipe. */
5966 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5968 goto exit_init_data
;
5971 /* Initialize global buffer per UID and PID registry. */
5972 buffer_reg_init_uid_registry();
5973 buffer_reg_init_pid_registry();
5975 /* Init UST command queue. */
5976 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5980 /* Check for the application socket timeout env variable. */
5981 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5982 if (env_app_timeout
) {
5983 config
.app_socket_timeout
= atoi(env_app_timeout
);
5985 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5988 ret
= write_pidfile();
5990 ERR("Error in write_pidfile");
5992 goto exit_init_data
;
5995 /* Initialize communication library */
5997 /* Initialize TCP timeout values */
5998 lttcomm_inet_init();
6000 if (load_session_init_data(&load_info
) < 0) {
6002 goto exit_init_data
;
6004 load_info
->path
= config
.load_session_path
.value
;
6006 /* Create health-check thread. */
6007 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6008 thread_manage_health
, (void *) NULL
);
6011 PERROR("pthread_create health");
6017 * The rotation thread needs the notification thread to be ready before
6018 * creating the rotate_notification_channel, so we use this semaphore as
6019 * a rendez-vous point.
6021 sem_init(¬ification_thread_ready
, 0, 0);
6023 /* notification_thread_data acquires the pipes' read side. */
6024 notification_thread_handle
= notification_thread_handle_create(
6025 ust32_channel_monitor_pipe
,
6026 ust64_channel_monitor_pipe
,
6027 kernel_channel_monitor_pipe
,
6028 ¬ification_thread_ready
);
6029 if (!notification_thread_handle
) {
6031 ERR("Failed to create notification thread shared data");
6033 goto exit_notification
;
6036 /* Create notification thread. */
6037 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6038 thread_notification
, notification_thread_handle
);
6041 PERROR("pthread_create notification");
6044 goto exit_notification
;
6046 notification_thread_launched
= true;
6048 /* Create timer thread. */
6049 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6050 timer_thread_func
, &timer_thread_ctx
);
6053 PERROR("pthread_create timer");
6056 goto exit_notification
;
6058 timer_thread_launched
= true;
6060 /* rotation_thread_data acquires the pipes' read side. */
6061 rotation_thread_handle
= rotation_thread_handle_create(
6062 rotation_timer_queue
,
6063 notification_thread_handle
,
6064 ¬ification_thread_ready
);
6065 if (!rotation_thread_handle
) {
6067 ERR("Failed to create rotation thread shared data");
6072 /* Create rotation thread. */
6073 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6074 thread_rotation
, rotation_thread_handle
);
6077 PERROR("pthread_create rotation");
6082 rotation_thread_launched
= true;
6084 /* Create thread to manage the client socket */
6085 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6086 thread_manage_clients
, (void *) NULL
);
6089 PERROR("pthread_create clients");
6095 /* Create thread to dispatch registration */
6096 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6097 thread_dispatch_ust_registration
, (void *) NULL
);
6100 PERROR("pthread_create dispatch");
6106 /* Create thread to manage application registration. */
6107 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6108 thread_registration_apps
, (void *) NULL
);
6111 PERROR("pthread_create registration");
6117 /* Create thread to manage application socket */
6118 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6119 thread_manage_apps
, (void *) NULL
);
6122 PERROR("pthread_create apps");
6128 /* Create thread to manage application notify socket */
6129 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6130 ust_thread_manage_notify
, (void *) NULL
);
6133 PERROR("pthread_create notify");
6136 goto exit_apps_notify
;
6139 /* Create agent registration thread. */
6140 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6141 agent_thread_manage_registration
, (void *) NULL
);
6144 PERROR("pthread_create agent");
6147 goto exit_agent_reg
;
6150 /* Don't start this thread if kernel tracing is not requested nor root */
6151 if (is_root
&& !config
.no_kernel
) {
6152 /* Create kernel thread to manage kernel event */
6153 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6154 thread_manage_kernel
, (void *) NULL
);
6157 PERROR("pthread_create kernel");
6164 /* Create session loading thread. */
6165 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6166 thread_load_session
, load_info
);
6169 PERROR("pthread_create load_session_thread");
6172 goto exit_load_session
;
6176 * This is where we start awaiting program completion (e.g. through
6177 * signal that asks threads to teardown).
6180 ret
= pthread_join(load_session_thread
, &status
);
6183 PERROR("pthread_join load_session_thread");
6188 if (is_root
&& !config
.no_kernel
) {
6189 ret
= pthread_join(kernel_thread
, &status
);
6192 PERROR("pthread_join");
6198 ret
= pthread_join(agent_reg_thread
, &status
);
6201 PERROR("pthread_join agent");
6206 ret
= pthread_join(apps_notify_thread
, &status
);
6209 PERROR("pthread_join apps notify");
6214 ret
= pthread_join(apps_thread
, &status
);
6217 PERROR("pthread_join apps");
6222 ret
= pthread_join(reg_apps_thread
, &status
);
6225 PERROR("pthread_join");
6231 * Join dispatch thread after joining reg_apps_thread to ensure
6232 * we don't leak applications in the queue.
6234 ret
= pthread_join(dispatch_thread
, &status
);
6237 PERROR("pthread_join");
6242 ret
= pthread_join(client_thread
, &status
);
6245 PERROR("pthread_join");
6252 sem_destroy(¬ification_thread_ready
);
6253 ret
= pthread_join(health_thread
, &status
);
6256 PERROR("pthread_join health thread");
6263 * Wait for all pending call_rcu work to complete before tearing
6264 * down data structures. call_rcu worker may be trying to
6265 * perform lookups in those structures.
6269 * sessiond_cleanup() is called when no other thread is running, except
6270 * the ht_cleanup thread, which is needed to destroy the hash tables.
6272 rcu_thread_online();
6276 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6277 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6278 * the queue is empty before shutting down the clean-up thread.
6283 * The teardown of the notification system is performed after the
6284 * session daemon's teardown in order to allow it to be notified
6285 * of the active session and channels at the moment of the teardown.
6287 if (notification_thread_handle
) {
6288 if (notification_thread_launched
) {
6289 notification_thread_command_quit(
6290 notification_thread_handle
);
6291 ret
= pthread_join(notification_thread
, &status
);
6294 PERROR("pthread_join notification thread");
6298 notification_thread_handle_destroy(notification_thread_handle
);
6301 if (rotation_thread_handle
) {
6302 if (rotation_thread_launched
) {
6303 ret
= pthread_join(rotation_thread
, &status
);
6306 PERROR("pthread_join rotation thread");
6310 rotation_thread_handle_destroy(rotation_thread_handle
);
6313 if (timer_thread_launched
) {
6315 ret
= pthread_join(timer_thread
, &status
);
6318 PERROR("pthread_join timer thread");
6324 * After the rotation and timer thread have quit, we can safely destroy
6325 * the rotation_timer_queue.
6327 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
6329 rcu_thread_offline();
6330 rcu_unregister_thread();
6332 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6336 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6337 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6338 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6341 health_app_destroy(health_sessiond
);
6342 exit_health_sessiond_cleanup
:
6343 exit_create_run_as_worker_cleanup
:
6346 sessiond_cleanup_lock_file();
6347 sessiond_cleanup_options();
6349 exit_set_signal_handler
: