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