2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
53 #include "lttng-sessiond.h"
54 #include "buffer-registry.h"
61 #include "kernel-consumer.h"
65 #include "ust-consumer.h"
68 #include "health-sessiond.h"
69 #include "testpoint.h"
70 #include "ust-thread.h"
71 #include "agent-thread.h"
73 #include "load-session-thread.h"
74 #include "notification-thread.h"
75 #include "notification-thread-commands.h"
76 #include "rotation-thread.h"
77 #include "lttng-syscall.h"
79 #include "ht-cleanup.h"
80 #include "sessiond-config.h"
81 #include "sessiond-timer.h"
83 static const char *help_msg
=
84 #ifdef LTTNG_EMBED_HELP
85 #include <lttng-sessiond.8.h>
92 static pid_t ppid
; /* Parent PID for --sig-parent option */
93 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
94 static int lockfile_fd
= -1;
96 /* Set to 1 when a SIGUSR1 signal is received. */
97 static int recv_child_signal
;
99 static struct lttng_kernel_tracer_version kernel_tracer_version
;
100 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
103 * Consumer daemon specific control data. Every value not initialized here is
104 * set to 0 by the static definition.
106 static struct consumer_data kconsumer_data
= {
107 .type
= LTTNG_CONSUMER_KERNEL
,
110 .channel_monitor_pipe
= -1,
111 .channel_rotate_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 .channel_rotate_pipe
= -1,
123 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 .lock
= PTHREAD_MUTEX_INITIALIZER
,
125 .cond
= PTHREAD_COND_INITIALIZER
,
126 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 static struct consumer_data ustconsumer32_data
= {
129 .type
= LTTNG_CONSUMER32_UST
,
132 .channel_monitor_pipe
= -1,
133 .channel_rotate_pipe
= -1,
134 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
135 .lock
= PTHREAD_MUTEX_INITIALIZER
,
136 .cond
= PTHREAD_COND_INITIALIZER
,
137 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
140 /* Command line options */
141 static const struct option long_options
[] = {
142 { "client-sock", required_argument
, 0, 'c' },
143 { "apps-sock", required_argument
, 0, 'a' },
144 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
145 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
150 { "consumerd32-path", required_argument
, 0, '\0' },
151 { "consumerd32-libdir", required_argument
, 0, '\0' },
152 { "consumerd64-path", required_argument
, 0, '\0' },
153 { "consumerd64-libdir", required_argument
, 0, '\0' },
154 { "daemonize", no_argument
, 0, 'd' },
155 { "background", no_argument
, 0, 'b' },
156 { "sig-parent", no_argument
, 0, 'S' },
157 { "help", no_argument
, 0, 'h' },
158 { "group", required_argument
, 0, 'g' },
159 { "version", no_argument
, 0, 'V' },
160 { "quiet", no_argument
, 0, 'q' },
161 { "verbose", no_argument
, 0, 'v' },
162 { "verbose-consumer", no_argument
, 0, '\0' },
163 { "no-kernel", no_argument
, 0, '\0' },
164 { "pidfile", required_argument
, 0, 'p' },
165 { "agent-tcp-port", required_argument
, 0, '\0' },
166 { "config", required_argument
, 0, 'f' },
167 { "load", required_argument
, 0, 'l' },
168 { "kmod-probes", required_argument
, 0, '\0' },
169 { "extra-kmod-probes", required_argument
, 0, '\0' },
173 struct sessiond_config config
;
175 /* Command line options to ignore from configuration file */
176 static const char *config_ignore_options
[] = { "help", "version", "config" };
178 /* Shared between threads */
179 static int dispatch_thread_exit
;
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
194 * This pipe is used to inform the thread managing application communication
195 * that a command is queued and ready to be processed.
197 static int apps_cmd_pipe
[2] = { -1, -1 };
199 int apps_cmd_notify_pipe
[2] = { -1, -1 };
201 /* Pthread, Mutexes and Semaphores */
202 static pthread_t apps_thread
;
203 static pthread_t apps_notify_thread
;
204 static pthread_t reg_apps_thread
;
205 static pthread_t client_thread
;
206 static pthread_t kernel_thread
;
207 static pthread_t dispatch_thread
;
208 static pthread_t health_thread
;
209 static pthread_t ht_cleanup_thread
;
210 static pthread_t agent_reg_thread
;
211 static pthread_t load_session_thread
;
212 static pthread_t notification_thread
;
213 static pthread_t rotation_thread
;
214 static pthread_t timer_thread
;
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
225 static struct ust_cmd_queue ust_cmd_queue
;
228 * Pointer initialized before thread creation.
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
235 * session_lock_list and session_unlock_list for lock acquisition.
237 static struct ltt_session_list
*session_list_ptr
;
239 int ust_consumerd64_fd
= -1;
240 int ust_consumerd32_fd
= -1;
242 static const char *module_proc_lttng
= "/proc/lttng";
245 * Consumer daemon state which is changed when spawning it, killing it or in
246 * case of a fatal error.
248 enum consumerd_state
{
249 CONSUMER_STARTED
= 1,
250 CONSUMER_STOPPED
= 2,
255 * This consumer daemon state is used to validate if a client command will be
256 * able to reach the consumer. If not, the client is informed. For instance,
257 * doing a "lttng start" when the consumer state is set to ERROR will return an
258 * error to the client.
260 * The following example shows a possible race condition of this scheme:
262 * consumer thread error happens
264 * client cmd checks state -> still OK
265 * consumer thread exit, sets error
266 * client cmd try to talk to consumer
269 * However, since the consumer is a different daemon, we have no way of making
270 * sure the command will reach it safely even with this state flag. This is why
271 * we consider that up to the state validation during command processing, the
272 * command is safe. After that, we can not guarantee the correctness of the
273 * client request vis-a-vis the consumer.
275 static enum consumerd_state ust_consumerd_state
;
276 static enum consumerd_state kernel_consumerd_state
;
278 /* Set in main() with the current page size. */
281 /* Application health monitoring */
282 struct health_app
*health_sessiond
;
284 /* Am I root or not. */
285 int is_root
; /* Set to 1 if the daemon is running as root */
287 const char * const config_section_name
= "sessiond";
289 /* Load session thread information to operate. */
290 struct load_session_thread_data
*load_info
;
292 /* Notification thread handle. */
293 struct notification_thread_handle
*notification_thread_handle
;
295 /* Rotation thread handle. */
296 struct rotation_thread_handle
*rotation_thread_handle
;
298 /* Global hash tables */
299 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
302 * Whether sessiond is ready for commands/notification channel/health check
304 * NR_LTTNG_SESSIOND_READY must match the number of calls to
305 * sessiond_notify_ready().
307 #define NR_LTTNG_SESSIOND_READY 6
308 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
310 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
312 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
315 /* Notify parents that we are ready for cmd and health check */
317 void sessiond_notify_ready(void)
319 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
321 * Notify parent pid that we are ready to accept command
322 * for client side. This ppid is the one from the
323 * external process that spawned us.
325 if (config
.sig_parent
) {
330 * Notify the parent of the fork() process that we are
333 if (config
.daemonize
|| config
.background
) {
334 kill(child_ppid
, SIGUSR1
);
340 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
347 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
353 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
367 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
369 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
373 * Init thread quit pipe.
375 * Return -1 on error or 0 if all pipes are created.
377 static int __init_thread_quit_pipe(int *a_pipe
)
383 PERROR("thread quit pipe");
387 for (i
= 0; i
< 2; i
++) {
388 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
399 static int init_thread_quit_pipe(void)
401 return __init_thread_quit_pipe(thread_quit_pipe
);
405 * Stop all threads by closing the thread quit pipe.
407 static void stop_threads(void)
411 /* Stopping all threads */
412 DBG("Terminating all threads");
413 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
415 ERR("write error on thread quit pipe");
418 /* Dispatch thread */
419 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
420 futex_nto1_wake(&ust_cmd_queue
.futex
);
424 * Close every consumer sockets.
426 static void close_consumer_sockets(void)
430 if (kconsumer_data
.err_sock
>= 0) {
431 ret
= close(kconsumer_data
.err_sock
);
433 PERROR("kernel consumer err_sock close");
436 if (ustconsumer32_data
.err_sock
>= 0) {
437 ret
= close(ustconsumer32_data
.err_sock
);
439 PERROR("UST consumerd32 err_sock close");
442 if (ustconsumer64_data
.err_sock
>= 0) {
443 ret
= close(ustconsumer64_data
.err_sock
);
445 PERROR("UST consumerd64 err_sock close");
448 if (kconsumer_data
.cmd_sock
>= 0) {
449 ret
= close(kconsumer_data
.cmd_sock
);
451 PERROR("kernel consumer cmd_sock close");
454 if (ustconsumer32_data
.cmd_sock
>= 0) {
455 ret
= close(ustconsumer32_data
.cmd_sock
);
457 PERROR("UST consumerd32 cmd_sock close");
460 if (ustconsumer64_data
.cmd_sock
>= 0) {
461 ret
= close(ustconsumer64_data
.cmd_sock
);
463 PERROR("UST consumerd64 cmd_sock close");
466 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
467 ret
= close(kconsumer_data
.channel_monitor_pipe
);
469 PERROR("kernel consumer channel monitor pipe close");
472 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
473 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
475 PERROR("UST consumerd32 channel monitor pipe close");
478 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
479 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
481 PERROR("UST consumerd64 channel monitor pipe close");
484 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
485 ret
= close(kconsumer_data
.channel_rotate_pipe
);
487 PERROR("kernel consumer channel rotate pipe close");
490 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
491 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
493 PERROR("UST consumerd32 channel rotate pipe close");
496 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
497 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
499 PERROR("UST consumerd64 channel rotate pipe close");
505 * Wait on consumer process termination.
507 * Need to be called with the consumer data lock held or from a context
508 * ensuring no concurrent access to data (e.g: cleanup).
510 static void wait_consumer(struct consumer_data
*consumer_data
)
515 if (consumer_data
->pid
<= 0) {
519 DBG("Waiting for complete teardown of consumerd (PID: %d)",
521 ret
= waitpid(consumer_data
->pid
, &status
, 0);
523 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
524 } else if (!WIFEXITED(status
)) {
525 ERR("consumerd termination with error: %d",
528 consumer_data
->pid
= 0;
532 * Cleanup the session daemon's data structures.
534 static void sessiond_cleanup(void)
537 struct ltt_session
*sess
, *stmp
;
539 DBG("Cleanup sessiond");
542 * Close the thread quit pipe. It has already done its job,
543 * since we are now called.
545 utils_close_pipe(thread_quit_pipe
);
548 * If config.pid_file_path.value is undefined, the default file will be
549 * wiped when removing the rundir.
551 if (config
.pid_file_path
.value
) {
552 ret
= remove(config
.pid_file_path
.value
);
554 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
558 DBG("Removing sessiond and consumerd content of directory %s",
559 config
.rundir
.value
);
562 DBG("Removing %s", config
.pid_file_path
.value
);
563 (void) unlink(config
.pid_file_path
.value
);
565 DBG("Removing %s", config
.agent_port_file_path
.value
);
566 (void) unlink(config
.agent_port_file_path
.value
);
569 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
570 (void) unlink(kconsumer_data
.err_unix_sock_path
);
572 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
573 (void) rmdir(config
.kconsumerd_path
.value
);
575 /* ust consumerd 32 */
576 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
577 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
579 DBG("Removing directory %s", config
.consumerd32_path
.value
);
580 (void) rmdir(config
.consumerd32_path
.value
);
582 /* ust consumerd 64 */
583 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
584 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
586 DBG("Removing directory %s", config
.consumerd64_path
.value
);
587 (void) rmdir(config
.consumerd64_path
.value
);
589 DBG("Cleaning up all sessions");
591 /* Destroy session list mutex */
592 if (session_list_ptr
!= NULL
) {
593 pthread_mutex_destroy(&session_list_ptr
->lock
);
595 /* Cleanup ALL session */
596 cds_list_for_each_entry_safe(sess
, stmp
,
597 &session_list_ptr
->head
, list
) {
598 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
599 notification_thread_handle
);
603 wait_consumer(&kconsumer_data
);
604 wait_consumer(&ustconsumer64_data
);
605 wait_consumer(&ustconsumer32_data
);
607 DBG("Cleaning up all agent apps");
608 agent_app_ht_clean();
610 DBG("Closing all UST sockets");
611 ust_app_clean_list();
612 buffer_reg_destroy_registries();
614 if (is_root
&& !config
.no_kernel
) {
615 DBG2("Closing kernel fd");
616 if (kernel_tracer_fd
>= 0) {
617 ret
= close(kernel_tracer_fd
);
622 DBG("Unloading kernel modules");
623 modprobe_remove_lttng_all();
627 close_consumer_sockets();
630 load_session_destroy_data(load_info
);
635 * We do NOT rmdir rundir because there are other processes
636 * using it, for instance lttng-relayd, which can start in
637 * parallel with this teardown.
642 * Cleanup the daemon's option data structures.
644 static void sessiond_cleanup_options(void)
646 DBG("Cleaning up options");
648 sessiond_config_fini(&config
);
650 run_as_destroy_worker();
654 * Send data on a unix socket using the liblttsessiondcomm API.
656 * Return lttcomm error code.
658 static int send_unix_sock(int sock
, void *buf
, size_t len
)
660 /* Check valid length */
665 return lttcomm_send_unix_sock(sock
, buf
, len
);
669 * Free memory of a command context structure.
671 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
673 DBG("Clean command context structure");
675 if ((*cmd_ctx
)->llm
) {
676 free((*cmd_ctx
)->llm
);
678 if ((*cmd_ctx
)->lsm
) {
679 free((*cmd_ctx
)->lsm
);
687 * Notify UST applications using the shm mmap futex.
689 static int notify_ust_apps(int active
)
693 DBG("Notifying applications of session daemon state: %d", active
);
695 /* See shm.c for this call implying mmap, shm and futex calls */
696 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
697 if (wait_shm_mmap
== NULL
) {
701 /* Wake waiting process */
702 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
704 /* Apps notified successfully */
712 * Setup the outgoing data buffer for the response (llm) by allocating the
713 * right amount of memory and copying the original information from the lsm
716 * Return 0 on success, negative value on error.
718 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
719 const void *payload_buf
, size_t payload_len
,
720 const void *cmd_header_buf
, size_t cmd_header_len
)
723 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
724 const size_t cmd_header_offset
= header_len
;
725 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
726 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
728 cmd_ctx
->llm
= zmalloc(total_msg_size
);
730 if (cmd_ctx
->llm
== NULL
) {
736 /* Copy common data */
737 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
738 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
739 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
740 cmd_ctx
->llm
->data_size
= payload_len
;
741 cmd_ctx
->lttng_msg_size
= total_msg_size
;
743 /* Copy command header */
744 if (cmd_header_len
) {
745 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
751 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
760 * Version of setup_lttng_msg() without command header.
762 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
763 void *payload_buf
, size_t payload_len
)
765 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
768 * Update the kernel poll set of all channel fd available over all tracing
769 * session. Add the wakeup pipe at the end of the set.
771 static int update_kernel_poll(struct lttng_poll_event
*events
)
774 struct ltt_session
*session
;
775 struct ltt_kernel_channel
*channel
;
777 DBG("Updating kernel poll set");
780 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
781 session_lock(session
);
782 if (session
->kernel_session
== NULL
) {
783 session_unlock(session
);
787 cds_list_for_each_entry(channel
,
788 &session
->kernel_session
->channel_list
.head
, list
) {
789 /* Add channel fd to the kernel poll set */
790 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
792 session_unlock(session
);
795 DBG("Channel fd %d added to kernel set", channel
->fd
);
797 session_unlock(session
);
799 session_unlock_list();
804 session_unlock_list();
809 * Find the channel fd from 'fd' over all tracing session. When found, check
810 * for new channel stream and send those stream fds to the kernel consumer.
812 * Useful for CPU hotplug feature.
814 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
817 struct ltt_session
*session
;
818 struct ltt_kernel_session
*ksess
;
819 struct ltt_kernel_channel
*channel
;
821 DBG("Updating kernel streams for channel fd %d", fd
);
824 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
825 session_lock(session
);
826 if (session
->kernel_session
== NULL
) {
827 session_unlock(session
);
830 ksess
= session
->kernel_session
;
832 cds_list_for_each_entry(channel
,
833 &ksess
->channel_list
.head
, list
) {
834 struct lttng_ht_iter iter
;
835 struct consumer_socket
*socket
;
837 if (channel
->fd
!= fd
) {
840 DBG("Channel found, updating kernel streams");
841 ret
= kernel_open_channel_stream(channel
);
845 /* Update the stream global counter */
846 ksess
->stream_count_global
+= ret
;
849 * Have we already sent fds to the consumer? If yes, it
850 * means that tracing is started so it is safe to send
851 * our updated stream fds.
853 if (ksess
->consumer_fds_sent
!= 1
854 || ksess
->consumer
== NULL
) {
860 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
861 &iter
.iter
, socket
, node
.node
) {
862 pthread_mutex_lock(socket
->lock
);
863 ret
= kernel_consumer_send_channel_stream(socket
,
865 session
->output_traces
? 1 : 0);
866 pthread_mutex_unlock(socket
->lock
);
874 session_unlock(session
);
876 session_unlock_list();
880 session_unlock(session
);
881 session_unlock_list();
886 * For each tracing session, update newly registered apps. The session list
887 * lock MUST be acquired before calling this.
889 static void update_ust_app(int app_sock
)
891 struct ltt_session
*sess
, *stmp
;
893 /* Consumer is in an ERROR state. Stop any application update. */
894 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
895 /* Stop the update process since the consumer is dead. */
899 /* For all tracing session(s) */
900 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
904 if (!sess
->ust_session
) {
909 assert(app_sock
>= 0);
910 app
= ust_app_find_by_sock(app_sock
);
913 * Application can be unregistered before so
914 * this is possible hence simply stopping the
917 DBG3("UST app update failed to find app sock %d",
921 ust_app_global_update(sess
->ust_session
, app
);
925 session_unlock(sess
);
930 * This thread manage event coming from the kernel.
932 * Features supported in this thread:
935 static void *thread_manage_kernel(void *data
)
937 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
938 uint32_t revents
, nb_fd
;
940 struct lttng_poll_event events
;
942 DBG("[thread] Thread manage kernel started");
944 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
947 * This first step of the while is to clean this structure which could free
948 * non NULL pointers so initialize it before the loop.
950 lttng_poll_init(&events
);
952 if (testpoint(sessiond_thread_manage_kernel
)) {
953 goto error_testpoint
;
956 health_code_update();
958 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
959 goto error_testpoint
;
963 health_code_update();
965 if (update_poll_flag
== 1) {
966 /* Clean events object. We are about to populate it again. */
967 lttng_poll_clean(&events
);
969 ret
= sessiond_set_thread_pollset(&events
, 2);
971 goto error_poll_create
;
974 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
979 /* This will add the available kernel channel if any. */
980 ret
= update_kernel_poll(&events
);
984 update_poll_flag
= 0;
987 DBG("Thread kernel polling");
989 /* Poll infinite value of time */
992 ret
= lttng_poll_wait(&events
, -1);
993 DBG("Thread kernel return from poll on %d fds",
994 LTTNG_POLL_GETNB(&events
));
998 * Restart interrupted system call.
1000 if (errno
== EINTR
) {
1004 } else if (ret
== 0) {
1005 /* Should not happen since timeout is infinite */
1006 ERR("Return value of poll is 0 with an infinite timeout.\n"
1007 "This should not have happened! Continuing...");
1013 for (i
= 0; i
< nb_fd
; i
++) {
1014 /* Fetch once the poll data */
1015 revents
= LTTNG_POLL_GETEV(&events
, i
);
1016 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1018 health_code_update();
1021 /* No activity for this FD (poll implementation). */
1025 /* Thread quit pipe has been closed. Killing thread. */
1026 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1032 /* Check for data on kernel pipe */
1033 if (revents
& LPOLLIN
) {
1034 if (pollfd
== kernel_poll_pipe
[0]) {
1035 (void) lttng_read(kernel_poll_pipe
[0],
1038 * Ret value is useless here, if this pipe gets any actions an
1039 * update is required anyway.
1041 update_poll_flag
= 1;
1045 * New CPU detected by the kernel. Adding kernel stream to
1046 * kernel session and updating the kernel consumer
1048 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1054 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1055 update_poll_flag
= 1;
1058 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1066 lttng_poll_clean(&events
);
1069 utils_close_pipe(kernel_poll_pipe
);
1070 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1073 ERR("Health error occurred in %s", __func__
);
1074 WARN("Kernel thread died unexpectedly. "
1075 "Kernel tracing can continue but CPU hotplug is disabled.");
1077 health_unregister(health_sessiond
);
1078 DBG("Kernel thread dying");
1083 * Signal pthread condition of the consumer data that the thread.
1085 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1087 pthread_mutex_lock(&data
->cond_mutex
);
1090 * The state is set before signaling. It can be any value, it's the waiter
1091 * job to correctly interpret this condition variable associated to the
1092 * consumer pthread_cond.
1094 * A value of 0 means that the corresponding thread of the consumer data
1095 * was not started. 1 indicates that the thread has started and is ready
1096 * for action. A negative value means that there was an error during the
1099 data
->consumer_thread_is_ready
= state
;
1100 (void) pthread_cond_signal(&data
->cond
);
1102 pthread_mutex_unlock(&data
->cond_mutex
);
1106 * This thread manage the consumer error sent back to the session daemon.
1108 static void *thread_manage_consumer(void *data
)
1110 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1111 uint32_t revents
, nb_fd
;
1112 enum lttcomm_return_code code
;
1113 struct lttng_poll_event events
;
1114 struct consumer_data
*consumer_data
= data
;
1115 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1117 DBG("[thread] Manage consumer started");
1119 rcu_register_thread();
1120 rcu_thread_online();
1122 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1124 health_code_update();
1127 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1128 * metadata_sock. Nothing more will be added to this poll set.
1130 ret
= sessiond_set_thread_pollset(&events
, 3);
1136 * The error socket here is already in a listening state which was done
1137 * just before spawning this thread to avoid a race between the consumer
1138 * daemon exec trying to connect and the listen() call.
1140 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1145 health_code_update();
1147 /* Infinite blocking call, waiting for transmission */
1149 health_poll_entry();
1151 if (testpoint(sessiond_thread_manage_consumer
)) {
1155 ret
= lttng_poll_wait(&events
, -1);
1159 * Restart interrupted system call.
1161 if (errno
== EINTR
) {
1169 for (i
= 0; i
< nb_fd
; i
++) {
1170 /* Fetch once the poll data */
1171 revents
= LTTNG_POLL_GETEV(&events
, i
);
1172 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1174 health_code_update();
1177 /* No activity for this FD (poll implementation). */
1181 /* Thread quit pipe has been closed. Killing thread. */
1182 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1188 /* Event on the registration socket */
1189 if (pollfd
== consumer_data
->err_sock
) {
1190 if (revents
& LPOLLIN
) {
1192 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1193 ERR("consumer err socket poll error");
1196 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1202 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1208 * Set the CLOEXEC flag. Return code is useless because either way, the
1211 (void) utils_set_fd_cloexec(sock
);
1213 health_code_update();
1215 DBG2("Receiving code from consumer err_sock");
1217 /* Getting status code from kconsumerd */
1218 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1219 sizeof(enum lttcomm_return_code
));
1224 health_code_update();
1225 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1226 ERR("consumer error when waiting for SOCK_READY : %s",
1227 lttcomm_get_readable_code(-code
));
1231 /* Connect both command and metadata sockets. */
1232 consumer_data
->cmd_sock
=
1233 lttcomm_connect_unix_sock(
1234 consumer_data
->cmd_unix_sock_path
);
1235 consumer_data
->metadata_fd
=
1236 lttcomm_connect_unix_sock(
1237 consumer_data
->cmd_unix_sock_path
);
1238 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1239 PERROR("consumer connect cmd socket");
1240 /* On error, signal condition and quit. */
1241 signal_consumer_condition(consumer_data
, -1);
1245 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1247 /* Create metadata socket lock. */
1248 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1249 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1250 PERROR("zmalloc pthread mutex");
1253 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1255 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1256 DBG("Consumer metadata socket ready (fd: %d)",
1257 consumer_data
->metadata_fd
);
1260 * Remove the consumerd error sock since we've established a connection.
1262 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1267 /* Add new accepted error socket. */
1268 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1273 /* Add metadata socket that is successfully connected. */
1274 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1275 LPOLLIN
| LPOLLRDHUP
);
1280 health_code_update();
1283 * Transfer the write-end of the channel monitoring and rotate pipe
1284 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1285 * SET_CHANNEL_ROTATE_PIPE commands.
1287 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1288 if (!cmd_socket_wrapper
) {
1291 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1293 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1294 consumer_data
->channel_monitor_pipe
);
1299 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1300 consumer_data
->channel_rotate_pipe
);
1305 /* Discard the socket wrapper as it is no longer needed. */
1306 consumer_destroy_socket(cmd_socket_wrapper
);
1307 cmd_socket_wrapper
= NULL
;
1309 /* The thread is completely initialized, signal that it is ready. */
1310 signal_consumer_condition(consumer_data
, 1);
1312 /* Infinite blocking call, waiting for transmission */
1315 health_code_update();
1317 /* Exit the thread because the thread quit pipe has been triggered. */
1319 /* Not a health error. */
1324 health_poll_entry();
1325 ret
= lttng_poll_wait(&events
, -1);
1329 * Restart interrupted system call.
1331 if (errno
== EINTR
) {
1339 for (i
= 0; i
< nb_fd
; i
++) {
1340 /* Fetch once the poll data */
1341 revents
= LTTNG_POLL_GETEV(&events
, i
);
1342 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1344 health_code_update();
1347 /* No activity for this FD (poll implementation). */
1352 * Thread quit pipe has been triggered, flag that we should stop
1353 * but continue the current loop to handle potential data from
1356 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1358 if (pollfd
== sock
) {
1359 /* Event on the consumerd socket */
1360 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1361 && !(revents
& LPOLLIN
)) {
1362 ERR("consumer err socket second poll error");
1365 health_code_update();
1366 /* Wait for any kconsumerd error */
1367 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1368 sizeof(enum lttcomm_return_code
));
1370 ERR("consumer closed the command socket");
1374 ERR("consumer return code : %s",
1375 lttcomm_get_readable_code(-code
));
1378 } else if (pollfd
== consumer_data
->metadata_fd
) {
1379 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1380 && !(revents
& LPOLLIN
)) {
1381 ERR("consumer err metadata socket second poll error");
1384 /* UST metadata requests */
1385 ret
= ust_consumer_metadata_request(
1386 &consumer_data
->metadata_sock
);
1388 ERR("Handling metadata request");
1392 /* No need for an else branch all FDs are tested prior. */
1394 health_code_update();
1400 * We lock here because we are about to close the sockets and some other
1401 * thread might be using them so get exclusive access which will abort all
1402 * other consumer command by other threads.
1404 pthread_mutex_lock(&consumer_data
->lock
);
1406 /* Immediately set the consumerd state to stopped */
1407 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1408 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1409 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1410 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1411 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1413 /* Code flow error... */
1417 if (consumer_data
->err_sock
>= 0) {
1418 ret
= close(consumer_data
->err_sock
);
1422 consumer_data
->err_sock
= -1;
1424 if (consumer_data
->cmd_sock
>= 0) {
1425 ret
= close(consumer_data
->cmd_sock
);
1429 consumer_data
->cmd_sock
= -1;
1431 if (consumer_data
->metadata_sock
.fd_ptr
&&
1432 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1433 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1445 unlink(consumer_data
->err_unix_sock_path
);
1446 unlink(consumer_data
->cmd_unix_sock_path
);
1447 pthread_mutex_unlock(&consumer_data
->lock
);
1449 /* Cleanup metadata socket mutex. */
1450 if (consumer_data
->metadata_sock
.lock
) {
1451 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1452 free(consumer_data
->metadata_sock
.lock
);
1454 lttng_poll_clean(&events
);
1456 if (cmd_socket_wrapper
) {
1457 consumer_destroy_socket(cmd_socket_wrapper
);
1462 ERR("Health error occurred in %s", __func__
);
1464 health_unregister(health_sessiond
);
1465 DBG("consumer thread cleanup completed");
1467 rcu_thread_offline();
1468 rcu_unregister_thread();
1474 * This thread receives application command sockets (FDs) on the
1475 * apps_cmd_pipe and waits (polls) on them until they are closed
1476 * or an error occurs.
1478 * At that point, it flushes the data (tracing and metadata) associated
1479 * with this application and tears down ust app sessions and other
1480 * associated data structures through ust_app_unregister().
1482 * Note that this thread never sends commands to the applications
1483 * through the command sockets; it merely listens for hang-ups
1484 * and errors on those sockets and cleans-up as they occur.
1486 static void *thread_manage_apps(void *data
)
1488 int i
, ret
, pollfd
, err
= -1;
1490 uint32_t revents
, nb_fd
;
1491 struct lttng_poll_event events
;
1493 DBG("[thread] Manage application started");
1495 rcu_register_thread();
1496 rcu_thread_online();
1498 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1500 if (testpoint(sessiond_thread_manage_apps
)) {
1501 goto error_testpoint
;
1504 health_code_update();
1506 ret
= sessiond_set_thread_pollset(&events
, 2);
1508 goto error_poll_create
;
1511 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1516 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1520 health_code_update();
1523 DBG("Apps thread polling");
1525 /* Inifinite blocking call, waiting for transmission */
1527 health_poll_entry();
1528 ret
= lttng_poll_wait(&events
, -1);
1529 DBG("Apps thread return from poll on %d fds",
1530 LTTNG_POLL_GETNB(&events
));
1534 * Restart interrupted system call.
1536 if (errno
== EINTR
) {
1544 for (i
= 0; i
< nb_fd
; i
++) {
1545 /* Fetch once the poll data */
1546 revents
= LTTNG_POLL_GETEV(&events
, i
);
1547 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1549 health_code_update();
1552 /* No activity for this FD (poll implementation). */
1556 /* Thread quit pipe has been closed. Killing thread. */
1557 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1563 /* Inspect the apps cmd pipe */
1564 if (pollfd
== apps_cmd_pipe
[0]) {
1565 if (revents
& LPOLLIN
) {
1569 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1570 if (size_ret
< sizeof(sock
)) {
1571 PERROR("read apps cmd pipe");
1575 health_code_update();
1578 * Since this is a command socket (write then read),
1579 * we only monitor the error events of the socket.
1581 ret
= lttng_poll_add(&events
, sock
,
1582 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1587 DBG("Apps with sock %d added to poll set", sock
);
1588 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1589 ERR("Apps command pipe error");
1592 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1597 * At this point, we know that a registered application made
1598 * the event at poll_wait.
1600 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1601 /* Removing from the poll set */
1602 ret
= lttng_poll_del(&events
, pollfd
);
1607 /* Socket closed on remote end. */
1608 ust_app_unregister(pollfd
);
1610 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1615 health_code_update();
1621 lttng_poll_clean(&events
);
1624 utils_close_pipe(apps_cmd_pipe
);
1625 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1628 * We don't clean the UST app hash table here since already registered
1629 * applications can still be controlled so let them be until the session
1630 * daemon dies or the applications stop.
1635 ERR("Health error occurred in %s", __func__
);
1637 health_unregister(health_sessiond
);
1638 DBG("Application communication apps thread cleanup complete");
1639 rcu_thread_offline();
1640 rcu_unregister_thread();
1645 * Send a socket to a thread This is called from the dispatch UST registration
1646 * thread once all sockets are set for the application.
1648 * The sock value can be invalid, we don't really care, the thread will handle
1649 * it and make the necessary cleanup if so.
1651 * On success, return 0 else a negative value being the errno message of the
1654 static int send_socket_to_thread(int fd
, int sock
)
1659 * It's possible that the FD is set as invalid with -1 concurrently just
1660 * before calling this function being a shutdown state of the thread.
1667 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1668 if (ret
< sizeof(sock
)) {
1669 PERROR("write apps pipe %d", fd
);
1676 /* All good. Don't send back the write positive ret value. */
1683 * Sanitize the wait queue of the dispatch registration thread meaning removing
1684 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1685 * notify socket is never received.
1687 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1689 int ret
, nb_fd
= 0, i
;
1690 unsigned int fd_added
= 0;
1691 struct lttng_poll_event events
;
1692 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1696 lttng_poll_init(&events
);
1698 /* Just skip everything for an empty queue. */
1699 if (!wait_queue
->count
) {
1703 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1708 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1709 &wait_queue
->head
, head
) {
1710 assert(wait_node
->app
);
1711 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1712 LPOLLHUP
| LPOLLERR
);
1725 * Poll but don't block so we can quickly identify the faulty events and
1726 * clean them afterwards from the wait queue.
1728 ret
= lttng_poll_wait(&events
, 0);
1734 for (i
= 0; i
< nb_fd
; i
++) {
1735 /* Get faulty FD. */
1736 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1737 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1740 /* No activity for this FD (poll implementation). */
1744 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1745 &wait_queue
->head
, head
) {
1746 if (pollfd
== wait_node
->app
->sock
&&
1747 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1748 cds_list_del(&wait_node
->head
);
1749 wait_queue
->count
--;
1750 ust_app_destroy(wait_node
->app
);
1753 * Silence warning of use-after-free in
1754 * cds_list_for_each_entry_safe which uses
1755 * __typeof__(*wait_node).
1760 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1767 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1771 lttng_poll_clean(&events
);
1775 lttng_poll_clean(&events
);
1777 ERR("Unable to sanitize wait queue");
1782 * Dispatch request from the registration threads to the application
1783 * communication thread.
1785 static void *thread_dispatch_ust_registration(void *data
)
1788 struct cds_wfcq_node
*node
;
1789 struct ust_command
*ust_cmd
= NULL
;
1790 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1791 struct ust_reg_wait_queue wait_queue
= {
1795 rcu_register_thread();
1797 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1799 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1800 goto error_testpoint
;
1803 health_code_update();
1805 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1807 DBG("[thread] Dispatch UST command started");
1810 health_code_update();
1812 /* Atomically prepare the queue futex */
1813 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1815 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1820 struct ust_app
*app
= NULL
;
1824 * Make sure we don't have node(s) that have hung up before receiving
1825 * the notify socket. This is to clean the list in order to avoid
1826 * memory leaks from notify socket that are never seen.
1828 sanitize_wait_queue(&wait_queue
);
1830 health_code_update();
1831 /* Dequeue command for registration */
1832 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1834 DBG("Woken up but nothing in the UST command queue");
1835 /* Continue thread execution */
1839 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1841 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1842 " gid:%d sock:%d name:%s (version %d.%d)",
1843 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1844 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1845 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1846 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1848 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1849 wait_node
= zmalloc(sizeof(*wait_node
));
1851 PERROR("zmalloc wait_node dispatch");
1852 ret
= close(ust_cmd
->sock
);
1854 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1856 lttng_fd_put(LTTNG_FD_APPS
, 1);
1860 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1862 /* Create application object if socket is CMD. */
1863 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1865 if (!wait_node
->app
) {
1866 ret
= close(ust_cmd
->sock
);
1868 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1870 lttng_fd_put(LTTNG_FD_APPS
, 1);
1876 * Add application to the wait queue so we can set the notify
1877 * socket before putting this object in the global ht.
1879 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1884 * We have to continue here since we don't have the notify
1885 * socket and the application MUST be added to the hash table
1886 * only at that moment.
1891 * Look for the application in the local wait queue and set the
1892 * notify socket if found.
1894 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1895 &wait_queue
.head
, head
) {
1896 health_code_update();
1897 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1898 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1899 cds_list_del(&wait_node
->head
);
1901 app
= wait_node
->app
;
1903 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1909 * With no application at this stage the received socket is
1910 * basically useless so close it before we free the cmd data
1911 * structure for good.
1914 ret
= close(ust_cmd
->sock
);
1916 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1918 lttng_fd_put(LTTNG_FD_APPS
, 1);
1925 * @session_lock_list
1927 * Lock the global session list so from the register up to the
1928 * registration done message, no thread can see the application
1929 * and change its state.
1931 session_lock_list();
1935 * Add application to the global hash table. This needs to be
1936 * done before the update to the UST registry can locate the
1941 /* Set app version. This call will print an error if needed. */
1942 (void) ust_app_version(app
);
1944 /* Send notify socket through the notify pipe. */
1945 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1949 session_unlock_list();
1951 * No notify thread, stop the UST tracing. However, this is
1952 * not an internal error of the this thread thus setting
1953 * the health error code to a normal exit.
1960 * Update newly registered application with the tracing
1961 * registry info already enabled information.
1963 update_ust_app(app
->sock
);
1966 * Don't care about return value. Let the manage apps threads
1967 * handle app unregistration upon socket close.
1969 (void) ust_app_register_done(app
);
1972 * Even if the application socket has been closed, send the app
1973 * to the thread and unregistration will take place at that
1976 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1979 session_unlock_list();
1981 * No apps. thread, stop the UST tracing. However, this is
1982 * not an internal error of the this thread thus setting
1983 * the health error code to a normal exit.
1990 session_unlock_list();
1992 } while (node
!= NULL
);
1994 health_poll_entry();
1995 /* Futex wait on queue. Blocking call on futex() */
1996 futex_nto1_wait(&ust_cmd_queue
.futex
);
1999 /* Normal exit, no error */
2003 /* Clean up wait queue. */
2004 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2005 &wait_queue
.head
, head
) {
2006 cds_list_del(&wait_node
->head
);
2011 /* Empty command queue. */
2013 /* Dequeue command for registration */
2014 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2018 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2019 ret
= close(ust_cmd
->sock
);
2021 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2023 lttng_fd_put(LTTNG_FD_APPS
, 1);
2028 DBG("Dispatch thread dying");
2031 ERR("Health error occurred in %s", __func__
);
2033 health_unregister(health_sessiond
);
2034 rcu_unregister_thread();
2039 * This thread manage application registration.
2041 static void *thread_registration_apps(void *data
)
2043 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2044 uint32_t revents
, nb_fd
;
2045 struct lttng_poll_event events
;
2047 * Get allocated in this thread, enqueued to a global queue, dequeued and
2048 * freed in the manage apps thread.
2050 struct ust_command
*ust_cmd
= NULL
;
2052 DBG("[thread] Manage application registration started");
2054 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2056 if (testpoint(sessiond_thread_registration_apps
)) {
2057 goto error_testpoint
;
2060 ret
= lttcomm_listen_unix_sock(apps_sock
);
2066 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2067 * more will be added to this poll set.
2069 ret
= sessiond_set_thread_pollset(&events
, 2);
2071 goto error_create_poll
;
2074 /* Add the application registration socket */
2075 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2077 goto error_poll_add
;
2080 /* Notify all applications to register */
2081 ret
= notify_ust_apps(1);
2083 ERR("Failed to notify applications or create the wait shared memory.\n"
2084 "Execution continues but there might be problem for already\n"
2085 "running applications that wishes to register.");
2089 DBG("Accepting application registration");
2091 /* Inifinite blocking call, waiting for transmission */
2093 health_poll_entry();
2094 ret
= lttng_poll_wait(&events
, -1);
2098 * Restart interrupted system call.
2100 if (errno
== EINTR
) {
2108 for (i
= 0; i
< nb_fd
; i
++) {
2109 health_code_update();
2111 /* Fetch once the poll data */
2112 revents
= LTTNG_POLL_GETEV(&events
, i
);
2113 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2116 /* No activity for this FD (poll implementation). */
2120 /* Thread quit pipe has been closed. Killing thread. */
2121 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2127 /* Event on the registration socket */
2128 if (pollfd
== apps_sock
) {
2129 if (revents
& LPOLLIN
) {
2130 sock
= lttcomm_accept_unix_sock(apps_sock
);
2136 * Set socket timeout for both receiving and ending.
2137 * app_socket_timeout is in seconds, whereas
2138 * lttcomm_setsockopt_rcv_timeout and
2139 * lttcomm_setsockopt_snd_timeout expect msec as
2142 if (config
.app_socket_timeout
>= 0) {
2143 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2144 config
.app_socket_timeout
* 1000);
2145 (void) lttcomm_setsockopt_snd_timeout(sock
,
2146 config
.app_socket_timeout
* 1000);
2150 * Set the CLOEXEC flag. Return code is useless because
2151 * either way, the show must go on.
2153 (void) utils_set_fd_cloexec(sock
);
2155 /* Create UST registration command for enqueuing */
2156 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2157 if (ust_cmd
== NULL
) {
2158 PERROR("ust command zmalloc");
2167 * Using message-based transmissions to ensure we don't
2168 * have to deal with partially received messages.
2170 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2172 ERR("Exhausted file descriptors allowed for applications.");
2182 health_code_update();
2183 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2186 /* Close socket of the application. */
2191 lttng_fd_put(LTTNG_FD_APPS
, 1);
2195 health_code_update();
2197 ust_cmd
->sock
= sock
;
2200 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2201 " gid:%d sock:%d name:%s (version %d.%d)",
2202 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2203 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2204 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2205 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2208 * Lock free enqueue the registration request. The red pill
2209 * has been taken! This apps will be part of the *system*.
2211 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2214 * Wake the registration queue futex. Implicit memory
2215 * barrier with the exchange in cds_wfcq_enqueue.
2217 futex_nto1_wake(&ust_cmd_queue
.futex
);
2218 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2219 ERR("Register apps socket poll error");
2222 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2231 /* Notify that the registration thread is gone */
2234 if (apps_sock
>= 0) {
2235 ret
= close(apps_sock
);
2245 lttng_fd_put(LTTNG_FD_APPS
, 1);
2247 unlink(config
.apps_unix_sock_path
.value
);
2250 lttng_poll_clean(&events
);
2254 DBG("UST Registration thread cleanup complete");
2257 ERR("Health error occurred in %s", __func__
);
2259 health_unregister(health_sessiond
);
2265 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2266 * exec or it will fails.
2268 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2271 struct timespec timeout
;
2274 * Make sure we set the readiness flag to 0 because we are NOT ready.
2275 * This access to consumer_thread_is_ready does not need to be
2276 * protected by consumer_data.cond_mutex (yet) since the consumer
2277 * management thread has not been started at this point.
2279 consumer_data
->consumer_thread_is_ready
= 0;
2281 /* Setup pthread condition */
2282 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2285 PERROR("pthread_condattr_init consumer data");
2290 * Set the monotonic clock in order to make sure we DO NOT jump in time
2291 * between the clock_gettime() call and the timedwait call. See bug #324
2292 * for a more details and how we noticed it.
2294 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2297 PERROR("pthread_condattr_setclock consumer data");
2301 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2304 PERROR("pthread_cond_init consumer data");
2308 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2309 thread_manage_consumer
, consumer_data
);
2312 PERROR("pthread_create consumer");
2317 /* We are about to wait on a pthread condition */
2318 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2320 /* Get time for sem_timedwait absolute timeout */
2321 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2323 * Set the timeout for the condition timed wait even if the clock gettime
2324 * call fails since we might loop on that call and we want to avoid to
2325 * increment the timeout too many times.
2327 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2330 * The following loop COULD be skipped in some conditions so this is why we
2331 * set ret to 0 in order to make sure at least one round of the loop is
2337 * Loop until the condition is reached or when a timeout is reached. Note
2338 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2339 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2340 * possible. This loop does not take any chances and works with both of
2343 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2344 if (clock_ret
< 0) {
2345 PERROR("clock_gettime spawn consumer");
2346 /* Infinite wait for the consumerd thread to be ready */
2347 ret
= pthread_cond_wait(&consumer_data
->cond
,
2348 &consumer_data
->cond_mutex
);
2350 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2351 &consumer_data
->cond_mutex
, &timeout
);
2355 /* Release the pthread condition */
2356 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2360 if (ret
== ETIMEDOUT
) {
2364 * Call has timed out so we kill the kconsumerd_thread and return
2367 ERR("Condition timed out. The consumer thread was never ready."
2369 pth_ret
= pthread_cancel(consumer_data
->thread
);
2371 PERROR("pthread_cancel consumer thread");
2374 PERROR("pthread_cond_wait failed consumer thread");
2376 /* Caller is expecting a negative value on failure. */
2381 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2382 if (consumer_data
->pid
== 0) {
2383 ERR("Consumerd did not start");
2384 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2387 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2396 * Join consumer thread
2398 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2402 /* Consumer pid must be a real one. */
2403 if (consumer_data
->pid
> 0) {
2405 ret
= kill(consumer_data
->pid
, SIGTERM
);
2407 PERROR("Error killing consumer daemon");
2410 return pthread_join(consumer_data
->thread
, &status
);
2417 * Fork and exec a consumer daemon (consumerd).
2419 * Return pid if successful else -1.
2421 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2425 const char *consumer_to_use
;
2426 const char *verbosity
;
2429 DBG("Spawning consumerd");
2436 if (config
.verbose_consumer
) {
2437 verbosity
= "--verbose";
2438 } else if (lttng_opt_quiet
) {
2439 verbosity
= "--quiet";
2444 switch (consumer_data
->type
) {
2445 case LTTNG_CONSUMER_KERNEL
:
2447 * Find out which consumerd to execute. We will first try the
2448 * 64-bit path, then the sessiond's installation directory, and
2449 * fallback on the 32-bit one,
2451 DBG3("Looking for a kernel consumer at these locations:");
2452 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2453 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2454 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2455 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2456 DBG3("Found location #1");
2457 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2458 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2459 DBG3("Found location #2");
2460 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2461 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2462 DBG3("Found location #3");
2463 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2465 DBG("Could not find any valid consumerd executable");
2469 DBG("Using kernel consumer at: %s", consumer_to_use
);
2470 (void) execl(consumer_to_use
,
2471 "lttng-consumerd", verbosity
, "-k",
2472 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2473 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2474 "--group", config
.tracing_group_name
.value
,
2477 case LTTNG_CONSUMER64_UST
:
2479 if (config
.consumerd64_lib_dir
.value
) {
2484 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2488 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2489 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2494 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2495 if (tmp
[0] != '\0') {
2496 strcat(tmpnew
, ":");
2497 strcat(tmpnew
, tmp
);
2499 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2506 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2507 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2508 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2509 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2510 "--group", config
.tracing_group_name
.value
,
2514 case LTTNG_CONSUMER32_UST
:
2516 if (config
.consumerd32_lib_dir
.value
) {
2521 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2525 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2526 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2531 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2532 if (tmp
[0] != '\0') {
2533 strcat(tmpnew
, ":");
2534 strcat(tmpnew
, tmp
);
2536 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2543 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2544 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2545 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2546 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2547 "--group", config
.tracing_group_name
.value
,
2552 ERR("unknown consumer type");
2556 PERROR("Consumer execl()");
2558 /* Reaching this point, we got a failure on our execl(). */
2560 } else if (pid
> 0) {
2563 PERROR("start consumer fork");
2571 * Spawn the consumerd daemon and session daemon thread.
2573 static int start_consumerd(struct consumer_data
*consumer_data
)
2578 * Set the listen() state on the socket since there is a possible race
2579 * between the exec() of the consumer daemon and this call if place in the
2580 * consumer thread. See bug #366 for more details.
2582 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2587 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2588 if (consumer_data
->pid
!= 0) {
2589 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2593 ret
= spawn_consumerd(consumer_data
);
2595 ERR("Spawning consumerd failed");
2596 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2600 /* Setting up the consumer_data pid */
2601 consumer_data
->pid
= ret
;
2602 DBG2("Consumer pid %d", consumer_data
->pid
);
2603 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2605 DBG2("Spawning consumer control thread");
2606 ret
= spawn_consumer_thread(consumer_data
);
2608 ERR("Fatal error spawning consumer control thread");
2616 /* Cleanup already created sockets on error. */
2617 if (consumer_data
->err_sock
>= 0) {
2620 err
= close(consumer_data
->err_sock
);
2622 PERROR("close consumer data error socket");
2629 * Setup necessary data for kernel tracer action.
2631 static int init_kernel_tracer(void)
2635 /* Modprobe lttng kernel modules */
2636 ret
= modprobe_lttng_control();
2641 /* Open debugfs lttng */
2642 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2643 if (kernel_tracer_fd
< 0) {
2644 DBG("Failed to open %s", module_proc_lttng
);
2648 /* Validate kernel version */
2649 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2650 &kernel_tracer_abi_version
);
2655 ret
= modprobe_lttng_data();
2660 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2667 WARN("Kernel tracer does not support buffer monitoring. "
2668 "The monitoring timer of channels in the kernel domain "
2669 "will be set to 0 (disabled).");
2672 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2676 modprobe_remove_lttng_control();
2677 ret
= close(kernel_tracer_fd
);
2681 kernel_tracer_fd
= -1;
2682 return LTTNG_ERR_KERN_VERSION
;
2685 ret
= close(kernel_tracer_fd
);
2691 modprobe_remove_lttng_control();
2694 WARN("No kernel tracer available");
2695 kernel_tracer_fd
= -1;
2697 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2699 return LTTNG_ERR_KERN_NA
;
2705 * Copy consumer output from the tracing session to the domain session. The
2706 * function also applies the right modification on a per domain basis for the
2707 * trace files destination directory.
2709 * Should *NOT* be called with RCU read-side lock held.
2711 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2714 const char *dir_name
;
2715 struct consumer_output
*consumer
;
2718 assert(session
->consumer
);
2721 case LTTNG_DOMAIN_KERNEL
:
2722 DBG3("Copying tracing session consumer output in kernel session");
2724 * XXX: We should audit the session creation and what this function
2725 * does "extra" in order to avoid a destroy since this function is used
2726 * in the domain session creation (kernel and ust) only. Same for UST
2729 if (session
->kernel_session
->consumer
) {
2730 consumer_output_put(session
->kernel_session
->consumer
);
2732 session
->kernel_session
->consumer
=
2733 consumer_copy_output(session
->consumer
);
2734 /* Ease our life a bit for the next part */
2735 consumer
= session
->kernel_session
->consumer
;
2736 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2738 case LTTNG_DOMAIN_JUL
:
2739 case LTTNG_DOMAIN_LOG4J
:
2740 case LTTNG_DOMAIN_PYTHON
:
2741 case LTTNG_DOMAIN_UST
:
2742 DBG3("Copying tracing session consumer output in UST session");
2743 if (session
->ust_session
->consumer
) {
2744 consumer_output_put(session
->ust_session
->consumer
);
2746 session
->ust_session
->consumer
=
2747 consumer_copy_output(session
->consumer
);
2748 /* Ease our life a bit for the next part */
2749 consumer
= session
->ust_session
->consumer
;
2750 dir_name
= DEFAULT_UST_TRACE_DIR
;
2753 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2757 /* Append correct directory to subdir */
2758 strncat(consumer
->subdir
, dir_name
,
2759 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2760 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2769 * Create an UST session and add it to the session ust list.
2771 * Should *NOT* be called with RCU read-side lock held.
2773 static int create_ust_session(struct ltt_session
*session
,
2774 struct lttng_domain
*domain
)
2777 struct ltt_ust_session
*lus
= NULL
;
2781 assert(session
->consumer
);
2783 switch (domain
->type
) {
2784 case LTTNG_DOMAIN_JUL
:
2785 case LTTNG_DOMAIN_LOG4J
:
2786 case LTTNG_DOMAIN_PYTHON
:
2787 case LTTNG_DOMAIN_UST
:
2790 ERR("Unknown UST domain on create session %d", domain
->type
);
2791 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2795 DBG("Creating UST session");
2797 lus
= trace_ust_create_session(session
->id
);
2799 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2803 lus
->uid
= session
->uid
;
2804 lus
->gid
= session
->gid
;
2805 lus
->output_traces
= session
->output_traces
;
2806 lus
->snapshot_mode
= session
->snapshot_mode
;
2807 lus
->live_timer_interval
= session
->live_timer
;
2808 session
->ust_session
= lus
;
2809 if (session
->shm_path
[0]) {
2810 strncpy(lus
->root_shm_path
, session
->shm_path
,
2811 sizeof(lus
->root_shm_path
));
2812 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2813 strncpy(lus
->shm_path
, session
->shm_path
,
2814 sizeof(lus
->shm_path
));
2815 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2816 strncat(lus
->shm_path
, "/ust",
2817 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2819 /* Copy session output to the newly created UST session */
2820 ret
= copy_session_consumer(domain
->type
, session
);
2821 if (ret
!= LTTNG_OK
) {
2829 session
->ust_session
= NULL
;
2834 * Create a kernel tracer session then create the default channel.
2836 static int create_kernel_session(struct ltt_session
*session
)
2840 DBG("Creating kernel session");
2842 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2844 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2848 /* Code flow safety */
2849 assert(session
->kernel_session
);
2851 /* Copy session output to the newly created Kernel session */
2852 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2853 if (ret
!= LTTNG_OK
) {
2857 session
->kernel_session
->uid
= session
->uid
;
2858 session
->kernel_session
->gid
= session
->gid
;
2859 session
->kernel_session
->output_traces
= session
->output_traces
;
2860 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2865 trace_kernel_destroy_session(session
->kernel_session
);
2866 session
->kernel_session
= NULL
;
2871 * Count number of session permitted by uid/gid.
2873 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2876 struct ltt_session
*session
;
2878 DBG("Counting number of available session for UID %d GID %d",
2880 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2882 * Only list the sessions the user can control.
2884 if (!session_access_ok(session
, uid
, gid
)) {
2893 * Check if the current kernel tracer supports the session rotation feature.
2894 * Return 1 if it does, 0 otherwise.
2896 static int check_rotate_compatible(void)
2900 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2901 DBG("Kernel tracer version is not compatible with the rotation feature");
2909 * Process the command requested by the lttng client within the command
2910 * context structure. This function make sure that the return structure (llm)
2911 * is set and ready for transmission before returning.
2913 * Return any error encountered or 0 for success.
2915 * "sock" is only used for special-case var. len data.
2917 * Should *NOT* be called with RCU read-side lock held.
2919 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2923 int need_tracing_session
= 1;
2926 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2928 assert(!rcu_read_ongoing());
2932 switch (cmd_ctx
->lsm
->cmd_type
) {
2933 case LTTNG_CREATE_SESSION
:
2934 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2935 case LTTNG_CREATE_SESSION_LIVE
:
2936 case LTTNG_DESTROY_SESSION
:
2937 case LTTNG_LIST_SESSIONS
:
2938 case LTTNG_LIST_DOMAINS
:
2939 case LTTNG_START_TRACE
:
2940 case LTTNG_STOP_TRACE
:
2941 case LTTNG_DATA_PENDING
:
2942 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2943 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2944 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2945 case LTTNG_SNAPSHOT_RECORD
:
2946 case LTTNG_SAVE_SESSION
:
2947 case LTTNG_SET_SESSION_SHM_PATH
:
2948 case LTTNG_REGENERATE_METADATA
:
2949 case LTTNG_REGENERATE_STATEDUMP
:
2950 case LTTNG_REGISTER_TRIGGER
:
2951 case LTTNG_UNREGISTER_TRIGGER
:
2952 case LTTNG_ROTATE_SESSION
:
2953 case LTTNG_ROTATION_GET_INFO
:
2954 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
2955 case LTTNG_ROTATION_SET_SCHEDULE
:
2956 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
2957 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
2964 if (config
.no_kernel
&& need_domain
2965 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2967 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2969 ret
= LTTNG_ERR_KERN_NA
;
2974 /* Deny register consumer if we already have a spawned consumer. */
2975 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2976 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2977 if (kconsumer_data
.pid
> 0) {
2978 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2979 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2982 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2986 * Check for command that don't needs to allocate a returned payload. We do
2987 * this here so we don't have to make the call for no payload at each
2990 switch(cmd_ctx
->lsm
->cmd_type
) {
2991 case LTTNG_LIST_SESSIONS
:
2992 case LTTNG_LIST_TRACEPOINTS
:
2993 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2994 case LTTNG_LIST_DOMAINS
:
2995 case LTTNG_LIST_CHANNELS
:
2996 case LTTNG_LIST_EVENTS
:
2997 case LTTNG_LIST_SYSCALLS
:
2998 case LTTNG_LIST_TRACKER_PIDS
:
2999 case LTTNG_DATA_PENDING
:
3000 case LTTNG_ROTATE_SESSION
:
3001 case LTTNG_ROTATION_GET_INFO
:
3002 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3003 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3006 /* Setup lttng message with no payload */
3007 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3009 /* This label does not try to unlock the session */
3010 goto init_setup_error
;
3014 /* Commands that DO NOT need a session. */
3015 switch (cmd_ctx
->lsm
->cmd_type
) {
3016 case LTTNG_CREATE_SESSION
:
3017 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3018 case LTTNG_CREATE_SESSION_LIVE
:
3019 case LTTNG_LIST_SESSIONS
:
3020 case LTTNG_LIST_TRACEPOINTS
:
3021 case LTTNG_LIST_SYSCALLS
:
3022 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3023 case LTTNG_SAVE_SESSION
:
3024 case LTTNG_REGISTER_TRIGGER
:
3025 case LTTNG_UNREGISTER_TRIGGER
:
3026 need_tracing_session
= 0;
3029 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3031 * We keep the session list lock across _all_ commands
3032 * for now, because the per-session lock does not
3033 * handle teardown properly.
3035 session_lock_list();
3036 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3037 if (cmd_ctx
->session
== NULL
) {
3038 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3041 /* Acquire lock for the session */
3042 session_lock(cmd_ctx
->session
);
3048 * Commands that need a valid session but should NOT create one if none
3049 * exists. Instead of creating one and destroying it when the command is
3050 * handled, process that right before so we save some round trip in useless
3053 switch (cmd_ctx
->lsm
->cmd_type
) {
3054 case LTTNG_DISABLE_CHANNEL
:
3055 case LTTNG_DISABLE_EVENT
:
3056 switch (cmd_ctx
->lsm
->domain
.type
) {
3057 case LTTNG_DOMAIN_KERNEL
:
3058 if (!cmd_ctx
->session
->kernel_session
) {
3059 ret
= LTTNG_ERR_NO_CHANNEL
;
3063 case LTTNG_DOMAIN_JUL
:
3064 case LTTNG_DOMAIN_LOG4J
:
3065 case LTTNG_DOMAIN_PYTHON
:
3066 case LTTNG_DOMAIN_UST
:
3067 if (!cmd_ctx
->session
->ust_session
) {
3068 ret
= LTTNG_ERR_NO_CHANNEL
;
3073 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3085 * Check domain type for specific "pre-action".
3087 switch (cmd_ctx
->lsm
->domain
.type
) {
3088 case LTTNG_DOMAIN_KERNEL
:
3090 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3094 /* Kernel tracer check */
3095 if (kernel_tracer_fd
== -1) {
3096 /* Basically, load kernel tracer modules */
3097 ret
= init_kernel_tracer();
3103 /* Consumer is in an ERROR state. Report back to client */
3104 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3105 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3109 /* Need a session for kernel command */
3110 if (need_tracing_session
) {
3111 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3112 ret
= create_kernel_session(cmd_ctx
->session
);
3114 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3119 /* Start the kernel consumer daemon */
3120 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3121 if (kconsumer_data
.pid
== 0 &&
3122 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3123 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3124 ret
= start_consumerd(&kconsumer_data
);
3126 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3129 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3131 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3135 * The consumer was just spawned so we need to add the socket to
3136 * the consumer output of the session if exist.
3138 ret
= consumer_create_socket(&kconsumer_data
,
3139 cmd_ctx
->session
->kernel_session
->consumer
);
3146 case LTTNG_DOMAIN_JUL
:
3147 case LTTNG_DOMAIN_LOG4J
:
3148 case LTTNG_DOMAIN_PYTHON
:
3149 case LTTNG_DOMAIN_UST
:
3151 if (!ust_app_supported()) {
3152 ret
= LTTNG_ERR_NO_UST
;
3155 /* Consumer is in an ERROR state. Report back to client */
3156 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3157 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3161 if (need_tracing_session
) {
3162 /* Create UST session if none exist. */
3163 if (cmd_ctx
->session
->ust_session
== NULL
) {
3164 ret
= create_ust_session(cmd_ctx
->session
,
3165 &cmd_ctx
->lsm
->domain
);
3166 if (ret
!= LTTNG_OK
) {
3171 /* Start the UST consumer daemons */
3173 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3174 if (config
.consumerd64_bin_path
.value
&&
3175 ustconsumer64_data
.pid
== 0 &&
3176 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3177 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3178 ret
= start_consumerd(&ustconsumer64_data
);
3180 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3181 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3185 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3186 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3188 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3192 * Setup socket for consumer 64 bit. No need for atomic access
3193 * since it was set above and can ONLY be set in this thread.
3195 ret
= consumer_create_socket(&ustconsumer64_data
,
3196 cmd_ctx
->session
->ust_session
->consumer
);
3202 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3203 if (config
.consumerd32_bin_path
.value
&&
3204 ustconsumer32_data
.pid
== 0 &&
3205 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3206 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3207 ret
= start_consumerd(&ustconsumer32_data
);
3209 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3210 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3214 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3215 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3217 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3221 * Setup socket for consumer 64 bit. No need for atomic access
3222 * since it was set above and can ONLY be set in this thread.
3224 ret
= consumer_create_socket(&ustconsumer32_data
,
3225 cmd_ctx
->session
->ust_session
->consumer
);
3237 /* Validate consumer daemon state when start/stop trace command */
3238 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3239 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3240 switch (cmd_ctx
->lsm
->domain
.type
) {
3241 case LTTNG_DOMAIN_NONE
:
3243 case LTTNG_DOMAIN_JUL
:
3244 case LTTNG_DOMAIN_LOG4J
:
3245 case LTTNG_DOMAIN_PYTHON
:
3246 case LTTNG_DOMAIN_UST
:
3247 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3248 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3252 case LTTNG_DOMAIN_KERNEL
:
3253 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3254 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3259 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3265 * Check that the UID or GID match that of the tracing session.
3266 * The root user can interact with all sessions.
3268 if (need_tracing_session
) {
3269 if (!session_access_ok(cmd_ctx
->session
,
3270 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3271 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3272 ret
= LTTNG_ERR_EPERM
;
3278 * Send relayd information to consumer as soon as we have a domain and a
3281 if (cmd_ctx
->session
&& need_domain
) {
3283 * Setup relayd if not done yet. If the relayd information was already
3284 * sent to the consumer, this call will gracefully return.
3286 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3287 if (ret
!= LTTNG_OK
) {
3292 /* Process by command type */
3293 switch (cmd_ctx
->lsm
->cmd_type
) {
3294 case LTTNG_ADD_CONTEXT
:
3297 * An LTTNG_ADD_CONTEXT command might have a supplementary
3298 * payload if the context being added is an application context.
3300 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3301 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3302 char *provider_name
= NULL
, *context_name
= NULL
;
3303 size_t provider_name_len
=
3304 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3305 size_t context_name_len
=
3306 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3308 if (provider_name_len
== 0 || context_name_len
== 0) {
3310 * Application provider and context names MUST
3313 ret
= -LTTNG_ERR_INVALID
;
3317 provider_name
= zmalloc(provider_name_len
+ 1);
3318 if (!provider_name
) {
3319 ret
= -LTTNG_ERR_NOMEM
;
3322 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3325 context_name
= zmalloc(context_name_len
+ 1);
3326 if (!context_name
) {
3327 ret
= -LTTNG_ERR_NOMEM
;
3328 goto error_add_context
;
3330 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3333 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3336 goto error_add_context
;
3339 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3342 goto error_add_context
;
3347 * cmd_add_context assumes ownership of the provider and context
3350 ret
= cmd_add_context(cmd_ctx
->session
,
3351 cmd_ctx
->lsm
->domain
.type
,
3352 cmd_ctx
->lsm
->u
.context
.channel_name
,
3353 &cmd_ctx
->lsm
->u
.context
.ctx
,
3354 kernel_poll_pipe
[1]);
3356 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3357 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3359 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3360 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3366 case LTTNG_DISABLE_CHANNEL
:
3368 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3369 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3372 case LTTNG_DISABLE_EVENT
:
3376 * FIXME: handle filter; for now we just receive the filter's
3377 * bytecode along with the filter expression which are sent by
3378 * liblttng-ctl and discard them.
3380 * This fixes an issue where the client may block while sending
3381 * the filter payload and encounter an error because the session
3382 * daemon closes the socket without ever handling this data.
3384 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3385 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3388 char data
[LTTNG_FILTER_MAX_LEN
];
3390 DBG("Discarding disable event command payload of size %zu", count
);
3392 ret
= lttcomm_recv_unix_sock(sock
, data
,
3393 count
> sizeof(data
) ? sizeof(data
) : count
);
3398 count
-= (size_t) ret
;
3401 /* FIXME: passing packed structure to non-packed pointer */
3402 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3403 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3404 &cmd_ctx
->lsm
->u
.disable
.event
);
3407 case LTTNG_ENABLE_CHANNEL
:
3409 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3410 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3411 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3412 &cmd_ctx
->lsm
->u
.channel
.chan
,
3413 kernel_poll_pipe
[1]);
3416 case LTTNG_TRACK_PID
:
3418 ret
= cmd_track_pid(cmd_ctx
->session
,
3419 cmd_ctx
->lsm
->domain
.type
,
3420 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3423 case LTTNG_UNTRACK_PID
:
3425 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3426 cmd_ctx
->lsm
->domain
.type
,
3427 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3430 case LTTNG_ENABLE_EVENT
:
3432 struct lttng_event_exclusion
*exclusion
= NULL
;
3433 struct lttng_filter_bytecode
*bytecode
= NULL
;
3434 char *filter_expression
= NULL
;
3436 /* Handle exclusion events and receive it from the client. */
3437 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3438 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3440 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3441 (count
* LTTNG_SYMBOL_NAME_LEN
));
3443 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3447 DBG("Receiving var len exclusion event list from client ...");
3448 exclusion
->count
= count
;
3449 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3450 count
* LTTNG_SYMBOL_NAME_LEN
);
3452 DBG("Nothing recv() from client var len data... continuing");
3455 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3460 /* Get filter expression from client. */
3461 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3462 size_t expression_len
=
3463 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3465 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3466 ret
= LTTNG_ERR_FILTER_INVAL
;
3471 filter_expression
= zmalloc(expression_len
);
3472 if (!filter_expression
) {
3474 ret
= LTTNG_ERR_FILTER_NOMEM
;
3478 /* Receive var. len. data */
3479 DBG("Receiving var len filter's expression from client ...");
3480 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3483 DBG("Nothing recv() from client car len data... continuing");
3485 free(filter_expression
);
3487 ret
= LTTNG_ERR_FILTER_INVAL
;
3492 /* Handle filter and get bytecode from client. */
3493 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3494 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3496 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3497 ret
= LTTNG_ERR_FILTER_INVAL
;
3498 free(filter_expression
);
3503 bytecode
= zmalloc(bytecode_len
);
3505 free(filter_expression
);
3507 ret
= LTTNG_ERR_FILTER_NOMEM
;
3511 /* Receive var. len. data */
3512 DBG("Receiving var len filter's bytecode from client ...");
3513 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3515 DBG("Nothing recv() from client car len data... continuing");
3517 free(filter_expression
);
3520 ret
= LTTNG_ERR_FILTER_INVAL
;
3524 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3525 free(filter_expression
);
3528 ret
= LTTNG_ERR_FILTER_INVAL
;
3533 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3534 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3535 &cmd_ctx
->lsm
->u
.enable
.event
,
3536 filter_expression
, bytecode
, exclusion
,
3537 kernel_poll_pipe
[1]);
3540 case LTTNG_LIST_TRACEPOINTS
:
3542 struct lttng_event
*events
;
3545 session_lock_list();
3546 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3547 session_unlock_list();
3548 if (nb_events
< 0) {
3549 /* Return value is a negative lttng_error_code. */
3555 * Setup lttng message with payload size set to the event list size in
3556 * bytes and then copy list into the llm payload.
3558 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3559 sizeof(struct lttng_event
) * nb_events
);
3569 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3571 struct lttng_event_field
*fields
;
3574 session_lock_list();
3575 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3577 session_unlock_list();
3578 if (nb_fields
< 0) {
3579 /* Return value is a negative lttng_error_code. */
3585 * Setup lttng message with payload size set to the event list size in
3586 * bytes and then copy list into the llm payload.
3588 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3589 sizeof(struct lttng_event_field
) * nb_fields
);
3599 case LTTNG_LIST_SYSCALLS
:
3601 struct lttng_event
*events
;
3604 nb_events
= cmd_list_syscalls(&events
);
3605 if (nb_events
< 0) {
3606 /* Return value is a negative lttng_error_code. */
3612 * Setup lttng message with payload size set to the event list size in
3613 * bytes and then copy list into the llm payload.
3615 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3616 sizeof(struct lttng_event
) * nb_events
);
3626 case LTTNG_LIST_TRACKER_PIDS
:
3628 int32_t *pids
= NULL
;
3631 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3632 cmd_ctx
->lsm
->domain
.type
, &pids
);
3634 /* Return value is a negative lttng_error_code. */
3640 * Setup lttng message with payload size set to the event list size in
3641 * bytes and then copy list into the llm payload.
3643 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3644 sizeof(int32_t) * nr_pids
);
3654 case LTTNG_SET_CONSUMER_URI
:
3657 struct lttng_uri
*uris
;
3659 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3660 len
= nb_uri
* sizeof(struct lttng_uri
);
3663 ret
= LTTNG_ERR_INVALID
;
3667 uris
= zmalloc(len
);
3669 ret
= LTTNG_ERR_FATAL
;
3673 /* Receive variable len data */
3674 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3675 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3677 DBG("No URIs received from client... continuing");
3679 ret
= LTTNG_ERR_SESSION_FAIL
;
3684 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3686 if (ret
!= LTTNG_OK
) {
3693 case LTTNG_START_TRACE
:
3696 * On the first start, if we have a kernel session and we have
3697 * enabled time or size-based rotations, we have to make sure
3698 * the kernel tracer supports it.
3700 if (!cmd_ctx
->session
->has_been_started
&& \
3701 cmd_ctx
->session
->kernel_session
&& \
3702 (cmd_ctx
->session
->rotate_timer_period
|| \
3703 cmd_ctx
->session
->rotate_size
) && \
3704 !check_rotate_compatible()) {
3705 DBG("Kernel tracer version is not compatible with the rotation feature");
3706 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3709 ret
= cmd_start_trace(cmd_ctx
->session
);
3712 case LTTNG_STOP_TRACE
:
3714 ret
= cmd_stop_trace(cmd_ctx
->session
);
3717 case LTTNG_CREATE_SESSION
:
3720 struct lttng_uri
*uris
= NULL
;
3722 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3723 len
= nb_uri
* sizeof(struct lttng_uri
);
3726 uris
= zmalloc(len
);
3728 ret
= LTTNG_ERR_FATAL
;
3732 /* Receive variable len data */
3733 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3734 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3736 DBG("No URIs received from client... continuing");
3738 ret
= LTTNG_ERR_SESSION_FAIL
;
3743 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3744 DBG("Creating session with ONE network URI is a bad call");
3745 ret
= LTTNG_ERR_SESSION_FAIL
;
3751 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3752 &cmd_ctx
->creds
, 0);
3758 case LTTNG_DESTROY_SESSION
:
3760 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3761 notification_thread_handle
);
3763 /* Set session to NULL so we do not unlock it after free. */
3764 cmd_ctx
->session
= NULL
;
3767 case LTTNG_LIST_DOMAINS
:
3770 struct lttng_domain
*domains
= NULL
;
3772 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3774 /* Return value is a negative lttng_error_code. */
3779 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3780 nb_dom
* sizeof(struct lttng_domain
));
3790 case LTTNG_LIST_CHANNELS
:
3792 ssize_t payload_size
;
3793 struct lttng_channel
*channels
= NULL
;
3795 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3796 cmd_ctx
->session
, &channels
);
3797 if (payload_size
< 0) {
3798 /* Return value is a negative lttng_error_code. */
3799 ret
= -payload_size
;
3803 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3814 case LTTNG_LIST_EVENTS
:
3817 struct lttng_event
*events
= NULL
;
3818 struct lttcomm_event_command_header cmd_header
;
3821 memset(&cmd_header
, 0, sizeof(cmd_header
));
3822 /* Extended infos are included at the end of events */
3823 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3824 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3825 &events
, &total_size
);
3828 /* Return value is a negative lttng_error_code. */
3833 cmd_header
.nb_events
= nb_event
;
3834 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3835 &cmd_header
, sizeof(cmd_header
));
3845 case LTTNG_LIST_SESSIONS
:
3847 unsigned int nr_sessions
;
3848 void *sessions_payload
;
3851 session_lock_list();
3852 nr_sessions
= lttng_sessions_count(
3853 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3854 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3855 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3856 sessions_payload
= zmalloc(payload_len
);
3858 if (!sessions_payload
) {
3859 session_unlock_list();
3864 cmd_list_lttng_sessions(sessions_payload
,
3865 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3866 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3867 session_unlock_list();
3869 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3871 free(sessions_payload
);
3880 case LTTNG_REGISTER_CONSUMER
:
3882 struct consumer_data
*cdata
;
3884 switch (cmd_ctx
->lsm
->domain
.type
) {
3885 case LTTNG_DOMAIN_KERNEL
:
3886 cdata
= &kconsumer_data
;
3889 ret
= LTTNG_ERR_UND
;
3893 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3894 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3897 case LTTNG_DATA_PENDING
:
3900 uint8_t pending_ret_byte
;
3902 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3907 * This function may returns 0 or 1 to indicate whether or not
3908 * there is data pending. In case of error, it should return an
3909 * LTTNG_ERR code. However, some code paths may still return
3910 * a nondescript error code, which we handle by returning an
3913 if (pending_ret
== 0 || pending_ret
== 1) {
3915 * ret will be set to LTTNG_OK at the end of
3918 } else if (pending_ret
< 0) {
3919 ret
= LTTNG_ERR_UNK
;
3926 pending_ret_byte
= (uint8_t) pending_ret
;
3928 /* 1 byte to return whether or not data is pending */
3929 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3930 &pending_ret_byte
, 1);
3939 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3941 struct lttcomm_lttng_output_id reply
;
3943 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3944 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3945 if (ret
!= LTTNG_OK
) {
3949 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3955 /* Copy output list into message payload */
3959 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3961 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3962 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3965 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3968 struct lttng_snapshot_output
*outputs
= NULL
;
3970 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3971 if (nb_output
< 0) {
3976 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3977 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3978 nb_output
* sizeof(struct lttng_snapshot_output
));
3988 case LTTNG_SNAPSHOT_RECORD
:
3990 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3991 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3992 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3995 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3998 struct lttng_uri
*uris
= NULL
;
4000 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4001 len
= nb_uri
* sizeof(struct lttng_uri
);
4004 uris
= zmalloc(len
);
4006 ret
= LTTNG_ERR_FATAL
;
4010 /* Receive variable len data */
4011 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4012 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4014 DBG("No URIs received from client... continuing");
4016 ret
= LTTNG_ERR_SESSION_FAIL
;
4021 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4022 DBG("Creating session with ONE network URI is a bad call");
4023 ret
= LTTNG_ERR_SESSION_FAIL
;
4029 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4030 nb_uri
, &cmd_ctx
->creds
);
4034 case LTTNG_CREATE_SESSION_LIVE
:
4037 struct lttng_uri
*uris
= NULL
;
4039 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4040 len
= nb_uri
* sizeof(struct lttng_uri
);
4043 uris
= zmalloc(len
);
4045 ret
= LTTNG_ERR_FATAL
;
4049 /* Receive variable len data */
4050 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4051 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4053 DBG("No URIs received from client... continuing");
4055 ret
= LTTNG_ERR_SESSION_FAIL
;
4060 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4061 DBG("Creating session with ONE network URI is a bad call");
4062 ret
= LTTNG_ERR_SESSION_FAIL
;
4068 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4069 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4073 case LTTNG_SAVE_SESSION
:
4075 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4079 case LTTNG_SET_SESSION_SHM_PATH
:
4081 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4082 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4085 case LTTNG_REGENERATE_METADATA
:
4087 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4090 case LTTNG_REGENERATE_STATEDUMP
:
4092 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4095 case LTTNG_REGISTER_TRIGGER
:
4097 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4098 notification_thread_handle
);
4101 case LTTNG_UNREGISTER_TRIGGER
:
4103 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4104 notification_thread_handle
);
4107 case LTTNG_ROTATE_SESSION
:
4109 struct lttng_rotate_session_return rotate_return
;
4111 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4113 memset(&rotate_return
, 0, sizeof(rotate_return
));
4114 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4115 DBG("Kernel tracer version is not compatible with the rotation feature");
4116 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4120 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4126 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4127 sizeof(rotate_return
));
4136 case LTTNG_ROTATION_GET_INFO
:
4138 struct lttng_rotation_get_info_return get_info_return
;
4140 memset(&get_info_return
, 0, sizeof(get_info_return
));
4141 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4142 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4148 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4149 sizeof(get_info_return
));
4158 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4160 struct lttng_session_get_current_output_return output_return
;
4162 memset(&output_return
, 0, sizeof(output_return
));
4163 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4170 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4171 sizeof(output_return
));
4180 case LTTNG_ROTATION_SET_SCHEDULE
:
4182 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4183 DBG("Kernel tracer version does not support session rotations");
4184 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4188 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4189 cmd_ctx
->lsm
->u
.rotate_setup
.timer_us
,
4190 cmd_ctx
->lsm
->u
.rotate_setup
.size
,
4191 notification_thread_handle
);
4192 if (ret
!= LTTNG_OK
) {
4198 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
4200 struct lttng_rotation_schedule_get_timer_period
*get_timer
;
4202 get_timer
= zmalloc(sizeof(struct lttng_rotation_schedule_get_timer_period
));
4207 get_timer
->rotate_timer
= cmd_ctx
->session
->rotate_timer_period
;
4209 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_timer
,
4210 sizeof(struct lttng_rotation_schedule_get_timer_period
));
4220 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
4222 struct lttng_rotation_schedule_get_size
*get_size
;
4224 get_size
= zmalloc(sizeof(struct lttng_rotation_schedule_get_size
));
4229 get_size
->rotate_size
= cmd_ctx
->session
->rotate_size
;
4231 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_size
,
4232 sizeof(struct lttng_rotation_schedule_get_size
));
4243 ret
= LTTNG_ERR_UND
;
4248 if (cmd_ctx
->llm
== NULL
) {
4249 DBG("Missing llm structure. Allocating one.");
4250 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4254 /* Set return code */
4255 cmd_ctx
->llm
->ret_code
= ret
;
4257 if (cmd_ctx
->session
) {
4258 session_unlock(cmd_ctx
->session
);
4260 if (need_tracing_session
) {
4261 session_unlock_list();
4264 assert(!rcu_read_ongoing());
4269 * Thread managing health check socket.
4271 static void *thread_manage_health(void *data
)
4273 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4274 uint32_t revents
, nb_fd
;
4275 struct lttng_poll_event events
;
4276 struct health_comm_msg msg
;
4277 struct health_comm_reply reply
;
4279 DBG("[thread] Manage health check started");
4281 rcu_register_thread();
4283 /* We might hit an error path before this is created. */
4284 lttng_poll_init(&events
);
4286 /* Create unix socket */
4287 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4289 ERR("Unable to create health check Unix socket");
4294 /* lttng health client socket path permissions */
4295 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4296 utils_get_group_id(config
.tracing_group_name
.value
));
4298 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4303 ret
= chmod(config
.health_unix_sock_path
.value
,
4304 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4306 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4313 * Set the CLOEXEC flag. Return code is useless because either way, the
4316 (void) utils_set_fd_cloexec(sock
);
4318 ret
= lttcomm_listen_unix_sock(sock
);
4324 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4325 * more will be added to this poll set.
4327 ret
= sessiond_set_thread_pollset(&events
, 2);
4332 /* Add the application registration socket */
4333 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4338 sessiond_notify_ready();
4341 DBG("Health check ready");
4343 /* Inifinite blocking call, waiting for transmission */
4345 ret
= lttng_poll_wait(&events
, -1);
4348 * Restart interrupted system call.
4350 if (errno
== EINTR
) {
4358 for (i
= 0; i
< nb_fd
; i
++) {
4359 /* Fetch once the poll data */
4360 revents
= LTTNG_POLL_GETEV(&events
, i
);
4361 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4364 /* No activity for this FD (poll implementation). */
4368 /* Thread quit pipe has been closed. Killing thread. */
4369 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4375 /* Event on the registration socket */
4376 if (pollfd
== sock
) {
4377 if (revents
& LPOLLIN
) {
4379 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4380 ERR("Health socket poll error");
4383 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4389 new_sock
= lttcomm_accept_unix_sock(sock
);
4395 * Set the CLOEXEC flag. Return code is useless because either way, the
4398 (void) utils_set_fd_cloexec(new_sock
);
4400 DBG("Receiving data from client for health...");
4401 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4403 DBG("Nothing recv() from client... continuing");
4404 ret
= close(new_sock
);
4411 rcu_thread_online();
4413 memset(&reply
, 0, sizeof(reply
));
4414 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4416 * health_check_state returns 0 if health is
4419 if (!health_check_state(health_sessiond
, i
)) {
4420 reply
.ret_code
|= 1ULL << i
;
4424 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4426 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4428 ERR("Failed to send health data back to client");
4431 /* End of transmission */
4432 ret
= close(new_sock
);
4441 ERR("Health error occurred in %s", __func__
);
4443 DBG("Health check thread dying");
4444 unlink(config
.health_unix_sock_path
.value
);
4452 lttng_poll_clean(&events
);
4454 rcu_unregister_thread();
4459 * This thread manage all clients request using the unix client socket for
4462 static void *thread_manage_clients(void *data
)
4464 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4466 uint32_t revents
, nb_fd
;
4467 struct command_ctx
*cmd_ctx
= NULL
;
4468 struct lttng_poll_event events
;
4470 DBG("[thread] Manage client started");
4472 rcu_register_thread();
4474 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4476 health_code_update();
4478 ret
= lttcomm_listen_unix_sock(client_sock
);
4484 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4485 * more will be added to this poll set.
4487 ret
= sessiond_set_thread_pollset(&events
, 2);
4489 goto error_create_poll
;
4492 /* Add the application registration socket */
4493 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4498 sessiond_notify_ready();
4500 ret
= sem_post(&load_info
->message_thread_ready
);
4502 PERROR("sem_post message_thread_ready");
4507 * Wait until all support threads are initialized before accepting
4510 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4512 struct timeval timeout
;
4515 FD_SET(thread_quit_pipe
[0], &read_fds
);
4516 memset(&timeout
, 0, sizeof(timeout
));
4517 timeout
.tv_usec
= 1000;
4520 * If a support thread failed to launch, it may signal that
4521 * we must exit and the sessiond would never be marked as
4524 * The timeout is set to 1ms, which serves as a way to
4525 * pace down this check.
4527 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4529 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4534 /* This testpoint is after we signal readiness to the parent. */
4535 if (testpoint(sessiond_thread_manage_clients
)) {
4539 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4543 health_code_update();
4546 DBG("Accepting client command ...");
4548 /* Inifinite blocking call, waiting for transmission */
4550 health_poll_entry();
4551 ret
= lttng_poll_wait(&events
, -1);
4555 * Restart interrupted system call.
4557 if (errno
== EINTR
) {
4565 for (i
= 0; i
< nb_fd
; i
++) {
4566 /* Fetch once the poll data */
4567 revents
= LTTNG_POLL_GETEV(&events
, i
);
4568 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4570 health_code_update();
4573 /* No activity for this FD (poll implementation). */
4577 /* Thread quit pipe has been closed. Killing thread. */
4578 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4584 /* Event on the registration socket */
4585 if (pollfd
== client_sock
) {
4586 if (revents
& LPOLLIN
) {
4588 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4589 ERR("Client socket poll error");
4592 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4598 DBG("Wait for client response");
4600 health_code_update();
4602 sock
= lttcomm_accept_unix_sock(client_sock
);
4608 * Set the CLOEXEC flag. Return code is useless because either way, the
4611 (void) utils_set_fd_cloexec(sock
);
4613 /* Set socket option for credentials retrieval */
4614 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4619 /* Allocate context command to process the client request */
4620 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4621 if (cmd_ctx
== NULL
) {
4622 PERROR("zmalloc cmd_ctx");
4626 /* Allocate data buffer for reception */
4627 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4628 if (cmd_ctx
->lsm
== NULL
) {
4629 PERROR("zmalloc cmd_ctx->lsm");
4633 cmd_ctx
->llm
= NULL
;
4634 cmd_ctx
->session
= NULL
;
4636 health_code_update();
4639 * Data is received from the lttng client. The struct
4640 * lttcomm_session_msg (lsm) contains the command and data request of
4643 DBG("Receiving data from client ...");
4644 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4645 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4647 DBG("Nothing recv() from client... continuing");
4653 clean_command_ctx(&cmd_ctx
);
4657 health_code_update();
4659 // TODO: Validate cmd_ctx including sanity check for
4660 // security purpose.
4662 rcu_thread_online();
4664 * This function dispatch the work to the kernel or userspace tracer
4665 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4666 * informations for the client. The command context struct contains
4667 * everything this function may needs.
4669 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4670 rcu_thread_offline();
4678 * TODO: Inform client somehow of the fatal error. At
4679 * this point, ret < 0 means that a zmalloc failed
4680 * (ENOMEM). Error detected but still accept
4681 * command, unless a socket error has been
4684 clean_command_ctx(&cmd_ctx
);
4688 health_code_update();
4690 DBG("Sending response (size: %d, retcode: %s (%d))",
4691 cmd_ctx
->lttng_msg_size
,
4692 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4693 cmd_ctx
->llm
->ret_code
);
4694 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4696 ERR("Failed to send data back to client");
4699 /* End of transmission */
4706 clean_command_ctx(&cmd_ctx
);
4708 health_code_update();
4720 lttng_poll_clean(&events
);
4721 clean_command_ctx(&cmd_ctx
);
4725 unlink(config
.client_unix_sock_path
.value
);
4726 if (client_sock
>= 0) {
4727 ret
= close(client_sock
);
4735 ERR("Health error occurred in %s", __func__
);
4738 health_unregister(health_sessiond
);
4740 DBG("Client thread dying");
4742 rcu_unregister_thread();
4745 * Since we are creating the consumer threads, we own them, so we need
4746 * to join them before our thread exits.
4748 ret
= join_consumer_thread(&kconsumer_data
);
4751 PERROR("join_consumer");
4754 ret
= join_consumer_thread(&ustconsumer32_data
);
4757 PERROR("join_consumer ust32");
4760 ret
= join_consumer_thread(&ustconsumer64_data
);
4763 PERROR("join_consumer ust64");
4768 static int string_match(const char *str1
, const char *str2
)
4770 return (str1
&& str2
) && !strcmp(str1
, str2
);
4774 * Take an option from the getopt output and set it in the right variable to be
4777 * Return 0 on success else a negative value.
4779 static int set_option(int opt
, const char *arg
, const char *optname
)
4783 if (string_match(optname
, "client-sock") || opt
== 'c') {
4784 if (!arg
|| *arg
== '\0') {
4788 if (lttng_is_setuid_setgid()) {
4789 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4790 "-c, --client-sock");
4792 config_string_set(&config
.client_unix_sock_path
,
4794 if (!config
.client_unix_sock_path
.value
) {
4799 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4800 if (!arg
|| *arg
== '\0') {
4804 if (lttng_is_setuid_setgid()) {
4805 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4808 config_string_set(&config
.apps_unix_sock_path
,
4810 if (!config
.apps_unix_sock_path
.value
) {
4815 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4816 config
.daemonize
= true;
4817 } else if (string_match(optname
, "background") || opt
== 'b') {
4818 config
.background
= true;
4819 } else if (string_match(optname
, "group") || opt
== 'g') {
4820 if (!arg
|| *arg
== '\0') {
4824 if (lttng_is_setuid_setgid()) {
4825 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4828 config_string_set(&config
.tracing_group_name
,
4830 if (!config
.tracing_group_name
.value
) {
4835 } else if (string_match(optname
, "help") || opt
== 'h') {
4836 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4838 ERR("Cannot show --help for `lttng-sessiond`");
4841 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4842 } else if (string_match(optname
, "version") || opt
== 'V') {
4843 fprintf(stdout
, "%s\n", VERSION
);
4845 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4846 config
.sig_parent
= true;
4847 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4848 if (!arg
|| *arg
== '\0') {
4852 if (lttng_is_setuid_setgid()) {
4853 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4854 "--kconsumerd-err-sock");
4856 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4858 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4863 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4864 if (!arg
|| *arg
== '\0') {
4868 if (lttng_is_setuid_setgid()) {
4869 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4870 "--kconsumerd-cmd-sock");
4872 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4874 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4879 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4880 if (!arg
|| *arg
== '\0') {
4884 if (lttng_is_setuid_setgid()) {
4885 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4886 "--ustconsumerd64-err-sock");
4888 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4890 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4895 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4896 if (!arg
|| *arg
== '\0') {
4900 if (lttng_is_setuid_setgid()) {
4901 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4902 "--ustconsumerd64-cmd-sock");
4904 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4906 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4911 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4912 if (!arg
|| *arg
== '\0') {
4916 if (lttng_is_setuid_setgid()) {
4917 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4918 "--ustconsumerd32-err-sock");
4920 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4922 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4927 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4928 if (!arg
|| *arg
== '\0') {
4932 if (lttng_is_setuid_setgid()) {
4933 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4934 "--ustconsumerd32-cmd-sock");
4936 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4938 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4943 } else if (string_match(optname
, "no-kernel")) {
4944 config
.no_kernel
= true;
4945 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4946 config
.quiet
= true;
4947 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4948 /* Verbose level can increase using multiple -v */
4950 /* Value obtained from config file */
4951 config
.verbose
= config_parse_value(arg
);
4953 /* -v used on command line */
4956 /* Clamp value to [0, 3] */
4957 config
.verbose
= config
.verbose
< 0 ? 0 :
4958 (config
.verbose
<= 3 ? config
.verbose
: 3);
4959 } else if (string_match(optname
, "verbose-consumer")) {
4961 config
.verbose_consumer
= config_parse_value(arg
);
4963 config
.verbose_consumer
++;
4965 } else if (string_match(optname
, "consumerd32-path")) {
4966 if (!arg
|| *arg
== '\0') {
4970 if (lttng_is_setuid_setgid()) {
4971 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4972 "--consumerd32-path");
4974 config_string_set(&config
.consumerd32_bin_path
,
4976 if (!config
.consumerd32_bin_path
.value
) {
4981 } else if (string_match(optname
, "consumerd32-libdir")) {
4982 if (!arg
|| *arg
== '\0') {
4986 if (lttng_is_setuid_setgid()) {
4987 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4988 "--consumerd32-libdir");
4990 config_string_set(&config
.consumerd32_lib_dir
,
4992 if (!config
.consumerd32_lib_dir
.value
) {
4997 } else if (string_match(optname
, "consumerd64-path")) {
4998 if (!arg
|| *arg
== '\0') {
5002 if (lttng_is_setuid_setgid()) {
5003 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5004 "--consumerd64-path");
5006 config_string_set(&config
.consumerd64_bin_path
,
5008 if (!config
.consumerd64_bin_path
.value
) {
5013 } else if (string_match(optname
, "consumerd64-libdir")) {
5014 if (!arg
|| *arg
== '\0') {
5018 if (lttng_is_setuid_setgid()) {
5019 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5020 "--consumerd64-libdir");
5022 config_string_set(&config
.consumerd64_lib_dir
,
5024 if (!config
.consumerd64_lib_dir
.value
) {
5029 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5030 if (!arg
|| *arg
== '\0') {
5034 if (lttng_is_setuid_setgid()) {
5035 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5038 config_string_set(&config
.pid_file_path
, strdup(arg
));
5039 if (!config
.pid_file_path
.value
) {
5044 } else if (string_match(optname
, "agent-tcp-port")) {
5045 if (!arg
|| *arg
== '\0') {
5049 if (lttng_is_setuid_setgid()) {
5050 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5051 "--agent-tcp-port");
5056 v
= strtoul(arg
, NULL
, 0);
5057 if (errno
!= 0 || !isdigit(arg
[0])) {
5058 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5061 if (v
== 0 || v
>= 65535) {
5062 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5065 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5066 DBG3("Agent TCP port set to non default: %i", (int) v
);
5068 } else if (string_match(optname
, "load") || opt
== 'l') {
5069 if (!arg
|| *arg
== '\0') {
5073 if (lttng_is_setuid_setgid()) {
5074 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5077 config_string_set(&config
.load_session_path
, strdup(arg
));
5078 if (!config
.load_session_path
.value
) {
5083 } else if (string_match(optname
, "kmod-probes")) {
5084 if (!arg
|| *arg
== '\0') {
5088 if (lttng_is_setuid_setgid()) {
5089 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5092 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5093 if (!config
.kmod_probes_list
.value
) {
5098 } else if (string_match(optname
, "extra-kmod-probes")) {
5099 if (!arg
|| *arg
== '\0') {
5103 if (lttng_is_setuid_setgid()) {
5104 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5105 "--extra-kmod-probes");
5107 config_string_set(&config
.kmod_extra_probes_list
,
5109 if (!config
.kmod_extra_probes_list
.value
) {
5114 } else if (string_match(optname
, "config") || opt
== 'f') {
5115 /* This is handled in set_options() thus silent skip. */
5118 /* Unknown option or other error.
5119 * Error is printed by getopt, just return */
5124 if (ret
== -EINVAL
) {
5125 const char *opt_name
= "unknown";
5128 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5130 if (opt
== long_options
[i
].val
) {
5131 opt_name
= long_options
[i
].name
;
5136 WARN("Invalid argument provided for option \"%s\", using default value.",
5144 * config_entry_handler_cb used to handle options read from a config file.
5145 * See config_entry_handler_cb comment in common/config/session-config.h for the
5146 * return value conventions.
5148 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5152 if (!entry
|| !entry
->name
|| !entry
->value
) {
5157 /* Check if the option is to be ignored */
5158 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5159 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5164 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5167 /* Ignore if not fully matched. */
5168 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5173 * If the option takes no argument on the command line, we have to
5174 * check if the value is "true". We support non-zero numeric values,
5177 if (!long_options
[i
].has_arg
) {
5178 ret
= config_parse_value(entry
->value
);
5181 WARN("Invalid configuration value \"%s\" for option %s",
5182 entry
->value
, entry
->name
);
5184 /* False, skip boolean config option. */
5189 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5193 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5200 * daemon configuration loading and argument parsing
5202 static int set_options(int argc
, char **argv
)
5204 int ret
= 0, c
= 0, option_index
= 0;
5205 int orig_optopt
= optopt
, orig_optind
= optind
;
5207 const char *config_path
= NULL
;
5209 optstring
= utils_generate_optstring(long_options
,
5210 sizeof(long_options
) / sizeof(struct option
));
5216 /* Check for the --config option */
5217 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5218 &option_index
)) != -1) {
5222 } else if (c
!= 'f') {
5223 /* if not equal to --config option. */
5227 if (lttng_is_setuid_setgid()) {
5228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5231 config_path
= utils_expand_path(optarg
);
5233 ERR("Failed to resolve path: %s", optarg
);
5238 ret
= config_get_section_entries(config_path
, config_section_name
,
5239 config_entry_handler
, NULL
);
5242 ERR("Invalid configuration option at line %i", ret
);
5248 /* Reset getopt's global state */
5249 optopt
= orig_optopt
;
5250 optind
= orig_optind
;
5254 * getopt_long() will not set option_index if it encounters a
5257 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5264 * Pass NULL as the long option name if popt left the index
5267 ret
= set_option(c
, optarg
,
5268 option_index
< 0 ? NULL
:
5269 long_options
[option_index
].name
);
5281 * Creates the two needed socket by the daemon.
5282 * apps_sock - The communication socket for all UST apps.
5283 * client_sock - The communication of the cli tool (lttng).
5285 static int init_daemon_socket(void)
5290 old_umask
= umask(0);
5292 /* Create client tool unix socket */
5293 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5294 if (client_sock
< 0) {
5295 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5300 /* Set the cloexec flag */
5301 ret
= utils_set_fd_cloexec(client_sock
);
5303 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5304 "Continuing but note that the consumer daemon will have a "
5305 "reference to this socket on exec()", client_sock
);
5308 /* File permission MUST be 660 */
5309 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5311 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5316 /* Create the application unix socket */
5317 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5318 if (apps_sock
< 0) {
5319 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5324 /* Set the cloexec flag */
5325 ret
= utils_set_fd_cloexec(apps_sock
);
5327 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5328 "Continuing but note that the consumer daemon will have a "
5329 "reference to this socket on exec()", apps_sock
);
5332 /* File permission MUST be 666 */
5333 ret
= chmod(config
.apps_unix_sock_path
.value
,
5334 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5336 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5341 DBG3("Session daemon client socket %d and application socket %d created",
5342 client_sock
, apps_sock
);
5350 * Create lockfile using the rundir and return its fd.
5352 static int create_lockfile(void)
5354 return utils_create_lock_file(config
.lock_file_path
.value
);
5358 * Check if the global socket is available, and if a daemon is answering at the
5359 * other side. If yes, error is returned.
5361 * Also attempts to create and hold the lock file.
5363 static int check_existing_daemon(void)
5367 /* Is there anybody out there ? */
5368 if (lttng_session_daemon_alive()) {
5373 lockfile_fd
= create_lockfile();
5374 if (lockfile_fd
< 0) {
5382 static void sessiond_cleanup_lock_file(void)
5387 * Cleanup lock file by deleting it and finaly closing it which will
5388 * release the file system lock.
5390 if (lockfile_fd
>= 0) {
5391 ret
= remove(config
.lock_file_path
.value
);
5393 PERROR("remove lock file");
5395 ret
= close(lockfile_fd
);
5397 PERROR("close lock file");
5403 * Set the tracing group gid onto the client socket.
5405 * Race window between mkdir and chown is OK because we are going from more
5406 * permissive (root.root) to less permissive (root.tracing).
5408 static int set_permissions(char *rundir
)
5413 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5415 /* Set lttng run dir */
5416 ret
= chown(rundir
, 0, gid
);
5418 ERR("Unable to set group on %s", rundir
);
5423 * Ensure all applications and tracing group can search the run
5424 * dir. Allow everyone to read the directory, since it does not
5425 * buy us anything to hide its content.
5427 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5429 ERR("Unable to set permissions on %s", rundir
);
5433 /* lttng client socket path */
5434 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5436 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5440 /* kconsumer error socket path */
5441 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5443 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5447 /* 64-bit ustconsumer error socket path */
5448 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5450 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5454 /* 32-bit ustconsumer compat32 error socket path */
5455 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5457 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5461 DBG("All permissions are set");
5467 * Create the lttng run directory needed for all global sockets and pipe.
5469 static int create_lttng_rundir(void)
5473 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5475 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5477 if (errno
!= EEXIST
) {
5478 ERR("Unable to create %s", config
.rundir
.value
);
5490 * Setup sockets and directory needed by the consumerds' communication with the
5493 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5498 switch (consumer_data
->type
) {
5499 case LTTNG_CONSUMER_KERNEL
:
5500 path
= config
.kconsumerd_path
.value
;
5502 case LTTNG_CONSUMER64_UST
:
5503 path
= config
.consumerd64_path
.value
;
5505 case LTTNG_CONSUMER32_UST
:
5506 path
= config
.consumerd32_path
.value
;
5509 ERR("Consumer type unknown");
5515 DBG2("Creating consumer directory: %s", path
);
5517 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5518 if (ret
< 0 && errno
!= EEXIST
) {
5520 ERR("Failed to create %s", path
);
5524 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5526 ERR("Unable to set group on %s", path
);
5532 /* Create the consumerd error unix socket */
5533 consumer_data
->err_sock
=
5534 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5535 if (consumer_data
->err_sock
< 0) {
5536 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5542 * Set the CLOEXEC flag. Return code is useless because either way, the
5545 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5547 PERROR("utils_set_fd_cloexec");
5548 /* continue anyway */
5551 /* File permission MUST be 660 */
5552 ret
= chmod(consumer_data
->err_unix_sock_path
,
5553 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5555 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5565 * Signal handler for the daemon
5567 * Simply stop all worker threads, leaving main() return gracefully after
5568 * joining all threads and calling cleanup().
5570 static void sighandler(int sig
)
5574 DBG("SIGINT caught");
5578 DBG("SIGTERM caught");
5582 CMM_STORE_SHARED(recv_child_signal
, 1);
5590 * Setup signal handler for :
5591 * SIGINT, SIGTERM, SIGPIPE
5593 static int set_signal_handler(void)
5596 struct sigaction sa
;
5599 if ((ret
= sigemptyset(&sigset
)) < 0) {
5600 PERROR("sigemptyset");
5604 sa
.sa_mask
= sigset
;
5607 sa
.sa_handler
= sighandler
;
5608 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5609 PERROR("sigaction");
5613 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5614 PERROR("sigaction");
5618 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5619 PERROR("sigaction");
5623 sa
.sa_handler
= SIG_IGN
;
5624 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5625 PERROR("sigaction");
5629 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5635 * Set open files limit to unlimited. This daemon can open a large number of
5636 * file descriptors in order to consume multiple kernel traces.
5638 static void set_ulimit(void)
5643 /* The kernel does not allow an infinite limit for open files */
5644 lim
.rlim_cur
= 65535;
5645 lim
.rlim_max
= 65535;
5647 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5649 PERROR("failed to set open files limit");
5653 static int write_pidfile(void)
5655 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5658 static int set_clock_plugin_env(void)
5661 char *env_value
= NULL
;
5663 if (!config
.lttng_ust_clock_plugin
.value
) {
5667 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5668 config
.lttng_ust_clock_plugin
.value
);
5674 ret
= putenv(env_value
);
5677 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5681 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5682 config
.lttng_ust_clock_plugin
.value
);
5688 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5690 struct rotation_thread_timer_queue
*queue
= NULL
;
5692 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5694 PERROR("Failed to allocate timer rotate queue");
5698 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5699 CDS_INIT_LIST_HEAD(&queue
->list
);
5700 pthread_mutex_init(&queue
->lock
, NULL
);
5707 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5709 struct sessiond_rotation_timer
*node
, *tmp_node
;
5715 lttng_pipe_destroy(queue
->event_pipe
);
5717 pthread_mutex_lock(&queue
->lock
);
5718 /* Empty wait queue. */
5719 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5720 cds_list_del(&node
->head
);
5723 pthread_mutex_unlock(&queue
->lock
);
5725 pthread_mutex_destroy(&queue
->lock
);
5732 int main(int argc
, char **argv
)
5734 int ret
= 0, retval
= 0;
5736 const char *env_app_timeout
;
5737 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5738 *ust64_channel_monitor_pipe
= NULL
,
5739 *kernel_channel_monitor_pipe
= NULL
;
5740 bool notification_thread_launched
= false;
5741 bool rotation_thread_launched
= false;
5742 bool timer_thread_launched
= false;
5743 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5744 *ust64_channel_rotate_pipe
= NULL
,
5745 *kernel_channel_rotate_pipe
= NULL
;
5746 struct timer_thread_parameters timer_thread_ctx
;
5747 /* Queue of rotation jobs populated by the sessiond-timer. */
5748 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5749 sem_t notification_thread_ready
;
5751 init_kernel_workarounds();
5753 rcu_register_thread();
5755 if (set_signal_handler()) {
5757 goto exit_set_signal_handler
;
5760 if (sessiond_timer_signal_init()) {
5762 goto exit_set_signal_handler
;
5765 page_size
= sysconf(_SC_PAGESIZE
);
5766 if (page_size
< 0) {
5767 PERROR("sysconf _SC_PAGESIZE");
5768 page_size
= LONG_MAX
;
5769 WARN("Fallback page size to %ld", page_size
);
5772 ret
= sessiond_config_init(&config
);
5775 goto exit_set_signal_handler
;
5779 * Parse arguments and load the daemon configuration file.
5781 * We have an exit_options exit path to free memory reserved by
5782 * set_options. This is needed because the rest of sessiond_cleanup()
5783 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5784 * depends on set_options.
5787 if (set_options(argc
, argv
)) {
5792 /* Init config from environment variables. */
5793 sessiond_config_apply_env_config(&config
);
5796 * Resolve all paths received as arguments, configuration option, or
5797 * through environment variable as absolute paths. This is necessary
5798 * since daemonizing causes the sessiond's current working directory
5801 ret
= sessiond_config_resolve_paths(&config
);
5807 lttng_opt_verbose
= config
.verbose
;
5808 lttng_opt_quiet
= config
.quiet
;
5809 kconsumer_data
.err_unix_sock_path
=
5810 config
.kconsumerd_err_unix_sock_path
.value
;
5811 kconsumer_data
.cmd_unix_sock_path
=
5812 config
.kconsumerd_cmd_unix_sock_path
.value
;
5813 ustconsumer32_data
.err_unix_sock_path
=
5814 config
.consumerd32_err_unix_sock_path
.value
;
5815 ustconsumer32_data
.cmd_unix_sock_path
=
5816 config
.consumerd32_cmd_unix_sock_path
.value
;
5817 ustconsumer64_data
.err_unix_sock_path
=
5818 config
.consumerd64_err_unix_sock_path
.value
;
5819 ustconsumer64_data
.cmd_unix_sock_path
=
5820 config
.consumerd64_cmd_unix_sock_path
.value
;
5821 set_clock_plugin_env();
5823 sessiond_config_log(&config
);
5825 if (create_lttng_rundir()) {
5830 /* Abort launch if a session daemon is already running. */
5831 if (check_existing_daemon()) {
5832 ERR("A session daemon is already running.");
5838 if (config
.daemonize
|| config
.background
) {
5841 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5842 !config
.background
);
5849 * We are in the child. Make sure all other file descriptors are
5850 * closed, in case we are called with more opened file
5851 * descriptors than the standard ones and the lock file.
5853 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5854 if (i
== lockfile_fd
) {
5861 if (run_as_create_worker(argv
[0]) < 0) {
5862 goto exit_create_run_as_worker_cleanup
;
5866 * Starting from here, we can create threads. This needs to be after
5867 * lttng_daemonize due to RCU.
5871 * Initialize the health check subsystem. This call should set the
5872 * appropriate time values.
5874 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5875 if (!health_sessiond
) {
5876 PERROR("health_app_create error");
5878 goto exit_health_sessiond_cleanup
;
5881 /* Create thread to clean up RCU hash tables */
5882 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5884 goto exit_ht_cleanup
;
5887 /* Create thread quit pipe */
5888 if (init_thread_quit_pipe()) {
5890 goto exit_init_data
;
5893 /* Check if daemon is UID = 0 */
5894 is_root
= !getuid();
5896 /* Create global run dir with root access */
5898 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5899 if (!kernel_channel_monitor_pipe
) {
5900 ERR("Failed to create kernel consumer channel monitor pipe");
5902 goto exit_init_data
;
5904 kconsumer_data
.channel_monitor_pipe
=
5905 lttng_pipe_release_writefd(
5906 kernel_channel_monitor_pipe
);
5907 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5909 goto exit_init_data
;
5911 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5912 if (!kernel_channel_rotate_pipe
) {
5913 ERR("Failed to create kernel consumer channel rotate pipe");
5915 goto exit_init_data
;
5917 kconsumer_data
.channel_rotate_pipe
=
5918 lttng_pipe_release_writefd(
5919 kernel_channel_rotate_pipe
);
5920 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5922 goto exit_init_data
;
5926 /* Set consumer initial state */
5927 kernel_consumerd_state
= CONSUMER_STOPPED
;
5928 ust_consumerd_state
= CONSUMER_STOPPED
;
5930 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5931 if (!ust32_channel_monitor_pipe
) {
5932 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5934 goto exit_init_data
;
5936 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5937 ust32_channel_monitor_pipe
);
5938 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5940 goto exit_init_data
;
5942 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
5943 if (!ust32_channel_rotate_pipe
) {
5944 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
5946 goto exit_init_data
;
5948 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5949 ust32_channel_rotate_pipe
);
5950 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
5952 goto exit_init_data
;
5956 * The rotation_timer_queue structure is shared between the sessiond timer
5957 * thread and the rotation thread. The main() keeps the ownership and
5958 * destroys it when both threads have quit.
5960 rotation_timer_queue
= create_rotate_timer_queue();
5961 if (!rotation_timer_queue
) {
5963 goto exit_init_data
;
5965 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
5967 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5968 if (!ust64_channel_monitor_pipe
) {
5969 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5971 goto exit_init_data
;
5973 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5974 ust64_channel_monitor_pipe
);
5975 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5977 goto exit_init_data
;
5979 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
5980 if (!ust64_channel_rotate_pipe
) {
5981 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
5983 goto exit_init_data
;
5985 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5986 ust64_channel_rotate_pipe
);
5987 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
5989 goto exit_init_data
;
5993 * Init UST app hash table. Alloc hash table before this point since
5994 * cleanup() can get called after that point.
5996 if (ust_app_ht_alloc()) {
5997 ERR("Failed to allocate UST app hash table");
5999 goto exit_init_data
;
6003 * Initialize agent app hash table. We allocate the hash table here
6004 * since cleanup() can get called after this point.
6006 if (agent_app_ht_alloc()) {
6007 ERR("Failed to allocate Agent app hash table");
6009 goto exit_init_data
;
6013 * These actions must be executed as root. We do that *after* setting up
6014 * the sockets path because we MUST make the check for another daemon using
6015 * those paths *before* trying to set the kernel consumer sockets and init
6019 if (set_consumer_sockets(&kconsumer_data
)) {
6021 goto exit_init_data
;
6024 /* Setup kernel tracer */
6025 if (!config
.no_kernel
) {
6026 init_kernel_tracer();
6027 if (kernel_tracer_fd
>= 0) {
6028 ret
= syscall_init_table();
6030 ERR("Unable to populate syscall table. "
6031 "Syscall tracing won't work "
6032 "for this session daemon.");
6037 /* Set ulimit for open files */
6040 /* init lttng_fd tracking must be done after set_ulimit. */
6043 if (set_consumer_sockets(&ustconsumer64_data
)) {
6045 goto exit_init_data
;
6048 if (set_consumer_sockets(&ustconsumer32_data
)) {
6050 goto exit_init_data
;
6053 /* Setup the needed unix socket */
6054 if (init_daemon_socket()) {
6056 goto exit_init_data
;
6059 /* Set credentials to socket */
6060 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6062 goto exit_init_data
;
6065 /* Get parent pid if -S, --sig-parent is specified. */
6066 if (config
.sig_parent
) {
6070 /* Setup the kernel pipe for waking up the kernel thread */
6071 if (is_root
&& !config
.no_kernel
) {
6072 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6074 goto exit_init_data
;
6078 /* Setup the thread apps communication pipe. */
6079 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6081 goto exit_init_data
;
6084 /* Setup the thread apps notify communication pipe. */
6085 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6087 goto exit_init_data
;
6090 /* Initialize global buffer per UID and PID registry. */
6091 buffer_reg_init_uid_registry();
6092 buffer_reg_init_pid_registry();
6094 /* Init UST command queue. */
6095 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6098 * Get session list pointer. This pointer MUST NOT be free'd. This list
6099 * is statically declared in session.c
6101 session_list_ptr
= session_get_list();
6105 /* Check for the application socket timeout env variable. */
6106 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6107 if (env_app_timeout
) {
6108 config
.app_socket_timeout
= atoi(env_app_timeout
);
6110 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6113 ret
= write_pidfile();
6115 ERR("Error in write_pidfile");
6117 goto exit_init_data
;
6120 /* Initialize communication library */
6122 /* Initialize TCP timeout values */
6123 lttcomm_inet_init();
6125 if (load_session_init_data(&load_info
) < 0) {
6127 goto exit_init_data
;
6129 load_info
->path
= config
.load_session_path
.value
;
6131 /* Create health-check thread. */
6132 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6133 thread_manage_health
, (void *) NULL
);
6136 PERROR("pthread_create health");
6142 * The rotation thread needs the notification thread to be ready before
6143 * creating the rotate_notification_channel, so we use this semaphore as
6144 * a rendez-vous point.
6146 sem_init(¬ification_thread_ready
, 0, 0);
6148 /* notification_thread_data acquires the pipes' read side. */
6149 notification_thread_handle
= notification_thread_handle_create(
6150 ust32_channel_monitor_pipe
,
6151 ust64_channel_monitor_pipe
,
6152 kernel_channel_monitor_pipe
,
6153 ¬ification_thread_ready
);
6154 if (!notification_thread_handle
) {
6156 ERR("Failed to create notification thread shared data");
6158 goto exit_notification
;
6161 /* Create notification thread. */
6162 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6163 thread_notification
, notification_thread_handle
);
6166 PERROR("pthread_create notification");
6169 goto exit_notification
;
6171 notification_thread_launched
= true;
6173 /* Create timer thread. */
6174 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6175 sessiond_timer_thread
, &timer_thread_ctx
);
6178 PERROR("pthread_create timer");
6181 goto exit_notification
;
6183 timer_thread_launched
= true;
6185 /* rotation_thread_data acquires the pipes' read side. */
6186 rotation_thread_handle
= rotation_thread_handle_create(
6187 ust32_channel_rotate_pipe
,
6188 ust64_channel_rotate_pipe
,
6189 kernel_channel_rotate_pipe
,
6190 thread_quit_pipe
[0],
6191 rotation_timer_queue
,
6192 notification_thread_handle
,
6193 ¬ification_thread_ready
);
6194 if (!rotation_thread_handle
) {
6196 ERR("Failed to create rotation thread shared data");
6201 /* Create rotation thread. */
6202 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6203 thread_rotation
, rotation_thread_handle
);
6206 PERROR("pthread_create rotation");
6211 rotation_thread_launched
= true;
6213 /* Create thread to manage the client socket */
6214 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6215 thread_manage_clients
, (void *) NULL
);
6218 PERROR("pthread_create clients");
6224 /* Create thread to dispatch registration */
6225 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6226 thread_dispatch_ust_registration
, (void *) NULL
);
6229 PERROR("pthread_create dispatch");
6235 /* Create thread to manage application registration. */
6236 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6237 thread_registration_apps
, (void *) NULL
);
6240 PERROR("pthread_create registration");
6246 /* Create thread to manage application socket */
6247 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6248 thread_manage_apps
, (void *) NULL
);
6251 PERROR("pthread_create apps");
6257 /* Create thread to manage application notify socket */
6258 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6259 ust_thread_manage_notify
, (void *) NULL
);
6262 PERROR("pthread_create notify");
6265 goto exit_apps_notify
;
6268 /* Create agent registration thread. */
6269 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6270 agent_thread_manage_registration
, (void *) NULL
);
6273 PERROR("pthread_create agent");
6276 goto exit_agent_reg
;
6279 /* Don't start this thread if kernel tracing is not requested nor root */
6280 if (is_root
&& !config
.no_kernel
) {
6281 /* Create kernel thread to manage kernel event */
6282 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6283 thread_manage_kernel
, (void *) NULL
);
6286 PERROR("pthread_create kernel");
6293 /* Create session loading thread. */
6294 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6295 thread_load_session
, load_info
);
6298 PERROR("pthread_create load_session_thread");
6301 goto exit_load_session
;
6305 * This is where we start awaiting program completion (e.g. through
6306 * signal that asks threads to teardown).
6309 ret
= pthread_join(load_session_thread
, &status
);
6312 PERROR("pthread_join load_session_thread");
6317 if (is_root
&& !config
.no_kernel
) {
6318 ret
= pthread_join(kernel_thread
, &status
);
6321 PERROR("pthread_join");
6327 ret
= pthread_join(agent_reg_thread
, &status
);
6330 PERROR("pthread_join agent");
6335 ret
= pthread_join(apps_notify_thread
, &status
);
6338 PERROR("pthread_join apps notify");
6343 ret
= pthread_join(apps_thread
, &status
);
6346 PERROR("pthread_join apps");
6351 ret
= pthread_join(reg_apps_thread
, &status
);
6354 PERROR("pthread_join");
6360 * Join dispatch thread after joining reg_apps_thread to ensure
6361 * we don't leak applications in the queue.
6363 ret
= pthread_join(dispatch_thread
, &status
);
6366 PERROR("pthread_join");
6371 ret
= pthread_join(client_thread
, &status
);
6374 PERROR("pthread_join");
6381 sem_destroy(¬ification_thread_ready
);
6382 ret
= pthread_join(health_thread
, &status
);
6385 PERROR("pthread_join health thread");
6392 * Wait for all pending call_rcu work to complete before tearing
6393 * down data structures. call_rcu worker may be trying to
6394 * perform lookups in those structures.
6398 * sessiond_cleanup() is called when no other thread is running, except
6399 * the ht_cleanup thread, which is needed to destroy the hash tables.
6401 rcu_thread_online();
6405 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6406 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6407 * the queue is empty before shutting down the clean-up thread.
6412 * The teardown of the notification system is performed after the
6413 * session daemon's teardown in order to allow it to be notified
6414 * of the active session and channels at the moment of the teardown.
6416 if (notification_thread_handle
) {
6417 if (notification_thread_launched
) {
6418 notification_thread_command_quit(
6419 notification_thread_handle
);
6420 ret
= pthread_join(notification_thread
, &status
);
6423 PERROR("pthread_join notification thread");
6427 notification_thread_handle_destroy(notification_thread_handle
);
6430 if (rotation_thread_handle
) {
6431 if (rotation_thread_launched
) {
6432 ret
= pthread_join(rotation_thread
, &status
);
6435 PERROR("pthread_join rotation thread");
6439 rotation_thread_handle_destroy(rotation_thread_handle
);
6442 if (timer_thread_launched
) {
6443 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6444 ret
= pthread_join(timer_thread
, &status
);
6447 PERROR("pthread_join timer thread");
6453 * After the rotation and timer thread have quit, we can safely destroy
6454 * the rotation_timer_queue.
6456 destroy_rotate_timer_queue(rotation_timer_queue
);
6458 rcu_thread_offline();
6459 rcu_unregister_thread();
6461 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6465 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6466 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6467 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6468 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6469 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6470 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6473 health_app_destroy(health_sessiond
);
6474 exit_health_sessiond_cleanup
:
6475 exit_create_run_as_worker_cleanup
:
6478 sessiond_cleanup_lock_file();
6479 sessiond_cleanup_options();
6481 exit_set_signal_handler
: