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