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