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