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