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