Comment why we cannot rmdir the lttng and relayd rundir
[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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <getopt.h>
21 #include <grp.h>
22 #include <limits.h>
23 #include <pthread.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <inttypes.h>
29 #include <sys/mman.h>
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <urcu/uatomic.h>
37 #include <unistd.h>
38 #include <config.h>
39
40 #include <common/common.h>
41 #include <common/compat/socket.h>
42 #include <common/defaults.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/futex.h>
45 #include <common/relayd/relayd.h>
46 #include <common/utils.h>
47
48 #include "lttng-sessiond.h"
49 #include "buffer-registry.h"
50 #include "channel.h"
51 #include "cmd.h"
52 #include "consumer.h"
53 #include "context.h"
54 #include "event.h"
55 #include "kernel.h"
56 #include "kernel-consumer.h"
57 #include "modprobe.h"
58 #include "shm.h"
59 #include "ust-ctl.h"
60 #include "ust-consumer.h"
61 #include "utils.h"
62 #include "fd-limit.h"
63 #include "health-sessiond.h"
64 #include "testpoint.h"
65 #include "ust-thread.h"
66 #include "jul-thread.h"
67
68 #define CONSUMERD_FILE "lttng-consumerd"
69
70 const char *progname;
71 static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
72 static const char *opt_pidfile;
73 static int opt_sig_parent;
74 static int opt_verbose_consumer;
75 static int opt_daemon;
76 static int opt_no_kernel;
77 static int is_root; /* Set to 1 if the daemon is running as root */
78 static pid_t ppid; /* Parent PID for --sig-parent option */
79 static char *rundir;
80
81 /*
82 * Consumer daemon specific control data. Every value not initialized here is
83 * set to 0 by the static definition.
84 */
85 static struct consumer_data kconsumer_data = {
86 .type = LTTNG_CONSUMER_KERNEL,
87 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
88 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
89 .err_sock = -1,
90 .cmd_sock = -1,
91 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
92 .lock = PTHREAD_MUTEX_INITIALIZER,
93 .cond = PTHREAD_COND_INITIALIZER,
94 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
95 };
96 static struct consumer_data ustconsumer64_data = {
97 .type = LTTNG_CONSUMER64_UST,
98 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
99 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
100 .err_sock = -1,
101 .cmd_sock = -1,
102 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
103 .lock = PTHREAD_MUTEX_INITIALIZER,
104 .cond = PTHREAD_COND_INITIALIZER,
105 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
106 };
107 static struct consumer_data ustconsumer32_data = {
108 .type = LTTNG_CONSUMER32_UST,
109 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
110 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
111 .err_sock = -1,
112 .cmd_sock = -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
119 /* Shared between threads */
120 static int dispatch_thread_exit;
121
122 /* Global application Unix socket path */
123 static char apps_unix_sock_path[PATH_MAX];
124 /* Global client Unix socket path */
125 static char client_unix_sock_path[PATH_MAX];
126 /* global wait shm path for UST */
127 static char wait_shm_path[PATH_MAX];
128 /* Global health check unix path */
129 static char health_unix_sock_path[PATH_MAX];
130
131 /* Sockets and FDs */
132 static int client_sock = -1;
133 static int apps_sock = -1;
134 int kernel_tracer_fd = -1;
135 static int kernel_poll_pipe[2] = { -1, -1 };
136
137 /*
138 * Quit pipe for all threads. This permits a single cancellation point
139 * for all threads when receiving an event on the pipe.
140 */
141 static int thread_quit_pipe[2] = { -1, -1 };
142
143 /*
144 * This pipe is used to inform the thread managing application communication
145 * that a command is queued and ready to be processed.
146 */
147 static int apps_cmd_pipe[2] = { -1, -1 };
148
149 int apps_cmd_notify_pipe[2] = { -1, -1 };
150
151 /* Pthread, Mutexes and Semaphores */
152 static pthread_t apps_thread;
153 static pthread_t apps_notify_thread;
154 static pthread_t reg_apps_thread;
155 static pthread_t client_thread;
156 static pthread_t kernel_thread;
157 static pthread_t dispatch_thread;
158 static pthread_t health_thread;
159 static pthread_t ht_cleanup_thread;
160 static pthread_t jul_reg_thread;
161
162 /*
163 * UST registration command queue. This queue is tied with a futex and uses a N
164 * wakers / 1 waiter implemented and detailed in futex.c/.h
165 *
166 * The thread_manage_apps and thread_dispatch_ust_registration interact with
167 * this queue and the wait/wake scheme.
168 */
169 static struct ust_cmd_queue ust_cmd_queue;
170
171 /*
172 * Pointer initialized before thread creation.
173 *
174 * This points to the tracing session list containing the session count and a
175 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
176 * MUST NOT be taken if you call a public function in session.c.
177 *
178 * The lock is nested inside the structure: session_list_ptr->lock. Please use
179 * session_lock_list and session_unlock_list for lock acquisition.
180 */
181 static struct ltt_session_list *session_list_ptr;
182
183 int ust_consumerd64_fd = -1;
184 int ust_consumerd32_fd = -1;
185
186 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
187 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
188 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
189 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
190
191 static const char *module_proc_lttng = "/proc/lttng";
192
193 /*
194 * Consumer daemon state which is changed when spawning it, killing it or in
195 * case of a fatal error.
196 */
197 enum consumerd_state {
198 CONSUMER_STARTED = 1,
199 CONSUMER_STOPPED = 2,
200 CONSUMER_ERROR = 3,
201 };
202
203 /*
204 * This consumer daemon state is used to validate if a client command will be
205 * able to reach the consumer. If not, the client is informed. For instance,
206 * doing a "lttng start" when the consumer state is set to ERROR will return an
207 * error to the client.
208 *
209 * The following example shows a possible race condition of this scheme:
210 *
211 * consumer thread error happens
212 * client cmd arrives
213 * client cmd checks state -> still OK
214 * consumer thread exit, sets error
215 * client cmd try to talk to consumer
216 * ...
217 *
218 * However, since the consumer is a different daemon, we have no way of making
219 * sure the command will reach it safely even with this state flag. This is why
220 * we consider that up to the state validation during command processing, the
221 * command is safe. After that, we can not guarantee the correctness of the
222 * client request vis-a-vis the consumer.
223 */
224 static enum consumerd_state ust_consumerd_state;
225 static enum consumerd_state kernel_consumerd_state;
226
227 /*
228 * Socket timeout for receiving and sending in seconds.
229 */
230 static int app_socket_timeout;
231
232 /* Set in main() with the current page size. */
233 long page_size;
234
235 /* Application health monitoring */
236 struct health_app *health_sessiond;
237
238 /* JUL TCP port for registration. Used by the JUL thread. */
239 unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT;
240
241 static
242 void setup_consumerd_path(void)
243 {
244 const char *bin, *libdir;
245
246 /*
247 * Allow INSTALL_BIN_PATH to be used as a target path for the
248 * native architecture size consumer if CONFIG_CONSUMER*_PATH
249 * has not been defined.
250 */
251 #if (CAA_BITS_PER_LONG == 32)
252 if (!consumerd32_bin[0]) {
253 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
254 }
255 if (!consumerd32_libdir[0]) {
256 consumerd32_libdir = INSTALL_LIB_PATH;
257 }
258 #elif (CAA_BITS_PER_LONG == 64)
259 if (!consumerd64_bin[0]) {
260 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
261 }
262 if (!consumerd64_libdir[0]) {
263 consumerd64_libdir = INSTALL_LIB_PATH;
264 }
265 #else
266 #error "Unknown bitness"
267 #endif
268
269 /*
270 * runtime env. var. overrides the build default.
271 */
272 bin = getenv("LTTNG_CONSUMERD32_BIN");
273 if (bin) {
274 consumerd32_bin = bin;
275 }
276 bin = getenv("LTTNG_CONSUMERD64_BIN");
277 if (bin) {
278 consumerd64_bin = bin;
279 }
280 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
281 if (libdir) {
282 consumerd32_libdir = libdir;
283 }
284 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
285 if (libdir) {
286 consumerd64_libdir = libdir;
287 }
288 }
289
290 /*
291 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
292 */
293 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
294 {
295 int ret;
296
297 assert(events);
298
299 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
300 if (ret < 0) {
301 goto error;
302 }
303
304 /* Add quit pipe */
305 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
306 if (ret < 0) {
307 goto error;
308 }
309
310 return 0;
311
312 error:
313 return ret;
314 }
315
316 /*
317 * Check if the thread quit pipe was triggered.
318 *
319 * Return 1 if it was triggered else 0;
320 */
321 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
322 {
323 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
324 return 1;
325 }
326
327 return 0;
328 }
329
330 /*
331 * Init thread quit pipe.
332 *
333 * Return -1 on error or 0 if all pipes are created.
334 */
335 static int init_thread_quit_pipe(void)
336 {
337 int ret, i;
338
339 ret = pipe(thread_quit_pipe);
340 if (ret < 0) {
341 PERROR("thread quit pipe");
342 goto error;
343 }
344
345 for (i = 0; i < 2; i++) {
346 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
347 if (ret < 0) {
348 PERROR("fcntl");
349 goto error;
350 }
351 }
352
353 error:
354 return ret;
355 }
356
357 /*
358 * Stop all threads by closing the thread quit pipe.
359 */
360 static void stop_threads(void)
361 {
362 int ret;
363
364 /* Stopping all threads */
365 DBG("Terminating all threads");
366 ret = notify_thread_pipe(thread_quit_pipe[1]);
367 if (ret < 0) {
368 ERR("write error on thread quit pipe");
369 }
370
371 /* Dispatch thread */
372 CMM_STORE_SHARED(dispatch_thread_exit, 1);
373 futex_nto1_wake(&ust_cmd_queue.futex);
374 }
375
376 /*
377 * Close every consumer sockets.
378 */
379 static void close_consumer_sockets(void)
380 {
381 int ret;
382
383 if (kconsumer_data.err_sock >= 0) {
384 ret = close(kconsumer_data.err_sock);
385 if (ret < 0) {
386 PERROR("kernel consumer err_sock close");
387 }
388 }
389 if (ustconsumer32_data.err_sock >= 0) {
390 ret = close(ustconsumer32_data.err_sock);
391 if (ret < 0) {
392 PERROR("UST consumerd32 err_sock close");
393 }
394 }
395 if (ustconsumer64_data.err_sock >= 0) {
396 ret = close(ustconsumer64_data.err_sock);
397 if (ret < 0) {
398 PERROR("UST consumerd64 err_sock close");
399 }
400 }
401 if (kconsumer_data.cmd_sock >= 0) {
402 ret = close(kconsumer_data.cmd_sock);
403 if (ret < 0) {
404 PERROR("kernel consumer cmd_sock close");
405 }
406 }
407 if (ustconsumer32_data.cmd_sock >= 0) {
408 ret = close(ustconsumer32_data.cmd_sock);
409 if (ret < 0) {
410 PERROR("UST consumerd32 cmd_sock close");
411 }
412 }
413 if (ustconsumer64_data.cmd_sock >= 0) {
414 ret = close(ustconsumer64_data.cmd_sock);
415 if (ret < 0) {
416 PERROR("UST consumerd64 cmd_sock close");
417 }
418 }
419 }
420
421 /*
422 * Cleanup the daemon
423 */
424 static void cleanup(void)
425 {
426 int ret;
427 struct ltt_session *sess, *stmp;
428 char path[PATH_MAX];
429
430 DBG("Cleaning up");
431
432 /*
433 * Close the thread quit pipe. It has already done its job,
434 * since we are now called.
435 */
436 utils_close_pipe(thread_quit_pipe);
437
438 /*
439 * If opt_pidfile is undefined, the default file will be wiped when
440 * removing the rundir.
441 */
442 if (opt_pidfile) {
443 ret = remove(opt_pidfile);
444 if (ret < 0) {
445 PERROR("remove pidfile %s", opt_pidfile);
446 }
447 }
448
449 DBG("Removing sessiond and consumerd content of directory %s", rundir);
450
451 /* sessiond */
452 snprintf(path, PATH_MAX,
453 "%s/%s",
454 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
455 DBG("Removing %s", path);
456 (void) unlink(path);
457
458 snprintf(path, PATH_MAX, "%s/%s", rundir,
459 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE);
460 DBG("Removing %s", path);
461 (void) unlink(path);
462
463 /* kconsumerd */
464 snprintf(path, PATH_MAX,
465 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
466 rundir);
467 DBG("Removing %s", path);
468 (void) unlink(path);
469
470 snprintf(path, PATH_MAX,
471 DEFAULT_KCONSUMERD_PATH,
472 rundir);
473 DBG("Removing directory %s", path);
474 (void) rmdir(path);
475
476 /* ust consumerd 32 */
477 snprintf(path, PATH_MAX,
478 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
479 rundir);
480 DBG("Removing %s", path);
481 (void) unlink(path);
482
483 snprintf(path, PATH_MAX,
484 DEFAULT_USTCONSUMERD32_PATH,
485 rundir);
486 DBG("Removing directory %s", path);
487 (void) rmdir(path);
488
489 /* ust consumerd 64 */
490 snprintf(path, PATH_MAX,
491 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
492 rundir);
493 DBG("Removing %s", path);
494 (void) unlink(path);
495
496 snprintf(path, PATH_MAX,
497 DEFAULT_USTCONSUMERD64_PATH,
498 rundir);
499 DBG("Removing directory %s", path);
500 (void) rmdir(path);
501
502 /*
503 * We do NOT rmdir rundir because there are other processes
504 * using it, for instance lttng-relayd, which can start in
505 * parallel with this teardown.
506 */
507
508 free(rundir);
509
510 DBG("Cleaning up all sessions");
511
512 /* Destroy session list mutex */
513 if (session_list_ptr != NULL) {
514 pthread_mutex_destroy(&session_list_ptr->lock);
515
516 /* Cleanup ALL session */
517 cds_list_for_each_entry_safe(sess, stmp,
518 &session_list_ptr->head, list) {
519 cmd_destroy_session(sess, kernel_poll_pipe[1]);
520 }
521 }
522
523 DBG("Closing all UST sockets");
524 ust_app_clean_list();
525 buffer_reg_destroy_registries();
526
527 if (is_root && !opt_no_kernel) {
528 DBG2("Closing kernel fd");
529 if (kernel_tracer_fd >= 0) {
530 ret = close(kernel_tracer_fd);
531 if (ret) {
532 PERROR("close");
533 }
534 }
535 DBG("Unloading kernel modules");
536 modprobe_remove_lttng_all();
537 }
538
539 close_consumer_sockets();
540
541 /* <fun> */
542 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
543 "Matthew, BEET driven development works!%c[%dm",
544 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
545 /* </fun> */
546 }
547
548 /*
549 * Send data on a unix socket using the liblttsessiondcomm API.
550 *
551 * Return lttcomm error code.
552 */
553 static int send_unix_sock(int sock, void *buf, size_t len)
554 {
555 /* Check valid length */
556 if (len == 0) {
557 return -1;
558 }
559
560 return lttcomm_send_unix_sock(sock, buf, len);
561 }
562
563 /*
564 * Free memory of a command context structure.
565 */
566 static void clean_command_ctx(struct command_ctx **cmd_ctx)
567 {
568 DBG("Clean command context structure");
569 if (*cmd_ctx) {
570 if ((*cmd_ctx)->llm) {
571 free((*cmd_ctx)->llm);
572 }
573 if ((*cmd_ctx)->lsm) {
574 free((*cmd_ctx)->lsm);
575 }
576 free(*cmd_ctx);
577 *cmd_ctx = NULL;
578 }
579 }
580
581 /*
582 * Notify UST applications using the shm mmap futex.
583 */
584 static int notify_ust_apps(int active)
585 {
586 char *wait_shm_mmap;
587
588 DBG("Notifying applications of session daemon state: %d", active);
589
590 /* See shm.c for this call implying mmap, shm and futex calls */
591 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
592 if (wait_shm_mmap == NULL) {
593 goto error;
594 }
595
596 /* Wake waiting process */
597 futex_wait_update((int32_t *) wait_shm_mmap, active);
598
599 /* Apps notified successfully */
600 return 0;
601
602 error:
603 return -1;
604 }
605
606 /*
607 * Setup the outgoing data buffer for the response (llm) by allocating the
608 * right amount of memory and copying the original information from the lsm
609 * structure.
610 *
611 * Return total size of the buffer pointed by buf.
612 */
613 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
614 {
615 int ret, buf_size;
616
617 buf_size = size;
618
619 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
620 if (cmd_ctx->llm == NULL) {
621 PERROR("zmalloc");
622 ret = -ENOMEM;
623 goto error;
624 }
625
626 /* Copy common data */
627 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
628 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
629
630 cmd_ctx->llm->data_size = size;
631 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
632
633 return buf_size;
634
635 error:
636 return ret;
637 }
638
639 /*
640 * Update the kernel poll set of all channel fd available over all tracing
641 * session. Add the wakeup pipe at the end of the set.
642 */
643 static int update_kernel_poll(struct lttng_poll_event *events)
644 {
645 int ret;
646 struct ltt_session *session;
647 struct ltt_kernel_channel *channel;
648
649 DBG("Updating kernel poll set");
650
651 session_lock_list();
652 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
653 session_lock(session);
654 if (session->kernel_session == NULL) {
655 session_unlock(session);
656 continue;
657 }
658
659 cds_list_for_each_entry(channel,
660 &session->kernel_session->channel_list.head, list) {
661 /* Add channel fd to the kernel poll set */
662 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
663 if (ret < 0) {
664 session_unlock(session);
665 goto error;
666 }
667 DBG("Channel fd %d added to kernel set", channel->fd);
668 }
669 session_unlock(session);
670 }
671 session_unlock_list();
672
673 return 0;
674
675 error:
676 session_unlock_list();
677 return -1;
678 }
679
680 /*
681 * Find the channel fd from 'fd' over all tracing session. When found, check
682 * for new channel stream and send those stream fds to the kernel consumer.
683 *
684 * Useful for CPU hotplug feature.
685 */
686 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
687 {
688 int ret = 0;
689 struct ltt_session *session;
690 struct ltt_kernel_session *ksess;
691 struct ltt_kernel_channel *channel;
692
693 DBG("Updating kernel streams for channel fd %d", fd);
694
695 session_lock_list();
696 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
697 session_lock(session);
698 if (session->kernel_session == NULL) {
699 session_unlock(session);
700 continue;
701 }
702 ksess = session->kernel_session;
703
704 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
705 if (channel->fd == fd) {
706 DBG("Channel found, updating kernel streams");
707 ret = kernel_open_channel_stream(channel);
708 if (ret < 0) {
709 goto error;
710 }
711 /* Update the stream global counter */
712 ksess->stream_count_global += ret;
713
714 /*
715 * Have we already sent fds to the consumer? If yes, it means
716 * that tracing is started so it is safe to send our updated
717 * stream fds.
718 */
719 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
720 struct lttng_ht_iter iter;
721 struct consumer_socket *socket;
722
723 rcu_read_lock();
724 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
725 &iter.iter, socket, node.node) {
726 pthread_mutex_lock(socket->lock);
727 ret = kernel_consumer_send_channel_stream(socket,
728 channel, ksess,
729 session->output_traces ? 1 : 0);
730 pthread_mutex_unlock(socket->lock);
731 if (ret < 0) {
732 rcu_read_unlock();
733 goto error;
734 }
735 }
736 rcu_read_unlock();
737 }
738 goto error;
739 }
740 }
741 session_unlock(session);
742 }
743 session_unlock_list();
744 return ret;
745
746 error:
747 session_unlock(session);
748 session_unlock_list();
749 return ret;
750 }
751
752 /*
753 * For each tracing session, update newly registered apps. The session list
754 * lock MUST be acquired before calling this.
755 */
756 static void update_ust_app(int app_sock)
757 {
758 struct ltt_session *sess, *stmp;
759
760 /* Consumer is in an ERROR state. Stop any application update. */
761 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
762 /* Stop the update process since the consumer is dead. */
763 return;
764 }
765
766 /* For all tracing session(s) */
767 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
768 session_lock(sess);
769 if (sess->ust_session) {
770 ust_app_global_update(sess->ust_session, app_sock);
771 }
772 session_unlock(sess);
773 }
774 }
775
776 /*
777 * This thread manage event coming from the kernel.
778 *
779 * Features supported in this thread:
780 * -) CPU Hotplug
781 */
782 static void *thread_manage_kernel(void *data)
783 {
784 int ret, i, pollfd, update_poll_flag = 1, err = -1;
785 uint32_t revents, nb_fd;
786 char tmp;
787 struct lttng_poll_event events;
788
789 DBG("[thread] Thread manage kernel started");
790
791 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
792
793 /*
794 * This first step of the while is to clean this structure which could free
795 * non NULL pointers so initialize it before the loop.
796 */
797 lttng_poll_init(&events);
798
799 if (testpoint(thread_manage_kernel)) {
800 goto error_testpoint;
801 }
802
803 health_code_update();
804
805 if (testpoint(thread_manage_kernel_before_loop)) {
806 goto error_testpoint;
807 }
808
809 while (1) {
810 health_code_update();
811
812 if (update_poll_flag == 1) {
813 /* Clean events object. We are about to populate it again. */
814 lttng_poll_clean(&events);
815
816 ret = sessiond_set_thread_pollset(&events, 2);
817 if (ret < 0) {
818 goto error_poll_create;
819 }
820
821 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
822 if (ret < 0) {
823 goto error;
824 }
825
826 /* This will add the available kernel channel if any. */
827 ret = update_kernel_poll(&events);
828 if (ret < 0) {
829 goto error;
830 }
831 update_poll_flag = 0;
832 }
833
834 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
835
836 /* Poll infinite value of time */
837 restart:
838 health_poll_entry();
839 ret = lttng_poll_wait(&events, -1);
840 health_poll_exit();
841 if (ret < 0) {
842 /*
843 * Restart interrupted system call.
844 */
845 if (errno == EINTR) {
846 goto restart;
847 }
848 goto error;
849 } else if (ret == 0) {
850 /* Should not happen since timeout is infinite */
851 ERR("Return value of poll is 0 with an infinite timeout.\n"
852 "This should not have happened! Continuing...");
853 continue;
854 }
855
856 nb_fd = ret;
857
858 for (i = 0; i < nb_fd; i++) {
859 /* Fetch once the poll data */
860 revents = LTTNG_POLL_GETEV(&events, i);
861 pollfd = LTTNG_POLL_GETFD(&events, i);
862
863 health_code_update();
864
865 /* Thread quit pipe has been closed. Killing thread. */
866 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
867 if (ret) {
868 err = 0;
869 goto exit;
870 }
871
872 /* Check for data on kernel pipe */
873 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
874 do {
875 ret = read(kernel_poll_pipe[0], &tmp, 1);
876 } while (ret < 0 && errno == EINTR);
877 /*
878 * Ret value is useless here, if this pipe gets any actions an
879 * update is required anyway.
880 */
881 update_poll_flag = 1;
882 continue;
883 } else {
884 /*
885 * New CPU detected by the kernel. Adding kernel stream to
886 * kernel session and updating the kernel consumer
887 */
888 if (revents & LPOLLIN) {
889 ret = update_kernel_stream(&kconsumer_data, pollfd);
890 if (ret < 0) {
891 continue;
892 }
893 break;
894 /*
895 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
896 * and unregister kernel stream at this point.
897 */
898 }
899 }
900 }
901 }
902
903 exit:
904 error:
905 lttng_poll_clean(&events);
906 error_poll_create:
907 error_testpoint:
908 utils_close_pipe(kernel_poll_pipe);
909 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
910 if (err) {
911 health_error();
912 ERR("Health error occurred in %s", __func__);
913 WARN("Kernel thread died unexpectedly. "
914 "Kernel tracing can continue but CPU hotplug is disabled.");
915 }
916 health_unregister(health_sessiond);
917 DBG("Kernel thread dying");
918 return NULL;
919 }
920
921 /*
922 * Signal pthread condition of the consumer data that the thread.
923 */
924 static void signal_consumer_condition(struct consumer_data *data, int state)
925 {
926 pthread_mutex_lock(&data->cond_mutex);
927
928 /*
929 * The state is set before signaling. It can be any value, it's the waiter
930 * job to correctly interpret this condition variable associated to the
931 * consumer pthread_cond.
932 *
933 * A value of 0 means that the corresponding thread of the consumer data
934 * was not started. 1 indicates that the thread has started and is ready
935 * for action. A negative value means that there was an error during the
936 * thread bootstrap.
937 */
938 data->consumer_thread_is_ready = state;
939 (void) pthread_cond_signal(&data->cond);
940
941 pthread_mutex_unlock(&data->cond_mutex);
942 }
943
944 /*
945 * This thread manage the consumer error sent back to the session daemon.
946 */
947 static void *thread_manage_consumer(void *data)
948 {
949 int sock = -1, i, ret, pollfd, err = -1;
950 uint32_t revents, nb_fd;
951 enum lttcomm_return_code code;
952 struct lttng_poll_event events;
953 struct consumer_data *consumer_data = data;
954
955 DBG("[thread] Manage consumer started");
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(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 /* Thread quit pipe has been closed. Killing thread. */
1012 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1013 if (ret) {
1014 err = 0;
1015 goto exit;
1016 }
1017
1018 /* Event on the registration socket */
1019 if (pollfd == consumer_data->err_sock) {
1020 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1021 ERR("consumer err socket poll error");
1022 goto error;
1023 }
1024 }
1025 }
1026
1027 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1028 if (sock < 0) {
1029 goto error;
1030 }
1031
1032 /*
1033 * Set the CLOEXEC flag. Return code is useless because either way, the
1034 * show must go on.
1035 */
1036 (void) utils_set_fd_cloexec(sock);
1037
1038 health_code_update();
1039
1040 DBG2("Receiving code from consumer err_sock");
1041
1042 /* Getting status code from kconsumerd */
1043 ret = lttcomm_recv_unix_sock(sock, &code,
1044 sizeof(enum lttcomm_return_code));
1045 if (ret <= 0) {
1046 goto error;
1047 }
1048
1049 health_code_update();
1050
1051 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1052 /* Connect both socket, command and metadata. */
1053 consumer_data->cmd_sock =
1054 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1055 consumer_data->metadata_fd =
1056 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1057 if (consumer_data->cmd_sock < 0
1058 || consumer_data->metadata_fd < 0) {
1059 PERROR("consumer connect cmd socket");
1060 /* On error, signal condition and quit. */
1061 signal_consumer_condition(consumer_data, -1);
1062 goto error;
1063 }
1064 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1065 /* Create metadata socket lock. */
1066 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1067 if (consumer_data->metadata_sock.lock == NULL) {
1068 PERROR("zmalloc pthread mutex");
1069 ret = -1;
1070 goto error;
1071 }
1072 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1073
1074 signal_consumer_condition(consumer_data, 1);
1075 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1076 DBG("Consumer metadata socket ready (fd: %d)",
1077 consumer_data->metadata_fd);
1078 } else {
1079 ERR("consumer error when waiting for SOCK_READY : %s",
1080 lttcomm_get_readable_code(-code));
1081 goto error;
1082 }
1083
1084 /* Remove the consumerd error sock since we've established a connexion */
1085 ret = lttng_poll_del(&events, consumer_data->err_sock);
1086 if (ret < 0) {
1087 goto error;
1088 }
1089
1090 /* Add new accepted error socket. */
1091 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1092 if (ret < 0) {
1093 goto error;
1094 }
1095
1096 /* Add metadata socket that is successfully connected. */
1097 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1098 LPOLLIN | LPOLLRDHUP);
1099 if (ret < 0) {
1100 goto error;
1101 }
1102
1103 health_code_update();
1104
1105 /* Infinite blocking call, waiting for transmission */
1106 restart_poll:
1107 while (1) {
1108 health_poll_entry();
1109 ret = lttng_poll_wait(&events, -1);
1110 health_poll_exit();
1111 if (ret < 0) {
1112 /*
1113 * Restart interrupted system call.
1114 */
1115 if (errno == EINTR) {
1116 goto restart_poll;
1117 }
1118 goto error;
1119 }
1120
1121 nb_fd = ret;
1122
1123 for (i = 0; i < nb_fd; i++) {
1124 /* Fetch once the poll data */
1125 revents = LTTNG_POLL_GETEV(&events, i);
1126 pollfd = LTTNG_POLL_GETFD(&events, i);
1127
1128 health_code_update();
1129
1130 /* Thread quit pipe has been closed. Killing thread. */
1131 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1132 if (ret) {
1133 err = 0;
1134 goto exit;
1135 }
1136
1137 if (pollfd == sock) {
1138 /* Event on the consumerd socket */
1139 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1140 ERR("consumer err socket second poll error");
1141 goto error;
1142 }
1143 health_code_update();
1144 /* Wait for any kconsumerd error */
1145 ret = lttcomm_recv_unix_sock(sock, &code,
1146 sizeof(enum lttcomm_return_code));
1147 if (ret <= 0) {
1148 ERR("consumer closed the command socket");
1149 goto error;
1150 }
1151
1152 ERR("consumer return code : %s",
1153 lttcomm_get_readable_code(-code));
1154
1155 goto exit;
1156 } else if (pollfd == consumer_data->metadata_fd) {
1157 /* UST metadata requests */
1158 ret = ust_consumer_metadata_request(
1159 &consumer_data->metadata_sock);
1160 if (ret < 0) {
1161 ERR("Handling metadata request");
1162 goto error;
1163 }
1164 break;
1165 } else {
1166 ERR("Unknown pollfd");
1167 goto error;
1168 }
1169 }
1170 health_code_update();
1171 }
1172
1173 exit:
1174 error:
1175 /*
1176 * We lock here because we are about to close the sockets and some other
1177 * thread might be using them so get exclusive access which will abort all
1178 * other consumer command by other threads.
1179 */
1180 pthread_mutex_lock(&consumer_data->lock);
1181
1182 /* Immediately set the consumerd state to stopped */
1183 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1184 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1185 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1186 consumer_data->type == LTTNG_CONSUMER32_UST) {
1187 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1188 } else {
1189 /* Code flow error... */
1190 assert(0);
1191 }
1192
1193 if (consumer_data->err_sock >= 0) {
1194 ret = close(consumer_data->err_sock);
1195 if (ret) {
1196 PERROR("close");
1197 }
1198 consumer_data->err_sock = -1;
1199 }
1200 if (consumer_data->cmd_sock >= 0) {
1201 ret = close(consumer_data->cmd_sock);
1202 if (ret) {
1203 PERROR("close");
1204 }
1205 consumer_data->cmd_sock = -1;
1206 }
1207 if (*consumer_data->metadata_sock.fd_ptr >= 0) {
1208 ret = close(*consumer_data->metadata_sock.fd_ptr);
1209 if (ret) {
1210 PERROR("close");
1211 }
1212 }
1213
1214 if (sock >= 0) {
1215 ret = close(sock);
1216 if (ret) {
1217 PERROR("close");
1218 }
1219 }
1220
1221 unlink(consumer_data->err_unix_sock_path);
1222 unlink(consumer_data->cmd_unix_sock_path);
1223 consumer_data->pid = 0;
1224 pthread_mutex_unlock(&consumer_data->lock);
1225
1226 /* Cleanup metadata socket mutex. */
1227 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1228 free(consumer_data->metadata_sock.lock);
1229
1230 lttng_poll_clean(&events);
1231 error_poll:
1232 if (err) {
1233 health_error();
1234 ERR("Health error occurred in %s", __func__);
1235 }
1236 health_unregister(health_sessiond);
1237 DBG("consumer thread cleanup completed");
1238
1239 return NULL;
1240 }
1241
1242 /*
1243 * This thread manage application communication.
1244 */
1245 static void *thread_manage_apps(void *data)
1246 {
1247 int i, ret, pollfd, err = -1;
1248 uint32_t revents, nb_fd;
1249 struct lttng_poll_event events;
1250
1251 DBG("[thread] Manage application started");
1252
1253 rcu_register_thread();
1254 rcu_thread_online();
1255
1256 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1257
1258 if (testpoint(thread_manage_apps)) {
1259 goto error_testpoint;
1260 }
1261
1262 health_code_update();
1263
1264 ret = sessiond_set_thread_pollset(&events, 2);
1265 if (ret < 0) {
1266 goto error_poll_create;
1267 }
1268
1269 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1270 if (ret < 0) {
1271 goto error;
1272 }
1273
1274 if (testpoint(thread_manage_apps_before_loop)) {
1275 goto error;
1276 }
1277
1278 health_code_update();
1279
1280 while (1) {
1281 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
1282
1283 /* Inifinite blocking call, waiting for transmission */
1284 restart:
1285 health_poll_entry();
1286 ret = lttng_poll_wait(&events, -1);
1287 health_poll_exit();
1288 if (ret < 0) {
1289 /*
1290 * Restart interrupted system call.
1291 */
1292 if (errno == EINTR) {
1293 goto restart;
1294 }
1295 goto error;
1296 }
1297
1298 nb_fd = ret;
1299
1300 for (i = 0; i < nb_fd; i++) {
1301 /* Fetch once the poll data */
1302 revents = LTTNG_POLL_GETEV(&events, i);
1303 pollfd = LTTNG_POLL_GETFD(&events, i);
1304
1305 health_code_update();
1306
1307 /* Thread quit pipe has been closed. Killing thread. */
1308 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1309 if (ret) {
1310 err = 0;
1311 goto exit;
1312 }
1313
1314 /* Inspect the apps cmd pipe */
1315 if (pollfd == apps_cmd_pipe[0]) {
1316 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1317 ERR("Apps command pipe error");
1318 goto error;
1319 } else if (revents & LPOLLIN) {
1320 int sock;
1321
1322 /* Empty pipe */
1323 do {
1324 ret = read(apps_cmd_pipe[0], &sock, sizeof(sock));
1325 } while (ret < 0 && errno == EINTR);
1326 if (ret < 0 || ret < sizeof(sock)) {
1327 PERROR("read apps cmd pipe");
1328 goto error;
1329 }
1330
1331 health_code_update();
1332
1333 /*
1334 * We only monitor the error events of the socket. This
1335 * thread does not handle any incoming data from UST
1336 * (POLLIN).
1337 */
1338 ret = lttng_poll_add(&events, sock,
1339 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1340 if (ret < 0) {
1341 goto error;
1342 }
1343
1344 DBG("Apps with sock %d added to poll set", sock);
1345
1346 health_code_update();
1347
1348 break;
1349 }
1350 } else {
1351 /*
1352 * At this point, we know that a registered application made
1353 * the event at poll_wait.
1354 */
1355 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1356 /* Removing from the poll set */
1357 ret = lttng_poll_del(&events, pollfd);
1358 if (ret < 0) {
1359 goto error;
1360 }
1361
1362 /* Socket closed on remote end. */
1363 ust_app_unregister(pollfd);
1364 break;
1365 }
1366 }
1367
1368 health_code_update();
1369 }
1370 }
1371
1372 exit:
1373 error:
1374 lttng_poll_clean(&events);
1375 error_poll_create:
1376 error_testpoint:
1377 utils_close_pipe(apps_cmd_pipe);
1378 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1379
1380 /*
1381 * We don't clean the UST app hash table here since already registered
1382 * applications can still be controlled so let them be until the session
1383 * daemon dies or the applications stop.
1384 */
1385
1386 if (err) {
1387 health_error();
1388 ERR("Health error occurred in %s", __func__);
1389 }
1390 health_unregister(health_sessiond);
1391 DBG("Application communication apps thread cleanup complete");
1392 rcu_thread_offline();
1393 rcu_unregister_thread();
1394 return NULL;
1395 }
1396
1397 /*
1398 * Send a socket to a thread This is called from the dispatch UST registration
1399 * thread once all sockets are set for the application.
1400 *
1401 * The sock value can be invalid, we don't really care, the thread will handle
1402 * it and make the necessary cleanup if so.
1403 *
1404 * On success, return 0 else a negative value being the errno message of the
1405 * write().
1406 */
1407 static int send_socket_to_thread(int fd, int sock)
1408 {
1409 int ret;
1410
1411 /*
1412 * It's possible that the FD is set as invalid with -1 concurrently just
1413 * before calling this function being a shutdown state of the thread.
1414 */
1415 if (fd < 0) {
1416 ret = -EBADF;
1417 goto error;
1418 }
1419
1420 do {
1421 ret = write(fd, &sock, sizeof(sock));
1422 } while (ret < 0 && errno == EINTR);
1423 if (ret < 0 || ret != sizeof(sock)) {
1424 PERROR("write apps pipe %d", fd);
1425 if (ret < 0) {
1426 ret = -errno;
1427 }
1428 goto error;
1429 }
1430
1431 /* All good. Don't send back the write positive ret value. */
1432 ret = 0;
1433 error:
1434 return ret;
1435 }
1436
1437 /*
1438 * Sanitize the wait queue of the dispatch registration thread meaning removing
1439 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1440 * notify socket is never received.
1441 */
1442 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1443 {
1444 int ret, nb_fd = 0, i;
1445 unsigned int fd_added = 0;
1446 struct lttng_poll_event events;
1447 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1448
1449 assert(wait_queue);
1450
1451 lttng_poll_init(&events);
1452
1453 /* Just skip everything for an empty queue. */
1454 if (!wait_queue->count) {
1455 goto end;
1456 }
1457
1458 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1459 if (ret < 0) {
1460 goto error_create;
1461 }
1462
1463 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1464 &wait_queue->head, head) {
1465 assert(wait_node->app);
1466 ret = lttng_poll_add(&events, wait_node->app->sock,
1467 LPOLLHUP | LPOLLERR);
1468 if (ret < 0) {
1469 goto error;
1470 }
1471
1472 fd_added = 1;
1473 }
1474
1475 if (!fd_added) {
1476 goto end;
1477 }
1478
1479 /*
1480 * Poll but don't block so we can quickly identify the faulty events and
1481 * clean them afterwards from the wait queue.
1482 */
1483 ret = lttng_poll_wait(&events, 0);
1484 if (ret < 0) {
1485 goto error;
1486 }
1487 nb_fd = ret;
1488
1489 for (i = 0; i < nb_fd; i++) {
1490 /* Get faulty FD. */
1491 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1492 int pollfd = LTTNG_POLL_GETFD(&events, i);
1493
1494 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1495 &wait_queue->head, head) {
1496 if (pollfd == wait_node->app->sock &&
1497 (revents & (LPOLLHUP | LPOLLERR))) {
1498 cds_list_del(&wait_node->head);
1499 wait_queue->count--;
1500 ust_app_destroy(wait_node->app);
1501 free(wait_node);
1502 break;
1503 }
1504 }
1505 }
1506
1507 if (nb_fd > 0) {
1508 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1509 }
1510
1511 end:
1512 lttng_poll_clean(&events);
1513 return;
1514
1515 error:
1516 lttng_poll_clean(&events);
1517 error_create:
1518 ERR("Unable to sanitize wait queue");
1519 return;
1520 }
1521
1522 /*
1523 * Dispatch request from the registration threads to the application
1524 * communication thread.
1525 */
1526 static void *thread_dispatch_ust_registration(void *data)
1527 {
1528 int ret, err = -1;
1529 struct cds_wfq_node *node;
1530 struct ust_command *ust_cmd = NULL;
1531 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1532 struct ust_reg_wait_queue wait_queue = {
1533 .count = 0,
1534 };
1535
1536 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1537
1538 health_code_update();
1539
1540 CDS_INIT_LIST_HEAD(&wait_queue.head);
1541
1542 DBG("[thread] Dispatch UST command started");
1543
1544 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1545 health_code_update();
1546
1547 /* Atomically prepare the queue futex */
1548 futex_nto1_prepare(&ust_cmd_queue.futex);
1549
1550 do {
1551 struct ust_app *app = NULL;
1552 ust_cmd = NULL;
1553
1554 /*
1555 * Make sure we don't have node(s) that have hung up before receiving
1556 * the notify socket. This is to clean the list in order to avoid
1557 * memory leaks from notify socket that are never seen.
1558 */
1559 sanitize_wait_queue(&wait_queue);
1560
1561 health_code_update();
1562 /* Dequeue command for registration */
1563 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1564 if (node == NULL) {
1565 DBG("Woken up but nothing in the UST command queue");
1566 /* Continue thread execution */
1567 break;
1568 }
1569
1570 ust_cmd = caa_container_of(node, struct ust_command, node);
1571
1572 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1573 " gid:%d sock:%d name:%s (version %d.%d)",
1574 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1575 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1576 ust_cmd->sock, ust_cmd->reg_msg.name,
1577 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1578
1579 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1580 wait_node = zmalloc(sizeof(*wait_node));
1581 if (!wait_node) {
1582 PERROR("zmalloc wait_node dispatch");
1583 ret = close(ust_cmd->sock);
1584 if (ret < 0) {
1585 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1586 }
1587 lttng_fd_put(LTTNG_FD_APPS, 1);
1588 free(ust_cmd);
1589 goto error;
1590 }
1591 CDS_INIT_LIST_HEAD(&wait_node->head);
1592
1593 /* Create application object if socket is CMD. */
1594 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1595 ust_cmd->sock);
1596 if (!wait_node->app) {
1597 ret = close(ust_cmd->sock);
1598 if (ret < 0) {
1599 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1600 }
1601 lttng_fd_put(LTTNG_FD_APPS, 1);
1602 free(wait_node);
1603 free(ust_cmd);
1604 continue;
1605 }
1606 /*
1607 * Add application to the wait queue so we can set the notify
1608 * socket before putting this object in the global ht.
1609 */
1610 cds_list_add(&wait_node->head, &wait_queue.head);
1611 wait_queue.count++;
1612
1613 free(ust_cmd);
1614 /*
1615 * We have to continue here since we don't have the notify
1616 * socket and the application MUST be added to the hash table
1617 * only at that moment.
1618 */
1619 continue;
1620 } else {
1621 /*
1622 * Look for the application in the local wait queue and set the
1623 * notify socket if found.
1624 */
1625 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1626 &wait_queue.head, head) {
1627 health_code_update();
1628 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1629 wait_node->app->notify_sock = ust_cmd->sock;
1630 cds_list_del(&wait_node->head);
1631 wait_queue.count--;
1632 app = wait_node->app;
1633 free(wait_node);
1634 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1635 break;
1636 }
1637 }
1638
1639 /*
1640 * With no application at this stage the received socket is
1641 * basically useless so close it before we free the cmd data
1642 * structure for good.
1643 */
1644 if (!app) {
1645 ret = close(ust_cmd->sock);
1646 if (ret < 0) {
1647 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1648 }
1649 lttng_fd_put(LTTNG_FD_APPS, 1);
1650 }
1651 free(ust_cmd);
1652 }
1653
1654 if (app) {
1655 /*
1656 * @session_lock_list
1657 *
1658 * Lock the global session list so from the register up to the
1659 * registration done message, no thread can see the application
1660 * and change its state.
1661 */
1662 session_lock_list();
1663 rcu_read_lock();
1664
1665 /*
1666 * Add application to the global hash table. This needs to be
1667 * done before the update to the UST registry can locate the
1668 * application.
1669 */
1670 ust_app_add(app);
1671
1672 /* Set app version. This call will print an error if needed. */
1673 (void) ust_app_version(app);
1674
1675 /* Send notify socket through the notify pipe. */
1676 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1677 app->notify_sock);
1678 if (ret < 0) {
1679 rcu_read_unlock();
1680 session_unlock_list();
1681 /*
1682 * No notify thread, stop the UST tracing. However, this is
1683 * not an internal error of the this thread thus setting
1684 * the health error code to a normal exit.
1685 */
1686 err = 0;
1687 goto error;
1688 }
1689
1690 /*
1691 * Update newly registered application with the tracing
1692 * registry info already enabled information.
1693 */
1694 update_ust_app(app->sock);
1695
1696 /*
1697 * Don't care about return value. Let the manage apps threads
1698 * handle app unregistration upon socket close.
1699 */
1700 (void) ust_app_register_done(app->sock);
1701
1702 /*
1703 * Even if the application socket has been closed, send the app
1704 * to the thread and unregistration will take place at that
1705 * place.
1706 */
1707 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
1708 if (ret < 0) {
1709 rcu_read_unlock();
1710 session_unlock_list();
1711 /*
1712 * No apps. thread, stop the UST tracing. However, this is
1713 * not an internal error of the this thread thus setting
1714 * the health error code to a normal exit.
1715 */
1716 err = 0;
1717 goto error;
1718 }
1719
1720 rcu_read_unlock();
1721 session_unlock_list();
1722 }
1723 } while (node != NULL);
1724
1725 health_poll_entry();
1726 /* Futex wait on queue. Blocking call on futex() */
1727 futex_nto1_wait(&ust_cmd_queue.futex);
1728 health_poll_exit();
1729 }
1730 /* Normal exit, no error */
1731 err = 0;
1732
1733 error:
1734 /* Clean up wait queue. */
1735 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1736 &wait_queue.head, head) {
1737 cds_list_del(&wait_node->head);
1738 wait_queue.count--;
1739 free(wait_node);
1740 }
1741
1742 DBG("Dispatch thread dying");
1743 if (err) {
1744 health_error();
1745 ERR("Health error occurred in %s", __func__);
1746 }
1747 health_unregister(health_sessiond);
1748 return NULL;
1749 }
1750
1751 /*
1752 * This thread manage application registration.
1753 */
1754 static void *thread_registration_apps(void *data)
1755 {
1756 int sock = -1, i, ret, pollfd, err = -1;
1757 uint32_t revents, nb_fd;
1758 struct lttng_poll_event events;
1759 /*
1760 * Get allocated in this thread, enqueued to a global queue, dequeued and
1761 * freed in the manage apps thread.
1762 */
1763 struct ust_command *ust_cmd = NULL;
1764
1765 DBG("[thread] Manage application registration started");
1766
1767 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
1768
1769 if (testpoint(thread_registration_apps)) {
1770 goto error_testpoint;
1771 }
1772
1773 ret = lttcomm_listen_unix_sock(apps_sock);
1774 if (ret < 0) {
1775 goto error_listen;
1776 }
1777
1778 /*
1779 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1780 * more will be added to this poll set.
1781 */
1782 ret = sessiond_set_thread_pollset(&events, 2);
1783 if (ret < 0) {
1784 goto error_create_poll;
1785 }
1786
1787 /* Add the application registration socket */
1788 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1789 if (ret < 0) {
1790 goto error_poll_add;
1791 }
1792
1793 /* Notify all applications to register */
1794 ret = notify_ust_apps(1);
1795 if (ret < 0) {
1796 ERR("Failed to notify applications or create the wait shared memory.\n"
1797 "Execution continues but there might be problem for already\n"
1798 "running applications that wishes to register.");
1799 }
1800
1801 while (1) {
1802 DBG("Accepting application registration");
1803
1804 /* Inifinite blocking call, waiting for transmission */
1805 restart:
1806 health_poll_entry();
1807 ret = lttng_poll_wait(&events, -1);
1808 health_poll_exit();
1809 if (ret < 0) {
1810 /*
1811 * Restart interrupted system call.
1812 */
1813 if (errno == EINTR) {
1814 goto restart;
1815 }
1816 goto error;
1817 }
1818
1819 nb_fd = ret;
1820
1821 for (i = 0; i < nb_fd; i++) {
1822 health_code_update();
1823
1824 /* Fetch once the poll data */
1825 revents = LTTNG_POLL_GETEV(&events, i);
1826 pollfd = LTTNG_POLL_GETFD(&events, i);
1827
1828 /* Thread quit pipe has been closed. Killing thread. */
1829 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1830 if (ret) {
1831 err = 0;
1832 goto exit;
1833 }
1834
1835 /* Event on the registration socket */
1836 if (pollfd == apps_sock) {
1837 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1838 ERR("Register apps socket poll error");
1839 goto error;
1840 } else if (revents & LPOLLIN) {
1841 sock = lttcomm_accept_unix_sock(apps_sock);
1842 if (sock < 0) {
1843 goto error;
1844 }
1845
1846 /*
1847 * Set socket timeout for both receiving and ending.
1848 * app_socket_timeout is in seconds, whereas
1849 * lttcomm_setsockopt_rcv_timeout and
1850 * lttcomm_setsockopt_snd_timeout expect msec as
1851 * parameter.
1852 */
1853 (void) lttcomm_setsockopt_rcv_timeout(sock,
1854 app_socket_timeout * 1000);
1855 (void) lttcomm_setsockopt_snd_timeout(sock,
1856 app_socket_timeout * 1000);
1857
1858 /*
1859 * Set the CLOEXEC flag. Return code is useless because
1860 * either way, the show must go on.
1861 */
1862 (void) utils_set_fd_cloexec(sock);
1863
1864 /* Create UST registration command for enqueuing */
1865 ust_cmd = zmalloc(sizeof(struct ust_command));
1866 if (ust_cmd == NULL) {
1867 PERROR("ust command zmalloc");
1868 goto error;
1869 }
1870
1871 /*
1872 * Using message-based transmissions to ensure we don't
1873 * have to deal with partially received messages.
1874 */
1875 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1876 if (ret < 0) {
1877 ERR("Exhausted file descriptors allowed for applications.");
1878 free(ust_cmd);
1879 ret = close(sock);
1880 if (ret) {
1881 PERROR("close");
1882 }
1883 sock = -1;
1884 continue;
1885 }
1886
1887 health_code_update();
1888 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
1889 if (ret < 0) {
1890 free(ust_cmd);
1891 /* Close socket of the application. */
1892 ret = close(sock);
1893 if (ret) {
1894 PERROR("close");
1895 }
1896 lttng_fd_put(LTTNG_FD_APPS, 1);
1897 sock = -1;
1898 continue;
1899 }
1900 health_code_update();
1901
1902 ust_cmd->sock = sock;
1903 sock = -1;
1904
1905 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1906 " gid:%d sock:%d name:%s (version %d.%d)",
1907 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1908 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1909 ust_cmd->sock, ust_cmd->reg_msg.name,
1910 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1911
1912 /*
1913 * Lock free enqueue the registration request. The red pill
1914 * has been taken! This apps will be part of the *system*.
1915 */
1916 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1917
1918 /*
1919 * Wake the registration queue futex. Implicit memory
1920 * barrier with the exchange in cds_wfq_enqueue.
1921 */
1922 futex_nto1_wake(&ust_cmd_queue.futex);
1923 }
1924 }
1925 }
1926 }
1927
1928 exit:
1929 error:
1930 if (err) {
1931 health_error();
1932 ERR("Health error occurred in %s", __func__);
1933 }
1934
1935 /* Notify that the registration thread is gone */
1936 notify_ust_apps(0);
1937
1938 if (apps_sock >= 0) {
1939 ret = close(apps_sock);
1940 if (ret) {
1941 PERROR("close");
1942 }
1943 }
1944 if (sock >= 0) {
1945 ret = close(sock);
1946 if (ret) {
1947 PERROR("close");
1948 }
1949 lttng_fd_put(LTTNG_FD_APPS, 1);
1950 }
1951 unlink(apps_unix_sock_path);
1952
1953 error_poll_add:
1954 lttng_poll_clean(&events);
1955 error_listen:
1956 error_create_poll:
1957 error_testpoint:
1958 DBG("UST Registration thread cleanup complete");
1959 health_unregister(health_sessiond);
1960
1961 return NULL;
1962 }
1963
1964 /*
1965 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1966 * exec or it will fails.
1967 */
1968 static int spawn_consumer_thread(struct consumer_data *consumer_data)
1969 {
1970 int ret, clock_ret;
1971 struct timespec timeout;
1972
1973 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1974 consumer_data->consumer_thread_is_ready = 0;
1975
1976 /* Setup pthread condition */
1977 ret = pthread_condattr_init(&consumer_data->condattr);
1978 if (ret != 0) {
1979 errno = ret;
1980 PERROR("pthread_condattr_init consumer data");
1981 goto error;
1982 }
1983
1984 /*
1985 * Set the monotonic clock in order to make sure we DO NOT jump in time
1986 * between the clock_gettime() call and the timedwait call. See bug #324
1987 * for a more details and how we noticed it.
1988 */
1989 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
1990 if (ret != 0) {
1991 errno = ret;
1992 PERROR("pthread_condattr_setclock consumer data");
1993 goto error;
1994 }
1995
1996 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
1997 if (ret != 0) {
1998 errno = ret;
1999 PERROR("pthread_cond_init consumer data");
2000 goto error;
2001 }
2002
2003 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2004 consumer_data);
2005 if (ret != 0) {
2006 PERROR("pthread_create consumer");
2007 ret = -1;
2008 goto error;
2009 }
2010
2011 /* We are about to wait on a pthread condition */
2012 pthread_mutex_lock(&consumer_data->cond_mutex);
2013
2014 /* Get time for sem_timedwait absolute timeout */
2015 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2016 /*
2017 * Set the timeout for the condition timed wait even if the clock gettime
2018 * call fails since we might loop on that call and we want to avoid to
2019 * increment the timeout too many times.
2020 */
2021 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2022
2023 /*
2024 * The following loop COULD be skipped in some conditions so this is why we
2025 * set ret to 0 in order to make sure at least one round of the loop is
2026 * done.
2027 */
2028 ret = 0;
2029
2030 /*
2031 * Loop until the condition is reached or when a timeout is reached. Note
2032 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2033 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2034 * possible. This loop does not take any chances and works with both of
2035 * them.
2036 */
2037 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2038 if (clock_ret < 0) {
2039 PERROR("clock_gettime spawn consumer");
2040 /* Infinite wait for the consumerd thread to be ready */
2041 ret = pthread_cond_wait(&consumer_data->cond,
2042 &consumer_data->cond_mutex);
2043 } else {
2044 ret = pthread_cond_timedwait(&consumer_data->cond,
2045 &consumer_data->cond_mutex, &timeout);
2046 }
2047 }
2048
2049 /* Release the pthread condition */
2050 pthread_mutex_unlock(&consumer_data->cond_mutex);
2051
2052 if (ret != 0) {
2053 errno = ret;
2054 if (ret == ETIMEDOUT) {
2055 /*
2056 * Call has timed out so we kill the kconsumerd_thread and return
2057 * an error.
2058 */
2059 ERR("Condition timed out. The consumer thread was never ready."
2060 " Killing it");
2061 ret = pthread_cancel(consumer_data->thread);
2062 if (ret < 0) {
2063 PERROR("pthread_cancel consumer thread");
2064 }
2065 } else {
2066 PERROR("pthread_cond_wait failed consumer thread");
2067 }
2068 goto error;
2069 }
2070
2071 pthread_mutex_lock(&consumer_data->pid_mutex);
2072 if (consumer_data->pid == 0) {
2073 ERR("Consumerd did not start");
2074 pthread_mutex_unlock(&consumer_data->pid_mutex);
2075 goto error;
2076 }
2077 pthread_mutex_unlock(&consumer_data->pid_mutex);
2078
2079 return 0;
2080
2081 error:
2082 return ret;
2083 }
2084
2085 /*
2086 * Join consumer thread
2087 */
2088 static int join_consumer_thread(struct consumer_data *consumer_data)
2089 {
2090 void *status;
2091
2092 /* Consumer pid must be a real one. */
2093 if (consumer_data->pid > 0) {
2094 int ret;
2095 ret = kill(consumer_data->pid, SIGTERM);
2096 if (ret) {
2097 ERR("Error killing consumer daemon");
2098 return ret;
2099 }
2100 return pthread_join(consumer_data->thread, &status);
2101 } else {
2102 return 0;
2103 }
2104 }
2105
2106 /*
2107 * Fork and exec a consumer daemon (consumerd).
2108 *
2109 * Return pid if successful else -1.
2110 */
2111 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2112 {
2113 int ret;
2114 pid_t pid;
2115 const char *consumer_to_use;
2116 const char *verbosity;
2117 struct stat st;
2118
2119 DBG("Spawning consumerd");
2120
2121 pid = fork();
2122 if (pid == 0) {
2123 /*
2124 * Exec consumerd.
2125 */
2126 if (opt_verbose_consumer) {
2127 verbosity = "--verbose";
2128 } else {
2129 verbosity = "--quiet";
2130 }
2131 switch (consumer_data->type) {
2132 case LTTNG_CONSUMER_KERNEL:
2133 /*
2134 * Find out which consumerd to execute. We will first try the
2135 * 64-bit path, then the sessiond's installation directory, and
2136 * fallback on the 32-bit one,
2137 */
2138 DBG3("Looking for a kernel consumer at these locations:");
2139 DBG3(" 1) %s", consumerd64_bin);
2140 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2141 DBG3(" 3) %s", consumerd32_bin);
2142 if (stat(consumerd64_bin, &st) == 0) {
2143 DBG3("Found location #1");
2144 consumer_to_use = consumerd64_bin;
2145 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
2146 DBG3("Found location #2");
2147 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
2148 } else if (stat(consumerd32_bin, &st) == 0) {
2149 DBG3("Found location #3");
2150 consumer_to_use = consumerd32_bin;
2151 } else {
2152 DBG("Could not find any valid consumerd executable");
2153 break;
2154 }
2155 DBG("Using kernel consumer at: %s", consumer_to_use);
2156 execl(consumer_to_use,
2157 "lttng-consumerd", verbosity, "-k",
2158 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2159 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2160 "--group", tracing_group_name,
2161 NULL);
2162 break;
2163 case LTTNG_CONSUMER64_UST:
2164 {
2165 char *tmpnew = NULL;
2166
2167 if (consumerd64_libdir[0] != '\0') {
2168 char *tmp;
2169 size_t tmplen;
2170
2171 tmp = getenv("LD_LIBRARY_PATH");
2172 if (!tmp) {
2173 tmp = "";
2174 }
2175 tmplen = strlen("LD_LIBRARY_PATH=")
2176 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2177 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2178 if (!tmpnew) {
2179 ret = -ENOMEM;
2180 goto error;
2181 }
2182 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2183 strcat(tmpnew, consumerd64_libdir);
2184 if (tmp[0] != '\0') {
2185 strcat(tmpnew, ":");
2186 strcat(tmpnew, tmp);
2187 }
2188 ret = putenv(tmpnew);
2189 if (ret) {
2190 ret = -errno;
2191 free(tmpnew);
2192 goto error;
2193 }
2194 }
2195 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
2196 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
2197 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2198 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2199 "--group", tracing_group_name,
2200 NULL);
2201 if (consumerd64_libdir[0] != '\0') {
2202 free(tmpnew);
2203 }
2204 if (ret) {
2205 goto error;
2206 }
2207 break;
2208 }
2209 case LTTNG_CONSUMER32_UST:
2210 {
2211 char *tmpnew = NULL;
2212
2213 if (consumerd32_libdir[0] != '\0') {
2214 char *tmp;
2215 size_t tmplen;
2216
2217 tmp = getenv("LD_LIBRARY_PATH");
2218 if (!tmp) {
2219 tmp = "";
2220 }
2221 tmplen = strlen("LD_LIBRARY_PATH=")
2222 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2223 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2224 if (!tmpnew) {
2225 ret = -ENOMEM;
2226 goto error;
2227 }
2228 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2229 strcat(tmpnew, consumerd32_libdir);
2230 if (tmp[0] != '\0') {
2231 strcat(tmpnew, ":");
2232 strcat(tmpnew, tmp);
2233 }
2234 ret = putenv(tmpnew);
2235 if (ret) {
2236 ret = -errno;
2237 free(tmpnew);
2238 goto error;
2239 }
2240 }
2241 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
2242 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
2243 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2244 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2245 "--group", tracing_group_name,
2246 NULL);
2247 if (consumerd32_libdir[0] != '\0') {
2248 free(tmpnew);
2249 }
2250 if (ret) {
2251 goto error;
2252 }
2253 break;
2254 }
2255 default:
2256 PERROR("unknown consumer type");
2257 exit(EXIT_FAILURE);
2258 }
2259 if (errno != 0) {
2260 PERROR("kernel start consumer exec");
2261 }
2262 exit(EXIT_FAILURE);
2263 } else if (pid > 0) {
2264 ret = pid;
2265 } else {
2266 PERROR("start consumer fork");
2267 ret = -errno;
2268 }
2269 error:
2270 return ret;
2271 }
2272
2273 /*
2274 * Spawn the consumerd daemon and session daemon thread.
2275 */
2276 static int start_consumerd(struct consumer_data *consumer_data)
2277 {
2278 int ret;
2279
2280 /*
2281 * Set the listen() state on the socket since there is a possible race
2282 * between the exec() of the consumer daemon and this call if place in the
2283 * consumer thread. See bug #366 for more details.
2284 */
2285 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2286 if (ret < 0) {
2287 goto error;
2288 }
2289
2290 pthread_mutex_lock(&consumer_data->pid_mutex);
2291 if (consumer_data->pid != 0) {
2292 pthread_mutex_unlock(&consumer_data->pid_mutex);
2293 goto end;
2294 }
2295
2296 ret = spawn_consumerd(consumer_data);
2297 if (ret < 0) {
2298 ERR("Spawning consumerd failed");
2299 pthread_mutex_unlock(&consumer_data->pid_mutex);
2300 goto error;
2301 }
2302
2303 /* Setting up the consumer_data pid */
2304 consumer_data->pid = ret;
2305 DBG2("Consumer pid %d", consumer_data->pid);
2306 pthread_mutex_unlock(&consumer_data->pid_mutex);
2307
2308 DBG2("Spawning consumer control thread");
2309 ret = spawn_consumer_thread(consumer_data);
2310 if (ret < 0) {
2311 ERR("Fatal error spawning consumer control thread");
2312 goto error;
2313 }
2314
2315 end:
2316 return 0;
2317
2318 error:
2319 /* Cleanup already created sockets on error. */
2320 if (consumer_data->err_sock >= 0) {
2321 int err;
2322
2323 err = close(consumer_data->err_sock);
2324 if (err < 0) {
2325 PERROR("close consumer data error socket");
2326 }
2327 }
2328 return ret;
2329 }
2330
2331 /*
2332 * Setup necessary data for kernel tracer action.
2333 */
2334 static int init_kernel_tracer(void)
2335 {
2336 int ret;
2337
2338 /* Modprobe lttng kernel modules */
2339 ret = modprobe_lttng_control();
2340 if (ret < 0) {
2341 goto error;
2342 }
2343
2344 /* Open debugfs lttng */
2345 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2346 if (kernel_tracer_fd < 0) {
2347 DBG("Failed to open %s", module_proc_lttng);
2348 ret = -1;
2349 goto error_open;
2350 }
2351
2352 /* Validate kernel version */
2353 ret = kernel_validate_version(kernel_tracer_fd);
2354 if (ret < 0) {
2355 goto error_version;
2356 }
2357
2358 ret = modprobe_lttng_data();
2359 if (ret < 0) {
2360 goto error_modules;
2361 }
2362
2363 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2364 return 0;
2365
2366 error_version:
2367 modprobe_remove_lttng_control();
2368 ret = close(kernel_tracer_fd);
2369 if (ret) {
2370 PERROR("close");
2371 }
2372 kernel_tracer_fd = -1;
2373 return LTTNG_ERR_KERN_VERSION;
2374
2375 error_modules:
2376 ret = close(kernel_tracer_fd);
2377 if (ret) {
2378 PERROR("close");
2379 }
2380
2381 error_open:
2382 modprobe_remove_lttng_control();
2383
2384 error:
2385 WARN("No kernel tracer available");
2386 kernel_tracer_fd = -1;
2387 if (!is_root) {
2388 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2389 } else {
2390 return LTTNG_ERR_KERN_NA;
2391 }
2392 }
2393
2394
2395 /*
2396 * Copy consumer output from the tracing session to the domain session. The
2397 * function also applies the right modification on a per domain basis for the
2398 * trace files destination directory.
2399 *
2400 * Should *NOT* be called with RCU read-side lock held.
2401 */
2402 static int copy_session_consumer(int domain, struct ltt_session *session)
2403 {
2404 int ret;
2405 const char *dir_name;
2406 struct consumer_output *consumer;
2407
2408 assert(session);
2409 assert(session->consumer);
2410
2411 switch (domain) {
2412 case LTTNG_DOMAIN_KERNEL:
2413 DBG3("Copying tracing session consumer output in kernel session");
2414 /*
2415 * XXX: We should audit the session creation and what this function
2416 * does "extra" in order to avoid a destroy since this function is used
2417 * in the domain session creation (kernel and ust) only. Same for UST
2418 * domain.
2419 */
2420 if (session->kernel_session->consumer) {
2421 consumer_destroy_output(session->kernel_session->consumer);
2422 }
2423 session->kernel_session->consumer =
2424 consumer_copy_output(session->consumer);
2425 /* Ease our life a bit for the next part */
2426 consumer = session->kernel_session->consumer;
2427 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2428 break;
2429 case LTTNG_DOMAIN_JUL:
2430 case LTTNG_DOMAIN_UST:
2431 DBG3("Copying tracing session consumer output in UST session");
2432 if (session->ust_session->consumer) {
2433 consumer_destroy_output(session->ust_session->consumer);
2434 }
2435 session->ust_session->consumer =
2436 consumer_copy_output(session->consumer);
2437 /* Ease our life a bit for the next part */
2438 consumer = session->ust_session->consumer;
2439 dir_name = DEFAULT_UST_TRACE_DIR;
2440 break;
2441 default:
2442 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2443 goto error;
2444 }
2445
2446 /* Append correct directory to subdir */
2447 strncat(consumer->subdir, dir_name,
2448 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2449 DBG3("Copy session consumer subdir %s", consumer->subdir);
2450
2451 ret = LTTNG_OK;
2452
2453 error:
2454 return ret;
2455 }
2456
2457 /*
2458 * Create an UST session and add it to the session ust list.
2459 *
2460 * Should *NOT* be called with RCU read-side lock held.
2461 */
2462 static int create_ust_session(struct ltt_session *session,
2463 struct lttng_domain *domain)
2464 {
2465 int ret;
2466 struct ltt_ust_session *lus = NULL;
2467
2468 assert(session);
2469 assert(domain);
2470 assert(session->consumer);
2471
2472 switch (domain->type) {
2473 case LTTNG_DOMAIN_JUL:
2474 case LTTNG_DOMAIN_UST:
2475 break;
2476 default:
2477 ERR("Unknown UST domain on create session %d", domain->type);
2478 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2479 goto error;
2480 }
2481
2482 DBG("Creating UST session");
2483
2484 lus = trace_ust_create_session(session->id);
2485 if (lus == NULL) {
2486 ret = LTTNG_ERR_UST_SESS_FAIL;
2487 goto error;
2488 }
2489
2490 lus->uid = session->uid;
2491 lus->gid = session->gid;
2492 lus->output_traces = session->output_traces;
2493 lus->snapshot_mode = session->snapshot_mode;
2494 lus->live_timer_interval = session->live_timer;
2495 session->ust_session = lus;
2496
2497 /* Copy session output to the newly created UST session */
2498 ret = copy_session_consumer(domain->type, session);
2499 if (ret != LTTNG_OK) {
2500 goto error;
2501 }
2502
2503 return LTTNG_OK;
2504
2505 error:
2506 free(lus);
2507 session->ust_session = NULL;
2508 return ret;
2509 }
2510
2511 /*
2512 * Create a kernel tracer session then create the default channel.
2513 */
2514 static int create_kernel_session(struct ltt_session *session)
2515 {
2516 int ret;
2517
2518 DBG("Creating kernel session");
2519
2520 ret = kernel_create_session(session, kernel_tracer_fd);
2521 if (ret < 0) {
2522 ret = LTTNG_ERR_KERN_SESS_FAIL;
2523 goto error;
2524 }
2525
2526 /* Code flow safety */
2527 assert(session->kernel_session);
2528
2529 /* Copy session output to the newly created Kernel session */
2530 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2531 if (ret != LTTNG_OK) {
2532 goto error;
2533 }
2534
2535 /* Create directory(ies) on local filesystem. */
2536 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2537 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2538 ret = run_as_mkdir_recursive(
2539 session->kernel_session->consumer->dst.trace_path,
2540 S_IRWXU | S_IRWXG, session->uid, session->gid);
2541 if (ret < 0) {
2542 if (ret != -EEXIST) {
2543 ERR("Trace directory creation error");
2544 goto error;
2545 }
2546 }
2547 }
2548
2549 session->kernel_session->uid = session->uid;
2550 session->kernel_session->gid = session->gid;
2551 session->kernel_session->output_traces = session->output_traces;
2552 session->kernel_session->snapshot_mode = session->snapshot_mode;
2553
2554 return LTTNG_OK;
2555
2556 error:
2557 trace_kernel_destroy_session(session->kernel_session);
2558 session->kernel_session = NULL;
2559 return ret;
2560 }
2561
2562 /*
2563 * Count number of session permitted by uid/gid.
2564 */
2565 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2566 {
2567 unsigned int i = 0;
2568 struct ltt_session *session;
2569
2570 DBG("Counting number of available session for UID %d GID %d",
2571 uid, gid);
2572 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2573 /*
2574 * Only list the sessions the user can control.
2575 */
2576 if (!session_access_ok(session, uid, gid)) {
2577 continue;
2578 }
2579 i++;
2580 }
2581 return i;
2582 }
2583
2584 /*
2585 * Process the command requested by the lttng client within the command
2586 * context structure. This function make sure that the return structure (llm)
2587 * is set and ready for transmission before returning.
2588 *
2589 * Return any error encountered or 0 for success.
2590 *
2591 * "sock" is only used for special-case var. len data.
2592 *
2593 * Should *NOT* be called with RCU read-side lock held.
2594 */
2595 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2596 int *sock_error)
2597 {
2598 int ret = LTTNG_OK;
2599 int need_tracing_session = 1;
2600 int need_domain;
2601
2602 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2603
2604 *sock_error = 0;
2605
2606 switch (cmd_ctx->lsm->cmd_type) {
2607 case LTTNG_CREATE_SESSION:
2608 case LTTNG_CREATE_SESSION_SNAPSHOT:
2609 case LTTNG_CREATE_SESSION_LIVE:
2610 case LTTNG_DESTROY_SESSION:
2611 case LTTNG_LIST_SESSIONS:
2612 case LTTNG_LIST_DOMAINS:
2613 case LTTNG_START_TRACE:
2614 case LTTNG_STOP_TRACE:
2615 case LTTNG_DATA_PENDING:
2616 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2617 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2618 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2619 case LTTNG_SNAPSHOT_RECORD:
2620 need_domain = 0;
2621 break;
2622 default:
2623 need_domain = 1;
2624 }
2625
2626 if (opt_no_kernel && need_domain
2627 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2628 if (!is_root) {
2629 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2630 } else {
2631 ret = LTTNG_ERR_KERN_NA;
2632 }
2633 goto error;
2634 }
2635
2636 /* Deny register consumer if we already have a spawned consumer. */
2637 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2638 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2639 if (kconsumer_data.pid > 0) {
2640 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2641 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2642 goto error;
2643 }
2644 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2645 }
2646
2647 /*
2648 * Check for command that don't needs to allocate a returned payload. We do
2649 * this here so we don't have to make the call for no payload at each
2650 * command.
2651 */
2652 switch(cmd_ctx->lsm->cmd_type) {
2653 case LTTNG_LIST_SESSIONS:
2654 case LTTNG_LIST_TRACEPOINTS:
2655 case LTTNG_LIST_TRACEPOINT_FIELDS:
2656 case LTTNG_LIST_DOMAINS:
2657 case LTTNG_LIST_CHANNELS:
2658 case LTTNG_LIST_EVENTS:
2659 break;
2660 default:
2661 /* Setup lttng message with no payload */
2662 ret = setup_lttng_msg(cmd_ctx, 0);
2663 if (ret < 0) {
2664 /* This label does not try to unlock the session */
2665 goto init_setup_error;
2666 }
2667 }
2668
2669 /* Commands that DO NOT need a session. */
2670 switch (cmd_ctx->lsm->cmd_type) {
2671 case LTTNG_CREATE_SESSION:
2672 case LTTNG_CREATE_SESSION_SNAPSHOT:
2673 case LTTNG_CREATE_SESSION_LIVE:
2674 case LTTNG_CALIBRATE:
2675 case LTTNG_LIST_SESSIONS:
2676 case LTTNG_LIST_TRACEPOINTS:
2677 case LTTNG_LIST_TRACEPOINT_FIELDS:
2678 need_tracing_session = 0;
2679 break;
2680 default:
2681 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2682 /*
2683 * We keep the session list lock across _all_ commands
2684 * for now, because the per-session lock does not
2685 * handle teardown properly.
2686 */
2687 session_lock_list();
2688 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2689 if (cmd_ctx->session == NULL) {
2690 ret = LTTNG_ERR_SESS_NOT_FOUND;
2691 goto error;
2692 } else {
2693 /* Acquire lock for the session */
2694 session_lock(cmd_ctx->session);
2695 }
2696 break;
2697 }
2698
2699 if (!need_domain) {
2700 goto skip_domain;
2701 }
2702
2703 /*
2704 * Check domain type for specific "pre-action".
2705 */
2706 switch (cmd_ctx->lsm->domain.type) {
2707 case LTTNG_DOMAIN_KERNEL:
2708 if (!is_root) {
2709 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2710 goto error;
2711 }
2712
2713 /* Kernel tracer check */
2714 if (kernel_tracer_fd == -1) {
2715 /* Basically, load kernel tracer modules */
2716 ret = init_kernel_tracer();
2717 if (ret != 0) {
2718 goto error;
2719 }
2720 }
2721
2722 /* Consumer is in an ERROR state. Report back to client */
2723 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
2724 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2725 goto error;
2726 }
2727
2728 /* Need a session for kernel command */
2729 if (need_tracing_session) {
2730 if (cmd_ctx->session->kernel_session == NULL) {
2731 ret = create_kernel_session(cmd_ctx->session);
2732 if (ret < 0) {
2733 ret = LTTNG_ERR_KERN_SESS_FAIL;
2734 goto error;
2735 }
2736 }
2737
2738 /* Start the kernel consumer daemon */
2739 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2740 if (kconsumer_data.pid == 0 &&
2741 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2742 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2743 ret = start_consumerd(&kconsumer_data);
2744 if (ret < 0) {
2745 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2746 goto error;
2747 }
2748 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
2749 } else {
2750 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2751 }
2752
2753 /*
2754 * The consumer was just spawned so we need to add the socket to
2755 * the consumer output of the session if exist.
2756 */
2757 ret = consumer_create_socket(&kconsumer_data,
2758 cmd_ctx->session->kernel_session->consumer);
2759 if (ret < 0) {
2760 goto error;
2761 }
2762 }
2763
2764 break;
2765 case LTTNG_DOMAIN_JUL:
2766 case LTTNG_DOMAIN_UST:
2767 {
2768 if (!ust_app_supported()) {
2769 ret = LTTNG_ERR_NO_UST;
2770 goto error;
2771 }
2772 /* Consumer is in an ERROR state. Report back to client */
2773 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
2774 ret = LTTNG_ERR_NO_USTCONSUMERD;
2775 goto error;
2776 }
2777
2778 if (need_tracing_session) {
2779 /* Create UST session if none exist. */
2780 if (cmd_ctx->session->ust_session == NULL) {
2781 ret = create_ust_session(cmd_ctx->session,
2782 &cmd_ctx->lsm->domain);
2783 if (ret != LTTNG_OK) {
2784 goto error;
2785 }
2786 }
2787
2788 /* Start the UST consumer daemons */
2789 /* 64-bit */
2790 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
2791 if (consumerd64_bin[0] != '\0' &&
2792 ustconsumer64_data.pid == 0 &&
2793 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2794 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2795 ret = start_consumerd(&ustconsumer64_data);
2796 if (ret < 0) {
2797 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
2798 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2799 goto error;
2800 }
2801
2802 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
2803 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2804 } else {
2805 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2806 }
2807
2808 /*
2809 * Setup socket for consumer 64 bit. No need for atomic access
2810 * since it was set above and can ONLY be set in this thread.
2811 */
2812 ret = consumer_create_socket(&ustconsumer64_data,
2813 cmd_ctx->session->ust_session->consumer);
2814 if (ret < 0) {
2815 goto error;
2816 }
2817
2818 /* 32-bit */
2819 if (consumerd32_bin[0] != '\0' &&
2820 ustconsumer32_data.pid == 0 &&
2821 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2822 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2823 ret = start_consumerd(&ustconsumer32_data);
2824 if (ret < 0) {
2825 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
2826 uatomic_set(&ust_consumerd32_fd, -EINVAL);
2827 goto error;
2828 }
2829
2830 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
2831 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2832 } else {
2833 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2834 }
2835
2836 /*
2837 * Setup socket for consumer 64 bit. No need for atomic access
2838 * since it was set above and can ONLY be set in this thread.
2839 */
2840 ret = consumer_create_socket(&ustconsumer32_data,
2841 cmd_ctx->session->ust_session->consumer);
2842 if (ret < 0) {
2843 goto error;
2844 }
2845 }
2846 break;
2847 }
2848 default:
2849 break;
2850 }
2851 skip_domain:
2852
2853 /* Validate consumer daemon state when start/stop trace command */
2854 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2855 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2856 switch (cmd_ctx->lsm->domain.type) {
2857 case LTTNG_DOMAIN_JUL:
2858 case LTTNG_DOMAIN_UST:
2859 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
2860 ret = LTTNG_ERR_NO_USTCONSUMERD;
2861 goto error;
2862 }
2863 break;
2864 case LTTNG_DOMAIN_KERNEL:
2865 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
2866 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2867 goto error;
2868 }
2869 break;
2870 }
2871 }
2872
2873 /*
2874 * Check that the UID or GID match that of the tracing session.
2875 * The root user can interact with all sessions.
2876 */
2877 if (need_tracing_session) {
2878 if (!session_access_ok(cmd_ctx->session,
2879 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2880 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
2881 ret = LTTNG_ERR_EPERM;
2882 goto error;
2883 }
2884 }
2885
2886 /*
2887 * Send relayd information to consumer as soon as we have a domain and a
2888 * session defined.
2889 */
2890 if (cmd_ctx->session && need_domain) {
2891 /*
2892 * Setup relayd if not done yet. If the relayd information was already
2893 * sent to the consumer, this call will gracefully return.
2894 */
2895 ret = cmd_setup_relayd(cmd_ctx->session);
2896 if (ret != LTTNG_OK) {
2897 goto error;
2898 }
2899 }
2900
2901 /* Process by command type */
2902 switch (cmd_ctx->lsm->cmd_type) {
2903 case LTTNG_ADD_CONTEXT:
2904 {
2905 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2906 cmd_ctx->lsm->u.context.channel_name,
2907 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
2908 break;
2909 }
2910 case LTTNG_DISABLE_CHANNEL:
2911 {
2912 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2913 cmd_ctx->lsm->u.disable.channel_name);
2914 break;
2915 }
2916 case LTTNG_DISABLE_EVENT:
2917 {
2918 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2919 cmd_ctx->lsm->u.disable.channel_name,
2920 cmd_ctx->lsm->u.disable.name);
2921 break;
2922 }
2923 case LTTNG_DISABLE_ALL_EVENT:
2924 {
2925 DBG("Disabling all events");
2926
2927 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2928 cmd_ctx->lsm->u.disable.channel_name);
2929 break;
2930 }
2931 case LTTNG_ENABLE_CHANNEL:
2932 {
2933 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2934 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
2935 break;
2936 }
2937 case LTTNG_ENABLE_EVENT:
2938 {
2939 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
2940 cmd_ctx->lsm->u.enable.channel_name,
2941 &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]);
2942 break;
2943 }
2944 case LTTNG_ENABLE_ALL_EVENT:
2945 {
2946 DBG("Enabling all events");
2947
2948 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
2949 cmd_ctx->lsm->u.enable.channel_name,
2950 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
2951 break;
2952 }
2953 case LTTNG_LIST_TRACEPOINTS:
2954 {
2955 struct lttng_event *events;
2956 ssize_t nb_events;
2957
2958 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
2959 if (nb_events < 0) {
2960 /* Return value is a negative lttng_error_code. */
2961 ret = -nb_events;
2962 goto error;
2963 }
2964
2965 /*
2966 * Setup lttng message with payload size set to the event list size in
2967 * bytes and then copy list into the llm payload.
2968 */
2969 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2970 if (ret < 0) {
2971 free(events);
2972 goto setup_error;
2973 }
2974
2975 /* Copy event list into message payload */
2976 memcpy(cmd_ctx->llm->payload, events,
2977 sizeof(struct lttng_event) * nb_events);
2978
2979 free(events);
2980
2981 ret = LTTNG_OK;
2982 break;
2983 }
2984 case LTTNG_LIST_TRACEPOINT_FIELDS:
2985 {
2986 struct lttng_event_field *fields;
2987 ssize_t nb_fields;
2988
2989 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
2990 &fields);
2991 if (nb_fields < 0) {
2992 /* Return value is a negative lttng_error_code. */
2993 ret = -nb_fields;
2994 goto error;
2995 }
2996
2997 /*
2998 * Setup lttng message with payload size set to the event list size in
2999 * bytes and then copy list into the llm payload.
3000 */
3001 ret = setup_lttng_msg(cmd_ctx,
3002 sizeof(struct lttng_event_field) * nb_fields);
3003 if (ret < 0) {
3004 free(fields);
3005 goto setup_error;
3006 }
3007
3008 /* Copy event list into message payload */
3009 memcpy(cmd_ctx->llm->payload, fields,
3010 sizeof(struct lttng_event_field) * nb_fields);
3011
3012 free(fields);
3013
3014 ret = LTTNG_OK;
3015 break;
3016 }
3017 case LTTNG_SET_CONSUMER_URI:
3018 {
3019 size_t nb_uri, len;
3020 struct lttng_uri *uris;
3021
3022 nb_uri = cmd_ctx->lsm->u.uri.size;
3023 len = nb_uri * sizeof(struct lttng_uri);
3024
3025 if (nb_uri == 0) {
3026 ret = LTTNG_ERR_INVALID;
3027 goto error;
3028 }
3029
3030 uris = zmalloc(len);
3031 if (uris == NULL) {
3032 ret = LTTNG_ERR_FATAL;
3033 goto error;
3034 }
3035
3036 /* Receive variable len data */
3037 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3038 ret = lttcomm_recv_unix_sock(sock, uris, len);
3039 if (ret <= 0) {
3040 DBG("No URIs received from client... continuing");
3041 *sock_error = 1;
3042 ret = LTTNG_ERR_SESSION_FAIL;
3043 free(uris);
3044 goto error;
3045 }
3046
3047 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3048 nb_uri, uris);
3049 if (ret != LTTNG_OK) {
3050 free(uris);
3051 goto error;
3052 }
3053
3054 /*
3055 * XXX: 0 means that this URI should be applied on the session. Should
3056 * be a DOMAIN enuam.
3057 */
3058 if (cmd_ctx->lsm->domain.type == 0) {
3059 /* Add the URI for the UST session if a consumer is present. */
3060 if (cmd_ctx->session->ust_session &&
3061 cmd_ctx->session->ust_session->consumer) {
3062 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
3063 nb_uri, uris);
3064 } else if (cmd_ctx->session->kernel_session &&
3065 cmd_ctx->session->kernel_session->consumer) {
3066 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
3067 cmd_ctx->session, nb_uri, uris);
3068 }
3069 }
3070
3071 free(uris);
3072
3073 break;
3074 }
3075 case LTTNG_START_TRACE:
3076 {
3077 ret = cmd_start_trace(cmd_ctx->session);
3078 break;
3079 }
3080 case LTTNG_STOP_TRACE:
3081 {
3082 ret = cmd_stop_trace(cmd_ctx->session);
3083 break;
3084 }
3085 case LTTNG_CREATE_SESSION:
3086 {
3087 size_t nb_uri, len;
3088 struct lttng_uri *uris = NULL;
3089
3090 nb_uri = cmd_ctx->lsm->u.uri.size;
3091 len = nb_uri * sizeof(struct lttng_uri);
3092
3093 if (nb_uri > 0) {
3094 uris = zmalloc(len);
3095 if (uris == NULL) {
3096 ret = LTTNG_ERR_FATAL;
3097 goto error;
3098 }
3099
3100 /* Receive variable len data */
3101 DBG("Waiting for %zu URIs from client ...", nb_uri);
3102 ret = lttcomm_recv_unix_sock(sock, uris, len);
3103 if (ret <= 0) {
3104 DBG("No URIs received from client... continuing");
3105 *sock_error = 1;
3106 ret = LTTNG_ERR_SESSION_FAIL;
3107 free(uris);
3108 goto error;
3109 }
3110
3111 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3112 DBG("Creating session with ONE network URI is a bad call");
3113 ret = LTTNG_ERR_SESSION_FAIL;
3114 free(uris);
3115 goto error;
3116 }
3117 }
3118
3119 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
3120 &cmd_ctx->creds, 0);
3121
3122 free(uris);
3123
3124 break;
3125 }
3126 case LTTNG_DESTROY_SESSION:
3127 {
3128 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
3129
3130 /* Set session to NULL so we do not unlock it after free. */
3131 cmd_ctx->session = NULL;
3132 break;
3133 }
3134 case LTTNG_LIST_DOMAINS:
3135 {
3136 ssize_t nb_dom;
3137 struct lttng_domain *domains;
3138
3139 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3140 if (nb_dom < 0) {
3141 /* Return value is a negative lttng_error_code. */
3142 ret = -nb_dom;
3143 goto error;
3144 }
3145
3146 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
3147 if (ret < 0) {
3148 free(domains);
3149 goto setup_error;
3150 }
3151
3152 /* Copy event list into message payload */
3153 memcpy(cmd_ctx->llm->payload, domains,
3154 nb_dom * sizeof(struct lttng_domain));
3155
3156 free(domains);
3157
3158 ret = LTTNG_OK;
3159 break;
3160 }
3161 case LTTNG_LIST_CHANNELS:
3162 {
3163 int nb_chan;
3164 struct lttng_channel *channels;
3165
3166 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3167 cmd_ctx->session, &channels);
3168 if (nb_chan < 0) {
3169 /* Return value is a negative lttng_error_code. */
3170 ret = -nb_chan;
3171 goto error;
3172 }
3173
3174 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
3175 if (ret < 0) {
3176 free(channels);
3177 goto setup_error;
3178 }
3179
3180 /* Copy event list into message payload */
3181 memcpy(cmd_ctx->llm->payload, channels,
3182 nb_chan * sizeof(struct lttng_channel));
3183
3184 free(channels);
3185
3186 ret = LTTNG_OK;
3187 break;
3188 }
3189 case LTTNG_LIST_EVENTS:
3190 {
3191 ssize_t nb_event;
3192 struct lttng_event *events = NULL;
3193
3194 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3195 cmd_ctx->lsm->u.list.channel_name, &events);
3196 if (nb_event < 0) {
3197 /* Return value is a negative lttng_error_code. */
3198 ret = -nb_event;
3199 goto error;
3200 }
3201
3202 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
3203 if (ret < 0) {
3204 free(events);
3205 goto setup_error;
3206 }
3207
3208 /* Copy event list into message payload */
3209 memcpy(cmd_ctx->llm->payload, events,
3210 nb_event * sizeof(struct lttng_event));
3211
3212 free(events);
3213
3214 ret = LTTNG_OK;
3215 break;
3216 }
3217 case LTTNG_LIST_SESSIONS:
3218 {
3219 unsigned int nr_sessions;
3220
3221 session_lock_list();
3222 nr_sessions = lttng_sessions_count(
3223 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3224 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3225
3226 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
3227 if (ret < 0) {
3228 session_unlock_list();
3229 goto setup_error;
3230 }
3231
3232 /* Filled the session array */
3233 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
3234 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3235 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3236
3237 session_unlock_list();
3238
3239 ret = LTTNG_OK;
3240 break;
3241 }
3242 case LTTNG_CALIBRATE:
3243 {
3244 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3245 &cmd_ctx->lsm->u.calibrate);
3246 break;
3247 }
3248 case LTTNG_REGISTER_CONSUMER:
3249 {
3250 struct consumer_data *cdata;
3251
3252 switch (cmd_ctx->lsm->domain.type) {
3253 case LTTNG_DOMAIN_KERNEL:
3254 cdata = &kconsumer_data;
3255 break;
3256 default:
3257 ret = LTTNG_ERR_UND;
3258 goto error;
3259 }
3260
3261 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3262 cmd_ctx->lsm->u.reg.path, cdata);
3263 break;
3264 }
3265 case LTTNG_ENABLE_EVENT_WITH_FILTER:
3266 {
3267 struct lttng_filter_bytecode *bytecode;
3268
3269 if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) {
3270 ret = LTTNG_ERR_FILTER_INVAL;
3271 goto error;
3272 }
3273 if (cmd_ctx->lsm->u.enable.bytecode_len == 0) {
3274 ret = LTTNG_ERR_FILTER_INVAL;
3275 goto error;
3276 }
3277 bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len);
3278 if (!bytecode) {
3279 ret = LTTNG_ERR_FILTER_NOMEM;
3280 goto error;
3281 }
3282 /* Receive var. len. data */
3283 DBG("Receiving var len data from client ...");
3284 ret = lttcomm_recv_unix_sock(sock, bytecode,
3285 cmd_ctx->lsm->u.enable.bytecode_len);
3286 if (ret <= 0) {
3287 DBG("Nothing recv() from client var len data... continuing");
3288 *sock_error = 1;
3289 ret = LTTNG_ERR_FILTER_INVAL;
3290 goto error;
3291 }
3292
3293 if (bytecode->len + sizeof(*bytecode)
3294 != cmd_ctx->lsm->u.enable.bytecode_len) {
3295 free(bytecode);
3296 ret = LTTNG_ERR_FILTER_INVAL;
3297 goto error;
3298 }
3299
3300 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3301 cmd_ctx->lsm->u.enable.channel_name,
3302 &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]);
3303 break;
3304 }
3305 case LTTNG_DATA_PENDING:
3306 {
3307 ret = cmd_data_pending(cmd_ctx->session);
3308 break;
3309 }
3310 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3311 {
3312 struct lttcomm_lttng_output_id reply;
3313
3314 ret = cmd_snapshot_add_output(cmd_ctx->session,
3315 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3316 if (ret != LTTNG_OK) {
3317 goto error;
3318 }
3319
3320 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3321 if (ret < 0) {
3322 goto setup_error;
3323 }
3324
3325 /* Copy output list into message payload */
3326 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3327 ret = LTTNG_OK;
3328 break;
3329 }
3330 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3331 {
3332 ret = cmd_snapshot_del_output(cmd_ctx->session,
3333 &cmd_ctx->lsm->u.snapshot_output.output);
3334 break;
3335 }
3336 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3337 {
3338 ssize_t nb_output;
3339 struct lttng_snapshot_output *outputs = NULL;
3340
3341 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3342 if (nb_output < 0) {
3343 ret = -nb_output;
3344 goto error;
3345 }
3346
3347 ret = setup_lttng_msg(cmd_ctx,
3348 nb_output * sizeof(struct lttng_snapshot_output));
3349 if (ret < 0) {
3350 free(outputs);
3351 goto setup_error;
3352 }
3353
3354 if (outputs) {
3355 /* Copy output list into message payload */
3356 memcpy(cmd_ctx->llm->payload, outputs,
3357 nb_output * sizeof(struct lttng_snapshot_output));
3358 free(outputs);
3359 }
3360
3361 ret = LTTNG_OK;
3362 break;
3363 }
3364 case LTTNG_SNAPSHOT_RECORD:
3365 {
3366 ret = cmd_snapshot_record(cmd_ctx->session,
3367 &cmd_ctx->lsm->u.snapshot_record.output,
3368 cmd_ctx->lsm->u.snapshot_record.wait);
3369 break;
3370 }
3371 case LTTNG_CREATE_SESSION_SNAPSHOT:
3372 {
3373 size_t nb_uri, len;
3374 struct lttng_uri *uris = NULL;
3375
3376 nb_uri = cmd_ctx->lsm->u.uri.size;
3377 len = nb_uri * sizeof(struct lttng_uri);
3378
3379 if (nb_uri > 0) {
3380 uris = zmalloc(len);
3381 if (uris == NULL) {
3382 ret = LTTNG_ERR_FATAL;
3383 goto error;
3384 }
3385
3386 /* Receive variable len data */
3387 DBG("Waiting for %zu URIs from client ...", nb_uri);
3388 ret = lttcomm_recv_unix_sock(sock, uris, len);
3389 if (ret <= 0) {
3390 DBG("No URIs received from client... continuing");
3391 *sock_error = 1;
3392 ret = LTTNG_ERR_SESSION_FAIL;
3393 free(uris);
3394 goto error;
3395 }
3396
3397 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3398 DBG("Creating session with ONE network URI is a bad call");
3399 ret = LTTNG_ERR_SESSION_FAIL;
3400 free(uris);
3401 goto error;
3402 }
3403 }
3404
3405 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3406 nb_uri, &cmd_ctx->creds);
3407 free(uris);
3408 break;
3409 }
3410 case LTTNG_CREATE_SESSION_LIVE:
3411 {
3412 size_t nb_uri, len;
3413 struct lttng_uri *uris = NULL;
3414
3415 nb_uri = cmd_ctx->lsm->u.uri.size;
3416 len = nb_uri * sizeof(struct lttng_uri);
3417
3418 if (nb_uri > 0) {
3419 uris = zmalloc(len);
3420 if (uris == NULL) {
3421 ret = LTTNG_ERR_FATAL;
3422 goto error;
3423 }
3424
3425 /* Receive variable len data */
3426 DBG("Waiting for %zu URIs from client ...", nb_uri);
3427 ret = lttcomm_recv_unix_sock(sock, uris, len);
3428 if (ret <= 0) {
3429 DBG("No URIs received from client... continuing");
3430 *sock_error = 1;
3431 ret = LTTNG_ERR_SESSION_FAIL;
3432 free(uris);
3433 goto error;
3434 }
3435
3436 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3437 DBG("Creating session with ONE network URI is a bad call");
3438 ret = LTTNG_ERR_SESSION_FAIL;
3439 free(uris);
3440 goto error;
3441 }
3442 }
3443
3444 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3445 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3446 free(uris);
3447 break;
3448 }
3449 default:
3450 ret = LTTNG_ERR_UND;
3451 break;
3452 }
3453
3454 error:
3455 if (cmd_ctx->llm == NULL) {
3456 DBG("Missing llm structure. Allocating one.");
3457 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3458 goto setup_error;
3459 }
3460 }
3461 /* Set return code */
3462 cmd_ctx->llm->ret_code = ret;
3463 setup_error:
3464 if (cmd_ctx->session) {
3465 session_unlock(cmd_ctx->session);
3466 }
3467 if (need_tracing_session) {
3468 session_unlock_list();
3469 }
3470 init_setup_error:
3471 return ret;
3472 }
3473
3474 /*
3475 * Thread managing health check socket.
3476 */
3477 static void *thread_manage_health(void *data)
3478 {
3479 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3480 uint32_t revents, nb_fd;
3481 struct lttng_poll_event events;
3482 struct health_comm_msg msg;
3483 struct health_comm_reply reply;
3484
3485 DBG("[thread] Manage health check started");
3486
3487 rcu_register_thread();
3488
3489 /* We might hit an error path before this is created. */
3490 lttng_poll_init(&events);
3491
3492 /* Create unix socket */
3493 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3494 if (sock < 0) {
3495 ERR("Unable to create health check Unix socket");
3496 ret = -1;
3497 goto error;
3498 }
3499
3500 if (is_root) {
3501 /* lttng health client socket path permissions */
3502 ret = chown(health_unix_sock_path, 0,
3503 utils_get_group_id(tracing_group_name));
3504 if (ret < 0) {
3505 ERR("Unable to set group on %s", health_unix_sock_path);
3506 PERROR("chown");
3507 ret = -1;
3508 goto error;
3509 }
3510
3511 ret = chmod(health_unix_sock_path,
3512 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3513 if (ret < 0) {
3514 ERR("Unable to set permissions on %s", health_unix_sock_path);
3515 PERROR("chmod");
3516 ret = -1;
3517 goto error;
3518 }
3519 }
3520
3521 /*
3522 * Set the CLOEXEC flag. Return code is useless because either way, the
3523 * show must go on.
3524 */
3525 (void) utils_set_fd_cloexec(sock);
3526
3527 ret = lttcomm_listen_unix_sock(sock);
3528 if (ret < 0) {
3529 goto error;
3530 }
3531
3532 /*
3533 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3534 * more will be added to this poll set.
3535 */
3536 ret = sessiond_set_thread_pollset(&events, 2);
3537 if (ret < 0) {
3538 goto error;
3539 }
3540
3541 /* Add the application registration socket */
3542 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3543 if (ret < 0) {
3544 goto error;
3545 }
3546
3547 while (1) {
3548 DBG("Health check ready");
3549
3550 /* Inifinite blocking call, waiting for transmission */
3551 restart:
3552 ret = lttng_poll_wait(&events, -1);
3553 if (ret < 0) {
3554 /*
3555 * Restart interrupted system call.
3556 */
3557 if (errno == EINTR) {
3558 goto restart;
3559 }
3560 goto error;
3561 }
3562
3563 nb_fd = ret;
3564
3565 for (i = 0; i < nb_fd; i++) {
3566 /* Fetch once the poll data */
3567 revents = LTTNG_POLL_GETEV(&events, i);
3568 pollfd = LTTNG_POLL_GETFD(&events, i);
3569
3570 /* Thread quit pipe has been closed. Killing thread. */
3571 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3572 if (ret) {
3573 err = 0;
3574 goto exit;
3575 }
3576
3577 /* Event on the registration socket */
3578 if (pollfd == sock) {
3579 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3580 ERR("Health socket poll error");
3581 goto error;
3582 }
3583 }
3584 }
3585
3586 new_sock = lttcomm_accept_unix_sock(sock);
3587 if (new_sock < 0) {
3588 goto error;
3589 }
3590
3591 /*
3592 * Set the CLOEXEC flag. Return code is useless because either way, the
3593 * show must go on.
3594 */
3595 (void) utils_set_fd_cloexec(new_sock);
3596
3597 DBG("Receiving data from client for health...");
3598 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3599 if (ret <= 0) {
3600 DBG("Nothing recv() from client... continuing");
3601 ret = close(new_sock);
3602 if (ret) {
3603 PERROR("close");
3604 }
3605 new_sock = -1;
3606 continue;
3607 }
3608
3609 rcu_thread_online();
3610
3611 reply.ret_code = 0;
3612 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
3613 /*
3614 * health_check_state returns 0 if health is
3615 * bad.
3616 */
3617 if (!health_check_state(health_sessiond, i)) {
3618 reply.ret_code |= 1ULL << i;
3619 }
3620 }
3621
3622 DBG2("Health check return value %" PRIx64, reply.ret_code);
3623
3624 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3625 if (ret < 0) {
3626 ERR("Failed to send health data back to client");
3627 }
3628
3629 /* End of transmission */
3630 ret = close(new_sock);
3631 if (ret) {
3632 PERROR("close");
3633 }
3634 new_sock = -1;
3635 }
3636
3637 exit:
3638 error:
3639 if (err) {
3640 ERR("Health error occurred in %s", __func__);
3641 }
3642 DBG("Health check thread dying");
3643 unlink(health_unix_sock_path);
3644 if (sock >= 0) {
3645 ret = close(sock);
3646 if (ret) {
3647 PERROR("close");
3648 }
3649 }
3650
3651 lttng_poll_clean(&events);
3652
3653 rcu_unregister_thread();
3654 return NULL;
3655 }
3656
3657 /*
3658 * This thread manage all clients request using the unix client socket for
3659 * communication.
3660 */
3661 static void *thread_manage_clients(void *data)
3662 {
3663 int sock = -1, ret, i, pollfd, err = -1;
3664 int sock_error;
3665 uint32_t revents, nb_fd;
3666 struct command_ctx *cmd_ctx = NULL;
3667 struct lttng_poll_event events;
3668
3669 DBG("[thread] Manage client started");
3670
3671 rcu_register_thread();
3672
3673 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
3674
3675 if (testpoint(thread_manage_clients)) {
3676 goto error_testpoint;
3677 }
3678
3679 health_code_update();
3680
3681 ret = lttcomm_listen_unix_sock(client_sock);
3682 if (ret < 0) {
3683 goto error_listen;
3684 }
3685
3686 /*
3687 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3688 * more will be added to this poll set.
3689 */
3690 ret = sessiond_set_thread_pollset(&events, 2);
3691 if (ret < 0) {
3692 goto error_create_poll;
3693 }
3694
3695 /* Add the application registration socket */
3696 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3697 if (ret < 0) {
3698 goto error;
3699 }
3700
3701 /*
3702 * Notify parent pid that we are ready to accept command for client side.
3703 */
3704 if (opt_sig_parent) {
3705 kill(ppid, SIGUSR1);
3706 }
3707
3708 if (testpoint(thread_manage_clients_before_loop)) {
3709 goto error;
3710 }
3711
3712 health_code_update();
3713
3714 while (1) {
3715 DBG("Accepting client command ...");
3716
3717 /* Inifinite blocking call, waiting for transmission */
3718 restart:
3719 health_poll_entry();
3720 ret = lttng_poll_wait(&events, -1);
3721 health_poll_exit();
3722 if (ret < 0) {
3723 /*
3724 * Restart interrupted system call.
3725 */
3726 if (errno == EINTR) {
3727 goto restart;
3728 }
3729 goto error;
3730 }
3731
3732 nb_fd = ret;
3733
3734 for (i = 0; i < nb_fd; i++) {
3735 /* Fetch once the poll data */
3736 revents = LTTNG_POLL_GETEV(&events, i);
3737 pollfd = LTTNG_POLL_GETFD(&events, i);
3738
3739 health_code_update();
3740
3741 /* Thread quit pipe has been closed. Killing thread. */
3742 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3743 if (ret) {
3744 err = 0;
3745 goto exit;
3746 }
3747
3748 /* Event on the registration socket */
3749 if (pollfd == client_sock) {
3750 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3751 ERR("Client socket poll error");
3752 goto error;
3753 }
3754 }
3755 }
3756
3757 DBG("Wait for client response");
3758
3759 health_code_update();
3760
3761 sock = lttcomm_accept_unix_sock(client_sock);
3762 if (sock < 0) {
3763 goto error;
3764 }
3765
3766 /*
3767 * Set the CLOEXEC flag. Return code is useless because either way, the
3768 * show must go on.
3769 */
3770 (void) utils_set_fd_cloexec(sock);
3771
3772 /* Set socket option for credentials retrieval */
3773 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3774 if (ret < 0) {
3775 goto error;
3776 }
3777
3778 /* Allocate context command to process the client request */
3779 cmd_ctx = zmalloc(sizeof(struct command_ctx));
3780 if (cmd_ctx == NULL) {
3781 PERROR("zmalloc cmd_ctx");
3782 goto error;
3783 }
3784
3785 /* Allocate data buffer for reception */
3786 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
3787 if (cmd_ctx->lsm == NULL) {
3788 PERROR("zmalloc cmd_ctx->lsm");
3789 goto error;
3790 }
3791
3792 cmd_ctx->llm = NULL;
3793 cmd_ctx->session = NULL;
3794
3795 health_code_update();
3796
3797 /*
3798 * Data is received from the lttng client. The struct
3799 * lttcomm_session_msg (lsm) contains the command and data request of
3800 * the client.
3801 */
3802 DBG("Receiving data from client ...");
3803 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3804 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
3805 if (ret <= 0) {
3806 DBG("Nothing recv() from client... continuing");
3807 ret = close(sock);
3808 if (ret) {
3809 PERROR("close");
3810 }
3811 sock = -1;
3812 clean_command_ctx(&cmd_ctx);
3813 continue;
3814 }
3815
3816 health_code_update();
3817
3818 // TODO: Validate cmd_ctx including sanity check for
3819 // security purpose.
3820
3821 rcu_thread_online();
3822 /*
3823 * This function dispatch the work to the kernel or userspace tracer
3824 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3825 * informations for the client. The command context struct contains
3826 * everything this function may needs.
3827 */
3828 ret = process_client_msg(cmd_ctx, sock, &sock_error);
3829 rcu_thread_offline();
3830 if (ret < 0) {
3831 ret = close(sock);
3832 if (ret) {
3833 PERROR("close");
3834 }
3835 sock = -1;
3836 /*
3837 * TODO: Inform client somehow of the fatal error. At
3838 * this point, ret < 0 means that a zmalloc failed
3839 * (ENOMEM). Error detected but still accept
3840 * command, unless a socket error has been
3841 * detected.
3842 */
3843 clean_command_ctx(&cmd_ctx);
3844 continue;
3845 }
3846
3847 health_code_update();
3848
3849 DBG("Sending response (size: %d, retcode: %s)",
3850 cmd_ctx->lttng_msg_size,
3851 lttng_strerror(-cmd_ctx->llm->ret_code));
3852 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
3853 if (ret < 0) {
3854 ERR("Failed to send data back to client");
3855 }
3856
3857 /* End of transmission */
3858 ret = close(sock);
3859 if (ret) {
3860 PERROR("close");
3861 }
3862 sock = -1;
3863
3864 clean_command_ctx(&cmd_ctx);
3865
3866 health_code_update();
3867 }
3868
3869 exit:
3870 error:
3871 if (sock >= 0) {
3872 ret = close(sock);
3873 if (ret) {
3874 PERROR("close");
3875 }
3876 }
3877
3878 lttng_poll_clean(&events);
3879 clean_command_ctx(&cmd_ctx);
3880
3881 error_listen:
3882 error_create_poll:
3883 error_testpoint:
3884 unlink(client_unix_sock_path);
3885 if (client_sock >= 0) {
3886 ret = close(client_sock);
3887 if (ret) {
3888 PERROR("close");
3889 }
3890 }
3891
3892 if (err) {
3893 health_error();
3894 ERR("Health error occurred in %s", __func__);
3895 }
3896
3897 health_unregister(health_sessiond);
3898
3899 DBG("Client thread dying");
3900
3901 rcu_unregister_thread();
3902 return NULL;
3903 }
3904
3905
3906 /*
3907 * usage function on stderr
3908 */
3909 static void usage(void)
3910 {
3911 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
3912 fprintf(stderr, " -h, --help Display this usage.\n");
3913 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3914 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3915 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3916 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3917 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3918 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3919 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3920 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3921 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3922 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3923 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3924 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3925 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
3926 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3927 fprintf(stderr, " -V, --version Show version number.\n");
3928 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3929 fprintf(stderr, " -q, --quiet No output at all.\n");
3930 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3931 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3932 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3933 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
3934 fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n");
3935 }
3936
3937 /*
3938 * daemon argument parsing
3939 */
3940 static int parse_args(int argc, char **argv)
3941 {
3942 int c;
3943
3944 static struct option long_options[] = {
3945 { "client-sock", 1, 0, 'c' },
3946 { "apps-sock", 1, 0, 'a' },
3947 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3948 { "kconsumerd-err-sock", 1, 0, 'E' },
3949 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3950 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3951 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3952 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3953 { "consumerd32-path", 1, 0, 'u' },
3954 { "consumerd32-libdir", 1, 0, 'U' },
3955 { "consumerd64-path", 1, 0, 't' },
3956 { "consumerd64-libdir", 1, 0, 'T' },
3957 { "daemonize", 0, 0, 'd' },
3958 { "sig-parent", 0, 0, 'S' },
3959 { "help", 0, 0, 'h' },
3960 { "group", 1, 0, 'g' },
3961 { "version", 0, 0, 'V' },
3962 { "quiet", 0, 0, 'q' },
3963 { "verbose", 0, 0, 'v' },
3964 { "verbose-consumer", 0, 0, 'Z' },
3965 { "no-kernel", 0, 0, 'N' },
3966 { "pidfile", 1, 0, 'p' },
3967 { "jul-tcp-port", 1, 0, 'J' },
3968 { NULL, 0, 0, 0 }
3969 };
3970
3971 while (1) {
3972 int option_index = 0;
3973 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:",
3974 long_options, &option_index);
3975 if (c == -1) {
3976 break;
3977 }
3978
3979 switch (c) {
3980 case 0:
3981 fprintf(stderr, "option %s", long_options[option_index].name);
3982 if (optarg) {
3983 fprintf(stderr, " with arg %s\n", optarg);
3984 }
3985 break;
3986 case 'c':
3987 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
3988 break;
3989 case 'a':
3990 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
3991 break;
3992 case 'd':
3993 opt_daemon = 1;
3994 break;
3995 case 'g':
3996 tracing_group_name = optarg;
3997 break;
3998 case 'h':
3999 usage();
4000 exit(EXIT_FAILURE);
4001 case 'V':
4002 fprintf(stdout, "%s\n", VERSION);
4003 exit(EXIT_SUCCESS);
4004 case 'S':
4005 opt_sig_parent = 1;
4006 break;
4007 case 'E':
4008 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4009 break;
4010 case 'C':
4011 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4012 break;
4013 case 'F':
4014 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4015 break;
4016 case 'D':
4017 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4018 break;
4019 case 'H':
4020 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
4021 break;
4022 case 'G':
4023 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
4024 break;
4025 case 'N':
4026 opt_no_kernel = 1;
4027 break;
4028 case 'q':
4029 lttng_opt_quiet = 1;
4030 break;
4031 case 'v':
4032 /* Verbose level can increase using multiple -v */
4033 lttng_opt_verbose += 1;
4034 break;
4035 case 'Z':
4036 opt_verbose_consumer += 1;
4037 break;
4038 case 'u':
4039 consumerd32_bin= optarg;
4040 break;
4041 case 'U':
4042 consumerd32_libdir = optarg;
4043 break;
4044 case 't':
4045 consumerd64_bin = optarg;
4046 break;
4047 case 'T':
4048 consumerd64_libdir = optarg;
4049 break;
4050 case 'p':
4051 opt_pidfile = optarg;
4052 break;
4053 case 'J': /* JUL TCP port. */
4054 {
4055 unsigned long v;
4056
4057 errno = 0;
4058 v = strtoul(optarg, NULL, 0);
4059 if (errno != 0 || !isdigit(optarg[0])) {
4060 ERR("Wrong value in --jul-tcp-port parameter: %s", optarg);
4061 return -1;
4062 }
4063 if (v == 0 || v >= 65535) {
4064 ERR("Port overflow in --jul-tcp-port parameter: %s", optarg);
4065 return -1;
4066 }
4067 jul_tcp_port = (uint32_t) v;
4068 DBG3("JUL TCP port set to non default: %u", jul_tcp_port);
4069 break;
4070 }
4071 default:
4072 /* Unknown option or other error.
4073 * Error is printed by getopt, just return */
4074 return -1;
4075 }
4076 }
4077
4078 return 0;
4079 }
4080
4081 /*
4082 * Creates the two needed socket by the daemon.
4083 * apps_sock - The communication socket for all UST apps.
4084 * client_sock - The communication of the cli tool (lttng).
4085 */
4086 static int init_daemon_socket(void)
4087 {
4088 int ret = 0;
4089 mode_t old_umask;
4090
4091 old_umask = umask(0);
4092
4093 /* Create client tool unix socket */
4094 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4095 if (client_sock < 0) {
4096 ERR("Create unix sock failed: %s", client_unix_sock_path);
4097 ret = -1;
4098 goto end;
4099 }
4100
4101 /* Set the cloexec flag */
4102 ret = utils_set_fd_cloexec(client_sock);
4103 if (ret < 0) {
4104 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4105 "Continuing but note that the consumer daemon will have a "
4106 "reference to this socket on exec()", client_sock);
4107 }
4108
4109 /* File permission MUST be 660 */
4110 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4111 if (ret < 0) {
4112 ERR("Set file permissions failed: %s", client_unix_sock_path);
4113 PERROR("chmod");
4114 goto end;
4115 }
4116
4117 /* Create the application unix socket */
4118 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4119 if (apps_sock < 0) {
4120 ERR("Create unix sock failed: %s", apps_unix_sock_path);
4121 ret = -1;
4122 goto end;
4123 }
4124
4125 /* Set the cloexec flag */
4126 ret = utils_set_fd_cloexec(apps_sock);
4127 if (ret < 0) {
4128 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4129 "Continuing but note that the consumer daemon will have a "
4130 "reference to this socket on exec()", apps_sock);
4131 }
4132
4133 /* File permission MUST be 666 */
4134 ret = chmod(apps_unix_sock_path,
4135 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
4136 if (ret < 0) {
4137 ERR("Set file permissions failed: %s", apps_unix_sock_path);
4138 PERROR("chmod");
4139 goto end;
4140 }
4141
4142 DBG3("Session daemon client socket %d and application socket %d created",
4143 client_sock, apps_sock);
4144
4145 end:
4146 umask(old_umask);
4147 return ret;
4148 }
4149
4150 /*
4151 * Check if the global socket is available, and if a daemon is answering at the
4152 * other side. If yes, error is returned.
4153 */
4154 static int check_existing_daemon(void)
4155 {
4156 /* Is there anybody out there ? */
4157 if (lttng_session_daemon_alive()) {
4158 return -EEXIST;
4159 }
4160
4161 return 0;
4162 }
4163
4164 /*
4165 * Set the tracing group gid onto the client socket.
4166 *
4167 * Race window between mkdir and chown is OK because we are going from more
4168 * permissive (root.root) to less permissive (root.tracing).
4169 */
4170 static int set_permissions(char *rundir)
4171 {
4172 int ret;
4173 gid_t gid;
4174
4175 gid = utils_get_group_id(tracing_group_name);
4176
4177 /* Set lttng run dir */
4178 ret = chown(rundir, 0, gid);
4179 if (ret < 0) {
4180 ERR("Unable to set group on %s", rundir);
4181 PERROR("chown");
4182 }
4183
4184 /*
4185 * Ensure all applications and tracing group can search the run
4186 * dir. Allow everyone to read the directory, since it does not
4187 * buy us anything to hide its content.
4188 */
4189 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
4190 if (ret < 0) {
4191 ERR("Unable to set permissions on %s", rundir);
4192 PERROR("chmod");
4193 }
4194
4195 /* lttng client socket path */
4196 ret = chown(client_unix_sock_path, 0, gid);
4197 if (ret < 0) {
4198 ERR("Unable to set group on %s", client_unix_sock_path);
4199 PERROR("chown");
4200 }
4201
4202 /* kconsumer error socket path */
4203 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
4204 if (ret < 0) {
4205 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
4206 PERROR("chown");
4207 }
4208
4209 /* 64-bit ustconsumer error socket path */
4210 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
4211 if (ret < 0) {
4212 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
4213 PERROR("chown");
4214 }
4215
4216 /* 32-bit ustconsumer compat32 error socket path */
4217 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
4218 if (ret < 0) {
4219 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
4220 PERROR("chown");
4221 }
4222
4223 DBG("All permissions are set");
4224
4225 return ret;
4226 }
4227
4228 /*
4229 * Create the lttng run directory needed for all global sockets and pipe.
4230 */
4231 static int create_lttng_rundir(const char *rundir)
4232 {
4233 int ret;
4234
4235 DBG3("Creating LTTng run directory: %s", rundir);
4236
4237 ret = mkdir(rundir, S_IRWXU);
4238 if (ret < 0) {
4239 if (errno != EEXIST) {
4240 ERR("Unable to create %s", rundir);
4241 goto error;
4242 } else {
4243 ret = 0;
4244 }
4245 }
4246
4247 error:
4248 return ret;
4249 }
4250
4251 /*
4252 * Setup sockets and directory needed by the kconsumerd communication with the
4253 * session daemon.
4254 */
4255 static int set_consumer_sockets(struct consumer_data *consumer_data,
4256 const char *rundir)
4257 {
4258 int ret;
4259 char path[PATH_MAX];
4260
4261 switch (consumer_data->type) {
4262 case LTTNG_CONSUMER_KERNEL:
4263 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
4264 break;
4265 case LTTNG_CONSUMER64_UST:
4266 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
4267 break;
4268 case LTTNG_CONSUMER32_UST:
4269 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
4270 break;
4271 default:
4272 ERR("Consumer type unknown");
4273 ret = -EINVAL;
4274 goto error;
4275 }
4276
4277 DBG2("Creating consumer directory: %s", path);
4278
4279 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
4280 if (ret < 0) {
4281 if (errno != EEXIST) {
4282 PERROR("mkdir");
4283 ERR("Failed to create %s", path);
4284 goto error;
4285 }
4286 ret = -1;
4287 }
4288 if (is_root) {
4289 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
4290 if (ret < 0) {
4291 ERR("Unable to set group on %s", path);
4292 PERROR("chown");
4293 goto error;
4294 }
4295 }
4296
4297 /* Create the kconsumerd error unix socket */
4298 consumer_data->err_sock =
4299 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
4300 if (consumer_data->err_sock < 0) {
4301 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
4302 ret = -1;
4303 goto error;
4304 }
4305
4306 /*
4307 * Set the CLOEXEC flag. Return code is useless because either way, the
4308 * show must go on.
4309 */
4310 ret = utils_set_fd_cloexec(consumer_data->err_sock);
4311 if (ret < 0) {
4312 PERROR("utils_set_fd_cloexec");
4313 /* continue anyway */
4314 }
4315
4316 /* File permission MUST be 660 */
4317 ret = chmod(consumer_data->err_unix_sock_path,
4318 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4319 if (ret < 0) {
4320 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
4321 PERROR("chmod");
4322 goto error;
4323 }
4324
4325 error:
4326 return ret;
4327 }
4328
4329 /*
4330 * Signal handler for the daemon
4331 *
4332 * Simply stop all worker threads, leaving main() return gracefully after
4333 * joining all threads and calling cleanup().
4334 */
4335 static void sighandler(int sig)
4336 {
4337 switch (sig) {
4338 case SIGPIPE:
4339 DBG("SIGPIPE caught");
4340 return;
4341 case SIGINT:
4342 DBG("SIGINT caught");
4343 stop_threads();
4344 break;
4345 case SIGTERM:
4346 DBG("SIGTERM caught");
4347 stop_threads();
4348 break;
4349 default:
4350 break;
4351 }
4352 }
4353
4354 /*
4355 * Setup signal handler for :
4356 * SIGINT, SIGTERM, SIGPIPE
4357 */
4358 static int set_signal_handler(void)
4359 {
4360 int ret = 0;
4361 struct sigaction sa;
4362 sigset_t sigset;
4363
4364 if ((ret = sigemptyset(&sigset)) < 0) {
4365 PERROR("sigemptyset");
4366 return ret;
4367 }
4368
4369 sa.sa_handler = sighandler;
4370 sa.sa_mask = sigset;
4371 sa.sa_flags = 0;
4372 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
4373 PERROR("sigaction");
4374 return ret;
4375 }
4376
4377 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
4378 PERROR("sigaction");
4379 return ret;
4380 }
4381
4382 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
4383 PERROR("sigaction");
4384 return ret;
4385 }
4386
4387 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
4388
4389 return ret;
4390 }
4391
4392 /*
4393 * Set open files limit to unlimited. This daemon can open a large number of
4394 * file descriptors in order to consumer multiple kernel traces.
4395 */
4396 static void set_ulimit(void)
4397 {
4398 int ret;
4399 struct rlimit lim;
4400
4401 /* The kernel does not allowed an infinite limit for open files */
4402 lim.rlim_cur = 65535;
4403 lim.rlim_max = 65535;
4404
4405 ret = setrlimit(RLIMIT_NOFILE, &lim);
4406 if (ret < 0) {
4407 PERROR("failed to set open files limit");
4408 }
4409 }
4410
4411 /*
4412 * Write pidfile using the rundir and opt_pidfile.
4413 */
4414 static void write_pidfile(void)
4415 {
4416 int ret;
4417 char pidfile_path[PATH_MAX];
4418
4419 assert(rundir);
4420
4421 if (opt_pidfile) {
4422 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
4423 } else {
4424 /* Build pidfile path from rundir and opt_pidfile. */
4425 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
4426 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
4427 if (ret < 0) {
4428 PERROR("snprintf pidfile path");
4429 goto error;
4430 }
4431 }
4432
4433 /*
4434 * Create pid file in rundir. Return value is of no importance. The
4435 * execution will continue even though we are not able to write the file.
4436 */
4437 (void) utils_create_pid_file(getpid(), pidfile_path);
4438
4439 error:
4440 return;
4441 }
4442
4443 /*
4444 * Write JUL TCP port using the rundir.
4445 */
4446 static void write_julport(void)
4447 {
4448 int ret;
4449 char path[PATH_MAX];
4450
4451 assert(rundir);
4452
4453 ret = snprintf(path, sizeof(path), "%s/"
4454 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir);
4455 if (ret < 0) {
4456 PERROR("snprintf julport path");
4457 goto error;
4458 }
4459
4460 /*
4461 * Create TCP JUL port file in rundir. Return value is of no importance.
4462 * The execution will continue even though we are not able to write the
4463 * file.
4464 */
4465 (void) utils_create_pid_file(jul_tcp_port, path);
4466
4467 error:
4468 return;
4469 }
4470
4471 /*
4472 * main
4473 */
4474 int main(int argc, char **argv)
4475 {
4476 int ret = 0;
4477 void *status;
4478 const char *home_path, *env_app_timeout;
4479
4480 init_kernel_workarounds();
4481
4482 rcu_register_thread();
4483
4484 setup_consumerd_path();
4485
4486 page_size = sysconf(_SC_PAGESIZE);
4487 if (page_size < 0) {
4488 PERROR("sysconf _SC_PAGESIZE");
4489 page_size = LONG_MAX;
4490 WARN("Fallback page size to %ld", page_size);
4491 }
4492
4493 /* Parse arguments */
4494 progname = argv[0];
4495 if ((ret = parse_args(argc, argv)) < 0) {
4496 goto error;
4497 }
4498
4499 /* Daemonize */
4500 if (opt_daemon) {
4501 int i;
4502
4503 /*
4504 * fork
4505 * child: setsid, close FD 0, 1, 2, chdir /
4506 * parent: exit (if fork is successful)
4507 */
4508 ret = daemon(0, 0);
4509 if (ret < 0) {
4510 PERROR("daemon");
4511 goto error;
4512 }
4513 /*
4514 * We are in the child. Make sure all other file
4515 * descriptors are closed, in case we are called with
4516 * more opened file descriptors than the standard ones.
4517 */
4518 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4519 (void) close(i);
4520 }
4521 }
4522
4523 /* Create thread quit pipe */
4524 if ((ret = init_thread_quit_pipe()) < 0) {
4525 goto error;
4526 }
4527
4528 /* Check if daemon is UID = 0 */
4529 is_root = !getuid();
4530
4531 if (is_root) {
4532 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4533
4534 /* Create global run dir with root access */
4535 ret = create_lttng_rundir(rundir);
4536 if (ret < 0) {
4537 goto error;
4538 }
4539
4540 if (strlen(apps_unix_sock_path) == 0) {
4541 snprintf(apps_unix_sock_path, PATH_MAX,
4542 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4543 }
4544
4545 if (strlen(client_unix_sock_path) == 0) {
4546 snprintf(client_unix_sock_path, PATH_MAX,
4547 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4548 }
4549
4550 /* Set global SHM for ust */
4551 if (strlen(wait_shm_path) == 0) {
4552 snprintf(wait_shm_path, PATH_MAX,
4553 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4554 }
4555
4556 if (strlen(health_unix_sock_path) == 0) {
4557 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4558 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4559 }
4560
4561 /* Setup kernel consumerd path */
4562 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
4563 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4564 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
4565 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4566
4567 DBG2("Kernel consumer err path: %s",
4568 kconsumer_data.err_unix_sock_path);
4569 DBG2("Kernel consumer cmd path: %s",
4570 kconsumer_data.cmd_unix_sock_path);
4571 } else {
4572 home_path = utils_get_home_dir();
4573 if (home_path == NULL) {
4574 /* TODO: Add --socket PATH option */
4575 ERR("Can't get HOME directory for sockets creation.");
4576 ret = -EPERM;
4577 goto error;
4578 }
4579
4580 /*
4581 * Create rundir from home path. This will create something like
4582 * $HOME/.lttng
4583 */
4584 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
4585 if (ret < 0) {
4586 ret = -ENOMEM;
4587 goto error;
4588 }
4589
4590 ret = create_lttng_rundir(rundir);
4591 if (ret < 0) {
4592 goto error;
4593 }
4594
4595 if (strlen(apps_unix_sock_path) == 0) {
4596 snprintf(apps_unix_sock_path, PATH_MAX,
4597 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
4598 }
4599
4600 /* Set the cli tool unix socket path */
4601 if (strlen(client_unix_sock_path) == 0) {
4602 snprintf(client_unix_sock_path, PATH_MAX,
4603 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
4604 }
4605
4606 /* Set global SHM for ust */
4607 if (strlen(wait_shm_path) == 0) {
4608 snprintf(wait_shm_path, PATH_MAX,
4609 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
4610 }
4611
4612 /* Set health check Unix path */
4613 if (strlen(health_unix_sock_path) == 0) {
4614 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4615 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4616 }
4617 }
4618
4619 /* Set consumer initial state */
4620 kernel_consumerd_state = CONSUMER_STOPPED;
4621 ust_consumerd_state = CONSUMER_STOPPED;
4622
4623 DBG("Client socket path %s", client_unix_sock_path);
4624 DBG("Application socket path %s", apps_unix_sock_path);
4625 DBG("Application wait path %s", wait_shm_path);
4626 DBG("LTTng run directory path: %s", rundir);
4627
4628 /* 32 bits consumerd path setup */
4629 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
4630 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4631 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
4632 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4633
4634 DBG2("UST consumer 32 bits err path: %s",
4635 ustconsumer32_data.err_unix_sock_path);
4636 DBG2("UST consumer 32 bits cmd path: %s",
4637 ustconsumer32_data.cmd_unix_sock_path);
4638
4639 /* 64 bits consumerd path setup */
4640 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
4641 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4642 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
4643 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4644
4645 DBG2("UST consumer 64 bits err path: %s",
4646 ustconsumer64_data.err_unix_sock_path);
4647 DBG2("UST consumer 64 bits cmd path: %s",
4648 ustconsumer64_data.cmd_unix_sock_path);
4649
4650 /*
4651 * See if daemon already exist.
4652 */
4653 if ((ret = check_existing_daemon()) < 0) {
4654 ERR("Already running daemon.\n");
4655 /*
4656 * We do not goto exit because we must not cleanup()
4657 * because a daemon is already running.
4658 */
4659 goto error;
4660 }
4661
4662 /*
4663 * Init UST app hash table. Alloc hash table before this point since
4664 * cleanup() can get called after that point.
4665 */
4666 ust_app_ht_alloc();
4667
4668 /* Initialize JUL domain subsystem. */
4669 if ((ret = jul_init()) < 0) {
4670 /* ENOMEM at this point. */
4671 goto error;
4672 }
4673
4674 /* After this point, we can safely call cleanup() with "goto exit" */
4675
4676 /*
4677 * These actions must be executed as root. We do that *after* setting up
4678 * the sockets path because we MUST make the check for another daemon using
4679 * those paths *before* trying to set the kernel consumer sockets and init
4680 * kernel tracer.
4681 */
4682 if (is_root) {
4683 ret = set_consumer_sockets(&kconsumer_data, rundir);
4684 if (ret < 0) {
4685 goto exit;
4686 }
4687
4688 /* Setup kernel tracer */
4689 if (!opt_no_kernel) {
4690 init_kernel_tracer();
4691 }
4692
4693 /* Set ulimit for open files */
4694 set_ulimit();
4695 }
4696 /* init lttng_fd tracking must be done after set_ulimit. */
4697 lttng_fd_init();
4698
4699 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4700 if (ret < 0) {
4701 goto exit;
4702 }
4703
4704 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4705 if (ret < 0) {
4706 goto exit;
4707 }
4708
4709 if ((ret = set_signal_handler()) < 0) {
4710 goto exit;
4711 }
4712
4713 /* Setup the needed unix socket */
4714 if ((ret = init_daemon_socket()) < 0) {
4715 goto exit;
4716 }
4717
4718 /* Set credentials to socket */
4719 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
4720 goto exit;
4721 }
4722
4723 /* Get parent pid if -S, --sig-parent is specified. */
4724 if (opt_sig_parent) {
4725 ppid = getppid();
4726 }
4727
4728 /* Setup the kernel pipe for waking up the kernel thread */
4729 if (is_root && !opt_no_kernel) {
4730 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4731 goto exit;
4732 }
4733 }
4734
4735 /* Setup the thread ht_cleanup communication pipe. */
4736 if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) {
4737 goto exit;
4738 }
4739
4740 /* Setup the thread apps communication pipe. */
4741 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
4742 goto exit;
4743 }
4744
4745 /* Setup the thread apps notify communication pipe. */
4746 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
4747 goto exit;
4748 }
4749
4750 /* Initialize global buffer per UID and PID registry. */
4751 buffer_reg_init_uid_registry();
4752 buffer_reg_init_pid_registry();
4753
4754 /* Init UST command queue. */
4755 cds_wfq_init(&ust_cmd_queue.queue);
4756
4757 /*
4758 * Get session list pointer. This pointer MUST NOT be free(). This list is
4759 * statically declared in session.c
4760 */
4761 session_list_ptr = session_get_list();
4762
4763 /* Set up max poll set size */
4764 lttng_poll_set_max_size();
4765
4766 cmd_init();
4767
4768 /* Check for the application socket timeout env variable. */
4769 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
4770 if (env_app_timeout) {
4771 app_socket_timeout = atoi(env_app_timeout);
4772 } else {
4773 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
4774 }
4775
4776 write_pidfile();
4777 write_julport();
4778
4779 /* Initialize communication library */
4780 lttcomm_init();
4781 /* This is to get the TCP timeout value. */
4782 lttcomm_inet_init();
4783
4784 /*
4785 * Initialize the health check subsystem. This call should set the
4786 * appropriate time values.
4787 */
4788 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
4789 if (!health_sessiond) {
4790 PERROR("health_app_create error");
4791 goto exit_health_sessiond_cleanup;
4792 }
4793
4794 /* Create thread to manage the client socket */
4795 ret = pthread_create(&ht_cleanup_thread, NULL,
4796 thread_ht_cleanup, (void *) NULL);
4797 if (ret != 0) {
4798 PERROR("pthread_create ht_cleanup");
4799 goto exit_ht_cleanup;
4800 }
4801
4802 /* Create thread to manage the client socket */
4803 ret = pthread_create(&health_thread, NULL,
4804 thread_manage_health, (void *) NULL);
4805 if (ret != 0) {
4806 PERROR("pthread_create health");
4807 goto exit_health;
4808 }
4809
4810 /* Create thread to manage the client socket */
4811 ret = pthread_create(&client_thread, NULL,
4812 thread_manage_clients, (void *) NULL);
4813 if (ret != 0) {
4814 PERROR("pthread_create clients");
4815 goto exit_client;
4816 }
4817
4818 /* Create thread to dispatch registration */
4819 ret = pthread_create(&dispatch_thread, NULL,
4820 thread_dispatch_ust_registration, (void *) NULL);
4821 if (ret != 0) {
4822 PERROR("pthread_create dispatch");
4823 goto exit_dispatch;
4824 }
4825
4826 /* Create thread to manage application registration. */
4827 ret = pthread_create(&reg_apps_thread, NULL,
4828 thread_registration_apps, (void *) NULL);
4829 if (ret != 0) {
4830 PERROR("pthread_create registration");
4831 goto exit_reg_apps;
4832 }
4833
4834 /* Create thread to manage application socket */
4835 ret = pthread_create(&apps_thread, NULL,
4836 thread_manage_apps, (void *) NULL);
4837 if (ret != 0) {
4838 PERROR("pthread_create apps");
4839 goto exit_apps;
4840 }
4841
4842 /* Create thread to manage application notify socket */
4843 ret = pthread_create(&apps_notify_thread, NULL,
4844 ust_thread_manage_notify, (void *) NULL);
4845 if (ret != 0) {
4846 PERROR("pthread_create apps");
4847 goto exit_apps_notify;
4848 }
4849
4850 /* Create JUL registration thread. */
4851 ret = pthread_create(&jul_reg_thread, NULL,
4852 jul_thread_manage_registration, (void *) NULL);
4853 if (ret != 0) {
4854 PERROR("pthread_create apps");
4855 goto exit_jul_reg;
4856 }
4857
4858 /* Don't start this thread if kernel tracing is not requested nor root */
4859 if (is_root && !opt_no_kernel) {
4860 /* Create kernel thread to manage kernel event */
4861 ret = pthread_create(&kernel_thread, NULL,
4862 thread_manage_kernel, (void *) NULL);
4863 if (ret != 0) {
4864 PERROR("pthread_create kernel");
4865 goto exit_kernel;
4866 }
4867
4868 ret = pthread_join(kernel_thread, &status);
4869 if (ret != 0) {
4870 PERROR("pthread_join");
4871 goto error; /* join error, exit without cleanup */
4872 }
4873 }
4874
4875 exit_kernel:
4876 ret = pthread_join(jul_reg_thread, &status);
4877 if (ret != 0) {
4878 PERROR("pthread_join JUL");
4879 goto error; /* join error, exit without cleanup */
4880 }
4881
4882 exit_jul_reg:
4883 ret = pthread_join(apps_notify_thread, &status);
4884 if (ret != 0) {
4885 PERROR("pthread_join apps notify");
4886 goto error; /* join error, exit without cleanup */
4887 }
4888
4889 exit_apps_notify:
4890 ret = pthread_join(apps_thread, &status);
4891 if (ret != 0) {
4892 PERROR("pthread_join apps");
4893 goto error; /* join error, exit without cleanup */
4894 }
4895
4896
4897 exit_apps:
4898 ret = pthread_join(reg_apps_thread, &status);
4899 if (ret != 0) {
4900 PERROR("pthread_join");
4901 goto error; /* join error, exit without cleanup */
4902 }
4903
4904 exit_reg_apps:
4905 ret = pthread_join(dispatch_thread, &status);
4906 if (ret != 0) {
4907 PERROR("pthread_join");
4908 goto error; /* join error, exit without cleanup */
4909 }
4910
4911 exit_dispatch:
4912 ret = pthread_join(client_thread, &status);
4913 if (ret != 0) {
4914 PERROR("pthread_join");
4915 goto error; /* join error, exit without cleanup */
4916 }
4917
4918 ret = join_consumer_thread(&kconsumer_data);
4919 if (ret != 0) {
4920 PERROR("join_consumer");
4921 goto error; /* join error, exit without cleanup */
4922 }
4923
4924 ret = join_consumer_thread(&ustconsumer32_data);
4925 if (ret != 0) {
4926 PERROR("join_consumer ust32");
4927 goto error; /* join error, exit without cleanup */
4928 }
4929
4930 ret = join_consumer_thread(&ustconsumer64_data);
4931 if (ret != 0) {
4932 PERROR("join_consumer ust64");
4933 goto error; /* join error, exit without cleanup */
4934 }
4935
4936 exit_client:
4937 ret = pthread_join(health_thread, &status);
4938 if (ret != 0) {
4939 PERROR("pthread_join health thread");
4940 goto error; /* join error, exit without cleanup */
4941 }
4942
4943 exit_health:
4944 ret = pthread_join(ht_cleanup_thread, &status);
4945 if (ret != 0) {
4946 PERROR("pthread_join ht cleanup thread");
4947 goto error; /* join error, exit without cleanup */
4948 }
4949 exit_ht_cleanup:
4950 health_app_destroy(health_sessiond);
4951 exit_health_sessiond_cleanup:
4952 exit:
4953 /*
4954 * cleanup() is called when no other thread is running.
4955 */
4956 rcu_thread_online();
4957 cleanup();
4958 rcu_thread_offline();
4959 rcu_unregister_thread();
4960 if (!ret) {
4961 exit(EXIT_SUCCESS);
4962 }
4963 error:
4964 exit(EXIT_FAILURE);
4965 }
This page took 0.16192 seconds and 5 git commands to generate.