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