Add a save API to lttng-ctl
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
0fdd1e2c 3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
26296c48 4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
fac6795d 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
91d76f53 9 *
d14d33bf
AM
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
91d76f53 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
18 */
19
20#define _GNU_SOURCE
fac6795d
DG
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
0bb7724a 24#include <paths.h>
fac6795d
DG
25#include <pthread.h>
26#include <signal.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
331744e3 30#include <inttypes.h>
0fdd1e2c 31#include <sys/mman.h>
b73401da 32#include <sys/mount.h>
1e307fab 33#include <sys/resource.h>
fac6795d
DG
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
0fdd1e2c 37#include <sys/wait.h>
5c827ce0 38#include <urcu/uatomic.h>
fac6795d 39#include <unistd.h>
3bd1e081 40#include <config.h>
fac6795d 41
990570ed 42#include <common/common.h>
d27c42b8 43#include <common/compat/socket.h>
db758600
DG
44#include <common/defaults.h>
45#include <common/kernel-consumer/kernel-consumer.h>
50c8f484 46#include <common/futex.h>
00e2e675 47#include <common/relayd/relayd.h>
81b86775 48#include <common/utils.h>
3ccdf997 49#include <common/daemonize.h>
26296c48 50#include <common/config/config.h>
fac6795d 51
10a8a223 52#include "lttng-sessiond.h"
7972aab2 53#include "buffer-registry.h"
54d01ffb 54#include "channel.h"
2f77fc4b 55#include "cmd.h"
00e2e675 56#include "consumer.h"
099e26bd 57#include "context.h"
54d01ffb 58#include "event.h"
4771f025 59#include "kernel.h"
f1e16794 60#include "kernel-consumer.h"
096102bd 61#include "modprobe.h"
0fdd1e2c 62#include "shm.h"
1e307fab 63#include "ust-ctl.h"
00e2e675 64#include "ust-consumer.h"
8e68d1c8 65#include "utils.h"
4063050c 66#include "fd-limit.h"
8782cc74 67#include "health-sessiond.h"
8ac94142 68#include "testpoint.h"
d0b96690 69#include "ust-thread.h"
4d076222 70#include "jul-thread.h"
fac6795d 71
ebaeda94
MD
72#define CONSUMERD_FILE "lttng-consumerd"
73
fac6795d 74const char *progname;
6c71277b 75static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
26296c48
JG
76static int tracing_group_name_override;
77static char *opt_pidfile;
5b8719f5 78static int opt_sig_parent;
97e19046 79static int opt_verbose_consumer;
72dd7491 80static int opt_daemon, opt_background;
4fba7219 81static int opt_no_kernel;
1d4b027a 82static pid_t ppid; /* Parent PID for --sig-parent option */
0bb7724a 83static pid_t child_ppid; /* Internal parent PID use with daemonize. */
67e40797 84static char *rundir;
3bd1e081 85
0bb7724a
DG
86/* Set to 1 when a SIGUSR1 signal is received. */
87static int recv_child_signal;
88
a23ec3a7
DG
89/*
90 * Consumer daemon specific control data. Every value not initialized here is
91 * set to 0 by the static definition.
92 */
3bd1e081
MD
93static struct consumer_data kconsumer_data = {
94 .type = LTTNG_CONSUMER_KERNEL,
60922cb0
DG
95 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
96 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
03550b58
MD
97 .err_sock = -1,
98 .cmd_sock = -1,
173af62f
DG
99 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
100 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
101 .cond = PTHREAD_COND_INITIALIZER,
102 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 103};
7753dea8
MD
104static struct consumer_data ustconsumer64_data = {
105 .type = LTTNG_CONSUMER64_UST,
60922cb0
DG
106 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
107 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
03550b58
MD
108 .err_sock = -1,
109 .cmd_sock = -1,
173af62f
DG
110 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
111 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
112 .cond = PTHREAD_COND_INITIALIZER,
113 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
114};
115static struct consumer_data ustconsumer32_data = {
116 .type = LTTNG_CONSUMER32_UST,
60922cb0
DG
117 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
118 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
03550b58
MD
119 .err_sock = -1,
120 .cmd_sock = -1,
173af62f
DG
121 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
122 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
123 .cond = PTHREAD_COND_INITIALIZER,
124 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
125};
126
26296c48
JG
127/* Command line options */
128static const struct option long_options[] = {
129 { "client-sock", 1, 0, 'c' },
130 { "apps-sock", 1, 0, 'a' },
131 { "kconsumerd-cmd-sock", 1, 0, 'C' },
132 { "kconsumerd-err-sock", 1, 0, 'E' },
133 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
134 { "ustconsumerd32-err-sock", 1, 0, 'H' },
135 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
136 { "ustconsumerd64-err-sock", 1, 0, 'F' },
137 { "consumerd32-path", 1, 0, 'u' },
138 { "consumerd32-libdir", 1, 0, 'U' },
139 { "consumerd64-path", 1, 0, 't' },
140 { "consumerd64-libdir", 1, 0, 'T' },
141 { "daemonize", 0, 0, 'd' },
72dd7491 142 { "background", 0, 0, 'b' },
26296c48
JG
143 { "sig-parent", 0, 0, 'S' },
144 { "help", 0, 0, 'h' },
145 { "group", 1, 0, 'g' },
146 { "version", 0, 0, 'V' },
147 { "quiet", 0, 0, 'q' },
148 { "verbose", 0, 0, 'v' },
149 { "verbose-consumer", 0, 0, 'Z' },
150 { "no-kernel", 0, 0, 'N' },
151 { "pidfile", 1, 0, 'p' },
152 { "jul-tcp-port", 1, 0, 'J' },
153 { "config", 1, 0, 'f' },
154 { NULL, 0, 0, 0 }
155};
156
157/* Command line options to ignore from configuration file */
158static const char *config_ignore_options[] = { "help", "version", "config" };
159
26c9d55e 160/* Shared between threads */
099e26bd 161static int dispatch_thread_exit;
fac6795d 162
54d01ffb
DG
163/* Global application Unix socket path */
164static char apps_unix_sock_path[PATH_MAX];
165/* Global client Unix socket path */
166static char client_unix_sock_path[PATH_MAX];
54d01ffb
DG
167/* global wait shm path for UST */
168static char wait_shm_path[PATH_MAX];
44a5e5eb
DG
169/* Global health check unix path */
170static char health_unix_sock_path[PATH_MAX];
fac6795d 171
1d4b027a 172/* Sockets and FDs */
a4b35e07
MD
173static int client_sock = -1;
174static int apps_sock = -1;
2f77fc4b 175int kernel_tracer_fd = -1;
76d7553f 176static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 177
273ea72c
DG
178/*
179 * Quit pipe for all threads. This permits a single cancellation point
180 * for all threads when receiving an event on the pipe.
181 */
76d7553f 182static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 183
099e26bd
DG
184/*
185 * This pipe is used to inform the thread managing application communication
186 * that a command is queued and ready to be processed.
187 */
76d7553f 188static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 189
d0b96690
DG
190int apps_cmd_notify_pipe[2] = { -1, -1 };
191
1d4b027a 192/* Pthread, Mutexes and Semaphores */
1d4b027a 193static pthread_t apps_thread;
d0b96690 194static pthread_t apps_notify_thread;
099e26bd 195static pthread_t reg_apps_thread;
1d4b027a 196static pthread_t client_thread;
7a485870 197static pthread_t kernel_thread;
099e26bd 198static pthread_t dispatch_thread;
44a5e5eb 199static pthread_t health_thread;
0b2dc8df 200static pthread_t ht_cleanup_thread;
4d076222 201static pthread_t jul_reg_thread;
5eb91c98 202
099e26bd
DG
203/*
204 * UST registration command queue. This queue is tied with a futex and uses a N
205 * wakers / 1 waiter implemented and detailed in futex.c/.h
206 *
207 * The thread_manage_apps and thread_dispatch_ust_registration interact with
208 * this queue and the wait/wake scheme.
209 */
210static struct ust_cmd_queue ust_cmd_queue;
211
b5541356
DG
212/*
213 * Pointer initialized before thread creation.
214 *
215 * This points to the tracing session list containing the session count and a
216 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
217 * MUST NOT be taken if you call a public function in session.c.
04ea676f 218 *
d063d709 219 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 220 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
221 */
222static struct ltt_session_list *session_list_ptr;
223
7753dea8
MD
224int ust_consumerd64_fd = -1;
225int ust_consumerd32_fd = -1;
226
fb6f1fa2
YB
227static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
228static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
229static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
230static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
26296c48
JG
231static int consumerd32_bin_override;
232static int consumerd64_bin_override;
233static int consumerd32_libdir_override;
234static int consumerd64_libdir_override;
fb09408a 235
2f77fc4b
DG
236static const char *module_proc_lttng = "/proc/lttng";
237
5c827ce0
DG
238/*
239 * Consumer daemon state which is changed when spawning it, killing it or in
240 * case of a fatal error.
241 */
242enum consumerd_state {
243 CONSUMER_STARTED = 1,
244 CONSUMER_STOPPED = 2,
245 CONSUMER_ERROR = 3,
246};
247
248/*
249 * This consumer daemon state is used to validate if a client command will be
250 * able to reach the consumer. If not, the client is informed. For instance,
251 * doing a "lttng start" when the consumer state is set to ERROR will return an
252 * error to the client.
253 *
254 * The following example shows a possible race condition of this scheme:
255 *
256 * consumer thread error happens
257 * client cmd arrives
258 * client cmd checks state -> still OK
259 * consumer thread exit, sets error
260 * client cmd try to talk to consumer
261 * ...
262 *
263 * However, since the consumer is a different daemon, we have no way of making
264 * sure the command will reach it safely even with this state flag. This is why
265 * we consider that up to the state validation during command processing, the
266 * command is safe. After that, we can not guarantee the correctness of the
267 * client request vis-a-vis the consumer.
268 */
269static enum consumerd_state ust_consumerd_state;
270static enum consumerd_state kernel_consumerd_state;
271
ae9e45b3
DG
272/*
273 * Socket timeout for receiving and sending in seconds.
274 */
275static int app_socket_timeout;
276
12744796
DG
277/* Set in main() with the current page size. */
278long page_size;
279
8782cc74
MD
280/* Application health monitoring */
281struct health_app *health_sessiond;
282
4d076222
DG
283/* JUL TCP port for registration. Used by the JUL thread. */
284unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT;
285
f43f95a9
DG
286/* Am I root or not. */
287int is_root; /* Set to 1 if the daemon is running as root */
288
26296c48
JG
289const char * const config_section_name = "sessiond";
290
97bc1426
MD
291/*
292 * Whether sessiond is ready for commands/health check requests.
293 * NR_LTTNG_SESSIOND_READY must match the number of calls to
294 * lttng_sessiond_notify_ready().
295 */
296#define NR_LTTNG_SESSIOND_READY 2
297int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
298
299/* Notify parents that we are ready for cmd and health check */
300static
301void lttng_sessiond_notify_ready(void)
302{
303 if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
304 /*
305 * Notify parent pid that we are ready to accept command
306 * for client side. This ppid is the one from the
307 * external process that spawned us.
308 */
309 if (opt_sig_parent) {
310 kill(ppid, SIGUSR1);
311 }
312
313 /*
314 * Notify the parent of the fork() process that we are
315 * ready.
316 */
72dd7491 317 if (opt_daemon || opt_background) {
97bc1426
MD
318 kill(child_ppid, SIGUSR1);
319 }
320 }
321}
322
fb09408a 323static
7753dea8 324void setup_consumerd_path(void)
fb09408a 325{
fc7a59ce 326 const char *bin, *libdir;
fb09408a 327
7753dea8
MD
328 /*
329 * Allow INSTALL_BIN_PATH to be used as a target path for the
ebaeda94
MD
330 * native architecture size consumer if CONFIG_CONSUMER*_PATH
331 * has not been defined.
7753dea8 332 */
ebaeda94 333#if (CAA_BITS_PER_LONG == 32)
fc7a59ce
AM
334 if (!consumerd32_bin[0]) {
335 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
ebaeda94
MD
336 }
337 if (!consumerd32_libdir[0]) {
338 consumerd32_libdir = INSTALL_LIB_PATH;
339 }
340#elif (CAA_BITS_PER_LONG == 64)
fc7a59ce
AM
341 if (!consumerd64_bin[0]) {
342 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
7753dea8 343 }
ebaeda94
MD
344 if (!consumerd64_libdir[0]) {
345 consumerd64_libdir = INSTALL_LIB_PATH;
7753dea8
MD
346 }
347#else
348#error "Unknown bitness"
349#endif
350
fb09408a
MD
351 /*
352 * runtime env. var. overrides the build default.
353 */
fc7a59ce
AM
354 bin = getenv("LTTNG_CONSUMERD32_BIN");
355 if (bin) {
356 consumerd32_bin = bin;
7753dea8 357 }
fc7a59ce
AM
358 bin = getenv("LTTNG_CONSUMERD64_BIN");
359 if (bin) {
360 consumerd64_bin = bin;
ebaeda94 361 }
72f579ee 362 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
ebaeda94
MD
363 if (libdir) {
364 consumerd32_libdir = libdir;
365 }
72f579ee 366 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
ebaeda94
MD
367 if (libdir) {
368 consumerd64_libdir = libdir;
fb09408a
MD
369 }
370}
371
5eb91c98
DG
372/*
373 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
374 */
d0b96690 375int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
5eb91c98
DG
376{
377 int ret;
378
d0b96690 379 assert(events);
5eb91c98
DG
380
381 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
382 if (ret < 0) {
383 goto error;
384 }
385
386 /* Add quit pipe */
d0b96690 387 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
5eb91c98
DG
388 if (ret < 0) {
389 goto error;
390 }
391
392 return 0;
393
394error:
395 return ret;
396}
397
398/*
399 * Check if the thread quit pipe was triggered.
400 *
401 * Return 1 if it was triggered else 0;
402 */
d0b96690 403int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
5eb91c98
DG
404{
405 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
406 return 1;
407 }
408
409 return 0;
410}
411
273ea72c 412/*
5eb91c98 413 * Init thread quit pipe.
273ea72c
DG
414 *
415 * Return -1 on error or 0 if all pipes are created.
416 */
417static int init_thread_quit_pipe(void)
418{
730389d9 419 int ret, i;
273ea72c 420
730389d9 421 ret = pipe(thread_quit_pipe);
273ea72c 422 if (ret < 0) {
730389d9 423 PERROR("thread quit pipe");
273ea72c
DG
424 goto error;
425 }
426
730389d9
DG
427 for (i = 0; i < 2; i++) {
428 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
429 if (ret < 0) {
430 PERROR("fcntl");
431 goto error;
432 }
433 }
434
273ea72c
DG
435error:
436 return ret;
437}
438
099e26bd
DG
439/*
440 * Stop all threads by closing the thread quit pipe.
441 */
cf3af59e
MD
442static void stop_threads(void)
443{
5eb91c98
DG
444 int ret;
445
cf3af59e
MD
446 /* Stopping all threads */
447 DBG("Terminating all threads");
54d01ffb 448 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
449 if (ret < 0) {
450 ERR("write error on thread quit pipe");
451 }
452
099e26bd 453 /* Dispatch thread */
26c9d55e 454 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 455 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
456}
457
e975f9f8
DG
458/*
459 * Close every consumer sockets.
460 */
461static void close_consumer_sockets(void)
462{
463 int ret;
464
465 if (kconsumer_data.err_sock >= 0) {
466 ret = close(kconsumer_data.err_sock);
467 if (ret < 0) {
468 PERROR("kernel consumer err_sock close");
469 }
470 }
471 if (ustconsumer32_data.err_sock >= 0) {
472 ret = close(ustconsumer32_data.err_sock);
473 if (ret < 0) {
a76cbd9f 474 PERROR("UST consumerd32 err_sock close");
e975f9f8
DG
475 }
476 }
477 if (ustconsumer64_data.err_sock >= 0) {
478 ret = close(ustconsumer64_data.err_sock);
479 if (ret < 0) {
a76cbd9f 480 PERROR("UST consumerd64 err_sock close");
e975f9f8
DG
481 }
482 }
483 if (kconsumer_data.cmd_sock >= 0) {
484 ret = close(kconsumer_data.cmd_sock);
485 if (ret < 0) {
486 PERROR("kernel consumer cmd_sock close");
487 }
488 }
489 if (ustconsumer32_data.cmd_sock >= 0) {
490 ret = close(ustconsumer32_data.cmd_sock);
491 if (ret < 0) {
a76cbd9f 492 PERROR("UST consumerd32 cmd_sock close");
e975f9f8
DG
493 }
494 }
495 if (ustconsumer64_data.cmd_sock >= 0) {
496 ret = close(ustconsumer64_data.cmd_sock);
497 if (ret < 0) {
a76cbd9f 498 PERROR("UST consumerd64 cmd_sock close");
e975f9f8
DG
499 }
500 }
501}
502
fac6795d 503/*
d063d709 504 * Cleanup the daemon
fac6795d 505 */
cf3af59e 506static void cleanup(void)
fac6795d 507{
ef599319 508 int ret;
af9737e9 509 struct ltt_session *sess, *stmp;
8c6c56c2 510 char path[PATH_MAX];
fac6795d 511
1d4b027a 512 DBG("Cleaning up");
e07ae692 513
4e449f3f
MD
514 /*
515 * Close the thread quit pipe. It has already done its job,
516 * since we are now called.
517 */
2f77fc4b
DG
518 utils_close_pipe(thread_quit_pipe);
519
35f90c40
DG
520 /*
521 * If opt_pidfile is undefined, the default file will be wiped when
522 * removing the rundir.
523 */
524 if (opt_pidfile) {
525 ret = remove(opt_pidfile);
526 if (ret < 0) {
527 PERROR("remove pidfile %s", opt_pidfile);
528 }
529 }
530
8c6c56c2
MD
531 DBG("Removing sessiond and consumerd content of directory %s", rundir);
532
533 /* sessiond */
534 snprintf(path, PATH_MAX,
535 "%s/%s",
536 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
537 DBG("Removing %s", path);
538 (void) unlink(path);
539
cd9290dd
DG
540 snprintf(path, PATH_MAX, "%s/%s", rundir,
541 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE);
542 DBG("Removing %s", path);
543 (void) unlink(path);
544
8c6c56c2
MD
545 /* kconsumerd */
546 snprintf(path, PATH_MAX,
547 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
548 rundir);
549 DBG("Removing %s", path);
550 (void) unlink(path);
551
552 snprintf(path, PATH_MAX,
553 DEFAULT_KCONSUMERD_PATH,
554 rundir);
555 DBG("Removing directory %s", path);
556 (void) rmdir(path);
557
558 /* ust consumerd 32 */
559 snprintf(path, PATH_MAX,
560 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
561 rundir);
562 DBG("Removing %s", path);
563 (void) unlink(path);
564
565 snprintf(path, PATH_MAX,
566 DEFAULT_USTCONSUMERD32_PATH,
567 rundir);
568 DBG("Removing directory %s", path);
569 (void) rmdir(path);
570
571 /* ust consumerd 64 */
572 snprintf(path, PATH_MAX,
573 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
574 rundir);
575 DBG("Removing %s", path);
576 (void) unlink(path);
577
578 snprintf(path, PATH_MAX,
579 DEFAULT_USTCONSUMERD64_PATH,
580 rundir);
581 DBG("Removing directory %s", path);
582 (void) rmdir(path);
5461b305 583
dcbcae3e
MD
584 /*
585 * We do NOT rmdir rundir because there are other processes
586 * using it, for instance lttng-relayd, which can start in
587 * parallel with this teardown.
588 */
589
bd69add2 590 free(rundir);
5461b305 591
99bab54f 592 DBG("Cleaning up all sessions");
fac6795d 593
b5541356 594 /* Destroy session list mutex */
273ea72c
DG
595 if (session_list_ptr != NULL) {
596 pthread_mutex_destroy(&session_list_ptr->lock);
597
598 /* Cleanup ALL session */
54d01ffb
DG
599 cds_list_for_each_entry_safe(sess, stmp,
600 &session_list_ptr->head, list) {
2f77fc4b 601 cmd_destroy_session(sess, kernel_poll_pipe[1]);
273ea72c
DG
602 }
603 }
604
099e26bd 605 DBG("Closing all UST sockets");
56fff090 606 ust_app_clean_list();
7972aab2 607 buffer_reg_destroy_registries();
099e26bd 608
4fba7219
DG
609 if (is_root && !opt_no_kernel) {
610 DBG2("Closing kernel fd");
a4b35e07 611 if (kernel_tracer_fd >= 0) {
76d7553f
MD
612 ret = close(kernel_tracer_fd);
613 if (ret) {
614 PERROR("close");
615 }
a4b35e07 616 }
2f50c8a3 617 DBG("Unloading kernel modules");
096102bd 618 modprobe_remove_lttng_all();
2f50c8a3 619 }
2f77fc4b 620
e975f9f8
DG
621 close_consumer_sockets();
622
26296c48
JG
623 /*
624 * If the override option is set, the pointer points to a *non* const thus
625 * freeing it even though the variable type is set to const.
626 */
627 if (tracing_group_name_override) {
628 free((void *) tracing_group_name);
629 }
630 if (consumerd32_bin_override) {
631 free((void *) consumerd32_bin);
632 }
633 if (consumerd64_bin_override) {
634 free((void *) consumerd64_bin);
635 }
636 if (consumerd32_libdir_override) {
637 free((void *) consumerd32_libdir);
638 }
639 if (consumerd64_libdir_override) {
640 free((void *) consumerd64_libdir);
641 }
642
643 if (opt_pidfile) {
644 free(opt_pidfile);
645 }
646
421cb601 647 /* <fun> */
f56a39af 648 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
421cb601
DG
649 "Matthew, BEET driven development works!%c[%dm",
650 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
651 /* </fun> */
fac6795d
DG
652}
653
e065084a 654/*
d063d709 655 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 656 *
d063d709 657 * Return lttcomm error code.
e065084a
DG
658 */
659static int send_unix_sock(int sock, void *buf, size_t len)
660{
661 /* Check valid length */
c617c0c6 662 if (len == 0) {
e065084a
DG
663 return -1;
664 }
665
666 return lttcomm_send_unix_sock(sock, buf, len);
667}
668
5461b305 669/*
d063d709 670 * Free memory of a command context structure.
5461b305 671 */
a2fb29a5 672static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 673{
a2fb29a5
DG
674 DBG("Clean command context structure");
675 if (*cmd_ctx) {
676 if ((*cmd_ctx)->llm) {
677 free((*cmd_ctx)->llm);
5461b305 678 }
a2fb29a5
DG
679 if ((*cmd_ctx)->lsm) {
680 free((*cmd_ctx)->lsm);
5461b305 681 }
a2fb29a5
DG
682 free(*cmd_ctx);
683 *cmd_ctx = NULL;
5461b305
DG
684 }
685}
686
fac6795d 687/*
0fdd1e2c 688 * Notify UST applications using the shm mmap futex.
fac6795d 689 */
0fdd1e2c 690static int notify_ust_apps(int active)
fac6795d 691{
0fdd1e2c 692 char *wait_shm_mmap;
fac6795d 693
0fdd1e2c 694 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 695
0fdd1e2c
DG
696 /* See shm.c for this call implying mmap, shm and futex calls */
697 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
698 if (wait_shm_mmap == NULL) {
fac6795d
DG
699 goto error;
700 }
701
0fdd1e2c
DG
702 /* Wake waiting process */
703 futex_wait_update((int32_t *) wait_shm_mmap, active);
704
705 /* Apps notified successfully */
706 return 0;
fac6795d
DG
707
708error:
0fdd1e2c 709 return -1;
fac6795d
DG
710}
711
e065084a 712/*
d063d709
DG
713 * Setup the outgoing data buffer for the response (llm) by allocating the
714 * right amount of memory and copying the original information from the lsm
715 * structure.
ca95a216 716 *
d063d709 717 * Return total size of the buffer pointed by buf.
ca95a216 718 */
5461b305 719static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 720{
f3ed775e 721 int ret, buf_size;
ca95a216 722
f3ed775e 723 buf_size = size;
5461b305 724
ba7f0ae5 725 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
5461b305 726 if (cmd_ctx->llm == NULL) {
76d7553f 727 PERROR("zmalloc");
5461b305 728 ret = -ENOMEM;
ca95a216
DG
729 goto error;
730 }
731
5461b305
DG
732 /* Copy common data */
733 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 734 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 735
5461b305
DG
736 cmd_ctx->llm->data_size = size;
737 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
738
ca95a216
DG
739 return buf_size;
740
741error:
742 return ret;
743}
744
7a485870 745/*
5eb91c98 746 * Update the kernel poll set of all channel fd available over all tracing
d063d709 747 * session. Add the wakeup pipe at the end of the set.
7a485870 748 */
5eb91c98 749static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 750{
5eb91c98 751 int ret;
7a485870
DG
752 struct ltt_session *session;
753 struct ltt_kernel_channel *channel;
754
5eb91c98 755 DBG("Updating kernel poll set");
7a485870 756
54d01ffb 757 session_lock_list();
b5541356 758 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 759 session_lock(session);
7a485870 760 if (session->kernel_session == NULL) {
54d01ffb 761 session_unlock(session);
7a485870
DG
762 continue;
763 }
7a485870 764
54d01ffb
DG
765 cds_list_for_each_entry(channel,
766 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
767 /* Add channel fd to the kernel poll set */
768 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
769 if (ret < 0) {
54d01ffb 770 session_unlock(session);
5eb91c98
DG
771 goto error;
772 }
773 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 774 }
54d01ffb 775 session_unlock(session);
7a485870 776 }
54d01ffb 777 session_unlock_list();
7a485870 778
5eb91c98 779 return 0;
7a485870
DG
780
781error:
54d01ffb 782 session_unlock_list();
7a485870
DG
783 return -1;
784}
785
786/*
54d01ffb 787 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 788 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 789 *
d063d709 790 * Useful for CPU hotplug feature.
7a485870 791 */
2bdd86d4 792static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
793{
794 int ret = 0;
795 struct ltt_session *session;
173af62f 796 struct ltt_kernel_session *ksess;
7a485870
DG
797 struct ltt_kernel_channel *channel;
798
799 DBG("Updating kernel streams for channel fd %d", fd);
800
54d01ffb 801 session_lock_list();
b5541356 802 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 803 session_lock(session);
7a485870 804 if (session->kernel_session == NULL) {
54d01ffb 805 session_unlock(session);
7a485870
DG
806 continue;
807 }
173af62f 808 ksess = session->kernel_session;
d9800920 809
173af62f 810 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
7a485870
DG
811 if (channel->fd == fd) {
812 DBG("Channel found, updating kernel streams");
813 ret = kernel_open_channel_stream(channel);
814 if (ret < 0) {
b3c750d2 815 goto error;
7a485870 816 }
5c786ded
JD
817 /* Update the stream global counter */
818 ksess->stream_count_global += ret;
d9800920 819
7a485870 820 /*
5eb91c98
DG
821 * Have we already sent fds to the consumer? If yes, it means
822 * that tracing is started so it is safe to send our updated
823 * stream fds.
7a485870 824 */
173af62f
DG
825 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
826 struct lttng_ht_iter iter;
827 struct consumer_socket *socket;
828
e7fe706f 829 rcu_read_lock();
173af62f
DG
830 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
831 &iter.iter, socket, node.node) {
173af62f 832 pthread_mutex_lock(socket->lock);
f50f23d9 833 ret = kernel_consumer_send_channel_stream(socket,
2bba9e53
DG
834 channel, ksess,
835 session->output_traces ? 1 : 0);
173af62f
DG
836 pthread_mutex_unlock(socket->lock);
837 if (ret < 0) {
e7fe706f 838 rcu_read_unlock();
173af62f
DG
839 goto error;
840 }
7a485870 841 }
e7fe706f 842 rcu_read_unlock();
7a485870 843 }
b3c750d2 844 goto error;
7a485870
DG
845 }
846 }
54d01ffb 847 session_unlock(session);
7a485870 848 }
54d01ffb 849 session_unlock_list();
b3c750d2 850 return ret;
7a485870 851
b3c750d2 852error:
54d01ffb
DG
853 session_unlock(session);
854 session_unlock_list();
7a485870
DG
855 return ret;
856}
857
487cf67c 858/*
ffe60014
DG
859 * For each tracing session, update newly registered apps. The session list
860 * lock MUST be acquired before calling this.
487cf67c
DG
861 */
862static void update_ust_app(int app_sock)
863{
864 struct ltt_session *sess, *stmp;
865
fdadac08
DG
866 /* Consumer is in an ERROR state. Stop any application update. */
867 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
868 /* Stop the update process since the consumer is dead. */
869 return;
870 }
871
487cf67c
DG
872 /* For all tracing session(s) */
873 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
4ee14516 874 session_lock(sess);
421cb601
DG
875 if (sess->ust_session) {
876 ust_app_global_update(sess->ust_session, app_sock);
877 }
4ee14516 878 session_unlock(sess);
487cf67c
DG
879 }
880}
881
7a485870 882/*
d063d709 883 * This thread manage event coming from the kernel.
7a485870 884 *
d063d709
DG
885 * Features supported in this thread:
886 * -) CPU Hotplug
7a485870
DG
887 */
888static void *thread_manage_kernel(void *data)
889{
139ac872 890 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 891 uint32_t revents, nb_fd;
7a485870 892 char tmp;
5eb91c98 893 struct lttng_poll_event events;
7a485870 894
6993eeb3 895 DBG("[thread] Thread manage kernel started");
7a485870 896
6c71277b 897 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 898
d5d63bf1
DG
899 /*
900 * This first step of the while is to clean this structure which could free
6d737ce4 901 * non NULL pointers so initialize it before the loop.
d5d63bf1 902 */
6d737ce4 903 lttng_poll_init(&events);
d5d63bf1 904
e547b070 905 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
906 goto error_testpoint;
907 }
8ac94142 908
840cb59c 909 health_code_update();
44a5e5eb 910
e547b070 911 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 912 goto error_testpoint;
6993eeb3
CB
913 }
914
7a485870 915 while (1) {
840cb59c 916 health_code_update();
44a5e5eb 917
7a485870 918 if (update_poll_flag == 1) {
d21b0d71
DG
919 /* Clean events object. We are about to populate it again. */
920 lttng_poll_clean(&events);
921
d0b96690 922 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
923 if (ret < 0) {
924 goto error_poll_create;
925 }
926
927 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
928 if (ret < 0) {
929 goto error;
930 }
5f822d0a 931
d21b0d71 932 /* This will add the available kernel channel if any. */
5eb91c98
DG
933 ret = update_kernel_poll(&events);
934 if (ret < 0) {
7a485870
DG
935 goto error;
936 }
937 update_poll_flag = 0;
938 }
939
d21b0d71 940 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
7a485870
DG
941
942 /* Poll infinite value of time */
88f2b785 943 restart:
a78af745 944 health_poll_entry();
5eb91c98 945 ret = lttng_poll_wait(&events, -1);
a78af745 946 health_poll_exit();
7a485870 947 if (ret < 0) {
88f2b785
MD
948 /*
949 * Restart interrupted system call.
950 */
951 if (errno == EINTR) {
952 goto restart;
953 }
7a485870
DG
954 goto error;
955 } else if (ret == 0) {
956 /* Should not happen since timeout is infinite */
85611738
DG
957 ERR("Return value of poll is 0 with an infinite timeout.\n"
958 "This should not have happened! Continuing...");
7a485870
DG
959 continue;
960 }
961
0d9c5d77
DG
962 nb_fd = ret;
963
5eb91c98
DG
964 for (i = 0; i < nb_fd; i++) {
965 /* Fetch once the poll data */
966 revents = LTTNG_POLL_GETEV(&events, i);
967 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 968
840cb59c 969 health_code_update();
44a5e5eb 970
5eb91c98 971 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 972 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 973 if (ret) {
139ac872
MD
974 err = 0;
975 goto exit;
5eb91c98 976 }
7a485870 977
5eb91c98
DG
978 /* Check for data on kernel pipe */
979 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
6cd525e8
MD
980 (void) lttng_read(kernel_poll_pipe[0],
981 &tmp, 1);
f921c78f
DG
982 /*
983 * Ret value is useless here, if this pipe gets any actions an
984 * update is required anyway.
985 */
5eb91c98
DG
986 update_poll_flag = 1;
987 continue;
988 } else {
989 /*
990 * New CPU detected by the kernel. Adding kernel stream to
991 * kernel session and updating the kernel consumer
992 */
993 if (revents & LPOLLIN) {
2bdd86d4 994 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
995 if (ret < 0) {
996 continue;
997 }
998 break;
999 /*
1000 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1001 * and unregister kernel stream at this point.
1002 */
7a485870 1003 }
7a485870
DG
1004 }
1005 }
1006 }
1007
139ac872 1008exit:
7a485870 1009error:
5eb91c98 1010 lttng_poll_clean(&events);
76d7553f 1011error_poll_create:
6993eeb3 1012error_testpoint:
6620da75
DG
1013 utils_close_pipe(kernel_poll_pipe);
1014 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 1015 if (err) {
840cb59c 1016 health_error();
139ac872 1017 ERR("Health error occurred in %s", __func__);
6620da75
DG
1018 WARN("Kernel thread died unexpectedly. "
1019 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 1020 }
8782cc74 1021 health_unregister(health_sessiond);
76d7553f 1022 DBG("Kernel thread dying");
7a485870
DG
1023 return NULL;
1024}
1025
a23ec3a7
DG
1026/*
1027 * Signal pthread condition of the consumer data that the thread.
1028 */
1029static void signal_consumer_condition(struct consumer_data *data, int state)
1030{
1031 pthread_mutex_lock(&data->cond_mutex);
1032
1033 /*
1034 * The state is set before signaling. It can be any value, it's the waiter
1035 * job to correctly interpret this condition variable associated to the
1036 * consumer pthread_cond.
1037 *
1038 * A value of 0 means that the corresponding thread of the consumer data
1039 * was not started. 1 indicates that the thread has started and is ready
1040 * for action. A negative value means that there was an error during the
1041 * thread bootstrap.
1042 */
1043 data->consumer_thread_is_ready = state;
1044 (void) pthread_cond_signal(&data->cond);
1045
1046 pthread_mutex_unlock(&data->cond_mutex);
1047}
1048
1d4b027a 1049/*
3bd1e081 1050 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 1051 */
3bd1e081 1052static void *thread_manage_consumer(void *data)
1d4b027a 1053{
139ac872 1054 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98 1055 uint32_t revents, nb_fd;
1d4b027a 1056 enum lttcomm_return_code code;
5eb91c98 1057 struct lttng_poll_event events;
3bd1e081 1058 struct consumer_data *consumer_data = data;
1d4b027a 1059
3bd1e081 1060 DBG("[thread] Manage consumer started");
1d4b027a 1061
6c71277b 1062 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 1063
855060f8 1064 health_code_update();
9449cc75 1065
5eb91c98 1066 /*
331744e3
JD
1067 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1068 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 1069 */
331744e3 1070 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 1071 if (ret < 0) {
76d7553f 1072 goto error_poll;
5eb91c98 1073 }
273ea72c 1074
edb8b045
DG
1075 /*
1076 * The error socket here is already in a listening state which was done
1077 * just before spawning this thread to avoid a race between the consumer
1078 * daemon exec trying to connect and the listen() call.
1079 */
3bd1e081 1080 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
1081 if (ret < 0) {
1082 goto error;
1083 }
1084
840cb59c 1085 health_code_update();
44a5e5eb 1086
331744e3 1087 /* Infinite blocking call, waiting for transmission */
88f2b785 1088restart:
a78af745 1089 health_poll_entry();
8ac94142 1090
e547b070 1091 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
1092 goto error;
1093 }
8ac94142 1094
5eb91c98 1095 ret = lttng_poll_wait(&events, -1);
a78af745 1096 health_poll_exit();
273ea72c 1097 if (ret < 0) {
88f2b785
MD
1098 /*
1099 * Restart interrupted system call.
1100 */
1101 if (errno == EINTR) {
1102 goto restart;
1103 }
273ea72c
DG
1104 goto error;
1105 }
1106
0d9c5d77
DG
1107 nb_fd = ret;
1108
5eb91c98
DG
1109 for (i = 0; i < nb_fd; i++) {
1110 /* Fetch once the poll data */
1111 revents = LTTNG_POLL_GETEV(&events, i);
1112 pollfd = LTTNG_POLL_GETFD(&events, i);
1113
840cb59c 1114 health_code_update();
44a5e5eb 1115
5eb91c98 1116 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1117 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1118 if (ret) {
139ac872
MD
1119 err = 0;
1120 goto exit;
5eb91c98
DG
1121 }
1122
1123 /* Event on the registration socket */
3bd1e081 1124 if (pollfd == consumer_data->err_sock) {
5eb91c98 1125 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1126 ERR("consumer err socket poll error");
5eb91c98
DG
1127 goto error;
1128 }
1129 }
273ea72c
DG
1130 }
1131
3bd1e081 1132 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
1133 if (sock < 0) {
1134 goto error;
1135 }
1136
b662582b
DG
1137 /*
1138 * Set the CLOEXEC flag. Return code is useless because either way, the
1139 * show must go on.
1140 */
1141 (void) utils_set_fd_cloexec(sock);
1142
840cb59c 1143 health_code_update();
44a5e5eb 1144
3bd1e081 1145 DBG2("Receiving code from consumer err_sock");
ee0b0061 1146
712ea556 1147 /* Getting status code from kconsumerd */
54d01ffb
DG
1148 ret = lttcomm_recv_unix_sock(sock, &code,
1149 sizeof(enum lttcomm_return_code));
1d4b027a
DG
1150 if (ret <= 0) {
1151 goto error;
1152 }
1153
840cb59c 1154 health_code_update();
f73fabfd 1155 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
331744e3 1156 /* Connect both socket, command and metadata. */
3bd1e081
MD
1157 consumer_data->cmd_sock =
1158 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
4ce514c4 1159 consumer_data->metadata_fd =
331744e3 1160 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
92db7cdc
DG
1161 if (consumer_data->cmd_sock < 0
1162 || consumer_data->metadata_fd < 0) {
331744e3 1163 PERROR("consumer connect cmd socket");
a23ec3a7
DG
1164 /* On error, signal condition and quit. */
1165 signal_consumer_condition(consumer_data, -1);
1d4b027a
DG
1166 goto error;
1167 }
9363801e 1168 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
331744e3
JD
1169 /* Create metadata socket lock. */
1170 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1171 if (consumer_data->metadata_sock.lock == NULL) {
1172 PERROR("zmalloc pthread mutex");
1173 ret = -1;
1174 goto error;
1175 }
1176 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1177
a23ec3a7 1178 signal_consumer_condition(consumer_data, 1);
331744e3
JD
1179 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1180 DBG("Consumer metadata socket ready (fd: %d)",
4ce514c4 1181 consumer_data->metadata_fd);
1d4b027a 1182 } else {
3bd1e081 1183 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1184 lttcomm_get_readable_code(-code));
1185 goto error;
1186 }
1187
331744e3 1188 /* Remove the consumerd error sock since we've established a connexion */
3bd1e081 1189 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1190 if (ret < 0) {
72079cae
DG
1191 goto error;
1192 }
1193
331744e3 1194 /* Add new accepted error socket. */
5eb91c98
DG
1195 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1196 if (ret < 0) {
72079cae 1197 goto error;
5eb91c98
DG
1198 }
1199
331744e3 1200 /* Add metadata socket that is successfully connected. */
4ce514c4 1201 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
1202 LPOLLIN | LPOLLRDHUP);
1203 if (ret < 0) {
1204 goto error;
1205 }
1206
840cb59c 1207 health_code_update();
44a5e5eb 1208
331744e3 1209 /* Infinite blocking call, waiting for transmission */
88f2b785 1210restart_poll:
331744e3
JD
1211 while (1) {
1212 health_poll_entry();
1213 ret = lttng_poll_wait(&events, -1);
1214 health_poll_exit();
1215 if (ret < 0) {
1216 /*
1217 * Restart interrupted system call.
1218 */
1219 if (errno == EINTR) {
1220 goto restart_poll;
1221 }
1222 goto error;
88f2b785 1223 }
72079cae 1224
331744e3 1225 nb_fd = ret;
0d9c5d77 1226
331744e3
JD
1227 for (i = 0; i < nb_fd; i++) {
1228 /* Fetch once the poll data */
1229 revents = LTTNG_POLL_GETEV(&events, i);
1230 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1231
331744e3 1232 health_code_update();
44a5e5eb 1233
331744e3
JD
1234 /* Thread quit pipe has been closed. Killing thread. */
1235 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1236 if (ret) {
1237 err = 0;
1238 goto exit;
1239 }
5eb91c98 1240
331744e3
JD
1241 if (pollfd == sock) {
1242 /* Event on the consumerd socket */
1243 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1244 ERR("consumer err socket second poll error");
1245 goto error;
1246 }
1247 health_code_update();
1248 /* Wait for any kconsumerd error */
1249 ret = lttcomm_recv_unix_sock(sock, &code,
1250 sizeof(enum lttcomm_return_code));
1251 if (ret <= 0) {
1252 ERR("consumer closed the command socket");
1253 goto error;
1254 }
1255
1256 ERR("consumer return code : %s",
1257 lttcomm_get_readable_code(-code));
1258
1259 goto exit;
4ce514c4 1260 } else if (pollfd == consumer_data->metadata_fd) {
331744e3
JD
1261 /* UST metadata requests */
1262 ret = ust_consumer_metadata_request(
1263 &consumer_data->metadata_sock);
1264 if (ret < 0) {
1265 ERR("Handling metadata request");
1266 goto error;
1267 }
1268 break;
1269 } else {
1270 ERR("Unknown pollfd");
5eb91c98
DG
1271 goto error;
1272 }
1273 }
331744e3 1274 health_code_update();
5eb91c98
DG
1275 }
1276
139ac872 1277exit:
1d4b027a 1278error:
fdadac08
DG
1279 /*
1280 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1281 * thread might be using them so get exclusive access which will abort all
1282 * other consumer command by other threads.
fdadac08
DG
1283 */
1284 pthread_mutex_lock(&consumer_data->lock);
1285
5c827ce0
DG
1286 /* Immediately set the consumerd state to stopped */
1287 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1288 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1289 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1290 consumer_data->type == LTTNG_CONSUMER32_UST) {
1291 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1292 } else {
1293 /* Code flow error... */
1294 assert(0);
1295 }
1296
76d7553f
MD
1297 if (consumer_data->err_sock >= 0) {
1298 ret = close(consumer_data->err_sock);
1299 if (ret) {
1300 PERROR("close");
1301 }
a76cbd9f 1302 consumer_data->err_sock = -1;
76d7553f
MD
1303 }
1304 if (consumer_data->cmd_sock >= 0) {
1305 ret = close(consumer_data->cmd_sock);
1306 if (ret) {
1307 PERROR("close");
1308 }
a76cbd9f 1309 consumer_data->cmd_sock = -1;
76d7553f 1310 }
96544455
SS
1311 if (consumer_data->metadata_sock.fd_ptr &&
1312 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1313 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1314 if (ret) {
1315 PERROR("close");
1316 }
1317 }
76d7553f
MD
1318 if (sock >= 0) {
1319 ret = close(sock);
1320 if (ret) {
1321 PERROR("close");
1322 }
1323 }
273ea72c 1324
3bd1e081
MD
1325 unlink(consumer_data->err_unix_sock_path);
1326 unlink(consumer_data->cmd_unix_sock_path);
1327 consumer_data->pid = 0;
fdadac08 1328 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1329
fdadac08 1330 /* Cleanup metadata socket mutex. */
96544455
SS
1331 if (consumer_data->metadata_sock.lock) {
1332 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1333 free(consumer_data->metadata_sock.lock);
1334 }
5eb91c98 1335 lttng_poll_clean(&events);
76d7553f 1336error_poll:
139ac872 1337 if (err) {
840cb59c 1338 health_error();
139ac872
MD
1339 ERR("Health error occurred in %s", __func__);
1340 }
8782cc74 1341 health_unregister(health_sessiond);
76d7553f 1342 DBG("consumer thread cleanup completed");
0177d773 1343
5eb91c98 1344 return NULL;
099e26bd
DG
1345}
1346
099e26bd
DG
1347/*
1348 * This thread manage application communication.
1d4b027a
DG
1349 */
1350static void *thread_manage_apps(void *data)
099e26bd 1351{
139ac872 1352 int i, ret, pollfd, err = -1;
6cd525e8 1353 ssize_t size_ret;
5eb91c98 1354 uint32_t revents, nb_fd;
5eb91c98 1355 struct lttng_poll_event events;
099e26bd
DG
1356
1357 DBG("[thread] Manage application started");
1358
f6a9efaa
DG
1359 rcu_register_thread();
1360 rcu_thread_online();
1361
6c71277b 1362 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1363
e547b070 1364 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1365 goto error_testpoint;
1366 }
1367
840cb59c 1368 health_code_update();
44a5e5eb 1369
d0b96690 1370 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1371 if (ret < 0) {
76d7553f 1372 goto error_poll_create;
5eb91c98 1373 }
099e26bd 1374
5eb91c98
DG
1375 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1376 if (ret < 0) {
1377 goto error;
1378 }
099e26bd 1379
e547b070 1380 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1381 goto error;
1382 }
8ac94142 1383
840cb59c 1384 health_code_update();
44a5e5eb 1385
5eb91c98 1386 while (1) {
d21b0d71 1387 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
099e26bd
DG
1388
1389 /* Inifinite blocking call, waiting for transmission */
88f2b785 1390 restart:
a78af745 1391 health_poll_entry();
5eb91c98 1392 ret = lttng_poll_wait(&events, -1);
a78af745 1393 health_poll_exit();
099e26bd 1394 if (ret < 0) {
88f2b785
MD
1395 /*
1396 * Restart interrupted system call.
1397 */
1398 if (errno == EINTR) {
1399 goto restart;
1400 }
099e26bd
DG
1401 goto error;
1402 }
1403
0d9c5d77
DG
1404 nb_fd = ret;
1405
5eb91c98
DG
1406 for (i = 0; i < nb_fd; i++) {
1407 /* Fetch once the poll data */
1408 revents = LTTNG_POLL_GETEV(&events, i);
1409 pollfd = LTTNG_POLL_GETFD(&events, i);
1410
840cb59c 1411 health_code_update();
44a5e5eb 1412
5eb91c98 1413 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1414 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1415 if (ret) {
139ac872
MD
1416 err = 0;
1417 goto exit;
5eb91c98 1418 }
099e26bd 1419
5eb91c98
DG
1420 /* Inspect the apps cmd pipe */
1421 if (pollfd == apps_cmd_pipe[0]) {
1422 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1423 ERR("Apps command pipe error");
0177d773 1424 goto error;
5eb91c98 1425 } else if (revents & LPOLLIN) {
d0b96690
DG
1426 int sock;
1427
5eb91c98 1428 /* Empty pipe */
6cd525e8
MD
1429 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1430 if (size_ret < sizeof(sock)) {
76d7553f 1431 PERROR("read apps cmd pipe");
5eb91c98
DG
1432 goto error;
1433 }
099e26bd 1434
840cb59c 1435 health_code_update();
44a5e5eb 1436
ffe60014 1437 /*
d0b96690
DG
1438 * We only monitor the error events of the socket. This
1439 * thread does not handle any incoming data from UST
1440 * (POLLIN).
ffe60014 1441 */
d0b96690
DG
1442 ret = lttng_poll_add(&events, sock,
1443 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1444 if (ret < 0) {
5eb91c98 1445 goto error;
e0c7ec2b 1446 }
acc7b41b 1447
d0b96690 1448 DBG("Apps with sock %d added to poll set", sock);
0177d773 1449 }
5eb91c98
DG
1450 } else {
1451 /*
54d01ffb
DG
1452 * At this point, we know that a registered application made
1453 * the event at poll_wait.
5eb91c98
DG
1454 */
1455 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1456 /* Removing from the poll set */
1457 ret = lttng_poll_del(&events, pollfd);
1458 if (ret < 0) {
1459 goto error;
1460 }
099e26bd 1461
b9d9b220 1462 /* Socket closed on remote end. */
56fff090 1463 ust_app_unregister(pollfd);
5eb91c98 1464 }
099e26bd 1465 }
44a5e5eb 1466
840cb59c 1467 health_code_update();
099e26bd 1468 }
099e26bd
DG
1469 }
1470
139ac872 1471exit:
099e26bd 1472error:
5eb91c98 1473 lttng_poll_clean(&events);
76d7553f 1474error_poll_create:
6993eeb3 1475error_testpoint:
6620da75
DG
1476 utils_close_pipe(apps_cmd_pipe);
1477 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1478
1479 /*
1480 * We don't clean the UST app hash table here since already registered
1481 * applications can still be controlled so let them be until the session
1482 * daemon dies or the applications stop.
1483 */
1484
139ac872 1485 if (err) {
840cb59c 1486 health_error();
139ac872
MD
1487 ERR("Health error occurred in %s", __func__);
1488 }
8782cc74 1489 health_unregister(health_sessiond);
76d7553f 1490 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1491 rcu_thread_offline();
1492 rcu_unregister_thread();
099e26bd
DG
1493 return NULL;
1494}
1495
d0b96690 1496/*
d88aee68
DG
1497 * Send a socket to a thread This is called from the dispatch UST registration
1498 * thread once all sockets are set for the application.
d0b96690 1499 *
b85dc84c
DG
1500 * The sock value can be invalid, we don't really care, the thread will handle
1501 * it and make the necessary cleanup if so.
1502 *
d0b96690
DG
1503 * On success, return 0 else a negative value being the errno message of the
1504 * write().
1505 */
d88aee68 1506static int send_socket_to_thread(int fd, int sock)
d0b96690 1507{
6cd525e8 1508 ssize_t ret;
d0b96690 1509
b85dc84c
DG
1510 /*
1511 * It's possible that the FD is set as invalid with -1 concurrently just
1512 * before calling this function being a shutdown state of the thread.
1513 */
1514 if (fd < 0) {
1515 ret = -EBADF;
1516 goto error;
1517 }
d0b96690 1518
6cd525e8
MD
1519 ret = lttng_write(fd, &sock, sizeof(sock));
1520 if (ret < sizeof(sock)) {
d88aee68 1521 PERROR("write apps pipe %d", fd);
d0b96690
DG
1522 if (ret < 0) {
1523 ret = -errno;
1524 }
1525 goto error;
1526 }
1527
1528 /* All good. Don't send back the write positive ret value. */
1529 ret = 0;
1530error:
6cd525e8 1531 return (int) ret;
d0b96690
DG
1532}
1533
f45e313d
DG
1534/*
1535 * Sanitize the wait queue of the dispatch registration thread meaning removing
1536 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1537 * notify socket is never received.
1538 */
1539static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1540{
1541 int ret, nb_fd = 0, i;
1542 unsigned int fd_added = 0;
1543 struct lttng_poll_event events;
1544 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1545
1546 assert(wait_queue);
1547
1548 lttng_poll_init(&events);
1549
1550 /* Just skip everything for an empty queue. */
1551 if (!wait_queue->count) {
1552 goto end;
1553 }
1554
1555 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1556 if (ret < 0) {
1557 goto error_create;
1558 }
1559
1560 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1561 &wait_queue->head, head) {
1562 assert(wait_node->app);
1563 ret = lttng_poll_add(&events, wait_node->app->sock,
1564 LPOLLHUP | LPOLLERR);
1565 if (ret < 0) {
1566 goto error;
1567 }
1568
1569 fd_added = 1;
1570 }
1571
1572 if (!fd_added) {
1573 goto end;
1574 }
1575
1576 /*
1577 * Poll but don't block so we can quickly identify the faulty events and
1578 * clean them afterwards from the wait queue.
1579 */
1580 ret = lttng_poll_wait(&events, 0);
1581 if (ret < 0) {
1582 goto error;
1583 }
1584 nb_fd = ret;
1585
1586 for (i = 0; i < nb_fd; i++) {
1587 /* Get faulty FD. */
1588 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1589 int pollfd = LTTNG_POLL_GETFD(&events, i);
1590
1591 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1592 &wait_queue->head, head) {
1593 if (pollfd == wait_node->app->sock &&
1594 (revents & (LPOLLHUP | LPOLLERR))) {
1595 cds_list_del(&wait_node->head);
1596 wait_queue->count--;
1597 ust_app_destroy(wait_node->app);
1598 free(wait_node);
1599 break;
1600 }
1601 }
1602 }
1603
1604 if (nb_fd > 0) {
1605 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1606 }
1607
1608end:
1609 lttng_poll_clean(&events);
1610 return;
1611
1612error:
1613 lttng_poll_clean(&events);
1614error_create:
1615 ERR("Unable to sanitize wait queue");
1616 return;
1617}
1618
099e26bd
DG
1619/*
1620 * Dispatch request from the registration threads to the application
1621 * communication thread.
1622 */
1623static void *thread_dispatch_ust_registration(void *data)
1624{
12e2b881 1625 int ret, err = -1;
099e26bd
DG
1626 struct cds_wfq_node *node;
1627 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1628 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1629 struct ust_reg_wait_queue wait_queue = {
1630 .count = 0,
1631 };
d0b96690 1632
6c71277b 1633 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1634
9ad42ec1
MD
1635 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1636 goto error_testpoint;
1637 }
1638
12e2b881
MD
1639 health_code_update();
1640
f45e313d 1641 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1642
1643 DBG("[thread] Dispatch UST command started");
1644
26c9d55e 1645 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
12e2b881
MD
1646 health_code_update();
1647
099e26bd
DG
1648 /* Atomically prepare the queue futex */
1649 futex_nto1_prepare(&ust_cmd_queue.futex);
1650
1651 do {
d0b96690 1652 struct ust_app *app = NULL;
7972aab2 1653 ust_cmd = NULL;
d0b96690 1654
f45e313d
DG
1655 /*
1656 * Make sure we don't have node(s) that have hung up before receiving
1657 * the notify socket. This is to clean the list in order to avoid
1658 * memory leaks from notify socket that are never seen.
1659 */
1660 sanitize_wait_queue(&wait_queue);
1661
12e2b881 1662 health_code_update();
099e26bd
DG
1663 /* Dequeue command for registration */
1664 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1665 if (node == NULL) {
00a17c97 1666 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1667 /* Continue thread execution */
1668 break;
1669 }
1670
1671 ust_cmd = caa_container_of(node, struct ust_command, node);
1672
2f50c8a3
DG
1673 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1674 " gid:%d sock:%d name:%s (version %d.%d)",
1675 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1676 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1677 ust_cmd->sock, ust_cmd->reg_msg.name,
1678 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1679
1680 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1681 wait_node = zmalloc(sizeof(*wait_node));
1682 if (!wait_node) {
1683 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1684 ret = close(ust_cmd->sock);
1685 if (ret < 0) {
1686 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1687 }
51dec90d 1688 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1689 free(ust_cmd);
d0b96690
DG
1690 goto error;
1691 }
1692 CDS_INIT_LIST_HEAD(&wait_node->head);
1693
1694 /* Create application object if socket is CMD. */
1695 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1696 ust_cmd->sock);
1697 if (!wait_node->app) {
1698 ret = close(ust_cmd->sock);
1699 if (ret < 0) {
1700 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1701 }
51dec90d 1702 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1703 free(wait_node);
7972aab2 1704 free(ust_cmd);
d0b96690
DG
1705 continue;
1706 }
1707 /*
1708 * Add application to the wait queue so we can set the notify
1709 * socket before putting this object in the global ht.
1710 */
f45e313d
DG
1711 cds_list_add(&wait_node->head, &wait_queue.head);
1712 wait_queue.count++;
d0b96690 1713
7972aab2 1714 free(ust_cmd);
d0b96690
DG
1715 /*
1716 * We have to continue here since we don't have the notify
1717 * socket and the application MUST be added to the hash table
1718 * only at that moment.
1719 */
1720 continue;
1721 } else {
1722 /*
1723 * Look for the application in the local wait queue and set the
1724 * notify socket if found.
1725 */
d88aee68 1726 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1727 &wait_queue.head, head) {
12e2b881 1728 health_code_update();
d0b96690
DG
1729 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1730 wait_node->app->notify_sock = ust_cmd->sock;
1731 cds_list_del(&wait_node->head);
f45e313d 1732 wait_queue.count--;
d0b96690
DG
1733 app = wait_node->app;
1734 free(wait_node);
1735 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1736 break;
1737 }
1738 }
020d7f60
DG
1739
1740 /*
1741 * With no application at this stage the received socket is
1742 * basically useless so close it before we free the cmd data
1743 * structure for good.
1744 */
1745 if (!app) {
1746 ret = close(ust_cmd->sock);
1747 if (ret < 0) {
1748 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1749 }
51dec90d 1750 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 1751 }
7972aab2 1752 free(ust_cmd);
d0b96690
DG
1753 }
1754
1755 if (app) {
d0b96690
DG
1756 /*
1757 * @session_lock_list
1758 *
1759 * Lock the global session list so from the register up to the
1760 * registration done message, no thread can see the application
1761 * and change its state.
1762 */
1763 session_lock_list();
1764 rcu_read_lock();
d88aee68 1765
d0b96690
DG
1766 /*
1767 * Add application to the global hash table. This needs to be
1768 * done before the update to the UST registry can locate the
1769 * application.
1770 */
1771 ust_app_add(app);
d88aee68
DG
1772
1773 /* Set app version. This call will print an error if needed. */
1774 (void) ust_app_version(app);
1775
1776 /* Send notify socket through the notify pipe. */
1777 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1778 app->notify_sock);
1779 if (ret < 0) {
1780 rcu_read_unlock();
1781 session_unlock_list();
b85dc84c
DG
1782 /*
1783 * No notify thread, stop the UST tracing. However, this is
1784 * not an internal error of the this thread thus setting
1785 * the health error code to a normal exit.
1786 */
1787 err = 0;
d88aee68 1788 goto error;
6620da75 1789 }
d88aee68 1790
d0b96690
DG
1791 /*
1792 * Update newly registered application with the tracing
1793 * registry info already enabled information.
1794 */
1795 update_ust_app(app->sock);
d88aee68
DG
1796
1797 /*
1798 * Don't care about return value. Let the manage apps threads
1799 * handle app unregistration upon socket close.
1800 */
1801 (void) ust_app_register_done(app->sock);
1802
1803 /*
1804 * Even if the application socket has been closed, send the app
1805 * to the thread and unregistration will take place at that
1806 * place.
1807 */
1808 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 1809 if (ret < 0) {
d88aee68
DG
1810 rcu_read_unlock();
1811 session_unlock_list();
b85dc84c
DG
1812 /*
1813 * No apps. thread, stop the UST tracing. However, this is
1814 * not an internal error of the this thread thus setting
1815 * the health error code to a normal exit.
1816 */
1817 err = 0;
d88aee68 1818 goto error;
d0b96690 1819 }
d88aee68 1820
d0b96690
DG
1821 rcu_read_unlock();
1822 session_unlock_list();
099e26bd 1823 }
099e26bd
DG
1824 } while (node != NULL);
1825
12e2b881 1826 health_poll_entry();
099e26bd
DG
1827 /* Futex wait on queue. Blocking call on futex() */
1828 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 1829 health_poll_exit();
099e26bd 1830 }
12e2b881
MD
1831 /* Normal exit, no error */
1832 err = 0;
099e26bd
DG
1833
1834error:
d88aee68
DG
1835 /* Clean up wait queue. */
1836 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1837 &wait_queue.head, head) {
d88aee68 1838 cds_list_del(&wait_node->head);
f45e313d 1839 wait_queue.count--;
d88aee68
DG
1840 free(wait_node);
1841 }
1842
9ad42ec1 1843error_testpoint:
099e26bd 1844 DBG("Dispatch thread dying");
12e2b881
MD
1845 if (err) {
1846 health_error();
1847 ERR("Health error occurred in %s", __func__);
1848 }
8782cc74 1849 health_unregister(health_sessiond);
099e26bd
DG
1850 return NULL;
1851}
1852
1853/*
1854 * This thread manage application registration.
1855 */
1856static void *thread_registration_apps(void *data)
1d4b027a 1857{
139ac872 1858 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1859 uint32_t revents, nb_fd;
1860 struct lttng_poll_event events;
099e26bd
DG
1861 /*
1862 * Get allocated in this thread, enqueued to a global queue, dequeued and
1863 * freed in the manage apps thread.
1864 */
1865 struct ust_command *ust_cmd = NULL;
1d4b027a 1866
099e26bd 1867 DBG("[thread] Manage application registration started");
1d4b027a 1868
6c71277b 1869 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 1870
e547b070 1871 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
1872 goto error_testpoint;
1873 }
8ac94142 1874
1d4b027a
DG
1875 ret = lttcomm_listen_unix_sock(apps_sock);
1876 if (ret < 0) {
76d7553f 1877 goto error_listen;
1d4b027a
DG
1878 }
1879
5eb91c98
DG
1880 /*
1881 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1882 * more will be added to this poll set.
1883 */
d0b96690 1884 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1885 if (ret < 0) {
76d7553f 1886 goto error_create_poll;
5eb91c98 1887 }
273ea72c 1888
5eb91c98
DG
1889 /* Add the application registration socket */
1890 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1891 if (ret < 0) {
76d7553f 1892 goto error_poll_add;
5eb91c98 1893 }
273ea72c 1894
1d4b027a 1895 /* Notify all applications to register */
0fdd1e2c
DG
1896 ret = notify_ust_apps(1);
1897 if (ret < 0) {
1898 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1899 "Execution continues but there might be problem for already\n"
1900 "running applications that wishes to register.");
0fdd1e2c 1901 }
1d4b027a
DG
1902
1903 while (1) {
1904 DBG("Accepting application registration");
273ea72c
DG
1905
1906 /* Inifinite blocking call, waiting for transmission */
88f2b785 1907 restart:
a78af745 1908 health_poll_entry();
5eb91c98 1909 ret = lttng_poll_wait(&events, -1);
a78af745 1910 health_poll_exit();
273ea72c 1911 if (ret < 0) {
88f2b785
MD
1912 /*
1913 * Restart interrupted system call.
1914 */
1915 if (errno == EINTR) {
1916 goto restart;
1917 }
273ea72c
DG
1918 goto error;
1919 }
1920
0d9c5d77
DG
1921 nb_fd = ret;
1922
5eb91c98 1923 for (i = 0; i < nb_fd; i++) {
840cb59c 1924 health_code_update();
139ac872 1925
5eb91c98
DG
1926 /* Fetch once the poll data */
1927 revents = LTTNG_POLL_GETEV(&events, i);
1928 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1929
5eb91c98 1930 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1931 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1932 if (ret) {
139ac872
MD
1933 err = 0;
1934 goto exit;
90014c57 1935 }
1d4b027a 1936
5eb91c98
DG
1937 /* Event on the registration socket */
1938 if (pollfd == apps_sock) {
1939 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1940 ERR("Register apps socket poll error");
1941 goto error;
1942 } else if (revents & LPOLLIN) {
1943 sock = lttcomm_accept_unix_sock(apps_sock);
1944 if (sock < 0) {
1945 goto error;
1946 }
099e26bd 1947
16c5c8fa
DG
1948 /*
1949 * Set socket timeout for both receiving and ending.
1950 * app_socket_timeout is in seconds, whereas
1951 * lttcomm_setsockopt_rcv_timeout and
1952 * lttcomm_setsockopt_snd_timeout expect msec as
1953 * parameter.
1954 */
1955 (void) lttcomm_setsockopt_rcv_timeout(sock,
1956 app_socket_timeout * 1000);
1957 (void) lttcomm_setsockopt_snd_timeout(sock,
1958 app_socket_timeout * 1000);
1959
b662582b
DG
1960 /*
1961 * Set the CLOEXEC flag. Return code is useless because
1962 * either way, the show must go on.
1963 */
1964 (void) utils_set_fd_cloexec(sock);
1965
5eb91c98 1966 /* Create UST registration command for enqueuing */
ba7f0ae5 1967 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1968 if (ust_cmd == NULL) {
76d7553f 1969 PERROR("ust command zmalloc");
5eb91c98
DG
1970 goto error;
1971 }
1d4b027a 1972
5eb91c98
DG
1973 /*
1974 * Using message-based transmissions to ensure we don't
1975 * have to deal with partially received messages.
1976 */
4063050c
MD
1977 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1978 if (ret < 0) {
1979 ERR("Exhausted file descriptors allowed for applications.");
1980 free(ust_cmd);
1981 ret = close(sock);
1982 if (ret) {
1983 PERROR("close");
1984 }
1985 sock = -1;
1986 continue;
1987 }
d88aee68 1988
840cb59c 1989 health_code_update();
d0b96690
DG
1990 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
1991 if (ret < 0) {
5eb91c98 1992 free(ust_cmd);
d0b96690 1993 /* Close socket of the application. */
76d7553f
MD
1994 ret = close(sock);
1995 if (ret) {
1996 PERROR("close");
1997 }
4063050c 1998 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 1999 sock = -1;
5eb91c98
DG
2000 continue;
2001 }
840cb59c 2002 health_code_update();
099e26bd 2003
5eb91c98 2004 ust_cmd->sock = sock;
34a2494f 2005 sock = -1;
099e26bd 2006
5eb91c98
DG
2007 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2008 " gid:%d sock:%d name:%s (version %d.%d)",
2009 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2010 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2011 ust_cmd->sock, ust_cmd->reg_msg.name,
2012 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 2013
5eb91c98
DG
2014 /*
2015 * Lock free enqueue the registration request. The red pill
54d01ffb 2016 * has been taken! This apps will be part of the *system*.
5eb91c98
DG
2017 */
2018 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
2019
2020 /*
2021 * Wake the registration queue futex. Implicit memory
2022 * barrier with the exchange in cds_wfq_enqueue.
2023 */
2024 futex_nto1_wake(&ust_cmd_queue.futex);
2025 }
2026 }
90014c57 2027 }
1d4b027a
DG
2028 }
2029
139ac872 2030exit:
1d4b027a 2031error:
0fdd1e2c
DG
2032 /* Notify that the registration thread is gone */
2033 notify_ust_apps(0);
2034
a4b35e07 2035 if (apps_sock >= 0) {
76d7553f
MD
2036 ret = close(apps_sock);
2037 if (ret) {
2038 PERROR("close");
2039 }
a4b35e07 2040 }
46c3f085 2041 if (sock >= 0) {
76d7553f
MD
2042 ret = close(sock);
2043 if (ret) {
2044 PERROR("close");
2045 }
4063050c 2046 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 2047 }
273ea72c 2048 unlink(apps_unix_sock_path);
0fdd1e2c 2049
76d7553f 2050error_poll_add:
5eb91c98 2051 lttng_poll_clean(&events);
76d7553f
MD
2052error_listen:
2053error_create_poll:
6993eeb3 2054error_testpoint:
76d7553f 2055 DBG("UST Registration thread cleanup complete");
9ad42ec1
MD
2056 if (err) {
2057 health_error();
2058 ERR("Health error occurred in %s", __func__);
2059 }
8782cc74 2060 health_unregister(health_sessiond);
5eb91c98 2061
1d4b027a
DG
2062 return NULL;
2063}
2064
8c0faa1d 2065/*
3bd1e081 2066 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 2067 * exec or it will fails.
8c0faa1d 2068 */
3bd1e081 2069static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 2070{
a23ec3a7 2071 int ret, clock_ret;
ee0b0061
DG
2072 struct timespec timeout;
2073
a23ec3a7
DG
2074 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2075 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 2076
a23ec3a7
DG
2077 /* Setup pthread condition */
2078 ret = pthread_condattr_init(&consumer_data->condattr);
2079 if (ret != 0) {
2080 errno = ret;
2081 PERROR("pthread_condattr_init consumer data");
2082 goto error;
2083 }
2084
2085 /*
2086 * Set the monotonic clock in order to make sure we DO NOT jump in time
2087 * between the clock_gettime() call and the timedwait call. See bug #324
2088 * for a more details and how we noticed it.
2089 */
2090 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2091 if (ret != 0) {
2092 errno = ret;
2093 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
2094 goto error;
2095 }
8c0faa1d 2096
a23ec3a7
DG
2097 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2098 if (ret != 0) {
2099 errno = ret;
2100 PERROR("pthread_cond_init consumer data");
2101 goto error;
2102 }
2103
2104 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2105 consumer_data);
8c0faa1d 2106 if (ret != 0) {
3bd1e081 2107 PERROR("pthread_create consumer");
ee0b0061 2108 ret = -1;
8c0faa1d
DG
2109 goto error;
2110 }
2111
a23ec3a7
DG
2112 /* We are about to wait on a pthread condition */
2113 pthread_mutex_lock(&consumer_data->cond_mutex);
2114
ee0b0061 2115 /* Get time for sem_timedwait absolute timeout */
a23ec3a7
DG
2116 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2117 /*
2118 * Set the timeout for the condition timed wait even if the clock gettime
2119 * call fails since we might loop on that call and we want to avoid to
2120 * increment the timeout too many times.
2121 */
2122 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2123
2124 /*
2125 * The following loop COULD be skipped in some conditions so this is why we
2126 * set ret to 0 in order to make sure at least one round of the loop is
2127 * done.
2128 */
2129 ret = 0;
2130
2131 /*
2132 * Loop until the condition is reached or when a timeout is reached. Note
2133 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2134 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2135 * possible. This loop does not take any chances and works with both of
2136 * them.
2137 */
2138 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2139 if (clock_ret < 0) {
2140 PERROR("clock_gettime spawn consumer");
2141 /* Infinite wait for the consumerd thread to be ready */
2142 ret = pthread_cond_wait(&consumer_data->cond,
2143 &consumer_data->cond_mutex);
2144 } else {
2145 ret = pthread_cond_timedwait(&consumer_data->cond,
2146 &consumer_data->cond_mutex, &timeout);
2147 }
ee0b0061 2148 }
8c0faa1d 2149
a23ec3a7
DG
2150 /* Release the pthread condition */
2151 pthread_mutex_unlock(&consumer_data->cond_mutex);
2152
2153 if (ret != 0) {
2154 errno = ret;
2155 if (ret == ETIMEDOUT) {
4282f9a3
DG
2156 int pth_ret;
2157
ee0b0061
DG
2158 /*
2159 * Call has timed out so we kill the kconsumerd_thread and return
2160 * an error.
2161 */
a23ec3a7
DG
2162 ERR("Condition timed out. The consumer thread was never ready."
2163 " Killing it");
4282f9a3
DG
2164 pth_ret = pthread_cancel(consumer_data->thread);
2165 if (pth_ret < 0) {
3bd1e081 2166 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
2167 }
2168 } else {
a23ec3a7 2169 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061 2170 }
4282f9a3
DG
2171 /* Caller is expecting a negative value on failure. */
2172 ret = -1;
ee0b0061
DG
2173 goto error;
2174 }
2175
3bd1e081
MD
2176 pthread_mutex_lock(&consumer_data->pid_mutex);
2177 if (consumer_data->pid == 0) {
a23ec3a7 2178 ERR("Consumerd did not start");
3bd1e081 2179 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
2180 goto error;
2181 }
3bd1e081 2182 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 2183
8c0faa1d
DG
2184 return 0;
2185
2186error:
2187 return ret;
2188}
2189
d9800920 2190/*
3bd1e081 2191 * Join consumer thread
d9800920 2192 */
3bd1e081 2193static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
2194{
2195 void *status;
cf3af59e 2196
e8209f6b
DG
2197 /* Consumer pid must be a real one. */
2198 if (consumer_data->pid > 0) {
c617c0c6 2199 int ret;
3bd1e081 2200 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 2201 if (ret) {
3bd1e081 2202 ERR("Error killing consumer daemon");
cf3af59e
MD
2203 return ret;
2204 }
3bd1e081 2205 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
2206 } else {
2207 return 0;
2208 }
2209}
2210
8c0faa1d 2211/*
3bd1e081 2212 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 2213 *
d063d709 2214 * Return pid if successful else -1.
8c0faa1d 2215 */
3bd1e081 2216static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
2217{
2218 int ret;
2219 pid_t pid;
94c55f17 2220 const char *consumer_to_use;
53086306 2221 const char *verbosity;
94c55f17 2222 struct stat st;
8c0faa1d 2223
3bd1e081 2224 DBG("Spawning consumerd");
c49dc785 2225
8c0faa1d
DG
2226 pid = fork();
2227 if (pid == 0) {
2228 /*
3bd1e081 2229 * Exec consumerd.
8c0faa1d 2230 */
daee5345 2231 if (opt_verbose_consumer) {
53086306
DG
2232 verbosity = "--verbose";
2233 } else {
2234 verbosity = "--quiet";
2235 }
3bd1e081
MD
2236 switch (consumer_data->type) {
2237 case LTTNG_CONSUMER_KERNEL:
94c55f17 2238 /*
c7704d57
DG
2239 * Find out which consumerd to execute. We will first try the
2240 * 64-bit path, then the sessiond's installation directory, and
2241 * fallback on the 32-bit one,
94c55f17 2242 */
63a799e8
AM
2243 DBG3("Looking for a kernel consumer at these locations:");
2244 DBG3(" 1) %s", consumerd64_bin);
2245 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2246 DBG3(" 3) %s", consumerd32_bin);
94c55f17 2247 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 2248 DBG3("Found location #1");
94c55f17 2249 consumer_to_use = consumerd64_bin;
94c55f17 2250 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 2251 DBG3("Found location #2");
94c55f17 2252 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 2253 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 2254 DBG3("Found location #3");
eb1e0bd4 2255 consumer_to_use = consumerd32_bin;
94c55f17 2256 } else {
63a799e8 2257 DBG("Could not find any valid consumerd executable");
4282f9a3 2258 ret = -EINVAL;
94c55f17
AM
2259 break;
2260 }
2261 DBG("Using kernel consumer at: %s", consumer_to_use);
4282f9a3 2262 ret = execl(consumer_to_use,
94c55f17
AM
2263 "lttng-consumerd", verbosity, "-k",
2264 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2265 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2266 "--group", tracing_group_name,
94c55f17 2267 NULL);
3bd1e081 2268 break;
7753dea8
MD
2269 case LTTNG_CONSUMER64_UST:
2270 {
b1e0b6b6 2271 char *tmpnew = NULL;
8f4905da
MD
2272
2273 if (consumerd64_libdir[0] != '\0') {
2274 char *tmp;
2275 size_t tmplen;
2276
2277 tmp = getenv("LD_LIBRARY_PATH");
2278 if (!tmp) {
2279 tmp = "";
2280 }
2281 tmplen = strlen("LD_LIBRARY_PATH=")
2282 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2283 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2284 if (!tmpnew) {
2285 ret = -ENOMEM;
2286 goto error;
2287 }
2288 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2289 strcat(tmpnew, consumerd64_libdir);
2290 if (tmp[0] != '\0') {
2291 strcat(tmpnew, ":");
2292 strcat(tmpnew, tmp);
2293 }
2294 ret = putenv(tmpnew);
2295 if (ret) {
2296 ret = -errno;
c6f76da9 2297 free(tmpnew);
8f4905da
MD
2298 goto error;
2299 }
2300 }
94c55f17 2301 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 2302 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2303 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2304 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2305 "--group", tracing_group_name,
7753dea8 2306 NULL);
8f4905da
MD
2307 if (consumerd64_libdir[0] != '\0') {
2308 free(tmpnew);
2309 }
3bd1e081 2310 break;
7753dea8
MD
2311 }
2312 case LTTNG_CONSUMER32_UST:
2313 {
937dde8e 2314 char *tmpnew = NULL;
8f4905da
MD
2315
2316 if (consumerd32_libdir[0] != '\0') {
2317 char *tmp;
2318 size_t tmplen;
2319
2320 tmp = getenv("LD_LIBRARY_PATH");
2321 if (!tmp) {
2322 tmp = "";
2323 }
2324 tmplen = strlen("LD_LIBRARY_PATH=")
2325 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2326 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2327 if (!tmpnew) {
2328 ret = -ENOMEM;
2329 goto error;
2330 }
2331 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2332 strcat(tmpnew, consumerd32_libdir);
2333 if (tmp[0] != '\0') {
2334 strcat(tmpnew, ":");
2335 strcat(tmpnew, tmp);
2336 }
2337 ret = putenv(tmpnew);
2338 if (ret) {
2339 ret = -errno;
c6f76da9 2340 free(tmpnew);
8f4905da
MD
2341 goto error;
2342 }
2343 }
94c55f17 2344 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 2345 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2346 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2347 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2348 "--group", tracing_group_name,
7753dea8 2349 NULL);
8f4905da
MD
2350 if (consumerd32_libdir[0] != '\0') {
2351 free(tmpnew);
2352 }
7753dea8
MD
2353 break;
2354 }
3bd1e081 2355 default:
76d7553f 2356 PERROR("unknown consumer type");
3bd1e081
MD
2357 exit(EXIT_FAILURE);
2358 }
8c0faa1d 2359 if (errno != 0) {
4282f9a3 2360 PERROR("Consumer execl()");
8c0faa1d 2361 }
4282f9a3 2362 /* Reaching this point, we got a failure on our execl(). */
8c0faa1d
DG
2363 exit(EXIT_FAILURE);
2364 } else if (pid > 0) {
2365 ret = pid;
8c0faa1d 2366 } else {
76d7553f 2367 PERROR("start consumer fork");
8c0faa1d 2368 ret = -errno;
8c0faa1d 2369 }
8f4905da 2370error:
8c0faa1d
DG
2371 return ret;
2372}
2373
693bd40b 2374/*
3bd1e081 2375 * Spawn the consumerd daemon and session daemon thread.
693bd40b 2376 */
3bd1e081 2377static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 2378{
c617c0c6 2379 int ret;
edb8b045
DG
2380
2381 /*
2382 * Set the listen() state on the socket since there is a possible race
2383 * between the exec() of the consumer daemon and this call if place in the
2384 * consumer thread. See bug #366 for more details.
2385 */
2386 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2387 if (ret < 0) {
2388 goto error;
2389 }
693bd40b 2390
3bd1e081
MD
2391 pthread_mutex_lock(&consumer_data->pid_mutex);
2392 if (consumer_data->pid != 0) {
2393 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
2394 goto end;
2395 }
693bd40b 2396
3bd1e081 2397 ret = spawn_consumerd(consumer_data);
c49dc785 2398 if (ret < 0) {
3bd1e081
MD
2399 ERR("Spawning consumerd failed");
2400 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 2401 goto error;
693bd40b 2402 }
c49dc785 2403
3bd1e081
MD
2404 /* Setting up the consumer_data pid */
2405 consumer_data->pid = ret;
48842b30 2406 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 2407 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 2408
3bd1e081
MD
2409 DBG2("Spawning consumer control thread");
2410 ret = spawn_consumer_thread(consumer_data);
693bd40b 2411 if (ret < 0) {
3bd1e081 2412 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
2413 goto error;
2414 }
2415
c49dc785 2416end:
693bd40b
DG
2417 return 0;
2418
2419error:
331744e3 2420 /* Cleanup already created sockets on error. */
edb8b045 2421 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
2422 int err;
2423
edb8b045
DG
2424 err = close(consumer_data->err_sock);
2425 if (err < 0) {
2426 PERROR("close consumer data error socket");
2427 }
2428 }
693bd40b
DG
2429 return ret;
2430}
2431
b73401da 2432/*
096102bd 2433 * Setup necessary data for kernel tracer action.
b73401da 2434 */
096102bd 2435static int init_kernel_tracer(void)
b73401da
DG
2436{
2437 int ret;
b73401da 2438
096102bd
DG
2439 /* Modprobe lttng kernel modules */
2440 ret = modprobe_lttng_control();
b73401da 2441 if (ret < 0) {
b73401da
DG
2442 goto error;
2443 }
2444
096102bd
DG
2445 /* Open debugfs lttng */
2446 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2447 if (kernel_tracer_fd < 0) {
2448 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b
DG
2449 ret = -1;
2450 goto error_open;
54d01ffb
DG
2451 }
2452
2f77fc4b
DG
2453 /* Validate kernel version */
2454 ret = kernel_validate_version(kernel_tracer_fd);
2455 if (ret < 0) {
2456 goto error_version;
b551a063 2457 }
54d01ffb 2458
2f77fc4b
DG
2459 ret = modprobe_lttng_data();
2460 if (ret < 0) {
2461 goto error_modules;
54d01ffb
DG
2462 }
2463
2f77fc4b
DG
2464 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2465 return 0;
2466
2467error_version:
2468 modprobe_remove_lttng_control();
2469 ret = close(kernel_tracer_fd);
2470 if (ret) {
2471 PERROR("close");
b551a063 2472 }
2f77fc4b 2473 kernel_tracer_fd = -1;
f73fabfd 2474 return LTTNG_ERR_KERN_VERSION;
b551a063 2475
2f77fc4b
DG
2476error_modules:
2477 ret = close(kernel_tracer_fd);
2478 if (ret) {
2479 PERROR("close");
b551a063 2480 }
54d01ffb 2481
2f77fc4b
DG
2482error_open:
2483 modprobe_remove_lttng_control();
54d01ffb
DG
2484
2485error:
2f77fc4b
DG
2486 WARN("No kernel tracer available");
2487 kernel_tracer_fd = -1;
2488 if (!is_root) {
f73fabfd 2489 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2490 } else {
f73fabfd 2491 return LTTNG_ERR_KERN_NA;
2f77fc4b 2492 }
54d01ffb
DG
2493}
2494
2f77fc4b 2495
54d01ffb 2496/*
2f77fc4b
DG
2497 * Copy consumer output from the tracing session to the domain session. The
2498 * function also applies the right modification on a per domain basis for the
2499 * trace files destination directory.
36b588ed
MD
2500 *
2501 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2502 */
2f77fc4b 2503static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2504{
2505 int ret;
2f77fc4b
DG
2506 const char *dir_name;
2507 struct consumer_output *consumer;
2508
2509 assert(session);
2510 assert(session->consumer);
54d01ffb 2511
b551a063
DG
2512 switch (domain) {
2513 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2514 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2515 /*
2516 * XXX: We should audit the session creation and what this function
2517 * does "extra" in order to avoid a destroy since this function is used
2518 * in the domain session creation (kernel and ust) only. Same for UST
2519 * domain.
2520 */
2521 if (session->kernel_session->consumer) {
2522 consumer_destroy_output(session->kernel_session->consumer);
2523 }
2f77fc4b
DG
2524 session->kernel_session->consumer =
2525 consumer_copy_output(session->consumer);
2526 /* Ease our life a bit for the next part */
2527 consumer = session->kernel_session->consumer;
2528 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063 2529 break;
f20baf8e 2530 case LTTNG_DOMAIN_JUL:
b551a063 2531 case LTTNG_DOMAIN_UST:
2f77fc4b 2532 DBG3("Copying tracing session consumer output in UST session");
09a90bcd
DG
2533 if (session->ust_session->consumer) {
2534 consumer_destroy_output(session->ust_session->consumer);
2535 }
2f77fc4b
DG
2536 session->ust_session->consumer =
2537 consumer_copy_output(session->consumer);
2538 /* Ease our life a bit for the next part */
2539 consumer = session->ust_session->consumer;
2540 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2541 break;
2542 default:
f73fabfd 2543 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2544 goto error;
2545 }
2546
2f77fc4b 2547 /* Append correct directory to subdir */
c30ce0b3
CB
2548 strncat(consumer->subdir, dir_name,
2549 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2550 DBG3("Copy session consumer subdir %s", consumer->subdir);
2551
f73fabfd 2552 ret = LTTNG_OK;
54d01ffb
DG
2553
2554error:
2555 return ret;
2556}
2557
00e2e675 2558/*
2f77fc4b 2559 * Create an UST session and add it to the session ust list.
36b588ed
MD
2560 *
2561 * Should *NOT* be called with RCU read-side lock held.
00e2e675 2562 */
2f77fc4b
DG
2563static int create_ust_session(struct ltt_session *session,
2564 struct lttng_domain *domain)
00e2e675
DG
2565{
2566 int ret;
2f77fc4b 2567 struct ltt_ust_session *lus = NULL;
00e2e675 2568
a4b92340 2569 assert(session);
2f77fc4b
DG
2570 assert(domain);
2571 assert(session->consumer);
a4b92340 2572
2f77fc4b 2573 switch (domain->type) {
f20baf8e 2574 case LTTNG_DOMAIN_JUL:
2f77fc4b
DG
2575 case LTTNG_DOMAIN_UST:
2576 break;
2577 default:
2578 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2579 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2580 goto error;
2581 }
2582
2f77fc4b
DG
2583 DBG("Creating UST session");
2584
dec56f6c 2585 lus = trace_ust_create_session(session->id);
2f77fc4b 2586 if (lus == NULL) {
f73fabfd 2587 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2588 goto error;
2589 }
2590
2f77fc4b
DG
2591 lus->uid = session->uid;
2592 lus->gid = session->gid;
2bba9e53 2593 lus->output_traces = session->output_traces;
27babd3a 2594 lus->snapshot_mode = session->snapshot_mode;
ecc48a90 2595 lus->live_timer_interval = session->live_timer;
2f77fc4b 2596 session->ust_session = lus;
00e2e675 2597
2f77fc4b
DG
2598 /* Copy session output to the newly created UST session */
2599 ret = copy_session_consumer(domain->type, session);
f73fabfd 2600 if (ret != LTTNG_OK) {
00e2e675
DG
2601 goto error;
2602 }
2603
f73fabfd 2604 return LTTNG_OK;
00e2e675
DG
2605
2606error:
2f77fc4b
DG
2607 free(lus);
2608 session->ust_session = NULL;
00e2e675
DG
2609 return ret;
2610}
2611
2612/*
2f77fc4b 2613 * Create a kernel tracer session then create the default channel.
00e2e675 2614 */
2f77fc4b 2615static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2616{
2617 int ret;
a4b92340 2618
2f77fc4b 2619 DBG("Creating kernel session");
00e2e675 2620
2f77fc4b
DG
2621 ret = kernel_create_session(session, kernel_tracer_fd);
2622 if (ret < 0) {
f73fabfd 2623 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2624 goto error;
2625 }
2626
2f77fc4b
DG
2627 /* Code flow safety */
2628 assert(session->kernel_session);
2629
2630 /* Copy session output to the newly created Kernel session */
2631 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2632 if (ret != LTTNG_OK) {
a4b92340
DG
2633 goto error;
2634 }
2635
2f77fc4b
DG
2636 /* Create directory(ies) on local filesystem. */
2637 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2638 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2639 ret = run_as_mkdir_recursive(
2640 session->kernel_session->consumer->dst.trace_path,
2641 S_IRWXU | S_IRWXG, session->uid, session->gid);
2642 if (ret < 0) {
2643 if (ret != -EEXIST) {
2644 ERR("Trace directory creation error");
00e2e675
DG
2645 goto error;
2646 }
00e2e675 2647 }
2f77fc4b 2648 }
00e2e675 2649
2f77fc4b
DG
2650 session->kernel_session->uid = session->uid;
2651 session->kernel_session->gid = session->gid;
2bba9e53 2652 session->kernel_session->output_traces = session->output_traces;
27babd3a 2653 session->kernel_session->snapshot_mode = session->snapshot_mode;
00e2e675 2654
f73fabfd 2655 return LTTNG_OK;
00e2e675 2656
2f77fc4b
DG
2657error:
2658 trace_kernel_destroy_session(session->kernel_session);
2659 session->kernel_session = NULL;
2660 return ret;
2661}
00e2e675 2662
2f77fc4b
DG
2663/*
2664 * Count number of session permitted by uid/gid.
2665 */
2666static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2667{
2668 unsigned int i = 0;
2669 struct ltt_session *session;
07424f16 2670
2f77fc4b
DG
2671 DBG("Counting number of available session for UID %d GID %d",
2672 uid, gid);
2673 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
00e2e675 2674 /*
2f77fc4b 2675 * Only list the sessions the user can control.
00e2e675 2676 */
2f77fc4b
DG
2677 if (!session_access_ok(session, uid, gid)) {
2678 continue;
2679 }
2680 i++;
a4b92340 2681 }
2f77fc4b 2682 return i;
00e2e675
DG
2683}
2684
54d01ffb
DG
2685/*
2686 * Process the command requested by the lttng client within the command
2687 * context structure. This function make sure that the return structure (llm)
2688 * is set and ready for transmission before returning.
2689 *
2690 * Return any error encountered or 0 for success.
53a80697
MD
2691 *
2692 * "sock" is only used for special-case var. len data.
36b588ed
MD
2693 *
2694 * Should *NOT* be called with RCU read-side lock held.
54d01ffb 2695 */
53a80697
MD
2696static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2697 int *sock_error)
54d01ffb 2698{
f73fabfd 2699 int ret = LTTNG_OK;
44d3bd01 2700 int need_tracing_session = 1;
2e09ba09 2701 int need_domain;
54d01ffb
DG
2702
2703 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2704
53a80697
MD
2705 *sock_error = 0;
2706
2e09ba09
MD
2707 switch (cmd_ctx->lsm->cmd_type) {
2708 case LTTNG_CREATE_SESSION:
27babd3a 2709 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2710 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09
MD
2711 case LTTNG_DESTROY_SESSION:
2712 case LTTNG_LIST_SESSIONS:
2713 case LTTNG_LIST_DOMAINS:
2714 case LTTNG_START_TRACE:
2715 case LTTNG_STOP_TRACE:
6d805429 2716 case LTTNG_DATA_PENDING:
da3c9ec1
DG
2717 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2718 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2719 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2720 case LTTNG_SNAPSHOT_RECORD:
2e09ba09 2721 need_domain = 0;
3aace903 2722 break;
2e09ba09
MD
2723 default:
2724 need_domain = 1;
2725 }
2726
2727 if (opt_no_kernel && need_domain
2728 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 2729 if (!is_root) {
f73fabfd 2730 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 2731 } else {
f73fabfd 2732 ret = LTTNG_ERR_KERN_NA;
531d29f9 2733 }
4fba7219
DG
2734 goto error;
2735 }
2736
8d3113b2
DG
2737 /* Deny register consumer if we already have a spawned consumer. */
2738 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2739 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2740 if (kconsumer_data.pid > 0) {
f73fabfd 2741 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 2742 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
2743 goto error;
2744 }
2745 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2746 }
2747
54d01ffb
DG
2748 /*
2749 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 2750 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
2751 * command.
2752 */
2753 switch(cmd_ctx->lsm->cmd_type) {
2754 case LTTNG_LIST_SESSIONS:
2755 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2756 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
2757 case LTTNG_LIST_DOMAINS:
2758 case LTTNG_LIST_CHANNELS:
2759 case LTTNG_LIST_EVENTS:
2760 break;
2761 default:
2762 /* Setup lttng message with no payload */
2763 ret = setup_lttng_msg(cmd_ctx, 0);
2764 if (ret < 0) {
2765 /* This label does not try to unlock the session */
2766 goto init_setup_error;
2767 }
2768 }
2769
2770 /* Commands that DO NOT need a session. */
2771 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 2772 case LTTNG_CREATE_SESSION:
27babd3a 2773 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2774 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09 2775 case LTTNG_CALIBRATE:
54d01ffb
DG
2776 case LTTNG_LIST_SESSIONS:
2777 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2778 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 2779 need_tracing_session = 0;
54d01ffb
DG
2780 break;
2781 default:
2782 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
2783 /*
2784 * We keep the session list lock across _all_ commands
2785 * for now, because the per-session lock does not
2786 * handle teardown properly.
2787 */
74babd95 2788 session_lock_list();
54d01ffb
DG
2789 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2790 if (cmd_ctx->session == NULL) {
bba2d65f 2791 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
2792 goto error;
2793 } else {
2794 /* Acquire lock for the session */
2795 session_lock(cmd_ctx->session);
2796 }
2797 break;
2798 }
b389abbe 2799
2e09ba09
MD
2800 if (!need_domain) {
2801 goto skip_domain;
2802 }
a4b92340 2803
54d01ffb
DG
2804 /*
2805 * Check domain type for specific "pre-action".
2806 */
2807 switch (cmd_ctx->lsm->domain.type) {
2808 case LTTNG_DOMAIN_KERNEL:
d1f1c568 2809 if (!is_root) {
f73fabfd 2810 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
2811 goto error;
2812 }
2813
54d01ffb 2814 /* Kernel tracer check */
a4b35e07 2815 if (kernel_tracer_fd == -1) {
54d01ffb 2816 /* Basically, load kernel tracer modules */
096102bd
DG
2817 ret = init_kernel_tracer();
2818 if (ret != 0) {
54d01ffb
DG
2819 goto error;
2820 }
2821 }
5eb91c98 2822
5c827ce0
DG
2823 /* Consumer is in an ERROR state. Report back to client */
2824 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2825 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2826 goto error;
2827 }
2828
54d01ffb 2829 /* Need a session for kernel command */
44d3bd01 2830 if (need_tracing_session) {
54d01ffb 2831 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 2832 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 2833 if (ret < 0) {
f73fabfd 2834 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
2835 goto error;
2836 }
b389abbe 2837 }
7d29a247 2838
54d01ffb 2839 /* Start the kernel consumer daemon */
3bd1e081
MD
2840 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2841 if (kconsumer_data.pid == 0 &&
785d2d0d 2842 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3bd1e081
MD
2843 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2844 ret = start_consumerd(&kconsumer_data);
7d29a247 2845 if (ret < 0) {
f73fabfd 2846 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 2847 goto error;
950131af 2848 }
5c827ce0 2849 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
2850 } else {
2851 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 2852 }
173af62f 2853
a4b92340
DG
2854 /*
2855 * The consumer was just spawned so we need to add the socket to
2856 * the consumer output of the session if exist.
2857 */
2858 ret = consumer_create_socket(&kconsumer_data,
2859 cmd_ctx->session->kernel_session->consumer);
2860 if (ret < 0) {
2861 goto error;
173af62f 2862 }
0d0c377a 2863 }
5c827ce0 2864
54d01ffb 2865 break;
b9dfb167 2866 case LTTNG_DOMAIN_JUL:
2bdd86d4 2867 case LTTNG_DOMAIN_UST:
44d3bd01 2868 {
b51ec5b4
MD
2869 if (!ust_app_supported()) {
2870 ret = LTTNG_ERR_NO_UST;
2871 goto error;
2872 }
5c827ce0
DG
2873 /* Consumer is in an ERROR state. Report back to client */
2874 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2875 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2876 goto error;
2877 }
2878
44d3bd01 2879 if (need_tracing_session) {
a4b92340 2880 /* Create UST session if none exist. */
f6a9efaa 2881 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 2882 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 2883 &cmd_ctx->lsm->domain);
f73fabfd 2884 if (ret != LTTNG_OK) {
44d3bd01
DG
2885 goto error;
2886 }
2887 }
00e2e675 2888
7753dea8
MD
2889 /* Start the UST consumer daemons */
2890 /* 64-bit */
2891 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 2892 if (consumerd64_bin[0] != '\0' &&
7753dea8 2893 ustconsumer64_data.pid == 0 &&
785d2d0d 2894 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
2895 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2896 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 2897 if (ret < 0) {
f73fabfd 2898 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 2899 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
2900 goto error;
2901 }
48842b30 2902
173af62f 2903 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 2904 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 2905 } else {
7753dea8
MD
2906 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2907 }
173af62f
DG
2908
2909 /*
2910 * Setup socket for consumer 64 bit. No need for atomic access
2911 * since it was set above and can ONLY be set in this thread.
2912 */
a4b92340
DG
2913 ret = consumer_create_socket(&ustconsumer64_data,
2914 cmd_ctx->session->ust_session->consumer);
2915 if (ret < 0) {
2916 goto error;
173af62f
DG
2917 }
2918
7753dea8 2919 /* 32-bit */
fc7a59ce 2920 if (consumerd32_bin[0] != '\0' &&
7753dea8 2921 ustconsumer32_data.pid == 0 &&
785d2d0d 2922 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
2923 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2924 ret = start_consumerd(&ustconsumer32_data);
2925 if (ret < 0) {
f73fabfd 2926 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 2927 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
2928 goto error;
2929 }
5c827ce0 2930
173af62f 2931 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 2932 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
2933 } else {
2934 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 2935 }
173af62f
DG
2936
2937 /*
2938 * Setup socket for consumer 64 bit. No need for atomic access
2939 * since it was set above and can ONLY be set in this thread.
2940 */
a4b92340
DG
2941 ret = consumer_create_socket(&ustconsumer32_data,
2942 cmd_ctx->session->ust_session->consumer);
2943 if (ret < 0) {
2944 goto error;
173af62f 2945 }
44d3bd01
DG
2946 }
2947 break;
48842b30 2948 }
54d01ffb 2949 default:
54d01ffb
DG
2950 break;
2951 }
2e09ba09 2952skip_domain:
33a2b854 2953
5c827ce0
DG
2954 /* Validate consumer daemon state when start/stop trace command */
2955 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2956 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2957 switch (cmd_ctx->lsm->domain.type) {
b9dfb167 2958 case LTTNG_DOMAIN_JUL:
5c827ce0
DG
2959 case LTTNG_DOMAIN_UST:
2960 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2961 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2962 goto error;
2963 }
2964 break;
2965 case LTTNG_DOMAIN_KERNEL:
2966 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2967 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2968 goto error;
2969 }
2970 break;
2971 }
2972 }
2973
8e0af1b4
MD
2974 /*
2975 * Check that the UID or GID match that of the tracing session.
2976 * The root user can interact with all sessions.
2977 */
2978 if (need_tracing_session) {
2979 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
2980 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2981 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
f73fabfd 2982 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
2983 goto error;
2984 }
2985 }
2986
ffe60014
DG
2987 /*
2988 * Send relayd information to consumer as soon as we have a domain and a
2989 * session defined.
2990 */
2991 if (cmd_ctx->session && need_domain) {
2992 /*
2993 * Setup relayd if not done yet. If the relayd information was already
2994 * sent to the consumer, this call will gracefully return.
2995 */
2996 ret = cmd_setup_relayd(cmd_ctx->session);
2997 if (ret != LTTNG_OK) {
2998 goto error;
2999 }
3000 }
3001
54d01ffb
DG
3002 /* Process by command type */
3003 switch (cmd_ctx->lsm->cmd_type) {
3004 case LTTNG_ADD_CONTEXT:
3005 {
3006 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3007 cmd_ctx->lsm->u.context.channel_name,
979e618e 3008 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
54d01ffb
DG
3009 break;
3010 }
3011 case LTTNG_DISABLE_CHANNEL:
3012 {
3013 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3014 cmd_ctx->lsm->u.disable.channel_name);
3015 break;
3016 }
3017 case LTTNG_DISABLE_EVENT:
3018 {
3019 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3020 cmd_ctx->lsm->u.disable.channel_name,
3021 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
3022 break;
3023 }
54d01ffb
DG
3024 case LTTNG_DISABLE_ALL_EVENT:
3025 {
2bdd86d4 3026 DBG("Disabling all events");
54d01ffb
DG
3027
3028 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3029 cmd_ctx->lsm->u.disable.channel_name);
3030 break;
3031 }
3032 case LTTNG_ENABLE_CHANNEL:
3033 {
7972aab2 3034 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2f77fc4b 3035 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
54d01ffb
DG
3036 break;
3037 }
3038 case LTTNG_ENABLE_EVENT:
3039 {
882ef835
JI
3040 struct lttng_event_exclusion *exclusion = NULL;
3041 struct lttng_filter_bytecode *bytecode = NULL;
6b453b5e 3042 char *filter_expression = NULL;
882ef835 3043
67676bd8
DG
3044 /* Handle exclusion events and receive it from the client. */
3045 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3046 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3047
3048 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3049 (count * LTTNG_SYMBOL_NAME_LEN));
3050 if (!exclusion) {
3051 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3052 goto error;
882ef835 3053 }
882ef835 3054
67676bd8
DG
3055 DBG("Receiving var len exclusion event list from client ...");
3056 exclusion->count = count;
3057 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3058 count * LTTNG_SYMBOL_NAME_LEN);
3059 if (ret <= 0) {
3060 DBG("Nothing recv() from client var len data... continuing");
3061 *sock_error = 1;
3062 free(exclusion);
3063 ret = LTTNG_ERR_EXCLUSION_INVAL;
3064 goto error;
3065 }
3066 }
3067
6b453b5e
JG
3068 /* Get filter expression from client. */
3069 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3070 size_t expression_len =
3071 cmd_ctx->lsm->u.enable.expression_len;
3072
3073 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3074 ret = LTTNG_ERR_FILTER_INVAL;
3075 free(exclusion);
3076 goto error;
3077 }
3078
3079 filter_expression = zmalloc(expression_len);
3080 if (!filter_expression) {
3081 free(exclusion);
3082 ret = LTTNG_ERR_FILTER_NOMEM;
3083 goto error;
3084 }
3085
3086 /* Receive var. len. data */
3087 DBG("Receiving var len filter's expression from client ...");
3088 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3089 expression_len);
3090 if (ret <= 0) {
3091 DBG("Nothing recv() from client car len data... continuing");
3092 *sock_error = 1;
3093 free(filter_expression);
3094 free(exclusion);
3095 ret = LTTNG_ERR_FILTER_INVAL;
3096 goto error;
3097 }
3098 }
3099
67676bd8
DG
3100 /* Handle filter and get bytecode from client. */
3101 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3102 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3103
3104 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3105 ret = LTTNG_ERR_FILTER_INVAL;
3106 free(exclusion);
3107 goto error;
3108 }
3109
3110 bytecode = zmalloc(bytecode_len);
3111 if (!bytecode) {
3112 free(exclusion);
3113 ret = LTTNG_ERR_FILTER_NOMEM;
3114 goto error;
882ef835
JI
3115 }
3116
67676bd8
DG
3117 /* Receive var. len. data */
3118 DBG("Receiving var len filter's bytecode from client ...");
3119 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3120 if (ret <= 0) {
3121 DBG("Nothing recv() from client car len data... continuing");
3122 *sock_error = 1;
3123 free(bytecode);
3124 free(exclusion);
3125 ret = LTTNG_ERR_FILTER_INVAL;
3126 goto error;
3127 }
3128
3129 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3130 free(bytecode);
3131 free(exclusion);
3132 ret = LTTNG_ERR_FILTER_INVAL;
3133 goto error;
3134 }
882ef835 3135 }
67676bd8
DG
3136
3137 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3138 cmd_ctx->lsm->u.enable.channel_name,
6b453b5e
JG
3139 &cmd_ctx->lsm->u.enable.event,
3140 filter_expression, bytecode, exclusion,
67676bd8 3141 kernel_poll_pipe[1]);
54d01ffb
DG
3142 break;
3143 }
3144 case LTTNG_ENABLE_ALL_EVENT:
3145 {
2bdd86d4 3146 DBG("Enabling all events");
54d01ffb 3147
7972aab2 3148 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
8c9ae521 3149 cmd_ctx->lsm->u.enable.channel_name,
6b453b5e
JG
3150 cmd_ctx->lsm->u.enable.event.type, NULL, NULL,
3151 kernel_poll_pipe[1]);
54d01ffb
DG
3152 break;
3153 }
052da939 3154 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 3155 {
9f19cc17 3156 struct lttng_event *events;
54d01ffb 3157 ssize_t nb_events;
052da939 3158
54d01ffb
DG
3159 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3160 if (nb_events < 0) {
f73fabfd 3161 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3162 ret = -nb_events;
3163 goto error;
2ef84c95
DG
3164 }
3165
3166 /*
3167 * Setup lttng message with payload size set to the event list size in
3168 * bytes and then copy list into the llm payload.
3169 */
052da939 3170 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 3171 if (ret < 0) {
052da939 3172 free(events);
2ef84c95
DG
3173 goto setup_error;
3174 }
3175
3176 /* Copy event list into message payload */
9f19cc17 3177 memcpy(cmd_ctx->llm->payload, events,
052da939 3178 sizeof(struct lttng_event) * nb_events);
2ef84c95 3179
9f19cc17 3180 free(events);
2ef84c95 3181
f73fabfd 3182 ret = LTTNG_OK;
2ef84c95
DG
3183 break;
3184 }
f37d259d
MD
3185 case LTTNG_LIST_TRACEPOINT_FIELDS:
3186 {
3187 struct lttng_event_field *fields;
3188 ssize_t nb_fields;
3189
a4b92340
DG
3190 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3191 &fields);
f37d259d 3192 if (nb_fields < 0) {
f73fabfd 3193 /* Return value is a negative lttng_error_code. */
f37d259d
MD
3194 ret = -nb_fields;
3195 goto error;
3196 }
3197
3198 /*
3199 * Setup lttng message with payload size set to the event list size in
3200 * bytes and then copy list into the llm payload.
3201 */
a4b92340
DG
3202 ret = setup_lttng_msg(cmd_ctx,
3203 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
3204 if (ret < 0) {
3205 free(fields);
3206 goto setup_error;
3207 }
3208
3209 /* Copy event list into message payload */
3210 memcpy(cmd_ctx->llm->payload, fields,
3211 sizeof(struct lttng_event_field) * nb_fields);
3212
3213 free(fields);
3214
f73fabfd 3215 ret = LTTNG_OK;
f37d259d
MD
3216 break;
3217 }
00e2e675
DG
3218 case LTTNG_SET_CONSUMER_URI:
3219 {
a4b92340
DG
3220 size_t nb_uri, len;
3221 struct lttng_uri *uris;
3222
3223 nb_uri = cmd_ctx->lsm->u.uri.size;
3224 len = nb_uri * sizeof(struct lttng_uri);
3225
3226 if (nb_uri == 0) {
f73fabfd 3227 ret = LTTNG_ERR_INVALID;
a4b92340
DG
3228 goto error;
3229 }
3230
3231 uris = zmalloc(len);
3232 if (uris == NULL) {
f73fabfd 3233 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3234 goto error;
3235 }
3236
3237 /* Receive variable len data */
77c7c900 3238 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
3239 ret = lttcomm_recv_unix_sock(sock, uris, len);
3240 if (ret <= 0) {
3241 DBG("No URIs received from client... continuing");
3242 *sock_error = 1;
f73fabfd 3243 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3244 free(uris);
a4b92340
DG
3245 goto error;
3246 }
3247
00e2e675 3248 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
a4b92340 3249 nb_uri, uris);
f73fabfd 3250 if (ret != LTTNG_OK) {
b1d41407 3251 free(uris);
a4b92340
DG
3252 goto error;
3253 }
3254
3255 /*
3256 * XXX: 0 means that this URI should be applied on the session. Should
3257 * be a DOMAIN enuam.
3258 */
3259 if (cmd_ctx->lsm->domain.type == 0) {
3260 /* Add the URI for the UST session if a consumer is present. */
3261 if (cmd_ctx->session->ust_session &&
3262 cmd_ctx->session->ust_session->consumer) {
3263 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
3264 nb_uri, uris);
3265 } else if (cmd_ctx->session->kernel_session &&
3266 cmd_ctx->session->kernel_session->consumer) {
3267 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
3268 cmd_ctx->session, nb_uri, uris);
3269 }
3270 }
3271
b1d41407
DG
3272 free(uris);
3273
00e2e675
DG
3274 break;
3275 }
f3ed775e 3276 case LTTNG_START_TRACE:
8c0faa1d 3277 {
54d01ffb 3278 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
3279 break;
3280 }
f3ed775e 3281 case LTTNG_STOP_TRACE:
8c0faa1d 3282 {
54d01ffb 3283 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
3284 break;
3285 }
5e16da05
MD
3286 case LTTNG_CREATE_SESSION:
3287 {
a4b92340
DG
3288 size_t nb_uri, len;
3289 struct lttng_uri *uris = NULL;
3290
3291 nb_uri = cmd_ctx->lsm->u.uri.size;
3292 len = nb_uri * sizeof(struct lttng_uri);
3293
3294 if (nb_uri > 0) {
3295 uris = zmalloc(len);
3296 if (uris == NULL) {
f73fabfd 3297 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3298 goto error;
3299 }
3300
3301 /* Receive variable len data */
77c7c900 3302 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
3303 ret = lttcomm_recv_unix_sock(sock, uris, len);
3304 if (ret <= 0) {
3305 DBG("No URIs received from client... continuing");
3306 *sock_error = 1;
f73fabfd 3307 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3308 free(uris);
a4b92340
DG
3309 goto error;
3310 }
3311
3312 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3313 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 3314 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3315 free(uris);
a4b92340
DG
3316 goto error;
3317 }
3318 }
3319
3320 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
ecc48a90 3321 &cmd_ctx->creds, 0);
a4b92340 3322
b1d41407
DG
3323 free(uris);
3324
00e2e675
DG
3325 break;
3326 }
5e16da05
MD
3327 case LTTNG_DESTROY_SESSION:
3328 {
2f77fc4b 3329 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
a4b92340
DG
3330
3331 /* Set session to NULL so we do not unlock it after free. */
256a5576 3332 cmd_ctx->session = NULL;
5461b305 3333 break;
5e16da05 3334 }
9f19cc17 3335 case LTTNG_LIST_DOMAINS:
5e16da05 3336 {
54d01ffb
DG
3337 ssize_t nb_dom;
3338 struct lttng_domain *domains;
5461b305 3339
54d01ffb
DG
3340 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3341 if (nb_dom < 0) {
f73fabfd 3342 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3343 ret = -nb_dom;
3344 goto error;
ce3d728c 3345 }
520ff687 3346
54d01ffb 3347 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305 3348 if (ret < 0) {
ae9c20bc 3349 free(domains);
5461b305 3350 goto setup_error;
520ff687 3351 }
57167058 3352
54d01ffb
DG
3353 /* Copy event list into message payload */
3354 memcpy(cmd_ctx->llm->payload, domains,
3355 nb_dom * sizeof(struct lttng_domain));
3356
3357 free(domains);
5e16da05 3358
f73fabfd 3359 ret = LTTNG_OK;
5e16da05
MD
3360 break;
3361 }
9f19cc17 3362 case LTTNG_LIST_CHANNELS:
5e16da05 3363 {
6775595e 3364 int nb_chan;
54d01ffb
DG
3365 struct lttng_channel *channels;
3366
b551a063
DG
3367 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3368 cmd_ctx->session, &channels);
54d01ffb 3369 if (nb_chan < 0) {
f73fabfd 3370 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3371 ret = -nb_chan;
3372 goto error;
5461b305 3373 }
ca95a216 3374
54d01ffb 3375 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305 3376 if (ret < 0) {
ae9c20bc 3377 free(channels);
5461b305
DG
3378 goto setup_error;
3379 }
9f19cc17 3380
54d01ffb
DG
3381 /* Copy event list into message payload */
3382 memcpy(cmd_ctx->llm->payload, channels,
3383 nb_chan * sizeof(struct lttng_channel));
3384
3385 free(channels);
9f19cc17 3386
f73fabfd 3387 ret = LTTNG_OK;
5461b305 3388 break;
5e16da05 3389 }
9f19cc17 3390 case LTTNG_LIST_EVENTS:
5e16da05 3391 {
b551a063 3392 ssize_t nb_event;
684d34d2 3393 struct lttng_event *events = NULL;
9f19cc17 3394
b551a063 3395 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
3396 cmd_ctx->lsm->u.list.channel_name, &events);
3397 if (nb_event < 0) {
f73fabfd 3398 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3399 ret = -nb_event;
3400 goto error;
5461b305 3401 }
ca95a216 3402
54d01ffb 3403 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305 3404 if (ret < 0) {
ae9c20bc 3405 free(events);
5461b305
DG
3406 goto setup_error;
3407 }
9f19cc17 3408
54d01ffb
DG
3409 /* Copy event list into message payload */
3410 memcpy(cmd_ctx->llm->payload, events,
3411 nb_event * sizeof(struct lttng_event));
9f19cc17 3412
54d01ffb 3413 free(events);
9f19cc17 3414
f73fabfd 3415 ret = LTTNG_OK;
5461b305 3416 break;
5e16da05
MD
3417 }
3418 case LTTNG_LIST_SESSIONS:
3419 {
8e0af1b4 3420 unsigned int nr_sessions;
5461b305 3421
8e0af1b4 3422 session_lock_list();
730389d9
DG
3423 nr_sessions = lttng_sessions_count(
3424 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3425 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 3426
8e0af1b4 3427 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 3428 if (ret < 0) {
54d01ffb 3429 session_unlock_list();
5461b305 3430 goto setup_error;
e065084a 3431 }
5e16da05 3432
6c9cc2ab 3433 /* Filled the session array */
2f77fc4b 3434 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
3435 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3436 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 3437
54d01ffb 3438 session_unlock_list();
5e16da05 3439
f73fabfd 3440 ret = LTTNG_OK;
5e16da05
MD
3441 break;
3442 }
d0254c7c
MD
3443 case LTTNG_CALIBRATE:
3444 {
54d01ffb
DG
3445 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3446 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
3447 break;
3448 }
d9800920
DG
3449 case LTTNG_REGISTER_CONSUMER:
3450 {
2f77fc4b
DG
3451 struct consumer_data *cdata;
3452
3453 switch (cmd_ctx->lsm->domain.type) {
3454 case LTTNG_DOMAIN_KERNEL:
3455 cdata = &kconsumer_data;
3456 break;
3457 default:
f73fabfd 3458 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3459 goto error;
3460 }
3461
54d01ffb 3462 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 3463 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
3464 break;
3465 }
6d805429 3466 case LTTNG_DATA_PENDING:
806e2684 3467 {
6d805429 3468 ret = cmd_data_pending(cmd_ctx->session);
806e2684
DG
3469 break;
3470 }
da3c9ec1
DG
3471 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3472 {
6dc3064a
DG
3473 struct lttcomm_lttng_output_id reply;
3474
3475 ret = cmd_snapshot_add_output(cmd_ctx->session,
3476 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3477 if (ret != LTTNG_OK) {
3478 goto error;
3479 }
3480
3481 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3482 if (ret < 0) {
3483 goto setup_error;
3484 }
3485
3486 /* Copy output list into message payload */
3487 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3488 ret = LTTNG_OK;
da3c9ec1
DG
3489 break;
3490 }
3491 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3492 {
6dc3064a
DG
3493 ret = cmd_snapshot_del_output(cmd_ctx->session,
3494 &cmd_ctx->lsm->u.snapshot_output.output);
da3c9ec1
DG
3495 break;
3496 }
3497 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3498 {
6dc3064a
DG
3499 ssize_t nb_output;
3500 struct lttng_snapshot_output *outputs = NULL;
3501
3502 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3503 if (nb_output < 0) {
3504 ret = -nb_output;
3505 goto error;
3506 }
3507
3508 ret = setup_lttng_msg(cmd_ctx,
3509 nb_output * sizeof(struct lttng_snapshot_output));
3510 if (ret < 0) {
3511 free(outputs);
3512 goto setup_error;
3513 }
3514
3515 if (outputs) {
3516 /* Copy output list into message payload */
3517 memcpy(cmd_ctx->llm->payload, outputs,
3518 nb_output * sizeof(struct lttng_snapshot_output));
3519 free(outputs);
3520 }
3521
3522 ret = LTTNG_OK;
da3c9ec1
DG
3523 break;
3524 }
3525 case LTTNG_SNAPSHOT_RECORD:
3526 {
6dc3064a
DG
3527 ret = cmd_snapshot_record(cmd_ctx->session,
3528 &cmd_ctx->lsm->u.snapshot_record.output,
3529 cmd_ctx->lsm->u.snapshot_record.wait);
da3c9ec1
DG
3530 break;
3531 }
27babd3a
DG
3532 case LTTNG_CREATE_SESSION_SNAPSHOT:
3533 {
3534 size_t nb_uri, len;
3535 struct lttng_uri *uris = NULL;
3536
3537 nb_uri = cmd_ctx->lsm->u.uri.size;
3538 len = nb_uri * sizeof(struct lttng_uri);
3539
3540 if (nb_uri > 0) {
3541 uris = zmalloc(len);
3542 if (uris == NULL) {
3543 ret = LTTNG_ERR_FATAL;
3544 goto error;
3545 }
3546
3547 /* Receive variable len data */
3548 DBG("Waiting for %zu URIs from client ...", nb_uri);
3549 ret = lttcomm_recv_unix_sock(sock, uris, len);
3550 if (ret <= 0) {
3551 DBG("No URIs received from client... continuing");
3552 *sock_error = 1;
3553 ret = LTTNG_ERR_SESSION_FAIL;
3554 free(uris);
3555 goto error;
3556 }
3557
3558 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3559 DBG("Creating session with ONE network URI is a bad call");
3560 ret = LTTNG_ERR_SESSION_FAIL;
3561 free(uris);
3562 goto error;
3563 }
3564 }
3565
3566 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3567 nb_uri, &cmd_ctx->creds);
3568 free(uris);
3569 break;
3570 }
ecc48a90
JD
3571 case LTTNG_CREATE_SESSION_LIVE:
3572 {
3573 size_t nb_uri, len;
3574 struct lttng_uri *uris = NULL;
3575
3576 nb_uri = cmd_ctx->lsm->u.uri.size;
3577 len = nb_uri * sizeof(struct lttng_uri);
3578
3579 if (nb_uri > 0) {
3580 uris = zmalloc(len);
3581 if (uris == NULL) {
3582 ret = LTTNG_ERR_FATAL;
3583 goto error;
3584 }
3585
3586 /* Receive variable len data */
3587 DBG("Waiting for %zu URIs from client ...", nb_uri);
3588 ret = lttcomm_recv_unix_sock(sock, uris, len);
3589 if (ret <= 0) {
3590 DBG("No URIs received from client... continuing");
3591 *sock_error = 1;
3592 ret = LTTNG_ERR_SESSION_FAIL;
3593 free(uris);
3594 goto error;
3595 }
3596
3597 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3598 DBG("Creating session with ONE network URI is a bad call");
3599 ret = LTTNG_ERR_SESSION_FAIL;
3600 free(uris);
3601 goto error;
3602 }
3603 }
3604
3605 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3606 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3607 free(uris);
3608 break;
3609 }
5e16da05 3610 default:
f73fabfd 3611 ret = LTTNG_ERR_UND;
5461b305 3612 break;
fac6795d
DG
3613 }
3614
5461b305 3615error:
5461b305
DG
3616 if (cmd_ctx->llm == NULL) {
3617 DBG("Missing llm structure. Allocating one.");
894be886 3618 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
3619 goto setup_error;
3620 }
3621 }
54d01ffb 3622 /* Set return code */
5461b305 3623 cmd_ctx->llm->ret_code = ret;
5461b305 3624setup_error:
b5541356 3625 if (cmd_ctx->session) {
54d01ffb 3626 session_unlock(cmd_ctx->session);
b5541356 3627 }
256a5576
MD
3628 if (need_tracing_session) {
3629 session_unlock_list();
3630 }
54d01ffb 3631init_setup_error:
8028d920 3632 return ret;
fac6795d
DG
3633}
3634
44a5e5eb
DG
3635/*
3636 * Thread managing health check socket.
3637 */
3638static void *thread_manage_health(void *data)
3639{
eb4a2943 3640 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
3641 uint32_t revents, nb_fd;
3642 struct lttng_poll_event events;
0c89d795
MD
3643 struct health_comm_msg msg;
3644 struct health_comm_reply reply;
44a5e5eb
DG
3645
3646 DBG("[thread] Manage health check started");
3647
3648 rcu_register_thread();
3649
6d737ce4
DG
3650 /* We might hit an error path before this is created. */
3651 lttng_poll_init(&events);
3cc04881 3652
44a5e5eb
DG
3653 /* Create unix socket */
3654 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3655 if (sock < 0) {
3656 ERR("Unable to create health check Unix socket");
3657 ret = -1;
3658 goto error;
3659 }
3660
6c71277b
MD
3661 if (is_root) {
3662 /* lttng health client socket path permissions */
3663 ret = chown(health_unix_sock_path, 0,
3664 utils_get_group_id(tracing_group_name));
3665 if (ret < 0) {
3666 ERR("Unable to set group on %s", health_unix_sock_path);
3667 PERROR("chown");
3668 ret = -1;
3669 goto error;
3670 }
3671
3672 ret = chmod(health_unix_sock_path,
3673 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3674 if (ret < 0) {
3675 ERR("Unable to set permissions on %s", health_unix_sock_path);
3676 PERROR("chmod");
3677 ret = -1;
3678 goto error;
3679 }
3680 }
3681
b662582b
DG
3682 /*
3683 * Set the CLOEXEC flag. Return code is useless because either way, the
3684 * show must go on.
3685 */
3686 (void) utils_set_fd_cloexec(sock);
3687
44a5e5eb
DG
3688 ret = lttcomm_listen_unix_sock(sock);
3689 if (ret < 0) {
3690 goto error;
3691 }
3692
3693 /*
3694 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3695 * more will be added to this poll set.
3696 */
d0b96690 3697 ret = sessiond_set_thread_pollset(&events, 2);
44a5e5eb
DG
3698 if (ret < 0) {
3699 goto error;
3700 }
3701
3702 /* Add the application registration socket */
3703 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3704 if (ret < 0) {
3705 goto error;
3706 }
3707
97bc1426
MD
3708 lttng_sessiond_notify_ready();
3709
44a5e5eb
DG
3710 while (1) {
3711 DBG("Health check ready");
3712
44a5e5eb
DG
3713 /* Inifinite blocking call, waiting for transmission */
3714restart:
3715 ret = lttng_poll_wait(&events, -1);
3716 if (ret < 0) {
3717 /*
3718 * Restart interrupted system call.
3719 */
3720 if (errno == EINTR) {
3721 goto restart;
3722 }
3723 goto error;
3724 }
3725
0d9c5d77
DG
3726 nb_fd = ret;
3727
44a5e5eb
DG
3728 for (i = 0; i < nb_fd; i++) {
3729 /* Fetch once the poll data */
3730 revents = LTTNG_POLL_GETEV(&events, i);
3731 pollfd = LTTNG_POLL_GETFD(&events, i);
3732
3733 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 3734 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
44a5e5eb 3735 if (ret) {
139ac872
MD
3736 err = 0;
3737 goto exit;
44a5e5eb
DG
3738 }
3739
3740 /* Event on the registration socket */
3741 if (pollfd == sock) {
3742 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3743 ERR("Health socket poll error");
3744 goto error;
3745 }
3746 }
3747 }
3748
3749 new_sock = lttcomm_accept_unix_sock(sock);
3750 if (new_sock < 0) {
3751 goto error;
3752 }
3753
b662582b
DG
3754 /*
3755 * Set the CLOEXEC flag. Return code is useless because either way, the
3756 * show must go on.
3757 */
3758 (void) utils_set_fd_cloexec(new_sock);
3759
44a5e5eb
DG
3760 DBG("Receiving data from client for health...");
3761 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3762 if (ret <= 0) {
3763 DBG("Nothing recv() from client... continuing");
3764 ret = close(new_sock);
3765 if (ret) {
3766 PERROR("close");
3767 }
3768 new_sock = -1;
3769 continue;
3770 }
3771
3772 rcu_thread_online();
3773
53efb85a 3774 memset(&reply, 0, sizeof(reply));
6c71277b
MD
3775 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
3776 /*
3777 * health_check_state returns 0 if health is
3778 * bad.
3779 */
3780 if (!health_check_state(health_sessiond, i)) {
3781 reply.ret_code |= 1ULL << i;
3782 }
44a5e5eb
DG
3783 }
3784
6c71277b 3785 DBG2("Health check return value %" PRIx64, reply.ret_code);
44a5e5eb
DG
3786
3787 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3788 if (ret < 0) {
3789 ERR("Failed to send health data back to client");
3790 }
3791
3792 /* End of transmission */
3793 ret = close(new_sock);
3794 if (ret) {
3795 PERROR("close");
3796 }
3797 new_sock = -1;
3798 }
3799
139ac872 3800exit:
44a5e5eb 3801error:
139ac872
MD
3802 if (err) {
3803 ERR("Health error occurred in %s", __func__);
3804 }
44a5e5eb
DG
3805 DBG("Health check thread dying");
3806 unlink(health_unix_sock_path);
3807 if (sock >= 0) {
3808 ret = close(sock);
3809 if (ret) {
3810 PERROR("close");
3811 }
3812 }
44a5e5eb
DG
3813
3814 lttng_poll_clean(&events);
3815
3816 rcu_unregister_thread();
3817 return NULL;
3818}
3819
1d4b027a 3820/*
d063d709
DG
3821 * This thread manage all clients request using the unix client socket for
3822 * communication.
1d4b027a
DG
3823 */
3824static void *thread_manage_clients(void *data)
3825{
139ac872 3826 int sock = -1, ret, i, pollfd, err = -1;
53a80697 3827 int sock_error;
5eb91c98 3828 uint32_t revents, nb_fd;
273ea72c 3829 struct command_ctx *cmd_ctx = NULL;
5eb91c98 3830 struct lttng_poll_event events;
1d4b027a
DG
3831
3832 DBG("[thread] Manage client started");
3833
f6a9efaa
DG
3834 rcu_register_thread();
3835
6c71277b 3836 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
927ca06a 3837
840cb59c 3838 health_code_update();
44a5e5eb 3839
1d4b027a
DG
3840 ret = lttcomm_listen_unix_sock(client_sock);
3841 if (ret < 0) {
35df2755 3842 goto error_listen;
1d4b027a
DG
3843 }
3844
5eb91c98
DG
3845 /*
3846 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3847 * more will be added to this poll set.
3848 */
d0b96690 3849 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 3850 if (ret < 0) {
35df2755 3851 goto error_create_poll;
5eb91c98 3852 }
273ea72c 3853
5eb91c98
DG
3854 /* Add the application registration socket */
3855 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3856 if (ret < 0) {
3857 goto error;
3858 }
273ea72c 3859
97bc1426 3860 lttng_sessiond_notify_ready();
0bb7724a 3861
72453c8c
MD
3862 /* This testpoint is after we signal readiness to the parent. */
3863 if (testpoint(sessiond_thread_manage_clients)) {
3864 goto error;
3865 }
3866
e547b070 3867 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
6993eeb3
CB
3868 goto error;
3869 }
8ac94142 3870
840cb59c 3871 health_code_update();
44a5e5eb 3872
1d4b027a 3873 while (1) {
1d4b027a 3874 DBG("Accepting client command ...");
273ea72c
DG
3875
3876 /* Inifinite blocking call, waiting for transmission */
88f2b785 3877 restart:
a78af745 3878 health_poll_entry();
5eb91c98 3879 ret = lttng_poll_wait(&events, -1);
a78af745 3880 health_poll_exit();
273ea72c 3881 if (ret < 0) {
88f2b785
MD
3882 /*
3883 * Restart interrupted system call.
3884 */
3885 if (errno == EINTR) {
3886 goto restart;
3887 }
273ea72c
DG
3888 goto error;
3889 }
3890
0d9c5d77
DG
3891 nb_fd = ret;
3892
5eb91c98
DG
3893 for (i = 0; i < nb_fd; i++) {
3894 /* Fetch once the poll data */
3895 revents = LTTNG_POLL_GETEV(&events, i);
3896 pollfd = LTTNG_POLL_GETFD(&events, i);
3897
840cb59c 3898 health_code_update();
44a5e5eb 3899
5eb91c98 3900 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 3901 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 3902 if (ret) {
139ac872
MD
3903 err = 0;
3904 goto exit;
5eb91c98
DG
3905 }
3906
3907 /* Event on the registration socket */
3908 if (pollfd == client_sock) {
3909 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3910 ERR("Client socket poll error");
3911 goto error;
3912 }
3913 }
3914 }
3915
3916 DBG("Wait for client response");
3917
840cb59c 3918 health_code_update();
44a5e5eb 3919
5eb91c98
DG
3920 sock = lttcomm_accept_unix_sock(client_sock);
3921 if (sock < 0) {
273ea72c 3922 goto error;
273ea72c
DG
3923 }
3924
b662582b
DG
3925 /*
3926 * Set the CLOEXEC flag. Return code is useless because either way, the
3927 * show must go on.
3928 */
3929 (void) utils_set_fd_cloexec(sock);
3930
be040666
DG
3931 /* Set socket option for credentials retrieval */
3932 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3933 if (ret < 0) {
3934 goto error;
3935 }
3936
5eb91c98 3937 /* Allocate context command to process the client request */
ba7f0ae5 3938 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 3939 if (cmd_ctx == NULL) {
76d7553f 3940 PERROR("zmalloc cmd_ctx");
1d4b027a 3941 goto error;
5eb91c98 3942 }
1d4b027a 3943
5eb91c98 3944 /* Allocate data buffer for reception */
ba7f0ae5 3945 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 3946 if (cmd_ctx->lsm == NULL) {
76d7553f 3947 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
3948 goto error;
3949 }
1d4b027a 3950
5eb91c98
DG
3951 cmd_ctx->llm = NULL;
3952 cmd_ctx->session = NULL;
1d4b027a 3953
840cb59c 3954 health_code_update();
44a5e5eb 3955
5eb91c98
DG
3956 /*
3957 * Data is received from the lttng client. The struct
3958 * lttcomm_session_msg (lsm) contains the command and data request of
3959 * the client.
3960 */
3961 DBG("Receiving data from client ...");
be040666
DG
3962 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3963 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
3964 if (ret <= 0) {
3965 DBG("Nothing recv() from client... continuing");
76d7553f
MD
3966 ret = close(sock);
3967 if (ret) {
3968 PERROR("close");
3969 }
3970 sock = -1;
a2c0da86 3971 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
3972 continue;
3973 }
1d4b027a 3974
840cb59c 3975 health_code_update();
44a5e5eb 3976
5eb91c98
DG
3977 // TODO: Validate cmd_ctx including sanity check for
3978 // security purpose.
f7776ea7 3979
f6a9efaa 3980 rcu_thread_online();
5eb91c98
DG
3981 /*
3982 * This function dispatch the work to the kernel or userspace tracer
3983 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3984 * informations for the client. The command context struct contains
3985 * everything this function may needs.
3986 */
53a80697 3987 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 3988 rcu_thread_offline();
5eb91c98 3989 if (ret < 0) {
36134aa1
DG
3990 ret = close(sock);
3991 if (ret) {
3992 PERROR("close");
53a80697 3993 }
36134aa1 3994 sock = -1;
bbd973c2 3995 /*
5eb91c98 3996 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 3997 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
3998 * (ENOMEM). Error detected but still accept
3999 * command, unless a socket error has been
4000 * detected.
bbd973c2 4001 */
bbd973c2 4002 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4003 continue;
4004 }
d6e4fca4 4005
840cb59c 4006 health_code_update();
44a5e5eb 4007
54d01ffb
DG
4008 DBG("Sending response (size: %d, retcode: %s)",
4009 cmd_ctx->lttng_msg_size,
9a745bc7 4010 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 4011 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
4012 if (ret < 0) {
4013 ERR("Failed to send data back to client");
bbd973c2 4014 }
1d4b027a 4015
5eb91c98 4016 /* End of transmission */
76d7553f
MD
4017 ret = close(sock);
4018 if (ret) {
4019 PERROR("close");
4020 }
4021 sock = -1;
be040666
DG
4022
4023 clean_command_ctx(&cmd_ctx);
44a5e5eb 4024
840cb59c 4025 health_code_update();
273ea72c
DG
4026 }
4027
139ac872 4028exit:
5eb91c98 4029error:
35df2755
CB
4030 if (sock >= 0) {
4031 ret = close(sock);
4032 if (ret) {
4033 PERROR("close");
4034 }
139ac872 4035 }
44a5e5eb 4036
35df2755
CB
4037 lttng_poll_clean(&events);
4038 clean_command_ctx(&cmd_ctx);
4039
4040error_listen:
4041error_create_poll:
273ea72c 4042 unlink(client_unix_sock_path);
76d7553f
MD
4043 if (client_sock >= 0) {
4044 ret = close(client_sock);
4045 if (ret) {
4046 PERROR("close");
4047 }
a4b35e07 4048 }
35df2755
CB
4049
4050 if (err) {
840cb59c 4051 health_error();
35df2755 4052 ERR("Health error occurred in %s", __func__);
a4b35e07 4053 }
273ea72c 4054
8782cc74 4055 health_unregister(health_sessiond);
35df2755
CB
4056
4057 DBG("Client thread dying");
f6a9efaa
DG
4058
4059 rcu_unregister_thread();
1d4b027a
DG
4060 return NULL;
4061}
4062
4063
fac6795d
DG
4064/*
4065 * usage function on stderr
4066 */
4067static void usage(void)
4068{
b716ce68 4069 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
4070 fprintf(stderr, " -h, --help Display this usage.\n");
4071 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
4072 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4073 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4074 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
4075 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4076 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4077 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4078 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
4079 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4080 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4081 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4082 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150 4083 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
72dd7491 4084 fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n");
d6f42150
DG
4085 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4086 fprintf(stderr, " -V, --version Show version number.\n");
7fe6d2c5 4087 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
d6f42150
DG
4088 fprintf(stderr, " -q, --quiet No output at all.\n");
4089 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
35f90c40 4090 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3bd1e081 4091 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 4092 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
4d076222 4093 fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n");
26296c48 4094 fprintf(stderr, " -f --config Load daemon configuration file\n");
fac6795d
DG
4095}
4096
4097/*
26296c48
JG
4098 * Take an option from the getopt output and set it in the right variable to be
4099 * used later.
4100 *
4101 * Return 0 on success else a negative value.
fac6795d 4102 */
26296c48 4103static int set_option(int opt, const char *arg, const char *optname)
fac6795d 4104{
26296c48 4105 int ret = 0;
fac6795d 4106
26296c48
JG
4107 switch (opt) {
4108 case 0:
4109 fprintf(stderr, "option %s", optname);
4110 if (arg) {
4111 fprintf(stderr, " with arg %s\n", arg);
fac6795d 4112 }
26296c48
JG
4113 break;
4114 case 'c':
4115 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4116 break;
4117 case 'a':
4118 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4119 break;
4120 case 'd':
4121 opt_daemon = 1;
4122 break;
72dd7491
MD
4123 case 'b':
4124 opt_background = 1;
4125 break;
26296c48
JG
4126 case 'g':
4127 tracing_group_name = strdup(arg);
4128 break;
4129 case 'h':
4130 usage();
4131 exit(EXIT_FAILURE);
4132 case 'V':
4133 fprintf(stdout, "%s\n", VERSION);
4134 exit(EXIT_SUCCESS);
4135 case 'S':
4136 opt_sig_parent = 1;
4137 break;
4138 case 'E':
4139 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4140 break;
4141 case 'C':
4142 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4143 break;
4144 case 'F':
4145 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4146 break;
4147 case 'D':
4148 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4149 break;
4150 case 'H':
4151 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4152 break;
4153 case 'G':
4154 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4155 break;
4156 case 'N':
4157 opt_no_kernel = 1;
4158 break;
4159 case 'q':
4160 lttng_opt_quiet = 1;
4161 break;
4162 case 'v':
4163 /* Verbose level can increase using multiple -v */
4164 if (arg) {
4165 lttng_opt_verbose = config_parse_value(arg);
4166 } else {
97e19046 4167 lttng_opt_verbose += 1;
26296c48
JG
4168 }
4169 break;
4170 case 'Z':
4171 if (arg) {
4172 opt_verbose_consumer = config_parse_value(arg);
4173 } else {
3bd1e081 4174 opt_verbose_consumer += 1;
26296c48
JG
4175 }
4176 break;
4177 case 'u':
4178 consumerd32_bin = strdup(arg);
4179 consumerd32_bin_override = 1;
4180 break;
4181 case 'U':
4182 consumerd32_libdir = strdup(arg);
4183 consumerd32_libdir_override = 1;
4184 break;
4185 case 't':
4186 consumerd64_bin = strdup(arg);
4187 consumerd64_bin_override = 1;
4188 break;
4189 case 'T':
4190 consumerd64_libdir = strdup(arg);
4191 consumerd64_libdir_override = 1;
4192 break;
4193 case 'p':
4194 opt_pidfile = strdup(arg);
4195 break;
4196 case 'J': /* JUL TCP port. */
4197 {
4198 unsigned long v;
4d076222 4199
26296c48
JG
4200 errno = 0;
4201 v = strtoul(arg, NULL, 0);
4202 if (errno != 0 || !isdigit(arg[0])) {
4203 ERR("Wrong value in --jul-tcp-port parameter: %s", arg);
4204 return -1;
4205 }
4206 if (v == 0 || v >= 65535) {
4207 ERR("Port overflow in --jul-tcp-port parameter: %s", arg);
4208 return -1;
4209 }
4210 jul_tcp_port = (uint32_t) v;
4211 DBG3("JUL TCP port set to non default: %u", jul_tcp_port);
4212 break;
4213 }
4214 default:
4215 /* Unknown option or other error.
4216 * Error is printed by getopt, just return */
4217 ret = -1;
4218 }
4219
4220 return ret;
4221}
4222
4223/*
4224 * config_entry_handler_cb used to handle options read from a config file.
4225 * See config_entry_handler_cb comment in common/config/config.h for the
4226 * return value conventions.
4227 */
4228static int config_entry_handler(const struct config_entry *entry, void *unused)
4229{
4230 int ret = 0, i;
4231
4232 if (!entry || !entry->name || !entry->value) {
4233 ret = -EINVAL;
4234 goto end;
4235 }
4236
4237 /* Check if the option is to be ignored */
4238 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4239 if (!strcmp(entry->name, config_ignore_options[i])) {
4240 goto end;
4241 }
4242 }
4243
4244 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4245 i++) {
4246
4247 /* Ignore if not fully matched. */
4248 if (strcmp(entry->name, long_options[i].name)) {
4249 continue;
4250 }
4251
4252 /*
4253 * If the option takes no argument on the command line, we have to
4254 * check if the value is "true". We support non-zero numeric values,
4255 * true, on and yes.
4256 */
4257 if (!long_options[i].has_arg) {
4258 ret = config_parse_value(entry->value);
4259 if (ret <= 0) {
4260 if (ret) {
4261 WARN("Invalid configuration value \"%s\" for option %s",
4262 entry->value, entry->name);
4263 }
4264 /* False, skip boolean config option. */
4265 goto end;
4d076222 4266 }
26296c48
JG
4267 }
4268
4269 ret = set_option(long_options[i].val, entry->value, entry->name);
4270 goto end;
4271 }
4272
4273 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4274
4275end:
4276 return ret;
4277}
4278
4279/*
4280 * daemon configuration loading and argument parsing
4281 */
4282static int set_options(int argc, char **argv)
4283{
4284 int ret = 0, c = 0, option_index = 0;
4285 int orig_optopt = optopt, orig_optind = optind;
4286 char *optstring;
4287 const char *config_path = NULL;
4288
4289 optstring = utils_generate_optstring(long_options,
4290 sizeof(long_options) / sizeof(struct option));
4291 if (!optstring) {
4292 ret = -ENOMEM;
4293 goto end;
4294 }
4295
4296 /* Check for the --config option */
4297 while ((c = getopt_long(argc, argv, optstring, long_options,
4298 &option_index)) != -1) {
4299 if (c == '?') {
4300 ret = -EINVAL;
4301 goto end;
4302 } else if (c != 'f') {
4303 /* if not equal to --config option. */
4304 continue;
4305 }
4306
4307 config_path = utils_expand_path(optarg);
4308 if (!config_path) {
4309 ERR("Failed to resolve path: %s", optarg);
4310 }
4311 }
4312
4313 ret = config_get_section_entries(config_path, config_section_name,
4314 config_entry_handler, NULL);
4315 if (ret) {
4316 if (ret > 0) {
4317 ERR("Invalid configuration option at line %i", ret);
4318 ret = -1;
4319 }
4320 goto end;
4321 }
4322
4323 /* Reset getopt's global state */
4324 optopt = orig_optopt;
4325 optind = orig_optind;
4326 while (1) {
4327 c = getopt_long(argc, argv, optstring, long_options, &option_index);
4328 if (c == -1) {
4d076222
DG
4329 break;
4330 }
26296c48
JG
4331
4332 ret = set_option(c, optarg, long_options[option_index].name);
4333 if (ret < 0) {
4334 break;
fac6795d
DG
4335 }
4336 }
4337
26296c48
JG
4338end:
4339 free(optstring);
4340 return ret;
fac6795d
DG
4341}
4342
4343/*
d063d709 4344 * Creates the two needed socket by the daemon.
d6f42150
DG
4345 * apps_sock - The communication socket for all UST apps.
4346 * client_sock - The communication of the cli tool (lttng).
fac6795d 4347 */
cf3af59e 4348static int init_daemon_socket(void)
fac6795d
DG
4349{
4350 int ret = 0;
4351 mode_t old_umask;
4352
4353 old_umask = umask(0);
4354
4355 /* Create client tool unix socket */
d6f42150
DG
4356 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4357 if (client_sock < 0) {
4358 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
4359 ret = -1;
4360 goto end;
4361 }
4362
b662582b
DG
4363 /* Set the cloexec flag */
4364 ret = utils_set_fd_cloexec(client_sock);
4365 if (ret < 0) {
4366 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4367 "Continuing but note that the consumer daemon will have a "
4368 "reference to this socket on exec()", client_sock);
4369 }
4370
fac6795d
DG
4371 /* File permission MUST be 660 */
4372 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4373 if (ret < 0) {
d6f42150 4374 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 4375 PERROR("chmod");
fac6795d
DG
4376 goto end;
4377 }
4378
4379 /* Create the application unix socket */
d6f42150
DG
4380 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4381 if (apps_sock < 0) {
4382 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
4383 ret = -1;
4384 goto end;
4385 }
4386
b662582b
DG
4387 /* Set the cloexec flag */
4388 ret = utils_set_fd_cloexec(apps_sock);
4389 if (ret < 0) {
4390 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4391 "Continuing but note that the consumer daemon will have a "
4392 "reference to this socket on exec()", apps_sock);
4393 }
4394
d6f42150 4395 /* File permission MUST be 666 */
54d01ffb
DG
4396 ret = chmod(apps_unix_sock_path,
4397 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 4398 if (ret < 0) {
d6f42150 4399 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 4400 PERROR("chmod");
fac6795d
DG
4401 goto end;
4402 }
4403
b662582b
DG
4404 DBG3("Session daemon client socket %d and application socket %d created",
4405 client_sock, apps_sock);
4406
fac6795d
DG
4407end:
4408 umask(old_umask);
4409 return ret;
4410}
4411
4412/*
54d01ffb
DG
4413 * Check if the global socket is available, and if a daemon is answering at the
4414 * other side. If yes, error is returned.
fac6795d 4415 */
cf3af59e 4416static int check_existing_daemon(void)
fac6795d 4417{
7d8234d9 4418 /* Is there anybody out there ? */
099e26bd 4419 if (lttng_session_daemon_alive()) {
7d8234d9 4420 return -EEXIST;
099e26bd 4421 }
b09c7c76
DG
4422
4423 return 0;
fac6795d
DG
4424}
4425
fac6795d 4426/*
d063d709 4427 * Set the tracing group gid onto the client socket.
5e16da05 4428 *
d063d709 4429 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 4430 * permissive (root.root) to less permissive (root.tracing).
fac6795d 4431 */
be040666 4432static int set_permissions(char *rundir)
fac6795d
DG
4433{
4434 int ret;
996b65c8 4435 gid_t gid;
fac6795d 4436
6c71277b 4437 gid = utils_get_group_id(tracing_group_name);
fac6795d 4438
d6f42150 4439 /* Set lttng run dir */
be040666 4440 ret = chown(rundir, 0, gid);
d6f42150 4441 if (ret < 0) {
be040666 4442 ERR("Unable to set group on %s", rundir);
76d7553f 4443 PERROR("chown");
d6f42150
DG
4444 }
4445
6c71277b
MD
4446 /*
4447 * Ensure all applications and tracing group can search the run
4448 * dir. Allow everyone to read the directory, since it does not
4449 * buy us anything to hide its content.
4450 */
4451 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
4452 if (ret < 0) {
4453 ERR("Unable to set permissions on %s", rundir);
76d7553f 4454 PERROR("chmod");
d1613cf5
JN
4455 }
4456
d6f42150 4457 /* lttng client socket path */
996b65c8 4458 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 4459 if (ret < 0) {
d6f42150 4460 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 4461 PERROR("chown");
d6f42150
DG
4462 }
4463
3bd1e081 4464 /* kconsumer error socket path */
6c71277b 4465 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 4466 if (ret < 0) {
3bd1e081 4467 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 4468 PERROR("chown");
3bd1e081
MD
4469 }
4470
7753dea8 4471 /* 64-bit ustconsumer error socket path */
6c71277b 4472 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
4473 if (ret < 0) {
4474 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 4475 PERROR("chown");
7753dea8
MD
4476 }
4477
4478 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 4479 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 4480 if (ret < 0) {
7753dea8 4481 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 4482 PERROR("chown");
fac6795d
DG
4483 }
4484
d6f42150 4485 DBG("All permissions are set");
e07ae692 4486
fac6795d
DG
4487 return ret;
4488}
4489
d6f42150 4490/*
d063d709 4491 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 4492 */
67e40797 4493static int create_lttng_rundir(const char *rundir)
d6f42150
DG
4494{
4495 int ret;
4496
67e40797
DG
4497 DBG3("Creating LTTng run directory: %s", rundir);
4498
d1613cf5 4499 ret = mkdir(rundir, S_IRWXU);
d6f42150 4500 if (ret < 0) {
b1f11e69 4501 if (errno != EEXIST) {
67e40797 4502 ERR("Unable to create %s", rundir);
b1f11e69
DG
4503 goto error;
4504 } else {
4505 ret = 0;
4506 }
d6f42150
DG
4507 }
4508
4509error:
4510 return ret;
4511}
4512
4513/*
d063d709
DG
4514 * Setup sockets and directory needed by the kconsumerd communication with the
4515 * session daemon.
d6f42150 4516 */
67e40797
DG
4517static int set_consumer_sockets(struct consumer_data *consumer_data,
4518 const char *rundir)
d6f42150
DG
4519{
4520 int ret;
67e40797 4521 char path[PATH_MAX];
d6f42150 4522
6c71277b 4523 switch (consumer_data->type) {
7753dea8 4524 case LTTNG_CONSUMER_KERNEL:
60922cb0 4525 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
4526 break;
4527 case LTTNG_CONSUMER64_UST:
60922cb0 4528 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
4529 break;
4530 case LTTNG_CONSUMER32_UST:
60922cb0 4531 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
4532 break;
4533 default:
4534 ERR("Consumer type unknown");
4535 ret = -EINVAL;
4536 goto error;
d6f42150
DG
4537 }
4538
67e40797
DG
4539 DBG2("Creating consumer directory: %s", path);
4540
6c71277b 4541 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
d6f42150 4542 if (ret < 0) {
6beb2242 4543 if (errno != EEXIST) {
f11e84c2 4544 PERROR("mkdir");
3bd1e081 4545 ERR("Failed to create %s", path);
6beb2242
DG
4546 goto error;
4547 }
f11e84c2 4548 ret = -1;
d6f42150 4549 }
6c71277b
MD
4550 if (is_root) {
4551 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
4552 if (ret < 0) {
4553 ERR("Unable to set group on %s", path);
4554 PERROR("chown");
4555 goto error;
4556 }
4557 }
d6f42150
DG
4558
4559 /* Create the kconsumerd error unix socket */
3bd1e081
MD
4560 consumer_data->err_sock =
4561 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
4562 if (consumer_data->err_sock < 0) {
4563 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
4564 ret = -1;
4565 goto error;
4566 }
4567
a24f05ab
MD
4568 /*
4569 * Set the CLOEXEC flag. Return code is useless because either way, the
4570 * show must go on.
4571 */
4572 ret = utils_set_fd_cloexec(consumer_data->err_sock);
4573 if (ret < 0) {
4574 PERROR("utils_set_fd_cloexec");
4575 /* continue anyway */
4576 }
4577
d6f42150 4578 /* File permission MUST be 660 */
3bd1e081 4579 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 4580 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 4581 if (ret < 0) {
3bd1e081 4582 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 4583 PERROR("chmod");
d6f42150
DG
4584 goto error;
4585 }
4586
4587error:
4588 return ret;
4589}
4590
fac6795d 4591/*
d063d709 4592 * Signal handler for the daemon
cf3af59e 4593 *
54d01ffb
DG
4594 * Simply stop all worker threads, leaving main() return gracefully after
4595 * joining all threads and calling cleanup().
fac6795d
DG
4596 */
4597static void sighandler(int sig)
4598{
4599 switch (sig) {
cf3af59e 4600 case SIGPIPE:
af87c45a 4601 DBG("SIGPIPE caught");
cf3af59e
MD
4602 return;
4603 case SIGINT:
af87c45a 4604 DBG("SIGINT caught");
cf3af59e
MD
4605 stop_threads();
4606 break;
4607 case SIGTERM:
af87c45a 4608 DBG("SIGTERM caught");
cf3af59e
MD
4609 stop_threads();
4610 break;
0bb7724a
DG
4611 case SIGUSR1:
4612 CMM_STORE_SHARED(recv_child_signal, 1);
4613 break;
cf3af59e
MD
4614 default:
4615 break;
fac6795d 4616 }
fac6795d
DG
4617}
4618
4619/*
d063d709 4620 * Setup signal handler for :
1d4b027a 4621 * SIGINT, SIGTERM, SIGPIPE
fac6795d 4622 */
1d4b027a 4623static int set_signal_handler(void)
fac6795d 4624{
1d4b027a
DG
4625 int ret = 0;
4626 struct sigaction sa;
4627 sigset_t sigset;
fac6795d 4628
1d4b027a 4629 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 4630 PERROR("sigemptyset");
1d4b027a
DG
4631 return ret;
4632 }
d6f42150 4633
1d4b027a
DG
4634 sa.sa_handler = sighandler;
4635 sa.sa_mask = sigset;
4636 sa.sa_flags = 0;
4637 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 4638 PERROR("sigaction");
1d4b027a 4639 return ret;
d6f42150
DG
4640 }
4641
1d4b027a 4642 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 4643 PERROR("sigaction");
1d4b027a 4644 return ret;
d6f42150 4645 }
aaf26714 4646
1d4b027a 4647 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 4648 PERROR("sigaction");
1d4b027a 4649 return ret;
8c0faa1d
DG
4650 }
4651
0bb7724a
DG
4652 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
4653 PERROR("sigaction");
4654 return ret;
4655 }
4656
4657 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
4658
4659 return ret;
fac6795d
DG
4660}
4661
f3ed775e 4662/*
d063d709
DG
4663 * Set open files limit to unlimited. This daemon can open a large number of
4664 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
4665 */
4666static void set_ulimit(void)
4667{
4668 int ret;
4669 struct rlimit lim;
4670
a88df331 4671 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
4672 lim.rlim_cur = 65535;
4673 lim.rlim_max = 65535;
4674
4675 ret = setrlimit(RLIMIT_NOFILE, &lim);
4676 if (ret < 0) {
76d7553f 4677 PERROR("failed to set open files limit");
f3ed775e
DG
4678 }
4679}
4680
35f90c40
DG
4681/*
4682 * Write pidfile using the rundir and opt_pidfile.
4683 */
4684static void write_pidfile(void)
4685{
4686 int ret;
4687 char pidfile_path[PATH_MAX];
4688
4689 assert(rundir);
4690
4691 if (opt_pidfile) {
4692 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
4693 } else {
4694 /* Build pidfile path from rundir and opt_pidfile. */
4695 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
4696 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
4697 if (ret < 0) {
4698 PERROR("snprintf pidfile path");
4699 goto error;
4700 }
4701 }
4702
4703 /*
4704 * Create pid file in rundir. Return value is of no importance. The
4705 * execution will continue even though we are not able to write the file.
4706 */
4707 (void) utils_create_pid_file(getpid(), pidfile_path);
4708
4709error:
4710 return;
4711}
4712
cd9290dd
DG
4713/*
4714 * Write JUL TCP port using the rundir.
4715 */
4716static void write_julport(void)
4717{
4718 int ret;
4719 char path[PATH_MAX];
4720
4721 assert(rundir);
4722
4723 ret = snprintf(path, sizeof(path), "%s/"
4724 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir);
4725 if (ret < 0) {
4726 PERROR("snprintf julport path");
4727 goto error;
4728 }
4729
4730 /*
4731 * Create TCP JUL port file in rundir. Return value is of no importance.
4732 * The execution will continue even though we are not able to write the
4733 * file.
4734 */
4735 (void) utils_create_pid_file(jul_tcp_port, path);
4736
4737error:
4738 return;
4739}
4740
fac6795d
DG
4741/*
4742 * main
4743 */
4744int main(int argc, char **argv)
4745{
fac6795d
DG
4746 int ret = 0;
4747 void *status;
ae9e45b3 4748 const char *home_path, *env_app_timeout;
fac6795d 4749
335a95b7
MD
4750 init_kernel_workarounds();
4751
f6a9efaa
DG
4752 rcu_register_thread();
4753
0bb7724a
DG
4754 if ((ret = set_signal_handler()) < 0) {
4755 goto error;
4756 }
4757
7753dea8 4758 setup_consumerd_path();
fb09408a 4759
12744796
DG
4760 page_size = sysconf(_SC_PAGESIZE);
4761 if (page_size < 0) {
4762 PERROR("sysconf _SC_PAGESIZE");
4763 page_size = LONG_MAX;
4764 WARN("Fallback page size to %ld", page_size);
4765 }
4766
26296c48 4767 /* Parse arguments and load the daemon configuration file */
fac6795d 4768 progname = argv[0];
26296c48 4769 if ((ret = set_options(argc, argv)) < 0) {
cf3af59e 4770 goto error;
fac6795d
DG
4771 }
4772
4773 /* Daemonize */
72dd7491 4774 if (opt_daemon || opt_background) {
ceed52b5
MD
4775 int i;
4776
72dd7491
MD
4777 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
4778 !opt_background);
53094c05 4779 if (ret < 0) {
cf3af59e 4780 goto error;
53094c05 4781 }
0bb7724a 4782
ceed52b5 4783 /*
0bb7724a
DG
4784 * We are in the child. Make sure all other file descriptors are
4785 * closed, in case we are called with more opened file descriptors than
4786 * the standard ones.
ceed52b5
MD
4787 */
4788 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4789 (void) close(i);
4790 }
4791 }
4792
4793 /* Create thread quit pipe */
4794 if ((ret = init_thread_quit_pipe()) < 0) {
4795 goto error;
fac6795d
DG
4796 }
4797
4798 /* Check if daemon is UID = 0 */
4799 is_root = !getuid();
4800
fac6795d 4801 if (is_root) {
990570ed 4802 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
67e40797
DG
4803
4804 /* Create global run dir with root access */
4805 ret = create_lttng_rundir(rundir);
d6f42150 4806 if (ret < 0) {
cf3af59e 4807 goto error;
d6f42150
DG
4808 }
4809
fac6795d 4810 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
4811 snprintf(apps_unix_sock_path, PATH_MAX,
4812 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
4813 }
4814
4815 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
4816 snprintf(client_unix_sock_path, PATH_MAX,
4817 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4818 }
0fdd1e2c
DG
4819
4820 /* Set global SHM for ust */
4821 if (strlen(wait_shm_path) == 0) {
4822 snprintf(wait_shm_path, PATH_MAX,
4823 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4824 }
67e40797 4825
44a5e5eb
DG
4826 if (strlen(health_unix_sock_path) == 0) {
4827 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4828 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4829 }
4830
67e40797
DG
4831 /* Setup kernel consumerd path */
4832 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 4833 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
67e40797 4834 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4835 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
67e40797
DG
4836
4837 DBG2("Kernel consumer err path: %s",
4838 kconsumer_data.err_unix_sock_path);
4839 DBG2("Kernel consumer cmd path: %s",
4840 kconsumer_data.cmd_unix_sock_path);
fac6795d 4841 } else {
feb0f3e5 4842 home_path = utils_get_home_dir();
b082db07 4843 if (home_path == NULL) {
273ea72c
DG
4844 /* TODO: Add --socket PATH option */
4845 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
4846 ret = -EPERM;
4847 goto error;
b082db07
DG
4848 }
4849
67e40797
DG
4850 /*
4851 * Create rundir from home path. This will create something like
4852 * $HOME/.lttng
4853 */
990570ed 4854 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797
DG
4855 if (ret < 0) {
4856 ret = -ENOMEM;
4857 goto error;
4858 }
4859
4860 ret = create_lttng_rundir(rundir);
4861 if (ret < 0) {
4862 goto error;
4863 }
4864
fac6795d 4865 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 4866 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 4867 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
4868 }
4869
4870 /* Set the cli tool unix socket path */
4871 if (strlen(client_unix_sock_path) == 0) {
d6f42150 4872 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 4873 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 4874 }
0fdd1e2c
DG
4875
4876 /* Set global SHM for ust */
4877 if (strlen(wait_shm_path) == 0) {
4878 snprintf(wait_shm_path, PATH_MAX,
d0b96690 4879 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
0fdd1e2c 4880 }
44a5e5eb
DG
4881
4882 /* Set health check Unix path */
4883 if (strlen(health_unix_sock_path) == 0) {
4884 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4885 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4886 }
fac6795d
DG
4887 }
4888
5c827ce0
DG
4889 /* Set consumer initial state */
4890 kernel_consumerd_state = CONSUMER_STOPPED;
4891 ust_consumerd_state = CONSUMER_STOPPED;
4892
847177cd
DG
4893 DBG("Client socket path %s", client_unix_sock_path);
4894 DBG("Application socket path %s", apps_unix_sock_path);
d0b96690 4895 DBG("Application wait path %s", wait_shm_path);
67e40797
DG
4896 DBG("LTTng run directory path: %s", rundir);
4897
4898 /* 32 bits consumerd path setup */
4899 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 4900 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
67e40797 4901 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4902 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
67e40797
DG
4903
4904 DBG2("UST consumer 32 bits err path: %s",
4905 ustconsumer32_data.err_unix_sock_path);
4906 DBG2("UST consumer 32 bits cmd path: %s",
4907 ustconsumer32_data.cmd_unix_sock_path);
4908
4909 /* 64 bits consumerd path setup */
4910 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 4911 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
67e40797 4912 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 4913 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
67e40797
DG
4914
4915 DBG2("UST consumer 64 bits err path: %s",
4916 ustconsumer64_data.err_unix_sock_path);
4917 DBG2("UST consumer 64 bits cmd path: %s",
4918 ustconsumer64_data.cmd_unix_sock_path);
847177cd 4919
273ea72c 4920 /*
7d8234d9 4921 * See if daemon already exist.
fac6795d 4922 */
7d8234d9 4923 if ((ret = check_existing_daemon()) < 0) {
75462a81 4924 ERR("Already running daemon.\n");
273ea72c 4925 /*
cf3af59e
MD
4926 * We do not goto exit because we must not cleanup()
4927 * because a daemon is already running.
ab118b20 4928 */
cf3af59e 4929 goto error;
a88df331
DG
4930 }
4931
1427f9b2
DG
4932 /*
4933 * Init UST app hash table. Alloc hash table before this point since
4934 * cleanup() can get called after that point.
4935 */
4936 ust_app_ht_alloc();
4937
f20baf8e
DG
4938 /* Initialize JUL domain subsystem. */
4939 if ((ret = jul_init()) < 0) {
4940 /* ENOMEM at this point. */
4941 goto error;
4942 }
4943
54d01ffb 4944 /* After this point, we can safely call cleanup() with "goto exit" */
a88df331
DG
4945
4946 /*
4947 * These actions must be executed as root. We do that *after* setting up
4948 * the sockets path because we MUST make the check for another daemon using
4949 * those paths *before* trying to set the kernel consumer sockets and init
4950 * kernel tracer.
4951 */
4952 if (is_root) {
67e40797 4953 ret = set_consumer_sockets(&kconsumer_data, rundir);
7753dea8
MD
4954 if (ret < 0) {
4955 goto exit;
4956 }
4957
a88df331 4958 /* Setup kernel tracer */
4fba7219
DG
4959 if (!opt_no_kernel) {
4960 init_kernel_tracer();
4961 }
a88df331
DG
4962
4963 /* Set ulimit for open files */
4964 set_ulimit();
fac6795d 4965 }
4063050c
MD
4966 /* init lttng_fd tracking must be done after set_ulimit. */
4967 lttng_fd_init();
fac6795d 4968
67e40797
DG
4969 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4970 if (ret < 0) {
4971 goto exit;
4972 }
4973
4974 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4975 if (ret < 0) {
4976 goto exit;
4977 }
4978
d6f42150 4979 /* Setup the needed unix socket */
cf3af59e
MD
4980 if ((ret = init_daemon_socket()) < 0) {
4981 goto exit;
fac6795d
DG
4982 }
4983
4984 /* Set credentials to socket */
be040666 4985 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
cf3af59e 4986 goto exit;
fac6795d
DG
4987 }
4988
5b8719f5
DG
4989 /* Get parent pid if -S, --sig-parent is specified. */
4990 if (opt_sig_parent) {
4991 ppid = getppid();
4992 }
4993
7a485870 4994 /* Setup the kernel pipe for waking up the kernel thread */
6620da75
DG
4995 if (is_root && !opt_no_kernel) {
4996 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4997 goto exit;
4998 }
7a485870
DG
4999 }
5000
0b2dc8df
MD
5001 /* Setup the thread ht_cleanup communication pipe. */
5002 if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) {
5003 goto exit;
5004 }
5005
099e26bd 5006 /* Setup the thread apps communication pipe. */
ef599319 5007 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
099e26bd
DG
5008 goto exit;
5009 }
5010
d0b96690
DG
5011 /* Setup the thread apps notify communication pipe. */
5012 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
5013 goto exit;
5014 }
5015
7972aab2
DG
5016 /* Initialize global buffer per UID and PID registry. */
5017 buffer_reg_init_uid_registry();
5018 buffer_reg_init_pid_registry();
5019
099e26bd
DG
5020 /* Init UST command queue. */
5021 cds_wfq_init(&ust_cmd_queue.queue);
5022
273ea72c 5023 /*
54d01ffb
DG
5024 * Get session list pointer. This pointer MUST NOT be free(). This list is
5025 * statically declared in session.c
273ea72c 5026 */
54d01ffb 5027 session_list_ptr = session_get_list();
b5541356 5028
5eb91c98
DG
5029 /* Set up max poll set size */
5030 lttng_poll_set_max_size();
5031
2f77fc4b 5032 cmd_init();
00e2e675 5033
ae9e45b3
DG
5034 /* Check for the application socket timeout env variable. */
5035 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5036 if (env_app_timeout) {
5037 app_socket_timeout = atoi(env_app_timeout);
5038 } else {
5039 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5040 }
5041
35f90c40 5042 write_pidfile();
cd9290dd 5043 write_julport();
35f90c40 5044
554831e7
MD
5045 /* Initialize communication library */
5046 lttcomm_init();
d831c249
DG
5047 /* This is to get the TCP timeout value. */
5048 lttcomm_inet_init();
554831e7 5049
67e05644
DG
5050 /*
5051 * Initialize the health check subsystem. This call should set the
5052 * appropriate time values.
5053 */
6c71277b 5054 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
8782cc74
MD
5055 if (!health_sessiond) {
5056 PERROR("health_app_create error");
5057 goto exit_health_sessiond_cleanup;
5058 }
67e05644 5059
d5b9fd2f 5060 /* Create thread to clean up RCU hash tables */
0b2dc8df
MD
5061 ret = pthread_create(&ht_cleanup_thread, NULL,
5062 thread_ht_cleanup, (void *) NULL);
5063 if (ret != 0) {
5064 PERROR("pthread_create ht_cleanup");
5065 goto exit_ht_cleanup;
5066 }
5067
d5b9fd2f 5068 /* Create health-check thread */
44a5e5eb
DG
5069 ret = pthread_create(&health_thread, NULL,
5070 thread_manage_health, (void *) NULL);
5071 if (ret != 0) {
5072 PERROR("pthread_create health");
5073 goto exit_health;
5074 }
5075
cf3af59e 5076 /* Create thread to manage the client socket */
099e26bd
DG
5077 ret = pthread_create(&client_thread, NULL,
5078 thread_manage_clients, (void *) NULL);
cf3af59e 5079 if (ret != 0) {
76d7553f 5080 PERROR("pthread_create clients");
cf3af59e
MD
5081 goto exit_client;
5082 }
fac6795d 5083
099e26bd
DG
5084 /* Create thread to dispatch registration */
5085 ret = pthread_create(&dispatch_thread, NULL,
5086 thread_dispatch_ust_registration, (void *) NULL);
5087 if (ret != 0) {
76d7553f 5088 PERROR("pthread_create dispatch");
099e26bd
DG
5089 goto exit_dispatch;
5090 }
5091
5092 /* Create thread to manage application registration. */
5093 ret = pthread_create(&reg_apps_thread, NULL,
5094 thread_registration_apps, (void *) NULL);
5095 if (ret != 0) {
76d7553f 5096 PERROR("pthread_create registration");
099e26bd
DG
5097 goto exit_reg_apps;
5098 }
5099
cf3af59e 5100 /* Create thread to manage application socket */
54d01ffb
DG
5101 ret = pthread_create(&apps_thread, NULL,
5102 thread_manage_apps, (void *) NULL);
cf3af59e 5103 if (ret != 0) {
d0b96690
DG
5104 PERROR("pthread_create apps");
5105 goto exit_apps;
5106 }
5107
5108 /* Create thread to manage application notify socket */
5109 ret = pthread_create(&apps_notify_thread, NULL,
5110 ust_thread_manage_notify, (void *) NULL);
5111 if (ret != 0) {
76d7553f 5112 PERROR("pthread_create apps");
9563b0ad 5113 goto exit_apps_notify;
cf3af59e 5114 }
fac6795d 5115
4d076222
DG
5116 /* Create JUL registration thread. */
5117 ret = pthread_create(&jul_reg_thread, NULL,
5118 jul_thread_manage_registration, (void *) NULL);
5119 if (ret != 0) {
5120 PERROR("pthread_create apps");
5121 goto exit_jul_reg;
5122 }
5123
6620da75
DG
5124 /* Don't start this thread if kernel tracing is not requested nor root */
5125 if (is_root && !opt_no_kernel) {
5126 /* Create kernel thread to manage kernel event */
5127 ret = pthread_create(&kernel_thread, NULL,
5128 thread_manage_kernel, (void *) NULL);
5129 if (ret != 0) {
5130 PERROR("pthread_create kernel");
5131 goto exit_kernel;
5132 }
7a485870 5133
6620da75
DG
5134 ret = pthread_join(kernel_thread, &status);
5135 if (ret != 0) {
5136 PERROR("pthread_join");
5137 goto error; /* join error, exit without cleanup */
5138 }
fac6795d
DG
5139 }
5140
cf3af59e 5141exit_kernel:
4d076222
DG
5142 ret = pthread_join(jul_reg_thread, &status);
5143 if (ret != 0) {
5144 PERROR("pthread_join JUL");
5145 goto error; /* join error, exit without cleanup */
5146 }
5147
5148exit_jul_reg:
9563b0ad
DG
5149 ret = pthread_join(apps_notify_thread, &status);
5150 if (ret != 0) {
5151 PERROR("pthread_join apps notify");
5152 goto error; /* join error, exit without cleanup */
5153 }
5154
5155exit_apps_notify:
cf3af59e
MD
5156 ret = pthread_join(apps_thread, &status);
5157 if (ret != 0) {
9563b0ad 5158 PERROR("pthread_join apps");
cf3af59e
MD
5159 goto error; /* join error, exit without cleanup */
5160 }
fac6795d 5161
4ccd8c8f 5162
cf3af59e 5163exit_apps:
099e26bd
DG
5164 ret = pthread_join(reg_apps_thread, &status);
5165 if (ret != 0) {
76d7553f 5166 PERROR("pthread_join");
099e26bd
DG
5167 goto error; /* join error, exit without cleanup */
5168 }
5169
5170exit_reg_apps:
5171 ret = pthread_join(dispatch_thread, &status);
5172 if (ret != 0) {
76d7553f 5173 PERROR("pthread_join");
099e26bd
DG
5174 goto error; /* join error, exit without cleanup */
5175 }
5176
5177exit_dispatch:
cf3af59e
MD
5178 ret = pthread_join(client_thread, &status);
5179 if (ret != 0) {
76d7553f 5180 PERROR("pthread_join");
cf3af59e
MD
5181 goto error; /* join error, exit without cleanup */
5182 }
5183
3bd1e081 5184 ret = join_consumer_thread(&kconsumer_data);
cf3af59e 5185 if (ret != 0) {
76d7553f 5186 PERROR("join_consumer");
cf3af59e
MD
5187 goto error; /* join error, exit without cleanup */
5188 }
a88df331 5189
06f525de
DG
5190 ret = join_consumer_thread(&ustconsumer32_data);
5191 if (ret != 0) {
5192 PERROR("join_consumer ust32");
5193 goto error; /* join error, exit without cleanup */
5194 }
5195
5196 ret = join_consumer_thread(&ustconsumer64_data);
5197 if (ret != 0) {
5198 PERROR("join_consumer ust64");
5199 goto error; /* join error, exit without cleanup */
5200 }
5201
cf3af59e 5202exit_client:
06f525de
DG
5203 ret = pthread_join(health_thread, &status);
5204 if (ret != 0) {
5205 PERROR("pthread_join health thread");
5206 goto error; /* join error, exit without cleanup */
5207 }
5208
44a5e5eb 5209exit_health:
0b2dc8df
MD
5210 ret = pthread_join(ht_cleanup_thread, &status);
5211 if (ret != 0) {
5212 PERROR("pthread_join ht cleanup thread");
5213 goto error; /* join error, exit without cleanup */
5214 }
5215exit_ht_cleanup:
8782cc74
MD
5216 health_app_destroy(health_sessiond);
5217exit_health_sessiond_cleanup:
a88df331 5218exit:
cf3af59e
MD
5219 /*
5220 * cleanup() is called when no other thread is running.
5221 */
f6a9efaa 5222 rcu_thread_online();
cf3af59e 5223 cleanup();
f6a9efaa
DG
5224 rcu_thread_offline();
5225 rcu_unregister_thread();
67e40797 5226 if (!ret) {
cf3af59e 5227 exit(EXIT_SUCCESS);
67e40797 5228 }
cf3af59e 5229error:
5e16da05 5230 exit(EXIT_FAILURE);
fac6795d 5231}
This page took 0.437378 seconds and 4 git commands to generate.