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