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