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