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