6d0fb42e393e8ab072c25c5016c9eb776073e0c6
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
1 /*
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>
5 *
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.
9 *
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.
14 *
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.
18 */
19
20 #define _LGPL_SOURCE
21 #include <getopt.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <paths.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <sys/mman.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <urcu/uatomic.h>
39 #include <unistd.h>
40 #include <ctype.h>
41
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>
54
55 #include "lttng-sessiond.h"
56 #include "buffer-registry.h"
57 #include "channel.h"
58 #include "cmd.h"
59 #include "consumer.h"
60 #include "context.h"
61 #include "event.h"
62 #include "kernel.h"
63 #include "kernel-consumer.h"
64 #include "modprobe.h"
65 #include "shm.h"
66 #include "ust-ctl.h"
67 #include "ust-consumer.h"
68 #include "utils.h"
69 #include "fd-limit.h"
70 #include "health-sessiond.h"
71 #include "testpoint.h"
72 #include "ust-thread.h"
73 #include "agent-thread.h"
74 #include "save.h"
75 #include "notification-thread.h"
76 #include "notification-thread-commands.h"
77 #include "rotation-thread.h"
78 #include "lttng-syscall.h"
79 #include "agent.h"
80 #include "ht-cleanup.h"
81 #include "sessiond-config.h"
82 #include "timer.h"
83 #include "thread.h"
84 #include "client.h"
85 #include "dispatch.h"
86 #include "register.h"
87 #include "manage-apps.h"
88 #include "manage-kernel.h"
89
90 static const char *help_msg =
91 #ifdef LTTNG_EMBED_HELP
92 #include <lttng-sessiond.8.h>
93 #else
94 NULL
95 #endif
96 ;
97
98 const char *progname;
99 static int lockfile_fd = -1;
100
101 /* Set to 1 when a SIGUSR1 signal is received. */
102 static int recv_child_signal;
103
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' },
134 { NULL, 0, 0, 0 }
135 };
136
137 /* Command line options to ignore from configuration file */
138 static const char *config_ignore_options[] = { "help", "version", "config" };
139
140 /*
141 * This pipe is used to inform the thread managing application communication
142 * that a command is queued and ready to be processed.
143 */
144 static int apps_cmd_pipe[2] = { -1, -1 };
145 static int apps_cmd_notify_pipe[2] = { -1, -1 };
146
147 /*
148 * UST registration command queue. This queue is tied with a futex and uses a N
149 * wakers / 1 waiter implemented and detailed in futex.c/.h
150 *
151 * The thread_registration_apps and thread_dispatch_ust_registration uses this
152 * queue along with the wait/wake scheme. The thread_manage_apps receives down
153 * the line new application socket and monitors it for any I/O error or clean
154 * close that triggers an unregistration of the application.
155 */
156 static struct ust_cmd_queue ust_cmd_queue;
157
158 static const char *module_proc_lttng = "/proc/lttng";
159
160 /*
161 * Section name to look for in the daemon configuration file.
162 */
163 static const char * const config_section_name = "sessiond";
164
165 /* Am I root or not. Set to 1 if the daemon is running as root */
166 static int is_root;
167
168 /*
169 * Stop all threads by closing the thread quit pipe.
170 */
171 static void stop_threads(void)
172 {
173 int ret;
174
175 /* Stopping all threads */
176 DBG("Terminating all threads");
177 ret = sessiond_notify_quit_pipe();
178 if (ret < 0) {
179 ERR("write error on thread quit pipe");
180 }
181 }
182
183 /*
184 * Close every consumer sockets.
185 */
186 static void close_consumer_sockets(void)
187 {
188 int ret;
189
190 if (kconsumer_data.err_sock >= 0) {
191 ret = close(kconsumer_data.err_sock);
192 if (ret < 0) {
193 PERROR("kernel consumer err_sock close");
194 }
195 }
196 if (ustconsumer32_data.err_sock >= 0) {
197 ret = close(ustconsumer32_data.err_sock);
198 if (ret < 0) {
199 PERROR("UST consumerd32 err_sock close");
200 }
201 }
202 if (ustconsumer64_data.err_sock >= 0) {
203 ret = close(ustconsumer64_data.err_sock);
204 if (ret < 0) {
205 PERROR("UST consumerd64 err_sock close");
206 }
207 }
208 if (kconsumer_data.cmd_sock >= 0) {
209 ret = close(kconsumer_data.cmd_sock);
210 if (ret < 0) {
211 PERROR("kernel consumer cmd_sock close");
212 }
213 }
214 if (ustconsumer32_data.cmd_sock >= 0) {
215 ret = close(ustconsumer32_data.cmd_sock);
216 if (ret < 0) {
217 PERROR("UST consumerd32 cmd_sock close");
218 }
219 }
220 if (ustconsumer64_data.cmd_sock >= 0) {
221 ret = close(ustconsumer64_data.cmd_sock);
222 if (ret < 0) {
223 PERROR("UST consumerd64 cmd_sock close");
224 }
225 }
226 if (kconsumer_data.channel_monitor_pipe >= 0) {
227 ret = close(kconsumer_data.channel_monitor_pipe);
228 if (ret < 0) {
229 PERROR("kernel consumer channel monitor pipe close");
230 }
231 }
232 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
233 ret = close(ustconsumer32_data.channel_monitor_pipe);
234 if (ret < 0) {
235 PERROR("UST consumerd32 channel monitor pipe close");
236 }
237 }
238 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
239 ret = close(ustconsumer64_data.channel_monitor_pipe);
240 if (ret < 0) {
241 PERROR("UST consumerd64 channel monitor pipe close");
242 }
243 }
244 }
245
246 /*
247 * Wait on consumer process termination.
248 *
249 * Need to be called with the consumer data lock held or from a context
250 * ensuring no concurrent access to data (e.g: cleanup).
251 */
252 static void wait_consumer(struct consumer_data *consumer_data)
253 {
254 pid_t ret;
255 int status;
256
257 if (consumer_data->pid <= 0) {
258 return;
259 }
260
261 DBG("Waiting for complete teardown of consumerd (PID: %d)",
262 consumer_data->pid);
263 ret = waitpid(consumer_data->pid, &status, 0);
264 if (ret == -1) {
265 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
266 } else if (!WIFEXITED(status)) {
267 ERR("consumerd termination with error: %d",
268 WEXITSTATUS(ret));
269 }
270 consumer_data->pid = 0;
271 }
272
273 /*
274 * Cleanup the session daemon's data structures.
275 */
276 static void sessiond_cleanup(void)
277 {
278 int ret;
279 struct ltt_session_list *session_list = session_get_list();
280
281 DBG("Cleanup sessiond");
282
283 /*
284 * Close the thread quit pipe. It has already done its job,
285 * since we are now called.
286 */
287 sessiond_close_quit_pipe();
288 utils_close_pipe(apps_cmd_pipe);
289 utils_close_pipe(apps_cmd_notify_pipe);
290 utils_close_pipe(kernel_poll_pipe);
291
292 ret = remove(config.pid_file_path.value);
293 if (ret < 0) {
294 PERROR("remove pidfile %s", config.pid_file_path.value);
295 }
296
297 DBG("Removing sessiond and consumerd content of directory %s",
298 config.rundir.value);
299
300 /* sessiond */
301 DBG("Removing %s", config.pid_file_path.value);
302 (void) unlink(config.pid_file_path.value);
303
304 DBG("Removing %s", config.agent_port_file_path.value);
305 (void) unlink(config.agent_port_file_path.value);
306
307 /* kconsumerd */
308 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
309 (void) unlink(kconsumer_data.err_unix_sock_path);
310
311 DBG("Removing directory %s", config.kconsumerd_path.value);
312 (void) rmdir(config.kconsumerd_path.value);
313
314 /* ust consumerd 32 */
315 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
316 (void) unlink(config.consumerd32_err_unix_sock_path.value);
317
318 DBG("Removing directory %s", config.consumerd32_path.value);
319 (void) rmdir(config.consumerd32_path.value);
320
321 /* ust consumerd 64 */
322 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
323 (void) unlink(config.consumerd64_err_unix_sock_path.value);
324
325 DBG("Removing directory %s", config.consumerd64_path.value);
326 (void) rmdir(config.consumerd64_path.value);
327
328 pthread_mutex_destroy(&session_list->lock);
329
330 wait_consumer(&kconsumer_data);
331 wait_consumer(&ustconsumer64_data);
332 wait_consumer(&ustconsumer32_data);
333
334 DBG("Cleaning up all agent apps");
335 agent_app_ht_clean();
336
337 DBG("Closing all UST sockets");
338 ust_app_clean_list();
339 buffer_reg_destroy_registries();
340
341 if (is_root && !config.no_kernel) {
342 DBG2("Closing kernel fd");
343 if (kernel_tracer_fd >= 0) {
344 ret = close(kernel_tracer_fd);
345 if (ret) {
346 PERROR("close");
347 }
348 }
349 DBG("Unloading kernel modules");
350 modprobe_remove_lttng_all();
351 free(syscall_table);
352 }
353
354 close_consumer_sockets();
355
356 /*
357 * We do NOT rmdir rundir because there are other processes
358 * using it, for instance lttng-relayd, which can start in
359 * parallel with this teardown.
360 */
361 }
362
363 /*
364 * Cleanup the daemon's option data structures.
365 */
366 static void sessiond_cleanup_options(void)
367 {
368 DBG("Cleaning up options");
369
370 sessiond_config_fini(&config);
371
372 run_as_destroy_worker();
373 }
374
375 /*
376 * Signal pthread condition of the consumer data that the thread.
377 */
378 static void signal_consumer_condition(struct consumer_data *data, int state)
379 {
380 pthread_mutex_lock(&data->cond_mutex);
381
382 /*
383 * The state is set before signaling. It can be any value, it's the waiter
384 * job to correctly interpret this condition variable associated to the
385 * consumer pthread_cond.
386 *
387 * A value of 0 means that the corresponding thread of the consumer data
388 * was not started. 1 indicates that the thread has started and is ready
389 * for action. A negative value means that there was an error during the
390 * thread bootstrap.
391 */
392 data->consumer_thread_is_ready = state;
393 (void) pthread_cond_signal(&data->cond);
394
395 pthread_mutex_unlock(&data->cond_mutex);
396 }
397
398 /*
399 * This thread manage the consumer error sent back to the session daemon.
400 */
401 void *thread_manage_consumer(void *data)
402 {
403 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
404 uint32_t revents, nb_fd;
405 enum lttcomm_return_code code;
406 struct lttng_poll_event events;
407 struct consumer_data *consumer_data = data;
408 struct consumer_socket *cmd_socket_wrapper = NULL;
409
410 DBG("[thread] Manage consumer started");
411
412 rcu_register_thread();
413 rcu_thread_online();
414
415 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
416
417 health_code_update();
418
419 /*
420 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
421 * metadata_sock. Nothing more will be added to this poll set.
422 */
423 ret = sessiond_set_thread_pollset(&events, 3);
424 if (ret < 0) {
425 goto error_poll;
426 }
427
428 /*
429 * The error socket here is already in a listening state which was done
430 * just before spawning this thread to avoid a race between the consumer
431 * daemon exec trying to connect and the listen() call.
432 */
433 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
434 if (ret < 0) {
435 goto error;
436 }
437
438 health_code_update();
439
440 /* Infinite blocking call, waiting for transmission */
441 restart:
442 health_poll_entry();
443
444 if (testpoint(sessiond_thread_manage_consumer)) {
445 goto error;
446 }
447
448 ret = lttng_poll_wait(&events, -1);
449 health_poll_exit();
450 if (ret < 0) {
451 /*
452 * Restart interrupted system call.
453 */
454 if (errno == EINTR) {
455 goto restart;
456 }
457 goto error;
458 }
459
460 nb_fd = ret;
461
462 for (i = 0; i < nb_fd; i++) {
463 /* Fetch once the poll data */
464 revents = LTTNG_POLL_GETEV(&events, i);
465 pollfd = LTTNG_POLL_GETFD(&events, i);
466
467 health_code_update();
468
469 if (!revents) {
470 /* No activity for this FD (poll implementation). */
471 continue;
472 }
473
474 /* Thread quit pipe has been closed. Killing thread. */
475 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
476 if (ret) {
477 err = 0;
478 goto exit;
479 }
480
481 /* Event on the registration socket */
482 if (pollfd == consumer_data->err_sock) {
483 if (revents & LPOLLIN) {
484 continue;
485 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
486 ERR("consumer err socket poll error");
487 goto error;
488 } else {
489 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
490 goto error;
491 }
492 }
493 }
494
495 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
496 if (sock < 0) {
497 goto error;
498 }
499
500 /*
501 * Set the CLOEXEC flag. Return code is useless because either way, the
502 * show must go on.
503 */
504 (void) utils_set_fd_cloexec(sock);
505
506 health_code_update();
507
508 DBG2("Receiving code from consumer err_sock");
509
510 /* Getting status code from kconsumerd */
511 ret = lttcomm_recv_unix_sock(sock, &code,
512 sizeof(enum lttcomm_return_code));
513 if (ret <= 0) {
514 goto error;
515 }
516
517 health_code_update();
518 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
519 ERR("consumer error when waiting for SOCK_READY : %s",
520 lttcomm_get_readable_code(-code));
521 goto error;
522 }
523
524 /* Connect both command and metadata sockets. */
525 consumer_data->cmd_sock =
526 lttcomm_connect_unix_sock(
527 consumer_data->cmd_unix_sock_path);
528 consumer_data->metadata_fd =
529 lttcomm_connect_unix_sock(
530 consumer_data->cmd_unix_sock_path);
531 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
532 PERROR("consumer connect cmd socket");
533 /* On error, signal condition and quit. */
534 signal_consumer_condition(consumer_data, -1);
535 goto error;
536 }
537
538 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
539
540 /* Create metadata socket lock. */
541 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
542 if (consumer_data->metadata_sock.lock == NULL) {
543 PERROR("zmalloc pthread mutex");
544 goto error;
545 }
546 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
547
548 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
549 DBG("Consumer metadata socket ready (fd: %d)",
550 consumer_data->metadata_fd);
551
552 /*
553 * Remove the consumerd error sock since we've established a connection.
554 */
555 ret = lttng_poll_del(&events, consumer_data->err_sock);
556 if (ret < 0) {
557 goto error;
558 }
559
560 /* Add new accepted error socket. */
561 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
562 if (ret < 0) {
563 goto error;
564 }
565
566 /* Add metadata socket that is successfully connected. */
567 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
568 LPOLLIN | LPOLLRDHUP);
569 if (ret < 0) {
570 goto error;
571 }
572
573 health_code_update();
574
575 /*
576 * Transfer the write-end of the channel monitoring and rotate pipe
577 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
578 */
579 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
580 if (!cmd_socket_wrapper) {
581 goto error;
582 }
583 cmd_socket_wrapper->lock = &consumer_data->lock;
584
585 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
586 consumer_data->channel_monitor_pipe);
587 if (ret) {
588 goto error;
589 }
590
591 /* Discard the socket wrapper as it is no longer needed. */
592 consumer_destroy_socket(cmd_socket_wrapper);
593 cmd_socket_wrapper = NULL;
594
595 /* The thread is completely initialized, signal that it is ready. */
596 signal_consumer_condition(consumer_data, 1);
597
598 /* Infinite blocking call, waiting for transmission */
599 restart_poll:
600 while (1) {
601 health_code_update();
602
603 /* Exit the thread because the thread quit pipe has been triggered. */
604 if (should_quit) {
605 /* Not a health error. */
606 err = 0;
607 goto exit;
608 }
609
610 health_poll_entry();
611 ret = lttng_poll_wait(&events, -1);
612 health_poll_exit();
613 if (ret < 0) {
614 /*
615 * Restart interrupted system call.
616 */
617 if (errno == EINTR) {
618 goto restart_poll;
619 }
620 goto error;
621 }
622
623 nb_fd = ret;
624
625 for (i = 0; i < nb_fd; i++) {
626 /* Fetch once the poll data */
627 revents = LTTNG_POLL_GETEV(&events, i);
628 pollfd = LTTNG_POLL_GETFD(&events, i);
629
630 health_code_update();
631
632 if (!revents) {
633 /* No activity for this FD (poll implementation). */
634 continue;
635 }
636
637 /*
638 * Thread quit pipe has been triggered, flag that we should stop
639 * but continue the current loop to handle potential data from
640 * consumer.
641 */
642 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
643
644 if (pollfd == sock) {
645 /* Event on the consumerd socket */
646 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
647 && !(revents & LPOLLIN)) {
648 ERR("consumer err socket second poll error");
649 goto error;
650 }
651 health_code_update();
652 /* Wait for any kconsumerd error */
653 ret = lttcomm_recv_unix_sock(sock, &code,
654 sizeof(enum lttcomm_return_code));
655 if (ret <= 0) {
656 ERR("consumer closed the command socket");
657 goto error;
658 }
659
660 ERR("consumer return code : %s",
661 lttcomm_get_readable_code(-code));
662
663 goto exit;
664 } else if (pollfd == consumer_data->metadata_fd) {
665 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
666 && !(revents & LPOLLIN)) {
667 ERR("consumer err metadata socket second poll error");
668 goto error;
669 }
670 /* UST metadata requests */
671 ret = ust_consumer_metadata_request(
672 &consumer_data->metadata_sock);
673 if (ret < 0) {
674 ERR("Handling metadata request");
675 goto error;
676 }
677 }
678 /* No need for an else branch all FDs are tested prior. */
679 }
680 health_code_update();
681 }
682
683 exit:
684 error:
685 /*
686 * We lock here because we are about to close the sockets and some other
687 * thread might be using them so get exclusive access which will abort all
688 * other consumer command by other threads.
689 */
690 pthread_mutex_lock(&consumer_data->lock);
691
692 /* Immediately set the consumerd state to stopped */
693 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
694 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
695 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
696 consumer_data->type == LTTNG_CONSUMER32_UST) {
697 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
698 } else {
699 /* Code flow error... */
700 assert(0);
701 }
702
703 if (consumer_data->err_sock >= 0) {
704 ret = close(consumer_data->err_sock);
705 if (ret) {
706 PERROR("close");
707 }
708 consumer_data->err_sock = -1;
709 }
710 if (consumer_data->cmd_sock >= 0) {
711 ret = close(consumer_data->cmd_sock);
712 if (ret) {
713 PERROR("close");
714 }
715 consumer_data->cmd_sock = -1;
716 }
717 if (consumer_data->metadata_sock.fd_ptr &&
718 *consumer_data->metadata_sock.fd_ptr >= 0) {
719 ret = close(*consumer_data->metadata_sock.fd_ptr);
720 if (ret) {
721 PERROR("close");
722 }
723 }
724 if (sock >= 0) {
725 ret = close(sock);
726 if (ret) {
727 PERROR("close");
728 }
729 }
730
731 unlink(consumer_data->err_unix_sock_path);
732 unlink(consumer_data->cmd_unix_sock_path);
733 pthread_mutex_unlock(&consumer_data->lock);
734
735 /* Cleanup metadata socket mutex. */
736 if (consumer_data->metadata_sock.lock) {
737 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
738 free(consumer_data->metadata_sock.lock);
739 }
740 lttng_poll_clean(&events);
741
742 if (cmd_socket_wrapper) {
743 consumer_destroy_socket(cmd_socket_wrapper);
744 }
745 error_poll:
746 if (err) {
747 health_error();
748 ERR("Health error occurred in %s", __func__);
749 }
750 health_unregister(health_sessiond);
751 DBG("consumer thread cleanup completed");
752
753 rcu_thread_offline();
754 rcu_unregister_thread();
755
756 return NULL;
757 }
758
759 /*
760 * Setup necessary data for kernel tracer action.
761 */
762 static int init_kernel_tracer(void)
763 {
764 int ret;
765
766 /* Modprobe lttng kernel modules */
767 ret = modprobe_lttng_control();
768 if (ret < 0) {
769 goto error;
770 }
771
772 /* Open debugfs lttng */
773 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
774 if (kernel_tracer_fd < 0) {
775 DBG("Failed to open %s", module_proc_lttng);
776 goto error_open;
777 }
778
779 /* Validate kernel version */
780 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
781 &kernel_tracer_abi_version);
782 if (ret < 0) {
783 goto error_version;
784 }
785
786 ret = modprobe_lttng_data();
787 if (ret < 0) {
788 goto error_modules;
789 }
790
791 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
792 kernel_tracer_fd);
793 if (ret < 0) {
794 goto error_modules;
795 }
796
797 if (ret < 1) {
798 WARN("Kernel tracer does not support buffer monitoring. "
799 "The monitoring timer of channels in the kernel domain "
800 "will be set to 0 (disabled).");
801 }
802
803 DBG("Kernel tracer fd %d", kernel_tracer_fd);
804 return 0;
805
806 error_version:
807 modprobe_remove_lttng_control();
808 ret = close(kernel_tracer_fd);
809 if (ret) {
810 PERROR("close");
811 }
812 kernel_tracer_fd = -1;
813 return LTTNG_ERR_KERN_VERSION;
814
815 error_modules:
816 ret = close(kernel_tracer_fd);
817 if (ret) {
818 PERROR("close");
819 }
820
821 error_open:
822 modprobe_remove_lttng_control();
823
824 error:
825 WARN("No kernel tracer available");
826 kernel_tracer_fd = -1;
827 if (!is_root) {
828 return LTTNG_ERR_NEED_ROOT_SESSIOND;
829 } else {
830 return LTTNG_ERR_KERN_NA;
831 }
832 }
833
834 static int string_match(const char *str1, const char *str2)
835 {
836 return (str1 && str2) && !strcmp(str1, str2);
837 }
838
839 /*
840 * Take an option from the getopt output and set it in the right variable to be
841 * used later.
842 *
843 * Return 0 on success else a negative value.
844 */
845 static int set_option(int opt, const char *arg, const char *optname)
846 {
847 int ret = 0;
848
849 if (string_match(optname, "client-sock") || opt == 'c') {
850 if (!arg || *arg == '\0') {
851 ret = -EINVAL;
852 goto end;
853 }
854 if (lttng_is_setuid_setgid()) {
855 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
856 "-c, --client-sock");
857 } else {
858 config_string_set(&config.client_unix_sock_path,
859 strdup(arg));
860 if (!config.client_unix_sock_path.value) {
861 ret = -ENOMEM;
862 PERROR("strdup");
863 }
864 }
865 } else if (string_match(optname, "apps-sock") || opt == 'a') {
866 if (!arg || *arg == '\0') {
867 ret = -EINVAL;
868 goto end;
869 }
870 if (lttng_is_setuid_setgid()) {
871 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
872 "-a, --apps-sock");
873 } else {
874 config_string_set(&config.apps_unix_sock_path,
875 strdup(arg));
876 if (!config.apps_unix_sock_path.value) {
877 ret = -ENOMEM;
878 PERROR("strdup");
879 }
880 }
881 } else if (string_match(optname, "daemonize") || opt == 'd') {
882 config.daemonize = true;
883 } else if (string_match(optname, "background") || opt == 'b') {
884 config.background = true;
885 } else if (string_match(optname, "group") || opt == 'g') {
886 if (!arg || *arg == '\0') {
887 ret = -EINVAL;
888 goto end;
889 }
890 if (lttng_is_setuid_setgid()) {
891 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
892 "-g, --group");
893 } else {
894 config_string_set(&config.tracing_group_name,
895 strdup(arg));
896 if (!config.tracing_group_name.value) {
897 ret = -ENOMEM;
898 PERROR("strdup");
899 }
900 }
901 } else if (string_match(optname, "help") || opt == 'h') {
902 ret = utils_show_help(8, "lttng-sessiond", help_msg);
903 if (ret) {
904 ERR("Cannot show --help for `lttng-sessiond`");
905 perror("exec");
906 }
907 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
908 } else if (string_match(optname, "version") || opt == 'V') {
909 fprintf(stdout, "%s\n", VERSION);
910 exit(EXIT_SUCCESS);
911 } else if (string_match(optname, "sig-parent") || opt == 'S') {
912 config.sig_parent = true;
913 } else if (string_match(optname, "kconsumerd-err-sock")) {
914 if (!arg || *arg == '\0') {
915 ret = -EINVAL;
916 goto end;
917 }
918 if (lttng_is_setuid_setgid()) {
919 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
920 "--kconsumerd-err-sock");
921 } else {
922 config_string_set(&config.kconsumerd_err_unix_sock_path,
923 strdup(arg));
924 if (!config.kconsumerd_err_unix_sock_path.value) {
925 ret = -ENOMEM;
926 PERROR("strdup");
927 }
928 }
929 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
930 if (!arg || *arg == '\0') {
931 ret = -EINVAL;
932 goto end;
933 }
934 if (lttng_is_setuid_setgid()) {
935 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
936 "--kconsumerd-cmd-sock");
937 } else {
938 config_string_set(&config.kconsumerd_cmd_unix_sock_path,
939 strdup(arg));
940 if (!config.kconsumerd_cmd_unix_sock_path.value) {
941 ret = -ENOMEM;
942 PERROR("strdup");
943 }
944 }
945 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
946 if (!arg || *arg == '\0') {
947 ret = -EINVAL;
948 goto end;
949 }
950 if (lttng_is_setuid_setgid()) {
951 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
952 "--ustconsumerd64-err-sock");
953 } else {
954 config_string_set(&config.consumerd64_err_unix_sock_path,
955 strdup(arg));
956 if (!config.consumerd64_err_unix_sock_path.value) {
957 ret = -ENOMEM;
958 PERROR("strdup");
959 }
960 }
961 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
962 if (!arg || *arg == '\0') {
963 ret = -EINVAL;
964 goto end;
965 }
966 if (lttng_is_setuid_setgid()) {
967 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
968 "--ustconsumerd64-cmd-sock");
969 } else {
970 config_string_set(&config.consumerd64_cmd_unix_sock_path,
971 strdup(arg));
972 if (!config.consumerd64_cmd_unix_sock_path.value) {
973 ret = -ENOMEM;
974 PERROR("strdup");
975 }
976 }
977 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
978 if (!arg || *arg == '\0') {
979 ret = -EINVAL;
980 goto end;
981 }
982 if (lttng_is_setuid_setgid()) {
983 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
984 "--ustconsumerd32-err-sock");
985 } else {
986 config_string_set(&config.consumerd32_err_unix_sock_path,
987 strdup(arg));
988 if (!config.consumerd32_err_unix_sock_path.value) {
989 ret = -ENOMEM;
990 PERROR("strdup");
991 }
992 }
993 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
994 if (!arg || *arg == '\0') {
995 ret = -EINVAL;
996 goto end;
997 }
998 if (lttng_is_setuid_setgid()) {
999 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1000 "--ustconsumerd32-cmd-sock");
1001 } else {
1002 config_string_set(&config.consumerd32_cmd_unix_sock_path,
1003 strdup(arg));
1004 if (!config.consumerd32_cmd_unix_sock_path.value) {
1005 ret = -ENOMEM;
1006 PERROR("strdup");
1007 }
1008 }
1009 } else if (string_match(optname, "no-kernel")) {
1010 config.no_kernel = true;
1011 } else if (string_match(optname, "quiet") || opt == 'q') {
1012 config.quiet = true;
1013 } else if (string_match(optname, "verbose") || opt == 'v') {
1014 /* Verbose level can increase using multiple -v */
1015 if (arg) {
1016 /* Value obtained from config file */
1017 config.verbose = config_parse_value(arg);
1018 } else {
1019 /* -v used on command line */
1020 config.verbose++;
1021 }
1022 /* Clamp value to [0, 3] */
1023 config.verbose = config.verbose < 0 ? 0 :
1024 (config.verbose <= 3 ? config.verbose : 3);
1025 } else if (string_match(optname, "verbose-consumer")) {
1026 if (arg) {
1027 config.verbose_consumer = config_parse_value(arg);
1028 } else {
1029 config.verbose_consumer++;
1030 }
1031 } else if (string_match(optname, "consumerd32-path")) {
1032 if (!arg || *arg == '\0') {
1033 ret = -EINVAL;
1034 goto end;
1035 }
1036 if (lttng_is_setuid_setgid()) {
1037 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1038 "--consumerd32-path");
1039 } else {
1040 config_string_set(&config.consumerd32_bin_path,
1041 strdup(arg));
1042 if (!config.consumerd32_bin_path.value) {
1043 PERROR("strdup");
1044 ret = -ENOMEM;
1045 }
1046 }
1047 } else if (string_match(optname, "consumerd32-libdir")) {
1048 if (!arg || *arg == '\0') {
1049 ret = -EINVAL;
1050 goto end;
1051 }
1052 if (lttng_is_setuid_setgid()) {
1053 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1054 "--consumerd32-libdir");
1055 } else {
1056 config_string_set(&config.consumerd32_lib_dir,
1057 strdup(arg));
1058 if (!config.consumerd32_lib_dir.value) {
1059 PERROR("strdup");
1060 ret = -ENOMEM;
1061 }
1062 }
1063 } else if (string_match(optname, "consumerd64-path")) {
1064 if (!arg || *arg == '\0') {
1065 ret = -EINVAL;
1066 goto end;
1067 }
1068 if (lttng_is_setuid_setgid()) {
1069 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1070 "--consumerd64-path");
1071 } else {
1072 config_string_set(&config.consumerd64_bin_path,
1073 strdup(arg));
1074 if (!config.consumerd64_bin_path.value) {
1075 PERROR("strdup");
1076 ret = -ENOMEM;
1077 }
1078 }
1079 } else if (string_match(optname, "consumerd64-libdir")) {
1080 if (!arg || *arg == '\0') {
1081 ret = -EINVAL;
1082 goto end;
1083 }
1084 if (lttng_is_setuid_setgid()) {
1085 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1086 "--consumerd64-libdir");
1087 } else {
1088 config_string_set(&config.consumerd64_lib_dir,
1089 strdup(arg));
1090 if (!config.consumerd64_lib_dir.value) {
1091 PERROR("strdup");
1092 ret = -ENOMEM;
1093 }
1094 }
1095 } else if (string_match(optname, "pidfile") || opt == 'p') {
1096 if (!arg || *arg == '\0') {
1097 ret = -EINVAL;
1098 goto end;
1099 }
1100 if (lttng_is_setuid_setgid()) {
1101 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1102 "-p, --pidfile");
1103 } else {
1104 config_string_set(&config.pid_file_path, strdup(arg));
1105 if (!config.pid_file_path.value) {
1106 PERROR("strdup");
1107 ret = -ENOMEM;
1108 }
1109 }
1110 } else if (string_match(optname, "agent-tcp-port")) {
1111 if (!arg || *arg == '\0') {
1112 ret = -EINVAL;
1113 goto end;
1114 }
1115 if (lttng_is_setuid_setgid()) {
1116 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1117 "--agent-tcp-port");
1118 } else {
1119 unsigned long v;
1120
1121 errno = 0;
1122 v = strtoul(arg, NULL, 0);
1123 if (errno != 0 || !isdigit(arg[0])) {
1124 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
1125 return -1;
1126 }
1127 if (v == 0 || v >= 65535) {
1128 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
1129 return -1;
1130 }
1131 config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v;
1132 DBG3("Agent TCP port set to non default: %i", (int) v);
1133 }
1134 } else if (string_match(optname, "load") || opt == 'l') {
1135 if (!arg || *arg == '\0') {
1136 ret = -EINVAL;
1137 goto end;
1138 }
1139 if (lttng_is_setuid_setgid()) {
1140 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1141 "-l, --load");
1142 } else {
1143 config_string_set(&config.load_session_path, strdup(arg));
1144 if (!config.load_session_path.value) {
1145 PERROR("strdup");
1146 ret = -ENOMEM;
1147 }
1148 }
1149 } else if (string_match(optname, "kmod-probes")) {
1150 if (!arg || *arg == '\0') {
1151 ret = -EINVAL;
1152 goto end;
1153 }
1154 if (lttng_is_setuid_setgid()) {
1155 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1156 "--kmod-probes");
1157 } else {
1158 config_string_set(&config.kmod_probes_list, strdup(arg));
1159 if (!config.kmod_probes_list.value) {
1160 PERROR("strdup");
1161 ret = -ENOMEM;
1162 }
1163 }
1164 } else if (string_match(optname, "extra-kmod-probes")) {
1165 if (!arg || *arg == '\0') {
1166 ret = -EINVAL;
1167 goto end;
1168 }
1169 if (lttng_is_setuid_setgid()) {
1170 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1171 "--extra-kmod-probes");
1172 } else {
1173 config_string_set(&config.kmod_extra_probes_list,
1174 strdup(arg));
1175 if (!config.kmod_extra_probes_list.value) {
1176 PERROR("strdup");
1177 ret = -ENOMEM;
1178 }
1179 }
1180 } else if (string_match(optname, "config") || opt == 'f') {
1181 /* This is handled in set_options() thus silent skip. */
1182 goto end;
1183 } else {
1184 /* Unknown option or other error.
1185 * Error is printed by getopt, just return */
1186 ret = -1;
1187 }
1188
1189 end:
1190 if (ret == -EINVAL) {
1191 const char *opt_name = "unknown";
1192 int i;
1193
1194 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
1195 i++) {
1196 if (opt == long_options[i].val) {
1197 opt_name = long_options[i].name;
1198 break;
1199 }
1200 }
1201
1202 WARN("Invalid argument provided for option \"%s\", using default value.",
1203 opt_name);
1204 }
1205
1206 return ret;
1207 }
1208
1209 /*
1210 * config_entry_handler_cb used to handle options read from a config file.
1211 * See config_entry_handler_cb comment in common/config/session-config.h for the
1212 * return value conventions.
1213 */
1214 static int config_entry_handler(const struct config_entry *entry, void *unused)
1215 {
1216 int ret = 0, i;
1217
1218 if (!entry || !entry->name || !entry->value) {
1219 ret = -EINVAL;
1220 goto end;
1221 }
1222
1223 /* Check if the option is to be ignored */
1224 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
1225 if (!strcmp(entry->name, config_ignore_options[i])) {
1226 goto end;
1227 }
1228 }
1229
1230 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
1231 i++) {
1232
1233 /* Ignore if not fully matched. */
1234 if (strcmp(entry->name, long_options[i].name)) {
1235 continue;
1236 }
1237
1238 /*
1239 * If the option takes no argument on the command line, we have to
1240 * check if the value is "true". We support non-zero numeric values,
1241 * true, on and yes.
1242 */
1243 if (!long_options[i].has_arg) {
1244 ret = config_parse_value(entry->value);
1245 if (ret <= 0) {
1246 if (ret) {
1247 WARN("Invalid configuration value \"%s\" for option %s",
1248 entry->value, entry->name);
1249 }
1250 /* False, skip boolean config option. */
1251 goto end;
1252 }
1253 }
1254
1255 ret = set_option(long_options[i].val, entry->value, entry->name);
1256 goto end;
1257 }
1258
1259 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
1260
1261 end:
1262 return ret;
1263 }
1264
1265 /*
1266 * daemon configuration loading and argument parsing
1267 */
1268 static int set_options(int argc, char **argv)
1269 {
1270 int ret = 0, c = 0, option_index = 0;
1271 int orig_optopt = optopt, orig_optind = optind;
1272 char *optstring;
1273 const char *config_path = NULL;
1274
1275 optstring = utils_generate_optstring(long_options,
1276 sizeof(long_options) / sizeof(struct option));
1277 if (!optstring) {
1278 ret = -ENOMEM;
1279 goto end;
1280 }
1281
1282 /* Check for the --config option */
1283 while ((c = getopt_long(argc, argv, optstring, long_options,
1284 &option_index)) != -1) {
1285 if (c == '?') {
1286 ret = -EINVAL;
1287 goto end;
1288 } else if (c != 'f') {
1289 /* if not equal to --config option. */
1290 continue;
1291 }
1292
1293 if (lttng_is_setuid_setgid()) {
1294 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1295 "-f, --config");
1296 } else {
1297 config_path = utils_expand_path(optarg);
1298 if (!config_path) {
1299 ERR("Failed to resolve path: %s", optarg);
1300 }
1301 }
1302 }
1303
1304 ret = config_get_section_entries(config_path, config_section_name,
1305 config_entry_handler, NULL);
1306 if (ret) {
1307 if (ret > 0) {
1308 ERR("Invalid configuration option at line %i", ret);
1309 ret = -1;
1310 }
1311 goto end;
1312 }
1313
1314 /* Reset getopt's global state */
1315 optopt = orig_optopt;
1316 optind = orig_optind;
1317 while (1) {
1318 option_index = -1;
1319 /*
1320 * getopt_long() will not set option_index if it encounters a
1321 * short option.
1322 */
1323 c = getopt_long(argc, argv, optstring, long_options,
1324 &option_index);
1325 if (c == -1) {
1326 break;
1327 }
1328
1329 /*
1330 * Pass NULL as the long option name if popt left the index
1331 * unset.
1332 */
1333 ret = set_option(c, optarg,
1334 option_index < 0 ? NULL :
1335 long_options[option_index].name);
1336 if (ret < 0) {
1337 break;
1338 }
1339 }
1340
1341 end:
1342 free(optstring);
1343 return ret;
1344 }
1345
1346 /*
1347 * Create lockfile using the rundir and return its fd.
1348 */
1349 static int create_lockfile(void)
1350 {
1351 return utils_create_lock_file(config.lock_file_path.value);
1352 }
1353
1354 /*
1355 * Check if the global socket is available, and if a daemon is answering at the
1356 * other side. If yes, error is returned.
1357 *
1358 * Also attempts to create and hold the lock file.
1359 */
1360 static int check_existing_daemon(void)
1361 {
1362 int ret = 0;
1363
1364 /* Is there anybody out there ? */
1365 if (lttng_session_daemon_alive()) {
1366 ret = -EEXIST;
1367 goto end;
1368 }
1369
1370 lockfile_fd = create_lockfile();
1371 if (lockfile_fd < 0) {
1372 ret = -EEXIST;
1373 goto end;
1374 }
1375 end:
1376 return ret;
1377 }
1378
1379 static void sessiond_cleanup_lock_file(void)
1380 {
1381 int ret;
1382
1383 /*
1384 * Cleanup lock file by deleting it and finaly closing it which will
1385 * release the file system lock.
1386 */
1387 if (lockfile_fd >= 0) {
1388 ret = remove(config.lock_file_path.value);
1389 if (ret < 0) {
1390 PERROR("remove lock file");
1391 }
1392 ret = close(lockfile_fd);
1393 if (ret < 0) {
1394 PERROR("close lock file");
1395 }
1396 }
1397 }
1398
1399 /*
1400 * Set the tracing group gid onto the client socket.
1401 *
1402 * Race window between mkdir and chown is OK because we are going from more
1403 * permissive (root.root) to less permissive (root.tracing).
1404 */
1405 static int set_permissions(char *rundir)
1406 {
1407 int ret;
1408 gid_t gid;
1409
1410 gid = utils_get_group_id(config.tracing_group_name.value);
1411
1412 /* Set lttng run dir */
1413 ret = chown(rundir, 0, gid);
1414 if (ret < 0) {
1415 ERR("Unable to set group on %s", rundir);
1416 PERROR("chown");
1417 }
1418
1419 /*
1420 * Ensure all applications and tracing group can search the run
1421 * dir. Allow everyone to read the directory, since it does not
1422 * buy us anything to hide its content.
1423 */
1424 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1425 if (ret < 0) {
1426 ERR("Unable to set permissions on %s", rundir);
1427 PERROR("chmod");
1428 }
1429
1430 /* lttng client socket path */
1431 ret = chown(config.client_unix_sock_path.value, 0, gid);
1432 if (ret < 0) {
1433 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
1434 PERROR("chown");
1435 }
1436
1437 /* kconsumer error socket path */
1438 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
1439 if (ret < 0) {
1440 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
1441 PERROR("chown");
1442 }
1443
1444 /* 64-bit ustconsumer error socket path */
1445 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
1446 if (ret < 0) {
1447 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
1448 PERROR("chown");
1449 }
1450
1451 /* 32-bit ustconsumer compat32 error socket path */
1452 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
1453 if (ret < 0) {
1454 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
1455 PERROR("chown");
1456 }
1457
1458 DBG("All permissions are set");
1459
1460 return ret;
1461 }
1462
1463 /*
1464 * Create the lttng run directory needed for all global sockets and pipe.
1465 */
1466 static int create_lttng_rundir(void)
1467 {
1468 int ret;
1469
1470 DBG3("Creating LTTng run directory: %s", config.rundir.value);
1471
1472 ret = mkdir(config.rundir.value, S_IRWXU);
1473 if (ret < 0) {
1474 if (errno != EEXIST) {
1475 ERR("Unable to create %s", config.rundir.value);
1476 goto error;
1477 } else {
1478 ret = 0;
1479 }
1480 }
1481
1482 error:
1483 return ret;
1484 }
1485
1486 /*
1487 * Setup sockets and directory needed by the consumerds' communication with the
1488 * session daemon.
1489 */
1490 static int set_consumer_sockets(struct consumer_data *consumer_data)
1491 {
1492 int ret;
1493 char *path = NULL;
1494
1495 switch (consumer_data->type) {
1496 case LTTNG_CONSUMER_KERNEL:
1497 path = config.kconsumerd_path.value;
1498 break;
1499 case LTTNG_CONSUMER64_UST:
1500 path = config.consumerd64_path.value;
1501 break;
1502 case LTTNG_CONSUMER32_UST:
1503 path = config.consumerd32_path.value;
1504 break;
1505 default:
1506 ERR("Consumer type unknown");
1507 ret = -EINVAL;
1508 goto error;
1509 }
1510 assert(path);
1511
1512 DBG2("Creating consumer directory: %s", path);
1513
1514 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
1515 if (ret < 0 && errno != EEXIST) {
1516 PERROR("mkdir");
1517 ERR("Failed to create %s", path);
1518 goto error;
1519 }
1520 if (is_root) {
1521 ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
1522 if (ret < 0) {
1523 ERR("Unable to set group on %s", path);
1524 PERROR("chown");
1525 goto error;
1526 }
1527 }
1528
1529 /* Create the consumerd error unix socket */
1530 consumer_data->err_sock =
1531 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
1532 if (consumer_data->err_sock < 0) {
1533 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
1534 ret = -1;
1535 goto error;
1536 }
1537
1538 /*
1539 * Set the CLOEXEC flag. Return code is useless because either way, the
1540 * show must go on.
1541 */
1542 ret = utils_set_fd_cloexec(consumer_data->err_sock);
1543 if (ret < 0) {
1544 PERROR("utils_set_fd_cloexec");
1545 /* continue anyway */
1546 }
1547
1548 /* File permission MUST be 660 */
1549 ret = chmod(consumer_data->err_unix_sock_path,
1550 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1551 if (ret < 0) {
1552 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
1553 PERROR("chmod");
1554 goto error;
1555 }
1556
1557 error:
1558 return ret;
1559 }
1560
1561 /*
1562 * Signal handler for the daemon
1563 *
1564 * Simply stop all worker threads, leaving main() return gracefully after
1565 * joining all threads and calling cleanup().
1566 */
1567 static void sighandler(int sig)
1568 {
1569 switch (sig) {
1570 case SIGINT:
1571 DBG("SIGINT caught");
1572 stop_threads();
1573 break;
1574 case SIGTERM:
1575 DBG("SIGTERM caught");
1576 stop_threads();
1577 break;
1578 case SIGUSR1:
1579 CMM_STORE_SHARED(recv_child_signal, 1);
1580 break;
1581 default:
1582 break;
1583 }
1584 }
1585
1586 /*
1587 * Setup signal handler for :
1588 * SIGINT, SIGTERM, SIGPIPE
1589 */
1590 static int set_signal_handler(void)
1591 {
1592 int ret = 0;
1593 struct sigaction sa;
1594 sigset_t sigset;
1595
1596 if ((ret = sigemptyset(&sigset)) < 0) {
1597 PERROR("sigemptyset");
1598 return ret;
1599 }
1600
1601 sa.sa_mask = sigset;
1602 sa.sa_flags = 0;
1603
1604 sa.sa_handler = sighandler;
1605 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1606 PERROR("sigaction");
1607 return ret;
1608 }
1609
1610 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1611 PERROR("sigaction");
1612 return ret;
1613 }
1614
1615 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
1616 PERROR("sigaction");
1617 return ret;
1618 }
1619
1620 sa.sa_handler = SIG_IGN;
1621 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
1622 PERROR("sigaction");
1623 return ret;
1624 }
1625
1626 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1627
1628 return ret;
1629 }
1630
1631 /*
1632 * Set open files limit to unlimited. This daemon can open a large number of
1633 * file descriptors in order to consume multiple kernel traces.
1634 */
1635 static void set_ulimit(void)
1636 {
1637 int ret;
1638 struct rlimit lim;
1639
1640 /* The kernel does not allow an infinite limit for open files */
1641 lim.rlim_cur = 65535;
1642 lim.rlim_max = 65535;
1643
1644 ret = setrlimit(RLIMIT_NOFILE, &lim);
1645 if (ret < 0) {
1646 PERROR("failed to set open files limit");
1647 }
1648 }
1649
1650 static int write_pidfile(void)
1651 {
1652 return utils_create_pid_file(getpid(), config.pid_file_path.value);
1653 }
1654
1655 static int set_clock_plugin_env(void)
1656 {
1657 int ret = 0;
1658 char *env_value = NULL;
1659
1660 if (!config.lttng_ust_clock_plugin.value) {
1661 goto end;
1662 }
1663
1664 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
1665 config.lttng_ust_clock_plugin.value);
1666 if (ret < 0) {
1667 PERROR("asprintf");
1668 goto end;
1669 }
1670
1671 ret = putenv(env_value);
1672 if (ret) {
1673 free(env_value);
1674 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1675 goto end;
1676 }
1677
1678 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1679 config.lttng_ust_clock_plugin.value);
1680 end:
1681 return ret;
1682 }
1683
1684 static void destroy_all_sessions_and_wait(void)
1685 {
1686 struct ltt_session *session, *tmp;
1687 struct ltt_session_list *session_list;
1688
1689 session_list = session_get_list();
1690 DBG("Initiating destruction of all sessions");
1691
1692 if (!session_list) {
1693 return;
1694 }
1695
1696 session_lock_list();
1697 /* Initiate the destruction of all sessions. */
1698 cds_list_for_each_entry_safe(session, tmp,
1699 &session_list->head, list) {
1700 if (!session_get(session)) {
1701 continue;
1702 }
1703
1704 session_lock(session);
1705 if (session->destroyed) {
1706 goto unlock_session;
1707 }
1708 (void) cmd_stop_trace(session);
1709 (void) cmd_destroy_session(session,
1710 notification_thread_handle);
1711 unlock_session:
1712 session_unlock(session);
1713 session_put(session);
1714 }
1715 session_unlock_list();
1716
1717 /* Wait for the destruction of all sessions to complete. */
1718 DBG("Waiting for the destruction of all sessions to complete");
1719 session_list_wait_empty();
1720 DBG("Destruction of all sessions completed");
1721 }
1722
1723 /*
1724 * main
1725 */
1726 int main(int argc, char **argv)
1727 {
1728 int ret = 0, retval = 0;
1729 const char *env_app_timeout;
1730 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
1731 *ust64_channel_monitor_pipe = NULL,
1732 *kernel_channel_monitor_pipe = NULL;
1733 struct lttng_thread *ht_cleanup_thread = NULL;
1734 struct timer_thread_parameters timer_thread_parameters;
1735 /* Rotation thread handle. */
1736 struct rotation_thread_handle *rotation_thread_handle = NULL;
1737 /* Queue of rotation jobs populated by the sessiond-timer. */
1738 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
1739 struct lttng_thread *client_thread = NULL;
1740
1741 init_kernel_workarounds();
1742
1743 rcu_register_thread();
1744
1745 if (set_signal_handler()) {
1746 retval = -1;
1747 goto exit_set_signal_handler;
1748 }
1749
1750 if (timer_signal_init()) {
1751 retval = -1;
1752 goto exit_set_signal_handler;
1753 }
1754
1755 page_size = sysconf(_SC_PAGESIZE);
1756 if (page_size < 0) {
1757 PERROR("sysconf _SC_PAGESIZE");
1758 page_size = LONG_MAX;
1759 WARN("Fallback page size to %ld", page_size);
1760 }
1761
1762 ret = sessiond_config_init(&config);
1763 if (ret) {
1764 retval = -1;
1765 goto exit_set_signal_handler;
1766 }
1767
1768 /*
1769 * Init config from environment variables.
1770 * Command line option override env configuration per-doc. Do env first.
1771 */
1772 sessiond_config_apply_env_config(&config);
1773
1774 /*
1775 * Parse arguments and load the daemon configuration file.
1776 *
1777 * We have an exit_options exit path to free memory reserved by
1778 * set_options. This is needed because the rest of sessiond_cleanup()
1779 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1780 * depends on set_options.
1781 */
1782 progname = argv[0];
1783 if (set_options(argc, argv)) {
1784 retval = -1;
1785 goto exit_options;
1786 }
1787
1788 /*
1789 * Resolve all paths received as arguments, configuration option, or
1790 * through environment variable as absolute paths. This is necessary
1791 * since daemonizing causes the sessiond's current working directory
1792 * to '/'.
1793 */
1794 ret = sessiond_config_resolve_paths(&config);
1795 if (ret) {
1796 goto exit_options;
1797 }
1798
1799 /* Apply config. */
1800 lttng_opt_verbose = config.verbose;
1801 lttng_opt_quiet = config.quiet;
1802 kconsumer_data.err_unix_sock_path =
1803 config.kconsumerd_err_unix_sock_path.value;
1804 kconsumer_data.cmd_unix_sock_path =
1805 config.kconsumerd_cmd_unix_sock_path.value;
1806 ustconsumer32_data.err_unix_sock_path =
1807 config.consumerd32_err_unix_sock_path.value;
1808 ustconsumer32_data.cmd_unix_sock_path =
1809 config.consumerd32_cmd_unix_sock_path.value;
1810 ustconsumer64_data.err_unix_sock_path =
1811 config.consumerd64_err_unix_sock_path.value;
1812 ustconsumer64_data.cmd_unix_sock_path =
1813 config.consumerd64_cmd_unix_sock_path.value;
1814 set_clock_plugin_env();
1815
1816 sessiond_config_log(&config);
1817
1818 if (create_lttng_rundir()) {
1819 retval = -1;
1820 goto exit_options;
1821 }
1822
1823 /* Abort launch if a session daemon is already running. */
1824 if (check_existing_daemon()) {
1825 ERR("A session daemon is already running.");
1826 retval = -1;
1827 goto exit_options;
1828 }
1829
1830 /* Daemonize */
1831 if (config.daemonize || config.background) {
1832 int i;
1833
1834 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
1835 !config.background);
1836 if (ret < 0) {
1837 retval = -1;
1838 goto exit_options;
1839 }
1840
1841 /*
1842 * We are in the child. Make sure all other file descriptors are
1843 * closed, in case we are called with more opened file
1844 * descriptors than the standard ones and the lock file.
1845 */
1846 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1847 if (i == lockfile_fd) {
1848 continue;
1849 }
1850 (void) close(i);
1851 }
1852 }
1853
1854 if (run_as_create_worker(argv[0]) < 0) {
1855 goto exit_create_run_as_worker_cleanup;
1856 }
1857
1858 /*
1859 * Starting from here, we can create threads. This needs to be after
1860 * lttng_daemonize due to RCU.
1861 */
1862
1863 /*
1864 * Initialize the health check subsystem. This call should set the
1865 * appropriate time values.
1866 */
1867 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
1868 if (!health_sessiond) {
1869 PERROR("health_app_create error");
1870 retval = -1;
1871 goto exit_health_sessiond_cleanup;
1872 }
1873
1874 /* Create thread to clean up RCU hash tables */
1875 ht_cleanup_thread = launch_ht_cleanup_thread();
1876 if (!ht_cleanup_thread) {
1877 retval = -1;
1878 goto exit_ht_cleanup;
1879 }
1880
1881 /* Create thread quit pipe */
1882 if (sessiond_init_thread_quit_pipe()) {
1883 retval = -1;
1884 goto exit_init_data;
1885 }
1886
1887 /* Check if daemon is UID = 0 */
1888 is_root = !getuid();
1889 if (is_root) {
1890 /* Create global run dir with root access */
1891
1892 kernel_channel_monitor_pipe = lttng_pipe_open(0);
1893 if (!kernel_channel_monitor_pipe) {
1894 ERR("Failed to create kernel consumer channel monitor pipe");
1895 retval = -1;
1896 goto exit_init_data;
1897 }
1898 kconsumer_data.channel_monitor_pipe =
1899 lttng_pipe_release_writefd(
1900 kernel_channel_monitor_pipe);
1901 if (kconsumer_data.channel_monitor_pipe < 0) {
1902 retval = -1;
1903 goto exit_init_data;
1904 }
1905 }
1906
1907 /* Set consumer initial state */
1908 kernel_consumerd_state = CONSUMER_STOPPED;
1909 ust_consumerd_state = CONSUMER_STOPPED;
1910
1911 ust32_channel_monitor_pipe = lttng_pipe_open(0);
1912 if (!ust32_channel_monitor_pipe) {
1913 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1914 retval = -1;
1915 goto exit_init_data;
1916 }
1917 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1918 ust32_channel_monitor_pipe);
1919 if (ustconsumer32_data.channel_monitor_pipe < 0) {
1920 retval = -1;
1921 goto exit_init_data;
1922 }
1923
1924 /*
1925 * The rotation_thread_timer_queue structure is shared between the
1926 * sessiond timer thread and the rotation thread. The main thread keeps
1927 * its ownership and destroys it when both threads have been joined.
1928 */
1929 rotation_timer_queue = rotation_thread_timer_queue_create();
1930 if (!rotation_timer_queue) {
1931 retval = -1;
1932 goto exit_init_data;
1933 }
1934 timer_thread_parameters.rotation_thread_job_queue =
1935 rotation_timer_queue;
1936
1937 ust64_channel_monitor_pipe = lttng_pipe_open(0);
1938 if (!ust64_channel_monitor_pipe) {
1939 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1940 retval = -1;
1941 goto exit_init_data;
1942 }
1943 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
1944 ust64_channel_monitor_pipe);
1945 if (ustconsumer64_data.channel_monitor_pipe < 0) {
1946 retval = -1;
1947 goto exit_init_data;
1948 }
1949
1950 /*
1951 * Init UST app hash table. Alloc hash table before this point since
1952 * cleanup() can get called after that point.
1953 */
1954 if (ust_app_ht_alloc()) {
1955 ERR("Failed to allocate UST app hash table");
1956 retval = -1;
1957 goto exit_init_data;
1958 }
1959
1960 /*
1961 * Initialize agent app hash table. We allocate the hash table here
1962 * since cleanup() can get called after this point.
1963 */
1964 if (agent_app_ht_alloc()) {
1965 ERR("Failed to allocate Agent app hash table");
1966 retval = -1;
1967 goto exit_init_data;
1968 }
1969
1970 /*
1971 * These actions must be executed as root. We do that *after* setting up
1972 * the sockets path because we MUST make the check for another daemon using
1973 * those paths *before* trying to set the kernel consumer sockets and init
1974 * kernel tracer.
1975 */
1976 if (is_root) {
1977 if (set_consumer_sockets(&kconsumer_data)) {
1978 retval = -1;
1979 goto exit_init_data;
1980 }
1981
1982 /* Setup kernel tracer */
1983 if (!config.no_kernel) {
1984 init_kernel_tracer();
1985 if (kernel_tracer_fd >= 0) {
1986 ret = syscall_init_table();
1987 if (ret < 0) {
1988 ERR("Unable to populate syscall table. "
1989 "Syscall tracing won't work "
1990 "for this session daemon.");
1991 }
1992 }
1993 }
1994
1995 /* Set ulimit for open files */
1996 set_ulimit();
1997 }
1998 /* init lttng_fd tracking must be done after set_ulimit. */
1999 lttng_fd_init();
2000
2001 if (set_consumer_sockets(&ustconsumer64_data)) {
2002 retval = -1;
2003 goto exit_init_data;
2004 }
2005
2006 if (set_consumer_sockets(&ustconsumer32_data)) {
2007 retval = -1;
2008 goto exit_init_data;
2009 }
2010
2011 /* Set credentials to socket */
2012 if (is_root && set_permissions(config.rundir.value)) {
2013 retval = -1;
2014 goto exit_init_data;
2015 }
2016
2017 /* Get parent pid if -S, --sig-parent is specified. */
2018 if (config.sig_parent) {
2019 ppid = getppid();
2020 }
2021
2022 /* Setup the kernel pipe for waking up the kernel thread */
2023 if (is_root && !config.no_kernel) {
2024 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
2025 retval = -1;
2026 goto exit_init_data;
2027 }
2028 }
2029
2030 /* Setup the thread apps communication pipe. */
2031 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
2032 retval = -1;
2033 goto exit_init_data;
2034 }
2035
2036 /* Setup the thread apps notify communication pipe. */
2037 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
2038 retval = -1;
2039 goto exit_init_data;
2040 }
2041
2042 /* Initialize global buffer per UID and PID registry. */
2043 buffer_reg_init_uid_registry();
2044 buffer_reg_init_pid_registry();
2045
2046 /* Init UST command queue. */
2047 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
2048
2049 cmd_init();
2050
2051 /* Check for the application socket timeout env variable. */
2052 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
2053 if (env_app_timeout) {
2054 config.app_socket_timeout = atoi(env_app_timeout);
2055 } else {
2056 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
2057 }
2058
2059 ret = write_pidfile();
2060 if (ret) {
2061 ERR("Error in write_pidfile");
2062 retval = -1;
2063 goto exit_init_data;
2064 }
2065
2066 /* Initialize communication library */
2067 lttcomm_init();
2068 /* Initialize TCP timeout values */
2069 lttcomm_inet_init();
2070
2071 /* Create health-check thread. */
2072 if (!launch_health_management_thread()) {
2073 retval = -1;
2074 goto exit_health;
2075 }
2076
2077 /* notification_thread_data acquires the pipes' read side. */
2078 notification_thread_handle = notification_thread_handle_create(
2079 ust32_channel_monitor_pipe,
2080 ust64_channel_monitor_pipe,
2081 kernel_channel_monitor_pipe);
2082 if (!notification_thread_handle) {
2083 retval = -1;
2084 ERR("Failed to create notification thread shared data");
2085 goto exit_notification;
2086 }
2087
2088 /* Create notification thread. */
2089 if (!launch_notification_thread(notification_thread_handle)) {
2090 retval = -1;
2091 goto exit_notification;
2092 }
2093
2094 /* Create timer thread. */
2095 if (!launch_timer_thread(&timer_thread_parameters)) {
2096 retval = -1;
2097 goto exit_notification;
2098 }
2099
2100 /* rotation_thread_data acquires the pipes' read side. */
2101 rotation_thread_handle = rotation_thread_handle_create(
2102 rotation_timer_queue,
2103 notification_thread_handle);
2104 if (!rotation_thread_handle) {
2105 retval = -1;
2106 ERR("Failed to create rotation thread shared data");
2107 stop_threads();
2108 goto exit_rotation;
2109 }
2110
2111 /* Create rotation thread. */
2112 if (!launch_rotation_thread(rotation_thread_handle)) {
2113 retval = -1;
2114 goto exit_rotation;
2115 }
2116
2117 /* Create thread to manage the client socket */
2118 client_thread = launch_client_thread();
2119 if (!client_thread) {
2120 retval = -1;
2121 goto exit_client;
2122 }
2123
2124 if (!launch_ust_dispatch_thread(&ust_cmd_queue, apps_cmd_pipe[1],
2125 apps_cmd_notify_pipe[1])) {
2126 retval = -1;
2127 goto exit_dispatch;
2128 }
2129
2130 /* Create thread to manage application registration. */
2131 if (!launch_application_registration_thread(&ust_cmd_queue)) {
2132 retval = -1;
2133 goto exit_reg_apps;
2134 }
2135
2136 /* Create thread to manage application socket */
2137 if (!launch_application_management_thread(apps_cmd_pipe[0])) {
2138 retval = -1;
2139 goto exit_apps;
2140 }
2141
2142 /* Create thread to manage application notify socket */
2143 if (!launch_application_notification_thread(apps_cmd_notify_pipe[0])) {
2144 retval = -1;
2145 goto exit_apps_notify;
2146 }
2147
2148 /* Create agent management thread. */
2149 if (!launch_agent_management_thread()) {
2150 retval = -1;
2151 goto exit_agent_reg;
2152 }
2153
2154 /* Don't start this thread if kernel tracing is not requested nor root */
2155 if (is_root && !config.no_kernel) {
2156 /* Create kernel thread to manage kernel event */
2157 if (!launch_kernel_management_thread(kernel_poll_pipe[0])) {
2158 retval = -1;
2159 goto exit_kernel;
2160 }
2161 }
2162
2163 /* Load sessions. */
2164 ret = config_load_session(config.load_session_path.value,
2165 NULL, 1, 1, NULL);
2166 if (ret) {
2167 ERR("Session load failed: %s", error_get_str(ret));
2168 retval = -1;
2169 goto exit_load_session;
2170 }
2171
2172 /* Initialization completed. */
2173 sessiond_signal_parents();
2174
2175 /*
2176 * This is where we start awaiting program completion (e.g. through
2177 * signal that asks threads to teardown).
2178 */
2179
2180 /* Initiate teardown once activity occurs on the quit pipe. */
2181 sessiond_wait_for_quit_pipe(-1U);
2182
2183 /*
2184 * Ensure that the client thread is no longer accepting new commands,
2185 * which could cause new sessions to be created.
2186 */
2187 if (!lttng_thread_shutdown(client_thread)) {
2188 ERR("Failed to shutdown the client thread, continuing teardown");
2189 lttng_thread_put(client_thread);
2190 client_thread = NULL;
2191 }
2192
2193 destroy_all_sessions_and_wait();
2194 exit_load_session:
2195 exit_kernel:
2196 exit_agent_reg:
2197 exit_apps_notify:
2198 exit_apps:
2199 exit_reg_apps:
2200 exit_dispatch:
2201 exit_client:
2202 exit_rotation:
2203 exit_notification:
2204 lttng_thread_list_shutdown_orphans();
2205 exit_health:
2206 exit_init_data:
2207 if (client_thread) {
2208 lttng_thread_put(client_thread);
2209 }
2210
2211 /*
2212 * Wait for all pending call_rcu work to complete before tearing
2213 * down data structures. call_rcu worker may be trying to
2214 * perform lookups in those structures.
2215 */
2216 rcu_barrier();
2217 /*
2218 * sessiond_cleanup() is called when no other thread is running, except
2219 * the ht_cleanup thread, which is needed to destroy the hash tables.
2220 */
2221 rcu_thread_online();
2222 sessiond_cleanup();
2223
2224 /*
2225 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2226 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2227 * the queue is empty before shutting down the clean-up thread.
2228 */
2229 rcu_barrier();
2230
2231 if (ht_cleanup_thread) {
2232 lttng_thread_shutdown(ht_cleanup_thread);
2233 lttng_thread_put(ht_cleanup_thread);
2234 }
2235
2236 rcu_thread_offline();
2237 rcu_unregister_thread();
2238
2239 if (rotation_thread_handle) {
2240 rotation_thread_handle_destroy(rotation_thread_handle);
2241 }
2242
2243 /*
2244 * After the rotation and timer thread have quit, we can safely destroy
2245 * the rotation_timer_queue.
2246 */
2247 rotation_thread_timer_queue_destroy(rotation_timer_queue);
2248 /*
2249 * The teardown of the notification system is performed after the
2250 * session daemon's teardown in order to allow it to be notified
2251 * of the active session and channels at the moment of the teardown.
2252 */
2253 if (notification_thread_handle) {
2254 notification_thread_handle_destroy(notification_thread_handle);
2255 }
2256 lttng_pipe_destroy(ust32_channel_monitor_pipe);
2257 lttng_pipe_destroy(ust64_channel_monitor_pipe);
2258 lttng_pipe_destroy(kernel_channel_monitor_pipe);
2259 exit_ht_cleanup:
2260
2261 health_app_destroy(health_sessiond);
2262 exit_health_sessiond_cleanup:
2263 exit_create_run_as_worker_cleanup:
2264
2265 exit_options:
2266 sessiond_cleanup_lock_file();
2267 sessiond_cleanup_options();
2268
2269 exit_set_signal_handler:
2270 if (!retval) {
2271 exit(EXIT_SUCCESS);
2272 } else {
2273 exit(EXIT_FAILURE);
2274 }
2275 }
This page took 0.133191 seconds and 4 git commands to generate.