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