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