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