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