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