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