85a20d795cb253123c0b2753a02a8fcad98e8436
[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 <sys/mman.h>
29 #include <sys/mount.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <urcu/uatomic.h>
36 #include <unistd.h>
37 #include <config.h>
38
39 #include <common/common.h>
40 #include <common/compat/poll.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 "channel.h"
50 #include "cmd.h"
51 #include "consumer.h"
52 #include "context.h"
53 #include "event.h"
54 #include "kernel.h"
55 #include "kernel-consumer.h"
56 #include "modprobe.h"
57 #include "shm.h"
58 #include "ust-ctl.h"
59 #include "ust-consumer.h"
60 #include "utils.h"
61 #include "fd-limit.h"
62 #include "filter.h"
63 #include "health.h"
64
65 #define CONSUMERD_FILE "lttng-consumerd"
66
67 /* Const values */
68 const char default_home_dir[] = DEFAULT_HOME_DIR;
69 const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
70 const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
71 const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
72
73 const char *progname;
74 const char *opt_tracing_group;
75 static int opt_sig_parent;
76 static int opt_verbose_consumer;
77 static int opt_daemon;
78 static int opt_no_kernel;
79 static int is_root; /* Set to 1 if the daemon is running as root */
80 static pid_t ppid; /* Parent PID for --sig-parent option */
81 static char *rundir;
82
83 /*
84 * Consumer daemon specific control data. Every value not initialized here is
85 * set to 0 by the static definition.
86 */
87 static struct consumer_data kconsumer_data = {
88 .type = LTTNG_CONSUMER_KERNEL,
89 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
90 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
91 .err_sock = -1,
92 .cmd_sock = -1,
93 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
94 .lock = PTHREAD_MUTEX_INITIALIZER,
95 .cond = PTHREAD_COND_INITIALIZER,
96 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
97 };
98 static struct consumer_data ustconsumer64_data = {
99 .type = LTTNG_CONSUMER64_UST,
100 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
101 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
102 .err_sock = -1,
103 .cmd_sock = -1,
104 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
105 .lock = PTHREAD_MUTEX_INITIALIZER,
106 .cond = PTHREAD_COND_INITIALIZER,
107 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
108 };
109 static struct consumer_data ustconsumer32_data = {
110 .type = LTTNG_CONSUMER32_UST,
111 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
112 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
113 .err_sock = -1,
114 .cmd_sock = -1,
115 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
116 .lock = PTHREAD_MUTEX_INITIALIZER,
117 .cond = PTHREAD_COND_INITIALIZER,
118 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
119 };
120
121 /* Shared between threads */
122 static int dispatch_thread_exit;
123
124 /* Global application Unix socket path */
125 static char apps_unix_sock_path[PATH_MAX];
126 /* Global client Unix socket path */
127 static char client_unix_sock_path[PATH_MAX];
128 /* global wait shm path for UST */
129 static char wait_shm_path[PATH_MAX];
130 /* Global health check unix path */
131 static char health_unix_sock_path[PATH_MAX];
132
133 /* Sockets and FDs */
134 static int client_sock = -1;
135 static int apps_sock = -1;
136 int kernel_tracer_fd = -1;
137 static int kernel_poll_pipe[2] = { -1, -1 };
138
139 /*
140 * Quit pipe for all threads. This permits a single cancellation point
141 * for all threads when receiving an event on the pipe.
142 */
143 static int thread_quit_pipe[2] = { -1, -1 };
144
145 /*
146 * This pipe is used to inform the thread managing application communication
147 * that a command is queued and ready to be processed.
148 */
149 static int apps_cmd_pipe[2] = { -1, -1 };
150
151 /* Pthread, Mutexes and Semaphores */
152 static pthread_t apps_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
159 /*
160 * UST registration command queue. This queue is tied with a futex and uses a N
161 * wakers / 1 waiter implemented and detailed in futex.c/.h
162 *
163 * The thread_manage_apps and thread_dispatch_ust_registration interact with
164 * this queue and the wait/wake scheme.
165 */
166 static struct ust_cmd_queue ust_cmd_queue;
167
168 /*
169 * Pointer initialized before thread creation.
170 *
171 * This points to the tracing session list containing the session count and a
172 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
173 * MUST NOT be taken if you call a public function in session.c.
174 *
175 * The lock is nested inside the structure: session_list_ptr->lock. Please use
176 * session_lock_list and session_unlock_list for lock acquisition.
177 */
178 static struct ltt_session_list *session_list_ptr;
179
180 int ust_consumerd64_fd = -1;
181 int ust_consumerd32_fd = -1;
182
183 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
184 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
185 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
186 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
187
188 static const char *module_proc_lttng = "/proc/lttng";
189
190 /*
191 * Consumer daemon state which is changed when spawning it, killing it or in
192 * case of a fatal error.
193 */
194 enum consumerd_state {
195 CONSUMER_STARTED = 1,
196 CONSUMER_STOPPED = 2,
197 CONSUMER_ERROR = 3,
198 };
199
200 /*
201 * This consumer daemon state is used to validate if a client command will be
202 * able to reach the consumer. If not, the client is informed. For instance,
203 * doing a "lttng start" when the consumer state is set to ERROR will return an
204 * error to the client.
205 *
206 * The following example shows a possible race condition of this scheme:
207 *
208 * consumer thread error happens
209 * client cmd arrives
210 * client cmd checks state -> still OK
211 * consumer thread exit, sets error
212 * client cmd try to talk to consumer
213 * ...
214 *
215 * However, since the consumer is a different daemon, we have no way of making
216 * sure the command will reach it safely even with this state flag. This is why
217 * we consider that up to the state validation during command processing, the
218 * command is safe. After that, we can not guarantee the correctness of the
219 * client request vis-a-vis the consumer.
220 */
221 static enum consumerd_state ust_consumerd_state;
222 static enum consumerd_state kernel_consumerd_state;
223
224 /* Used for the health monitoring of the session daemon. See health.h */
225 struct health_state health_thread_cmd;
226 struct health_state health_thread_app_manage;
227 struct health_state health_thread_app_reg;
228 struct health_state health_thread_kernel;
229
230 static
231 void setup_consumerd_path(void)
232 {
233 const char *bin, *libdir;
234
235 /*
236 * Allow INSTALL_BIN_PATH to be used as a target path for the
237 * native architecture size consumer if CONFIG_CONSUMER*_PATH
238 * has not been defined.
239 */
240 #if (CAA_BITS_PER_LONG == 32)
241 if (!consumerd32_bin[0]) {
242 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
243 }
244 if (!consumerd32_libdir[0]) {
245 consumerd32_libdir = INSTALL_LIB_PATH;
246 }
247 #elif (CAA_BITS_PER_LONG == 64)
248 if (!consumerd64_bin[0]) {
249 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
250 }
251 if (!consumerd64_libdir[0]) {
252 consumerd64_libdir = INSTALL_LIB_PATH;
253 }
254 #else
255 #error "Unknown bitness"
256 #endif
257
258 /*
259 * runtime env. var. overrides the build default.
260 */
261 bin = getenv("LTTNG_CONSUMERD32_BIN");
262 if (bin) {
263 consumerd32_bin = bin;
264 }
265 bin = getenv("LTTNG_CONSUMERD64_BIN");
266 if (bin) {
267 consumerd64_bin = bin;
268 }
269 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
270 if (libdir) {
271 consumerd32_libdir = libdir;
272 }
273 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
274 if (libdir) {
275 consumerd64_libdir = libdir;
276 }
277 }
278
279 /*
280 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
281 */
282 static int create_thread_poll_set(struct lttng_poll_event *events,
283 unsigned int size)
284 {
285 int ret;
286
287 if (events == NULL || size == 0) {
288 ret = -1;
289 goto error;
290 }
291
292 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
293 if (ret < 0) {
294 goto error;
295 }
296
297 /* Add quit pipe */
298 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
299 if (ret < 0) {
300 goto error;
301 }
302
303 return 0;
304
305 error:
306 return ret;
307 }
308
309 /*
310 * Check if the thread quit pipe was triggered.
311 *
312 * Return 1 if it was triggered else 0;
313 */
314 static int check_thread_quit_pipe(int fd, uint32_t events)
315 {
316 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
317 return 1;
318 }
319
320 return 0;
321 }
322
323 /*
324 * Return group ID of the tracing group or -1 if not found.
325 */
326 static gid_t allowed_group(void)
327 {
328 struct group *grp;
329
330 if (opt_tracing_group) {
331 grp = getgrnam(opt_tracing_group);
332 } else {
333 grp = getgrnam(default_tracing_group);
334 }
335 if (!grp) {
336 return -1;
337 } else {
338 return grp->gr_gid;
339 }
340 }
341
342 /*
343 * Init thread quit pipe.
344 *
345 * Return -1 on error or 0 if all pipes are created.
346 */
347 static int init_thread_quit_pipe(void)
348 {
349 int ret, i;
350
351 ret = pipe(thread_quit_pipe);
352 if (ret < 0) {
353 PERROR("thread quit pipe");
354 goto error;
355 }
356
357 for (i = 0; i < 2; i++) {
358 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
359 if (ret < 0) {
360 PERROR("fcntl");
361 goto error;
362 }
363 }
364
365 error:
366 return ret;
367 }
368
369 /*
370 * Stop all threads by closing the thread quit pipe.
371 */
372 static void stop_threads(void)
373 {
374 int ret;
375
376 /* Stopping all threads */
377 DBG("Terminating all threads");
378 ret = notify_thread_pipe(thread_quit_pipe[1]);
379 if (ret < 0) {
380 ERR("write error on thread quit pipe");
381 }
382
383 /* Dispatch thread */
384 CMM_STORE_SHARED(dispatch_thread_exit, 1);
385 futex_nto1_wake(&ust_cmd_queue.futex);
386 }
387
388 /*
389 * Cleanup the daemon
390 */
391 static void cleanup(void)
392 {
393 int ret;
394 char *cmd;
395 struct ltt_session *sess, *stmp;
396
397 DBG("Cleaning up");
398
399 /* First thing first, stop all threads */
400 utils_close_pipe(thread_quit_pipe);
401
402 DBG("Removing %s directory", rundir);
403 ret = asprintf(&cmd, "rm -rf %s", rundir);
404 if (ret < 0) {
405 ERR("asprintf failed. Something is really wrong!");
406 }
407
408 /* Remove lttng run directory */
409 ret = system(cmd);
410 if (ret < 0) {
411 ERR("Unable to clean %s", rundir);
412 }
413 free(cmd);
414
415 DBG("Cleaning up all sessions");
416
417 /* Destroy session list mutex */
418 if (session_list_ptr != NULL) {
419 pthread_mutex_destroy(&session_list_ptr->lock);
420
421 /* Cleanup ALL session */
422 cds_list_for_each_entry_safe(sess, stmp,
423 &session_list_ptr->head, list) {
424 cmd_destroy_session(sess, kernel_poll_pipe[1]);
425 }
426 }
427
428 DBG("Closing all UST sockets");
429 ust_app_clean_list();
430
431 if (is_root && !opt_no_kernel) {
432 DBG2("Closing kernel fd");
433 if (kernel_tracer_fd >= 0) {
434 ret = close(kernel_tracer_fd);
435 if (ret) {
436 PERROR("close");
437 }
438 }
439 DBG("Unloading kernel modules");
440 modprobe_remove_lttng_all();
441 }
442
443 utils_close_pipe(kernel_poll_pipe);
444 utils_close_pipe(apps_cmd_pipe);
445
446 /* <fun> */
447 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
448 "Matthew, BEET driven development works!%c[%dm",
449 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
450 /* </fun> */
451 }
452
453 /*
454 * Send data on a unix socket using the liblttsessiondcomm API.
455 *
456 * Return lttcomm error code.
457 */
458 static int send_unix_sock(int sock, void *buf, size_t len)
459 {
460 /* Check valid length */
461 if (len <= 0) {
462 return -1;
463 }
464
465 return lttcomm_send_unix_sock(sock, buf, len);
466 }
467
468 /*
469 * Free memory of a command context structure.
470 */
471 static void clean_command_ctx(struct command_ctx **cmd_ctx)
472 {
473 DBG("Clean command context structure");
474 if (*cmd_ctx) {
475 if ((*cmd_ctx)->llm) {
476 free((*cmd_ctx)->llm);
477 }
478 if ((*cmd_ctx)->lsm) {
479 free((*cmd_ctx)->lsm);
480 }
481 free(*cmd_ctx);
482 *cmd_ctx = NULL;
483 }
484 }
485
486 /*
487 * Notify UST applications using the shm mmap futex.
488 */
489 static int notify_ust_apps(int active)
490 {
491 char *wait_shm_mmap;
492
493 DBG("Notifying applications of session daemon state: %d", active);
494
495 /* See shm.c for this call implying mmap, shm and futex calls */
496 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
497 if (wait_shm_mmap == NULL) {
498 goto error;
499 }
500
501 /* Wake waiting process */
502 futex_wait_update((int32_t *) wait_shm_mmap, active);
503
504 /* Apps notified successfully */
505 return 0;
506
507 error:
508 return -1;
509 }
510
511 /*
512 * Setup the outgoing data buffer for the response (llm) by allocating the
513 * right amount of memory and copying the original information from the lsm
514 * structure.
515 *
516 * Return total size of the buffer pointed by buf.
517 */
518 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
519 {
520 int ret, buf_size;
521
522 buf_size = size;
523
524 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
525 if (cmd_ctx->llm == NULL) {
526 PERROR("zmalloc");
527 ret = -ENOMEM;
528 goto error;
529 }
530
531 /* Copy common data */
532 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
533 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
534
535 cmd_ctx->llm->data_size = size;
536 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
537
538 return buf_size;
539
540 error:
541 return ret;
542 }
543
544 /*
545 * Update the kernel poll set of all channel fd available over all tracing
546 * session. Add the wakeup pipe at the end of the set.
547 */
548 static int update_kernel_poll(struct lttng_poll_event *events)
549 {
550 int ret;
551 struct ltt_session *session;
552 struct ltt_kernel_channel *channel;
553
554 DBG("Updating kernel poll set");
555
556 session_lock_list();
557 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
558 session_lock(session);
559 if (session->kernel_session == NULL) {
560 session_unlock(session);
561 continue;
562 }
563
564 cds_list_for_each_entry(channel,
565 &session->kernel_session->channel_list.head, list) {
566 /* Add channel fd to the kernel poll set */
567 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
568 if (ret < 0) {
569 session_unlock(session);
570 goto error;
571 }
572 DBG("Channel fd %d added to kernel set", channel->fd);
573 }
574 session_unlock(session);
575 }
576 session_unlock_list();
577
578 return 0;
579
580 error:
581 session_unlock_list();
582 return -1;
583 }
584
585 /*
586 * Find the channel fd from 'fd' over all tracing session. When found, check
587 * for new channel stream and send those stream fds to the kernel consumer.
588 *
589 * Useful for CPU hotplug feature.
590 */
591 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
592 {
593 int ret = 0;
594 struct ltt_session *session;
595 struct ltt_kernel_session *ksess;
596 struct ltt_kernel_channel *channel;
597
598 DBG("Updating kernel streams for channel fd %d", fd);
599
600 session_lock_list();
601 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
602 session_lock(session);
603 if (session->kernel_session == NULL) {
604 session_unlock(session);
605 continue;
606 }
607 ksess = session->kernel_session;
608
609 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
610 if (channel->fd == fd) {
611 DBG("Channel found, updating kernel streams");
612 ret = kernel_open_channel_stream(channel);
613 if (ret < 0) {
614 goto error;
615 }
616
617 /*
618 * Have we already sent fds to the consumer? If yes, it means
619 * that tracing is started so it is safe to send our updated
620 * stream fds.
621 */
622 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
623 struct lttng_ht_iter iter;
624 struct consumer_socket *socket;
625
626
627 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
628 &iter.iter, socket, node.node) {
629 /* Code flow error */
630 assert(socket->fd >= 0);
631
632 pthread_mutex_lock(socket->lock);
633 ret = kernel_consumer_send_channel_stream(socket->fd,
634 channel, ksess);
635 pthread_mutex_unlock(socket->lock);
636 if (ret < 0) {
637 goto error;
638 }
639 }
640 }
641 goto error;
642 }
643 }
644 session_unlock(session);
645 }
646 session_unlock_list();
647 return ret;
648
649 error:
650 session_unlock(session);
651 session_unlock_list();
652 return ret;
653 }
654
655 /*
656 * For each tracing session, update newly registered apps.
657 */
658 static void update_ust_app(int app_sock)
659 {
660 struct ltt_session *sess, *stmp;
661
662 session_lock_list();
663
664 /* For all tracing session(s) */
665 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
666 session_lock(sess);
667 if (sess->ust_session) {
668 ust_app_global_update(sess->ust_session, app_sock);
669 }
670 session_unlock(sess);
671 }
672
673 session_unlock_list();
674 }
675
676 /*
677 * This thread manage event coming from the kernel.
678 *
679 * Features supported in this thread:
680 * -) CPU Hotplug
681 */
682 static void *thread_manage_kernel(void *data)
683 {
684 int ret, i, pollfd, update_poll_flag = 1, err = -1;
685 uint32_t revents, nb_fd;
686 char tmp;
687 struct lttng_poll_event events;
688
689 DBG("Thread manage kernel started");
690
691 health_code_update(&health_thread_kernel);
692
693 ret = create_thread_poll_set(&events, 2);
694 if (ret < 0) {
695 goto error_poll_create;
696 }
697
698 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
699 if (ret < 0) {
700 goto error;
701 }
702
703 while (1) {
704 health_code_update(&health_thread_kernel);
705
706 if (update_poll_flag == 1) {
707 /*
708 * Reset number of fd in the poll set. Always 2 since there is the thread
709 * quit pipe and the kernel pipe.
710 */
711 events.nb_fd = 2;
712
713 ret = update_kernel_poll(&events);
714 if (ret < 0) {
715 goto error;
716 }
717 update_poll_flag = 0;
718 }
719
720 nb_fd = LTTNG_POLL_GETNB(&events);
721
722 DBG("Thread kernel polling on %d fds", nb_fd);
723
724 /* Zeroed the poll events */
725 lttng_poll_reset(&events);
726
727 /* Poll infinite value of time */
728 restart:
729 health_poll_update(&health_thread_kernel);
730 ret = lttng_poll_wait(&events, -1);
731 health_poll_update(&health_thread_kernel);
732 if (ret < 0) {
733 /*
734 * Restart interrupted system call.
735 */
736 if (errno == EINTR) {
737 goto restart;
738 }
739 goto error;
740 } else if (ret == 0) {
741 /* Should not happen since timeout is infinite */
742 ERR("Return value of poll is 0 with an infinite timeout.\n"
743 "This should not have happened! Continuing...");
744 continue;
745 }
746
747 for (i = 0; i < nb_fd; i++) {
748 /* Fetch once the poll data */
749 revents = LTTNG_POLL_GETEV(&events, i);
750 pollfd = LTTNG_POLL_GETFD(&events, i);
751
752 health_code_update(&health_thread_kernel);
753
754 /* Thread quit pipe has been closed. Killing thread. */
755 ret = check_thread_quit_pipe(pollfd, revents);
756 if (ret) {
757 err = 0;
758 goto exit;
759 }
760
761 /* Check for data on kernel pipe */
762 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
763 ret = read(kernel_poll_pipe[0], &tmp, 1);
764 update_poll_flag = 1;
765 continue;
766 } else {
767 /*
768 * New CPU detected by the kernel. Adding kernel stream to
769 * kernel session and updating the kernel consumer
770 */
771 if (revents & LPOLLIN) {
772 ret = update_kernel_stream(&kconsumer_data, pollfd);
773 if (ret < 0) {
774 continue;
775 }
776 break;
777 /*
778 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
779 * and unregister kernel stream at this point.
780 */
781 }
782 }
783 }
784 }
785
786 exit:
787 error:
788 lttng_poll_clean(&events);
789 error_poll_create:
790 if (err) {
791 health_error(&health_thread_kernel);
792 ERR("Health error occurred in %s", __func__);
793 }
794 health_exit(&health_thread_kernel);
795 DBG("Kernel thread dying");
796 return NULL;
797 }
798
799 /*
800 * Signal pthread condition of the consumer data that the thread.
801 */
802 static void signal_consumer_condition(struct consumer_data *data, int state)
803 {
804 pthread_mutex_lock(&data->cond_mutex);
805
806 /*
807 * The state is set before signaling. It can be any value, it's the waiter
808 * job to correctly interpret this condition variable associated to the
809 * consumer pthread_cond.
810 *
811 * A value of 0 means that the corresponding thread of the consumer data
812 * was not started. 1 indicates that the thread has started and is ready
813 * for action. A negative value means that there was an error during the
814 * thread bootstrap.
815 */
816 data->consumer_thread_is_ready = state;
817 (void) pthread_cond_signal(&data->cond);
818
819 pthread_mutex_unlock(&data->cond_mutex);
820 }
821
822 /*
823 * This thread manage the consumer error sent back to the session daemon.
824 */
825 static void *thread_manage_consumer(void *data)
826 {
827 int sock = -1, i, ret, pollfd, err = -1;
828 uint32_t revents, nb_fd;
829 enum lttcomm_return_code code;
830 struct lttng_poll_event events;
831 struct consumer_data *consumer_data = data;
832
833 DBG("[thread] Manage consumer started");
834
835 health_code_update(&consumer_data->health);
836
837 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
838 if (ret < 0) {
839 goto error_listen;
840 }
841
842 /*
843 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
844 * Nothing more will be added to this poll set.
845 */
846 ret = create_thread_poll_set(&events, 2);
847 if (ret < 0) {
848 goto error_poll;
849 }
850
851 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
852 if (ret < 0) {
853 goto error;
854 }
855
856 nb_fd = LTTNG_POLL_GETNB(&events);
857
858 health_code_update(&consumer_data->health);
859
860 /* Inifinite blocking call, waiting for transmission */
861 restart:
862 health_poll_update(&consumer_data->health);
863 ret = lttng_poll_wait(&events, -1);
864 health_poll_update(&consumer_data->health);
865 if (ret < 0) {
866 /*
867 * Restart interrupted system call.
868 */
869 if (errno == EINTR) {
870 goto restart;
871 }
872 goto error;
873 }
874
875 for (i = 0; i < nb_fd; i++) {
876 /* Fetch once the poll data */
877 revents = LTTNG_POLL_GETEV(&events, i);
878 pollfd = LTTNG_POLL_GETFD(&events, i);
879
880 health_code_update(&consumer_data->health);
881
882 /* Thread quit pipe has been closed. Killing thread. */
883 ret = check_thread_quit_pipe(pollfd, revents);
884 if (ret) {
885 err = 0;
886 goto exit;
887 }
888
889 /* Event on the registration socket */
890 if (pollfd == consumer_data->err_sock) {
891 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
892 ERR("consumer err socket poll error");
893 goto error;
894 }
895 }
896 }
897
898 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
899 if (sock < 0) {
900 goto error;
901 }
902
903 health_code_update(&consumer_data->health);
904
905 DBG2("Receiving code from consumer err_sock");
906
907 /* Getting status code from kconsumerd */
908 ret = lttcomm_recv_unix_sock(sock, &code,
909 sizeof(enum lttcomm_return_code));
910 if (ret <= 0) {
911 goto error;
912 }
913
914 health_code_update(&consumer_data->health);
915
916 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
917 consumer_data->cmd_sock =
918 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
919 if (consumer_data->cmd_sock < 0) {
920 /* On error, signal condition and quit. */
921 signal_consumer_condition(consumer_data, -1);
922 PERROR("consumer connect");
923 goto error;
924 }
925 signal_consumer_condition(consumer_data, 1);
926 DBG("Consumer command socket ready");
927 } else {
928 ERR("consumer error when waiting for SOCK_READY : %s",
929 lttcomm_get_readable_code(-code));
930 goto error;
931 }
932
933 /* Remove the kconsumerd error sock since we've established a connexion */
934 ret = lttng_poll_del(&events, consumer_data->err_sock);
935 if (ret < 0) {
936 goto error;
937 }
938
939 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
940 if (ret < 0) {
941 goto error;
942 }
943
944 health_code_update(&consumer_data->health);
945
946 /* Update number of fd */
947 nb_fd = LTTNG_POLL_GETNB(&events);
948
949 /* Inifinite blocking call, waiting for transmission */
950 restart_poll:
951 health_poll_update(&consumer_data->health);
952 ret = lttng_poll_wait(&events, -1);
953 health_poll_update(&consumer_data->health);
954 if (ret < 0) {
955 /*
956 * Restart interrupted system call.
957 */
958 if (errno == EINTR) {
959 goto restart_poll;
960 }
961 goto error;
962 }
963
964 for (i = 0; i < nb_fd; i++) {
965 /* Fetch once the poll data */
966 revents = LTTNG_POLL_GETEV(&events, i);
967 pollfd = LTTNG_POLL_GETFD(&events, i);
968
969 health_code_update(&consumer_data->health);
970
971 /* Thread quit pipe has been closed. Killing thread. */
972 ret = check_thread_quit_pipe(pollfd, revents);
973 if (ret) {
974 err = 0;
975 goto exit;
976 }
977
978 /* Event on the kconsumerd socket */
979 if (pollfd == sock) {
980 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
981 ERR("consumer err socket second poll error");
982 goto error;
983 }
984 }
985 }
986
987 health_code_update(&consumer_data->health);
988
989 /* Wait for any kconsumerd error */
990 ret = lttcomm_recv_unix_sock(sock, &code,
991 sizeof(enum lttcomm_return_code));
992 if (ret <= 0) {
993 ERR("consumer closed the command socket");
994 goto error;
995 }
996
997 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
998
999 exit:
1000 error:
1001 /* Immediately set the consumerd state to stopped */
1002 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1003 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1004 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1005 consumer_data->type == LTTNG_CONSUMER32_UST) {
1006 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1007 } else {
1008 /* Code flow error... */
1009 assert(0);
1010 }
1011
1012 if (consumer_data->err_sock >= 0) {
1013 ret = close(consumer_data->err_sock);
1014 if (ret) {
1015 PERROR("close");
1016 }
1017 }
1018 if (consumer_data->cmd_sock >= 0) {
1019 ret = close(consumer_data->cmd_sock);
1020 if (ret) {
1021 PERROR("close");
1022 }
1023 }
1024 if (sock >= 0) {
1025 ret = close(sock);
1026 if (ret) {
1027 PERROR("close");
1028 }
1029 }
1030
1031 unlink(consumer_data->err_unix_sock_path);
1032 unlink(consumer_data->cmd_unix_sock_path);
1033 consumer_data->pid = 0;
1034
1035 lttng_poll_clean(&events);
1036 error_poll:
1037 error_listen:
1038 if (err) {
1039 health_error(&consumer_data->health);
1040 ERR("Health error occurred in %s", __func__);
1041 }
1042 health_exit(&consumer_data->health);
1043 DBG("consumer thread cleanup completed");
1044
1045 return NULL;
1046 }
1047
1048 /*
1049 * This thread manage application communication.
1050 */
1051 static void *thread_manage_apps(void *data)
1052 {
1053 int i, ret, pollfd, err = -1;
1054 uint32_t revents, nb_fd;
1055 struct ust_command ust_cmd;
1056 struct lttng_poll_event events;
1057
1058 DBG("[thread] Manage application started");
1059
1060 rcu_register_thread();
1061 rcu_thread_online();
1062
1063 health_code_update(&health_thread_app_manage);
1064
1065 ret = create_thread_poll_set(&events, 2);
1066 if (ret < 0) {
1067 goto error_poll_create;
1068 }
1069
1070 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1071 if (ret < 0) {
1072 goto error;
1073 }
1074
1075 health_code_update(&health_thread_app_manage);
1076
1077 while (1) {
1078 /* Zeroed the events structure */
1079 lttng_poll_reset(&events);
1080
1081 nb_fd = LTTNG_POLL_GETNB(&events);
1082
1083 DBG("Apps thread polling on %d fds", nb_fd);
1084
1085 /* Inifinite blocking call, waiting for transmission */
1086 restart:
1087 health_poll_update(&health_thread_app_manage);
1088 ret = lttng_poll_wait(&events, -1);
1089 health_poll_update(&health_thread_app_manage);
1090 if (ret < 0) {
1091 /*
1092 * Restart interrupted system call.
1093 */
1094 if (errno == EINTR) {
1095 goto restart;
1096 }
1097 goto error;
1098 }
1099
1100 for (i = 0; i < nb_fd; i++) {
1101 /* Fetch once the poll data */
1102 revents = LTTNG_POLL_GETEV(&events, i);
1103 pollfd = LTTNG_POLL_GETFD(&events, i);
1104
1105 health_code_update(&health_thread_app_manage);
1106
1107 /* Thread quit pipe has been closed. Killing thread. */
1108 ret = check_thread_quit_pipe(pollfd, revents);
1109 if (ret) {
1110 err = 0;
1111 goto exit;
1112 }
1113
1114 /* Inspect the apps cmd pipe */
1115 if (pollfd == apps_cmd_pipe[0]) {
1116 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1117 ERR("Apps command pipe error");
1118 goto error;
1119 } else if (revents & LPOLLIN) {
1120 /* Empty pipe */
1121 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1122 if (ret < 0 || ret < sizeof(ust_cmd)) {
1123 PERROR("read apps cmd pipe");
1124 goto error;
1125 }
1126
1127 health_code_update(&health_thread_app_manage);
1128
1129 /* Register applicaton to the session daemon */
1130 ret = ust_app_register(&ust_cmd.reg_msg,
1131 ust_cmd.sock);
1132 if (ret == -ENOMEM) {
1133 goto error;
1134 } else if (ret < 0) {
1135 break;
1136 }
1137
1138 health_code_update(&health_thread_app_manage);
1139
1140 /*
1141 * Validate UST version compatibility.
1142 */
1143 ret = ust_app_validate_version(ust_cmd.sock);
1144 if (ret >= 0) {
1145 /*
1146 * Add channel(s) and event(s) to newly registered apps
1147 * from lttng global UST domain.
1148 */
1149 update_ust_app(ust_cmd.sock);
1150 }
1151
1152 health_code_update(&health_thread_app_manage);
1153
1154 ret = ust_app_register_done(ust_cmd.sock);
1155 if (ret < 0) {
1156 /*
1157 * If the registration is not possible, we simply
1158 * unregister the apps and continue
1159 */
1160 ust_app_unregister(ust_cmd.sock);
1161 } else {
1162 /*
1163 * We just need here to monitor the close of the UST
1164 * socket and poll set monitor those by default.
1165 * Listen on POLLIN (even if we never expect any
1166 * data) to ensure that hangup wakes us.
1167 */
1168 ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN);
1169 if (ret < 0) {
1170 goto error;
1171 }
1172
1173 DBG("Apps with sock %d added to poll set",
1174 ust_cmd.sock);
1175 }
1176
1177 health_code_update(&health_thread_app_manage);
1178
1179 break;
1180 }
1181 } else {
1182 /*
1183 * At this point, we know that a registered application made
1184 * the event at poll_wait.
1185 */
1186 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1187 /* Removing from the poll set */
1188 ret = lttng_poll_del(&events, pollfd);
1189 if (ret < 0) {
1190 goto error;
1191 }
1192
1193 /* Socket closed on remote end. */
1194 ust_app_unregister(pollfd);
1195 break;
1196 }
1197 }
1198
1199 health_code_update(&health_thread_app_manage);
1200 }
1201 }
1202
1203 exit:
1204 error:
1205 lttng_poll_clean(&events);
1206 error_poll_create:
1207 if (err) {
1208 health_error(&health_thread_app_manage);
1209 ERR("Health error occurred in %s", __func__);
1210 }
1211 health_exit(&health_thread_app_manage);
1212 DBG("Application communication apps thread cleanup complete");
1213 rcu_thread_offline();
1214 rcu_unregister_thread();
1215 return NULL;
1216 }
1217
1218 /*
1219 * Dispatch request from the registration threads to the application
1220 * communication thread.
1221 */
1222 static void *thread_dispatch_ust_registration(void *data)
1223 {
1224 int ret;
1225 struct cds_wfq_node *node;
1226 struct ust_command *ust_cmd = NULL;
1227
1228 DBG("[thread] Dispatch UST command started");
1229
1230 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1231 /* Atomically prepare the queue futex */
1232 futex_nto1_prepare(&ust_cmd_queue.futex);
1233
1234 do {
1235 /* Dequeue command for registration */
1236 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1237 if (node == NULL) {
1238 DBG("Woken up but nothing in the UST command queue");
1239 /* Continue thread execution */
1240 break;
1241 }
1242
1243 ust_cmd = caa_container_of(node, struct ust_command, node);
1244
1245 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1246 " gid:%d sock:%d name:%s (version %d.%d)",
1247 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1248 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1249 ust_cmd->sock, ust_cmd->reg_msg.name,
1250 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1251 /*
1252 * Inform apps thread of the new application registration. This
1253 * call is blocking so we can be assured that the data will be read
1254 * at some point in time or wait to the end of the world :)
1255 */
1256 ret = write(apps_cmd_pipe[1], ust_cmd,
1257 sizeof(struct ust_command));
1258 if (ret < 0) {
1259 PERROR("write apps cmd pipe");
1260 if (errno == EBADF) {
1261 /*
1262 * We can't inform the application thread to process
1263 * registration. We will exit or else application
1264 * registration will not occur and tracing will never
1265 * start.
1266 */
1267 goto error;
1268 }
1269 }
1270 free(ust_cmd);
1271 } while (node != NULL);
1272
1273 /* Futex wait on queue. Blocking call on futex() */
1274 futex_nto1_wait(&ust_cmd_queue.futex);
1275 }
1276
1277 error:
1278 DBG("Dispatch thread dying");
1279 return NULL;
1280 }
1281
1282 /*
1283 * This thread manage application registration.
1284 */
1285 static void *thread_registration_apps(void *data)
1286 {
1287 int sock = -1, i, ret, pollfd, err = -1;
1288 uint32_t revents, nb_fd;
1289 struct lttng_poll_event events;
1290 /*
1291 * Get allocated in this thread, enqueued to a global queue, dequeued and
1292 * freed in the manage apps thread.
1293 */
1294 struct ust_command *ust_cmd = NULL;
1295
1296 DBG("[thread] Manage application registration started");
1297
1298 ret = lttcomm_listen_unix_sock(apps_sock);
1299 if (ret < 0) {
1300 goto error_listen;
1301 }
1302
1303 /*
1304 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1305 * more will be added to this poll set.
1306 */
1307 ret = create_thread_poll_set(&events, 2);
1308 if (ret < 0) {
1309 goto error_create_poll;
1310 }
1311
1312 /* Add the application registration socket */
1313 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1314 if (ret < 0) {
1315 goto error_poll_add;
1316 }
1317
1318 /* Notify all applications to register */
1319 ret = notify_ust_apps(1);
1320 if (ret < 0) {
1321 ERR("Failed to notify applications or create the wait shared memory.\n"
1322 "Execution continues but there might be problem for already\n"
1323 "running applications that wishes to register.");
1324 }
1325
1326 while (1) {
1327 DBG("Accepting application registration");
1328
1329 nb_fd = LTTNG_POLL_GETNB(&events);
1330
1331 /* Inifinite blocking call, waiting for transmission */
1332 restart:
1333 health_poll_update(&health_thread_app_reg);
1334 ret = lttng_poll_wait(&events, -1);
1335 health_poll_update(&health_thread_app_reg);
1336 if (ret < 0) {
1337 /*
1338 * Restart interrupted system call.
1339 */
1340 if (errno == EINTR) {
1341 goto restart;
1342 }
1343 goto error;
1344 }
1345
1346 for (i = 0; i < nb_fd; i++) {
1347 health_code_update(&health_thread_app_reg);
1348
1349 /* Fetch once the poll data */
1350 revents = LTTNG_POLL_GETEV(&events, i);
1351 pollfd = LTTNG_POLL_GETFD(&events, i);
1352
1353 /* Thread quit pipe has been closed. Killing thread. */
1354 ret = check_thread_quit_pipe(pollfd, revents);
1355 if (ret) {
1356 err = 0;
1357 goto exit;
1358 }
1359
1360 /* Event on the registration socket */
1361 if (pollfd == apps_sock) {
1362 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1363 ERR("Register apps socket poll error");
1364 goto error;
1365 } else if (revents & LPOLLIN) {
1366 sock = lttcomm_accept_unix_sock(apps_sock);
1367 if (sock < 0) {
1368 goto error;
1369 }
1370
1371 /* Create UST registration command for enqueuing */
1372 ust_cmd = zmalloc(sizeof(struct ust_command));
1373 if (ust_cmd == NULL) {
1374 PERROR("ust command zmalloc");
1375 goto error;
1376 }
1377
1378 /*
1379 * Using message-based transmissions to ensure we don't
1380 * have to deal with partially received messages.
1381 */
1382 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1383 if (ret < 0) {
1384 ERR("Exhausted file descriptors allowed for applications.");
1385 free(ust_cmd);
1386 ret = close(sock);
1387 if (ret) {
1388 PERROR("close");
1389 }
1390 sock = -1;
1391 continue;
1392 }
1393 health_code_update(&health_thread_app_reg);
1394 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1395 sizeof(struct ust_register_msg));
1396 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1397 if (ret < 0) {
1398 PERROR("lttcomm_recv_unix_sock register apps");
1399 } else {
1400 ERR("Wrong size received on apps register");
1401 }
1402 free(ust_cmd);
1403 ret = close(sock);
1404 if (ret) {
1405 PERROR("close");
1406 }
1407 lttng_fd_put(LTTNG_FD_APPS, 1);
1408 sock = -1;
1409 continue;
1410 }
1411 health_code_update(&health_thread_app_reg);
1412
1413 ust_cmd->sock = sock;
1414 sock = -1;
1415
1416 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1417 " gid:%d sock:%d name:%s (version %d.%d)",
1418 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1419 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1420 ust_cmd->sock, ust_cmd->reg_msg.name,
1421 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1422
1423 /*
1424 * Lock free enqueue the registration request. The red pill
1425 * has been taken! This apps will be part of the *system*.
1426 */
1427 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1428
1429 /*
1430 * Wake the registration queue futex. Implicit memory
1431 * barrier with the exchange in cds_wfq_enqueue.
1432 */
1433 futex_nto1_wake(&ust_cmd_queue.futex);
1434 }
1435 }
1436 }
1437 }
1438
1439 exit:
1440 error:
1441 if (err) {
1442 health_error(&health_thread_app_reg);
1443 ERR("Health error occurred in %s", __func__);
1444 }
1445 health_exit(&health_thread_app_reg);
1446
1447 /* Notify that the registration thread is gone */
1448 notify_ust_apps(0);
1449
1450 if (apps_sock >= 0) {
1451 ret = close(apps_sock);
1452 if (ret) {
1453 PERROR("close");
1454 }
1455 }
1456 if (sock >= 0) {
1457 ret = close(sock);
1458 if (ret) {
1459 PERROR("close");
1460 }
1461 lttng_fd_put(LTTNG_FD_APPS, 1);
1462 }
1463 unlink(apps_unix_sock_path);
1464
1465 error_poll_add:
1466 lttng_poll_clean(&events);
1467 error_listen:
1468 error_create_poll:
1469 DBG("UST Registration thread cleanup complete");
1470
1471 return NULL;
1472 }
1473
1474 /*
1475 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1476 * exec or it will fails.
1477 */
1478 static int spawn_consumer_thread(struct consumer_data *consumer_data)
1479 {
1480 int ret, clock_ret;
1481 struct timespec timeout;
1482
1483 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1484 consumer_data->consumer_thread_is_ready = 0;
1485
1486 /* Setup pthread condition */
1487 ret = pthread_condattr_init(&consumer_data->condattr);
1488 if (ret != 0) {
1489 errno = ret;
1490 PERROR("pthread_condattr_init consumer data");
1491 goto error;
1492 }
1493
1494 /*
1495 * Set the monotonic clock in order to make sure we DO NOT jump in time
1496 * between the clock_gettime() call and the timedwait call. See bug #324
1497 * for a more details and how we noticed it.
1498 */
1499 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
1500 if (ret != 0) {
1501 errno = ret;
1502 PERROR("pthread_condattr_setclock consumer data");
1503 goto error;
1504 }
1505
1506 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
1507 if (ret != 0) {
1508 errno = ret;
1509 PERROR("pthread_cond_init consumer data");
1510 goto error;
1511 }
1512
1513 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
1514 consumer_data);
1515 if (ret != 0) {
1516 PERROR("pthread_create consumer");
1517 ret = -1;
1518 goto error;
1519 }
1520
1521 /* We are about to wait on a pthread condition */
1522 pthread_mutex_lock(&consumer_data->cond_mutex);
1523
1524 /* Get time for sem_timedwait absolute timeout */
1525 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1526 /*
1527 * Set the timeout for the condition timed wait even if the clock gettime
1528 * call fails since we might loop on that call and we want to avoid to
1529 * increment the timeout too many times.
1530 */
1531 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
1532
1533 /*
1534 * The following loop COULD be skipped in some conditions so this is why we
1535 * set ret to 0 in order to make sure at least one round of the loop is
1536 * done.
1537 */
1538 ret = 0;
1539
1540 /*
1541 * Loop until the condition is reached or when a timeout is reached. Note
1542 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1543 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1544 * possible. This loop does not take any chances and works with both of
1545 * them.
1546 */
1547 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
1548 if (clock_ret < 0) {
1549 PERROR("clock_gettime spawn consumer");
1550 /* Infinite wait for the consumerd thread to be ready */
1551 ret = pthread_cond_wait(&consumer_data->cond,
1552 &consumer_data->cond_mutex);
1553 } else {
1554 ret = pthread_cond_timedwait(&consumer_data->cond,
1555 &consumer_data->cond_mutex, &timeout);
1556 }
1557 }
1558
1559 /* Release the pthread condition */
1560 pthread_mutex_unlock(&consumer_data->cond_mutex);
1561
1562 if (ret != 0) {
1563 errno = ret;
1564 if (ret == ETIMEDOUT) {
1565 /*
1566 * Call has timed out so we kill the kconsumerd_thread and return
1567 * an error.
1568 */
1569 ERR("Condition timed out. The consumer thread was never ready."
1570 " Killing it");
1571 ret = pthread_cancel(consumer_data->thread);
1572 if (ret < 0) {
1573 PERROR("pthread_cancel consumer thread");
1574 }
1575 } else {
1576 PERROR("pthread_cond_wait failed consumer thread");
1577 }
1578 goto error;
1579 }
1580
1581 pthread_mutex_lock(&consumer_data->pid_mutex);
1582 if (consumer_data->pid == 0) {
1583 ERR("Consumerd did not start");
1584 pthread_mutex_unlock(&consumer_data->pid_mutex);
1585 goto error;
1586 }
1587 pthread_mutex_unlock(&consumer_data->pid_mutex);
1588
1589 return 0;
1590
1591 error:
1592 return ret;
1593 }
1594
1595 /*
1596 * Join consumer thread
1597 */
1598 static int join_consumer_thread(struct consumer_data *consumer_data)
1599 {
1600 void *status;
1601 int ret;
1602
1603 /* Consumer pid must be a real one. */
1604 if (consumer_data->pid > 0) {
1605 ret = kill(consumer_data->pid, SIGTERM);
1606 if (ret) {
1607 ERR("Error killing consumer daemon");
1608 return ret;
1609 }
1610 return pthread_join(consumer_data->thread, &status);
1611 } else {
1612 return 0;
1613 }
1614 }
1615
1616 /*
1617 * Fork and exec a consumer daemon (consumerd).
1618 *
1619 * Return pid if successful else -1.
1620 */
1621 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
1622 {
1623 int ret;
1624 pid_t pid;
1625 const char *consumer_to_use;
1626 const char *verbosity;
1627 struct stat st;
1628
1629 DBG("Spawning consumerd");
1630
1631 pid = fork();
1632 if (pid == 0) {
1633 /*
1634 * Exec consumerd.
1635 */
1636 if (opt_verbose_consumer) {
1637 verbosity = "--verbose";
1638 } else {
1639 verbosity = "--quiet";
1640 }
1641 switch (consumer_data->type) {
1642 case LTTNG_CONSUMER_KERNEL:
1643 /*
1644 * Find out which consumerd to execute. We will first try the
1645 * 64-bit path, then the sessiond's installation directory, and
1646 * fallback on the 32-bit one,
1647 */
1648 DBG3("Looking for a kernel consumer at these locations:");
1649 DBG3(" 1) %s", consumerd64_bin);
1650 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1651 DBG3(" 3) %s", consumerd32_bin);
1652 if (stat(consumerd64_bin, &st) == 0) {
1653 DBG3("Found location #1");
1654 consumer_to_use = consumerd64_bin;
1655 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
1656 DBG3("Found location #2");
1657 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
1658 } else if (stat(consumerd32_bin, &st) == 0) {
1659 DBG3("Found location #3");
1660 consumer_to_use = consumerd32_bin;
1661 } else {
1662 DBG("Could not find any valid consumerd executable");
1663 break;
1664 }
1665 DBG("Using kernel consumer at: %s", consumer_to_use);
1666 execl(consumer_to_use,
1667 "lttng-consumerd", verbosity, "-k",
1668 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1669 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1670 NULL);
1671 break;
1672 case LTTNG_CONSUMER64_UST:
1673 {
1674 char *tmpnew = NULL;
1675
1676 if (consumerd64_libdir[0] != '\0') {
1677 char *tmp;
1678 size_t tmplen;
1679
1680 tmp = getenv("LD_LIBRARY_PATH");
1681 if (!tmp) {
1682 tmp = "";
1683 }
1684 tmplen = strlen("LD_LIBRARY_PATH=")
1685 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1686 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1687 if (!tmpnew) {
1688 ret = -ENOMEM;
1689 goto error;
1690 }
1691 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1692 strcat(tmpnew, consumerd64_libdir);
1693 if (tmp[0] != '\0') {
1694 strcat(tmpnew, ":");
1695 strcat(tmpnew, tmp);
1696 }
1697 ret = putenv(tmpnew);
1698 if (ret) {
1699 ret = -errno;
1700 goto error;
1701 }
1702 }
1703 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
1704 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
1705 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1706 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1707 NULL);
1708 if (consumerd64_libdir[0] != '\0') {
1709 free(tmpnew);
1710 }
1711 if (ret) {
1712 goto error;
1713 }
1714 break;
1715 }
1716 case LTTNG_CONSUMER32_UST:
1717 {
1718 char *tmpnew = NULL;
1719
1720 if (consumerd32_libdir[0] != '\0') {
1721 char *tmp;
1722 size_t tmplen;
1723
1724 tmp = getenv("LD_LIBRARY_PATH");
1725 if (!tmp) {
1726 tmp = "";
1727 }
1728 tmplen = strlen("LD_LIBRARY_PATH=")
1729 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1730 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1731 if (!tmpnew) {
1732 ret = -ENOMEM;
1733 goto error;
1734 }
1735 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1736 strcat(tmpnew, consumerd32_libdir);
1737 if (tmp[0] != '\0') {
1738 strcat(tmpnew, ":");
1739 strcat(tmpnew, tmp);
1740 }
1741 ret = putenv(tmpnew);
1742 if (ret) {
1743 ret = -errno;
1744 goto error;
1745 }
1746 }
1747 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
1748 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
1749 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1750 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1751 NULL);
1752 if (consumerd32_libdir[0] != '\0') {
1753 free(tmpnew);
1754 }
1755 if (ret) {
1756 goto error;
1757 }
1758 break;
1759 }
1760 default:
1761 PERROR("unknown consumer type");
1762 exit(EXIT_FAILURE);
1763 }
1764 if (errno != 0) {
1765 PERROR("kernel start consumer exec");
1766 }
1767 exit(EXIT_FAILURE);
1768 } else if (pid > 0) {
1769 ret = pid;
1770 } else {
1771 PERROR("start consumer fork");
1772 ret = -errno;
1773 }
1774 error:
1775 return ret;
1776 }
1777
1778 /*
1779 * Spawn the consumerd daemon and session daemon thread.
1780 */
1781 static int start_consumerd(struct consumer_data *consumer_data)
1782 {
1783 int ret;
1784
1785 pthread_mutex_lock(&consumer_data->pid_mutex);
1786 if (consumer_data->pid != 0) {
1787 pthread_mutex_unlock(&consumer_data->pid_mutex);
1788 goto end;
1789 }
1790
1791 ret = spawn_consumerd(consumer_data);
1792 if (ret < 0) {
1793 ERR("Spawning consumerd failed");
1794 pthread_mutex_unlock(&consumer_data->pid_mutex);
1795 goto error;
1796 }
1797
1798 /* Setting up the consumer_data pid */
1799 consumer_data->pid = ret;
1800 DBG2("Consumer pid %d", consumer_data->pid);
1801 pthread_mutex_unlock(&consumer_data->pid_mutex);
1802
1803 DBG2("Spawning consumer control thread");
1804 ret = spawn_consumer_thread(consumer_data);
1805 if (ret < 0) {
1806 ERR("Fatal error spawning consumer control thread");
1807 goto error;
1808 }
1809
1810 end:
1811 return 0;
1812
1813 error:
1814 return ret;
1815 }
1816
1817 /*
1818 * Compute health status of each consumer. If one of them is zero (bad
1819 * state), we return 0.
1820 */
1821 static int check_consumer_health(void)
1822 {
1823 int ret;
1824
1825 ret = health_check_state(&kconsumer_data.health) &&
1826 health_check_state(&ustconsumer32_data.health) &&
1827 health_check_state(&ustconsumer64_data.health);
1828
1829 DBG3("Health consumer check %d", ret);
1830
1831 return ret;
1832 }
1833
1834 /*
1835 * Setup necessary data for kernel tracer action.
1836 */
1837 static int init_kernel_tracer(void)
1838 {
1839 int ret;
1840
1841 /* Modprobe lttng kernel modules */
1842 ret = modprobe_lttng_control();
1843 if (ret < 0) {
1844 goto error;
1845 }
1846
1847 /* Open debugfs lttng */
1848 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1849 if (kernel_tracer_fd < 0) {
1850 DBG("Failed to open %s", module_proc_lttng);
1851 ret = -1;
1852 goto error_open;
1853 }
1854
1855 /* Validate kernel version */
1856 ret = kernel_validate_version(kernel_tracer_fd);
1857 if (ret < 0) {
1858 goto error_version;
1859 }
1860
1861 ret = modprobe_lttng_data();
1862 if (ret < 0) {
1863 goto error_modules;
1864 }
1865
1866 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1867 return 0;
1868
1869 error_version:
1870 modprobe_remove_lttng_control();
1871 ret = close(kernel_tracer_fd);
1872 if (ret) {
1873 PERROR("close");
1874 }
1875 kernel_tracer_fd = -1;
1876 return LTTNG_ERR_KERN_VERSION;
1877
1878 error_modules:
1879 ret = close(kernel_tracer_fd);
1880 if (ret) {
1881 PERROR("close");
1882 }
1883
1884 error_open:
1885 modprobe_remove_lttng_control();
1886
1887 error:
1888 WARN("No kernel tracer available");
1889 kernel_tracer_fd = -1;
1890 if (!is_root) {
1891 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1892 } else {
1893 return LTTNG_ERR_KERN_NA;
1894 }
1895 }
1896
1897
1898 /*
1899 * Copy consumer output from the tracing session to the domain session. The
1900 * function also applies the right modification on a per domain basis for the
1901 * trace files destination directory.
1902 */
1903 static int copy_session_consumer(int domain, struct ltt_session *session)
1904 {
1905 int ret;
1906 const char *dir_name;
1907 struct consumer_output *consumer;
1908
1909 assert(session);
1910 assert(session->consumer);
1911
1912 switch (domain) {
1913 case LTTNG_DOMAIN_KERNEL:
1914 DBG3("Copying tracing session consumer output in kernel session");
1915 session->kernel_session->consumer =
1916 consumer_copy_output(session->consumer);
1917 /* Ease our life a bit for the next part */
1918 consumer = session->kernel_session->consumer;
1919 dir_name = DEFAULT_KERNEL_TRACE_DIR;
1920 break;
1921 case LTTNG_DOMAIN_UST:
1922 DBG3("Copying tracing session consumer output in UST session");
1923 session->ust_session->consumer =
1924 consumer_copy_output(session->consumer);
1925 /* Ease our life a bit for the next part */
1926 consumer = session->ust_session->consumer;
1927 dir_name = DEFAULT_UST_TRACE_DIR;
1928 break;
1929 default:
1930 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1931 goto error;
1932 }
1933
1934 /* Append correct directory to subdir */
1935 strncat(consumer->subdir, dir_name,
1936 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
1937 DBG3("Copy session consumer subdir %s", consumer->subdir);
1938
1939 ret = LTTNG_OK;
1940
1941 error:
1942 return ret;
1943 }
1944
1945 /*
1946 * Create an UST session and add it to the session ust list.
1947 */
1948 static int create_ust_session(struct ltt_session *session,
1949 struct lttng_domain *domain)
1950 {
1951 int ret;
1952 struct ltt_ust_session *lus = NULL;
1953
1954 assert(session);
1955 assert(domain);
1956 assert(session->consumer);
1957
1958 switch (domain->type) {
1959 case LTTNG_DOMAIN_UST:
1960 break;
1961 default:
1962 ERR("Unknown UST domain on create session %d", domain->type);
1963 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1964 goto error;
1965 }
1966
1967 DBG("Creating UST session");
1968
1969 lus = trace_ust_create_session(session->path, session->id, domain);
1970 if (lus == NULL) {
1971 ret = LTTNG_ERR_UST_SESS_FAIL;
1972 goto error;
1973 }
1974
1975 lus->uid = session->uid;
1976 lus->gid = session->gid;
1977 session->ust_session = lus;
1978
1979 /* Copy session output to the newly created UST session */
1980 ret = copy_session_consumer(domain->type, session);
1981 if (ret != LTTNG_OK) {
1982 goto error;
1983 }
1984
1985 return LTTNG_OK;
1986
1987 error:
1988 free(lus);
1989 session->ust_session = NULL;
1990 return ret;
1991 }
1992
1993 /*
1994 * Create a kernel tracer session then create the default channel.
1995 */
1996 static int create_kernel_session(struct ltt_session *session)
1997 {
1998 int ret;
1999
2000 DBG("Creating kernel session");
2001
2002 ret = kernel_create_session(session, kernel_tracer_fd);
2003 if (ret < 0) {
2004 ret = LTTNG_ERR_KERN_SESS_FAIL;
2005 goto error;
2006 }
2007
2008 /* Code flow safety */
2009 assert(session->kernel_session);
2010
2011 /* Copy session output to the newly created Kernel session */
2012 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2013 if (ret != LTTNG_OK) {
2014 goto error;
2015 }
2016
2017 /* Create directory(ies) on local filesystem. */
2018 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2019 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2020 ret = run_as_mkdir_recursive(
2021 session->kernel_session->consumer->dst.trace_path,
2022 S_IRWXU | S_IRWXG, session->uid, session->gid);
2023 if (ret < 0) {
2024 if (ret != -EEXIST) {
2025 ERR("Trace directory creation error");
2026 goto error;
2027 }
2028 }
2029 }
2030
2031 session->kernel_session->uid = session->uid;
2032 session->kernel_session->gid = session->gid;
2033
2034 return LTTNG_OK;
2035
2036 error:
2037 trace_kernel_destroy_session(session->kernel_session);
2038 session->kernel_session = NULL;
2039 return ret;
2040 }
2041
2042 /*
2043 * Count number of session permitted by uid/gid.
2044 */
2045 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2046 {
2047 unsigned int i = 0;
2048 struct ltt_session *session;
2049
2050 DBG("Counting number of available session for UID %d GID %d",
2051 uid, gid);
2052 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2053 /*
2054 * Only list the sessions the user can control.
2055 */
2056 if (!session_access_ok(session, uid, gid)) {
2057 continue;
2058 }
2059 i++;
2060 }
2061 return i;
2062 }
2063
2064 /*
2065 * Process the command requested by the lttng client within the command
2066 * context structure. This function make sure that the return structure (llm)
2067 * is set and ready for transmission before returning.
2068 *
2069 * Return any error encountered or 0 for success.
2070 *
2071 * "sock" is only used for special-case var. len data.
2072 */
2073 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2074 int *sock_error)
2075 {
2076 int ret = LTTNG_OK;
2077 int need_tracing_session = 1;
2078 int need_domain;
2079
2080 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2081
2082 *sock_error = 0;
2083
2084 switch (cmd_ctx->lsm->cmd_type) {
2085 case LTTNG_CREATE_SESSION:
2086 case LTTNG_DESTROY_SESSION:
2087 case LTTNG_LIST_SESSIONS:
2088 case LTTNG_LIST_DOMAINS:
2089 case LTTNG_START_TRACE:
2090 case LTTNG_STOP_TRACE:
2091 need_domain = 0;
2092 break;
2093 default:
2094 need_domain = 1;
2095 }
2096
2097 if (opt_no_kernel && need_domain
2098 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2099 if (!is_root) {
2100 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2101 } else {
2102 ret = LTTNG_ERR_KERN_NA;
2103 }
2104 goto error;
2105 }
2106
2107 /* Deny register consumer if we already have a spawned consumer. */
2108 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2109 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2110 if (kconsumer_data.pid > 0) {
2111 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2112 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2113 goto error;
2114 }
2115 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2116 }
2117
2118 /*
2119 * Check for command that don't needs to allocate a returned payload. We do
2120 * this here so we don't have to make the call for no payload at each
2121 * command.
2122 */
2123 switch(cmd_ctx->lsm->cmd_type) {
2124 case LTTNG_LIST_SESSIONS:
2125 case LTTNG_LIST_TRACEPOINTS:
2126 case LTTNG_LIST_TRACEPOINT_FIELDS:
2127 case LTTNG_LIST_DOMAINS:
2128 case LTTNG_LIST_CHANNELS:
2129 case LTTNG_LIST_EVENTS:
2130 break;
2131 default:
2132 /* Setup lttng message with no payload */
2133 ret = setup_lttng_msg(cmd_ctx, 0);
2134 if (ret < 0) {
2135 /* This label does not try to unlock the session */
2136 goto init_setup_error;
2137 }
2138 }
2139
2140 /* Commands that DO NOT need a session. */
2141 switch (cmd_ctx->lsm->cmd_type) {
2142 case LTTNG_CREATE_SESSION:
2143 case LTTNG_CALIBRATE:
2144 case LTTNG_LIST_SESSIONS:
2145 case LTTNG_LIST_TRACEPOINTS:
2146 case LTTNG_LIST_TRACEPOINT_FIELDS:
2147 need_tracing_session = 0;
2148 break;
2149 default:
2150 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2151 /*
2152 * We keep the session list lock across _all_ commands
2153 * for now, because the per-session lock does not
2154 * handle teardown properly.
2155 */
2156 session_lock_list();
2157 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2158 if (cmd_ctx->session == NULL) {
2159 if (cmd_ctx->lsm->session.name != NULL) {
2160 ret = LTTNG_ERR_SESS_NOT_FOUND;
2161 } else {
2162 /* If no session name specified */
2163 ret = LTTNG_ERR_SELECT_SESS;
2164 }
2165 goto error;
2166 } else {
2167 /* Acquire lock for the session */
2168 session_lock(cmd_ctx->session);
2169 }
2170 break;
2171 }
2172
2173 if (!need_domain) {
2174 goto skip_domain;
2175 }
2176
2177 /*
2178 * Check domain type for specific "pre-action".
2179 */
2180 switch (cmd_ctx->lsm->domain.type) {
2181 case LTTNG_DOMAIN_KERNEL:
2182 if (!is_root) {
2183 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2184 goto error;
2185 }
2186
2187 /* Kernel tracer check */
2188 if (kernel_tracer_fd == -1) {
2189 /* Basically, load kernel tracer modules */
2190 ret = init_kernel_tracer();
2191 if (ret != 0) {
2192 goto error;
2193 }
2194 }
2195
2196 /* Consumer is in an ERROR state. Report back to client */
2197 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
2198 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2199 goto error;
2200 }
2201
2202 /* Need a session for kernel command */
2203 if (need_tracing_session) {
2204 if (cmd_ctx->session->kernel_session == NULL) {
2205 ret = create_kernel_session(cmd_ctx->session);
2206 if (ret < 0) {
2207 ret = LTTNG_ERR_KERN_SESS_FAIL;
2208 goto error;
2209 }
2210 }
2211
2212 /* Start the kernel consumer daemon */
2213 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2214 if (kconsumer_data.pid == 0 &&
2215 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2216 cmd_ctx->session->start_consumer) {
2217 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2218 ret = start_consumerd(&kconsumer_data);
2219 if (ret < 0) {
2220 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2221 goto error;
2222 }
2223 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
2224 } else {
2225 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2226 }
2227
2228 /*
2229 * The consumer was just spawned so we need to add the socket to
2230 * the consumer output of the session if exist.
2231 */
2232 ret = consumer_create_socket(&kconsumer_data,
2233 cmd_ctx->session->kernel_session->consumer);
2234 if (ret < 0) {
2235 goto error;
2236 }
2237 }
2238
2239 break;
2240 case LTTNG_DOMAIN_UST:
2241 {
2242 /* Consumer is in an ERROR state. Report back to client */
2243 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
2244 ret = LTTNG_ERR_NO_USTCONSUMERD;
2245 goto error;
2246 }
2247
2248 if (need_tracing_session) {
2249 /* Create UST session if none exist. */
2250 if (cmd_ctx->session->ust_session == NULL) {
2251 ret = create_ust_session(cmd_ctx->session,
2252 &cmd_ctx->lsm->domain);
2253 if (ret != LTTNG_OK) {
2254 goto error;
2255 }
2256 }
2257
2258 /* Start the UST consumer daemons */
2259 /* 64-bit */
2260 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
2261 if (consumerd64_bin[0] != '\0' &&
2262 ustconsumer64_data.pid == 0 &&
2263 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2264 cmd_ctx->session->start_consumer) {
2265 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2266 ret = start_consumerd(&ustconsumer64_data);
2267 if (ret < 0) {
2268 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
2269 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2270 goto error;
2271 }
2272
2273 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
2274 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2275 } else {
2276 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2277 }
2278
2279 /*
2280 * Setup socket for consumer 64 bit. No need for atomic access
2281 * since it was set above and can ONLY be set in this thread.
2282 */
2283 ret = consumer_create_socket(&ustconsumer64_data,
2284 cmd_ctx->session->ust_session->consumer);
2285 if (ret < 0) {
2286 goto error;
2287 }
2288
2289 /* 32-bit */
2290 if (consumerd32_bin[0] != '\0' &&
2291 ustconsumer32_data.pid == 0 &&
2292 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2293 cmd_ctx->session->start_consumer) {
2294 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2295 ret = start_consumerd(&ustconsumer32_data);
2296 if (ret < 0) {
2297 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
2298 uatomic_set(&ust_consumerd32_fd, -EINVAL);
2299 goto error;
2300 }
2301
2302 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
2303 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2304 } else {
2305 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2306 }
2307
2308 /*
2309 * Setup socket for consumer 64 bit. No need for atomic access
2310 * since it was set above and can ONLY be set in this thread.
2311 */
2312 ret = consumer_create_socket(&ustconsumer32_data,
2313 cmd_ctx->session->ust_session->consumer);
2314 if (ret < 0) {
2315 goto error;
2316 }
2317 }
2318 break;
2319 }
2320 default:
2321 break;
2322 }
2323 skip_domain:
2324
2325 /* Validate consumer daemon state when start/stop trace command */
2326 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2327 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2328 switch (cmd_ctx->lsm->domain.type) {
2329 case LTTNG_DOMAIN_UST:
2330 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
2331 ret = LTTNG_ERR_NO_USTCONSUMERD;
2332 goto error;
2333 }
2334 break;
2335 case LTTNG_DOMAIN_KERNEL:
2336 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
2337 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2338 goto error;
2339 }
2340 break;
2341 }
2342 }
2343
2344 /*
2345 * Check that the UID or GID match that of the tracing session.
2346 * The root user can interact with all sessions.
2347 */
2348 if (need_tracing_session) {
2349 if (!session_access_ok(cmd_ctx->session,
2350 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2351 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
2352 ret = LTTNG_ERR_EPERM;
2353 goto error;
2354 }
2355 }
2356
2357 /* Process by command type */
2358 switch (cmd_ctx->lsm->cmd_type) {
2359 case LTTNG_ADD_CONTEXT:
2360 {
2361 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2362 cmd_ctx->lsm->u.context.channel_name,
2363 cmd_ctx->lsm->u.context.event_name,
2364 &cmd_ctx->lsm->u.context.ctx);
2365 break;
2366 }
2367 case LTTNG_DISABLE_CHANNEL:
2368 {
2369 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2370 cmd_ctx->lsm->u.disable.channel_name);
2371 break;
2372 }
2373 case LTTNG_DISABLE_EVENT:
2374 {
2375 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2376 cmd_ctx->lsm->u.disable.channel_name,
2377 cmd_ctx->lsm->u.disable.name);
2378 break;
2379 }
2380 case LTTNG_DISABLE_ALL_EVENT:
2381 {
2382 DBG("Disabling all events");
2383
2384 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2385 cmd_ctx->lsm->u.disable.channel_name);
2386 break;
2387 }
2388 case LTTNG_DISABLE_CONSUMER:
2389 {
2390 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
2391 break;
2392 }
2393 case LTTNG_ENABLE_CHANNEL:
2394 {
2395 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2396 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
2397 break;
2398 }
2399 case LTTNG_ENABLE_CONSUMER:
2400 {
2401 /*
2402 * XXX: 0 means that this URI should be applied on the session. Should
2403 * be a DOMAIN enuam.
2404 */
2405 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
2406 if (ret != LTTNG_OK) {
2407 goto error;
2408 }
2409
2410 if (cmd_ctx->lsm->domain.type == 0) {
2411 /* Add the URI for the UST session if a consumer is present. */
2412 if (cmd_ctx->session->ust_session &&
2413 cmd_ctx->session->ust_session->consumer) {
2414 ret = cmd_enable_consumer(LTTNG_DOMAIN_UST, cmd_ctx->session);
2415 } else if (cmd_ctx->session->kernel_session &&
2416 cmd_ctx->session->kernel_session->consumer) {
2417 ret = cmd_enable_consumer(LTTNG_DOMAIN_KERNEL,
2418 cmd_ctx->session);
2419 }
2420 }
2421 break;
2422 }
2423 case LTTNG_ENABLE_EVENT:
2424 {
2425 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2426 cmd_ctx->lsm->u.enable.channel_name,
2427 &cmd_ctx->lsm->u.enable.event, kernel_poll_pipe[1]);
2428 break;
2429 }
2430 case LTTNG_ENABLE_ALL_EVENT:
2431 {
2432 DBG("Enabling all events");
2433
2434 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2435 cmd_ctx->lsm->u.enable.channel_name,
2436 cmd_ctx->lsm->u.enable.event.type, kernel_poll_pipe[1]);
2437 break;
2438 }
2439 case LTTNG_LIST_TRACEPOINTS:
2440 {
2441 struct lttng_event *events;
2442 ssize_t nb_events;
2443
2444 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
2445 if (nb_events < 0) {
2446 /* Return value is a negative lttng_error_code. */
2447 ret = -nb_events;
2448 goto error;
2449 }
2450
2451 /*
2452 * Setup lttng message with payload size set to the event list size in
2453 * bytes and then copy list into the llm payload.
2454 */
2455 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2456 if (ret < 0) {
2457 free(events);
2458 goto setup_error;
2459 }
2460
2461 /* Copy event list into message payload */
2462 memcpy(cmd_ctx->llm->payload, events,
2463 sizeof(struct lttng_event) * nb_events);
2464
2465 free(events);
2466
2467 ret = LTTNG_OK;
2468 break;
2469 }
2470 case LTTNG_LIST_TRACEPOINT_FIELDS:
2471 {
2472 struct lttng_event_field *fields;
2473 ssize_t nb_fields;
2474
2475 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
2476 &fields);
2477 if (nb_fields < 0) {
2478 /* Return value is a negative lttng_error_code. */
2479 ret = -nb_fields;
2480 goto error;
2481 }
2482
2483 /*
2484 * Setup lttng message with payload size set to the event list size in
2485 * bytes and then copy list into the llm payload.
2486 */
2487 ret = setup_lttng_msg(cmd_ctx,
2488 sizeof(struct lttng_event_field) * nb_fields);
2489 if (ret < 0) {
2490 free(fields);
2491 goto setup_error;
2492 }
2493
2494 /* Copy event list into message payload */
2495 memcpy(cmd_ctx->llm->payload, fields,
2496 sizeof(struct lttng_event_field) * nb_fields);
2497
2498 free(fields);
2499
2500 ret = LTTNG_OK;
2501 break;
2502 }
2503 case LTTNG_SET_CONSUMER_URI:
2504 {
2505 size_t nb_uri, len;
2506 struct lttng_uri *uris;
2507
2508 nb_uri = cmd_ctx->lsm->u.uri.size;
2509 len = nb_uri * sizeof(struct lttng_uri);
2510
2511 if (nb_uri == 0) {
2512 ret = LTTNG_ERR_INVALID;
2513 goto error;
2514 }
2515
2516 uris = zmalloc(len);
2517 if (uris == NULL) {
2518 ret = LTTNG_ERR_FATAL;
2519 goto error;
2520 }
2521
2522 /* Receive variable len data */
2523 DBG("Receiving %zu URI(s) from client ...", nb_uri);
2524 ret = lttcomm_recv_unix_sock(sock, uris, len);
2525 if (ret <= 0) {
2526 DBG("No URIs received from client... continuing");
2527 *sock_error = 1;
2528 ret = LTTNG_ERR_SESSION_FAIL;
2529 goto error;
2530 }
2531
2532 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
2533 nb_uri, uris);
2534 if (ret != LTTNG_OK) {
2535 goto error;
2536 }
2537
2538 /*
2539 * XXX: 0 means that this URI should be applied on the session. Should
2540 * be a DOMAIN enuam.
2541 */
2542 if (cmd_ctx->lsm->domain.type == 0) {
2543 /* Add the URI for the UST session if a consumer is present. */
2544 if (cmd_ctx->session->ust_session &&
2545 cmd_ctx->session->ust_session->consumer) {
2546 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
2547 nb_uri, uris);
2548 } else if (cmd_ctx->session->kernel_session &&
2549 cmd_ctx->session->kernel_session->consumer) {
2550 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
2551 cmd_ctx->session, nb_uri, uris);
2552 }
2553 }
2554
2555 break;
2556 }
2557 case LTTNG_START_TRACE:
2558 {
2559 ret = cmd_start_trace(cmd_ctx->session);
2560 break;
2561 }
2562 case LTTNG_STOP_TRACE:
2563 {
2564 ret = cmd_stop_trace(cmd_ctx->session);
2565 break;
2566 }
2567 case LTTNG_CREATE_SESSION:
2568 {
2569 size_t nb_uri, len;
2570 struct lttng_uri *uris = NULL;
2571
2572 nb_uri = cmd_ctx->lsm->u.uri.size;
2573 len = nb_uri * sizeof(struct lttng_uri);
2574
2575 if (nb_uri > 0) {
2576 uris = zmalloc(len);
2577 if (uris == NULL) {
2578 ret = LTTNG_ERR_FATAL;
2579 goto error;
2580 }
2581
2582 /* Receive variable len data */
2583 DBG("Waiting for %zu URIs from client ...", nb_uri);
2584 ret = lttcomm_recv_unix_sock(sock, uris, len);
2585 if (ret <= 0) {
2586 DBG("No URIs received from client... continuing");
2587 *sock_error = 1;
2588 ret = LTTNG_ERR_SESSION_FAIL;
2589 goto error;
2590 }
2591
2592 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
2593 DBG("Creating session with ONE network URI is a bad call");
2594 ret = LTTNG_ERR_SESSION_FAIL;
2595 goto error;
2596 }
2597 }
2598
2599 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
2600 &cmd_ctx->creds);
2601
2602 break;
2603 }
2604 case LTTNG_DESTROY_SESSION:
2605 {
2606 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
2607
2608 /* Set session to NULL so we do not unlock it after free. */
2609 cmd_ctx->session = NULL;
2610 break;
2611 }
2612 case LTTNG_LIST_DOMAINS:
2613 {
2614 ssize_t nb_dom;
2615 struct lttng_domain *domains;
2616
2617 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
2618 if (nb_dom < 0) {
2619 /* Return value is a negative lttng_error_code. */
2620 ret = -nb_dom;
2621 goto error;
2622 }
2623
2624 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
2625 if (ret < 0) {
2626 goto setup_error;
2627 }
2628
2629 /* Copy event list into message payload */
2630 memcpy(cmd_ctx->llm->payload, domains,
2631 nb_dom * sizeof(struct lttng_domain));
2632
2633 free(domains);
2634
2635 ret = LTTNG_OK;
2636 break;
2637 }
2638 case LTTNG_LIST_CHANNELS:
2639 {
2640 int nb_chan;
2641 struct lttng_channel *channels;
2642
2643 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
2644 cmd_ctx->session, &channels);
2645 if (nb_chan < 0) {
2646 /* Return value is a negative lttng_error_code. */
2647 ret = -nb_chan;
2648 goto error;
2649 }
2650
2651 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
2652 if (ret < 0) {
2653 goto setup_error;
2654 }
2655
2656 /* Copy event list into message payload */
2657 memcpy(cmd_ctx->llm->payload, channels,
2658 nb_chan * sizeof(struct lttng_channel));
2659
2660 free(channels);
2661
2662 ret = LTTNG_OK;
2663 break;
2664 }
2665 case LTTNG_LIST_EVENTS:
2666 {
2667 ssize_t nb_event;
2668 struct lttng_event *events = NULL;
2669
2670 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
2671 cmd_ctx->lsm->u.list.channel_name, &events);
2672 if (nb_event < 0) {
2673 /* Return value is a negative lttng_error_code. */
2674 ret = -nb_event;
2675 goto error;
2676 }
2677
2678 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
2679 if (ret < 0) {
2680 goto setup_error;
2681 }
2682
2683 /* Copy event list into message payload */
2684 memcpy(cmd_ctx->llm->payload, events,
2685 nb_event * sizeof(struct lttng_event));
2686
2687 free(events);
2688
2689 ret = LTTNG_OK;
2690 break;
2691 }
2692 case LTTNG_LIST_SESSIONS:
2693 {
2694 unsigned int nr_sessions;
2695
2696 session_lock_list();
2697 nr_sessions = lttng_sessions_count(
2698 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2699 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
2700
2701 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
2702 if (ret < 0) {
2703 session_unlock_list();
2704 goto setup_error;
2705 }
2706
2707 /* Filled the session array */
2708 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
2709 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2710 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
2711
2712 session_unlock_list();
2713
2714 ret = LTTNG_OK;
2715 break;
2716 }
2717 case LTTNG_CALIBRATE:
2718 {
2719 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
2720 &cmd_ctx->lsm->u.calibrate);
2721 break;
2722 }
2723 case LTTNG_REGISTER_CONSUMER:
2724 {
2725 struct consumer_data *cdata;
2726
2727 switch (cmd_ctx->lsm->domain.type) {
2728 case LTTNG_DOMAIN_KERNEL:
2729 cdata = &kconsumer_data;
2730 break;
2731 default:
2732 ret = LTTNG_ERR_UND;
2733 goto error;
2734 }
2735
2736 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2737 cmd_ctx->lsm->u.reg.path, cdata);
2738 break;
2739 }
2740 case LTTNG_SET_FILTER:
2741 {
2742 struct lttng_filter_bytecode *bytecode;
2743
2744 if (cmd_ctx->lsm->u.filter.bytecode_len > LTTNG_FILTER_MAX_LEN) {
2745 ret = LTTNG_ERR_FILTER_INVAL;
2746 goto error;
2747 }
2748 bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len);
2749 if (!bytecode) {
2750 ret = LTTNG_ERR_FILTER_NOMEM;
2751 goto error;
2752 }
2753 /* Receive var. len. data */
2754 DBG("Receiving var len data from client ...");
2755 ret = lttcomm_recv_unix_sock(sock, bytecode,
2756 cmd_ctx->lsm->u.filter.bytecode_len);
2757 if (ret <= 0) {
2758 DBG("Nothing recv() from client var len data... continuing");
2759 *sock_error = 1;
2760 ret = LTTNG_ERR_FILTER_INVAL;
2761 goto error;
2762 }
2763
2764 if (bytecode->len + sizeof(*bytecode)
2765 != cmd_ctx->lsm->u.filter.bytecode_len) {
2766 free(bytecode);
2767 ret = LTTNG_ERR_FILTER_INVAL;
2768 goto error;
2769 }
2770
2771 ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2772 cmd_ctx->lsm->u.filter.channel_name,
2773 cmd_ctx->lsm->u.filter.event_name,
2774 bytecode);
2775 break;
2776 }
2777 default:
2778 ret = LTTNG_ERR_UND;
2779 break;
2780 }
2781
2782 error:
2783 if (cmd_ctx->llm == NULL) {
2784 DBG("Missing llm structure. Allocating one.");
2785 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
2786 goto setup_error;
2787 }
2788 }
2789 /* Set return code */
2790 cmd_ctx->llm->ret_code = ret;
2791 setup_error:
2792 if (cmd_ctx->session) {
2793 session_unlock(cmd_ctx->session);
2794 }
2795 if (need_tracing_session) {
2796 session_unlock_list();
2797 }
2798 init_setup_error:
2799 return ret;
2800 }
2801
2802 /*
2803 * Thread managing health check socket.
2804 */
2805 static void *thread_manage_health(void *data)
2806 {
2807 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
2808 uint32_t revents, nb_fd;
2809 struct lttng_poll_event events;
2810 struct lttcomm_health_msg msg;
2811 struct lttcomm_health_data reply;
2812
2813 DBG("[thread] Manage health check started");
2814
2815 rcu_register_thread();
2816
2817 /* Create unix socket */
2818 sock = lttcomm_create_unix_sock(health_unix_sock_path);
2819 if (sock < 0) {
2820 ERR("Unable to create health check Unix socket");
2821 ret = -1;
2822 goto error;
2823 }
2824
2825 ret = lttcomm_listen_unix_sock(sock);
2826 if (ret < 0) {
2827 goto error;
2828 }
2829
2830 /*
2831 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2832 * more will be added to this poll set.
2833 */
2834 ret = create_thread_poll_set(&events, 2);
2835 if (ret < 0) {
2836 goto error;
2837 }
2838
2839 /* Add the application registration socket */
2840 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
2841 if (ret < 0) {
2842 goto error;
2843 }
2844
2845 while (1) {
2846 DBG("Health check ready");
2847
2848 nb_fd = LTTNG_POLL_GETNB(&events);
2849
2850 /* Inifinite blocking call, waiting for transmission */
2851 restart:
2852 ret = lttng_poll_wait(&events, -1);
2853 if (ret < 0) {
2854 /*
2855 * Restart interrupted system call.
2856 */
2857 if (errno == EINTR) {
2858 goto restart;
2859 }
2860 goto error;
2861 }
2862
2863 for (i = 0; i < nb_fd; i++) {
2864 /* Fetch once the poll data */
2865 revents = LTTNG_POLL_GETEV(&events, i);
2866 pollfd = LTTNG_POLL_GETFD(&events, i);
2867
2868 /* Thread quit pipe has been closed. Killing thread. */
2869 ret = check_thread_quit_pipe(pollfd, revents);
2870 if (ret) {
2871 err = 0;
2872 goto exit;
2873 }
2874
2875 /* Event on the registration socket */
2876 if (pollfd == sock) {
2877 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2878 ERR("Health socket poll error");
2879 goto error;
2880 }
2881 }
2882 }
2883
2884 new_sock = lttcomm_accept_unix_sock(sock);
2885 if (new_sock < 0) {
2886 goto error;
2887 }
2888
2889 DBG("Receiving data from client for health...");
2890 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
2891 if (ret <= 0) {
2892 DBG("Nothing recv() from client... continuing");
2893 ret = close(new_sock);
2894 if (ret) {
2895 PERROR("close");
2896 }
2897 new_sock = -1;
2898 continue;
2899 }
2900
2901 rcu_thread_online();
2902
2903 switch (msg.component) {
2904 case LTTNG_HEALTH_CMD:
2905 reply.ret_code = health_check_state(&health_thread_cmd);
2906 break;
2907 case LTTNG_HEALTH_APP_MANAGE:
2908 reply.ret_code = health_check_state(&health_thread_app_manage);
2909 break;
2910 case LTTNG_HEALTH_APP_REG:
2911 reply.ret_code = health_check_state(&health_thread_app_reg);
2912 break;
2913 case LTTNG_HEALTH_KERNEL:
2914 reply.ret_code = health_check_state(&health_thread_kernel);
2915 break;
2916 case LTTNG_HEALTH_CONSUMER:
2917 reply.ret_code = check_consumer_health();
2918 break;
2919 case LTTNG_HEALTH_ALL:
2920 reply.ret_code =
2921 health_check_state(&health_thread_app_manage) &&
2922 health_check_state(&health_thread_app_reg) &&
2923 health_check_state(&health_thread_cmd) &&
2924 health_check_state(&health_thread_kernel) &&
2925 check_consumer_health();
2926 break;
2927 default:
2928 reply.ret_code = LTTNG_ERR_UND;
2929 break;
2930 }
2931
2932 /*
2933 * Flip ret value since 0 is a success and 1 indicates a bad health for
2934 * the client where in the sessiond it is the opposite. Again, this is
2935 * just to make things easier for us poor developer which enjoy a lot
2936 * lazyness.
2937 */
2938 if (reply.ret_code == 0 || reply.ret_code == 1) {
2939 reply.ret_code = !reply.ret_code;
2940 }
2941
2942 DBG2("Health check return value %d", reply.ret_code);
2943
2944 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
2945 if (ret < 0) {
2946 ERR("Failed to send health data back to client");
2947 }
2948
2949 /* End of transmission */
2950 ret = close(new_sock);
2951 if (ret) {
2952 PERROR("close");
2953 }
2954 new_sock = -1;
2955 }
2956
2957 exit:
2958 error:
2959 if (err) {
2960 ERR("Health error occurred in %s", __func__);
2961 }
2962 DBG("Health check thread dying");
2963 unlink(health_unix_sock_path);
2964 if (sock >= 0) {
2965 ret = close(sock);
2966 if (ret) {
2967 PERROR("close");
2968 }
2969 }
2970 if (new_sock >= 0) {
2971 ret = close(new_sock);
2972 if (ret) {
2973 PERROR("close");
2974 }
2975 }
2976
2977 lttng_poll_clean(&events);
2978
2979 rcu_unregister_thread();
2980 return NULL;
2981 }
2982
2983 /*
2984 * This thread manage all clients request using the unix client socket for
2985 * communication.
2986 */
2987 static void *thread_manage_clients(void *data)
2988 {
2989 int sock = -1, ret, i, pollfd, err = -1;
2990 int sock_error;
2991 uint32_t revents, nb_fd;
2992 struct command_ctx *cmd_ctx = NULL;
2993 struct lttng_poll_event events;
2994
2995 DBG("[thread] Manage client started");
2996
2997 rcu_register_thread();
2998
2999 health_code_update(&health_thread_cmd);
3000
3001 ret = lttcomm_listen_unix_sock(client_sock);
3002 if (ret < 0) {
3003 goto error;
3004 }
3005
3006 /*
3007 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3008 * more will be added to this poll set.
3009 */
3010 ret = create_thread_poll_set(&events, 2);
3011 if (ret < 0) {
3012 goto error;
3013 }
3014
3015 /* Add the application registration socket */
3016 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3017 if (ret < 0) {
3018 goto error;
3019 }
3020
3021 /*
3022 * Notify parent pid that we are ready to accept command for client side.
3023 */
3024 if (opt_sig_parent) {
3025 kill(ppid, SIGUSR1);
3026 }
3027
3028 health_code_update(&health_thread_cmd);
3029
3030 while (1) {
3031 DBG("Accepting client command ...");
3032
3033 nb_fd = LTTNG_POLL_GETNB(&events);
3034
3035 /* Inifinite blocking call, waiting for transmission */
3036 restart:
3037 health_poll_update(&health_thread_cmd);
3038 ret = lttng_poll_wait(&events, -1);
3039 health_poll_update(&health_thread_cmd);
3040 if (ret < 0) {
3041 /*
3042 * Restart interrupted system call.
3043 */
3044 if (errno == EINTR) {
3045 goto restart;
3046 }
3047 goto error;
3048 }
3049
3050 for (i = 0; i < nb_fd; i++) {
3051 /* Fetch once the poll data */
3052 revents = LTTNG_POLL_GETEV(&events, i);
3053 pollfd = LTTNG_POLL_GETFD(&events, i);
3054
3055 health_code_update(&health_thread_cmd);
3056
3057 /* Thread quit pipe has been closed. Killing thread. */
3058 ret = check_thread_quit_pipe(pollfd, revents);
3059 if (ret) {
3060 err = 0;
3061 goto exit;
3062 }
3063
3064 /* Event on the registration socket */
3065 if (pollfd == client_sock) {
3066 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3067 ERR("Client socket poll error");
3068 goto error;
3069 }
3070 }
3071 }
3072
3073 DBG("Wait for client response");
3074
3075 health_code_update(&health_thread_cmd);
3076
3077 sock = lttcomm_accept_unix_sock(client_sock);
3078 if (sock < 0) {
3079 goto error;
3080 }
3081
3082 /* Set socket option for credentials retrieval */
3083 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3084 if (ret < 0) {
3085 goto error;
3086 }
3087
3088 /* Allocate context command to process the client request */
3089 cmd_ctx = zmalloc(sizeof(struct command_ctx));
3090 if (cmd_ctx == NULL) {
3091 PERROR("zmalloc cmd_ctx");
3092 goto error;
3093 }
3094
3095 /* Allocate data buffer for reception */
3096 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
3097 if (cmd_ctx->lsm == NULL) {
3098 PERROR("zmalloc cmd_ctx->lsm");
3099 goto error;
3100 }
3101
3102 cmd_ctx->llm = NULL;
3103 cmd_ctx->session = NULL;
3104
3105 health_code_update(&health_thread_cmd);
3106
3107 /*
3108 * Data is received from the lttng client. The struct
3109 * lttcomm_session_msg (lsm) contains the command and data request of
3110 * the client.
3111 */
3112 DBG("Receiving data from client ...");
3113 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3114 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
3115 if (ret <= 0) {
3116 DBG("Nothing recv() from client... continuing");
3117 ret = close(sock);
3118 if (ret) {
3119 PERROR("close");
3120 }
3121 sock = -1;
3122 clean_command_ctx(&cmd_ctx);
3123 continue;
3124 }
3125
3126 health_code_update(&health_thread_cmd);
3127
3128 // TODO: Validate cmd_ctx including sanity check for
3129 // security purpose.
3130
3131 rcu_thread_online();
3132 /*
3133 * This function dispatch the work to the kernel or userspace tracer
3134 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3135 * informations for the client. The command context struct contains
3136 * everything this function may needs.
3137 */
3138 ret = process_client_msg(cmd_ctx, sock, &sock_error);
3139 rcu_thread_offline();
3140 if (ret < 0) {
3141 if (sock_error) {
3142 ret = close(sock);
3143 if (ret) {
3144 PERROR("close");
3145 }
3146 sock = -1;
3147 }
3148 /*
3149 * TODO: Inform client somehow of the fatal error. At
3150 * this point, ret < 0 means that a zmalloc failed
3151 * (ENOMEM). Error detected but still accept
3152 * command, unless a socket error has been
3153 * detected.
3154 */
3155 clean_command_ctx(&cmd_ctx);
3156 continue;
3157 }
3158
3159 health_code_update(&health_thread_cmd);
3160
3161 DBG("Sending response (size: %d, retcode: %s)",
3162 cmd_ctx->lttng_msg_size,
3163 lttng_strerror(-cmd_ctx->llm->ret_code));
3164 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
3165 if (ret < 0) {
3166 ERR("Failed to send data back to client");
3167 }
3168
3169 /* End of transmission */
3170 ret = close(sock);
3171 if (ret) {
3172 PERROR("close");
3173 }
3174 sock = -1;
3175
3176 clean_command_ctx(&cmd_ctx);
3177
3178 health_code_update(&health_thread_cmd);
3179 }
3180
3181 exit:
3182 error:
3183 if (err) {
3184 health_error(&health_thread_cmd);
3185 ERR("Health error occurred in %s", __func__);
3186 }
3187 health_exit(&health_thread_cmd);
3188
3189 DBG("Client thread dying");
3190 unlink(client_unix_sock_path);
3191 if (client_sock >= 0) {
3192 ret = close(client_sock);
3193 if (ret) {
3194 PERROR("close");
3195 }
3196 }
3197 if (sock >= 0) {
3198 ret = close(sock);
3199 if (ret) {
3200 PERROR("close");
3201 }
3202 }
3203
3204 lttng_poll_clean(&events);
3205 clean_command_ctx(&cmd_ctx);
3206
3207 rcu_unregister_thread();
3208 return NULL;
3209 }
3210
3211
3212 /*
3213 * usage function on stderr
3214 */
3215 static void usage(void)
3216 {
3217 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
3218 fprintf(stderr, " -h, --help Display this usage.\n");
3219 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3220 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3221 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3222 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3223 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3224 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3225 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3226 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3227 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3228 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3229 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3230 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3231 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
3232 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3233 fprintf(stderr, " -V, --version Show version number.\n");
3234 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3235 fprintf(stderr, " -q, --quiet No output at all.\n");
3236 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3237 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3238 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
3239 }
3240
3241 /*
3242 * daemon argument parsing
3243 */
3244 static int parse_args(int argc, char **argv)
3245 {
3246 int c;
3247
3248 static struct option long_options[] = {
3249 { "client-sock", 1, 0, 'c' },
3250 { "apps-sock", 1, 0, 'a' },
3251 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3252 { "kconsumerd-err-sock", 1, 0, 'E' },
3253 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3254 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3255 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3256 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3257 { "consumerd32-path", 1, 0, 'u' },
3258 { "consumerd32-libdir", 1, 0, 'U' },
3259 { "consumerd64-path", 1, 0, 't' },
3260 { "consumerd64-libdir", 1, 0, 'T' },
3261 { "daemonize", 0, 0, 'd' },
3262 { "sig-parent", 0, 0, 'S' },
3263 { "help", 0, 0, 'h' },
3264 { "group", 1, 0, 'g' },
3265 { "version", 0, 0, 'V' },
3266 { "quiet", 0, 0, 'q' },
3267 { "verbose", 0, 0, 'v' },
3268 { "verbose-consumer", 0, 0, 'Z' },
3269 { "no-kernel", 0, 0, 'N' },
3270 { NULL, 0, 0, 0 }
3271 };
3272
3273 while (1) {
3274 int option_index = 0;
3275 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
3276 long_options, &option_index);
3277 if (c == -1) {
3278 break;
3279 }
3280
3281 switch (c) {
3282 case 0:
3283 fprintf(stderr, "option %s", long_options[option_index].name);
3284 if (optarg) {
3285 fprintf(stderr, " with arg %s\n", optarg);
3286 }
3287 break;
3288 case 'c':
3289 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
3290 break;
3291 case 'a':
3292 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
3293 break;
3294 case 'd':
3295 opt_daemon = 1;
3296 break;
3297 case 'g':
3298 opt_tracing_group = optarg;
3299 break;
3300 case 'h':
3301 usage();
3302 exit(EXIT_FAILURE);
3303 case 'V':
3304 fprintf(stdout, "%s\n", VERSION);
3305 exit(EXIT_SUCCESS);
3306 case 'S':
3307 opt_sig_parent = 1;
3308 break;
3309 case 'E':
3310 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3311 break;
3312 case 'C':
3313 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3314 break;
3315 case 'F':
3316 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3317 break;
3318 case 'D':
3319 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3320 break;
3321 case 'H':
3322 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3323 break;
3324 case 'G':
3325 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3326 break;
3327 case 'N':
3328 opt_no_kernel = 1;
3329 break;
3330 case 'q':
3331 lttng_opt_quiet = 1;
3332 break;
3333 case 'v':
3334 /* Verbose level can increase using multiple -v */
3335 lttng_opt_verbose += 1;
3336 break;
3337 case 'Z':
3338 opt_verbose_consumer += 1;
3339 break;
3340 case 'u':
3341 consumerd32_bin= optarg;
3342 break;
3343 case 'U':
3344 consumerd32_libdir = optarg;
3345 break;
3346 case 't':
3347 consumerd64_bin = optarg;
3348 break;
3349 case 'T':
3350 consumerd64_libdir = optarg;
3351 break;
3352 default:
3353 /* Unknown option or other error.
3354 * Error is printed by getopt, just return */
3355 return -1;
3356 }
3357 }
3358
3359 return 0;
3360 }
3361
3362 /*
3363 * Creates the two needed socket by the daemon.
3364 * apps_sock - The communication socket for all UST apps.
3365 * client_sock - The communication of the cli tool (lttng).
3366 */
3367 static int init_daemon_socket(void)
3368 {
3369 int ret = 0;
3370 mode_t old_umask;
3371
3372 old_umask = umask(0);
3373
3374 /* Create client tool unix socket */
3375 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
3376 if (client_sock < 0) {
3377 ERR("Create unix sock failed: %s", client_unix_sock_path);
3378 ret = -1;
3379 goto end;
3380 }
3381
3382 /* File permission MUST be 660 */
3383 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3384 if (ret < 0) {
3385 ERR("Set file permissions failed: %s", client_unix_sock_path);
3386 PERROR("chmod");
3387 goto end;
3388 }
3389
3390 /* Create the application unix socket */
3391 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
3392 if (apps_sock < 0) {
3393 ERR("Create unix sock failed: %s", apps_unix_sock_path);
3394 ret = -1;
3395 goto end;
3396 }
3397
3398 /* File permission MUST be 666 */
3399 ret = chmod(apps_unix_sock_path,
3400 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
3401 if (ret < 0) {
3402 ERR("Set file permissions failed: %s", apps_unix_sock_path);
3403 PERROR("chmod");
3404 goto end;
3405 }
3406
3407 end:
3408 umask(old_umask);
3409 return ret;
3410 }
3411
3412 /*
3413 * Check if the global socket is available, and if a daemon is answering at the
3414 * other side. If yes, error is returned.
3415 */
3416 static int check_existing_daemon(void)
3417 {
3418 /* Is there anybody out there ? */
3419 if (lttng_session_daemon_alive()) {
3420 return -EEXIST;
3421 }
3422
3423 return 0;
3424 }
3425
3426 /*
3427 * Set the tracing group gid onto the client socket.
3428 *
3429 * Race window between mkdir and chown is OK because we are going from more
3430 * permissive (root.root) to less permissive (root.tracing).
3431 */
3432 static int set_permissions(char *rundir)
3433 {
3434 int ret;
3435 gid_t gid;
3436
3437 ret = allowed_group();
3438 if (ret < 0) {
3439 WARN("No tracing group detected");
3440 ret = 0;
3441 goto end;
3442 }
3443
3444 gid = ret;
3445
3446 /* Set lttng run dir */
3447 ret = chown(rundir, 0, gid);
3448 if (ret < 0) {
3449 ERR("Unable to set group on %s", rundir);
3450 PERROR("chown");
3451 }
3452
3453 /* Ensure tracing group can search the run dir */
3454 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
3455 if (ret < 0) {
3456 ERR("Unable to set permissions on %s", rundir);
3457 PERROR("chmod");
3458 }
3459
3460 /* lttng client socket path */
3461 ret = chown(client_unix_sock_path, 0, gid);
3462 if (ret < 0) {
3463 ERR("Unable to set group on %s", client_unix_sock_path);
3464 PERROR("chown");
3465 }
3466
3467 /* kconsumer error socket path */
3468 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
3469 if (ret < 0) {
3470 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
3471 PERROR("chown");
3472 }
3473
3474 /* 64-bit ustconsumer error socket path */
3475 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
3476 if (ret < 0) {
3477 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
3478 PERROR("chown");
3479 }
3480
3481 /* 32-bit ustconsumer compat32 error socket path */
3482 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3483 if (ret < 0) {
3484 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
3485 PERROR("chown");
3486 }
3487
3488 DBG("All permissions are set");
3489
3490 end:
3491 return ret;
3492 }
3493
3494 /*
3495 * Create the lttng run directory needed for all global sockets and pipe.
3496 */
3497 static int create_lttng_rundir(const char *rundir)
3498 {
3499 int ret;
3500
3501 DBG3("Creating LTTng run directory: %s", rundir);
3502
3503 ret = mkdir(rundir, S_IRWXU);
3504 if (ret < 0) {
3505 if (errno != EEXIST) {
3506 ERR("Unable to create %s", rundir);
3507 goto error;
3508 } else {
3509 ret = 0;
3510 }
3511 }
3512
3513 error:
3514 return ret;
3515 }
3516
3517 /*
3518 * Setup sockets and directory needed by the kconsumerd communication with the
3519 * session daemon.
3520 */
3521 static int set_consumer_sockets(struct consumer_data *consumer_data,
3522 const char *rundir)
3523 {
3524 int ret;
3525 char path[PATH_MAX];
3526
3527 switch (consumer_data->type) {
3528 case LTTNG_CONSUMER_KERNEL:
3529 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
3530 break;
3531 case LTTNG_CONSUMER64_UST:
3532 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
3533 break;
3534 case LTTNG_CONSUMER32_UST:
3535 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
3536 break;
3537 default:
3538 ERR("Consumer type unknown");
3539 ret = -EINVAL;
3540 goto error;
3541 }
3542
3543 DBG2("Creating consumer directory: %s", path);
3544
3545 ret = mkdir(path, S_IRWXU);
3546 if (ret < 0) {
3547 if (errno != EEXIST) {
3548 PERROR("mkdir");
3549 ERR("Failed to create %s", path);
3550 goto error;
3551 }
3552 ret = -1;
3553 }
3554
3555 /* Create the kconsumerd error unix socket */
3556 consumer_data->err_sock =
3557 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
3558 if (consumer_data->err_sock < 0) {
3559 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
3560 ret = -1;
3561 goto error;
3562 }
3563
3564 /* File permission MUST be 660 */
3565 ret = chmod(consumer_data->err_unix_sock_path,
3566 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3567 if (ret < 0) {
3568 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
3569 PERROR("chmod");
3570 goto error;
3571 }
3572
3573 error:
3574 return ret;
3575 }
3576
3577 /*
3578 * Signal handler for the daemon
3579 *
3580 * Simply stop all worker threads, leaving main() return gracefully after
3581 * joining all threads and calling cleanup().
3582 */
3583 static void sighandler(int sig)
3584 {
3585 switch (sig) {
3586 case SIGPIPE:
3587 DBG("SIGPIPE caught");
3588 return;
3589 case SIGINT:
3590 DBG("SIGINT caught");
3591 stop_threads();
3592 break;
3593 case SIGTERM:
3594 DBG("SIGTERM caught");
3595 stop_threads();
3596 break;
3597 default:
3598 break;
3599 }
3600 }
3601
3602 /*
3603 * Setup signal handler for :
3604 * SIGINT, SIGTERM, SIGPIPE
3605 */
3606 static int set_signal_handler(void)
3607 {
3608 int ret = 0;
3609 struct sigaction sa;
3610 sigset_t sigset;
3611
3612 if ((ret = sigemptyset(&sigset)) < 0) {
3613 PERROR("sigemptyset");
3614 return ret;
3615 }
3616
3617 sa.sa_handler = sighandler;
3618 sa.sa_mask = sigset;
3619 sa.sa_flags = 0;
3620 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3621 PERROR("sigaction");
3622 return ret;
3623 }
3624
3625 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3626 PERROR("sigaction");
3627 return ret;
3628 }
3629
3630 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3631 PERROR("sigaction");
3632 return ret;
3633 }
3634
3635 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3636
3637 return ret;
3638 }
3639
3640 /*
3641 * Set open files limit to unlimited. This daemon can open a large number of
3642 * file descriptors in order to consumer multiple kernel traces.
3643 */
3644 static void set_ulimit(void)
3645 {
3646 int ret;
3647 struct rlimit lim;
3648
3649 /* The kernel does not allowed an infinite limit for open files */
3650 lim.rlim_cur = 65535;
3651 lim.rlim_max = 65535;
3652
3653 ret = setrlimit(RLIMIT_NOFILE, &lim);
3654 if (ret < 0) {
3655 PERROR("failed to set open files limit");
3656 }
3657 }
3658
3659 /*
3660 * main
3661 */
3662 int main(int argc, char **argv)
3663 {
3664 int ret = 0;
3665 void *status;
3666 const char *home_path;
3667
3668 init_kernel_workarounds();
3669
3670 rcu_register_thread();
3671
3672 setup_consumerd_path();
3673
3674 /* Parse arguments */
3675 progname = argv[0];
3676 if ((ret = parse_args(argc, argv) < 0)) {
3677 goto error;
3678 }
3679
3680 /* Daemonize */
3681 if (opt_daemon) {
3682 int i;
3683
3684 /*
3685 * fork
3686 * child: setsid, close FD 0, 1, 2, chdir /
3687 * parent: exit (if fork is successful)
3688 */
3689 ret = daemon(0, 0);
3690 if (ret < 0) {
3691 PERROR("daemon");
3692 goto error;
3693 }
3694 /*
3695 * We are in the child. Make sure all other file
3696 * descriptors are closed, in case we are called with
3697 * more opened file descriptors than the standard ones.
3698 */
3699 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
3700 (void) close(i);
3701 }
3702 }
3703
3704 /* Create thread quit pipe */
3705 if ((ret = init_thread_quit_pipe()) < 0) {
3706 goto error;
3707 }
3708
3709 /* Check if daemon is UID = 0 */
3710 is_root = !getuid();
3711
3712 if (is_root) {
3713 rundir = strdup(DEFAULT_LTTNG_RUNDIR);