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 };
145 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
147 /* Pthread, Mutexes and Semaphores */
148 static pthread_t kernel_thread
;
149 static pthread_t load_session_thread
;
152 * UST registration command queue. This queue is tied with a futex and uses a N
153 * wakers / 1 waiter implemented and detailed in futex.c/.h
155 * The thread_registration_apps and thread_dispatch_ust_registration uses this
156 * queue along with the wait/wake scheme. The thread_manage_apps receives down
157 * the line new application socket and monitors it for any I/O error or clean
158 * close that triggers an unregistration of the application.
160 static struct ust_cmd_queue ust_cmd_queue
;
162 static const char *module_proc_lttng
= "/proc/lttng";
164 /* Load session thread information to operate. */
165 static struct load_session_thread_data
*load_info
;
168 * Section name to look for in the daemon configuration file.
170 static const char * const config_section_name
= "sessiond";
172 /* Am I root or not. Set to 1 if the daemon is running as root */
176 * Stop all threads by closing the thread quit pipe.
178 static void stop_threads(void)
182 /* Stopping all threads */
183 DBG("Terminating all threads");
184 ret
= sessiond_notify_quit_pipe();
186 ERR("write error on thread quit pipe");
191 * Close every consumer sockets.
193 static void close_consumer_sockets(void)
197 if (kconsumer_data
.err_sock
>= 0) {
198 ret
= close(kconsumer_data
.err_sock
);
200 PERROR("kernel consumer err_sock close");
203 if (ustconsumer32_data
.err_sock
>= 0) {
204 ret
= close(ustconsumer32_data
.err_sock
);
206 PERROR("UST consumerd32 err_sock close");
209 if (ustconsumer64_data
.err_sock
>= 0) {
210 ret
= close(ustconsumer64_data
.err_sock
);
212 PERROR("UST consumerd64 err_sock close");
215 if (kconsumer_data
.cmd_sock
>= 0) {
216 ret
= close(kconsumer_data
.cmd_sock
);
218 PERROR("kernel consumer cmd_sock close");
221 if (ustconsumer32_data
.cmd_sock
>= 0) {
222 ret
= close(ustconsumer32_data
.cmd_sock
);
224 PERROR("UST consumerd32 cmd_sock close");
227 if (ustconsumer64_data
.cmd_sock
>= 0) {
228 ret
= close(ustconsumer64_data
.cmd_sock
);
230 PERROR("UST consumerd64 cmd_sock close");
233 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
234 ret
= close(kconsumer_data
.channel_monitor_pipe
);
236 PERROR("kernel consumer channel monitor pipe close");
239 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
240 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
242 PERROR("UST consumerd32 channel monitor pipe close");
245 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
246 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
248 PERROR("UST consumerd64 channel monitor pipe close");
254 * Wait on consumer process termination.
256 * Need to be called with the consumer data lock held or from a context
257 * ensuring no concurrent access to data (e.g: cleanup).
259 static void wait_consumer(struct consumer_data
*consumer_data
)
264 if (consumer_data
->pid
<= 0) {
268 DBG("Waiting for complete teardown of consumerd (PID: %d)",
270 ret
= waitpid(consumer_data
->pid
, &status
, 0);
272 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
273 } else if (!WIFEXITED(status
)) {
274 ERR("consumerd termination with error: %d",
277 consumer_data
->pid
= 0;
281 * Cleanup the session daemon's data structures.
283 static void sessiond_cleanup(void)
286 struct ltt_session_list
*session_list
= session_get_list();
288 DBG("Cleanup sessiond");
291 * Close the thread quit pipe. It has already done its job,
292 * since we are now called.
294 sessiond_close_quit_pipe();
295 utils_close_pipe(apps_cmd_pipe
);
296 utils_close_pipe(apps_cmd_notify_pipe
);
298 ret
= remove(config
.pid_file_path
.value
);
300 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
303 DBG("Removing sessiond and consumerd content of directory %s",
304 config
.rundir
.value
);
307 DBG("Removing %s", config
.pid_file_path
.value
);
308 (void) unlink(config
.pid_file_path
.value
);
310 DBG("Removing %s", config
.agent_port_file_path
.value
);
311 (void) unlink(config
.agent_port_file_path
.value
);
314 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
315 (void) unlink(kconsumer_data
.err_unix_sock_path
);
317 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
318 (void) rmdir(config
.kconsumerd_path
.value
);
320 /* ust consumerd 32 */
321 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
322 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
324 DBG("Removing directory %s", config
.consumerd32_path
.value
);
325 (void) rmdir(config
.consumerd32_path
.value
);
327 /* ust consumerd 64 */
328 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
329 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
331 DBG("Removing directory %s", config
.consumerd64_path
.value
);
332 (void) rmdir(config
.consumerd64_path
.value
);
334 pthread_mutex_destroy(&session_list
->lock
);
336 wait_consumer(&kconsumer_data
);
337 wait_consumer(&ustconsumer64_data
);
338 wait_consumer(&ustconsumer32_data
);
340 DBG("Cleaning up all agent apps");
341 agent_app_ht_clean();
343 DBG("Closing all UST sockets");
344 ust_app_clean_list();
345 buffer_reg_destroy_registries();
347 if (is_root
&& !config
.no_kernel
) {
348 DBG2("Closing kernel fd");
349 if (kernel_tracer_fd
>= 0) {
350 ret
= close(kernel_tracer_fd
);
355 DBG("Unloading kernel modules");
356 modprobe_remove_lttng_all();
360 close_consumer_sockets();
363 load_session_destroy_data(load_info
);
368 * We do NOT rmdir rundir because there are other processes
369 * using it, for instance lttng-relayd, which can start in
370 * parallel with this teardown.
375 * Cleanup the daemon's option data structures.
377 static void sessiond_cleanup_options(void)
379 DBG("Cleaning up options");
381 sessiond_config_fini(&config
);
383 run_as_destroy_worker();
387 * Update the kernel poll set of all channel fd available over all tracing
388 * session. Add the wakeup pipe at the end of the set.
390 static int update_kernel_poll(struct lttng_poll_event
*events
)
393 struct ltt_kernel_channel
*channel
;
394 struct ltt_session
*session
;
395 const struct ltt_session_list
*session_list
= session_get_list();
397 DBG("Updating kernel poll set");
400 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
401 if (!session_get(session
)) {
404 session_lock(session
);
405 if (session
->kernel_session
== NULL
) {
406 session_unlock(session
);
407 session_put(session
);
411 cds_list_for_each_entry(channel
,
412 &session
->kernel_session
->channel_list
.head
, list
) {
413 /* Add channel fd to the kernel poll set */
414 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
416 session_unlock(session
);
417 session_put(session
);
420 DBG("Channel fd %d added to kernel set", channel
->fd
);
422 session_unlock(session
);
424 session_unlock_list();
429 session_unlock_list();
434 * Find the channel fd from 'fd' over all tracing session. When found, check
435 * for new channel stream and send those stream fds to the kernel consumer.
437 * Useful for CPU hotplug feature.
439 static int update_kernel_stream(int fd
)
442 struct ltt_session
*session
;
443 struct ltt_kernel_session
*ksess
;
444 struct ltt_kernel_channel
*channel
;
445 const struct ltt_session_list
*session_list
= session_get_list();
447 DBG("Updating kernel streams for channel fd %d", fd
);
450 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
451 if (!session_get(session
)) {
454 session_lock(session
);
455 if (session
->kernel_session
== NULL
) {
456 session_unlock(session
);
457 session_put(session
);
460 ksess
= session
->kernel_session
;
462 cds_list_for_each_entry(channel
,
463 &ksess
->channel_list
.head
, list
) {
464 struct lttng_ht_iter iter
;
465 struct consumer_socket
*socket
;
467 if (channel
->fd
!= fd
) {
470 DBG("Channel found, updating kernel streams");
471 ret
= kernel_open_channel_stream(channel
);
475 /* Update the stream global counter */
476 ksess
->stream_count_global
+= ret
;
479 * Have we already sent fds to the consumer? If yes, it
480 * means that tracing is started so it is safe to send
481 * our updated stream fds.
483 if (ksess
->consumer_fds_sent
!= 1
484 || ksess
->consumer
== NULL
) {
490 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
491 &iter
.iter
, socket
, node
.node
) {
492 pthread_mutex_lock(socket
->lock
);
493 ret
= kernel_consumer_send_channel_streams(socket
,
495 session
->output_traces
? 1 : 0);
496 pthread_mutex_unlock(socket
->lock
);
504 session_unlock(session
);
505 session_put(session
);
507 session_unlock_list();
511 session_unlock(session
);
512 session_put(session
);
513 session_unlock_list();
518 * This thread manage event coming from the kernel.
520 * Features supported in this thread:
523 static void *thread_manage_kernel(void *data
)
525 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
526 uint32_t revents
, nb_fd
;
528 struct lttng_poll_event events
;
530 DBG("[thread] Thread manage kernel started");
532 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
535 * This first step of the while is to clean this structure which could free
536 * non NULL pointers so initialize it before the loop.
538 lttng_poll_init(&events
);
540 if (testpoint(sessiond_thread_manage_kernel
)) {
541 goto error_testpoint
;
544 health_code_update();
546 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
547 goto error_testpoint
;
551 health_code_update();
553 if (update_poll_flag
== 1) {
554 /* Clean events object. We are about to populate it again. */
555 lttng_poll_clean(&events
);
557 ret
= sessiond_set_thread_pollset(&events
, 2);
559 goto error_poll_create
;
562 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
567 /* This will add the available kernel channel if any. */
568 ret
= update_kernel_poll(&events
);
572 update_poll_flag
= 0;
575 DBG("Thread kernel polling");
577 /* Poll infinite value of time */
580 ret
= lttng_poll_wait(&events
, -1);
581 DBG("Thread kernel return from poll on %d fds",
582 LTTNG_POLL_GETNB(&events
));
586 * Restart interrupted system call.
588 if (errno
== EINTR
) {
592 } else if (ret
== 0) {
593 /* Should not happen since timeout is infinite */
594 ERR("Return value of poll is 0 with an infinite timeout.\n"
595 "This should not have happened! Continuing...");
601 for (i
= 0; i
< nb_fd
; i
++) {
602 /* Fetch once the poll data */
603 revents
= LTTNG_POLL_GETEV(&events
, i
);
604 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
606 health_code_update();
609 /* No activity for this FD (poll implementation). */
613 /* Thread quit pipe has been closed. Killing thread. */
614 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
620 /* Check for data on kernel pipe */
621 if (revents
& LPOLLIN
) {
622 if (pollfd
== kernel_poll_pipe
[0]) {
623 (void) lttng_read(kernel_poll_pipe
[0],
626 * Ret value is useless here, if this pipe gets any actions an
627 * update is required anyway.
629 update_poll_flag
= 1;
633 * New CPU detected by the kernel. Adding kernel stream to
634 * kernel session and updating the kernel consumer
636 ret
= update_kernel_stream(pollfd
);
642 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
643 update_poll_flag
= 1;
646 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
654 lttng_poll_clean(&events
);
657 utils_close_pipe(kernel_poll_pipe
);
658 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
661 ERR("Health error occurred in %s", __func__
);
662 WARN("Kernel thread died unexpectedly. "
663 "Kernel tracing can continue but CPU hotplug is disabled.");
665 health_unregister(health_sessiond
);
666 DBG("Kernel thread dying");
671 * Signal pthread condition of the consumer data that the thread.
673 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
675 pthread_mutex_lock(&data
->cond_mutex
);
678 * The state is set before signaling. It can be any value, it's the waiter
679 * job to correctly interpret this condition variable associated to the
680 * consumer pthread_cond.
682 * A value of 0 means that the corresponding thread of the consumer data
683 * was not started. 1 indicates that the thread has started and is ready
684 * for action. A negative value means that there was an error during the
687 data
->consumer_thread_is_ready
= state
;
688 (void) pthread_cond_signal(&data
->cond
);
690 pthread_mutex_unlock(&data
->cond_mutex
);
694 * This thread manage the consumer error sent back to the session daemon.
696 void *thread_manage_consumer(void *data
)
698 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
699 uint32_t revents
, nb_fd
;
700 enum lttcomm_return_code code
;
701 struct lttng_poll_event events
;
702 struct consumer_data
*consumer_data
= data
;
703 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
705 DBG("[thread] Manage consumer started");
707 rcu_register_thread();
710 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
712 health_code_update();
715 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
716 * metadata_sock. Nothing more will be added to this poll set.
718 ret
= sessiond_set_thread_pollset(&events
, 3);
724 * The error socket here is already in a listening state which was done
725 * just before spawning this thread to avoid a race between the consumer
726 * daemon exec trying to connect and the listen() call.
728 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
733 health_code_update();
735 /* Infinite blocking call, waiting for transmission */
739 if (testpoint(sessiond_thread_manage_consumer
)) {
743 ret
= lttng_poll_wait(&events
, -1);
747 * Restart interrupted system call.
749 if (errno
== EINTR
) {
757 for (i
= 0; i
< nb_fd
; i
++) {
758 /* Fetch once the poll data */
759 revents
= LTTNG_POLL_GETEV(&events
, i
);
760 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
762 health_code_update();
765 /* No activity for this FD (poll implementation). */
769 /* Thread quit pipe has been closed. Killing thread. */
770 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
776 /* Event on the registration socket */
777 if (pollfd
== consumer_data
->err_sock
) {
778 if (revents
& LPOLLIN
) {
780 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
781 ERR("consumer err socket poll error");
784 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
790 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
796 * Set the CLOEXEC flag. Return code is useless because either way, the
799 (void) utils_set_fd_cloexec(sock
);
801 health_code_update();
803 DBG2("Receiving code from consumer err_sock");
805 /* Getting status code from kconsumerd */
806 ret
= lttcomm_recv_unix_sock(sock
, &code
,
807 sizeof(enum lttcomm_return_code
));
812 health_code_update();
813 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
814 ERR("consumer error when waiting for SOCK_READY : %s",
815 lttcomm_get_readable_code(-code
));
819 /* Connect both command and metadata sockets. */
820 consumer_data
->cmd_sock
=
821 lttcomm_connect_unix_sock(
822 consumer_data
->cmd_unix_sock_path
);
823 consumer_data
->metadata_fd
=
824 lttcomm_connect_unix_sock(
825 consumer_data
->cmd_unix_sock_path
);
826 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
827 PERROR("consumer connect cmd socket");
828 /* On error, signal condition and quit. */
829 signal_consumer_condition(consumer_data
, -1);
833 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
835 /* Create metadata socket lock. */
836 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
837 if (consumer_data
->metadata_sock
.lock
== NULL
) {
838 PERROR("zmalloc pthread mutex");
841 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
843 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
844 DBG("Consumer metadata socket ready (fd: %d)",
845 consumer_data
->metadata_fd
);
848 * Remove the consumerd error sock since we've established a connection.
850 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
855 /* Add new accepted error socket. */
856 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
861 /* Add metadata socket that is successfully connected. */
862 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
863 LPOLLIN
| LPOLLRDHUP
);
868 health_code_update();
871 * Transfer the write-end of the channel monitoring and rotate pipe
872 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
874 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
875 if (!cmd_socket_wrapper
) {
878 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
880 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
881 consumer_data
->channel_monitor_pipe
);
886 /* Discard the socket wrapper as it is no longer needed. */
887 consumer_destroy_socket(cmd_socket_wrapper
);
888 cmd_socket_wrapper
= NULL
;
890 /* The thread is completely initialized, signal that it is ready. */
891 signal_consumer_condition(consumer_data
, 1);
893 /* Infinite blocking call, waiting for transmission */
896 health_code_update();
898 /* Exit the thread because the thread quit pipe has been triggered. */
900 /* Not a health error. */
906 ret
= lttng_poll_wait(&events
, -1);
910 * Restart interrupted system call.
912 if (errno
== EINTR
) {
920 for (i
= 0; i
< nb_fd
; i
++) {
921 /* Fetch once the poll data */
922 revents
= LTTNG_POLL_GETEV(&events
, i
);
923 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
925 health_code_update();
928 /* No activity for this FD (poll implementation). */
933 * Thread quit pipe has been triggered, flag that we should stop
934 * but continue the current loop to handle potential data from
937 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
939 if (pollfd
== sock
) {
940 /* Event on the consumerd socket */
941 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
942 && !(revents
& LPOLLIN
)) {
943 ERR("consumer err socket second poll error");
946 health_code_update();
947 /* Wait for any kconsumerd error */
948 ret
= lttcomm_recv_unix_sock(sock
, &code
,
949 sizeof(enum lttcomm_return_code
));
951 ERR("consumer closed the command socket");
955 ERR("consumer return code : %s",
956 lttcomm_get_readable_code(-code
));
959 } else if (pollfd
== consumer_data
->metadata_fd
) {
960 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
961 && !(revents
& LPOLLIN
)) {
962 ERR("consumer err metadata socket second poll error");
965 /* UST metadata requests */
966 ret
= ust_consumer_metadata_request(
967 &consumer_data
->metadata_sock
);
969 ERR("Handling metadata request");
973 /* No need for an else branch all FDs are tested prior. */
975 health_code_update();
981 * We lock here because we are about to close the sockets and some other
982 * thread might be using them so get exclusive access which will abort all
983 * other consumer command by other threads.
985 pthread_mutex_lock(&consumer_data
->lock
);
987 /* Immediately set the consumerd state to stopped */
988 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
989 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
990 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
991 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
992 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
994 /* Code flow error... */
998 if (consumer_data
->err_sock
>= 0) {
999 ret
= close(consumer_data
->err_sock
);
1003 consumer_data
->err_sock
= -1;
1005 if (consumer_data
->cmd_sock
>= 0) {
1006 ret
= close(consumer_data
->cmd_sock
);
1010 consumer_data
->cmd_sock
= -1;
1012 if (consumer_data
->metadata_sock
.fd_ptr
&&
1013 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1014 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1026 unlink(consumer_data
->err_unix_sock_path
);
1027 unlink(consumer_data
->cmd_unix_sock_path
);
1028 pthread_mutex_unlock(&consumer_data
->lock
);
1030 /* Cleanup metadata socket mutex. */
1031 if (consumer_data
->metadata_sock
.lock
) {
1032 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1033 free(consumer_data
->metadata_sock
.lock
);
1035 lttng_poll_clean(&events
);
1037 if (cmd_socket_wrapper
) {
1038 consumer_destroy_socket(cmd_socket_wrapper
);
1043 ERR("Health error occurred in %s", __func__
);
1045 health_unregister(health_sessiond
);
1046 DBG("consumer thread cleanup completed");
1048 rcu_thread_offline();
1049 rcu_unregister_thread();
1055 * Setup necessary data for kernel tracer action.
1057 static int init_kernel_tracer(void)
1061 /* Modprobe lttng kernel modules */
1062 ret
= modprobe_lttng_control();
1067 /* Open debugfs lttng */
1068 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1069 if (kernel_tracer_fd
< 0) {
1070 DBG("Failed to open %s", module_proc_lttng
);
1074 /* Validate kernel version */
1075 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
1076 &kernel_tracer_abi_version
);
1081 ret
= modprobe_lttng_data();
1086 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1093 WARN("Kernel tracer does not support buffer monitoring. "
1094 "The monitoring timer of channels in the kernel domain "
1095 "will be set to 0 (disabled).");
1098 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1102 modprobe_remove_lttng_control();
1103 ret
= close(kernel_tracer_fd
);
1107 kernel_tracer_fd
= -1;
1108 return LTTNG_ERR_KERN_VERSION
;
1111 ret
= close(kernel_tracer_fd
);
1117 modprobe_remove_lttng_control();
1120 WARN("No kernel tracer available");
1121 kernel_tracer_fd
= -1;
1123 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1125 return LTTNG_ERR_KERN_NA
;
1129 static int string_match(const char *str1
, const char *str2
)
1131 return (str1
&& str2
) && !strcmp(str1
, str2
);
1135 * Take an option from the getopt output and set it in the right variable to be
1138 * Return 0 on success else a negative value.
1140 static int set_option(int opt
, const char *arg
, const char *optname
)
1144 if (string_match(optname
, "client-sock") || opt
== 'c') {
1145 if (!arg
|| *arg
== '\0') {
1149 if (lttng_is_setuid_setgid()) {
1150 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1151 "-c, --client-sock");
1153 config_string_set(&config
.client_unix_sock_path
,
1155 if (!config
.client_unix_sock_path
.value
) {
1160 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
1161 if (!arg
|| *arg
== '\0') {
1165 if (lttng_is_setuid_setgid()) {
1166 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1169 config_string_set(&config
.apps_unix_sock_path
,
1171 if (!config
.apps_unix_sock_path
.value
) {
1176 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
1177 config
.daemonize
= true;
1178 } else if (string_match(optname
, "background") || opt
== 'b') {
1179 config
.background
= true;
1180 } else if (string_match(optname
, "group") || opt
== 'g') {
1181 if (!arg
|| *arg
== '\0') {
1185 if (lttng_is_setuid_setgid()) {
1186 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1189 config_string_set(&config
.tracing_group_name
,
1191 if (!config
.tracing_group_name
.value
) {
1196 } else if (string_match(optname
, "help") || opt
== 'h') {
1197 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
1199 ERR("Cannot show --help for `lttng-sessiond`");
1202 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1203 } else if (string_match(optname
, "version") || opt
== 'V') {
1204 fprintf(stdout
, "%s\n", VERSION
);
1206 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
1207 config
.sig_parent
= true;
1208 } else if (string_match(optname
, "kconsumerd-err-sock")) {
1209 if (!arg
|| *arg
== '\0') {
1213 if (lttng_is_setuid_setgid()) {
1214 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1215 "--kconsumerd-err-sock");
1217 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
1219 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
1224 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
1225 if (!arg
|| *arg
== '\0') {
1229 if (lttng_is_setuid_setgid()) {
1230 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1231 "--kconsumerd-cmd-sock");
1233 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
1235 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
1240 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
1241 if (!arg
|| *arg
== '\0') {
1245 if (lttng_is_setuid_setgid()) {
1246 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1247 "--ustconsumerd64-err-sock");
1249 config_string_set(&config
.consumerd64_err_unix_sock_path
,
1251 if (!config
.consumerd64_err_unix_sock_path
.value
) {
1256 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
1257 if (!arg
|| *arg
== '\0') {
1261 if (lttng_is_setuid_setgid()) {
1262 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1263 "--ustconsumerd64-cmd-sock");
1265 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
1267 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
1272 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
1273 if (!arg
|| *arg
== '\0') {
1277 if (lttng_is_setuid_setgid()) {
1278 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1279 "--ustconsumerd32-err-sock");
1281 config_string_set(&config
.consumerd32_err_unix_sock_path
,
1283 if (!config
.consumerd32_err_unix_sock_path
.value
) {
1288 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
1289 if (!arg
|| *arg
== '\0') {
1293 if (lttng_is_setuid_setgid()) {
1294 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1295 "--ustconsumerd32-cmd-sock");
1297 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
1299 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
1304 } else if (string_match(optname
, "no-kernel")) {
1305 config
.no_kernel
= true;
1306 } else if (string_match(optname
, "quiet") || opt
== 'q') {
1307 config
.quiet
= true;
1308 } else if (string_match(optname
, "verbose") || opt
== 'v') {
1309 /* Verbose level can increase using multiple -v */
1311 /* Value obtained from config file */
1312 config
.verbose
= config_parse_value(arg
);
1314 /* -v used on command line */
1317 /* Clamp value to [0, 3] */
1318 config
.verbose
= config
.verbose
< 0 ? 0 :
1319 (config
.verbose
<= 3 ? config
.verbose
: 3);
1320 } else if (string_match(optname
, "verbose-consumer")) {
1322 config
.verbose_consumer
= config_parse_value(arg
);
1324 config
.verbose_consumer
++;
1326 } else if (string_match(optname
, "consumerd32-path")) {
1327 if (!arg
|| *arg
== '\0') {
1331 if (lttng_is_setuid_setgid()) {
1332 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1333 "--consumerd32-path");
1335 config_string_set(&config
.consumerd32_bin_path
,
1337 if (!config
.consumerd32_bin_path
.value
) {
1342 } else if (string_match(optname
, "consumerd32-libdir")) {
1343 if (!arg
|| *arg
== '\0') {
1347 if (lttng_is_setuid_setgid()) {
1348 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1349 "--consumerd32-libdir");
1351 config_string_set(&config
.consumerd32_lib_dir
,
1353 if (!config
.consumerd32_lib_dir
.value
) {
1358 } else if (string_match(optname
, "consumerd64-path")) {
1359 if (!arg
|| *arg
== '\0') {
1363 if (lttng_is_setuid_setgid()) {
1364 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1365 "--consumerd64-path");
1367 config_string_set(&config
.consumerd64_bin_path
,
1369 if (!config
.consumerd64_bin_path
.value
) {
1374 } else if (string_match(optname
, "consumerd64-libdir")) {
1375 if (!arg
|| *arg
== '\0') {
1379 if (lttng_is_setuid_setgid()) {
1380 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1381 "--consumerd64-libdir");
1383 config_string_set(&config
.consumerd64_lib_dir
,
1385 if (!config
.consumerd64_lib_dir
.value
) {
1390 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
1391 if (!arg
|| *arg
== '\0') {
1395 if (lttng_is_setuid_setgid()) {
1396 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1399 config_string_set(&config
.pid_file_path
, strdup(arg
));
1400 if (!config
.pid_file_path
.value
) {
1405 } else if (string_match(optname
, "agent-tcp-port")) {
1406 if (!arg
|| *arg
== '\0') {
1410 if (lttng_is_setuid_setgid()) {
1411 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1412 "--agent-tcp-port");
1417 v
= strtoul(arg
, NULL
, 0);
1418 if (errno
!= 0 || !isdigit(arg
[0])) {
1419 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
1422 if (v
== 0 || v
>= 65535) {
1423 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
1426 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
1427 DBG3("Agent TCP port set to non default: %i", (int) v
);
1429 } else if (string_match(optname
, "load") || opt
== 'l') {
1430 if (!arg
|| *arg
== '\0') {
1434 if (lttng_is_setuid_setgid()) {
1435 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1438 config_string_set(&config
.load_session_path
, strdup(arg
));
1439 if (!config
.load_session_path
.value
) {
1444 } else if (string_match(optname
, "kmod-probes")) {
1445 if (!arg
|| *arg
== '\0') {
1449 if (lttng_is_setuid_setgid()) {
1450 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1453 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
1454 if (!config
.kmod_probes_list
.value
) {
1459 } else if (string_match(optname
, "extra-kmod-probes")) {
1460 if (!arg
|| *arg
== '\0') {
1464 if (lttng_is_setuid_setgid()) {
1465 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1466 "--extra-kmod-probes");
1468 config_string_set(&config
.kmod_extra_probes_list
,
1470 if (!config
.kmod_extra_probes_list
.value
) {
1475 } else if (string_match(optname
, "config") || opt
== 'f') {
1476 /* This is handled in set_options() thus silent skip. */
1479 /* Unknown option or other error.
1480 * Error is printed by getopt, just return */
1485 if (ret
== -EINVAL
) {
1486 const char *opt_name
= "unknown";
1489 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
1491 if (opt
== long_options
[i
].val
) {
1492 opt_name
= long_options
[i
].name
;
1497 WARN("Invalid argument provided for option \"%s\", using default value.",
1505 * config_entry_handler_cb used to handle options read from a config file.
1506 * See config_entry_handler_cb comment in common/config/session-config.h for the
1507 * return value conventions.
1509 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
1513 if (!entry
|| !entry
->name
|| !entry
->value
) {
1518 /* Check if the option is to be ignored */
1519 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
1520 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
1525 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
1528 /* Ignore if not fully matched. */
1529 if (strcmp(entry
->name
, long_options
[i
].name
)) {
1534 * If the option takes no argument on the command line, we have to
1535 * check if the value is "true". We support non-zero numeric values,
1538 if (!long_options
[i
].has_arg
) {
1539 ret
= config_parse_value(entry
->value
);
1542 WARN("Invalid configuration value \"%s\" for option %s",
1543 entry
->value
, entry
->name
);
1545 /* False, skip boolean config option. */
1550 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
1554 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
1561 * daemon configuration loading and argument parsing
1563 static int set_options(int argc
, char **argv
)
1565 int ret
= 0, c
= 0, option_index
= 0;
1566 int orig_optopt
= optopt
, orig_optind
= optind
;
1568 const char *config_path
= NULL
;
1570 optstring
= utils_generate_optstring(long_options
,
1571 sizeof(long_options
) / sizeof(struct option
));
1577 /* Check for the --config option */
1578 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
1579 &option_index
)) != -1) {
1583 } else if (c
!= 'f') {
1584 /* if not equal to --config option. */
1588 if (lttng_is_setuid_setgid()) {
1589 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1592 config_path
= utils_expand_path(optarg
);
1594 ERR("Failed to resolve path: %s", optarg
);
1599 ret
= config_get_section_entries(config_path
, config_section_name
,
1600 config_entry_handler
, NULL
);
1603 ERR("Invalid configuration option at line %i", ret
);
1609 /* Reset getopt's global state */
1610 optopt
= orig_optopt
;
1611 optind
= orig_optind
;
1615 * getopt_long() will not set option_index if it encounters a
1618 c
= getopt_long(argc
, argv
, optstring
, long_options
,
1625 * Pass NULL as the long option name if popt left the index
1628 ret
= set_option(c
, optarg
,
1629 option_index
< 0 ? NULL
:
1630 long_options
[option_index
].name
);
1642 * Create lockfile using the rundir and return its fd.
1644 static int create_lockfile(void)
1646 return utils_create_lock_file(config
.lock_file_path
.value
);
1650 * Check if the global socket is available, and if a daemon is answering at the
1651 * other side. If yes, error is returned.
1653 * Also attempts to create and hold the lock file.
1655 static int check_existing_daemon(void)
1659 /* Is there anybody out there ? */
1660 if (lttng_session_daemon_alive()) {
1665 lockfile_fd
= create_lockfile();
1666 if (lockfile_fd
< 0) {
1674 static void sessiond_cleanup_lock_file(void)
1679 * Cleanup lock file by deleting it and finaly closing it which will
1680 * release the file system lock.
1682 if (lockfile_fd
>= 0) {
1683 ret
= remove(config
.lock_file_path
.value
);
1685 PERROR("remove lock file");
1687 ret
= close(lockfile_fd
);
1689 PERROR("close lock file");
1695 * Set the tracing group gid onto the client socket.
1697 * Race window between mkdir and chown is OK because we are going from more
1698 * permissive (root.root) to less permissive (root.tracing).
1700 static int set_permissions(char *rundir
)
1705 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
1707 /* Set lttng run dir */
1708 ret
= chown(rundir
, 0, gid
);
1710 ERR("Unable to set group on %s", rundir
);
1715 * Ensure all applications and tracing group can search the run
1716 * dir. Allow everyone to read the directory, since it does not
1717 * buy us anything to hide its content.
1719 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1721 ERR("Unable to set permissions on %s", rundir
);
1725 /* lttng client socket path */
1726 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
1728 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
1732 /* kconsumer error socket path */
1733 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
1735 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
1739 /* 64-bit ustconsumer error socket path */
1740 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1742 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
1746 /* 32-bit ustconsumer compat32 error socket path */
1747 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1749 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
1753 DBG("All permissions are set");
1759 * Create the lttng run directory needed for all global sockets and pipe.
1761 static int create_lttng_rundir(void)
1765 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1767 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1769 if (errno
!= EEXIST
) {
1770 ERR("Unable to create %s", config
.rundir
.value
);
1782 * Setup sockets and directory needed by the consumerds' communication with the
1785 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1790 switch (consumer_data
->type
) {
1791 case LTTNG_CONSUMER_KERNEL
:
1792 path
= config
.kconsumerd_path
.value
;
1794 case LTTNG_CONSUMER64_UST
:
1795 path
= config
.consumerd64_path
.value
;
1797 case LTTNG_CONSUMER32_UST
:
1798 path
= config
.consumerd32_path
.value
;
1801 ERR("Consumer type unknown");
1807 DBG2("Creating consumer directory: %s", path
);
1809 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1810 if (ret
< 0 && errno
!= EEXIST
) {
1812 ERR("Failed to create %s", path
);
1816 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
1818 ERR("Unable to set group on %s", path
);
1824 /* Create the consumerd error unix socket */
1825 consumer_data
->err_sock
=
1826 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1827 if (consumer_data
->err_sock
< 0) {
1828 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1834 * Set the CLOEXEC flag. Return code is useless because either way, the
1837 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1839 PERROR("utils_set_fd_cloexec");
1840 /* continue anyway */
1843 /* File permission MUST be 660 */
1844 ret
= chmod(consumer_data
->err_unix_sock_path
,
1845 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1847 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1857 * Signal handler for the daemon
1859 * Simply stop all worker threads, leaving main() return gracefully after
1860 * joining all threads and calling cleanup().
1862 static void sighandler(int sig
)
1866 DBG("SIGINT caught");
1870 DBG("SIGTERM caught");
1874 CMM_STORE_SHARED(recv_child_signal
, 1);
1882 * Setup signal handler for :
1883 * SIGINT, SIGTERM, SIGPIPE
1885 static int set_signal_handler(void)
1888 struct sigaction sa
;
1891 if ((ret
= sigemptyset(&sigset
)) < 0) {
1892 PERROR("sigemptyset");
1896 sa
.sa_mask
= sigset
;
1899 sa
.sa_handler
= sighandler
;
1900 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1901 PERROR("sigaction");
1905 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1906 PERROR("sigaction");
1910 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1911 PERROR("sigaction");
1915 sa
.sa_handler
= SIG_IGN
;
1916 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1917 PERROR("sigaction");
1921 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1927 * Set open files limit to unlimited. This daemon can open a large number of
1928 * file descriptors in order to consume multiple kernel traces.
1930 static void set_ulimit(void)
1935 /* The kernel does not allow an infinite limit for open files */
1936 lim
.rlim_cur
= 65535;
1937 lim
.rlim_max
= 65535;
1939 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1941 PERROR("failed to set open files limit");
1945 static int write_pidfile(void)
1947 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1950 static int set_clock_plugin_env(void)
1953 char *env_value
= NULL
;
1955 if (!config
.lttng_ust_clock_plugin
.value
) {
1959 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1960 config
.lttng_ust_clock_plugin
.value
);
1966 ret
= putenv(env_value
);
1969 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1973 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1974 config
.lttng_ust_clock_plugin
.value
);
1979 static void destroy_all_sessions_and_wait(void)
1981 struct ltt_session
*session
, *tmp
;
1982 struct ltt_session_list
*session_list
;
1984 session_list
= session_get_list();
1985 DBG("Initiating destruction of all sessions");
1987 if (!session_list
) {
1991 session_lock_list();
1992 /* Initiate the destruction of all sessions. */
1993 cds_list_for_each_entry_safe(session
, tmp
,
1994 &session_list
->head
, list
) {
1995 if (!session_get(session
)) {
1999 session_lock(session
);
2000 if (session
->destroyed
) {
2001 goto unlock_session
;
2003 (void) cmd_destroy_session(session
,
2004 notification_thread_handle
);
2006 session_unlock(session
);
2007 session_put(session
);
2009 session_unlock_list();
2011 /* Wait for the destruction of all sessions to complete. */
2012 DBG("Waiting for the destruction of all sessions to complete");
2013 session_list_wait_empty();
2014 DBG("Destruction of all sessions completed");
2020 int main(int argc
, char **argv
)
2022 int ret
= 0, retval
= 0;
2024 const char *env_app_timeout
;
2025 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
2026 *ust64_channel_monitor_pipe
= NULL
,
2027 *kernel_channel_monitor_pipe
= NULL
;
2028 struct lttng_thread
*ht_cleanup_thread
= NULL
;
2029 struct timer_thread_parameters timer_thread_parameters
;
2030 /* Rotation thread handle. */
2031 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
2032 /* Queue of rotation jobs populated by the sessiond-timer. */
2033 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
2034 struct lttng_thread
*client_thread
= NULL
;
2036 init_kernel_workarounds();
2038 rcu_register_thread();
2040 if (set_signal_handler()) {
2042 goto exit_set_signal_handler
;
2045 if (timer_signal_init()) {
2047 goto exit_set_signal_handler
;
2050 page_size
= sysconf(_SC_PAGESIZE
);
2051 if (page_size
< 0) {
2052 PERROR("sysconf _SC_PAGESIZE");
2053 page_size
= LONG_MAX
;
2054 WARN("Fallback page size to %ld", page_size
);
2057 ret
= sessiond_config_init(&config
);
2060 goto exit_set_signal_handler
;
2064 * Init config from environment variables.
2065 * Command line option override env configuration per-doc. Do env first.
2067 sessiond_config_apply_env_config(&config
);
2070 * Parse arguments and load the daemon configuration file.
2072 * We have an exit_options exit path to free memory reserved by
2073 * set_options. This is needed because the rest of sessiond_cleanup()
2074 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
2075 * depends on set_options.
2078 if (set_options(argc
, argv
)) {
2084 * Resolve all paths received as arguments, configuration option, or
2085 * through environment variable as absolute paths. This is necessary
2086 * since daemonizing causes the sessiond's current working directory
2089 ret
= sessiond_config_resolve_paths(&config
);
2095 lttng_opt_verbose
= config
.verbose
;
2096 lttng_opt_quiet
= config
.quiet
;
2097 kconsumer_data
.err_unix_sock_path
=
2098 config
.kconsumerd_err_unix_sock_path
.value
;
2099 kconsumer_data
.cmd_unix_sock_path
=
2100 config
.kconsumerd_cmd_unix_sock_path
.value
;
2101 ustconsumer32_data
.err_unix_sock_path
=
2102 config
.consumerd32_err_unix_sock_path
.value
;
2103 ustconsumer32_data
.cmd_unix_sock_path
=
2104 config
.consumerd32_cmd_unix_sock_path
.value
;
2105 ustconsumer64_data
.err_unix_sock_path
=
2106 config
.consumerd64_err_unix_sock_path
.value
;
2107 ustconsumer64_data
.cmd_unix_sock_path
=
2108 config
.consumerd64_cmd_unix_sock_path
.value
;
2109 set_clock_plugin_env();
2111 sessiond_config_log(&config
);
2113 if (create_lttng_rundir()) {
2118 /* Abort launch if a session daemon is already running. */
2119 if (check_existing_daemon()) {
2120 ERR("A session daemon is already running.");
2126 if (config
.daemonize
|| config
.background
) {
2129 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2130 !config
.background
);
2137 * We are in the child. Make sure all other file descriptors are
2138 * closed, in case we are called with more opened file
2139 * descriptors than the standard ones and the lock file.
2141 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2142 if (i
== lockfile_fd
) {
2149 if (run_as_create_worker(argv
[0]) < 0) {
2150 goto exit_create_run_as_worker_cleanup
;
2154 * Starting from here, we can create threads. This needs to be after
2155 * lttng_daemonize due to RCU.
2159 * Initialize the health check subsystem. This call should set the
2160 * appropriate time values.
2162 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
2163 if (!health_sessiond
) {
2164 PERROR("health_app_create error");
2166 goto exit_health_sessiond_cleanup
;
2169 /* Create thread to clean up RCU hash tables */
2170 ht_cleanup_thread
= launch_ht_cleanup_thread();
2171 if (!ht_cleanup_thread
) {
2173 goto exit_ht_cleanup
;
2176 /* Create thread quit pipe */
2177 if (sessiond_init_thread_quit_pipe()) {
2179 goto exit_init_data
;
2182 /* Check if daemon is UID = 0 */
2183 is_root
= !getuid();
2185 /* Create global run dir with root access */
2187 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
2188 if (!kernel_channel_monitor_pipe
) {
2189 ERR("Failed to create kernel consumer channel monitor pipe");
2191 goto exit_init_data
;
2193 kconsumer_data
.channel_monitor_pipe
=
2194 lttng_pipe_release_writefd(
2195 kernel_channel_monitor_pipe
);
2196 if (kconsumer_data
.channel_monitor_pipe
< 0) {
2198 goto exit_init_data
;
2202 /* Set consumer initial state */
2203 kernel_consumerd_state
= CONSUMER_STOPPED
;
2204 ust_consumerd_state
= CONSUMER_STOPPED
;
2206 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
2207 if (!ust32_channel_monitor_pipe
) {
2208 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
2210 goto exit_init_data
;
2212 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
2213 ust32_channel_monitor_pipe
);
2214 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
2216 goto exit_init_data
;
2220 * The rotation_thread_timer_queue structure is shared between the
2221 * sessiond timer thread and the rotation thread. The main thread keeps
2222 * its ownership and destroys it when both threads have been joined.
2224 rotation_timer_queue
= rotation_thread_timer_queue_create();
2225 if (!rotation_timer_queue
) {
2227 goto exit_init_data
;
2229 timer_thread_parameters
.rotation_thread_job_queue
=
2230 rotation_timer_queue
;
2232 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
2233 if (!ust64_channel_monitor_pipe
) {
2234 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
2236 goto exit_init_data
;
2238 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
2239 ust64_channel_monitor_pipe
);
2240 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
2242 goto exit_init_data
;
2246 * Init UST app hash table. Alloc hash table before this point since
2247 * cleanup() can get called after that point.
2249 if (ust_app_ht_alloc()) {
2250 ERR("Failed to allocate UST app hash table");
2252 goto exit_init_data
;
2256 * Initialize agent app hash table. We allocate the hash table here
2257 * since cleanup() can get called after this point.
2259 if (agent_app_ht_alloc()) {
2260 ERR("Failed to allocate Agent app hash table");
2262 goto exit_init_data
;
2266 * These actions must be executed as root. We do that *after* setting up
2267 * the sockets path because we MUST make the check for another daemon using
2268 * those paths *before* trying to set the kernel consumer sockets and init
2272 if (set_consumer_sockets(&kconsumer_data
)) {
2274 goto exit_init_data
;
2277 /* Setup kernel tracer */
2278 if (!config
.no_kernel
) {
2279 init_kernel_tracer();
2280 if (kernel_tracer_fd
>= 0) {
2281 ret
= syscall_init_table();
2283 ERR("Unable to populate syscall table. "
2284 "Syscall tracing won't work "
2285 "for this session daemon.");
2290 /* Set ulimit for open files */
2293 /* init lttng_fd tracking must be done after set_ulimit. */
2296 if (set_consumer_sockets(&ustconsumer64_data
)) {
2298 goto exit_init_data
;
2301 if (set_consumer_sockets(&ustconsumer32_data
)) {
2303 goto exit_init_data
;
2306 /* Set credentials to socket */
2307 if (is_root
&& set_permissions(config
.rundir
.value
)) {
2309 goto exit_init_data
;
2312 /* Get parent pid if -S, --sig-parent is specified. */
2313 if (config
.sig_parent
) {
2317 /* Setup the kernel pipe for waking up the kernel thread */
2318 if (is_root
&& !config
.no_kernel
) {
2319 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
2321 goto exit_init_data
;
2325 /* Setup the thread apps communication pipe. */
2326 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
2328 goto exit_init_data
;
2331 /* Setup the thread apps notify communication pipe. */
2332 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
2334 goto exit_init_data
;
2337 /* Initialize global buffer per UID and PID registry. */
2338 buffer_reg_init_uid_registry();
2339 buffer_reg_init_pid_registry();
2341 /* Init UST command queue. */
2342 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2346 /* Check for the application socket timeout env variable. */
2347 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
2348 if (env_app_timeout
) {
2349 config
.app_socket_timeout
= atoi(env_app_timeout
);
2351 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
2354 ret
= write_pidfile();
2356 ERR("Error in write_pidfile");
2358 goto exit_init_data
;
2361 /* Initialize communication library */
2363 /* Initialize TCP timeout values */
2364 lttcomm_inet_init();
2366 if (load_session_init_data(&load_info
) < 0) {
2368 goto exit_init_data
;
2370 load_info
->path
= config
.load_session_path
.value
;
2372 /* Create health-check thread. */
2373 if (!launch_health_management_thread()) {
2378 /* notification_thread_data acquires the pipes' read side. */
2379 notification_thread_handle
= notification_thread_handle_create(
2380 ust32_channel_monitor_pipe
,
2381 ust64_channel_monitor_pipe
,
2382 kernel_channel_monitor_pipe
);
2383 if (!notification_thread_handle
) {
2385 ERR("Failed to create notification thread shared data");
2386 goto exit_notification
;
2389 /* Create notification thread. */
2390 if (!launch_notification_thread(notification_thread_handle
)) {
2392 goto exit_notification
;
2395 /* Create timer thread. */
2396 if (!launch_timer_thread(&timer_thread_parameters
)) {
2398 goto exit_notification
;
2401 /* rotation_thread_data acquires the pipes' read side. */
2402 rotation_thread_handle
= rotation_thread_handle_create(
2403 rotation_timer_queue
,
2404 notification_thread_handle
);
2405 if (!rotation_thread_handle
) {
2407 ERR("Failed to create rotation thread shared data");
2412 /* Create rotation thread. */
2413 if (!launch_rotation_thread(rotation_thread_handle
)) {
2418 /* Create thread to manage the client socket */
2419 client_thread
= launch_client_thread();
2420 if (!client_thread
) {
2425 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
2426 apps_cmd_notify_pipe
[1])) {
2431 /* Create thread to manage application registration. */
2432 if (!launch_application_registration_thread(&ust_cmd_queue
)) {
2437 /* Create thread to manage application socket */
2438 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
2443 /* Create thread to manage application notify socket */
2444 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
2446 goto exit_apps_notify
;
2449 /* Create agent management thread. */
2450 if (!launch_agent_management_thread()) {
2452 goto exit_agent_reg
;
2455 /* Don't start this thread if kernel tracing is not requested nor root */
2456 if (is_root
&& !config
.no_kernel
) {
2457 /* Create kernel thread to manage kernel event */
2458 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
2459 thread_manage_kernel
, (void *) NULL
);
2462 PERROR("pthread_create kernel");
2469 /* Create session loading thread. */
2470 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
2471 thread_load_session
, load_info
);
2474 PERROR("pthread_create load_session_thread");
2477 goto exit_load_session
;
2481 * This is where we start awaiting program completion (e.g. through
2482 * signal that asks threads to teardown).
2485 ret
= pthread_join(load_session_thread
, &status
);
2488 PERROR("pthread_join load_session_thread");
2492 /* Initiate teardown once activity occurs on the quit pipe. */
2493 sessiond_wait_for_quit_pipe(-1U);
2496 * Ensure that the client thread is no longer accepting new commands,
2497 * which could cause new sessions to be created.
2499 if (!lttng_thread_shutdown(client_thread
)) {
2500 ERR("Failed to shutdown the client thread, continuing teardown");
2501 lttng_thread_put(client_thread
);
2502 client_thread
= NULL
;
2505 destroy_all_sessions_and_wait();
2508 if (is_root
&& !config
.no_kernel
) {
2509 ret
= pthread_join(kernel_thread
, &status
);
2512 PERROR("pthread_join");
2525 lttng_thread_list_shutdown_orphans();
2528 if (client_thread
) {
2529 lttng_thread_put(client_thread
);
2533 * Wait for all pending call_rcu work to complete before tearing
2534 * down data structures. call_rcu worker may be trying to
2535 * perform lookups in those structures.
2539 * sessiond_cleanup() is called when no other thread is running, except
2540 * the ht_cleanup thread, which is needed to destroy the hash tables.
2542 rcu_thread_online();
2546 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2547 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2548 * the queue is empty before shutting down the clean-up thread.
2552 if (ht_cleanup_thread
) {
2553 lttng_thread_shutdown(ht_cleanup_thread
);
2554 lttng_thread_put(ht_cleanup_thread
);
2557 rcu_thread_offline();
2558 rcu_unregister_thread();
2560 if (rotation_thread_handle
) {
2561 rotation_thread_handle_destroy(rotation_thread_handle
);
2565 * After the rotation and timer thread have quit, we can safely destroy
2566 * the rotation_timer_queue.
2568 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2570 * The teardown of the notification system is performed after the
2571 * session daemon's teardown in order to allow it to be notified
2572 * of the active session and channels at the moment of the teardown.
2574 if (notification_thread_handle
) {
2575 notification_thread_handle_destroy(notification_thread_handle
);
2577 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2578 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2579 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2582 health_app_destroy(health_sessiond
);
2583 exit_health_sessiond_cleanup
:
2584 exit_create_run_as_worker_cleanup
:
2587 sessiond_cleanup_lock_file();
2588 sessiond_cleanup_options();
2590 exit_set_signal_handler
: