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