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