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