Fix: missing include ctype.h for isdigit()
[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 */
ef367a93 316#define NR_LTTNG_SESSIOND_READY 3
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)
567 }
568 if (!WIFEXITED(status)) {
569 ERR("consumerd termination with error: %d",
570 WEXITSTATUS(ret));
571 }
572 consumer_data->pid = 0;
573}
574
fac6795d 575/*
4a15001e 576 * Cleanup the session daemon's data structures.
fac6795d 577 */
4a15001e 578static void sessiond_cleanup(void)
fac6795d 579{
ef599319 580 int ret;
af9737e9 581 struct ltt_session *sess, *stmp;
8c6c56c2 582 char path[PATH_MAX];
fac6795d 583
4a15001e 584 DBG("Cleanup sessiond");
e07ae692 585
4e449f3f
MD
586 /*
587 * Close the thread quit pipe. It has already done its job,
588 * since we are now called.
589 */
2f77fc4b
DG
590 utils_close_pipe(thread_quit_pipe);
591
35f90c40
DG
592 /*
593 * If opt_pidfile is undefined, the default file will be wiped when
594 * removing the rundir.
595 */
596 if (opt_pidfile) {
597 ret = remove(opt_pidfile);
598 if (ret < 0) {
599 PERROR("remove pidfile %s", opt_pidfile);
600 }
601 }
602
8c6c56c2
MD
603 DBG("Removing sessiond and consumerd content of directory %s", rundir);
604
605 /* sessiond */
606 snprintf(path, PATH_MAX,
607 "%s/%s",
608 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
609 DBG("Removing %s", path);
610 (void) unlink(path);
611
cd9290dd 612 snprintf(path, PATH_MAX, "%s/%s", rundir,
022d91ba 613 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE);
cd9290dd
DG
614 DBG("Removing %s", path);
615 (void) unlink(path);
616
8c6c56c2
MD
617 /* kconsumerd */
618 snprintf(path, PATH_MAX,
619 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
620 rundir);
621 DBG("Removing %s", path);
622 (void) unlink(path);
623
624 snprintf(path, PATH_MAX,
625 DEFAULT_KCONSUMERD_PATH,
626 rundir);
627 DBG("Removing directory %s", path);
628 (void) rmdir(path);
629
630 /* ust consumerd 32 */
631 snprintf(path, PATH_MAX,
632 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
633 rundir);
634 DBG("Removing %s", path);
635 (void) unlink(path);
636
637 snprintf(path, PATH_MAX,
638 DEFAULT_USTCONSUMERD32_PATH,
639 rundir);
640 DBG("Removing directory %s", path);
641 (void) rmdir(path);
642
643 /* ust consumerd 64 */
644 snprintf(path, PATH_MAX,
645 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
646 rundir);
647 DBG("Removing %s", path);
648 (void) unlink(path);
649
650 snprintf(path, PATH_MAX,
651 DEFAULT_USTCONSUMERD64_PATH,
652 rundir);
653 DBG("Removing directory %s", path);
654 (void) rmdir(path);
5461b305 655
99bab54f 656 DBG("Cleaning up all sessions");
fac6795d 657
b5541356 658 /* Destroy session list mutex */
273ea72c
DG
659 if (session_list_ptr != NULL) {
660 pthread_mutex_destroy(&session_list_ptr->lock);
661
662 /* Cleanup ALL session */
54d01ffb
DG
663 cds_list_for_each_entry_safe(sess, stmp,
664 &session_list_ptr->head, list) {
2f77fc4b 665 cmd_destroy_session(sess, kernel_poll_pipe[1]);
273ea72c
DG
666 }
667 }
668
4e4714cb
JR
669 wait_consumer(&kconsumer_data);
670 wait_consumer(&ustconsumer64_data);
671 wait_consumer(&ustconsumer32_data);
672
6a4e4039
JG
673 DBG("Cleaning up all agent apps");
674 agent_app_ht_clean();
675
099e26bd 676 DBG("Closing all UST sockets");
56fff090 677 ust_app_clean_list();
7972aab2 678 buffer_reg_destroy_registries();
099e26bd 679
4fba7219
DG
680 if (is_root && !opt_no_kernel) {
681 DBG2("Closing kernel fd");
a4b35e07 682 if (kernel_tracer_fd >= 0) {
76d7553f
MD
683 ret = close(kernel_tracer_fd);
684 if (ret) {
685 PERROR("close");
686 }
a4b35e07 687 }
2f50c8a3 688 DBG("Unloading kernel modules");
096102bd 689 modprobe_remove_lttng_all();
834978fd 690 free(syscall_table);
2f50c8a3 691 }
2f77fc4b 692
e975f9f8
DG
693 close_consumer_sockets();
694
ef367a93
JG
695 if (load_info) {
696 load_session_destroy_data(load_info);
697 free(load_info);
698 }
699
c9cb3e7d
JG
700 /*
701 * Cleanup lock file by deleting it and finaly closing it which will
702 * release the file system lock.
703 */
704 if (lockfile_fd >= 0) {
705 char lockfile_path[PATH_MAX];
706
4a15001e
MD
707 ret = generate_lock_file_path(lockfile_path,
708 sizeof(lockfile_path));
c9cb3e7d
JG
709 if (ret > 0) {
710 ret = remove(lockfile_path);
711 if (ret < 0) {
712 PERROR("remove lock file");
713 }
714 ret = close(lockfile_fd);
715 if (ret < 0) {
716 PERROR("close lock file");
717 }
718 }
719 }
720
721 /*
722 * We do NOT rmdir rundir because there are other processes
723 * using it, for instance lttng-relayd, which can start in
724 * parallel with this teardown.
725 */
726
727 free(rundir);
4a15001e
MD
728}
729
730/*
731 * Cleanup the daemon's option data structures.
732 */
733static void sessiond_cleanup_options(void)
734{
735 DBG("Cleaning up options");
736
737 /*
738 * If the override option is set, the pointer points to a *non* const
739 * thus freeing it even though the variable type is set to const.
740 */
741 if (tracing_group_name_override) {
742 free((void *) tracing_group_name);
743 }
744 if (consumerd32_bin_override) {
745 free((void *) consumerd32_bin);
746 }
747 if (consumerd64_bin_override) {
748 free((void *) consumerd64_bin);
749 }
750 if (consumerd32_libdir_override) {
751 free((void *) consumerd32_libdir);
752 }
753 if (consumerd64_libdir_override) {
754 free((void *) consumerd64_libdir);
755 }
756
757 free(opt_pidfile);
758 free(opt_load_session_path);
759 free(kmod_probes_list);
760 free(kmod_extra_probes_list);
c9cb3e7d 761
7567352f
MD
762 run_as_destroy_worker();
763
421cb601 764 /* <fun> */
f56a39af 765 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
421cb601
DG
766 "Matthew, BEET driven development works!%c[%dm",
767 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
768 /* </fun> */
fac6795d
DG
769}
770
e065084a 771/*
d063d709 772 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 773 *
d063d709 774 * Return lttcomm error code.
e065084a
DG
775 */
776static int send_unix_sock(int sock, void *buf, size_t len)
777{
778 /* Check valid length */
c617c0c6 779 if (len == 0) {
e065084a
DG
780 return -1;
781 }
782
783 return lttcomm_send_unix_sock(sock, buf, len);
784}
785
5461b305 786/*
d063d709 787 * Free memory of a command context structure.
5461b305 788 */
a2fb29a5 789static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 790{
a2fb29a5
DG
791 DBG("Clean command context structure");
792 if (*cmd_ctx) {
793 if ((*cmd_ctx)->llm) {
794 free((*cmd_ctx)->llm);
5461b305 795 }
a2fb29a5
DG
796 if ((*cmd_ctx)->lsm) {
797 free((*cmd_ctx)->lsm);
5461b305 798 }
a2fb29a5
DG
799 free(*cmd_ctx);
800 *cmd_ctx = NULL;
5461b305
DG
801 }
802}
803
fac6795d 804/*
0fdd1e2c 805 * Notify UST applications using the shm mmap futex.
fac6795d 806 */
0fdd1e2c 807static int notify_ust_apps(int active)
fac6795d 808{
0fdd1e2c 809 char *wait_shm_mmap;
fac6795d 810
0fdd1e2c 811 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 812
0fdd1e2c
DG
813 /* See shm.c for this call implying mmap, shm and futex calls */
814 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
815 if (wait_shm_mmap == NULL) {
fac6795d
DG
816 goto error;
817 }
818
0fdd1e2c
DG
819 /* Wake waiting process */
820 futex_wait_update((int32_t *) wait_shm_mmap, active);
821
822 /* Apps notified successfully */
823 return 0;
fac6795d
DG
824
825error:
0fdd1e2c 826 return -1;
fac6795d
DG
827}
828
e065084a 829/*
d063d709
DG
830 * Setup the outgoing data buffer for the response (llm) by allocating the
831 * right amount of memory and copying the original information from the lsm
832 * structure.
ca95a216 833 *
6e10c9b9 834 * Return 0 on success, negative value on error.
ca95a216 835 */
6e10c9b9
PP
836static int setup_lttng_msg(struct command_ctx *cmd_ctx,
837 const void *payload_buf, size_t payload_len,
838 const void *cmd_header_buf, size_t cmd_header_len)
ca95a216 839{
6e10c9b9
PP
840 int ret = 0;
841 const size_t header_len = sizeof(struct lttcomm_lttng_msg);
842 const size_t cmd_header_offset = header_len;
843 const size_t payload_offset = cmd_header_offset + cmd_header_len;
844 const size_t total_msg_size = header_len + cmd_header_len + payload_len;
ca95a216 845
6e10c9b9 846 cmd_ctx->llm = zmalloc(total_msg_size);
5461b305 847
5461b305 848 if (cmd_ctx->llm == NULL) {
76d7553f 849 PERROR("zmalloc");
5461b305 850 ret = -ENOMEM;
6e10c9b9 851 goto end;
ca95a216
DG
852 }
853
5461b305
DG
854 /* Copy common data */
855 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 856 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
6e10c9b9
PP
857 cmd_ctx->llm->cmd_header_size = cmd_header_len;
858 cmd_ctx->llm->data_size = payload_len;
859 cmd_ctx->lttng_msg_size = total_msg_size;
5461b305 860
6e10c9b9 861 /* Copy command header */
b4e3ceb9
PP
862 if (cmd_header_len) {
863 memcpy(((uint8_t *) cmd_ctx->llm) + cmd_header_offset, cmd_header_buf,
864 cmd_header_len);
865 }
5461b305 866
6e10c9b9 867 /* Copy payload */
b4e3ceb9
PP
868 if (payload_len) {
869 memcpy(((uint8_t *) cmd_ctx->llm) + payload_offset, payload_buf,
870 payload_len);
871 }
ca95a216 872
6e10c9b9 873end:
ca95a216
DG
874 return ret;
875}
876
6e10c9b9
PP
877/*
878 * Version of setup_lttng_msg() without command header.
879 */
880static int setup_lttng_msg_no_cmd_header(struct command_ctx *cmd_ctx,
881 void *payload_buf, size_t payload_len)
882{
883 return setup_lttng_msg(cmd_ctx, payload_buf, payload_len, NULL, 0);
884}
7a485870 885/*
5eb91c98 886 * Update the kernel poll set of all channel fd available over all tracing
d063d709 887 * session. Add the wakeup pipe at the end of the set.
7a485870 888 */
5eb91c98 889static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 890{
5eb91c98 891 int ret;
7a485870
DG
892 struct ltt_session *session;
893 struct ltt_kernel_channel *channel;
894
5eb91c98 895 DBG("Updating kernel poll set");
7a485870 896
54d01ffb 897 session_lock_list();
b5541356 898 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 899 session_lock(session);
7a485870 900 if (session->kernel_session == NULL) {
54d01ffb 901 session_unlock(session);
7a485870
DG
902 continue;
903 }
7a485870 904
54d01ffb
DG
905 cds_list_for_each_entry(channel,
906 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
907 /* Add channel fd to the kernel poll set */
908 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
909 if (ret < 0) {
54d01ffb 910 session_unlock(session);
5eb91c98
DG
911 goto error;
912 }
913 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 914 }
54d01ffb 915 session_unlock(session);
7a485870 916 }
54d01ffb 917 session_unlock_list();
7a485870 918
5eb91c98 919 return 0;
7a485870
DG
920
921error:
54d01ffb 922 session_unlock_list();
7a485870
DG
923 return -1;
924}
925
926/*
54d01ffb 927 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 928 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 929 *
d063d709 930 * Useful for CPU hotplug feature.
7a485870 931 */
2bdd86d4 932static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
933{
934 int ret = 0;
935 struct ltt_session *session;
173af62f 936 struct ltt_kernel_session *ksess;
7a485870
DG
937 struct ltt_kernel_channel *channel;
938
939 DBG("Updating kernel streams for channel fd %d", fd);
940
54d01ffb 941 session_lock_list();
b5541356 942 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 943 session_lock(session);
7a485870 944 if (session->kernel_session == NULL) {
54d01ffb 945 session_unlock(session);
7a485870
DG
946 continue;
947 }
173af62f 948 ksess = session->kernel_session;
d9800920 949
4a15001e
MD
950 cds_list_for_each_entry(channel,
951 &ksess->channel_list.head, list) {
952 struct lttng_ht_iter iter;
953 struct consumer_socket *socket;
d9800920 954
4a15001e
MD
955 if (channel->fd != fd) {
956 continue;
957 }
958 DBG("Channel found, updating kernel streams");
959 ret = kernel_open_channel_stream(channel);
960 if (ret < 0) {
961 goto error;
962 }
963 /* Update the stream global counter */
964 ksess->stream_count_global += ret;
965
966 /*
967 * Have we already sent fds to the consumer? If yes, it
968 * means that tracing is started so it is safe to send
969 * our updated stream fds.
970 */
971 if (ksess->consumer_fds_sent != 1
972 || ksess->consumer == NULL) {
973 ret = -1;
974 goto error;
975 }
976
977 rcu_read_lock();
978 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
979 &iter.iter, socket, node.node) {
980 pthread_mutex_lock(socket->lock);
981 ret = kernel_consumer_send_channel_stream(socket,
982 channel, ksess,
983 session->output_traces ? 1 : 0);
984 pthread_mutex_unlock(socket->lock);
985 if (ret < 0) {
e7fe706f 986 rcu_read_unlock();
4a15001e 987 goto error;
7a485870 988 }
7a485870 989 }
4a15001e 990 rcu_read_unlock();
7a485870 991 }
54d01ffb 992 session_unlock(session);
7a485870 993 }
54d01ffb 994 session_unlock_list();
b3c750d2 995 return ret;
7a485870 996
b3c750d2 997error:
54d01ffb
DG
998 session_unlock(session);
999 session_unlock_list();
7a485870
DG
1000 return ret;
1001}
1002
487cf67c 1003/*
ffe60014
DG
1004 * For each tracing session, update newly registered apps. The session list
1005 * lock MUST be acquired before calling this.
487cf67c
DG
1006 */
1007static void update_ust_app(int app_sock)
1008{
1009 struct ltt_session *sess, *stmp;
1010
fdadac08
DG
1011 /* Consumer is in an ERROR state. Stop any application update. */
1012 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
1013 /* Stop the update process since the consumer is dead. */
1014 return;
1015 }
1016
487cf67c
DG
1017 /* For all tracing session(s) */
1018 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
a9ad0c8f
MD
1019 struct ust_app *app;
1020
4ee14516 1021 session_lock(sess);
a9ad0c8f
MD
1022 if (!sess->ust_session) {
1023 goto unlock_session;
1024 }
1025
1026 rcu_read_lock();
1027 assert(app_sock >= 0);
1028 app = ust_app_find_by_sock(app_sock);
1029 if (app == NULL) {
1030 /*
1031 * Application can be unregistered before so
1032 * this is possible hence simply stopping the
1033 * update.
1034 */
1035 DBG3("UST app update failed to find app sock %d",
1036 app_sock);
1037 goto unlock_rcu;
421cb601 1038 }
a9ad0c8f
MD
1039 ust_app_global_update(sess->ust_session, app);
1040 unlock_rcu:
1041 rcu_read_unlock();
1042 unlock_session:
4ee14516 1043 session_unlock(sess);
487cf67c
DG
1044 }
1045}
1046
7a485870 1047/*
d063d709 1048 * This thread manage event coming from the kernel.
7a485870 1049 *
d063d709
DG
1050 * Features supported in this thread:
1051 * -) CPU Hotplug
7a485870
DG
1052 */
1053static void *thread_manage_kernel(void *data)
1054{
139ac872 1055 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 1056 uint32_t revents, nb_fd;
7a485870 1057 char tmp;
5eb91c98 1058 struct lttng_poll_event events;
7a485870 1059
6993eeb3 1060 DBG("[thread] Thread manage kernel started");
7a485870 1061
6c71277b 1062 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
927ca06a 1063
d5d63bf1
DG
1064 /*
1065 * This first step of the while is to clean this structure which could free
6d737ce4 1066 * non NULL pointers so initialize it before the loop.
d5d63bf1 1067 */
6d737ce4 1068 lttng_poll_init(&events);
d5d63bf1 1069
e547b070 1070 if (testpoint(sessiond_thread_manage_kernel)) {
6993eeb3
CB
1071 goto error_testpoint;
1072 }
8ac94142 1073
840cb59c 1074 health_code_update();
44a5e5eb 1075
e547b070 1076 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
d21b0d71 1077 goto error_testpoint;
6993eeb3
CB
1078 }
1079
7a485870 1080 while (1) {
840cb59c 1081 health_code_update();
44a5e5eb 1082
7a485870 1083 if (update_poll_flag == 1) {
d21b0d71
DG
1084 /* Clean events object. We are about to populate it again. */
1085 lttng_poll_clean(&events);
1086
d0b96690 1087 ret = sessiond_set_thread_pollset(&events, 2);
d21b0d71
DG
1088 if (ret < 0) {
1089 goto error_poll_create;
1090 }
1091
1092 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
1093 if (ret < 0) {
1094 goto error;
1095 }
5f822d0a 1096
d21b0d71 1097 /* This will add the available kernel channel if any. */
5eb91c98
DG
1098 ret = update_kernel_poll(&events);
1099 if (ret < 0) {
7a485870
DG
1100 goto error;
1101 }
1102 update_poll_flag = 0;
1103 }
1104
7fa2082e 1105 DBG("Thread kernel polling");
7a485870
DG
1106
1107 /* Poll infinite value of time */
88f2b785 1108 restart:
a78af745 1109 health_poll_entry();
5eb91c98 1110 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1111 DBG("Thread kernel return from poll on %d fds",
1112 LTTNG_POLL_GETNB(&events));
a78af745 1113 health_poll_exit();
7a485870 1114 if (ret < 0) {
88f2b785
MD
1115 /*
1116 * Restart interrupted system call.
1117 */
1118 if (errno == EINTR) {
1119 goto restart;
1120 }
7a485870
DG
1121 goto error;
1122 } else if (ret == 0) {
1123 /* Should not happen since timeout is infinite */
85611738
DG
1124 ERR("Return value of poll is 0 with an infinite timeout.\n"
1125 "This should not have happened! Continuing...");
7a485870
DG
1126 continue;
1127 }
1128
0d9c5d77
DG
1129 nb_fd = ret;
1130
5eb91c98
DG
1131 for (i = 0; i < nb_fd; i++) {
1132 /* Fetch once the poll data */
1133 revents = LTTNG_POLL_GETEV(&events, i);
1134 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 1135
840cb59c 1136 health_code_update();
44a5e5eb 1137
fd20dac9
MD
1138 if (!revents) {
1139 /* No activity for this FD (poll implementation). */
1140 continue;
1141 }
1142
5eb91c98 1143 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1144 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1145 if (ret) {
139ac872
MD
1146 err = 0;
1147 goto exit;
5eb91c98 1148 }
7a485870 1149
5eb91c98 1150 /* Check for data on kernel pipe */
03e43155
MD
1151 if (revents & LPOLLIN) {
1152 if (pollfd == kernel_poll_pipe[0]) {
1153 (void) lttng_read(kernel_poll_pipe[0],
1154 &tmp, 1);
1155 /*
1156 * Ret value is useless here, if this pipe gets any actions an
1157 * update is required anyway.
1158 */
1159 update_poll_flag = 1;
1160 continue;
1161 } else {
1162 /*
1163 * New CPU detected by the kernel. Adding kernel stream to
1164 * kernel session and updating the kernel consumer
1165 */
2bdd86d4 1166 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
1167 if (ret < 0) {
1168 continue;
1169 }
1170 break;
7a485870 1171 }
03e43155
MD
1172 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1173 update_poll_flag = 1;
1174 continue;
1175 } else {
1176 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1177 goto error;
7a485870
DG
1178 }
1179 }
1180 }
1181
139ac872 1182exit:
7a485870 1183error:
5eb91c98 1184 lttng_poll_clean(&events);
76d7553f 1185error_poll_create:
6993eeb3 1186error_testpoint:
6620da75
DG
1187 utils_close_pipe(kernel_poll_pipe);
1188 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872 1189 if (err) {
840cb59c 1190 health_error();
139ac872 1191 ERR("Health error occurred in %s", __func__);
6620da75
DG
1192 WARN("Kernel thread died unexpectedly. "
1193 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 1194 }
8782cc74 1195 health_unregister(health_sessiond);
76d7553f 1196 DBG("Kernel thread dying");
7a485870
DG
1197 return NULL;
1198}
1199
a23ec3a7
DG
1200/*
1201 * Signal pthread condition of the consumer data that the thread.
1202 */
1203static void signal_consumer_condition(struct consumer_data *data, int state)
1204{
1205 pthread_mutex_lock(&data->cond_mutex);
1206
1207 /*
1208 * The state is set before signaling. It can be any value, it's the waiter
1209 * job to correctly interpret this condition variable associated to the
1210 * consumer pthread_cond.
1211 *
1212 * A value of 0 means that the corresponding thread of the consumer data
1213 * was not started. 1 indicates that the thread has started and is ready
1214 * for action. A negative value means that there was an error during the
1215 * thread bootstrap.
1216 */
1217 data->consumer_thread_is_ready = state;
1218 (void) pthread_cond_signal(&data->cond);
1219
1220 pthread_mutex_unlock(&data->cond_mutex);
1221}
1222
1d4b027a 1223/*
3bd1e081 1224 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 1225 */
3bd1e081 1226static void *thread_manage_consumer(void *data)
1d4b027a 1227{
42fc1d0b 1228 int sock = -1, i, ret, pollfd, err = -1, should_quit = 0;
5eb91c98 1229 uint32_t revents, nb_fd;
1d4b027a 1230 enum lttcomm_return_code code;
5eb91c98 1231 struct lttng_poll_event events;
3bd1e081 1232 struct consumer_data *consumer_data = data;
1d4b027a 1233
3bd1e081 1234 DBG("[thread] Manage consumer started");
1d4b027a 1235
34c1e15a
MD
1236 rcu_register_thread();
1237 rcu_thread_online();
1238
6c71277b 1239 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
927ca06a 1240
855060f8 1241 health_code_update();
9449cc75 1242
5eb91c98 1243 /*
331744e3
JD
1244 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1245 * metadata_sock. Nothing more will be added to this poll set.
5eb91c98 1246 */
331744e3 1247 ret = sessiond_set_thread_pollset(&events, 3);
5eb91c98 1248 if (ret < 0) {
76d7553f 1249 goto error_poll;
5eb91c98 1250 }
273ea72c 1251
edb8b045
DG
1252 /*
1253 * The error socket here is already in a listening state which was done
1254 * just before spawning this thread to avoid a race between the consumer
1255 * daemon exec trying to connect and the listen() call.
1256 */
3bd1e081 1257 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
1258 if (ret < 0) {
1259 goto error;
1260 }
1261
840cb59c 1262 health_code_update();
44a5e5eb 1263
331744e3 1264 /* Infinite blocking call, waiting for transmission */
88f2b785 1265restart:
a78af745 1266 health_poll_entry();
8ac94142 1267
e547b070 1268 if (testpoint(sessiond_thread_manage_consumer)) {
6993eeb3
CB
1269 goto error;
1270 }
8ac94142 1271
5eb91c98 1272 ret = lttng_poll_wait(&events, -1);
a78af745 1273 health_poll_exit();
273ea72c 1274 if (ret < 0) {
88f2b785
MD
1275 /*
1276 * Restart interrupted system call.
1277 */
1278 if (errno == EINTR) {
1279 goto restart;
1280 }
273ea72c
DG
1281 goto error;
1282 }
1283
0d9c5d77
DG
1284 nb_fd = ret;
1285
5eb91c98
DG
1286 for (i = 0; i < nb_fd; i++) {
1287 /* Fetch once the poll data */
1288 revents = LTTNG_POLL_GETEV(&events, i);
1289 pollfd = LTTNG_POLL_GETFD(&events, i);
1290
840cb59c 1291 health_code_update();
44a5e5eb 1292
fd20dac9
MD
1293 if (!revents) {
1294 /* No activity for this FD (poll implementation). */
1295 continue;
1296 }
1297
5eb91c98 1298 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1299 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1300 if (ret) {
139ac872
MD
1301 err = 0;
1302 goto exit;
5eb91c98
DG
1303 }
1304
1305 /* Event on the registration socket */
3bd1e081 1306 if (pollfd == consumer_data->err_sock) {
03e43155
MD
1307 if (revents & LPOLLIN) {
1308 continue;
1309 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1310 ERR("consumer err socket poll error");
5eb91c98 1311 goto error;
03e43155
MD
1312 } else {
1313 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1314 goto error;
5eb91c98
DG
1315 }
1316 }
273ea72c
DG
1317 }
1318
3bd1e081 1319 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
1320 if (sock < 0) {
1321 goto error;
1322 }
1323
b662582b
DG
1324 /*
1325 * Set the CLOEXEC flag. Return code is useless because either way, the
1326 * show must go on.
1327 */
1328 (void) utils_set_fd_cloexec(sock);
1329
840cb59c 1330 health_code_update();
44a5e5eb 1331
3bd1e081 1332 DBG2("Receiving code from consumer err_sock");
ee0b0061 1333
712ea556 1334 /* Getting status code from kconsumerd */
54d01ffb
DG
1335 ret = lttcomm_recv_unix_sock(sock, &code,
1336 sizeof(enum lttcomm_return_code));
1d4b027a
DG
1337 if (ret <= 0) {
1338 goto error;
1339 }
1340
840cb59c 1341 health_code_update();
f73fabfd 1342 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
331744e3 1343 /* Connect both socket, command and metadata. */
3bd1e081
MD
1344 consumer_data->cmd_sock =
1345 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
4ce514c4 1346 consumer_data->metadata_fd =
331744e3 1347 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
92db7cdc
DG
1348 if (consumer_data->cmd_sock < 0
1349 || consumer_data->metadata_fd < 0) {
331744e3 1350 PERROR("consumer connect cmd socket");
a23ec3a7
DG
1351 /* On error, signal condition and quit. */
1352 signal_consumer_condition(consumer_data, -1);
1d4b027a
DG
1353 goto error;
1354 }
9363801e 1355 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
331744e3
JD
1356 /* Create metadata socket lock. */
1357 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1358 if (consumer_data->metadata_sock.lock == NULL) {
1359 PERROR("zmalloc pthread mutex");
1360 ret = -1;
1361 goto error;
1362 }
1363 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1364
a23ec3a7 1365 signal_consumer_condition(consumer_data, 1);
331744e3
JD
1366 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1367 DBG("Consumer metadata socket ready (fd: %d)",
4ce514c4 1368 consumer_data->metadata_fd);
1d4b027a 1369 } else {
3bd1e081 1370 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1371 lttcomm_get_readable_code(-code));
1372 goto error;
1373 }
1374
331744e3 1375 /* Remove the consumerd error sock since we've established a connexion */
3bd1e081 1376 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1377 if (ret < 0) {
72079cae
DG
1378 goto error;
1379 }
1380
331744e3 1381 /* Add new accepted error socket. */
5eb91c98
DG
1382 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1383 if (ret < 0) {
72079cae 1384 goto error;
5eb91c98
DG
1385 }
1386
331744e3 1387 /* Add metadata socket that is successfully connected. */
4ce514c4 1388 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
331744e3
JD
1389 LPOLLIN | LPOLLRDHUP);
1390 if (ret < 0) {
1391 goto error;
1392 }
1393
840cb59c 1394 health_code_update();
44a5e5eb 1395
331744e3 1396 /* Infinite blocking call, waiting for transmission */
88f2b785 1397restart_poll:
331744e3 1398 while (1) {
42fc1d0b
DG
1399 health_code_update();
1400
1401 /* Exit the thread because the thread quit pipe has been triggered. */
1402 if (should_quit) {
1403 /* Not a health error. */
1404 err = 0;
1405 goto exit;
1406 }
1407
331744e3
JD
1408 health_poll_entry();
1409 ret = lttng_poll_wait(&events, -1);
1410 health_poll_exit();
1411 if (ret < 0) {
1412 /*
1413 * Restart interrupted system call.
1414 */
1415 if (errno == EINTR) {
1416 goto restart_poll;
1417 }
1418 goto error;
88f2b785 1419 }
72079cae 1420
331744e3 1421 nb_fd = ret;
0d9c5d77 1422
331744e3
JD
1423 for (i = 0; i < nb_fd; i++) {
1424 /* Fetch once the poll data */
1425 revents = LTTNG_POLL_GETEV(&events, i);
1426 pollfd = LTTNG_POLL_GETFD(&events, i);
5eb91c98 1427
331744e3 1428 health_code_update();
44a5e5eb 1429
fd20dac9
MD
1430 if (!revents) {
1431 /* No activity for this FD (poll implementation). */
1432 continue;
1433 }
1434
42fc1d0b
DG
1435 /*
1436 * Thread quit pipe has been triggered, flag that we should stop
1437 * but continue the current loop to handle potential data from
1438 * consumer.
1439 */
1440 should_quit = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1441
331744e3
JD
1442 if (pollfd == sock) {
1443 /* Event on the consumerd socket */
03e43155
MD
1444 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1445 && !(revents & LPOLLIN)) {
331744e3
JD
1446 ERR("consumer err socket second poll error");
1447 goto error;
1448 }
1449 health_code_update();
1450 /* Wait for any kconsumerd error */
1451 ret = lttcomm_recv_unix_sock(sock, &code,
1452 sizeof(enum lttcomm_return_code));
1453 if (ret <= 0) {
1454 ERR("consumer closed the command socket");
1455 goto error;
1456 }
1457
1458 ERR("consumer return code : %s",
1459 lttcomm_get_readable_code(-code));
1460
1461 goto exit;
4ce514c4 1462 } else if (pollfd == consumer_data->metadata_fd) {
03e43155
MD
1463 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)
1464 && !(revents & LPOLLIN)) {
1465 ERR("consumer err metadata socket second poll error");
1466 goto error;
1467 }
331744e3
JD
1468 /* UST metadata requests */
1469 ret = ust_consumer_metadata_request(
1470 &consumer_data->metadata_sock);
1471 if (ret < 0) {
1472 ERR("Handling metadata request");
1473 goto error;
1474 }
5eb91c98 1475 }
42fc1d0b 1476 /* No need for an else branch all FDs are tested prior. */
5eb91c98 1477 }
331744e3 1478 health_code_update();
5eb91c98
DG
1479 }
1480
139ac872 1481exit:
1d4b027a 1482error:
fdadac08
DG
1483 /*
1484 * We lock here because we are about to close the sockets and some other
92db7cdc
DG
1485 * thread might be using them so get exclusive access which will abort all
1486 * other consumer command by other threads.
fdadac08
DG
1487 */
1488 pthread_mutex_lock(&consumer_data->lock);
1489
5c827ce0
DG
1490 /* Immediately set the consumerd state to stopped */
1491 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1492 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1493 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1494 consumer_data->type == LTTNG_CONSUMER32_UST) {
1495 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1496 } else {
1497 /* Code flow error... */
1498 assert(0);
1499 }
1500
76d7553f
MD
1501 if (consumer_data->err_sock >= 0) {
1502 ret = close(consumer_data->err_sock);
1503 if (ret) {
1504 PERROR("close");
1505 }
a76cbd9f 1506 consumer_data->err_sock = -1;
76d7553f
MD
1507 }
1508 if (consumer_data->cmd_sock >= 0) {
1509 ret = close(consumer_data->cmd_sock);
1510 if (ret) {
1511 PERROR("close");
1512 }
a76cbd9f 1513 consumer_data->cmd_sock = -1;
76d7553f 1514 }
96544455
SS
1515 if (consumer_data->metadata_sock.fd_ptr &&
1516 *consumer_data->metadata_sock.fd_ptr >= 0) {
9363801e 1517 ret = close(*consumer_data->metadata_sock.fd_ptr);
331744e3
JD
1518 if (ret) {
1519 PERROR("close");
1520 }
1521 }
76d7553f
MD
1522 if (sock >= 0) {
1523 ret = close(sock);
1524 if (ret) {
1525 PERROR("close");
1526 }
1527 }
273ea72c 1528
3bd1e081
MD
1529 unlink(consumer_data->err_unix_sock_path);
1530 unlink(consumer_data->cmd_unix_sock_path);
fdadac08 1531 pthread_mutex_unlock(&consumer_data->lock);
92db7cdc 1532
fdadac08 1533 /* Cleanup metadata socket mutex. */
96544455
SS
1534 if (consumer_data->metadata_sock.lock) {
1535 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1536 free(consumer_data->metadata_sock.lock);
1537 }
5eb91c98 1538 lttng_poll_clean(&events);
76d7553f 1539error_poll:
139ac872 1540 if (err) {
840cb59c 1541 health_error();
139ac872
MD
1542 ERR("Health error occurred in %s", __func__);
1543 }
8782cc74 1544 health_unregister(health_sessiond);
76d7553f 1545 DBG("consumer thread cleanup completed");
0177d773 1546
34c1e15a
MD
1547 rcu_thread_offline();
1548 rcu_unregister_thread();
1549
5eb91c98 1550 return NULL;
099e26bd
DG
1551}
1552
099e26bd
DG
1553/*
1554 * This thread manage application communication.
1d4b027a
DG
1555 */
1556static void *thread_manage_apps(void *data)
099e26bd 1557{
139ac872 1558 int i, ret, pollfd, err = -1;
6cd525e8 1559 ssize_t size_ret;
5eb91c98 1560 uint32_t revents, nb_fd;
5eb91c98 1561 struct lttng_poll_event events;
099e26bd
DG
1562
1563 DBG("[thread] Manage application started");
1564
f6a9efaa
DG
1565 rcu_register_thread();
1566 rcu_thread_online();
1567
6c71277b 1568 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
927ca06a 1569
e547b070 1570 if (testpoint(sessiond_thread_manage_apps)) {
6993eeb3
CB
1571 goto error_testpoint;
1572 }
1573
840cb59c 1574 health_code_update();
44a5e5eb 1575
d0b96690 1576 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 1577 if (ret < 0) {
76d7553f 1578 goto error_poll_create;
5eb91c98 1579 }
099e26bd 1580
5eb91c98
DG
1581 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1582 if (ret < 0) {
1583 goto error;
1584 }
099e26bd 1585
e547b070 1586 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
6993eeb3
CB
1587 goto error;
1588 }
8ac94142 1589
840cb59c 1590 health_code_update();
44a5e5eb 1591
5eb91c98 1592 while (1) {
7fa2082e 1593 DBG("Apps thread polling");
099e26bd
DG
1594
1595 /* Inifinite blocking call, waiting for transmission */
88f2b785 1596 restart:
a78af745 1597 health_poll_entry();
5eb91c98 1598 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
1599 DBG("Apps thread return from poll on %d fds",
1600 LTTNG_POLL_GETNB(&events));
a78af745 1601 health_poll_exit();
099e26bd 1602 if (ret < 0) {
88f2b785
MD
1603 /*
1604 * Restart interrupted system call.
1605 */
1606 if (errno == EINTR) {
1607 goto restart;
1608 }
099e26bd
DG
1609 goto error;
1610 }
1611
0d9c5d77
DG
1612 nb_fd = ret;
1613
5eb91c98
DG
1614 for (i = 0; i < nb_fd; i++) {
1615 /* Fetch once the poll data */
1616 revents = LTTNG_POLL_GETEV(&events, i);
1617 pollfd = LTTNG_POLL_GETFD(&events, i);
1618
840cb59c 1619 health_code_update();
44a5e5eb 1620
fd20dac9
MD
1621 if (!revents) {
1622 /* No activity for this FD (poll implementation). */
1623 continue;
1624 }
1625
5eb91c98 1626 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 1627 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 1628 if (ret) {
139ac872
MD
1629 err = 0;
1630 goto exit;
5eb91c98 1631 }
099e26bd 1632
5eb91c98
DG
1633 /* Inspect the apps cmd pipe */
1634 if (pollfd == apps_cmd_pipe[0]) {
03e43155 1635 if (revents & LPOLLIN) {
d0b96690
DG
1636 int sock;
1637
5eb91c98 1638 /* Empty pipe */
6cd525e8
MD
1639 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1640 if (size_ret < sizeof(sock)) {
76d7553f 1641 PERROR("read apps cmd pipe");
5eb91c98
DG
1642 goto error;
1643 }
099e26bd 1644
840cb59c 1645 health_code_update();
44a5e5eb 1646
ffe60014 1647 /*
03e43155
MD
1648 * Since this is a command socket (write then read),
1649 * we only monitor the error events of the socket.
ffe60014 1650 */
d0b96690
DG
1651 ret = lttng_poll_add(&events, sock,
1652 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1653 if (ret < 0) {
5eb91c98 1654 goto error;
e0c7ec2b 1655 }
acc7b41b 1656
d0b96690 1657 DBG("Apps with sock %d added to poll set", sock);
03e43155
MD
1658 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1659 ERR("Apps command pipe error");
1660 goto error;
1661 } else {
1662 ERR("Unknown poll events %u for sock %d", revents, pollfd);
1663 goto error;
0177d773 1664 }
5eb91c98
DG
1665 } else {
1666 /*
54d01ffb
DG
1667 * At this point, we know that a registered application made
1668 * the event at poll_wait.
5eb91c98
DG
1669 */
1670 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1671 /* Removing from the poll set */
1672 ret = lttng_poll_del(&events, pollfd);
1673 if (ret < 0) {
1674 goto error;
1675 }
099e26bd 1676
b9d9b220 1677 /* Socket closed on remote end. */
56fff090 1678 ust_app_unregister(pollfd);
03e43155
MD
1679 } else {
1680 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1681 goto error;
5eb91c98 1682 }
099e26bd 1683 }
44a5e5eb 1684
840cb59c 1685 health_code_update();
099e26bd 1686 }
099e26bd
DG
1687 }
1688
139ac872 1689exit:
099e26bd 1690error:
5eb91c98 1691 lttng_poll_clean(&events);
76d7553f 1692error_poll_create:
6993eeb3 1693error_testpoint:
6620da75
DG
1694 utils_close_pipe(apps_cmd_pipe);
1695 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1696
1697 /*
1698 * We don't clean the UST app hash table here since already registered
1699 * applications can still be controlled so let them be until the session
1700 * daemon dies or the applications stop.
1701 */
1702
139ac872 1703 if (err) {
840cb59c 1704 health_error();
139ac872
MD
1705 ERR("Health error occurred in %s", __func__);
1706 }
8782cc74 1707 health_unregister(health_sessiond);
76d7553f 1708 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1709 rcu_thread_offline();
1710 rcu_unregister_thread();
099e26bd
DG
1711 return NULL;
1712}
1713
d0b96690 1714/*
d88aee68
DG
1715 * Send a socket to a thread This is called from the dispatch UST registration
1716 * thread once all sockets are set for the application.
d0b96690 1717 *
b85dc84c
DG
1718 * The sock value can be invalid, we don't really care, the thread will handle
1719 * it and make the necessary cleanup if so.
1720 *
d0b96690
DG
1721 * On success, return 0 else a negative value being the errno message of the
1722 * write().
1723 */
d88aee68 1724static int send_socket_to_thread(int fd, int sock)
d0b96690 1725{
6cd525e8 1726 ssize_t ret;
d0b96690 1727
b85dc84c
DG
1728 /*
1729 * It's possible that the FD is set as invalid with -1 concurrently just
1730 * before calling this function being a shutdown state of the thread.
1731 */
1732 if (fd < 0) {
1733 ret = -EBADF;
1734 goto error;
1735 }
d0b96690 1736
6cd525e8
MD
1737 ret = lttng_write(fd, &sock, sizeof(sock));
1738 if (ret < sizeof(sock)) {
d88aee68 1739 PERROR("write apps pipe %d", fd);
d0b96690
DG
1740 if (ret < 0) {
1741 ret = -errno;
1742 }
1743 goto error;
1744 }
1745
1746 /* All good. Don't send back the write positive ret value. */
1747 ret = 0;
1748error:
6cd525e8 1749 return (int) ret;
d0b96690
DG
1750}
1751
f45e313d
DG
1752/*
1753 * Sanitize the wait queue of the dispatch registration thread meaning removing
1754 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1755 * notify socket is never received.
1756 */
1757static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1758{
1759 int ret, nb_fd = 0, i;
1760 unsigned int fd_added = 0;
1761 struct lttng_poll_event events;
1762 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1763
1764 assert(wait_queue);
1765
1766 lttng_poll_init(&events);
1767
1768 /* Just skip everything for an empty queue. */
1769 if (!wait_queue->count) {
1770 goto end;
1771 }
1772
1773 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1774 if (ret < 0) {
1775 goto error_create;
1776 }
1777
1778 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1779 &wait_queue->head, head) {
1780 assert(wait_node->app);
1781 ret = lttng_poll_add(&events, wait_node->app->sock,
1782 LPOLLHUP | LPOLLERR);
1783 if (ret < 0) {
1784 goto error;
1785 }
1786
1787 fd_added = 1;
1788 }
1789
1790 if (!fd_added) {
1791 goto end;
1792 }
1793
1794 /*
1795 * Poll but don't block so we can quickly identify the faulty events and
1796 * clean them afterwards from the wait queue.
1797 */
1798 ret = lttng_poll_wait(&events, 0);
1799 if (ret < 0) {
1800 goto error;
1801 }
1802 nb_fd = ret;
1803
1804 for (i = 0; i < nb_fd; i++) {
1805 /* Get faulty FD. */
1806 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1807 int pollfd = LTTNG_POLL_GETFD(&events, i);
1808
fd20dac9
MD
1809 if (!revents) {
1810 /* No activity for this FD (poll implementation). */
1811 continue;
1812 }
1813
f45e313d
DG
1814 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1815 &wait_queue->head, head) {
1816 if (pollfd == wait_node->app->sock &&
1817 (revents & (LPOLLHUP | LPOLLERR))) {
1818 cds_list_del(&wait_node->head);
1819 wait_queue->count--;
1820 ust_app_destroy(wait_node->app);
1821 free(wait_node);
48b40bcf
JG
1822 /*
1823 * Silence warning of use-after-free in
1824 * cds_list_for_each_entry_safe which uses
1825 * __typeof__(*wait_node).
1826 */
1827 wait_node = NULL;
f45e313d 1828 break;
03e43155
MD
1829 } else {
1830 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
1831 goto error;
f45e313d
DG
1832 }
1833 }
1834 }
1835
1836 if (nb_fd > 0) {
1837 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1838 }
1839
1840end:
1841 lttng_poll_clean(&events);
1842 return;
1843
1844error:
1845 lttng_poll_clean(&events);
1846error_create:
1847 ERR("Unable to sanitize wait queue");
1848 return;
1849}
1850
099e26bd
DG
1851/*
1852 * Dispatch request from the registration threads to the application
1853 * communication thread.
1854 */
1855static void *thread_dispatch_ust_registration(void *data)
1856{
12e2b881 1857 int ret, err = -1;
8bdee6e2 1858 struct cds_wfcq_node *node;
099e26bd 1859 struct ust_command *ust_cmd = NULL;
f45e313d
DG
1860 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1861 struct ust_reg_wait_queue wait_queue = {
1862 .count = 0,
1863 };
d0b96690 1864
6c71277b 1865 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
12e2b881 1866
9ad42ec1
MD
1867 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1868 goto error_testpoint;
1869 }
1870
12e2b881
MD
1871 health_code_update();
1872
f45e313d 1873 CDS_INIT_LIST_HEAD(&wait_queue.head);
099e26bd
DG
1874
1875 DBG("[thread] Dispatch UST command started");
1876
26c9d55e 1877 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
12e2b881
MD
1878 health_code_update();
1879
099e26bd
DG
1880 /* Atomically prepare the queue futex */
1881 futex_nto1_prepare(&ust_cmd_queue.futex);
1882
1883 do {
d0b96690 1884 struct ust_app *app = NULL;
7972aab2 1885 ust_cmd = NULL;
d0b96690 1886
f45e313d
DG
1887 /*
1888 * Make sure we don't have node(s) that have hung up before receiving
1889 * the notify socket. This is to clean the list in order to avoid
1890 * memory leaks from notify socket that are never seen.
1891 */
1892 sanitize_wait_queue(&wait_queue);
1893
12e2b881 1894 health_code_update();
099e26bd 1895 /* Dequeue command for registration */
8bdee6e2 1896 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 1897 if (node == NULL) {
00a17c97 1898 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1899 /* Continue thread execution */
1900 break;
1901 }
1902
1903 ust_cmd = caa_container_of(node, struct ust_command, node);
1904
2f50c8a3
DG
1905 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1906 " gid:%d sock:%d name:%s (version %d.%d)",
1907 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1908 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1909 ust_cmd->sock, ust_cmd->reg_msg.name,
1910 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
d0b96690
DG
1911
1912 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1913 wait_node = zmalloc(sizeof(*wait_node));
1914 if (!wait_node) {
1915 PERROR("zmalloc wait_node dispatch");
020d7f60
DG
1916 ret = close(ust_cmd->sock);
1917 if (ret < 0) {
1918 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1919 }
51dec90d 1920 lttng_fd_put(LTTNG_FD_APPS, 1);
7972aab2 1921 free(ust_cmd);
d0b96690
DG
1922 goto error;
1923 }
1924 CDS_INIT_LIST_HEAD(&wait_node->head);
1925
1926 /* Create application object if socket is CMD. */
1927 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1928 ust_cmd->sock);
1929 if (!wait_node->app) {
1930 ret = close(ust_cmd->sock);
1931 if (ret < 0) {
1932 PERROR("close ust sock dispatch %d", ust_cmd->sock);
6620da75 1933 }
51dec90d 1934 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 1935 free(wait_node);
7972aab2 1936 free(ust_cmd);
d0b96690
DG
1937 continue;
1938 }
1939 /*
1940 * Add application to the wait queue so we can set the notify
1941 * socket before putting this object in the global ht.
1942 */
f45e313d
DG
1943 cds_list_add(&wait_node->head, &wait_queue.head);
1944 wait_queue.count++;
d0b96690 1945
7972aab2 1946 free(ust_cmd);
d0b96690
DG
1947 /*
1948 * We have to continue here since we don't have the notify
1949 * socket and the application MUST be added to the hash table
1950 * only at that moment.
1951 */
1952 continue;
1953 } else {
1954 /*
1955 * Look for the application in the local wait queue and set the
1956 * notify socket if found.
1957 */
d88aee68 1958 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 1959 &wait_queue.head, head) {
12e2b881 1960 health_code_update();
d0b96690
DG
1961 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1962 wait_node->app->notify_sock = ust_cmd->sock;
1963 cds_list_del(&wait_node->head);
f45e313d 1964 wait_queue.count--;
d0b96690
DG
1965 app = wait_node->app;
1966 free(wait_node);
1967 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1968 break;
1969 }
1970 }
020d7f60
DG
1971
1972 /*
1973 * With no application at this stage the received socket is
1974 * basically useless so close it before we free the cmd data
1975 * structure for good.
1976 */
1977 if (!app) {
1978 ret = close(ust_cmd->sock);
1979 if (ret < 0) {
1980 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1981 }
51dec90d 1982 lttng_fd_put(LTTNG_FD_APPS, 1);
020d7f60 1983 }
7972aab2 1984 free(ust_cmd);
d0b96690
DG
1985 }
1986
1987 if (app) {
d0b96690
DG
1988 /*
1989 * @session_lock_list
1990 *
1991 * Lock the global session list so from the register up to the
1992 * registration done message, no thread can see the application
1993 * and change its state.
1994 */
1995 session_lock_list();
1996 rcu_read_lock();
d88aee68 1997
d0b96690
DG
1998 /*
1999 * Add application to the global hash table. This needs to be
2000 * done before the update to the UST registry can locate the
2001 * application.
2002 */
2003 ust_app_add(app);
d88aee68
DG
2004
2005 /* Set app version. This call will print an error if needed. */
2006 (void) ust_app_version(app);
2007
2008 /* Send notify socket through the notify pipe. */
2009 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
2010 app->notify_sock);
2011 if (ret < 0) {
2012 rcu_read_unlock();
2013 session_unlock_list();
b85dc84c
DG
2014 /*
2015 * No notify thread, stop the UST tracing. However, this is
2016 * not an internal error of the this thread thus setting
2017 * the health error code to a normal exit.
2018 */
2019 err = 0;
d88aee68 2020 goto error;
6620da75 2021 }
d88aee68 2022
d0b96690
DG
2023 /*
2024 * Update newly registered application with the tracing
2025 * registry info already enabled information.
2026 */
2027 update_ust_app(app->sock);
d88aee68
DG
2028
2029 /*
2030 * Don't care about return value. Let the manage apps threads
2031 * handle app unregistration upon socket close.
2032 */
fb45065e 2033 (void) ust_app_register_done(app);
d88aee68
DG
2034
2035 /*
2036 * Even if the application socket has been closed, send the app
2037 * to the thread and unregistration will take place at that
2038 * place.
2039 */
2040 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
d0b96690 2041 if (ret < 0) {
d88aee68
DG
2042 rcu_read_unlock();
2043 session_unlock_list();
b85dc84c
DG
2044 /*
2045 * No apps. thread, stop the UST tracing. However, this is
2046 * not an internal error of the this thread thus setting
2047 * the health error code to a normal exit.
2048 */
2049 err = 0;
d88aee68 2050 goto error;
d0b96690 2051 }
d88aee68 2052
d0b96690
DG
2053 rcu_read_unlock();
2054 session_unlock_list();
099e26bd 2055 }
099e26bd
DG
2056 } while (node != NULL);
2057
12e2b881 2058 health_poll_entry();
099e26bd
DG
2059 /* Futex wait on queue. Blocking call on futex() */
2060 futex_nto1_wait(&ust_cmd_queue.futex);
12e2b881 2061 health_poll_exit();
099e26bd 2062 }
12e2b881
MD
2063 /* Normal exit, no error */
2064 err = 0;
099e26bd
DG
2065
2066error:
d88aee68
DG
2067 /* Clean up wait queue. */
2068 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
f45e313d 2069 &wait_queue.head, head) {
d88aee68 2070 cds_list_del(&wait_node->head);
f45e313d 2071 wait_queue.count--;
d88aee68
DG
2072 free(wait_node);
2073 }
2074
772b8f4d
MD
2075 /* Empty command queue. */
2076 for (;;) {
2077 /* Dequeue command for registration */
2078 node = cds_wfcq_dequeue_blocking(&ust_cmd_queue.head, &ust_cmd_queue.tail);
2079 if (node == NULL) {
2080 break;
2081 }
2082 ust_cmd = caa_container_of(node, struct ust_command, node);
2083 ret = close(ust_cmd->sock);
2084 if (ret < 0) {
2085 PERROR("close ust sock exit dispatch %d", ust_cmd->sock);
2086 }
2087 lttng_fd_put(LTTNG_FD_APPS, 1);
2088 free(ust_cmd);
2089 }
2090
9ad42ec1 2091error_testpoint:
099e26bd 2092 DBG("Dispatch thread dying");
12e2b881
MD
2093 if (err) {
2094 health_error();
2095 ERR("Health error occurred in %s", __func__);
2096 }
8782cc74 2097 health_unregister(health_sessiond);
099e26bd
DG
2098 return NULL;
2099}
2100
2101/*
2102 * This thread manage application registration.
2103 */
2104static void *thread_registration_apps(void *data)
1d4b027a 2105{
139ac872 2106 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
2107 uint32_t revents, nb_fd;
2108 struct lttng_poll_event events;
099e26bd
DG
2109 /*
2110 * Get allocated in this thread, enqueued to a global queue, dequeued and
2111 * freed in the manage apps thread.
2112 */
2113 struct ust_command *ust_cmd = NULL;
1d4b027a 2114
099e26bd 2115 DBG("[thread] Manage application registration started");
1d4b027a 2116
6c71277b 2117 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
927ca06a 2118
e547b070 2119 if (testpoint(sessiond_thread_registration_apps)) {
6993eeb3
CB
2120 goto error_testpoint;
2121 }
8ac94142 2122
1d4b027a
DG
2123 ret = lttcomm_listen_unix_sock(apps_sock);
2124 if (ret < 0) {
76d7553f 2125 goto error_listen;
1d4b027a
DG
2126 }
2127
5eb91c98
DG
2128 /*
2129 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2130 * more will be added to this poll set.
2131 */
d0b96690 2132 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 2133 if (ret < 0) {
76d7553f 2134 goto error_create_poll;
5eb91c98 2135 }
273ea72c 2136
5eb91c98
DG
2137 /* Add the application registration socket */
2138 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
2139 if (ret < 0) {
76d7553f 2140 goto error_poll_add;
5eb91c98 2141 }
273ea72c 2142
1d4b027a 2143 /* Notify all applications to register */
0fdd1e2c
DG
2144 ret = notify_ust_apps(1);
2145 if (ret < 0) {
2146 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
2147 "Execution continues but there might be problem for already\n"
2148 "running applications that wishes to register.");
0fdd1e2c 2149 }
1d4b027a
DG
2150
2151 while (1) {
2152 DBG("Accepting application registration");
273ea72c
DG
2153
2154 /* Inifinite blocking call, waiting for transmission */
88f2b785 2155 restart:
a78af745 2156 health_poll_entry();
5eb91c98 2157 ret = lttng_poll_wait(&events, -1);
a78af745 2158 health_poll_exit();
273ea72c 2159 if (ret < 0) {
88f2b785
MD
2160 /*
2161 * Restart interrupted system call.
2162 */
2163 if (errno == EINTR) {
2164 goto restart;
2165 }
273ea72c
DG
2166 goto error;
2167 }
2168
0d9c5d77
DG
2169 nb_fd = ret;
2170
5eb91c98 2171 for (i = 0; i < nb_fd; i++) {
840cb59c 2172 health_code_update();
139ac872 2173
5eb91c98
DG
2174 /* Fetch once the poll data */
2175 revents = LTTNG_POLL_GETEV(&events, i);
2176 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 2177
fd20dac9
MD
2178 if (!revents) {
2179 /* No activity for this FD (poll implementation). */
2180 continue;
2181 }
2182
5eb91c98 2183 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 2184 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 2185 if (ret) {
139ac872
MD
2186 err = 0;
2187 goto exit;
90014c57 2188 }
1d4b027a 2189
5eb91c98
DG
2190 /* Event on the registration socket */
2191 if (pollfd == apps_sock) {
03e43155 2192 if (revents & LPOLLIN) {
5eb91c98
DG
2193 sock = lttcomm_accept_unix_sock(apps_sock);
2194 if (sock < 0) {
2195 goto error;
2196 }
099e26bd 2197
16c5c8fa
DG
2198 /*
2199 * Set socket timeout for both receiving and ending.
2200 * app_socket_timeout is in seconds, whereas
2201 * lttcomm_setsockopt_rcv_timeout and
2202 * lttcomm_setsockopt_snd_timeout expect msec as
2203 * parameter.
2204 */
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
b662582b
DG
2210 /*
2211 * Set the CLOEXEC flag. Return code is useless because
2212 * either way, the show must go on.
2213 */
2214 (void) utils_set_fd_cloexec(sock);
2215
5eb91c98 2216 /* Create UST registration command for enqueuing */
ba7f0ae5 2217 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 2218 if (ust_cmd == NULL) {
76d7553f 2219 PERROR("ust command zmalloc");
41ed8e47
MD
2220 ret = close(sock);
2221 if (ret) {
2222 PERROR("close");
2223 }
5eb91c98
DG
2224 goto error;
2225 }
1d4b027a 2226
5eb91c98
DG
2227 /*
2228 * Using message-based transmissions to ensure we don't
2229 * have to deal with partially received messages.
2230 */
4063050c
MD
2231 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2232 if (ret < 0) {
2233 ERR("Exhausted file descriptors allowed for applications.");
2234 free(ust_cmd);
2235 ret = close(sock);
2236 if (ret) {
2237 PERROR("close");
2238 }
2239 sock = -1;
2240 continue;
2241 }
d88aee68 2242
840cb59c 2243 health_code_update();
d0b96690
DG
2244 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
2245 if (ret < 0) {
5eb91c98 2246 free(ust_cmd);
d0b96690 2247 /* Close socket of the application. */
76d7553f
MD
2248 ret = close(sock);
2249 if (ret) {
2250 PERROR("close");
2251 }
4063050c 2252 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 2253 sock = -1;
5eb91c98
DG
2254 continue;
2255 }
840cb59c 2256 health_code_update();
099e26bd 2257
5eb91c98 2258 ust_cmd->sock = sock;
34a2494f 2259 sock = -1;
099e26bd 2260
5eb91c98
DG
2261 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2262 " gid:%d sock:%d name:%s (version %d.%d)",
2263 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2264 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2265 ust_cmd->sock, ust_cmd->reg_msg.name,
2266 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 2267
5eb91c98
DG
2268 /*
2269 * Lock free enqueue the registration request. The red pill
54d01ffb 2270 * has been taken! This apps will be part of the *system*.
5eb91c98 2271 */
8bdee6e2 2272 cds_wfcq_enqueue(&ust_cmd_queue.head, &ust_cmd_queue.tail, &ust_cmd->node);
5eb91c98
DG
2273
2274 /*
2275 * Wake the registration queue futex. Implicit memory
8bdee6e2 2276 * barrier with the exchange in cds_wfcq_enqueue.
5eb91c98
DG
2277 */
2278 futex_nto1_wake(&ust_cmd_queue.futex);
03e43155
MD
2279 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
2280 ERR("Register apps socket poll error");
2281 goto error;
2282 } else {
2283 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
2284 goto error;
5eb91c98
DG
2285 }
2286 }
90014c57 2287 }
1d4b027a
DG
2288 }
2289
139ac872 2290exit:
1d4b027a 2291error:
0fdd1e2c
DG
2292 /* Notify that the registration thread is gone */
2293 notify_ust_apps(0);
2294
a4b35e07 2295 if (apps_sock >= 0) {
76d7553f
MD
2296 ret = close(apps_sock);
2297 if (ret) {
2298 PERROR("close");
2299 }
a4b35e07 2300 }
46c3f085 2301 if (sock >= 0) {
76d7553f
MD
2302 ret = close(sock);
2303 if (ret) {
2304 PERROR("close");
2305 }
4063050c 2306 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 2307 }
273ea72c 2308 unlink(apps_unix_sock_path);
0fdd1e2c 2309
76d7553f 2310error_poll_add:
5eb91c98 2311 lttng_poll_clean(&events);
76d7553f
MD
2312error_listen:
2313error_create_poll:
6993eeb3 2314error_testpoint:
76d7553f 2315 DBG("UST Registration thread cleanup complete");
9ad42ec1
MD
2316 if (err) {
2317 health_error();
2318 ERR("Health error occurred in %s", __func__);
2319 }
8782cc74 2320 health_unregister(health_sessiond);
5eb91c98 2321
1d4b027a
DG
2322 return NULL;
2323}
2324
8c0faa1d 2325/*
3bd1e081 2326 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 2327 * exec or it will fails.
8c0faa1d 2328 */
3bd1e081 2329static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 2330{
a23ec3a7 2331 int ret, clock_ret;
ee0b0061
DG
2332 struct timespec timeout;
2333
13a7bce3
JG
2334 /*
2335 * Make sure we set the readiness flag to 0 because we are NOT ready.
2336 * This access to consumer_thread_is_ready does not need to be
2337 * protected by consumer_data.cond_mutex (yet) since the consumer
2338 * management thread has not been started at this point.
2339 */
a23ec3a7 2340 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 2341
a23ec3a7
DG
2342 /* Setup pthread condition */
2343 ret = pthread_condattr_init(&consumer_data->condattr);
4a15001e 2344 if (ret) {
a23ec3a7
DG
2345 errno = ret;
2346 PERROR("pthread_condattr_init consumer data");
2347 goto error;
2348 }
2349
2350 /*
2351 * Set the monotonic clock in order to make sure we DO NOT jump in time
2352 * between the clock_gettime() call and the timedwait call. See bug #324
2353 * for a more details and how we noticed it.
2354 */
2355 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
4a15001e 2356 if (ret) {
a23ec3a7
DG
2357 errno = ret;
2358 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
2359 goto error;
2360 }
8c0faa1d 2361
a23ec3a7 2362 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
4a15001e 2363 if (ret) {
a23ec3a7
DG
2364 errno = ret;
2365 PERROR("pthread_cond_init consumer data");
2366 goto error;
2367 }
2368
2369 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2370 consumer_data);
4a15001e
MD
2371 if (ret) {
2372 errno = ret;
3bd1e081 2373 PERROR("pthread_create consumer");
ee0b0061 2374 ret = -1;
8c0faa1d
DG
2375 goto error;
2376 }
2377
a23ec3a7
DG
2378 /* We are about to wait on a pthread condition */
2379 pthread_mutex_lock(&consumer_data->cond_mutex);
2380
ee0b0061 2381 /* Get time for sem_timedwait absolute timeout */
a23ec3a7
DG
2382 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2383 /*
2384 * Set the timeout for the condition timed wait even if the clock gettime
2385 * call fails since we might loop on that call and we want to avoid to
2386 * increment the timeout too many times.
2387 */
2388 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2389
2390 /*
2391 * The following loop COULD be skipped in some conditions so this is why we
2392 * set ret to 0 in order to make sure at least one round of the loop is
2393 * done.
2394 */
2395 ret = 0;
2396
2397 /*
2398 * Loop until the condition is reached or when a timeout is reached. Note
2399 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2400 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2401 * possible. This loop does not take any chances and works with both of
2402 * them.
2403 */
2404 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2405 if (clock_ret < 0) {
2406 PERROR("clock_gettime spawn consumer");
2407 /* Infinite wait for the consumerd thread to be ready */
2408 ret = pthread_cond_wait(&consumer_data->cond,
2409 &consumer_data->cond_mutex);
2410 } else {
2411 ret = pthread_cond_timedwait(&consumer_data->cond,
2412 &consumer_data->cond_mutex, &timeout);
2413 }
ee0b0061 2414 }
8c0faa1d 2415
a23ec3a7
DG
2416 /* Release the pthread condition */
2417 pthread_mutex_unlock(&consumer_data->cond_mutex);
2418
2419 if (ret != 0) {
2420 errno = ret;
2421 if (ret == ETIMEDOUT) {
4282f9a3
DG
2422 int pth_ret;
2423
ee0b0061
DG
2424 /*
2425 * Call has timed out so we kill the kconsumerd_thread and return
2426 * an error.
2427 */
a23ec3a7
DG
2428 ERR("Condition timed out. The consumer thread was never ready."
2429 " Killing it");
4282f9a3
DG
2430 pth_ret = pthread_cancel(consumer_data->thread);
2431 if (pth_ret < 0) {
3bd1e081 2432 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
2433 }
2434 } else {
a23ec3a7 2435 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061 2436 }
4282f9a3
DG
2437 /* Caller is expecting a negative value on failure. */
2438 ret = -1;
ee0b0061
DG
2439 goto error;
2440 }
2441
3bd1e081
MD
2442 pthread_mutex_lock(&consumer_data->pid_mutex);
2443 if (consumer_data->pid == 0) {
a23ec3a7 2444 ERR("Consumerd did not start");
3bd1e081 2445 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
2446 goto error;
2447 }
3bd1e081 2448 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 2449
8c0faa1d
DG
2450 return 0;
2451
2452error:
2453 return ret;
2454}
2455
d9800920 2456/*
3bd1e081 2457 * Join consumer thread
d9800920 2458 */
3bd1e081 2459static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
2460{
2461 void *status;
cf3af59e 2462
e8209f6b
DG
2463 /* Consumer pid must be a real one. */
2464 if (consumer_data->pid > 0) {
c617c0c6 2465 int ret;
3bd1e081 2466 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 2467 if (ret) {
4a15001e 2468 PERROR("Error killing consumer daemon");
cf3af59e
MD
2469 return ret;
2470 }
3bd1e081 2471 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
2472 } else {
2473 return 0;
2474 }
2475}
2476
8c0faa1d 2477/*
3bd1e081 2478 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 2479 *
d063d709 2480 * Return pid if successful else -1.
8c0faa1d 2481 */
3bd1e081 2482static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
2483{
2484 int ret;
2485 pid_t pid;
94c55f17 2486 const char *consumer_to_use;
53086306 2487 const char *verbosity;
94c55f17 2488 struct stat st;
8c0faa1d 2489
3bd1e081 2490 DBG("Spawning consumerd");
c49dc785 2491
8c0faa1d
DG
2492 pid = fork();
2493 if (pid == 0) {
2494 /*
3bd1e081 2495 * Exec consumerd.
8c0faa1d 2496 */
daee5345 2497 if (opt_verbose_consumer) {
53086306 2498 verbosity = "--verbose";
4421f712 2499 } else if (lttng_opt_quiet) {
53086306 2500 verbosity = "--quiet";
4421f712
DG
2501 } else {
2502 verbosity = "";
53086306 2503 }
4421f712 2504
3bd1e081
MD
2505 switch (consumer_data->type) {
2506 case LTTNG_CONSUMER_KERNEL:
94c55f17 2507 /*
c7704d57
DG
2508 * Find out which consumerd to execute. We will first try the
2509 * 64-bit path, then the sessiond's installation directory, and
2510 * fallback on the 32-bit one,
94c55f17 2511 */
63a799e8
AM
2512 DBG3("Looking for a kernel consumer at these locations:");
2513 DBG3(" 1) %s", consumerd64_bin);
2514 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2515 DBG3(" 3) %s", consumerd32_bin);
94c55f17 2516 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 2517 DBG3("Found location #1");
94c55f17 2518 consumer_to_use = consumerd64_bin;
94c55f17 2519 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 2520 DBG3("Found location #2");
94c55f17 2521 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 2522 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 2523 DBG3("Found location #3");
eb1e0bd4 2524 consumer_to_use = consumerd32_bin;
94c55f17 2525 } else {
63a799e8 2526 DBG("Could not find any valid consumerd executable");
4282f9a3 2527 ret = -EINVAL;
94c55f17
AM
2528 break;
2529 }
2530 DBG("Using kernel consumer at: %s", consumer_to_use);
4282f9a3 2531 ret = execl(consumer_to_use,
94c55f17
AM
2532 "lttng-consumerd", verbosity, "-k",
2533 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2534 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2535 "--group", tracing_group_name,
94c55f17 2536 NULL);
3bd1e081 2537 break;
7753dea8
MD
2538 case LTTNG_CONSUMER64_UST:
2539 {
b1e0b6b6 2540 char *tmpnew = NULL;
8f4905da
MD
2541
2542 if (consumerd64_libdir[0] != '\0') {
2543 char *tmp;
2544 size_t tmplen;
2545
e8fa9fb0 2546 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2547 if (!tmp) {
2548 tmp = "";
2549 }
2550 tmplen = strlen("LD_LIBRARY_PATH=")
2551 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2552 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2553 if (!tmpnew) {
2554 ret = -ENOMEM;
2555 goto error;
2556 }
2557 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2558 strcat(tmpnew, consumerd64_libdir);
2559 if (tmp[0] != '\0') {
2560 strcat(tmpnew, ":");
2561 strcat(tmpnew, tmp);
2562 }
2563 ret = putenv(tmpnew);
2564 if (ret) {
2565 ret = -errno;
c6f76da9 2566 free(tmpnew);
8f4905da
MD
2567 goto error;
2568 }
2569 }
94c55f17 2570 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 2571 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2572 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2573 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2574 "--group", tracing_group_name,
7753dea8 2575 NULL);
8f4905da
MD
2576 if (consumerd64_libdir[0] != '\0') {
2577 free(tmpnew);
2578 }
3bd1e081 2579 break;
7753dea8
MD
2580 }
2581 case LTTNG_CONSUMER32_UST:
2582 {
937dde8e 2583 char *tmpnew = NULL;
8f4905da
MD
2584
2585 if (consumerd32_libdir[0] != '\0') {
2586 char *tmp;
2587 size_t tmplen;
2588
e8fa9fb0 2589 tmp = lttng_secure_getenv("LD_LIBRARY_PATH");
8f4905da
MD
2590 if (!tmp) {
2591 tmp = "";
2592 }
2593 tmplen = strlen("LD_LIBRARY_PATH=")
2594 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2595 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2596 if (!tmpnew) {
2597 ret = -ENOMEM;
2598 goto error;
2599 }
2600 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2601 strcat(tmpnew, consumerd32_libdir);
2602 if (tmp[0] != '\0') {
2603 strcat(tmpnew, ":");
2604 strcat(tmpnew, tmp);
2605 }
2606 ret = putenv(tmpnew);
2607 if (ret) {
2608 ret = -errno;
c6f76da9 2609 free(tmpnew);
8f4905da
MD
2610 goto error;
2611 }
2612 }
94c55f17 2613 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 2614 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
2615 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2616 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
6c71277b 2617 "--group", tracing_group_name,
7753dea8 2618 NULL);
8f4905da
MD
2619 if (consumerd32_libdir[0] != '\0') {
2620 free(tmpnew);
2621 }
7753dea8
MD
2622 break;
2623 }
3bd1e081 2624 default:
76d7553f 2625 PERROR("unknown consumer type");
3bd1e081
MD
2626 exit(EXIT_FAILURE);
2627 }
8c0faa1d 2628 if (errno != 0) {
4282f9a3 2629 PERROR("Consumer execl()");
8c0faa1d 2630 }
4282f9a3 2631 /* Reaching this point, we got a failure on our execl(). */
8c0faa1d
DG
2632 exit(EXIT_FAILURE);
2633 } else if (pid > 0) {
2634 ret = pid;
8c0faa1d 2635 } else {
76d7553f 2636 PERROR("start consumer fork");
8c0faa1d 2637 ret = -errno;
8c0faa1d 2638 }
8f4905da 2639error:
8c0faa1d
DG
2640 return ret;
2641}
2642
693bd40b 2643/*
3bd1e081 2644 * Spawn the consumerd daemon and session daemon thread.
693bd40b 2645 */
3bd1e081 2646static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 2647{
c617c0c6 2648 int ret;
edb8b045
DG
2649
2650 /*
2651 * Set the listen() state on the socket since there is a possible race
2652 * between the exec() of the consumer daemon and this call if place in the
2653 * consumer thread. See bug #366 for more details.
2654 */
2655 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2656 if (ret < 0) {
2657 goto error;
2658 }
693bd40b 2659
3bd1e081
MD
2660 pthread_mutex_lock(&consumer_data->pid_mutex);
2661 if (consumer_data->pid != 0) {
2662 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
2663 goto end;
2664 }
693bd40b 2665
3bd1e081 2666 ret = spawn_consumerd(consumer_data);
c49dc785 2667 if (ret < 0) {
3bd1e081
MD
2668 ERR("Spawning consumerd failed");
2669 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 2670 goto error;
693bd40b 2671 }
c49dc785 2672
3bd1e081
MD
2673 /* Setting up the consumer_data pid */
2674 consumer_data->pid = ret;
48842b30 2675 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 2676 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 2677
3bd1e081
MD
2678 DBG2("Spawning consumer control thread");
2679 ret = spawn_consumer_thread(consumer_data);
693bd40b 2680 if (ret < 0) {
3bd1e081 2681 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
2682 goto error;
2683 }
2684
c49dc785 2685end:
693bd40b
DG
2686 return 0;
2687
2688error:
331744e3 2689 /* Cleanup already created sockets on error. */
edb8b045 2690 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
2691 int err;
2692
edb8b045
DG
2693 err = close(consumer_data->err_sock);
2694 if (err < 0) {
2695 PERROR("close consumer data error socket");
2696 }
2697 }
693bd40b
DG
2698 return ret;
2699}
2700
b73401da 2701/*
096102bd 2702 * Setup necessary data for kernel tracer action.
b73401da 2703 */
096102bd 2704static int init_kernel_tracer(void)
b73401da
DG
2705{
2706 int ret;
b73401da 2707
096102bd
DG
2708 /* Modprobe lttng kernel modules */
2709 ret = modprobe_lttng_control();
b73401da 2710 if (ret < 0) {
b73401da
DG
2711 goto error;
2712 }
2713
096102bd
DG
2714 /* Open debugfs lttng */
2715 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2716 if (kernel_tracer_fd < 0) {
2717 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b
DG
2718 ret = -1;
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:
93ec662e 3007 case LTTNG_METADATA_REGENERATE:
2e09ba09 3008 need_domain = 0;
3aace903 3009 break;
2e09ba09
MD
3010 default:
3011 need_domain = 1;
3012 }
3013
3014 if (opt_no_kernel && need_domain
3015 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 3016 if (!is_root) {
f73fabfd 3017 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 3018 } else {
f73fabfd 3019 ret = LTTNG_ERR_KERN_NA;
531d29f9 3020 }
4fba7219
DG
3021 goto error;
3022 }
3023
8d3113b2
DG
3024 /* Deny register consumer if we already have a spawned consumer. */
3025 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
3026 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3027 if (kconsumer_data.pid > 0) {
f73fabfd 3028 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 3029 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
3030 goto error;
3031 }
3032 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3033 }
3034
54d01ffb
DG
3035 /*
3036 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 3037 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
3038 * command.
3039 */
3040 switch(cmd_ctx->lsm->cmd_type) {
3041 case LTTNG_LIST_SESSIONS:
3042 case LTTNG_LIST_TRACEPOINTS:
f37d259d 3043 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
3044 case LTTNG_LIST_DOMAINS:
3045 case LTTNG_LIST_CHANNELS:
3046 case LTTNG_LIST_EVENTS:
834978fd 3047 case LTTNG_LIST_SYSCALLS:
a5dfbb9d 3048 case LTTNG_LIST_TRACKER_PIDS:
5cd0780d 3049 case LTTNG_DATA_PENDING:
54d01ffb
DG
3050 break;
3051 default:
3052 /* Setup lttng message with no payload */
6e10c9b9 3053 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0);
54d01ffb
DG
3054 if (ret < 0) {
3055 /* This label does not try to unlock the session */
3056 goto init_setup_error;
3057 }
3058 }
3059
3060 /* Commands that DO NOT need a session. */
3061 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 3062 case LTTNG_CREATE_SESSION:
27babd3a 3063 case LTTNG_CREATE_SESSION_SNAPSHOT:
ecc48a90 3064 case LTTNG_CREATE_SESSION_LIVE:
2e09ba09 3065 case LTTNG_CALIBRATE:
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 }
d0254c7c
MD
3907 case LTTNG_CALIBRATE:
3908 {
54d01ffb
DG
3909 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3910 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
3911 break;
3912 }
d9800920
DG
3913 case LTTNG_REGISTER_CONSUMER:
3914 {
2f77fc4b
DG
3915 struct consumer_data *cdata;
3916
3917 switch (cmd_ctx->lsm->domain.type) {
3918 case LTTNG_DOMAIN_KERNEL:
3919 cdata = &kconsumer_data;
3920 break;
3921 default:
f73fabfd 3922 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3923 goto error;
3924 }
3925
54d01ffb 3926 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 3927 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
3928 break;
3929 }
6d805429 3930 case LTTNG_DATA_PENDING:
806e2684 3931 {
f6151c55 3932 int pending_ret;
6e10c9b9 3933 uint8_t pending_ret_byte;
f6151c55
JG
3934
3935 pending_ret = cmd_data_pending(cmd_ctx->session);
6e10c9b9 3936
f6151c55
JG
3937 /*
3938 * FIXME
3939 *
3940 * This function may returns 0 or 1 to indicate whether or not
3941 * there is data pending. In case of error, it should return an
3942 * LTTNG_ERR code. However, some code paths may still return
3943 * a nondescript error code, which we handle by returning an
3944 * "unknown" error.
3945 */
3946 if (pending_ret == 0 || pending_ret == 1) {
6e10c9b9
PP
3947 /*
3948 * ret will be set to LTTNG_OK at the end of
3949 * this function.
3950 */
f6151c55
JG
3951 } else if (pending_ret < 0) {
3952 ret = LTTNG_ERR_UNK;
3953 goto setup_error;
3954 } else {
3955 ret = pending_ret;
3956 goto setup_error;
3957 }
3958
6e10c9b9
PP
3959 pending_ret_byte = (uint8_t) pending_ret;
3960
3961 /* 1 byte to return whether or not data is pending */
3962 ret = setup_lttng_msg_no_cmd_header(cmd_ctx,
3963 &pending_ret_byte, 1);
3964
3965 if (ret < 0) {
3966 goto setup_error;
3967 }
3968
3969 ret = LTTNG_OK;
806e2684
DG
3970 break;
3971 }
da3c9ec1
DG
3972 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3973 {
6dc3064a
DG
3974 struct lttcomm_lttng_output_id reply;
3975
3976 ret = cmd_snapshot_add_output(cmd_ctx->session,
3977 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3978 if (ret != LTTNG_OK) {
3979 goto error;
3980 }
3981
6e10c9b9
PP
3982 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, &reply,
3983 sizeof(reply));
6dc3064a
DG
3984 if (ret < 0) {
3985 goto setup_error;
3986 }
3987
3988 /* Copy output list into message payload */
6dc3064a 3989 ret = LTTNG_OK;
da3c9ec1
DG
3990 break;
3991 }
3992 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3993 {
6dc3064a
DG
3994 ret = cmd_snapshot_del_output(cmd_ctx->session,
3995 &cmd_ctx->lsm->u.snapshot_output.output);
da3c9ec1
DG
3996 break;
3997 }
3998 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3999 {
6dc3064a
DG
4000 ssize_t nb_output;
4001 struct lttng_snapshot_output *outputs = NULL;
4002
4003 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
4004 if (nb_output < 0) {
4005 ret = -nb_output;
4006 goto error;
4007 }
4008
6e10c9b9
PP
4009 assert((nb_output > 0 && outputs) || nb_output == 0);
4010 ret = setup_lttng_msg_no_cmd_header(cmd_ctx, outputs,
6dc3064a 4011 nb_output * sizeof(struct lttng_snapshot_output));
6e10c9b9
PP
4012 free(outputs);
4013
6dc3064a 4014 if (ret < 0) {
6dc3064a
DG
4015 goto setup_error;
4016 }
4017
6dc3064a 4018 ret = LTTNG_OK;
da3c9ec1
DG
4019 break;
4020 }
4021 case LTTNG_SNAPSHOT_RECORD:
4022 {
6dc3064a
DG
4023 ret = cmd_snapshot_record(cmd_ctx->session,
4024 &cmd_ctx->lsm->u.snapshot_record.output,
4025 cmd_ctx->lsm->u.snapshot_record.wait);
da3c9ec1
DG
4026 break;
4027 }
27babd3a
DG
4028 case LTTNG_CREATE_SESSION_SNAPSHOT:
4029 {
4030 size_t nb_uri, len;
4031 struct lttng_uri *uris = NULL;
4032
4033 nb_uri = cmd_ctx->lsm->u.uri.size;
4034 len = nb_uri * sizeof(struct lttng_uri);
4035
4036 if (nb_uri > 0) {
4037 uris = zmalloc(len);
4038 if (uris == NULL) {
4039 ret = LTTNG_ERR_FATAL;
4040 goto error;
4041 }
4042
4043 /* Receive variable len data */
4044 DBG("Waiting for %zu URIs from client ...", nb_uri);
4045 ret = lttcomm_recv_unix_sock(sock, uris, len);
4046 if (ret <= 0) {
4047 DBG("No URIs received from client... continuing");
4048 *sock_error = 1;
4049 ret = LTTNG_ERR_SESSION_FAIL;
4050 free(uris);
4051 goto error;
4052 }
4053
4054 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4055 DBG("Creating session with ONE network URI is a bad call");
4056 ret = LTTNG_ERR_SESSION_FAIL;
4057 free(uris);
4058 goto error;
4059 }
4060 }
4061
4062 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
4063 nb_uri, &cmd_ctx->creds);
4064 free(uris);
4065 break;
4066 }
ecc48a90
JD
4067 case LTTNG_CREATE_SESSION_LIVE:
4068 {
4069 size_t nb_uri, len;
4070 struct lttng_uri *uris = NULL;
4071
4072 nb_uri = cmd_ctx->lsm->u.uri.size;
4073 len = nb_uri * sizeof(struct lttng_uri);
4074
4075 if (nb_uri > 0) {
4076 uris = zmalloc(len);
4077 if (uris == NULL) {
4078 ret = LTTNG_ERR_FATAL;
4079 goto error;
4080 }
4081
4082 /* Receive variable len data */
4083 DBG("Waiting for %zu URIs from client ...", nb_uri);
4084 ret = lttcomm_recv_unix_sock(sock, uris, len);
4085 if (ret <= 0) {
4086 DBG("No URIs received from client... continuing");
4087 *sock_error = 1;
4088 ret = LTTNG_ERR_SESSION_FAIL;
4089 free(uris);
4090 goto error;
4091 }
4092
4093 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4094 DBG("Creating session with ONE network URI is a bad call");
4095 ret = LTTNG_ERR_SESSION_FAIL;
4096 free(uris);
4097 goto error;
4098 }
4099 }
4100
4101 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
4102 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
4103 free(uris);
4104 break;
4105 }
fb198a11
JG
4106 case LTTNG_SAVE_SESSION:
4107 {
4108 ret = cmd_save_sessions(&cmd_ctx->lsm->u.save_session.attr,
4109 &cmd_ctx->creds);
4110 break;
4111 }
d7ba1388
MD
4112 case LTTNG_SET_SESSION_SHM_PATH:
4113 {
4114 ret = cmd_set_session_shm_path(cmd_ctx->session,
4115 cmd_ctx->lsm->u.set_shm_path.shm_path);
4116 break;
4117 }
93ec662e
JD
4118 case LTTNG_METADATA_REGENERATE:
4119 {
4120 ret = cmd_metadata_regenerate(cmd_ctx->session);
4121 break;
4122 }
5e16da05 4123 default:
f73fabfd 4124 ret = LTTNG_ERR_UND;
5461b305 4125 break;
fac6795d
DG
4126 }
4127
5461b305 4128error:
5461b305
DG
4129 if (cmd_ctx->llm == NULL) {
4130 DBG("Missing llm structure. Allocating one.");
6e10c9b9 4131 if (setup_lttng_msg_no_cmd_header(cmd_ctx, NULL, 0) < 0) {
5461b305
DG
4132 goto setup_error;
4133 }
4134 }
54d01ffb 4135 /* Set return code */
5461b305 4136 cmd_ctx->llm->ret_code = ret;
5461b305 4137setup_error:
b5541356 4138 if (cmd_ctx->session) {
54d01ffb 4139 session_unlock(cmd_ctx->session);
b5541356 4140 }
256a5576
MD
4141 if (need_tracing_session) {
4142 session_unlock_list();
4143 }
54d01ffb 4144init_setup_error:
3745d315 4145 assert(!rcu_read_ongoing());
8028d920 4146 return ret;
fac6795d
DG
4147}
4148
44a5e5eb
DG
4149/*
4150 * Thread managing health check socket.
4151 */
4152static void *thread_manage_health(void *data)
4153{
eb4a2943 4154 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
4155 uint32_t revents, nb_fd;
4156 struct lttng_poll_event events;
0c89d795
MD
4157 struct health_comm_msg msg;
4158 struct health_comm_reply reply;
44a5e5eb
DG
4159
4160 DBG("[thread] Manage health check started");
4161
4162 rcu_register_thread();
4163
6d737ce4
DG
4164 /* We might hit an error path before this is created. */
4165 lttng_poll_init(&events);
3cc04881 4166
44a5e5eb
DG
4167 /* Create unix socket */
4168 sock = lttcomm_create_unix_sock(health_unix_sock_path);
4169 if (sock < 0) {
4170 ERR("Unable to create health check Unix socket");
44a5e5eb
DG
4171 goto error;
4172 }
4173
6c71277b
MD
4174 if (is_root) {
4175 /* lttng health client socket path permissions */
4176 ret = chown(health_unix_sock_path, 0,
4177 utils_get_group_id(tracing_group_name));
4178 if (ret < 0) {
4179 ERR("Unable to set group on %s", health_unix_sock_path);
4180 PERROR("chown");
6c71277b
MD
4181 goto error;
4182 }
4183
4184 ret = chmod(health_unix_sock_path,
4185 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4186 if (ret < 0) {
4187 ERR("Unable to set permissions on %s", health_unix_sock_path);
4188 PERROR("chmod");
6c71277b
MD
4189 goto error;
4190 }
4191 }
4192
b662582b
DG
4193 /*
4194 * Set the CLOEXEC flag. Return code is useless because either way, the
4195 * show must go on.
4196 */
4197 (void) utils_set_fd_cloexec(sock);
4198
44a5e5eb
DG
4199 ret = lttcomm_listen_unix_sock(sock);
4200 if (ret < 0) {
4201 goto error;
4202 }
4203
4204 /*
4205 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4206 * more will be added to this poll set.
4207 */
d0b96690 4208 ret = sessiond_set_thread_pollset(&events, 2);
44a5e5eb
DG
4209 if (ret < 0) {
4210 goto error;
4211 }
4212
4213 /* Add the application registration socket */
4214 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
4215 if (ret < 0) {
4216 goto error;
4217 }
4218
ef367a93 4219 sessiond_notify_ready();
97bc1426 4220
44a5e5eb
DG
4221 while (1) {
4222 DBG("Health check ready");
4223
44a5e5eb
DG
4224 /* Inifinite blocking call, waiting for transmission */
4225restart:
4226 ret = lttng_poll_wait(&events, -1);
4227 if (ret < 0) {
4228 /*
4229 * Restart interrupted system call.
4230 */
4231 if (errno == EINTR) {
4232 goto restart;
4233 }
4234 goto error;
4235 }
4236
0d9c5d77
DG
4237 nb_fd = ret;
4238
44a5e5eb
DG
4239 for (i = 0; i < nb_fd; i++) {
4240 /* Fetch once the poll data */
4241 revents = LTTNG_POLL_GETEV(&events, i);
4242 pollfd = LTTNG_POLL_GETFD(&events, i);
4243
fd20dac9
MD
4244 if (!revents) {
4245 /* No activity for this FD (poll implementation). */
4246 continue;
4247 }
4248
44a5e5eb 4249 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4250 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
44a5e5eb 4251 if (ret) {
139ac872
MD
4252 err = 0;
4253 goto exit;
44a5e5eb
DG
4254 }
4255
4256 /* Event on the registration socket */
4257 if (pollfd == sock) {
03e43155
MD
4258 if (revents & LPOLLIN) {
4259 continue;
4260 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
44a5e5eb
DG
4261 ERR("Health socket poll error");
4262 goto error;
03e43155
MD
4263 } else {
4264 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
4265 goto error;
44a5e5eb
DG
4266 }
4267 }
4268 }
4269
4270 new_sock = lttcomm_accept_unix_sock(sock);
4271 if (new_sock < 0) {
4272 goto error;
4273 }
4274
b662582b
DG
4275 /*
4276 * Set the CLOEXEC flag. Return code is useless because either way, the
4277 * show must go on.
4278 */
4279 (void) utils_set_fd_cloexec(new_sock);
4280
44a5e5eb
DG
4281 DBG("Receiving data from client for health...");
4282 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4283 if (ret <= 0) {
4284 DBG("Nothing recv() from client... continuing");
4285 ret = close(new_sock);
4286 if (ret) {
4287 PERROR("close");
4288 }
4289 new_sock = -1;
4290 continue;
4291 }
4292
4293 rcu_thread_online();
4294
53efb85a 4295 memset(&reply, 0, sizeof(reply));
6c71277b
MD
4296 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
4297 /*
4298 * health_check_state returns 0 if health is
4299 * bad.
4300 */
4301 if (!health_check_state(health_sessiond, i)) {
4302 reply.ret_code |= 1ULL << i;
4303 }
44a5e5eb
DG
4304 }
4305
6c71277b 4306 DBG2("Health check return value %" PRIx64, reply.ret_code);
44a5e5eb
DG
4307
4308 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4309 if (ret < 0) {
4310 ERR("Failed to send health data back to client");
4311 }
4312
4313 /* End of transmission */
4314 ret = close(new_sock);
4315 if (ret) {
4316 PERROR("close");
4317 }
4318 new_sock = -1;
4319 }
4320
139ac872 4321exit:
44a5e5eb 4322error:
139ac872
MD
4323 if (err) {
4324 ERR("Health error occurred in %s", __func__);
4325 }
44a5e5eb
DG
4326 DBG("Health check thread dying");
4327 unlink(health_unix_sock_path);
4328 if (sock >= 0) {
4329 ret = close(sock);
4330 if (ret) {
4331 PERROR("close");
4332 }
4333 }
44a5e5eb
DG
4334
4335 lttng_poll_clean(&events);
4336
4337 rcu_unregister_thread();
4338 return NULL;
4339}
4340
1d4b027a 4341/*
d063d709
DG
4342 * This thread manage all clients request using the unix client socket for
4343 * communication.
1d4b027a
DG
4344 */
4345static void *thread_manage_clients(void *data)
4346{
139ac872 4347 int sock = -1, ret, i, pollfd, err = -1;
53a80697 4348 int sock_error;
5eb91c98 4349 uint32_t revents, nb_fd;
273ea72c 4350 struct command_ctx *cmd_ctx = NULL;
5eb91c98 4351 struct lttng_poll_event events;
1d4b027a
DG
4352
4353 DBG("[thread] Manage client started");
4354
f6a9efaa
DG
4355 rcu_register_thread();
4356
6c71277b 4357 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
927ca06a 4358
840cb59c 4359 health_code_update();
44a5e5eb 4360
1d4b027a
DG
4361 ret = lttcomm_listen_unix_sock(client_sock);
4362 if (ret < 0) {
35df2755 4363 goto error_listen;
1d4b027a
DG
4364 }
4365
5eb91c98
DG
4366 /*
4367 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4368 * more will be added to this poll set.
4369 */
d0b96690 4370 ret = sessiond_set_thread_pollset(&events, 2);
5eb91c98 4371 if (ret < 0) {
35df2755 4372 goto error_create_poll;
5eb91c98 4373 }
273ea72c 4374
5eb91c98
DG
4375 /* Add the application registration socket */
4376 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
4377 if (ret < 0) {
4378 goto error;
4379 }
273ea72c 4380
ef367a93
JG
4381 sessiond_notify_ready();
4382 ret = sem_post(&load_info->message_thread_ready);
4383 if (ret) {
4384 PERROR("sem_post message_thread_ready");
4385 goto error;
4386 }
0bb7724a 4387
72453c8c
MD
4388 /* This testpoint is after we signal readiness to the parent. */
4389 if (testpoint(sessiond_thread_manage_clients)) {
4390 goto error;
4391 }
4392
e547b070 4393 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
6993eeb3
CB
4394 goto error;
4395 }
8ac94142 4396
840cb59c 4397 health_code_update();
44a5e5eb 4398
1d4b027a 4399 while (1) {
1d4b027a 4400 DBG("Accepting client command ...");
273ea72c
DG
4401
4402 /* Inifinite blocking call, waiting for transmission */
88f2b785 4403 restart:
a78af745 4404 health_poll_entry();
5eb91c98 4405 ret = lttng_poll_wait(&events, -1);
a78af745 4406 health_poll_exit();
273ea72c 4407 if (ret < 0) {
88f2b785
MD
4408 /*
4409 * Restart interrupted system call.
4410 */
4411 if (errno == EINTR) {
4412 goto restart;
4413 }
273ea72c
DG
4414 goto error;
4415 }
4416
0d9c5d77
DG
4417 nb_fd = ret;
4418
5eb91c98
DG
4419 for (i = 0; i < nb_fd; i++) {
4420 /* Fetch once the poll data */
4421 revents = LTTNG_POLL_GETEV(&events, i);
4422 pollfd = LTTNG_POLL_GETFD(&events, i);
4423
840cb59c 4424 health_code_update();
44a5e5eb 4425
fd20dac9
MD
4426 if (!revents) {
4427 /* No activity for this FD (poll implementation). */
4428 continue;
4429 }
4430
5eb91c98 4431 /* Thread quit pipe has been closed. Killing thread. */
d0b96690 4432 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
5eb91c98 4433 if (ret) {
139ac872
MD
4434 err = 0;
4435 goto exit;
5eb91c98
DG
4436 }
4437
4438 /* Event on the registration socket */
4439 if (pollfd == client_sock) {
03e43155
MD
4440 if (revents & LPOLLIN) {
4441 continue;
4442 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5eb91c98
DG
4443 ERR("Client socket poll error");
4444 goto error;
03e43155
MD
4445 } else {
4446 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
4447 goto error;
5eb91c98
DG
4448 }
4449 }
4450 }
4451
4452 DBG("Wait for client response");
4453
840cb59c 4454 health_code_update();
44a5e5eb 4455
5eb91c98
DG
4456 sock = lttcomm_accept_unix_sock(client_sock);
4457 if (sock < 0) {
273ea72c 4458 goto error;
273ea72c
DG
4459 }
4460
b662582b
DG
4461 /*
4462 * Set the CLOEXEC flag. Return code is useless because either way, the
4463 * show must go on.
4464 */
4465 (void) utils_set_fd_cloexec(sock);
4466
be040666
DG
4467 /* Set socket option for credentials retrieval */
4468 ret = lttcomm_setsockopt_creds_unix_sock(sock);
4469 if (ret < 0) {
4470 goto error;
4471 }
4472
5eb91c98 4473 /* Allocate context command to process the client request */
ba7f0ae5 4474 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 4475 if (cmd_ctx == NULL) {
76d7553f 4476 PERROR("zmalloc cmd_ctx");
1d4b027a 4477 goto error;
5eb91c98 4478 }
1d4b027a 4479
5eb91c98 4480 /* Allocate data buffer for reception */
ba7f0ae5 4481 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 4482 if (cmd_ctx->lsm == NULL) {
76d7553f 4483 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
4484 goto error;
4485 }
1d4b027a 4486
5eb91c98
DG
4487 cmd_ctx->llm = NULL;
4488 cmd_ctx->session = NULL;
1d4b027a 4489
840cb59c 4490 health_code_update();
44a5e5eb 4491
5eb91c98
DG
4492 /*
4493 * Data is received from the lttng client. The struct
4494 * lttcomm_session_msg (lsm) contains the command and data request of
4495 * the client.
4496 */
4497 DBG("Receiving data from client ...");
be040666
DG
4498 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
4499 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
4500 if (ret <= 0) {
4501 DBG("Nothing recv() from client... continuing");
76d7553f
MD
4502 ret = close(sock);
4503 if (ret) {
4504 PERROR("close");
4505 }
4506 sock = -1;
a2c0da86 4507 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4508 continue;
4509 }
1d4b027a 4510
840cb59c 4511 health_code_update();
44a5e5eb 4512
5eb91c98
DG
4513 // TODO: Validate cmd_ctx including sanity check for
4514 // security purpose.
f7776ea7 4515
f6a9efaa 4516 rcu_thread_online();
5eb91c98
DG
4517 /*
4518 * This function dispatch the work to the kernel or userspace tracer
4519 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4520 * informations for the client. The command context struct contains
4521 * everything this function may needs.
4522 */
53a80697 4523 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 4524 rcu_thread_offline();
5eb91c98 4525 if (ret < 0) {
36134aa1
DG
4526 ret = close(sock);
4527 if (ret) {
4528 PERROR("close");
53a80697 4529 }
36134aa1 4530 sock = -1;
bbd973c2 4531 /*
5eb91c98 4532 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 4533 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
4534 * (ENOMEM). Error detected but still accept
4535 * command, unless a socket error has been
4536 * detected.
bbd973c2 4537 */
bbd973c2 4538 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
4539 continue;
4540 }
d6e4fca4 4541
840cb59c 4542 health_code_update();
44a5e5eb 4543
a1ac8b97 4544 DBG("Sending response (size: %d, retcode: %s (%d))",
54d01ffb 4545 cmd_ctx->lttng_msg_size,
a1ac8b97
JG
4546 lttng_strerror(-cmd_ctx->llm->ret_code),
4547 cmd_ctx->llm->ret_code);
54d01ffb 4548 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
4549 if (ret < 0) {
4550 ERR("Failed to send data back to client");
bbd973c2 4551 }
1d4b027a 4552
5eb91c98 4553 /* End of transmission */
76d7553f
MD
4554 ret = close(sock);
4555 if (ret) {
4556 PERROR("close");
4557 }
4558 sock = -1;
be040666
DG
4559
4560 clean_command_ctx(&cmd_ctx);
44a5e5eb 4561
840cb59c 4562 health_code_update();
273ea72c
DG
4563 }
4564
139ac872 4565exit:
5eb91c98 4566error:
35df2755
CB
4567 if (sock >= 0) {
4568 ret = close(sock);
4569 if (ret) {
4570 PERROR("close");
4571 }
139ac872 4572 }
44a5e5eb 4573
35df2755
CB
4574 lttng_poll_clean(&events);
4575 clean_command_ctx(&cmd_ctx);
4576
4577error_listen:
4578error_create_poll:
273ea72c 4579 unlink(client_unix_sock_path);
76d7553f
MD
4580 if (client_sock >= 0) {
4581 ret = close(client_sock);
4582 if (ret) {
4583 PERROR("close");
4584 }
a4b35e07 4585 }
35df2755
CB
4586
4587 if (err) {
840cb59c 4588 health_error();
35df2755 4589 ERR("Health error occurred in %s", __func__);
a4b35e07 4590 }
273ea72c 4591
8782cc74 4592 health_unregister(health_sessiond);
35df2755
CB
4593
4594 DBG("Client thread dying");
f6a9efaa
DG
4595
4596 rcu_unregister_thread();
4a15001e
MD
4597
4598 /*
4599 * Since we are creating the consumer threads, we own them, so we need
4600 * to join them before our thread exits.
4601 */
4602 ret = join_consumer_thread(&kconsumer_data);
4603 if (ret) {
4604 errno = ret;
4605 PERROR("join_consumer");
4606 }
4607
4608 ret = join_consumer_thread(&ustconsumer32_data);
4609 if (ret) {
4610 errno = ret;
4611 PERROR("join_consumer ust32");
4612 }
4613
4614 ret = join_consumer_thread(&ustconsumer64_data);
4615 if (ret) {
4616 errno = ret;
4617 PERROR("join_consumer ust64");
4618 }
1d4b027a
DG
4619 return NULL;
4620}
4621
c0232ea5
JG
4622static int string_match(const char *str1, const char *str2)
4623{
4624 return (str1 && str2) && !strcmp(str1, str2);
4625}
4626
fac6795d 4627/*
26296c48
JG
4628 * Take an option from the getopt output and set it in the right variable to be
4629 * used later.
4630 *
4631 * Return 0 on success else a negative value.
fac6795d 4632 */
26296c48 4633static int set_option(int opt, const char *arg, const char *optname)
fac6795d 4634{
26296c48 4635 int ret = 0;
fac6795d 4636
c0232ea5 4637 if (string_match(optname, "client-sock") || opt == 'c') {
66b2ce8e
JG
4638 if (!arg || *arg == '\0') {
4639 ret = -EINVAL;
4640 goto end;
4641 }
e8fa9fb0
MD
4642 if (lttng_is_setuid_setgid()) {
4643 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4644 "-c, --client-sock");
4645 } else {
4646 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4647 }
c0232ea5 4648 } else if (string_match(optname, "apps-sock") || opt == 'a') {
66b2ce8e
JG
4649 if (!arg || *arg == '\0') {
4650 ret = -EINVAL;
4651 goto end;
4652 }
e8fa9fb0
MD
4653 if (lttng_is_setuid_setgid()) {
4654 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4655 "-a, --apps-sock");
4656 } else {
4657 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4658 }
c0232ea5 4659 } else if (string_match(optname, "daemonize") || opt == 'd') {
26296c48 4660 opt_daemon = 1;
c0232ea5 4661 } else if (string_match(optname, "background") || opt == 'b') {
72dd7491 4662 opt_background = 1;
c0232ea5 4663 } else if (string_match(optname, "group") || opt == 'g') {
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 "-g, --group");
4671 } else {
4672 /*
4673 * If the override option is set, the pointer points to a
4674 * *non* const thus freeing it even though the variable type is
4675 * set to const.
4676 */
4677 if (tracing_group_name_override) {
4678 free((void *) tracing_group_name);
4679 }
4680 tracing_group_name = strdup(arg);
4681 if (!tracing_group_name) {
4682 PERROR("strdup");
4683 ret = -ENOMEM;
4684 }
4685 tracing_group_name_override = 1;
db322c4d 4686 }
c0232ea5 4687 } else if (string_match(optname, "help") || opt == 'h') {
8190767e
PP
4688 ret = utils_show_man_page(8, "lttng-sessiond");
4689 if (ret) {
4690 ERR("Cannot view man page lttng-sessiond(8)");
4691 perror("exec");
4692 }
4693 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
c0232ea5 4694 } else if (string_match(optname, "version") || opt == 'V') {
26296c48
JG
4695 fprintf(stdout, "%s\n", VERSION);
4696 exit(EXIT_SUCCESS);
c0232ea5 4697 } else if (string_match(optname, "sig-parent") || opt == 'S') {
26296c48 4698 opt_sig_parent = 1;
c0232ea5 4699 } else if (string_match(optname, "kconsumerd-err-sock")) {
66b2ce8e
JG
4700 if (!arg || *arg == '\0') {
4701 ret = -EINVAL;
4702 goto end;
4703 }
e8fa9fb0
MD
4704 if (lttng_is_setuid_setgid()) {
4705 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4706 "--kconsumerd-err-sock");
4707 } else {
4708 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4709 }
c0232ea5 4710 } else if (string_match(optname, "kconsumerd-cmd-sock")) {
66b2ce8e
JG
4711 if (!arg || *arg == '\0') {
4712 ret = -EINVAL;
4713 goto end;
4714 }
e8fa9fb0
MD
4715 if (lttng_is_setuid_setgid()) {
4716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4717 "--kconsumerd-cmd-sock");
4718 } else {
4719 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4720 }
c0232ea5 4721 } else if (string_match(optname, "ustconsumerd64-err-sock")) {
66b2ce8e
JG
4722 if (!arg || *arg == '\0') {
4723 ret = -EINVAL;
4724 goto end;
4725 }
e8fa9fb0
MD
4726 if (lttng_is_setuid_setgid()) {
4727 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4728 "--ustconsumerd64-err-sock");
4729 } else {
4730 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4731 }
c0232ea5 4732 } else if (string_match(optname, "ustconsumerd64-cmd-sock")) {
66b2ce8e
JG
4733 if (!arg || *arg == '\0') {
4734 ret = -EINVAL;
4735 goto end;
4736 }
e8fa9fb0
MD
4737 if (lttng_is_setuid_setgid()) {
4738 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4739 "--ustconsumerd64-cmd-sock");
4740 } else {
4741 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4742 }
c0232ea5 4743 } else if (string_match(optname, "ustconsumerd32-err-sock")) {
66b2ce8e
JG
4744 if (!arg || *arg == '\0') {
4745 ret = -EINVAL;
4746 goto end;
4747 }
e8fa9fb0
MD
4748 if (lttng_is_setuid_setgid()) {
4749 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4750 "--ustconsumerd32-err-sock");
4751 } else {
4752 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4753 }
c0232ea5 4754 } else if (string_match(optname, "ustconsumerd32-cmd-sock")) {
66b2ce8e
JG
4755 if (!arg || *arg == '\0') {
4756 ret = -EINVAL;
4757 goto end;
4758 }
e8fa9fb0
MD
4759 if (lttng_is_setuid_setgid()) {
4760 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4761 "--ustconsumerd32-cmd-sock");
4762 } else {
4763 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4764 }
c0232ea5 4765 } else if (string_match(optname, "no-kernel")) {
26296c48 4766 opt_no_kernel = 1;
c0232ea5 4767 } else if (string_match(optname, "quiet") || opt == 'q') {
26296c48 4768 lttng_opt_quiet = 1;
c0232ea5 4769 } else if (string_match(optname, "verbose") || opt == 'v') {
26296c48
JG
4770 /* Verbose level can increase using multiple -v */
4771 if (arg) {
13755a18 4772 /* Value obtained from config file */
26296c48
JG
4773 lttng_opt_verbose = config_parse_value(arg);
4774 } else {
13755a18
JG
4775 /* -v used on command line */
4776 lttng_opt_verbose++;
26296c48 4777 }
13755a18
JG
4778 /* Clamp value to [0, 3] */
4779 lttng_opt_verbose = lttng_opt_verbose < 0 ? 0 :
4780 (lttng_opt_verbose <= 3 ? lttng_opt_verbose : 3);
c0232ea5 4781 } else if (string_match(optname, "verbose-consumer")) {
26296c48
JG
4782 if (arg) {
4783 opt_verbose_consumer = config_parse_value(arg);
4784 } else {
ca939de8 4785 opt_verbose_consumer++;
26296c48 4786 }
c0232ea5 4787 } else if (string_match(optname, "consumerd32-path")) {
66b2ce8e
JG
4788 if (!arg || *arg == '\0') {
4789 ret = -EINVAL;
4790 goto end;
4791 }
e8fa9fb0
MD
4792 if (lttng_is_setuid_setgid()) {
4793 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4794 "--consumerd32-path");
4795 } else {
4796 if (consumerd32_bin_override) {
4797 free((void *) consumerd32_bin);
4798 }
4799 consumerd32_bin = strdup(arg);
4800 if (!consumerd32_bin) {
4801 PERROR("strdup");
4802 ret = -ENOMEM;
4803 }
4804 consumerd32_bin_override = 1;
db322c4d 4805 }
c0232ea5 4806 } else if (string_match(optname, "consumerd32-libdir")) {
66b2ce8e
JG
4807 if (!arg || *arg == '\0') {
4808 ret = -EINVAL;
4809 goto end;
4810 }
e8fa9fb0
MD
4811 if (lttng_is_setuid_setgid()) {
4812 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4813 "--consumerd32-libdir");
4814 } else {
4815 if (consumerd32_libdir_override) {
4816 free((void *) consumerd32_libdir);
4817 }
4818 consumerd32_libdir = strdup(arg);
4819 if (!consumerd32_libdir) {
4820 PERROR("strdup");
4821 ret = -ENOMEM;
4822 }
4823 consumerd32_libdir_override = 1;
db322c4d 4824 }
c0232ea5 4825 } else if (string_match(optname, "consumerd64-path")) {
66b2ce8e
JG
4826 if (!arg || *arg == '\0') {
4827 ret = -EINVAL;
4828 goto end;
4829 }
e8fa9fb0
MD
4830 if (lttng_is_setuid_setgid()) {
4831 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4832 "--consumerd64-path");
4833 } else {
4834 if (consumerd64_bin_override) {
4835 free((void *) consumerd64_bin);
4836 }
4837 consumerd64_bin = strdup(arg);
4838 if (!consumerd64_bin) {
4839 PERROR("strdup");
4840 ret = -ENOMEM;
4841 }
4842 consumerd64_bin_override = 1;
db322c4d 4843 }
c0232ea5 4844 } else if (string_match(optname, "consumerd64-libdir")) {
66b2ce8e
JG
4845 if (!arg || *arg == '\0') {
4846 ret = -EINVAL;
4847 goto end;
4848 }
e8fa9fb0
MD
4849 if (lttng_is_setuid_setgid()) {
4850 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4851 "--consumerd64-libdir");
4852 } else {
4853 if (consumerd64_libdir_override) {
4854 free((void *) consumerd64_libdir);
4855 }
4856 consumerd64_libdir = strdup(arg);
4857 if (!consumerd64_libdir) {
4858 PERROR("strdup");
4859 ret = -ENOMEM;
4860 }
4861 consumerd64_libdir_override = 1;
db322c4d 4862 }
c0232ea5 4863 } else if (string_match(optname, "pidfile") || opt == 'p') {
66b2ce8e
JG
4864 if (!arg || *arg == '\0') {
4865 ret = -EINVAL;
4866 goto end;
4867 }
e8fa9fb0
MD
4868 if (lttng_is_setuid_setgid()) {
4869 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4870 "-p, --pidfile");
4871 } else {
4872 free(opt_pidfile);
4873 opt_pidfile = strdup(arg);
4874 if (!opt_pidfile) {
4875 PERROR("strdup");
4876 ret = -ENOMEM;
4877 }
db322c4d 4878 }
c0232ea5 4879 } else if (string_match(optname, "agent-tcp-port")) {
66b2ce8e
JG
4880 if (!arg || *arg == '\0') {
4881 ret = -EINVAL;
4882 goto end;
4883 }
e8fa9fb0
MD
4884 if (lttng_is_setuid_setgid()) {
4885 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4886 "--agent-tcp-port");
4887 } else {
4888 unsigned long v;
4d076222 4889
e8fa9fb0
MD
4890 errno = 0;
4891 v = strtoul(arg, NULL, 0);
4892 if (errno != 0 || !isdigit(arg[0])) {
4893 ERR("Wrong value in --agent-tcp-port parameter: %s", arg);
4894 return -1;
4895 }
4896 if (v == 0 || v >= 65535) {
4897 ERR("Port overflow in --agent-tcp-port parameter: %s", arg);
4898 return -1;
4899 }
4900 agent_tcp_port = (uint32_t) v;
4901 DBG3("Agent TCP port set to non default: %u", agent_tcp_port);
26296c48 4902 }
c0232ea5 4903 } else if (string_match(optname, "load") || opt == 'l') {
66b2ce8e
JG
4904 if (!arg || *arg == '\0') {
4905 ret = -EINVAL;
4906 goto end;
4907 }
e8fa9fb0
MD
4908 if (lttng_is_setuid_setgid()) {
4909 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4910 "-l, --load");
4911 } else {
4912 free(opt_load_session_path);
4913 opt_load_session_path = strdup(arg);
4914 if (!opt_load_session_path) {
4915 PERROR("strdup");
4916 ret = -ENOMEM;
4917 }
ef367a93 4918 }
c0232ea5 4919 } else if (string_match(optname, "kmod-probes")) {
66b2ce8e
JG
4920 if (!arg || *arg == '\0') {
4921 ret = -EINVAL;
4922 goto end;
4923 }
e8fa9fb0
MD
4924 if (lttng_is_setuid_setgid()) {
4925 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4926 "--kmod-probes");
4927 } else {
4928 free(kmod_probes_list);
4929 kmod_probes_list = strdup(arg);
4930 if (!kmod_probes_list) {
4931 PERROR("strdup");
4932 ret = -ENOMEM;
4933 }
c9d42407 4934 }
c0232ea5 4935 } else if (string_match(optname, "extra-kmod-probes")) {
66b2ce8e
JG
4936 if (!arg || *arg == '\0') {
4937 ret = -EINVAL;
4938 goto end;
4939 }
e8fa9fb0
MD
4940 if (lttng_is_setuid_setgid()) {
4941 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4942 "--extra-kmod-probes");
4943 } else {
4944 free(kmod_extra_probes_list);
4945 kmod_extra_probes_list = strdup(arg);
4946 if (!kmod_extra_probes_list) {
4947 PERROR("strdup");
4948 ret = -ENOMEM;
4949 }
db322c4d 4950 }
c0232ea5
JG
4951 } else if (string_match(optname, "config") || opt == 'f') {
4952 /* This is handled in set_options() thus silent skip. */
4953 goto end;
4954 } else {
26296c48
JG
4955 /* Unknown option or other error.
4956 * Error is printed by getopt, just return */
4957 ret = -1;
4958 }
4959
f3dd7ce7 4960end:
c5d350b2
JG
4961 if (ret == -EINVAL) {
4962 const char *opt_name = "unknown";
4963 int i;
4964
4965 for (i = 0; i < sizeof(long_options) / sizeof(struct option);
4966 i++) {
4967 if (opt == long_options[i].val) {
4968 opt_name = long_options[i].name;
4969 break;
4970 }
4971 }
4972
4973 WARN("Invalid argument provided for option \"%s\", using default value.",
4974 opt_name);
4975 }
f3dd7ce7 4976
26296c48
JG
4977 return ret;
4978}
4979
4980/*
4981 * config_entry_handler_cb used to handle options read from a config file.
f40ef1d5 4982 * See config_entry_handler_cb comment in common/config/session-config.h for the
26296c48
JG
4983 * return value conventions.
4984 */
4985static int config_entry_handler(const struct config_entry *entry, void *unused)
4986{
4987 int ret = 0, i;
4988
4989 if (!entry || !entry->name || !entry->value) {
4990 ret = -EINVAL;
4991 goto end;
4992 }
4993
4994 /* Check if the option is to be ignored */
4995 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4996 if (!strcmp(entry->name, config_ignore_options[i])) {
4997 goto end;
4998 }
4999 }
5000
5001 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
5002 i++) {
5003
5004 /* Ignore if not fully matched. */
5005 if (strcmp(entry->name, long_options[i].name)) {
5006 continue;
5007 }
5008
5009 /*
5010 * If the option takes no argument on the command line, we have to
5011 * check if the value is "true". We support non-zero numeric values,
5012 * true, on and yes.
5013 */
5014 if (!long_options[i].has_arg) {
5015 ret = config_parse_value(entry->value);
5016 if (ret <= 0) {
5017 if (ret) {
5018 WARN("Invalid configuration value \"%s\" for option %s",
5019 entry->value, entry->name);
5020 }
5021 /* False, skip boolean config option. */
5022 goto end;
4d076222 5023 }
26296c48
JG
5024 }
5025
5026 ret = set_option(long_options[i].val, entry->value, entry->name);
5027 goto end;
5028 }
5029
5030 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
5031
5032end:
5033 return ret;
5034}
5035
5036/*
5037 * daemon configuration loading and argument parsing
5038 */
5039static int set_options(int argc, char **argv)
5040{
5041 int ret = 0, c = 0, option_index = 0;
5042 int orig_optopt = optopt, orig_optind = optind;
5043 char *optstring;
5044 const char *config_path = NULL;
5045
5046 optstring = utils_generate_optstring(long_options,
5047 sizeof(long_options) / sizeof(struct option));
5048 if (!optstring) {
5049 ret = -ENOMEM;
5050 goto end;
5051 }
5052
5053 /* Check for the --config option */
5054 while ((c = getopt_long(argc, argv, optstring, long_options,
5055 &option_index)) != -1) {
5056 if (c == '?') {
5057 ret = -EINVAL;
5058 goto end;
5059 } else if (c != 'f') {
5060 /* if not equal to --config option. */
5061 continue;
5062 }
5063
e8fa9fb0
MD
5064 if (lttng_is_setuid_setgid()) {
5065 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5066 "-f, --config");
5067 } else {
5068 config_path = utils_expand_path(optarg);
5069 if (!config_path) {
5070 ERR("Failed to resolve path: %s", optarg);
5071 }
26296c48
JG
5072 }
5073 }
5074
5075 ret = config_get_section_entries(config_path, config_section_name,
5076 config_entry_handler, NULL);
5077 if (ret) {
5078 if (ret > 0) {
5079 ERR("Invalid configuration option at line %i", ret);
5080 ret = -1;
5081 }
5082 goto end;
5083 }
5084
5085 /* Reset getopt's global state */
5086 optopt = orig_optopt;
5087 optind = orig_optind;
5088 while (1) {
c0232ea5
JG
5089 option_index = -1;
5090 /*
5091 * getopt_long() will not set option_index if it encounters a
5092 * short option.
5093 */
5094 c = getopt_long(argc, argv, optstring, long_options,
5095 &option_index);
26296c48 5096 if (c == -1) {
4d076222
DG
5097 break;
5098 }
26296c48 5099
c0232ea5
JG
5100 /*
5101 * Pass NULL as the long option name if popt left the index
5102 * unset.
5103 */
5104 ret = set_option(c, optarg,
5105 option_index < 0 ? NULL :
5106 long_options[option_index].name);
26296c48
JG
5107 if (ret < 0) {
5108 break;
fac6795d
DG
5109 }
5110 }
5111
26296c48
JG
5112end:
5113 free(optstring);
5114 return ret;
fac6795d
DG
5115}
5116
5117/*
d063d709 5118 * Creates the two needed socket by the daemon.
d6f42150
DG
5119 * apps_sock - The communication socket for all UST apps.
5120 * client_sock - The communication of the cli tool (lttng).
fac6795d 5121 */
cf3af59e 5122static int init_daemon_socket(void)
fac6795d
DG
5123{
5124 int ret = 0;
5125 mode_t old_umask;
5126
5127 old_umask = umask(0);
5128
5129 /* Create client tool unix socket */
d6f42150
DG
5130 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5131 if (client_sock < 0) {
5132 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
5133 ret = -1;
5134 goto end;
5135 }
5136
b662582b
DG
5137 /* Set the cloexec flag */
5138 ret = utils_set_fd_cloexec(client_sock);
5139 if (ret < 0) {
5140 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5141 "Continuing but note that the consumer daemon will have a "
5142 "reference to this socket on exec()", client_sock);
5143 }
5144
fac6795d
DG
5145 /* File permission MUST be 660 */
5146 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5147 if (ret < 0) {
d6f42150 5148 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 5149 PERROR("chmod");
fac6795d
DG
5150 goto end;
5151 }
5152
5153 /* Create the application unix socket */
d6f42150
DG
5154 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5155 if (apps_sock < 0) {
5156 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
5157 ret = -1;
5158 goto end;
5159 }
5160
b662582b
DG
5161 /* Set the cloexec flag */
5162 ret = utils_set_fd_cloexec(apps_sock);
5163 if (ret < 0) {
5164 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5165 "Continuing but note that the consumer daemon will have a "
5166 "reference to this socket on exec()", apps_sock);
5167 }
5168
d6f42150 5169 /* File permission MUST be 666 */
54d01ffb
DG
5170 ret = chmod(apps_unix_sock_path,
5171 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 5172 if (ret < 0) {
d6f42150 5173 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 5174 PERROR("chmod");
fac6795d
DG
5175 goto end;
5176 }
5177
b662582b
DG
5178 DBG3("Session daemon client socket %d and application socket %d created",
5179 client_sock, apps_sock);
5180
fac6795d
DG
5181end:
5182 umask(old_umask);
5183 return ret;
5184}
5185
5186/*
54d01ffb
DG
5187 * Check if the global socket is available, and if a daemon is answering at the
5188 * other side. If yes, error is returned.
fac6795d 5189 */
cf3af59e 5190static int check_existing_daemon(void)
fac6795d 5191{
7d8234d9 5192 /* Is there anybody out there ? */
099e26bd 5193 if (lttng_session_daemon_alive()) {
7d8234d9 5194 return -EEXIST;
099e26bd 5195 }
b09c7c76
DG
5196
5197 return 0;
fac6795d
DG
5198}
5199
fac6795d 5200/*
d063d709 5201 * Set the tracing group gid onto the client socket.
5e16da05 5202 *
d063d709 5203 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 5204 * permissive (root.root) to less permissive (root.tracing).
fac6795d 5205 */
be040666 5206static int set_permissions(char *rundir)
fac6795d
DG
5207{
5208 int ret;
996b65c8 5209 gid_t gid;
fac6795d 5210
6c71277b 5211 gid = utils_get_group_id(tracing_group_name);
fac6795d 5212
d6f42150 5213 /* Set lttng run dir */
be040666 5214 ret = chown(rundir, 0, gid);
d6f42150 5215 if (ret < 0) {
be040666 5216 ERR("Unable to set group on %s", rundir);
76d7553f 5217 PERROR("chown");
d6f42150
DG
5218 }
5219
6c71277b
MD
5220 /*
5221 * Ensure all applications and tracing group can search the run
5222 * dir. Allow everyone to read the directory, since it does not
5223 * buy us anything to hide its content.
5224 */
5225 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
d1613cf5
JN
5226 if (ret < 0) {
5227 ERR("Unable to set permissions on %s", rundir);
76d7553f 5228 PERROR("chmod");
d1613cf5
JN
5229 }
5230
d6f42150 5231 /* lttng client socket path */
996b65c8 5232 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 5233 if (ret < 0) {
d6f42150 5234 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 5235 PERROR("chown");
d6f42150
DG
5236 }
5237
3bd1e081 5238 /* kconsumer error socket path */
6c71277b 5239 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
d6f42150 5240 if (ret < 0) {
3bd1e081 5241 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 5242 PERROR("chown");
3bd1e081
MD
5243 }
5244
7753dea8 5245 /* 64-bit ustconsumer error socket path */
6c71277b 5246 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
7753dea8
MD
5247 if (ret < 0) {
5248 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 5249 PERROR("chown");
7753dea8
MD
5250 }
5251
5252 /* 32-bit ustconsumer compat32 error socket path */
6c71277b 5253 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
3bd1e081 5254 if (ret < 0) {
7753dea8 5255 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5256 PERROR("chown");
fac6795d
DG
5257 }
5258
d6f42150 5259 DBG("All permissions are set");
e07ae692 5260
fac6795d
DG
5261 return ret;
5262}
5263
d6f42150 5264/*
d063d709 5265 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5266 */
67e40797 5267static int create_lttng_rundir(const char *rundir)
d6f42150
DG
5268{
5269 int ret;
5270
67e40797
DG
5271 DBG3("Creating LTTng run directory: %s", rundir);
5272
d1613cf5 5273 ret = mkdir(rundir, S_IRWXU);
d6f42150 5274 if (ret < 0) {
b1f11e69 5275 if (errno != EEXIST) {
67e40797 5276 ERR("Unable to create %s", rundir);
b1f11e69
DG
5277 goto error;
5278 } else {
5279 ret = 0;
5280 }
d6f42150
DG
5281 }
5282
5283error:
5284 return ret;
5285}
5286
5287/*
d063d709
DG
5288 * Setup sockets and directory needed by the kconsumerd communication with the
5289 * session daemon.
d6f42150 5290 */
67e40797
DG
5291static int set_consumer_sockets(struct consumer_data *consumer_data,
5292 const char *rundir)
d6f42150
DG
5293{
5294 int ret;
67e40797 5295 char path[PATH_MAX];
d6f42150 5296
6c71277b 5297 switch (consumer_data->type) {
7753dea8 5298 case LTTNG_CONSUMER_KERNEL:
60922cb0 5299 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
5300 break;
5301 case LTTNG_CONSUMER64_UST:
60922cb0 5302 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
5303 break;
5304 case LTTNG_CONSUMER32_UST:
60922cb0 5305 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
5306 break;
5307 default:
5308 ERR("Consumer type unknown");
5309 ret = -EINVAL;
5310 goto error;
d6f42150
DG
5311 }
5312
67e40797
DG
5313 DBG2("Creating consumer directory: %s", path);
5314
6c71277b 5315 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
d6f42150 5316 if (ret < 0) {
6beb2242 5317 if (errno != EEXIST) {
f11e84c2 5318 PERROR("mkdir");
3bd1e081 5319 ERR("Failed to create %s", path);
6beb2242
DG
5320 goto error;
5321 }
f11e84c2 5322 ret = -1;
d6f42150 5323 }
6c71277b
MD
5324 if (is_root) {
5325 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
5326 if (ret < 0) {
5327 ERR("Unable to set group on %s", path);
5328 PERROR("chown");
5329 goto error;
5330 }
5331 }
d6f42150
DG
5332
5333 /* Create the kconsumerd error unix socket */
3bd1e081
MD
5334 consumer_data->err_sock =
5335 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5336 if (consumer_data->err_sock < 0) {
5337 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5338 ret = -1;
5339 goto error;
5340 }
5341
a24f05ab
MD
5342 /*
5343 * Set the CLOEXEC flag. Return code is useless because either way, the
5344 * show must go on.
5345 */
5346 ret = utils_set_fd_cloexec(consumer_data->err_sock);
5347 if (ret < 0) {
5348 PERROR("utils_set_fd_cloexec");
5349 /* continue anyway */
5350 }
5351
d6f42150 5352 /* File permission MUST be 660 */
3bd1e081 5353 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5354 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5355 if (ret < 0) {
3bd1e081 5356 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5357 PERROR("chmod");
d6f42150
DG
5358 goto error;
5359 }
5360
5361error:
5362 return ret;
5363}
5364
fac6795d 5365/*
d063d709 5366 * Signal handler for the daemon
cf3af59e 5367 *
54d01ffb
DG
5368 * Simply stop all worker threads, leaving main() return gracefully after
5369 * joining all threads and calling cleanup().
fac6795d
DG
5370 */
5371static void sighandler(int sig)
5372{
5373 switch (sig) {
cf3af59e 5374 case SIGPIPE:
af87c45a 5375 DBG("SIGPIPE caught");
cf3af59e
MD
5376 return;
5377 case SIGINT:
af87c45a 5378 DBG("SIGINT caught");
cf3af59e
MD
5379 stop_threads();
5380 break;
5381 case SIGTERM:
af87c45a 5382 DBG("SIGTERM caught");
cf3af59e
MD
5383 stop_threads();
5384 break;
0bb7724a
DG
5385 case SIGUSR1:
5386 CMM_STORE_SHARED(recv_child_signal, 1);
5387 break;
cf3af59e
MD
5388 default:
5389 break;
fac6795d 5390 }
fac6795d
DG
5391}
5392
5393/*
d063d709 5394 * Setup signal handler for :
1d4b027a 5395 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5396 */
1d4b027a 5397static int set_signal_handler(void)
fac6795d 5398{
1d4b027a
DG
5399 int ret = 0;
5400 struct sigaction sa;
5401 sigset_t sigset;
fac6795d 5402
1d4b027a 5403 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5404 PERROR("sigemptyset");
1d4b027a
DG
5405 return ret;
5406 }
d6f42150 5407
1d4b027a
DG
5408 sa.sa_handler = sighandler;
5409 sa.sa_mask = sigset;
5410 sa.sa_flags = 0;
5411 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5412 PERROR("sigaction");
1d4b027a 5413 return ret;
d6f42150
DG
5414 }
5415
1d4b027a 5416 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5417 PERROR("sigaction");
1d4b027a 5418 return ret;
d6f42150 5419 }
aaf26714 5420
1d4b027a 5421 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 5422 PERROR("sigaction");
1d4b027a 5423 return ret;
8c0faa1d
DG
5424 }
5425
0bb7724a
DG
5426 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
5427 PERROR("sigaction");
5428 return ret;
5429 }
5430
5431 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1d4b027a
DG
5432
5433 return ret;
fac6795d
DG
5434}
5435
f3ed775e 5436/*
d063d709 5437 * Set open files limit to unlimited. This daemon can open a large number of
514bb9f0 5438 * file descriptors in order to consume multiple kernel traces.
f3ed775e
DG
5439 */
5440static void set_ulimit(void)
5441{
5442 int ret;
5443 struct rlimit lim;
5444
514bb9f0 5445 /* The kernel does not allow an infinite limit for open files */
f3ed775e
DG
5446 lim.rlim_cur = 65535;
5447 lim.rlim_max = 65535;
5448
5449 ret = setrlimit(RLIMIT_NOFILE, &lim);
5450 if (ret < 0) {
76d7553f 5451 PERROR("failed to set open files limit");
f3ed775e
DG
5452 }
5453}
5454
35f90c40
DG
5455/*
5456 * Write pidfile using the rundir and opt_pidfile.
5457 */
4a15001e 5458static int write_pidfile(void)
35f90c40
DG
5459{
5460 int ret;
5461 char pidfile_path[PATH_MAX];
5462
5463 assert(rundir);
5464
5465 if (opt_pidfile) {
b8d76705
MD
5466 if (lttng_strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path))) {
5467 ret = -1;
5468 goto error;
5469 }
35f90c40
DG
5470 } else {
5471 /* Build pidfile path from rundir and opt_pidfile. */
5472 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
5473 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
5474 if (ret < 0) {
5475 PERROR("snprintf pidfile path");
5476 goto error;
5477 }
5478 }
5479
5480 /*
4a15001e 5481 * Create pid file in rundir.
35f90c40 5482 */
4a15001e 5483 ret = utils_create_pid_file(getpid(), pidfile_path);
35f90c40 5484error:
4a15001e 5485 return ret;
35f90c40
DG
5486}
5487
c9cb3e7d
JG
5488/*
5489 * Create lockfile using the rundir and return its fd.
5490 */
5491static int create_lockfile(void)
5492{
5493 int ret;
5494 char lockfile_path[PATH_MAX];
5495
5496 ret = generate_lock_file_path(lockfile_path, sizeof(lockfile_path));
5497 if (ret < 0) {
5498 goto error;
5499 }
5500
5501 ret = utils_create_lock_file(lockfile_path);
5502error:
5503 return ret;
5504}
5505
cd9290dd 5506/*
022d91ba 5507 * Write agent TCP port using the rundir.
cd9290dd 5508 */
4a15001e 5509static int write_agent_port(void)
cd9290dd
DG
5510{
5511 int ret;
5512 char path[PATH_MAX];
5513
5514 assert(rundir);
5515
5516 ret = snprintf(path, sizeof(path), "%s/"
022d91ba 5517 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE, rundir);
cd9290dd 5518 if (ret < 0) {
022d91ba 5519 PERROR("snprintf agent port path");
cd9290dd
DG
5520 goto error;
5521 }
5522
5523 /*
4a15001e 5524 * Create TCP agent port file in rundir.
cd9290dd 5525 */
4a15001e 5526 ret = utils_create_pid_file(agent_tcp_port, path);
cd9290dd
DG
5527
5528error:
4a15001e 5529 return ret;
ef367a93
JG
5530}
5531
fac6795d
DG
5532/*
5533 * main
5534 */
5535int main(int argc, char **argv)
5536{
4a15001e 5537 int ret = 0, retval = 0;
fac6795d 5538 void *status;
ae9e45b3 5539 const char *home_path, *env_app_timeout;
fac6795d 5540
335a95b7
MD
5541 init_kernel_workarounds();
5542
f6a9efaa
DG
5543 rcu_register_thread();
5544
4a15001e
MD
5545 if (set_signal_handler()) {
5546 retval = -1;
5547 goto exit_set_signal_handler;
0bb7724a
DG
5548 }
5549
7753dea8 5550 setup_consumerd_path();
fb09408a 5551
12744796
DG
5552 page_size = sysconf(_SC_PAGESIZE);
5553 if (page_size < 0) {
5554 PERROR("sysconf _SC_PAGESIZE");
5555 page_size = LONG_MAX;
5556 WARN("Fallback page size to %ld", page_size);
5557 }
5558
4a15001e
MD
5559 /*
5560 * Parse arguments and load the daemon configuration file.
5561 *
5562 * We have an exit_options exit path to free memory reserved by
5563 * set_options. This is needed because the rest of sessiond_cleanup()
5564 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5565 * depends on set_options.
5566 */
fac6795d 5567 progname = argv[0];
4a15001e
MD
5568 if (set_options(argc, argv)) {
5569 retval = -1;
5570 goto exit_options;
fac6795d
DG
5571 }
5572
5573 /* Daemonize */
72dd7491 5574 if (opt_daemon || opt_background) {
ceed52b5
MD
5575 int i;
5576
72dd7491
MD
5577 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
5578 !opt_background);
53094c05 5579 if (ret < 0) {
4a15001e
MD
5580 retval = -1;
5581 goto exit_options;
53094c05 5582 }
0bb7724a 5583
ceed52b5 5584 /*
0bb7724a 5585 * We are in the child. Make sure all other file descriptors are
4a15001e
MD
5586 * closed, in case we are called with more opened file
5587 * descriptors than the standard ones.
ceed52b5
MD
5588 */
5589 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5590 (void) close(i);
5591 }
5592 }
5593
7567352f
MD
5594 if (run_as_create_worker(argv[0]) < 0) {
5595 goto exit_create_run_as_worker_cleanup;
5596 }
5597
4a15001e
MD
5598 /*
5599 * Starting from here, we can create threads. This needs to be after
5600 * lttng_daemonize due to RCU.
5601 */
5602
5603 /*
5604 * Initialize the health check subsystem. This call should set the
5605 * appropriate time values.
5606 */
5607 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5608 if (!health_sessiond) {
5609 PERROR("health_app_create error");
5610 retval = -1;
5611 goto exit_health_sessiond_cleanup;
5612 }
5613
4a15001e 5614 /* Create thread to clean up RCU hash tables */
5e97de00 5615 if (init_ht_cleanup_thread(&ht_cleanup_thread)) {
4a15001e
MD
5616 retval = -1;
5617 goto exit_ht_cleanup;
5618 }
5619
ceed52b5 5620 /* Create thread quit pipe */
4a15001e
MD
5621 if (init_thread_quit_pipe()) {
5622 retval = -1;
5623 goto exit_init_data;
fac6795d
DG
5624 }
5625
5626 /* Check if daemon is UID = 0 */
5627 is_root = !getuid();
5628
fac6795d 5629 if (is_root) {
990570ed 5630 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4ea184d1 5631 if (!rundir) {
4a15001e
MD
5632 retval = -1;
5633 goto exit_init_data;
4ea184d1 5634 }
67e40797
DG
5635
5636 /* Create global run dir with root access */
4a15001e
MD
5637 if (create_lttng_rundir(rundir)) {
5638 retval = -1;
5639 goto exit_init_data;
d6f42150
DG
5640 }
5641
fac6795d 5642 if (strlen(apps_unix_sock_path) == 0) {
4a15001e 5643 ret = snprintf(apps_unix_sock_path, PATH_MAX,
d6f42150 5644 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4a15001e
MD
5645 if (ret < 0) {
5646 retval = -1;
5647 goto exit_init_data;
5648 }
fac6795d
DG
5649 }
5650
5651 if (strlen(client_unix_sock_path) == 0) {
4a15001e 5652 ret = snprintf(client_unix_sock_path, PATH_MAX,
d6f42150 5653 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4a15001e
MD
5654 if (ret < 0) {
5655 retval = -1;
5656 goto exit_init_data;
5657 }
d6f42150 5658 }
0fdd1e2c
DG
5659
5660 /* Set global SHM for ust */
5661 if (strlen(wait_shm_path) == 0) {
4a15001e 5662 ret = snprintf(wait_shm_path, PATH_MAX,
0fdd1e2c 5663 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4a15001e
MD
5664 if (ret < 0) {
5665 retval = -1;
5666 goto exit_init_data;
5667 }
0fdd1e2c 5668 }
67e40797 5669
44a5e5eb 5670 if (strlen(health_unix_sock_path) == 0) {
4a15001e
MD
5671 ret = snprintf(health_unix_sock_path,
5672 sizeof(health_unix_sock_path),
44a5e5eb 5673 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4a15001e
MD
5674 if (ret < 0) {
5675 retval = -1;
5676 goto exit_init_data;
5677 }
44a5e5eb
DG
5678 }
5679
67e40797 5680 /* Setup kernel consumerd path */
4a15001e 5681 ret = snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 5682 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4a15001e
MD
5683 if (ret < 0) {
5684 retval = -1;
5685 goto exit_init_data;
5686 }
5687 ret = snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5688 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4a15001e
MD
5689 if (ret < 0) {
5690 retval = -1;
5691 goto exit_init_data;
5692 }
67e40797
DG
5693
5694 DBG2("Kernel consumer err path: %s",
5695 kconsumer_data.err_unix_sock_path);
5696 DBG2("Kernel consumer cmd path: %s",
5697 kconsumer_data.cmd_unix_sock_path);
fac6795d 5698 } else {
feb0f3e5 5699 home_path = utils_get_home_dir();
b082db07 5700 if (home_path == NULL) {
273ea72c
DG
5701 /* TODO: Add --socket PATH option */
5702 ERR("Can't get HOME directory for sockets creation.");
4a15001e
MD
5703 retval = -1;
5704 goto exit_init_data;
b082db07
DG
5705 }
5706
67e40797
DG
5707 /*
5708 * Create rundir from home path. This will create something like
5709 * $HOME/.lttng
5710 */
990570ed 5711 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797 5712 if (ret < 0) {
4a15001e
MD
5713 retval = -1;
5714 goto exit_init_data;
67e40797
DG
5715 }
5716
4a15001e
MD
5717 if (create_lttng_rundir(rundir)) {
5718 retval = -1;
5719 goto exit_init_data;
67e40797
DG
5720 }
5721
fac6795d 5722 if (strlen(apps_unix_sock_path) == 0) {
4a15001e
MD
5723 ret = snprintf(apps_unix_sock_path, PATH_MAX,
5724 DEFAULT_HOME_APPS_UNIX_SOCK,
5725 home_path);
5726 if (ret < 0) {
5727 retval = -1;
5728 goto exit_init_data;
5729 }
fac6795d
DG
5730 }
5731
5732 /* Set the cli tool unix socket path */
5733 if (strlen(client_unix_sock_path) == 0) {
4a15001e
MD
5734 ret = snprintf(client_unix_sock_path, PATH_MAX,
5735 DEFAULT_HOME_CLIENT_UNIX_SOCK,
5736 home_path);
5737 if (ret < 0) {
5738 retval = -1;
5739 goto exit_init_data;
5740 }
fac6795d 5741 }
0fdd1e2c
DG
5742
5743 /* Set global SHM for ust */
5744 if (strlen(wait_shm_path) == 0) {
4a15001e
MD
5745 ret = snprintf(wait_shm_path, PATH_MAX,
5746 DEFAULT_HOME_APPS_WAIT_SHM_PATH,
5747 getuid());
5748 if (ret < 0) {
5749 retval = -1;
5750 goto exit_init_data;
5751 }
0fdd1e2c 5752 }
44a5e5eb
DG
5753
5754 /* Set health check Unix path */
5755 if (strlen(health_unix_sock_path) == 0) {
4a15001e
MD
5756 ret = snprintf(health_unix_sock_path,
5757 sizeof(health_unix_sock_path),
5758 DEFAULT_HOME_HEALTH_UNIX_SOCK,
5759 home_path);
5760 if (ret < 0) {
5761 retval = -1;
5762 goto exit_init_data;
5763 }
44a5e5eb 5764 }
fac6795d
DG
5765 }
5766
c9cb3e7d
JG
5767 lockfile_fd = create_lockfile();
5768 if (lockfile_fd < 0) {
4a15001e
MD
5769 retval = -1;
5770 goto exit_init_data;
c9cb3e7d
JG
5771 }
5772
5c827ce0
DG
5773 /* Set consumer initial state */
5774 kernel_consumerd_state = CONSUMER_STOPPED;
5775 ust_consumerd_state = CONSUMER_STOPPED;
5776
847177cd
DG
5777 DBG("Client socket path %s", client_unix_sock_path);
5778 DBG("Application socket path %s", apps_unix_sock_path);
d0b96690 5779 DBG("Application wait path %s", wait_shm_path);
67e40797
DG
5780 DBG("LTTng run directory path: %s", rundir);
5781
5782 /* 32 bits consumerd path setup */
4a15001e 5783 ret = snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 5784 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4a15001e
MD
5785 if (ret < 0) {
5786 PERROR("snprintf 32-bit consumer error socket path");
5787 retval = -1;
5788 goto exit_init_data;
5789 }
5790 ret = snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5791 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4a15001e
MD
5792 if (ret < 0) {
5793 PERROR("snprintf 32-bit consumer command socket path");
5794 retval = -1;
5795 goto exit_init_data;
5796 }
67e40797
DG
5797
5798 DBG2("UST consumer 32 bits err path: %s",
5799 ustconsumer32_data.err_unix_sock_path);
5800 DBG2("UST consumer 32 bits cmd path: %s",
5801 ustconsumer32_data.cmd_unix_sock_path);
5802
5803 /* 64 bits consumerd path setup */
4a15001e 5804 ret = snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 5805 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4a15001e
MD
5806 if (ret < 0) {
5807 PERROR("snprintf 64-bit consumer error socket path");
5808 retval = -1;
5809 goto exit_init_data;
5810 }
5811 ret = snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5812 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4a15001e
MD
5813 if (ret < 0) {
5814 PERROR("snprintf 64-bit consumer command socket path");
5815 retval = -1;
5816 goto exit_init_data;
5817 }
67e40797
DG
5818
5819 DBG2("UST consumer 64 bits err path: %s",
5820 ustconsumer64_data.err_unix_sock_path);
5821 DBG2("UST consumer 64 bits cmd path: %s",
5822 ustconsumer64_data.cmd_unix_sock_path);
847177cd 5823
273ea72c 5824 /*
7d8234d9 5825 * See if daemon already exist.
fac6795d 5826 */
4a15001e 5827 if (check_existing_daemon()) {
75462a81 5828 ERR("Already running daemon.\n");
273ea72c 5829 /*
cf3af59e
MD
5830 * We do not goto exit because we must not cleanup()
5831 * because a daemon is already running.
ab118b20 5832 */
4a15001e
MD
5833 retval = -1;
5834 goto exit_init_data;
a88df331
DG
5835 }
5836
1427f9b2
DG
5837 /*
5838 * Init UST app hash table. Alloc hash table before this point since
5839 * cleanup() can get called after that point.
5840 */
4a15001e 5841 if (ust_app_ht_alloc()) {
ddbeb0f6 5842 ERR("Failed to allocate UST app hash table");
4a15001e
MD
5843 retval = -1;
5844 goto exit_init_data;
5845 }
1427f9b2 5846
6a4e4039
JG
5847 /*
5848 * Initialize agent app hash table. We allocate the hash table here
5849 * since cleanup() can get called after this point.
5850 */
5851 if (agent_app_ht_alloc()) {
5852 ERR("Failed to allocate Agent app hash table");
4a15001e
MD
5853 retval = -1;
5854 goto exit_init_data;
f20baf8e
DG
5855 }
5856
a88df331
DG
5857 /*
5858 * These actions must be executed as root. We do that *after* setting up
5859 * the sockets path because we MUST make the check for another daemon using
5860 * those paths *before* trying to set the kernel consumer sockets and init
5861 * kernel tracer.
5862 */
5863 if (is_root) {
4a15001e
MD
5864 if (set_consumer_sockets(&kconsumer_data, rundir)) {
5865 retval = -1;
5866 goto exit_init_data;
7753dea8
MD
5867 }
5868
a88df331 5869 /* Setup kernel tracer */
4fba7219
DG
5870 if (!opt_no_kernel) {
5871 init_kernel_tracer();
89588270
DG
5872 if (kernel_tracer_fd >= 0) {
5873 ret = syscall_init_table();
5874 if (ret < 0) {
4a15001e
MD
5875 ERR("Unable to populate syscall table. "
5876 "Syscall tracing won't work "
5877 "for this session daemon.");
89588270 5878 }
834978fd 5879 }
4fba7219 5880 }
a88df331
DG
5881
5882 /* Set ulimit for open files */
5883 set_ulimit();
fac6795d 5884 }
4063050c
MD
5885 /* init lttng_fd tracking must be done after set_ulimit. */
5886 lttng_fd_init();
fac6795d 5887
4a15001e
MD
5888 if (set_consumer_sockets(&ustconsumer64_data, rundir)) {
5889 retval = -1;
5890 goto exit_init_data;
67e40797
DG
5891 }
5892
4a15001e
MD
5893 if (set_consumer_sockets(&ustconsumer32_data, rundir)) {
5894 retval = -1;
5895 goto exit_init_data;
67e40797
DG
5896 }
5897
d6f42150 5898 /* Setup the needed unix socket */
4a15001e
MD
5899 if (init_daemon_socket()) {
5900 retval = -1;
5901 goto exit_init_data;
fac6795d
DG
5902 }
5903
5904 /* Set credentials to socket */
4a15001e
MD
5905 if (is_root && set_permissions(rundir)) {
5906 retval = -1;
5907 goto exit_init_data;
fac6795d
DG
5908 }
5909
5b8719f5
DG
5910 /* Get parent pid if -S, --sig-parent is specified. */
5911 if (opt_sig_parent) {
5912 ppid = getppid();
5913 }
5914
7a485870 5915 /* Setup the kernel pipe for waking up the kernel thread */
6620da75 5916 if (is_root && !opt_no_kernel) {
4a15001e
MD
5917 if (utils_create_pipe_cloexec(kernel_poll_pipe)) {
5918 retval = -1;
5919 goto exit_init_data;
6620da75 5920 }
7a485870
DG
5921 }
5922
099e26bd 5923 /* Setup the thread apps communication pipe. */
4a15001e
MD
5924 if (utils_create_pipe_cloexec(apps_cmd_pipe)) {
5925 retval = -1;
5926 goto exit_init_data;
099e26bd
DG
5927 }
5928
d0b96690 5929 /* Setup the thread apps notify communication pipe. */
4a15001e
MD
5930 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe)) {
5931 retval = -1;
5932 goto exit_init_data;
d0b96690
DG
5933 }
5934
7972aab2
DG
5935 /* Initialize global buffer per UID and PID registry. */
5936 buffer_reg_init_uid_registry();
5937 buffer_reg_init_pid_registry();
5938
099e26bd 5939 /* Init UST command queue. */
8bdee6e2 5940 cds_wfcq_init(&ust_cmd_queue.head, &ust_cmd_queue.tail);
099e26bd 5941
273ea72c 5942 /*
4a15001e
MD
5943 * Get session list pointer. This pointer MUST NOT be free'd. This list
5944 * is statically declared in session.c
273ea72c 5945 */
54d01ffb 5946 session_list_ptr = session_get_list();
b5541356 5947
2f77fc4b 5948 cmd_init();
00e2e675 5949
ae9e45b3
DG
5950 /* Check for the application socket timeout env variable. */
5951 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5952 if (env_app_timeout) {
5953 app_socket_timeout = atoi(env_app_timeout);
5954 } else {
5955 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5956 }
5957
4a15001e
MD
5958 ret = write_pidfile();
5959 if (ret) {
5960 ERR("Error in write_pidfile");
5961 retval = -1;
5962 goto exit_init_data;
5963 }
5964 ret = write_agent_port();
5965 if (ret) {
5966 ERR("Error in write_agent_port");
5967 retval = -1;
5968 goto exit_init_data;
5969 }
35f90c40 5970
554831e7
MD
5971 /* Initialize communication library */
5972 lttcomm_init();
4a15001e 5973 /* Initialize TCP timeout values */
d831c249 5974 lttcomm_inet_init();
554831e7 5975
ef367a93 5976 if (load_session_init_data(&load_info) < 0) {
4a15001e
MD
5977 retval = -1;
5978 goto exit_init_data;
ef367a93
JG
5979 }
5980 load_info->path = opt_load_session_path;
5981
d5b9fd2f 5982 /* Create health-check thread */
44a5e5eb
DG
5983 ret = pthread_create(&health_thread, NULL,
5984 thread_manage_health, (void *) NULL);
4a15001e
MD
5985 if (ret) {
5986 errno = ret;
44a5e5eb 5987 PERROR("pthread_create health");
4a15001e 5988 retval = -1;
44a5e5eb
DG
5989 goto exit_health;
5990 }
5991
cf3af59e 5992 /* Create thread to manage the client socket */
099e26bd
DG
5993 ret = pthread_create(&client_thread, NULL,
5994 thread_manage_clients, (void *) NULL);
4a15001e
MD
5995 if (ret) {
5996 errno = ret;
76d7553f 5997 PERROR("pthread_create clients");
4a15001e 5998 retval = -1;
cf3af59e
MD
5999 goto exit_client;
6000 }
fac6795d 6001
099e26bd
DG
6002 /* Create thread to dispatch registration */
6003 ret = pthread_create(&dispatch_thread, NULL,
6004 thread_dispatch_ust_registration, (void *) NULL);
4a15001e
MD
6005 if (ret) {
6006 errno = ret;
76d7553f 6007 PERROR("pthread_create dispatch");
4a15001e 6008 retval = -1;
099e26bd
DG
6009 goto exit_dispatch;
6010 }
6011
6012 /* Create thread to manage application registration. */
6013 ret = pthread_create(&reg_apps_thread, NULL,
6014 thread_registration_apps, (void *) NULL);
4a15001e
MD
6015 if (ret) {
6016 errno = ret;
76d7553f 6017 PERROR("pthread_create registration");
4a15001e 6018 retval = -1;
099e26bd
DG
6019 goto exit_reg_apps;
6020 }
6021
cf3af59e 6022 /* Create thread to manage application socket */
54d01ffb
DG
6023 ret = pthread_create(&apps_thread, NULL,
6024 thread_manage_apps, (void *) NULL);
4a15001e
MD
6025 if (ret) {
6026 errno = ret;
d0b96690 6027 PERROR("pthread_create apps");
4a15001e 6028 retval = -1;
d0b96690
DG
6029 goto exit_apps;
6030 }
6031
6032 /* Create thread to manage application notify socket */
6033 ret = pthread_create(&apps_notify_thread, NULL,
6034 ust_thread_manage_notify, (void *) NULL);
4a15001e
MD
6035 if (ret) {
6036 errno = ret;
f5c32ef1 6037 PERROR("pthread_create notify");
4a15001e 6038 retval = -1;
9563b0ad 6039 goto exit_apps_notify;
cf3af59e 6040 }
fac6795d 6041
022d91ba
DG
6042 /* Create agent registration thread. */
6043 ret = pthread_create(&agent_reg_thread, NULL,
6044 agent_thread_manage_registration, (void *) NULL);
4a15001e
MD
6045 if (ret) {
6046 errno = ret;
022d91ba 6047 PERROR("pthread_create agent");
4a15001e 6048 retval = -1;
022d91ba 6049 goto exit_agent_reg;
4d076222
DG
6050 }
6051
6620da75
DG
6052 /* Don't start this thread if kernel tracing is not requested nor root */
6053 if (is_root && !opt_no_kernel) {
6054 /* Create kernel thread to manage kernel event */
6055 ret = pthread_create(&kernel_thread, NULL,
6056 thread_manage_kernel, (void *) NULL);
4a15001e
MD
6057 if (ret) {
6058 errno = ret;
6620da75 6059 PERROR("pthread_create kernel");
4a15001e 6060 retval = -1;
6620da75
DG
6061 goto exit_kernel;
6062 }
ef367a93 6063 }
7a485870 6064
4a15001e
MD
6065 /* Create session loading thread. */
6066 ret = pthread_create(&load_session_thread, NULL, thread_load_session,
6067 load_info);
6068 if (ret) {
6069 errno = ret;
6070 PERROR("pthread_create load_session_thread");
6071 retval = -1;
6072 goto exit_load_session;
6073 }
6074
6075 /*
6076 * This is where we start awaiting program completion (e.g. through
6077 * signal that asks threads to teardown).
6078 */
6079
6080 ret = pthread_join(load_session_thread, &status);
6081 if (ret) {
6082 errno = ret;
6083 PERROR("pthread_join load_session_thread");
6084 retval = -1;
6085 }
6086exit_load_session:
ef367a93
JG
6087
6088 if (is_root && !opt_no_kernel) {
6620da75 6089 ret = pthread_join(kernel_thread, &status);
4a15001e
MD
6090 if (ret) {
6091 errno = ret;
6620da75 6092 PERROR("pthread_join");
4a15001e 6093 retval = -1;
6620da75 6094 }
fac6795d 6095 }
cf3af59e 6096exit_kernel:
4a15001e 6097
022d91ba 6098 ret = pthread_join(agent_reg_thread, &status);
4a15001e
MD
6099 if (ret) {
6100 errno = ret;
022d91ba 6101 PERROR("pthread_join agent");
4a15001e 6102 retval = -1;
4d076222 6103 }
022d91ba 6104exit_agent_reg:
4a15001e 6105
9563b0ad 6106 ret = pthread_join(apps_notify_thread, &status);
4a15001e
MD
6107 if (ret) {
6108 errno = ret;
9563b0ad 6109 PERROR("pthread_join apps notify");
4a15001e 6110 retval = -1;
9563b0ad 6111 }
9563b0ad 6112exit_apps_notify:
4a15001e 6113
cf3af59e 6114 ret = pthread_join(apps_thread, &status);
4a15001e
MD
6115 if (ret) {
6116 errno = ret;
9563b0ad 6117 PERROR("pthread_join apps");
4a15001e 6118 retval = -1;
cf3af59e 6119 }
cf3af59e 6120exit_apps:
4a15001e 6121
099e26bd 6122 ret = pthread_join(reg_apps_thread, &status);
4a15001e
MD
6123 if (ret) {
6124 errno = ret;
76d7553f 6125 PERROR("pthread_join");
4a15001e 6126 retval = -1;
099e26bd 6127 }
099e26bd 6128exit_reg_apps:
4a15001e 6129
772b8f4d
MD
6130 /*
6131 * Join dispatch thread after joining reg_apps_thread to ensure
6132 * we don't leak applications in the queue.
6133 */
099e26bd 6134 ret = pthread_join(dispatch_thread, &status);
4a15001e
MD
6135 if (ret) {
6136 errno = ret;
76d7553f 6137 PERROR("pthread_join");
4a15001e 6138 retval = -1;
099e26bd 6139 }
099e26bd 6140exit_dispatch:
4a15001e 6141
cf3af59e 6142 ret = pthread_join(client_thread, &status);
4a15001e
MD
6143 if (ret) {
6144 errno = ret;
76d7553f 6145 PERROR("pthread_join");
4a15001e 6146 retval = -1;
cf3af59e 6147 }
4a15001e 6148exit_client:
a88df331 6149
4a15001e
MD
6150 ret = pthread_join(health_thread, &status);
6151 if (ret) {
6152 errno = ret;
6153 PERROR("pthread_join health thread");
6154 retval = -1;
06f525de 6155 }
4a15001e 6156exit_health:
06f525de 6157
4a15001e
MD
6158exit_init_data:
6159 /*
6160 * sessiond_cleanup() is called when no other thread is running, except
6161 * the ht_cleanup thread, which is needed to destroy the hash tables.
6162 */
6163 rcu_thread_online();
6164 sessiond_cleanup();
6165 rcu_thread_offline();
6166 rcu_unregister_thread();
06f525de 6167
c4d5de9d
JG
6168 /*
6169 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6170 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6171 * the queue is empty before shutting down the clean-up thread.
6172 */
6173 rcu_barrier();
6174
5e97de00 6175 ret = fini_ht_cleanup_thread(&ht_cleanup_thread);
4a15001e 6176 if (ret) {
4a15001e 6177 retval = -1;
0b2dc8df
MD
6178 }
6179exit_ht_cleanup:
4a15001e 6180
4a15001e
MD
6181 health_app_destroy(health_sessiond);
6182exit_health_sessiond_cleanup:
7567352f 6183exit_create_run_as_worker_cleanup:
4a15001e
MD
6184
6185exit_options:
6186 sessiond_cleanup_options();
6187
6188exit_set_signal_handler:
0658924e 6189
4a15001e 6190 if (!retval) {
cf3af59e 6191 exit(EXIT_SUCCESS);
4a15001e
MD
6192 } else {
6193 exit(EXIT_FAILURE);
67e40797 6194 }
fac6795d 6195}
This page took 0.554712 seconds and 4 git commands to generate.