Implement UST PID tracker
[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:
54d01ffb
DG
2963 break;
2964 default:
2965 /* Setup lttng message with no payload */
2966 ret = setup_lttng_msg(cmd_ctx, 0);
2967 if (ret < 0) {
2968 /* This label does not try to unlock the session */
2969 goto init_setup_error;
2970 }
2971 }
2972
2973 /* Commands that DO NOT need a session. */
2974 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 2975 case LTTNG_CREATE_SESSION:
27babd3a 2976 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 2977 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09 2978 case LTTNG_CALIBRATE:
54d01ffb
DG
2979 case LTTNG_LIST_SESSIONS:
2980 case LTTNG_LIST_TRACEPOINTS:
834978fd 2981 case LTTNG_LIST_SYSCALLS:
f37d259d 2982 case LTTNG_LIST_TRACEPOINT_FIELDS:
fb198a11 2983 case LTTNG_SAVE_SESSION:
44d3bd01 2984 need_tracing_session = 0;
54d01ffb
DG
2985 break;
2986 default:
2987 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
2988 /*
2989 * We keep the session list lock across _all_ commands
2990 * for now, because the per-session lock does not
2991 * handle teardown properly.
2992 */
74babd95 2993 session_lock_list();
54d01ffb
DG
2994 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2995 if (cmd_ctx->session == NULL) {
bba2d65f 2996 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
2997 goto error;
2998 } else {
2999 /* Acquire lock for the session */
3000 session_lock(cmd_ctx->session);
3001 }
3002 break;
3003 }
b389abbe 3004
5f3ecf22
DG
3005 /*
3006 * Commands that need a valid session but should NOT create one if none
3007 * exists. Instead of creating one and destroying it when the command is
3008 * handled, process that right before so we save some round trip in useless
3009 * code path.
3010 */
3011 switch (cmd_ctx->lsm->cmd_type) {
3012 case LTTNG_DISABLE_CHANNEL:
3013 case LTTNG_DISABLE_EVENT:
5f3ecf22
DG
3014 switch (cmd_ctx->lsm->domain.type) {
3015 case LTTNG_DOMAIN_KERNEL:
3016 if (!cmd_ctx->session->kernel_session) {
3017 ret = LTTNG_ERR_NO_CHANNEL;
3018 goto error;
3019 }
3020 break;
3021 case LTTNG_DOMAIN_JUL:
5cdb6027 3022 case LTTNG_DOMAIN_LOG4J:
0e115563 3023 case LTTNG_DOMAIN_PYTHON:
5f3ecf22
DG
3024 case LTTNG_DOMAIN_UST:
3025 if (!cmd_ctx->session->ust_session) {
3026 ret = LTTNG_ERR_NO_CHANNEL;
3027 goto error;
3028 }
3029 break;
3030 default:
3031 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
3032 goto error;
3033 }
3034 default:
3035 break;
3036 }
3037
2e09ba09
MD
3038 if (!need_domain) {
3039 goto skip_domain;
3040 }
a4b92340 3041
54d01ffb
DG
3042 /*
3043 * Check domain type for specific "pre-action".
3044 */
3045 switch (cmd_ctx->lsm->domain.type) {
3046 case LTTNG_DOMAIN_KERNEL:
d1f1c568 3047 if (!is_root) {
f73fabfd 3048 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
3049 goto error;
3050 }
3051
54d01ffb 3052 /* Kernel tracer check */
a4b35e07 3053 if (kernel_tracer_fd == -1) {
54d01ffb 3054 /* Basically, load kernel tracer modules */
096102bd
DG
3055 ret = init_kernel_tracer();
3056 if (ret != 0) {
54d01ffb
DG
3057 goto error;
3058 }
3059 }
5eb91c98 3060
5c827ce0
DG
3061 /* Consumer is in an ERROR state. Report back to client */
3062 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 3063 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
3064 goto error;
3065 }
3066
54d01ffb 3067 /* Need a session for kernel command */
44d3bd01 3068 if (need_tracing_session) {
54d01ffb 3069 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 3070 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 3071 if (ret < 0) {
f73fabfd 3072 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
3073 goto error;
3074 }
b389abbe 3075 }
7d29a247 3076
54d01ffb 3077 /* Start the kernel consumer daemon */
3bd1e081
MD
3078 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3079 if (kconsumer_data.pid == 0 &&
785d2d0d 3080 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3bd1e081
MD
3081 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3082 ret = start_consumerd(&kconsumer_data);
7d29a247 3083 if (ret < 0) {
f73fabfd 3084 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 3085 goto error;
950131af 3086 }
5c827ce0 3087 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
3088 } else {
3089 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 3090 }
173af62f 3091
a4b92340
DG
3092 /*
3093 * The consumer was just spawned so we need to add the socket to
3094 * the consumer output of the session if exist.
3095 */
3096 ret = consumer_create_socket(&kconsumer_data,
3097 cmd_ctx->session->kernel_session->consumer);
3098 if (ret < 0) {
3099 goto error;
173af62f 3100 }
0d0c377a 3101 }
5c827ce0 3102
54d01ffb 3103 break;
b9dfb167 3104 case LTTNG_DOMAIN_JUL:
5cdb6027 3105 case LTTNG_DOMAIN_LOG4J:
0e115563 3106 case LTTNG_DOMAIN_PYTHON:
2bdd86d4 3107 case LTTNG_DOMAIN_UST:
44d3bd01 3108 {
b51ec5b4
MD
3109 if (!ust_app_supported()) {
3110 ret = LTTNG_ERR_NO_UST;
3111 goto error;
3112 }
5c827ce0
DG
3113 /* Consumer is in an ERROR state. Report back to client */
3114 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 3115 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
3116 goto error;
3117 }
3118
44d3bd01 3119 if (need_tracing_session) {
a4b92340 3120 /* Create UST session if none exist. */
f6a9efaa 3121 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 3122 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 3123 &cmd_ctx->lsm->domain);
f73fabfd 3124 if (ret != LTTNG_OK) {
44d3bd01
DG
3125 goto error;
3126 }
3127 }
00e2e675 3128
7753dea8
MD
3129 /* Start the UST consumer daemons */
3130 /* 64-bit */
3131 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 3132 if (consumerd64_bin[0] != '\0' &&
7753dea8 3133 ustconsumer64_data.pid == 0 &&
785d2d0d 3134 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
3135 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3136 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 3137 if (ret < 0) {
f73fabfd 3138 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 3139 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
3140 goto error;
3141 }
48842b30 3142
173af62f 3143 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 3144 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 3145 } else {
7753dea8
MD
3146 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
3147 }
173af62f
DG
3148
3149 /*
3150 * Setup socket for consumer 64 bit. No need for atomic access
3151 * since it was set above and can ONLY be set in this thread.
3152 */
a4b92340
DG
3153 ret = consumer_create_socket(&ustconsumer64_data,
3154 cmd_ctx->session->ust_session->consumer);
3155 if (ret < 0) {
3156 goto error;
173af62f
DG
3157 }
3158
7753dea8 3159 /* 32-bit */
385b881b 3160 pthread_mutex_lock(&ustconsumer32_data.pid_mutex);
fc7a59ce 3161 if (consumerd32_bin[0] != '\0' &&
7753dea8 3162 ustconsumer32_data.pid == 0 &&
785d2d0d 3163 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
3164 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
3165 ret = start_consumerd(&ustconsumer32_data);
3166 if (ret < 0) {
f73fabfd 3167 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 3168 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
3169 goto error;
3170 }
5c827ce0 3171
173af62f 3172 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 3173 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
3174 } else {
3175 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 3176 }
173af62f
DG
3177
3178 /*
3179 * Setup socket for consumer 64 bit. No need for atomic access
3180 * since it was set above and can ONLY be set in this thread.
3181 */
a4b92340
DG
3182 ret = consumer_create_socket(&ustconsumer32_data,
3183 cmd_ctx->session->ust_session->consumer);
3184 if (ret < 0) {
3185 goto error;
173af62f 3186 }
44d3bd01
DG
3187 }
3188 break;
48842b30 3189 }
54d01ffb 3190 default:
54d01ffb
DG
3191 break;
3192 }
2e09ba09 3193skip_domain:
33a2b854 3194
5c827ce0
DG
3195 /* Validate consumer daemon state when start/stop trace command */
3196 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
3197 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
3198 switch (cmd_ctx->lsm->domain.type) {
b9dfb167 3199 case LTTNG_DOMAIN_JUL:
5cdb6027 3200 case LTTNG_DOMAIN_LOG4J:
0e115563 3201 case LTTNG_DOMAIN_PYTHON:
5c827ce0
DG
3202 case LTTNG_DOMAIN_UST:
3203 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 3204 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
3205 goto error;
3206 }
3207 break;
3208 case LTTNG_DOMAIN_KERNEL:
3209 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 3210 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
3211 goto error;
3212 }
3213 break;
3214 }
3215 }
3216
8e0af1b4
MD
3217 /*
3218 * Check that the UID or GID match that of the tracing session.
3219 * The root user can interact with all sessions.
3220 */
3221 if (need_tracing_session) {
3222 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
3223 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3224 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
f73fabfd 3225 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
3226 goto error;
3227 }
3228 }
3229
ffe60014
DG
3230 /*
3231 * Send relayd information to consumer as soon as we have a domain and a
3232 * session defined.
3233 */
3234 if (cmd_ctx->session && need_domain) {
3235 /*
3236 * Setup relayd if not done yet. If the relayd information was already
3237 * sent to the consumer, this call will gracefully return.
3238 */
3239 ret = cmd_setup_relayd(cmd_ctx->session);
3240 if (ret != LTTNG_OK) {
3241 goto error;
3242 }
3243 }
3244
54d01ffb
DG
3245 /* Process by command type */
3246 switch (cmd_ctx->lsm->cmd_type) {
3247 case LTTNG_ADD_CONTEXT:
3248 {
3249 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3250 cmd_ctx->lsm->u.context.channel_name,
979e618e 3251 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
54d01ffb
DG
3252 break;
3253 }
3254 case LTTNG_DISABLE_CHANNEL:
3255 {
3256 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3257 cmd_ctx->lsm->u.disable.channel_name);
3258 break;
3259 }
3260 case LTTNG_DISABLE_EVENT:
3261 {
6e911cad
MD
3262 /* FIXME: passing packed structure to non-packed pointer */
3263 /* TODO: handle filter */
54d01ffb
DG
3264 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3265 cmd_ctx->lsm->u.disable.channel_name,
6e911cad 3266 &cmd_ctx->lsm->u.disable.event);
33a2b854
DG
3267 break;
3268 }
54d01ffb
DG
3269 case LTTNG_ENABLE_CHANNEL:
3270 {
7972aab2 3271 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
2f77fc4b 3272 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
54d01ffb
DG
3273 break;
3274 }
ccf10263
MD
3275 case LTTNG_TRACK_PID:
3276 {
3277 ret = cmd_track_pid(cmd_ctx->session,
3278 cmd_ctx->lsm->domain.type,
3279 cmd_ctx->lsm->u.pid_tracker.pid);
3280 break;
3281 }
3282 case LTTNG_UNTRACK_PID:
3283 {
3284 ret = cmd_untrack_pid(cmd_ctx->session,
3285 cmd_ctx->lsm->domain.type,
3286 cmd_ctx->lsm->u.pid_tracker.pid);
3287 break;
3288 }
54d01ffb
DG
3289 case LTTNG_ENABLE_EVENT:
3290 {
882ef835
JI
3291 struct lttng_event_exclusion *exclusion = NULL;
3292 struct lttng_filter_bytecode *bytecode = NULL;
6b453b5e 3293 char *filter_expression = NULL;
882ef835 3294
67676bd8
DG
3295 /* Handle exclusion events and receive it from the client. */
3296 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3297 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3298
3299 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3300 (count * LTTNG_SYMBOL_NAME_LEN));
3301 if (!exclusion) {
3302 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3303 goto error;
882ef835 3304 }
882ef835 3305
67676bd8
DG
3306 DBG("Receiving var len exclusion event list from client ...");
3307 exclusion->count = count;
3308 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3309 count * LTTNG_SYMBOL_NAME_LEN);
3310 if (ret <= 0) {
3311 DBG("Nothing recv() from client var len data... continuing");
3312 *sock_error = 1;
3313 free(exclusion);
3314 ret = LTTNG_ERR_EXCLUSION_INVAL;
3315 goto error;
3316 }
3317 }
3318
6b453b5e
JG
3319 /* Get filter expression from client. */
3320 if (cmd_ctx->lsm->u.enable.expression_len > 0) {
3321 size_t expression_len =
3322 cmd_ctx->lsm->u.enable.expression_len;
3323
3324 if (expression_len > LTTNG_FILTER_MAX_LEN) {
3325 ret = LTTNG_ERR_FILTER_INVAL;
3326 free(exclusion);
3327 goto error;
3328 }
3329
3330 filter_expression = zmalloc(expression_len);
3331 if (!filter_expression) {
3332 free(exclusion);
3333 ret = LTTNG_ERR_FILTER_NOMEM;
3334 goto error;
3335 }
3336
3337 /* Receive var. len. data */
3338 DBG("Receiving var len filter's expression from client ...");
3339 ret = lttcomm_recv_unix_sock(sock, filter_expression,
3340 expression_len);
3341 if (ret <= 0) {
3342 DBG("Nothing recv() from client car len data... continuing");
3343 *sock_error = 1;
3344 free(filter_expression);
3345 free(exclusion);
3346 ret = LTTNG_ERR_FILTER_INVAL;
3347 goto error;
3348 }
3349 }
3350
67676bd8
DG
3351 /* Handle filter and get bytecode from client. */
3352 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3353 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3354
3355 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3356 ret = LTTNG_ERR_FILTER_INVAL;
49d21f93 3357 free(filter_expression);
67676bd8
DG
3358 free(exclusion);
3359 goto error;
3360 }
3361
3362 bytecode = zmalloc(bytecode_len);
3363 if (!bytecode) {
49d21f93 3364 free(filter_expression);
67676bd8
DG
3365 free(exclusion);
3366 ret = LTTNG_ERR_FILTER_NOMEM;
3367 goto error;
882ef835
JI
3368 }
3369
67676bd8
DG
3370 /* Receive var. len. data */
3371 DBG("Receiving var len filter's bytecode from client ...");
3372 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3373 if (ret <= 0) {
3374 DBG("Nothing recv() from client car len data... continuing");
3375 *sock_error = 1;
49d21f93 3376 free(filter_expression);
67676bd8
DG
3377 free(bytecode);
3378 free(exclusion);
3379 ret = LTTNG_ERR_FILTER_INVAL;
3380 goto error;
3381 }
3382
3383 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
49d21f93 3384 free(filter_expression);
67676bd8
DG
3385 free(bytecode);
3386 free(exclusion);
3387 ret = LTTNG_ERR_FILTER_INVAL;
3388 goto error;
3389 }
882ef835 3390 }
67676bd8
DG
3391
3392 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3393 cmd_ctx->lsm->u.enable.channel_name,
6b453b5e
JG
3394 &cmd_ctx->lsm->u.enable.event,
3395 filter_expression, bytecode, exclusion,
67676bd8 3396 kernel_poll_pipe[1]);
54d01ffb
DG
3397 break;
3398 }
052da939 3399 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 3400 {
9f19cc17 3401 struct lttng_event *events;
54d01ffb 3402 ssize_t nb_events;
052da939 3403
0845bbfa 3404 session_lock_list();
54d01ffb 3405 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
0845bbfa 3406 session_unlock_list();
54d01ffb 3407 if (nb_events < 0) {
f73fabfd 3408 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3409 ret = -nb_events;
3410 goto error;
2ef84c95
DG
3411 }
3412
3413 /*
3414 * Setup lttng message with payload size set to the event list size in
3415 * bytes and then copy list into the llm payload.
3416 */
052da939 3417 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 3418 if (ret < 0) {
052da939 3419 free(events);
2ef84c95
DG
3420 goto setup_error;
3421 }
3422
3423 /* Copy event list into message payload */
9f19cc17 3424 memcpy(cmd_ctx->llm->payload, events,
052da939 3425 sizeof(struct lttng_event) * nb_events);
2ef84c95 3426
9f19cc17 3427 free(events);
2ef84c95 3428
f73fabfd 3429 ret = LTTNG_OK;
2ef84c95
DG
3430 break;
3431 }
f37d259d
MD
3432 case LTTNG_LIST_TRACEPOINT_FIELDS:
3433 {
3434 struct lttng_event_field *fields;
3435 ssize_t nb_fields;
3436
0845bbfa 3437 session_lock_list();
a4b92340
DG
3438 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3439 &fields);
0845bbfa 3440 session_unlock_list();
f37d259d 3441 if (nb_fields < 0) {
f73fabfd 3442 /* Return value is a negative lttng_error_code. */
f37d259d
MD
3443 ret = -nb_fields;
3444 goto error;
3445 }
3446
3447 /*
3448 * Setup lttng message with payload size set to the event list size in
3449 * bytes and then copy list into the llm payload.
3450 */
a4b92340
DG
3451 ret = setup_lttng_msg(cmd_ctx,
3452 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
3453 if (ret < 0) {
3454 free(fields);
3455 goto setup_error;
3456 }
3457
3458 /* Copy event list into message payload */
3459 memcpy(cmd_ctx->llm->payload, fields,
3460 sizeof(struct lttng_event_field) * nb_fields);
3461
3462 free(fields);
3463
f73fabfd 3464 ret = LTTNG_OK;
f37d259d
MD
3465 break;
3466 }
834978fd
DG
3467 case LTTNG_LIST_SYSCALLS:
3468 {
3469 struct lttng_event *events;
3470 ssize_t nb_events;
3471
3472 nb_events = cmd_list_syscalls(&events);
3473 if (nb_events < 0) {
3474 /* Return value is a negative lttng_error_code. */
3475 ret = -nb_events;
3476 goto error;
3477 }
3478
3479 /*
3480 * Setup lttng message with payload size set to the event list size in
3481 * bytes and then copy list into the llm payload.
3482 */
3483 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3484 if (ret < 0) {
3485 free(events);
3486 goto setup_error;
3487 }
3488
3489 /* Copy event list into message payload */
3490 memcpy(cmd_ctx->llm->payload, events,
3491 sizeof(struct lttng_event) * nb_events);
3492
3493 free(events);
3494
3495 ret = LTTNG_OK;
3496 break;
3497 }
00e2e675
DG
3498 case LTTNG_SET_CONSUMER_URI:
3499 {
a4b92340
DG
3500 size_t nb_uri, len;
3501 struct lttng_uri *uris;
3502
3503 nb_uri = cmd_ctx->lsm->u.uri.size;
3504 len = nb_uri * sizeof(struct lttng_uri);
3505
3506 if (nb_uri == 0) {
f73fabfd 3507 ret = LTTNG_ERR_INVALID;
a4b92340
DG
3508 goto error;
3509 }
3510
3511 uris = zmalloc(len);
3512 if (uris == NULL) {
f73fabfd 3513 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3514 goto error;
3515 }
3516
3517 /* Receive variable len data */
77c7c900 3518 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
3519 ret = lttcomm_recv_unix_sock(sock, uris, len);
3520 if (ret <= 0) {
3521 DBG("No URIs received from client... continuing");
3522 *sock_error = 1;
f73fabfd 3523 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3524 free(uris);
a4b92340
DG
3525 goto error;
3526 }
3527
bda32d56
JG
3528 ret = cmd_set_consumer_uri(cmd_ctx->session, nb_uri, uris);
3529 free(uris);
f73fabfd 3530 if (ret != LTTNG_OK) {
a4b92340
DG
3531 goto error;
3532 }
3533
b1d41407 3534
00e2e675
DG
3535 break;
3536 }
f3ed775e 3537 case LTTNG_START_TRACE:
8c0faa1d 3538 {
54d01ffb 3539 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
3540 break;
3541 }
f3ed775e 3542 case LTTNG_STOP_TRACE:
8c0faa1d 3543 {
54d01ffb 3544 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
3545 break;
3546 }
5e16da05
MD
3547 case LTTNG_CREATE_SESSION:
3548 {
a4b92340
DG
3549 size_t nb_uri, len;
3550 struct lttng_uri *uris = NULL;
3551
3552 nb_uri = cmd_ctx->lsm->u.uri.size;
3553 len = nb_uri * sizeof(struct lttng_uri);
3554
3555 if (nb_uri > 0) {
3556 uris = zmalloc(len);
3557 if (uris == NULL) {
f73fabfd 3558 ret = LTTNG_ERR_FATAL;
a4b92340
DG
3559 goto error;
3560 }
3561
3562 /* Receive variable len data */
77c7c900 3563 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
3564 ret = lttcomm_recv_unix_sock(sock, uris, len);
3565 if (ret <= 0) {
3566 DBG("No URIs received from client... continuing");
3567 *sock_error = 1;
f73fabfd 3568 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3569 free(uris);
a4b92340
DG
3570 goto error;
3571 }
3572
3573 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3574 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 3575 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 3576 free(uris);
a4b92340
DG
3577 goto error;
3578 }
3579 }
3580
3581 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
ecc48a90 3582 &cmd_ctx->creds, 0);
a4b92340 3583
b1d41407
DG
3584 free(uris);
3585
00e2e675
DG
3586 break;
3587 }
5e16da05
MD
3588 case LTTNG_DESTROY_SESSION:
3589 {
2f77fc4b 3590 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
a4b92340
DG
3591
3592 /* Set session to NULL so we do not unlock it after free. */
256a5576 3593 cmd_ctx->session = NULL;
5461b305 3594 break;
5e16da05 3595 }
9f19cc17 3596 case LTTNG_LIST_DOMAINS:
5e16da05 3597 {
54d01ffb 3598 ssize_t nb_dom;
fa64dfb4 3599 struct lttng_domain *domains = NULL;
5461b305 3600
54d01ffb
DG
3601 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3602 if (nb_dom < 0) {
f73fabfd 3603 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3604 ret = -nb_dom;
3605 goto error;
ce3d728c 3606 }
520ff687 3607
54d01ffb 3608 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305 3609 if (ret < 0) {
ae9c20bc 3610 free(domains);
5461b305 3611 goto setup_error;
520ff687 3612 }
57167058 3613
54d01ffb
DG
3614 /* Copy event list into message payload */
3615 memcpy(cmd_ctx->llm->payload, domains,
3616 nb_dom * sizeof(struct lttng_domain));
3617
3618 free(domains);
5e16da05 3619
f73fabfd 3620 ret = LTTNG_OK;
5e16da05
MD
3621 break;
3622 }
9f19cc17 3623 case LTTNG_LIST_CHANNELS:
5e16da05 3624 {
6775595e 3625 int nb_chan;
ade7ce52 3626 struct lttng_channel *channels = NULL;
54d01ffb 3627
b551a063
DG
3628 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3629 cmd_ctx->session, &channels);
54d01ffb 3630 if (nb_chan < 0) {
f73fabfd 3631 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3632 ret = -nb_chan;
3633 goto error;
5461b305 3634 }
ca95a216 3635
54d01ffb 3636 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305 3637 if (ret < 0) {
ae9c20bc 3638 free(channels);
5461b305
DG
3639 goto setup_error;
3640 }
9f19cc17 3641
54d01ffb
DG
3642 /* Copy event list into message payload */
3643 memcpy(cmd_ctx->llm->payload, channels,
3644 nb_chan * sizeof(struct lttng_channel));
3645
3646 free(channels);
9f19cc17 3647
f73fabfd 3648 ret = LTTNG_OK;
5461b305 3649 break;
5e16da05 3650 }
9f19cc17 3651 case LTTNG_LIST_EVENTS:
5e16da05 3652 {
b551a063 3653 ssize_t nb_event;
684d34d2 3654 struct lttng_event *events = NULL;
9f19cc17 3655
b551a063 3656 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
3657 cmd_ctx->lsm->u.list.channel_name, &events);
3658 if (nb_event < 0) {
f73fabfd 3659 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
3660 ret = -nb_event;
3661 goto error;
5461b305 3662 }
ca95a216 3663
54d01ffb 3664 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305 3665 if (ret < 0) {
ae9c20bc 3666 free(events);
5461b305
DG
3667 goto setup_error;
3668 }
9f19cc17 3669
54d01ffb
DG
3670 /* Copy event list into message payload */
3671 memcpy(cmd_ctx->llm->payload, events,
3672 nb_event * sizeof(struct lttng_event));
9f19cc17 3673
54d01ffb 3674 free(events);
9f19cc17 3675
f73fabfd 3676 ret = LTTNG_OK;
5461b305 3677 break;
5e16da05
MD
3678 }
3679 case LTTNG_LIST_SESSIONS:
3680 {
8e0af1b4 3681 unsigned int nr_sessions;
5461b305 3682
8e0af1b4 3683 session_lock_list();
730389d9
DG
3684 nr_sessions = lttng_sessions_count(
3685 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3686 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 3687
8e0af1b4 3688 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 3689 if (ret < 0) {
54d01ffb 3690 session_unlock_list();
5461b305 3691 goto setup_error;
e065084a 3692 }
5e16da05 3693
6c9cc2ab 3694 /* Filled the session array */
2f77fc4b 3695 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
3696 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3697 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 3698
54d01ffb 3699 session_unlock_list();
5e16da05 3700
f73fabfd 3701 ret = LTTNG_OK;
5e16da05
MD
3702 break;
3703 }
d0254c7c
MD
3704 case LTTNG_CALIBRATE:
3705 {
54d01ffb
DG
3706 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3707 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
3708 break;
3709 }
d9800920
DG
3710 case LTTNG_REGISTER_CONSUMER:
3711 {
2f77fc4b
DG
3712 struct consumer_data *cdata;
3713
3714 switch (cmd_ctx->lsm->domain.type) {
3715 case LTTNG_DOMAIN_KERNEL:
3716 cdata = &kconsumer_data;
3717 break;
3718 default:
f73fabfd 3719 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3720 goto error;
3721 }
3722
54d01ffb 3723 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 3724 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
3725 break;
3726 }
6d805429 3727 case LTTNG_DATA_PENDING:
806e2684 3728 {
6d805429 3729 ret = cmd_data_pending(cmd_ctx->session);
806e2684
DG
3730 break;
3731 }
da3c9ec1
DG
3732 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3733 {
6dc3064a
DG
3734 struct lttcomm_lttng_output_id reply;
3735
3736 ret = cmd_snapshot_add_output(cmd_ctx->session,
3737 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3738 if (ret != LTTNG_OK) {
3739 goto error;
3740 }
3741
3742 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3743 if (ret < 0) {
3744 goto setup_error;
3745 }
3746
3747 /* Copy output list into message payload */
3748 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3749 ret = LTTNG_OK;
da3c9ec1
DG
3750 break;
3751 }
3752 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3753 {
6dc3064a
DG
3754 ret = cmd_snapshot_del_output(cmd_ctx->session,
3755 &cmd_ctx->lsm->u.snapshot_output.output);
da3c9ec1
DG
3756 break;
3757 }
3758 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3759 {
6dc3064a
DG
3760 ssize_t nb_output;
3761 struct lttng_snapshot_output *outputs = NULL;
3762
3763 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3764 if (nb_output < 0) {
3765 ret = -nb_output;
3766 goto error;
3767 }
3768
3769 ret = setup_lttng_msg(cmd_ctx,
3770 nb_output * sizeof(struct lttng_snapshot_output));
3771 if (ret < 0) {
3772 free(outputs);
3773 goto setup_error;
3774 }
3775
3776 if (outputs) {
3777 /* Copy output list into message payload */
3778 memcpy(cmd_ctx->llm->payload, outputs,
3779 nb_output * sizeof(struct lttng_snapshot_output));
3780 free(outputs);
3781 }
3782
3783 ret = LTTNG_OK;
da3c9ec1
DG
3784 break;
3785 }
3786 case LTTNG_SNAPSHOT_RECORD:
3787 {
6dc3064a
DG
3788 ret = cmd_snapshot_record(cmd_ctx->session,
3789 &cmd_ctx->lsm->u.snapshot_record.output,
3790 cmd_ctx->lsm->u.snapshot_record.wait);
da3c9ec1
DG
3791 break;
3792 }
27babd3a
DG
3793 case LTTNG_CREATE_SESSION_SNAPSHOT:
3794 {
3795 size_t nb_uri, len;
3796 struct lttng_uri *uris = NULL;
3797
3798 nb_uri = cmd_ctx->lsm->u.uri.size;
3799 len = nb_uri * sizeof(struct lttng_uri);
3800
3801 if (nb_uri > 0) {
3802 uris = zmalloc(len);
3803 if (uris == NULL) {
3804 ret = LTTNG_ERR_FATAL;
3805 goto error;
3806 }
3807
3808 /* Receive variable len data */
3809 DBG("Waiting for %zu URIs from client ...", nb_uri);
3810 ret = lttcomm_recv_unix_sock(sock, uris, len);
3811 if (ret <= 0) {
3812 DBG("No URIs received from client... continuing");
3813 *sock_error = 1;
3814 ret = LTTNG_ERR_SESSION_FAIL;
3815 free(uris);
3816 goto error;
3817 }
3818
3819 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3820 DBG("Creating session with ONE network URI is a bad call");
3821 ret = LTTNG_ERR_SESSION_FAIL;
3822 free(uris);
3823 goto error;
3824 }
3825 }
3826
3827 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3828 nb_uri, &cmd_ctx->creds);
3829 free(uris);
3830 break;
3831 }
ecc48a90
JD
3832 case LTTNG_CREATE_SESSION_LIVE:
3833 {
3834 size_t nb_uri, len;
3835 struct lttng_uri *uris = NULL;
3836
3837 nb_uri = cmd_ctx->lsm->u.uri.size;
3838 len = nb_uri * sizeof(struct lttng_uri);
3839
3840 if (nb_uri > 0) {
3841 uris = zmalloc(len);
3842 if (uris == NULL) {
3843 ret = LTTNG_ERR_FATAL;
3844 goto error;
3845 }
3846
3847 /* Receive variable len data */
3848 DBG("Waiting for %zu URIs from client ...", nb_uri);
3849 ret = lttcomm_recv_unix_sock(sock, uris, len);
3850 if (ret <= 0) {
3851 DBG("No URIs received from client... continuing");
3852 *sock_error = 1;
3853 ret = LTTNG_ERR_SESSION_FAIL;
3854 free(uris);
3855 goto error;
3856 }
3857
3858 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3859 DBG("Creating session with ONE network URI is a bad call");
3860 ret = LTTNG_ERR_SESSION_FAIL;
3861 free(uris);
3862 goto error;
3863 }
3864 }
3865
3866 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3867 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3868 free(uris);
3869 break;
3870 }
fb198a11
JG
3871 case LTTNG_SAVE_SESSION:
3872 {
3873 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
3874 &cmd_ctx->creds);
3875 break;
3876 }
d7ba1388
MD
3877 case LTTNG_SET_SESSION_SHM_PATH:
3878 {
3879 ret = cmd_set_session_shm_path(cmd_ctx->session,
3880 cmd_ctx->lsm->u.set_shm_path.shm_path);
3881 break;
3882 }
5e16da05 3883 default:
f73fabfd 3884 ret = LTTNG_ERR_UND;
5461b305 3885 break;
fac6795d
DG
3886 }
3887
5461b305 3888error:
5461b305
DG
3889 if (cmd_ctx->llm == NULL) {
3890 DBG("Missing llm structure. Allocating one.");
894be886 3891 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
3892 goto setup_error;
3893 }
3894 }
54d01ffb 3895 /* Set return code */
5461b305 3896 cmd_ctx->llm->ret_code = ret;
5461b305 3897setup_error:
b5541356 3898 if (cmd_ctx->session) {
54d01ffb 3899 session_unlock(cmd_ctx->session);
b5541356 3900 }
256a5576
MD
3901 if (need_tracing_session) {
3902 session_unlock_list();
3903 }
54d01ffb 3904init_setup_error:
8028d920 3905 return ret;
fac6795d
DG
3906}
3907
44a5e5eb
DG
3908/*
3909 * Thread managing health check socket.
3910 */
3911static void *thread_manage_health(void *data)
3912{
eb4a2943 3913 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
3914 uint32_t revents, nb_fd;
3915 struct lttng_poll_event events;
0c89d795
MD
3916 struct health_comm_msg msg;
3917 struct health_comm_reply reply;
44a5e5eb
DG
3918
3919 DBG("[thread] Manage health check started");
3920
3921 rcu_register_thread();
3922
6d737ce4
DG
3923 /* We might hit an error path before this is created. */
3924 lttng_poll_init(&events);
3cc04881 3925
44a5e5eb
DG
3926 /* Create unix socket */
3927 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3928 if (sock < 0) {
3929 ERR("Unable to create health check Unix socket");
3930 ret = -1;
3931 goto error;
3932 }
3933
6c71277b
MD
3934 if (is_root) {
3935 /* lttng health client socket path permissions */
3936 ret = chown(health_unix_sock_path, 0,
3937 utils_get_group_id(tracing_group_name));
3938 if (ret < 0) {
3939 ERR("Unable to set group on %s", health_unix_sock_path);
3940 PERROR("chown");
3941 ret = -1;
3942 goto error;
3943 }
3944
3945 ret = chmod(health_unix_sock_path,
3946 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3947 if (ret < 0) {
3948 ERR("Unable to set permissions on %s", health_unix_sock_path);
3949 PERROR("chmod");
3950 ret = -1;
3951 goto error;
3952 }
3953 }
3954
b662582b
DG
3955 /*
3956 * Set the CLOEXEC flag. Return code is useless because either way, the
3957 * show must go on.
3958 */
3959 (void) utils_set_fd_cloexec(sock);
3960
44a5e5eb
DG
3961 ret = lttcomm_listen_unix_sock(sock);
3962 if (ret < 0) {
3963 goto error;
3964 }
3965
3966 /*
3967 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3968 * more will be added to this poll set.
3969 */
d0b96690 3970 ret = sessiond_set_thread_pollset(&events, 2);
44a5e5eb
DG
3971 if (ret < 0) {
3972 goto error;
3973 }
3974
3975 /* Add the application registration socket */
3976 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3977 if (ret < 0) {
3978 goto error;
3979 }
3980
ef367a93 3981 sessiond_notify_ready();
97bc1426 3982
44a5e5eb
DG
3983 while (1) {
3984 DBG("Health check ready");
3985
44a5e5eb
DG
3986 /* Inifinite blocking call, waiting for transmission */
3987restart:
3988 ret = lttng_poll_wait(&events, -1);
3989 if (ret < 0) {
3990 /*
3991 * Restart interrupted system call.
3992 */
3993 if (errno == EINTR) {
3994 goto restart;
3995 }
3996 goto error;
3997 }
3998
0d9c5d77
DG
3999 nb_fd = ret;
4000
44a5e5eb
DG
4001 for (i = 0; i < nb_fd; i++) {
4002 /* Fetch once the poll data */
4003 revents = LTTNG_POLL_GETEV(&events, i);
4004 pollfd = LTTNG_POLL_GETFD(&events, i);
4005
fd20dac9
MD
4006 if (!revents) {
4007 /* No activity for this FD (poll implementation). */
4008 continue;
4009 }
4010
44a5e5eb 4011 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4012 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
44a5e5eb 4013 if (ret) {
139ac872
MD
4014 err = 0;
4015 goto exit;
44a5e5eb
DG
4016 }
4017
4018 /* Event on the registration socket */
4019 if (pollfd == sock) {
4020 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4021 ERR("Health socket poll error");
4022 goto error;
4023 }
4024 }
4025 }
4026
4027 new_sock = lttcomm_accept_unix_sock(sock);
4028 if (new_sock < 0) {
4029 goto error;
4030 }
4031
b662582b
DG
4032 /*
4033 * Set the CLOEXEC flag. Return code is useless because either way, the
4034 * show must go on.
4035 */
4036 (void) utils_set_fd_cloexec(new_sock);
4037
44a5e5eb
DG
4038 DBG("Receiving data from client for health...");
4039 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4040 if (ret <= 0) {
4041 DBG("Nothing recv() from client... continuing");
4042 ret = close(new_sock);
4043 if (ret) {
4044 PERROR("close");
4045 }
4046 new_sock = -1;
4047 continue;
4048 }
4049
4050 rcu_thread_online();
4051
53efb85a 4052 memset(&reply, 0, sizeof(reply));
6c71277b
MD
4053 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
4054 /*
4055 * health_check_state returns 0 if health is
4056 * bad.
4057 */
4058 if (!health_check_state(health_sessiond, i)) {
4059 reply.ret_code |= 1ULL << i;
4060 }
44a5e5eb
DG
4061 }
4062
6c71277b 4063 DBG2("Health check return value %" PRIx64, reply.ret_code);
44a5e5eb
DG
4064
4065 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4066 if (ret < 0) {
4067 ERR("Failed to send health data back to client");
4068 }
4069
4070 /* End of transmission */
4071 ret = close(new_sock);
4072 if (ret) {
4073 PERROR("close");
4074 }
4075 new_sock = -1;
4076 }
4077
139ac872 4078exit:
44a5e5eb 4079error:
139ac872
MD
4080 if (err) {
4081 ERR("Health error occurred in %s", __func__);
4082 }
44a5e5eb
DG
4083 DBG("Health check thread dying");
4084 unlink(health_unix_sock_path);
4085 if (sock >= 0) {
4086 ret = close(sock);
4087 if (ret) {
4088 PERROR("close");
4089 }
4090 }
44a5e5eb
DG
4091
4092 lttng_poll_clean(&events);
4093
4094 rcu_unregister_thread();
4095 return NULL;
4096}
4097
1d4b027a 4098/*
d063d709
DG
4099 * This thread manage all clients request using the unix client socket for
4100 * communication.
1d4b027a
DG
4101 */
4102static void *thread_manage_clients(void *data)
4103{
139ac872 4104 int sock = -1, ret, i, pollfd, err = -1;
53a80697 4105 int sock_error;
5eb91c98 4106 uint32_t revents, nb_fd;
273ea72c 4107 struct command_ctx *cmd_ctx = NULL;
5eb91c98 4108 struct lttng_poll_event events;
1d4b027a
DG
4109
4110 DBG("[thread] Manage client started");
4111
f6a9efaa
DG
4112 rcu_register_thread();
4113
6c71277b 4114 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
927ca06a 4115
840cb59c 4116 health_code_update();
44a5e5eb 4117
1d4b027a
DG
4118 ret = lttcomm_listen_unix_sock(client_sock);
4119 if (ret < 0) {
35df2755 4120 goto error_listen;
1d4b027a
DG
4121 }
4122
5eb91c98
DG
4123 /*
4124 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4125 * more will be added to this poll set.
4126 */
d0b96690 4127 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 4128 if (ret < 0) {
35df2755 4129 goto error_create_poll;
5eb91c98 4130 }
273ea72c 4131
5eb91c98
DG
4132 /* Add the application registration socket */
4133 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4134 if (ret < 0) {
4135 goto error;
4136 }
273ea72c 4137
ef367a93
JG
4138 sessiond_notify_ready();
4139 ret = sem_post(&load_info->message_thread_ready);
4140 if (ret) {
4141 PERROR("sem_post message_thread_ready");
4142 goto error;
4143 }
0bb7724a 4144
72453c8c
MD
4145 /* This testpoint is after we signal readiness to the parent. */
4146 if (testpoint(sessiond_thread_manage_clients)) {
4147 goto error;
4148 }
4149
e547b070 4150 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
6993eeb3
CB
4151 goto error;
4152 }
8ac94142 4153
840cb59c 4154 health_code_update();
44a5e5eb 4155
1d4b027a 4156 while (1) {
1d4b027a 4157 DBG("Accepting client command ...");
273ea72c
DG
4158
4159 /* Inifinite blocking call, waiting for transmission */
88f2b785 4160 restart:
a78af745 4161 health_poll_entry();
5eb91c98 4162 ret = lttng_poll_wait(&events, -1);
a78af745 4163 health_poll_exit();
273ea72c 4164 if (ret < 0) {
88f2b785
MD
4165 /*
4166 * Restart interrupted system call.
4167 */
4168 if (errno == EINTR) {
4169 goto restart;
4170 }
273ea72c
DG
4171 goto error;
4172 }
4173
0d9c5d77
DG
4174 nb_fd = ret;
4175
5eb91c98
DG
4176 for (i = 0; i < nb_fd; i++) {
4177 /* Fetch once the poll data */
4178 revents = LTTNG_POLL_GETEV(&events, i);
4179 pollfd = LTTNG_POLL_GETFD(&events, i);
4180
840cb59c 4181 health_code_update();
44a5e5eb 4182
fd20dac9
MD
4183 if (!revents) {
4184 /* No activity for this FD (poll implementation). */
4185 continue;
4186 }
4187
5eb91c98 4188 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4189 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 4190 if (ret) {
139ac872
MD
4191 err = 0;
4192 goto exit;
5eb91c98
DG
4193 }
4194
4195 /* Event on the registration socket */
4196 if (pollfd == client_sock) {
4197 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4198 ERR("Client socket poll error");
4199 goto error;
4200 }
4201 }
4202 }
4203
4204 DBG("Wait for client response");
4205
840cb59c 4206 health_code_update();
44a5e5eb 4207
5eb91c98
DG
4208 sock = lttcomm_accept_unix_sock(client_sock);
4209 if (sock < 0) {
273ea72c 4210 goto error;
273ea72c
DG
4211 }
4212
b662582b
DG
4213 /*
4214 * Set the CLOEXEC flag. Return code is useless because either way, the
4215 * show must go on.
4216 */
4217 (void) utils_set_fd_cloexec(sock);
4218
be040666
DG
4219 /* Set socket option for credentials retrieval */
4220 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4221 if (ret < 0) {
4222 goto error;
4223 }
4224
5eb91c98 4225 /* Allocate context command to process the client request */
ba7f0ae5 4226 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 4227 if (cmd_ctx == NULL) {
76d7553f 4228 PERROR("zmalloc cmd_ctx");
1d4b027a 4229 goto error;
5eb91c98 4230 }
1d4b027a 4231
5eb91c98 4232 /* Allocate data buffer for reception */
ba7f0ae5 4233 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 4234 if (cmd_ctx->lsm == NULL) {
76d7553f 4235 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
4236 goto error;
4237 }
1d4b027a 4238
5eb91c98
DG
4239 cmd_ctx->llm = NULL;
4240 cmd_ctx->session = NULL;
1d4b027a 4241
840cb59c 4242 health_code_update();
44a5e5eb 4243
5eb91c98
DG
4244 /*
4245 * Data is received from the lttng client. The struct
4246 * lttcomm_session_msg (lsm) contains the command and data request of
4247 * the client.
4248 */
4249 DBG("Receiving data from client ...");
be040666
DG
4250 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4251 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
4252 if (ret <= 0) {
4253 DBG("Nothing recv() from client... continuing");
76d7553f
MD
4254 ret = close(sock);
4255 if (ret) {
4256 PERROR("close");
4257 }
4258 sock = -1;
a2c0da86 4259 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4260 continue;
4261 }
1d4b027a 4262
840cb59c 4263 health_code_update();
44a5e5eb 4264
5eb91c98
DG
4265 // TODO: Validate cmd_ctx including sanity check for
4266 // security purpose.
f7776ea7 4267
f6a9efaa 4268 rcu_thread_online();
5eb91c98
DG
4269 /*
4270 * This function dispatch the work to the kernel or userspace tracer
4271 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4272 * informations for the client. The command context struct contains
4273 * everything this function may needs.
4274 */
53a80697 4275 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 4276 rcu_thread_offline();
5eb91c98 4277 if (ret < 0) {
36134aa1
DG
4278 ret = close(sock);
4279 if (ret) {
4280 PERROR("close");
53a80697 4281 }
36134aa1 4282 sock = -1;
bbd973c2 4283 /*
5eb91c98 4284 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 4285 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
4286 * (ENOMEM). Error detected but still accept
4287 * command, unless a socket error has been
4288 * detected.
bbd973c2 4289 */
bbd973c2 4290 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4291 continue;
4292 }
d6e4fca4 4293
840cb59c 4294 health_code_update();
44a5e5eb 4295
54d01ffb
DG
4296 DBG("Sending response (size: %d, retcode: %s)",
4297 cmd_ctx->lttng_msg_size,
9a745bc7 4298 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 4299 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
4300 if (ret < 0) {
4301 ERR("Failed to send data back to client");
bbd973c2 4302 }
1d4b027a 4303
5eb91c98 4304 /* End of transmission */
76d7553f
MD
4305 ret = close(sock);
4306 if (ret) {
4307 PERROR("close");
4308 }
4309 sock = -1;
be040666
DG
4310
4311 clean_command_ctx(&cmd_ctx);
44a5e5eb 4312
840cb59c 4313 health_code_update();
273ea72c
DG
4314 }
4315
139ac872 4316exit:
5eb91c98 4317error:
35df2755
CB
4318 if (sock >= 0) {
4319 ret = close(sock);
4320 if (ret) {
4321 PERROR("close");
4322 }
139ac872 4323 }
44a5e5eb 4324
35df2755
CB
4325 lttng_poll_clean(&events);
4326 clean_command_ctx(&cmd_ctx);
4327
4328error_listen:
4329error_create_poll:
273ea72c 4330 unlink(client_unix_sock_path);
76d7553f
MD
4331 if (client_sock >= 0) {
4332 ret = close(client_sock);
4333 if (ret) {
4334 PERROR("close");
4335 }
a4b35e07 4336 }
35df2755
CB
4337
4338 if (err) {
840cb59c 4339 health_error();
35df2755 4340 ERR("Health error occurred in %s", __func__);
a4b35e07 4341 }
273ea72c 4342
8782cc74 4343 health_unregister(health_sessiond);
35df2755
CB
4344
4345 DBG("Client thread dying");
f6a9efaa
DG
4346
4347 rcu_unregister_thread();
4a15001e
MD
4348
4349 /*
4350 * Since we are creating the consumer threads, we own them, so we need
4351 * to join them before our thread exits.
4352 */
4353 ret = join_consumer_thread(&kconsumer_data);
4354 if (ret) {
4355 errno = ret;
4356 PERROR("join_consumer");
4357 }
4358
4359 ret = join_consumer_thread(&ustconsumer32_data);
4360 if (ret) {
4361 errno = ret;
4362 PERROR("join_consumer ust32");
4363 }
4364
4365 ret = join_consumer_thread(&ustconsumer64_data);
4366 if (ret) {
4367 errno = ret;
4368 PERROR("join_consumer ust64");
4369 }
1d4b027a
DG
4370 return NULL;
4371}
4372
4373
fac6795d
DG
4374/*
4375 * usage function on stderr
4376 */
4377static void usage(void)
4378{
b716ce68 4379 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
4380 fprintf(stderr, " -h, --help Display this usage.\n");
4381 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
4382 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4383 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4384 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
4385 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4386 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4387 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4388 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
4389 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4390 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4391 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4392 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150 4393 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
72dd7491 4394 fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n");
d6f42150
DG
4395 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4396 fprintf(stderr, " -V, --version Show version number.\n");
7fe6d2c5 4397 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
d6f42150
DG
4398 fprintf(stderr, " -q, --quiet No output at all.\n");
4399 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
35f90c40 4400 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
3bd1e081 4401 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 4402 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
1b2ef7fe 4403 fprintf(stderr, " --agent-tcp-port Agent registration TCP port\n");
76d01ebb 4404 fprintf(stderr, " -f --config PATH Load daemon configuration file\n");
ef367a93 4405 fprintf(stderr, " -l --load PATH Load session configuration\n");
fbb9748b 4406 fprintf(stderr, " --kmod-probes Specify kernel module probes to load\n");
c9d42407 4407 fprintf(stderr, " --extra-kmod-probes Specify extra kernel module probes to load\n");
fac6795d
DG
4408}
4409
4410/*
26296c48
JG
4411 * Take an option from the getopt output and set it in the right variable to be
4412 * used later.
4413 *
4414 * Return 0 on success else a negative value.
fac6795d 4415 */
26296c48 4416static int set_option(int opt, const char *arg, const char *optname)
fac6795d 4417{
26296c48 4418 int ret = 0;
fac6795d 4419
c5d350b2
JG
4420 if (arg && arg[0] == '\0') {
4421 /*
4422 * This only happens if the value is read from daemon config
4423 * file. This means the option requires an argument and the
4424 * configuration file contains a line such as:
4425 * my_option =
4426 */
4427 ret = -EINVAL;
4428 goto end;
4429 }
4430
26296c48
JG
4431 switch (opt) {
4432 case 0:
4433 fprintf(stderr, "option %s", optname);
4434 if (arg) {
4435 fprintf(stderr, " with arg %s\n", arg);
fac6795d 4436 }
26296c48
JG
4437 break;
4438 case 'c':
e8fa9fb0
MD
4439 if (lttng_is_setuid_setgid()) {
4440 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4441 "-c, --client-sock");
4442 } else {
4443 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4444 }
26296c48
JG
4445 break;
4446 case 'a':
e8fa9fb0
MD
4447 if (lttng_is_setuid_setgid()) {
4448 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4449 "-a, --apps-sock");
4450 } else {
4451 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4452 }
26296c48
JG
4453 break;
4454 case 'd':
4455 opt_daemon = 1;
4456 break;
72dd7491
MD
4457 case 'b':
4458 opt_background = 1;
4459 break;
26296c48 4460 case 'g':
e8fa9fb0
MD
4461 if (lttng_is_setuid_setgid()) {
4462 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4463 "-g, --group");
4464 } else {
4465 /*
4466 * If the override option is set, the pointer points to a
4467 * *non* const thus freeing it even though the variable type is
4468 * set to const.
4469 */
4470 if (tracing_group_name_override) {
4471 free((void *) tracing_group_name);
4472 }
4473 tracing_group_name = strdup(arg);
4474 if (!tracing_group_name) {
4475 PERROR("strdup");
4476 ret = -ENOMEM;
4477 }
4478 tracing_group_name_override = 1;
db322c4d 4479 }
26296c48
JG
4480 break;
4481 case 'h':
4482 usage();
4a15001e 4483 exit(EXIT_SUCCESS);
26296c48
JG
4484 case 'V':
4485 fprintf(stdout, "%s\n", VERSION);
4486 exit(EXIT_SUCCESS);
4487 case 'S':
4488 opt_sig_parent = 1;
4489 break;
4490 case 'E':
e8fa9fb0
MD
4491 if (lttng_is_setuid_setgid()) {
4492 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4493 "--kconsumerd-err-sock");
4494 } else {
4495 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4496 }
26296c48
JG
4497 break;
4498 case 'C':
e8fa9fb0
MD
4499 if (lttng_is_setuid_setgid()) {
4500 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4501 "--kconsumerd-cmd-sock");
4502 } else {
4503 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4504 }
26296c48
JG
4505 break;
4506 case 'F':
e8fa9fb0
MD
4507 if (lttng_is_setuid_setgid()) {
4508 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4509 "--ustconsumerd64-err-sock");
4510 } else {
4511 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4512 }
26296c48
JG
4513 break;
4514 case 'D':
e8fa9fb0
MD
4515 if (lttng_is_setuid_setgid()) {
4516 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4517 "--ustconsumerd64-cmd-sock");
4518 } else {
4519 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4520 }
26296c48
JG
4521 break;
4522 case 'H':
e8fa9fb0
MD
4523 if (lttng_is_setuid_setgid()) {
4524 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4525 "--ustconsumerd32-err-sock");
4526 } else {
4527 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4528 }
26296c48
JG
4529 break;
4530 case 'G':
e8fa9fb0
MD
4531 if (lttng_is_setuid_setgid()) {
4532 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4533 "--ustconsumerd32-cmd-sock");
4534 } else {
4535 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4536 }
26296c48
JG
4537 break;
4538 case 'N':
4539 opt_no_kernel = 1;
4540 break;
4541 case 'q':
4542 lttng_opt_quiet = 1;
4543 break;
4544 case 'v':
4545 /* Verbose level can increase using multiple -v */
4546 if (arg) {
13755a18 4547 /* Value obtained from config file */
26296c48
JG
4548 lttng_opt_verbose = config_parse_value(arg);
4549 } else {
13755a18
JG
4550 /* -v used on command line */
4551 lttng_opt_verbose++;
26296c48 4552 }
13755a18
JG
4553 /* Clamp value to [0, 3] */
4554 lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 :
4555 (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3);
26296c48
JG
4556 break;
4557 case 'Z':
4558 if (arg) {
4559 opt_verbose_consumer = config_parse_value(arg);
4560 } else {
3bd1e081 4561 opt_verbose_consumer += 1;
26296c48
JG
4562 }
4563 break;
4564 case 'u':
e8fa9fb0
MD
4565 if (lttng_is_setuid_setgid()) {
4566 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4567 "--consumerd32-path");
4568 } else {
4569 if (consumerd32_bin_override) {
4570 free((void *) consumerd32_bin);
4571 }
4572 consumerd32_bin = strdup(arg);
4573 if (!consumerd32_bin) {
4574 PERROR("strdup");
4575 ret = -ENOMEM;
4576 }
4577 consumerd32_bin_override = 1;
db322c4d 4578 }
26296c48
JG
4579 break;
4580 case 'U':
e8fa9fb0
MD
4581 if (lttng_is_setuid_setgid()) {
4582 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4583 "--consumerd32-libdir");
4584 } else {
4585 if (consumerd32_libdir_override) {
4586 free((void *) consumerd32_libdir);
4587 }
4588 consumerd32_libdir = strdup(arg);
4589 if (!consumerd32_libdir) {
4590 PERROR("strdup");
4591 ret = -ENOMEM;
4592 }
4593 consumerd32_libdir_override = 1;
db322c4d 4594 }
26296c48
JG
4595 break;
4596 case 't':
e8fa9fb0
MD
4597 if (lttng_is_setuid_setgid()) {
4598 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4599 "--consumerd64-path");
4600 } else {
4601 if (consumerd64_bin_override) {
4602 free((void *) consumerd64_bin);
4603 }
4604 consumerd64_bin = strdup(arg);
4605 if (!consumerd64_bin) {
4606 PERROR("strdup");
4607 ret = -ENOMEM;
4608 }
4609 consumerd64_bin_override = 1;
db322c4d 4610 }
26296c48
JG
4611 break;
4612 case 'T':
e8fa9fb0
MD
4613 if (lttng_is_setuid_setgid()) {
4614 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4615 "--consumerd64-libdir");
4616 } else {
4617 if (consumerd64_libdir_override) {
4618 free((void *) consumerd64_libdir);
4619 }
4620 consumerd64_libdir = strdup(arg);
4621 if (!consumerd64_libdir) {
4622 PERROR("strdup");
4623 ret = -ENOMEM;
4624 }
4625 consumerd64_libdir_override = 1;
db322c4d 4626 }
26296c48
JG
4627 break;
4628 case 'p':
e8fa9fb0
MD
4629 if (lttng_is_setuid_setgid()) {
4630 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4631 "-p, --pidfile");
4632 } else {
4633 free(opt_pidfile);
4634 opt_pidfile = strdup(arg);
4635 if (!opt_pidfile) {
4636 PERROR("strdup");
4637 ret = -ENOMEM;
4638 }
db322c4d 4639 }
26296c48 4640 break;
022d91ba 4641 case 'J': /* Agent TCP port. */
26296c48 4642 {
e8fa9fb0
MD
4643 if (lttng_is_setuid_setgid()) {
4644 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4645 "--agent-tcp-port");
4646 } else {
4647 unsigned long v;
4d076222 4648
e04aa23e
JG
4649 if (!arg) {
4650 ret = -EINVAL;
4651 goto end;
4652 }
e8fa9fb0
MD
4653 errno = 0;
4654 v = strtoul(arg, NULL, 0);
4655 if (errno != 0 || !isdigit(arg[0])) {
4656 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4657 return -1;
4658 }
4659 if (v == 0 || v >= 65535) {
4660 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4661 return -1;
4662 }
4663 agent_tcp_port = (uint32_t) v;
4664 DBG3("Agent TCP port set to non default: %u", agent_tcp_port);
26296c48 4665 }
26296c48
JG
4666 break;
4667 }
ef367a93 4668 case 'l':
e8fa9fb0
MD
4669 if (lttng_is_setuid_setgid()) {
4670 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4671 "-l, --load");
4672 } else {
4673 free(opt_load_session_path);
4674 opt_load_session_path = strdup(arg);
4675 if (!opt_load_session_path) {
4676 PERROR("strdup");
4677 ret = -ENOMEM;
4678 }
ef367a93
JG
4679 }
4680 break;
fbb9748b 4681 case 'P': /* probe modules list */
e8fa9fb0
MD
4682 if (lttng_is_setuid_setgid()) {
4683 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4684 "--kmod-probes");
4685 } else {
4686 free(kmod_probes_list);
4687 kmod_probes_list = strdup(arg);
4688 if (!kmod_probes_list) {
4689 PERROR("strdup");
4690 ret = -ENOMEM;
4691 }
c9d42407
PP
4692 }
4693 break;
4694 case 'e':
e8fa9fb0
MD
4695 if (lttng_is_setuid_setgid()) {
4696 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4697 "--extra-kmod-probes");
4698 } else {
4699 free(kmod_extra_probes_list);
4700 kmod_extra_probes_list = strdup(arg);
4701 if (!kmod_extra_probes_list) {
4702 PERROR("strdup");
4703 ret = -ENOMEM;
4704 }
db322c4d 4705 }
fbb9748b 4706 break;
7ab02a27
DG
4707 case 'f':
4708 /* This is handled in set_options() thus silent break. */
4709 break;
26296c48
JG
4710 default:
4711 /* Unknown option or other error.
4712 * Error is printed by getopt, just return */
4713 ret = -1;
4714 }
4715
f3dd7ce7 4716end:
c5d350b2
JG
4717 if (ret == -EINVAL) {
4718 const char *opt_name = "unknown";
4719 int i;
4720
4721 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
4722 i++) {
4723 if (opt == long_options[i].val) {
4724 opt_name = long_options[i].name;
4725 break;
4726 }
4727 }
4728
4729 WARN("Invalid argument provided for option \"%s\", using default value.",
4730 opt_name);
4731 }
f3dd7ce7 4732
26296c48
JG
4733 return ret;
4734}
4735
4736/*
4737 * config_entry_handler_cb used to handle options read from a config file.
4738 * See config_entry_handler_cb comment in common/config/config.h for the
4739 * return value conventions.
4740 */
4741static int config_entry_handler(const struct config_entry *entry, void *unused)
4742{
4743 int ret = 0, i;
4744
4745 if (!entry || !entry->name || !entry->value) {
4746 ret = -EINVAL;
4747 goto end;
4748 }
4749
4750 /* Check if the option is to be ignored */
4751 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4752 if (!strcmp(entry->name, config_ignore_options[i])) {
4753 goto end;
4754 }
4755 }
4756
4757 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4758 i++) {
4759
4760 /* Ignore if not fully matched. */
4761 if (strcmp(entry->name, long_options[i].name)) {
4762 continue;
4763 }
4764
4765 /*
4766 * If the option takes no argument on the command line, we have to
4767 * check if the value is "true". We support non-zero numeric values,
4768 * true, on and yes.
4769 */
4770 if (!long_options[i].has_arg) {
4771 ret = config_parse_value(entry->value);
4772 if (ret <= 0) {
4773 if (ret) {
4774 WARN("Invalid configuration value \"%s\" for option %s",
4775 entry->value, entry->name);
4776 }
4777 /* False, skip boolean config option. */
4778 goto end;
4d076222 4779 }
26296c48
JG
4780 }
4781
4782 ret = set_option(long_options[i].val, entry->value, entry->name);
4783 goto end;
4784 }
4785
4786 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4787
4788end:
4789 return ret;
4790}
4791
4792/*
4793 * daemon configuration loading and argument parsing
4794 */
4795static int set_options(int argc, char **argv)
4796{
4797 int ret = 0, c = 0, option_index = 0;
4798 int orig_optopt = optopt, orig_optind = optind;
4799 char *optstring;
4800 const char *config_path = NULL;
4801
4802 optstring = utils_generate_optstring(long_options,
4803 sizeof(long_options) / sizeof(struct option));
4804 if (!optstring) {
4805 ret = -ENOMEM;
4806 goto end;
4807 }
4808
4809 /* Check for the --config option */
4810 while ((c = getopt_long(argc, argv, optstring, long_options,
4811 &option_index)) != -1) {
4812 if (c == '?') {
4813 ret = -EINVAL;
4814 goto end;
4815 } else if (c != 'f') {
4816 /* if not equal to --config option. */
4817 continue;
4818 }
4819
e8fa9fb0
MD
4820 if (lttng_is_setuid_setgid()) {
4821 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4822 "-f, --config");
4823 } else {
4824 config_path = utils_expand_path(optarg);
4825 if (!config_path) {
4826 ERR("Failed to resolve path: %s", optarg);
4827 }
26296c48
JG
4828 }
4829 }
4830
4831 ret = config_get_section_entries(config_path, config_section_name,
4832 config_entry_handler, NULL);
4833 if (ret) {
4834 if (ret > 0) {
4835 ERR("Invalid configuration option at line %i", ret);
4836 ret = -1;
4837 }
4838 goto end;
4839 }
4840
4841 /* Reset getopt's global state */
4842 optopt = orig_optopt;
4843 optind = orig_optind;
4844 while (1) {
4845 c = getopt_long(argc, argv, optstring, long_options, &option_index);
4846 if (c == -1) {
4d076222
DG
4847 break;
4848 }
26296c48
JG
4849
4850 ret = set_option(c, optarg, long_options[option_index].name);
4851 if (ret < 0) {
4852 break;
fac6795d
DG
4853 }
4854 }
4855
26296c48
JG
4856end:
4857 free(optstring);
4858 return ret;
fac6795d
DG
4859}
4860
4861/*
d063d709 4862 * Creates the two needed socket by the daemon.
d6f42150
DG
4863 * apps_sock - The communication socket for all UST apps.
4864 * client_sock - The communication of the cli tool (lttng).
fac6795d 4865 */
cf3af59e 4866static int init_daemon_socket(void)
fac6795d
DG
4867{
4868 int ret = 0;
4869 mode_t old_umask;
4870
4871 old_umask = umask(0);
4872
4873 /* Create client tool unix socket */
d6f42150
DG
4874 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4875 if (client_sock < 0) {
4876 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
4877 ret = -1;
4878 goto end;
4879 }
4880
b662582b
DG
4881 /* Set the cloexec flag */
4882 ret = utils_set_fd_cloexec(client_sock);
4883 if (ret < 0) {
4884 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4885 "Continuing but note that the consumer daemon will have a "
4886 "reference to this socket on exec()", client_sock);
4887 }
4888
fac6795d
DG
4889 /* File permission MUST be 660 */
4890 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4891 if (ret < 0) {
d6f42150 4892 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 4893 PERROR("chmod");
fac6795d
DG
4894 goto end;
4895 }
4896
4897 /* Create the application unix socket */
d6f42150
DG
4898 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4899 if (apps_sock < 0) {
4900 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
4901 ret = -1;
4902 goto end;
4903 }
4904
b662582b
DG
4905 /* Set the cloexec flag */
4906 ret = utils_set_fd_cloexec(apps_sock);
4907 if (ret < 0) {
4908 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4909 "Continuing but note that the consumer daemon will have a "
4910 "reference to this socket on exec()", apps_sock);
4911 }
4912
d6f42150 4913 /* File permission MUST be 666 */
54d01ffb
DG
4914 ret = chmod(apps_unix_sock_path,
4915 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 4916 if (ret < 0) {
d6f42150 4917 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 4918 PERROR("chmod");
fac6795d
DG
4919 goto end;
4920 }
4921
b662582b
DG
4922 DBG3("Session daemon client socket %d and application socket %d created",
4923 client_sock, apps_sock);
4924
fac6795d
DG
4925end:
4926 umask(old_umask);
4927 return ret;
4928}
4929
4930/*
54d01ffb
DG
4931 * Check if the global socket is available, and if a daemon is answering at the
4932 * other side. If yes, error is returned.
fac6795d 4933 */
cf3af59e 4934static int check_existing_daemon(void)
fac6795d 4935{
7d8234d9 4936 /* Is there anybody out there ? */
099e26bd 4937 if (lttng_session_daemon_alive()) {
7d8234d9 4938 return -EEXIST;
099e26bd 4939 }
b09c7c76
DG
4940
4941 return 0;
fac6795d
DG
4942}
4943
fac6795d 4944/*
d063d709 4945 * Set the tracing group gid onto the client socket.
5e16da05 4946 *
d063d709 4947 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 4948 * permissive (root.root) to less permissive (root.tracing).
fac6795d 4949 */
be040666 4950static int set_permissions(char *rundir)
fac6795d
DG
4951{
4952 int ret;
996b65c8 4953 gid_t gid;
fac6795d 4954
6c71277b 4955 gid = utils_get_group_id(tracing_group_name);
fac6795d 4956
d6f42150 4957 /* Set lttng run dir */
be040666 4958 ret = chown(rundir, 0, gid);
d6f42150 4959 if (ret < 0) {
be040666 4960 ERR("Unable to set group on %s", rundir);
76d7553f 4961 PERROR("chown");
d6f42150
DG
4962 }
4963
6c71277b
MD
4964 /*
4965 * Ensure all applications and tracing group can search the run
4966 * dir. Allow everyone to read the directory, since it does not
4967 * buy us anything to hide its content.
4968 */
4969 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
4970 if (ret < 0) {
4971 ERR("Unable to set permissions on %s", rundir);
76d7553f 4972 PERROR("chmod");
d1613cf5
JN
4973 }
4974
d6f42150 4975 /* lttng client socket path */
996b65c8 4976 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 4977 if (ret < 0) {
d6f42150 4978 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 4979 PERROR("chown");
d6f42150
DG
4980 }
4981
3bd1e081 4982 /* kconsumer error socket path */
6c71277b 4983 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 4984 if (ret < 0) {
3bd1e081 4985 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 4986 PERROR("chown");
3bd1e081
MD
4987 }
4988
7753dea8 4989 /* 64-bit ustconsumer error socket path */
6c71277b 4990 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
4991 if (ret < 0) {
4992 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 4993 PERROR("chown");
7753dea8
MD
4994 }
4995
4996 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 4997 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 4998 if (ret < 0) {
7753dea8 4999 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5000 PERROR("chown");
fac6795d
DG
5001 }
5002
d6f42150 5003 DBG("All permissions are set");
e07ae692 5004
fac6795d
DG
5005 return ret;
5006}
5007
d6f42150 5008/*
d063d709 5009 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5010 */
67e40797 5011static int create_lttng_rundir(const char *rundir)
d6f42150
DG
5012{
5013 int ret;
5014
67e40797
DG
5015 DBG3("Creating LTTng run directory: %s", rundir);
5016
d1613cf5 5017 ret = mkdir(rundir, S_IRWXU);
d6f42150 5018 if (ret < 0) {
b1f11e69 5019 if (errno != EEXIST) {
67e40797 5020 ERR("Unable to create %s", rundir);
b1f11e69
DG
5021 goto error;
5022 } else {
5023 ret = 0;
5024 }
d6f42150
DG
5025 }
5026
5027error:
5028 return ret;
5029}
5030
5031/*
d063d709
DG
5032 * Setup sockets and directory needed by the kconsumerd communication with the
5033 * session daemon.
d6f42150 5034 */
67e40797
DG
5035static int set_consumer_sockets(struct consumer_data *consumer_data,
5036 const char *rundir)
d6f42150
DG
5037{
5038 int ret;
67e40797 5039 char path[PATH_MAX];
d6f42150 5040
6c71277b 5041 switch (consumer_data->type) {
7753dea8 5042 case LTTNG_CONSUMER_KERNEL:
60922cb0 5043 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
5044 break;
5045 case LTTNG_CONSUMER64_UST:
60922cb0 5046 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
5047 break;
5048 case LTTNG_CONSUMER32_UST:
60922cb0 5049 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
5050 break;
5051 default:
5052 ERR("Consumer type unknown");
5053 ret = -EINVAL;
5054 goto error;
d6f42150
DG
5055 }
5056
67e40797
DG
5057 DBG2("Creating consumer directory: %s", path);
5058
6c71277b 5059 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
d6f42150 5060 if (ret < 0) {
6beb2242 5061 if (errno != EEXIST) {
f11e84c2 5062 PERROR("mkdir");
3bd1e081 5063 ERR("Failed to create %s", path);
6beb2242
DG
5064 goto error;
5065 }
f11e84c2 5066 ret = -1;
d6f42150 5067 }
6c71277b
MD
5068 if (is_root) {
5069 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
5070 if (ret < 0) {
5071 ERR("Unable to set group on %s", path);
5072 PERROR("chown");
5073 goto error;
5074 }
5075 }
d6f42150
DG
5076
5077 /* Create the kconsumerd error unix socket */
3bd1e081
MD
5078 consumer_data->err_sock =
5079 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5080 if (consumer_data->err_sock < 0) {
5081 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5082 ret = -1;
5083 goto error;
5084 }
5085
a24f05ab
MD
5086 /*
5087 * Set the CLOEXEC flag. Return code is useless because either way, the
5088 * show must go on.
5089 */
5090 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5091 if (ret < 0) {
5092 PERROR("utils_set_fd_cloexec");
5093 /* continue anyway */
5094 }
5095
d6f42150 5096 /* File permission MUST be 660 */
3bd1e081 5097 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5098 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5099 if (ret < 0) {
3bd1e081 5100 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5101 PERROR("chmod");
d6f42150
DG
5102 goto error;
5103 }
5104
5105error:
5106 return ret;
5107}
5108
fac6795d 5109/*
d063d709 5110 * Signal handler for the daemon
cf3af59e 5111 *
54d01ffb
DG
5112 * Simply stop all worker threads, leaving main() return gracefully after
5113 * joining all threads and calling cleanup().
fac6795d
DG
5114 */
5115static void sighandler(int sig)
5116{
5117 switch (sig) {
cf3af59e 5118 case SIGPIPE:
af87c45a 5119 DBG("SIGPIPE caught");
cf3af59e
MD
5120 return;
5121 case SIGINT:
af87c45a 5122 DBG("SIGINT caught");
cf3af59e
MD
5123 stop_threads();
5124 break;
5125 case SIGTERM:
af87c45a 5126 DBG("SIGTERM caught");
cf3af59e
MD
5127 stop_threads();
5128 break;
0bb7724a
DG
5129 case SIGUSR1:
5130 CMM_STORE_SHARED(recv_child_signal, 1);
5131 break;
cf3af59e
MD
5132 default:
5133 break;
fac6795d 5134 }
fac6795d
DG
5135}
5136
5137/*
d063d709 5138 * Setup signal handler for :
1d4b027a 5139 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5140 */
1d4b027a 5141static int set_signal_handler(void)
fac6795d 5142{
1d4b027a
DG
5143 int ret = 0;
5144 struct sigaction sa;
5145 sigset_t sigset;
fac6795d 5146
1d4b027a 5147 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5148 PERROR("sigemptyset");
1d4b027a
DG
5149 return ret;
5150 }
d6f42150 5151
1d4b027a
DG
5152 sa.sa_handler = sighandler;
5153 sa.sa_mask = sigset;
5154 sa.sa_flags = 0;
5155 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5156 PERROR("sigaction");
1d4b027a 5157 return ret;
d6f42150
DG
5158 }
5159
1d4b027a 5160 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5161 PERROR("sigaction");
1d4b027a 5162 return ret;
d6f42150 5163 }
aaf26714 5164
1d4b027a 5165 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 5166 PERROR("sigaction");
1d4b027a 5167 return ret;
8c0faa1d
DG
5168 }
5169
0bb7724a
DG
5170 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
5171 PERROR("sigaction");
5172 return ret;
5173 }
5174
5175 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
5176
5177 return ret;
fac6795d
DG
5178}
5179
f3ed775e 5180/*
d063d709
DG
5181 * Set open files limit to unlimited. This daemon can open a large number of
5182 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
5183 */
5184static void set_ulimit(void)
5185{
5186 int ret;
5187 struct rlimit lim;
5188
a88df331 5189 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
5190 lim.rlim_cur = 65535;
5191 lim.rlim_max = 65535;
5192
5193 ret = setrlimit(RLIMIT_NOFILE, &lim);
5194 if (ret < 0) {
76d7553f 5195 PERROR("failed to set open files limit");
f3ed775e
DG
5196 }
5197}
5198
35f90c40
DG
5199/*
5200 * Write pidfile using the rundir and opt_pidfile.
5201 */
4a15001e 5202static int write_pidfile(void)
35f90c40
DG
5203{
5204 int ret;
5205 char pidfile_path[PATH_MAX];
5206
5207 assert(rundir);
5208
5209 if (opt_pidfile) {
5210 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
5211 } else {
5212 /* Build pidfile path from rundir and opt_pidfile. */
5213 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
5214 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
5215 if (ret < 0) {
5216 PERROR("snprintf pidfile path");
5217 goto error;
5218 }
5219 }
5220
5221 /*
4a15001e 5222 * Create pid file in rundir.
35f90c40 5223 */
4a15001e 5224 ret = utils_create_pid_file(getpid(), pidfile_path);
35f90c40 5225error:
4a15001e 5226 return ret;
35f90c40
DG
5227}
5228
c9cb3e7d
JG
5229/*
5230 * Create lockfile using the rundir and return its fd.
5231 */
5232static int create_lockfile(void)
5233{
5234 int ret;
5235 char lockfile_path[PATH_MAX];
5236
5237 ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path));
5238 if (ret < 0) {
5239 goto error;
5240 }
5241
5242 ret = utils_create_lock_file(lockfile_path);
5243error:
5244 return ret;
5245}
5246
cd9290dd 5247/*
022d91ba 5248 * Write agent TCP port using the rundir.
cd9290dd 5249 */
4a15001e 5250static int write_agent_port(void)
cd9290dd
DG
5251{
5252 int ret;
5253 char path[PATH_MAX];
5254
5255 assert(rundir);
5256
5257 ret = snprintf(path, sizeof(path), "%s/"
022d91ba 5258 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE, rundir);
cd9290dd 5259 if (ret < 0) {
022d91ba 5260 PERROR("snprintf agent port path");
cd9290dd
DG
5261 goto error;
5262 }
5263
5264 /*
4a15001e 5265 * Create TCP agent port file in rundir.
cd9290dd 5266 */
4a15001e 5267 ret = utils_create_pid_file(agent_tcp_port, path);
cd9290dd
DG
5268
5269error:
4a15001e 5270 return ret;
ef367a93
JG
5271}
5272
fac6795d
DG
5273/*
5274 * main
5275 */
5276int main(int argc, char **argv)
5277{
4a15001e 5278 int ret = 0, retval = 0;
fac6795d 5279 void *status;
ae9e45b3 5280 const char *home_path, *env_app_timeout;
fac6795d 5281
335a95b7
MD
5282 init_kernel_workarounds();
5283
f6a9efaa
DG
5284 rcu_register_thread();
5285
4a15001e
MD
5286 if (set_signal_handler()) {
5287 retval = -1;
5288 goto exit_set_signal_handler;
0bb7724a
DG
5289 }
5290
7753dea8 5291 setup_consumerd_path();
fb09408a 5292
12744796
DG
5293 page_size = sysconf(_SC_PAGESIZE);
5294 if (page_size < 0) {
5295 PERROR("sysconf _SC_PAGESIZE");
5296 page_size = LONG_MAX;
5297 WARN("Fallback page size to %ld", page_size);
5298 }
5299
4a15001e
MD
5300 /*
5301 * Parse arguments and load the daemon configuration file.
5302 *
5303 * We have an exit_options exit path to free memory reserved by
5304 * set_options. This is needed because the rest of sessiond_cleanup()
5305 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5306 * depends on set_options.
5307 */
fac6795d 5308 progname = argv[0];
4a15001e
MD
5309 if (set_options(argc, argv)) {
5310 retval = -1;
5311 goto exit_options;
fac6795d
DG
5312 }
5313
5314 /* Daemonize */
72dd7491 5315 if (opt_daemon || opt_background) {
ceed52b5
MD
5316 int i;
5317
72dd7491
MD
5318 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
5319 !opt_background);
53094c05 5320 if (ret < 0) {
4a15001e
MD
5321 retval = -1;
5322 goto exit_options;
53094c05 5323 }
0bb7724a 5324
ceed52b5 5325 /*
0bb7724a 5326 * We are in the child. Make sure all other file descriptors are
4a15001e
MD
5327 * closed, in case we are called with more opened file
5328 * descriptors than the standard ones.
ceed52b5
MD
5329 */
5330 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5331 (void) close(i);
5332 }
5333 }
5334
4a15001e
MD
5335 /*
5336 * Starting from here, we can create threads. This needs to be after
5337 * lttng_daemonize due to RCU.
5338 */
5339
5340 /*
5341 * Initialize the health check subsystem. This call should set the
5342 * appropriate time values.
5343 */
5344 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5345 if (!health_sessiond) {
5346 PERROR("health_app_create error");
5347 retval = -1;
5348 goto exit_health_sessiond_cleanup;
5349 }
5350
5351 if (init_ht_cleanup_quit_pipe()) {
5352 retval = -1;
5353 goto exit_ht_cleanup_quit_pipe;
5354 }
5355
5356 /* Setup the thread ht_cleanup communication pipe. */
5357 if (utils_create_pipe_cloexec(ht_cleanup_pipe)) {
5358 retval = -1;
5359 goto exit_ht_cleanup_pipe;
5360 }
5361
25b397f9
MD
5362 /* Set up max poll set size */
5363 if (lttng_poll_set_max_size()) {
5364 retval = -1;
5365 goto exit_set_max_size;
5366 }
5367
4a15001e
MD
5368 /* Create thread to clean up RCU hash tables */
5369 ret = pthread_create(&ht_cleanup_thread, NULL,
5370 thread_ht_cleanup, (void *) NULL);
5371 if (ret) {
5372 errno = ret;
5373 PERROR("pthread_create ht_cleanup");
5374 retval = -1;
5375 goto exit_ht_cleanup;
5376 }
5377
ceed52b5 5378 /* Create thread quit pipe */
4a15001e
MD
5379 if (init_thread_quit_pipe()) {
5380 retval = -1;
5381 goto exit_init_data;
fac6795d
DG
5382 }
5383
5384 /* Check if daemon is UID = 0 */
5385 is_root = !getuid();
5386
fac6795d 5387 if (is_root) {
990570ed 5388 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4ea184d1 5389 if (!rundir) {
4a15001e
MD
5390 retval = -1;
5391 goto exit_init_data;
4ea184d1 5392 }
67e40797
DG
5393
5394 /* Create global run dir with root access */
4a15001e
MD
5395 if (create_lttng_rundir(rundir)) {
5396 retval = -1;
5397 goto exit_init_data;
d6f42150
DG
5398 }
5399
fac6795d 5400 if (strlen(apps_unix_sock_path) == 0) {
4a15001e 5401 ret = snprintf(apps_unix_sock_path, PATH_MAX,
d6f42150 5402 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4a15001e
MD
5403 if (ret < 0) {
5404 retval = -1;
5405 goto exit_init_data;
5406 }
fac6795d
DG
5407 }
5408
5409 if (strlen(client_unix_sock_path) == 0) {
4a15001e 5410 ret = snprintf(client_unix_sock_path, PATH_MAX,
d6f42150 5411 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4a15001e
MD
5412 if (ret < 0) {
5413 retval = -1;
5414 goto exit_init_data;
5415 }
d6f42150 5416 }
0fdd1e2c
DG
5417
5418 /* Set global SHM for ust */
5419 if (strlen(wait_shm_path) == 0) {
4a15001e 5420 ret = snprintf(wait_shm_path, PATH_MAX,
0fdd1e2c 5421 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4a15001e
MD
5422 if (ret < 0) {
5423 retval = -1;
5424 goto exit_init_data;
5425 }
0fdd1e2c 5426 }
67e40797 5427
44a5e5eb 5428 if (strlen(health_unix_sock_path) == 0) {
4a15001e
MD
5429 ret = snprintf(health_unix_sock_path,
5430 sizeof(health_unix_sock_path),
44a5e5eb 5431 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4a15001e
MD
5432 if (ret < 0) {
5433 retval = -1;
5434 goto exit_init_data;
5435 }
44a5e5eb
DG
5436 }
5437
67e40797 5438 /* Setup kernel consumerd path */
4a15001e 5439 ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 5440 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4a15001e
MD
5441 if (ret < 0) {
5442 retval = -1;
5443 goto exit_init_data;
5444 }
5445 ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5446 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4a15001e
MD
5447 if (ret < 0) {
5448 retval = -1;
5449 goto exit_init_data;
5450 }
67e40797
DG
5451
5452 DBG2("Kernel consumer err path: %s",
5453 kconsumer_data.err_unix_sock_path);
5454 DBG2("Kernel consumer cmd path: %s",
5455 kconsumer_data.cmd_unix_sock_path);
fac6795d 5456 } else {
feb0f3e5 5457 home_path = utils_get_home_dir();
b082db07 5458 if (home_path == NULL) {
273ea72c
DG
5459 /* TODO: Add --socket PATH option */
5460 ERR("Can't get HOME directory for sockets creation.");
4a15001e
MD
5461 retval = -1;
5462 goto exit_init_data;
b082db07
DG
5463 }
5464
67e40797
DG
5465 /*
5466 * Create rundir from home path. This will create something like
5467 * $HOME/.lttng
5468 */
990570ed 5469 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797 5470 if (ret < 0) {
4a15001e
MD
5471 retval = -1;
5472 goto exit_init_data;
67e40797
DG
5473 }
5474
4a15001e
MD
5475 if (create_lttng_rundir(rundir)) {
5476 retval = -1;
5477 goto exit_init_data;
67e40797
DG
5478 }
5479
fac6795d 5480 if (strlen(apps_unix_sock_path) == 0) {
4a15001e
MD
5481 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5482 DEFAULT_HOME_APPS_UNIX_SOCK,
5483 home_path);
5484 if (ret < 0) {
5485 retval = -1;
5486 goto exit_init_data;
5487 }
fac6795d
DG
5488 }
5489
5490 /* Set the cli tool unix socket path */
5491 if (strlen(client_unix_sock_path) == 0) {
4a15001e
MD
5492 ret = snprintf(client_unix_sock_path, PATH_MAX,
5493 DEFAULT_HOME_CLIENT_UNIX_SOCK,
5494 home_path);
5495 if (ret < 0) {
5496 retval = -1;
5497 goto exit_init_data;
5498 }
fac6795d 5499 }
0fdd1e2c
DG
5500
5501 /* Set global SHM for ust */
5502 if (strlen(wait_shm_path) == 0) {
4a15001e
MD
5503 ret = snprintf(wait_shm_path, PATH_MAX,
5504 DEFAULT_HOME_APPS_WAIT_SHM_PATH,
5505 getuid());
5506 if (ret < 0) {
5507 retval = -1;
5508 goto exit_init_data;
5509 }
0fdd1e2c 5510 }
44a5e5eb
DG
5511
5512 /* Set health check Unix path */
5513 if (strlen(health_unix_sock_path) == 0) {
4a15001e
MD
5514 ret = snprintf(health_unix_sock_path,
5515 sizeof(health_unix_sock_path),
5516 DEFAULT_HOME_HEALTH_UNIX_SOCK,
5517 home_path);
5518 if (ret < 0) {
5519 retval = -1;
5520 goto exit_init_data;
5521 }
44a5e5eb 5522 }
fac6795d
DG
5523 }
5524
c9cb3e7d
JG
5525 lockfile_fd = create_lockfile();
5526 if (lockfile_fd < 0) {
4a15001e
MD
5527 retval = -1;
5528 goto exit_init_data;
c9cb3e7d
JG
5529 }
5530
5c827ce0
DG
5531 /* Set consumer initial state */
5532 kernel_consumerd_state = CONSUMER_STOPPED;
5533 ust_consumerd_state = CONSUMER_STOPPED;
5534
847177cd
DG
5535 DBG("Client socket path %s", client_unix_sock_path);
5536 DBG("Application socket path %s", apps_unix_sock_path);
d0b96690 5537 DBG("Application wait path %s", wait_shm_path);
67e40797
DG
5538 DBG("LTTng run directory path: %s", rundir);
5539
5540 /* 32 bits consumerd path setup */
4a15001e 5541 ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 5542 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4a15001e
MD
5543 if (ret < 0) {
5544 PERROR("snprintf 32-bit consumer error socket path");
5545 retval = -1;
5546 goto exit_init_data;
5547 }
5548 ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5549 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4a15001e
MD
5550 if (ret < 0) {
5551 PERROR("snprintf 32-bit consumer command socket path");
5552 retval = -1;
5553 goto exit_init_data;
5554 }
67e40797
DG
5555
5556 DBG2("UST consumer 32 bits err path: %s",
5557 ustconsumer32_data.err_unix_sock_path);
5558 DBG2("UST consumer 32 bits cmd path: %s",
5559 ustconsumer32_data.cmd_unix_sock_path);
5560
5561 /* 64 bits consumerd path setup */
4a15001e 5562 ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 5563 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4a15001e
MD
5564 if (ret < 0) {
5565 PERROR("snprintf 64-bit consumer error socket path");
5566 retval = -1;
5567 goto exit_init_data;
5568 }
5569 ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5570 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4a15001e
MD
5571 if (ret < 0) {
5572 PERROR("snprintf 64-bit consumer command socket path");
5573 retval = -1;
5574 goto exit_init_data;
5575 }
67e40797
DG
5576
5577 DBG2("UST consumer 64 bits err path: %s",
5578 ustconsumer64_data.err_unix_sock_path);
5579 DBG2("UST consumer 64 bits cmd path: %s",
5580 ustconsumer64_data.cmd_unix_sock_path);
847177cd 5581
273ea72c 5582 /*
7d8234d9 5583 * See if daemon already exist.
fac6795d 5584 */
4a15001e 5585 if (check_existing_daemon()) {
75462a81 5586 ERR("Already running daemon.\n");
273ea72c 5587 /*
cf3af59e
MD
5588 * We do not goto exit because we must not cleanup()
5589 * because a daemon is already running.
ab118b20 5590 */
4a15001e
MD
5591 retval = -1;
5592 goto exit_init_data;
a88df331
DG
5593 }
5594
1427f9b2
DG
5595 /*
5596 * Init UST app hash table. Alloc hash table before this point since
5597 * cleanup() can get called after that point.
5598 */
4a15001e
MD
5599 if (ust_app_ht_alloc()) {
5600 retval = -1;
5601 goto exit_init_data;
5602 }
1427f9b2 5603
022d91ba 5604 /* Initialize agent domain subsystem. */
4a15001e 5605 if (agent_setup()) {
f20baf8e 5606 /* ENOMEM at this point. */
4a15001e
MD
5607 retval = -1;
5608 goto exit_init_data;
f20baf8e
DG
5609 }
5610
a88df331
DG
5611 /*
5612 * These actions must be executed as root. We do that *after* setting up
5613 * the sockets path because we MUST make the check for another daemon using
5614 * those paths *before* trying to set the kernel consumer sockets and init
5615 * kernel tracer.
5616 */
5617 if (is_root) {
4a15001e
MD
5618 if (set_consumer_sockets(&kconsumer_data, rundir)) {
5619 retval = -1;
5620 goto exit_init_data;
7753dea8
MD
5621 }
5622
a88df331 5623 /* Setup kernel tracer */
4fba7219
DG
5624 if (!opt_no_kernel) {
5625 init_kernel_tracer();
89588270
DG
5626 if (kernel_tracer_fd >= 0) {
5627 ret = syscall_init_table();
5628 if (ret < 0) {
4a15001e
MD
5629 ERR("Unable to populate syscall table. "
5630 "Syscall tracing won't work "
5631 "for this session daemon.");
89588270 5632 }
834978fd 5633 }
4fba7219 5634 }
a88df331
DG
5635
5636 /* Set ulimit for open files */
5637 set_ulimit();
fac6795d 5638 }
4063050c
MD
5639 /* init lttng_fd tracking must be done after set_ulimit. */
5640 lttng_fd_init();
fac6795d 5641
4a15001e
MD
5642 if (set_consumer_sockets(&ustconsumer64_data, rundir)) {
5643 retval = -1;
5644 goto exit_init_data;
67e40797
DG
5645 }
5646
4a15001e
MD
5647 if (set_consumer_sockets(&ustconsumer32_data, rundir)) {
5648 retval = -1;
5649 goto exit_init_data;
67e40797
DG
5650 }
5651
d6f42150 5652 /* Setup the needed unix socket */
4a15001e
MD
5653 if (init_daemon_socket()) {
5654 retval = -1;
5655 goto exit_init_data;
fac6795d
DG
5656 }
5657
5658 /* Set credentials to socket */
4a15001e
MD
5659 if (is_root && set_permissions(rundir)) {
5660 retval = -1;
5661 goto exit_init_data;
fac6795d
DG
5662 }
5663
5b8719f5
DG
5664 /* Get parent pid if -S, --sig-parent is specified. */
5665 if (opt_sig_parent) {
5666 ppid = getppid();
5667 }
5668
7a485870 5669 /* Setup the kernel pipe for waking up the kernel thread */
6620da75 5670 if (is_root && !opt_no_kernel) {
4a15001e
MD
5671 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
5672 retval = -1;
5673 goto exit_init_data;
6620da75 5674 }
7a485870
DG
5675 }
5676
099e26bd 5677 /* Setup the thread apps communication pipe. */
4a15001e
MD
5678 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
5679 retval = -1;
5680 goto exit_init_data;
099e26bd
DG
5681 }
5682
d0b96690 5683 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
5684 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
5685 retval = -1;
5686 goto exit_init_data;
d0b96690
DG
5687 }
5688
7972aab2
DG
5689 /* Initialize global buffer per UID and PID registry. */
5690 buffer_reg_init_uid_registry();
5691 buffer_reg_init_pid_registry();
5692
099e26bd 5693 /* Init UST command queue. */
8bdee6e2 5694 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 5695
273ea72c 5696 /*
4a15001e
MD
5697 * Get session list pointer. This pointer MUST NOT be free'd. This list
5698 * is statically declared in session.c
273ea72c 5699 */
54d01ffb 5700 session_list_ptr = session_get_list();
b5541356 5701
2f77fc4b 5702 cmd_init();
00e2e675 5703
ae9e45b3
DG
5704 /* Check for the application socket timeout env variable. */
5705 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5706 if (env_app_timeout) {
5707 app_socket_timeout = atoi(env_app_timeout);
5708 } else {
5709 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5710 }
5711
4a15001e
MD
5712 ret = write_pidfile();
5713 if (ret) {
5714 ERR("Error in write_pidfile");
5715 retval = -1;
5716 goto exit_init_data;
5717 }
5718 ret = write_agent_port();
5719 if (ret) {
5720 ERR("Error in write_agent_port");
5721 retval = -1;
5722 goto exit_init_data;
5723 }
35f90c40 5724
554831e7
MD
5725 /* Initialize communication library */
5726 lttcomm_init();
4a15001e 5727 /* Initialize TCP timeout values */
d831c249 5728 lttcomm_inet_init();
554831e7 5729
ef367a93 5730 if (load_session_init_data(&load_info) < 0) {
4a15001e
MD
5731 retval = -1;
5732 goto exit_init_data;
ef367a93
JG
5733 }
5734 load_info->path = opt_load_session_path;
5735
d5b9fd2f 5736 /* Create health-check thread */
44a5e5eb
DG
5737 ret = pthread_create(&health_thread, NULL,
5738 thread_manage_health, (void *) NULL);
4a15001e
MD
5739 if (ret) {
5740 errno = ret;
44a5e5eb 5741 PERROR("pthread_create health");
4a15001e 5742 retval = -1;
44a5e5eb
DG
5743 goto exit_health;
5744 }
5745
cf3af59e 5746 /* Create thread to manage the client socket */
099e26bd
DG
5747 ret = pthread_create(&client_thread, NULL,
5748 thread_manage_clients, (void *) NULL);
4a15001e
MD
5749 if (ret) {
5750 errno = ret;
76d7553f 5751 PERROR("pthread_create clients");
4a15001e 5752 retval = -1;
cf3af59e
MD
5753 goto exit_client;
5754 }
fac6795d 5755
099e26bd
DG
5756 /* Create thread to dispatch registration */
5757 ret = pthread_create(&dispatch_thread, NULL,
5758 thread_dispatch_ust_registration, (void *) NULL);
4a15001e
MD
5759 if (ret) {
5760 errno = ret;
76d7553f 5761 PERROR("pthread_create dispatch");
4a15001e 5762 retval = -1;
099e26bd
DG
5763 goto exit_dispatch;
5764 }
5765
5766 /* Create thread to manage application registration. */
5767 ret = pthread_create(&reg_apps_thread, NULL,
5768 thread_registration_apps, (void *) NULL);
4a15001e
MD
5769 if (ret) {
5770 errno = ret;
76d7553f 5771 PERROR("pthread_create registration");
4a15001e 5772 retval = -1;
099e26bd
DG
5773 goto exit_reg_apps;
5774 }
5775
cf3af59e 5776 /* Create thread to manage application socket */
54d01ffb
DG
5777 ret = pthread_create(&apps_thread, NULL,
5778 thread_manage_apps, (void *) NULL);
4a15001e
MD
5779 if (ret) {
5780 errno = ret;
d0b96690 5781 PERROR("pthread_create apps");
4a15001e 5782 retval = -1;
d0b96690
DG
5783 goto exit_apps;
5784 }
5785
5786 /* Create thread to manage application notify socket */
5787 ret = pthread_create(&apps_notify_thread, NULL,
5788 ust_thread_manage_notify, (void *) NULL);
4a15001e
MD
5789 if (ret) {
5790 errno = ret;
f5c32ef1 5791 PERROR("pthread_create notify");
4a15001e 5792 retval = -1;
9563b0ad 5793 goto exit_apps_notify;
cf3af59e 5794 }
fac6795d 5795
022d91ba
DG
5796 /* Create agent registration thread. */
5797 ret = pthread_create(&agent_reg_thread, NULL,
5798 agent_thread_manage_registration, (void *) NULL);
4a15001e
MD
5799 if (ret) {
5800 errno = ret;
022d91ba 5801 PERROR("pthread_create agent");
4a15001e 5802 retval = -1;
022d91ba 5803 goto exit_agent_reg;
4d076222
DG
5804 }
5805
6620da75
DG
5806 /* Don't start this thread if kernel tracing is not requested nor root */
5807 if (is_root && !opt_no_kernel) {
5808 /* Create kernel thread to manage kernel event */
5809 ret = pthread_create(&kernel_thread, NULL,
5810 thread_manage_kernel, (void *) NULL);
4a15001e
MD
5811 if (ret) {
5812 errno = ret;
6620da75 5813 PERROR("pthread_create kernel");
4a15001e 5814 retval = -1;
6620da75
DG
5815 goto exit_kernel;
5816 }
ef367a93 5817 }
7a485870 5818
4a15001e
MD
5819 /* Create session loading thread. */
5820 ret = pthread_create(&load_session_thread, NULL, thread_load_session,
5821 load_info);
5822 if (ret) {
5823 errno = ret;
5824 PERROR("pthread_create load_session_thread");
5825 retval = -1;
5826 goto exit_load_session;
5827 }
5828
5829 /*
5830 * This is where we start awaiting program completion (e.g. through
5831 * signal that asks threads to teardown).
5832 */
5833
5834 ret = pthread_join(load_session_thread, &status);
5835 if (ret) {
5836 errno = ret;
5837 PERROR("pthread_join load_session_thread");
5838 retval = -1;
5839 }
5840exit_load_session:
ef367a93
JG
5841
5842 if (is_root && !opt_no_kernel) {
6620da75 5843 ret = pthread_join(kernel_thread, &status);
4a15001e
MD
5844 if (ret) {
5845 errno = ret;
6620da75 5846 PERROR("pthread_join");
4a15001e 5847 retval = -1;
6620da75 5848 }
fac6795d 5849 }
cf3af59e 5850exit_kernel:
4a15001e 5851
022d91ba 5852 ret = pthread_join(agent_reg_thread, &status);
4a15001e
MD
5853 if (ret) {
5854 errno = ret;
022d91ba 5855 PERROR("pthread_join agent");
4a15001e 5856 retval = -1;
4d076222 5857 }
022d91ba 5858exit_agent_reg:
4a15001e 5859
9563b0ad 5860 ret = pthread_join(apps_notify_thread, &status);
4a15001e
MD
5861 if (ret) {
5862 errno = ret;
9563b0ad 5863 PERROR("pthread_join apps notify");
4a15001e 5864 retval = -1;
9563b0ad 5865 }
9563b0ad 5866exit_apps_notify:
4a15001e 5867
cf3af59e 5868 ret = pthread_join(apps_thread, &status);
4a15001e
MD
5869 if (ret) {
5870 errno = ret;
9563b0ad 5871 PERROR("pthread_join apps");
4a15001e 5872 retval = -1;
cf3af59e 5873 }
cf3af59e 5874exit_apps:
4a15001e 5875
099e26bd 5876 ret = pthread_join(reg_apps_thread, &status);
4a15001e
MD
5877 if (ret) {
5878 errno = ret;
76d7553f 5879 PERROR("pthread_join");
4a15001e 5880 retval = -1;
099e26bd 5881 }
099e26bd 5882exit_reg_apps:
4a15001e 5883
099e26bd 5884 ret = pthread_join(dispatch_thread, &status);
4a15001e
MD
5885 if (ret) {
5886 errno = ret;
76d7553f 5887 PERROR("pthread_join");
4a15001e 5888 retval = -1;
099e26bd 5889 }
099e26bd 5890exit_dispatch:
4a15001e 5891
cf3af59e 5892 ret = pthread_join(client_thread, &status);
4a15001e
MD
5893 if (ret) {
5894 errno = ret;
76d7553f 5895 PERROR("pthread_join");
4a15001e 5896 retval = -1;
cf3af59e 5897 }
4a15001e 5898exit_client:
a88df331 5899
4a15001e
MD
5900 ret = pthread_join(health_thread, &status);
5901 if (ret) {
5902 errno = ret;
5903 PERROR("pthread_join health thread");
5904 retval = -1;
06f525de 5905 }
4a15001e 5906exit_health:
06f525de 5907
4a15001e
MD
5908exit_init_data:
5909 /*
5910 * sessiond_cleanup() is called when no other thread is running, except
5911 * the ht_cleanup thread, which is needed to destroy the hash tables.
5912 */
5913 rcu_thread_online();
5914 sessiond_cleanup();
5915 rcu_thread_offline();
5916 rcu_unregister_thread();
06f525de 5917
4a15001e
MD
5918 ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]);
5919 if (ret < 0) {
5920 ERR("write error on ht_cleanup quit pipe");
5921 retval = -1;
06f525de
DG
5922 }
5923
0b2dc8df 5924 ret = pthread_join(ht_cleanup_thread, &status);
4a15001e
MD
5925 if (ret) {
5926 errno = ret;
0b2dc8df 5927 PERROR("pthread_join ht cleanup thread");
4a15001e 5928 retval = -1;
0b2dc8df
MD
5929 }
5930exit_ht_cleanup:
25b397f9 5931exit_set_max_size:
4a15001e
MD
5932
5933 utils_close_pipe(ht_cleanup_pipe);
5934exit_ht_cleanup_pipe:
5935
cf3af59e 5936 /*
4a15001e 5937 * Close the ht_cleanup quit pipe.
cf3af59e 5938 */
4a15001e
MD
5939 utils_close_pipe(ht_cleanup_quit_pipe);
5940exit_ht_cleanup_quit_pipe:
5941
5942 health_app_destroy(health_sessiond);
5943exit_health_sessiond_cleanup:
5944
5945exit_options:
5946 sessiond_cleanup_options();
5947
5948exit_set_signal_handler:
5949 if (!retval) {
cf3af59e 5950 exit(EXIT_SUCCESS);
4a15001e
MD
5951 } else {
5952 exit(EXIT_FAILURE);
67e40797 5953 }
fac6795d 5954}
This page took 0.485196 seconds and 5 git commands to generate.