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