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