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