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