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