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