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