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