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