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