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