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