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"
76 #include "ht-cleanup.h"
78 #define CONSUMERD_FILE "lttng-consumerd"
81 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
82 static int tracing_group_name_override
;
83 static char *opt_pidfile
;
84 static int opt_sig_parent
;
85 static int opt_verbose_consumer
;
86 static int opt_daemon
, opt_background
;
87 static int opt_no_kernel
;
88 static char *opt_load_session_path
;
89 static pid_t ppid
; /* Parent PID for --sig-parent option */
90 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
92 static int lockfile_fd
= -1;
93 static int opt_print_version
;
95 /* Set to 1 when a SIGUSR1 signal is received. */
96 static int recv_child_signal
;
99 * Consumer daemon specific control data. Every value not initialized here is
100 * set to 0 by the static definition.
102 static struct consumer_data kconsumer_data
= {
103 .type
= LTTNG_CONSUMER_KERNEL
,
104 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
105 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
108 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
109 .lock
= PTHREAD_MUTEX_INITIALIZER
,
110 .cond
= PTHREAD_COND_INITIALIZER
,
111 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 static struct consumer_data ustconsumer64_data
= {
114 .type
= LTTNG_CONSUMER64_UST
,
115 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
116 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
119 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
120 .lock
= PTHREAD_MUTEX_INITIALIZER
,
121 .cond
= PTHREAD_COND_INITIALIZER
,
122 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 static struct consumer_data ustconsumer32_data
= {
125 .type
= LTTNG_CONSUMER32_UST
,
126 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
127 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
130 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
131 .lock
= PTHREAD_MUTEX_INITIALIZER
,
132 .cond
= PTHREAD_COND_INITIALIZER
,
133 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
136 /* Command line options */
137 static const struct option long_options
[] = {
138 { "client-sock", required_argument
, 0, 'c' },
139 { "apps-sock", required_argument
, 0, 'a' },
140 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
141 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
142 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
143 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
144 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
145 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
146 { "consumerd32-path", required_argument
, 0, '\0' },
147 { "consumerd32-libdir", required_argument
, 0, '\0' },
148 { "consumerd64-path", required_argument
, 0, '\0' },
149 { "consumerd64-libdir", required_argument
, 0, '\0' },
150 { "daemonize", no_argument
, 0, 'd' },
151 { "background", no_argument
, 0, 'b' },
152 { "sig-parent", no_argument
, 0, 'S' },
153 { "help", no_argument
, 0, 'h' },
154 { "group", required_argument
, 0, 'g' },
155 { "version", no_argument
, 0, 'V' },
156 { "quiet", no_argument
, 0, 'q' },
157 { "verbose", no_argument
, 0, 'v' },
158 { "verbose-consumer", no_argument
, 0, '\0' },
159 { "no-kernel", no_argument
, 0, '\0' },
160 { "pidfile", required_argument
, 0, 'p' },
161 { "agent-tcp-port", required_argument
, 0, '\0' },
162 { "config", required_argument
, 0, 'f' },
163 { "load", required_argument
, 0, 'l' },
164 { "kmod-probes", required_argument
, 0, '\0' },
165 { "extra-kmod-probes", required_argument
, 0, '\0' },
169 /* Command line options to ignore from configuration file */
170 static const char *config_ignore_options
[] = { "help", "version", "config" };
172 /* Shared between threads */
173 static int dispatch_thread_exit
;
175 /* Global application Unix socket path */
176 static char apps_unix_sock_path
[PATH_MAX
];
177 /* Global client Unix socket path */
178 static char client_unix_sock_path
[PATH_MAX
];
179 /* global wait shm path for UST */
180 static char wait_shm_path
[PATH_MAX
];
181 /* Global health check unix path */
182 static char health_unix_sock_path
[PATH_MAX
];
184 /* Sockets and FDs */
185 static int client_sock
= -1;
186 static int apps_sock
= -1;
187 int kernel_tracer_fd
= -1;
188 static int kernel_poll_pipe
[2] = { -1, -1 };
191 * Quit pipe for all threads. This permits a single cancellation point
192 * for all threads when receiving an event on the pipe.
194 static int thread_quit_pipe
[2] = { -1, -1 };
197 * This pipe is used to inform the thread managing application communication
198 * that a command is queued and ready to be processed.
200 static int apps_cmd_pipe
[2] = { -1, -1 };
202 int apps_cmd_notify_pipe
[2] = { -1, -1 };
204 /* Pthread, Mutexes and Semaphores */
205 static pthread_t apps_thread
;
206 static pthread_t apps_notify_thread
;
207 static pthread_t reg_apps_thread
;
208 static pthread_t client_thread
;
209 static pthread_t kernel_thread
;
210 static pthread_t dispatch_thread
;
211 static pthread_t health_thread
;
212 static pthread_t ht_cleanup_thread
;
213 static pthread_t agent_reg_thread
;
214 static pthread_t load_session_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 *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
243 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
244 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
245 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
246 static int consumerd32_bin_override
;
247 static int consumerd64_bin_override
;
248 static int consumerd32_libdir_override
;
249 static int consumerd64_libdir_override
;
251 static const char *module_proc_lttng
= "/proc/lttng";
254 * Consumer daemon state which is changed when spawning it, killing it or in
255 * case of a fatal error.
257 enum consumerd_state
{
258 CONSUMER_STARTED
= 1,
259 CONSUMER_STOPPED
= 2,
264 * This consumer daemon state is used to validate if a client command will be
265 * able to reach the consumer. If not, the client is informed. For instance,
266 * doing a "lttng start" when the consumer state is set to ERROR will return an
267 * error to the client.
269 * The following example shows a possible race condition of this scheme:
271 * consumer thread error happens
273 * client cmd checks state -> still OK
274 * consumer thread exit, sets error
275 * client cmd try to talk to consumer
278 * However, since the consumer is a different daemon, we have no way of making
279 * sure the command will reach it safely even with this state flag. This is why
280 * we consider that up to the state validation during command processing, the
281 * command is safe. After that, we can not guarantee the correctness of the
282 * client request vis-a-vis the consumer.
284 static enum consumerd_state ust_consumerd_state
;
285 static enum consumerd_state kernel_consumerd_state
;
288 * Socket timeout for receiving and sending in seconds.
290 static int app_socket_timeout
;
292 /* Set in main() with the current page size. */
295 /* Application health monitoring */
296 struct health_app
*health_sessiond
;
298 /* Agent TCP port for registration. Used by the agent thread. */
299 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
301 /* Am I root or not. */
302 int is_root
; /* Set to 1 if the daemon is running as root */
304 const char * const config_section_name
= "sessiond";
306 /* Load session thread information to operate. */
307 struct load_session_thread_data
*load_info
;
309 /* Global hash tables */
310 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
313 * Whether sessiond is ready for commands/health check requests.
314 * NR_LTTNG_SESSIOND_READY must match the number of calls to
315 * sessiond_notify_ready().
317 #define NR_LTTNG_SESSIOND_READY 4
318 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
320 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
322 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
325 /* Notify parents that we are ready for cmd and health check */
327 void sessiond_notify_ready(void)
329 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
331 * Notify parent pid that we are ready to accept command
332 * for client side. This ppid is the one from the
333 * external process that spawned us.
335 if (opt_sig_parent
) {
340 * Notify the parent of the fork() process that we are
343 if (opt_daemon
|| opt_background
) {
344 kill(child_ppid
, SIGUSR1
);
350 void setup_consumerd_path(void)
352 const char *bin
, *libdir
;
355 * Allow INSTALL_BIN_PATH to be used as a target path for the
356 * native architecture size consumer if CONFIG_CONSUMER*_PATH
357 * has not been defined.
359 #if (CAA_BITS_PER_LONG == 32)
360 if (!consumerd32_bin
[0]) {
361 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
363 if (!consumerd32_libdir
[0]) {
364 consumerd32_libdir
= INSTALL_LIB_PATH
;
366 #elif (CAA_BITS_PER_LONG == 64)
367 if (!consumerd64_bin
[0]) {
368 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
370 if (!consumerd64_libdir
[0]) {
371 consumerd64_libdir
= INSTALL_LIB_PATH
;
374 #error "Unknown bitness"
378 * runtime env. var. overrides the build default.
380 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
382 consumerd32_bin
= bin
;
384 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
386 consumerd64_bin
= bin
;
388 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
390 consumerd32_libdir
= libdir
;
392 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
394 consumerd64_libdir
= libdir
;
399 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
406 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
412 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
424 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
426 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
428 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
432 * Init thread quit pipe.
434 * Return -1 on error or 0 if all pipes are created.
436 static int __init_thread_quit_pipe(int *a_pipe
)
442 PERROR("thread quit pipe");
446 for (i
= 0; i
< 2; i
++) {
447 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
458 static int init_thread_quit_pipe(void)
460 return __init_thread_quit_pipe(thread_quit_pipe
);
464 * Stop all threads by closing the thread quit pipe.
466 static void stop_threads(void)
470 /* Stopping all threads */
471 DBG("Terminating all threads");
472 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
474 ERR("write error on thread quit pipe");
477 /* Dispatch thread */
478 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
479 futex_nto1_wake(&ust_cmd_queue
.futex
);
483 * Close every consumer sockets.
485 static void close_consumer_sockets(void)
489 if (kconsumer_data
.err_sock
>= 0) {
490 ret
= close(kconsumer_data
.err_sock
);
492 PERROR("kernel consumer err_sock close");
495 if (ustconsumer32_data
.err_sock
>= 0) {
496 ret
= close(ustconsumer32_data
.err_sock
);
498 PERROR("UST consumerd32 err_sock close");
501 if (ustconsumer64_data
.err_sock
>= 0) {
502 ret
= close(ustconsumer64_data
.err_sock
);
504 PERROR("UST consumerd64 err_sock close");
507 if (kconsumer_data
.cmd_sock
>= 0) {
508 ret
= close(kconsumer_data
.cmd_sock
);
510 PERROR("kernel consumer cmd_sock close");
513 if (ustconsumer32_data
.cmd_sock
>= 0) {
514 ret
= close(ustconsumer32_data
.cmd_sock
);
516 PERROR("UST consumerd32 cmd_sock close");
519 if (ustconsumer64_data
.cmd_sock
>= 0) {
520 ret
= close(ustconsumer64_data
.cmd_sock
);
522 PERROR("UST consumerd64 cmd_sock close");
528 * Generate the full lock file path using the rundir.
530 * Return the snprintf() return value thus a negative value is an error.
532 static int generate_lock_file_path(char *path
, size_t len
)
539 /* Build lockfile path from rundir. */
540 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
542 PERROR("snprintf lockfile path");
549 * Wait on consumer process termination.
551 * Need to be called with the consumer data lock held or from a context
552 * ensuring no concurrent access to data (e.g: cleanup).
554 static void wait_consumer(struct consumer_data
*consumer_data
)
559 if (consumer_data
->pid
<= 0) {
563 DBG("Waiting for complete teardown of consumerd (PID: %d)",
565 ret
= waitpid(consumer_data
->pid
, &status
, 0);
567 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
568 } else if (!WIFEXITED(status
)) {
569 ERR("consumerd termination with error: %d",
572 consumer_data
->pid
= 0;
576 * Cleanup the session daemon's data structures.
578 static void sessiond_cleanup(void)
581 struct ltt_session
*sess
, *stmp
;
584 DBG("Cleanup sessiond");
587 * Close the thread quit pipe. It has already done its job,
588 * since we are now called.
590 utils_close_pipe(thread_quit_pipe
);
593 * If opt_pidfile is undefined, the default file will be wiped when
594 * removing the rundir.
597 ret
= remove(opt_pidfile
);
599 PERROR("remove pidfile %s", opt_pidfile
);
603 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
606 snprintf(path
, PATH_MAX
,
608 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
609 DBG("Removing %s", path
);
612 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
613 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
614 DBG("Removing %s", path
);
618 snprintf(path
, PATH_MAX
,
619 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
621 DBG("Removing %s", path
);
624 snprintf(path
, PATH_MAX
,
625 DEFAULT_KCONSUMERD_PATH
,
627 DBG("Removing directory %s", path
);
630 /* ust consumerd 32 */
631 snprintf(path
, PATH_MAX
,
632 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
634 DBG("Removing %s", path
);
637 snprintf(path
, PATH_MAX
,
638 DEFAULT_USTCONSUMERD32_PATH
,
640 DBG("Removing directory %s", path
);
643 /* ust consumerd 64 */
644 snprintf(path
, PATH_MAX
,
645 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
647 DBG("Removing %s", path
);
650 snprintf(path
, PATH_MAX
,
651 DEFAULT_USTCONSUMERD64_PATH
,
653 DBG("Removing directory %s", path
);
656 DBG("Cleaning up all sessions");
658 /* Destroy session list mutex */
659 if (session_list_ptr
!= NULL
) {
660 pthread_mutex_destroy(&session_list_ptr
->lock
);
662 /* Cleanup ALL session */
663 cds_list_for_each_entry_safe(sess
, stmp
,
664 &session_list_ptr
->head
, list
) {
665 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
669 wait_consumer(&kconsumer_data
);
670 wait_consumer(&ustconsumer64_data
);
671 wait_consumer(&ustconsumer32_data
);
673 DBG("Cleaning up all agent apps");
674 agent_app_ht_clean();
676 DBG("Closing all UST sockets");
677 ust_app_clean_list();
678 buffer_reg_destroy_registries();
680 if (is_root
&& !opt_no_kernel
) {
681 DBG2("Closing kernel fd");
682 if (kernel_tracer_fd
>= 0) {
683 ret
= close(kernel_tracer_fd
);
688 DBG("Unloading kernel modules");
689 modprobe_remove_lttng_all();
693 close_consumer_sockets();
696 load_session_destroy_data(load_info
);
701 * Cleanup lock file by deleting it and finaly closing it which will
702 * release the file system lock.
704 if (lockfile_fd
>= 0) {
705 char lockfile_path
[PATH_MAX
];
707 ret
= generate_lock_file_path(lockfile_path
,
708 sizeof(lockfile_path
));
710 ret
= remove(lockfile_path
);
712 PERROR("remove lock file");
714 ret
= close(lockfile_fd
);
716 PERROR("close lock file");
722 * We do NOT rmdir rundir because there are other processes
723 * using it, for instance lttng-relayd, which can start in
724 * parallel with this teardown.
731 * Cleanup the daemon's option data structures.
733 static void sessiond_cleanup_options(void)
735 DBG("Cleaning up options");
738 * If the override option is set, the pointer points to a *non* const
739 * thus freeing it even though the variable type is set to const.
741 if (tracing_group_name_override
) {
742 free((void *) tracing_group_name
);
744 if (consumerd32_bin_override
) {
745 free((void *) consumerd32_bin
);
747 if (consumerd64_bin_override
) {
748 free((void *) consumerd64_bin
);
750 if (consumerd32_libdir_override
) {
751 free((void *) consumerd32_libdir
);
753 if (consumerd64_libdir_override
) {
754 free((void *) consumerd64_libdir
);
758 free(opt_load_session_path
);
759 free(kmod_probes_list
);
760 free(kmod_extra_probes_list
);
762 run_as_destroy_worker();
766 * Send data on a unix socket using the liblttsessiondcomm API.
768 * Return lttcomm error code.
770 static int send_unix_sock(int sock
, void *buf
, size_t len
)
772 /* Check valid length */
777 return lttcomm_send_unix_sock(sock
, buf
, len
);
781 * Free memory of a command context structure.
783 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
785 DBG("Clean command context structure");
787 if ((*cmd_ctx
)->llm
) {
788 free((*cmd_ctx
)->llm
);
790 if ((*cmd_ctx
)->lsm
) {
791 free((*cmd_ctx
)->lsm
);
799 * Notify UST applications using the shm mmap futex.
801 static int notify_ust_apps(int active
)
805 DBG("Notifying applications of session daemon state: %d", active
);
807 /* See shm.c for this call implying mmap, shm and futex calls */
808 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
809 if (wait_shm_mmap
== NULL
) {
813 /* Wake waiting process */
814 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
816 /* Apps notified successfully */
824 * Setup the outgoing data buffer for the response (llm) by allocating the
825 * right amount of memory and copying the original information from the lsm
828 * Return 0 on success, negative value on error.
830 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
831 const void *payload_buf
, size_t payload_len
,
832 const void *cmd_header_buf
, size_t cmd_header_len
)
835 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
836 const size_t cmd_header_offset
= header_len
;
837 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
838 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
840 cmd_ctx
->llm
= zmalloc(total_msg_size
);
842 if (cmd_ctx
->llm
== NULL
) {
848 /* Copy common data */
849 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
850 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
851 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
852 cmd_ctx
->llm
->data_size
= payload_len
;
853 cmd_ctx
->lttng_msg_size
= total_msg_size
;
855 /* Copy command header */
856 if (cmd_header_len
) {
857 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
863 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
872 * Version of setup_lttng_msg() without command header.
874 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
875 void *payload_buf
, size_t payload_len
)
877 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
880 * Update the kernel poll set of all channel fd available over all tracing
881 * session. Add the wakeup pipe at the end of the set.
883 static int update_kernel_poll(struct lttng_poll_event
*events
)
886 struct ltt_session
*session
;
887 struct ltt_kernel_channel
*channel
;
889 DBG("Updating kernel poll set");
892 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
893 session_lock(session
);
894 if (session
->kernel_session
== NULL
) {
895 session_unlock(session
);
899 cds_list_for_each_entry(channel
,
900 &session
->kernel_session
->channel_list
.head
, list
) {
901 /* Add channel fd to the kernel poll set */
902 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
904 session_unlock(session
);
907 DBG("Channel fd %d added to kernel set", channel
->fd
);
909 session_unlock(session
);
911 session_unlock_list();
916 session_unlock_list();
921 * Find the channel fd from 'fd' over all tracing session. When found, check
922 * for new channel stream and send those stream fds to the kernel consumer.
924 * Useful for CPU hotplug feature.
926 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
929 struct ltt_session
*session
;
930 struct ltt_kernel_session
*ksess
;
931 struct ltt_kernel_channel
*channel
;
933 DBG("Updating kernel streams for channel fd %d", fd
);
936 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
937 session_lock(session
);
938 if (session
->kernel_session
== NULL
) {
939 session_unlock(session
);
942 ksess
= session
->kernel_session
;
944 cds_list_for_each_entry(channel
,
945 &ksess
->channel_list
.head
, list
) {
946 struct lttng_ht_iter iter
;
947 struct consumer_socket
*socket
;
949 if (channel
->fd
!= fd
) {
952 DBG("Channel found, updating kernel streams");
953 ret
= kernel_open_channel_stream(channel
);
957 /* Update the stream global counter */
958 ksess
->stream_count_global
+= ret
;
961 * Have we already sent fds to the consumer? If yes, it
962 * means that tracing is started so it is safe to send
963 * our updated stream fds.
965 if (ksess
->consumer_fds_sent
!= 1
966 || ksess
->consumer
== NULL
) {
972 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
973 &iter
.iter
, socket
, node
.node
) {
974 pthread_mutex_lock(socket
->lock
);
975 ret
= kernel_consumer_send_channel_stream(socket
,
977 session
->output_traces
? 1 : 0);
978 pthread_mutex_unlock(socket
->lock
);
986 session_unlock(session
);
988 session_unlock_list();
992 session_unlock(session
);
993 session_unlock_list();
998 * For each tracing session, update newly registered apps. The session list
999 * lock MUST be acquired before calling this.
1001 static void update_ust_app(int app_sock
)
1003 struct ltt_session
*sess
, *stmp
;
1005 /* Consumer is in an ERROR state. Stop any application update. */
1006 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1007 /* Stop the update process since the consumer is dead. */
1011 /* For all tracing session(s) */
1012 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1013 struct ust_app
*app
;
1016 if (!sess
->ust_session
) {
1017 goto unlock_session
;
1021 assert(app_sock
>= 0);
1022 app
= ust_app_find_by_sock(app_sock
);
1025 * Application can be unregistered before so
1026 * this is possible hence simply stopping the
1029 DBG3("UST app update failed to find app sock %d",
1033 ust_app_global_update(sess
->ust_session
, app
);
1037 session_unlock(sess
);
1042 * This thread manage event coming from the kernel.
1044 * Features supported in this thread:
1047 static void *thread_manage_kernel(void *data
)
1049 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1050 uint32_t revents
, nb_fd
;
1052 struct lttng_poll_event events
;
1054 DBG("[thread] Thread manage kernel started");
1056 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1059 * This first step of the while is to clean this structure which could free
1060 * non NULL pointers so initialize it before the loop.
1062 lttng_poll_init(&events
);
1064 if (testpoint(sessiond_thread_manage_kernel
)) {
1065 goto error_testpoint
;
1068 health_code_update();
1070 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1071 goto error_testpoint
;
1075 health_code_update();
1077 if (update_poll_flag
== 1) {
1078 /* Clean events object. We are about to populate it again. */
1079 lttng_poll_clean(&events
);
1081 ret
= sessiond_set_thread_pollset(&events
, 2);
1083 goto error_poll_create
;
1086 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1091 /* This will add the available kernel channel if any. */
1092 ret
= update_kernel_poll(&events
);
1096 update_poll_flag
= 0;
1099 DBG("Thread kernel polling");
1101 /* Poll infinite value of time */
1103 health_poll_entry();
1104 ret
= lttng_poll_wait(&events
, -1);
1105 DBG("Thread kernel return from poll on %d fds",
1106 LTTNG_POLL_GETNB(&events
));
1110 * Restart interrupted system call.
1112 if (errno
== EINTR
) {
1116 } else if (ret
== 0) {
1117 /* Should not happen since timeout is infinite */
1118 ERR("Return value of poll is 0 with an infinite timeout.\n"
1119 "This should not have happened! Continuing...");
1125 for (i
= 0; i
< nb_fd
; i
++) {
1126 /* Fetch once the poll data */
1127 revents
= LTTNG_POLL_GETEV(&events
, i
);
1128 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1130 health_code_update();
1133 /* No activity for this FD (poll implementation). */
1137 /* Thread quit pipe has been closed. Killing thread. */
1138 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1144 /* Check for data on kernel pipe */
1145 if (revents
& LPOLLIN
) {
1146 if (pollfd
== kernel_poll_pipe
[0]) {
1147 (void) lttng_read(kernel_poll_pipe
[0],
1150 * Ret value is useless here, if this pipe gets any actions an
1151 * update is required anyway.
1153 update_poll_flag
= 1;
1157 * New CPU detected by the kernel. Adding kernel stream to
1158 * kernel session and updating the kernel consumer
1160 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1166 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1167 update_poll_flag
= 1;
1170 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1178 lttng_poll_clean(&events
);
1181 utils_close_pipe(kernel_poll_pipe
);
1182 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1185 ERR("Health error occurred in %s", __func__
);
1186 WARN("Kernel thread died unexpectedly. "
1187 "Kernel tracing can continue but CPU hotplug is disabled.");
1189 health_unregister(health_sessiond
);
1190 DBG("Kernel thread dying");
1195 * Signal pthread condition of the consumer data that the thread.
1197 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1199 pthread_mutex_lock(&data
->cond_mutex
);
1202 * The state is set before signaling. It can be any value, it's the waiter
1203 * job to correctly interpret this condition variable associated to the
1204 * consumer pthread_cond.
1206 * A value of 0 means that the corresponding thread of the consumer data
1207 * was not started. 1 indicates that the thread has started and is ready
1208 * for action. A negative value means that there was an error during the
1211 data
->consumer_thread_is_ready
= state
;
1212 (void) pthread_cond_signal(&data
->cond
);
1214 pthread_mutex_unlock(&data
->cond_mutex
);
1218 * This thread manage the consumer error sent back to the session daemon.
1220 static void *thread_manage_consumer(void *data
)
1222 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1223 uint32_t revents
, nb_fd
;
1224 enum lttcomm_return_code code
;
1225 struct lttng_poll_event events
;
1226 struct consumer_data
*consumer_data
= data
;
1228 DBG("[thread] Manage consumer started");
1230 rcu_register_thread();
1231 rcu_thread_online();
1233 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1235 health_code_update();
1238 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1239 * metadata_sock. Nothing more will be added to this poll set.
1241 ret
= sessiond_set_thread_pollset(&events
, 3);
1247 * The error socket here is already in a listening state which was done
1248 * just before spawning this thread to avoid a race between the consumer
1249 * daemon exec trying to connect and the listen() call.
1251 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1256 health_code_update();
1258 /* Infinite blocking call, waiting for transmission */
1260 health_poll_entry();
1262 if (testpoint(sessiond_thread_manage_consumer
)) {
1266 ret
= lttng_poll_wait(&events
, -1);
1270 * Restart interrupted system call.
1272 if (errno
== EINTR
) {
1280 for (i
= 0; i
< nb_fd
; i
++) {
1281 /* Fetch once the poll data */
1282 revents
= LTTNG_POLL_GETEV(&events
, i
);
1283 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1285 health_code_update();
1288 /* No activity for this FD (poll implementation). */
1292 /* Thread quit pipe has been closed. Killing thread. */
1293 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1299 /* Event on the registration socket */
1300 if (pollfd
== consumer_data
->err_sock
) {
1301 if (revents
& LPOLLIN
) {
1303 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1304 ERR("consumer err socket poll error");
1307 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1313 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1319 * Set the CLOEXEC flag. Return code is useless because either way, the
1322 (void) utils_set_fd_cloexec(sock
);
1324 health_code_update();
1326 DBG2("Receiving code from consumer err_sock");
1328 /* Getting status code from kconsumerd */
1329 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1330 sizeof(enum lttcomm_return_code
));
1335 health_code_update();
1336 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1337 /* Connect both socket, command and metadata. */
1338 consumer_data
->cmd_sock
=
1339 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1340 consumer_data
->metadata_fd
=
1341 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1342 if (consumer_data
->cmd_sock
< 0
1343 || consumer_data
->metadata_fd
< 0) {
1344 PERROR("consumer connect cmd socket");
1345 /* On error, signal condition and quit. */
1346 signal_consumer_condition(consumer_data
, -1);
1349 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1350 /* Create metadata socket lock. */
1351 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1352 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1353 PERROR("zmalloc pthread mutex");
1356 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1358 signal_consumer_condition(consumer_data
, 1);
1359 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1360 DBG("Consumer metadata socket ready (fd: %d)",
1361 consumer_data
->metadata_fd
);
1363 ERR("consumer error when waiting for SOCK_READY : %s",
1364 lttcomm_get_readable_code(-code
));
1368 /* Remove the consumerd error sock since we've established a connexion */
1369 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1374 /* Add new accepted error socket. */
1375 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1380 /* Add metadata socket that is successfully connected. */
1381 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1382 LPOLLIN
| LPOLLRDHUP
);
1387 health_code_update();
1389 /* Infinite blocking call, waiting for transmission */
1392 health_code_update();
1394 /* Exit the thread because the thread quit pipe has been triggered. */
1396 /* Not a health error. */
1401 health_poll_entry();
1402 ret
= lttng_poll_wait(&events
, -1);
1406 * Restart interrupted system call.
1408 if (errno
== EINTR
) {
1416 for (i
= 0; i
< nb_fd
; i
++) {
1417 /* Fetch once the poll data */
1418 revents
= LTTNG_POLL_GETEV(&events
, i
);
1419 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1421 health_code_update();
1424 /* No activity for this FD (poll implementation). */
1429 * Thread quit pipe has been triggered, flag that we should stop
1430 * but continue the current loop to handle potential data from
1433 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1435 if (pollfd
== sock
) {
1436 /* Event on the consumerd socket */
1437 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1438 && !(revents
& LPOLLIN
)) {
1439 ERR("consumer err socket second poll error");
1442 health_code_update();
1443 /* Wait for any kconsumerd error */
1444 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1445 sizeof(enum lttcomm_return_code
));
1447 ERR("consumer closed the command socket");
1451 ERR("consumer return code : %s",
1452 lttcomm_get_readable_code(-code
));
1455 } else if (pollfd
== consumer_data
->metadata_fd
) {
1456 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1457 && !(revents
& LPOLLIN
)) {
1458 ERR("consumer err metadata socket second poll error");
1461 /* UST metadata requests */
1462 ret
= ust_consumer_metadata_request(
1463 &consumer_data
->metadata_sock
);
1465 ERR("Handling metadata request");
1469 /* No need for an else branch all FDs are tested prior. */
1471 health_code_update();
1477 * We lock here because we are about to close the sockets and some other
1478 * thread might be using them so get exclusive access which will abort all
1479 * other consumer command by other threads.
1481 pthread_mutex_lock(&consumer_data
->lock
);
1483 /* Immediately set the consumerd state to stopped */
1484 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1485 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1486 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1487 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1488 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1490 /* Code flow error... */
1494 if (consumer_data
->err_sock
>= 0) {
1495 ret
= close(consumer_data
->err_sock
);
1499 consumer_data
->err_sock
= -1;
1501 if (consumer_data
->cmd_sock
>= 0) {
1502 ret
= close(consumer_data
->cmd_sock
);
1506 consumer_data
->cmd_sock
= -1;
1508 if (consumer_data
->metadata_sock
.fd_ptr
&&
1509 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1510 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1522 unlink(consumer_data
->err_unix_sock_path
);
1523 unlink(consumer_data
->cmd_unix_sock_path
);
1524 pthread_mutex_unlock(&consumer_data
->lock
);
1526 /* Cleanup metadata socket mutex. */
1527 if (consumer_data
->metadata_sock
.lock
) {
1528 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1529 free(consumer_data
->metadata_sock
.lock
);
1531 lttng_poll_clean(&events
);
1535 ERR("Health error occurred in %s", __func__
);
1537 health_unregister(health_sessiond
);
1538 DBG("consumer thread cleanup completed");
1540 rcu_thread_offline();
1541 rcu_unregister_thread();
1547 * This thread manage application communication.
1549 static void *thread_manage_apps(void *data
)
1551 int i
, ret
, pollfd
, err
= -1;
1553 uint32_t revents
, nb_fd
;
1554 struct lttng_poll_event events
;
1556 DBG("[thread] Manage application started");
1558 rcu_register_thread();
1559 rcu_thread_online();
1561 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1563 if (testpoint(sessiond_thread_manage_apps
)) {
1564 goto error_testpoint
;
1567 health_code_update();
1569 ret
= sessiond_set_thread_pollset(&events
, 2);
1571 goto error_poll_create
;
1574 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1579 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1583 health_code_update();
1586 DBG("Apps thread polling");
1588 /* Inifinite blocking call, waiting for transmission */
1590 health_poll_entry();
1591 ret
= lttng_poll_wait(&events
, -1);
1592 DBG("Apps thread return from poll on %d fds",
1593 LTTNG_POLL_GETNB(&events
));
1597 * Restart interrupted system call.
1599 if (errno
== EINTR
) {
1607 for (i
= 0; i
< nb_fd
; i
++) {
1608 /* Fetch once the poll data */
1609 revents
= LTTNG_POLL_GETEV(&events
, i
);
1610 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1612 health_code_update();
1615 /* No activity for this FD (poll implementation). */
1619 /* Thread quit pipe has been closed. Killing thread. */
1620 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1626 /* Inspect the apps cmd pipe */
1627 if (pollfd
== apps_cmd_pipe
[0]) {
1628 if (revents
& LPOLLIN
) {
1632 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1633 if (size_ret
< sizeof(sock
)) {
1634 PERROR("read apps cmd pipe");
1638 health_code_update();
1641 * Since this is a command socket (write then read),
1642 * we only monitor the error events of the socket.
1644 ret
= lttng_poll_add(&events
, sock
,
1645 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1650 DBG("Apps with sock %d added to poll set", sock
);
1651 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1652 ERR("Apps command pipe error");
1655 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1660 * At this point, we know that a registered application made
1661 * the event at poll_wait.
1663 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1664 /* Removing from the poll set */
1665 ret
= lttng_poll_del(&events
, pollfd
);
1670 /* Socket closed on remote end. */
1671 ust_app_unregister(pollfd
);
1673 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1678 health_code_update();
1684 lttng_poll_clean(&events
);
1687 utils_close_pipe(apps_cmd_pipe
);
1688 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1691 * We don't clean the UST app hash table here since already registered
1692 * applications can still be controlled so let them be until the session
1693 * daemon dies or the applications stop.
1698 ERR("Health error occurred in %s", __func__
);
1700 health_unregister(health_sessiond
);
1701 DBG("Application communication apps thread cleanup complete");
1702 rcu_thread_offline();
1703 rcu_unregister_thread();
1708 * Send a socket to a thread This is called from the dispatch UST registration
1709 * thread once all sockets are set for the application.
1711 * The sock value can be invalid, we don't really care, the thread will handle
1712 * it and make the necessary cleanup if so.
1714 * On success, return 0 else a negative value being the errno message of the
1717 static int send_socket_to_thread(int fd
, int sock
)
1722 * It's possible that the FD is set as invalid with -1 concurrently just
1723 * before calling this function being a shutdown state of the thread.
1730 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1731 if (ret
< sizeof(sock
)) {
1732 PERROR("write apps pipe %d", fd
);
1739 /* All good. Don't send back the write positive ret value. */
1746 * Sanitize the wait queue of the dispatch registration thread meaning removing
1747 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1748 * notify socket is never received.
1750 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1752 int ret
, nb_fd
= 0, i
;
1753 unsigned int fd_added
= 0;
1754 struct lttng_poll_event events
;
1755 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1759 lttng_poll_init(&events
);
1761 /* Just skip everything for an empty queue. */
1762 if (!wait_queue
->count
) {
1766 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1771 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1772 &wait_queue
->head
, head
) {
1773 assert(wait_node
->app
);
1774 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1775 LPOLLHUP
| LPOLLERR
);
1788 * Poll but don't block so we can quickly identify the faulty events and
1789 * clean them afterwards from the wait queue.
1791 ret
= lttng_poll_wait(&events
, 0);
1797 for (i
= 0; i
< nb_fd
; i
++) {
1798 /* Get faulty FD. */
1799 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1800 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1803 /* No activity for this FD (poll implementation). */
1807 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1808 &wait_queue
->head
, head
) {
1809 if (pollfd
== wait_node
->app
->sock
&&
1810 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1811 cds_list_del(&wait_node
->head
);
1812 wait_queue
->count
--;
1813 ust_app_destroy(wait_node
->app
);
1816 * Silence warning of use-after-free in
1817 * cds_list_for_each_entry_safe which uses
1818 * __typeof__(*wait_node).
1823 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1830 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1834 lttng_poll_clean(&events
);
1838 lttng_poll_clean(&events
);
1840 ERR("Unable to sanitize wait queue");
1845 * Dispatch request from the registration threads to the application
1846 * communication thread.
1848 static void *thread_dispatch_ust_registration(void *data
)
1851 struct cds_wfcq_node
*node
;
1852 struct ust_command
*ust_cmd
= NULL
;
1853 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1854 struct ust_reg_wait_queue wait_queue
= {
1858 rcu_register_thread();
1860 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1862 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1863 goto error_testpoint
;
1866 health_code_update();
1868 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1870 DBG("[thread] Dispatch UST command started");
1873 health_code_update();
1875 /* Atomically prepare the queue futex */
1876 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1878 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1883 struct ust_app
*app
= NULL
;
1887 * Make sure we don't have node(s) that have hung up before receiving
1888 * the notify socket. This is to clean the list in order to avoid
1889 * memory leaks from notify socket that are never seen.
1891 sanitize_wait_queue(&wait_queue
);
1893 health_code_update();
1894 /* Dequeue command for registration */
1895 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1897 DBG("Woken up but nothing in the UST command queue");
1898 /* Continue thread execution */
1902 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1904 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1905 " gid:%d sock:%d name:%s (version %d.%d)",
1906 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1907 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1908 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1909 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1911 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1912 wait_node
= zmalloc(sizeof(*wait_node
));
1914 PERROR("zmalloc wait_node dispatch");
1915 ret
= close(ust_cmd
->sock
);
1917 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1919 lttng_fd_put(LTTNG_FD_APPS
, 1);
1923 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1925 /* Create application object if socket is CMD. */
1926 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1928 if (!wait_node
->app
) {
1929 ret
= close(ust_cmd
->sock
);
1931 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1933 lttng_fd_put(LTTNG_FD_APPS
, 1);
1939 * Add application to the wait queue so we can set the notify
1940 * socket before putting this object in the global ht.
1942 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1947 * We have to continue here since we don't have the notify
1948 * socket and the application MUST be added to the hash table
1949 * only at that moment.
1954 * Look for the application in the local wait queue and set the
1955 * notify socket if found.
1957 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1958 &wait_queue
.head
, head
) {
1959 health_code_update();
1960 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1961 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1962 cds_list_del(&wait_node
->head
);
1964 app
= wait_node
->app
;
1966 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1972 * With no application at this stage the received socket is
1973 * basically useless so close it before we free the cmd data
1974 * structure for good.
1977 ret
= close(ust_cmd
->sock
);
1979 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1981 lttng_fd_put(LTTNG_FD_APPS
, 1);
1988 * @session_lock_list
1990 * Lock the global session list so from the register up to the
1991 * registration done message, no thread can see the application
1992 * and change its state.
1994 session_lock_list();
1998 * Add application to the global hash table. This needs to be
1999 * done before the update to the UST registry can locate the
2004 /* Set app version. This call will print an error if needed. */
2005 (void) ust_app_version(app
);
2007 /* Send notify socket through the notify pipe. */
2008 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2012 session_unlock_list();
2014 * No notify thread, stop the UST tracing. However, this is
2015 * not an internal error of the this thread thus setting
2016 * the health error code to a normal exit.
2023 * Update newly registered application with the tracing
2024 * registry info already enabled information.
2026 update_ust_app(app
->sock
);
2029 * Don't care about return value. Let the manage apps threads
2030 * handle app unregistration upon socket close.
2032 (void) ust_app_register_done(app
);
2035 * Even if the application socket has been closed, send the app
2036 * to the thread and unregistration will take place at that
2039 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2042 session_unlock_list();
2044 * No apps. thread, stop the UST tracing. However, this is
2045 * not an internal error of the this thread thus setting
2046 * the health error code to a normal exit.
2053 session_unlock_list();
2055 } while (node
!= NULL
);
2057 health_poll_entry();
2058 /* Futex wait on queue. Blocking call on futex() */
2059 futex_nto1_wait(&ust_cmd_queue
.futex
);
2062 /* Normal exit, no error */
2066 /* Clean up wait queue. */
2067 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2068 &wait_queue
.head
, head
) {
2069 cds_list_del(&wait_node
->head
);
2074 /* Empty command queue. */
2076 /* Dequeue command for registration */
2077 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2081 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2082 ret
= close(ust_cmd
->sock
);
2084 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2086 lttng_fd_put(LTTNG_FD_APPS
, 1);
2091 DBG("Dispatch thread dying");
2094 ERR("Health error occurred in %s", __func__
);
2096 health_unregister(health_sessiond
);
2097 rcu_unregister_thread();
2102 * This thread manage application registration.
2104 static void *thread_registration_apps(void *data
)
2106 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2107 uint32_t revents
, nb_fd
;
2108 struct lttng_poll_event events
;
2110 * Get allocated in this thread, enqueued to a global queue, dequeued and
2111 * freed in the manage apps thread.
2113 struct ust_command
*ust_cmd
= NULL
;
2115 DBG("[thread] Manage application registration started");
2117 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2119 if (testpoint(sessiond_thread_registration_apps
)) {
2120 goto error_testpoint
;
2123 ret
= lttcomm_listen_unix_sock(apps_sock
);
2129 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2130 * more will be added to this poll set.
2132 ret
= sessiond_set_thread_pollset(&events
, 2);
2134 goto error_create_poll
;
2137 /* Add the application registration socket */
2138 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2140 goto error_poll_add
;
2143 /* Notify all applications to register */
2144 ret
= notify_ust_apps(1);
2146 ERR("Failed to notify applications or create the wait shared memory.\n"
2147 "Execution continues but there might be problem for already\n"
2148 "running applications that wishes to register.");
2152 DBG("Accepting application registration");
2154 /* Inifinite blocking call, waiting for transmission */
2156 health_poll_entry();
2157 ret
= lttng_poll_wait(&events
, -1);
2161 * Restart interrupted system call.
2163 if (errno
== EINTR
) {
2171 for (i
= 0; i
< nb_fd
; i
++) {
2172 health_code_update();
2174 /* Fetch once the poll data */
2175 revents
= LTTNG_POLL_GETEV(&events
, i
);
2176 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2179 /* No activity for this FD (poll implementation). */
2183 /* Thread quit pipe has been closed. Killing thread. */
2184 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2190 /* Event on the registration socket */
2191 if (pollfd
== apps_sock
) {
2192 if (revents
& LPOLLIN
) {
2193 sock
= lttcomm_accept_unix_sock(apps_sock
);
2199 * Set socket timeout for both receiving and ending.
2200 * app_socket_timeout is in seconds, whereas
2201 * lttcomm_setsockopt_rcv_timeout and
2202 * lttcomm_setsockopt_snd_timeout expect msec as
2205 if (app_socket_timeout
>= 0) {
2206 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2207 app_socket_timeout
* 1000);
2208 (void) lttcomm_setsockopt_snd_timeout(sock
,
2209 app_socket_timeout
* 1000);
2213 * Set the CLOEXEC flag. Return code is useless because
2214 * either way, the show must go on.
2216 (void) utils_set_fd_cloexec(sock
);
2218 /* Create UST registration command for enqueuing */
2219 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2220 if (ust_cmd
== NULL
) {
2221 PERROR("ust command zmalloc");
2230 * Using message-based transmissions to ensure we don't
2231 * have to deal with partially received messages.
2233 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2235 ERR("Exhausted file descriptors allowed for applications.");
2245 health_code_update();
2246 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2249 /* Close socket of the application. */
2254 lttng_fd_put(LTTNG_FD_APPS
, 1);
2258 health_code_update();
2260 ust_cmd
->sock
= sock
;
2263 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2264 " gid:%d sock:%d name:%s (version %d.%d)",
2265 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2266 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2267 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2268 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2271 * Lock free enqueue the registration request. The red pill
2272 * has been taken! This apps will be part of the *system*.
2274 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2277 * Wake the registration queue futex. Implicit memory
2278 * barrier with the exchange in cds_wfcq_enqueue.
2280 futex_nto1_wake(&ust_cmd_queue
.futex
);
2281 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2282 ERR("Register apps socket poll error");
2285 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2294 /* Notify that the registration thread is gone */
2297 if (apps_sock
>= 0) {
2298 ret
= close(apps_sock
);
2308 lttng_fd_put(LTTNG_FD_APPS
, 1);
2310 unlink(apps_unix_sock_path
);
2313 lttng_poll_clean(&events
);
2317 DBG("UST Registration thread cleanup complete");
2320 ERR("Health error occurred in %s", __func__
);
2322 health_unregister(health_sessiond
);
2328 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2329 * exec or it will fails.
2331 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2334 struct timespec timeout
;
2337 * Make sure we set the readiness flag to 0 because we are NOT ready.
2338 * This access to consumer_thread_is_ready does not need to be
2339 * protected by consumer_data.cond_mutex (yet) since the consumer
2340 * management thread has not been started at this point.
2342 consumer_data
->consumer_thread_is_ready
= 0;
2344 /* Setup pthread condition */
2345 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2348 PERROR("pthread_condattr_init consumer data");
2353 * Set the monotonic clock in order to make sure we DO NOT jump in time
2354 * between the clock_gettime() call and the timedwait call. See bug #324
2355 * for a more details and how we noticed it.
2357 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2360 PERROR("pthread_condattr_setclock consumer data");
2364 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2367 PERROR("pthread_cond_init consumer data");
2371 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2372 thread_manage_consumer
, consumer_data
);
2375 PERROR("pthread_create consumer");
2380 /* We are about to wait on a pthread condition */
2381 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2383 /* Get time for sem_timedwait absolute timeout */
2384 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2386 * Set the timeout for the condition timed wait even if the clock gettime
2387 * call fails since we might loop on that call and we want to avoid to
2388 * increment the timeout too many times.
2390 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2393 * The following loop COULD be skipped in some conditions so this is why we
2394 * set ret to 0 in order to make sure at least one round of the loop is
2400 * Loop until the condition is reached or when a timeout is reached. Note
2401 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2402 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2403 * possible. This loop does not take any chances and works with both of
2406 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2407 if (clock_ret
< 0) {
2408 PERROR("clock_gettime spawn consumer");
2409 /* Infinite wait for the consumerd thread to be ready */
2410 ret
= pthread_cond_wait(&consumer_data
->cond
,
2411 &consumer_data
->cond_mutex
);
2413 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2414 &consumer_data
->cond_mutex
, &timeout
);
2418 /* Release the pthread condition */
2419 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2423 if (ret
== ETIMEDOUT
) {
2427 * Call has timed out so we kill the kconsumerd_thread and return
2430 ERR("Condition timed out. The consumer thread was never ready."
2432 pth_ret
= pthread_cancel(consumer_data
->thread
);
2434 PERROR("pthread_cancel consumer thread");
2437 PERROR("pthread_cond_wait failed consumer thread");
2439 /* Caller is expecting a negative value on failure. */
2444 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2445 if (consumer_data
->pid
== 0) {
2446 ERR("Consumerd did not start");
2447 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2450 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2459 * Join consumer thread
2461 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2465 /* Consumer pid must be a real one. */
2466 if (consumer_data
->pid
> 0) {
2468 ret
= kill(consumer_data
->pid
, SIGTERM
);
2470 PERROR("Error killing consumer daemon");
2473 return pthread_join(consumer_data
->thread
, &status
);
2480 * Fork and exec a consumer daemon (consumerd).
2482 * Return pid if successful else -1.
2484 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2488 const char *consumer_to_use
;
2489 const char *verbosity
;
2492 DBG("Spawning consumerd");
2499 if (opt_verbose_consumer
) {
2500 verbosity
= "--verbose";
2501 } else if (lttng_opt_quiet
) {
2502 verbosity
= "--quiet";
2507 switch (consumer_data
->type
) {
2508 case LTTNG_CONSUMER_KERNEL
:
2510 * Find out which consumerd to execute. We will first try the
2511 * 64-bit path, then the sessiond's installation directory, and
2512 * fallback on the 32-bit one,
2514 DBG3("Looking for a kernel consumer at these locations:");
2515 DBG3(" 1) %s", consumerd64_bin
);
2516 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2517 DBG3(" 3) %s", consumerd32_bin
);
2518 if (stat(consumerd64_bin
, &st
) == 0) {
2519 DBG3("Found location #1");
2520 consumer_to_use
= consumerd64_bin
;
2521 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2522 DBG3("Found location #2");
2523 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2524 } else if (stat(consumerd32_bin
, &st
) == 0) {
2525 DBG3("Found location #3");
2526 consumer_to_use
= consumerd32_bin
;
2528 DBG("Could not find any valid consumerd executable");
2532 DBG("Using kernel consumer at: %s", consumer_to_use
);
2533 ret
= execl(consumer_to_use
,
2534 "lttng-consumerd", verbosity
, "-k",
2535 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2536 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2537 "--group", tracing_group_name
,
2540 case LTTNG_CONSUMER64_UST
:
2542 char *tmpnew
= NULL
;
2544 if (consumerd64_libdir
[0] != '\0') {
2548 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2552 tmplen
= strlen("LD_LIBRARY_PATH=")
2553 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2554 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2559 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2560 strcat(tmpnew
, consumerd64_libdir
);
2561 if (tmp
[0] != '\0') {
2562 strcat(tmpnew
, ":");
2563 strcat(tmpnew
, tmp
);
2565 ret
= putenv(tmpnew
);
2572 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2573 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2574 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2575 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2576 "--group", tracing_group_name
,
2578 if (consumerd64_libdir
[0] != '\0') {
2583 case LTTNG_CONSUMER32_UST
:
2585 char *tmpnew
= NULL
;
2587 if (consumerd32_libdir
[0] != '\0') {
2591 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2595 tmplen
= strlen("LD_LIBRARY_PATH=")
2596 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2597 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2602 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2603 strcat(tmpnew
, consumerd32_libdir
);
2604 if (tmp
[0] != '\0') {
2605 strcat(tmpnew
, ":");
2606 strcat(tmpnew
, tmp
);
2608 ret
= putenv(tmpnew
);
2615 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2616 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2617 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2618 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2619 "--group", tracing_group_name
,
2621 if (consumerd32_libdir
[0] != '\0') {
2627 ERR("unknown consumer type");
2631 PERROR("Consumer execl()");
2633 /* Reaching this point, we got a failure on our execl(). */
2635 } else if (pid
> 0) {
2638 PERROR("start consumer fork");
2646 * Spawn the consumerd daemon and session daemon thread.
2648 static int start_consumerd(struct consumer_data
*consumer_data
)
2653 * Set the listen() state on the socket since there is a possible race
2654 * between the exec() of the consumer daemon and this call if place in the
2655 * consumer thread. See bug #366 for more details.
2657 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2662 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2663 if (consumer_data
->pid
!= 0) {
2664 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2668 ret
= spawn_consumerd(consumer_data
);
2670 ERR("Spawning consumerd failed");
2671 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2675 /* Setting up the consumer_data pid */
2676 consumer_data
->pid
= ret
;
2677 DBG2("Consumer pid %d", consumer_data
->pid
);
2678 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2680 DBG2("Spawning consumer control thread");
2681 ret
= spawn_consumer_thread(consumer_data
);
2683 ERR("Fatal error spawning consumer control thread");
2691 /* Cleanup already created sockets on error. */
2692 if (consumer_data
->err_sock
>= 0) {
2695 err
= close(consumer_data
->err_sock
);
2697 PERROR("close consumer data error socket");
2704 * Setup necessary data for kernel tracer action.
2706 static int init_kernel_tracer(void)
2710 /* Modprobe lttng kernel modules */
2711 ret
= modprobe_lttng_control();
2716 /* Open debugfs lttng */
2717 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2718 if (kernel_tracer_fd
< 0) {
2719 DBG("Failed to open %s", module_proc_lttng
);
2723 /* Validate kernel version */
2724 ret
= kernel_validate_version(kernel_tracer_fd
);
2729 ret
= modprobe_lttng_data();
2734 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2738 modprobe_remove_lttng_control();
2739 ret
= close(kernel_tracer_fd
);
2743 kernel_tracer_fd
= -1;
2744 return LTTNG_ERR_KERN_VERSION
;
2747 ret
= close(kernel_tracer_fd
);
2753 modprobe_remove_lttng_control();
2756 WARN("No kernel tracer available");
2757 kernel_tracer_fd
= -1;
2759 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2761 return LTTNG_ERR_KERN_NA
;
2767 * Copy consumer output from the tracing session to the domain session. The
2768 * function also applies the right modification on a per domain basis for the
2769 * trace files destination directory.
2771 * Should *NOT* be called with RCU read-side lock held.
2773 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2776 const char *dir_name
;
2777 struct consumer_output
*consumer
;
2780 assert(session
->consumer
);
2783 case LTTNG_DOMAIN_KERNEL
:
2784 DBG3("Copying tracing session consumer output in kernel session");
2786 * XXX: We should audit the session creation and what this function
2787 * does "extra" in order to avoid a destroy since this function is used
2788 * in the domain session creation (kernel and ust) only. Same for UST
2791 if (session
->kernel_session
->consumer
) {
2792 consumer_output_put(session
->kernel_session
->consumer
);
2794 session
->kernel_session
->consumer
=
2795 consumer_copy_output(session
->consumer
);
2796 /* Ease our life a bit for the next part */
2797 consumer
= session
->kernel_session
->consumer
;
2798 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2800 case LTTNG_DOMAIN_JUL
:
2801 case LTTNG_DOMAIN_LOG4J
:
2802 case LTTNG_DOMAIN_PYTHON
:
2803 case LTTNG_DOMAIN_UST
:
2804 DBG3("Copying tracing session consumer output in UST session");
2805 if (session
->ust_session
->consumer
) {
2806 consumer_output_put(session
->ust_session
->consumer
);
2808 session
->ust_session
->consumer
=
2809 consumer_copy_output(session
->consumer
);
2810 /* Ease our life a bit for the next part */
2811 consumer
= session
->ust_session
->consumer
;
2812 dir_name
= DEFAULT_UST_TRACE_DIR
;
2815 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2819 /* Append correct directory to subdir */
2820 strncat(consumer
->subdir
, dir_name
,
2821 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2822 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2831 * Create an UST session and add it to the session ust list.
2833 * Should *NOT* be called with RCU read-side lock held.
2835 static int create_ust_session(struct ltt_session
*session
,
2836 struct lttng_domain
*domain
)
2839 struct ltt_ust_session
*lus
= NULL
;
2843 assert(session
->consumer
);
2845 switch (domain
->type
) {
2846 case LTTNG_DOMAIN_JUL
:
2847 case LTTNG_DOMAIN_LOG4J
:
2848 case LTTNG_DOMAIN_PYTHON
:
2849 case LTTNG_DOMAIN_UST
:
2852 ERR("Unknown UST domain on create session %d", domain
->type
);
2853 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2857 DBG("Creating UST session");
2859 lus
= trace_ust_create_session(session
->id
);
2861 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2865 lus
->uid
= session
->uid
;
2866 lus
->gid
= session
->gid
;
2867 lus
->output_traces
= session
->output_traces
;
2868 lus
->snapshot_mode
= session
->snapshot_mode
;
2869 lus
->live_timer_interval
= session
->live_timer
;
2870 session
->ust_session
= lus
;
2871 if (session
->shm_path
[0]) {
2872 strncpy(lus
->root_shm_path
, session
->shm_path
,
2873 sizeof(lus
->root_shm_path
));
2874 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2875 strncpy(lus
->shm_path
, session
->shm_path
,
2876 sizeof(lus
->shm_path
));
2877 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2878 strncat(lus
->shm_path
, "/ust",
2879 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2881 /* Copy session output to the newly created UST session */
2882 ret
= copy_session_consumer(domain
->type
, session
);
2883 if (ret
!= LTTNG_OK
) {
2891 session
->ust_session
= NULL
;
2896 * Create a kernel tracer session then create the default channel.
2898 static int create_kernel_session(struct ltt_session
*session
)
2902 DBG("Creating kernel session");
2904 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2906 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2910 /* Code flow safety */
2911 assert(session
->kernel_session
);
2913 /* Copy session output to the newly created Kernel session */
2914 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2915 if (ret
!= LTTNG_OK
) {
2919 /* Create directory(ies) on local filesystem. */
2920 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2921 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2922 ret
= run_as_mkdir_recursive(
2923 session
->kernel_session
->consumer
->dst
.trace_path
,
2924 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2926 if (errno
!= EEXIST
) {
2927 ERR("Trace directory creation error");
2933 session
->kernel_session
->uid
= session
->uid
;
2934 session
->kernel_session
->gid
= session
->gid
;
2935 session
->kernel_session
->output_traces
= session
->output_traces
;
2936 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2941 trace_kernel_destroy_session(session
->kernel_session
);
2942 session
->kernel_session
= NULL
;
2947 * Count number of session permitted by uid/gid.
2949 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2952 struct ltt_session
*session
;
2954 DBG("Counting number of available session for UID %d GID %d",
2956 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2958 * Only list the sessions the user can control.
2960 if (!session_access_ok(session
, uid
, gid
)) {
2969 * Process the command requested by the lttng client within the command
2970 * context structure. This function make sure that the return structure (llm)
2971 * is set and ready for transmission before returning.
2973 * Return any error encountered or 0 for success.
2975 * "sock" is only used for special-case var. len data.
2977 * Should *NOT* be called with RCU read-side lock held.
2979 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2983 int need_tracing_session
= 1;
2986 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2988 assert(!rcu_read_ongoing());
2992 switch (cmd_ctx
->lsm
->cmd_type
) {
2993 case LTTNG_CREATE_SESSION
:
2994 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2995 case LTTNG_CREATE_SESSION_LIVE
:
2996 case LTTNG_DESTROY_SESSION
:
2997 case LTTNG_LIST_SESSIONS
:
2998 case LTTNG_LIST_DOMAINS
:
2999 case LTTNG_START_TRACE
:
3000 case LTTNG_STOP_TRACE
:
3001 case LTTNG_DATA_PENDING
:
3002 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3003 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3004 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3005 case LTTNG_SNAPSHOT_RECORD
:
3006 case LTTNG_SAVE_SESSION
:
3007 case LTTNG_SET_SESSION_SHM_PATH
:
3008 case LTTNG_REGENERATE_METADATA
:
3009 case LTTNG_REGENERATE_STATEDUMP
:
3016 if (opt_no_kernel
&& need_domain
3017 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3019 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3021 ret
= LTTNG_ERR_KERN_NA
;
3026 /* Deny register consumer if we already have a spawned consumer. */
3027 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3028 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3029 if (kconsumer_data
.pid
> 0) {
3030 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3031 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3034 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3038 * Check for command that don't needs to allocate a returned payload. We do
3039 * this here so we don't have to make the call for no payload at each
3042 switch(cmd_ctx
->lsm
->cmd_type
) {
3043 case LTTNG_LIST_SESSIONS
:
3044 case LTTNG_LIST_TRACEPOINTS
:
3045 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3046 case LTTNG_LIST_DOMAINS
:
3047 case LTTNG_LIST_CHANNELS
:
3048 case LTTNG_LIST_EVENTS
:
3049 case LTTNG_LIST_SYSCALLS
:
3050 case LTTNG_LIST_TRACKER_PIDS
:
3051 case LTTNG_DATA_PENDING
:
3054 /* Setup lttng message with no payload */
3055 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3057 /* This label does not try to unlock the session */
3058 goto init_setup_error
;
3062 /* Commands that DO NOT need a session. */
3063 switch (cmd_ctx
->lsm
->cmd_type
) {
3064 case LTTNG_CREATE_SESSION
:
3065 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3066 case LTTNG_CREATE_SESSION_LIVE
:
3067 case LTTNG_LIST_SESSIONS
:
3068 case LTTNG_LIST_TRACEPOINTS
:
3069 case LTTNG_LIST_SYSCALLS
:
3070 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3071 case LTTNG_SAVE_SESSION
:
3072 need_tracing_session
= 0;
3075 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3077 * We keep the session list lock across _all_ commands
3078 * for now, because the per-session lock does not
3079 * handle teardown properly.
3081 session_lock_list();
3082 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3083 if (cmd_ctx
->session
== NULL
) {
3084 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3087 /* Acquire lock for the session */
3088 session_lock(cmd_ctx
->session
);
3094 * Commands that need a valid session but should NOT create one if none
3095 * exists. Instead of creating one and destroying it when the command is
3096 * handled, process that right before so we save some round trip in useless
3099 switch (cmd_ctx
->lsm
->cmd_type
) {
3100 case LTTNG_DISABLE_CHANNEL
:
3101 case LTTNG_DISABLE_EVENT
:
3102 switch (cmd_ctx
->lsm
->domain
.type
) {
3103 case LTTNG_DOMAIN_KERNEL
:
3104 if (!cmd_ctx
->session
->kernel_session
) {
3105 ret
= LTTNG_ERR_NO_CHANNEL
;
3109 case LTTNG_DOMAIN_JUL
:
3110 case LTTNG_DOMAIN_LOG4J
:
3111 case LTTNG_DOMAIN_PYTHON
:
3112 case LTTNG_DOMAIN_UST
:
3113 if (!cmd_ctx
->session
->ust_session
) {
3114 ret
= LTTNG_ERR_NO_CHANNEL
;
3119 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3131 * Check domain type for specific "pre-action".
3133 switch (cmd_ctx
->lsm
->domain
.type
) {
3134 case LTTNG_DOMAIN_KERNEL
:
3136 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3140 /* Kernel tracer check */
3141 if (kernel_tracer_fd
== -1) {
3142 /* Basically, load kernel tracer modules */
3143 ret
= init_kernel_tracer();
3149 /* Consumer is in an ERROR state. Report back to client */
3150 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3151 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3155 /* Need a session for kernel command */
3156 if (need_tracing_session
) {
3157 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3158 ret
= create_kernel_session(cmd_ctx
->session
);
3160 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3165 /* Start the kernel consumer daemon */
3166 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3167 if (kconsumer_data
.pid
== 0 &&
3168 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3169 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3170 ret
= start_consumerd(&kconsumer_data
);
3172 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3175 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3177 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3181 * The consumer was just spawned so we need to add the socket to
3182 * the consumer output of the session if exist.
3184 ret
= consumer_create_socket(&kconsumer_data
,
3185 cmd_ctx
->session
->kernel_session
->consumer
);
3192 case LTTNG_DOMAIN_JUL
:
3193 case LTTNG_DOMAIN_LOG4J
:
3194 case LTTNG_DOMAIN_PYTHON
:
3195 case LTTNG_DOMAIN_UST
:
3197 if (!ust_app_supported()) {
3198 ret
= LTTNG_ERR_NO_UST
;
3201 /* Consumer is in an ERROR state. Report back to client */
3202 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3203 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3207 if (need_tracing_session
) {
3208 /* Create UST session if none exist. */
3209 if (cmd_ctx
->session
->ust_session
== NULL
) {
3210 ret
= create_ust_session(cmd_ctx
->session
,
3211 &cmd_ctx
->lsm
->domain
);
3212 if (ret
!= LTTNG_OK
) {
3217 /* Start the UST consumer daemons */
3219 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3220 if (consumerd64_bin
[0] != '\0' &&
3221 ustconsumer64_data
.pid
== 0 &&
3222 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3223 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3224 ret
= start_consumerd(&ustconsumer64_data
);
3226 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3227 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3231 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3232 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3234 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3238 * Setup socket for consumer 64 bit. No need for atomic access
3239 * since it was set above and can ONLY be set in this thread.
3241 ret
= consumer_create_socket(&ustconsumer64_data
,
3242 cmd_ctx
->session
->ust_session
->consumer
);
3248 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3249 if (consumerd32_bin
[0] != '\0' &&
3250 ustconsumer32_data
.pid
== 0 &&
3251 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3252 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3253 ret
= start_consumerd(&ustconsumer32_data
);
3255 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3256 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3260 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3261 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3263 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3267 * Setup socket for consumer 64 bit. No need for atomic access
3268 * since it was set above and can ONLY be set in this thread.
3270 ret
= consumer_create_socket(&ustconsumer32_data
,
3271 cmd_ctx
->session
->ust_session
->consumer
);
3283 /* Validate consumer daemon state when start/stop trace command */
3284 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3285 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3286 switch (cmd_ctx
->lsm
->domain
.type
) {
3287 case LTTNG_DOMAIN_NONE
:
3289 case LTTNG_DOMAIN_JUL
:
3290 case LTTNG_DOMAIN_LOG4J
:
3291 case LTTNG_DOMAIN_PYTHON
:
3292 case LTTNG_DOMAIN_UST
:
3293 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3294 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3298 case LTTNG_DOMAIN_KERNEL
:
3299 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3300 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3305 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3311 * Check that the UID or GID match that of the tracing session.
3312 * The root user can interact with all sessions.
3314 if (need_tracing_session
) {
3315 if (!session_access_ok(cmd_ctx
->session
,
3316 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3317 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3318 ret
= LTTNG_ERR_EPERM
;
3324 * Send relayd information to consumer as soon as we have a domain and a
3327 if (cmd_ctx
->session
&& need_domain
) {
3329 * Setup relayd if not done yet. If the relayd information was already
3330 * sent to the consumer, this call will gracefully return.
3332 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3333 if (ret
!= LTTNG_OK
) {
3338 /* Process by command type */
3339 switch (cmd_ctx
->lsm
->cmd_type
) {
3340 case LTTNG_ADD_CONTEXT
:
3343 * An LTTNG_ADD_CONTEXT command might have a supplementary
3344 * payload if the context being added is an application context.
3346 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3347 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3348 char *provider_name
= NULL
, *context_name
= NULL
;
3349 size_t provider_name_len
=
3350 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3351 size_t context_name_len
=
3352 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3354 if (provider_name_len
== 0 || context_name_len
== 0) {
3356 * Application provider and context names MUST
3359 ret
= -LTTNG_ERR_INVALID
;
3363 provider_name
= zmalloc(provider_name_len
+ 1);
3364 if (!provider_name
) {
3365 ret
= -LTTNG_ERR_NOMEM
;
3368 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3371 context_name
= zmalloc(context_name_len
+ 1);
3372 if (!context_name
) {
3373 ret
= -LTTNG_ERR_NOMEM
;
3374 goto error_add_context
;
3376 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3379 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3382 goto error_add_context
;
3385 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3388 goto error_add_context
;
3393 * cmd_add_context assumes ownership of the provider and context
3396 ret
= cmd_add_context(cmd_ctx
->session
,
3397 cmd_ctx
->lsm
->domain
.type
,
3398 cmd_ctx
->lsm
->u
.context
.channel_name
,
3399 &cmd_ctx
->lsm
->u
.context
.ctx
,
3400 kernel_poll_pipe
[1]);
3402 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3403 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3405 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3406 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3412 case LTTNG_DISABLE_CHANNEL
:
3414 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3415 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3418 case LTTNG_DISABLE_EVENT
:
3422 * FIXME: handle filter; for now we just receive the filter's
3423 * bytecode along with the filter expression which are sent by
3424 * liblttng-ctl and discard them.
3426 * This fixes an issue where the client may block while sending
3427 * the filter payload and encounter an error because the session
3428 * daemon closes the socket without ever handling this data.
3430 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3431 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3434 char data
[LTTNG_FILTER_MAX_LEN
];
3436 DBG("Discarding disable event command payload of size %zu", count
);
3438 ret
= lttcomm_recv_unix_sock(sock
, data
,
3439 count
> sizeof(data
) ? sizeof(data
) : count
);
3444 count
-= (size_t) ret
;
3447 /* FIXME: passing packed structure to non-packed pointer */
3448 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3449 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3450 &cmd_ctx
->lsm
->u
.disable
.event
);
3453 case LTTNG_ENABLE_CHANNEL
:
3455 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3456 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3459 case LTTNG_TRACK_PID
:
3461 ret
= cmd_track_pid(cmd_ctx
->session
,
3462 cmd_ctx
->lsm
->domain
.type
,
3463 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3466 case LTTNG_UNTRACK_PID
:
3468 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3469 cmd_ctx
->lsm
->domain
.type
,
3470 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3473 case LTTNG_ENABLE_EVENT
:
3475 struct lttng_event_exclusion
*exclusion
= NULL
;
3476 struct lttng_filter_bytecode
*bytecode
= NULL
;
3477 char *filter_expression
= NULL
;
3479 /* Handle exclusion events and receive it from the client. */
3480 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3481 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3483 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3484 (count
* LTTNG_SYMBOL_NAME_LEN
));
3486 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3490 DBG("Receiving var len exclusion event list from client ...");
3491 exclusion
->count
= count
;
3492 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3493 count
* LTTNG_SYMBOL_NAME_LEN
);
3495 DBG("Nothing recv() from client var len data... continuing");
3498 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3503 /* Get filter expression from client. */
3504 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3505 size_t expression_len
=
3506 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3508 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3509 ret
= LTTNG_ERR_FILTER_INVAL
;
3514 filter_expression
= zmalloc(expression_len
);
3515 if (!filter_expression
) {
3517 ret
= LTTNG_ERR_FILTER_NOMEM
;
3521 /* Receive var. len. data */
3522 DBG("Receiving var len filter's expression from client ...");
3523 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3526 DBG("Nothing recv() from client car len data... continuing");
3528 free(filter_expression
);
3530 ret
= LTTNG_ERR_FILTER_INVAL
;
3535 /* Handle filter and get bytecode from client. */
3536 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3537 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3539 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3540 ret
= LTTNG_ERR_FILTER_INVAL
;
3541 free(filter_expression
);
3546 bytecode
= zmalloc(bytecode_len
);
3548 free(filter_expression
);
3550 ret
= LTTNG_ERR_FILTER_NOMEM
;
3554 /* Receive var. len. data */
3555 DBG("Receiving var len filter's bytecode from client ...");
3556 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3558 DBG("Nothing recv() from client car len data... continuing");
3560 free(filter_expression
);
3563 ret
= LTTNG_ERR_FILTER_INVAL
;
3567 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3568 free(filter_expression
);
3571 ret
= LTTNG_ERR_FILTER_INVAL
;
3576 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3577 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3578 &cmd_ctx
->lsm
->u
.enable
.event
,
3579 filter_expression
, bytecode
, exclusion
,
3580 kernel_poll_pipe
[1]);
3583 case LTTNG_LIST_TRACEPOINTS
:
3585 struct lttng_event
*events
;
3588 session_lock_list();
3589 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3590 session_unlock_list();
3591 if (nb_events
< 0) {
3592 /* Return value is a negative lttng_error_code. */
3598 * Setup lttng message with payload size set to the event list size in
3599 * bytes and then copy list into the llm payload.
3601 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3602 sizeof(struct lttng_event
) * nb_events
);
3612 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3614 struct lttng_event_field
*fields
;
3617 session_lock_list();
3618 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3620 session_unlock_list();
3621 if (nb_fields
< 0) {
3622 /* Return value is a negative lttng_error_code. */
3628 * Setup lttng message with payload size set to the event list size in
3629 * bytes and then copy list into the llm payload.
3631 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3632 sizeof(struct lttng_event_field
) * nb_fields
);
3642 case LTTNG_LIST_SYSCALLS
:
3644 struct lttng_event
*events
;
3647 nb_events
= cmd_list_syscalls(&events
);
3648 if (nb_events
< 0) {
3649 /* Return value is a negative lttng_error_code. */
3655 * Setup lttng message with payload size set to the event list size in
3656 * bytes and then copy list into the llm payload.
3658 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3659 sizeof(struct lttng_event
) * nb_events
);
3669 case LTTNG_LIST_TRACKER_PIDS
:
3671 int32_t *pids
= NULL
;
3674 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3675 cmd_ctx
->lsm
->domain
.type
, &pids
);
3677 /* Return value is a negative lttng_error_code. */
3683 * Setup lttng message with payload size set to the event list size in
3684 * bytes and then copy list into the llm payload.
3686 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3687 sizeof(int32_t) * nr_pids
);
3697 case LTTNG_SET_CONSUMER_URI
:
3700 struct lttng_uri
*uris
;
3702 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3703 len
= nb_uri
* sizeof(struct lttng_uri
);
3706 ret
= LTTNG_ERR_INVALID
;
3710 uris
= zmalloc(len
);
3712 ret
= LTTNG_ERR_FATAL
;
3716 /* Receive variable len data */
3717 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3718 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3720 DBG("No URIs received from client... continuing");
3722 ret
= LTTNG_ERR_SESSION_FAIL
;
3727 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3729 if (ret
!= LTTNG_OK
) {
3736 case LTTNG_START_TRACE
:
3738 ret
= cmd_start_trace(cmd_ctx
->session
);
3741 case LTTNG_STOP_TRACE
:
3743 ret
= cmd_stop_trace(cmd_ctx
->session
);
3746 case LTTNG_CREATE_SESSION
:
3749 struct lttng_uri
*uris
= NULL
;
3751 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3752 len
= nb_uri
* sizeof(struct lttng_uri
);
3755 uris
= zmalloc(len
);
3757 ret
= LTTNG_ERR_FATAL
;
3761 /* Receive variable len data */
3762 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3763 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3765 DBG("No URIs received from client... continuing");
3767 ret
= LTTNG_ERR_SESSION_FAIL
;
3772 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3773 DBG("Creating session with ONE network URI is a bad call");
3774 ret
= LTTNG_ERR_SESSION_FAIL
;
3780 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3781 &cmd_ctx
->creds
, 0);
3787 case LTTNG_DESTROY_SESSION
:
3789 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3791 /* Set session to NULL so we do not unlock it after free. */
3792 cmd_ctx
->session
= NULL
;
3795 case LTTNG_LIST_DOMAINS
:
3798 struct lttng_domain
*domains
= NULL
;
3800 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3802 /* Return value is a negative lttng_error_code. */
3807 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3808 nb_dom
* sizeof(struct lttng_domain
));
3818 case LTTNG_LIST_CHANNELS
:
3820 ssize_t payload_size
;
3821 struct lttng_channel
*channels
= NULL
;
3823 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3824 cmd_ctx
->session
, &channels
);
3825 if (payload_size
< 0) {
3826 /* Return value is a negative lttng_error_code. */
3827 ret
= -payload_size
;
3831 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3842 case LTTNG_LIST_EVENTS
:
3845 struct lttng_event
*events
= NULL
;
3846 struct lttcomm_event_command_header cmd_header
;
3849 memset(&cmd_header
, 0, sizeof(cmd_header
));
3850 /* Extended infos are included at the end of events */
3851 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3852 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3853 &events
, &total_size
);
3856 /* Return value is a negative lttng_error_code. */
3861 cmd_header
.nb_events
= nb_event
;
3862 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3863 &cmd_header
, sizeof(cmd_header
));
3873 case LTTNG_LIST_SESSIONS
:
3875 unsigned int nr_sessions
;
3876 void *sessions_payload
;
3879 session_lock_list();
3880 nr_sessions
= lttng_sessions_count(
3881 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3882 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3883 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3884 sessions_payload
= zmalloc(payload_len
);
3886 if (!sessions_payload
) {
3887 session_unlock_list();
3892 cmd_list_lttng_sessions(sessions_payload
,
3893 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3894 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3895 session_unlock_list();
3897 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3899 free(sessions_payload
);
3908 case LTTNG_REGISTER_CONSUMER
:
3910 struct consumer_data
*cdata
;
3912 switch (cmd_ctx
->lsm
->domain
.type
) {
3913 case LTTNG_DOMAIN_KERNEL
:
3914 cdata
= &kconsumer_data
;
3917 ret
= LTTNG_ERR_UND
;
3921 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3922 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3925 case LTTNG_DATA_PENDING
:
3928 uint8_t pending_ret_byte
;
3930 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3935 * This function may returns 0 or 1 to indicate whether or not
3936 * there is data pending. In case of error, it should return an
3937 * LTTNG_ERR code. However, some code paths may still return
3938 * a nondescript error code, which we handle by returning an
3941 if (pending_ret
== 0 || pending_ret
== 1) {
3943 * ret will be set to LTTNG_OK at the end of
3946 } else if (pending_ret
< 0) {
3947 ret
= LTTNG_ERR_UNK
;
3954 pending_ret_byte
= (uint8_t) pending_ret
;
3956 /* 1 byte to return whether or not data is pending */
3957 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3958 &pending_ret_byte
, 1);
3967 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3969 struct lttcomm_lttng_output_id reply
;
3971 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3972 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3973 if (ret
!= LTTNG_OK
) {
3977 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3983 /* Copy output list into message payload */
3987 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3989 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3990 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3993 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3996 struct lttng_snapshot_output
*outputs
= NULL
;
3998 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3999 if (nb_output
< 0) {
4004 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4005 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4006 nb_output
* sizeof(struct lttng_snapshot_output
));
4016 case LTTNG_SNAPSHOT_RECORD
:
4018 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4019 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4020 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4023 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4026 struct lttng_uri
*uris
= NULL
;
4028 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4029 len
= nb_uri
* sizeof(struct lttng_uri
);
4032 uris
= zmalloc(len
);
4034 ret
= LTTNG_ERR_FATAL
;
4038 /* Receive variable len data */
4039 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4040 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4042 DBG("No URIs received from client... continuing");
4044 ret
= LTTNG_ERR_SESSION_FAIL
;
4049 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4050 DBG("Creating session with ONE network URI is a bad call");
4051 ret
= LTTNG_ERR_SESSION_FAIL
;
4057 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4058 nb_uri
, &cmd_ctx
->creds
);
4062 case LTTNG_CREATE_SESSION_LIVE
:
4065 struct lttng_uri
*uris
= NULL
;
4067 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4068 len
= nb_uri
* sizeof(struct lttng_uri
);
4071 uris
= zmalloc(len
);
4073 ret
= LTTNG_ERR_FATAL
;
4077 /* Receive variable len data */
4078 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4079 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4081 DBG("No URIs received from client... continuing");
4083 ret
= LTTNG_ERR_SESSION_FAIL
;
4088 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4089 DBG("Creating session with ONE network URI is a bad call");
4090 ret
= LTTNG_ERR_SESSION_FAIL
;
4096 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4097 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4101 case LTTNG_SAVE_SESSION
:
4103 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4107 case LTTNG_SET_SESSION_SHM_PATH
:
4109 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4110 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4113 case LTTNG_REGENERATE_METADATA
:
4115 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4118 case LTTNG_REGENERATE_STATEDUMP
:
4120 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4124 ret
= LTTNG_ERR_UND
;
4129 if (cmd_ctx
->llm
== NULL
) {
4130 DBG("Missing llm structure. Allocating one.");
4131 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4135 /* Set return code */
4136 cmd_ctx
->llm
->ret_code
= ret
;
4138 if (cmd_ctx
->session
) {
4139 session_unlock(cmd_ctx
->session
);
4141 if (need_tracing_session
) {
4142 session_unlock_list();
4145 assert(!rcu_read_ongoing());
4150 * Thread managing health check socket.
4152 static void *thread_manage_health(void *data
)
4154 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4155 uint32_t revents
, nb_fd
;
4156 struct lttng_poll_event events
;
4157 struct health_comm_msg msg
;
4158 struct health_comm_reply reply
;
4160 DBG("[thread] Manage health check started");
4162 rcu_register_thread();
4164 /* We might hit an error path before this is created. */
4165 lttng_poll_init(&events
);
4167 /* Create unix socket */
4168 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4170 ERR("Unable to create health check Unix socket");
4175 /* lttng health client socket path permissions */
4176 ret
= chown(health_unix_sock_path
, 0,
4177 utils_get_group_id(tracing_group_name
));
4179 ERR("Unable to set group on %s", health_unix_sock_path
);
4184 ret
= chmod(health_unix_sock_path
,
4185 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4187 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4194 * Set the CLOEXEC flag. Return code is useless because either way, the
4197 (void) utils_set_fd_cloexec(sock
);
4199 ret
= lttcomm_listen_unix_sock(sock
);
4205 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4206 * more will be added to this poll set.
4208 ret
= sessiond_set_thread_pollset(&events
, 2);
4213 /* Add the application registration socket */
4214 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4219 sessiond_notify_ready();
4222 DBG("Health check ready");
4224 /* Inifinite blocking call, waiting for transmission */
4226 ret
= lttng_poll_wait(&events
, -1);
4229 * Restart interrupted system call.
4231 if (errno
== EINTR
) {
4239 for (i
= 0; i
< nb_fd
; i
++) {
4240 /* Fetch once the poll data */
4241 revents
= LTTNG_POLL_GETEV(&events
, i
);
4242 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4245 /* No activity for this FD (poll implementation). */
4249 /* Thread quit pipe has been closed. Killing thread. */
4250 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4256 /* Event on the registration socket */
4257 if (pollfd
== sock
) {
4258 if (revents
& LPOLLIN
) {
4260 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4261 ERR("Health socket poll error");
4264 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4270 new_sock
= lttcomm_accept_unix_sock(sock
);
4276 * Set the CLOEXEC flag. Return code is useless because either way, the
4279 (void) utils_set_fd_cloexec(new_sock
);
4281 DBG("Receiving data from client for health...");
4282 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4284 DBG("Nothing recv() from client... continuing");
4285 ret
= close(new_sock
);
4292 rcu_thread_online();
4294 memset(&reply
, 0, sizeof(reply
));
4295 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4297 * health_check_state returns 0 if health is
4300 if (!health_check_state(health_sessiond
, i
)) {
4301 reply
.ret_code
|= 1ULL << i
;
4305 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4307 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4309 ERR("Failed to send health data back to client");
4312 /* End of transmission */
4313 ret
= close(new_sock
);
4322 ERR("Health error occurred in %s", __func__
);
4324 DBG("Health check thread dying");
4325 unlink(health_unix_sock_path
);
4333 lttng_poll_clean(&events
);
4335 rcu_unregister_thread();
4340 * This thread manage all clients request using the unix client socket for
4343 static void *thread_manage_clients(void *data
)
4345 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4347 uint32_t revents
, nb_fd
;
4348 struct command_ctx
*cmd_ctx
= NULL
;
4349 struct lttng_poll_event events
;
4351 DBG("[thread] Manage client started");
4353 rcu_register_thread();
4355 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4357 health_code_update();
4359 ret
= lttcomm_listen_unix_sock(client_sock
);
4365 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4366 * more will be added to this poll set.
4368 ret
= sessiond_set_thread_pollset(&events
, 2);
4370 goto error_create_poll
;
4373 /* Add the application registration socket */
4374 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4379 sessiond_notify_ready();
4381 ret
= sem_post(&load_info
->message_thread_ready
);
4383 PERROR("sem_post message_thread_ready");
4388 * Wait until all support threads are initialized before accepting
4391 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4393 struct timeval timeout
;
4396 FD_SET(thread_quit_pipe
[0], &read_fds
);
4397 memset(&timeout
, 0, sizeof(timeout
));
4398 timeout
.tv_usec
= 1000;
4401 * If a support thread failed to launch, it may signal that
4402 * we must exit and the sessiond would never be marked as
4405 * The timeout is set to 1ms, which serves as a way to
4406 * pace down this check.
4408 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4410 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4415 /* This testpoint is after we signal readiness to the parent. */
4416 if (testpoint(sessiond_thread_manage_clients
)) {
4420 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4424 health_code_update();
4427 DBG("Accepting client command ...");
4429 /* Inifinite blocking call, waiting for transmission */
4431 health_poll_entry();
4432 ret
= lttng_poll_wait(&events
, -1);
4436 * Restart interrupted system call.
4438 if (errno
== EINTR
) {
4446 for (i
= 0; i
< nb_fd
; i
++) {
4447 /* Fetch once the poll data */
4448 revents
= LTTNG_POLL_GETEV(&events
, i
);
4449 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4451 health_code_update();
4454 /* No activity for this FD (poll implementation). */
4458 /* Thread quit pipe has been closed. Killing thread. */
4459 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4465 /* Event on the registration socket */
4466 if (pollfd
== client_sock
) {
4467 if (revents
& LPOLLIN
) {
4469 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4470 ERR("Client socket poll error");
4473 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4479 DBG("Wait for client response");
4481 health_code_update();
4483 sock
= lttcomm_accept_unix_sock(client_sock
);
4489 * Set the CLOEXEC flag. Return code is useless because either way, the
4492 (void) utils_set_fd_cloexec(sock
);
4494 /* Set socket option for credentials retrieval */
4495 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4500 /* Allocate context command to process the client request */
4501 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4502 if (cmd_ctx
== NULL
) {
4503 PERROR("zmalloc cmd_ctx");
4507 /* Allocate data buffer for reception */
4508 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4509 if (cmd_ctx
->lsm
== NULL
) {
4510 PERROR("zmalloc cmd_ctx->lsm");
4514 cmd_ctx
->llm
= NULL
;
4515 cmd_ctx
->session
= NULL
;
4517 health_code_update();
4520 * Data is received from the lttng client. The struct
4521 * lttcomm_session_msg (lsm) contains the command and data request of
4524 DBG("Receiving data from client ...");
4525 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4526 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4528 DBG("Nothing recv() from client... continuing");
4534 clean_command_ctx(&cmd_ctx
);
4538 health_code_update();
4540 // TODO: Validate cmd_ctx including sanity check for
4541 // security purpose.
4543 rcu_thread_online();
4545 * This function dispatch the work to the kernel or userspace tracer
4546 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4547 * informations for the client. The command context struct contains
4548 * everything this function may needs.
4550 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4551 rcu_thread_offline();
4559 * TODO: Inform client somehow of the fatal error. At
4560 * this point, ret < 0 means that a zmalloc failed
4561 * (ENOMEM). Error detected but still accept
4562 * command, unless a socket error has been
4565 clean_command_ctx(&cmd_ctx
);
4569 health_code_update();
4571 DBG("Sending response (size: %d, retcode: %s (%d))",
4572 cmd_ctx
->lttng_msg_size
,
4573 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4574 cmd_ctx
->llm
->ret_code
);
4575 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4577 ERR("Failed to send data back to client");
4580 /* End of transmission */
4587 clean_command_ctx(&cmd_ctx
);
4589 health_code_update();
4601 lttng_poll_clean(&events
);
4602 clean_command_ctx(&cmd_ctx
);
4606 unlink(client_unix_sock_path
);
4607 if (client_sock
>= 0) {
4608 ret
= close(client_sock
);
4616 ERR("Health error occurred in %s", __func__
);
4619 health_unregister(health_sessiond
);
4621 DBG("Client thread dying");
4623 rcu_unregister_thread();
4626 * Since we are creating the consumer threads, we own them, so we need
4627 * to join them before our thread exits.
4629 ret
= join_consumer_thread(&kconsumer_data
);
4632 PERROR("join_consumer");
4635 ret
= join_consumer_thread(&ustconsumer32_data
);
4638 PERROR("join_consumer ust32");
4641 ret
= join_consumer_thread(&ustconsumer64_data
);
4644 PERROR("join_consumer ust64");
4649 static int string_match(const char *str1
, const char *str2
)
4651 return (str1
&& str2
) && !strcmp(str1
, str2
);
4655 * Take an option from the getopt output and set it in the right variable to be
4658 * Return 0 on success else a negative value.
4660 static int set_option(int opt
, const char *arg
, const char *optname
)
4664 if (string_match(optname
, "client-sock") || opt
== 'c') {
4665 if (!arg
|| *arg
== '\0') {
4669 if (lttng_is_setuid_setgid()) {
4670 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4671 "-c, --client-sock");
4673 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4675 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4676 if (!arg
|| *arg
== '\0') {
4680 if (lttng_is_setuid_setgid()) {
4681 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4684 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4686 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4688 } else if (string_match(optname
, "background") || opt
== 'b') {
4690 } else if (string_match(optname
, "group") || opt
== 'g') {
4691 if (!arg
|| *arg
== '\0') {
4695 if (lttng_is_setuid_setgid()) {
4696 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4700 * If the override option is set, the pointer points to a
4701 * *non* const thus freeing it even though the variable type is
4704 if (tracing_group_name_override
) {
4705 free((void *) tracing_group_name
);
4707 tracing_group_name
= strdup(arg
);
4708 if (!tracing_group_name
) {
4712 tracing_group_name_override
= 1;
4714 } else if (string_match(optname
, "help") || opt
== 'h') {
4715 ret
= utils_show_man_page(8, "lttng-sessiond");
4717 ERR("Cannot view man page lttng-sessiond(8)");
4720 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4721 } else if (string_match(optname
, "version") || opt
== 'V') {
4722 opt_print_version
= 1;
4723 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4725 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4726 if (!arg
|| *arg
== '\0') {
4730 if (lttng_is_setuid_setgid()) {
4731 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4732 "--kconsumerd-err-sock");
4734 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4736 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4737 if (!arg
|| *arg
== '\0') {
4741 if (lttng_is_setuid_setgid()) {
4742 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4743 "--kconsumerd-cmd-sock");
4745 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4747 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4748 if (!arg
|| *arg
== '\0') {
4752 if (lttng_is_setuid_setgid()) {
4753 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4754 "--ustconsumerd64-err-sock");
4756 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4758 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4759 if (!arg
|| *arg
== '\0') {
4763 if (lttng_is_setuid_setgid()) {
4764 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4765 "--ustconsumerd64-cmd-sock");
4767 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4769 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4770 if (!arg
|| *arg
== '\0') {
4774 if (lttng_is_setuid_setgid()) {
4775 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4776 "--ustconsumerd32-err-sock");
4778 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4780 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4781 if (!arg
|| *arg
== '\0') {
4785 if (lttng_is_setuid_setgid()) {
4786 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4787 "--ustconsumerd32-cmd-sock");
4789 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4791 } else if (string_match(optname
, "no-kernel")) {
4793 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4794 lttng_opt_quiet
= 1;
4795 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4796 /* Verbose level can increase using multiple -v */
4798 /* Value obtained from config file */
4799 lttng_opt_verbose
= config_parse_value(arg
);
4801 /* -v used on command line */
4802 lttng_opt_verbose
++;
4804 /* Clamp value to [0, 3] */
4805 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4806 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4807 } else if (string_match(optname
, "verbose-consumer")) {
4809 opt_verbose_consumer
= config_parse_value(arg
);
4811 opt_verbose_consumer
++;
4813 } else if (string_match(optname
, "consumerd32-path")) {
4814 if (!arg
|| *arg
== '\0') {
4818 if (lttng_is_setuid_setgid()) {
4819 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4820 "--consumerd32-path");
4822 if (consumerd32_bin_override
) {
4823 free((void *) consumerd32_bin
);
4825 consumerd32_bin
= strdup(arg
);
4826 if (!consumerd32_bin
) {
4830 consumerd32_bin_override
= 1;
4832 } else if (string_match(optname
, "consumerd32-libdir")) {
4833 if (!arg
|| *arg
== '\0') {
4837 if (lttng_is_setuid_setgid()) {
4838 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4839 "--consumerd32-libdir");
4841 if (consumerd32_libdir_override
) {
4842 free((void *) consumerd32_libdir
);
4844 consumerd32_libdir
= strdup(arg
);
4845 if (!consumerd32_libdir
) {
4849 consumerd32_libdir_override
= 1;
4851 } else if (string_match(optname
, "consumerd64-path")) {
4852 if (!arg
|| *arg
== '\0') {
4856 if (lttng_is_setuid_setgid()) {
4857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4858 "--consumerd64-path");
4860 if (consumerd64_bin_override
) {
4861 free((void *) consumerd64_bin
);
4863 consumerd64_bin
= strdup(arg
);
4864 if (!consumerd64_bin
) {
4868 consumerd64_bin_override
= 1;
4870 } else if (string_match(optname
, "consumerd64-libdir")) {
4871 if (!arg
|| *arg
== '\0') {
4875 if (lttng_is_setuid_setgid()) {
4876 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4877 "--consumerd64-libdir");
4879 if (consumerd64_libdir_override
) {
4880 free((void *) consumerd64_libdir
);
4882 consumerd64_libdir
= strdup(arg
);
4883 if (!consumerd64_libdir
) {
4887 consumerd64_libdir_override
= 1;
4889 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4890 if (!arg
|| *arg
== '\0') {
4894 if (lttng_is_setuid_setgid()) {
4895 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4899 opt_pidfile
= strdup(arg
);
4905 } else if (string_match(optname
, "agent-tcp-port")) {
4906 if (!arg
|| *arg
== '\0') {
4910 if (lttng_is_setuid_setgid()) {
4911 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4912 "--agent-tcp-port");
4917 v
= strtoul(arg
, NULL
, 0);
4918 if (errno
!= 0 || !isdigit(arg
[0])) {
4919 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4922 if (v
== 0 || v
>= 65535) {
4923 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4926 agent_tcp_port
= (uint32_t) v
;
4927 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4929 } else if (string_match(optname
, "load") || opt
== 'l') {
4930 if (!arg
|| *arg
== '\0') {
4934 if (lttng_is_setuid_setgid()) {
4935 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4938 free(opt_load_session_path
);
4939 opt_load_session_path
= strdup(arg
);
4940 if (!opt_load_session_path
) {
4945 } else if (string_match(optname
, "kmod-probes")) {
4946 if (!arg
|| *arg
== '\0') {
4950 if (lttng_is_setuid_setgid()) {
4951 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4954 free(kmod_probes_list
);
4955 kmod_probes_list
= strdup(arg
);
4956 if (!kmod_probes_list
) {
4961 } else if (string_match(optname
, "extra-kmod-probes")) {
4962 if (!arg
|| *arg
== '\0') {
4966 if (lttng_is_setuid_setgid()) {
4967 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4968 "--extra-kmod-probes");
4970 free(kmod_extra_probes_list
);
4971 kmod_extra_probes_list
= strdup(arg
);
4972 if (!kmod_extra_probes_list
) {
4977 } else if (string_match(optname
, "config") || opt
== 'f') {
4978 /* This is handled in set_options() thus silent skip. */
4981 /* Unknown option or other error.
4982 * Error is printed by getopt, just return */
4987 if (ret
== -EINVAL
) {
4988 const char *opt_name
= "unknown";
4991 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4993 if (opt
== long_options
[i
].val
) {
4994 opt_name
= long_options
[i
].name
;
4999 WARN("Invalid argument provided for option \"%s\", using default value.",
5007 * config_entry_handler_cb used to handle options read from a config file.
5008 * See config_entry_handler_cb comment in common/config/session-config.h for the
5009 * return value conventions.
5011 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5015 if (!entry
|| !entry
->name
|| !entry
->value
) {
5020 /* Check if the option is to be ignored */
5021 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5022 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5027 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5030 /* Ignore if not fully matched. */
5031 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5036 * If the option takes no argument on the command line, we have to
5037 * check if the value is "true". We support non-zero numeric values,
5040 if (!long_options
[i
].has_arg
) {
5041 ret
= config_parse_value(entry
->value
);
5044 WARN("Invalid configuration value \"%s\" for option %s",
5045 entry
->value
, entry
->name
);
5047 /* False, skip boolean config option. */
5052 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5056 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5062 static void sessiond_config_log(void)
5064 DBG("LTTng-sessiond " VERSION
" - " VERSION_NAME
"%s%s",
5065 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
5066 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
5067 if (EXTRA_VERSION_DESCRIPTION
[0] != '\0') {
5068 DBG("LTTng-sessiond extra version description:\n\t" EXTRA_VERSION_DESCRIPTION
"\n");
5072 static void print_version(void) {
5073 fprintf(stdout
, "%s\n", VERSION
);
5077 * daemon configuration loading and argument parsing
5079 static int set_options(int argc
, char **argv
)
5081 int ret
= 0, c
= 0, option_index
= 0;
5082 int orig_optopt
= optopt
, orig_optind
= optind
;
5084 const char *config_path
= NULL
;
5086 optstring
= utils_generate_optstring(long_options
,
5087 sizeof(long_options
) / sizeof(struct option
));
5093 /* Check for the --config option */
5094 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5095 &option_index
)) != -1) {
5099 } else if (c
!= 'f') {
5100 /* if not equal to --config option. */
5104 if (lttng_is_setuid_setgid()) {
5105 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5108 config_path
= utils_expand_path(optarg
);
5110 ERR("Failed to resolve path: %s", optarg
);
5115 ret
= config_get_section_entries(config_path
, config_section_name
,
5116 config_entry_handler
, NULL
);
5119 ERR("Invalid configuration option at line %i", ret
);
5125 /* Reset getopt's global state */
5126 optopt
= orig_optopt
;
5127 optind
= orig_optind
;
5131 * getopt_long() will not set option_index if it encounters a
5134 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5141 * Pass NULL as the long option name if popt left the index
5144 ret
= set_option(c
, optarg
,
5145 option_index
< 0 ? NULL
:
5146 long_options
[option_index
].name
);
5158 * Creates the two needed socket by the daemon.
5159 * apps_sock - The communication socket for all UST apps.
5160 * client_sock - The communication of the cli tool (lttng).
5162 static int init_daemon_socket(void)
5167 old_umask
= umask(0);
5169 /* Create client tool unix socket */
5170 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5171 if (client_sock
< 0) {
5172 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5177 /* Set the cloexec flag */
5178 ret
= utils_set_fd_cloexec(client_sock
);
5180 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5181 "Continuing but note that the consumer daemon will have a "
5182 "reference to this socket on exec()", client_sock
);
5185 /* File permission MUST be 660 */
5186 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5188 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5193 /* Create the application unix socket */
5194 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5195 if (apps_sock
< 0) {
5196 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5201 /* Set the cloexec flag */
5202 ret
= utils_set_fd_cloexec(apps_sock
);
5204 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5205 "Continuing but note that the consumer daemon will have a "
5206 "reference to this socket on exec()", apps_sock
);
5209 /* File permission MUST be 666 */
5210 ret
= chmod(apps_unix_sock_path
,
5211 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5213 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5218 DBG3("Session daemon client socket %d and application socket %d created",
5219 client_sock
, apps_sock
);
5227 * Check if the global socket is available, and if a daemon is answering at the
5228 * other side. If yes, error is returned.
5230 static int check_existing_daemon(void)
5232 /* Is there anybody out there ? */
5233 if (lttng_session_daemon_alive()) {
5241 * Set the tracing group gid onto the client socket.
5243 * Race window between mkdir and chown is OK because we are going from more
5244 * permissive (root.root) to less permissive (root.tracing).
5246 static int set_permissions(char *rundir
)
5251 gid
= utils_get_group_id(tracing_group_name
);
5253 /* Set lttng run dir */
5254 ret
= chown(rundir
, 0, gid
);
5256 ERR("Unable to set group on %s", rundir
);
5261 * Ensure all applications and tracing group can search the run
5262 * dir. Allow everyone to read the directory, since it does not
5263 * buy us anything to hide its content.
5265 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5267 ERR("Unable to set permissions on %s", rundir
);
5271 /* lttng client socket path */
5272 ret
= chown(client_unix_sock_path
, 0, gid
);
5274 ERR("Unable to set group on %s", client_unix_sock_path
);
5278 /* kconsumer error socket path */
5279 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5281 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5285 /* 64-bit ustconsumer error socket path */
5286 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5288 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5292 /* 32-bit ustconsumer compat32 error socket path */
5293 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5295 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5299 DBG("All permissions are set");
5305 * Create the lttng run directory needed for all global sockets and pipe.
5307 static int create_lttng_rundir(const char *rundir
)
5311 DBG3("Creating LTTng run directory: %s", rundir
);
5313 ret
= mkdir(rundir
, S_IRWXU
);
5315 if (errno
!= EEXIST
) {
5316 ERR("Unable to create %s", rundir
);
5328 * Setup sockets and directory needed by the kconsumerd communication with the
5331 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5335 char path
[PATH_MAX
];
5337 switch (consumer_data
->type
) {
5338 case LTTNG_CONSUMER_KERNEL
:
5339 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5341 case LTTNG_CONSUMER64_UST
:
5342 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5344 case LTTNG_CONSUMER32_UST
:
5345 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5348 ERR("Consumer type unknown");
5353 DBG2("Creating consumer directory: %s", path
);
5355 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5357 if (errno
!= EEXIST
) {
5359 ERR("Failed to create %s", path
);
5365 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5367 ERR("Unable to set group on %s", path
);
5373 /* Create the kconsumerd error unix socket */
5374 consumer_data
->err_sock
=
5375 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5376 if (consumer_data
->err_sock
< 0) {
5377 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5383 * Set the CLOEXEC flag. Return code is useless because either way, the
5386 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5388 PERROR("utils_set_fd_cloexec");
5389 /* continue anyway */
5392 /* File permission MUST be 660 */
5393 ret
= chmod(consumer_data
->err_unix_sock_path
,
5394 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5396 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5406 * Signal handler for the daemon
5408 * Simply stop all worker threads, leaving main() return gracefully after
5409 * joining all threads and calling cleanup().
5411 static void sighandler(int sig
)
5415 DBG("SIGINT caught");
5419 DBG("SIGTERM caught");
5423 CMM_STORE_SHARED(recv_child_signal
, 1);
5431 * Setup signal handler for :
5432 * SIGINT, SIGTERM, SIGPIPE
5434 static int set_signal_handler(void)
5437 struct sigaction sa
;
5440 if ((ret
= sigemptyset(&sigset
)) < 0) {
5441 PERROR("sigemptyset");
5445 sa
.sa_mask
= sigset
;
5448 sa
.sa_handler
= sighandler
;
5449 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5450 PERROR("sigaction");
5454 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5455 PERROR("sigaction");
5459 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5460 PERROR("sigaction");
5464 sa
.sa_handler
= SIG_IGN
;
5465 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5466 PERROR("sigaction");
5470 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5476 * Set open files limit to unlimited. This daemon can open a large number of
5477 * file descriptors in order to consume multiple kernel traces.
5479 static void set_ulimit(void)
5484 /* The kernel does not allow an infinite limit for open files */
5485 lim
.rlim_cur
= 65535;
5486 lim
.rlim_max
= 65535;
5488 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5490 PERROR("failed to set open files limit");
5495 * Write pidfile using the rundir and opt_pidfile.
5497 static int write_pidfile(void)
5500 char pidfile_path
[PATH_MAX
];
5505 if (lttng_strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
))) {
5510 /* Build pidfile path from rundir and opt_pidfile. */
5511 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5512 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5514 PERROR("snprintf pidfile path");
5520 * Create pid file in rundir.
5522 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5528 * Create lockfile using the rundir and return its fd.
5530 static int create_lockfile(void)
5533 char lockfile_path
[PATH_MAX
];
5535 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5540 ret
= utils_create_lock_file(lockfile_path
);
5546 * Write agent TCP port using the rundir.
5548 static int write_agent_port(void)
5551 char path
[PATH_MAX
];
5555 ret
= snprintf(path
, sizeof(path
), "%s/"
5556 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5558 PERROR("snprintf agent port path");
5563 * Create TCP agent port file in rundir.
5565 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5571 static int set_clock_plugin_env(void)
5574 const char *original_env_value
;
5575 char *full_path
= NULL
;
5576 char *new_env_value
= NULL
;
5578 original_env_value
= getenv("LTTNG_UST_CLOCK_PLUGIN");
5579 if (!original_env_value
) {
5583 full_path
= utils_expand_path(original_env_value
);
5585 ERR("Failed to expand LTTNG_UST_CLOCK_PLUGIN path \"%s\"",
5586 original_env_value
);
5590 ret
= asprintf(&new_env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5598 DBG("Updating environment: %s", new_env_value
);
5599 ret
= putenv(new_env_value
);
5601 free(new_env_value
);
5602 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5612 int main(int argc
, char **argv
)
5614 int ret
= 0, retval
= 0;
5616 const char *home_path
, *env_app_timeout
;
5618 init_kernel_workarounds();
5620 rcu_register_thread();
5622 if (set_signal_handler()) {
5624 goto exit_set_signal_handler
;
5627 setup_consumerd_path();
5629 page_size
= sysconf(_SC_PAGESIZE
);
5630 if (page_size
< 0) {
5631 PERROR("sysconf _SC_PAGESIZE");
5632 page_size
= LONG_MAX
;
5633 WARN("Fallback page size to %ld", page_size
);
5637 * Parse arguments and load the daemon configuration file.
5639 * We have an exit_options exit path to free memory reserved by
5640 * set_options. This is needed because the rest of sessiond_cleanup()
5641 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5642 * depends on set_options.
5645 if (set_options(argc
, argv
)) {
5650 sessiond_config_log();
5652 if (opt_print_version
) {
5658 ret
= set_clock_plugin_env();
5665 if (opt_daemon
|| opt_background
) {
5668 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5676 * We are in the child. Make sure all other file descriptors are
5677 * closed, in case we are called with more opened file
5678 * descriptors than the standard ones.
5680 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5685 if (run_as_create_worker(argv
[0]) < 0) {
5686 goto exit_create_run_as_worker_cleanup
;
5690 * Starting from here, we can create threads. This needs to be after
5691 * lttng_daemonize due to RCU.
5695 * Initialize the health check subsystem. This call should set the
5696 * appropriate time values.
5698 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5699 if (!health_sessiond
) {
5700 PERROR("health_app_create error");
5702 goto exit_health_sessiond_cleanup
;
5705 /* Create thread to clean up RCU hash tables */
5706 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5708 goto exit_ht_cleanup
;
5711 /* Create thread quit pipe */
5712 if (init_thread_quit_pipe()) {
5714 goto exit_init_data
;
5717 /* Check if daemon is UID = 0 */
5718 is_root
= !getuid();
5721 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5724 goto exit_init_data
;
5727 /* Create global run dir with root access */
5728 if (create_lttng_rundir(rundir
)) {
5730 goto exit_init_data
;
5733 if (strlen(apps_unix_sock_path
) == 0) {
5734 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5735 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5738 goto exit_init_data
;
5742 if (strlen(client_unix_sock_path
) == 0) {
5743 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5744 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5747 goto exit_init_data
;
5751 /* Set global SHM for ust */
5752 if (strlen(wait_shm_path
) == 0) {
5753 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5754 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5757 goto exit_init_data
;
5761 if (strlen(health_unix_sock_path
) == 0) {
5762 ret
= snprintf(health_unix_sock_path
,
5763 sizeof(health_unix_sock_path
),
5764 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5767 goto exit_init_data
;
5771 /* Setup kernel consumerd path */
5772 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5773 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5776 goto exit_init_data
;
5778 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5779 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5782 goto exit_init_data
;
5785 DBG2("Kernel consumer err path: %s",
5786 kconsumer_data
.err_unix_sock_path
);
5787 DBG2("Kernel consumer cmd path: %s",
5788 kconsumer_data
.cmd_unix_sock_path
);
5790 home_path
= utils_get_home_dir();
5791 if (home_path
== NULL
) {
5792 /* TODO: Add --socket PATH option */
5793 ERR("Can't get HOME directory for sockets creation.");
5795 goto exit_init_data
;
5799 * Create rundir from home path. This will create something like
5802 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5805 goto exit_init_data
;
5808 if (create_lttng_rundir(rundir
)) {
5810 goto exit_init_data
;
5813 if (strlen(apps_unix_sock_path
) == 0) {
5814 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5815 DEFAULT_HOME_APPS_UNIX_SOCK
,
5819 goto exit_init_data
;
5823 /* Set the cli tool unix socket path */
5824 if (strlen(client_unix_sock_path
) == 0) {
5825 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5826 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5830 goto exit_init_data
;
5834 /* Set global SHM for ust */
5835 if (strlen(wait_shm_path
) == 0) {
5836 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5837 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5841 goto exit_init_data
;
5845 /* Set health check Unix path */
5846 if (strlen(health_unix_sock_path
) == 0) {
5847 ret
= snprintf(health_unix_sock_path
,
5848 sizeof(health_unix_sock_path
),
5849 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5853 goto exit_init_data
;
5858 lockfile_fd
= create_lockfile();
5859 if (lockfile_fd
< 0) {
5861 goto exit_init_data
;
5864 /* Set consumer initial state */
5865 kernel_consumerd_state
= CONSUMER_STOPPED
;
5866 ust_consumerd_state
= CONSUMER_STOPPED
;
5868 DBG("Client socket path %s", client_unix_sock_path
);
5869 DBG("Application socket path %s", apps_unix_sock_path
);
5870 DBG("Application wait path %s", wait_shm_path
);
5871 DBG("LTTng run directory path: %s", rundir
);
5873 /* 32 bits consumerd path setup */
5874 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5875 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5877 PERROR("snprintf 32-bit consumer error socket path");
5879 goto exit_init_data
;
5881 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5882 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5884 PERROR("snprintf 32-bit consumer command socket path");
5886 goto exit_init_data
;
5889 DBG2("UST consumer 32 bits err path: %s",
5890 ustconsumer32_data
.err_unix_sock_path
);
5891 DBG2("UST consumer 32 bits cmd path: %s",
5892 ustconsumer32_data
.cmd_unix_sock_path
);
5894 /* 64 bits consumerd path setup */
5895 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5896 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5898 PERROR("snprintf 64-bit consumer error socket path");
5900 goto exit_init_data
;
5902 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5903 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5905 PERROR("snprintf 64-bit consumer command socket path");
5907 goto exit_init_data
;
5910 DBG2("UST consumer 64 bits err path: %s",
5911 ustconsumer64_data
.err_unix_sock_path
);
5912 DBG2("UST consumer 64 bits cmd path: %s",
5913 ustconsumer64_data
.cmd_unix_sock_path
);
5916 * See if daemon already exist.
5918 if (check_existing_daemon()) {
5919 ERR("Already running daemon.\n");
5921 * We do not goto exit because we must not cleanup()
5922 * because a daemon is already running.
5925 goto exit_init_data
;
5929 * Init UST app hash table. Alloc hash table before this point since
5930 * cleanup() can get called after that point.
5932 if (ust_app_ht_alloc()) {
5933 ERR("Failed to allocate UST app hash table");
5935 goto exit_init_data
;
5939 * Initialize agent app hash table. We allocate the hash table here
5940 * since cleanup() can get called after this point.
5942 if (agent_app_ht_alloc()) {
5943 ERR("Failed to allocate Agent app hash table");
5945 goto exit_init_data
;
5949 * These actions must be executed as root. We do that *after* setting up
5950 * the sockets path because we MUST make the check for another daemon using
5951 * those paths *before* trying to set the kernel consumer sockets and init
5955 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5957 goto exit_init_data
;
5960 /* Setup kernel tracer */
5961 if (!opt_no_kernel
) {
5962 init_kernel_tracer();
5963 if (kernel_tracer_fd
>= 0) {
5964 ret
= syscall_init_table();
5966 ERR("Unable to populate syscall table. "
5967 "Syscall tracing won't work "
5968 "for this session daemon.");
5973 /* Set ulimit for open files */
5976 /* init lttng_fd tracking must be done after set_ulimit. */
5979 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5981 goto exit_init_data
;
5984 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5986 goto exit_init_data
;
5989 /* Setup the needed unix socket */
5990 if (init_daemon_socket()) {
5992 goto exit_init_data
;
5995 /* Set credentials to socket */
5996 if (is_root
&& set_permissions(rundir
)) {
5998 goto exit_init_data
;
6001 /* Get parent pid if -S, --sig-parent is specified. */
6002 if (opt_sig_parent
) {
6006 /* Setup the kernel pipe for waking up the kernel thread */
6007 if (is_root
&& !opt_no_kernel
) {
6008 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6010 goto exit_init_data
;
6014 /* Setup the thread apps communication pipe. */
6015 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6017 goto exit_init_data
;
6020 /* Setup the thread apps notify communication pipe. */
6021 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6023 goto exit_init_data
;
6026 /* Initialize global buffer per UID and PID registry. */
6027 buffer_reg_init_uid_registry();
6028 buffer_reg_init_pid_registry();
6030 /* Init UST command queue. */
6031 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6034 * Get session list pointer. This pointer MUST NOT be free'd. This list
6035 * is statically declared in session.c
6037 session_list_ptr
= session_get_list();
6041 /* Check for the application socket timeout env variable. */
6042 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6043 if (env_app_timeout
) {
6044 app_socket_timeout
= atoi(env_app_timeout
);
6046 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6049 ret
= write_pidfile();
6051 ERR("Error in write_pidfile");
6053 goto exit_init_data
;
6055 ret
= write_agent_port();
6057 ERR("Error in write_agent_port");
6059 goto exit_init_data
;
6062 /* Initialize communication library */
6064 /* Initialize TCP timeout values */
6065 lttcomm_inet_init();
6067 if (load_session_init_data(&load_info
) < 0) {
6069 goto exit_init_data
;
6071 load_info
->path
= opt_load_session_path
;
6073 /* Create health-check thread */
6074 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6075 thread_manage_health
, (void *) NULL
);
6078 PERROR("pthread_create health");
6083 /* Create thread to manage the client socket */
6084 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6085 thread_manage_clients
, (void *) NULL
);
6088 PERROR("pthread_create clients");
6093 /* Create thread to dispatch registration */
6094 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6095 thread_dispatch_ust_registration
, (void *) NULL
);
6098 PERROR("pthread_create dispatch");
6103 /* Create thread to manage application registration. */
6104 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6105 thread_registration_apps
, (void *) NULL
);
6108 PERROR("pthread_create registration");
6113 /* Create thread to manage application socket */
6114 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6115 thread_manage_apps
, (void *) NULL
);
6118 PERROR("pthread_create apps");
6123 /* Create thread to manage application notify socket */
6124 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6125 ust_thread_manage_notify
, (void *) NULL
);
6128 PERROR("pthread_create notify");
6130 goto exit_apps_notify
;
6133 /* Create agent registration thread. */
6134 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6135 agent_thread_manage_registration
, (void *) NULL
);
6138 PERROR("pthread_create agent");
6140 goto exit_agent_reg
;
6143 /* Don't start this thread if kernel tracing is not requested nor root */
6144 if (is_root
&& !opt_no_kernel
) {
6145 /* Create kernel thread to manage kernel event */
6146 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6147 thread_manage_kernel
, (void *) NULL
);
6150 PERROR("pthread_create kernel");
6156 /* Create session loading thread. */
6157 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6158 thread_load_session
, load_info
);
6161 PERROR("pthread_create load_session_thread");
6163 goto exit_load_session
;
6167 * This is where we start awaiting program completion (e.g. through
6168 * signal that asks threads to teardown).
6171 ret
= pthread_join(load_session_thread
, &status
);
6174 PERROR("pthread_join load_session_thread");
6179 if (is_root
&& !opt_no_kernel
) {
6180 ret
= pthread_join(kernel_thread
, &status
);
6183 PERROR("pthread_join");
6189 ret
= pthread_join(agent_reg_thread
, &status
);
6192 PERROR("pthread_join agent");
6197 ret
= pthread_join(apps_notify_thread
, &status
);
6200 PERROR("pthread_join apps notify");
6205 ret
= pthread_join(apps_thread
, &status
);
6208 PERROR("pthread_join apps");
6213 ret
= pthread_join(reg_apps_thread
, &status
);
6216 PERROR("pthread_join");
6222 * Join dispatch thread after joining reg_apps_thread to ensure
6223 * we don't leak applications in the queue.
6225 ret
= pthread_join(dispatch_thread
, &status
);
6228 PERROR("pthread_join");
6233 ret
= pthread_join(client_thread
, &status
);
6236 PERROR("pthread_join");
6241 ret
= pthread_join(health_thread
, &status
);
6244 PERROR("pthread_join health thread");
6251 * Wait for all pending call_rcu work to complete before tearing
6252 * down data structures. call_rcu worker may be trying to
6253 * perform lookups in those structures.
6257 * sessiond_cleanup() is called when no other thread is running, except
6258 * the ht_cleanup thread, which is needed to destroy the hash tables.
6260 rcu_thread_online();
6262 rcu_thread_offline();
6263 rcu_unregister_thread();
6266 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6267 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6268 * the queue is empty before shutting down the clean-up thread.
6272 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6278 health_app_destroy(health_sessiond
);
6279 exit_health_sessiond_cleanup
:
6280 exit_create_run_as_worker_cleanup
:
6283 sessiond_cleanup_options();
6285 exit_set_signal_handler
: