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