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