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