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