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