Support per UID buffers
[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 goto error;
1889 }
1890 }
1891 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
1892 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
1893 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1894 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1895 NULL);
1896 if (consumerd64_libdir[0] != '\0') {
1897 free(tmpnew);
1898 }
1899 if (ret) {
1900 goto error;
1901 }
1902 break;
1903 }
1904 case LTTNG_CONSUMER32_UST:
1905 {
1906 char *tmpnew = NULL;
1907
1908 if (consumerd32_libdir[0] != '\0') {
1909 char *tmp;
1910 size_t tmplen;
1911
1912 tmp = getenv("LD_LIBRARY_PATH");
1913 if (!tmp) {
1914 tmp = "";
1915 }
1916 tmplen = strlen("LD_LIBRARY_PATH=")
1917 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1918 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1919 if (!tmpnew) {
1920 ret = -ENOMEM;
1921 goto error;
1922 }
1923 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1924 strcat(tmpnew, consumerd32_libdir);
1925 if (tmp[0] != '\0') {
1926 strcat(tmpnew, ":");
1927 strcat(tmpnew, tmp);
1928 }
1929 ret = putenv(tmpnew);
1930 if (ret) {
1931 ret = -errno;
1932 goto error;
1933 }
1934 }
1935 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
1936 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
1937 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1938 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1939 NULL);
1940 if (consumerd32_libdir[0] != '\0') {
1941 free(tmpnew);
1942 }
1943 if (ret) {
1944 goto error;
1945 }
1946 break;
1947 }
1948 default:
1949 PERROR("unknown consumer type");
1950 exit(EXIT_FAILURE);
1951 }
1952 if (errno != 0) {
1953 PERROR("kernel start consumer exec");
1954 }
1955 exit(EXIT_FAILURE);
1956 } else if (pid > 0) {
1957 ret = pid;
1958 } else {
1959 PERROR("start consumer fork");
1960 ret = -errno;
1961 }
1962 error:
1963 return ret;
1964 }
1965
1966 /*
1967 * Spawn the consumerd daemon and session daemon thread.
1968 */
1969 static int start_consumerd(struct consumer_data *consumer_data)
1970 {
1971 int ret;
1972
1973 /*
1974 * Set the listen() state on the socket since there is a possible race
1975 * between the exec() of the consumer daemon and this call if place in the
1976 * consumer thread. See bug #366 for more details.
1977 */
1978 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
1979 if (ret < 0) {
1980 goto error;
1981 }
1982
1983 pthread_mutex_lock(&consumer_data->pid_mutex);
1984 if (consumer_data->pid != 0) {
1985 pthread_mutex_unlock(&consumer_data->pid_mutex);
1986 goto end;
1987 }
1988
1989 ret = spawn_consumerd(consumer_data);
1990 if (ret < 0) {
1991 ERR("Spawning consumerd failed");
1992 pthread_mutex_unlock(&consumer_data->pid_mutex);
1993 goto error;
1994 }
1995
1996 /* Setting up the consumer_data pid */
1997 consumer_data->pid = ret;
1998 DBG2("Consumer pid %d", consumer_data->pid);
1999 pthread_mutex_unlock(&consumer_data->pid_mutex);
2000
2001 DBG2("Spawning consumer control thread");
2002 ret = spawn_consumer_thread(consumer_data);
2003 if (ret < 0) {
2004 ERR("Fatal error spawning consumer control thread");
2005 goto error;
2006 }
2007
2008 end:
2009 return 0;
2010
2011 error:
2012 /* Cleanup already created socket on error. */
2013 if (consumer_data->err_sock >= 0) {
2014 int err;
2015
2016 err = close(consumer_data->err_sock);
2017 if (err < 0) {
2018 PERROR("close consumer data error socket");
2019 }
2020 }
2021 return ret;
2022 }
2023
2024 /*
2025 * Compute health status of each consumer. If one of them is zero (bad
2026 * state), we return 0.
2027 */
2028 static int check_consumer_health(void)
2029 {
2030 int ret;
2031
2032 ret = health_check_state(HEALTH_TYPE_CONSUMER);
2033
2034 DBG3("Health consumer check %d", ret);
2035
2036 return ret;
2037 }
2038
2039 /*
2040 * Setup necessary data for kernel tracer action.
2041 */
2042 static int init_kernel_tracer(void)
2043 {
2044 int ret;
2045
2046 /* Modprobe lttng kernel modules */
2047 ret = modprobe_lttng_control();
2048 if (ret < 0) {
2049 goto error;
2050 }
2051
2052 /* Open debugfs lttng */
2053 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2054 if (kernel_tracer_fd < 0) {
2055 DBG("Failed to open %s", module_proc_lttng);
2056 ret = -1;
2057 goto error_open;
2058 }
2059
2060 /* Validate kernel version */
2061 ret = kernel_validate_version(kernel_tracer_fd);
2062 if (ret < 0) {
2063 goto error_version;
2064 }
2065
2066 ret = modprobe_lttng_data();
2067 if (ret < 0) {
2068 goto error_modules;
2069 }
2070
2071 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2072 return 0;
2073
2074 error_version:
2075 modprobe_remove_lttng_control();
2076 ret = close(kernel_tracer_fd);
2077 if (ret) {
2078 PERROR("close");
2079 }
2080 kernel_tracer_fd = -1;
2081 return LTTNG_ERR_KERN_VERSION;
2082
2083 error_modules:
2084 ret = close(kernel_tracer_fd);
2085 if (ret) {
2086 PERROR("close");
2087 }
2088
2089 error_open:
2090 modprobe_remove_lttng_control();
2091
2092 error:
2093 WARN("No kernel tracer available");
2094 kernel_tracer_fd = -1;
2095 if (!is_root) {
2096 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2097 } else {
2098 return LTTNG_ERR_KERN_NA;
2099 }
2100 }
2101
2102
2103 /*
2104 * Copy consumer output from the tracing session to the domain session. The
2105 * function also applies the right modification on a per domain basis for the
2106 * trace files destination directory.
2107 */
2108 static int copy_session_consumer(int domain, struct ltt_session *session)
2109 {
2110 int ret;
2111 const char *dir_name;
2112 struct consumer_output *consumer;
2113
2114 assert(session);
2115 assert(session->consumer);
2116
2117 switch (domain) {
2118 case LTTNG_DOMAIN_KERNEL:
2119 DBG3("Copying tracing session consumer output in kernel session");
2120 /*
2121 * XXX: We should audit the session creation and what this function
2122 * does "extra" in order to avoid a destroy since this function is used
2123 * in the domain session creation (kernel and ust) only. Same for UST
2124 * domain.
2125 */
2126 if (session->kernel_session->consumer) {
2127 consumer_destroy_output(session->kernel_session->consumer);
2128 }
2129 session->kernel_session->consumer =
2130 consumer_copy_output(session->consumer);
2131 /* Ease our life a bit for the next part */
2132 consumer = session->kernel_session->consumer;
2133 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2134 break;
2135 case LTTNG_DOMAIN_UST:
2136 DBG3("Copying tracing session consumer output in UST session");
2137 if (session->ust_session->consumer) {
2138 consumer_destroy_output(session->ust_session->consumer);
2139 }
2140 session->ust_session->consumer =
2141 consumer_copy_output(session->consumer);
2142 /* Ease our life a bit for the next part */
2143 consumer = session->ust_session->consumer;
2144 dir_name = DEFAULT_UST_TRACE_DIR;
2145 break;
2146 default:
2147 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2148 goto error;
2149 }
2150
2151 /* Append correct directory to subdir */
2152 strncat(consumer->subdir, dir_name,
2153 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2154 DBG3("Copy session consumer subdir %s", consumer->subdir);
2155
2156 ret = LTTNG_OK;
2157
2158 error:
2159 return ret;
2160 }
2161
2162 /*
2163 * Create an UST session and add it to the session ust list.
2164 */
2165 static int create_ust_session(struct ltt_session *session,
2166 struct lttng_domain *domain)
2167 {
2168 int ret;
2169 struct ltt_ust_session *lus = NULL;
2170
2171 assert(session);
2172 assert(domain);
2173 assert(session->consumer);
2174
2175 switch (domain->type) {
2176 case LTTNG_DOMAIN_UST:
2177 break;
2178 default:
2179 ERR("Unknown UST domain on create session %d", domain->type);
2180 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2181 goto error;
2182 }
2183
2184 DBG("Creating UST session");
2185
2186 lus = trace_ust_create_session(session->path, session->id);
2187 if (lus == NULL) {
2188 ret = LTTNG_ERR_UST_SESS_FAIL;
2189 goto error;
2190 }
2191
2192 lus->uid = session->uid;
2193 lus->gid = session->gid;
2194 session->ust_session = lus;
2195
2196 /* Copy session output to the newly created UST session */
2197 ret = copy_session_consumer(domain->type, session);
2198 if (ret != LTTNG_OK) {
2199 goto error;
2200 }
2201
2202 return LTTNG_OK;
2203
2204 error:
2205 free(lus);
2206 session->ust_session = NULL;
2207 return ret;
2208 }
2209
2210 /*
2211 * Create a kernel tracer session then create the default channel.
2212 */
2213 static int create_kernel_session(struct ltt_session *session)
2214 {
2215 int ret;
2216
2217 DBG("Creating kernel session");
2218
2219 ret = kernel_create_session(session, kernel_tracer_fd);
2220 if (ret < 0) {
2221 ret = LTTNG_ERR_KERN_SESS_FAIL;
2222 goto error;
2223 }
2224
2225 /* Code flow safety */
2226 assert(session->kernel_session);
2227
2228 /* Copy session output to the newly created Kernel session */
2229 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2230 if (ret != LTTNG_OK) {
2231 goto error;
2232 }
2233
2234 /* Create directory(ies) on local filesystem. */
2235 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2236 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2237 ret = run_as_mkdir_recursive(
2238 session->kernel_session->consumer->dst.trace_path,
2239 S_IRWXU | S_IRWXG, session->uid, session->gid);
2240 if (ret < 0) {
2241 if (ret != -EEXIST) {
2242 ERR("Trace directory creation error");
2243 goto error;
2244 }
2245 }
2246 }
2247
2248 session->kernel_session->uid = session->uid;
2249 session->kernel_session->gid = session->gid;
2250
2251 return LTTNG_OK;
2252
2253 error:
2254 trace_kernel_destroy_session(session->kernel_session);
2255 session->kernel_session = NULL;
2256 return ret;
2257 }
2258
2259 /*
2260 * Count number of session permitted by uid/gid.
2261 */
2262 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2263 {
2264 unsigned int i = 0;
2265 struct ltt_session *session;
2266
2267 DBG("Counting number of available session for UID %d GID %d",
2268 uid, gid);
2269 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2270 /*
2271 * Only list the sessions the user can control.
2272 */
2273 if (!session_access_ok(session, uid, gid)) {
2274 continue;
2275 }
2276 i++;
2277 }
2278 return i;
2279 }
2280
2281 /*
2282 * Process the command requested by the lttng client within the command
2283 * context structure. This function make sure that the return structure (llm)
2284 * is set and ready for transmission before returning.
2285 *
2286 * Return any error encountered or 0 for success.
2287 *
2288 * "sock" is only used for special-case var. len data.
2289 */
2290 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2291 int *sock_error)
2292 {
2293 int ret = LTTNG_OK;
2294 int need_tracing_session = 1;
2295 int need_domain;
2296
2297 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2298
2299 *sock_error = 0;
2300
2301 switch (cmd_ctx->lsm->cmd_type) {
2302 case LTTNG_CREATE_SESSION:
2303 case LTTNG_DESTROY_SESSION:
2304 case LTTNG_LIST_SESSIONS:
2305 case LTTNG_LIST_DOMAINS:
2306 case LTTNG_START_TRACE:
2307 case LTTNG_STOP_TRACE:
2308 case LTTNG_DATA_PENDING:
2309 need_domain = 0;
2310 break;
2311 default:
2312 need_domain = 1;
2313 }
2314
2315 if (opt_no_kernel && need_domain
2316 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2317 if (!is_root) {
2318 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2319 } else {
2320 ret = LTTNG_ERR_KERN_NA;
2321 }
2322 goto error;
2323 }
2324
2325 /* Deny register consumer if we already have a spawned consumer. */
2326 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2327 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2328 if (kconsumer_data.pid > 0) {
2329 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2330 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2331 goto error;
2332 }
2333 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2334 }
2335
2336 /*
2337 * Check for command that don't needs to allocate a returned payload. We do
2338 * this here so we don't have to make the call for no payload at each
2339 * command.
2340 */
2341 switch(cmd_ctx->lsm->cmd_type) {
2342 case LTTNG_LIST_SESSIONS:
2343 case LTTNG_LIST_TRACEPOINTS:
2344 case LTTNG_LIST_TRACEPOINT_FIELDS:
2345 case LTTNG_LIST_DOMAINS:
2346 case LTTNG_LIST_CHANNELS:
2347 case LTTNG_LIST_EVENTS:
2348 break;
2349 default:
2350 /* Setup lttng message with no payload */
2351 ret = setup_lttng_msg(cmd_ctx, 0);
2352 if (ret < 0) {
2353 /* This label does not try to unlock the session */
2354 goto init_setup_error;
2355 }
2356 }
2357
2358 /* Commands that DO NOT need a session. */
2359 switch (cmd_ctx->lsm->cmd_type) {
2360 case LTTNG_CREATE_SESSION:
2361 case LTTNG_CALIBRATE:
2362 case LTTNG_LIST_SESSIONS:
2363 case LTTNG_LIST_TRACEPOINTS:
2364 case LTTNG_LIST_TRACEPOINT_FIELDS:
2365 need_tracing_session = 0;
2366 break;
2367 default:
2368 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2369 /*
2370 * We keep the session list lock across _all_ commands
2371 * for now, because the per-session lock does not
2372 * handle teardown properly.
2373 */
2374 session_lock_list();
2375 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2376 if (cmd_ctx->session == NULL) {
2377 if (cmd_ctx->lsm->session.name != NULL) {
2378 ret = LTTNG_ERR_SESS_NOT_FOUND;
2379 } else {
2380 /* If no session name specified */
2381 ret = LTTNG_ERR_SELECT_SESS;
2382 }
2383 goto error;
2384 } else {
2385 /* Acquire lock for the session */
2386 session_lock(cmd_ctx->session);
2387 }
2388 break;
2389 }
2390
2391 if (!need_domain) {
2392 goto skip_domain;
2393 }
2394
2395 /*
2396 * Check domain type for specific "pre-action".
2397 */
2398 switch (cmd_ctx->lsm->domain.type) {
2399 case LTTNG_DOMAIN_KERNEL:
2400 if (!is_root) {
2401 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2402 goto error;
2403 }
2404
2405 /* Kernel tracer check */
2406 if (kernel_tracer_fd == -1) {
2407 /* Basically, load kernel tracer modules */
2408 ret = init_kernel_tracer();
2409 if (ret != 0) {
2410 goto error;
2411 }
2412 }
2413
2414 /* Consumer is in an ERROR state. Report back to client */
2415 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
2416 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2417 goto error;
2418 }
2419
2420 /* Need a session for kernel command */
2421 if (need_tracing_session) {
2422 if (cmd_ctx->session->kernel_session == NULL) {
2423 ret = create_kernel_session(cmd_ctx->session);
2424 if (ret < 0) {
2425 ret = LTTNG_ERR_KERN_SESS_FAIL;
2426 goto error;
2427 }
2428 }
2429
2430 /* Start the kernel consumer daemon */
2431 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2432 if (kconsumer_data.pid == 0 &&
2433 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2434 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2435 ret = start_consumerd(&kconsumer_data);
2436 if (ret < 0) {
2437 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2438 goto error;
2439 }
2440 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
2441 } else {
2442 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2443 }
2444
2445 /*
2446 * The consumer was just spawned so we need to add the socket to
2447 * the consumer output of the session if exist.
2448 */
2449 ret = consumer_create_socket(&kconsumer_data,
2450 cmd_ctx->session->kernel_session->consumer);
2451 if (ret < 0) {
2452 goto error;
2453 }
2454 }
2455
2456 break;
2457 case LTTNG_DOMAIN_UST:
2458 {
2459 /* Consumer is in an ERROR state. Report back to client */
2460 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
2461 ret = LTTNG_ERR_NO_USTCONSUMERD;
2462 goto error;
2463 }
2464
2465 if (need_tracing_session) {
2466 /* Create UST session if none exist. */
2467 if (cmd_ctx->session->ust_session == NULL) {
2468 ret = create_ust_session(cmd_ctx->session,
2469 &cmd_ctx->lsm->domain);
2470 if (ret != LTTNG_OK) {
2471 goto error;
2472 }
2473 }
2474
2475 /* Start the UST consumer daemons */
2476 /* 64-bit */
2477 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
2478 if (consumerd64_bin[0] != '\0' &&
2479 ustconsumer64_data.pid == 0 &&
2480 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2481 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2482 ret = start_consumerd(&ustconsumer64_data);
2483 if (ret < 0) {
2484 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
2485 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2486 goto error;
2487 }
2488
2489 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
2490 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2491 } else {
2492 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2493 }
2494
2495 /*
2496 * Setup socket for consumer 64 bit. No need for atomic access
2497 * since it was set above and can ONLY be set in this thread.
2498 */
2499 ret = consumer_create_socket(&ustconsumer64_data,
2500 cmd_ctx->session->ust_session->consumer);
2501 if (ret < 0) {
2502 goto error;
2503 }
2504
2505 /* 32-bit */
2506 if (consumerd32_bin[0] != '\0' &&
2507 ustconsumer32_data.pid == 0 &&
2508 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2509 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2510 ret = start_consumerd(&ustconsumer32_data);
2511 if (ret < 0) {
2512 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
2513 uatomic_set(&ust_consumerd32_fd, -EINVAL);
2514 goto error;
2515 }
2516
2517 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
2518 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2519 } else {
2520 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2521 }
2522
2523 /*
2524 * Setup socket for consumer 64 bit. No need for atomic access
2525 * since it was set above and can ONLY be set in this thread.
2526 */
2527 ret = consumer_create_socket(&ustconsumer32_data,
2528 cmd_ctx->session->ust_session->consumer);
2529 if (ret < 0) {
2530 goto error;
2531 }
2532 }
2533 break;
2534 }
2535 default:
2536 break;
2537 }
2538 skip_domain:
2539
2540 /* Validate consumer daemon state when start/stop trace command */
2541 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2542 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2543 switch (cmd_ctx->lsm->domain.type) {
2544 case LTTNG_DOMAIN_UST:
2545 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
2546 ret = LTTNG_ERR_NO_USTCONSUMERD;
2547 goto error;
2548 }
2549 break;
2550 case LTTNG_DOMAIN_KERNEL:
2551 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
2552 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2553 goto error;
2554 }
2555 break;
2556 }
2557 }
2558
2559 /*
2560 * Check that the UID or GID match that of the tracing session.
2561 * The root user can interact with all sessions.
2562 */
2563 if (need_tracing_session) {
2564 if (!session_access_ok(cmd_ctx->session,
2565 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2566 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
2567 ret = LTTNG_ERR_EPERM;
2568 goto error;
2569 }
2570 }
2571
2572 /*
2573 * Send relayd information to consumer as soon as we have a domain and a
2574 * session defined.
2575 */
2576 if (cmd_ctx->session && need_domain) {
2577 /*
2578 * Setup relayd if not done yet. If the relayd information was already
2579 * sent to the consumer, this call will gracefully return.
2580 */
2581 ret = cmd_setup_relayd(cmd_ctx->session);
2582 if (ret != LTTNG_OK) {
2583 goto error;
2584 }
2585 }
2586
2587 /* Process by command type */
2588 switch (cmd_ctx->lsm->cmd_type) {
2589 case LTTNG_ADD_CONTEXT:
2590 {
2591 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2592 cmd_ctx->lsm->u.context.channel_name,
2593 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
2594 break;
2595 }
2596 case LTTNG_DISABLE_CHANNEL:
2597 {
2598 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2599 cmd_ctx->lsm->u.disable.channel_name);
2600 break;
2601 }
2602 case LTTNG_DISABLE_EVENT:
2603 {
2604 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2605 cmd_ctx->lsm->u.disable.channel_name,
2606 cmd_ctx->lsm->u.disable.name);
2607 break;
2608 }
2609 case LTTNG_DISABLE_ALL_EVENT:
2610 {
2611 DBG("Disabling all events");
2612
2613 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2614 cmd_ctx->lsm->u.disable.channel_name);
2615 break;
2616 }
2617 case LTTNG_ENABLE_CHANNEL:
2618 {
2619 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2620 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
2621 break;
2622 }
2623 case LTTNG_ENABLE_EVENT:
2624 {
2625 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
2626 cmd_ctx->lsm->u.enable.channel_name,
2627 &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]);
2628 break;
2629 }
2630 case LTTNG_ENABLE_ALL_EVENT:
2631 {
2632 DBG("Enabling all events");
2633
2634 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
2635 cmd_ctx->lsm->u.enable.channel_name,
2636 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
2637 break;
2638 }
2639 case LTTNG_LIST_TRACEPOINTS:
2640 {
2641 struct lttng_event *events;
2642 ssize_t nb_events;
2643
2644 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
2645 if (nb_events < 0) {
2646 /* Return value is a negative lttng_error_code. */
2647 ret = -nb_events;
2648 goto error;
2649 }
2650
2651 /*
2652 * Setup lttng message with payload size set to the event list size in
2653 * bytes and then copy list into the llm payload.
2654 */
2655 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2656 if (ret < 0) {
2657 free(events);
2658 goto setup_error;
2659 }
2660
2661 /* Copy event list into message payload */
2662 memcpy(cmd_ctx->llm->payload, events,
2663 sizeof(struct lttng_event) * nb_events);
2664
2665 free(events);
2666
2667 ret = LTTNG_OK;
2668 break;
2669 }
2670 case LTTNG_LIST_TRACEPOINT_FIELDS:
2671 {
2672 struct lttng_event_field *fields;
2673 ssize_t nb_fields;
2674
2675 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
2676 &fields);
2677 if (nb_fields < 0) {
2678 /* Return value is a negative lttng_error_code. */
2679 ret = -nb_fields;
2680 goto error;
2681 }
2682
2683 /*
2684 * Setup lttng message with payload size set to the event list size in
2685 * bytes and then copy list into the llm payload.
2686 */
2687 ret = setup_lttng_msg(cmd_ctx,
2688 sizeof(struct lttng_event_field) * nb_fields);
2689 if (ret < 0) {
2690 free(fields);
2691 goto setup_error;
2692 }
2693
2694 /* Copy event list into message payload */
2695 memcpy(cmd_ctx->llm->payload, fields,
2696 sizeof(struct lttng_event_field) * nb_fields);
2697
2698 free(fields);
2699
2700 ret = LTTNG_OK;
2701 break;
2702 }
2703 case LTTNG_SET_CONSUMER_URI:
2704 {
2705 size_t nb_uri, len;
2706 struct lttng_uri *uris;
2707
2708 nb_uri = cmd_ctx->lsm->u.uri.size;
2709 len = nb_uri * sizeof(struct lttng_uri);
2710
2711 if (nb_uri == 0) {
2712 ret = LTTNG_ERR_INVALID;
2713 goto error;
2714 }
2715
2716 uris = zmalloc(len);
2717 if (uris == NULL) {
2718 ret = LTTNG_ERR_FATAL;
2719 goto error;
2720 }
2721
2722 /* Receive variable len data */
2723 DBG("Receiving %zu URI(s) from client ...", nb_uri);
2724 ret = lttcomm_recv_unix_sock(sock, uris, len);
2725 if (ret <= 0) {
2726 DBG("No URIs received from client... continuing");
2727 *sock_error = 1;
2728 ret = LTTNG_ERR_SESSION_FAIL;
2729 free(uris);
2730 goto error;
2731 }
2732
2733 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
2734 nb_uri, uris);
2735 if (ret != LTTNG_OK) {
2736 free(uris);
2737 goto error;
2738 }
2739
2740 /*
2741 * XXX: 0 means that this URI should be applied on the session. Should
2742 * be a DOMAIN enuam.
2743 */
2744 if (cmd_ctx->lsm->domain.type == 0) {
2745 /* Add the URI for the UST session if a consumer is present. */
2746 if (cmd_ctx->session->ust_session &&
2747 cmd_ctx->session->ust_session->consumer) {
2748 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
2749 nb_uri, uris);
2750 } else if (cmd_ctx->session->kernel_session &&
2751 cmd_ctx->session->kernel_session->consumer) {
2752 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
2753 cmd_ctx->session, nb_uri, uris);
2754 }
2755 }
2756
2757 free(uris);
2758
2759 break;
2760 }
2761 case LTTNG_START_TRACE:
2762 {
2763 ret = cmd_start_trace(cmd_ctx->session);
2764 break;
2765 }
2766 case LTTNG_STOP_TRACE:
2767 {
2768 ret = cmd_stop_trace(cmd_ctx->session);
2769 break;
2770 }
2771 case LTTNG_CREATE_SESSION:
2772 {
2773 size_t nb_uri, len;
2774 struct lttng_uri *uris = NULL;
2775
2776 nb_uri = cmd_ctx->lsm->u.uri.size;
2777 len = nb_uri * sizeof(struct lttng_uri);
2778
2779 if (nb_uri > 0) {
2780 uris = zmalloc(len);
2781 if (uris == NULL) {
2782 ret = LTTNG_ERR_FATAL;
2783 goto error;
2784 }
2785
2786 /* Receive variable len data */
2787 DBG("Waiting for %zu URIs from client ...", nb_uri);
2788 ret = lttcomm_recv_unix_sock(sock, uris, len);
2789 if (ret <= 0) {
2790 DBG("No URIs received from client... continuing");
2791 *sock_error = 1;
2792 ret = LTTNG_ERR_SESSION_FAIL;
2793 free(uris);
2794 goto error;
2795 }
2796
2797 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
2798 DBG("Creating session with ONE network URI is a bad call");
2799 ret = LTTNG_ERR_SESSION_FAIL;
2800 free(uris);
2801 goto error;
2802 }
2803 }
2804
2805 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
2806 &cmd_ctx->creds);
2807
2808 free(uris);
2809
2810 break;
2811 }
2812 case LTTNG_DESTROY_SESSION:
2813 {
2814 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
2815
2816 /* Set session to NULL so we do not unlock it after free. */
2817 cmd_ctx->session = NULL;
2818 break;
2819 }
2820 case LTTNG_LIST_DOMAINS:
2821 {
2822 ssize_t nb_dom;
2823 struct lttng_domain *domains;
2824
2825 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
2826 if (nb_dom < 0) {
2827 /* Return value is a negative lttng_error_code. */
2828 ret = -nb_dom;
2829 goto error;
2830 }
2831
2832 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
2833 if (ret < 0) {
2834 goto setup_error;
2835 }
2836
2837 /* Copy event list into message payload */
2838 memcpy(cmd_ctx->llm->payload, domains,
2839 nb_dom * sizeof(struct lttng_domain));
2840
2841 free(domains);
2842
2843 ret = LTTNG_OK;
2844 break;
2845 }
2846 case LTTNG_LIST_CHANNELS:
2847 {
2848 int nb_chan;
2849 struct lttng_channel *channels;
2850
2851 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
2852 cmd_ctx->session, &channels);
2853 if (nb_chan < 0) {
2854 /* Return value is a negative lttng_error_code. */
2855 ret = -nb_chan;
2856 goto error;
2857 }
2858
2859 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
2860 if (ret < 0) {
2861 goto setup_error;
2862 }
2863
2864 /* Copy event list into message payload */
2865 memcpy(cmd_ctx->llm->payload, channels,
2866 nb_chan * sizeof(struct lttng_channel));
2867
2868 free(channels);
2869
2870 ret = LTTNG_OK;
2871 break;
2872 }
2873 case LTTNG_LIST_EVENTS:
2874 {
2875 ssize_t nb_event;
2876 struct lttng_event *events = NULL;
2877
2878 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
2879 cmd_ctx->lsm->u.list.channel_name, &events);
2880 if (nb_event < 0) {
2881 /* Return value is a negative lttng_error_code. */
2882 ret = -nb_event;
2883 goto error;
2884 }
2885
2886 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
2887 if (ret < 0) {
2888 goto setup_error;
2889 }
2890
2891 /* Copy event list into message payload */
2892 memcpy(cmd_ctx->llm->payload, events,
2893 nb_event * sizeof(struct lttng_event));
2894
2895 free(events);
2896
2897 ret = LTTNG_OK;
2898 break;
2899 }
2900 case LTTNG_LIST_SESSIONS:
2901 {
2902 unsigned int nr_sessions;
2903
2904 session_lock_list();
2905 nr_sessions = lttng_sessions_count(
2906 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2907 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
2908
2909 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
2910 if (ret < 0) {
2911 session_unlock_list();
2912 goto setup_error;
2913 }
2914
2915 /* Filled the session array */
2916 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
2917 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2918 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
2919
2920 session_unlock_list();
2921
2922 ret = LTTNG_OK;
2923 break;
2924 }
2925 case LTTNG_CALIBRATE:
2926 {
2927 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
2928 &cmd_ctx->lsm->u.calibrate);
2929 break;
2930 }
2931 case LTTNG_REGISTER_CONSUMER:
2932 {
2933 struct consumer_data *cdata;
2934
2935 switch (cmd_ctx->lsm->domain.type) {
2936 case LTTNG_DOMAIN_KERNEL:
2937 cdata = &kconsumer_data;
2938 break;
2939 default:
2940 ret = LTTNG_ERR_UND;
2941 goto error;
2942 }
2943
2944 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2945 cmd_ctx->lsm->u.reg.path, cdata);
2946 break;
2947 }
2948 case LTTNG_ENABLE_EVENT_WITH_FILTER:
2949 {
2950 struct lttng_filter_bytecode *bytecode;
2951
2952 if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) {
2953 ret = LTTNG_ERR_FILTER_INVAL;
2954 goto error;
2955 }
2956 if (cmd_ctx->lsm->u.enable.bytecode_len == 0) {
2957 ret = LTTNG_ERR_FILTER_INVAL;
2958 goto error;
2959 }
2960 bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len);
2961 if (!bytecode) {
2962 ret = LTTNG_ERR_FILTER_NOMEM;
2963 goto error;
2964 }
2965 /* Receive var. len. data */
2966 DBG("Receiving var len data from client ...");
2967 ret = lttcomm_recv_unix_sock(sock, bytecode,
2968 cmd_ctx->lsm->u.enable.bytecode_len);
2969 if (ret <= 0) {
2970 DBG("Nothing recv() from client var len data... continuing");
2971 *sock_error = 1;
2972 ret = LTTNG_ERR_FILTER_INVAL;
2973 goto error;
2974 }
2975
2976 if (bytecode->len + sizeof(*bytecode)
2977 != cmd_ctx->lsm->u.enable.bytecode_len) {
2978 free(bytecode);
2979 ret = LTTNG_ERR_FILTER_INVAL;
2980 goto error;
2981 }
2982
2983 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
2984 cmd_ctx->lsm->u.enable.channel_name,
2985 &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]);
2986 break;
2987 }
2988 case LTTNG_DATA_PENDING:
2989 {
2990 ret = cmd_data_pending(cmd_ctx->session);
2991 break;
2992 }
2993 default:
2994 ret = LTTNG_ERR_UND;
2995 break;
2996 }
2997
2998 error:
2999 if (cmd_ctx->llm == NULL) {
3000 DBG("Missing llm structure. Allocating one.");
3001 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3002 goto setup_error;
3003 }
3004 }
3005 /* Set return code */
3006 cmd_ctx->llm->ret_code = ret;
3007 setup_error:
3008 if (cmd_ctx->session) {
3009 session_unlock(cmd_ctx->session);
3010 }
3011 if (need_tracing_session) {
3012 session_unlock_list();
3013 }
3014 init_setup_error:
3015 return ret;
3016 }
3017
3018 /*
3019 * Thread managing health check socket.
3020 */
3021 static void *thread_manage_health(void *data)
3022 {
3023 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3024 uint32_t revents, nb_fd;
3025 struct lttng_poll_event events;
3026 struct lttcomm_health_msg msg;
3027 struct lttcomm_health_data reply;
3028
3029 DBG("[thread] Manage health check started");
3030
3031 rcu_register_thread();
3032
3033 /* Create unix socket */
3034 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3035 if (sock < 0) {
3036 ERR("Unable to create health check Unix socket");
3037 ret = -1;
3038 goto error;
3039 }
3040
3041 /*
3042 * Set the CLOEXEC flag. Return code is useless because either way, the
3043 * show must go on.
3044 */
3045 (void) utils_set_fd_cloexec(sock);
3046
3047 ret = lttcomm_listen_unix_sock(sock);
3048 if (ret < 0) {
3049 goto error;
3050 }
3051
3052 /*
3053 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3054 * more will be added to this poll set.
3055 */
3056 ret = sessiond_set_thread_pollset(&events, 2);
3057 if (ret < 0) {
3058 goto error;
3059 }
3060
3061 /* Add the application registration socket */
3062 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3063 if (ret < 0) {
3064 goto error;
3065 }
3066
3067 while (1) {
3068 DBG("Health check ready");
3069
3070 /* Inifinite blocking call, waiting for transmission */
3071 restart:
3072 ret = lttng_poll_wait(&events, -1);
3073 if (ret < 0) {
3074 /*
3075 * Restart interrupted system call.
3076 */
3077 if (errno == EINTR) {
3078 goto restart;
3079 }
3080 goto error;
3081 }
3082
3083 nb_fd = ret;
3084
3085 for (i = 0; i < nb_fd; i++) {
3086 /* Fetch once the poll data */
3087 revents = LTTNG_POLL_GETEV(&events, i);
3088 pollfd = LTTNG_POLL_GETFD(&events, i);
3089
3090 /* Thread quit pipe has been closed. Killing thread. */
3091 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3092 if (ret) {
3093 err = 0;
3094 goto exit;
3095 }
3096
3097 /* Event on the registration socket */
3098 if (pollfd == sock) {
3099 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3100 ERR("Health socket poll error");
3101 goto error;
3102 }
3103 }
3104 }
3105
3106 new_sock = lttcomm_accept_unix_sock(sock);
3107 if (new_sock < 0) {
3108 goto error;
3109 }
3110
3111 /*
3112 * Set the CLOEXEC flag. Return code is useless because either way, the
3113 * show must go on.
3114 */
3115 (void) utils_set_fd_cloexec(new_sock);
3116
3117 DBG("Receiving data from client for health...");
3118 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3119 if (ret <= 0) {
3120 DBG("Nothing recv() from client... continuing");
3121 ret = close(new_sock);
3122 if (ret) {
3123 PERROR("close");
3124 }
3125 new_sock = -1;
3126 continue;
3127 }
3128
3129 rcu_thread_online();
3130
3131 switch (msg.component) {
3132 case LTTNG_HEALTH_CMD:
3133 reply.ret_code = health_check_state(HEALTH_TYPE_CMD);
3134 break;
3135 case LTTNG_HEALTH_APP_MANAGE:
3136 reply.ret_code = health_check_state(HEALTH_TYPE_APP_MANAGE);
3137 break;
3138 case LTTNG_HEALTH_APP_REG:
3139 reply.ret_code = health_check_state(HEALTH_TYPE_APP_REG);
3140 break;
3141 case LTTNG_HEALTH_KERNEL:
3142 reply.ret_code = health_check_state(HEALTH_TYPE_KERNEL);
3143 break;
3144 case LTTNG_HEALTH_CONSUMER:
3145 reply.ret_code = check_consumer_health();
3146 break;
3147 case LTTNG_HEALTH_ALL:
3148 reply.ret_code =
3149 health_check_state(HEALTH_TYPE_APP_MANAGE) &&
3150 health_check_state(HEALTH_TYPE_APP_REG) &&
3151 health_check_state(HEALTH_TYPE_CMD) &&
3152 health_check_state(HEALTH_TYPE_KERNEL) &&
3153 check_consumer_health();
3154 break;
3155 default:
3156 reply.ret_code = LTTNG_ERR_UND;
3157 break;
3158 }
3159
3160 /*
3161 * Flip ret value since 0 is a success and 1 indicates a bad health for
3162 * the client where in the sessiond it is the opposite. Again, this is
3163 * just to make things easier for us poor developer which enjoy a lot
3164 * lazyness.
3165 */
3166 if (reply.ret_code == 0 || reply.ret_code == 1) {
3167 reply.ret_code = !reply.ret_code;
3168 }
3169
3170 DBG2("Health check return value %d", reply.ret_code);
3171
3172 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3173 if (ret < 0) {
3174 ERR("Failed to send health data back to client");
3175 }
3176
3177 /* End of transmission */
3178 ret = close(new_sock);
3179 if (ret) {
3180 PERROR("close");
3181 }
3182 new_sock = -1;
3183 }
3184
3185 exit:
3186 error:
3187 if (err) {
3188 ERR("Health error occurred in %s", __func__);
3189 }
3190 DBG("Health check thread dying");
3191 unlink(health_unix_sock_path);
3192 if (sock >= 0) {
3193 ret = close(sock);
3194 if (ret) {
3195 PERROR("close");
3196 }
3197 }
3198 if (new_sock >= 0) {
3199 ret = close(new_sock);
3200 if (ret) {
3201 PERROR("close");
3202 }
3203 }
3204
3205 lttng_poll_clean(&events);
3206
3207 rcu_unregister_thread();
3208 return NULL;
3209 }
3210
3211 /*
3212 * This thread manage all clients request using the unix client socket for
3213 * communication.
3214 */
3215 static void *thread_manage_clients(void *data)
3216 {
3217 int sock = -1, ret, i, pollfd, err = -1;
3218 int sock_error;
3219 uint32_t revents, nb_fd;
3220 struct command_ctx *cmd_ctx = NULL;
3221 struct lttng_poll_event events;
3222
3223 DBG("[thread] Manage client started");
3224
3225 rcu_register_thread();
3226
3227 health_register(HEALTH_TYPE_CMD);
3228
3229 if (testpoint(thread_manage_clients)) {
3230 goto error_testpoint;
3231 }
3232
3233 health_code_update();
3234
3235 ret = lttcomm_listen_unix_sock(client_sock);
3236 if (ret < 0) {
3237 goto error_listen;
3238 }
3239
3240 /*
3241 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3242 * more will be added to this poll set.
3243 */
3244 ret = sessiond_set_thread_pollset(&events, 2);
3245 if (ret < 0) {
3246 goto error_create_poll;
3247 }
3248
3249 /* Add the application registration socket */
3250 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3251 if (ret < 0) {
3252 goto error;
3253 }
3254
3255 /*
3256 * Notify parent pid that we are ready to accept command for client side.
3257 */
3258 if (opt_sig_parent) {
3259 kill(ppid, SIGUSR1);
3260 }
3261
3262 if (testpoint(thread_manage_clients_before_loop)) {
3263 goto error;
3264 }
3265
3266 health_code_update();
3267
3268 while (1) {
3269 DBG("Accepting client command ...");
3270
3271 /* Inifinite blocking call, waiting for transmission */
3272 restart:
3273 health_poll_entry();
3274 ret = lttng_poll_wait(&events, -1);
3275 health_poll_exit();
3276 if (ret < 0) {
3277 /*
3278 * Restart interrupted system call.
3279 */
3280 if (errno == EINTR) {
3281 goto restart;
3282 }
3283 goto error;
3284 }
3285
3286 nb_fd = ret;
3287
3288 for (i = 0; i < nb_fd; i++) {
3289 /* Fetch once the poll data */
3290 revents = LTTNG_POLL_GETEV(&events, i);
3291 pollfd = LTTNG_POLL_GETFD(&events, i);
3292
3293 health_code_update();
3294
3295 /* Thread quit pipe has been closed. Killing thread. */
3296 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3297 if (ret) {
3298 err = 0;
3299 goto exit;
3300 }
3301
3302 /* Event on the registration socket */
3303 if (pollfd == client_sock) {
3304 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3305 ERR("Client socket poll error");
3306 goto error;
3307 }
3308 }
3309 }
3310
3311 DBG("Wait for client response");
3312
3313 health_code_update();
3314
3315 sock = lttcomm_accept_unix_sock(client_sock);
3316 if (sock < 0) {
3317 goto error;
3318 }
3319
3320 /*
3321 * Set the CLOEXEC flag. Return code is useless because either way, the
3322 * show must go on.
3323 */
3324 (void) utils_set_fd_cloexec(sock);
3325
3326 /* Set socket option for credentials retrieval */
3327 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3328 if (ret < 0) {
3329 goto error;
3330 }
3331
3332 /* Allocate context command to process the client request */
3333 cmd_ctx = zmalloc(sizeof(struct command_ctx));
3334 if (cmd_ctx == NULL) {
3335 PERROR("zmalloc cmd_ctx");
3336 goto error;
3337 }
3338
3339 /* Allocate data buffer for reception */
3340 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
3341 if (cmd_ctx->lsm == NULL) {
3342 PERROR("zmalloc cmd_ctx->lsm");
3343 goto error;
3344 }
3345
3346 cmd_ctx->llm = NULL;
3347 cmd_ctx->session = NULL;
3348
3349 health_code_update();
3350
3351 /*
3352 * Data is received from the lttng client. The struct
3353 * lttcomm_session_msg (lsm) contains the command and data request of
3354 * the client.
3355 */
3356 DBG("Receiving data from client ...");
3357 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3358 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
3359 if (ret <= 0) {
3360 DBG("Nothing recv() from client... continuing");
3361 ret = close(sock);
3362 if (ret) {
3363 PERROR("close");
3364 }
3365 sock = -1;
3366 clean_command_ctx(&cmd_ctx);
3367 continue;
3368 }
3369
3370 health_code_update();
3371
3372 // TODO: Validate cmd_ctx including sanity check for
3373 // security purpose.
3374
3375 rcu_thread_online();
3376 /*
3377 * This function dispatch the work to the kernel or userspace tracer
3378 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3379 * informations for the client. The command context struct contains
3380 * everything this function may needs.
3381 */
3382 ret = process_client_msg(cmd_ctx, sock, &sock_error);
3383 rcu_thread_offline();
3384 if (ret < 0) {
3385 if (sock_error) {
3386 ret = close(sock);
3387 if (ret) {
3388 PERROR("close");
3389 }
3390 sock = -1;
3391 }
3392 /*
3393 * TODO: Inform client somehow of the fatal error. At
3394 * this point, ret < 0 means that a zmalloc failed
3395 * (ENOMEM). Error detected but still accept
3396 * command, unless a socket error has been
3397 * detected.
3398 */
3399 clean_command_ctx(&cmd_ctx);
3400 continue;
3401 }
3402
3403 health_code_update();
3404
3405 DBG("Sending response (size: %d, retcode: %s)",
3406 cmd_ctx->lttng_msg_size,
3407 lttng_strerror(-cmd_ctx->llm->ret_code));
3408 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
3409 if (ret < 0) {
3410 ERR("Failed to send data back to client");
3411 }
3412
3413 /* End of transmission */
3414 ret = close(sock);
3415 if (ret) {
3416 PERROR("close");
3417 }
3418 sock = -1;
3419
3420 clean_command_ctx(&cmd_ctx);
3421
3422 health_code_update();
3423 }
3424
3425 exit:
3426 error:
3427 if (sock >= 0) {
3428 ret = close(sock);
3429 if (ret) {
3430 PERROR("close");
3431 }
3432 }
3433
3434 lttng_poll_clean(&events);
3435 clean_command_ctx(&cmd_ctx);
3436
3437 error_listen:
3438 error_create_poll:
3439 error_testpoint:
3440 unlink(client_unix_sock_path);
3441 if (client_sock >= 0) {
3442 ret = close(client_sock);
3443 if (ret) {
3444 PERROR("close");
3445 }
3446 }
3447
3448 if (err) {
3449 health_error();
3450 ERR("Health error occurred in %s", __func__);
3451 }
3452
3453 health_unregister();
3454
3455 DBG("Client thread dying");
3456
3457 rcu_unregister_thread();
3458 return NULL;
3459 }
3460
3461
3462 /*
3463 * usage function on stderr
3464 */
3465 static void usage(void)
3466 {
3467 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
3468 fprintf(stderr, " -h, --help Display this usage.\n");
3469 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
3470 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
3471 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
3472 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
3473 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
3474 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
3475 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
3476 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
3477 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
3478 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
3479 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
3480 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
3481 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
3482 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
3483 fprintf(stderr, " -V, --version Show version number.\n");
3484 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
3485 fprintf(stderr, " -q, --quiet No output at all.\n");
3486 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3487 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3488 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
3489 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
3490 }
3491
3492 /*
3493 * daemon argument parsing
3494 */
3495 static int parse_args(int argc, char **argv)
3496 {
3497 int c;
3498
3499 static struct option long_options[] = {
3500 { "client-sock", 1, 0, 'c' },
3501 { "apps-sock", 1, 0, 'a' },
3502 { "kconsumerd-cmd-sock", 1, 0, 'C' },
3503 { "kconsumerd-err-sock", 1, 0, 'E' },
3504 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
3505 { "ustconsumerd32-err-sock", 1, 0, 'H' },
3506 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
3507 { "ustconsumerd64-err-sock", 1, 0, 'F' },
3508 { "consumerd32-path", 1, 0, 'u' },
3509 { "consumerd32-libdir", 1, 0, 'U' },
3510 { "consumerd64-path", 1, 0, 't' },
3511 { "consumerd64-libdir", 1, 0, 'T' },
3512 { "daemonize", 0, 0, 'd' },
3513 { "sig-parent", 0, 0, 'S' },
3514 { "help", 0, 0, 'h' },
3515 { "group", 1, 0, 'g' },
3516 { "version", 0, 0, 'V' },
3517 { "quiet", 0, 0, 'q' },
3518 { "verbose", 0, 0, 'v' },
3519 { "verbose-consumer", 0, 0, 'Z' },
3520 { "no-kernel", 0, 0, 'N' },
3521 { "pidfile", 1, 0, 'p' },
3522 { NULL, 0, 0, 0 }
3523 };
3524
3525 while (1) {
3526 int option_index = 0;
3527 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:",
3528 long_options, &option_index);
3529 if (c == -1) {
3530 break;
3531 }
3532
3533 switch (c) {
3534 case 0:
3535 fprintf(stderr, "option %s", long_options[option_index].name);
3536 if (optarg) {
3537 fprintf(stderr, " with arg %s\n", optarg);
3538 }
3539 break;
3540 case 'c':
3541 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
3542 break;
3543 case 'a':
3544 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
3545 break;
3546 case 'd':
3547 opt_daemon = 1;
3548 break;
3549 case 'g':
3550 opt_tracing_group = optarg;
3551 break;
3552 case 'h':
3553 usage();
3554 exit(EXIT_FAILURE);
3555 case 'V':
3556 fprintf(stdout, "%s\n", VERSION);
3557 exit(EXIT_SUCCESS);
3558 case 'S':
3559 opt_sig_parent = 1;
3560 break;
3561 case 'E':
3562 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3563 break;
3564 case 'C':
3565 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3566 break;
3567 case 'F':
3568 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3569 break;
3570 case 'D':
3571 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3572 break;
3573 case 'H':
3574 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3575 break;
3576 case 'G':
3577 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
3578 break;
3579 case 'N':
3580 opt_no_kernel = 1;
3581 break;
3582 case 'q':
3583 lttng_opt_quiet = 1;
3584 break;
3585 case 'v':
3586 /* Verbose level can increase using multiple -v */
3587 lttng_opt_verbose += 1;
3588 break;
3589 case 'Z':
3590 opt_verbose_consumer += 1;
3591 break;
3592 case 'u':
3593 consumerd32_bin= optarg;
3594 break;
3595 case 'U':
3596 consumerd32_libdir = optarg;
3597 break;
3598 case 't':
3599 consumerd64_bin = optarg;
3600 break;
3601 case 'T':
3602 consumerd64_libdir = optarg;
3603 break;
3604 case 'p':
3605 opt_pidfile = optarg;
3606 break;
3607 default:
3608 /* Unknown option or other error.
3609 * Error is printed by getopt, just return */
3610 return -1;
3611 }
3612 }
3613
3614 return 0;
3615 }
3616
3617 /*
3618 * Creates the two needed socket by the daemon.
3619 * apps_sock - The communication socket for all UST apps.
3620 * client_sock - The communication of the cli tool (lttng).
3621 */
3622 static int init_daemon_socket(void)
3623 {
3624 int ret = 0;
3625 mode_t old_umask;
3626
3627 old_umask = umask(0);
3628
3629 /* Create client tool unix socket */
3630 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
3631 if (client_sock < 0) {
3632 ERR("Create unix sock failed: %s", client_unix_sock_path);
3633 ret = -1;
3634 goto end;
3635 }
3636
3637 /* Set the cloexec flag */
3638 ret = utils_set_fd_cloexec(client_sock);
3639 if (ret < 0) {
3640 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
3641 "Continuing but note that the consumer daemon will have a "
3642 "reference to this socket on exec()", client_sock);
3643 }
3644
3645 /* File permission MUST be 660 */
3646 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3647 if (ret < 0) {
3648 ERR("Set file permissions failed: %s", client_unix_sock_path);
3649 PERROR("chmod");
3650 goto end;
3651 }
3652
3653 /* Create the application unix socket */
3654 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
3655 if (apps_sock < 0) {
3656 ERR("Create unix sock failed: %s", apps_unix_sock_path);
3657 ret = -1;
3658 goto end;
3659 }
3660
3661 /* Set the cloexec flag */
3662 ret = utils_set_fd_cloexec(apps_sock);
3663 if (ret < 0) {
3664 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
3665 "Continuing but note that the consumer daemon will have a "
3666 "reference to this socket on exec()", apps_sock);
3667 }
3668
3669 /* File permission MUST be 666 */
3670 ret = chmod(apps_unix_sock_path,
3671 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
3672 if (ret < 0) {
3673 ERR("Set file permissions failed: %s", apps_unix_sock_path);
3674 PERROR("chmod");
3675 goto end;
3676 }
3677
3678 DBG3("Session daemon client socket %d and application socket %d created",
3679 client_sock, apps_sock);
3680
3681 end:
3682 umask(old_umask);
3683 return ret;
3684 }
3685
3686 /*
3687 * Check if the global socket is available, and if a daemon is answering at the
3688 * other side. If yes, error is returned.
3689 */
3690 static int check_existing_daemon(void)
3691 {
3692 /* Is there anybody out there ? */
3693 if (lttng_session_daemon_alive()) {
3694 return -EEXIST;
3695 }
3696
3697 return 0;
3698 }
3699
3700 /*
3701 * Set the tracing group gid onto the client socket.
3702 *
3703 * Race window between mkdir and chown is OK because we are going from more
3704 * permissive (root.root) to less permissive (root.tracing).
3705 */
3706 static int set_permissions(char *rundir)
3707 {
3708 int ret;
3709 gid_t gid;
3710
3711 ret = allowed_group();
3712 if (ret < 0) {
3713 WARN("No tracing group detected");
3714 ret = 0;
3715 goto end;
3716 }
3717
3718 gid = ret;
3719
3720 /* Set lttng run dir */
3721 ret = chown(rundir, 0, gid);
3722 if (ret < 0) {
3723 ERR("Unable to set group on %s", rundir);
3724 PERROR("chown");
3725 }
3726
3727 /* Ensure tracing group can search the run dir */
3728 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
3729 if (ret < 0) {
3730 ERR("Unable to set permissions on %s", rundir);
3731 PERROR("chmod");
3732 }
3733
3734 /* lttng client socket path */
3735 ret = chown(client_unix_sock_path, 0, gid);
3736 if (ret < 0) {
3737 ERR("Unable to set group on %s", client_unix_sock_path);
3738 PERROR("chown");
3739 }
3740
3741 /* kconsumer error socket path */
3742 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
3743 if (ret < 0) {
3744 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
3745 PERROR("chown");
3746 }
3747
3748 /* 64-bit ustconsumer error socket path */
3749 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
3750 if (ret < 0) {
3751 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
3752 PERROR("chown");
3753 }
3754
3755 /* 32-bit ustconsumer compat32 error socket path */
3756 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3757 if (ret < 0) {
3758 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
3759 PERROR("chown");
3760 }
3761
3762 DBG("All permissions are set");
3763
3764 end:
3765 return ret;
3766 }
3767
3768 /*
3769 * Create the lttng run directory needed for all global sockets and pipe.
3770 */
3771 static int create_lttng_rundir(const char *rundir)
3772 {
3773 int ret;
3774
3775 DBG3("Creating LTTng run directory: %s", rundir);
3776
3777 ret = mkdir(rundir, S_IRWXU);
3778 if (ret < 0) {
3779 if (errno != EEXIST) {
3780 ERR("Unable to create %s", rundir);
3781 goto error;
3782 } else {
3783 ret = 0;
3784 }
3785 }
3786
3787 error:
3788 return ret;
3789 }
3790
3791 /*
3792 * Setup sockets and directory needed by the kconsumerd communication with the
3793 * session daemon.
3794 */
3795 static int set_consumer_sockets(struct consumer_data *consumer_data,
3796 const char *rundir)
3797 {
3798 int ret;
3799 char path[PATH_MAX];
3800
3801 switch (consumer_data->type) {
3802 case LTTNG_CONSUMER_KERNEL:
3803 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
3804 break;
3805 case LTTNG_CONSUMER64_UST:
3806 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
3807 break;
3808 case LTTNG_CONSUMER32_UST:
3809 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
3810 break;
3811 default:
3812 ERR("Consumer type unknown");
3813 ret = -EINVAL;
3814 goto error;
3815 }
3816
3817 DBG2("Creating consumer directory: %s", path);
3818
3819 ret = mkdir(path, S_IRWXU);
3820 if (ret < 0) {
3821 if (errno != EEXIST) {
3822 PERROR("mkdir");
3823 ERR("Failed to create %s", path);
3824 goto error;
3825 }
3826 ret = -1;
3827 }
3828
3829 /* Create the kconsumerd error unix socket */
3830 consumer_data->err_sock =
3831 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
3832 if (consumer_data->err_sock < 0) {
3833 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
3834 ret = -1;
3835 goto error;
3836 }
3837
3838 /* File permission MUST be 660 */
3839 ret = chmod(consumer_data->err_unix_sock_path,
3840 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3841 if (ret < 0) {
3842 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
3843 PERROR("chmod");
3844 goto error;
3845 }
3846
3847 error:
3848 return ret;
3849 }
3850
3851 /*
3852 * Signal handler for the daemon
3853 *
3854 * Simply stop all worker threads, leaving main() return gracefully after
3855 * joining all threads and calling cleanup().
3856 */
3857 static void sighandler(int sig)
3858 {
3859 switch (sig) {
3860 case SIGPIPE:
3861 DBG("SIGPIPE caught");
3862 return;
3863 case SIGINT:
3864 DBG("SIGINT caught");
3865 stop_threads();
3866 break;
3867 case SIGTERM:
3868 DBG("SIGTERM caught");
3869 stop_threads();
3870 break;
3871 default:
3872 break;
3873 }
3874 }
3875
3876 /*
3877 * Setup signal handler for :
3878 * SIGINT, SIGTERM, SIGPIPE
3879 */
3880 static int set_signal_handler(void)
3881 {
3882 int ret = 0;
3883 struct sigaction sa;
3884 sigset_t sigset;
3885
3886 if ((ret = sigemptyset(&sigset)) < 0) {
3887 PERROR("sigemptyset");
3888 return ret;
3889 }
3890
3891 sa.sa_handler = sighandler;
3892 sa.sa_mask = sigset;
3893 sa.sa_flags = 0;
3894 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
3895 PERROR("sigaction");
3896 return ret;
3897 }
3898
3899 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
3900 PERROR("sigaction");
3901 return ret;
3902 }
3903
3904 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
3905 PERROR("sigaction");
3906 return ret;
3907 }
3908
3909 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3910
3911 return ret;
3912 }
3913
3914 /*
3915 * Set open files limit to unlimited. This daemon can open a large number of
3916 * file descriptors in order to consumer multiple kernel traces.
3917 */
3918 static void set_ulimit(void)
3919 {
3920 int ret;
3921 struct rlimit lim;
3922
3923 /* The kernel does not allowed an infinite limit for open files */
3924 lim.rlim_cur = 65535;
3925 lim.rlim_max = 65535;
3926
3927 ret = setrlimit(RLIMIT_NOFILE, &lim);
3928 if (ret < 0) {
3929 PERROR("failed to set open files limit");
3930 }
3931 }
3932
3933 /*
3934 * Write pidfile using the rundir and opt_pidfile.
3935 */
3936 static void write_pidfile(void)
3937 {
3938 int ret;
3939 char pidfile_path[PATH_MAX];
3940
3941 assert(rundir);
3942
3943 if (opt_pidfile) {
3944 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
3945 } else {
3946 /* Build pidfile path from rundir and opt_pidfile. */
3947 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
3948 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
3949 if (ret < 0) {
3950 PERROR("snprintf pidfile path");
3951 goto error;
3952 }
3953 }
3954
3955 /*
3956 * Create pid file in rundir. Return value is of no importance. The
3957 * execution will continue even though we are not able to write the file.
3958 */
3959 (void) utils_create_pid_file(getpid(), pidfile_path);
3960
3961 error:
3962 return;
3963 }
3964
3965 /*
3966 * main
3967 */
3968 int main(int argc, char **argv)
3969 {
3970 int ret = 0;
3971 void *status;
3972 const char *home_path, *env_app_timeout;
3973
3974 init_kernel_workarounds();
3975
3976 rcu_register_thread();
3977
3978 setup_consumerd_path();
3979
3980 /* Parse arguments */
3981 progname = argv[0];
3982 if ((ret = parse_args(argc, argv)) < 0) {
3983 goto error;
3984 }
3985
3986 /* Daemonize */
3987 if (opt_daemon) {
3988 int i;
3989
3990 /*
3991 * fork
3992 * child: setsid, close FD 0, 1, 2, chdir /
3993 * parent: exit (if fork is successful)
3994 */
3995 ret = daemon(0, 0);
3996 if (ret < 0) {
3997 PERROR("daemon");
3998 goto error;
3999 }
4000 /*
4001 * We are in the child. Make sure all other file
4002 * descriptors are closed, in case we are called with
4003 * more opened file descriptors than the standard ones.
4004 */
4005 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4006 (void) close(i);
4007 }
4008 }
4009
4010 /* Create thread quit pipe */
4011 if ((ret = init_thread_quit_pipe()) < 0) {
4012 goto error;
4013 }
4014
4015 /* Check if daemon is UID = 0 */
4016 is_root = !getuid();
4017
4018 if (is_root) {
4019 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4020
4021 /* Create global run dir with root access */
4022 ret = create_lttng_rundir(rundir);
4023 if (ret < 0) {
4024 goto error;
4025 }
4026
4027 if (strlen(apps_unix_sock_path) == 0) {
4028 snprintf(apps_unix_sock_path, PATH_MAX,
4029 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4030 }
4031
4032 if (strlen(client_unix_sock_path) == 0) {
4033 snprintf(client_unix_sock_path, PATH_MAX,
4034 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4035 }
4036
4037 /* Set global SHM for ust */
4038 if (strlen(wait_shm_path) == 0) {
4039 snprintf(wait_shm_path, PATH_MAX,
4040 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4041 }
4042
4043 if (strlen(health_unix_sock_path) == 0) {
4044 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4045 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4046 }
4047
4048 /* Setup kernel consumerd path */
4049 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
4050 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4051 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
4052 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4053
4054 DBG2("Kernel consumer err path: %s",
4055 kconsumer_data.err_unix_sock_path);
4056 DBG2("Kernel consumer cmd path: %s",
4057 kconsumer_data.cmd_unix_sock_path);
4058 } else {
4059 home_path = get_home_dir();
4060 if (home_path == NULL) {
4061 /* TODO: Add --socket PATH option */
4062 ERR("Can't get HOME directory for sockets creation.");
4063 ret = -EPERM;
4064 goto error;
4065 }
4066
4067 /*
4068 * Create rundir from home path. This will create something like
4069 * $HOME/.lttng
4070 */
4071 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
4072 if (ret < 0) {
4073 ret = -ENOMEM;
4074 goto error;
4075 }
4076
4077 ret = create_lttng_rundir(rundir);
4078 if (ret < 0) {
4079 goto error;
4080 }
4081
4082 if (strlen(apps_unix_sock_path) == 0) {
4083 snprintf(apps_unix_sock_path, PATH_MAX,
4084 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
4085 }
4086
4087 /* Set the cli tool unix socket path */
4088 if (strlen(client_unix_sock_path) == 0) {
4089 snprintf(client_unix_sock_path, PATH_MAX,
4090 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
4091 }
4092
4093 /* Set global SHM for ust */
4094 if (strlen(wait_shm_path) == 0) {
4095 snprintf(wait_shm_path, PATH_MAX,
4096 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
4097 }
4098
4099 /* Set health check Unix path */
4100 if (strlen(health_unix_sock_path) == 0) {
4101 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4102 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4103 }
4104 }
4105
4106 /* Set consumer initial state */
4107 kernel_consumerd_state = CONSUMER_STOPPED;
4108 ust_consumerd_state = CONSUMER_STOPPED;
4109
4110 DBG("Client socket path %s", client_unix_sock_path);
4111 DBG("Application socket path %s", apps_unix_sock_path);
4112 DBG("Application wait path %s", wait_shm_path);
4113 DBG("LTTng run directory path: %s", rundir);
4114
4115 /* 32 bits consumerd path setup */
4116 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
4117 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4118 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
4119 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4120
4121 DBG2("UST consumer 32 bits err path: %s",
4122 ustconsumer32_data.err_unix_sock_path);
4123 DBG2("UST consumer 32 bits cmd path: %s",
4124 ustconsumer32_data.cmd_unix_sock_path);
4125
4126 /* 64 bits consumerd path setup */
4127 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
4128 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4129 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
4130 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4131
4132 DBG2("UST consumer 64 bits err path: %s",
4133 ustconsumer64_data.err_unix_sock_path);
4134 DBG2("UST consumer 64 bits cmd path: %s",
4135 ustconsumer64_data.cmd_unix_sock_path);
4136
4137 /*
4138 * See if daemon already exist.
4139 */
4140 if ((ret = check_existing_daemon()) < 0) {
4141 ERR("Already running daemon.\n");
4142 /*
4143 * We do not goto exit because we must not cleanup()
4144 * because a daemon is already running.
4145 */
4146 goto error;
4147 }
4148
4149 /*
4150 * Init UST app hash table. Alloc hash table before this point since
4151 * cleanup() can get called after that point.
4152 */
4153 ust_app_ht_alloc();
4154
4155 /* After this point, we can safely call cleanup() with "goto exit" */
4156
4157 /*
4158 * These actions must be executed as root. We do that *after* setting up
4159 * the sockets path because we MUST make the check for another daemon using
4160 * those paths *before* trying to set the kernel consumer sockets and init
4161 * kernel tracer.
4162 */
4163 if (is_root) {
4164 ret = set_consumer_sockets(&kconsumer_data, rundir);
4165 if (ret < 0) {
4166 goto exit;
4167 }
4168
4169 /* Setup kernel tracer */
4170 if (!opt_no_kernel) {
4171 init_kernel_tracer();
4172 }
4173
4174 /* Set ulimit for open files */
4175 set_ulimit();
4176 }
4177 /* init lttng_fd tracking must be done after set_ulimit. */
4178 lttng_fd_init();
4179
4180 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4181 if (ret < 0) {
4182 goto exit;
4183 }
4184
4185 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4186 if (ret < 0) {
4187 goto exit;
4188 }
4189
4190 if ((ret = set_signal_handler()) < 0) {
4191 goto exit;
4192 }
4193
4194 /* Setup the needed unix socket */
4195 if ((ret = init_daemon_socket()) < 0) {
4196 goto exit;
4197 }
4198
4199 /* Set credentials to socket */
4200 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
4201 goto exit;
4202 }
4203
4204 /* Get parent pid if -S, --sig-parent is specified. */
4205 if (opt_sig_parent) {
4206 ppid = getppid();
4207 }
4208
4209 /* Setup the kernel pipe for waking up the kernel thread */
4210 if (is_root && !opt_no_kernel) {
4211 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4212 goto exit;
4213 }
4214 }
4215
4216 /* Setup the thread apps communication pipe. */
4217 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
4218 goto exit;
4219 }
4220
4221 /* Setup the thread apps notify communication pipe. */
4222 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
4223 goto exit;
4224 }
4225
4226 /* Initialize global buffer per UID and PID registry. */
4227 buffer_reg_init_uid_registry();
4228 buffer_reg_init_pid_registry();
4229
4230 /* Init UST command queue. */
4231 cds_wfq_init(&ust_cmd_queue.queue);
4232
4233 /*
4234 * Get session list pointer. This pointer MUST NOT be free(). This list is
4235 * statically declared in session.c
4236 */
4237 session_list_ptr = session_get_list();
4238
4239 /* Set up max poll set size */
4240 lttng_poll_set_max_size();
4241
4242 cmd_init();
4243
4244 /* Check for the application socket timeout env variable. */
4245 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
4246 if (env_app_timeout) {
4247 app_socket_timeout = atoi(env_app_timeout);
4248 } else {
4249 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
4250 }
4251
4252 write_pidfile();
4253
4254 /* Create thread to manage the client socket */
4255 ret = pthread_create(&health_thread, NULL,
4256 thread_manage_health, (void *) NULL);
4257 if (ret != 0) {
4258 PERROR("pthread_create health");
4259 goto exit_health;
4260 }
4261
4262 /* Create thread to manage the client socket */
4263 ret = pthread_create(&client_thread, NULL,
4264 thread_manage_clients, (void *) NULL);
4265 if (ret != 0) {
4266 PERROR("pthread_create clients");
4267 goto exit_client;
4268 }
4269
4270 /* Create thread to dispatch registration */
4271 ret = pthread_create(&dispatch_thread, NULL,
4272 thread_dispatch_ust_registration, (void *) NULL);
4273 if (ret != 0) {
4274 PERROR("pthread_create dispatch");
4275 goto exit_dispatch;
4276 }
4277
4278 /* Create thread to manage application registration. */
4279 ret = pthread_create(&reg_apps_thread, NULL,
4280 thread_registration_apps, (void *) NULL);
4281 if (ret != 0) {
4282 PERROR("pthread_create registration");
4283 goto exit_reg_apps;
4284 }
4285
4286 /* Create thread to manage application socket */
4287 ret = pthread_create(&apps_thread, NULL,
4288 thread_manage_apps, (void *) NULL);
4289 if (ret != 0) {
4290 PERROR("pthread_create apps");
4291 goto exit_apps;
4292 }
4293
4294 /* Create thread to manage application notify socket */
4295 ret = pthread_create(&apps_notify_thread, NULL,
4296 ust_thread_manage_notify, (void *) NULL);
4297 if (ret != 0) {
4298 PERROR("pthread_create apps");
4299 goto exit_apps;
4300 }
4301
4302 /* Don't start this thread if kernel tracing is not requested nor root */
4303 if (is_root && !opt_no_kernel) {
4304 /* Create kernel thread to manage kernel event */
4305 ret = pthread_create(&kernel_thread, NULL,
4306 thread_manage_kernel, (void *) NULL);
4307 if (ret != 0) {
4308 PERROR("pthread_create kernel");
4309 goto exit_kernel;
4310 }
4311
4312 ret = pthread_join(kernel_thread, &status);
4313 if (ret != 0) {
4314 PERROR("pthread_join");
4315 goto error; /* join error, exit without cleanup */
4316 }
4317 }
4318
4319 exit_kernel:
4320 ret = pthread_join(apps_thread, &status);
4321 if (ret != 0) {
4322 PERROR("pthread_join");
4323 goto error; /* join error, exit without cleanup */
4324 }
4325
4326 exit_apps:
4327 ret = pthread_join(reg_apps_thread, &status);
4328 if (ret != 0) {
4329 PERROR("pthread_join");
4330 goto error; /* join error, exit without cleanup */
4331 }
4332
4333 exit_reg_apps:
4334 ret = pthread_join(dispatch_thread, &status);
4335 if (ret != 0) {
4336 PERROR("pthread_join");
4337 goto error; /* join error, exit without cleanup */
4338 }
4339
4340 exit_dispatch:
4341 ret = pthread_join(client_thread, &status);
4342 if (ret != 0) {
4343 PERROR("pthread_join");
4344 goto error; /* join error, exit without cleanup */
4345 }
4346
4347 ret = join_consumer_thread(&kconsumer_data);
4348 if (ret != 0) {
4349 PERROR("join_consumer");
4350 goto error; /* join error, exit without cleanup */
4351 }
4352
4353 ret = join_consumer_thread(&ustconsumer32_data);
4354 if (ret != 0) {
4355 PERROR("join_consumer ust32");
4356 goto error; /* join error, exit without cleanup */
4357 }
4358
4359 ret = join_consumer_thread(&ustconsumer64_data);
4360 if (ret != 0) {
4361 PERROR("join_consumer ust64");
4362 goto error; /* join error, exit without cleanup */
4363 }
4364
4365 exit_client:
4366 ret = pthread_join(health_thread, &status);
4367 if (ret != 0) {
4368 PERROR("pthread_join health thread");
4369 goto error; /* join error, exit without cleanup */
4370 }
4371
4372 exit_health:
4373 exit:
4374 /*
4375 * cleanup() is called when no other thread is running.
4376 */
4377 rcu_thread_online();
4378 cleanup();
4379 rcu_thread_offline();
4380 rcu_unregister_thread();
4381 if (!ret) {
4382 exit(EXIT_SUCCESS);
4383 }
4384 error:
4385 exit(EXIT_FAILURE);
4386 }
This page took 0.158618 seconds and 5 git commands to generate.