Launch the rotation 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/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
55
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
58 #include "channel.h"
59 #include "cmd.h"
60 #include "consumer.h"
61 #include "context.h"
62 #include "event.h"
63 #include "kernel.h"
64 #include "kernel-consumer.h"
65 #include "modprobe.h"
66 #include "shm.h"
67 #include "ust-ctl.h"
68 #include "ust-consumer.h"
69 #include "utils.h"
70 #include "fd-limit.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
75 #include "save.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
81 #include "agent.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
84 #include "timer.h"
85 #include "thread.h"
86
87 static const char *help_msg =
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-sessiond.8.h>
90 #else
91 NULL
92 #endif
93 ;
94
95 const char *progname;
96 static int lockfile_fd = -1;
97
98 /* Set to 1 when a SIGUSR1 signal is received. */
99 static int recv_child_signal;
100
101 static struct lttng_kernel_tracer_version kernel_tracer_version;
102 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version;
103
104 /*
105 * Consumer daemon specific control data. Every value not initialized here is
106 * set to 0 by the static definition.
107 */
108 static struct consumer_data kconsumer_data = {
109 .type = LTTNG_CONSUMER_KERNEL,
110 .err_sock = -1,
111 .cmd_sock = -1,
112 .channel_monitor_pipe = -1,
113 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
114 .lock = PTHREAD_MUTEX_INITIALIZER,
115 .cond = PTHREAD_COND_INITIALIZER,
116 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
117 };
118 static struct consumer_data ustconsumer64_data = {
119 .type = LTTNG_CONSUMER64_UST,
120 .err_sock = -1,
121 .cmd_sock = -1,
122 .channel_monitor_pipe = -1,
123 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
124 .lock = PTHREAD_MUTEX_INITIALIZER,
125 .cond = PTHREAD_COND_INITIALIZER,
126 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
127 };
128 static struct consumer_data ustconsumer32_data = {
129 .type = LTTNG_CONSUMER32_UST,
130 .err_sock = -1,
131 .cmd_sock = -1,
132 .channel_monitor_pipe = -1,
133 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
134 .lock = PTHREAD_MUTEX_INITIALIZER,
135 .cond = PTHREAD_COND_INITIALIZER,
136 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
137 };
138
139 /* Command line options */
140 static const struct option long_options[] = {
141 { "client-sock", required_argument, 0, 'c' },
142 { "apps-sock", required_argument, 0, 'a' },
143 { "kconsumerd-cmd-sock", required_argument, 0, '\0' },
144 { "kconsumerd-err-sock", required_argument, 0, '\0' },
145 { "ustconsumerd32-cmd-sock", required_argument, 0, '\0' },
146 { "ustconsumerd32-err-sock", required_argument, 0, '\0' },
147 { "ustconsumerd64-cmd-sock", required_argument, 0, '\0' },
148 { "ustconsumerd64-err-sock", required_argument, 0, '\0' },
149 { "consumerd32-path", required_argument, 0, '\0' },
150 { "consumerd32-libdir", required_argument, 0, '\0' },
151 { "consumerd64-path", required_argument, 0, '\0' },
152 { "consumerd64-libdir", required_argument, 0, '\0' },
153 { "daemonize", no_argument, 0, 'd' },
154 { "background", no_argument, 0, 'b' },
155 { "sig-parent", no_argument, 0, 'S' },
156 { "help", no_argument, 0, 'h' },
157 { "group", required_argument, 0, 'g' },
158 { "version", no_argument, 0, 'V' },
159 { "quiet", no_argument, 0, 'q' },
160 { "verbose", no_argument, 0, 'v' },
161 { "verbose-consumer", no_argument, 0, '\0' },
162 { "no-kernel", no_argument, 0, '\0' },
163 { "pidfile", required_argument, 0, 'p' },
164 { "agent-tcp-port", required_argument, 0, '\0' },
165 { "config", required_argument, 0, 'f' },
166 { "load", required_argument, 0, 'l' },
167 { "kmod-probes", required_argument, 0, '\0' },
168 { "extra-kmod-probes", required_argument, 0, '\0' },
169 { NULL, 0, 0, 0 }
170 };
171
172 /* Command line options to ignore from configuration file */
173 static const char *config_ignore_options[] = { "help", "version", "config" };
174
175 /* Shared between threads */
176 static int dispatch_thread_exit;
177
178 /* Sockets and FDs */
179 static int client_sock = -1;
180 static int apps_sock = -1;
181
182 /*
183 * This pipe is used to inform the thread managing application communication
184 * that a command is queued and ready to be processed.
185 */
186 static int apps_cmd_pipe[2] = { -1, -1 };
187
188 /* Pthread, Mutexes and Semaphores */
189 static pthread_t apps_thread;
190 static pthread_t apps_notify_thread;
191 static pthread_t reg_apps_thread;
192 static pthread_t client_thread;
193 static pthread_t kernel_thread;
194 static pthread_t dispatch_thread;
195 static pthread_t agent_reg_thread;
196 static pthread_t load_session_thread;
197 static pthread_t timer_thread;
198
199 /*
200 * UST registration command queue. This queue is tied with a futex and uses a N
201 * wakers / 1 waiter implemented and detailed in futex.c/.h
202 *
203 * The thread_registration_apps and thread_dispatch_ust_registration uses this
204 * queue along with the wait/wake scheme. The thread_manage_apps receives down
205 * the line new application socket and monitors it for any I/O error or clean
206 * close that triggers an unregistration of the application.
207 */
208 static struct ust_cmd_queue ust_cmd_queue;
209
210 static const char *module_proc_lttng = "/proc/lttng";
211
212 /*
213 * Consumer daemon state which is changed when spawning it, killing it or in
214 * case of a fatal error.
215 */
216 enum consumerd_state {
217 CONSUMER_STARTED = 1,
218 CONSUMER_STOPPED = 2,
219 CONSUMER_ERROR = 3,
220 };
221
222 /*
223 * This consumer daemon state is used to validate if a client command will be
224 * able to reach the consumer. If not, the client is informed. For instance,
225 * doing a "lttng start" when the consumer state is set to ERROR will return an
226 * error to the client.
227 *
228 * The following example shows a possible race condition of this scheme:
229 *
230 * consumer thread error happens
231 * client cmd arrives
232 * client cmd checks state -> still OK
233 * consumer thread exit, sets error
234 * client cmd try to talk to consumer
235 * ...
236 *
237 * However, since the consumer is a different daemon, we have no way of making
238 * sure the command will reach it safely even with this state flag. This is why
239 * we consider that up to the state validation during command processing, the
240 * command is safe. After that, we can not guarantee the correctness of the
241 * client request vis-a-vis the consumer.
242 */
243 static enum consumerd_state ust_consumerd_state;
244 static enum consumerd_state kernel_consumerd_state;
245
246 /* Load session thread information to operate. */
247 static struct load_session_thread_data *load_info;
248
249 /*
250 * Section name to look for in the daemon configuration file.
251 */
252 static const char * const config_section_name = "sessiond";
253
254 /* Am I root or not. Set to 1 if the daemon is running as root */
255 static int is_root;
256
257 /*
258 * Stop all threads by closing the thread quit pipe.
259 */
260 static void stop_threads(void)
261 {
262 int ret;
263
264 /* Stopping all threads */
265 DBG("Terminating all threads");
266 ret = sessiond_notify_quit_pipe();
267 if (ret < 0) {
268 ERR("write error on thread quit pipe");
269 }
270
271 /* Dispatch thread */
272 CMM_STORE_SHARED(dispatch_thread_exit, 1);
273 futex_nto1_wake(&ust_cmd_queue.futex);
274 }
275
276 /*
277 * Close every consumer sockets.
278 */
279 static void close_consumer_sockets(void)
280 {
281 int ret;
282
283 if (kconsumer_data.err_sock >= 0) {
284 ret = close(kconsumer_data.err_sock);
285 if (ret < 0) {
286 PERROR("kernel consumer err_sock close");
287 }
288 }
289 if (ustconsumer32_data.err_sock >= 0) {
290 ret = close(ustconsumer32_data.err_sock);
291 if (ret < 0) {
292 PERROR("UST consumerd32 err_sock close");
293 }
294 }
295 if (ustconsumer64_data.err_sock >= 0) {
296 ret = close(ustconsumer64_data.err_sock);
297 if (ret < 0) {
298 PERROR("UST consumerd64 err_sock close");
299 }
300 }
301 if (kconsumer_data.cmd_sock >= 0) {
302 ret = close(kconsumer_data.cmd_sock);
303 if (ret < 0) {
304 PERROR("kernel consumer cmd_sock close");
305 }
306 }
307 if (ustconsumer32_data.cmd_sock >= 0) {
308 ret = close(ustconsumer32_data.cmd_sock);
309 if (ret < 0) {
310 PERROR("UST consumerd32 cmd_sock close");
311 }
312 }
313 if (ustconsumer64_data.cmd_sock >= 0) {
314 ret = close(ustconsumer64_data.cmd_sock);
315 if (ret < 0) {
316 PERROR("UST consumerd64 cmd_sock close");
317 }
318 }
319 if (kconsumer_data.channel_monitor_pipe >= 0) {
320 ret = close(kconsumer_data.channel_monitor_pipe);
321 if (ret < 0) {
322 PERROR("kernel consumer channel monitor pipe close");
323 }
324 }
325 if (ustconsumer32_data.channel_monitor_pipe >= 0) {
326 ret = close(ustconsumer32_data.channel_monitor_pipe);
327 if (ret < 0) {
328 PERROR("UST consumerd32 channel monitor pipe close");
329 }
330 }
331 if (ustconsumer64_data.channel_monitor_pipe >= 0) {
332 ret = close(ustconsumer64_data.channel_monitor_pipe);
333 if (ret < 0) {
334 PERROR("UST consumerd64 channel monitor pipe close");
335 }
336 }
337 }
338
339 /*
340 * Wait on consumer process termination.
341 *
342 * Need to be called with the consumer data lock held or from a context
343 * ensuring no concurrent access to data (e.g: cleanup).
344 */
345 static void wait_consumer(struct consumer_data *consumer_data)
346 {
347 pid_t ret;
348 int status;
349
350 if (consumer_data->pid <= 0) {
351 return;
352 }
353
354 DBG("Waiting for complete teardown of consumerd (PID: %d)",
355 consumer_data->pid);
356 ret = waitpid(consumer_data->pid, &status, 0);
357 if (ret == -1) {
358 PERROR("consumerd waitpid pid: %d", consumer_data->pid)
359 } else if (!WIFEXITED(status)) {
360 ERR("consumerd termination with error: %d",
361 WEXITSTATUS(ret));
362 }
363 consumer_data->pid = 0;
364 }
365
366 /*
367 * Cleanup the session daemon's data structures.
368 */
369 static void sessiond_cleanup(void)
370 {
371 int ret;
372 struct ltt_session_list *session_list = session_get_list();
373
374 DBG("Cleanup sessiond");
375
376 /*
377 * Close the thread quit pipe. It has already done its job,
378 * since we are now called.
379 */
380 sessiond_close_quit_pipe();
381
382 ret = remove(config.pid_file_path.value);
383 if (ret < 0) {
384 PERROR("remove pidfile %s", config.pid_file_path.value);
385 }
386
387 DBG("Removing sessiond and consumerd content of directory %s",
388 config.rundir.value);
389
390 /* sessiond */
391 DBG("Removing %s", config.pid_file_path.value);
392 (void) unlink(config.pid_file_path.value);
393
394 DBG("Removing %s", config.agent_port_file_path.value);
395 (void) unlink(config.agent_port_file_path.value);
396
397 /* kconsumerd */
398 DBG("Removing %s", kconsumer_data.err_unix_sock_path);
399 (void) unlink(kconsumer_data.err_unix_sock_path);
400
401 DBG("Removing directory %s", config.kconsumerd_path.value);
402 (void) rmdir(config.kconsumerd_path.value);
403
404 /* ust consumerd 32 */
405 DBG("Removing %s", config.consumerd32_err_unix_sock_path.value);
406 (void) unlink(config.consumerd32_err_unix_sock_path.value);
407
408 DBG("Removing directory %s", config.consumerd32_path.value);
409 (void) rmdir(config.consumerd32_path.value);
410
411 /* ust consumerd 64 */
412 DBG("Removing %s", config.consumerd64_err_unix_sock_path.value);
413 (void) unlink(config.consumerd64_err_unix_sock_path.value);
414
415 DBG("Removing directory %s", config.consumerd64_path.value);
416 (void) rmdir(config.consumerd64_path.value);
417
418 pthread_mutex_destroy(&session_list->lock);
419
420 wait_consumer(&kconsumer_data);
421 wait_consumer(&ustconsumer64_data);
422 wait_consumer(&ustconsumer32_data);
423
424 DBG("Cleaning up all agent apps");
425 agent_app_ht_clean();
426
427 DBG("Closing all UST sockets");
428 ust_app_clean_list();
429 buffer_reg_destroy_registries();
430
431 if (is_root && !config.no_kernel) {
432 DBG2("Closing kernel fd");
433 if (kernel_tracer_fd >= 0) {
434 ret = close(kernel_tracer_fd);
435 if (ret) {
436 PERROR("close");
437 }
438 }
439 DBG("Unloading kernel modules");
440 modprobe_remove_lttng_all();
441 free(syscall_table);
442 }
443
444 close_consumer_sockets();
445
446 if (load_info) {
447 load_session_destroy_data(load_info);
448 free(load_info);
449 }
450
451 /*
452 * We do NOT rmdir rundir because there are other processes
453 * using it, for instance lttng-relayd, which can start in
454 * parallel with this teardown.
455 */
456 }
457
458 /*
459 * Cleanup the daemon's option data structures.
460 */
461 static void sessiond_cleanup_options(void)
462 {
463 DBG("Cleaning up options");
464
465 sessiond_config_fini(&config);
466
467 run_as_destroy_worker();
468 }
469
470 /*
471 * Send data on a unix socket using the liblttsessiondcomm API.
472 *
473 * Return lttcomm error code.
474 */
475 static int send_unix_sock(int sock, void *buf, size_t len)
476 {
477 /* Check valid length */
478 if (len == 0) {
479 return -1;
480 }
481
482 return lttcomm_send_unix_sock(sock, buf, len);
483 }
484
485 /*
486 * Free memory of a command context structure.
487 */
488 static void clean_command_ctx(struct command_ctx **cmd_ctx)
489 {
490 DBG("Clean command context structure");
491 if (*cmd_ctx) {
492 if ((*cmd_ctx)->llm) {
493 free((*cmd_ctx)->llm);
494 }
495 if ((*cmd_ctx)->lsm) {
496 free((*cmd_ctx)->lsm);
497 }
498 free(*cmd_ctx);
499 *cmd_ctx = NULL;
500 }
501 }
502
503 /*
504 * Notify UST applications using the shm mmap futex.
505 */
506 static int notify_ust_apps(int active)
507 {
508 char *wait_shm_mmap;
509
510 DBG("Notifying applications of session daemon state: %d", active);
511
512 /* See shm.c for this call implying mmap, shm and futex calls */
513 wait_shm_mmap = shm_ust_get_mmap(config.wait_shm_path.value, is_root);
514 if (wait_shm_mmap == NULL) {
515 goto error;
516 }
517
518 /* Wake waiting process */
519 futex_wait_update((int32_t *) wait_shm_mmap, active);
520
521 /* Apps notified successfully */
522 return 0;
523
524 error:
525 return -1;
526 }
527
528 /*
529 * Setup the outgoing data buffer for the response (llm) by allocating the
530 * right amount of memory and copying the original information from the lsm
531 * structure.
532 *
533 * Return 0 on success, negative value on error.
534 */
535 static int setup_lttng_msg(struct command_ctx *cmd_ctx,
536 const void *payload_buf, size_t payload_len,
537 const void *cmd_header_buf, size_t cmd_header_len)
538 {
539 int ret = 0;
540 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
541 const size_t cmd_header_offset = header_len;
542 const size_t payload_offset = cmd_header_offset + cmd_header_len;
543 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
544
545 cmd_ctx->llm = zmalloc(total_msg_size);
546
547 if (cmd_ctx->llm == NULL) {
548 PERROR("zmalloc");
549 ret = -ENOMEM;
550 goto end;
551 }
552
553 /* Copy common data */
554 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
555 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
556 cmd_ctx->llm->cmd_header_size = cmd_header_len;
557 cmd_ctx->llm->data_size = payload_len;
558 cmd_ctx->lttng_msg_size = total_msg_size;
559
560 /* Copy command header */
561 if (cmd_header_len) {
562 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
563 cmd_header_len);
564 }
565
566 /* Copy payload */
567 if (payload_len) {
568 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
569 payload_len);
570 }
571
572 end:
573 return ret;
574 }
575
576 /*
577 * Version of setup_lttng_msg() without command header.
578 */
579 static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
580 void *payload_buf, size_t payload_len)
581 {
582 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
583 }
584 /*
585 * Update the kernel poll set of all channel fd available over all tracing
586 * session. Add the wakeup pipe at the end of the set.
587 */
588 static int update_kernel_poll(struct lttng_poll_event *events)
589 {
590 int ret;
591 struct ltt_kernel_channel *channel;
592 struct ltt_session *session;
593 const struct ltt_session_list *session_list = session_get_list();
594
595 DBG("Updating kernel poll set");
596
597 session_lock_list();
598 cds_list_for_each_entry(session, &session_list->head, list) {
599 if (!session_get(session)) {
600 continue;
601 }
602 session_lock(session);
603 if (session->kernel_session == NULL) {
604 session_unlock(session);
605 session_put(session);
606 continue;
607 }
608
609 cds_list_for_each_entry(channel,
610 &session->kernel_session->channel_list.head, list) {
611 /* Add channel fd to the kernel poll set */
612 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
613 if (ret < 0) {
614 session_unlock(session);
615 session_put(session);
616 goto error;
617 }
618 DBG("Channel fd %d added to kernel set", channel->fd);
619 }
620 session_unlock(session);
621 }
622 session_unlock_list();
623
624 return 0;
625
626 error:
627 session_unlock_list();
628 return -1;
629 }
630
631 /*
632 * Find the channel fd from 'fd' over all tracing session. When found, check
633 * for new channel stream and send those stream fds to the kernel consumer.
634 *
635 * Useful for CPU hotplug feature.
636 */
637 static int update_kernel_stream(int fd)
638 {
639 int ret = 0;
640 struct ltt_session *session;
641 struct ltt_kernel_session *ksess;
642 struct ltt_kernel_channel *channel;
643 const struct ltt_session_list *session_list = session_get_list();
644
645 DBG("Updating kernel streams for channel fd %d", fd);
646
647 session_lock_list();
648 cds_list_for_each_entry(session, &session_list->head, list) {
649 if (!session_get(session)) {
650 continue;
651 }
652 session_lock(session);
653 if (session->kernel_session == NULL) {
654 session_unlock(session);
655 session_put(session);
656 continue;
657 }
658 ksess = session->kernel_session;
659
660 cds_list_for_each_entry(channel,
661 &ksess->channel_list.head, list) {
662 struct lttng_ht_iter iter;
663 struct consumer_socket *socket;
664
665 if (channel->fd != fd) {
666 continue;
667 }
668 DBG("Channel found, updating kernel streams");
669 ret = kernel_open_channel_stream(channel);
670 if (ret < 0) {
671 goto error;
672 }
673 /* Update the stream global counter */
674 ksess->stream_count_global += ret;
675
676 /*
677 * Have we already sent fds to the consumer? If yes, it
678 * means that tracing is started so it is safe to send
679 * our updated stream fds.
680 */
681 if (ksess->consumer_fds_sent != 1
682 || ksess->consumer == NULL) {
683 ret = -1;
684 goto error;
685 }
686
687 rcu_read_lock();
688 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
689 &iter.iter, socket, node.node) {
690 pthread_mutex_lock(socket->lock);
691 ret = kernel_consumer_send_channel_streams(socket,
692 channel, ksess,
693 session->output_traces ? 1 : 0);
694 pthread_mutex_unlock(socket->lock);
695 if (ret < 0) {
696 rcu_read_unlock();
697 goto error;
698 }
699 }
700 rcu_read_unlock();
701 }
702 session_unlock(session);
703 session_put(session);
704 }
705 session_unlock_list();
706 return ret;
707
708 error:
709 session_unlock(session);
710 session_put(session);
711 session_unlock_list();
712 return ret;
713 }
714
715 /*
716 * For each tracing session, update newly registered apps. The session list
717 * lock MUST be acquired before calling this.
718 */
719 static void update_ust_app(int app_sock)
720 {
721 struct ltt_session *sess, *stmp;
722 const struct ltt_session_list *session_list = session_get_list();
723
724 /* Consumer is in an ERROR state. Stop any application update. */
725 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
726 /* Stop the update process since the consumer is dead. */
727 return;
728 }
729
730 /* For all tracing session(s) */
731 cds_list_for_each_entry_safe(sess, stmp, &session_list->head, list) {
732 struct ust_app *app;
733
734 if (!session_get(sess)) {
735 continue;
736 }
737 session_lock(sess);
738 if (!sess->ust_session) {
739 goto unlock_session;
740 }
741
742 rcu_read_lock();
743 assert(app_sock >= 0);
744 app = ust_app_find_by_sock(app_sock);
745 if (app == NULL) {
746 /*
747 * Application can be unregistered before so
748 * this is possible hence simply stopping the
749 * update.
750 */
751 DBG3("UST app update failed to find app sock %d",
752 app_sock);
753 goto unlock_rcu;
754 }
755 ust_app_global_update(sess->ust_session, app);
756 unlock_rcu:
757 rcu_read_unlock();
758 unlock_session:
759 session_unlock(sess);
760 session_put(sess);
761 }
762 }
763
764 /*
765 * This thread manage event coming from the kernel.
766 *
767 * Features supported in this thread:
768 * -) CPU Hotplug
769 */
770 static void *thread_manage_kernel(void *data)
771 {
772 int ret, i, pollfd, update_poll_flag = 1, err = -1;
773 uint32_t revents, nb_fd;
774 char tmp;
775 struct lttng_poll_event events;
776
777 DBG("[thread] Thread manage kernel started");
778
779 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
780
781 /*
782 * This first step of the while is to clean this structure which could free
783 * non NULL pointers so initialize it before the loop.
784 */
785 lttng_poll_init(&events);
786
787 if (testpoint(sessiond_thread_manage_kernel)) {
788 goto error_testpoint;
789 }
790
791 health_code_update();
792
793 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
794 goto error_testpoint;
795 }
796
797 while (1) {
798 health_code_update();
799
800 if (update_poll_flag == 1) {
801 /* Clean events object. We are about to populate it again. */
802 lttng_poll_clean(&events);
803
804 ret = sessiond_set_thread_pollset(&events, 2);
805 if (ret < 0) {
806 goto error_poll_create;
807 }
808
809 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
810 if (ret < 0) {
811 goto error;
812 }
813
814 /* This will add the available kernel channel if any. */
815 ret = update_kernel_poll(&events);
816 if (ret < 0) {
817 goto error;
818 }
819 update_poll_flag = 0;
820 }
821
822 DBG("Thread kernel polling");
823
824 /* Poll infinite value of time */
825 restart:
826 health_poll_entry();
827 ret = lttng_poll_wait(&events, -1);
828 DBG("Thread kernel return from poll on %d fds",
829 LTTNG_POLL_GETNB(&events));
830 health_poll_exit();
831 if (ret < 0) {
832 /*
833 * Restart interrupted system call.
834 */
835 if (errno == EINTR) {
836 goto restart;
837 }
838 goto error;
839 } else if (ret == 0) {
840 /* Should not happen since timeout is infinite */
841 ERR("Return value of poll is 0 with an infinite timeout.\n"
842 "This should not have happened! Continuing...");
843 continue;
844 }
845
846 nb_fd = ret;
847
848 for (i = 0; i < nb_fd; i++) {
849 /* Fetch once the poll data */
850 revents = LTTNG_POLL_GETEV(&events, i);
851 pollfd = LTTNG_POLL_GETFD(&events, i);
852
853 health_code_update();
854
855 if (!revents) {
856 /* No activity for this FD (poll implementation). */
857 continue;
858 }
859
860 /* Thread quit pipe has been closed. Killing thread. */
861 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
862 if (ret) {
863 err = 0;
864 goto exit;
865 }
866
867 /* Check for data on kernel pipe */
868 if (revents & LPOLLIN) {
869 if (pollfd == kernel_poll_pipe[0]) {
870 (void) lttng_read(kernel_poll_pipe[0],
871 &tmp, 1);
872 /*
873 * Ret value is useless here, if this pipe gets any actions an
874 * update is required anyway.
875 */
876 update_poll_flag = 1;
877 continue;
878 } else {
879 /*
880 * New CPU detected by the kernel. Adding kernel stream to
881 * kernel session and updating the kernel consumer
882 */
883 ret = update_kernel_stream(pollfd);
884 if (ret < 0) {
885 continue;
886 }
887 break;
888 }
889 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
890 update_poll_flag = 1;
891 continue;
892 } else {
893 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
894 goto error;
895 }
896 }
897 }
898
899 exit:
900 error:
901 lttng_poll_clean(&events);
902 error_poll_create:
903 error_testpoint:
904 utils_close_pipe(kernel_poll_pipe);
905 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
906 if (err) {
907 health_error();
908 ERR("Health error occurred in %s", __func__);
909 WARN("Kernel thread died unexpectedly. "
910 "Kernel tracing can continue but CPU hotplug is disabled.");
911 }
912 health_unregister(health_sessiond);
913 DBG("Kernel thread dying");
914 return NULL;
915 }
916
917 /*
918 * Signal pthread condition of the consumer data that the thread.
919 */
920 static void signal_consumer_condition(struct consumer_data *data, int state)
921 {
922 pthread_mutex_lock(&data->cond_mutex);
923
924 /*
925 * The state is set before signaling. It can be any value, it's the waiter
926 * job to correctly interpret this condition variable associated to the
927 * consumer pthread_cond.
928 *
929 * A value of 0 means that the corresponding thread of the consumer data
930 * was not started. 1 indicates that the thread has started and is ready
931 * for action. A negative value means that there was an error during the
932 * thread bootstrap.
933 */
934 data->consumer_thread_is_ready = state;
935 (void) pthread_cond_signal(&data->cond);
936
937 pthread_mutex_unlock(&data->cond_mutex);
938 }
939
940 /*
941 * This thread manage the consumer error sent back to the session daemon.
942 */
943 static void *thread_manage_consumer(void *data)
944 {
945 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
946 uint32_t revents, nb_fd;
947 enum lttcomm_return_code code;
948 struct lttng_poll_event events;
949 struct consumer_data *consumer_data = data;
950 struct consumer_socket *cmd_socket_wrapper = NULL;
951
952 DBG("[thread] Manage consumer started");
953
954 rcu_register_thread();
955 rcu_thread_online();
956
957 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
958
959 health_code_update();
960
961 /*
962 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
963 * metadata_sock. Nothing more will be added to this poll set.
964 */
965 ret = sessiond_set_thread_pollset(&events, 3);
966 if (ret < 0) {
967 goto error_poll;
968 }
969
970 /*
971 * The error socket here is already in a listening state which was done
972 * just before spawning this thread to avoid a race between the consumer
973 * daemon exec trying to connect and the listen() call.
974 */
975 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
976 if (ret < 0) {
977 goto error;
978 }
979
980 health_code_update();
981
982 /* Infinite blocking call, waiting for transmission */
983 restart:
984 health_poll_entry();
985
986 if (testpoint(sessiond_thread_manage_consumer)) {
987 goto error;
988 }
989
990 ret = lttng_poll_wait(&events, -1);
991 health_poll_exit();
992 if (ret < 0) {
993 /*
994 * Restart interrupted system call.
995 */
996 if (errno == EINTR) {
997 goto restart;
998 }
999 goto error;
1000 }
1001
1002 nb_fd = ret;
1003
1004 for (i = 0; i < nb_fd; i++) {
1005 /* Fetch once the poll data */
1006 revents = LTTNG_POLL_GETEV(&events, i);
1007 pollfd = LTTNG_POLL_GETFD(&events, i);
1008
1009 health_code_update();
1010
1011 if (!revents) {
1012 /* No activity for this FD (poll implementation). */
1013 continue;
1014 }
1015
1016 /* Thread quit pipe has been closed. Killing thread. */
1017 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1018 if (ret) {
1019 err = 0;
1020 goto exit;
1021 }
1022
1023 /* Event on the registration socket */
1024 if (pollfd == consumer_data->err_sock) {
1025 if (revents & LPOLLIN) {
1026 continue;
1027 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1028 ERR("consumer err socket poll error");
1029 goto error;
1030 } else {
1031 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1032 goto error;
1033 }
1034 }
1035 }
1036
1037 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1038 if (sock < 0) {
1039 goto error;
1040 }
1041
1042 /*
1043 * Set the CLOEXEC flag. Return code is useless because either way, the
1044 * show must go on.
1045 */
1046 (void) utils_set_fd_cloexec(sock);
1047
1048 health_code_update();
1049
1050 DBG2("Receiving code from consumer err_sock");
1051
1052 /* Getting status code from kconsumerd */
1053 ret = lttcomm_recv_unix_sock(sock, &code,
1054 sizeof(enum lttcomm_return_code));
1055 if (ret <= 0) {
1056 goto error;
1057 }
1058
1059 health_code_update();
1060 if (code != LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1061 ERR("consumer error when waiting for SOCK_READY : %s",
1062 lttcomm_get_readable_code(-code));
1063 goto error;
1064 }
1065
1066 /* Connect both command and metadata sockets. */
1067 consumer_data->cmd_sock =
1068 lttcomm_connect_unix_sock(
1069 consumer_data->cmd_unix_sock_path);
1070 consumer_data->metadata_fd =
1071 lttcomm_connect_unix_sock(
1072 consumer_data->cmd_unix_sock_path);
1073 if (consumer_data->cmd_sock < 0 || consumer_data->metadata_fd < 0) {
1074 PERROR("consumer connect cmd socket");
1075 /* On error, signal condition and quit. */
1076 signal_consumer_condition(consumer_data, -1);
1077 goto error;
1078 }
1079
1080 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1081
1082 /* Create metadata socket lock. */
1083 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1084 if (consumer_data->metadata_sock.lock == NULL) {
1085 PERROR("zmalloc pthread mutex");
1086 goto error;
1087 }
1088 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1089
1090 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1091 DBG("Consumer metadata socket ready (fd: %d)",
1092 consumer_data->metadata_fd);
1093
1094 /*
1095 * Remove the consumerd error sock since we've established a connection.
1096 */
1097 ret = lttng_poll_del(&events, consumer_data->err_sock);
1098 if (ret < 0) {
1099 goto error;
1100 }
1101
1102 /* Add new accepted error socket. */
1103 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1104 if (ret < 0) {
1105 goto error;
1106 }
1107
1108 /* Add metadata socket that is successfully connected. */
1109 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1110 LPOLLIN | LPOLLRDHUP);
1111 if (ret < 0) {
1112 goto error;
1113 }
1114
1115 health_code_update();
1116
1117 /*
1118 * Transfer the write-end of the channel monitoring and rotate pipe
1119 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1120 */
1121 cmd_socket_wrapper = consumer_allocate_socket(&consumer_data->cmd_sock);
1122 if (!cmd_socket_wrapper) {
1123 goto error;
1124 }
1125 cmd_socket_wrapper->lock = &consumer_data->lock;
1126
1127 ret = consumer_send_channel_monitor_pipe(cmd_socket_wrapper,
1128 consumer_data->channel_monitor_pipe);
1129 if (ret) {
1130 goto error;
1131 }
1132
1133 /* Discard the socket wrapper as it is no longer needed. */
1134 consumer_destroy_socket(cmd_socket_wrapper);
1135 cmd_socket_wrapper = NULL;
1136
1137 /* The thread is completely initialized, signal that it is ready. */
1138 signal_consumer_condition(consumer_data, 1);
1139
1140 /* Infinite blocking call, waiting for transmission */
1141 restart_poll:
1142 while (1) {
1143 health_code_update();
1144
1145 /* Exit the thread because the thread quit pipe has been triggered. */
1146 if (should_quit) {
1147 /* Not a health error. */
1148 err = 0;
1149 goto exit;
1150 }
1151
1152 health_poll_entry();
1153 ret = lttng_poll_wait(&events, -1);
1154 health_poll_exit();
1155 if (ret < 0) {
1156 /*
1157 * Restart interrupted system call.
1158 */
1159 if (errno == EINTR) {
1160 goto restart_poll;
1161 }
1162 goto error;
1163 }
1164
1165 nb_fd = ret;
1166
1167 for (i = 0; i < nb_fd; i++) {
1168 /* Fetch once the poll data */
1169 revents = LTTNG_POLL_GETEV(&events, i);
1170 pollfd = LTTNG_POLL_GETFD(&events, i);
1171
1172 health_code_update();
1173
1174 if (!revents) {
1175 /* No activity for this FD (poll implementation). */
1176 continue;
1177 }
1178
1179 /*
1180 * Thread quit pipe has been triggered, flag that we should stop
1181 * but continue the current loop to handle potential data from
1182 * consumer.
1183 */
1184 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
1185
1186 if (pollfd == sock) {
1187 /* Event on the consumerd socket */
1188 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1189 && !(revents & LPOLLIN)) {
1190 ERR("consumer err socket second poll error");
1191 goto error;
1192 }
1193 health_code_update();
1194 /* Wait for any kconsumerd error */
1195 ret = lttcomm_recv_unix_sock(sock, &code,
1196 sizeof(enum lttcomm_return_code));
1197 if (ret <= 0) {
1198 ERR("consumer closed the command socket");
1199 goto error;
1200 }
1201
1202 ERR("consumer return code : %s",
1203 lttcomm_get_readable_code(-code));
1204
1205 goto exit;
1206 } else if (pollfd == consumer_data->metadata_fd) {
1207 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1208 && !(revents & LPOLLIN)) {
1209 ERR("consumer err metadata socket second poll error");
1210 goto error;
1211 }
1212 /* UST metadata requests */
1213 ret = ust_consumer_metadata_request(
1214 &consumer_data->metadata_sock);
1215 if (ret < 0) {
1216 ERR("Handling metadata request");
1217 goto error;
1218 }
1219 }
1220 /* No need for an else branch all FDs are tested prior. */
1221 }
1222 health_code_update();
1223 }
1224
1225 exit:
1226 error:
1227 /*
1228 * We lock here because we are about to close the sockets and some other
1229 * thread might be using them so get exclusive access which will abort all
1230 * other consumer command by other threads.
1231 */
1232 pthread_mutex_lock(&consumer_data->lock);
1233
1234 /* Immediately set the consumerd state to stopped */
1235 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1236 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1237 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1238 consumer_data->type == LTTNG_CONSUMER32_UST) {
1239 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1240 } else {
1241 /* Code flow error... */
1242 assert(0);
1243 }
1244
1245 if (consumer_data->err_sock >= 0) {
1246 ret = close(consumer_data->err_sock);
1247 if (ret) {
1248 PERROR("close");
1249 }
1250 consumer_data->err_sock = -1;
1251 }
1252 if (consumer_data->cmd_sock >= 0) {
1253 ret = close(consumer_data->cmd_sock);
1254 if (ret) {
1255 PERROR("close");
1256 }
1257 consumer_data->cmd_sock = -1;
1258 }
1259 if (consumer_data->metadata_sock.fd_ptr &&
1260 *consumer_data->metadata_sock.fd_ptr >= 0) {
1261 ret = close(*consumer_data->metadata_sock.fd_ptr);
1262 if (ret) {
1263 PERROR("close");
1264 }
1265 }
1266 if (sock >= 0) {
1267 ret = close(sock);
1268 if (ret) {
1269 PERROR("close");
1270 }
1271 }
1272
1273 unlink(consumer_data->err_unix_sock_path);
1274 unlink(consumer_data->cmd_unix_sock_path);
1275 pthread_mutex_unlock(&consumer_data->lock);
1276
1277 /* Cleanup metadata socket mutex. */
1278 if (consumer_data->metadata_sock.lock) {
1279 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1280 free(consumer_data->metadata_sock.lock);
1281 }
1282 lttng_poll_clean(&events);
1283
1284 if (cmd_socket_wrapper) {
1285 consumer_destroy_socket(cmd_socket_wrapper);
1286 }
1287 error_poll:
1288 if (err) {
1289 health_error();
1290 ERR("Health error occurred in %s", __func__);
1291 }
1292 health_unregister(health_sessiond);
1293 DBG("consumer thread cleanup completed");
1294
1295 rcu_thread_offline();
1296 rcu_unregister_thread();
1297
1298 return NULL;
1299 }
1300
1301 /*
1302 * This thread receives application command sockets (FDs) on the
1303 * apps_cmd_pipe and waits (polls) on them until they are closed
1304 * or an error occurs.
1305 *
1306 * At that point, it flushes the data (tracing and metadata) associated
1307 * with this application and tears down ust app sessions and other
1308 * associated data structures through ust_app_unregister().
1309 *
1310 * Note that this thread never sends commands to the applications
1311 * through the command sockets; it merely listens for hang-ups
1312 * and errors on those sockets and cleans-up as they occur.
1313 */
1314 static void *thread_manage_apps(void *data)
1315 {
1316 int i, ret, pollfd, err = -1;
1317 ssize_t size_ret;
1318 uint32_t revents, nb_fd;
1319 struct lttng_poll_event events;
1320
1321 DBG("[thread] Manage application started");
1322
1323 rcu_register_thread();
1324 rcu_thread_online();
1325
1326 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1327
1328 if (testpoint(sessiond_thread_manage_apps)) {
1329 goto error_testpoint;
1330 }
1331
1332 health_code_update();
1333
1334 ret = sessiond_set_thread_pollset(&events, 2);
1335 if (ret < 0) {
1336 goto error_poll_create;
1337 }
1338
1339 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1340 if (ret < 0) {
1341 goto error;
1342 }
1343
1344 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1345 goto error;
1346 }
1347
1348 health_code_update();
1349
1350 while (1) {
1351 DBG("Apps thread polling");
1352
1353 /* Inifinite blocking call, waiting for transmission */
1354 restart:
1355 health_poll_entry();
1356 ret = lttng_poll_wait(&events, -1);
1357 DBG("Apps thread return from poll on %d fds",
1358 LTTNG_POLL_GETNB(&events));
1359 health_poll_exit();
1360 if (ret < 0) {
1361 /*
1362 * Restart interrupted system call.
1363 */
1364 if (errno == EINTR) {
1365 goto restart;
1366 }
1367 goto error;
1368 }
1369
1370 nb_fd = ret;
1371
1372 for (i = 0; i < nb_fd; i++) {
1373 /* Fetch once the poll data */
1374 revents = LTTNG_POLL_GETEV(&events, i);
1375 pollfd = LTTNG_POLL_GETFD(&events, i);
1376
1377 health_code_update();
1378
1379 if (!revents) {
1380 /* No activity for this FD (poll implementation). */
1381 continue;
1382 }
1383
1384 /* Thread quit pipe has been closed. Killing thread. */
1385 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1386 if (ret) {
1387 err = 0;
1388 goto exit;
1389 }
1390
1391 /* Inspect the apps cmd pipe */
1392 if (pollfd == apps_cmd_pipe[0]) {
1393 if (revents & LPOLLIN) {
1394 int sock;
1395
1396 /* Empty pipe */
1397 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1398 if (size_ret < sizeof(sock)) {
1399 PERROR("read apps cmd pipe");
1400 goto error;
1401 }
1402
1403 health_code_update();
1404
1405 /*
1406 * Since this is a command socket (write then read),
1407 * we only monitor the error events of the socket.
1408 */
1409 ret = lttng_poll_add(&events, sock,
1410 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1411 if (ret < 0) {
1412 goto error;
1413 }
1414
1415 DBG("Apps with sock %d added to poll set", sock);
1416 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1417 ERR("Apps command pipe error");
1418 goto error;
1419 } else {
1420 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1421 goto error;
1422 }
1423 } else {
1424 /*
1425 * At this point, we know that a registered application made
1426 * the event at poll_wait.
1427 */
1428 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1429 /* Removing from the poll set */
1430 ret = lttng_poll_del(&events, pollfd);
1431 if (ret < 0) {
1432 goto error;
1433 }
1434
1435 /* Socket closed on remote end. */
1436 ust_app_unregister(pollfd);
1437 } else {
1438 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1439 goto error;
1440 }
1441 }
1442
1443 health_code_update();
1444 }
1445 }
1446
1447 exit:
1448 error:
1449 lttng_poll_clean(&events);
1450 error_poll_create:
1451 error_testpoint:
1452 utils_close_pipe(apps_cmd_pipe);
1453 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1454
1455 /*
1456 * We don't clean the UST app hash table here since already registered
1457 * applications can still be controlled so let them be until the session
1458 * daemon dies or the applications stop.
1459 */
1460
1461 if (err) {
1462 health_error();
1463 ERR("Health error occurred in %s", __func__);
1464 }
1465 health_unregister(health_sessiond);
1466 DBG("Application communication apps thread cleanup complete");
1467 rcu_thread_offline();
1468 rcu_unregister_thread();
1469 return NULL;
1470 }
1471
1472 /*
1473 * Send a socket to a thread This is called from the dispatch UST registration
1474 * thread once all sockets are set for the application.
1475 *
1476 * The sock value can be invalid, we don't really care, the thread will handle
1477 * it and make the necessary cleanup if so.
1478 *
1479 * On success, return 0 else a negative value being the errno message of the
1480 * write().
1481 */
1482 static int send_socket_to_thread(int fd, int sock)
1483 {
1484 ssize_t ret;
1485
1486 /*
1487 * It's possible that the FD is set as invalid with -1 concurrently just
1488 * before calling this function being a shutdown state of the thread.
1489 */
1490 if (fd < 0) {
1491 ret = -EBADF;
1492 goto error;
1493 }
1494
1495 ret = lttng_write(fd, &sock, sizeof(sock));
1496 if (ret < sizeof(sock)) {
1497 PERROR("write apps pipe %d", fd);
1498 if (ret < 0) {
1499 ret = -errno;
1500 }
1501 goto error;
1502 }
1503
1504 /* All good. Don't send back the write positive ret value. */
1505 ret = 0;
1506 error:
1507 return (int) ret;
1508 }
1509
1510 /*
1511 * Sanitize the wait queue of the dispatch registration thread meaning removing
1512 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1513 * notify socket is never received.
1514 */
1515 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1516 {
1517 int ret, nb_fd = 0, i;
1518 unsigned int fd_added = 0;
1519 struct lttng_poll_event events;
1520 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1521
1522 assert(wait_queue);
1523
1524 lttng_poll_init(&events);
1525
1526 /* Just skip everything for an empty queue. */
1527 if (!wait_queue->count) {
1528 goto end;
1529 }
1530
1531 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1532 if (ret < 0) {
1533 goto error_create;
1534 }
1535
1536 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1537 &wait_queue->head, head) {
1538 assert(wait_node->app);
1539 ret = lttng_poll_add(&events, wait_node->app->sock,
1540 LPOLLHUP | LPOLLERR);
1541 if (ret < 0) {
1542 goto error;
1543 }
1544
1545 fd_added = 1;
1546 }
1547
1548 if (!fd_added) {
1549 goto end;
1550 }
1551
1552 /*
1553 * Poll but don't block so we can quickly identify the faulty events and
1554 * clean them afterwards from the wait queue.
1555 */
1556 ret = lttng_poll_wait(&events, 0);
1557 if (ret < 0) {
1558 goto error;
1559 }
1560 nb_fd = ret;
1561
1562 for (i = 0; i < nb_fd; i++) {
1563 /* Get faulty FD. */
1564 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1565 int pollfd = LTTNG_POLL_GETFD(&events, i);
1566
1567 if (!revents) {
1568 /* No activity for this FD (poll implementation). */
1569 continue;
1570 }
1571
1572 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1573 &wait_queue->head, head) {
1574 if (pollfd == wait_node->app->sock &&
1575 (revents & (LPOLLHUP | LPOLLERR))) {
1576 cds_list_del(&wait_node->head);
1577 wait_queue->count--;
1578 ust_app_destroy(wait_node->app);
1579 free(wait_node);
1580 /*
1581 * Silence warning of use-after-free in
1582 * cds_list_for_each_entry_safe which uses
1583 * __typeof__(*wait_node).
1584 */
1585 wait_node = NULL;
1586 break;
1587 } else {
1588 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1589 goto error;
1590 }
1591 }
1592 }
1593
1594 if (nb_fd > 0) {
1595 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1596 }
1597
1598 end:
1599 lttng_poll_clean(&events);
1600 return;
1601
1602 error:
1603 lttng_poll_clean(&events);
1604 error_create:
1605 ERR("Unable to sanitize wait queue");
1606 return;
1607 }
1608
1609 /*
1610 * Dispatch request from the registration threads to the application
1611 * communication thread.
1612 */
1613 static void *thread_dispatch_ust_registration(void *data)
1614 {
1615 int ret, err = -1;
1616 struct cds_wfcq_node *node;
1617 struct ust_command *ust_cmd = NULL;
1618 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1619 struct ust_reg_wait_queue wait_queue = {
1620 .count = 0,
1621 };
1622
1623 rcu_register_thread();
1624
1625 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1626
1627 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1628 goto error_testpoint;
1629 }
1630
1631 health_code_update();
1632
1633 CDS_INIT_LIST_HEAD(&wait_queue.head);
1634
1635 DBG("[thread] Dispatch UST command started");
1636
1637 for (;;) {
1638 health_code_update();
1639
1640 /* Atomically prepare the queue futex */
1641 futex_nto1_prepare(&ust_cmd_queue.futex);
1642
1643 if (CMM_LOAD_SHARED(dispatch_thread_exit)) {
1644 break;
1645 }
1646
1647 do {
1648 struct ust_app *app = NULL;
1649 ust_cmd = NULL;
1650
1651 /*
1652 * Make sure we don't have node(s) that have hung up before receiving
1653 * the notify socket. This is to clean the list in order to avoid
1654 * memory leaks from notify socket that are never seen.
1655 */
1656 sanitize_wait_queue(&wait_queue);
1657
1658 health_code_update();
1659 /* Dequeue command for registration */
1660 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1661 if (node == NULL) {
1662 DBG("Woken up but nothing in the UST command queue");
1663 /* Continue thread execution */
1664 break;
1665 }
1666
1667 ust_cmd = caa_container_of(node, struct ust_command, node);
1668
1669 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1670 " gid:%d sock:%d name:%s (version %d.%d)",
1671 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1672 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1673 ust_cmd->sock, ust_cmd->reg_msg.name,
1674 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1675
1676 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1677 wait_node = zmalloc(sizeof(*wait_node));
1678 if (!wait_node) {
1679 PERROR("zmalloc wait_node dispatch");
1680 ret = close(ust_cmd->sock);
1681 if (ret < 0) {
1682 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1683 }
1684 lttng_fd_put(LTTNG_FD_APPS, 1);
1685 free(ust_cmd);
1686 goto error;
1687 }
1688 CDS_INIT_LIST_HEAD(&wait_node->head);
1689
1690 /* Create application object if socket is CMD. */
1691 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1692 ust_cmd->sock);
1693 if (!wait_node->app) {
1694 ret = close(ust_cmd->sock);
1695 if (ret < 0) {
1696 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1697 }
1698 lttng_fd_put(LTTNG_FD_APPS, 1);
1699 free(wait_node);
1700 free(ust_cmd);
1701 continue;
1702 }
1703 /*
1704 * Add application to the wait queue so we can set the notify
1705 * socket before putting this object in the global ht.
1706 */
1707 cds_list_add(&wait_node->head, &wait_queue.head);
1708 wait_queue.count++;
1709
1710 free(ust_cmd);
1711 /*
1712 * We have to continue here since we don't have the notify
1713 * socket and the application MUST be added to the hash table
1714 * only at that moment.
1715 */
1716 continue;
1717 } else {
1718 /*
1719 * Look for the application in the local wait queue and set the
1720 * notify socket if found.
1721 */
1722 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1723 &wait_queue.head, head) {
1724 health_code_update();
1725 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1726 wait_node->app->notify_sock = ust_cmd->sock;
1727 cds_list_del(&wait_node->head);
1728 wait_queue.count--;
1729 app = wait_node->app;
1730 free(wait_node);
1731 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1732 break;
1733 }
1734 }
1735
1736 /*
1737 * With no application at this stage the received socket is
1738 * basically useless so close it before we free the cmd data
1739 * structure for good.
1740 */
1741 if (!app) {
1742 ret = close(ust_cmd->sock);
1743 if (ret < 0) {
1744 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1745 }
1746 lttng_fd_put(LTTNG_FD_APPS, 1);
1747 }
1748 free(ust_cmd);
1749 }
1750
1751 if (app) {
1752 /*
1753 * @session_lock_list
1754 *
1755 * Lock the global session list so from the register up to the
1756 * registration done message, no thread can see the application
1757 * and change its state.
1758 */
1759 session_lock_list();
1760 rcu_read_lock();
1761
1762 /*
1763 * Add application to the global hash table. This needs to be
1764 * done before the update to the UST registry can locate the
1765 * application.
1766 */
1767 ust_app_add(app);
1768
1769 /* Set app version. This call will print an error if needed. */
1770 (void) ust_app_version(app);
1771
1772 /* Send notify socket through the notify pipe. */
1773 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1774 app->notify_sock);
1775 if (ret < 0) {
1776 rcu_read_unlock();
1777 session_unlock_list();
1778 /*
1779 * No notify thread, stop the UST tracing. However, this is
1780 * not an internal error of the this thread thus setting
1781 * the health error code to a normal exit.
1782 */
1783 err = 0;
1784 goto error;
1785 }
1786
1787 /*
1788 * Update newly registered application with the tracing
1789 * registry info already enabled information.
1790 */
1791 update_ust_app(app->sock);
1792
1793 /*
1794 * Don't care about return value. Let the manage apps threads
1795 * handle app unregistration upon socket close.
1796 */
1797 (void) ust_app_register_done(app);
1798
1799 /*
1800 * Even if the application socket has been closed, send the app
1801 * to the thread and unregistration will take place at that
1802 * place.
1803 */
1804 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
1805 if (ret < 0) {
1806 rcu_read_unlock();
1807 session_unlock_list();
1808 /*
1809 * No apps. thread, stop the UST tracing. However, this is
1810 * not an internal error of the this thread thus setting
1811 * the health error code to a normal exit.
1812 */
1813 err = 0;
1814 goto error;
1815 }
1816
1817 rcu_read_unlock();
1818 session_unlock_list();
1819 }
1820 } while (node != NULL);
1821
1822 health_poll_entry();
1823 /* Futex wait on queue. Blocking call on futex() */
1824 futex_nto1_wait(&ust_cmd_queue.futex);
1825 health_poll_exit();
1826 }
1827 /* Normal exit, no error */
1828 err = 0;
1829
1830 error:
1831 /* Clean up wait queue. */
1832 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1833 &wait_queue.head, head) {
1834 cds_list_del(&wait_node->head);
1835 wait_queue.count--;
1836 free(wait_node);
1837 }
1838
1839 /* Empty command queue. */
1840 for (;;) {
1841 /* Dequeue command for registration */
1842 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
1843 if (node == NULL) {
1844 break;
1845 }
1846 ust_cmd = caa_container_of(node, struct ust_command, node);
1847 ret = close(ust_cmd->sock);
1848 if (ret < 0) {
1849 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
1850 }
1851 lttng_fd_put(LTTNG_FD_APPS, 1);
1852 free(ust_cmd);
1853 }
1854
1855 error_testpoint:
1856 DBG("Dispatch thread dying");
1857 if (err) {
1858 health_error();
1859 ERR("Health error occurred in %s", __func__);
1860 }
1861 health_unregister(health_sessiond);
1862 rcu_unregister_thread();
1863 return NULL;
1864 }
1865
1866 /*
1867 * This thread manage application registration.
1868 */
1869 static void *thread_registration_apps(void *data)
1870 {
1871 int sock = -1, i, ret, pollfd, err = -1;
1872 uint32_t revents, nb_fd;
1873 struct lttng_poll_event events;
1874 /*
1875 * Get allocated in this thread, enqueued to a global queue, dequeued and
1876 * freed in the manage apps thread.
1877 */
1878 struct ust_command *ust_cmd = NULL;
1879
1880 DBG("[thread] Manage application registration started");
1881
1882 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
1883
1884 if (testpoint(sessiond_thread_registration_apps)) {
1885 goto error_testpoint;
1886 }
1887
1888 ret = lttcomm_listen_unix_sock(apps_sock);
1889 if (ret < 0) {
1890 goto error_listen;
1891 }
1892
1893 /*
1894 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1895 * more will be added to this poll set.
1896 */
1897 ret = sessiond_set_thread_pollset(&events, 2);
1898 if (ret < 0) {
1899 goto error_create_poll;
1900 }
1901
1902 /* Add the application registration socket */
1903 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1904 if (ret < 0) {
1905 goto error_poll_add;
1906 }
1907
1908 /* Notify all applications to register */
1909 ret = notify_ust_apps(1);
1910 if (ret < 0) {
1911 ERR("Failed to notify applications or create the wait shared memory.\n"
1912 "Execution continues but there might be problem for already\n"
1913 "running applications that wishes to register.");
1914 }
1915
1916 while (1) {
1917 DBG("Accepting application registration");
1918
1919 /* Inifinite blocking call, waiting for transmission */
1920 restart:
1921 health_poll_entry();
1922 ret = lttng_poll_wait(&events, -1);
1923 health_poll_exit();
1924 if (ret < 0) {
1925 /*
1926 * Restart interrupted system call.
1927 */
1928 if (errno == EINTR) {
1929 goto restart;
1930 }
1931 goto error;
1932 }
1933
1934 nb_fd = ret;
1935
1936 for (i = 0; i < nb_fd; i++) {
1937 health_code_update();
1938
1939 /* Fetch once the poll data */
1940 revents = LTTNG_POLL_GETEV(&events, i);
1941 pollfd = LTTNG_POLL_GETFD(&events, i);
1942
1943 if (!revents) {
1944 /* No activity for this FD (poll implementation). */
1945 continue;
1946 }
1947
1948 /* Thread quit pipe has been closed. Killing thread. */
1949 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1950 if (ret) {
1951 err = 0;
1952 goto exit;
1953 }
1954
1955 /* Event on the registration socket */
1956 if (pollfd == apps_sock) {
1957 if (revents & LPOLLIN) {
1958 sock = lttcomm_accept_unix_sock(apps_sock);
1959 if (sock < 0) {
1960 goto error;
1961 }
1962
1963 /*
1964 * Set socket timeout for both receiving and ending.
1965 * app_socket_timeout is in seconds, whereas
1966 * lttcomm_setsockopt_rcv_timeout and
1967 * lttcomm_setsockopt_snd_timeout expect msec as
1968 * parameter.
1969 */
1970 if (config.app_socket_timeout >= 0) {
1971 (void) lttcomm_setsockopt_rcv_timeout(sock,
1972 config.app_socket_timeout * 1000);
1973 (void) lttcomm_setsockopt_snd_timeout(sock,
1974 config.app_socket_timeout * 1000);
1975 }
1976
1977 /*
1978 * Set the CLOEXEC flag. Return code is useless because
1979 * either way, the show must go on.
1980 */
1981 (void) utils_set_fd_cloexec(sock);
1982
1983 /* Create UST registration command for enqueuing */
1984 ust_cmd = zmalloc(sizeof(struct ust_command));
1985 if (ust_cmd == NULL) {
1986 PERROR("ust command zmalloc");
1987 ret = close(sock);
1988 if (ret) {
1989 PERROR("close");
1990 }
1991 goto error;
1992 }
1993
1994 /*
1995 * Using message-based transmissions to ensure we don't
1996 * have to deal with partially received messages.
1997 */
1998 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1999 if (ret < 0) {
2000 ERR("Exhausted file descriptors allowed for applications.");
2001 free(ust_cmd);
2002 ret = close(sock);
2003 if (ret) {
2004 PERROR("close");
2005 }
2006 sock = -1;
2007 continue;
2008 }
2009
2010 health_code_update();
2011 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2012 if (ret < 0) {
2013 free(ust_cmd);
2014 /* Close socket of the application. */
2015 ret = close(sock);
2016 if (ret) {
2017 PERROR("close");
2018 }
2019 lttng_fd_put(LTTNG_FD_APPS, 1);
2020 sock = -1;
2021 continue;
2022 }
2023 health_code_update();
2024
2025 ust_cmd->sock = sock;
2026 sock = -1;
2027
2028 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2029 " gid:%d sock:%d name:%s (version %d.%d)",
2030 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2031 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2032 ust_cmd->sock, ust_cmd->reg_msg.name,
2033 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
2034
2035 /*
2036 * Lock free enqueue the registration request. The red pill
2037 * has been taken! This apps will be part of the *system*.
2038 */
2039 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
2040
2041 /*
2042 * Wake the registration queue futex. Implicit memory
2043 * barrier with the exchange in cds_wfcq_enqueue.
2044 */
2045 futex_nto1_wake(&ust_cmd_queue.futex);
2046 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2047 ERR("Register apps socket poll error");
2048 goto error;
2049 } else {
2050 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2051 goto error;
2052 }
2053 }
2054 }
2055 }
2056
2057 exit:
2058 error:
2059 /* Notify that the registration thread is gone */
2060 notify_ust_apps(0);
2061
2062 if (apps_sock >= 0) {
2063 ret = close(apps_sock);
2064 if (ret) {
2065 PERROR("close");
2066 }
2067 }
2068 if (sock >= 0) {
2069 ret = close(sock);
2070 if (ret) {
2071 PERROR("close");
2072 }
2073 lttng_fd_put(LTTNG_FD_APPS, 1);
2074 }
2075 unlink(config.apps_unix_sock_path.value);
2076
2077 error_poll_add:
2078 lttng_poll_clean(&events);
2079 error_listen:
2080 error_create_poll:
2081 error_testpoint:
2082 DBG("UST Registration thread cleanup complete");
2083 if (err) {
2084 health_error();
2085 ERR("Health error occurred in %s", __func__);
2086 }
2087 health_unregister(health_sessiond);
2088
2089 return NULL;
2090 }
2091
2092 /*
2093 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2094 * exec or it will fails.
2095 */
2096 static int spawn_consumer_thread(struct consumer_data *consumer_data)
2097 {
2098 int ret, clock_ret;
2099 struct timespec timeout;
2100
2101 /*
2102 * Make sure we set the readiness flag to 0 because we are NOT ready.
2103 * This access to consumer_thread_is_ready does not need to be
2104 * protected by consumer_data.cond_mutex (yet) since the consumer
2105 * management thread has not been started at this point.
2106 */
2107 consumer_data->consumer_thread_is_ready = 0;
2108
2109 /* Setup pthread condition */
2110 ret = pthread_condattr_init(&consumer_data->condattr);
2111 if (ret) {
2112 errno = ret;
2113 PERROR("pthread_condattr_init consumer data");
2114 goto error;
2115 }
2116
2117 /*
2118 * Set the monotonic clock in order to make sure we DO NOT jump in time
2119 * between the clock_gettime() call and the timedwait call. See bug #324
2120 * for a more details and how we noticed it.
2121 */
2122 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2123 if (ret) {
2124 errno = ret;
2125 PERROR("pthread_condattr_setclock consumer data");
2126 goto error;
2127 }
2128
2129 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2130 if (ret) {
2131 errno = ret;
2132 PERROR("pthread_cond_init consumer data");
2133 goto error;
2134 }
2135
2136 ret = pthread_create(&consumer_data->thread, default_pthread_attr(),
2137 thread_manage_consumer, consumer_data);
2138 if (ret) {
2139 errno = ret;
2140 PERROR("pthread_create consumer");
2141 ret = -1;
2142 goto error;
2143 }
2144
2145 /* We are about to wait on a pthread condition */
2146 pthread_mutex_lock(&consumer_data->cond_mutex);
2147
2148 /* Get time for sem_timedwait absolute timeout */
2149 clock_ret = lttng_clock_gettime(CLOCK_MONOTONIC, &timeout);
2150 /*
2151 * Set the timeout for the condition timed wait even if the clock gettime
2152 * call fails since we might loop on that call and we want to avoid to
2153 * increment the timeout too many times.
2154 */
2155 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2156
2157 /*
2158 * The following loop COULD be skipped in some conditions so this is why we
2159 * set ret to 0 in order to make sure at least one round of the loop is
2160 * done.
2161 */
2162 ret = 0;
2163
2164 /*
2165 * Loop until the condition is reached or when a timeout is reached. Note
2166 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2167 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2168 * possible. This loop does not take any chances and works with both of
2169 * them.
2170 */
2171 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2172 if (clock_ret < 0) {
2173 PERROR("clock_gettime spawn consumer");
2174 /* Infinite wait for the consumerd thread to be ready */
2175 ret = pthread_cond_wait(&consumer_data->cond,
2176 &consumer_data->cond_mutex);
2177 } else {
2178 ret = pthread_cond_timedwait(&consumer_data->cond,
2179 &consumer_data->cond_mutex, &timeout);
2180 }
2181 }
2182
2183 /* Release the pthread condition */
2184 pthread_mutex_unlock(&consumer_data->cond_mutex);
2185
2186 if (ret != 0) {
2187 errno = ret;
2188 if (ret == ETIMEDOUT) {
2189 int pth_ret;
2190
2191 /*
2192 * Call has timed out so we kill the kconsumerd_thread and return
2193 * an error.
2194 */
2195 ERR("Condition timed out. The consumer thread was never ready."
2196 " Killing it");
2197 pth_ret = pthread_cancel(consumer_data->thread);
2198 if (pth_ret < 0) {
2199 PERROR("pthread_cancel consumer thread");
2200 }
2201 } else {
2202 PERROR("pthread_cond_wait failed consumer thread");
2203 }
2204 /* Caller is expecting a negative value on failure. */
2205 ret = -1;
2206 goto error;
2207 }
2208
2209 pthread_mutex_lock(&consumer_data->pid_mutex);
2210 if (consumer_data->pid == 0) {
2211 ERR("Consumerd did not start");
2212 pthread_mutex_unlock(&consumer_data->pid_mutex);
2213 goto error;
2214 }
2215 pthread_mutex_unlock(&consumer_data->pid_mutex);
2216
2217 return 0;
2218
2219 error:
2220 return ret;
2221 }
2222
2223 /*
2224 * Join consumer thread
2225 */
2226 static int join_consumer_thread(struct consumer_data *consumer_data)
2227 {
2228 void *status;
2229
2230 /* Consumer pid must be a real one. */
2231 if (consumer_data->pid > 0) {
2232 int ret;
2233 ret = kill(consumer_data->pid, SIGTERM);
2234 if (ret) {
2235 PERROR("Error killing consumer daemon");
2236 return ret;
2237 }
2238 return pthread_join(consumer_data->thread, &status);
2239 } else {
2240 return 0;
2241 }
2242 }
2243
2244 /*
2245 * Fork and exec a consumer daemon (consumerd).
2246 *
2247 * Return pid if successful else -1.
2248 */
2249 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2250 {
2251 int ret;
2252 pid_t pid;
2253 const char *consumer_to_use;
2254 const char *verbosity;
2255 struct stat st;
2256
2257 DBG("Spawning consumerd");
2258
2259 pid = fork();
2260 if (pid == 0) {
2261 /*
2262 * Exec consumerd.
2263 */
2264 if (config.verbose_consumer) {
2265 verbosity = "--verbose";
2266 } else if (lttng_opt_quiet) {
2267 verbosity = "--quiet";
2268 } else {
2269 verbosity = "";
2270 }
2271
2272 switch (consumer_data->type) {
2273 case LTTNG_CONSUMER_KERNEL:
2274 /*
2275 * Find out which consumerd to execute. We will first try the
2276 * 64-bit path, then the sessiond's installation directory, and
2277 * fallback on the 32-bit one,
2278 */
2279 DBG3("Looking for a kernel consumer at these locations:");
2280 DBG3(" 1) %s", config.consumerd64_bin_path.value ? : "NULL");
2281 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, DEFAULT_CONSUMERD_FILE);
2282 DBG3(" 3) %s", config.consumerd32_bin_path.value ? : "NULL");
2283 if (stat(config.consumerd64_bin_path.value, &st) == 0) {
2284 DBG3("Found location #1");
2285 consumer_to_use = config.consumerd64_bin_path.value;
2286 } else if (stat(INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE, &st) == 0) {
2287 DBG3("Found location #2");
2288 consumer_to_use = INSTALL_BIN_PATH "/" DEFAULT_CONSUMERD_FILE;
2289 } else if (config.consumerd32_bin_path.value &&
2290 stat(config.consumerd32_bin_path.value, &st) == 0) {
2291 DBG3("Found location #3");
2292 consumer_to_use = config.consumerd32_bin_path.value;
2293 } else {
2294 DBG("Could not find any valid consumerd executable");
2295 ret = -EINVAL;
2296 goto error;
2297 }
2298 DBG("Using kernel consumer at: %s", consumer_to_use);
2299 (void) execl(consumer_to_use,
2300 "lttng-consumerd", verbosity, "-k",
2301 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2302 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2303 "--group", config.tracing_group_name.value,
2304 NULL);
2305 break;
2306 case LTTNG_CONSUMER64_UST:
2307 {
2308 if (config.consumerd64_lib_dir.value) {
2309 char *tmp;
2310 size_t tmplen;
2311 char *tmpnew;
2312
2313 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2314 if (!tmp) {
2315 tmp = "";
2316 }
2317 tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
2318 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2319 if (!tmpnew) {
2320 ret = -ENOMEM;
2321 goto error;
2322 }
2323 strcat(tmpnew, config.consumerd64_lib_dir.value);
2324 if (tmp[0] != '\0') {
2325 strcat(tmpnew, ":");
2326 strcat(tmpnew, tmp);
2327 }
2328 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
2329 free(tmpnew);
2330 if (ret) {
2331 ret = -errno;
2332 goto error;
2333 }
2334 }
2335 DBG("Using 64-bit UST consumer at: %s", config.consumerd64_bin_path.value);
2336 (void) execl(config.consumerd64_bin_path.value, "lttng-consumerd", verbosity, "-u",
2337 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2338 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2339 "--group", config.tracing_group_name.value,
2340 NULL);
2341 break;
2342 }
2343 case LTTNG_CONSUMER32_UST:
2344 {
2345 if (config.consumerd32_lib_dir.value) {
2346 char *tmp;
2347 size_t tmplen;
2348 char *tmpnew;
2349
2350 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
2351 if (!tmp) {
2352 tmp = "";
2353 }
2354 tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
2355 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2356 if (!tmpnew) {
2357 ret = -ENOMEM;
2358 goto error;
2359 }
2360 strcat(tmpnew, config.consumerd32_lib_dir.value);
2361 if (tmp[0] != '\0') {
2362 strcat(tmpnew, ":");
2363 strcat(tmpnew, tmp);
2364 }
2365 ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
2366 free(tmpnew);
2367 if (ret) {
2368 ret = -errno;
2369 goto error;
2370 }
2371 }
2372 DBG("Using 32-bit UST consumer at: %s", config.consumerd32_bin_path.value);
2373 (void) execl(config.consumerd32_bin_path.value, "lttng-consumerd", verbosity, "-u",
2374 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2375 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2376 "--group", config.tracing_group_name.value,
2377 NULL);
2378 break;
2379 }
2380 default:
2381 ERR("unknown consumer type");
2382 errno = 0;
2383 }
2384 if (errno != 0) {
2385 PERROR("Consumer execl()");
2386 }
2387 /* Reaching this point, we got a failure on our execl(). */
2388 exit(EXIT_FAILURE);
2389 } else if (pid > 0) {
2390 ret = pid;
2391 } else {
2392 PERROR("start consumer fork");
2393 ret = -errno;
2394 }
2395 error:
2396 return ret;
2397 }
2398
2399 /*
2400 * Spawn the consumerd daemon and session daemon thread.
2401 */
2402 static int start_consumerd(struct consumer_data *consumer_data)
2403 {
2404 int ret;
2405
2406 /*
2407 * Set the listen() state on the socket since there is a possible race
2408 * between the exec() of the consumer daemon and this call if place in the
2409 * consumer thread. See bug #366 for more details.
2410 */
2411 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2412 if (ret < 0) {
2413 goto error;
2414 }
2415
2416 pthread_mutex_lock(&consumer_data->pid_mutex);
2417 if (consumer_data->pid != 0) {
2418 pthread_mutex_unlock(&consumer_data->pid_mutex);
2419 goto end;
2420 }
2421
2422 ret = spawn_consumerd(consumer_data);
2423 if (ret < 0) {
2424 ERR("Spawning consumerd failed");
2425 pthread_mutex_unlock(&consumer_data->pid_mutex);
2426 goto error;
2427 }
2428
2429 /* Setting up the consumer_data pid */
2430 consumer_data->pid = ret;
2431 DBG2("Consumer pid %d", consumer_data->pid);
2432 pthread_mutex_unlock(&consumer_data->pid_mutex);
2433
2434 DBG2("Spawning consumer control thread");
2435 ret = spawn_consumer_thread(consumer_data);
2436 if (ret < 0) {
2437 ERR("Fatal error spawning consumer control thread");
2438 goto error;
2439 }
2440
2441 end:
2442 return 0;
2443
2444 error:
2445 /* Cleanup already created sockets on error. */
2446 if (consumer_data->err_sock >= 0) {
2447 int err;
2448
2449 err = close(consumer_data->err_sock);
2450 if (err < 0) {
2451 PERROR("close consumer data error socket");
2452 }
2453 }
2454 return ret;
2455 }
2456
2457 /*
2458 * Setup necessary data for kernel tracer action.
2459 */
2460 static int init_kernel_tracer(void)
2461 {
2462 int ret;
2463
2464 /* Modprobe lttng kernel modules */
2465 ret = modprobe_lttng_control();
2466 if (ret < 0) {
2467 goto error;
2468 }
2469
2470 /* Open debugfs lttng */
2471 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2472 if (kernel_tracer_fd < 0) {
2473 DBG("Failed to open %s", module_proc_lttng);
2474 goto error_open;
2475 }
2476
2477 /* Validate kernel version */
2478 ret = kernel_validate_version(kernel_tracer_fd, &kernel_tracer_version,
2479 &kernel_tracer_abi_version);
2480 if (ret < 0) {
2481 goto error_version;
2482 }
2483
2484 ret = modprobe_lttng_data();
2485 if (ret < 0) {
2486 goto error_modules;
2487 }
2488
2489 ret = kernel_supports_ring_buffer_snapshot_sample_positions(
2490 kernel_tracer_fd);
2491 if (ret < 0) {
2492 goto error_modules;
2493 }
2494
2495 if (ret < 1) {
2496 WARN("Kernel tracer does not support buffer monitoring. "
2497 "The monitoring timer of channels in the kernel domain "
2498 "will be set to 0 (disabled).");
2499 }
2500
2501 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2502 return 0;
2503
2504 error_version:
2505 modprobe_remove_lttng_control();
2506 ret = close(kernel_tracer_fd);
2507 if (ret) {
2508 PERROR("close");
2509 }
2510 kernel_tracer_fd = -1;
2511 return LTTNG_ERR_KERN_VERSION;
2512
2513 error_modules:
2514 ret = close(kernel_tracer_fd);
2515 if (ret) {
2516 PERROR("close");
2517 }
2518
2519 error_open:
2520 modprobe_remove_lttng_control();
2521
2522 error:
2523 WARN("No kernel tracer available");
2524 kernel_tracer_fd = -1;
2525 if (!is_root) {
2526 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2527 } else {
2528 return LTTNG_ERR_KERN_NA;
2529 }
2530 }
2531
2532
2533 /*
2534 * Copy consumer output from the tracing session to the domain session. The
2535 * function also applies the right modification on a per domain basis for the
2536 * trace files destination directory.
2537 *
2538 * Should *NOT* be called with RCU read-side lock held.
2539 */
2540 static int copy_session_consumer(int domain, struct ltt_session *session)
2541 {
2542 int ret;
2543 const char *dir_name;
2544 struct consumer_output *consumer;
2545
2546 assert(session);
2547 assert(session->consumer);
2548
2549 switch (domain) {
2550 case LTTNG_DOMAIN_KERNEL:
2551 DBG3("Copying tracing session consumer output in kernel session");
2552 /*
2553 * XXX: We should audit the session creation and what this function
2554 * does "extra" in order to avoid a destroy since this function is used
2555 * in the domain session creation (kernel and ust) only. Same for UST
2556 * domain.
2557 */
2558 if (session->kernel_session->consumer) {
2559 consumer_output_put(session->kernel_session->consumer);
2560 }
2561 session->kernel_session->consumer =
2562 consumer_copy_output(session->consumer);
2563 /* Ease our life a bit for the next part */
2564 consumer = session->kernel_session->consumer;
2565 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2566 break;
2567 case LTTNG_DOMAIN_JUL:
2568 case LTTNG_DOMAIN_LOG4J:
2569 case LTTNG_DOMAIN_PYTHON:
2570 case LTTNG_DOMAIN_UST:
2571 DBG3("Copying tracing session consumer output in UST session");
2572 if (session->ust_session->consumer) {
2573 consumer_output_put(session->ust_session->consumer);
2574 }
2575 session->ust_session->consumer =
2576 consumer_copy_output(session->consumer);
2577 /* Ease our life a bit for the next part */
2578 consumer = session->ust_session->consumer;
2579 dir_name = DEFAULT_UST_TRACE_DIR;
2580 break;
2581 default:
2582 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2583 goto error;
2584 }
2585
2586 /* Append correct directory to subdir */
2587 strncat(consumer->subdir, dir_name,
2588 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2589 DBG3("Copy session consumer subdir %s", consumer->subdir);
2590
2591 ret = LTTNG_OK;
2592
2593 error:
2594 return ret;
2595 }
2596
2597 /*
2598 * Create an UST session and add it to the session ust list.
2599 *
2600 * Should *NOT* be called with RCU read-side lock held.
2601 */
2602 static int create_ust_session(struct ltt_session *session,
2603 struct lttng_domain *domain)
2604 {
2605 int ret;
2606 struct ltt_ust_session *lus = NULL;
2607
2608 assert(session);
2609 assert(domain);
2610 assert(session->consumer);
2611
2612 switch (domain->type) {
2613 case LTTNG_DOMAIN_JUL:
2614 case LTTNG_DOMAIN_LOG4J:
2615 case LTTNG_DOMAIN_PYTHON:
2616 case LTTNG_DOMAIN_UST:
2617 break;
2618 default:
2619 ERR("Unknown UST domain on create session %d", domain->type);
2620 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2621 goto error;
2622 }
2623
2624 DBG("Creating UST session");
2625
2626 lus = trace_ust_create_session(session->id);
2627 if (lus == NULL) {
2628 ret = LTTNG_ERR_UST_SESS_FAIL;
2629 goto error;
2630 }
2631
2632 lus->uid = session->uid;
2633 lus->gid = session->gid;
2634 lus->output_traces = session->output_traces;
2635 lus->snapshot_mode = session->snapshot_mode;
2636 lus->live_timer_interval = session->live_timer;
2637 session->ust_session = lus;
2638 if (session->shm_path[0]) {
2639 strncpy(lus->root_shm_path, session->shm_path,
2640 sizeof(lus->root_shm_path));
2641 lus->root_shm_path[sizeof(lus->root_shm_path) - 1] = '\0';
2642 strncpy(lus->shm_path, session->shm_path,
2643 sizeof(lus->shm_path));
2644 lus->shm_path[sizeof(lus->shm_path) - 1] = '\0';
2645 strncat(lus->shm_path, "/ust",
2646 sizeof(lus->shm_path) - strlen(lus->shm_path) - 1);
2647 }
2648 /* Copy session output to the newly created UST session */
2649 ret = copy_session_consumer(domain->type, session);
2650 if (ret != LTTNG_OK) {
2651 goto error;
2652 }
2653
2654 return LTTNG_OK;
2655
2656 error:
2657 free(lus);
2658 session->ust_session = NULL;
2659 return ret;
2660 }
2661
2662 /*
2663 * Create a kernel tracer session then create the default channel.
2664 */
2665 static int create_kernel_session(struct ltt_session *session)
2666 {
2667 int ret;
2668
2669 DBG("Creating kernel session");
2670
2671 ret = kernel_create_session(session, kernel_tracer_fd);
2672 if (ret < 0) {
2673 ret = LTTNG_ERR_KERN_SESS_FAIL;
2674 goto error;
2675 }
2676
2677 /* Code flow safety */
2678 assert(session->kernel_session);
2679
2680 /* Copy session output to the newly created Kernel session */
2681 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2682 if (ret != LTTNG_OK) {
2683 goto error;
2684 }
2685
2686 session->kernel_session->uid = session->uid;
2687 session->kernel_session->gid = session->gid;
2688 session->kernel_session->output_traces = session->output_traces;
2689 session->kernel_session->snapshot_mode = session->snapshot_mode;
2690
2691 return LTTNG_OK;
2692
2693 error:
2694 trace_kernel_destroy_session(session->kernel_session);
2695 session->kernel_session = NULL;
2696 return ret;
2697 }
2698
2699 /*
2700 * Count number of session permitted by uid/gid.
2701 */
2702 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2703 {
2704 unsigned int i = 0;
2705 struct ltt_session *session;
2706 const struct ltt_session_list *session_list = session_get_list();
2707
2708 DBG("Counting number of available session for UID %d GID %d",
2709 uid, gid);
2710 cds_list_for_each_entry(session, &session_list->head, list) {
2711 if (!session_get(session)) {
2712 continue;
2713 }
2714 session_lock(session);
2715 /* Only count the sessions the user can control. */
2716 if (session_access_ok(session, uid, gid) &&
2717 !session->destroyed) {
2718 i++;
2719 }
2720 session_unlock(session);
2721 session_put(session);
2722 }
2723 return i;
2724 }
2725
2726 static int receive_userspace_probe(struct command_ctx *cmd_ctx, int sock,
2727 int *sock_error, struct lttng_event *event)
2728 {
2729 int fd, ret;
2730 struct lttng_userspace_probe_location *probe_location;
2731 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
2732 struct lttng_dynamic_buffer probe_location_buffer;
2733 struct lttng_buffer_view buffer_view;
2734
2735 /*
2736 * Create a buffer to store the serialized version of the probe
2737 * location.
2738 */
2739 lttng_dynamic_buffer_init(&probe_location_buffer);
2740 ret = lttng_dynamic_buffer_set_size(&probe_location_buffer,
2741 cmd_ctx->lsm->u.enable.userspace_probe_location_len);
2742 if (ret) {
2743 ret = LTTNG_ERR_NOMEM;
2744 goto error;
2745 }
2746
2747 /*
2748 * Receive the probe location.
2749 */
2750 ret = lttcomm_recv_unix_sock(sock, probe_location_buffer.data,
2751 probe_location_buffer.size);
2752 if (ret <= 0) {
2753 DBG("Nothing recv() from client var len data... continuing");
2754 *sock_error = 1;
2755 lttng_dynamic_buffer_reset(&probe_location_buffer);
2756 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2757 goto error;
2758 }
2759
2760 buffer_view = lttng_buffer_view_from_dynamic_buffer(
2761 &probe_location_buffer, 0, probe_location_buffer.size);
2762
2763 /*
2764 * Extract the probe location from the serialized version.
2765 */
2766 ret = lttng_userspace_probe_location_create_from_buffer(
2767 &buffer_view, &probe_location);
2768 if (ret < 0) {
2769 WARN("Failed to create a userspace probe location from the received buffer");
2770 lttng_dynamic_buffer_reset( &probe_location_buffer);
2771 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2772 goto error;
2773 }
2774
2775 /*
2776 * Receive the file descriptor to the target binary from the client.
2777 */
2778 DBG("Receiving userspace probe target FD from client ...");
2779 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2780 if (ret <= 0) {
2781 DBG("Nothing recv() from client userspace probe fd... continuing");
2782 *sock_error = 1;
2783 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2784 goto error;
2785 }
2786
2787 /*
2788 * Set the file descriptor received from the client through the unix
2789 * socket in the probe location.
2790 */
2791 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
2792 if (!lookup) {
2793 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2794 goto error;
2795 }
2796
2797 /*
2798 * From the kernel tracer's perspective, all userspace probe event types
2799 * are all the same: a file and an offset.
2800 */
2801 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
2802 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
2803 ret = lttng_userspace_probe_location_function_set_binary_fd(
2804 probe_location, fd);
2805 break;
2806 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
2807 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd(
2808 probe_location, fd);
2809 break;
2810 default:
2811 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2812 goto error;
2813 }
2814
2815 if (ret) {
2816 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2817 goto error;
2818 }
2819
2820 /* Attach the probe location to the event. */
2821 ret = lttng_event_set_userspace_probe_location(event, probe_location);
2822 if (ret) {
2823 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
2824 goto error;
2825 }
2826
2827 lttng_dynamic_buffer_reset(&probe_location_buffer);
2828 error:
2829 return ret;
2830 }
2831
2832 /*
2833 * Check if the current kernel tracer supports the session rotation feature.
2834 * Return 1 if it does, 0 otherwise.
2835 */
2836 static int check_rotate_compatible(void)
2837 {
2838 int ret = 1;
2839
2840 if (kernel_tracer_version.major != 2 || kernel_tracer_version.minor < 11) {
2841 DBG("Kernel tracer version is not compatible with the rotation feature");
2842 ret = 0;
2843 }
2844
2845 return ret;
2846 }
2847
2848 /*
2849 * Process the command requested by the lttng client within the command
2850 * context structure. This function make sure that the return structure (llm)
2851 * is set and ready for transmission before returning.
2852 *
2853 * Return any error encountered or 0 for success.
2854 *
2855 * "sock" is only used for special-case var. len data.
2856 *
2857 * Should *NOT* be called with RCU read-side lock held.
2858 */
2859 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2860 int *sock_error)
2861 {
2862 int ret = LTTNG_OK;
2863 int need_tracing_session = 1;
2864 int need_domain;
2865
2866 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2867
2868 assert(!rcu_read_ongoing());
2869
2870 *sock_error = 0;
2871
2872 switch (cmd_ctx->lsm->cmd_type) {
2873 case LTTNG_CREATE_SESSION:
2874 case LTTNG_CREATE_SESSION_SNAPSHOT:
2875 case LTTNG_CREATE_SESSION_LIVE:
2876 case LTTNG_DESTROY_SESSION:
2877 case LTTNG_LIST_SESSIONS:
2878 case LTTNG_LIST_DOMAINS:
2879 case LTTNG_START_TRACE:
2880 case LTTNG_STOP_TRACE:
2881 case LTTNG_DATA_PENDING:
2882 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2883 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2884 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2885 case LTTNG_SNAPSHOT_RECORD:
2886 case LTTNG_SAVE_SESSION:
2887 case LTTNG_SET_SESSION_SHM_PATH:
2888 case LTTNG_REGENERATE_METADATA:
2889 case LTTNG_REGENERATE_STATEDUMP:
2890 case LTTNG_REGISTER_TRIGGER:
2891 case LTTNG_UNREGISTER_TRIGGER:
2892 case LTTNG_ROTATE_SESSION:
2893 case LTTNG_ROTATION_GET_INFO:
2894 case LTTNG_ROTATION_SET_SCHEDULE:
2895 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2896 need_domain = 0;
2897 break;
2898 default:
2899 need_domain = 1;
2900 }
2901
2902 if (config.no_kernel && need_domain
2903 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2904 if (!is_root) {
2905 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2906 } else {
2907 ret = LTTNG_ERR_KERN_NA;
2908 }
2909 goto error;
2910 }
2911
2912 /* Deny register consumer if we already have a spawned consumer. */
2913 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2914 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2915 if (kconsumer_data.pid > 0) {
2916 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2917 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2918 goto error;
2919 }
2920 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2921 }
2922
2923 /*
2924 * Check for command that don't needs to allocate a returned payload. We do
2925 * this here so we don't have to make the call for no payload at each
2926 * command.
2927 */
2928 switch(cmd_ctx->lsm->cmd_type) {
2929 case LTTNG_LIST_SESSIONS:
2930 case LTTNG_LIST_TRACEPOINTS:
2931 case LTTNG_LIST_TRACEPOINT_FIELDS:
2932 case LTTNG_LIST_DOMAINS:
2933 case LTTNG_LIST_CHANNELS:
2934 case LTTNG_LIST_EVENTS:
2935 case LTTNG_LIST_SYSCALLS:
2936 case LTTNG_LIST_TRACKER_PIDS:
2937 case LTTNG_DATA_PENDING:
2938 case LTTNG_ROTATE_SESSION:
2939 case LTTNG_ROTATION_GET_INFO:
2940 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
2941 break;
2942 default:
2943 /* Setup lttng message with no payload */
2944 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
2945 if (ret < 0) {
2946 /* This label does not try to unlock the session */
2947 goto init_setup_error;
2948 }
2949 }
2950
2951 /* Commands that DO NOT need a session. */
2952 switch (cmd_ctx->lsm->cmd_type) {
2953 case LTTNG_CREATE_SESSION:
2954 case LTTNG_CREATE_SESSION_SNAPSHOT:
2955 case LTTNG_CREATE_SESSION_LIVE:
2956 case LTTNG_LIST_SESSIONS:
2957 case LTTNG_LIST_TRACEPOINTS:
2958 case LTTNG_LIST_SYSCALLS:
2959 case LTTNG_LIST_TRACEPOINT_FIELDS:
2960 case LTTNG_SAVE_SESSION:
2961 case LTTNG_REGISTER_TRIGGER:
2962 case LTTNG_UNREGISTER_TRIGGER:
2963 need_tracing_session = 0;
2964 break;
2965 default:
2966 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2967 /*
2968 * We keep the session list lock across _all_ commands
2969 * for now, because the per-session lock does not
2970 * handle teardown properly.
2971 */
2972 session_lock_list();
2973 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2974 if (cmd_ctx->session == NULL) {
2975 ret = LTTNG_ERR_SESS_NOT_FOUND;
2976 goto error;
2977 } else {
2978 /* Acquire lock for the session */
2979 session_lock(cmd_ctx->session);
2980 }
2981 break;
2982 }
2983
2984 /*
2985 * Commands that need a valid session but should NOT create one if none
2986 * exists. Instead of creating one and destroying it when the command is
2987 * handled, process that right before so we save some round trip in useless
2988 * code path.
2989 */
2990 switch (cmd_ctx->lsm->cmd_type) {
2991 case LTTNG_DISABLE_CHANNEL:
2992 case LTTNG_DISABLE_EVENT:
2993 switch (cmd_ctx->lsm->domain.type) {
2994 case LTTNG_DOMAIN_KERNEL:
2995 if (!cmd_ctx->session->kernel_session) {
2996 ret = LTTNG_ERR_NO_CHANNEL;
2997 goto error;
2998 }
2999 break;
3000 case LTTNG_DOMAIN_JUL:
3001 case LTTNG_DOMAIN_LOG4J:
3002 case LTTNG_DOMAIN_PYTHON:
3003 case LTTNG_DOMAIN_UST:
3004 if (!cmd_ctx->session->ust_session) {
3005 ret = LTTNG_ERR_NO_CHANNEL;
3006 goto error;
3007 }
3008 break;
3009 default:
3010 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3011 goto error;
3012 }
3013 default:
3014 break;
3015 }
3016
3017 if (!need_domain) {
3018 goto skip_domain;
3019 }
3020
3021 /*
3022 * Check domain type for specific "pre-action".
3023 */
3024 switch (cmd_ctx->lsm->domain.type) {
3025 case LTTNG_DOMAIN_KERNEL:
3026 if (!is_root) {
3027 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
3028 goto error;
3029 }
3030
3031 /* Kernel tracer check */
3032 if (kernel_tracer_fd == -1) {
3033 /* Basically, load kernel tracer modules */
3034 ret = init_kernel_tracer();
3035 if (ret != 0) {
3036 goto error;
3037 }
3038 }
3039
3040 /* Consumer is in an ERROR state. Report back to client */
3041 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
3042 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3043 goto error;
3044 }
3045
3046 /* Need a session for kernel command */
3047 if (need_tracing_session) {
3048 if (cmd_ctx->session->kernel_session == NULL) {
3049 ret = create_kernel_session(cmd_ctx->session);
3050 if (ret < 0) {
3051 ret = LTTNG_ERR_KERN_SESS_FAIL;
3052 goto error;
3053 }
3054 }
3055
3056 /* Start the kernel consumer daemon */
3057 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3058 if (kconsumer_data.pid == 0 &&
3059 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3060 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3061 ret = start_consumerd(&kconsumer_data);
3062 if (ret < 0) {
3063 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3064 goto error;
3065 }
3066 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3067 } else {
3068 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3069 }
3070
3071 /*
3072 * The consumer was just spawned so we need to add the socket to
3073 * the consumer output of the session if exist.
3074 */
3075 ret = consumer_create_socket(&kconsumer_data,
3076 cmd_ctx->session->kernel_session->consumer);
3077 if (ret < 0) {
3078 goto error;
3079 }
3080 }
3081
3082 break;
3083 case LTTNG_DOMAIN_JUL:
3084 case LTTNG_DOMAIN_LOG4J:
3085 case LTTNG_DOMAIN_PYTHON:
3086 case LTTNG_DOMAIN_UST:
3087 {
3088 if (!ust_app_supported()) {
3089 ret = LTTNG_ERR_NO_UST;
3090 goto error;
3091 }
3092 /* Consumer is in an ERROR state. Report back to client */
3093 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
3094 ret = LTTNG_ERR_NO_USTCONSUMERD;
3095 goto error;
3096 }
3097
3098 if (need_tracing_session) {
3099 /* Create UST session if none exist. */
3100 if (cmd_ctx->session->ust_session == NULL) {
3101 ret = create_ust_session(cmd_ctx->session,
3102 &cmd_ctx->lsm->domain);
3103 if (ret != LTTNG_OK) {
3104 goto error;
3105 }
3106 }
3107
3108 /* Start the UST consumer daemons */
3109 /* 64-bit */
3110 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
3111 if (config.consumerd64_bin_path.value &&
3112 ustconsumer64_data.pid == 0 &&
3113 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3114 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3115 ret = start_consumerd(&ustconsumer64_data);
3116 if (ret < 0) {
3117 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
3118 uatomic_set(&ust_consumerd64_fd, -EINVAL);
3119 goto error;
3120 }
3121
3122 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
3123 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3124 } else {
3125 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3126 }
3127
3128 /*
3129 * Setup socket for consumer 64 bit. No need for atomic access
3130 * since it was set above and can ONLY be set in this thread.
3131 */
3132 ret = consumer_create_socket(&ustconsumer64_data,
3133 cmd_ctx->session->ust_session->consumer);
3134 if (ret < 0) {
3135 goto error;
3136 }
3137
3138 /* 32-bit */
3139 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
3140 if (config.consumerd32_bin_path.value &&
3141 ustconsumer32_data.pid == 0 &&
3142 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3143 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3144 ret = start_consumerd(&ustconsumer32_data);
3145 if (ret < 0) {
3146 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
3147 uatomic_set(&ust_consumerd32_fd, -EINVAL);
3148 goto error;
3149 }
3150
3151 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
3152 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3153 } else {
3154 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3155 }
3156
3157 /*
3158 * Setup socket for consumer 32 bit. No need for atomic access
3159 * since it was set above and can ONLY be set in this thread.
3160 */
3161 ret = consumer_create_socket(&ustconsumer32_data,
3162 cmd_ctx->session->ust_session->consumer);
3163 if (ret < 0) {
3164 goto error;
3165 }
3166 }
3167 break;
3168 }
3169 default:
3170 break;
3171 }
3172 skip_domain:
3173
3174 /* Validate consumer daemon state when start/stop trace command */
3175 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3176 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3177 switch (cmd_ctx->lsm->domain.type) {
3178 case LTTNG_DOMAIN_NONE:
3179 break;
3180 case LTTNG_DOMAIN_JUL:
3181 case LTTNG_DOMAIN_LOG4J:
3182 case LTTNG_DOMAIN_PYTHON:
3183 case LTTNG_DOMAIN_UST:
3184 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
3185 ret = LTTNG_ERR_NO_USTCONSUMERD;
3186 goto error;
3187 }
3188 break;
3189 case LTTNG_DOMAIN_KERNEL:
3190 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
3191 ret = LTTNG_ERR_NO_KERNCONSUMERD;
3192 goto error;
3193 }
3194 break;
3195 default:
3196 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3197 goto error;
3198 }
3199 }
3200
3201 /*
3202 * Check that the UID or GID match that of the tracing session.
3203 * The root user can interact with all sessions.
3204 */
3205 if (need_tracing_session) {
3206 if (!session_access_ok(cmd_ctx->session,
3207 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3208 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds)) ||
3209 cmd_ctx->session->destroyed) {
3210 ret = LTTNG_ERR_EPERM;
3211 goto error;
3212 }
3213 }
3214
3215 /*
3216 * Send relayd information to consumer as soon as we have a domain and a
3217 * session defined.
3218 */
3219 if (cmd_ctx->session && need_domain) {
3220 /*
3221 * Setup relayd if not done yet. If the relayd information was already
3222 * sent to the consumer, this call will gracefully return.
3223 */
3224 ret = cmd_setup_relayd(cmd_ctx->session);
3225 if (ret != LTTNG_OK) {
3226 goto error;
3227 }
3228 }
3229
3230 /* Process by command type */
3231 switch (cmd_ctx->lsm->cmd_type) {
3232 case LTTNG_ADD_CONTEXT:
3233 {
3234 /*
3235 * An LTTNG_ADD_CONTEXT command might have a supplementary
3236 * payload if the context being added is an application context.
3237 */
3238 if (cmd_ctx->lsm->u.context.ctx.ctx ==
3239 LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
3240 char *provider_name = NULL, *context_name = NULL;
3241 size_t provider_name_len =
3242 cmd_ctx->lsm->u.context.provider_name_len;
3243 size_t context_name_len =
3244 cmd_ctx->lsm->u.context.context_name_len;
3245
3246 if (provider_name_len == 0 || context_name_len == 0) {
3247 /*
3248 * Application provider and context names MUST
3249 * be provided.
3250 */
3251 ret = -LTTNG_ERR_INVALID;
3252 goto error;
3253 }
3254
3255 provider_name = zmalloc(provider_name_len + 1);
3256 if (!provider_name) {
3257 ret = -LTTNG_ERR_NOMEM;
3258 goto error;
3259 }
3260 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name =
3261 provider_name;
3262
3263 context_name = zmalloc(context_name_len + 1);
3264 if (!context_name) {
3265 ret = -LTTNG_ERR_NOMEM;
3266 goto error_add_context;
3267 }
3268 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name =
3269 context_name;
3270
3271 ret = lttcomm_recv_unix_sock(sock, provider_name,
3272 provider_name_len);
3273 if (ret < 0) {
3274 goto error_add_context;
3275 }
3276
3277 ret = lttcomm_recv_unix_sock(sock, context_name,
3278 context_name_len);
3279 if (ret < 0) {
3280 goto error_add_context;
3281 }
3282 }
3283
3284 /*
3285 * cmd_add_context assumes ownership of the provider and context
3286 * names.
3287 */
3288 ret = cmd_add_context(cmd_ctx->session,
3289 cmd_ctx->lsm->domain.type,
3290 cmd_ctx->lsm->u.context.channel_name,
3291 &cmd_ctx->lsm->u.context.ctx,
3292 kernel_poll_pipe[1]);
3293
3294 cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name = NULL;
3295 cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name = NULL;
3296 error_add_context:
3297 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.provider_name);
3298 free(cmd_ctx->lsm->u.context.ctx.u.app_ctx.ctx_name);
3299 if (ret < 0) {
3300 goto error;
3301 }
3302 break;
3303 }
3304 case LTTNG_DISABLE_CHANNEL:
3305 {
3306 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3307 cmd_ctx->lsm->u.disable.channel_name);
3308 break;
3309 }
3310 case LTTNG_DISABLE_EVENT:
3311 {
3312
3313 /*
3314 * FIXME: handle filter; for now we just receive the filter's
3315 * bytecode along with the filter expression which are sent by
3316 * liblttng-ctl and discard them.
3317 *
3318 * This fixes an issue where the client may block while sending
3319 * the filter payload and encounter an error because the session
3320 * daemon closes the socket without ever handling this data.
3321 */
3322 size_t count = cmd_ctx->lsm->u.disable.expression_len +
3323 cmd_ctx->lsm->u.disable.bytecode_len;
3324
3325 if (count) {
3326 char data[LTTNG_FILTER_MAX_LEN];
3327
3328 DBG("Discarding disable event command payload of size %zu", count);
3329 while (count) {
3330 ret = lttcomm_recv_unix_sock(sock, data,
3331 count > sizeof(data) ? sizeof(data) : count);
3332 if (ret < 0) {
3333 goto error;
3334 }
3335
3336 count -= (size_t) ret;
3337 }
3338 }
3339 /* FIXME: passing packed structure to non-packed pointer */
3340 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3341 cmd_ctx->lsm->u.disable.channel_name,
3342 &cmd_ctx->lsm->u.disable.event);
3343 break;
3344 }
3345 case LTTNG_ENABLE_CHANNEL:
3346 {
3347 cmd_ctx->lsm->u.channel.chan.attr.extended.ptr =
3348 (struct lttng_channel_extended *) &cmd_ctx->lsm->u.channel.extended;
3349 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
3350 &cmd_ctx->lsm->u.channel.chan,
3351 kernel_poll_pipe[1]);
3352 break;
3353 }
3354 case LTTNG_TRACK_PID:
3355 {
3356 ret = cmd_track_pid(cmd_ctx->session,
3357 cmd_ctx->lsm->domain.type,
3358 cmd_ctx->lsm->u.pid_tracker.pid);
3359 break;
3360 }
3361 case LTTNG_UNTRACK_PID:
3362 {
3363 ret = cmd_untrack_pid(cmd_ctx->session,
3364 cmd_ctx->lsm->domain.type,
3365 cmd_ctx->lsm->u.pid_tracker.pid);
3366 break;
3367 }
3368 case LTTNG_ENABLE_EVENT:
3369 {
3370 struct lttng_event *ev = NULL;
3371 struct lttng_event_exclusion *exclusion = NULL;
3372 struct lttng_filter_bytecode *bytecode = NULL;
3373 char *filter_expression = NULL;
3374
3375 /* Handle exclusion events and receive it from the client. */
3376 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3377 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3378
3379 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3380 (count * LTTNG_SYMBOL_NAME_LEN));
3381 if (!exclusion) {
3382 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3383 goto error;
3384 }
3385
3386 DBG("Receiving var len exclusion event list from client ...");
3387 exclusion->count = count;
3388 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3389 count * LTTNG_SYMBOL_NAME_LEN);
3390 if (ret <= 0) {
3391 DBG("Nothing recv() from client var len data... continuing");
3392 *sock_error = 1;
3393 free(exclusion);
3394 ret = LTTNG_ERR_EXCLUSION_INVAL;
3395 goto error;
3396 }
3397 }
3398
3399 /* Get filter expression from client. */
3400 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3401 size_t expression_len =
3402 cmd_ctx->lsm->u.enable.expression_len;
3403
3404 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3405 ret = LTTNG_ERR_FILTER_INVAL;
3406 free(exclusion);
3407 goto error;
3408 }
3409
3410 filter_expression = zmalloc(expression_len);
3411 if (!filter_expression) {
3412 free(exclusion);
3413 ret = LTTNG_ERR_FILTER_NOMEM;
3414 goto error;
3415 }
3416
3417 /* Receive var. len. data */
3418 DBG("Receiving var len filter's expression from client ...");
3419 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3420 expression_len);
3421 if (ret <= 0) {
3422 DBG("Nothing recv() from client var len data... continuing");
3423 *sock_error = 1;
3424 free(filter_expression);
3425 free(exclusion);
3426 ret = LTTNG_ERR_FILTER_INVAL;
3427 goto error;
3428 }
3429 }
3430
3431 /* Handle filter and get bytecode from client. */
3432 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3433 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3434
3435 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3436 ret = LTTNG_ERR_FILTER_INVAL;
3437 free(filter_expression);
3438 free(exclusion);
3439 goto error;
3440 }
3441
3442 bytecode = zmalloc(bytecode_len);
3443 if (!bytecode) {
3444 free(filter_expression);
3445 free(exclusion);
3446 ret = LTTNG_ERR_FILTER_NOMEM;
3447 goto error;
3448 }
3449
3450 /* Receive var. len. data */
3451 DBG("Receiving var len filter's bytecode from client ...");
3452 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3453 if (ret <= 0) {
3454 DBG("Nothing recv() from client var len data... continuing");
3455 *sock_error = 1;
3456 free(filter_expression);
3457 free(bytecode);
3458 free(exclusion);
3459 ret = LTTNG_ERR_FILTER_INVAL;
3460 goto error;
3461 }
3462
3463 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3464 free(filter_expression);
3465 free(bytecode);
3466 free(exclusion);
3467 ret = LTTNG_ERR_FILTER_INVAL;
3468 goto error;
3469 }
3470 }
3471
3472 ev = lttng_event_copy(&cmd_ctx->lsm->u.enable.event);
3473 if (!ev) {
3474 DBG("Failed to copy event: %s",
3475 cmd_ctx->lsm->u.enable.event.name);
3476 free(filter_expression);
3477 free(bytecode);
3478 free(exclusion);
3479 ret = LTTNG_ERR_NOMEM;
3480 goto error;
3481 }
3482
3483
3484 if (cmd_ctx->lsm->u.enable.userspace_probe_location_len > 0) {
3485 /* Expect a userspace probe description. */
3486 ret = receive_userspace_probe(cmd_ctx, sock, sock_error, ev);
3487 if (ret) {
3488 free(filter_expression);
3489 free(bytecode);
3490 free(exclusion);
3491 lttng_event_destroy(ev);
3492 goto error;
3493 }
3494 }
3495
3496 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3497 cmd_ctx->lsm->u.enable.channel_name,
3498 ev,
3499 filter_expression, bytecode, exclusion,
3500 kernel_poll_pipe[1]);
3501 lttng_event_destroy(ev);
3502 break;
3503 }
3504 case LTTNG_LIST_TRACEPOINTS:
3505 {
3506 struct lttng_event *events;
3507 ssize_t nb_events;
3508
3509 session_lock_list();
3510 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3511 session_unlock_list();
3512 if (nb_events < 0) {
3513 /* Return value is a negative lttng_error_code. */
3514 ret = -nb_events;
3515 goto error;
3516 }
3517
3518 /*
3519 * Setup lttng message with payload size set to the event list size in
3520 * bytes and then copy list into the llm payload.
3521 */
3522 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3523 sizeof(struct lttng_event) * nb_events);
3524 free(events);
3525
3526 if (ret < 0) {
3527 goto setup_error;
3528 }
3529
3530 ret = LTTNG_OK;
3531 break;
3532 }
3533 case LTTNG_LIST_TRACEPOINT_FIELDS:
3534 {
3535 struct lttng_event_field *fields;
3536 ssize_t nb_fields;
3537
3538 session_lock_list();
3539 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3540 &fields);
3541 session_unlock_list();
3542 if (nb_fields < 0) {
3543 /* Return value is a negative lttng_error_code. */
3544 ret = -nb_fields;
3545 goto error;
3546 }
3547
3548 /*
3549 * Setup lttng message with payload size set to the event list size in
3550 * bytes and then copy list into the llm payload.
3551 */
3552 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, fields,
3553 sizeof(struct lttng_event_field) * nb_fields);
3554 free(fields);
3555
3556 if (ret < 0) {
3557 goto setup_error;
3558 }
3559
3560 ret = LTTNG_OK;
3561 break;
3562 }
3563 case LTTNG_LIST_SYSCALLS:
3564 {
3565 struct lttng_event *events;
3566 ssize_t nb_events;
3567
3568 nb_events = cmd_list_syscalls(&events);
3569 if (nb_events < 0) {
3570 /* Return value is a negative lttng_error_code. */
3571 ret = -nb_events;
3572 goto error;
3573 }
3574
3575 /*
3576 * Setup lttng message with payload size set to the event list size in
3577 * bytes and then copy list into the llm payload.
3578 */
3579 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, events,
3580 sizeof(struct lttng_event) * nb_events);
3581 free(events);
3582
3583 if (ret < 0) {
3584 goto setup_error;
3585 }
3586
3587 ret = LTTNG_OK;
3588 break;
3589 }
3590 case LTTNG_LIST_TRACKER_PIDS:
3591 {
3592 int32_t *pids = NULL;
3593 ssize_t nr_pids;
3594
3595 nr_pids = cmd_list_tracker_pids(cmd_ctx->session,
3596 cmd_ctx->lsm->domain.type, &pids);
3597 if (nr_pids < 0) {
3598 /* Return value is a negative lttng_error_code. */
3599 ret = -nr_pids;
3600 goto error;
3601 }
3602
3603 /*
3604 * Setup lttng message with payload size set to the event list size in
3605 * bytes and then copy list into the llm payload.
3606 */
3607 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, pids,
3608 sizeof(int32_t) * nr_pids);
3609 free(pids);
3610
3611 if (ret < 0) {
3612 goto setup_error;
3613 }
3614
3615 ret = LTTNG_OK;
3616 break;
3617 }
3618 case LTTNG_SET_CONSUMER_URI:
3619 {
3620 size_t nb_uri, len;
3621 struct lttng_uri *uris;
3622
3623 nb_uri = cmd_ctx->lsm->u.uri.size;
3624 len = nb_uri * sizeof(struct lttng_uri);
3625
3626 if (nb_uri == 0) {
3627 ret = LTTNG_ERR_INVALID;
3628 goto error;
3629 }
3630
3631 uris = zmalloc(len);
3632 if (uris == NULL) {
3633 ret = LTTNG_ERR_FATAL;
3634 goto error;
3635 }
3636
3637 /* Receive variable len data */
3638 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3639 ret = lttcomm_recv_unix_sock(sock, uris, len);
3640 if (ret <= 0) {
3641 DBG("No URIs received from client... continuing");
3642 *sock_error = 1;
3643 ret = LTTNG_ERR_SESSION_FAIL;
3644 free(uris);
3645 goto error;
3646 }
3647
3648 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3649 free(uris);
3650 if (ret != LTTNG_OK) {
3651 goto error;
3652 }
3653
3654
3655 break;
3656 }
3657 case LTTNG_START_TRACE:
3658 {
3659 /*
3660 * On the first start, if we have a kernel session and we have
3661 * enabled time or size-based rotations, we have to make sure
3662 * the kernel tracer supports it.
3663 */
3664 if (!cmd_ctx->session->has_been_started && \
3665 cmd_ctx->session->kernel_session && \
3666 (cmd_ctx->session->rotate_timer_period || \
3667 cmd_ctx->session->rotate_size) && \
3668 !check_rotate_compatible()) {
3669 DBG("Kernel tracer version is not compatible with the rotation feature");
3670 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
3671 goto error;
3672 }
3673 ret = cmd_start_trace(cmd_ctx->session);
3674 break;
3675 }
3676 case LTTNG_STOP_TRACE:
3677 {
3678 ret = cmd_stop_trace(cmd_ctx->session);
3679 break;
3680 }
3681 case LTTNG_CREATE_SESSION:
3682 {
3683 size_t nb_uri, len;
3684 struct lttng_uri *uris = NULL;
3685
3686 nb_uri = cmd_ctx->lsm->u.uri.size;
3687 len = nb_uri * sizeof(struct lttng_uri);
3688
3689 if (nb_uri > 0) {
3690 uris = zmalloc(len);
3691 if (uris == NULL) {
3692 ret = LTTNG_ERR_FATAL;
3693 goto error;
3694 }
3695
3696 /* Receive variable len data */
3697 DBG("Waiting for %zu URIs from client ...", nb_uri);
3698 ret = lttcomm_recv_unix_sock(sock, uris, len);
3699 if (ret <= 0) {
3700 DBG("No URIs received from client... continuing");
3701 *sock_error = 1;
3702 ret = LTTNG_ERR_SESSION_FAIL;
3703 free(uris);
3704 goto error;
3705 }
3706
3707 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3708 DBG("Creating session with ONE network URI is a bad call");
3709 ret = LTTNG_ERR_SESSION_FAIL;
3710 free(uris);
3711 goto error;
3712 }
3713 }
3714
3715 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
3716 &cmd_ctx->creds, 0);
3717
3718 free(uris);
3719
3720 break;
3721 }
3722 case LTTNG_DESTROY_SESSION:
3723 {
3724 ret = cmd_destroy_session(cmd_ctx->session,
3725 notification_thread_handle);
3726 break;
3727 }
3728 case LTTNG_LIST_DOMAINS:
3729 {
3730 ssize_t nb_dom;
3731 struct lttng_domain *domains = NULL;
3732
3733 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3734 if (nb_dom < 0) {
3735 /* Return value is a negative lttng_error_code. */
3736 ret = -nb_dom;
3737 goto error;
3738 }
3739
3740 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, domains,
3741 nb_dom * sizeof(struct lttng_domain));
3742 free(domains);
3743
3744 if (ret < 0) {
3745 goto setup_error;
3746 }
3747
3748 ret = LTTNG_OK;
3749 break;
3750 }
3751 case LTTNG_LIST_CHANNELS:
3752 {
3753 ssize_t payload_size;
3754 struct lttng_channel *channels = NULL;
3755
3756 payload_size = cmd_list_channels(cmd_ctx->lsm->domain.type,
3757 cmd_ctx->session, &channels);
3758 if (payload_size < 0) {
3759 /* Return value is a negative lttng_error_code. */
3760 ret = -payload_size;
3761 goto error;
3762 }
3763
3764 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, channels,
3765 payload_size);
3766 free(channels);
3767
3768 if (ret < 0) {
3769 goto setup_error;
3770 }
3771
3772 ret = LTTNG_OK;
3773 break;
3774 }
3775 case LTTNG_LIST_EVENTS:
3776 {
3777 ssize_t nb_event;
3778 struct lttng_event *events = NULL;
3779 struct lttcomm_event_command_header cmd_header;
3780 size_t total_size;
3781
3782 memset(&cmd_header, 0, sizeof(cmd_header));
3783 /* Extended infos are included at the end of events */
3784 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type,
3785 cmd_ctx->session, cmd_ctx->lsm->u.list.channel_name,
3786 &events, &total_size);
3787
3788 if (nb_event < 0) {
3789 /* Return value is a negative lttng_error_code. */
3790 ret = -nb_event;
3791 goto error;
3792 }
3793
3794 cmd_header.nb_events = nb_event;
3795 ret = setup_lttng_msg(cmd_ctx, events, total_size,
3796 &cmd_header, sizeof(cmd_header));
3797 free(events);
3798
3799 if (ret < 0) {
3800 goto setup_error;
3801 }
3802
3803 ret = LTTNG_OK;
3804 break;
3805 }
3806 case LTTNG_LIST_SESSIONS:
3807 {
3808 unsigned int nr_sessions;
3809 void *sessions_payload;
3810 size_t payload_len;
3811
3812 session_lock_list();
3813 nr_sessions = lttng_sessions_count(
3814 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3815 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3816 payload_len = sizeof(struct lttng_session) * nr_sessions;
3817 sessions_payload = zmalloc(payload_len);
3818
3819 if (!sessions_payload) {
3820 session_unlock_list();
3821 ret = -ENOMEM;
3822 goto setup_error;
3823 }
3824
3825 cmd_list_lttng_sessions(sessions_payload,
3826 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3827 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3828 session_unlock_list();
3829
3830 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, sessions_payload,
3831 payload_len);
3832 free(sessions_payload);
3833
3834 if (ret < 0) {
3835 goto setup_error;
3836 }
3837
3838 ret = LTTNG_OK;
3839 break;
3840 }
3841 case LTTNG_REGISTER_CONSUMER:
3842 {
3843 struct consumer_data *cdata;
3844
3845 switch (cmd_ctx->lsm->domain.type) {
3846 case LTTNG_DOMAIN_KERNEL:
3847 cdata = &kconsumer_data;
3848 break;
3849 default:
3850 ret = LTTNG_ERR_UND;
3851 goto error;
3852 }
3853
3854 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3855 cmd_ctx->lsm->u.reg.path, cdata);
3856 break;
3857 }
3858 case LTTNG_DATA_PENDING:
3859 {
3860 int pending_ret;
3861 uint8_t pending_ret_byte;
3862
3863 pending_ret = cmd_data_pending(cmd_ctx->session);
3864
3865 /*
3866 * FIXME
3867 *
3868 * This function may returns 0 or 1 to indicate whether or not
3869 * there is data pending. In case of error, it should return an
3870 * LTTNG_ERR code. However, some code paths may still return
3871 * a nondescript error code, which we handle by returning an
3872 * "unknown" error.
3873 */
3874 if (pending_ret == 0 || pending_ret == 1) {
3875 /*
3876 * ret will be set to LTTNG_OK at the end of
3877 * this function.
3878 */
3879 } else if (pending_ret < 0) {
3880 ret = LTTNG_ERR_UNK;
3881 goto setup_error;
3882 } else {
3883 ret = pending_ret;
3884 goto setup_error;
3885 }
3886
3887 pending_ret_byte = (uint8_t) pending_ret;
3888
3889 /* 1 byte to return whether or not data is pending */
3890 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
3891 &pending_ret_byte, 1);
3892
3893 if (ret < 0) {
3894 goto setup_error;
3895 }
3896
3897 ret = LTTNG_OK;
3898 break;
3899 }
3900 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3901 {
3902 struct lttcomm_lttng_output_id reply;
3903
3904 ret = cmd_snapshot_add_output(cmd_ctx->session,
3905 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3906 if (ret != LTTNG_OK) {
3907 goto error;
3908 }
3909
3910 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
3911 sizeof(reply));
3912 if (ret < 0) {
3913 goto setup_error;
3914 }
3915
3916 /* Copy output list into message payload */
3917 ret = LTTNG_OK;
3918 break;
3919 }
3920 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3921 {
3922 ret = cmd_snapshot_del_output(cmd_ctx->session,
3923 &cmd_ctx->lsm->u.snapshot_output.output);
3924 break;
3925 }
3926 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3927 {
3928 ssize_t nb_output;
3929 struct lttng_snapshot_output *outputs = NULL;
3930
3931 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3932 if (nb_output < 0) {
3933 ret = -nb_output;
3934 goto error;
3935 }
3936
3937 assert((nb_output > 0 && outputs) || nb_output == 0);
3938 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
3939 nb_output * sizeof(struct lttng_snapshot_output));
3940 free(outputs);
3941
3942 if (ret < 0) {
3943 goto setup_error;
3944 }
3945
3946 ret = LTTNG_OK;
3947 break;
3948 }
3949 case LTTNG_SNAPSHOT_RECORD:
3950 {
3951 ret = cmd_snapshot_record(cmd_ctx->session,
3952 &cmd_ctx->lsm->u.snapshot_record.output,
3953 cmd_ctx->lsm->u.snapshot_record.wait);
3954 break;
3955 }
3956 case LTTNG_CREATE_SESSION_SNAPSHOT:
3957 {
3958 size_t nb_uri, len;
3959 struct lttng_uri *uris = NULL;
3960
3961 nb_uri = cmd_ctx->lsm->u.uri.size;
3962 len = nb_uri * sizeof(struct lttng_uri);
3963
3964 if (nb_uri > 0) {
3965 uris = zmalloc(len);
3966 if (uris == NULL) {
3967 ret = LTTNG_ERR_FATAL;
3968 goto error;
3969 }
3970
3971 /* Receive variable len data */
3972 DBG("Waiting for %zu URIs from client ...", nb_uri);
3973 ret = lttcomm_recv_unix_sock(sock, uris, len);
3974 if (ret <= 0) {
3975 DBG("No URIs received from client... continuing");
3976 *sock_error = 1;
3977 ret = LTTNG_ERR_SESSION_FAIL;
3978 free(uris);
3979 goto error;
3980 }
3981
3982 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3983 DBG("Creating session with ONE network URI is a bad call");
3984 ret = LTTNG_ERR_SESSION_FAIL;
3985 free(uris);
3986 goto error;
3987 }
3988 }
3989
3990 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3991 nb_uri, &cmd_ctx->creds);
3992 free(uris);
3993 break;
3994 }
3995 case LTTNG_CREATE_SESSION_LIVE:
3996 {
3997 size_t nb_uri, len;
3998 struct lttng_uri *uris = NULL;
3999
4000 nb_uri = cmd_ctx->lsm->u.uri.size;
4001 len = nb_uri * sizeof(struct lttng_uri);
4002
4003 if (nb_uri > 0) {
4004 uris = zmalloc(len);
4005 if (uris == NULL) {
4006 ret = LTTNG_ERR_FATAL;
4007 goto error;
4008 }
4009
4010 /* Receive variable len data */
4011 DBG("Waiting for %zu URIs from client ...", nb_uri);
4012 ret = lttcomm_recv_unix_sock(sock, uris, len);
4013 if (ret <= 0) {
4014 DBG("No URIs received from client... continuing");
4015 *sock_error = 1;
4016 ret = LTTNG_ERR_SESSION_FAIL;
4017 free(uris);
4018 goto error;
4019 }
4020
4021 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4022 DBG("Creating session with ONE network URI is a bad call");
4023 ret = LTTNG_ERR_SESSION_FAIL;
4024 free(uris);
4025 goto error;
4026 }
4027 }
4028
4029 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
4030 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
4031 free(uris);
4032 break;
4033 }
4034 case LTTNG_SAVE_SESSION:
4035 {
4036 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
4037 &cmd_ctx->creds);
4038 break;
4039 }
4040 case LTTNG_SET_SESSION_SHM_PATH:
4041 {
4042 ret = cmd_set_session_shm_path(cmd_ctx->session,
4043 cmd_ctx->lsm->u.set_shm_path.shm_path);
4044 break;
4045 }
4046 case LTTNG_REGENERATE_METADATA:
4047 {
4048 ret = cmd_regenerate_metadata(cmd_ctx->session);
4049 break;
4050 }
4051 case LTTNG_REGENERATE_STATEDUMP:
4052 {
4053 ret = cmd_regenerate_statedump(cmd_ctx->session);
4054 break;
4055 }
4056 case LTTNG_REGISTER_TRIGGER:
4057 {
4058 ret = cmd_register_trigger(cmd_ctx, sock,
4059 notification_thread_handle);
4060 break;
4061 }
4062 case LTTNG_UNREGISTER_TRIGGER:
4063 {
4064 ret = cmd_unregister_trigger(cmd_ctx, sock,
4065 notification_thread_handle);
4066 break;
4067 }
4068 case LTTNG_ROTATE_SESSION:
4069 {
4070 struct lttng_rotate_session_return rotate_return;
4071
4072 DBG("Client rotate session \"%s\"", cmd_ctx->session->name);
4073
4074 memset(&rotate_return, 0, sizeof(rotate_return));
4075 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
4076 DBG("Kernel tracer version is not compatible with the rotation feature");
4077 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
4078 goto error;
4079 }
4080
4081 ret = cmd_rotate_session(cmd_ctx->session, &rotate_return);
4082 if (ret < 0) {
4083 ret = -ret;
4084 goto error;
4085 }
4086
4087 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &rotate_return,
4088 sizeof(rotate_return));
4089 if (ret < 0) {
4090 ret = -ret;
4091 goto error;
4092 }
4093
4094 ret = LTTNG_OK;
4095 break;
4096 }
4097 case LTTNG_ROTATION_GET_INFO:
4098 {
4099 struct lttng_rotation_get_info_return get_info_return;
4100
4101 memset(&get_info_return, 0, sizeof(get_info_return));
4102 ret = cmd_rotate_get_info(cmd_ctx->session, &get_info_return,
4103 cmd_ctx->lsm->u.get_rotation_info.rotation_id);
4104 if (ret < 0) {
4105 ret = -ret;
4106 goto error;
4107 }
4108
4109 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &get_info_return,
4110 sizeof(get_info_return));
4111 if (ret < 0) {
4112 ret = -ret;
4113 goto error;
4114 }
4115
4116 ret = LTTNG_OK;
4117 break;
4118 }
4119 case LTTNG_ROTATION_SET_SCHEDULE:
4120 {
4121 bool set_schedule;
4122 enum lttng_rotation_schedule_type schedule_type;
4123 uint64_t value;
4124
4125 if (cmd_ctx->session->kernel_session && !check_rotate_compatible()) {
4126 DBG("Kernel tracer version does not support session rotations");
4127 ret = LTTNG_ERR_ROTATION_WRONG_VERSION;
4128 goto error;
4129 }
4130
4131 set_schedule = cmd_ctx->lsm->u.rotation_set_schedule.set == 1;
4132 schedule_type = (enum lttng_rotation_schedule_type) cmd_ctx->lsm->u.rotation_set_schedule.type;
4133 value = cmd_ctx->lsm->u.rotation_set_schedule.value;
4134
4135 ret = cmd_rotation_set_schedule(cmd_ctx->session,
4136 set_schedule,
4137 schedule_type,
4138 value,
4139 notification_thread_handle);
4140 if (ret != LTTNG_OK) {
4141 goto error;
4142 }
4143
4144 break;
4145 }
4146 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES:
4147 {
4148 struct lttng_session_list_schedules_return schedules = {
4149 .periodic.set = !!cmd_ctx->session->rotate_timer_period,
4150 .periodic.value = cmd_ctx->session->rotate_timer_period,
4151 .size.set = !!cmd_ctx->session->rotate_size,
4152 .size.value = cmd_ctx->session->rotate_size,
4153 };
4154
4155 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &schedules,
4156 sizeof(schedules));
4157 if (ret < 0) {
4158 ret = -ret;
4159 goto error;
4160 }
4161
4162 ret = LTTNG_OK;
4163 break;
4164 }
4165 default:
4166 ret = LTTNG_ERR_UND;
4167 break;
4168 }
4169
4170 error:
4171 if (cmd_ctx->llm == NULL) {
4172 DBG("Missing llm structure. Allocating one.");
4173 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
4174 goto setup_error;
4175 }
4176 }
4177 /* Set return code */
4178 cmd_ctx->llm->ret_code = ret;
4179 setup_error:
4180 if (cmd_ctx->session) {
4181 session_unlock(cmd_ctx->session);
4182 session_put(cmd_ctx->session);
4183 }
4184 if (need_tracing_session) {
4185 session_unlock_list();
4186 }
4187 init_setup_error:
4188 assert(!rcu_read_ongoing());
4189 return ret;
4190 }
4191
4192 /*
4193 * This thread manage all clients request using the unix client socket for
4194 * communication.
4195 */
4196 static void *thread_manage_clients(void *data)
4197 {
4198 int sock = -1, ret, i, pollfd, err = -1;
4199 int sock_error;
4200 uint32_t revents, nb_fd;
4201 struct command_ctx *cmd_ctx = NULL;
4202 struct lttng_poll_event events;
4203
4204 DBG("[thread] Manage client started");
4205
4206 rcu_register_thread();
4207
4208 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
4209
4210 health_code_update();
4211
4212 ret = lttcomm_listen_unix_sock(client_sock);
4213 if (ret < 0) {
4214 goto error_listen;
4215 }
4216
4217 /*
4218 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4219 * more will be added to this poll set.
4220 */
4221 ret = sessiond_set_thread_pollset(&events, 2);
4222 if (ret < 0) {
4223 goto error_create_poll;
4224 }
4225
4226 /* Add the application registration socket */
4227 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4228 if (ret < 0) {
4229 goto error;
4230 }
4231
4232 ret = sem_post(&load_info->message_thread_ready);
4233 if (ret) {
4234 PERROR("sem_post message_thread_ready");
4235 goto error;
4236 }
4237
4238 /*
4239 * Wait until all support threads are initialized before accepting
4240 * commands.
4241 */
4242 while (uatomic_read(&lttng_sessiond_ready) != 0) {
4243 /*
4244 * If a support thread failed to launch, it may signal that
4245 * we must exit and the sessiond would never be marked as
4246 * "ready".
4247 *
4248 * The timeout is set to 1ms, which serves as a way to
4249 * pace down this check.
4250 */
4251 ret = sessiond_wait_for_quit_pipe(1000);
4252 if (ret > 0) {
4253 goto exit;
4254 }
4255 }
4256 /*
4257 * This barrier is paired with the one in sessiond_notify_ready() to
4258 * ensure that loads accessing data initialized by the other threads,
4259 * on which this thread was waiting, are not performed before this point.
4260 *
4261 * Note that this could be a 'read' memory barrier, but a full barrier
4262 * is used in case the code changes. The performance implications of
4263 * this choice are minimal since this is a slow path.
4264 */
4265 cmm_smp_mb();
4266
4267 /* This testpoint is after we signal readiness to the parent. */
4268 if (testpoint(sessiond_thread_manage_clients)) {
4269 goto error;
4270 }
4271
4272 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
4273 goto error;
4274 }
4275
4276 health_code_update();
4277
4278 /* Set state as running. */
4279 sessiond_set_client_thread_state(true);
4280
4281 while (1) {
4282 const struct cmd_completion_handler *cmd_completion_handler;
4283
4284 DBG("Accepting client command ...");
4285
4286 /* Inifinite blocking call, waiting for transmission */
4287 restart:
4288 health_poll_entry();
4289 ret = lttng_poll_wait(&events, -1);
4290 health_poll_exit();
4291 if (ret < 0) {
4292 /*
4293 * Restart interrupted system call.
4294 */
4295 if (errno == EINTR) {
4296 goto restart;
4297 }
4298 goto error;
4299 }
4300
4301 nb_fd = ret;
4302
4303 for (i = 0; i < nb_fd; i++) {
4304 /* Fetch once the poll data */
4305 revents = LTTNG_POLL_GETEV(&events, i);
4306 pollfd = LTTNG_POLL_GETFD(&events, i);
4307
4308 health_code_update();
4309
4310 if (!revents) {
4311 /* No activity for this FD (poll implementation). */
4312 continue;
4313 }
4314
4315 /* Thread quit pipe has been closed. Killing thread. */
4316 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
4317 if (ret) {
4318 err = 0;
4319 goto exit;
4320 }
4321
4322 /* Event on the registration socket */
4323 if (pollfd == client_sock) {
4324 if (revents & LPOLLIN) {
4325 continue;
4326 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4327 ERR("Client socket poll error");
4328 goto error;
4329 } else {
4330 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
4331 goto error;
4332 }
4333 }
4334 }
4335
4336 DBG("Wait for client response");
4337
4338 health_code_update();
4339
4340 sock = lttcomm_accept_unix_sock(client_sock);
4341 if (sock < 0) {
4342 goto error;
4343 }
4344
4345 /*
4346 * Set the CLOEXEC flag. Return code is useless because either way, the
4347 * show must go on.
4348 */
4349 (void) utils_set_fd_cloexec(sock);
4350
4351 /* Set socket option for credentials retrieval */
4352 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4353 if (ret < 0) {
4354 goto error;
4355 }
4356
4357 /* Allocate context command to process the client request */
4358 cmd_ctx = zmalloc(sizeof(struct command_ctx));
4359 if (cmd_ctx == NULL) {
4360 PERROR("zmalloc cmd_ctx");
4361 goto error;
4362 }
4363
4364 /* Allocate data buffer for reception */
4365 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
4366 if (cmd_ctx->lsm == NULL) {
4367 PERROR("zmalloc cmd_ctx->lsm");
4368 goto error;
4369 }
4370
4371 cmd_ctx->llm = NULL;
4372 cmd_ctx->session = NULL;
4373
4374 health_code_update();
4375
4376 /*
4377 * Data is received from the lttng client. The struct
4378 * lttcomm_session_msg (lsm) contains the command and data request of
4379 * the client.
4380 */
4381 DBG("Receiving data from client ...");
4382 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4383 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
4384 if (ret <= 0) {
4385 DBG("Nothing recv() from client... continuing");
4386 ret = close(sock);
4387 if (ret) {
4388 PERROR("close");
4389 }
4390 sock = -1;
4391 clean_command_ctx(&cmd_ctx);
4392 continue;
4393 }
4394
4395 health_code_update();
4396
4397 // TODO: Validate cmd_ctx including sanity check for
4398 // security purpose.
4399
4400 rcu_thread_online();
4401 /*
4402 * This function dispatch the work to the kernel or userspace tracer
4403 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4404 * informations for the client. The command context struct contains
4405 * everything this function may needs.
4406 */
4407 ret = process_client_msg(cmd_ctx, sock, &sock_error);
4408 rcu_thread_offline();
4409 if (ret < 0) {
4410 ret = close(sock);
4411 if (ret) {
4412 PERROR("close");
4413 }
4414 sock = -1;
4415 /*
4416 * TODO: Inform client somehow of the fatal error. At
4417 * this point, ret < 0 means that a zmalloc failed
4418 * (ENOMEM). Error detected but still accept
4419 * command, unless a socket error has been
4420 * detected.
4421 */
4422 clean_command_ctx(&cmd_ctx);
4423 continue;
4424 }
4425
4426 cmd_completion_handler = cmd_pop_completion_handler();
4427 if (cmd_completion_handler) {
4428 enum lttng_error_code completion_code;
4429
4430 completion_code = cmd_completion_handler->run(
4431 cmd_completion_handler->data);
4432 if (completion_code != LTTNG_OK) {
4433 clean_command_ctx(&cmd_ctx);
4434 continue;
4435 }
4436 }
4437
4438 health_code_update();
4439
4440 DBG("Sending response (size: %d, retcode: %s (%d))",
4441 cmd_ctx->lttng_msg_size,
4442 lttng_strerror(-cmd_ctx->llm->ret_code),
4443 cmd_ctx->llm->ret_code);
4444 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
4445 if (ret < 0) {
4446 ERR("Failed to send data back to client");
4447 }
4448
4449 /* End of transmission */
4450 ret = close(sock);
4451 if (ret) {
4452 PERROR("close");
4453 }
4454 sock = -1;
4455
4456 clean_command_ctx(&cmd_ctx);
4457
4458 health_code_update();
4459 }
4460
4461 exit:
4462 error:
4463 if (sock >= 0) {
4464 ret = close(sock);
4465 if (ret) {
4466 PERROR("close");
4467 }
4468 }
4469
4470 lttng_poll_clean(&events);
4471 clean_command_ctx(&cmd_ctx);
4472
4473 error_listen:
4474 error_create_poll:
4475 unlink(config.client_unix_sock_path.value);
4476 if (client_sock >= 0) {
4477 ret = close(client_sock);
4478 if (ret) {
4479 PERROR("close");
4480 }
4481 }
4482
4483 if (err) {
4484 health_error();
4485 ERR("Health error occurred in %s", __func__);
4486 }
4487
4488 health_unregister(health_sessiond);
4489
4490 DBG("Client thread dying");
4491
4492 rcu_unregister_thread();
4493
4494 /*
4495 * Since we are creating the consumer threads, we own them, so we need
4496 * to join them before our thread exits.
4497 */
4498 ret = join_consumer_thread(&kconsumer_data);
4499 if (ret) {
4500 errno = ret;
4501 PERROR("join_consumer");
4502 }
4503
4504 ret = join_consumer_thread(&ustconsumer32_data);
4505 if (ret) {
4506 errno = ret;
4507 PERROR("join_consumer ust32");
4508 }
4509
4510 ret = join_consumer_thread(&ustconsumer64_data);
4511 if (ret) {
4512 errno = ret;
4513 PERROR("join_consumer ust64");
4514 }
4515
4516 /* Set state as non-running. */
4517 sessiond_set_client_thread_state(false);
4518 return NULL;
4519 }
4520
4521 static int string_match(const char *str1, const char *str2)
4522 {
4523 return (str1 && str2) && !strcmp(str1, str2);
4524 }
4525
4526 /*
4527 * Take an option from the getopt output and set it in the right variable to be
4528 * used later.
4529 *
4530 * Return 0 on success else a negative value.
4531 */
4532 static int set_option(int opt, const char *arg, const char *optname)
4533 {
4534 int ret = 0;
4535
4536 if (string_match(optname, "client-sock") || opt == 'c') {
4537 if (!arg || *arg == '\0') {
4538 ret = -EINVAL;
4539 goto end;
4540 }
4541 if (lttng_is_setuid_setgid()) {
4542 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4543 "-c, --client-sock");
4544 } else {
4545 config_string_set(&config.client_unix_sock_path,
4546 strdup(arg));
4547 if (!config.client_unix_sock_path.value) {
4548 ret = -ENOMEM;
4549 PERROR("strdup");
4550 }
4551 }
4552 } else if (string_match(optname, "apps-sock") || opt == 'a') {
4553 if (!arg || *arg == '\0') {
4554 ret = -EINVAL;
4555 goto end;
4556 }
4557 if (lttng_is_setuid_setgid()) {
4558 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4559 "-a, --apps-sock");
4560 } else {
4561 config_string_set(&config.apps_unix_sock_path,
4562 strdup(arg));
4563 if (!config.apps_unix_sock_path.value) {
4564 ret = -ENOMEM;
4565 PERROR("strdup");
4566 }
4567 }
4568 } else if (string_match(optname, "daemonize") || opt == 'd') {
4569 config.daemonize = true;
4570 } else if (string_match(optname, "background") || opt == 'b') {
4571 config.background = true;
4572 } else if (string_match(optname, "group") || opt == 'g') {
4573 if (!arg || *arg == '\0') {
4574 ret = -EINVAL;
4575 goto end;
4576 }
4577 if (lttng_is_setuid_setgid()) {
4578 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4579 "-g, --group");
4580 } else {
4581 config_string_set(&config.tracing_group_name,
4582 strdup(arg));
4583 if (!config.tracing_group_name.value) {
4584 ret = -ENOMEM;
4585 PERROR("strdup");
4586 }
4587 }
4588 } else if (string_match(optname, "help") || opt == 'h') {
4589 ret = utils_show_help(8, "lttng-sessiond", help_msg);
4590 if (ret) {
4591 ERR("Cannot show --help for `lttng-sessiond`");
4592 perror("exec");
4593 }
4594 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
4595 } else if (string_match(optname, "version") || opt == 'V') {
4596 fprintf(stdout, "%s\n", VERSION);
4597 exit(EXIT_SUCCESS);
4598 } else if (string_match(optname, "sig-parent") || opt == 'S') {
4599 config.sig_parent = true;
4600 } else if (string_match(optname, "kconsumerd-err-sock")) {
4601 if (!arg || *arg == '\0') {
4602 ret = -EINVAL;
4603 goto end;
4604 }
4605 if (lttng_is_setuid_setgid()) {
4606 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4607 "--kconsumerd-err-sock");
4608 } else {
4609 config_string_set(&config.kconsumerd_err_unix_sock_path,
4610 strdup(arg));
4611 if (!config.kconsumerd_err_unix_sock_path.value) {
4612 ret = -ENOMEM;
4613 PERROR("strdup");
4614 }
4615 }
4616 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
4617 if (!arg || *arg == '\0') {
4618 ret = -EINVAL;
4619 goto end;
4620 }
4621 if (lttng_is_setuid_setgid()) {
4622 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4623 "--kconsumerd-cmd-sock");
4624 } else {
4625 config_string_set(&config.kconsumerd_cmd_unix_sock_path,
4626 strdup(arg));
4627 if (!config.kconsumerd_cmd_unix_sock_path.value) {
4628 ret = -ENOMEM;
4629 PERROR("strdup");
4630 }
4631 }
4632 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
4633 if (!arg || *arg == '\0') {
4634 ret = -EINVAL;
4635 goto end;
4636 }
4637 if (lttng_is_setuid_setgid()) {
4638 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4639 "--ustconsumerd64-err-sock");
4640 } else {
4641 config_string_set(&config.consumerd64_err_unix_sock_path,
4642 strdup(arg));
4643 if (!config.consumerd64_err_unix_sock_path.value) {
4644 ret = -ENOMEM;
4645 PERROR("strdup");
4646 }
4647 }
4648 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
4649 if (!arg || *arg == '\0') {
4650 ret = -EINVAL;
4651 goto end;
4652 }
4653 if (lttng_is_setuid_setgid()) {
4654 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4655 "--ustconsumerd64-cmd-sock");
4656 } else {
4657 config_string_set(&config.consumerd64_cmd_unix_sock_path,
4658 strdup(arg));
4659 if (!config.consumerd64_cmd_unix_sock_path.value) {
4660 ret = -ENOMEM;
4661 PERROR("strdup");
4662 }
4663 }
4664 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
4665 if (!arg || *arg == '\0') {
4666 ret = -EINVAL;
4667 goto end;
4668 }
4669 if (lttng_is_setuid_setgid()) {
4670 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4671 "--ustconsumerd32-err-sock");
4672 } else {
4673 config_string_set(&config.consumerd32_err_unix_sock_path,
4674 strdup(arg));
4675 if (!config.consumerd32_err_unix_sock_path.value) {
4676 ret = -ENOMEM;
4677 PERROR("strdup");
4678 }
4679 }
4680 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
4681 if (!arg || *arg == '\0') {
4682 ret = -EINVAL;
4683 goto end;
4684 }
4685 if (lttng_is_setuid_setgid()) {
4686 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4687 "--ustconsumerd32-cmd-sock");
4688 } else {
4689 config_string_set(&config.consumerd32_cmd_unix_sock_path,
4690 strdup(arg));
4691 if (!config.consumerd32_cmd_unix_sock_path.value) {
4692 ret = -ENOMEM;
4693 PERROR("strdup");
4694 }
4695 }
4696 } else if (string_match(optname, "no-kernel")) {
4697 config.no_kernel = true;
4698 } else if (string_match(optname, "quiet") || opt == 'q') {
4699 config.quiet = true;
4700 } else if (string_match(optname, "verbose") || opt == 'v') {
4701 /* Verbose level can increase using multiple -v */
4702 if (arg) {
4703 /* Value obtained from config file */
4704 config.verbose = config_parse_value(arg);
4705 } else {
4706 /* -v used on command line */
4707 config.verbose++;
4708 }
4709 /* Clamp value to [0, 3] */
4710 config.verbose = config.verbose < 0 ? 0 :
4711 (config.verbose <= 3 ? config.verbose : 3);
4712 } else if (string_match(optname, "verbose-consumer")) {
4713 if (arg) {
4714 config.verbose_consumer = config_parse_value(arg);
4715 } else {
4716 config.verbose_consumer++;
4717 }
4718 } else if (string_match(optname, "consumerd32-path")) {
4719 if (!arg || *arg == '\0') {
4720 ret = -EINVAL;
4721 goto end;
4722 }
4723 if (lttng_is_setuid_setgid()) {
4724 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4725 "--consumerd32-path");
4726 } else {
4727 config_string_set(&config.consumerd32_bin_path,
4728 strdup(arg));
4729 if (!config.consumerd32_bin_path.value) {
4730 PERROR("strdup");
4731 ret = -ENOMEM;
4732 }
4733 }
4734 } else if (string_match(optname, "consumerd32-libdir")) {
4735 if (!arg || *arg == '\0') {
4736 ret = -EINVAL;
4737 goto end;
4738 }
4739 if (lttng_is_setuid_setgid()) {
4740 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4741 "--consumerd32-libdir");
4742 } else {
4743 config_string_set(&config.consumerd32_lib_dir,
4744 strdup(arg));
4745 if (!config.consumerd32_lib_dir.value) {
4746 PERROR("strdup");
4747 ret = -ENOMEM;
4748 }
4749 }
4750 } else if (string_match(optname, "consumerd64-path")) {
4751 if (!arg || *arg == '\0') {
4752 ret = -EINVAL;
4753 goto end;
4754 }
4755 if (lttng_is_setuid_setgid()) {
4756 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4757 "--consumerd64-path");
4758 } else {
4759 config_string_set(&config.consumerd64_bin_path,
4760 strdup(arg));
4761 if (!config.consumerd64_bin_path.value) {
4762 PERROR("strdup");
4763 ret = -ENOMEM;
4764 }
4765 }
4766 } else if (string_match(optname, "consumerd64-libdir")) {
4767 if (!arg || *arg == '\0') {
4768 ret = -EINVAL;
4769 goto end;
4770 }
4771 if (lttng_is_setuid_setgid()) {
4772 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4773 "--consumerd64-libdir");
4774 } else {
4775 config_string_set(&config.consumerd64_lib_dir,
4776 strdup(arg));
4777 if (!config.consumerd64_lib_dir.value) {
4778 PERROR("strdup");
4779 ret = -ENOMEM;
4780 }
4781 }
4782 } else if (string_match(optname, "pidfile") || opt == 'p') {
4783 if (!arg || *arg == '\0') {
4784 ret = -EINVAL;
4785 goto end;
4786 }
4787 if (lttng_is_setuid_setgid()) {
4788 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4789 "-p, --pidfile");
4790 } else {
4791 config_string_set(&config.pid_file_path, strdup(arg));
4792 if (!config.pid_file_path.value) {
4793 PERROR("strdup");
4794 ret = -ENOMEM;
4795 }
4796 }
4797 } else if (string_match(optname, "agent-tcp-port")) {
4798 if (!arg || *arg == '\0') {
4799 ret = -EINVAL;
4800 goto end;
4801 }
4802 if (lttng_is_setuid_setgid()) {
4803 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4804 "--agent-tcp-port");
4805 } else {
4806 unsigned long v;
4807
4808 errno = 0;
4809 v = strtoul(arg, NULL, 0);
4810 if (errno != 0 || !isdigit(arg[0])) {
4811 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4812 return -1;
4813 }
4814 if (v == 0 || v >= 65535) {
4815 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4816 return -1;
4817 }
4818 config.agent_tcp_port.begin = config.agent_tcp_port.end = (int) v;
4819 DBG3("Agent TCP port set to non default: %i", (int) v);
4820 }
4821 } else if (string_match(optname, "load") || opt == 'l') {
4822 if (!arg || *arg == '\0') {
4823 ret = -EINVAL;
4824 goto end;
4825 }
4826 if (lttng_is_setuid_setgid()) {
4827 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4828 "-l, --load");
4829 } else {
4830 config_string_set(&config.load_session_path, strdup(arg));
4831 if (!config.load_session_path.value) {
4832 PERROR("strdup");
4833 ret = -ENOMEM;
4834 }
4835 }
4836 } else if (string_match(optname, "kmod-probes")) {
4837 if (!arg || *arg == '\0') {
4838 ret = -EINVAL;
4839 goto end;
4840 }
4841 if (lttng_is_setuid_setgid()) {
4842 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4843 "--kmod-probes");
4844 } else {
4845 config_string_set(&config.kmod_probes_list, strdup(arg));
4846 if (!config.kmod_probes_list.value) {
4847 PERROR("strdup");
4848 ret = -ENOMEM;
4849 }
4850 }
4851 } else if (string_match(optname, "extra-kmod-probes")) {
4852 if (!arg || *arg == '\0') {
4853 ret = -EINVAL;
4854 goto end;
4855 }
4856 if (lttng_is_setuid_setgid()) {
4857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4858 "--extra-kmod-probes");
4859 } else {
4860 config_string_set(&config.kmod_extra_probes_list,
4861 strdup(arg));
4862 if (!config.kmod_extra_probes_list.value) {
4863 PERROR("strdup");
4864 ret = -ENOMEM;
4865 }
4866 }
4867 } else if (string_match(optname, "config") || opt == 'f') {
4868 /* This is handled in set_options() thus silent skip. */
4869 goto end;
4870 } else {
4871 /* Unknown option or other error.
4872 * Error is printed by getopt, just return */
4873 ret = -1;
4874 }
4875
4876 end:
4877 if (ret == -EINVAL) {
4878 const char *opt_name = "unknown";
4879 int i;
4880
4881 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
4882 i++) {
4883 if (opt == long_options[i].val) {
4884 opt_name = long_options[i].name;
4885 break;
4886 }
4887 }
4888
4889 WARN("Invalid argument provided for option \"%s\", using default value.",
4890 opt_name);
4891 }
4892
4893 return ret;
4894 }
4895
4896 /*
4897 * config_entry_handler_cb used to handle options read from a config file.
4898 * See config_entry_handler_cb comment in common/config/session-config.h for the
4899 * return value conventions.
4900 */
4901 static int config_entry_handler(const struct config_entry *entry, void *unused)
4902 {
4903 int ret = 0, i;
4904
4905 if (!entry || !entry->name || !entry->value) {
4906 ret = -EINVAL;
4907 goto end;
4908 }
4909
4910 /* Check if the option is to be ignored */
4911 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4912 if (!strcmp(entry->name, config_ignore_options[i])) {
4913 goto end;
4914 }
4915 }
4916
4917 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4918 i++) {
4919
4920 /* Ignore if not fully matched. */
4921 if (strcmp(entry->name, long_options[i].name)) {
4922 continue;
4923 }
4924
4925 /*
4926 * If the option takes no argument on the command line, we have to
4927 * check if the value is "true". We support non-zero numeric values,
4928 * true, on and yes.
4929 */
4930 if (!long_options[i].has_arg) {
4931 ret = config_parse_value(entry->value);
4932 if (ret <= 0) {
4933 if (ret) {
4934 WARN("Invalid configuration value \"%s\" for option %s",
4935 entry->value, entry->name);
4936 }
4937 /* False, skip boolean config option. */
4938 goto end;
4939 }
4940 }
4941
4942 ret = set_option(long_options[i].val, entry->value, entry->name);
4943 goto end;
4944 }
4945
4946 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4947
4948 end:
4949 return ret;
4950 }
4951
4952 /*
4953 * daemon configuration loading and argument parsing
4954 */
4955 static int set_options(int argc, char **argv)
4956 {
4957 int ret = 0, c = 0, option_index = 0;
4958 int orig_optopt = optopt, orig_optind = optind;
4959 char *optstring;
4960 const char *config_path = NULL;
4961
4962 optstring = utils_generate_optstring(long_options,
4963 sizeof(long_options) / sizeof(struct option));
4964 if (!optstring) {
4965 ret = -ENOMEM;
4966 goto end;
4967 }
4968
4969 /* Check for the --config option */
4970 while ((c = getopt_long(argc, argv, optstring, long_options,
4971 &option_index)) != -1) {
4972 if (c == '?') {
4973 ret = -EINVAL;
4974 goto end;
4975 } else if (c != 'f') {
4976 /* if not equal to --config option. */
4977 continue;
4978 }
4979
4980 if (lttng_is_setuid_setgid()) {
4981 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4982 "-f, --config");
4983 } else {
4984 config_path = utils_expand_path(optarg);
4985 if (!config_path) {
4986 ERR("Failed to resolve path: %s", optarg);
4987 }
4988 }
4989 }
4990
4991 ret = config_get_section_entries(config_path, config_section_name,
4992 config_entry_handler, NULL);
4993 if (ret) {
4994 if (ret > 0) {
4995 ERR("Invalid configuration option at line %i", ret);
4996 ret = -1;
4997 }
4998 goto end;
4999 }
5000
5001 /* Reset getopt's global state */
5002 optopt = orig_optopt;
5003 optind = orig_optind;
5004 while (1) {
5005 option_index = -1;
5006 /*
5007 * getopt_long() will not set option_index if it encounters a
5008 * short option.
5009 */
5010 c = getopt_long(argc, argv, optstring, long_options,
5011 &option_index);
5012 if (c == -1) {
5013 break;
5014 }
5015
5016 /*
5017 * Pass NULL as the long option name if popt left the index
5018 * unset.
5019 */
5020 ret = set_option(c, optarg,
5021 option_index < 0 ? NULL :
5022 long_options[option_index].name);
5023 if (ret < 0) {
5024 break;
5025 }
5026 }
5027
5028 end:
5029 free(optstring);
5030 return ret;
5031 }
5032
5033 /*
5034 * Creates the two needed socket by the daemon.
5035 * apps_sock - The communication socket for all UST apps.
5036 * client_sock - The communication of the cli tool (lttng).
5037 */
5038 static int init_daemon_socket(void)
5039 {
5040 int ret = 0;
5041 mode_t old_umask;
5042
5043 old_umask = umask(0);
5044
5045 /* Create client tool unix socket */
5046 client_sock = lttcomm_create_unix_sock(config.client_unix_sock_path.value);
5047 if (client_sock < 0) {
5048 ERR("Create unix sock failed: %s", config.client_unix_sock_path.value);
5049 ret = -1;
5050 goto end;
5051 }
5052
5053 /* Set the cloexec flag */
5054 ret = utils_set_fd_cloexec(client_sock);
5055 if (ret < 0) {
5056 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5057 "Continuing but note that the consumer daemon will have a "
5058 "reference to this socket on exec()", client_sock);
5059 }
5060
5061 /* File permission MUST be 660 */
5062 ret = chmod(config.client_unix_sock_path.value, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5063 if (ret < 0) {
5064 ERR("Set file permissions failed: %s", config.client_unix_sock_path.value);
5065 PERROR("chmod");
5066 goto end;
5067 }
5068
5069 /* Create the application unix socket */
5070 apps_sock = lttcomm_create_unix_sock(config.apps_unix_sock_path.value);
5071 if (apps_sock < 0) {
5072 ERR("Create unix sock failed: %s", config.apps_unix_sock_path.value);
5073 ret = -1;
5074 goto end;
5075 }
5076
5077 /* Set the cloexec flag */
5078 ret = utils_set_fd_cloexec(apps_sock);
5079 if (ret < 0) {
5080 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5081 "Continuing but note that the consumer daemon will have a "
5082 "reference to this socket on exec()", apps_sock);
5083 }
5084
5085 /* File permission MUST be 666 */
5086 ret = chmod(config.apps_unix_sock_path.value,
5087 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
5088 if (ret < 0) {
5089 ERR("Set file permissions failed: %s", config.apps_unix_sock_path.value);
5090 PERROR("chmod");
5091 goto end;
5092 }
5093
5094 DBG3("Session daemon client socket %d and application socket %d created",
5095 client_sock, apps_sock);
5096
5097 end:
5098 umask(old_umask);
5099 return ret;
5100 }
5101
5102 /*
5103 * Create lockfile using the rundir and return its fd.
5104 */
5105 static int create_lockfile(void)
5106 {
5107 return utils_create_lock_file(config.lock_file_path.value);
5108 }
5109
5110 /*
5111 * Check if the global socket is available, and if a daemon is answering at the
5112 * other side. If yes, error is returned.
5113 *
5114 * Also attempts to create and hold the lock file.
5115 */
5116 static int check_existing_daemon(void)
5117 {
5118 int ret = 0;
5119
5120 /* Is there anybody out there ? */
5121 if (lttng_session_daemon_alive()) {
5122 ret = -EEXIST;
5123 goto end;
5124 }
5125
5126 lockfile_fd = create_lockfile();
5127 if (lockfile_fd < 0) {
5128 ret = -EEXIST;
5129 goto end;
5130 }
5131 end:
5132 return ret;
5133 }
5134
5135 static void sessiond_cleanup_lock_file(void)
5136 {
5137 int ret;
5138
5139 /*
5140 * Cleanup lock file by deleting it and finaly closing it which will
5141 * release the file system lock.
5142 */
5143 if (lockfile_fd >= 0) {
5144 ret = remove(config.lock_file_path.value);
5145 if (ret < 0) {
5146 PERROR("remove lock file");
5147 }
5148 ret = close(lockfile_fd);
5149 if (ret < 0) {
5150 PERROR("close lock file");
5151 }
5152 }
5153 }
5154
5155 /*
5156 * Set the tracing group gid onto the client socket.
5157 *
5158 * Race window between mkdir and chown is OK because we are going from more
5159 * permissive (root.root) to less permissive (root.tracing).
5160 */
5161 static int set_permissions(char *rundir)
5162 {
5163 int ret;
5164 gid_t gid;
5165
5166 gid = utils_get_group_id(config.tracing_group_name.value);
5167
5168 /* Set lttng run dir */
5169 ret = chown(rundir, 0, gid);
5170 if (ret < 0) {
5171 ERR("Unable to set group on %s", rundir);
5172 PERROR("chown");
5173 }
5174
5175 /*
5176 * Ensure all applications and tracing group can search the run
5177 * dir. Allow everyone to read the directory, since it does not
5178 * buy us anything to hide its content.
5179 */
5180 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
5181 if (ret < 0) {
5182 ERR("Unable to set permissions on %s", rundir);
5183 PERROR("chmod");
5184 }
5185
5186 /* lttng client socket path */
5187 ret = chown(config.client_unix_sock_path.value, 0, gid);
5188 if (ret < 0) {
5189 ERR("Unable to set group on %s", config.client_unix_sock_path.value);
5190 PERROR("chown");
5191 }
5192
5193 /* kconsumer error socket path */
5194 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
5195 if (ret < 0) {
5196 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
5197 PERROR("chown");
5198 }
5199
5200 /* 64-bit ustconsumer error socket path */
5201 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
5202 if (ret < 0) {
5203 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
5204 PERROR("chown");
5205 }
5206
5207 /* 32-bit ustconsumer compat32 error socket path */
5208 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
5209 if (ret < 0) {
5210 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
5211 PERROR("chown");
5212 }
5213
5214 DBG("All permissions are set");
5215
5216 return ret;
5217 }
5218
5219 /*
5220 * Create the lttng run directory needed for all global sockets and pipe.
5221 */
5222 static int create_lttng_rundir(void)
5223 {
5224 int ret;
5225
5226 DBG3("Creating LTTng run directory: %s", config.rundir.value);
5227
5228 ret = mkdir(config.rundir.value, S_IRWXU);
5229 if (ret < 0) {
5230 if (errno != EEXIST) {
5231 ERR("Unable to create %s", config.rundir.value);
5232 goto error;
5233 } else {
5234 ret = 0;
5235 }
5236 }
5237
5238 error:
5239 return ret;
5240 }
5241
5242 /*
5243 * Setup sockets and directory needed by the consumerds' communication with the
5244 * session daemon.
5245 */
5246 static int set_consumer_sockets(struct consumer_data *consumer_data)
5247 {
5248 int ret;
5249 char *path = NULL;
5250
5251 switch (consumer_data->type) {
5252 case LTTNG_CONSUMER_KERNEL:
5253 path = config.kconsumerd_path.value;
5254 break;
5255 case LTTNG_CONSUMER64_UST:
5256 path = config.consumerd64_path.value;
5257 break;
5258 case LTTNG_CONSUMER32_UST:
5259 path = config.consumerd32_path.value;
5260 break;
5261 default:
5262 ERR("Consumer type unknown");
5263 ret = -EINVAL;
5264 goto error;
5265 }
5266 assert(path);
5267
5268 DBG2("Creating consumer directory: %s", path);
5269
5270 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
5271 if (ret < 0 && errno != EEXIST) {
5272 PERROR("mkdir");
5273 ERR("Failed to create %s", path);
5274 goto error;
5275 }
5276 if (is_root) {
5277 ret = chown(path, 0, utils_get_group_id(config.tracing_group_name.value));
5278 if (ret < 0) {
5279 ERR("Unable to set group on %s", path);
5280 PERROR("chown");
5281 goto error;
5282 }
5283 }
5284
5285 /* Create the consumerd error unix socket */
5286 consumer_data->err_sock =
5287 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5288 if (consumer_data->err_sock < 0) {
5289 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
5290 ret = -1;
5291 goto error;
5292 }
5293
5294 /*
5295 * Set the CLOEXEC flag. Return code is useless because either way, the
5296 * show must go on.
5297 */
5298 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5299 if (ret < 0) {
5300 PERROR("utils_set_fd_cloexec");
5301 /* continue anyway */
5302 }
5303
5304 /* File permission MUST be 660 */
5305 ret = chmod(consumer_data->err_unix_sock_path,
5306 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5307 if (ret < 0) {
5308 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
5309 PERROR("chmod");
5310 goto error;
5311 }
5312
5313 error:
5314 return ret;
5315 }
5316
5317 /*
5318 * Signal handler for the daemon
5319 *
5320 * Simply stop all worker threads, leaving main() return gracefully after
5321 * joining all threads and calling cleanup().
5322 */
5323 static void sighandler(int sig)
5324 {
5325 switch (sig) {
5326 case SIGINT:
5327 DBG("SIGINT caught");
5328 stop_threads();
5329 break;
5330 case SIGTERM:
5331 DBG("SIGTERM caught");
5332 stop_threads();
5333 break;
5334 case SIGUSR1:
5335 CMM_STORE_SHARED(recv_child_signal, 1);
5336 break;
5337 default:
5338 break;
5339 }
5340 }
5341
5342 /*
5343 * Setup signal handler for :
5344 * SIGINT, SIGTERM, SIGPIPE
5345 */
5346 static int set_signal_handler(void)
5347 {
5348 int ret = 0;
5349 struct sigaction sa;
5350 sigset_t sigset;
5351
5352 if ((ret = sigemptyset(&sigset)) < 0) {
5353 PERROR("sigemptyset");
5354 return ret;
5355 }
5356
5357 sa.sa_mask = sigset;
5358 sa.sa_flags = 0;
5359
5360 sa.sa_handler = sighandler;
5361 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
5362 PERROR("sigaction");
5363 return ret;
5364 }
5365
5366 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
5367 PERROR("sigaction");
5368 return ret;
5369 }
5370
5371 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
5372 PERROR("sigaction");
5373 return ret;
5374 }
5375
5376 sa.sa_handler = SIG_IGN;
5377 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
5378 PERROR("sigaction");
5379 return ret;
5380 }
5381
5382 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5383
5384 return ret;
5385 }
5386
5387 /*
5388 * Set open files limit to unlimited. This daemon can open a large number of
5389 * file descriptors in order to consume multiple kernel traces.
5390 */
5391 static void set_ulimit(void)
5392 {
5393 int ret;
5394 struct rlimit lim;
5395
5396 /* The kernel does not allow an infinite limit for open files */
5397 lim.rlim_cur = 65535;
5398 lim.rlim_max = 65535;
5399
5400 ret = setrlimit(RLIMIT_NOFILE, &lim);
5401 if (ret < 0) {
5402 PERROR("failed to set open files limit");
5403 }
5404 }
5405
5406 static int write_pidfile(void)
5407 {
5408 return utils_create_pid_file(getpid(), config.pid_file_path.value);
5409 }
5410
5411 static int set_clock_plugin_env(void)
5412 {
5413 int ret = 0;
5414 char *env_value = NULL;
5415
5416 if (!config.lttng_ust_clock_plugin.value) {
5417 goto end;
5418 }
5419
5420 ret = asprintf(&env_value, "LTTNG_UST_CLOCK_PLUGIN=%s",
5421 config.lttng_ust_clock_plugin.value);
5422 if (ret < 0) {
5423 PERROR("asprintf");
5424 goto end;
5425 }
5426
5427 ret = putenv(env_value);
5428 if (ret) {
5429 free(env_value);
5430 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5431 goto end;
5432 }
5433
5434 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5435 config.lttng_ust_clock_plugin.value);
5436 end:
5437 return ret;
5438 }
5439
5440 static void destroy_all_sessions_and_wait(void)
5441 {
5442 struct ltt_session *session, *tmp;
5443 struct ltt_session_list *session_list;
5444
5445 session_list = session_get_list();
5446 DBG("Initiating destruction of all sessions");
5447
5448 if (!session_list) {
5449 return;
5450 }
5451
5452 /*
5453 * Ensure that the client thread is no longer accepting new commands,
5454 * which could cause new sessions to be created.
5455 */
5456 sessiond_wait_client_thread_stopped();
5457
5458 session_lock_list();
5459 /* Initiate the destruction of all sessions. */
5460 cds_list_for_each_entry_safe(session, tmp,
5461 &session_list->head, list) {
5462 if (!session_get(session)) {
5463 continue;
5464 }
5465
5466 session_lock(session);
5467 if (session->destroyed) {
5468 goto unlock_session;
5469 }
5470 (void) cmd_destroy_session(session,
5471 notification_thread_handle);
5472 unlock_session:
5473 session_unlock(session);
5474 session_put(session);
5475 }
5476 session_unlock_list();
5477
5478 /* Wait for the destruction of all sessions to complete. */
5479 DBG("Waiting for the destruction of all sessions to complete");
5480 session_list_wait_empty();
5481 DBG("Destruction of all sessions completed");
5482 }
5483
5484 /*
5485 * main
5486 */
5487 int main(int argc, char **argv)
5488 {
5489 int ret = 0, retval = 0;
5490 void *status;
5491 const char *env_app_timeout;
5492 struct lttng_pipe *ust32_channel_monitor_pipe = NULL,
5493 *ust64_channel_monitor_pipe = NULL,
5494 *kernel_channel_monitor_pipe = NULL;
5495 bool timer_thread_launched = false;
5496 struct lttng_thread *ht_cleanup_thread = NULL;
5497 struct timer_thread_parameters timer_thread_ctx;
5498 /* Rotation thread handle. */
5499 struct rotation_thread_handle *rotation_thread_handle = NULL;
5500 /* Queue of rotation jobs populated by the sessiond-timer. */
5501 struct rotation_thread_timer_queue *rotation_timer_queue = NULL;
5502
5503 init_kernel_workarounds();
5504
5505 rcu_register_thread();
5506
5507 if (set_signal_handler()) {
5508 retval = -1;
5509 goto exit_set_signal_handler;
5510 }
5511
5512 if (timer_signal_init()) {
5513 retval = -1;
5514 goto exit_set_signal_handler;
5515 }
5516
5517 page_size = sysconf(_SC_PAGESIZE);
5518 if (page_size < 0) {
5519 PERROR("sysconf _SC_PAGESIZE");
5520 page_size = LONG_MAX;
5521 WARN("Fallback page size to %ld", page_size);
5522 }
5523
5524 ret = sessiond_config_init(&config);
5525 if (ret) {
5526 retval = -1;
5527 goto exit_set_signal_handler;
5528 }
5529
5530 /*
5531 * Init config from environment variables.
5532 * Command line option override env configuration per-doc. Do env first.
5533 */
5534 sessiond_config_apply_env_config(&config);
5535
5536 /*
5537 * Parse arguments and load the daemon configuration file.
5538 *
5539 * We have an exit_options exit path to free memory reserved by
5540 * set_options. This is needed because the rest of sessiond_cleanup()
5541 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5542 * depends on set_options.
5543 */
5544 progname = argv[0];
5545 if (set_options(argc, argv)) {
5546 retval = -1;
5547 goto exit_options;
5548 }
5549
5550 /*
5551 * Resolve all paths received as arguments, configuration option, or
5552 * through environment variable as absolute paths. This is necessary
5553 * since daemonizing causes the sessiond's current working directory
5554 * to '/'.
5555 */
5556 ret = sessiond_config_resolve_paths(&config);
5557 if (ret) {
5558 goto exit_options;
5559 }
5560
5561 /* Apply config. */
5562 lttng_opt_verbose = config.verbose;
5563 lttng_opt_quiet = config.quiet;
5564 kconsumer_data.err_unix_sock_path =
5565 config.kconsumerd_err_unix_sock_path.value;
5566 kconsumer_data.cmd_unix_sock_path =
5567 config.kconsumerd_cmd_unix_sock_path.value;
5568 ustconsumer32_data.err_unix_sock_path =
5569 config.consumerd32_err_unix_sock_path.value;
5570 ustconsumer32_data.cmd_unix_sock_path =
5571 config.consumerd32_cmd_unix_sock_path.value;
5572 ustconsumer64_data.err_unix_sock_path =
5573 config.consumerd64_err_unix_sock_path.value;
5574 ustconsumer64_data.cmd_unix_sock_path =
5575 config.consumerd64_cmd_unix_sock_path.value;
5576 set_clock_plugin_env();
5577
5578 sessiond_config_log(&config);
5579
5580 if (create_lttng_rundir()) {
5581 retval = -1;
5582 goto exit_options;
5583 }
5584
5585 /* Abort launch if a session daemon is already running. */
5586 if (check_existing_daemon()) {
5587 ERR("A session daemon is already running.");
5588 retval = -1;
5589 goto exit_options;
5590 }
5591
5592 /* Daemonize */
5593 if (config.daemonize || config.background) {
5594 int i;
5595
5596 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
5597 !config.background);
5598 if (ret < 0) {
5599 retval = -1;
5600 goto exit_options;
5601 }
5602
5603 /*
5604 * We are in the child. Make sure all other file descriptors are
5605 * closed, in case we are called with more opened file
5606 * descriptors than the standard ones and the lock file.
5607 */
5608 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5609 if (i == lockfile_fd) {
5610 continue;
5611 }
5612 (void) close(i);
5613 }
5614 }
5615
5616 if (run_as_create_worker(argv[0]) < 0) {
5617 goto exit_create_run_as_worker_cleanup;
5618 }
5619
5620 /*
5621 * Starting from here, we can create threads. This needs to be after
5622 * lttng_daemonize due to RCU.
5623 */
5624
5625 /*
5626 * Initialize the health check subsystem. This call should set the
5627 * appropriate time values.
5628 */
5629 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5630 if (!health_sessiond) {
5631 PERROR("health_app_create error");
5632 retval = -1;
5633 goto exit_health_sessiond_cleanup;
5634 }
5635
5636 /* Create thread to clean up RCU hash tables */
5637 ht_cleanup_thread = launch_ht_cleanup_thread();
5638 if (!ht_cleanup_thread) {
5639 retval = -1;
5640 goto exit_ht_cleanup;
5641 }
5642
5643 /* Create thread quit pipe */
5644 if (sessiond_init_thread_quit_pipe()) {
5645 retval = -1;
5646 goto exit_init_data;
5647 }
5648
5649 /* Check if daemon is UID = 0 */
5650 is_root = !getuid();
5651 if (is_root) {
5652 /* Create global run dir with root access */
5653
5654 kernel_channel_monitor_pipe = lttng_pipe_open(0);
5655 if (!kernel_channel_monitor_pipe) {
5656 ERR("Failed to create kernel consumer channel monitor pipe");
5657 retval = -1;
5658 goto exit_init_data;
5659 }
5660 kconsumer_data.channel_monitor_pipe =
5661 lttng_pipe_release_writefd(
5662 kernel_channel_monitor_pipe);
5663 if (kconsumer_data.channel_monitor_pipe < 0) {
5664 retval = -1;
5665 goto exit_init_data;
5666 }
5667 }
5668
5669 /* Set consumer initial state */
5670 kernel_consumerd_state = CONSUMER_STOPPED;
5671 ust_consumerd_state = CONSUMER_STOPPED;
5672
5673 ust32_channel_monitor_pipe = lttng_pipe_open(0);
5674 if (!ust32_channel_monitor_pipe) {
5675 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5676 retval = -1;
5677 goto exit_init_data;
5678 }
5679 ustconsumer32_data.channel_monitor_pipe = lttng_pipe_release_writefd(
5680 ust32_channel_monitor_pipe);
5681 if (ustconsumer32_data.channel_monitor_pipe < 0) {
5682 retval = -1;
5683 goto exit_init_data;
5684 }
5685
5686 /*
5687 * The rotation_thread_timer_queue structure is shared between the
5688 * sessiond timer thread and the rotation thread. The main thread keeps
5689 * its ownership and destroys it when both threads have been joined.
5690 */
5691 rotation_timer_queue = rotation_thread_timer_queue_create();
5692 if (!rotation_timer_queue) {
5693 retval = -1;
5694 goto exit_init_data;
5695 }
5696 timer_thread_ctx.rotation_thread_job_queue = rotation_timer_queue;
5697
5698 ust64_channel_monitor_pipe = lttng_pipe_open(0);
5699 if (!ust64_channel_monitor_pipe) {
5700 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5701 retval = -1;
5702 goto exit_init_data;
5703 }
5704 ustconsumer64_data.channel_monitor_pipe = lttng_pipe_release_writefd(
5705 ust64_channel_monitor_pipe);
5706 if (ustconsumer64_data.channel_monitor_pipe < 0) {
5707 retval = -1;
5708 goto exit_init_data;
5709 }
5710
5711 /*
5712 * Init UST app hash table. Alloc hash table before this point since
5713 * cleanup() can get called after that point.
5714 */
5715 if (ust_app_ht_alloc()) {
5716 ERR("Failed to allocate UST app hash table");
5717 retval = -1;
5718 goto exit_init_data;
5719 }
5720
5721 /*
5722 * Initialize agent app hash table. We allocate the hash table here
5723 * since cleanup() can get called after this point.
5724 */
5725 if (agent_app_ht_alloc()) {
5726 ERR("Failed to allocate Agent app hash table");
5727 retval = -1;
5728 goto exit_init_data;
5729 }
5730
5731 /*
5732 * These actions must be executed as root. We do that *after* setting up
5733 * the sockets path because we MUST make the check for another daemon using
5734 * those paths *before* trying to set the kernel consumer sockets and init
5735 * kernel tracer.
5736 */
5737 if (is_root) {
5738 if (set_consumer_sockets(&kconsumer_data)) {
5739 retval = -1;
5740 goto exit_init_data;
5741 }
5742
5743 /* Setup kernel tracer */
5744 if (!config.no_kernel) {
5745 init_kernel_tracer();
5746 if (kernel_tracer_fd >= 0) {
5747 ret = syscall_init_table();
5748 if (ret < 0) {
5749 ERR("Unable to populate syscall table. "
5750 "Syscall tracing won't work "
5751 "for this session daemon.");
5752 }
5753 }
5754 }
5755
5756 /* Set ulimit for open files */
5757 set_ulimit();
5758 }
5759 /* init lttng_fd tracking must be done after set_ulimit. */
5760 lttng_fd_init();
5761
5762 if (set_consumer_sockets(&ustconsumer64_data)) {
5763 retval = -1;
5764 goto exit_init_data;
5765 }
5766
5767 if (set_consumer_sockets(&ustconsumer32_data)) {
5768 retval = -1;
5769 goto exit_init_data;
5770 }
5771
5772 /* Setup the needed unix socket */
5773 if (init_daemon_socket()) {
5774 retval = -1;
5775 goto exit_init_data;
5776 }
5777
5778 /* Set credentials to socket */
5779 if (is_root && set_permissions(config.rundir.value)) {
5780 retval = -1;
5781 goto exit_init_data;
5782 }
5783
5784 /* Get parent pid if -S, --sig-parent is specified. */
5785 if (config.sig_parent) {
5786 ppid = getppid();
5787 }
5788
5789 /* Setup the kernel pipe for waking up the kernel thread */
5790 if (is_root && !config.no_kernel) {
5791 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
5792 retval = -1;
5793 goto exit_init_data;
5794 }
5795 }
5796
5797 /* Setup the thread apps communication pipe. */
5798 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
5799 retval = -1;
5800 goto exit_init_data;
5801 }
5802
5803 /* Setup the thread apps notify communication pipe. */
5804 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
5805 retval = -1;
5806 goto exit_init_data;
5807 }
5808
5809 /* Initialize global buffer per UID and PID registry. */
5810 buffer_reg_init_uid_registry();
5811 buffer_reg_init_pid_registry();
5812
5813 /* Init UST command queue. */
5814 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
5815
5816 cmd_init();
5817
5818 /* Check for the application socket timeout env variable. */
5819 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5820 if (env_app_timeout) {
5821 config.app_socket_timeout = atoi(env_app_timeout);
5822 } else {
5823 config.app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5824 }
5825
5826 ret = write_pidfile();
5827 if (ret) {
5828 ERR("Error in write_pidfile");
5829 retval = -1;
5830 goto exit_init_data;
5831 }
5832
5833 /* Initialize communication library */
5834 lttcomm_init();
5835 /* Initialize TCP timeout values */
5836 lttcomm_inet_init();
5837
5838 if (load_session_init_data(&load_info) < 0) {
5839 retval = -1;
5840 goto exit_init_data;
5841 }
5842 load_info->path = config.load_session_path.value;
5843
5844 /* Create health-check thread. */
5845 if (!launch_health_management_thread()) {
5846 retval = -1;
5847 goto exit_health;
5848 }
5849
5850 /* notification_thread_data acquires the pipes' read side. */
5851 notification_thread_handle = notification_thread_handle_create(
5852 ust32_channel_monitor_pipe,
5853 ust64_channel_monitor_pipe,
5854 kernel_channel_monitor_pipe);
5855 if (!notification_thread_handle) {
5856 retval = -1;
5857 ERR("Failed to create notification thread shared data");
5858 goto exit_notification;
5859 }
5860
5861 /* Create notification thread. */
5862 if (!launch_notification_thread(notification_thread_handle)) {
5863 retval = -1;
5864 goto exit_notification;
5865
5866 }
5867
5868 /* Create timer thread. */
5869 ret = pthread_create(&timer_thread, default_pthread_attr(),
5870 timer_thread_func, &timer_thread_ctx);
5871 if (ret) {
5872 errno = ret;
5873 PERROR("pthread_create timer");
5874 retval = -1;
5875 stop_threads();
5876 goto exit_notification;
5877 }
5878 timer_thread_launched = true;
5879
5880 /* rotation_thread_data acquires the pipes' read side. */
5881 rotation_thread_handle = rotation_thread_handle_create(
5882 rotation_timer_queue,
5883 notification_thread_handle);
5884 if (!rotation_thread_handle) {
5885 retval = -1;
5886 ERR("Failed to create rotation thread shared data");
5887 stop_threads();
5888 goto exit_rotation;
5889 }
5890
5891 /* Create rotation thread. */
5892 if (!launch_rotation_thread(rotation_thread_handle)) {
5893 retval = -1;
5894 goto exit_rotation;
5895 }
5896
5897 /* Create thread to manage the client socket */
5898 ret = pthread_create(&client_thread, default_pthread_attr(),
5899 thread_manage_clients, (void *) NULL);
5900 if (ret) {
5901 errno = ret;
5902 PERROR("pthread_create clients");
5903 retval = -1;
5904 stop_threads();
5905 goto exit_client;
5906 }
5907
5908 /* Create thread to dispatch registration */
5909 ret = pthread_create(&dispatch_thread, default_pthread_attr(),
5910 thread_dispatch_ust_registration, (void *) NULL);
5911 if (ret) {
5912 errno = ret;
5913 PERROR("pthread_create dispatch");
5914 retval = -1;
5915 stop_threads();
5916 goto exit_dispatch;
5917 }
5918
5919 /* Create thread to manage application registration. */
5920 ret = pthread_create(&reg_apps_thread, default_pthread_attr(),
5921 thread_registration_apps, (void *) NULL);
5922 if (ret) {
5923 errno = ret;
5924 PERROR("pthread_create registration");
5925 retval = -1;
5926 stop_threads();
5927 goto exit_reg_apps;
5928 }
5929
5930 /* Create thread to manage application socket */
5931 ret = pthread_create(&apps_thread, default_pthread_attr(),
5932 thread_manage_apps, (void *) NULL);
5933 if (ret) {
5934 errno = ret;
5935 PERROR("pthread_create apps");
5936 retval = -1;
5937 stop_threads();
5938 goto exit_apps;
5939 }
5940
5941 /* Create thread to manage application notify socket */
5942 ret = pthread_create(&apps_notify_thread, default_pthread_attr(),
5943 ust_thread_manage_notify, (void *) NULL);
5944 if (ret) {
5945 errno = ret;
5946 PERROR("pthread_create notify");
5947 retval = -1;
5948 stop_threads();
5949 goto exit_apps_notify;
5950 }
5951
5952 /* Create agent registration thread. */
5953 ret = pthread_create(&agent_reg_thread, default_pthread_attr(),
5954 agent_thread_manage_registration, (void *) NULL);
5955 if (ret) {
5956 errno = ret;
5957 PERROR("pthread_create agent");
5958 retval = -1;
5959 stop_threads();
5960 goto exit_agent_reg;
5961 }
5962
5963 /* Don't start this thread if kernel tracing is not requested nor root */
5964 if (is_root && !config.no_kernel) {
5965 /* Create kernel thread to manage kernel event */
5966 ret = pthread_create(&kernel_thread, default_pthread_attr(),
5967 thread_manage_kernel, (void *) NULL);
5968 if (ret) {
5969 errno = ret;
5970 PERROR("pthread_create kernel");
5971 retval = -1;
5972 stop_threads();
5973 goto exit_kernel;
5974 }
5975 }
5976
5977 /* Create session loading thread. */
5978 ret = pthread_create(&load_session_thread, default_pthread_attr(),
5979 thread_load_session, load_info);
5980 if (ret) {
5981 errno = ret;
5982 PERROR("pthread_create load_session_thread");
5983 retval = -1;
5984 stop_threads();
5985 goto exit_load_session;
5986 }
5987
5988 /*
5989 * This is where we start awaiting program completion (e.g. through
5990 * signal that asks threads to teardown).
5991 */
5992
5993 ret = pthread_join(load_session_thread, &status);
5994 if (ret) {
5995 errno = ret;
5996 PERROR("pthread_join load_session_thread");
5997 retval = -1;
5998 }
5999
6000 /* Initiate teardown once activity occurs on the quit pipe. */
6001 sessiond_wait_for_quit_pipe(-1U);
6002 destroy_all_sessions_and_wait();
6003 exit_load_session:
6004
6005 if (is_root && !config.no_kernel) {
6006 ret = pthread_join(kernel_thread, &status);
6007 if (ret) {
6008 errno = ret;
6009 PERROR("pthread_join");
6010 retval = -1;
6011 }
6012 }
6013 exit_kernel:
6014
6015 ret = pthread_join(agent_reg_thread, &status);
6016 if (ret) {
6017 errno = ret;
6018 PERROR("pthread_join agent");
6019 retval = -1;
6020 }
6021 exit_agent_reg:
6022
6023 ret = pthread_join(apps_notify_thread, &status);
6024 if (ret) {
6025 errno = ret;
6026 PERROR("pthread_join apps notify");
6027 retval = -1;
6028 }
6029 exit_apps_notify:
6030
6031 ret = pthread_join(apps_thread, &status);
6032 if (ret) {
6033 errno = ret;
6034 PERROR("pthread_join apps");
6035 retval = -1;
6036 }
6037 exit_apps:
6038
6039 ret = pthread_join(reg_apps_thread, &status);
6040 if (ret) {
6041 errno = ret;
6042 PERROR("pthread_join");
6043 retval = -1;
6044 }
6045 exit_reg_apps:
6046
6047 /*
6048 * Join dispatch thread after joining reg_apps_thread to ensure
6049 * we don't leak applications in the queue.
6050 */
6051 ret = pthread_join(dispatch_thread, &status);
6052 if (ret) {
6053 errno = ret;
6054 PERROR("pthread_join");
6055 retval = -1;
6056 }
6057 exit_dispatch:
6058
6059 ret = pthread_join(client_thread, &status);
6060 if (ret) {
6061 errno = ret;
6062 PERROR("pthread_join");
6063 retval = -1;
6064 }
6065
6066 exit_client:
6067 exit_rotation:
6068 exit_notification:
6069 lttng_thread_list_shutdown_orphans();
6070 exit_health:
6071 exit_init_data:
6072 /*
6073 * Wait for all pending call_rcu work to complete before tearing
6074 * down data structures. call_rcu worker may be trying to
6075 * perform lookups in those structures.
6076 */
6077 rcu_barrier();
6078 /*
6079 * sessiond_cleanup() is called when no other thread is running, except
6080 * the ht_cleanup thread, which is needed to destroy the hash tables.
6081 */
6082 rcu_thread_online();
6083 sessiond_cleanup();
6084
6085 /*
6086 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6087 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6088 * the queue is empty before shutting down the clean-up thread.
6089 */
6090 rcu_barrier();
6091
6092 if (timer_thread_launched) {
6093 timer_exit();
6094 ret = pthread_join(timer_thread, &status);
6095 if (ret) {
6096 errno = ret;
6097 PERROR("pthread_join timer thread");
6098 retval = -1;
6099 }
6100 }
6101
6102 if (ht_cleanup_thread) {
6103 lttng_thread_shutdown(ht_cleanup_thread);
6104 lttng_thread_put(ht_cleanup_thread);
6105 }
6106
6107 rcu_thread_offline();
6108 rcu_unregister_thread();
6109
6110 if (rotation_thread_handle) {
6111 rotation_thread_handle_destroy(rotation_thread_handle);
6112 }
6113
6114 /*
6115 * After the rotation and timer thread have quit, we can safely destroy
6116 * the rotation_timer_queue.
6117 */
6118 rotation_thread_timer_queue_destroy(rotation_timer_queue);
6119 /*
6120 * The teardown of the notification system is performed after the
6121 * session daemon's teardown in order to allow it to be notified
6122 * of the active session and channels at the moment of the teardown.
6123 */
6124 if (notification_thread_handle) {
6125 notification_thread_handle_destroy(notification_thread_handle);
6126 }
6127 lttng_pipe_destroy(ust32_channel_monitor_pipe);
6128 lttng_pipe_destroy(ust64_channel_monitor_pipe);
6129 lttng_pipe_destroy(kernel_channel_monitor_pipe);
6130 exit_ht_cleanup:
6131
6132 health_app_destroy(health_sessiond);
6133 exit_health_sessiond_cleanup:
6134 exit_create_run_as_worker_cleanup:
6135
6136 exit_options:
6137 sessiond_cleanup_lock_file();
6138 sessiond_cleanup_options();
6139
6140 exit_set_signal_handler:
6141 if (!retval) {
6142 exit(EXIT_SUCCESS);
6143 } else {
6144 exit(EXIT_FAILURE);
6145 }
6146 }
This page took 0.192537 seconds and 5 git commands to generate.