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