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