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