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