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