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