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