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