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