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