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