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