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.
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <urcu/uatomic.h>
43 #include <common/common.h>
44 #include <common/compat/socket.h>
45 #include <common/compat/getenv.h>
46 #include <common/defaults.h>
47 #include <common/kernel-consumer/kernel-consumer.h>
48 #include <common/futex.h>
49 #include <common/relayd/relayd.h>
50 #include <common/utils.h>
51 #include <common/daemonize.h>
52 #include <common/config/config.h>
54 #include "lttng-sessiond.h"
55 #include "buffer-registry.h"
62 #include "kernel-consumer.h"
66 #include "ust-consumer.h"
69 #include "health-sessiond.h"
70 #include "testpoint.h"
71 #include "ust-thread.h"
72 #include "agent-thread.h"
74 #include "load-session-thread.h"
77 #define CONSUMERD_FILE "lttng-consumerd"
80 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
81 static int tracing_group_name_override
;
82 static char *opt_pidfile
;
83 static int opt_sig_parent
;
84 static int opt_verbose_consumer
;
85 static int opt_daemon
, opt_background
;
86 static int opt_no_kernel
;
87 static char *opt_load_session_path
;
88 static pid_t ppid
; /* Parent PID for --sig-parent option */
89 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
91 static int lockfile_fd
= -1;
93 /* Set to 1 when a SIGUSR1 signal is received. */
94 static int recv_child_signal
;
97 * Consumer daemon specific control data. Every value not initialized here is
98 * set to 0 by the static definition.
100 static struct consumer_data kconsumer_data
= {
101 .type
= LTTNG_CONSUMER_KERNEL
,
102 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
103 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
106 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 .lock
= PTHREAD_MUTEX_INITIALIZER
,
108 .cond
= PTHREAD_COND_INITIALIZER
,
109 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 static struct consumer_data ustconsumer64_data
= {
112 .type
= LTTNG_CONSUMER64_UST
,
113 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
114 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
117 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 .lock
= PTHREAD_MUTEX_INITIALIZER
,
119 .cond
= PTHREAD_COND_INITIALIZER
,
120 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
122 static struct consumer_data ustconsumer32_data
= {
123 .type
= LTTNG_CONSUMER32_UST
,
124 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
125 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
128 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
129 .lock
= PTHREAD_MUTEX_INITIALIZER
,
130 .cond
= PTHREAD_COND_INITIALIZER
,
131 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
134 /* Command line options */
135 static const struct option long_options
[] = {
136 { "client-sock", 1, 0, 'c' },
137 { "apps-sock", 1, 0, 'a' },
138 { "kconsumerd-cmd-sock", 1, 0, '\0' },
139 { "kconsumerd-err-sock", 1, 0, '\0' },
140 { "ustconsumerd32-cmd-sock", 1, 0, '\0' },
141 { "ustconsumerd32-err-sock", 1, 0, '\0' },
142 { "ustconsumerd64-cmd-sock", 1, 0, '\0' },
143 { "ustconsumerd64-err-sock", 1, 0, '\0' },
144 { "consumerd32-path", 1, 0, '\0' },
145 { "consumerd32-libdir", 1, 0, '\0' },
146 { "consumerd64-path", 1, 0, '\0' },
147 { "consumerd64-libdir", 1, 0, '\0' },
148 { "daemonize", 0, 0, 'd' },
149 { "background", 0, 0, 'b' },
150 { "sig-parent", 0, 0, 'S' },
151 { "help", 0, 0, 'h' },
152 { "group", 1, 0, 'g' },
153 { "version", 0, 0, 'V' },
154 { "quiet", 0, 0, 'q' },
155 { "verbose", 0, 0, 'v' },
156 { "verbose-consumer", 0, 0, '\0' },
157 { "no-kernel", 0, 0, '\0' },
158 { "pidfile", 1, 0, 'p' },
159 { "agent-tcp-port", 1, 0, '\0' },
160 { "config", 1, 0, 'f' },
161 { "load", 1, 0, 'l' },
162 { "kmod-probes", 1, 0, '\0' },
163 { "extra-kmod-probes", 1, 0, '\0' },
167 /* Command line options to ignore from configuration file */
168 static const char *config_ignore_options
[] = { "help", "version", "config" };
170 /* Shared between threads */
171 static int dispatch_thread_exit
;
173 /* Global application Unix socket path */
174 static char apps_unix_sock_path
[PATH_MAX
];
175 /* Global client Unix socket path */
176 static char client_unix_sock_path
[PATH_MAX
];
177 /* global wait shm path for UST */
178 static char wait_shm_path
[PATH_MAX
];
179 /* Global health check unix path */
180 static char health_unix_sock_path
[PATH_MAX
];
182 /* Sockets and FDs */
183 static int client_sock
= -1;
184 static int apps_sock
= -1;
185 int kernel_tracer_fd
= -1;
186 static int kernel_poll_pipe
[2] = { -1, -1 };
189 * Quit pipe for all threads. This permits a single cancellation point
190 * for all threads when receiving an event on the pipe.
192 static int thread_quit_pipe
[2] = { -1, -1 };
193 static int ht_cleanup_quit_pipe
[2] = { -1, -1 };
196 * This pipe is used to inform the thread managing application communication
197 * that a command is queued and ready to be processed.
199 static int apps_cmd_pipe
[2] = { -1, -1 };
201 int apps_cmd_notify_pipe
[2] = { -1, -1 };
203 /* Pthread, Mutexes and Semaphores */
204 static pthread_t apps_thread
;
205 static pthread_t apps_notify_thread
;
206 static pthread_t reg_apps_thread
;
207 static pthread_t client_thread
;
208 static pthread_t kernel_thread
;
209 static pthread_t dispatch_thread
;
210 static pthread_t health_thread
;
211 static pthread_t ht_cleanup_thread
;
212 static pthread_t agent_reg_thread
;
213 static pthread_t load_session_thread
;
216 * UST registration command queue. This queue is tied with a futex and uses a N
217 * wakers / 1 waiter implemented and detailed in futex.c/.h
219 * The thread_registration_apps and thread_dispatch_ust_registration uses this
220 * queue along with the wait/wake scheme. The thread_manage_apps receives down
221 * the line new application socket and monitors it for any I/O error or clean
222 * close that triggers an unregistration of the application.
224 static struct ust_cmd_queue ust_cmd_queue
;
227 * Pointer initialized before thread creation.
229 * This points to the tracing session list containing the session count and a
230 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
231 * MUST NOT be taken if you call a public function in session.c.
233 * The lock is nested inside the structure: session_list_ptr->lock. Please use
234 * session_lock_list and session_unlock_list for lock acquisition.
236 static struct ltt_session_list
*session_list_ptr
;
238 int ust_consumerd64_fd
= -1;
239 int ust_consumerd32_fd
= -1;
241 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
242 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
243 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
244 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
245 static int consumerd32_bin_override
;
246 static int consumerd64_bin_override
;
247 static int consumerd32_libdir_override
;
248 static int consumerd64_libdir_override
;
250 static const char *module_proc_lttng
= "/proc/lttng";
253 * Consumer daemon state which is changed when spawning it, killing it or in
254 * case of a fatal error.
256 enum consumerd_state
{
257 CONSUMER_STARTED
= 1,
258 CONSUMER_STOPPED
= 2,
263 * This consumer daemon state is used to validate if a client command will be
264 * able to reach the consumer. If not, the client is informed. For instance,
265 * doing a "lttng start" when the consumer state is set to ERROR will return an
266 * error to the client.
268 * The following example shows a possible race condition of this scheme:
270 * consumer thread error happens
272 * client cmd checks state -> still OK
273 * consumer thread exit, sets error
274 * client cmd try to talk to consumer
277 * However, since the consumer is a different daemon, we have no way of making
278 * sure the command will reach it safely even with this state flag. This is why
279 * we consider that up to the state validation during command processing, the
280 * command is safe. After that, we can not guarantee the correctness of the
281 * client request vis-a-vis the consumer.
283 static enum consumerd_state ust_consumerd_state
;
284 static enum consumerd_state kernel_consumerd_state
;
287 * Socket timeout for receiving and sending in seconds.
289 static int app_socket_timeout
;
291 /* Set in main() with the current page size. */
294 /* Application health monitoring */
295 struct health_app
*health_sessiond
;
297 /* Agent TCP port for registration. Used by the agent thread. */
298 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
300 /* Am I root or not. */
301 int is_root
; /* Set to 1 if the daemon is running as root */
303 const char * const config_section_name
= "sessiond";
305 /* Load session thread information to operate. */
306 struct load_session_thread_data
*load_info
;
309 * Whether sessiond is ready for commands/health check requests.
310 * NR_LTTNG_SESSIOND_READY must match the number of calls to
311 * sessiond_notify_ready().
313 #define NR_LTTNG_SESSIOND_READY 3
314 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
316 /* Notify parents that we are ready for cmd and health check */
318 void sessiond_notify_ready(void)
320 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
322 * Notify parent pid that we are ready to accept command
323 * for client side. This ppid is the one from the
324 * external process that spawned us.
326 if (opt_sig_parent
) {
331 * Notify the parent of the fork() process that we are
334 if (opt_daemon
|| opt_background
) {
335 kill(child_ppid
, SIGUSR1
);
341 void setup_consumerd_path(void)
343 const char *bin
, *libdir
;
346 * Allow INSTALL_BIN_PATH to be used as a target path for the
347 * native architecture size consumer if CONFIG_CONSUMER*_PATH
348 * has not been defined.
350 #if (CAA_BITS_PER_LONG == 32)
351 if (!consumerd32_bin
[0]) {
352 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
354 if (!consumerd32_libdir
[0]) {
355 consumerd32_libdir
= INSTALL_LIB_PATH
;
357 #elif (CAA_BITS_PER_LONG == 64)
358 if (!consumerd64_bin
[0]) {
359 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
361 if (!consumerd64_libdir
[0]) {
362 consumerd64_libdir
= INSTALL_LIB_PATH
;
365 #error "Unknown bitness"
369 * runtime env. var. overrides the build default.
371 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
373 consumerd32_bin
= bin
;
375 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
377 consumerd64_bin
= bin
;
379 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
381 consumerd32_libdir
= libdir
;
383 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
385 consumerd64_libdir
= libdir
;
390 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
397 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
403 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
415 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
417 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
419 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
423 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
425 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event
*events
,
428 return __sessiond_set_thread_pollset(events
, size
,
429 ht_cleanup_quit_pipe
);
433 int __sessiond_check_thread_quit_pipe(int fd
, uint32_t events
, int a_pipe
)
435 if (fd
== a_pipe
&& (events
& LPOLLIN
)) {
442 * Check if the thread quit pipe was triggered.
444 * Return 1 if it was triggered else 0;
446 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
448 return __sessiond_check_thread_quit_pipe(fd
, events
,
449 thread_quit_pipe
[0]);
453 * Check if the ht_cleanup thread quit pipe was triggered.
455 * Return 1 if it was triggered else 0;
457 int sessiond_check_ht_cleanup_quit(int fd
, uint32_t events
)
459 return __sessiond_check_thread_quit_pipe(fd
, events
,
460 ht_cleanup_quit_pipe
[0]);
464 * Init thread quit pipe.
466 * Return -1 on error or 0 if all pipes are created.
468 static int __init_thread_quit_pipe(int *a_pipe
)
474 PERROR("thread quit pipe");
478 for (i
= 0; i
< 2; i
++) {
479 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
490 static int init_thread_quit_pipe(void)
492 return __init_thread_quit_pipe(thread_quit_pipe
);
495 static int init_ht_cleanup_quit_pipe(void)
497 return __init_thread_quit_pipe(ht_cleanup_quit_pipe
);
501 * Stop all threads by closing the thread quit pipe.
503 static void stop_threads(void)
507 /* Stopping all threads */
508 DBG("Terminating all threads");
509 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
511 ERR("write error on thread quit pipe");
514 /* Dispatch thread */
515 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
516 futex_nto1_wake(&ust_cmd_queue
.futex
);
520 * Close every consumer sockets.
522 static void close_consumer_sockets(void)
526 if (kconsumer_data
.err_sock
>= 0) {
527 ret
= close(kconsumer_data
.err_sock
);
529 PERROR("kernel consumer err_sock close");
532 if (ustconsumer32_data
.err_sock
>= 0) {
533 ret
= close(ustconsumer32_data
.err_sock
);
535 PERROR("UST consumerd32 err_sock close");
538 if (ustconsumer64_data
.err_sock
>= 0) {
539 ret
= close(ustconsumer64_data
.err_sock
);
541 PERROR("UST consumerd64 err_sock close");
544 if (kconsumer_data
.cmd_sock
>= 0) {
545 ret
= close(kconsumer_data
.cmd_sock
);
547 PERROR("kernel consumer cmd_sock close");
550 if (ustconsumer32_data
.cmd_sock
>= 0) {
551 ret
= close(ustconsumer32_data
.cmd_sock
);
553 PERROR("UST consumerd32 cmd_sock close");
556 if (ustconsumer64_data
.cmd_sock
>= 0) {
557 ret
= close(ustconsumer64_data
.cmd_sock
);
559 PERROR("UST consumerd64 cmd_sock close");
565 * Generate the full lock file path using the rundir.
567 * Return the snprintf() return value thus a negative value is an error.
569 static int generate_lock_file_path(char *path
, size_t len
)
576 /* Build lockfile path from rundir. */
577 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
579 PERROR("snprintf lockfile path");
586 * Cleanup the session daemon's data structures.
588 static void sessiond_cleanup(void)
591 struct ltt_session
*sess
, *stmp
;
594 DBG("Cleanup sessiond");
597 * Close the thread quit pipe. It has already done its job,
598 * since we are now called.
600 utils_close_pipe(thread_quit_pipe
);
603 * If opt_pidfile is undefined, the default file will be wiped when
604 * removing the rundir.
607 ret
= remove(opt_pidfile
);
609 PERROR("remove pidfile %s", opt_pidfile
);
613 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
616 snprintf(path
, PATH_MAX
,
618 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
619 DBG("Removing %s", path
);
622 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
623 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
624 DBG("Removing %s", path
);
628 snprintf(path
, PATH_MAX
,
629 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
631 DBG("Removing %s", path
);
634 snprintf(path
, PATH_MAX
,
635 DEFAULT_KCONSUMERD_PATH
,
637 DBG("Removing directory %s", path
);
640 /* ust consumerd 32 */
641 snprintf(path
, PATH_MAX
,
642 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
644 DBG("Removing %s", path
);
647 snprintf(path
, PATH_MAX
,
648 DEFAULT_USTCONSUMERD32_PATH
,
650 DBG("Removing directory %s", path
);
653 /* ust consumerd 64 */
654 snprintf(path
, PATH_MAX
,
655 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
657 DBG("Removing %s", path
);
660 snprintf(path
, PATH_MAX
,
661 DEFAULT_USTCONSUMERD64_PATH
,
663 DBG("Removing directory %s", path
);
666 DBG("Cleaning up all sessions");
668 /* Destroy session list mutex */
669 if (session_list_ptr
!= NULL
) {
670 pthread_mutex_destroy(&session_list_ptr
->lock
);
672 /* Cleanup ALL session */
673 cds_list_for_each_entry_safe(sess
, stmp
,
674 &session_list_ptr
->head
, list
) {
675 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
679 DBG("Closing all UST sockets");
680 ust_app_clean_list();
681 buffer_reg_destroy_registries();
683 if (is_root
&& !opt_no_kernel
) {
684 DBG2("Closing kernel fd");
685 if (kernel_tracer_fd
>= 0) {
686 ret
= close(kernel_tracer_fd
);
691 DBG("Unloading kernel modules");
692 modprobe_remove_lttng_all();
696 close_consumer_sockets();
699 load_session_destroy_data(load_info
);
704 * Cleanup lock file by deleting it and finaly closing it which will
705 * release the file system lock.
707 if (lockfile_fd
>= 0) {
708 char lockfile_path
[PATH_MAX
];
710 ret
= generate_lock_file_path(lockfile_path
,
711 sizeof(lockfile_path
));
713 ret
= remove(lockfile_path
);
715 PERROR("remove lock file");
717 ret
= close(lockfile_fd
);
719 PERROR("close lock file");
725 * We do NOT rmdir rundir because there are other processes
726 * using it, for instance lttng-relayd, which can start in
727 * parallel with this teardown.
734 * Cleanup the daemon's option data structures.
736 static void sessiond_cleanup_options(void)
738 DBG("Cleaning up options");
741 * If the override option is set, the pointer points to a *non* const
742 * thus freeing it even though the variable type is set to const.
744 if (tracing_group_name_override
) {
745 free((void *) tracing_group_name
);
747 if (consumerd32_bin_override
) {
748 free((void *) consumerd32_bin
);
750 if (consumerd64_bin_override
) {
751 free((void *) consumerd64_bin
);
753 if (consumerd32_libdir_override
) {
754 free((void *) consumerd32_libdir
);
756 if (consumerd64_libdir_override
) {
757 free((void *) consumerd64_libdir
);
761 free(opt_load_session_path
);
762 free(kmod_probes_list
);
763 free(kmod_extra_probes_list
);
766 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
767 "Matthew, BEET driven development works!%c[%dm",
768 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
773 * Send data on a unix socket using the liblttsessiondcomm API.
775 * Return lttcomm error code.
777 static int send_unix_sock(int sock
, void *buf
, size_t len
)
779 /* Check valid length */
784 return lttcomm_send_unix_sock(sock
, buf
, len
);
788 * Free memory of a command context structure.
790 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
792 DBG("Clean command context structure");
794 if ((*cmd_ctx
)->llm
) {
795 free((*cmd_ctx
)->llm
);
797 if ((*cmd_ctx
)->lsm
) {
798 free((*cmd_ctx
)->lsm
);
806 * Notify UST applications using the shm mmap futex.
808 static int notify_ust_apps(int active
)
812 DBG("Notifying applications of session daemon state: %d", active
);
814 /* See shm.c for this call implying mmap, shm and futex calls */
815 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
816 if (wait_shm_mmap
== NULL
) {
820 /* Wake waiting process */
821 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
823 /* Apps notified successfully */
831 * Setup the outgoing data buffer for the response (llm) by allocating the
832 * right amount of memory and copying the original information from the lsm
835 * Return total size of the buffer pointed by buf.
837 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
843 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
844 if (cmd_ctx
->llm
== NULL
) {
850 /* Copy common data */
851 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
852 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
854 cmd_ctx
->llm
->data_size
= size
;
855 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
864 * Update the kernel poll set of all channel fd available over all tracing
865 * session. Add the wakeup pipe at the end of the set.
867 static int update_kernel_poll(struct lttng_poll_event
*events
)
870 struct ltt_session
*session
;
871 struct ltt_kernel_channel
*channel
;
873 DBG("Updating kernel poll set");
876 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
877 session_lock(session
);
878 if (session
->kernel_session
== NULL
) {
879 session_unlock(session
);
883 cds_list_for_each_entry(channel
,
884 &session
->kernel_session
->channel_list
.head
, list
) {
885 /* Add channel fd to the kernel poll set */
886 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
888 session_unlock(session
);
891 DBG("Channel fd %d added to kernel set", channel
->fd
);
893 session_unlock(session
);
895 session_unlock_list();
900 session_unlock_list();
905 * Find the channel fd from 'fd' over all tracing session. When found, check
906 * for new channel stream and send those stream fds to the kernel consumer.
908 * Useful for CPU hotplug feature.
910 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
913 struct ltt_session
*session
;
914 struct ltt_kernel_session
*ksess
;
915 struct ltt_kernel_channel
*channel
;
917 DBG("Updating kernel streams for channel fd %d", fd
);
920 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
921 session_lock(session
);
922 if (session
->kernel_session
== NULL
) {
923 session_unlock(session
);
926 ksess
= session
->kernel_session
;
928 cds_list_for_each_entry(channel
,
929 &ksess
->channel_list
.head
, list
) {
930 struct lttng_ht_iter iter
;
931 struct consumer_socket
*socket
;
933 if (channel
->fd
!= fd
) {
936 DBG("Channel found, updating kernel streams");
937 ret
= kernel_open_channel_stream(channel
);
941 /* Update the stream global counter */
942 ksess
->stream_count_global
+= ret
;
945 * Have we already sent fds to the consumer? If yes, it
946 * means that tracing is started so it is safe to send
947 * our updated stream fds.
949 if (ksess
->consumer_fds_sent
!= 1
950 || ksess
->consumer
== NULL
) {
956 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
957 &iter
.iter
, socket
, node
.node
) {
958 pthread_mutex_lock(socket
->lock
);
959 ret
= kernel_consumer_send_channel_stream(socket
,
961 session
->output_traces
? 1 : 0);
962 pthread_mutex_unlock(socket
->lock
);
970 session_unlock(session
);
972 session_unlock_list();
976 session_unlock(session
);
977 session_unlock_list();
982 * For each tracing session, update newly registered apps. The session list
983 * lock MUST be acquired before calling this.
985 static void update_ust_app(int app_sock
)
987 struct ltt_session
*sess
, *stmp
;
989 /* Consumer is in an ERROR state. Stop any application update. */
990 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
991 /* Stop the update process since the consumer is dead. */
995 /* For all tracing session(s) */
996 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1000 if (!sess
->ust_session
) {
1001 goto unlock_session
;
1005 assert(app_sock
>= 0);
1006 app
= ust_app_find_by_sock(app_sock
);
1009 * Application can be unregistered before so
1010 * this is possible hence simply stopping the
1013 DBG3("UST app update failed to find app sock %d",
1017 ust_app_global_update(sess
->ust_session
, app
);
1021 session_unlock(sess
);
1026 * This thread manage event coming from the kernel.
1028 * Features supported in this thread:
1031 static void *thread_manage_kernel(void *data
)
1033 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1034 uint32_t revents
, nb_fd
;
1036 struct lttng_poll_event events
;
1038 DBG("[thread] Thread manage kernel started");
1040 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1043 * This first step of the while is to clean this structure which could free
1044 * non NULL pointers so initialize it before the loop.
1046 lttng_poll_init(&events
);
1048 if (testpoint(sessiond_thread_manage_kernel
)) {
1049 goto error_testpoint
;
1052 health_code_update();
1054 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1055 goto error_testpoint
;
1059 health_code_update();
1061 if (update_poll_flag
== 1) {
1062 /* Clean events object. We are about to populate it again. */
1063 lttng_poll_clean(&events
);
1065 ret
= sessiond_set_thread_pollset(&events
, 2);
1067 goto error_poll_create
;
1070 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1075 /* This will add the available kernel channel if any. */
1076 ret
= update_kernel_poll(&events
);
1080 update_poll_flag
= 0;
1083 DBG("Thread kernel polling");
1085 /* Poll infinite value of time */
1087 health_poll_entry();
1088 ret
= lttng_poll_wait(&events
, -1);
1089 DBG("Thread kernel return from poll on %d fds",
1090 LTTNG_POLL_GETNB(&events
));
1094 * Restart interrupted system call.
1096 if (errno
== EINTR
) {
1100 } else if (ret
== 0) {
1101 /* Should not happen since timeout is infinite */
1102 ERR("Return value of poll is 0 with an infinite timeout.\n"
1103 "This should not have happened! Continuing...");
1109 for (i
= 0; i
< nb_fd
; i
++) {
1110 /* Fetch once the poll data */
1111 revents
= LTTNG_POLL_GETEV(&events
, i
);
1112 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1114 health_code_update();
1117 /* No activity for this FD (poll implementation). */
1121 /* Thread quit pipe has been closed. Killing thread. */
1122 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1128 /* Check for data on kernel pipe */
1129 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
1130 (void) lttng_read(kernel_poll_pipe
[0],
1133 * Ret value is useless here, if this pipe gets any actions an
1134 * update is required anyway.
1136 update_poll_flag
= 1;
1140 * New CPU detected by the kernel. Adding kernel stream to
1141 * kernel session and updating the kernel consumer
1143 if (revents
& LPOLLIN
) {
1144 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1150 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1151 * and unregister kernel stream at this point.
1160 lttng_poll_clean(&events
);
1163 utils_close_pipe(kernel_poll_pipe
);
1164 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1167 ERR("Health error occurred in %s", __func__
);
1168 WARN("Kernel thread died unexpectedly. "
1169 "Kernel tracing can continue but CPU hotplug is disabled.");
1171 health_unregister(health_sessiond
);
1172 DBG("Kernel thread dying");
1177 * Signal pthread condition of the consumer data that the thread.
1179 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1181 pthread_mutex_lock(&data
->cond_mutex
);
1184 * The state is set before signaling. It can be any value, it's the waiter
1185 * job to correctly interpret this condition variable associated to the
1186 * consumer pthread_cond.
1188 * A value of 0 means that the corresponding thread of the consumer data
1189 * was not started. 1 indicates that the thread has started and is ready
1190 * for action. A negative value means that there was an error during the
1193 data
->consumer_thread_is_ready
= state
;
1194 (void) pthread_cond_signal(&data
->cond
);
1196 pthread_mutex_unlock(&data
->cond_mutex
);
1200 * This thread manage the consumer error sent back to the session daemon.
1202 static void *thread_manage_consumer(void *data
)
1204 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1205 uint32_t revents
, nb_fd
;
1206 enum lttcomm_return_code code
;
1207 struct lttng_poll_event events
;
1208 struct consumer_data
*consumer_data
= data
;
1210 DBG("[thread] Manage consumer started");
1212 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1214 health_code_update();
1217 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1218 * metadata_sock. Nothing more will be added to this poll set.
1220 ret
= sessiond_set_thread_pollset(&events
, 3);
1226 * The error socket here is already in a listening state which was done
1227 * just before spawning this thread to avoid a race between the consumer
1228 * daemon exec trying to connect and the listen() call.
1230 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1235 health_code_update();
1237 /* Infinite blocking call, waiting for transmission */
1239 health_poll_entry();
1241 if (testpoint(sessiond_thread_manage_consumer
)) {
1245 ret
= lttng_poll_wait(&events
, -1);
1249 * Restart interrupted system call.
1251 if (errno
== EINTR
) {
1259 for (i
= 0; i
< nb_fd
; i
++) {
1260 /* Fetch once the poll data */
1261 revents
= LTTNG_POLL_GETEV(&events
, i
);
1262 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1264 health_code_update();
1267 /* No activity for this FD (poll implementation). */
1271 /* Thread quit pipe has been closed. Killing thread. */
1272 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1278 /* Event on the registration socket */
1279 if (pollfd
== consumer_data
->err_sock
) {
1280 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1281 ERR("consumer err socket poll error");
1287 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1293 * Set the CLOEXEC flag. Return code is useless because either way, the
1296 (void) utils_set_fd_cloexec(sock
);
1298 health_code_update();
1300 DBG2("Receiving code from consumer err_sock");
1302 /* Getting status code from kconsumerd */
1303 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1304 sizeof(enum lttcomm_return_code
));
1309 health_code_update();
1310 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1311 /* Connect both socket, command and metadata. */
1312 consumer_data
->cmd_sock
=
1313 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1314 consumer_data
->metadata_fd
=
1315 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1316 if (consumer_data
->cmd_sock
< 0
1317 || consumer_data
->metadata_fd
< 0) {
1318 PERROR("consumer connect cmd socket");
1319 /* On error, signal condition and quit. */
1320 signal_consumer_condition(consumer_data
, -1);
1323 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1324 /* Create metadata socket lock. */
1325 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1326 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1327 PERROR("zmalloc pthread mutex");
1331 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1333 signal_consumer_condition(consumer_data
, 1);
1334 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1335 DBG("Consumer metadata socket ready (fd: %d)",
1336 consumer_data
->metadata_fd
);
1338 ERR("consumer error when waiting for SOCK_READY : %s",
1339 lttcomm_get_readable_code(-code
));
1343 /* Remove the consumerd error sock since we've established a connexion */
1344 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1349 /* Add new accepted error socket. */
1350 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1355 /* Add metadata socket that is successfully connected. */
1356 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1357 LPOLLIN
| LPOLLRDHUP
);
1362 health_code_update();
1364 /* Infinite blocking call, waiting for transmission */
1367 health_code_update();
1369 /* Exit the thread because the thread quit pipe has been triggered. */
1371 /* Not a health error. */
1376 health_poll_entry();
1377 ret
= lttng_poll_wait(&events
, -1);
1381 * Restart interrupted system call.
1383 if (errno
== EINTR
) {
1391 for (i
= 0; i
< nb_fd
; i
++) {
1392 /* Fetch once the poll data */
1393 revents
= LTTNG_POLL_GETEV(&events
, i
);
1394 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1396 health_code_update();
1399 /* No activity for this FD (poll implementation). */
1404 * Thread quit pipe has been triggered, flag that we should stop
1405 * but continue the current loop to handle potential data from
1408 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1410 if (pollfd
== sock
) {
1411 /* Event on the consumerd socket */
1412 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1413 ERR("consumer err socket second poll error");
1416 health_code_update();
1417 /* Wait for any kconsumerd error */
1418 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1419 sizeof(enum lttcomm_return_code
));
1421 ERR("consumer closed the command socket");
1425 ERR("consumer return code : %s",
1426 lttcomm_get_readable_code(-code
));
1429 } else if (pollfd
== consumer_data
->metadata_fd
) {
1430 /* UST metadata requests */
1431 ret
= ust_consumer_metadata_request(
1432 &consumer_data
->metadata_sock
);
1434 ERR("Handling metadata request");
1438 /* No need for an else branch all FDs are tested prior. */
1440 health_code_update();
1446 * We lock here because we are about to close the sockets and some other
1447 * thread might be using them so get exclusive access which will abort all
1448 * other consumer command by other threads.
1450 pthread_mutex_lock(&consumer_data
->lock
);
1452 /* Immediately set the consumerd state to stopped */
1453 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1454 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1455 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1456 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1457 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1459 /* Code flow error... */
1463 if (consumer_data
->err_sock
>= 0) {
1464 ret
= close(consumer_data
->err_sock
);
1468 consumer_data
->err_sock
= -1;
1470 if (consumer_data
->cmd_sock
>= 0) {
1471 ret
= close(consumer_data
->cmd_sock
);
1475 consumer_data
->cmd_sock
= -1;
1477 if (consumer_data
->metadata_sock
.fd_ptr
&&
1478 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1479 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1491 unlink(consumer_data
->err_unix_sock_path
);
1492 unlink(consumer_data
->cmd_unix_sock_path
);
1493 consumer_data
->pid
= 0;
1494 pthread_mutex_unlock(&consumer_data
->lock
);
1496 /* Cleanup metadata socket mutex. */
1497 if (consumer_data
->metadata_sock
.lock
) {
1498 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1499 free(consumer_data
->metadata_sock
.lock
);
1501 lttng_poll_clean(&events
);
1505 ERR("Health error occurred in %s", __func__
);
1507 health_unregister(health_sessiond
);
1508 DBG("consumer thread cleanup completed");
1514 * This thread manage application communication.
1516 static void *thread_manage_apps(void *data
)
1518 int i
, ret
, pollfd
, err
= -1;
1520 uint32_t revents
, nb_fd
;
1521 struct lttng_poll_event events
;
1523 DBG("[thread] Manage application started");
1525 rcu_register_thread();
1526 rcu_thread_online();
1528 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1530 if (testpoint(sessiond_thread_manage_apps
)) {
1531 goto error_testpoint
;
1534 health_code_update();
1536 ret
= sessiond_set_thread_pollset(&events
, 2);
1538 goto error_poll_create
;
1541 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1546 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1550 health_code_update();
1553 DBG("Apps thread polling");
1555 /* Inifinite blocking call, waiting for transmission */
1557 health_poll_entry();
1558 ret
= lttng_poll_wait(&events
, -1);
1559 DBG("Apps thread return from poll on %d fds",
1560 LTTNG_POLL_GETNB(&events
));
1564 * Restart interrupted system call.
1566 if (errno
== EINTR
) {
1574 for (i
= 0; i
< nb_fd
; i
++) {
1575 /* Fetch once the poll data */
1576 revents
= LTTNG_POLL_GETEV(&events
, i
);
1577 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1579 health_code_update();
1582 /* No activity for this FD (poll implementation). */
1586 /* Thread quit pipe has been closed. Killing thread. */
1587 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1593 /* Inspect the apps cmd pipe */
1594 if (pollfd
== apps_cmd_pipe
[0]) {
1595 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1596 ERR("Apps command pipe error");
1598 } else if (revents
& LPOLLIN
) {
1602 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1603 if (size_ret
< sizeof(sock
)) {
1604 PERROR("read apps cmd pipe");
1608 health_code_update();
1611 * We only monitor the error events of the socket. This
1612 * thread does not handle any incoming data from UST
1615 ret
= lttng_poll_add(&events
, sock
,
1616 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1621 DBG("Apps with sock %d added to poll set", sock
);
1625 * At this point, we know that a registered application made
1626 * the event at poll_wait.
1628 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1629 /* Removing from the poll set */
1630 ret
= lttng_poll_del(&events
, pollfd
);
1635 /* Socket closed on remote end. */
1636 ust_app_unregister(pollfd
);
1640 health_code_update();
1646 lttng_poll_clean(&events
);
1649 utils_close_pipe(apps_cmd_pipe
);
1650 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1653 * We don't clean the UST app hash table here since already registered
1654 * applications can still be controlled so let them be until the session
1655 * daemon dies or the applications stop.
1660 ERR("Health error occurred in %s", __func__
);
1662 health_unregister(health_sessiond
);
1663 DBG("Application communication apps thread cleanup complete");
1664 rcu_thread_offline();
1665 rcu_unregister_thread();
1670 * Send a socket to a thread This is called from the dispatch UST registration
1671 * thread once all sockets are set for the application.
1673 * The sock value can be invalid, we don't really care, the thread will handle
1674 * it and make the necessary cleanup if so.
1676 * On success, return 0 else a negative value being the errno message of the
1679 static int send_socket_to_thread(int fd
, int sock
)
1684 * It's possible that the FD is set as invalid with -1 concurrently just
1685 * before calling this function being a shutdown state of the thread.
1692 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1693 if (ret
< sizeof(sock
)) {
1694 PERROR("write apps pipe %d", fd
);
1701 /* All good. Don't send back the write positive ret value. */
1708 * Sanitize the wait queue of the dispatch registration thread meaning removing
1709 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1710 * notify socket is never received.
1712 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1714 int ret
, nb_fd
= 0, i
;
1715 unsigned int fd_added
= 0;
1716 struct lttng_poll_event events
;
1717 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1721 lttng_poll_init(&events
);
1723 /* Just skip everything for an empty queue. */
1724 if (!wait_queue
->count
) {
1728 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1733 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1734 &wait_queue
->head
, head
) {
1735 assert(wait_node
->app
);
1736 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1737 LPOLLHUP
| LPOLLERR
);
1750 * Poll but don't block so we can quickly identify the faulty events and
1751 * clean them afterwards from the wait queue.
1753 ret
= lttng_poll_wait(&events
, 0);
1759 for (i
= 0; i
< nb_fd
; i
++) {
1760 /* Get faulty FD. */
1761 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1762 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1765 /* No activity for this FD (poll implementation). */
1769 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1770 &wait_queue
->head
, head
) {
1771 if (pollfd
== wait_node
->app
->sock
&&
1772 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1773 cds_list_del(&wait_node
->head
);
1774 wait_queue
->count
--;
1775 ust_app_destroy(wait_node
->app
);
1783 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1787 lttng_poll_clean(&events
);
1791 lttng_poll_clean(&events
);
1793 ERR("Unable to sanitize wait queue");
1798 * Dispatch request from the registration threads to the application
1799 * communication thread.
1801 static void *thread_dispatch_ust_registration(void *data
)
1804 struct cds_wfcq_node
*node
;
1805 struct ust_command
*ust_cmd
= NULL
;
1806 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1807 struct ust_reg_wait_queue wait_queue
= {
1811 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1813 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1814 goto error_testpoint
;
1817 health_code_update();
1819 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1821 DBG("[thread] Dispatch UST command started");
1823 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1824 health_code_update();
1826 /* Atomically prepare the queue futex */
1827 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1830 struct ust_app
*app
= NULL
;
1834 * Make sure we don't have node(s) that have hung up before receiving
1835 * the notify socket. This is to clean the list in order to avoid
1836 * memory leaks from notify socket that are never seen.
1838 sanitize_wait_queue(&wait_queue
);
1840 health_code_update();
1841 /* Dequeue command for registration */
1842 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1844 DBG("Woken up but nothing in the UST command queue");
1845 /* Continue thread execution */
1849 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1851 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1852 " gid:%d sock:%d name:%s (version %d.%d)",
1853 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1854 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1855 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1856 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1858 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1859 wait_node
= zmalloc(sizeof(*wait_node
));
1861 PERROR("zmalloc wait_node dispatch");
1862 ret
= close(ust_cmd
->sock
);
1864 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1866 lttng_fd_put(LTTNG_FD_APPS
, 1);
1870 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1872 /* Create application object if socket is CMD. */
1873 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1875 if (!wait_node
->app
) {
1876 ret
= close(ust_cmd
->sock
);
1878 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1880 lttng_fd_put(LTTNG_FD_APPS
, 1);
1886 * Add application to the wait queue so we can set the notify
1887 * socket before putting this object in the global ht.
1889 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1894 * We have to continue here since we don't have the notify
1895 * socket and the application MUST be added to the hash table
1896 * only at that moment.
1901 * Look for the application in the local wait queue and set the
1902 * notify socket if found.
1904 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1905 &wait_queue
.head
, head
) {
1906 health_code_update();
1907 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1908 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1909 cds_list_del(&wait_node
->head
);
1911 app
= wait_node
->app
;
1913 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1919 * With no application at this stage the received socket is
1920 * basically useless so close it before we free the cmd data
1921 * structure for good.
1924 ret
= close(ust_cmd
->sock
);
1926 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1928 lttng_fd_put(LTTNG_FD_APPS
, 1);
1935 * @session_lock_list
1937 * Lock the global session list so from the register up to the
1938 * registration done message, no thread can see the application
1939 * and change its state.
1941 session_lock_list();
1945 * Add application to the global hash table. This needs to be
1946 * done before the update to the UST registry can locate the
1951 /* Set app version. This call will print an error if needed. */
1952 (void) ust_app_version(app
);
1954 /* Send notify socket through the notify pipe. */
1955 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1959 session_unlock_list();
1961 * No notify thread, stop the UST tracing. However, this is
1962 * not an internal error of the this thread thus setting
1963 * the health error code to a normal exit.
1970 * Update newly registered application with the tracing
1971 * registry info already enabled information.
1973 update_ust_app(app
->sock
);
1976 * Don't care about return value. Let the manage apps threads
1977 * handle app unregistration upon socket close.
1979 (void) ust_app_register_done(app
->sock
);
1982 * Even if the application socket has been closed, send the app
1983 * to the thread and unregistration will take place at that
1986 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1989 session_unlock_list();
1991 * No apps. thread, stop the UST tracing. However, this is
1992 * not an internal error of the this thread thus setting
1993 * the health error code to a normal exit.
2000 session_unlock_list();
2002 } while (node
!= NULL
);
2004 health_poll_entry();
2005 /* Futex wait on queue. Blocking call on futex() */
2006 futex_nto1_wait(&ust_cmd_queue
.futex
);
2009 /* Normal exit, no error */
2013 /* Clean up wait queue. */
2014 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2015 &wait_queue
.head
, head
) {
2016 cds_list_del(&wait_node
->head
);
2022 DBG("Dispatch thread dying");
2025 ERR("Health error occurred in %s", __func__
);
2027 health_unregister(health_sessiond
);
2032 * This thread manage application registration.
2034 static void *thread_registration_apps(void *data
)
2036 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2037 uint32_t revents
, nb_fd
;
2038 struct lttng_poll_event events
;
2040 * Get allocated in this thread, enqueued to a global queue, dequeued and
2041 * freed in the manage apps thread.
2043 struct ust_command
*ust_cmd
= NULL
;
2045 DBG("[thread] Manage application registration started");
2047 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2049 if (testpoint(sessiond_thread_registration_apps
)) {
2050 goto error_testpoint
;
2053 ret
= lttcomm_listen_unix_sock(apps_sock
);
2059 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2060 * more will be added to this poll set.
2062 ret
= sessiond_set_thread_pollset(&events
, 2);
2064 goto error_create_poll
;
2067 /* Add the application registration socket */
2068 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2070 goto error_poll_add
;
2073 /* Notify all applications to register */
2074 ret
= notify_ust_apps(1);
2076 ERR("Failed to notify applications or create the wait shared memory.\n"
2077 "Execution continues but there might be problem for already\n"
2078 "running applications that wishes to register.");
2082 DBG("Accepting application registration");
2084 /* Inifinite blocking call, waiting for transmission */
2086 health_poll_entry();
2087 ret
= lttng_poll_wait(&events
, -1);
2091 * Restart interrupted system call.
2093 if (errno
== EINTR
) {
2101 for (i
= 0; i
< nb_fd
; i
++) {
2102 health_code_update();
2104 /* Fetch once the poll data */
2105 revents
= LTTNG_POLL_GETEV(&events
, i
);
2106 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2109 /* No activity for this FD (poll implementation). */
2113 /* Thread quit pipe has been closed. Killing thread. */
2114 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2120 /* Event on the registration socket */
2121 if (pollfd
== apps_sock
) {
2122 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2123 ERR("Register apps socket poll error");
2125 } else if (revents
& LPOLLIN
) {
2126 sock
= lttcomm_accept_unix_sock(apps_sock
);
2132 * Set socket timeout for both receiving and ending.
2133 * app_socket_timeout is in seconds, whereas
2134 * lttcomm_setsockopt_rcv_timeout and
2135 * lttcomm_setsockopt_snd_timeout expect msec as
2138 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2139 app_socket_timeout
* 1000);
2140 (void) lttcomm_setsockopt_snd_timeout(sock
,
2141 app_socket_timeout
* 1000);
2144 * Set the CLOEXEC flag. Return code is useless because
2145 * either way, the show must go on.
2147 (void) utils_set_fd_cloexec(sock
);
2149 /* Create UST registration command for enqueuing */
2150 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2151 if (ust_cmd
== NULL
) {
2152 PERROR("ust command zmalloc");
2157 * Using message-based transmissions to ensure we don't
2158 * have to deal with partially received messages.
2160 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2162 ERR("Exhausted file descriptors allowed for applications.");
2172 health_code_update();
2173 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2176 /* Close socket of the application. */
2181 lttng_fd_put(LTTNG_FD_APPS
, 1);
2185 health_code_update();
2187 ust_cmd
->sock
= sock
;
2190 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2191 " gid:%d sock:%d name:%s (version %d.%d)",
2192 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2193 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2194 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2195 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2198 * Lock free enqueue the registration request. The red pill
2199 * has been taken! This apps will be part of the *system*.
2201 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2204 * Wake the registration queue futex. Implicit memory
2205 * barrier with the exchange in cds_wfcq_enqueue.
2207 futex_nto1_wake(&ust_cmd_queue
.futex
);
2215 /* Notify that the registration thread is gone */
2218 if (apps_sock
>= 0) {
2219 ret
= close(apps_sock
);
2229 lttng_fd_put(LTTNG_FD_APPS
, 1);
2231 unlink(apps_unix_sock_path
);
2234 lttng_poll_clean(&events
);
2238 DBG("UST Registration thread cleanup complete");
2241 ERR("Health error occurred in %s", __func__
);
2243 health_unregister(health_sessiond
);
2249 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2250 * exec or it will fails.
2252 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2255 struct timespec timeout
;
2257 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2258 consumer_data
->consumer_thread_is_ready
= 0;
2260 /* Setup pthread condition */
2261 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2264 PERROR("pthread_condattr_init consumer data");
2269 * Set the monotonic clock in order to make sure we DO NOT jump in time
2270 * between the clock_gettime() call and the timedwait call. See bug #324
2271 * for a more details and how we noticed it.
2273 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2276 PERROR("pthread_condattr_setclock consumer data");
2280 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2283 PERROR("pthread_cond_init consumer data");
2287 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2291 PERROR("pthread_create consumer");
2296 /* We are about to wait on a pthread condition */
2297 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2299 /* Get time for sem_timedwait absolute timeout */
2300 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2302 * Set the timeout for the condition timed wait even if the clock gettime
2303 * call fails since we might loop on that call and we want to avoid to
2304 * increment the timeout too many times.
2306 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2309 * The following loop COULD be skipped in some conditions so this is why we
2310 * set ret to 0 in order to make sure at least one round of the loop is
2316 * Loop until the condition is reached or when a timeout is reached. Note
2317 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2318 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2319 * possible. This loop does not take any chances and works with both of
2322 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2323 if (clock_ret
< 0) {
2324 PERROR("clock_gettime spawn consumer");
2325 /* Infinite wait for the consumerd thread to be ready */
2326 ret
= pthread_cond_wait(&consumer_data
->cond
,
2327 &consumer_data
->cond_mutex
);
2329 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2330 &consumer_data
->cond_mutex
, &timeout
);
2334 /* Release the pthread condition */
2335 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2339 if (ret
== ETIMEDOUT
) {
2343 * Call has timed out so we kill the kconsumerd_thread and return
2346 ERR("Condition timed out. The consumer thread was never ready."
2348 pth_ret
= pthread_cancel(consumer_data
->thread
);
2350 PERROR("pthread_cancel consumer thread");
2353 PERROR("pthread_cond_wait failed consumer thread");
2355 /* Caller is expecting a negative value on failure. */
2360 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2361 if (consumer_data
->pid
== 0) {
2362 ERR("Consumerd did not start");
2363 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2366 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2375 * Join consumer thread
2377 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2381 /* Consumer pid must be a real one. */
2382 if (consumer_data
->pid
> 0) {
2384 ret
= kill(consumer_data
->pid
, SIGTERM
);
2386 PERROR("Error killing consumer daemon");
2389 return pthread_join(consumer_data
->thread
, &status
);
2396 * Fork and exec a consumer daemon (consumerd).
2398 * Return pid if successful else -1.
2400 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2404 const char *consumer_to_use
;
2405 const char *verbosity
;
2408 DBG("Spawning consumerd");
2415 if (opt_verbose_consumer
) {
2416 verbosity
= "--verbose";
2417 } else if (lttng_opt_quiet
) {
2418 verbosity
= "--quiet";
2423 switch (consumer_data
->type
) {
2424 case LTTNG_CONSUMER_KERNEL
:
2426 * Find out which consumerd to execute. We will first try the
2427 * 64-bit path, then the sessiond's installation directory, and
2428 * fallback on the 32-bit one,
2430 DBG3("Looking for a kernel consumer at these locations:");
2431 DBG3(" 1) %s", consumerd64_bin
);
2432 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2433 DBG3(" 3) %s", consumerd32_bin
);
2434 if (stat(consumerd64_bin
, &st
) == 0) {
2435 DBG3("Found location #1");
2436 consumer_to_use
= consumerd64_bin
;
2437 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2438 DBG3("Found location #2");
2439 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2440 } else if (stat(consumerd32_bin
, &st
) == 0) {
2441 DBG3("Found location #3");
2442 consumer_to_use
= consumerd32_bin
;
2444 DBG("Could not find any valid consumerd executable");
2448 DBG("Using kernel consumer at: %s", consumer_to_use
);
2449 ret
= execl(consumer_to_use
,
2450 "lttng-consumerd", verbosity
, "-k",
2451 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2452 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2453 "--group", tracing_group_name
,
2456 case LTTNG_CONSUMER64_UST
:
2458 char *tmpnew
= NULL
;
2460 if (consumerd64_libdir
[0] != '\0') {
2464 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2468 tmplen
= strlen("LD_LIBRARY_PATH=")
2469 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2470 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2475 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2476 strcat(tmpnew
, consumerd64_libdir
);
2477 if (tmp
[0] != '\0') {
2478 strcat(tmpnew
, ":");
2479 strcat(tmpnew
, tmp
);
2481 ret
= putenv(tmpnew
);
2488 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2489 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2490 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2491 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2492 "--group", tracing_group_name
,
2494 if (consumerd64_libdir
[0] != '\0') {
2499 case LTTNG_CONSUMER32_UST
:
2501 char *tmpnew
= NULL
;
2503 if (consumerd32_libdir
[0] != '\0') {
2507 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2511 tmplen
= strlen("LD_LIBRARY_PATH=")
2512 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2513 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2518 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2519 strcat(tmpnew
, consumerd32_libdir
);
2520 if (tmp
[0] != '\0') {
2521 strcat(tmpnew
, ":");
2522 strcat(tmpnew
, tmp
);
2524 ret
= putenv(tmpnew
);
2531 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2532 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2533 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2534 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2535 "--group", tracing_group_name
,
2537 if (consumerd32_libdir
[0] != '\0') {
2543 PERROR("unknown consumer type");
2547 PERROR("Consumer execl()");
2549 /* Reaching this point, we got a failure on our execl(). */
2551 } else if (pid
> 0) {
2554 PERROR("start consumer fork");
2562 * Spawn the consumerd daemon and session daemon thread.
2564 static int start_consumerd(struct consumer_data
*consumer_data
)
2569 * Set the listen() state on the socket since there is a possible race
2570 * between the exec() of the consumer daemon and this call if place in the
2571 * consumer thread. See bug #366 for more details.
2573 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2578 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2579 if (consumer_data
->pid
!= 0) {
2580 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2584 ret
= spawn_consumerd(consumer_data
);
2586 ERR("Spawning consumerd failed");
2587 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2591 /* Setting up the consumer_data pid */
2592 consumer_data
->pid
= ret
;
2593 DBG2("Consumer pid %d", consumer_data
->pid
);
2594 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2596 DBG2("Spawning consumer control thread");
2597 ret
= spawn_consumer_thread(consumer_data
);
2599 ERR("Fatal error spawning consumer control thread");
2607 /* Cleanup already created sockets on error. */
2608 if (consumer_data
->err_sock
>= 0) {
2611 err
= close(consumer_data
->err_sock
);
2613 PERROR("close consumer data error socket");
2620 * Setup necessary data for kernel tracer action.
2622 static int init_kernel_tracer(void)
2626 /* Modprobe lttng kernel modules */
2627 ret
= modprobe_lttng_control();
2632 /* Open debugfs lttng */
2633 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2634 if (kernel_tracer_fd
< 0) {
2635 DBG("Failed to open %s", module_proc_lttng
);
2640 /* Validate kernel version */
2641 ret
= kernel_validate_version(kernel_tracer_fd
);
2646 ret
= modprobe_lttng_data();
2651 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2655 modprobe_remove_lttng_control();
2656 ret
= close(kernel_tracer_fd
);
2660 kernel_tracer_fd
= -1;
2661 return LTTNG_ERR_KERN_VERSION
;
2664 ret
= close(kernel_tracer_fd
);
2670 modprobe_remove_lttng_control();
2673 WARN("No kernel tracer available");
2674 kernel_tracer_fd
= -1;
2676 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2678 return LTTNG_ERR_KERN_NA
;
2684 * Copy consumer output from the tracing session to the domain session. The
2685 * function also applies the right modification on a per domain basis for the
2686 * trace files destination directory.
2688 * Should *NOT* be called with RCU read-side lock held.
2690 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2693 const char *dir_name
;
2694 struct consumer_output
*consumer
;
2697 assert(session
->consumer
);
2700 case LTTNG_DOMAIN_KERNEL
:
2701 DBG3("Copying tracing session consumer output in kernel session");
2703 * XXX: We should audit the session creation and what this function
2704 * does "extra" in order to avoid a destroy since this function is used
2705 * in the domain session creation (kernel and ust) only. Same for UST
2708 if (session
->kernel_session
->consumer
) {
2709 consumer_destroy_output(session
->kernel_session
->consumer
);
2711 session
->kernel_session
->consumer
=
2712 consumer_copy_output(session
->consumer
);
2713 /* Ease our life a bit for the next part */
2714 consumer
= session
->kernel_session
->consumer
;
2715 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2717 case LTTNG_DOMAIN_JUL
:
2718 case LTTNG_DOMAIN_LOG4J
:
2719 case LTTNG_DOMAIN_PYTHON
:
2720 case LTTNG_DOMAIN_UST
:
2721 DBG3("Copying tracing session consumer output in UST session");
2722 if (session
->ust_session
->consumer
) {
2723 consumer_destroy_output(session
->ust_session
->consumer
);
2725 session
->ust_session
->consumer
=
2726 consumer_copy_output(session
->consumer
);
2727 /* Ease our life a bit for the next part */
2728 consumer
= session
->ust_session
->consumer
;
2729 dir_name
= DEFAULT_UST_TRACE_DIR
;
2732 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2736 /* Append correct directory to subdir */
2737 strncat(consumer
->subdir
, dir_name
,
2738 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2739 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2748 * Create an UST session and add it to the session ust list.
2750 * Should *NOT* be called with RCU read-side lock held.
2752 static int create_ust_session(struct ltt_session
*session
,
2753 struct lttng_domain
*domain
)
2756 struct ltt_ust_session
*lus
= NULL
;
2760 assert(session
->consumer
);
2762 switch (domain
->type
) {
2763 case LTTNG_DOMAIN_JUL
:
2764 case LTTNG_DOMAIN_LOG4J
:
2765 case LTTNG_DOMAIN_PYTHON
:
2766 case LTTNG_DOMAIN_UST
:
2769 ERR("Unknown UST domain on create session %d", domain
->type
);
2770 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2774 DBG("Creating UST session");
2776 lus
= trace_ust_create_session(session
->id
);
2778 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2782 lus
->uid
= session
->uid
;
2783 lus
->gid
= session
->gid
;
2784 lus
->output_traces
= session
->output_traces
;
2785 lus
->snapshot_mode
= session
->snapshot_mode
;
2786 lus
->live_timer_interval
= session
->live_timer
;
2787 session
->ust_session
= lus
;
2788 if (session
->shm_path
[0]) {
2789 strncpy(lus
->root_shm_path
, session
->shm_path
,
2790 sizeof(lus
->root_shm_path
));
2791 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2792 strncpy(lus
->shm_path
, session
->shm_path
,
2793 sizeof(lus
->shm_path
));
2794 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2795 strncat(lus
->shm_path
, "/ust",
2796 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2798 /* Copy session output to the newly created UST session */
2799 ret
= copy_session_consumer(domain
->type
, session
);
2800 if (ret
!= LTTNG_OK
) {
2808 session
->ust_session
= NULL
;
2813 * Create a kernel tracer session then create the default channel.
2815 static int create_kernel_session(struct ltt_session
*session
)
2819 DBG("Creating kernel session");
2821 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2823 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2827 /* Code flow safety */
2828 assert(session
->kernel_session
);
2830 /* Copy session output to the newly created Kernel session */
2831 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2832 if (ret
!= LTTNG_OK
) {
2836 /* Create directory(ies) on local filesystem. */
2837 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2838 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2839 ret
= run_as_mkdir_recursive(
2840 session
->kernel_session
->consumer
->dst
.trace_path
,
2841 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2843 if (ret
!= -EEXIST
) {
2844 ERR("Trace directory creation error");
2850 session
->kernel_session
->uid
= session
->uid
;
2851 session
->kernel_session
->gid
= session
->gid
;
2852 session
->kernel_session
->output_traces
= session
->output_traces
;
2853 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2858 trace_kernel_destroy_session(session
->kernel_session
);
2859 session
->kernel_session
= NULL
;
2864 * Count number of session permitted by uid/gid.
2866 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2869 struct ltt_session
*session
;
2871 DBG("Counting number of available session for UID %d GID %d",
2873 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2875 * Only list the sessions the user can control.
2877 if (!session_access_ok(session
, uid
, gid
)) {
2886 * Process the command requested by the lttng client within the command
2887 * context structure. This function make sure that the return structure (llm)
2888 * is set and ready for transmission before returning.
2890 * Return any error encountered or 0 for success.
2892 * "sock" is only used for special-case var. len data.
2894 * Should *NOT* be called with RCU read-side lock held.
2896 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2900 int need_tracing_session
= 1;
2903 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2907 switch (cmd_ctx
->lsm
->cmd_type
) {
2908 case LTTNG_CREATE_SESSION
:
2909 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2910 case LTTNG_CREATE_SESSION_LIVE
:
2911 case LTTNG_DESTROY_SESSION
:
2912 case LTTNG_LIST_SESSIONS
:
2913 case LTTNG_LIST_DOMAINS
:
2914 case LTTNG_START_TRACE
:
2915 case LTTNG_STOP_TRACE
:
2916 case LTTNG_DATA_PENDING
:
2917 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2918 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2919 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2920 case LTTNG_SNAPSHOT_RECORD
:
2921 case LTTNG_SAVE_SESSION
:
2922 case LTTNG_SET_SESSION_SHM_PATH
:
2929 if (opt_no_kernel
&& need_domain
2930 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2932 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2934 ret
= LTTNG_ERR_KERN_NA
;
2939 /* Deny register consumer if we already have a spawned consumer. */
2940 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2941 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2942 if (kconsumer_data
.pid
> 0) {
2943 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2944 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2947 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2951 * Check for command that don't needs to allocate a returned payload. We do
2952 * this here so we don't have to make the call for no payload at each
2955 switch(cmd_ctx
->lsm
->cmd_type
) {
2956 case LTTNG_LIST_SESSIONS
:
2957 case LTTNG_LIST_TRACEPOINTS
:
2958 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2959 case LTTNG_LIST_DOMAINS
:
2960 case LTTNG_LIST_CHANNELS
:
2961 case LTTNG_LIST_EVENTS
:
2962 case LTTNG_LIST_SYSCALLS
:
2963 case LTTNG_LIST_TRACKER_PIDS
:
2966 /* Setup lttng message with no payload */
2967 ret
= setup_lttng_msg(cmd_ctx
, 0);
2969 /* This label does not try to unlock the session */
2970 goto init_setup_error
;
2974 /* Commands that DO NOT need a session. */
2975 switch (cmd_ctx
->lsm
->cmd_type
) {
2976 case LTTNG_CREATE_SESSION
:
2977 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2978 case LTTNG_CREATE_SESSION_LIVE
:
2979 case LTTNG_CALIBRATE
:
2980 case LTTNG_LIST_SESSIONS
:
2981 case LTTNG_LIST_TRACEPOINTS
:
2982 case LTTNG_LIST_SYSCALLS
:
2983 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2984 case LTTNG_SAVE_SESSION
:
2985 need_tracing_session
= 0;
2988 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2990 * We keep the session list lock across _all_ commands
2991 * for now, because the per-session lock does not
2992 * handle teardown properly.
2994 session_lock_list();
2995 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2996 if (cmd_ctx
->session
== NULL
) {
2997 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3000 /* Acquire lock for the session */
3001 session_lock(cmd_ctx
->session
);
3007 * Commands that need a valid session but should NOT create one if none
3008 * exists. Instead of creating one and destroying it when the command is
3009 * handled, process that right before so we save some round trip in useless
3012 switch (cmd_ctx
->lsm
->cmd_type
) {
3013 case LTTNG_DISABLE_CHANNEL
:
3014 case LTTNG_DISABLE_EVENT
:
3015 switch (cmd_ctx
->lsm
->domain
.type
) {
3016 case LTTNG_DOMAIN_KERNEL
:
3017 if (!cmd_ctx
->session
->kernel_session
) {
3018 ret
= LTTNG_ERR_NO_CHANNEL
;
3022 case LTTNG_DOMAIN_JUL
:
3023 case LTTNG_DOMAIN_LOG4J
:
3024 case LTTNG_DOMAIN_PYTHON
:
3025 case LTTNG_DOMAIN_UST
:
3026 if (!cmd_ctx
->session
->ust_session
) {
3027 ret
= LTTNG_ERR_NO_CHANNEL
;
3032 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3044 * Check domain type for specific "pre-action".
3046 switch (cmd_ctx
->lsm
->domain
.type
) {
3047 case LTTNG_DOMAIN_KERNEL
:
3049 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3053 /* Kernel tracer check */
3054 if (kernel_tracer_fd
== -1) {
3055 /* Basically, load kernel tracer modules */
3056 ret
= init_kernel_tracer();
3062 /* Consumer is in an ERROR state. Report back to client */
3063 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3064 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3068 /* Need a session for kernel command */
3069 if (need_tracing_session
) {
3070 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3071 ret
= create_kernel_session(cmd_ctx
->session
);
3073 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3078 /* Start the kernel consumer daemon */
3079 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3080 if (kconsumer_data
.pid
== 0 &&
3081 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3082 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3083 ret
= start_consumerd(&kconsumer_data
);
3085 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3088 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3090 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3094 * The consumer was just spawned so we need to add the socket to
3095 * the consumer output of the session if exist.
3097 ret
= consumer_create_socket(&kconsumer_data
,
3098 cmd_ctx
->session
->kernel_session
->consumer
);
3105 case LTTNG_DOMAIN_JUL
:
3106 case LTTNG_DOMAIN_LOG4J
:
3107 case LTTNG_DOMAIN_PYTHON
:
3108 case LTTNG_DOMAIN_UST
:
3110 if (!ust_app_supported()) {
3111 ret
= LTTNG_ERR_NO_UST
;
3114 /* Consumer is in an ERROR state. Report back to client */
3115 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3116 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3120 if (need_tracing_session
) {
3121 /* Create UST session if none exist. */
3122 if (cmd_ctx
->session
->ust_session
== NULL
) {
3123 ret
= create_ust_session(cmd_ctx
->session
,
3124 &cmd_ctx
->lsm
->domain
);
3125 if (ret
!= LTTNG_OK
) {
3130 /* Start the UST consumer daemons */
3132 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3133 if (consumerd64_bin
[0] != '\0' &&
3134 ustconsumer64_data
.pid
== 0 &&
3135 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3136 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3137 ret
= start_consumerd(&ustconsumer64_data
);
3139 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3140 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3144 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3145 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3147 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3151 * Setup socket for consumer 64 bit. No need for atomic access
3152 * since it was set above and can ONLY be set in this thread.
3154 ret
= consumer_create_socket(&ustconsumer64_data
,
3155 cmd_ctx
->session
->ust_session
->consumer
);
3161 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3162 if (consumerd32_bin
[0] != '\0' &&
3163 ustconsumer32_data
.pid
== 0 &&
3164 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3165 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3166 ret
= start_consumerd(&ustconsumer32_data
);
3168 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3169 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3173 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3174 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3176 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3180 * Setup socket for consumer 64 bit. No need for atomic access
3181 * since it was set above and can ONLY be set in this thread.
3183 ret
= consumer_create_socket(&ustconsumer32_data
,
3184 cmd_ctx
->session
->ust_session
->consumer
);
3196 /* Validate consumer daemon state when start/stop trace command */
3197 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3198 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3199 switch (cmd_ctx
->lsm
->domain
.type
) {
3200 case LTTNG_DOMAIN_JUL
:
3201 case LTTNG_DOMAIN_LOG4J
:
3202 case LTTNG_DOMAIN_PYTHON
:
3203 case LTTNG_DOMAIN_UST
:
3204 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3205 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3209 case LTTNG_DOMAIN_KERNEL
:
3210 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3211 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3219 * Check that the UID or GID match that of the tracing session.
3220 * The root user can interact with all sessions.
3222 if (need_tracing_session
) {
3223 if (!session_access_ok(cmd_ctx
->session
,
3224 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3225 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3226 ret
= LTTNG_ERR_EPERM
;
3232 * Send relayd information to consumer as soon as we have a domain and a
3235 if (cmd_ctx
->session
&& need_domain
) {
3237 * Setup relayd if not done yet. If the relayd information was already
3238 * sent to the consumer, this call will gracefully return.
3240 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3241 if (ret
!= LTTNG_OK
) {
3246 /* Process by command type */
3247 switch (cmd_ctx
->lsm
->cmd_type
) {
3248 case LTTNG_ADD_CONTEXT
:
3250 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3251 cmd_ctx
->lsm
->u
.context
.channel_name
,
3252 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3255 case LTTNG_DISABLE_CHANNEL
:
3257 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3258 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3261 case LTTNG_DISABLE_EVENT
:
3263 /* FIXME: passing packed structure to non-packed pointer */
3264 /* TODO: handle filter */
3265 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3266 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3267 &cmd_ctx
->lsm
->u
.disable
.event
);
3270 case LTTNG_ENABLE_CHANNEL
:
3272 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3273 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3276 case LTTNG_TRACK_PID
:
3278 ret
= cmd_track_pid(cmd_ctx
->session
,
3279 cmd_ctx
->lsm
->domain
.type
,
3280 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3283 case LTTNG_UNTRACK_PID
:
3285 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3286 cmd_ctx
->lsm
->domain
.type
,
3287 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3290 case LTTNG_ENABLE_EVENT
:
3292 struct lttng_event_exclusion
*exclusion
= NULL
;
3293 struct lttng_filter_bytecode
*bytecode
= NULL
;
3294 char *filter_expression
= NULL
;
3296 /* Handle exclusion events and receive it from the client. */
3297 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3298 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3300 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3301 (count
* LTTNG_SYMBOL_NAME_LEN
));
3303 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3307 DBG("Receiving var len exclusion event list from client ...");
3308 exclusion
->count
= count
;
3309 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3310 count
* LTTNG_SYMBOL_NAME_LEN
);
3312 DBG("Nothing recv() from client var len data... continuing");
3315 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3320 /* Get filter expression from client. */
3321 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3322 size_t expression_len
=
3323 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3325 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3326 ret
= LTTNG_ERR_FILTER_INVAL
;
3331 filter_expression
= zmalloc(expression_len
);
3332 if (!filter_expression
) {
3334 ret
= LTTNG_ERR_FILTER_NOMEM
;
3338 /* Receive var. len. data */
3339 DBG("Receiving var len filter's expression from client ...");
3340 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3343 DBG("Nothing recv() from client car len data... continuing");
3345 free(filter_expression
);
3347 ret
= LTTNG_ERR_FILTER_INVAL
;
3352 /* Handle filter and get bytecode from client. */
3353 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3354 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3356 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3357 ret
= LTTNG_ERR_FILTER_INVAL
;
3358 free(filter_expression
);
3363 bytecode
= zmalloc(bytecode_len
);
3365 free(filter_expression
);
3367 ret
= LTTNG_ERR_FILTER_NOMEM
;
3371 /* Receive var. len. data */
3372 DBG("Receiving var len filter's bytecode from client ...");
3373 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3375 DBG("Nothing recv() from client car len data... continuing");
3377 free(filter_expression
);
3380 ret
= LTTNG_ERR_FILTER_INVAL
;
3384 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3385 free(filter_expression
);
3388 ret
= LTTNG_ERR_FILTER_INVAL
;
3393 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3394 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3395 &cmd_ctx
->lsm
->u
.enable
.event
,
3396 filter_expression
, bytecode
, exclusion
,
3397 kernel_poll_pipe
[1]);
3400 case LTTNG_LIST_TRACEPOINTS
:
3402 struct lttng_event
*events
;
3405 session_lock_list();
3406 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3407 session_unlock_list();
3408 if (nb_events
< 0) {
3409 /* Return value is a negative lttng_error_code. */
3415 * Setup lttng message with payload size set to the event list size in
3416 * bytes and then copy list into the llm payload.
3418 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3424 /* Copy event list into message payload */
3425 memcpy(cmd_ctx
->llm
->payload
, events
,
3426 sizeof(struct lttng_event
) * nb_events
);
3433 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3435 struct lttng_event_field
*fields
;
3438 session_lock_list();
3439 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3441 session_unlock_list();
3442 if (nb_fields
< 0) {
3443 /* Return value is a negative lttng_error_code. */
3449 * Setup lttng message with payload size set to the event list size in
3450 * bytes and then copy list into the llm payload.
3452 ret
= setup_lttng_msg(cmd_ctx
,
3453 sizeof(struct lttng_event_field
) * nb_fields
);
3459 /* Copy event list into message payload */
3460 memcpy(cmd_ctx
->llm
->payload
, fields
,
3461 sizeof(struct lttng_event_field
) * nb_fields
);
3468 case LTTNG_LIST_SYSCALLS
:
3470 struct lttng_event
*events
;
3473 nb_events
= cmd_list_syscalls(&events
);
3474 if (nb_events
< 0) {
3475 /* Return value is a negative lttng_error_code. */
3481 * Setup lttng message with payload size set to the event list size in
3482 * bytes and then copy list into the llm payload.
3484 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3490 /* Copy event list into message payload */
3491 memcpy(cmd_ctx
->llm
->payload
, events
,
3492 sizeof(struct lttng_event
) * nb_events
);
3499 case LTTNG_LIST_TRACKER_PIDS
:
3501 int32_t *pids
= NULL
;
3504 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3505 cmd_ctx
->lsm
->domain
.type
, &pids
);
3507 /* Return value is a negative lttng_error_code. */
3513 * Setup lttng message with payload size set to the event list size in
3514 * bytes and then copy list into the llm payload.
3516 ret
= setup_lttng_msg(cmd_ctx
, sizeof(int32_t) * nr_pids
);
3522 /* Copy event list into message payload */
3523 memcpy(cmd_ctx
->llm
->payload
, pids
,
3524 sizeof(int) * nr_pids
);
3531 case LTTNG_SET_CONSUMER_URI
:
3534 struct lttng_uri
*uris
;
3536 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3537 len
= nb_uri
* sizeof(struct lttng_uri
);
3540 ret
= LTTNG_ERR_INVALID
;
3544 uris
= zmalloc(len
);
3546 ret
= LTTNG_ERR_FATAL
;
3550 /* Receive variable len data */
3551 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3552 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3554 DBG("No URIs received from client... continuing");
3556 ret
= LTTNG_ERR_SESSION_FAIL
;
3561 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3563 if (ret
!= LTTNG_OK
) {
3570 case LTTNG_START_TRACE
:
3572 ret
= cmd_start_trace(cmd_ctx
->session
);
3575 case LTTNG_STOP_TRACE
:
3577 ret
= cmd_stop_trace(cmd_ctx
->session
);
3580 case LTTNG_CREATE_SESSION
:
3583 struct lttng_uri
*uris
= NULL
;
3585 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3586 len
= nb_uri
* sizeof(struct lttng_uri
);
3589 uris
= zmalloc(len
);
3591 ret
= LTTNG_ERR_FATAL
;
3595 /* Receive variable len data */
3596 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3597 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3599 DBG("No URIs received from client... continuing");
3601 ret
= LTTNG_ERR_SESSION_FAIL
;
3606 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3607 DBG("Creating session with ONE network URI is a bad call");
3608 ret
= LTTNG_ERR_SESSION_FAIL
;
3614 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3615 &cmd_ctx
->creds
, 0);
3621 case LTTNG_DESTROY_SESSION
:
3623 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3625 /* Set session to NULL so we do not unlock it after free. */
3626 cmd_ctx
->session
= NULL
;
3629 case LTTNG_LIST_DOMAINS
:
3632 struct lttng_domain
*domains
= NULL
;
3634 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3636 /* Return value is a negative lttng_error_code. */
3641 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3647 /* Copy event list into message payload */
3648 memcpy(cmd_ctx
->llm
->payload
, domains
,
3649 nb_dom
* sizeof(struct lttng_domain
));
3656 case LTTNG_LIST_CHANNELS
:
3659 struct lttng_channel
*channels
= NULL
;
3661 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3662 cmd_ctx
->session
, &channels
);
3664 /* Return value is a negative lttng_error_code. */
3669 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3675 /* Copy event list into message payload */
3676 memcpy(cmd_ctx
->llm
->payload
, channels
,
3677 nb_chan
* sizeof(struct lttng_channel
));
3684 case LTTNG_LIST_EVENTS
:
3687 struct lttng_event
*events
= NULL
;
3689 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3690 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3692 /* Return value is a negative lttng_error_code. */
3697 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3703 /* Copy event list into message payload */
3704 memcpy(cmd_ctx
->llm
->payload
, events
,
3705 nb_event
* sizeof(struct lttng_event
));
3712 case LTTNG_LIST_SESSIONS
:
3714 unsigned int nr_sessions
;
3716 session_lock_list();
3717 nr_sessions
= lttng_sessions_count(
3718 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3719 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3721 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3723 session_unlock_list();
3727 /* Filled the session array */
3728 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3729 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3730 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3732 session_unlock_list();
3737 case LTTNG_CALIBRATE
:
3739 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3740 &cmd_ctx
->lsm
->u
.calibrate
);
3743 case LTTNG_REGISTER_CONSUMER
:
3745 struct consumer_data
*cdata
;
3747 switch (cmd_ctx
->lsm
->domain
.type
) {
3748 case LTTNG_DOMAIN_KERNEL
:
3749 cdata
= &kconsumer_data
;
3752 ret
= LTTNG_ERR_UND
;
3756 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3757 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3760 case LTTNG_DATA_PENDING
:
3762 ret
= cmd_data_pending(cmd_ctx
->session
);
3765 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3767 struct lttcomm_lttng_output_id reply
;
3769 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3770 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3771 if (ret
!= LTTNG_OK
) {
3775 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3780 /* Copy output list into message payload */
3781 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3785 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3787 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3788 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3791 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3794 struct lttng_snapshot_output
*outputs
= NULL
;
3796 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3797 if (nb_output
< 0) {
3802 ret
= setup_lttng_msg(cmd_ctx
,
3803 nb_output
* sizeof(struct lttng_snapshot_output
));
3810 /* Copy output list into message payload */
3811 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3812 nb_output
* sizeof(struct lttng_snapshot_output
));
3819 case LTTNG_SNAPSHOT_RECORD
:
3821 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3822 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3823 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3826 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3829 struct lttng_uri
*uris
= NULL
;
3831 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3832 len
= nb_uri
* sizeof(struct lttng_uri
);
3835 uris
= zmalloc(len
);
3837 ret
= LTTNG_ERR_FATAL
;
3841 /* Receive variable len data */
3842 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3843 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3845 DBG("No URIs received from client... continuing");
3847 ret
= LTTNG_ERR_SESSION_FAIL
;
3852 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3853 DBG("Creating session with ONE network URI is a bad call");
3854 ret
= LTTNG_ERR_SESSION_FAIL
;
3860 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3861 nb_uri
, &cmd_ctx
->creds
);
3865 case LTTNG_CREATE_SESSION_LIVE
:
3868 struct lttng_uri
*uris
= NULL
;
3870 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3871 len
= nb_uri
* sizeof(struct lttng_uri
);
3874 uris
= zmalloc(len
);
3876 ret
= LTTNG_ERR_FATAL
;
3880 /* Receive variable len data */
3881 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3882 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3884 DBG("No URIs received from client... continuing");
3886 ret
= LTTNG_ERR_SESSION_FAIL
;
3891 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3892 DBG("Creating session with ONE network URI is a bad call");
3893 ret
= LTTNG_ERR_SESSION_FAIL
;
3899 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3900 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3904 case LTTNG_SAVE_SESSION
:
3906 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
3910 case LTTNG_SET_SESSION_SHM_PATH
:
3912 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
3913 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
3917 ret
= LTTNG_ERR_UND
;
3922 if (cmd_ctx
->llm
== NULL
) {
3923 DBG("Missing llm structure. Allocating one.");
3924 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3928 /* Set return code */
3929 cmd_ctx
->llm
->ret_code
= ret
;
3931 if (cmd_ctx
->session
) {
3932 session_unlock(cmd_ctx
->session
);
3934 if (need_tracing_session
) {
3935 session_unlock_list();
3942 * Thread managing health check socket.
3944 static void *thread_manage_health(void *data
)
3946 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3947 uint32_t revents
, nb_fd
;
3948 struct lttng_poll_event events
;
3949 struct health_comm_msg msg
;
3950 struct health_comm_reply reply
;
3952 DBG("[thread] Manage health check started");
3954 rcu_register_thread();
3956 /* We might hit an error path before this is created. */
3957 lttng_poll_init(&events
);
3959 /* Create unix socket */
3960 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3962 ERR("Unable to create health check Unix socket");
3968 /* lttng health client socket path permissions */
3969 ret
= chown(health_unix_sock_path
, 0,
3970 utils_get_group_id(tracing_group_name
));
3972 ERR("Unable to set group on %s", health_unix_sock_path
);
3978 ret
= chmod(health_unix_sock_path
,
3979 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3981 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3989 * Set the CLOEXEC flag. Return code is useless because either way, the
3992 (void) utils_set_fd_cloexec(sock
);
3994 ret
= lttcomm_listen_unix_sock(sock
);
4000 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4001 * more will be added to this poll set.
4003 ret
= sessiond_set_thread_pollset(&events
, 2);
4008 /* Add the application registration socket */
4009 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4014 sessiond_notify_ready();
4017 DBG("Health check ready");
4019 /* Inifinite blocking call, waiting for transmission */
4021 ret
= lttng_poll_wait(&events
, -1);
4024 * Restart interrupted system call.
4026 if (errno
== EINTR
) {
4034 for (i
= 0; i
< nb_fd
; i
++) {
4035 /* Fetch once the poll data */
4036 revents
= LTTNG_POLL_GETEV(&events
, i
);
4037 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4040 /* No activity for this FD (poll implementation). */
4044 /* Thread quit pipe has been closed. Killing thread. */
4045 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4051 /* Event on the registration socket */
4052 if (pollfd
== sock
) {
4053 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4054 ERR("Health socket poll error");
4060 new_sock
= lttcomm_accept_unix_sock(sock
);
4066 * Set the CLOEXEC flag. Return code is useless because either way, the
4069 (void) utils_set_fd_cloexec(new_sock
);
4071 DBG("Receiving data from client for health...");
4072 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4074 DBG("Nothing recv() from client... continuing");
4075 ret
= close(new_sock
);
4083 rcu_thread_online();
4085 memset(&reply
, 0, sizeof(reply
));
4086 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4088 * health_check_state returns 0 if health is
4091 if (!health_check_state(health_sessiond
, i
)) {
4092 reply
.ret_code
|= 1ULL << i
;
4096 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4098 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4100 ERR("Failed to send health data back to client");
4103 /* End of transmission */
4104 ret
= close(new_sock
);
4114 ERR("Health error occurred in %s", __func__
);
4116 DBG("Health check thread dying");
4117 unlink(health_unix_sock_path
);
4125 lttng_poll_clean(&events
);
4127 rcu_unregister_thread();
4132 * This thread manage all clients request using the unix client socket for
4135 static void *thread_manage_clients(void *data
)
4137 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4139 uint32_t revents
, nb_fd
;
4140 struct command_ctx
*cmd_ctx
= NULL
;
4141 struct lttng_poll_event events
;
4143 DBG("[thread] Manage client started");
4145 rcu_register_thread();
4147 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4149 health_code_update();
4151 ret
= lttcomm_listen_unix_sock(client_sock
);
4157 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4158 * more will be added to this poll set.
4160 ret
= sessiond_set_thread_pollset(&events
, 2);
4162 goto error_create_poll
;
4165 /* Add the application registration socket */
4166 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4171 sessiond_notify_ready();
4172 ret
= sem_post(&load_info
->message_thread_ready
);
4174 PERROR("sem_post message_thread_ready");
4178 /* This testpoint is after we signal readiness to the parent. */
4179 if (testpoint(sessiond_thread_manage_clients
)) {
4183 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4187 health_code_update();
4190 DBG("Accepting client command ...");
4192 /* Inifinite blocking call, waiting for transmission */
4194 health_poll_entry();
4195 ret
= lttng_poll_wait(&events
, -1);
4199 * Restart interrupted system call.
4201 if (errno
== EINTR
) {
4209 for (i
= 0; i
< nb_fd
; i
++) {
4210 /* Fetch once the poll data */
4211 revents
= LTTNG_POLL_GETEV(&events
, i
);
4212 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4214 health_code_update();
4217 /* No activity for this FD (poll implementation). */
4221 /* Thread quit pipe has been closed. Killing thread. */
4222 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4228 /* Event on the registration socket */
4229 if (pollfd
== client_sock
) {
4230 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4231 ERR("Client socket poll error");
4237 DBG("Wait for client response");
4239 health_code_update();
4241 sock
= lttcomm_accept_unix_sock(client_sock
);
4247 * Set the CLOEXEC flag. Return code is useless because either way, the
4250 (void) utils_set_fd_cloexec(sock
);
4252 /* Set socket option for credentials retrieval */
4253 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4258 /* Allocate context command to process the client request */
4259 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4260 if (cmd_ctx
== NULL
) {
4261 PERROR("zmalloc cmd_ctx");
4265 /* Allocate data buffer for reception */
4266 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4267 if (cmd_ctx
->lsm
== NULL
) {
4268 PERROR("zmalloc cmd_ctx->lsm");
4272 cmd_ctx
->llm
= NULL
;
4273 cmd_ctx
->session
= NULL
;
4275 health_code_update();
4278 * Data is received from the lttng client. The struct
4279 * lttcomm_session_msg (lsm) contains the command and data request of
4282 DBG("Receiving data from client ...");
4283 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4284 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4286 DBG("Nothing recv() from client... continuing");
4292 clean_command_ctx(&cmd_ctx
);
4296 health_code_update();
4298 // TODO: Validate cmd_ctx including sanity check for
4299 // security purpose.
4301 rcu_thread_online();
4303 * This function dispatch the work to the kernel or userspace tracer
4304 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4305 * informations for the client. The command context struct contains
4306 * everything this function may needs.
4308 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4309 rcu_thread_offline();
4317 * TODO: Inform client somehow of the fatal error. At
4318 * this point, ret < 0 means that a zmalloc failed
4319 * (ENOMEM). Error detected but still accept
4320 * command, unless a socket error has been
4323 clean_command_ctx(&cmd_ctx
);
4327 health_code_update();
4329 DBG("Sending response (size: %d, retcode: %s)",
4330 cmd_ctx
->lttng_msg_size
,
4331 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4332 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4334 ERR("Failed to send data back to client");
4337 /* End of transmission */
4344 clean_command_ctx(&cmd_ctx
);
4346 health_code_update();
4358 lttng_poll_clean(&events
);
4359 clean_command_ctx(&cmd_ctx
);
4363 unlink(client_unix_sock_path
);
4364 if (client_sock
>= 0) {
4365 ret
= close(client_sock
);
4373 ERR("Health error occurred in %s", __func__
);
4376 health_unregister(health_sessiond
);
4378 DBG("Client thread dying");
4380 rcu_unregister_thread();
4383 * Since we are creating the consumer threads, we own them, so we need
4384 * to join them before our thread exits.
4386 ret
= join_consumer_thread(&kconsumer_data
);
4389 PERROR("join_consumer");
4392 ret
= join_consumer_thread(&ustconsumer32_data
);
4395 PERROR("join_consumer ust32");
4398 ret
= join_consumer_thread(&ustconsumer64_data
);
4401 PERROR("join_consumer ust64");
4408 * usage function on stderr
4410 static void usage(void)
4412 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4413 fprintf(stderr
, " -h, --help Display this usage.\n");
4414 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4415 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4416 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4417 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4418 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4419 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4420 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4421 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4422 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4423 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4424 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4425 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4426 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4427 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4428 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4429 fprintf(stderr
, " -V, --version Show version number.\n");
4430 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4431 fprintf(stderr
, " -q, --quiet No output at all.\n");
4432 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4433 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4434 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4435 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4436 fprintf(stderr
, " --agent-tcp-port Agent registration TCP port\n");
4437 fprintf(stderr
, " -f --config PATH Load daemon configuration file\n");
4438 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4439 fprintf(stderr
, " --kmod-probes Specify kernel module probes to load\n");
4440 fprintf(stderr
, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4444 * Take an option from the getopt output and set it in the right variable to be
4447 * Return 0 on success else a negative value.
4449 static int set_option(int opt
, const char *arg
, const char *optname
)
4453 if (arg
&& arg
[0] == '\0') {
4455 * This only happens if the value is read from daemon config
4456 * file. This means the option requires an argument and the
4457 * configuration file contains a line such as:
4466 fprintf(stderr
, "option %s", optname
);
4468 fprintf(stderr
, " with arg %s\n", arg
);
4472 if (lttng_is_setuid_setgid()) {
4473 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4474 "-c, --client-sock");
4476 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4480 if (lttng_is_setuid_setgid()) {
4481 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4484 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4494 if (lttng_is_setuid_setgid()) {
4495 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4499 * If the override option is set, the pointer points to a
4500 * *non* const thus freeing it even though the variable type is
4503 if (tracing_group_name_override
) {
4504 free((void *) tracing_group_name
);
4506 tracing_group_name
= strdup(arg
);
4507 if (!tracing_group_name
) {
4511 tracing_group_name_override
= 1;
4518 fprintf(stdout
, "%s\n", VERSION
);
4524 if (lttng_is_setuid_setgid()) {
4525 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4526 "--kconsumerd-err-sock");
4528 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4532 if (lttng_is_setuid_setgid()) {
4533 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4534 "--kconsumerd-cmd-sock");
4536 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4540 if (lttng_is_setuid_setgid()) {
4541 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4542 "--ustconsumerd64-err-sock");
4544 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4548 if (lttng_is_setuid_setgid()) {
4549 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4550 "--ustconsumerd64-cmd-sock");
4552 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4556 if (lttng_is_setuid_setgid()) {
4557 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4558 "--ustconsumerd32-err-sock");
4560 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4564 if (lttng_is_setuid_setgid()) {
4565 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4566 "--ustconsumerd32-cmd-sock");
4568 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4575 lttng_opt_quiet
= 1;
4578 /* Verbose level can increase using multiple -v */
4580 /* Value obtained from config file */
4581 lttng_opt_verbose
= config_parse_value(arg
);
4583 /* -v used on command line */
4584 lttng_opt_verbose
++;
4586 /* Clamp value to [0, 3] */
4587 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4588 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4592 opt_verbose_consumer
= config_parse_value(arg
);
4594 opt_verbose_consumer
+= 1;
4598 if (lttng_is_setuid_setgid()) {
4599 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4600 "--consumerd32-path");
4602 if (consumerd32_bin_override
) {
4603 free((void *) consumerd32_bin
);
4605 consumerd32_bin
= strdup(arg
);
4606 if (!consumerd32_bin
) {
4610 consumerd32_bin_override
= 1;
4614 if (lttng_is_setuid_setgid()) {
4615 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4616 "--consumerd32-libdir");
4618 if (consumerd32_libdir_override
) {
4619 free((void *) consumerd32_libdir
);
4621 consumerd32_libdir
= strdup(arg
);
4622 if (!consumerd32_libdir
) {
4626 consumerd32_libdir_override
= 1;
4630 if (lttng_is_setuid_setgid()) {
4631 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4632 "--consumerd64-path");
4634 if (consumerd64_bin_override
) {
4635 free((void *) consumerd64_bin
);
4637 consumerd64_bin
= strdup(arg
);
4638 if (!consumerd64_bin
) {
4642 consumerd64_bin_override
= 1;
4646 if (lttng_is_setuid_setgid()) {
4647 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4648 "--consumerd64-libdir");
4650 if (consumerd64_libdir_override
) {
4651 free((void *) consumerd64_libdir
);
4653 consumerd64_libdir
= strdup(arg
);
4654 if (!consumerd64_libdir
) {
4658 consumerd64_libdir_override
= 1;
4662 if (lttng_is_setuid_setgid()) {
4663 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4667 opt_pidfile
= strdup(arg
);
4674 case 'J': /* Agent TCP port. */
4676 if (lttng_is_setuid_setgid()) {
4677 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4678 "--agent-tcp-port");
4687 v
= strtoul(arg
, NULL
, 0);
4688 if (errno
!= 0 || !isdigit(arg
[0])) {
4689 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4692 if (v
== 0 || v
>= 65535) {
4693 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4696 agent_tcp_port
= (uint32_t) v
;
4697 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4702 if (lttng_is_setuid_setgid()) {
4703 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4706 free(opt_load_session_path
);
4707 opt_load_session_path
= strdup(arg
);
4708 if (!opt_load_session_path
) {
4714 case 'P': /* probe modules list */
4715 if (lttng_is_setuid_setgid()) {
4716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4719 free(kmod_probes_list
);
4720 kmod_probes_list
= strdup(arg
);
4721 if (!kmod_probes_list
) {
4728 if (lttng_is_setuid_setgid()) {
4729 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4730 "--extra-kmod-probes");
4732 free(kmod_extra_probes_list
);
4733 kmod_extra_probes_list
= strdup(arg
);
4734 if (!kmod_extra_probes_list
) {
4741 /* This is handled in set_options() thus silent break. */
4744 /* Unknown option or other error.
4745 * Error is printed by getopt, just return */
4750 if (ret
== -EINVAL
) {
4751 const char *opt_name
= "unknown";
4754 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4756 if (opt
== long_options
[i
].val
) {
4757 opt_name
= long_options
[i
].name
;
4762 WARN("Invalid argument provided for option \"%s\", using default value.",
4770 * config_entry_handler_cb used to handle options read from a config file.
4771 * See config_entry_handler_cb comment in common/config/config.h for the
4772 * return value conventions.
4774 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4778 if (!entry
|| !entry
->name
|| !entry
->value
) {
4783 /* Check if the option is to be ignored */
4784 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4785 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4790 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4793 /* Ignore if not fully matched. */
4794 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4799 * If the option takes no argument on the command line, we have to
4800 * check if the value is "true". We support non-zero numeric values,
4803 if (!long_options
[i
].has_arg
) {
4804 ret
= config_parse_value(entry
->value
);
4807 WARN("Invalid configuration value \"%s\" for option %s",
4808 entry
->value
, entry
->name
);
4810 /* False, skip boolean config option. */
4815 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4819 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4826 * daemon configuration loading and argument parsing
4828 static int set_options(int argc
, char **argv
)
4830 int ret
= 0, c
= 0, option_index
= 0;
4831 int orig_optopt
= optopt
, orig_optind
= optind
;
4833 const char *config_path
= NULL
;
4835 optstring
= utils_generate_optstring(long_options
,
4836 sizeof(long_options
) / sizeof(struct option
));
4842 /* Check for the --config option */
4843 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4844 &option_index
)) != -1) {
4848 } else if (c
!= 'f') {
4849 /* if not equal to --config option. */
4853 if (lttng_is_setuid_setgid()) {
4854 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4857 config_path
= utils_expand_path(optarg
);
4859 ERR("Failed to resolve path: %s", optarg
);
4864 ret
= config_get_section_entries(config_path
, config_section_name
,
4865 config_entry_handler
, NULL
);
4868 ERR("Invalid configuration option at line %i", ret
);
4874 /* Reset getopt's global state */
4875 optopt
= orig_optopt
;
4876 optind
= orig_optind
;
4878 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4883 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4895 * Creates the two needed socket by the daemon.
4896 * apps_sock - The communication socket for all UST apps.
4897 * client_sock - The communication of the cli tool (lttng).
4899 static int init_daemon_socket(void)
4904 old_umask
= umask(0);
4906 /* Create client tool unix socket */
4907 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4908 if (client_sock
< 0) {
4909 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4914 /* Set the cloexec flag */
4915 ret
= utils_set_fd_cloexec(client_sock
);
4917 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4918 "Continuing but note that the consumer daemon will have a "
4919 "reference to this socket on exec()", client_sock
);
4922 /* File permission MUST be 660 */
4923 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4925 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4930 /* Create the application unix socket */
4931 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4932 if (apps_sock
< 0) {
4933 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4938 /* Set the cloexec flag */
4939 ret
= utils_set_fd_cloexec(apps_sock
);
4941 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4942 "Continuing but note that the consumer daemon will have a "
4943 "reference to this socket on exec()", apps_sock
);
4946 /* File permission MUST be 666 */
4947 ret
= chmod(apps_unix_sock_path
,
4948 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4950 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4955 DBG3("Session daemon client socket %d and application socket %d created",
4956 client_sock
, apps_sock
);
4964 * Check if the global socket is available, and if a daemon is answering at the
4965 * other side. If yes, error is returned.
4967 static int check_existing_daemon(void)
4969 /* Is there anybody out there ? */
4970 if (lttng_session_daemon_alive()) {
4978 * Set the tracing group gid onto the client socket.
4980 * Race window between mkdir and chown is OK because we are going from more
4981 * permissive (root.root) to less permissive (root.tracing).
4983 static int set_permissions(char *rundir
)
4988 gid
= utils_get_group_id(tracing_group_name
);
4990 /* Set lttng run dir */
4991 ret
= chown(rundir
, 0, gid
);
4993 ERR("Unable to set group on %s", rundir
);
4998 * Ensure all applications and tracing group can search the run
4999 * dir. Allow everyone to read the directory, since it does not
5000 * buy us anything to hide its content.
5002 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5004 ERR("Unable to set permissions on %s", rundir
);
5008 /* lttng client socket path */
5009 ret
= chown(client_unix_sock_path
, 0, gid
);
5011 ERR("Unable to set group on %s", client_unix_sock_path
);
5015 /* kconsumer error socket path */
5016 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5018 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5022 /* 64-bit ustconsumer error socket path */
5023 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5025 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5029 /* 32-bit ustconsumer compat32 error socket path */
5030 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5032 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5036 DBG("All permissions are set");
5042 * Create the lttng run directory needed for all global sockets and pipe.
5044 static int create_lttng_rundir(const char *rundir
)
5048 DBG3("Creating LTTng run directory: %s", rundir
);
5050 ret
= mkdir(rundir
, S_IRWXU
);
5052 if (errno
!= EEXIST
) {
5053 ERR("Unable to create %s", rundir
);
5065 * Setup sockets and directory needed by the kconsumerd communication with the
5068 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5072 char path
[PATH_MAX
];
5074 switch (consumer_data
->type
) {
5075 case LTTNG_CONSUMER_KERNEL
:
5076 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5078 case LTTNG_CONSUMER64_UST
:
5079 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5081 case LTTNG_CONSUMER32_UST
:
5082 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5085 ERR("Consumer type unknown");
5090 DBG2("Creating consumer directory: %s", path
);
5092 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5094 if (errno
!= EEXIST
) {
5096 ERR("Failed to create %s", path
);
5102 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5104 ERR("Unable to set group on %s", path
);
5110 /* Create the kconsumerd error unix socket */
5111 consumer_data
->err_sock
=
5112 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5113 if (consumer_data
->err_sock
< 0) {
5114 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5120 * Set the CLOEXEC flag. Return code is useless because either way, the
5123 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5125 PERROR("utils_set_fd_cloexec");
5126 /* continue anyway */
5129 /* File permission MUST be 660 */
5130 ret
= chmod(consumer_data
->err_unix_sock_path
,
5131 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5133 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5143 * Signal handler for the daemon
5145 * Simply stop all worker threads, leaving main() return gracefully after
5146 * joining all threads and calling cleanup().
5148 static void sighandler(int sig
)
5152 DBG("SIGPIPE caught");
5155 DBG("SIGINT caught");
5159 DBG("SIGTERM caught");
5163 CMM_STORE_SHARED(recv_child_signal
, 1);
5171 * Setup signal handler for :
5172 * SIGINT, SIGTERM, SIGPIPE
5174 static int set_signal_handler(void)
5177 struct sigaction sa
;
5180 if ((ret
= sigemptyset(&sigset
)) < 0) {
5181 PERROR("sigemptyset");
5185 sa
.sa_handler
= sighandler
;
5186 sa
.sa_mask
= sigset
;
5188 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5189 PERROR("sigaction");
5193 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5194 PERROR("sigaction");
5198 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5199 PERROR("sigaction");
5203 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5204 PERROR("sigaction");
5208 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5214 * Set open files limit to unlimited. This daemon can open a large number of
5215 * file descriptors in order to consumer multiple kernel traces.
5217 static void set_ulimit(void)
5222 /* The kernel does not allowed an infinite limit for open files */
5223 lim
.rlim_cur
= 65535;
5224 lim
.rlim_max
= 65535;
5226 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5228 PERROR("failed to set open files limit");
5233 * Write pidfile using the rundir and opt_pidfile.
5235 static int write_pidfile(void)
5238 char pidfile_path
[PATH_MAX
];
5243 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
5245 /* Build pidfile path from rundir and opt_pidfile. */
5246 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5247 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5249 PERROR("snprintf pidfile path");
5255 * Create pid file in rundir.
5257 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5263 * Create lockfile using the rundir and return its fd.
5265 static int create_lockfile(void)
5268 char lockfile_path
[PATH_MAX
];
5270 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5275 ret
= utils_create_lock_file(lockfile_path
);
5281 * Write agent TCP port using the rundir.
5283 static int write_agent_port(void)
5286 char path
[PATH_MAX
];
5290 ret
= snprintf(path
, sizeof(path
), "%s/"
5291 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5293 PERROR("snprintf agent port path");
5298 * Create TCP agent port file in rundir.
5300 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5309 int main(int argc
, char **argv
)
5311 int ret
= 0, retval
= 0;
5313 const char *home_path
, *env_app_timeout
;
5315 init_kernel_workarounds();
5317 rcu_register_thread();
5319 if (set_signal_handler()) {
5321 goto exit_set_signal_handler
;
5324 setup_consumerd_path();
5326 page_size
= sysconf(_SC_PAGESIZE
);
5327 if (page_size
< 0) {
5328 PERROR("sysconf _SC_PAGESIZE");
5329 page_size
= LONG_MAX
;
5330 WARN("Fallback page size to %ld", page_size
);
5334 * Parse arguments and load the daemon configuration file.
5336 * We have an exit_options exit path to free memory reserved by
5337 * set_options. This is needed because the rest of sessiond_cleanup()
5338 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5339 * depends on set_options.
5342 if (set_options(argc
, argv
)) {
5348 if (opt_daemon
|| opt_background
) {
5351 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5359 * We are in the child. Make sure all other file descriptors are
5360 * closed, in case we are called with more opened file
5361 * descriptors than the standard ones.
5363 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5369 * Starting from here, we can create threads. This needs to be after
5370 * lttng_daemonize due to RCU.
5374 * Initialize the health check subsystem. This call should set the
5375 * appropriate time values.
5377 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5378 if (!health_sessiond
) {
5379 PERROR("health_app_create error");
5381 goto exit_health_sessiond_cleanup
;
5384 if (init_ht_cleanup_quit_pipe()) {
5386 goto exit_ht_cleanup_quit_pipe
;
5389 /* Setup the thread ht_cleanup communication pipe. */
5390 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5392 goto exit_ht_cleanup_pipe
;
5395 /* Set up max poll set size */
5396 if (lttng_poll_set_max_size()) {
5398 goto exit_set_max_size
;
5401 /* Create thread to clean up RCU hash tables */
5402 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5403 thread_ht_cleanup
, (void *) NULL
);
5406 PERROR("pthread_create ht_cleanup");
5408 goto exit_ht_cleanup
;
5411 /* Create thread quit pipe */
5412 if (init_thread_quit_pipe()) {
5414 goto exit_init_data
;
5417 /* Check if daemon is UID = 0 */
5418 is_root
= !getuid();
5421 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5424 goto exit_init_data
;
5427 /* Create global run dir with root access */
5428 if (create_lttng_rundir(rundir
)) {
5430 goto exit_init_data
;
5433 if (strlen(apps_unix_sock_path
) == 0) {
5434 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5435 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5438 goto exit_init_data
;
5442 if (strlen(client_unix_sock_path
) == 0) {
5443 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5444 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5447 goto exit_init_data
;
5451 /* Set global SHM for ust */
5452 if (strlen(wait_shm_path
) == 0) {
5453 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5454 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5457 goto exit_init_data
;
5461 if (strlen(health_unix_sock_path
) == 0) {
5462 ret
= snprintf(health_unix_sock_path
,
5463 sizeof(health_unix_sock_path
),
5464 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5467 goto exit_init_data
;
5471 /* Setup kernel consumerd path */
5472 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5473 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5476 goto exit_init_data
;
5478 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5479 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5482 goto exit_init_data
;
5485 DBG2("Kernel consumer err path: %s",
5486 kconsumer_data
.err_unix_sock_path
);
5487 DBG2("Kernel consumer cmd path: %s",
5488 kconsumer_data
.cmd_unix_sock_path
);
5490 home_path
= utils_get_home_dir();
5491 if (home_path
== NULL
) {
5492 /* TODO: Add --socket PATH option */
5493 ERR("Can't get HOME directory for sockets creation.");
5495 goto exit_init_data
;
5499 * Create rundir from home path. This will create something like
5502 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5505 goto exit_init_data
;
5508 if (create_lttng_rundir(rundir
)) {
5510 goto exit_init_data
;
5513 if (strlen(apps_unix_sock_path
) == 0) {
5514 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5515 DEFAULT_HOME_APPS_UNIX_SOCK
,
5519 goto exit_init_data
;
5523 /* Set the cli tool unix socket path */
5524 if (strlen(client_unix_sock_path
) == 0) {
5525 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5526 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5530 goto exit_init_data
;
5534 /* Set global SHM for ust */
5535 if (strlen(wait_shm_path
) == 0) {
5536 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5537 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5541 goto exit_init_data
;
5545 /* Set health check Unix path */
5546 if (strlen(health_unix_sock_path
) == 0) {
5547 ret
= snprintf(health_unix_sock_path
,
5548 sizeof(health_unix_sock_path
),
5549 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5553 goto exit_init_data
;
5558 lockfile_fd
= create_lockfile();
5559 if (lockfile_fd
< 0) {
5561 goto exit_init_data
;
5564 /* Set consumer initial state */
5565 kernel_consumerd_state
= CONSUMER_STOPPED
;
5566 ust_consumerd_state
= CONSUMER_STOPPED
;
5568 DBG("Client socket path %s", client_unix_sock_path
);
5569 DBG("Application socket path %s", apps_unix_sock_path
);
5570 DBG("Application wait path %s", wait_shm_path
);
5571 DBG("LTTng run directory path: %s", rundir
);
5573 /* 32 bits consumerd path setup */
5574 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5575 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5577 PERROR("snprintf 32-bit consumer error socket path");
5579 goto exit_init_data
;
5581 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5582 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5584 PERROR("snprintf 32-bit consumer command socket path");
5586 goto exit_init_data
;
5589 DBG2("UST consumer 32 bits err path: %s",
5590 ustconsumer32_data
.err_unix_sock_path
);
5591 DBG2("UST consumer 32 bits cmd path: %s",
5592 ustconsumer32_data
.cmd_unix_sock_path
);
5594 /* 64 bits consumerd path setup */
5595 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5596 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5598 PERROR("snprintf 64-bit consumer error socket path");
5600 goto exit_init_data
;
5602 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5603 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5605 PERROR("snprintf 64-bit consumer command socket path");
5607 goto exit_init_data
;
5610 DBG2("UST consumer 64 bits err path: %s",
5611 ustconsumer64_data
.err_unix_sock_path
);
5612 DBG2("UST consumer 64 bits cmd path: %s",
5613 ustconsumer64_data
.cmd_unix_sock_path
);
5616 * See if daemon already exist.
5618 if (check_existing_daemon()) {
5619 ERR("Already running daemon.\n");
5621 * We do not goto exit because we must not cleanup()
5622 * because a daemon is already running.
5625 goto exit_init_data
;
5629 * Init UST app hash table. Alloc hash table before this point since
5630 * cleanup() can get called after that point.
5632 if (ust_app_ht_alloc()) {
5633 ERR("Failed to allocate UST app hash table");
5635 goto exit_init_data
;
5638 /* Initialize agent domain subsystem. */
5639 if (agent_setup()) {
5640 /* ENOMEM at this point. */
5642 goto exit_init_data
;
5646 * These actions must be executed as root. We do that *after* setting up
5647 * the sockets path because we MUST make the check for another daemon using
5648 * those paths *before* trying to set the kernel consumer sockets and init
5652 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5654 goto exit_init_data
;
5657 /* Setup kernel tracer */
5658 if (!opt_no_kernel
) {
5659 init_kernel_tracer();
5660 if (kernel_tracer_fd
>= 0) {
5661 ret
= syscall_init_table();
5663 ERR("Unable to populate syscall table. "
5664 "Syscall tracing won't work "
5665 "for this session daemon.");
5670 /* Set ulimit for open files */
5673 /* init lttng_fd tracking must be done after set_ulimit. */
5676 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5678 goto exit_init_data
;
5681 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5683 goto exit_init_data
;
5686 /* Setup the needed unix socket */
5687 if (init_daemon_socket()) {
5689 goto exit_init_data
;
5692 /* Set credentials to socket */
5693 if (is_root
&& set_permissions(rundir
)) {
5695 goto exit_init_data
;
5698 /* Get parent pid if -S, --sig-parent is specified. */
5699 if (opt_sig_parent
) {
5703 /* Setup the kernel pipe for waking up the kernel thread */
5704 if (is_root
&& !opt_no_kernel
) {
5705 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5707 goto exit_init_data
;
5711 /* Setup the thread apps communication pipe. */
5712 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5714 goto exit_init_data
;
5717 /* Setup the thread apps notify communication pipe. */
5718 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5720 goto exit_init_data
;
5723 /* Initialize global buffer per UID and PID registry. */
5724 buffer_reg_init_uid_registry();
5725 buffer_reg_init_pid_registry();
5727 /* Init UST command queue. */
5728 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5731 * Get session list pointer. This pointer MUST NOT be free'd. This list
5732 * is statically declared in session.c
5734 session_list_ptr
= session_get_list();
5738 /* Check for the application socket timeout env variable. */
5739 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5740 if (env_app_timeout
) {
5741 app_socket_timeout
= atoi(env_app_timeout
);
5743 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5746 ret
= write_pidfile();
5748 ERR("Error in write_pidfile");
5750 goto exit_init_data
;
5752 ret
= write_agent_port();
5754 ERR("Error in write_agent_port");
5756 goto exit_init_data
;
5759 /* Initialize communication library */
5761 /* Initialize TCP timeout values */
5762 lttcomm_inet_init();
5764 if (load_session_init_data(&load_info
) < 0) {
5766 goto exit_init_data
;
5768 load_info
->path
= opt_load_session_path
;
5770 /* Create health-check thread */
5771 ret
= pthread_create(&health_thread
, NULL
,
5772 thread_manage_health
, (void *) NULL
);
5775 PERROR("pthread_create health");
5780 /* Create thread to manage the client socket */
5781 ret
= pthread_create(&client_thread
, NULL
,
5782 thread_manage_clients
, (void *) NULL
);
5785 PERROR("pthread_create clients");
5790 /* Create thread to dispatch registration */
5791 ret
= pthread_create(&dispatch_thread
, NULL
,
5792 thread_dispatch_ust_registration
, (void *) NULL
);
5795 PERROR("pthread_create dispatch");
5800 /* Create thread to manage application registration. */
5801 ret
= pthread_create(®_apps_thread
, NULL
,
5802 thread_registration_apps
, (void *) NULL
);
5805 PERROR("pthread_create registration");
5810 /* Create thread to manage application socket */
5811 ret
= pthread_create(&apps_thread
, NULL
,
5812 thread_manage_apps
, (void *) NULL
);
5815 PERROR("pthread_create apps");
5820 /* Create thread to manage application notify socket */
5821 ret
= pthread_create(&apps_notify_thread
, NULL
,
5822 ust_thread_manage_notify
, (void *) NULL
);
5825 PERROR("pthread_create notify");
5827 goto exit_apps_notify
;
5830 /* Create agent registration thread. */
5831 ret
= pthread_create(&agent_reg_thread
, NULL
,
5832 agent_thread_manage_registration
, (void *) NULL
);
5835 PERROR("pthread_create agent");
5837 goto exit_agent_reg
;
5840 /* Don't start this thread if kernel tracing is not requested nor root */
5841 if (is_root
&& !opt_no_kernel
) {
5842 /* Create kernel thread to manage kernel event */
5843 ret
= pthread_create(&kernel_thread
, NULL
,
5844 thread_manage_kernel
, (void *) NULL
);
5847 PERROR("pthread_create kernel");
5853 /* Create session loading thread. */
5854 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
5858 PERROR("pthread_create load_session_thread");
5860 goto exit_load_session
;
5864 * This is where we start awaiting program completion (e.g. through
5865 * signal that asks threads to teardown).
5868 ret
= pthread_join(load_session_thread
, &status
);
5871 PERROR("pthread_join load_session_thread");
5876 if (is_root
&& !opt_no_kernel
) {
5877 ret
= pthread_join(kernel_thread
, &status
);
5880 PERROR("pthread_join");
5886 ret
= pthread_join(agent_reg_thread
, &status
);
5889 PERROR("pthread_join agent");
5894 ret
= pthread_join(apps_notify_thread
, &status
);
5897 PERROR("pthread_join apps notify");
5902 ret
= pthread_join(apps_thread
, &status
);
5905 PERROR("pthread_join apps");
5910 ret
= pthread_join(reg_apps_thread
, &status
);
5913 PERROR("pthread_join");
5918 ret
= pthread_join(dispatch_thread
, &status
);
5921 PERROR("pthread_join");
5926 ret
= pthread_join(client_thread
, &status
);
5929 PERROR("pthread_join");
5934 ret
= pthread_join(health_thread
, &status
);
5937 PERROR("pthread_join health thread");
5944 * sessiond_cleanup() is called when no other thread is running, except
5945 * the ht_cleanup thread, which is needed to destroy the hash tables.
5947 rcu_thread_online();
5949 rcu_thread_offline();
5950 rcu_unregister_thread();
5952 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
5954 ERR("write error on ht_cleanup quit pipe");
5958 ret
= pthread_join(ht_cleanup_thread
, &status
);
5961 PERROR("pthread_join ht cleanup thread");
5967 utils_close_pipe(ht_cleanup_pipe
);
5968 exit_ht_cleanup_pipe
:
5971 * Close the ht_cleanup quit pipe.
5973 utils_close_pipe(ht_cleanup_quit_pipe
);
5974 exit_ht_cleanup_quit_pipe
:
5976 health_app_destroy(health_sessiond
);
5977 exit_health_sessiond_cleanup
:
5980 sessiond_cleanup_options();
5982 exit_set_signal_handler
: