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