2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/event-internal.h>
55 #include "lttng-sessiond.h"
56 #include "buffer-registry.h"
63 #include "kernel-consumer.h"
67 #include "ust-consumer.h"
70 #include "health-sessiond.h"
71 #include "testpoint.h"
72 #include "ust-thread.h"
73 #include "agent-thread.h"
75 #include "load-session-thread.h"
76 #include "notification-thread.h"
77 #include "notification-thread-commands.h"
78 #include "rotation-thread.h"
79 #include "lttng-syscall.h"
81 #include "ht-cleanup.h"
82 #include "sessiond-config.h"
88 #include "manage-apps.h"
90 static const char *help_msg
=
91 #ifdef LTTNG_EMBED_HELP
92 #include <lttng-sessiond.8.h>
99 static int lockfile_fd
= -1;
101 /* Set to 1 when a SIGUSR1 signal is received. */
102 static int recv_child_signal
;
104 /* Command line options */
105 static const struct option long_options
[] = {
106 { "client-sock", required_argument
, 0, 'c' },
107 { "apps-sock", required_argument
, 0, 'a' },
108 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
109 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
114 { "consumerd32-path", required_argument
, 0, '\0' },
115 { "consumerd32-libdir", required_argument
, 0, '\0' },
116 { "consumerd64-path", required_argument
, 0, '\0' },
117 { "consumerd64-libdir", required_argument
, 0, '\0' },
118 { "daemonize", no_argument
, 0, 'd' },
119 { "background", no_argument
, 0, 'b' },
120 { "sig-parent", no_argument
, 0, 'S' },
121 { "help", no_argument
, 0, 'h' },
122 { "group", required_argument
, 0, 'g' },
123 { "version", no_argument
, 0, 'V' },
124 { "quiet", no_argument
, 0, 'q' },
125 { "verbose", no_argument
, 0, 'v' },
126 { "verbose-consumer", no_argument
, 0, '\0' },
127 { "no-kernel", no_argument
, 0, '\0' },
128 { "pidfile", required_argument
, 0, 'p' },
129 { "agent-tcp-port", required_argument
, 0, '\0' },
130 { "config", required_argument
, 0, 'f' },
131 { "load", required_argument
, 0, 'l' },
132 { "kmod-probes", required_argument
, 0, '\0' },
133 { "extra-kmod-probes", required_argument
, 0, '\0' },
137 /* Command line options to ignore from configuration file */
138 static const char *config_ignore_options
[] = { "help", "version", "config" };
141 * This pipe is used to inform the thread managing application communication
142 * that a command is queued and ready to be processed.
144 static int apps_cmd_pipe
[2] = { -1, -1 };
146 /* Pthread, Mutexes and Semaphores */
147 static pthread_t apps_notify_thread
;
148 static pthread_t kernel_thread
;
149 static pthread_t agent_reg_thread
;
150 static pthread_t load_session_thread
;
153 * UST registration command queue. This queue is tied with a futex and uses a N
154 * wakers / 1 waiter implemented and detailed in futex.c/.h
156 * The thread_registration_apps and thread_dispatch_ust_registration uses this
157 * queue along with the wait/wake scheme. The thread_manage_apps receives down
158 * the line new application socket and monitors it for any I/O error or clean
159 * close that triggers an unregistration of the application.
161 static struct ust_cmd_queue ust_cmd_queue
;
163 static const char *module_proc_lttng
= "/proc/lttng";
165 /* Load session thread information to operate. */
166 static struct load_session_thread_data
*load_info
;
169 * Section name to look for in the daemon configuration file.
171 static const char * const config_section_name
= "sessiond";
173 /* Am I root or not. Set to 1 if the daemon is running as root */
177 * Stop all threads by closing the thread quit pipe.
179 static void stop_threads(void)
183 /* Stopping all threads */
184 DBG("Terminating all threads");
185 ret
= sessiond_notify_quit_pipe();
187 ERR("write error on thread quit pipe");
192 * Close every consumer sockets.
194 static void close_consumer_sockets(void)
198 if (kconsumer_data
.err_sock
>= 0) {
199 ret
= close(kconsumer_data
.err_sock
);
201 PERROR("kernel consumer err_sock close");
204 if (ustconsumer32_data
.err_sock
>= 0) {
205 ret
= close(ustconsumer32_data
.err_sock
);
207 PERROR("UST consumerd32 err_sock close");
210 if (ustconsumer64_data
.err_sock
>= 0) {
211 ret
= close(ustconsumer64_data
.err_sock
);
213 PERROR("UST consumerd64 err_sock close");
216 if (kconsumer_data
.cmd_sock
>= 0) {
217 ret
= close(kconsumer_data
.cmd_sock
);
219 PERROR("kernel consumer cmd_sock close");
222 if (ustconsumer32_data
.cmd_sock
>= 0) {
223 ret
= close(ustconsumer32_data
.cmd_sock
);
225 PERROR("UST consumerd32 cmd_sock close");
228 if (ustconsumer64_data
.cmd_sock
>= 0) {
229 ret
= close(ustconsumer64_data
.cmd_sock
);
231 PERROR("UST consumerd64 cmd_sock close");
234 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
235 ret
= close(kconsumer_data
.channel_monitor_pipe
);
237 PERROR("kernel consumer channel monitor pipe close");
240 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
241 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
243 PERROR("UST consumerd32 channel monitor pipe close");
246 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
247 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
249 PERROR("UST consumerd64 channel monitor pipe close");
255 * Wait on consumer process termination.
257 * Need to be called with the consumer data lock held or from a context
258 * ensuring no concurrent access to data (e.g: cleanup).
260 static void wait_consumer(struct consumer_data
*consumer_data
)
265 if (consumer_data
->pid
<= 0) {
269 DBG("Waiting for complete teardown of consumerd (PID: %d)",
271 ret
= waitpid(consumer_data
->pid
, &status
, 0);
273 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
274 } else if (!WIFEXITED(status
)) {
275 ERR("consumerd termination with error: %d",
278 consumer_data
->pid
= 0;
282 * Cleanup the session daemon's data structures.
284 static void sessiond_cleanup(void)
287 struct ltt_session_list
*session_list
= session_get_list();
289 DBG("Cleanup sessiond");
292 * Close the thread quit pipe. It has already done its job,
293 * since we are now called.
295 sessiond_close_quit_pipe();
297 ret
= remove(config
.pid_file_path
.value
);
299 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
302 utils_close_pipe(apps_cmd_pipe
);
304 DBG("Removing sessiond and consumerd content of directory %s",
305 config
.rundir
.value
);
308 DBG("Removing %s", config
.pid_file_path
.value
);
309 (void) unlink(config
.pid_file_path
.value
);
311 DBG("Removing %s", config
.agent_port_file_path
.value
);
312 (void) unlink(config
.agent_port_file_path
.value
);
315 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
316 (void) unlink(kconsumer_data
.err_unix_sock_path
);
318 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
319 (void) rmdir(config
.kconsumerd_path
.value
);
321 /* ust consumerd 32 */
322 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
323 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
325 DBG("Removing directory %s", config
.consumerd32_path
.value
);
326 (void) rmdir(config
.consumerd32_path
.value
);
328 /* ust consumerd 64 */
329 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
330 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
332 DBG("Removing directory %s", config
.consumerd64_path
.value
);
333 (void) rmdir(config
.consumerd64_path
.value
);
335 pthread_mutex_destroy(&session_list
->lock
);
337 wait_consumer(&kconsumer_data
);
338 wait_consumer(&ustconsumer64_data
);
339 wait_consumer(&ustconsumer32_data
);
341 DBG("Cleaning up all agent apps");
342 agent_app_ht_clean();
344 DBG("Closing all UST sockets");
345 ust_app_clean_list();
346 buffer_reg_destroy_registries();
348 if (is_root
&& !config
.no_kernel
) {
349 DBG2("Closing kernel fd");
350 if (kernel_tracer_fd
>= 0) {
351 ret
= close(kernel_tracer_fd
);
356 DBG("Unloading kernel modules");
357 modprobe_remove_lttng_all();
361 close_consumer_sockets();
364 load_session_destroy_data(load_info
);
369 * We do NOT rmdir rundir because there are other processes
370 * using it, for instance lttng-relayd, which can start in
371 * parallel with this teardown.
376 * Cleanup the daemon's option data structures.
378 static void sessiond_cleanup_options(void)
380 DBG("Cleaning up options");
382 sessiond_config_fini(&config
);
384 run_as_destroy_worker();
388 * Update the kernel poll set of all channel fd available over all tracing
389 * session. Add the wakeup pipe at the end of the set.
391 static int update_kernel_poll(struct lttng_poll_event
*events
)
394 struct ltt_kernel_channel
*channel
;
395 struct ltt_session
*session
;
396 const struct ltt_session_list
*session_list
= session_get_list();
398 DBG("Updating kernel poll set");
401 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
402 if (!session_get(session
)) {
405 session_lock(session
);
406 if (session
->kernel_session
== NULL
) {
407 session_unlock(session
);
408 session_put(session
);
412 cds_list_for_each_entry(channel
,
413 &session
->kernel_session
->channel_list
.head
, list
) {
414 /* Add channel fd to the kernel poll set */
415 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
417 session_unlock(session
);
418 session_put(session
);
421 DBG("Channel fd %d added to kernel set", channel
->fd
);
423 session_unlock(session
);
425 session_unlock_list();
430 session_unlock_list();
435 * Find the channel fd from 'fd' over all tracing session. When found, check
436 * for new channel stream and send those stream fds to the kernel consumer.
438 * Useful for CPU hotplug feature.
440 static int update_kernel_stream(int fd
)
443 struct ltt_session
*session
;
444 struct ltt_kernel_session
*ksess
;
445 struct ltt_kernel_channel
*channel
;
446 const struct ltt_session_list
*session_list
= session_get_list();
448 DBG("Updating kernel streams for channel fd %d", fd
);
451 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
452 if (!session_get(session
)) {
455 session_lock(session
);
456 if (session
->kernel_session
== NULL
) {
457 session_unlock(session
);
458 session_put(session
);
461 ksess
= session
->kernel_session
;
463 cds_list_for_each_entry(channel
,
464 &ksess
->channel_list
.head
, list
) {
465 struct lttng_ht_iter iter
;
466 struct consumer_socket
*socket
;
468 if (channel
->fd
!= fd
) {
471 DBG("Channel found, updating kernel streams");
472 ret
= kernel_open_channel_stream(channel
);
476 /* Update the stream global counter */
477 ksess
->stream_count_global
+= ret
;
480 * Have we already sent fds to the consumer? If yes, it
481 * means that tracing is started so it is safe to send
482 * our updated stream fds.
484 if (ksess
->consumer_fds_sent
!= 1
485 || ksess
->consumer
== NULL
) {
491 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
492 &iter
.iter
, socket
, node
.node
) {
493 pthread_mutex_lock(socket
->lock
);
494 ret
= kernel_consumer_send_channel_streams(socket
,
496 session
->output_traces
? 1 : 0);
497 pthread_mutex_unlock(socket
->lock
);
505 session_unlock(session
);
506 session_put(session
);
508 session_unlock_list();
512 session_unlock(session
);
513 session_put(session
);
514 session_unlock_list();
519 * This thread manage event coming from the kernel.
521 * Features supported in this thread:
524 static void *thread_manage_kernel(void *data
)
526 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
527 uint32_t revents
, nb_fd
;
529 struct lttng_poll_event events
;
531 DBG("[thread] Thread manage kernel started");
533 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
536 * This first step of the while is to clean this structure which could free
537 * non NULL pointers so initialize it before the loop.
539 lttng_poll_init(&events
);
541 if (testpoint(sessiond_thread_manage_kernel
)) {
542 goto error_testpoint
;
545 health_code_update();
547 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
548 goto error_testpoint
;
552 health_code_update();
554 if (update_poll_flag
== 1) {
555 /* Clean events object. We are about to populate it again. */
556 lttng_poll_clean(&events
);
558 ret
= sessiond_set_thread_pollset(&events
, 2);
560 goto error_poll_create
;
563 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
568 /* This will add the available kernel channel if any. */
569 ret
= update_kernel_poll(&events
);
573 update_poll_flag
= 0;
576 DBG("Thread kernel polling");
578 /* Poll infinite value of time */
581 ret
= lttng_poll_wait(&events
, -1);
582 DBG("Thread kernel return from poll on %d fds",
583 LTTNG_POLL_GETNB(&events
));
587 * Restart interrupted system call.
589 if (errno
== EINTR
) {
593 } else if (ret
== 0) {
594 /* Should not happen since timeout is infinite */
595 ERR("Return value of poll is 0 with an infinite timeout.\n"
596 "This should not have happened! Continuing...");
602 for (i
= 0; i
< nb_fd
; i
++) {
603 /* Fetch once the poll data */
604 revents
= LTTNG_POLL_GETEV(&events
, i
);
605 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
607 health_code_update();
610 /* No activity for this FD (poll implementation). */
614 /* Thread quit pipe has been closed. Killing thread. */
615 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
621 /* Check for data on kernel pipe */
622 if (revents
& LPOLLIN
) {
623 if (pollfd
== kernel_poll_pipe
[0]) {
624 (void) lttng_read(kernel_poll_pipe
[0],
627 * Ret value is useless here, if this pipe gets any actions an
628 * update is required anyway.
630 update_poll_flag
= 1;
634 * New CPU detected by the kernel. Adding kernel stream to
635 * kernel session and updating the kernel consumer
637 ret
= update_kernel_stream(pollfd
);
643 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
644 update_poll_flag
= 1;
647 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
655 lttng_poll_clean(&events
);
658 utils_close_pipe(kernel_poll_pipe
);
659 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
662 ERR("Health error occurred in %s", __func__
);
663 WARN("Kernel thread died unexpectedly. "
664 "Kernel tracing can continue but CPU hotplug is disabled.");
666 health_unregister(health_sessiond
);
667 DBG("Kernel thread dying");
672 * Signal pthread condition of the consumer data that the thread.
674 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
676 pthread_mutex_lock(&data
->cond_mutex
);
679 * The state is set before signaling. It can be any value, it's the waiter
680 * job to correctly interpret this condition variable associated to the
681 * consumer pthread_cond.
683 * A value of 0 means that the corresponding thread of the consumer data
684 * was not started. 1 indicates that the thread has started and is ready
685 * for action. A negative value means that there was an error during the
688 data
->consumer_thread_is_ready
= state
;
689 (void) pthread_cond_signal(&data
->cond
);
691 pthread_mutex_unlock(&data
->cond_mutex
);
695 * This thread manage the consumer error sent back to the session daemon.
697 void *thread_manage_consumer(void *data
)
699 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
700 uint32_t revents
, nb_fd
;
701 enum lttcomm_return_code code
;
702 struct lttng_poll_event events
;
703 struct consumer_data
*consumer_data
= data
;
704 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
706 DBG("[thread] Manage consumer started");
708 rcu_register_thread();
711 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
713 health_code_update();
716 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
717 * metadata_sock. Nothing more will be added to this poll set.
719 ret
= sessiond_set_thread_pollset(&events
, 3);
725 * The error socket here is already in a listening state which was done
726 * just before spawning this thread to avoid a race between the consumer
727 * daemon exec trying to connect and the listen() call.
729 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
734 health_code_update();
736 /* Infinite blocking call, waiting for transmission */
740 if (testpoint(sessiond_thread_manage_consumer
)) {
744 ret
= lttng_poll_wait(&events
, -1);
748 * Restart interrupted system call.
750 if (errno
== EINTR
) {
758 for (i
= 0; i
< nb_fd
; i
++) {
759 /* Fetch once the poll data */
760 revents
= LTTNG_POLL_GETEV(&events
, i
);
761 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
763 health_code_update();
766 /* No activity for this FD (poll implementation). */
770 /* Thread quit pipe has been closed. Killing thread. */
771 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
777 /* Event on the registration socket */
778 if (pollfd
== consumer_data
->err_sock
) {
779 if (revents
& LPOLLIN
) {
781 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
782 ERR("consumer err socket poll error");
785 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
791 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
797 * Set the CLOEXEC flag. Return code is useless because either way, the
800 (void) utils_set_fd_cloexec(sock
);
802 health_code_update();
804 DBG2("Receiving code from consumer err_sock");
806 /* Getting status code from kconsumerd */
807 ret
= lttcomm_recv_unix_sock(sock
, &code
,
808 sizeof(enum lttcomm_return_code
));
813 health_code_update();
814 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
815 ERR("consumer error when waiting for SOCK_READY : %s",
816 lttcomm_get_readable_code(-code
));
820 /* Connect both command and metadata sockets. */
821 consumer_data
->cmd_sock
=
822 lttcomm_connect_unix_sock(
823 consumer_data
->cmd_unix_sock_path
);
824 consumer_data
->metadata_fd
=
825 lttcomm_connect_unix_sock(
826 consumer_data
->cmd_unix_sock_path
);
827 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
828 PERROR("consumer connect cmd socket");
829 /* On error, signal condition and quit. */
830 signal_consumer_condition(consumer_data
, -1);
834 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
836 /* Create metadata socket lock. */
837 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
838 if (consumer_data
->metadata_sock
.lock
== NULL
) {
839 PERROR("zmalloc pthread mutex");
842 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
844 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
845 DBG("Consumer metadata socket ready (fd: %d)",
846 consumer_data
->metadata_fd
);
849 * Remove the consumerd error sock since we've established a connection.
851 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
856 /* Add new accepted error socket. */
857 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
862 /* Add metadata socket that is successfully connected. */
863 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
864 LPOLLIN
| LPOLLRDHUP
);
869 health_code_update();
872 * Transfer the write-end of the channel monitoring and rotate pipe
873 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
875 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
876 if (!cmd_socket_wrapper
) {
879 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
881 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
882 consumer_data
->channel_monitor_pipe
);
887 /* Discard the socket wrapper as it is no longer needed. */
888 consumer_destroy_socket(cmd_socket_wrapper
);
889 cmd_socket_wrapper
= NULL
;
891 /* The thread is completely initialized, signal that it is ready. */
892 signal_consumer_condition(consumer_data
, 1);
894 /* Infinite blocking call, waiting for transmission */
897 health_code_update();
899 /* Exit the thread because the thread quit pipe has been triggered. */
901 /* Not a health error. */
907 ret
= lttng_poll_wait(&events
, -1);
911 * Restart interrupted system call.
913 if (errno
== EINTR
) {
921 for (i
= 0; i
< nb_fd
; i
++) {
922 /* Fetch once the poll data */
923 revents
= LTTNG_POLL_GETEV(&events
, i
);
924 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
926 health_code_update();
929 /* No activity for this FD (poll implementation). */
934 * Thread quit pipe has been triggered, flag that we should stop
935 * but continue the current loop to handle potential data from
938 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
940 if (pollfd
== sock
) {
941 /* Event on the consumerd socket */
942 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
943 && !(revents
& LPOLLIN
)) {
944 ERR("consumer err socket second poll error");
947 health_code_update();
948 /* Wait for any kconsumerd error */
949 ret
= lttcomm_recv_unix_sock(sock
, &code
,
950 sizeof(enum lttcomm_return_code
));
952 ERR("consumer closed the command socket");
956 ERR("consumer return code : %s",
957 lttcomm_get_readable_code(-code
));
960 } else if (pollfd
== consumer_data
->metadata_fd
) {
961 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
962 && !(revents
& LPOLLIN
)) {
963 ERR("consumer err metadata socket second poll error");
966 /* UST metadata requests */
967 ret
= ust_consumer_metadata_request(
968 &consumer_data
->metadata_sock
);
970 ERR("Handling metadata request");
974 /* No need for an else branch all FDs are tested prior. */
976 health_code_update();
982 * We lock here because we are about to close the sockets and some other
983 * thread might be using them so get exclusive access which will abort all
984 * other consumer command by other threads.
986 pthread_mutex_lock(&consumer_data
->lock
);
988 /* Immediately set the consumerd state to stopped */
989 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
990 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
991 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
992 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
993 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
995 /* Code flow error... */
999 if (consumer_data
->err_sock
>= 0) {
1000 ret
= close(consumer_data
->err_sock
);
1004 consumer_data
->err_sock
= -1;
1006 if (consumer_data
->cmd_sock
>= 0) {
1007 ret
= close(consumer_data
->cmd_sock
);
1011 consumer_data
->cmd_sock
= -1;
1013 if (consumer_data
->metadata_sock
.fd_ptr
&&
1014 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1015 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1027 unlink(consumer_data
->err_unix_sock_path
);
1028 unlink(consumer_data
->cmd_unix_sock_path
);
1029 pthread_mutex_unlock(&consumer_data
->lock
);
1031 /* Cleanup metadata socket mutex. */
1032 if (consumer_data
->metadata_sock
.lock
) {
1033 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1034 free(consumer_data
->metadata_sock
.lock
);
1036 lttng_poll_clean(&events
);
1038 if (cmd_socket_wrapper
) {
1039 consumer_destroy_socket(cmd_socket_wrapper
);
1044 ERR("Health error occurred in %s", __func__
);
1046 health_unregister(health_sessiond
);
1047 DBG("consumer thread cleanup completed");
1049 rcu_thread_offline();
1050 rcu_unregister_thread();
1056 * Setup necessary data for kernel tracer action.
1058 static int init_kernel_tracer(void)
1062 /* Modprobe lttng kernel modules */
1063 ret
= modprobe_lttng_control();
1068 /* Open debugfs lttng */
1069 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1070 if (kernel_tracer_fd
< 0) {
1071 DBG("Failed to open %s", module_proc_lttng
);
1075 /* Validate kernel version */
1076 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
1077 &kernel_tracer_abi_version
);
1082 ret
= modprobe_lttng_data();
1087 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1094 WARN("Kernel tracer does not support buffer monitoring. "
1095 "The monitoring timer of channels in the kernel domain "
1096 "will be set to 0 (disabled).");
1099 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1103 modprobe_remove_lttng_control();
1104 ret
= close(kernel_tracer_fd
);
1108 kernel_tracer_fd
= -1;
1109 return LTTNG_ERR_KERN_VERSION
;
1112 ret
= close(kernel_tracer_fd
);
1118 modprobe_remove_lttng_control();
1121 WARN("No kernel tracer available");
1122 kernel_tracer_fd
= -1;
1124 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1126 return LTTNG_ERR_KERN_NA
;
1130 static int string_match(const char *str1
, const char *str2
)
1132 return (str1
&& str2
) && !strcmp(str1
, str2
);
1136 * Take an option from the getopt output and set it in the right variable to be
1139 * Return 0 on success else a negative value.
1141 static int set_option(int opt
, const char *arg
, const char *optname
)
1145 if (string_match(optname
, "client-sock") || opt
== 'c') {
1146 if (!arg
|| *arg
== '\0') {
1150 if (lttng_is_setuid_setgid()) {
1151 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1152 "-c, --client-sock");
1154 config_string_set(&config
.client_unix_sock_path
,
1156 if (!config
.client_unix_sock_path
.value
) {
1161 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
1162 if (!arg
|| *arg
== '\0') {
1166 if (lttng_is_setuid_setgid()) {
1167 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1170 config_string_set(&config
.apps_unix_sock_path
,
1172 if (!config
.apps_unix_sock_path
.value
) {
1177 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
1178 config
.daemonize
= true;
1179 } else if (string_match(optname
, "background") || opt
== 'b') {
1180 config
.background
= true;
1181 } else if (string_match(optname
, "group") || opt
== 'g') {
1182 if (!arg
|| *arg
== '\0') {
1186 if (lttng_is_setuid_setgid()) {
1187 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1190 config_string_set(&config
.tracing_group_name
,
1192 if (!config
.tracing_group_name
.value
) {
1197 } else if (string_match(optname
, "help") || opt
== 'h') {
1198 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
1200 ERR("Cannot show --help for `lttng-sessiond`");
1203 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1204 } else if (string_match(optname
, "version") || opt
== 'V') {
1205 fprintf(stdout
, "%s\n", VERSION
);
1207 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
1208 config
.sig_parent
= true;
1209 } else if (string_match(optname
, "kconsumerd-err-sock")) {
1210 if (!arg
|| *arg
== '\0') {
1214 if (lttng_is_setuid_setgid()) {
1215 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1216 "--kconsumerd-err-sock");
1218 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
1220 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
1225 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
1226 if (!arg
|| *arg
== '\0') {
1230 if (lttng_is_setuid_setgid()) {
1231 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1232 "--kconsumerd-cmd-sock");
1234 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
1236 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
1241 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
1242 if (!arg
|| *arg
== '\0') {
1246 if (lttng_is_setuid_setgid()) {
1247 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1248 "--ustconsumerd64-err-sock");
1250 config_string_set(&config
.consumerd64_err_unix_sock_path
,
1252 if (!config
.consumerd64_err_unix_sock_path
.value
) {
1257 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
1258 if (!arg
|| *arg
== '\0') {
1262 if (lttng_is_setuid_setgid()) {
1263 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1264 "--ustconsumerd64-cmd-sock");
1266 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
1268 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
1273 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
1274 if (!arg
|| *arg
== '\0') {
1278 if (lttng_is_setuid_setgid()) {
1279 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1280 "--ustconsumerd32-err-sock");
1282 config_string_set(&config
.consumerd32_err_unix_sock_path
,
1284 if (!config
.consumerd32_err_unix_sock_path
.value
) {
1289 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
1290 if (!arg
|| *arg
== '\0') {
1294 if (lttng_is_setuid_setgid()) {
1295 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1296 "--ustconsumerd32-cmd-sock");
1298 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
1300 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
1305 } else if (string_match(optname
, "no-kernel")) {
1306 config
.no_kernel
= true;
1307 } else if (string_match(optname
, "quiet") || opt
== 'q') {
1308 config
.quiet
= true;
1309 } else if (string_match(optname
, "verbose") || opt
== 'v') {
1310 /* Verbose level can increase using multiple -v */
1312 /* Value obtained from config file */
1313 config
.verbose
= config_parse_value(arg
);
1315 /* -v used on command line */
1318 /* Clamp value to [0, 3] */
1319 config
.verbose
= config
.verbose
< 0 ? 0 :
1320 (config
.verbose
<= 3 ? config
.verbose
: 3);
1321 } else if (string_match(optname
, "verbose-consumer")) {
1323 config
.verbose_consumer
= config_parse_value(arg
);
1325 config
.verbose_consumer
++;
1327 } else if (string_match(optname
, "consumerd32-path")) {
1328 if (!arg
|| *arg
== '\0') {
1332 if (lttng_is_setuid_setgid()) {
1333 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1334 "--consumerd32-path");
1336 config_string_set(&config
.consumerd32_bin_path
,
1338 if (!config
.consumerd32_bin_path
.value
) {
1343 } else if (string_match(optname
, "consumerd32-libdir")) {
1344 if (!arg
|| *arg
== '\0') {
1348 if (lttng_is_setuid_setgid()) {
1349 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1350 "--consumerd32-libdir");
1352 config_string_set(&config
.consumerd32_lib_dir
,
1354 if (!config
.consumerd32_lib_dir
.value
) {
1359 } else if (string_match(optname
, "consumerd64-path")) {
1360 if (!arg
|| *arg
== '\0') {
1364 if (lttng_is_setuid_setgid()) {
1365 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1366 "--consumerd64-path");
1368 config_string_set(&config
.consumerd64_bin_path
,
1370 if (!config
.consumerd64_bin_path
.value
) {
1375 } else if (string_match(optname
, "consumerd64-libdir")) {
1376 if (!arg
|| *arg
== '\0') {
1380 if (lttng_is_setuid_setgid()) {
1381 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1382 "--consumerd64-libdir");
1384 config_string_set(&config
.consumerd64_lib_dir
,
1386 if (!config
.consumerd64_lib_dir
.value
) {
1391 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
1392 if (!arg
|| *arg
== '\0') {
1396 if (lttng_is_setuid_setgid()) {
1397 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1400 config_string_set(&config
.pid_file_path
, strdup(arg
));
1401 if (!config
.pid_file_path
.value
) {
1406 } else if (string_match(optname
, "agent-tcp-port")) {
1407 if (!arg
|| *arg
== '\0') {
1411 if (lttng_is_setuid_setgid()) {
1412 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1413 "--agent-tcp-port");
1418 v
= strtoul(arg
, NULL
, 0);
1419 if (errno
!= 0 || !isdigit(arg
[0])) {
1420 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
1423 if (v
== 0 || v
>= 65535) {
1424 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
1427 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
1428 DBG3("Agent TCP port set to non default: %i", (int) v
);
1430 } else if (string_match(optname
, "load") || opt
== 'l') {
1431 if (!arg
|| *arg
== '\0') {
1435 if (lttng_is_setuid_setgid()) {
1436 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1439 config_string_set(&config
.load_session_path
, strdup(arg
));
1440 if (!config
.load_session_path
.value
) {
1445 } else if (string_match(optname
, "kmod-probes")) {
1446 if (!arg
|| *arg
== '\0') {
1450 if (lttng_is_setuid_setgid()) {
1451 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1454 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
1455 if (!config
.kmod_probes_list
.value
) {
1460 } else if (string_match(optname
, "extra-kmod-probes")) {
1461 if (!arg
|| *arg
== '\0') {
1465 if (lttng_is_setuid_setgid()) {
1466 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1467 "--extra-kmod-probes");
1469 config_string_set(&config
.kmod_extra_probes_list
,
1471 if (!config
.kmod_extra_probes_list
.value
) {
1476 } else if (string_match(optname
, "config") || opt
== 'f') {
1477 /* This is handled in set_options() thus silent skip. */
1480 /* Unknown option or other error.
1481 * Error is printed by getopt, just return */
1486 if (ret
== -EINVAL
) {
1487 const char *opt_name
= "unknown";
1490 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
1492 if (opt
== long_options
[i
].val
) {
1493 opt_name
= long_options
[i
].name
;
1498 WARN("Invalid argument provided for option \"%s\", using default value.",
1506 * config_entry_handler_cb used to handle options read from a config file.
1507 * See config_entry_handler_cb comment in common/config/session-config.h for the
1508 * return value conventions.
1510 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
1514 if (!entry
|| !entry
->name
|| !entry
->value
) {
1519 /* Check if the option is to be ignored */
1520 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
1521 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
1526 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
1529 /* Ignore if not fully matched. */
1530 if (strcmp(entry
->name
, long_options
[i
].name
)) {
1535 * If the option takes no argument on the command line, we have to
1536 * check if the value is "true". We support non-zero numeric values,
1539 if (!long_options
[i
].has_arg
) {
1540 ret
= config_parse_value(entry
->value
);
1543 WARN("Invalid configuration value \"%s\" for option %s",
1544 entry
->value
, entry
->name
);
1546 /* False, skip boolean config option. */
1551 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
1555 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
1562 * daemon configuration loading and argument parsing
1564 static int set_options(int argc
, char **argv
)
1566 int ret
= 0, c
= 0, option_index
= 0;
1567 int orig_optopt
= optopt
, orig_optind
= optind
;
1569 const char *config_path
= NULL
;
1571 optstring
= utils_generate_optstring(long_options
,
1572 sizeof(long_options
) / sizeof(struct option
));
1578 /* Check for the --config option */
1579 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
1580 &option_index
)) != -1) {
1584 } else if (c
!= 'f') {
1585 /* if not equal to --config option. */
1589 if (lttng_is_setuid_setgid()) {
1590 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1593 config_path
= utils_expand_path(optarg
);
1595 ERR("Failed to resolve path: %s", optarg
);
1600 ret
= config_get_section_entries(config_path
, config_section_name
,
1601 config_entry_handler
, NULL
);
1604 ERR("Invalid configuration option at line %i", ret
);
1610 /* Reset getopt's global state */
1611 optopt
= orig_optopt
;
1612 optind
= orig_optind
;
1616 * getopt_long() will not set option_index if it encounters a
1619 c
= getopt_long(argc
, argv
, optstring
, long_options
,
1626 * Pass NULL as the long option name if popt left the index
1629 ret
= set_option(c
, optarg
,
1630 option_index
< 0 ? NULL
:
1631 long_options
[option_index
].name
);
1643 * Create lockfile using the rundir and return its fd.
1645 static int create_lockfile(void)
1647 return utils_create_lock_file(config
.lock_file_path
.value
);
1651 * Check if the global socket is available, and if a daemon is answering at the
1652 * other side. If yes, error is returned.
1654 * Also attempts to create and hold the lock file.
1656 static int check_existing_daemon(void)
1660 /* Is there anybody out there ? */
1661 if (lttng_session_daemon_alive()) {
1666 lockfile_fd
= create_lockfile();
1667 if (lockfile_fd
< 0) {
1675 static void sessiond_cleanup_lock_file(void)
1680 * Cleanup lock file by deleting it and finaly closing it which will
1681 * release the file system lock.
1683 if (lockfile_fd
>= 0) {
1684 ret
= remove(config
.lock_file_path
.value
);
1686 PERROR("remove lock file");
1688 ret
= close(lockfile_fd
);
1690 PERROR("close lock file");
1696 * Set the tracing group gid onto the client socket.
1698 * Race window between mkdir and chown is OK because we are going from more
1699 * permissive (root.root) to less permissive (root.tracing).
1701 static int set_permissions(char *rundir
)
1706 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
1708 /* Set lttng run dir */
1709 ret
= chown(rundir
, 0, gid
);
1711 ERR("Unable to set group on %s", rundir
);
1716 * Ensure all applications and tracing group can search the run
1717 * dir. Allow everyone to read the directory, since it does not
1718 * buy us anything to hide its content.
1720 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1722 ERR("Unable to set permissions on %s", rundir
);
1726 /* lttng client socket path */
1727 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
1729 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
1733 /* kconsumer error socket path */
1734 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
1736 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
1740 /* 64-bit ustconsumer error socket path */
1741 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1743 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
1747 /* 32-bit ustconsumer compat32 error socket path */
1748 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1750 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
1754 DBG("All permissions are set");
1760 * Create the lttng run directory needed for all global sockets and pipe.
1762 static int create_lttng_rundir(void)
1766 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1768 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1770 if (errno
!= EEXIST
) {
1771 ERR("Unable to create %s", config
.rundir
.value
);
1783 * Setup sockets and directory needed by the consumerds' communication with the
1786 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1791 switch (consumer_data
->type
) {
1792 case LTTNG_CONSUMER_KERNEL
:
1793 path
= config
.kconsumerd_path
.value
;
1795 case LTTNG_CONSUMER64_UST
:
1796 path
= config
.consumerd64_path
.value
;
1798 case LTTNG_CONSUMER32_UST
:
1799 path
= config
.consumerd32_path
.value
;
1802 ERR("Consumer type unknown");
1808 DBG2("Creating consumer directory: %s", path
);
1810 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1811 if (ret
< 0 && errno
!= EEXIST
) {
1813 ERR("Failed to create %s", path
);
1817 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
1819 ERR("Unable to set group on %s", path
);
1825 /* Create the consumerd error unix socket */
1826 consumer_data
->err_sock
=
1827 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1828 if (consumer_data
->err_sock
< 0) {
1829 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1835 * Set the CLOEXEC flag. Return code is useless because either way, the
1838 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1840 PERROR("utils_set_fd_cloexec");
1841 /* continue anyway */
1844 /* File permission MUST be 660 */
1845 ret
= chmod(consumer_data
->err_unix_sock_path
,
1846 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1848 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1858 * Signal handler for the daemon
1860 * Simply stop all worker threads, leaving main() return gracefully after
1861 * joining all threads and calling cleanup().
1863 static void sighandler(int sig
)
1867 DBG("SIGINT caught");
1871 DBG("SIGTERM caught");
1875 CMM_STORE_SHARED(recv_child_signal
, 1);
1883 * Setup signal handler for :
1884 * SIGINT, SIGTERM, SIGPIPE
1886 static int set_signal_handler(void)
1889 struct sigaction sa
;
1892 if ((ret
= sigemptyset(&sigset
)) < 0) {
1893 PERROR("sigemptyset");
1897 sa
.sa_mask
= sigset
;
1900 sa
.sa_handler
= sighandler
;
1901 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1902 PERROR("sigaction");
1906 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1907 PERROR("sigaction");
1911 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1912 PERROR("sigaction");
1916 sa
.sa_handler
= SIG_IGN
;
1917 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1918 PERROR("sigaction");
1922 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1928 * Set open files limit to unlimited. This daemon can open a large number of
1929 * file descriptors in order to consume multiple kernel traces.
1931 static void set_ulimit(void)
1936 /* The kernel does not allow an infinite limit for open files */
1937 lim
.rlim_cur
= 65535;
1938 lim
.rlim_max
= 65535;
1940 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1942 PERROR("failed to set open files limit");
1946 static int write_pidfile(void)
1948 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1951 static int set_clock_plugin_env(void)
1954 char *env_value
= NULL
;
1956 if (!config
.lttng_ust_clock_plugin
.value
) {
1960 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1961 config
.lttng_ust_clock_plugin
.value
);
1967 ret
= putenv(env_value
);
1970 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1974 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1975 config
.lttng_ust_clock_plugin
.value
);
1980 static void destroy_all_sessions_and_wait(void)
1982 struct ltt_session
*session
, *tmp
;
1983 struct ltt_session_list
*session_list
;
1985 session_list
= session_get_list();
1986 DBG("Initiating destruction of all sessions");
1988 if (!session_list
) {
1992 session_lock_list();
1993 /* Initiate the destruction of all sessions. */
1994 cds_list_for_each_entry_safe(session
, tmp
,
1995 &session_list
->head
, list
) {
1996 if (!session_get(session
)) {
2000 session_lock(session
);
2001 if (session
->destroyed
) {
2002 goto unlock_session
;
2004 (void) cmd_destroy_session(session
,
2005 notification_thread_handle
);
2007 session_unlock(session
);
2008 session_put(session
);
2010 session_unlock_list();
2012 /* Wait for the destruction of all sessions to complete. */
2013 DBG("Waiting for the destruction of all sessions to complete");
2014 session_list_wait_empty();
2015 DBG("Destruction of all sessions completed");
2021 int main(int argc
, char **argv
)
2023 int ret
= 0, retval
= 0;
2025 const char *env_app_timeout
;
2026 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
2027 *ust64_channel_monitor_pipe
= NULL
,
2028 *kernel_channel_monitor_pipe
= NULL
;
2029 struct lttng_thread
*ht_cleanup_thread
= NULL
;
2030 struct timer_thread_parameters timer_thread_parameters
;
2031 /* Rotation thread handle. */
2032 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
2033 /* Queue of rotation jobs populated by the sessiond-timer. */
2034 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
2035 struct lttng_thread
*client_thread
= NULL
;
2037 init_kernel_workarounds();
2039 rcu_register_thread();
2041 if (set_signal_handler()) {
2043 goto exit_set_signal_handler
;
2046 if (timer_signal_init()) {
2048 goto exit_set_signal_handler
;
2051 page_size
= sysconf(_SC_PAGESIZE
);
2052 if (page_size
< 0) {
2053 PERROR("sysconf _SC_PAGESIZE");
2054 page_size
= LONG_MAX
;
2055 WARN("Fallback page size to %ld", page_size
);
2058 ret
= sessiond_config_init(&config
);
2061 goto exit_set_signal_handler
;
2065 * Init config from environment variables.
2066 * Command line option override env configuration per-doc. Do env first.
2068 sessiond_config_apply_env_config(&config
);
2071 * Parse arguments and load the daemon configuration file.
2073 * We have an exit_options exit path to free memory reserved by
2074 * set_options. This is needed because the rest of sessiond_cleanup()
2075 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
2076 * depends on set_options.
2079 if (set_options(argc
, argv
)) {
2085 * Resolve all paths received as arguments, configuration option, or
2086 * through environment variable as absolute paths. This is necessary
2087 * since daemonizing causes the sessiond's current working directory
2090 ret
= sessiond_config_resolve_paths(&config
);
2096 lttng_opt_verbose
= config
.verbose
;
2097 lttng_opt_quiet
= config
.quiet
;
2098 kconsumer_data
.err_unix_sock_path
=
2099 config
.kconsumerd_err_unix_sock_path
.value
;
2100 kconsumer_data
.cmd_unix_sock_path
=
2101 config
.kconsumerd_cmd_unix_sock_path
.value
;
2102 ustconsumer32_data
.err_unix_sock_path
=
2103 config
.consumerd32_err_unix_sock_path
.value
;
2104 ustconsumer32_data
.cmd_unix_sock_path
=
2105 config
.consumerd32_cmd_unix_sock_path
.value
;
2106 ustconsumer64_data
.err_unix_sock_path
=
2107 config
.consumerd64_err_unix_sock_path
.value
;
2108 ustconsumer64_data
.cmd_unix_sock_path
=
2109 config
.consumerd64_cmd_unix_sock_path
.value
;
2110 set_clock_plugin_env();
2112 sessiond_config_log(&config
);
2114 if (create_lttng_rundir()) {
2119 /* Abort launch if a session daemon is already running. */
2120 if (check_existing_daemon()) {
2121 ERR("A session daemon is already running.");
2127 if (config
.daemonize
|| config
.background
) {
2130 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2131 !config
.background
);
2138 * We are in the child. Make sure all other file descriptors are
2139 * closed, in case we are called with more opened file
2140 * descriptors than the standard ones and the lock file.
2142 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2143 if (i
== lockfile_fd
) {
2150 if (run_as_create_worker(argv
[0]) < 0) {
2151 goto exit_create_run_as_worker_cleanup
;
2155 * Starting from here, we can create threads. This needs to be after
2156 * lttng_daemonize due to RCU.
2160 * Initialize the health check subsystem. This call should set the
2161 * appropriate time values.
2163 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
2164 if (!health_sessiond
) {
2165 PERROR("health_app_create error");
2167 goto exit_health_sessiond_cleanup
;
2170 /* Create thread to clean up RCU hash tables */
2171 ht_cleanup_thread
= launch_ht_cleanup_thread();
2172 if (!ht_cleanup_thread
) {
2174 goto exit_ht_cleanup
;
2177 /* Create thread quit pipe */
2178 if (sessiond_init_thread_quit_pipe()) {
2180 goto exit_init_data
;
2183 /* Check if daemon is UID = 0 */
2184 is_root
= !getuid();
2186 /* Create global run dir with root access */
2188 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
2189 if (!kernel_channel_monitor_pipe
) {
2190 ERR("Failed to create kernel consumer channel monitor pipe");
2192 goto exit_init_data
;
2194 kconsumer_data
.channel_monitor_pipe
=
2195 lttng_pipe_release_writefd(
2196 kernel_channel_monitor_pipe
);
2197 if (kconsumer_data
.channel_monitor_pipe
< 0) {
2199 goto exit_init_data
;
2203 /* Set consumer initial state */
2204 kernel_consumerd_state
= CONSUMER_STOPPED
;
2205 ust_consumerd_state
= CONSUMER_STOPPED
;
2207 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
2208 if (!ust32_channel_monitor_pipe
) {
2209 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
2211 goto exit_init_data
;
2213 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
2214 ust32_channel_monitor_pipe
);
2215 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
2217 goto exit_init_data
;
2221 * The rotation_thread_timer_queue structure is shared between the
2222 * sessiond timer thread and the rotation thread. The main thread keeps
2223 * its ownership and destroys it when both threads have been joined.
2225 rotation_timer_queue
= rotation_thread_timer_queue_create();
2226 if (!rotation_timer_queue
) {
2228 goto exit_init_data
;
2230 timer_thread_parameters
.rotation_thread_job_queue
=
2231 rotation_timer_queue
;
2233 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
2234 if (!ust64_channel_monitor_pipe
) {
2235 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
2237 goto exit_init_data
;
2239 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
2240 ust64_channel_monitor_pipe
);
2241 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
2243 goto exit_init_data
;
2247 * Init UST app hash table. Alloc hash table before this point since
2248 * cleanup() can get called after that point.
2250 if (ust_app_ht_alloc()) {
2251 ERR("Failed to allocate UST app hash table");
2253 goto exit_init_data
;
2257 * Initialize agent app hash table. We allocate the hash table here
2258 * since cleanup() can get called after this point.
2260 if (agent_app_ht_alloc()) {
2261 ERR("Failed to allocate Agent app hash table");
2263 goto exit_init_data
;
2267 * These actions must be executed as root. We do that *after* setting up
2268 * the sockets path because we MUST make the check for another daemon using
2269 * those paths *before* trying to set the kernel consumer sockets and init
2273 if (set_consumer_sockets(&kconsumer_data
)) {
2275 goto exit_init_data
;
2278 /* Setup kernel tracer */
2279 if (!config
.no_kernel
) {
2280 init_kernel_tracer();
2281 if (kernel_tracer_fd
>= 0) {
2282 ret
= syscall_init_table();
2284 ERR("Unable to populate syscall table. "
2285 "Syscall tracing won't work "
2286 "for this session daemon.");
2291 /* Set ulimit for open files */
2294 /* init lttng_fd tracking must be done after set_ulimit. */
2297 if (set_consumer_sockets(&ustconsumer64_data
)) {
2299 goto exit_init_data
;
2302 if (set_consumer_sockets(&ustconsumer32_data
)) {
2304 goto exit_init_data
;
2307 /* Set credentials to socket */
2308 if (is_root
&& set_permissions(config
.rundir
.value
)) {
2310 goto exit_init_data
;
2313 /* Get parent pid if -S, --sig-parent is specified. */
2314 if (config
.sig_parent
) {
2318 /* Setup the kernel pipe for waking up the kernel thread */
2319 if (is_root
&& !config
.no_kernel
) {
2320 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
2322 goto exit_init_data
;
2326 /* Setup the thread apps communication pipe. */
2327 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
2329 goto exit_init_data
;
2332 /* Setup the thread apps notify communication pipe. */
2333 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
2335 goto exit_init_data
;
2338 /* Initialize global buffer per UID and PID registry. */
2339 buffer_reg_init_uid_registry();
2340 buffer_reg_init_pid_registry();
2342 /* Init UST command queue. */
2343 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2347 /* Check for the application socket timeout env variable. */
2348 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
2349 if (env_app_timeout
) {
2350 config
.app_socket_timeout
= atoi(env_app_timeout
);
2352 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
2355 ret
= write_pidfile();
2357 ERR("Error in write_pidfile");
2359 goto exit_init_data
;
2362 /* Initialize communication library */
2364 /* Initialize TCP timeout values */
2365 lttcomm_inet_init();
2367 if (load_session_init_data(&load_info
) < 0) {
2369 goto exit_init_data
;
2371 load_info
->path
= config
.load_session_path
.value
;
2373 /* Create health-check thread. */
2374 if (!launch_health_management_thread()) {
2379 /* notification_thread_data acquires the pipes' read side. */
2380 notification_thread_handle
= notification_thread_handle_create(
2381 ust32_channel_monitor_pipe
,
2382 ust64_channel_monitor_pipe
,
2383 kernel_channel_monitor_pipe
);
2384 if (!notification_thread_handle
) {
2386 ERR("Failed to create notification thread shared data");
2387 goto exit_notification
;
2390 /* Create notification thread. */
2391 if (!launch_notification_thread(notification_thread_handle
)) {
2393 goto exit_notification
;
2396 /* Create timer thread. */
2397 if (!launch_timer_thread(&timer_thread_parameters
)) {
2399 goto exit_notification
;
2402 /* rotation_thread_data acquires the pipes' read side. */
2403 rotation_thread_handle
= rotation_thread_handle_create(
2404 rotation_timer_queue
,
2405 notification_thread_handle
);
2406 if (!rotation_thread_handle
) {
2408 ERR("Failed to create rotation thread shared data");
2413 /* Create rotation thread. */
2414 if (!launch_rotation_thread(rotation_thread_handle
)) {
2419 /* Create thread to manage the client socket */
2420 client_thread
= launch_client_thread();
2421 if (!client_thread
) {
2426 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
2427 apps_cmd_notify_pipe
[1])) {
2432 /* Create thread to manage application registration. */
2433 if (!launch_application_registration_thread(&ust_cmd_queue
)) {
2438 /* Create thread to manage application socket */
2439 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
2444 /* Create thread to manage application notify socket */
2445 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
2446 ust_thread_manage_notify
, (void *) NULL
);
2449 PERROR("pthread_create notify");
2452 goto exit_apps_notify
;
2455 /* Create agent registration thread. */
2456 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
2457 agent_thread_manage_registration
, (void *) NULL
);
2460 PERROR("pthread_create agent");
2463 goto exit_agent_reg
;
2466 /* Don't start this thread if kernel tracing is not requested nor root */
2467 if (is_root
&& !config
.no_kernel
) {
2468 /* Create kernel thread to manage kernel event */
2469 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
2470 thread_manage_kernel
, (void *) NULL
);
2473 PERROR("pthread_create kernel");
2480 /* Create session loading thread. */
2481 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
2482 thread_load_session
, load_info
);
2485 PERROR("pthread_create load_session_thread");
2488 goto exit_load_session
;
2492 * This is where we start awaiting program completion (e.g. through
2493 * signal that asks threads to teardown).
2496 ret
= pthread_join(load_session_thread
, &status
);
2499 PERROR("pthread_join load_session_thread");
2503 /* Initiate teardown once activity occurs on the quit pipe. */
2504 sessiond_wait_for_quit_pipe(-1U);
2507 * Ensure that the client thread is no longer accepting new commands,
2508 * which could cause new sessions to be created.
2510 if (!lttng_thread_shutdown(client_thread
)) {
2511 ERR("Failed to shutdown the client thread, continuing teardown");
2512 lttng_thread_put(client_thread
);
2513 client_thread
= NULL
;
2516 destroy_all_sessions_and_wait();
2519 if (is_root
&& !config
.no_kernel
) {
2520 ret
= pthread_join(kernel_thread
, &status
);
2523 PERROR("pthread_join");
2529 ret
= pthread_join(agent_reg_thread
, &status
);
2532 PERROR("pthread_join agent");
2537 ret
= pthread_join(apps_notify_thread
, &status
);
2540 PERROR("pthread_join apps notify");
2550 lttng_thread_list_shutdown_orphans();
2553 if (client_thread
) {
2554 lttng_thread_put(client_thread
);
2558 * Wait for all pending call_rcu work to complete before tearing
2559 * down data structures. call_rcu worker may be trying to
2560 * perform lookups in those structures.
2564 * sessiond_cleanup() is called when no other thread is running, except
2565 * the ht_cleanup thread, which is needed to destroy the hash tables.
2567 rcu_thread_online();
2571 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2572 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2573 * the queue is empty before shutting down the clean-up thread.
2577 if (ht_cleanup_thread
) {
2578 lttng_thread_shutdown(ht_cleanup_thread
);
2579 lttng_thread_put(ht_cleanup_thread
);
2582 rcu_thread_offline();
2583 rcu_unregister_thread();
2585 if (rotation_thread_handle
) {
2586 rotation_thread_handle_destroy(rotation_thread_handle
);
2590 * After the rotation and timer thread have quit, we can safely destroy
2591 * the rotation_timer_queue.
2593 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2595 * The teardown of the notification system is performed after the
2596 * session daemon's teardown in order to allow it to be notified
2597 * of the active session and channels at the moment of the teardown.
2599 if (notification_thread_handle
) {
2600 notification_thread_handle_destroy(notification_thread_handle
);
2602 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2603 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2604 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2607 health_app_destroy(health_sessiond
);
2608 exit_health_sessiond_cleanup
:
2609 exit_create_run_as_worker_cleanup
:
2612 sessiond_cleanup_lock_file();
2613 sessiond_cleanup_options();
2615 exit_set_signal_handler
: