Fix: health subsystem issues with shared code
[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>
0fdd1e2c 28#include <sys/mman.h>
b73401da 29#include <sys/mount.h>
1e307fab 30#include <sys/resource.h>
fac6795d
DG
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
0fdd1e2c 34#include <sys/wait.h>
5c827ce0 35#include <urcu/uatomic.h>
fac6795d 36#include <unistd.h>
3bd1e081 37#include <config.h>
fac6795d 38
990570ed 39#include <common/common.h>
0ba98ebc 40#include <common/compat/poll.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"
54d01ffb 49#include "channel.h"
2f77fc4b 50#include "cmd.h"
00e2e675 51#include "consumer.h"
099e26bd 52#include "context.h"
54d01ffb 53#include "event.h"
4771f025 54#include "kernel.h"
f1e16794 55#include "kernel-consumer.h"
096102bd 56#include "modprobe.h"
0fdd1e2c 57#include "shm.h"
1e307fab 58#include "ust-ctl.h"
00e2e675 59#include "ust-consumer.h"
8e68d1c8 60#include "utils.h"
4063050c 61#include "fd-limit.h"
44a5e5eb 62#include "health.h"
8ac94142 63#include "testpoint.h"
fac6795d 64
ebaeda94
MD
65#define CONSUMERD_FILE "lttng-consumerd"
66
75462a81 67/* Const values */
686204ab 68const char default_home_dir[] = DEFAULT_HOME_DIR;
bbccc3d2 69const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
686204ab
MD
70const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
71const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
72
fac6795d
DG
73const char *progname;
74const char *opt_tracing_group;
35f90c40 75static const char *opt_pidfile;
5b8719f5 76static int opt_sig_parent;
97e19046 77static int opt_verbose_consumer;
fac6795d 78static int opt_daemon;
4fba7219 79static int opt_no_kernel;
fac6795d 80static int is_root; /* Set to 1 if the daemon is running as root */
1d4b027a 81static pid_t ppid; /* Parent PID for --sig-parent option */
67e40797 82static char *rundir;
3bd1e081 83
a23ec3a7
DG
84/*
85 * Consumer daemon specific control data. Every value not initialized here is
86 * set to 0 by the static definition.
87 */
3bd1e081
MD
88static struct consumer_data kconsumer_data = {
89 .type = LTTNG_CONSUMER_KERNEL,
60922cb0
DG
90 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
91 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
03550b58
MD
92 .err_sock = -1,
93 .cmd_sock = -1,
173af62f
DG
94 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
96 .cond = PTHREAD_COND_INITIALIZER,
97 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 98};
7753dea8
MD
99static struct consumer_data ustconsumer64_data = {
100 .type = LTTNG_CONSUMER64_UST,
60922cb0
DG
101 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
102 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
03550b58
MD
103 .err_sock = -1,
104 .cmd_sock = -1,
173af62f
DG
105 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
106 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
107 .cond = PTHREAD_COND_INITIALIZER,
108 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
109};
110static struct consumer_data ustconsumer32_data = {
111 .type = LTTNG_CONSUMER32_UST,
60922cb0
DG
112 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
113 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
03550b58
MD
114 .err_sock = -1,
115 .cmd_sock = -1,
173af62f
DG
116 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
117 .lock = PTHREAD_MUTEX_INITIALIZER,
a23ec3a7
DG
118 .cond = PTHREAD_COND_INITIALIZER,
119 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
120};
121
26c9d55e 122/* Shared between threads */
099e26bd 123static int dispatch_thread_exit;
fac6795d 124
54d01ffb
DG
125/* Global application Unix socket path */
126static char apps_unix_sock_path[PATH_MAX];
127/* Global client Unix socket path */
128static char client_unix_sock_path[PATH_MAX];
54d01ffb
DG
129/* global wait shm path for UST */
130static char wait_shm_path[PATH_MAX];
44a5e5eb
DG
131/* Global health check unix path */
132static char health_unix_sock_path[PATH_MAX];
fac6795d 133
1d4b027a 134/* Sockets and FDs */
a4b35e07
MD
135static int client_sock = -1;
136static int apps_sock = -1;
2f77fc4b 137int kernel_tracer_fd = -1;
76d7553f 138static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 139
273ea72c
DG
140/*
141 * Quit pipe for all threads. This permits a single cancellation point
142 * for all threads when receiving an event on the pipe.
143 */
76d7553f 144static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 145
099e26bd
DG
146/*
147 * This pipe is used to inform the thread managing application communication
148 * that a command is queued and ready to be processed.
149 */
76d7553f 150static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 151
1d4b027a 152/* Pthread, Mutexes and Semaphores */
1d4b027a 153static pthread_t apps_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;
5eb91c98 159
099e26bd
DG
160/*
161 * UST registration command queue. This queue is tied with a futex and uses a N
162 * wakers / 1 waiter implemented and detailed in futex.c/.h
163 *
164 * The thread_manage_apps and thread_dispatch_ust_registration interact with
165 * this queue and the wait/wake scheme.
166 */
167static struct ust_cmd_queue ust_cmd_queue;
168
b5541356
DG
169/*
170 * Pointer initialized before thread creation.
171 *
172 * This points to the tracing session list containing the session count and a
173 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
174 * MUST NOT be taken if you call a public function in session.c.
04ea676f 175 *
d063d709 176 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 177 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
178 */
179static struct ltt_session_list *session_list_ptr;
180
7753dea8
MD
181int ust_consumerd64_fd = -1;
182int ust_consumerd32_fd = -1;
183
fb6f1fa2
YB
184static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
185static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
186static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
187static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
fb09408a 188
2f77fc4b
DG
189static const char *module_proc_lttng = "/proc/lttng";
190
5c827ce0
DG
191/*
192 * Consumer daemon state which is changed when spawning it, killing it or in
193 * case of a fatal error.
194 */
195enum consumerd_state {
196 CONSUMER_STARTED = 1,
197 CONSUMER_STOPPED = 2,
198 CONSUMER_ERROR = 3,
199};
200
201/*
202 * This consumer daemon state is used to validate if a client command will be
203 * able to reach the consumer. If not, the client is informed. For instance,
204 * doing a "lttng start" when the consumer state is set to ERROR will return an
205 * error to the client.
206 *
207 * The following example shows a possible race condition of this scheme:
208 *
209 * consumer thread error happens
210 * client cmd arrives
211 * client cmd checks state -> still OK
212 * consumer thread exit, sets error
213 * client cmd try to talk to consumer
214 * ...
215 *
216 * However, since the consumer is a different daemon, we have no way of making
217 * sure the command will reach it safely even with this state flag. This is why
218 * we consider that up to the state validation during command processing, the
219 * command is safe. After that, we can not guarantee the correctness of the
220 * client request vis-a-vis the consumer.
221 */
222static enum consumerd_state ust_consumerd_state;
223static enum consumerd_state kernel_consumerd_state;
224
44a5e5eb
DG
225/* Used for the health monitoring of the session daemon. See health.h */
226struct health_state health_thread_cmd;
139ac872 227struct health_state health_thread_app_manage;
44a5e5eb
DG
228struct health_state health_thread_app_reg;
229struct health_state health_thread_kernel;
230
ae9e45b3
DG
231/*
232 * Socket timeout for receiving and sending in seconds.
233 */
234static int app_socket_timeout;
235
fb09408a 236static
7753dea8 237void setup_consumerd_path(void)
fb09408a 238{
fc7a59ce 239 const char *bin, *libdir;
fb09408a 240
7753dea8
MD
241 /*
242 * Allow INSTALL_BIN_PATH to be used as a target path for the
ebaeda94
MD
243 * native architecture size consumer if CONFIG_CONSUMER*_PATH
244 * has not been defined.
7753dea8 245 */
ebaeda94 246#if (CAA_BITS_PER_LONG == 32)
fc7a59ce
AM
247 if (!consumerd32_bin[0]) {
248 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
ebaeda94
MD
249 }
250 if (!consumerd32_libdir[0]) {
251 consumerd32_libdir = INSTALL_LIB_PATH;
252 }
253#elif (CAA_BITS_PER_LONG == 64)
fc7a59ce
AM
254 if (!consumerd64_bin[0]) {
255 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
7753dea8 256 }
ebaeda94
MD
257 if (!consumerd64_libdir[0]) {
258 consumerd64_libdir = INSTALL_LIB_PATH;
7753dea8
MD
259 }
260#else
261#error "Unknown bitness"
262#endif
263
fb09408a
MD
264 /*
265 * runtime env. var. overrides the build default.
266 */
fc7a59ce
AM
267 bin = getenv("LTTNG_CONSUMERD32_BIN");
268 if (bin) {
269 consumerd32_bin = bin;
7753dea8 270 }
fc7a59ce
AM
271 bin = getenv("LTTNG_CONSUMERD64_BIN");
272 if (bin) {
273 consumerd64_bin = bin;
ebaeda94 274 }
72f579ee 275 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
ebaeda94
MD
276 if (libdir) {
277 consumerd32_libdir = libdir;
278 }
72f579ee 279 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
ebaeda94
MD
280 if (libdir) {
281 consumerd64_libdir = libdir;
fb09408a
MD
282 }
283}
284
5eb91c98
DG
285/*
286 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
287 */
288static int create_thread_poll_set(struct lttng_poll_event *events,
289 unsigned int size)
290{
291 int ret;
292
293 if (events == NULL || size == 0) {
294 ret = -1;
295 goto error;
296 }
297
298 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
299 if (ret < 0) {
300 goto error;
301 }
302
303 /* Add quit pipe */
304 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
305 if (ret < 0) {
306 goto error;
307 }
308
309 return 0;
310
311error:
312 return ret;
313}
314
315/*
316 * Check if the thread quit pipe was triggered.
317 *
318 * Return 1 if it was triggered else 0;
319 */
320static int check_thread_quit_pipe(int fd, uint32_t events)
321{
322 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
323 return 1;
324 }
325
326 return 0;
327}
328
0fdd1e2c
DG
329/*
330 * Return group ID of the tracing group or -1 if not found.
331 */
996b65c8
MD
332static gid_t allowed_group(void)
333{
334 struct group *grp;
335
274e143b
MD
336 if (opt_tracing_group) {
337 grp = getgrnam(opt_tracing_group);
338 } else {
339 grp = getgrnam(default_tracing_group);
340 }
996b65c8
MD
341 if (!grp) {
342 return -1;
343 } else {
344 return grp->gr_gid;
345 }
346}
347
273ea72c 348/*
5eb91c98 349 * Init thread quit pipe.
273ea72c
DG
350 *
351 * Return -1 on error or 0 if all pipes are created.
352 */
353static int init_thread_quit_pipe(void)
354{
730389d9 355 int ret, i;
273ea72c 356
730389d9 357 ret = pipe(thread_quit_pipe);
273ea72c 358 if (ret < 0) {
730389d9 359 PERROR("thread quit pipe");
273ea72c
DG
360 goto error;
361 }
362
730389d9
DG
363 for (i = 0; i < 2; i++) {
364 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
365 if (ret < 0) {
366 PERROR("fcntl");
367 goto error;
368 }
369 }
370
273ea72c
DG
371error:
372 return ret;
373}
374
099e26bd
DG
375/*
376 * Stop all threads by closing the thread quit pipe.
377 */
cf3af59e
MD
378static void stop_threads(void)
379{
5eb91c98
DG
380 int ret;
381
cf3af59e
MD
382 /* Stopping all threads */
383 DBG("Terminating all threads");
54d01ffb 384 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
385 if (ret < 0) {
386 ERR("write error on thread quit pipe");
387 }
388
099e26bd 389 /* Dispatch thread */
26c9d55e 390 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 391 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
392}
393
fac6795d 394/*
d063d709 395 * Cleanup the daemon
fac6795d 396 */
cf3af59e 397static void cleanup(void)
fac6795d 398{
ef599319 399 int ret;
6620da75 400 char *cmd = NULL;
af9737e9 401 struct ltt_session *sess, *stmp;
fac6795d 402
1d4b027a 403 DBG("Cleaning up");
e07ae692 404
2f77fc4b
DG
405 /* First thing first, stop all threads */
406 utils_close_pipe(thread_quit_pipe);
407
35f90c40
DG
408 /*
409 * If opt_pidfile is undefined, the default file will be wiped when
410 * removing the rundir.
411 */
412 if (opt_pidfile) {
413 ret = remove(opt_pidfile);
414 if (ret < 0) {
415 PERROR("remove pidfile %s", opt_pidfile);
416 }
417 }
418
67e40797
DG
419 DBG("Removing %s directory", rundir);
420 ret = asprintf(&cmd, "rm -rf %s", rundir);
421 if (ret < 0) {
422 ERR("asprintf failed. Something is really wrong!");
423 }
5461b305 424
67e40797
DG
425 /* Remove lttng run directory */
426 ret = system(cmd);
427 if (ret < 0) {
428 ERR("Unable to clean %s", rundir);
1d4b027a 429 }
67e40797 430 free(cmd);
bd69add2 431 free(rundir);
5461b305 432
99bab54f 433 DBG("Cleaning up all sessions");
fac6795d 434
b5541356 435 /* Destroy session list mutex */
273ea72c
DG
436 if (session_list_ptr != NULL) {
437 pthread_mutex_destroy(&session_list_ptr->lock);
438
439 /* Cleanup ALL session */
54d01ffb
DG
440 cds_list_for_each_entry_safe(sess, stmp,
441 &session_list_ptr->head, list) {
2f77fc4b 442 cmd_destroy_session(sess, kernel_poll_pipe[1]);
273ea72c
DG
443 }
444 }
445
099e26bd 446 DBG("Closing all UST sockets");
56fff090 447 ust_app_clean_list();
099e26bd 448
4fba7219
DG
449 if (is_root && !opt_no_kernel) {
450 DBG2("Closing kernel fd");
a4b35e07 451 if (kernel_tracer_fd >= 0) {
76d7553f
MD
452 ret = close(kernel_tracer_fd);
453 if (ret) {
454 PERROR("close");
455 }
a4b35e07 456 }
2f50c8a3 457 DBG("Unloading kernel modules");
096102bd 458 modprobe_remove_lttng_all();
2f50c8a3 459 }
2f77fc4b 460
421cb601 461 /* <fun> */
f56a39af 462 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
421cb601
DG
463 "Matthew, BEET driven development works!%c[%dm",
464 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
465 /* </fun> */
fac6795d
DG
466}
467
e065084a 468/*
d063d709 469 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 470 *
d063d709 471 * Return lttcomm error code.
e065084a
DG
472 */
473static int send_unix_sock(int sock, void *buf, size_t len)
474{
475 /* Check valid length */
c617c0c6 476 if (len == 0) {
e065084a
DG
477 return -1;
478 }
479
480 return lttcomm_send_unix_sock(sock, buf, len);
481}
482
5461b305 483/*
d063d709 484 * Free memory of a command context structure.
5461b305 485 */
a2fb29a5 486static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 487{
a2fb29a5
DG
488 DBG("Clean command context structure");
489 if (*cmd_ctx) {
490 if ((*cmd_ctx)->llm) {
491 free((*cmd_ctx)->llm);
5461b305 492 }
a2fb29a5
DG
493 if ((*cmd_ctx)->lsm) {
494 free((*cmd_ctx)->lsm);
5461b305 495 }
a2fb29a5
DG
496 free(*cmd_ctx);
497 *cmd_ctx = NULL;
5461b305
DG
498 }
499}
500
fac6795d 501/*
0fdd1e2c 502 * Notify UST applications using the shm mmap futex.
fac6795d 503 */
0fdd1e2c 504static int notify_ust_apps(int active)
fac6795d 505{
0fdd1e2c 506 char *wait_shm_mmap;
fac6795d 507
0fdd1e2c 508 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 509
0fdd1e2c
DG
510 /* See shm.c for this call implying mmap, shm and futex calls */
511 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
512 if (wait_shm_mmap == NULL) {
fac6795d
DG
513 goto error;
514 }
515
0fdd1e2c
DG
516 /* Wake waiting process */
517 futex_wait_update((int32_t *) wait_shm_mmap, active);
518
519 /* Apps notified successfully */
520 return 0;
fac6795d
DG
521
522error:
0fdd1e2c 523 return -1;
fac6795d
DG
524}
525
e065084a 526/*
d063d709
DG
527 * Setup the outgoing data buffer for the response (llm) by allocating the
528 * right amount of memory and copying the original information from the lsm
529 * structure.
ca95a216 530 *
d063d709 531 * Return total size of the buffer pointed by buf.
ca95a216 532 */
5461b305 533static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 534{
f3ed775e 535 int ret, buf_size;
ca95a216 536
f3ed775e 537 buf_size = size;
5461b305 538
ba7f0ae5 539 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
5461b305 540 if (cmd_ctx->llm == NULL) {
76d7553f 541 PERROR("zmalloc");
5461b305 542 ret = -ENOMEM;
ca95a216
DG
543 goto error;
544 }
545
5461b305
DG
546 /* Copy common data */
547 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 548 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 549
5461b305
DG
550 cmd_ctx->llm->data_size = size;
551 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
552
ca95a216
DG
553 return buf_size;
554
555error:
556 return ret;
557}
558
7a485870 559/*
5eb91c98 560 * Update the kernel poll set of all channel fd available over all tracing
d063d709 561 * session. Add the wakeup pipe at the end of the set.
7a485870 562 */
5eb91c98 563static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 564{
5eb91c98 565 int ret;
7a485870
DG
566 struct ltt_session *session;
567 struct ltt_kernel_channel *channel;
568
5eb91c98 569 DBG("Updating kernel poll set");
7a485870 570
54d01ffb 571 session_lock_list();
b5541356 572 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 573 session_lock(session);
7a485870 574 if (session->kernel_session == NULL) {
54d01ffb 575 session_unlock(session);
7a485870
DG
576 continue;
577 }
7a485870 578
54d01ffb
DG
579 cds_list_for_each_entry(channel,
580 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
581 /* Add channel fd to the kernel poll set */
582 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
583 if (ret < 0) {
54d01ffb 584 session_unlock(session);
5eb91c98
DG
585 goto error;
586 }
587 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 588 }
54d01ffb 589 session_unlock(session);
7a485870 590 }
54d01ffb 591 session_unlock_list();
7a485870 592
5eb91c98 593 return 0;
7a485870
DG
594
595error:
54d01ffb 596 session_unlock_list();
7a485870
DG
597 return -1;
598}
599
600/*
54d01ffb 601 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 602 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 603 *
d063d709 604 * Useful for CPU hotplug feature.
7a485870 605 */
2bdd86d4 606static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
607{
608 int ret = 0;
609 struct ltt_session *session;
173af62f 610 struct ltt_kernel_session *ksess;
7a485870
DG
611 struct ltt_kernel_channel *channel;
612
613 DBG("Updating kernel streams for channel fd %d", fd);
614
54d01ffb 615 session_lock_list();
b5541356 616 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 617 session_lock(session);
7a485870 618 if (session->kernel_session == NULL) {
54d01ffb 619 session_unlock(session);
7a485870
DG
620 continue;
621 }
173af62f 622 ksess = session->kernel_session;
d9800920 623
173af62f 624 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
7a485870
DG
625 if (channel->fd == fd) {
626 DBG("Channel found, updating kernel streams");
627 ret = kernel_open_channel_stream(channel);
628 if (ret < 0) {
b3c750d2 629 goto error;
7a485870 630 }
d9800920 631
7a485870 632 /*
5eb91c98
DG
633 * Have we already sent fds to the consumer? If yes, it means
634 * that tracing is started so it is safe to send our updated
635 * stream fds.
7a485870 636 */
173af62f
DG
637 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
638 struct lttng_ht_iter iter;
639 struct consumer_socket *socket;
640
e7fe706f 641 rcu_read_lock();
173af62f
DG
642 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
643 &iter.iter, socket, node.node) {
644 /* Code flow error */
645 assert(socket->fd >= 0);
646
647 pthread_mutex_lock(socket->lock);
f50f23d9 648 ret = kernel_consumer_send_channel_stream(socket,
173af62f
DG
649 channel, ksess);
650 pthread_mutex_unlock(socket->lock);
651 if (ret < 0) {
e7fe706f 652 rcu_read_unlock();
173af62f
DG
653 goto error;
654 }
7a485870 655 }
e7fe706f 656 rcu_read_unlock();
7a485870 657 }
b3c750d2 658 goto error;
7a485870
DG
659 }
660 }
54d01ffb 661 session_unlock(session);
7a485870 662 }
54d01ffb 663 session_unlock_list();
b3c750d2 664 return ret;
7a485870 665
b3c750d2 666error:
54d01ffb
DG
667 session_unlock(session);
668 session_unlock_list();
7a485870
DG
669 return ret;
670}
671
487cf67c
DG
672/*
673 * For each tracing session, update newly registered apps.
674 */
675static void update_ust_app(int app_sock)
676{
677 struct ltt_session *sess, *stmp;
678
4ee14516
DG
679 session_lock_list();
680
487cf67c
DG
681 /* For all tracing session(s) */
682 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
4ee14516 683 session_lock(sess);
421cb601
DG
684 if (sess->ust_session) {
685 ust_app_global_update(sess->ust_session, app_sock);
686 }
4ee14516 687 session_unlock(sess);
487cf67c 688 }
4ee14516
DG
689
690 session_unlock_list();
487cf67c
DG
691}
692
7a485870 693/*
d063d709 694 * This thread manage event coming from the kernel.
7a485870 695 *
d063d709
DG
696 * Features supported in this thread:
697 * -) CPU Hotplug
7a485870
DG
698 */
699static void *thread_manage_kernel(void *data)
700{
139ac872 701 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 702 uint32_t revents, nb_fd;
7a485870 703 char tmp;
5eb91c98 704 struct lttng_poll_event events;
7a485870 705
6993eeb3 706 DBG("[thread] Thread manage kernel started");
7a485870 707
927ca06a
DG
708 health_register(HEALTH_TYPE_KERNEL);
709
d5d63bf1
DG
710 /*
711 * This first step of the while is to clean this structure which could free
712 * non NULL pointers so zero it before the loop.
713 */
714 memset(&events, 0, sizeof(events));
715
6993eeb3
CB
716 if (testpoint(thread_manage_kernel)) {
717 goto error_testpoint;
718 }
8ac94142 719
44a5e5eb
DG
720 health_code_update(&health_thread_kernel);
721
6993eeb3 722 if (testpoint(thread_manage_kernel_before_loop)) {
d21b0d71 723 goto error_testpoint;
6993eeb3
CB
724 }
725
7a485870 726 while (1) {
44a5e5eb
DG
727 health_code_update(&health_thread_kernel);
728
7a485870 729 if (update_poll_flag == 1) {
d21b0d71
DG
730 /* Clean events object. We are about to populate it again. */
731 lttng_poll_clean(&events);
732
733 ret = create_thread_poll_set(&events, 2);
734 if (ret < 0) {
735 goto error_poll_create;
736 }
737
738 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
739 if (ret < 0) {
740 goto error;
741 }
5f822d0a 742
d21b0d71 743 /* This will add the available kernel channel if any. */
5eb91c98
DG
744 ret = update_kernel_poll(&events);
745 if (ret < 0) {
7a485870
DG
746 goto error;
747 }
748 update_poll_flag = 0;
749 }
750
d21b0d71 751 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
7a485870
DG
752
753 /* Poll infinite value of time */
88f2b785 754 restart:
44a5e5eb 755 health_poll_update(&health_thread_kernel);
5eb91c98 756 ret = lttng_poll_wait(&events, -1);
44a5e5eb 757 health_poll_update(&health_thread_kernel);
7a485870 758 if (ret < 0) {
88f2b785
MD
759 /*
760 * Restart interrupted system call.
761 */
762 if (errno == EINTR) {
763 goto restart;
764 }
7a485870
DG
765 goto error;
766 } else if (ret == 0) {
767 /* Should not happen since timeout is infinite */
85611738
DG
768 ERR("Return value of poll is 0 with an infinite timeout.\n"
769 "This should not have happened! Continuing...");
7a485870
DG
770 continue;
771 }
772
0d9c5d77
DG
773 nb_fd = ret;
774
5eb91c98
DG
775 for (i = 0; i < nb_fd; i++) {
776 /* Fetch once the poll data */
777 revents = LTTNG_POLL_GETEV(&events, i);
778 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 779
44a5e5eb
DG
780 health_code_update(&health_thread_kernel);
781
5eb91c98
DG
782 /* Thread quit pipe has been closed. Killing thread. */
783 ret = check_thread_quit_pipe(pollfd, revents);
784 if (ret) {
139ac872
MD
785 err = 0;
786 goto exit;
5eb91c98 787 }
7a485870 788
5eb91c98
DG
789 /* Check for data on kernel pipe */
790 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
f921c78f
DG
791 do {
792 ret = read(kernel_poll_pipe[0], &tmp, 1);
793 } while (ret < 0 && errno == EINTR);
794 /*
795 * Ret value is useless here, if this pipe gets any actions an
796 * update is required anyway.
797 */
5eb91c98
DG
798 update_poll_flag = 1;
799 continue;
800 } else {
801 /*
802 * New CPU detected by the kernel. Adding kernel stream to
803 * kernel session and updating the kernel consumer
804 */
805 if (revents & LPOLLIN) {
2bdd86d4 806 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
807 if (ret < 0) {
808 continue;
809 }
810 break;
811 /*
812 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
813 * and unregister kernel stream at this point.
814 */
7a485870 815 }
7a485870
DG
816 }
817 }
818 }
819
139ac872 820exit:
7a485870 821error:
5eb91c98 822 lttng_poll_clean(&events);
76d7553f 823error_poll_create:
6993eeb3 824error_testpoint:
6620da75
DG
825 utils_close_pipe(kernel_poll_pipe);
826 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
139ac872
MD
827 if (err) {
828 health_error(&health_thread_kernel);
829 ERR("Health error occurred in %s", __func__);
6620da75
DG
830 WARN("Kernel thread died unexpectedly. "
831 "Kernel tracing can continue but CPU hotplug is disabled.");
139ac872 832 }
927ca06a 833 health_unregister();
76d7553f 834 DBG("Kernel thread dying");
7a485870
DG
835 return NULL;
836}
837
a23ec3a7
DG
838/*
839 * Signal pthread condition of the consumer data that the thread.
840 */
841static void signal_consumer_condition(struct consumer_data *data, int state)
842{
843 pthread_mutex_lock(&data->cond_mutex);
844
845 /*
846 * The state is set before signaling. It can be any value, it's the waiter
847 * job to correctly interpret this condition variable associated to the
848 * consumer pthread_cond.
849 *
850 * A value of 0 means that the corresponding thread of the consumer data
851 * was not started. 1 indicates that the thread has started and is ready
852 * for action. A negative value means that there was an error during the
853 * thread bootstrap.
854 */
855 data->consumer_thread_is_ready = state;
856 (void) pthread_cond_signal(&data->cond);
857
858 pthread_mutex_unlock(&data->cond_mutex);
859}
860
1d4b027a 861/*
3bd1e081 862 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 863 */
3bd1e081 864static void *thread_manage_consumer(void *data)
1d4b027a 865{
139ac872 866 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98 867 uint32_t revents, nb_fd;
1d4b027a 868 enum lttcomm_return_code code;
5eb91c98 869 struct lttng_poll_event events;
3bd1e081 870 struct consumer_data *consumer_data = data;
1d4b027a 871
3bd1e081 872 DBG("[thread] Manage consumer started");
1d4b027a 873
927ca06a
DG
874 health_register(HEALTH_TYPE_CONSUMER);
875
9449cc75
DG
876 /*
877 * Since the consumer thread can be spawned at any moment in time, we init
878 * the health to a poll status (1, which is a valid health over time).
879 * When the thread starts, we update here the health to a "code" path being
880 * an even value so this thread, when reaching a poll wait, does not
881 * trigger an error with an even value.
882 *
883 * Here is the use case we avoid.
884 *
885 * +1: the first poll update during initialization (main())
886 * +2 * x: multiple code update once in this thread.
887 * +1: poll wait in this thread (being a good health state).
888 * == even number which after the wait period shows as a bad health.
889 *
890 * In a nutshell, the following poll update to the health state brings back
891 * the state to an even value meaning a code path.
892 */
893 health_poll_update(&consumer_data->health);
894
5eb91c98
DG
895 /*
896 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
897 * Nothing more will be added to this poll set.
898 */
899 ret = create_thread_poll_set(&events, 2);
900 if (ret < 0) {
76d7553f 901 goto error_poll;
5eb91c98 902 }
273ea72c 903
edb8b045
DG
904 /*
905 * The error socket here is already in a listening state which was done
906 * just before spawning this thread to avoid a race between the consumer
907 * daemon exec trying to connect and the listen() call.
908 */
3bd1e081 909 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
910 if (ret < 0) {
911 goto error;
912 }
913
44a5e5eb
DG
914 health_code_update(&consumer_data->health);
915
273ea72c 916 /* Inifinite blocking call, waiting for transmission */
88f2b785 917restart:
44a5e5eb 918 health_poll_update(&consumer_data->health);
8ac94142 919
6993eeb3
CB
920 if (testpoint(thread_manage_consumer)) {
921 goto error;
922 }
8ac94142 923
5eb91c98 924 ret = lttng_poll_wait(&events, -1);
44a5e5eb 925 health_poll_update(&consumer_data->health);
273ea72c 926 if (ret < 0) {
88f2b785
MD
927 /*
928 * Restart interrupted system call.
929 */
930 if (errno == EINTR) {
931 goto restart;
932 }
273ea72c
DG
933 goto error;
934 }
935
0d9c5d77
DG
936 nb_fd = ret;
937
5eb91c98
DG
938 for (i = 0; i < nb_fd; i++) {
939 /* Fetch once the poll data */
940 revents = LTTNG_POLL_GETEV(&events, i);
941 pollfd = LTTNG_POLL_GETFD(&events, i);
942
44a5e5eb
DG
943 health_code_update(&consumer_data->health);
944
5eb91c98
DG
945 /* Thread quit pipe has been closed. Killing thread. */
946 ret = check_thread_quit_pipe(pollfd, revents);
947 if (ret) {
139ac872
MD
948 err = 0;
949 goto exit;
5eb91c98
DG
950 }
951
952 /* Event on the registration socket */
3bd1e081 953 if (pollfd == consumer_data->err_sock) {
5eb91c98 954 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 955 ERR("consumer err socket poll error");
5eb91c98
DG
956 goto error;
957 }
958 }
273ea72c
DG
959 }
960
3bd1e081 961 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
962 if (sock < 0) {
963 goto error;
964 }
965
b662582b
DG
966 /*
967 * Set the CLOEXEC flag. Return code is useless because either way, the
968 * show must go on.
969 */
970 (void) utils_set_fd_cloexec(sock);
971
44a5e5eb
DG
972 health_code_update(&consumer_data->health);
973
3bd1e081 974 DBG2("Receiving code from consumer err_sock");
ee0b0061 975
712ea556 976 /* Getting status code from kconsumerd */
54d01ffb
DG
977 ret = lttcomm_recv_unix_sock(sock, &code,
978 sizeof(enum lttcomm_return_code));
1d4b027a
DG
979 if (ret <= 0) {
980 goto error;
981 }
982
44a5e5eb
DG
983 health_code_update(&consumer_data->health);
984
f73fabfd 985 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
3bd1e081
MD
986 consumer_data->cmd_sock =
987 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
988 if (consumer_data->cmd_sock < 0) {
a23ec3a7
DG
989 /* On error, signal condition and quit. */
990 signal_consumer_condition(consumer_data, -1);
48842b30 991 PERROR("consumer connect");
1d4b027a
DG
992 goto error;
993 }
a23ec3a7
DG
994 signal_consumer_condition(consumer_data, 1);
995 DBG("Consumer command socket ready");
1d4b027a 996 } else {
3bd1e081 997 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
998 lttcomm_get_readable_code(-code));
999 goto error;
1000 }
1001
54d01ffb 1002 /* Remove the kconsumerd error sock since we've established a connexion */
3bd1e081 1003 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1004 if (ret < 0) {
72079cae
DG
1005 goto error;
1006 }
1007
5eb91c98
DG
1008 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1009 if (ret < 0) {
72079cae 1010 goto error;
5eb91c98
DG
1011 }
1012
44a5e5eb
DG
1013 health_code_update(&consumer_data->health);
1014
5eb91c98 1015 /* Inifinite blocking call, waiting for transmission */
88f2b785 1016restart_poll:
44a5e5eb 1017 health_poll_update(&consumer_data->health);
5eb91c98 1018 ret = lttng_poll_wait(&events, -1);
44a5e5eb 1019 health_poll_update(&consumer_data->health);
5eb91c98 1020 if (ret < 0) {
88f2b785
MD
1021 /*
1022 * Restart interrupted system call.
1023 */
1024 if (errno == EINTR) {
1025 goto restart_poll;
1026 }
72079cae
DG
1027 goto error;
1028 }
1029
0d9c5d77
DG
1030 nb_fd = ret;
1031
5eb91c98
DG
1032 for (i = 0; i < nb_fd; i++) {
1033 /* Fetch once the poll data */
1034 revents = LTTNG_POLL_GETEV(&events, i);
1035 pollfd = LTTNG_POLL_GETFD(&events, i);
1036
44a5e5eb
DG
1037 health_code_update(&consumer_data->health);
1038
5eb91c98
DG
1039 /* Thread quit pipe has been closed. Killing thread. */
1040 ret = check_thread_quit_pipe(pollfd, revents);
1041 if (ret) {
139ac872
MD
1042 err = 0;
1043 goto exit;
5eb91c98
DG
1044 }
1045
1046 /* Event on the kconsumerd socket */
1047 if (pollfd == sock) {
1048 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1049 ERR("consumer err socket second poll error");
5eb91c98
DG
1050 goto error;
1051 }
1052 }
1053 }
1054
44a5e5eb
DG
1055 health_code_update(&consumer_data->health);
1056
712ea556 1057 /* Wait for any kconsumerd error */
54d01ffb
DG
1058 ret = lttcomm_recv_unix_sock(sock, &code,
1059 sizeof(enum lttcomm_return_code));
712ea556 1060 if (ret <= 0) {
3bd1e081 1061 ERR("consumer closed the command socket");
712ea556 1062 goto error;
6f61d021 1063 }
1d4b027a 1064
3bd1e081 1065 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
712ea556 1066
139ac872 1067exit:
1d4b027a 1068error:
5c827ce0
DG
1069 /* Immediately set the consumerd state to stopped */
1070 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1071 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1072 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1073 consumer_data->type == LTTNG_CONSUMER32_UST) {
1074 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1075 } else {
1076 /* Code flow error... */
1077 assert(0);
1078 }
1079
76d7553f
MD
1080 if (consumer_data->err_sock >= 0) {
1081 ret = close(consumer_data->err_sock);
1082 if (ret) {
1083 PERROR("close");
1084 }
1085 }
1086 if (consumer_data->cmd_sock >= 0) {
1087 ret = close(consumer_data->cmd_sock);
1088 if (ret) {
1089 PERROR("close");
1090 }
1091 }
1092 if (sock >= 0) {
1093 ret = close(sock);
1094 if (ret) {
1095 PERROR("close");
1096 }
1097 }
273ea72c 1098
3bd1e081
MD
1099 unlink(consumer_data->err_unix_sock_path);
1100 unlink(consumer_data->cmd_unix_sock_path);
1101 consumer_data->pid = 0;
1d4b027a 1102
5eb91c98 1103 lttng_poll_clean(&events);
76d7553f 1104error_poll:
139ac872
MD
1105 if (err) {
1106 health_error(&consumer_data->health);
1107 ERR("Health error occurred in %s", __func__);
1108 }
927ca06a 1109 health_unregister();
76d7553f 1110 DBG("consumer thread cleanup completed");
0177d773 1111
5eb91c98 1112 return NULL;
099e26bd
DG
1113}
1114
099e26bd
DG
1115/*
1116 * This thread manage application communication.
1d4b027a
DG
1117 */
1118static void *thread_manage_apps(void *data)
099e26bd 1119{
139ac872 1120 int i, ret, pollfd, err = -1;
5eb91c98 1121 uint32_t revents, nb_fd;
099e26bd 1122 struct ust_command ust_cmd;
5eb91c98 1123 struct lttng_poll_event events;
099e26bd
DG
1124
1125 DBG("[thread] Manage application started");
1126
f6a9efaa
DG
1127 rcu_register_thread();
1128 rcu_thread_online();
1129
927ca06a
DG
1130 health_register(HEALTH_TYPE_APP_MANAGE);
1131
6993eeb3
CB
1132 if (testpoint(thread_manage_apps)) {
1133 goto error_testpoint;
1134 }
1135
139ac872 1136 health_code_update(&health_thread_app_manage);
44a5e5eb 1137
5eb91c98
DG
1138 ret = create_thread_poll_set(&events, 2);
1139 if (ret < 0) {
76d7553f 1140 goto error_poll_create;
5eb91c98 1141 }
099e26bd 1142
5eb91c98
DG
1143 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1144 if (ret < 0) {
1145 goto error;
1146 }
099e26bd 1147
6993eeb3
CB
1148 if (testpoint(thread_manage_apps_before_loop)) {
1149 goto error;
1150 }
8ac94142 1151
139ac872 1152 health_code_update(&health_thread_app_manage);
44a5e5eb 1153
5eb91c98 1154 while (1) {
d21b0d71 1155 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
099e26bd
DG
1156
1157 /* Inifinite blocking call, waiting for transmission */
88f2b785 1158 restart:
139ac872 1159 health_poll_update(&health_thread_app_manage);
5eb91c98 1160 ret = lttng_poll_wait(&events, -1);
139ac872 1161 health_poll_update(&health_thread_app_manage);
099e26bd 1162 if (ret < 0) {
88f2b785
MD
1163 /*
1164 * Restart interrupted system call.
1165 */
1166 if (errno == EINTR) {
1167 goto restart;
1168 }
099e26bd
DG
1169 goto error;
1170 }
1171
0d9c5d77
DG
1172 nb_fd = ret;
1173
5eb91c98
DG
1174 for (i = 0; i < nb_fd; i++) {
1175 /* Fetch once the poll data */
1176 revents = LTTNG_POLL_GETEV(&events, i);
1177 pollfd = LTTNG_POLL_GETFD(&events, i);
1178
139ac872 1179 health_code_update(&health_thread_app_manage);
44a5e5eb 1180
5eb91c98
DG
1181 /* Thread quit pipe has been closed. Killing thread. */
1182 ret = check_thread_quit_pipe(pollfd, revents);
1183 if (ret) {
139ac872
MD
1184 err = 0;
1185 goto exit;
5eb91c98 1186 }
099e26bd 1187
5eb91c98
DG
1188 /* Inspect the apps cmd pipe */
1189 if (pollfd == apps_cmd_pipe[0]) {
1190 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1191 ERR("Apps command pipe error");
0177d773 1192 goto error;
5eb91c98
DG
1193 } else if (revents & LPOLLIN) {
1194 /* Empty pipe */
f921c78f
DG
1195 do {
1196 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1197 } while (ret < 0 && errno == EINTR);
5eb91c98 1198 if (ret < 0 || ret < sizeof(ust_cmd)) {
76d7553f 1199 PERROR("read apps cmd pipe");
5eb91c98
DG
1200 goto error;
1201 }
099e26bd 1202
139ac872 1203 health_code_update(&health_thread_app_manage);
44a5e5eb 1204
5eb91c98 1205 /* Register applicaton to the session daemon */
56fff090 1206 ret = ust_app_register(&ust_cmd.reg_msg,
54d01ffb 1207 ust_cmd.sock);
88ff5b7f 1208 if (ret == -ENOMEM) {
5eb91c98 1209 goto error;
88ff5b7f
MD
1210 } else if (ret < 0) {
1211 break;
5eb91c98
DG
1212 }
1213
139ac872 1214 health_code_update(&health_thread_app_manage);
44a5e5eb 1215
acc7b41b 1216 /*
e0c7ec2b 1217 * Validate UST version compatibility.
acc7b41b 1218 */
e0c7ec2b
DG
1219 ret = ust_app_validate_version(ust_cmd.sock);
1220 if (ret >= 0) {
1221 /*
1222 * Add channel(s) and event(s) to newly registered apps
1223 * from lttng global UST domain.
1224 */
1225 update_ust_app(ust_cmd.sock);
1226 }
acc7b41b 1227
139ac872 1228 health_code_update(&health_thread_app_manage);
44a5e5eb 1229
f2ca2e25 1230 ret = ust_app_register_done(ust_cmd.sock);
5eb91c98
DG
1231 if (ret < 0) {
1232 /*
1233 * If the registration is not possible, we simply
1234 * unregister the apps and continue
1235 */
56fff090 1236 ust_app_unregister(ust_cmd.sock);
5eb91c98
DG
1237 } else {
1238 /*
622c9ecf
DG
1239 * We only monitor the error events of the socket. This
1240 * thread does not handle any incoming data from UST
1241 * (POLLIN).
5eb91c98 1242 */
622c9ecf
DG
1243 ret = lttng_poll_add(&events, ust_cmd.sock,
1244 LPOLLERR & LPOLLHUP & LPOLLRDHUP);
5eb91c98
DG
1245 if (ret < 0) {
1246 goto error;
1247 }
1248
ae9e45b3
DG
1249 /* Set socket timeout for both receiving and ending */
1250 (void) lttcomm_setsockopt_rcv_timeout(ust_cmd.sock,
1251 app_socket_timeout);
1252 (void) lttcomm_setsockopt_snd_timeout(ust_cmd.sock,
1253 app_socket_timeout);
1254
54d01ffb
DG
1255 DBG("Apps with sock %d added to poll set",
1256 ust_cmd.sock);
5eb91c98 1257 }
487cf67c 1258
139ac872 1259 health_code_update(&health_thread_app_manage);
44a5e5eb 1260
5eb91c98 1261 break;
0177d773 1262 }
5eb91c98
DG
1263 } else {
1264 /*
54d01ffb
DG
1265 * At this point, we know that a registered application made
1266 * the event at poll_wait.
5eb91c98
DG
1267 */
1268 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1269 /* Removing from the poll set */
1270 ret = lttng_poll_del(&events, pollfd);
1271 if (ret < 0) {
1272 goto error;
1273 }
099e26bd 1274
b9d9b220 1275 /* Socket closed on remote end. */
56fff090 1276 ust_app_unregister(pollfd);
5eb91c98
DG
1277 break;
1278 }
099e26bd 1279 }
44a5e5eb 1280
139ac872 1281 health_code_update(&health_thread_app_manage);
099e26bd 1282 }
099e26bd
DG
1283 }
1284
139ac872 1285exit:
099e26bd 1286error:
5eb91c98 1287 lttng_poll_clean(&events);
76d7553f 1288error_poll_create:
6993eeb3 1289error_testpoint:
6620da75
DG
1290 utils_close_pipe(apps_cmd_pipe);
1291 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1292
1293 /*
1294 * We don't clean the UST app hash table here since already registered
1295 * applications can still be controlled so let them be until the session
1296 * daemon dies or the applications stop.
1297 */
1298
139ac872
MD
1299 if (err) {
1300 health_error(&health_thread_app_manage);
1301 ERR("Health error occurred in %s", __func__);
1302 }
927ca06a 1303 health_unregister();
76d7553f 1304 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1305 rcu_thread_offline();
1306 rcu_unregister_thread();
099e26bd
DG
1307 return NULL;
1308}
1309
1310/*
1311 * Dispatch request from the registration threads to the application
1312 * communication thread.
1313 */
1314static void *thread_dispatch_ust_registration(void *data)
1315{
1316 int ret;
1317 struct cds_wfq_node *node;
1318 struct ust_command *ust_cmd = NULL;
1319
1320 DBG("[thread] Dispatch UST command started");
1321
26c9d55e 1322 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
099e26bd
DG
1323 /* Atomically prepare the queue futex */
1324 futex_nto1_prepare(&ust_cmd_queue.futex);
1325
1326 do {
1327 /* Dequeue command for registration */
1328 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1329 if (node == NULL) {
00a17c97 1330 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1331 /* Continue thread execution */
1332 break;
1333 }
1334
1335 ust_cmd = caa_container_of(node, struct ust_command, node);
1336
2f50c8a3
DG
1337 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1338 " gid:%d sock:%d name:%s (version %d.%d)",
1339 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1340 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1341 ust_cmd->sock, ust_cmd->reg_msg.name,
1342 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1343 /*
1344 * Inform apps thread of the new application registration. This
1345 * call is blocking so we can be assured that the data will be read
1346 * at some point in time or wait to the end of the world :)
1347 */
6620da75 1348 if (apps_cmd_pipe[1] >= 0) {
fbb3b395
DG
1349 do {
1350 ret = write(apps_cmd_pipe[1], ust_cmd,
1351 sizeof(struct ust_command));
1352 } while (ret < 0 && errno == EINTR);
4cec016f 1353 if (ret < 0 || ret != sizeof(struct ust_command)) {
6620da75
DG
1354 PERROR("write apps cmd pipe");
1355 if (errno == EBADF) {
1356 /*
1357 * We can't inform the application thread to process
1358 * registration. We will exit or else application
1359 * registration will not occur and tracing will never
1360 * start.
1361 */
1362 goto error;
1363 }
1364 }
1365 } else {
1366 /* Application manager thread is not available. */
1367 ret = close(ust_cmd->sock);
1368 if (ret < 0) {
1369 PERROR("close ust_cmd sock");
099e26bd
DG
1370 }
1371 }
1372 free(ust_cmd);
1373 } while (node != NULL);
1374
1375 /* Futex wait on queue. Blocking call on futex() */
1376 futex_nto1_wait(&ust_cmd_queue.futex);
1377 }
1378
1379error:
1380 DBG("Dispatch thread dying");
1381 return NULL;
1382}
1383
1384/*
1385 * This thread manage application registration.
1386 */
1387static void *thread_registration_apps(void *data)
1d4b027a 1388{
139ac872 1389 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1390 uint32_t revents, nb_fd;
1391 struct lttng_poll_event events;
099e26bd
DG
1392 /*
1393 * Get allocated in this thread, enqueued to a global queue, dequeued and
1394 * freed in the manage apps thread.
1395 */
1396 struct ust_command *ust_cmd = NULL;
1d4b027a 1397
099e26bd 1398 DBG("[thread] Manage application registration started");
1d4b027a 1399
927ca06a
DG
1400 health_register(HEALTH_TYPE_APP_REG);
1401
6993eeb3
CB
1402 if (testpoint(thread_registration_apps)) {
1403 goto error_testpoint;
1404 }
8ac94142 1405
1d4b027a
DG
1406 ret = lttcomm_listen_unix_sock(apps_sock);
1407 if (ret < 0) {
76d7553f 1408 goto error_listen;
1d4b027a
DG
1409 }
1410
5eb91c98
DG
1411 /*
1412 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1413 * more will be added to this poll set.
1414 */
1415 ret = create_thread_poll_set(&events, 2);
1416 if (ret < 0) {
76d7553f 1417 goto error_create_poll;
5eb91c98 1418 }
273ea72c 1419
5eb91c98
DG
1420 /* Add the application registration socket */
1421 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1422 if (ret < 0) {
76d7553f 1423 goto error_poll_add;
5eb91c98 1424 }
273ea72c 1425
1d4b027a 1426 /* Notify all applications to register */
0fdd1e2c
DG
1427 ret = notify_ust_apps(1);
1428 if (ret < 0) {
1429 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1430 "Execution continues but there might be problem for already\n"
1431 "running applications that wishes to register.");
0fdd1e2c 1432 }
1d4b027a
DG
1433
1434 while (1) {
1435 DBG("Accepting application registration");
273ea72c
DG
1436
1437 /* Inifinite blocking call, waiting for transmission */
88f2b785 1438 restart:
139ac872 1439 health_poll_update(&health_thread_app_reg);
5eb91c98 1440 ret = lttng_poll_wait(&events, -1);
139ac872 1441 health_poll_update(&health_thread_app_reg);
273ea72c 1442 if (ret < 0) {
88f2b785
MD
1443 /*
1444 * Restart interrupted system call.
1445 */
1446 if (errno == EINTR) {
1447 goto restart;
1448 }
273ea72c
DG
1449 goto error;
1450 }
1451
0d9c5d77
DG
1452 nb_fd = ret;
1453
5eb91c98 1454 for (i = 0; i < nb_fd; i++) {
139ac872
MD
1455 health_code_update(&health_thread_app_reg);
1456
5eb91c98
DG
1457 /* Fetch once the poll data */
1458 revents = LTTNG_POLL_GETEV(&events, i);
1459 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1460
5eb91c98
DG
1461 /* Thread quit pipe has been closed. Killing thread. */
1462 ret = check_thread_quit_pipe(pollfd, revents);
1463 if (ret) {
139ac872
MD
1464 err = 0;
1465 goto exit;
90014c57 1466 }
1d4b027a 1467
5eb91c98
DG
1468 /* Event on the registration socket */
1469 if (pollfd == apps_sock) {
1470 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1471 ERR("Register apps socket poll error");
1472 goto error;
1473 } else if (revents & LPOLLIN) {
1474 sock = lttcomm_accept_unix_sock(apps_sock);
1475 if (sock < 0) {
1476 goto error;
1477 }
099e26bd 1478
b662582b
DG
1479 /*
1480 * Set the CLOEXEC flag. Return code is useless because
1481 * either way, the show must go on.
1482 */
1483 (void) utils_set_fd_cloexec(sock);
1484
5eb91c98 1485 /* Create UST registration command for enqueuing */
ba7f0ae5 1486 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1487 if (ust_cmd == NULL) {
76d7553f 1488 PERROR("ust command zmalloc");
5eb91c98
DG
1489 goto error;
1490 }
1d4b027a 1491
5eb91c98
DG
1492 /*
1493 * Using message-based transmissions to ensure we don't
1494 * have to deal with partially received messages.
1495 */
4063050c
MD
1496 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1497 if (ret < 0) {
1498 ERR("Exhausted file descriptors allowed for applications.");
1499 free(ust_cmd);
1500 ret = close(sock);
1501 if (ret) {
1502 PERROR("close");
1503 }
1504 sock = -1;
1505 continue;
1506 }
139ac872 1507 health_code_update(&health_thread_app_reg);
5eb91c98
DG
1508 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1509 sizeof(struct ust_register_msg));
1510 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1511 if (ret < 0) {
76d7553f 1512 PERROR("lttcomm_recv_unix_sock register apps");
5eb91c98
DG
1513 } else {
1514 ERR("Wrong size received on apps register");
1515 }
1516 free(ust_cmd);
76d7553f
MD
1517 ret = close(sock);
1518 if (ret) {
1519 PERROR("close");
1520 }
4063050c 1521 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 1522 sock = -1;
5eb91c98
DG
1523 continue;
1524 }
139ac872 1525 health_code_update(&health_thread_app_reg);
099e26bd 1526
5eb91c98 1527 ust_cmd->sock = sock;
34a2494f 1528 sock = -1;
099e26bd 1529
5eb91c98
DG
1530 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1531 " gid:%d sock:%d name:%s (version %d.%d)",
1532 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1533 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1534 ust_cmd->sock, ust_cmd->reg_msg.name,
1535 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 1536
5eb91c98
DG
1537 /*
1538 * Lock free enqueue the registration request. The red pill
54d01ffb 1539 * has been taken! This apps will be part of the *system*.
5eb91c98
DG
1540 */
1541 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1542
1543 /*
1544 * Wake the registration queue futex. Implicit memory
1545 * barrier with the exchange in cds_wfq_enqueue.
1546 */
1547 futex_nto1_wake(&ust_cmd_queue.futex);
1548 }
1549 }
90014c57 1550 }
1d4b027a
DG
1551 }
1552
139ac872 1553exit:
1d4b027a 1554error:
139ac872
MD
1555 if (err) {
1556 health_error(&health_thread_app_reg);
1557 ERR("Health error occurred in %s", __func__);
1558 }
139ac872 1559
0fdd1e2c
DG
1560 /* Notify that the registration thread is gone */
1561 notify_ust_apps(0);
1562
a4b35e07 1563 if (apps_sock >= 0) {
76d7553f
MD
1564 ret = close(apps_sock);
1565 if (ret) {
1566 PERROR("close");
1567 }
a4b35e07 1568 }
46c3f085 1569 if (sock >= 0) {
76d7553f
MD
1570 ret = close(sock);
1571 if (ret) {
1572 PERROR("close");
1573 }
4063050c 1574 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 1575 }
273ea72c 1576 unlink(apps_unix_sock_path);
0fdd1e2c 1577
76d7553f 1578error_poll_add:
5eb91c98 1579 lttng_poll_clean(&events);
76d7553f
MD
1580error_listen:
1581error_create_poll:
6993eeb3 1582error_testpoint:
76d7553f 1583 DBG("UST Registration thread cleanup complete");
927ca06a 1584 health_unregister();
5eb91c98 1585
1d4b027a
DG
1586 return NULL;
1587}
1588
8c0faa1d 1589/*
3bd1e081 1590 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 1591 * exec or it will fails.
8c0faa1d 1592 */
3bd1e081 1593static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d 1594{
a23ec3a7 1595 int ret, clock_ret;
ee0b0061
DG
1596 struct timespec timeout;
1597
a23ec3a7
DG
1598 /* Make sure we set the readiness flag to 0 because we are NOT ready */
1599 consumer_data->consumer_thread_is_ready = 0;
8c0faa1d 1600
a23ec3a7
DG
1601 /* Setup pthread condition */
1602 ret = pthread_condattr_init(&consumer_data->condattr);
1603 if (ret != 0) {
1604 errno = ret;
1605 PERROR("pthread_condattr_init consumer data");
1606 goto error;
1607 }
1608
1609 /*
1610 * Set the monotonic clock in order to make sure we DO NOT jump in time
1611 * between the clock_gettime() call and the timedwait call. See bug #324
1612 * for a more details and how we noticed it.
1613 */
1614 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
1615 if (ret != 0) {
1616 errno = ret;
1617 PERROR("pthread_condattr_setclock consumer data");
ee0b0061
DG
1618 goto error;
1619 }
8c0faa1d 1620
a23ec3a7
DG
1621 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
1622 if (ret != 0) {
1623 errno = ret;
1624 PERROR("pthread_cond_init consumer data");
1625 goto error;
1626 }
1627
1628 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
1629 consumer_data);
8c0faa1d 1630 if (ret != 0) {
3bd1e081 1631 PERROR("pthread_create consumer");
ee0b0061 1632 ret = -1;
8c0faa1d
DG
1633 goto error;
1634 }
1635
a23ec3a7
DG
1636 /* We are about to wait on a pthread condition */
1637 pthread_mutex_lock(&consumer_data->cond_mutex);
1638
ee0b0061 1639 /* Get time for sem_timedwait absolute timeout */
a23ec3a7
DG
1640 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1641 /*
1642 * Set the timeout for the condition timed wait even if the clock gettime
1643 * call fails since we might loop on that call and we want to avoid to
1644 * increment the timeout too many times.
1645 */
1646 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
1647
1648 /*
1649 * The following loop COULD be skipped in some conditions so this is why we
1650 * set ret to 0 in order to make sure at least one round of the loop is
1651 * done.
1652 */
1653 ret = 0;
1654
1655 /*
1656 * Loop until the condition is reached or when a timeout is reached. Note
1657 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
1658 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
1659 * possible. This loop does not take any chances and works with both of
1660 * them.
1661 */
1662 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
1663 if (clock_ret < 0) {
1664 PERROR("clock_gettime spawn consumer");
1665 /* Infinite wait for the consumerd thread to be ready */
1666 ret = pthread_cond_wait(&consumer_data->cond,
1667 &consumer_data->cond_mutex);
1668 } else {
1669 ret = pthread_cond_timedwait(&consumer_data->cond,
1670 &consumer_data->cond_mutex, &timeout);
1671 }
ee0b0061 1672 }
8c0faa1d 1673
a23ec3a7
DG
1674 /* Release the pthread condition */
1675 pthread_mutex_unlock(&consumer_data->cond_mutex);
1676
1677 if (ret != 0) {
1678 errno = ret;
1679 if (ret == ETIMEDOUT) {
ee0b0061
DG
1680 /*
1681 * Call has timed out so we kill the kconsumerd_thread and return
1682 * an error.
1683 */
a23ec3a7
DG
1684 ERR("Condition timed out. The consumer thread was never ready."
1685 " Killing it");
3bd1e081 1686 ret = pthread_cancel(consumer_data->thread);
ee0b0061 1687 if (ret < 0) {
3bd1e081 1688 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
1689 }
1690 } else {
a23ec3a7 1691 PERROR("pthread_cond_wait failed consumer thread");
ee0b0061
DG
1692 }
1693 goto error;
1694 }
1695
3bd1e081
MD
1696 pthread_mutex_lock(&consumer_data->pid_mutex);
1697 if (consumer_data->pid == 0) {
a23ec3a7 1698 ERR("Consumerd did not start");
3bd1e081 1699 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
1700 goto error;
1701 }
3bd1e081 1702 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 1703
8c0faa1d
DG
1704 return 0;
1705
1706error:
1707 return ret;
1708}
1709
d9800920 1710/*
3bd1e081 1711 * Join consumer thread
d9800920 1712 */
3bd1e081 1713static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
1714{
1715 void *status;
cf3af59e 1716
e8209f6b
DG
1717 /* Consumer pid must be a real one. */
1718 if (consumer_data->pid > 0) {
c617c0c6 1719 int ret;
3bd1e081 1720 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 1721 if (ret) {
3bd1e081 1722 ERR("Error killing consumer daemon");
cf3af59e
MD
1723 return ret;
1724 }
3bd1e081 1725 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
1726 } else {
1727 return 0;
1728 }
1729}
1730
8c0faa1d 1731/*
3bd1e081 1732 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 1733 *
d063d709 1734 * Return pid if successful else -1.
8c0faa1d 1735 */
3bd1e081 1736static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
1737{
1738 int ret;
1739 pid_t pid;
94c55f17 1740 const char *consumer_to_use;
53086306 1741 const char *verbosity;
94c55f17 1742 struct stat st;
8c0faa1d 1743
3bd1e081 1744 DBG("Spawning consumerd");
c49dc785 1745
8c0faa1d
DG
1746 pid = fork();
1747 if (pid == 0) {
1748 /*
3bd1e081 1749 * Exec consumerd.
8c0faa1d 1750 */
daee5345 1751 if (opt_verbose_consumer) {
53086306
DG
1752 verbosity = "--verbose";
1753 } else {
1754 verbosity = "--quiet";
1755 }
3bd1e081
MD
1756 switch (consumer_data->type) {
1757 case LTTNG_CONSUMER_KERNEL:
94c55f17 1758 /*
c7704d57
DG
1759 * Find out which consumerd to execute. We will first try the
1760 * 64-bit path, then the sessiond's installation directory, and
1761 * fallback on the 32-bit one,
94c55f17 1762 */
63a799e8
AM
1763 DBG3("Looking for a kernel consumer at these locations:");
1764 DBG3(" 1) %s", consumerd64_bin);
1765 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1766 DBG3(" 3) %s", consumerd32_bin);
94c55f17 1767 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 1768 DBG3("Found location #1");
94c55f17 1769 consumer_to_use = consumerd64_bin;
94c55f17 1770 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 1771 DBG3("Found location #2");
94c55f17 1772 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 1773 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 1774 DBG3("Found location #3");
eb1e0bd4 1775 consumer_to_use = consumerd32_bin;
94c55f17 1776 } else {
63a799e8 1777 DBG("Could not find any valid consumerd executable");
94c55f17
AM
1778 break;
1779 }
1780 DBG("Using kernel consumer at: %s", consumer_to_use);
1781 execl(consumer_to_use,
1782 "lttng-consumerd", verbosity, "-k",
1783 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1784 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1785 NULL);
3bd1e081 1786 break;
7753dea8
MD
1787 case LTTNG_CONSUMER64_UST:
1788 {
b1e0b6b6 1789 char *tmpnew = NULL;
8f4905da
MD
1790
1791 if (consumerd64_libdir[0] != '\0') {
1792 char *tmp;
1793 size_t tmplen;
1794
1795 tmp = getenv("LD_LIBRARY_PATH");
1796 if (!tmp) {
1797 tmp = "";
1798 }
1799 tmplen = strlen("LD_LIBRARY_PATH=")
1800 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1801 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1802 if (!tmpnew) {
1803 ret = -ENOMEM;
1804 goto error;
1805 }
1806 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1807 strcat(tmpnew, consumerd64_libdir);
1808 if (tmp[0] != '\0') {
1809 strcat(tmpnew, ":");
1810 strcat(tmpnew, tmp);
1811 }
1812 ret = putenv(tmpnew);
1813 if (ret) {
1814 ret = -errno;
1815 goto error;
1816 }
1817 }
94c55f17 1818 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 1819 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1820 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1821 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1822 NULL);
8f4905da
MD
1823 if (consumerd64_libdir[0] != '\0') {
1824 free(tmpnew);
1825 }
1826 if (ret) {
1827 goto error;
1828 }
3bd1e081 1829 break;
7753dea8
MD
1830 }
1831 case LTTNG_CONSUMER32_UST:
1832 {
937dde8e 1833 char *tmpnew = NULL;
8f4905da
MD
1834
1835 if (consumerd32_libdir[0] != '\0') {
1836 char *tmp;
1837 size_t tmplen;
1838
1839 tmp = getenv("LD_LIBRARY_PATH");
1840 if (!tmp) {
1841 tmp = "";
1842 }
1843 tmplen = strlen("LD_LIBRARY_PATH=")
1844 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1845 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1846 if (!tmpnew) {
1847 ret = -ENOMEM;
1848 goto error;
1849 }
1850 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1851 strcat(tmpnew, consumerd32_libdir);
1852 if (tmp[0] != '\0') {
1853 strcat(tmpnew, ":");
1854 strcat(tmpnew, tmp);
1855 }
1856 ret = putenv(tmpnew);
1857 if (ret) {
1858 ret = -errno;
1859 goto error;
1860 }
1861 }
94c55f17 1862 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 1863 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1864 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1865 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1866 NULL);
8f4905da
MD
1867 if (consumerd32_libdir[0] != '\0') {
1868 free(tmpnew);
1869 }
1870 if (ret) {
1871 goto error;
1872 }
7753dea8
MD
1873 break;
1874 }
3bd1e081 1875 default:
76d7553f 1876 PERROR("unknown consumer type");
3bd1e081
MD
1877 exit(EXIT_FAILURE);
1878 }
8c0faa1d 1879 if (errno != 0) {
76d7553f 1880 PERROR("kernel start consumer exec");
8c0faa1d
DG
1881 }
1882 exit(EXIT_FAILURE);
1883 } else if (pid > 0) {
1884 ret = pid;
8c0faa1d 1885 } else {
76d7553f 1886 PERROR("start consumer fork");
8c0faa1d 1887 ret = -errno;
8c0faa1d 1888 }
8f4905da 1889error:
8c0faa1d
DG
1890 return ret;
1891}
1892
693bd40b 1893/*
3bd1e081 1894 * Spawn the consumerd daemon and session daemon thread.
693bd40b 1895 */
3bd1e081 1896static int start_consumerd(struct consumer_data *consumer_data)
693bd40b 1897{
c617c0c6 1898 int ret;
edb8b045
DG
1899
1900 /*
1901 * Set the listen() state on the socket since there is a possible race
1902 * between the exec() of the consumer daemon and this call if place in the
1903 * consumer thread. See bug #366 for more details.
1904 */
1905 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
1906 if (ret < 0) {
1907 goto error;
1908 }
693bd40b 1909
3bd1e081
MD
1910 pthread_mutex_lock(&consumer_data->pid_mutex);
1911 if (consumer_data->pid != 0) {
1912 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
1913 goto end;
1914 }
693bd40b 1915
3bd1e081 1916 ret = spawn_consumerd(consumer_data);
c49dc785 1917 if (ret < 0) {
3bd1e081
MD
1918 ERR("Spawning consumerd failed");
1919 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 1920 goto error;
693bd40b 1921 }
c49dc785 1922
3bd1e081
MD
1923 /* Setting up the consumer_data pid */
1924 consumer_data->pid = ret;
48842b30 1925 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 1926 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 1927
3bd1e081
MD
1928 DBG2("Spawning consumer control thread");
1929 ret = spawn_consumer_thread(consumer_data);
693bd40b 1930 if (ret < 0) {
3bd1e081 1931 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
1932 goto error;
1933 }
1934
c49dc785 1935end:
693bd40b
DG
1936 return 0;
1937
1938error:
edb8b045
DG
1939 /* Cleanup already created socket on error. */
1940 if (consumer_data->err_sock >= 0) {
c617c0c6
MD
1941 int err;
1942
edb8b045
DG
1943 err = close(consumer_data->err_sock);
1944 if (err < 0) {
1945 PERROR("close consumer data error socket");
1946 }
1947 }
693bd40b
DG
1948 return ret;
1949}
1950
44a5e5eb 1951/*
139ac872
MD
1952 * Compute health status of each consumer. If one of them is zero (bad
1953 * state), we return 0.
44a5e5eb
DG
1954 */
1955static int check_consumer_health(void)
1956{
1957 int ret;
1958
927ca06a 1959 ret = health_check_state(HEALTH_TYPE_CONSUMER);
44a5e5eb
DG
1960
1961 DBG3("Health consumer check %d", ret);
1962
1963 return ret;
1964}
1965
b73401da 1966/*
096102bd 1967 * Setup necessary data for kernel tracer action.
b73401da 1968 */
096102bd 1969static int init_kernel_tracer(void)
b73401da
DG
1970{
1971 int ret;
b73401da 1972
096102bd
DG
1973 /* Modprobe lttng kernel modules */
1974 ret = modprobe_lttng_control();
b73401da 1975 if (ret < 0) {
b73401da
DG
1976 goto error;
1977 }
1978
096102bd
DG
1979 /* Open debugfs lttng */
1980 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1981 if (kernel_tracer_fd < 0) {
1982 DBG("Failed to open %s", module_proc_lttng);
2f77fc4b
DG
1983 ret = -1;
1984 goto error_open;
54d01ffb
DG
1985 }
1986
2f77fc4b
DG
1987 /* Validate kernel version */
1988 ret = kernel_validate_version(kernel_tracer_fd);
1989 if (ret < 0) {
1990 goto error_version;
b551a063 1991 }
54d01ffb 1992
2f77fc4b
DG
1993 ret = modprobe_lttng_data();
1994 if (ret < 0) {
1995 goto error_modules;
54d01ffb
DG
1996 }
1997
2f77fc4b
DG
1998 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1999 return 0;
2000
2001error_version:
2002 modprobe_remove_lttng_control();
2003 ret = close(kernel_tracer_fd);
2004 if (ret) {
2005 PERROR("close");
b551a063 2006 }
2f77fc4b 2007 kernel_tracer_fd = -1;
f73fabfd 2008 return LTTNG_ERR_KERN_VERSION;
b551a063 2009
2f77fc4b
DG
2010error_modules:
2011 ret = close(kernel_tracer_fd);
2012 if (ret) {
2013 PERROR("close");
b551a063 2014 }
54d01ffb 2015
2f77fc4b
DG
2016error_open:
2017 modprobe_remove_lttng_control();
54d01ffb
DG
2018
2019error:
2f77fc4b
DG
2020 WARN("No kernel tracer available");
2021 kernel_tracer_fd = -1;
2022 if (!is_root) {
f73fabfd 2023 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2f77fc4b 2024 } else {
f73fabfd 2025 return LTTNG_ERR_KERN_NA;
2f77fc4b 2026 }
54d01ffb
DG
2027}
2028
2f77fc4b 2029
54d01ffb 2030/*
2f77fc4b
DG
2031 * Copy consumer output from the tracing session to the domain session. The
2032 * function also applies the right modification on a per domain basis for the
2033 * trace files destination directory.
54d01ffb 2034 */
2f77fc4b 2035static int copy_session_consumer(int domain, struct ltt_session *session)
54d01ffb
DG
2036{
2037 int ret;
2f77fc4b
DG
2038 const char *dir_name;
2039 struct consumer_output *consumer;
2040
2041 assert(session);
2042 assert(session->consumer);
54d01ffb 2043
b551a063
DG
2044 switch (domain) {
2045 case LTTNG_DOMAIN_KERNEL:
2f77fc4b 2046 DBG3("Copying tracing session consumer output in kernel session");
09a90bcd
DG
2047 /*
2048 * XXX: We should audit the session creation and what this function
2049 * does "extra" in order to avoid a destroy since this function is used
2050 * in the domain session creation (kernel and ust) only. Same for UST
2051 * domain.
2052 */
2053 if (session->kernel_session->consumer) {
2054 consumer_destroy_output(session->kernel_session->consumer);
2055 }
2f77fc4b
DG
2056 session->kernel_session->consumer =
2057 consumer_copy_output(session->consumer);
2058 /* Ease our life a bit for the next part */
2059 consumer = session->kernel_session->consumer;
2060 dir_name = DEFAULT_KERNEL_TRACE_DIR;
b551a063
DG
2061 break;
2062 case LTTNG_DOMAIN_UST:
2f77fc4b 2063 DBG3("Copying tracing session consumer output in UST session");
09a90bcd
DG
2064 if (session->ust_session->consumer) {
2065 consumer_destroy_output(session->ust_session->consumer);
2066 }
2f77fc4b
DG
2067 session->ust_session->consumer =
2068 consumer_copy_output(session->consumer);
2069 /* Ease our life a bit for the next part */
2070 consumer = session->ust_session->consumer;
2071 dir_name = DEFAULT_UST_TRACE_DIR;
b551a063
DG
2072 break;
2073 default:
f73fabfd 2074 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
54d01ffb
DG
2075 goto error;
2076 }
2077
2f77fc4b 2078 /* Append correct directory to subdir */
c30ce0b3
CB
2079 strncat(consumer->subdir, dir_name,
2080 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
2081 DBG3("Copy session consumer subdir %s", consumer->subdir);
2082
f73fabfd 2083 ret = LTTNG_OK;
54d01ffb
DG
2084
2085error:
2086 return ret;
2087}
2088
00e2e675 2089/*
2f77fc4b 2090 * Create an UST session and add it to the session ust list.
00e2e675 2091 */
2f77fc4b
DG
2092static int create_ust_session(struct ltt_session *session,
2093 struct lttng_domain *domain)
00e2e675
DG
2094{
2095 int ret;
2f77fc4b 2096 struct ltt_ust_session *lus = NULL;
00e2e675 2097
a4b92340 2098 assert(session);
2f77fc4b
DG
2099 assert(domain);
2100 assert(session->consumer);
a4b92340 2101
2f77fc4b
DG
2102 switch (domain->type) {
2103 case LTTNG_DOMAIN_UST:
2104 break;
2105 default:
2106 ERR("Unknown UST domain on create session %d", domain->type);
f73fabfd 2107 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
00e2e675
DG
2108 goto error;
2109 }
2110
2f77fc4b
DG
2111 DBG("Creating UST session");
2112
fdd9eb17 2113 lus = trace_ust_create_session(session->path, session->id);
2f77fc4b 2114 if (lus == NULL) {
f73fabfd 2115 ret = LTTNG_ERR_UST_SESS_FAIL;
a4b92340
DG
2116 goto error;
2117 }
2118
2f77fc4b
DG
2119 lus->uid = session->uid;
2120 lus->gid = session->gid;
2121 session->ust_session = lus;
00e2e675 2122
2f77fc4b
DG
2123 /* Copy session output to the newly created UST session */
2124 ret = copy_session_consumer(domain->type, session);
f73fabfd 2125 if (ret != LTTNG_OK) {
00e2e675
DG
2126 goto error;
2127 }
2128
f73fabfd 2129 return LTTNG_OK;
00e2e675
DG
2130
2131error:
2f77fc4b
DG
2132 free(lus);
2133 session->ust_session = NULL;
00e2e675
DG
2134 return ret;
2135}
2136
2137/*
2f77fc4b 2138 * Create a kernel tracer session then create the default channel.
00e2e675 2139 */
2f77fc4b 2140static int create_kernel_session(struct ltt_session *session)
00e2e675
DG
2141{
2142 int ret;
a4b92340 2143
2f77fc4b 2144 DBG("Creating kernel session");
00e2e675 2145
2f77fc4b
DG
2146 ret = kernel_create_session(session, kernel_tracer_fd);
2147 if (ret < 0) {
f73fabfd 2148 ret = LTTNG_ERR_KERN_SESS_FAIL;
00e2e675
DG
2149 goto error;
2150 }
2151
2f77fc4b
DG
2152 /* Code flow safety */
2153 assert(session->kernel_session);
2154
2155 /* Copy session output to the newly created Kernel session */
2156 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
f73fabfd 2157 if (ret != LTTNG_OK) {
a4b92340
DG
2158 goto error;
2159 }
2160
2f77fc4b
DG
2161 /* Create directory(ies) on local filesystem. */
2162 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2163 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2164 ret = run_as_mkdir_recursive(
2165 session->kernel_session->consumer->dst.trace_path,
2166 S_IRWXU | S_IRWXG, session->uid, session->gid);
2167 if (ret < 0) {
2168 if (ret != -EEXIST) {
2169 ERR("Trace directory creation error");
00e2e675
DG
2170 goto error;
2171 }
00e2e675 2172 }
2f77fc4b 2173 }
00e2e675 2174
2f77fc4b
DG
2175 session->kernel_session->uid = session->uid;
2176 session->kernel_session->gid = session->gid;
00e2e675 2177
f73fabfd 2178 return LTTNG_OK;
00e2e675 2179
2f77fc4b
DG
2180error:
2181 trace_kernel_destroy_session(session->kernel_session);
2182 session->kernel_session = NULL;
2183 return ret;
2184}
00e2e675 2185
2f77fc4b
DG
2186/*
2187 * Count number of session permitted by uid/gid.
2188 */
2189static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2190{
2191 unsigned int i = 0;
2192 struct ltt_session *session;
07424f16 2193
2f77fc4b
DG
2194 DBG("Counting number of available session for UID %d GID %d",
2195 uid, gid);
2196 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
00e2e675 2197 /*
2f77fc4b 2198 * Only list the sessions the user can control.
00e2e675 2199 */
2f77fc4b
DG
2200 if (!session_access_ok(session, uid, gid)) {
2201 continue;
2202 }
2203 i++;
a4b92340 2204 }
2f77fc4b 2205 return i;
00e2e675
DG
2206}
2207
54d01ffb
DG
2208/*
2209 * Process the command requested by the lttng client within the command
2210 * context structure. This function make sure that the return structure (llm)
2211 * is set and ready for transmission before returning.
2212 *
2213 * Return any error encountered or 0 for success.
53a80697
MD
2214 *
2215 * "sock" is only used for special-case var. len data.
54d01ffb 2216 */
53a80697
MD
2217static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2218 int *sock_error)
54d01ffb 2219{
f73fabfd 2220 int ret = LTTNG_OK;
44d3bd01 2221 int need_tracing_session = 1;
2e09ba09 2222 int need_domain;
54d01ffb
DG
2223
2224 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2225
53a80697
MD
2226 *sock_error = 0;
2227
2e09ba09
MD
2228 switch (cmd_ctx->lsm->cmd_type) {
2229 case LTTNG_CREATE_SESSION:
2230 case LTTNG_DESTROY_SESSION:
2231 case LTTNG_LIST_SESSIONS:
2232 case LTTNG_LIST_DOMAINS:
2233 case LTTNG_START_TRACE:
2234 case LTTNG_STOP_TRACE:
6d805429 2235 case LTTNG_DATA_PENDING:
2e09ba09 2236 need_domain = 0;
3aace903 2237 break;
2e09ba09
MD
2238 default:
2239 need_domain = 1;
2240 }
2241
2242 if (opt_no_kernel && need_domain
2243 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9 2244 if (!is_root) {
f73fabfd 2245 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
531d29f9 2246 } else {
f73fabfd 2247 ret = LTTNG_ERR_KERN_NA;
531d29f9 2248 }
4fba7219
DG
2249 goto error;
2250 }
2251
8d3113b2
DG
2252 /* Deny register consumer if we already have a spawned consumer. */
2253 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2254 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2255 if (kconsumer_data.pid > 0) {
f73fabfd 2256 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
fa317f24 2257 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
8d3113b2
DG
2258 goto error;
2259 }
2260 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2261 }
2262
54d01ffb
DG
2263 /*
2264 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 2265 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
2266 * command.
2267 */
2268 switch(cmd_ctx->lsm->cmd_type) {
2269 case LTTNG_LIST_SESSIONS:
2270 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2271 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
2272 case LTTNG_LIST_DOMAINS:
2273 case LTTNG_LIST_CHANNELS:
2274 case LTTNG_LIST_EVENTS:
2275 break;
2276 default:
2277 /* Setup lttng message with no payload */
2278 ret = setup_lttng_msg(cmd_ctx, 0);
2279 if (ret < 0) {
2280 /* This label does not try to unlock the session */
2281 goto init_setup_error;
2282 }
2283 }
2284
2285 /* Commands that DO NOT need a session. */
2286 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 2287 case LTTNG_CREATE_SESSION:
2e09ba09 2288 case LTTNG_CALIBRATE:
54d01ffb
DG
2289 case LTTNG_LIST_SESSIONS:
2290 case LTTNG_LIST_TRACEPOINTS:
f37d259d 2291 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 2292 need_tracing_session = 0;
54d01ffb
DG
2293 break;
2294 default:
2295 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
2296 /*
2297 * We keep the session list lock across _all_ commands
2298 * for now, because the per-session lock does not
2299 * handle teardown properly.
2300 */
74babd95 2301 session_lock_list();
54d01ffb
DG
2302 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2303 if (cmd_ctx->session == NULL) {
2304 if (cmd_ctx->lsm->session.name != NULL) {
f73fabfd 2305 ret = LTTNG_ERR_SESS_NOT_FOUND;
54d01ffb
DG
2306 } else {
2307 /* If no session name specified */
f73fabfd 2308 ret = LTTNG_ERR_SELECT_SESS;
54d01ffb
DG
2309 }
2310 goto error;
2311 } else {
2312 /* Acquire lock for the session */
2313 session_lock(cmd_ctx->session);
2314 }
2315 break;
2316 }
b389abbe 2317
2e09ba09
MD
2318 if (!need_domain) {
2319 goto skip_domain;
2320 }
a4b92340 2321
54d01ffb
DG
2322 /*
2323 * Check domain type for specific "pre-action".
2324 */
2325 switch (cmd_ctx->lsm->domain.type) {
2326 case LTTNG_DOMAIN_KERNEL:
d1f1c568 2327 if (!is_root) {
f73fabfd 2328 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
d1f1c568
DG
2329 goto error;
2330 }
2331
54d01ffb 2332 /* Kernel tracer check */
a4b35e07 2333 if (kernel_tracer_fd == -1) {
54d01ffb 2334 /* Basically, load kernel tracer modules */
096102bd
DG
2335 ret = init_kernel_tracer();
2336 if (ret != 0) {
54d01ffb
DG
2337 goto error;
2338 }
2339 }
5eb91c98 2340
5c827ce0
DG
2341 /* Consumer is in an ERROR state. Report back to client */
2342 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2343 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2344 goto error;
2345 }
2346
54d01ffb 2347 /* Need a session for kernel command */
44d3bd01 2348 if (need_tracing_session) {
54d01ffb 2349 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 2350 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 2351 if (ret < 0) {
f73fabfd 2352 ret = LTTNG_ERR_KERN_SESS_FAIL;
5eb91c98
DG
2353 goto error;
2354 }
b389abbe 2355 }
7d29a247 2356
54d01ffb 2357 /* Start the kernel consumer daemon */
3bd1e081
MD
2358 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2359 if (kconsumer_data.pid == 0 &&
a4b92340
DG
2360 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2361 cmd_ctx->session->start_consumer) {
3bd1e081
MD
2362 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2363 ret = start_consumerd(&kconsumer_data);
7d29a247 2364 if (ret < 0) {
f73fabfd 2365 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
54d01ffb 2366 goto error;
950131af 2367 }
5c827ce0 2368 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
2369 } else {
2370 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 2371 }
173af62f 2372
a4b92340
DG
2373 /*
2374 * The consumer was just spawned so we need to add the socket to
2375 * the consumer output of the session if exist.
2376 */
2377 ret = consumer_create_socket(&kconsumer_data,
2378 cmd_ctx->session->kernel_session->consumer);
2379 if (ret < 0) {
2380 goto error;
173af62f 2381 }
0d0c377a 2382 }
5c827ce0 2383
54d01ffb 2384 break;
2bdd86d4 2385 case LTTNG_DOMAIN_UST:
44d3bd01 2386 {
5c827ce0
DG
2387 /* Consumer is in an ERROR state. Report back to client */
2388 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
f73fabfd 2389 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2390 goto error;
2391 }
2392
44d3bd01 2393 if (need_tracing_session) {
a4b92340 2394 /* Create UST session if none exist. */
f6a9efaa 2395 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 2396 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 2397 &cmd_ctx->lsm->domain);
f73fabfd 2398 if (ret != LTTNG_OK) {
44d3bd01
DG
2399 goto error;
2400 }
2401 }
00e2e675 2402
7753dea8
MD
2403 /* Start the UST consumer daemons */
2404 /* 64-bit */
2405 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 2406 if (consumerd64_bin[0] != '\0' &&
7753dea8 2407 ustconsumer64_data.pid == 0 &&
a4b92340
DG
2408 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2409 cmd_ctx->session->start_consumer) {
7753dea8
MD
2410 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2411 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 2412 if (ret < 0) {
f73fabfd 2413 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
173af62f 2414 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
2415 goto error;
2416 }
48842b30 2417
173af62f 2418 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 2419 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 2420 } else {
7753dea8
MD
2421 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2422 }
173af62f
DG
2423
2424 /*
2425 * Setup socket for consumer 64 bit. No need for atomic access
2426 * since it was set above and can ONLY be set in this thread.
2427 */
a4b92340
DG
2428 ret = consumer_create_socket(&ustconsumer64_data,
2429 cmd_ctx->session->ust_session->consumer);
2430 if (ret < 0) {
2431 goto error;
173af62f
DG
2432 }
2433
7753dea8 2434 /* 32-bit */
fc7a59ce 2435 if (consumerd32_bin[0] != '\0' &&
7753dea8 2436 ustconsumer32_data.pid == 0 &&
a4b92340
DG
2437 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
2438 cmd_ctx->session->start_consumer) {
7753dea8
MD
2439 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2440 ret = start_consumerd(&ustconsumer32_data);
2441 if (ret < 0) {
f73fabfd 2442 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
173af62f 2443 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
2444 goto error;
2445 }
5c827ce0 2446
173af62f 2447 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 2448 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
2449 } else {
2450 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 2451 }
173af62f
DG
2452
2453 /*
2454 * Setup socket for consumer 64 bit. No need for atomic access
2455 * since it was set above and can ONLY be set in this thread.
2456 */
a4b92340
DG
2457 ret = consumer_create_socket(&ustconsumer32_data,
2458 cmd_ctx->session->ust_session->consumer);
2459 if (ret < 0) {
2460 goto error;
173af62f 2461 }
44d3bd01
DG
2462 }
2463 break;
48842b30 2464 }
54d01ffb 2465 default:
54d01ffb
DG
2466 break;
2467 }
2e09ba09 2468skip_domain:
33a2b854 2469
5c827ce0
DG
2470 /* Validate consumer daemon state when start/stop trace command */
2471 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2472 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2473 switch (cmd_ctx->lsm->domain.type) {
2474 case LTTNG_DOMAIN_UST:
2475 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2476 ret = LTTNG_ERR_NO_USTCONSUMERD;
5c827ce0
DG
2477 goto error;
2478 }
2479 break;
2480 case LTTNG_DOMAIN_KERNEL:
2481 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
f73fabfd 2482 ret = LTTNG_ERR_NO_KERNCONSUMERD;
5c827ce0
DG
2483 goto error;
2484 }
2485 break;
2486 }
2487 }
2488
8e0af1b4
MD
2489 /*
2490 * Check that the UID or GID match that of the tracing session.
2491 * The root user can interact with all sessions.
2492 */
2493 if (need_tracing_session) {
2494 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
2495 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2496 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
f73fabfd 2497 ret = LTTNG_ERR_EPERM;
8e0af1b4
MD
2498 goto error;
2499 }
2500 }
2501
54d01ffb
DG
2502 /* Process by command type */
2503 switch (cmd_ctx->lsm->cmd_type) {
2504 case LTTNG_ADD_CONTEXT:
2505 {
2506 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2507 cmd_ctx->lsm->u.context.channel_name,
979e618e 2508 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
54d01ffb
DG
2509 break;
2510 }
2511 case LTTNG_DISABLE_CHANNEL:
2512 {
2513 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2514 cmd_ctx->lsm->u.disable.channel_name);
2515 break;
2516 }
2517 case LTTNG_DISABLE_EVENT:
2518 {
2519 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2520 cmd_ctx->lsm->u.disable.channel_name,
2521 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
2522 break;
2523 }
54d01ffb
DG
2524 case LTTNG_DISABLE_ALL_EVENT:
2525 {
2bdd86d4 2526 DBG("Disabling all events");
54d01ffb
DG
2527
2528 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2529 cmd_ctx->lsm->u.disable.channel_name);
2530 break;
2531 }
00e2e675
DG
2532 case LTTNG_DISABLE_CONSUMER:
2533 {
2534 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
2535 break;
2536 }
54d01ffb
DG
2537 case LTTNG_ENABLE_CHANNEL:
2538 {
aea829b3 2539 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 2540 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
54d01ffb
DG
2541 break;
2542 }
00e2e675
DG
2543 case LTTNG_ENABLE_CONSUMER:
2544 {
a4b92340
DG
2545 /*
2546 * XXX: 0 means that this URI should be applied on the session. Should
2547 * be a DOMAIN enuam.
2548 */
00e2e675 2549 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
f73fabfd 2550 if (ret != LTTNG_OK) {
a4b92340
DG
2551 goto error;
2552 }
2553
2554 if (cmd_ctx->lsm->domain.type == 0) {
2555 /* Add the URI for the UST session if a consumer is present. */
2556 if (cmd_ctx->session->ust_session &&
2557 cmd_ctx->session->ust_session->consumer) {
2558 ret = cmd_enable_consumer(LTTNG_DOMAIN_UST, cmd_ctx->session);
2559 } else if (cmd_ctx->session->kernel_session &&
2560 cmd_ctx->session->kernel_session->consumer) {
2561 ret = cmd_enable_consumer(LTTNG_DOMAIN_KERNEL,
2562 cmd_ctx->session);
2563 }
2564 }
00e2e675
DG
2565 break;
2566 }
54d01ffb
DG
2567 case LTTNG_ENABLE_EVENT:
2568 {
2569 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2570 cmd_ctx->lsm->u.enable.channel_name,
025faf73 2571 &cmd_ctx->lsm->u.enable.event, NULL, kernel_poll_pipe[1]);
54d01ffb
DG
2572 break;
2573 }
2574 case LTTNG_ENABLE_ALL_EVENT:
2575 {
2bdd86d4 2576 DBG("Enabling all events");
54d01ffb
DG
2577
2578 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
8c9ae521 2579 cmd_ctx->lsm->u.enable.channel_name,
025faf73 2580 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
54d01ffb
DG
2581 break;
2582 }
052da939 2583 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 2584 {
9f19cc17 2585 struct lttng_event *events;
54d01ffb 2586 ssize_t nb_events;
052da939 2587
54d01ffb
DG
2588 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
2589 if (nb_events < 0) {
f73fabfd 2590 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2591 ret = -nb_events;
2592 goto error;
2ef84c95
DG
2593 }
2594
2595 /*
2596 * Setup lttng message with payload size set to the event list size in
2597 * bytes and then copy list into the llm payload.
2598 */
052da939 2599 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 2600 if (ret < 0) {
052da939 2601 free(events);
2ef84c95
DG
2602 goto setup_error;
2603 }
2604
2605 /* Copy event list into message payload */
9f19cc17 2606 memcpy(cmd_ctx->llm->payload, events,
052da939 2607 sizeof(struct lttng_event) * nb_events);
2ef84c95 2608
9f19cc17 2609 free(events);
2ef84c95 2610
f73fabfd 2611 ret = LTTNG_OK;
2ef84c95
DG
2612 break;
2613 }
f37d259d
MD
2614 case LTTNG_LIST_TRACEPOINT_FIELDS:
2615 {
2616 struct lttng_event_field *fields;
2617 ssize_t nb_fields;
2618
a4b92340
DG
2619 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
2620 &fields);
f37d259d 2621 if (nb_fields < 0) {
f73fabfd 2622 /* Return value is a negative lttng_error_code. */
f37d259d
MD
2623 ret = -nb_fields;
2624 goto error;
2625 }
2626
2627 /*
2628 * Setup lttng message with payload size set to the event list size in
2629 * bytes and then copy list into the llm payload.
2630 */
a4b92340
DG
2631 ret = setup_lttng_msg(cmd_ctx,
2632 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
2633 if (ret < 0) {
2634 free(fields);
2635 goto setup_error;
2636 }
2637
2638 /* Copy event list into message payload */
2639 memcpy(cmd_ctx->llm->payload, fields,
2640 sizeof(struct lttng_event_field) * nb_fields);
2641
2642 free(fields);
2643
f73fabfd 2644 ret = LTTNG_OK;
f37d259d
MD
2645 break;
2646 }
00e2e675
DG
2647 case LTTNG_SET_CONSUMER_URI:
2648 {
a4b92340
DG
2649 size_t nb_uri, len;
2650 struct lttng_uri *uris;
2651
2652 nb_uri = cmd_ctx->lsm->u.uri.size;
2653 len = nb_uri * sizeof(struct lttng_uri);
2654
2655 if (nb_uri == 0) {
f73fabfd 2656 ret = LTTNG_ERR_INVALID;
a4b92340
DG
2657 goto error;
2658 }
2659
2660 uris = zmalloc(len);
2661 if (uris == NULL) {
f73fabfd 2662 ret = LTTNG_ERR_FATAL;
a4b92340
DG
2663 goto error;
2664 }
2665
2666 /* Receive variable len data */
77c7c900 2667 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
2668 ret = lttcomm_recv_unix_sock(sock, uris, len);
2669 if (ret <= 0) {
2670 DBG("No URIs received from client... continuing");
2671 *sock_error = 1;
f73fabfd 2672 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 2673 free(uris);
a4b92340
DG
2674 goto error;
2675 }
2676
00e2e675 2677 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
a4b92340 2678 nb_uri, uris);
f73fabfd 2679 if (ret != LTTNG_OK) {
b1d41407 2680 free(uris);
a4b92340
DG
2681 goto error;
2682 }
2683
2684 /*
2685 * XXX: 0 means that this URI should be applied on the session. Should
2686 * be a DOMAIN enuam.
2687 */
2688 if (cmd_ctx->lsm->domain.type == 0) {
2689 /* Add the URI for the UST session if a consumer is present. */
2690 if (cmd_ctx->session->ust_session &&
2691 cmd_ctx->session->ust_session->consumer) {
2692 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
2693 nb_uri, uris);
2694 } else if (cmd_ctx->session->kernel_session &&
2695 cmd_ctx->session->kernel_session->consumer) {
2696 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
2697 cmd_ctx->session, nb_uri, uris);
2698 }
2699 }
2700
b1d41407
DG
2701 free(uris);
2702
00e2e675
DG
2703 break;
2704 }
f3ed775e 2705 case LTTNG_START_TRACE:
8c0faa1d 2706 {
54d01ffb 2707 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
2708 break;
2709 }
f3ed775e 2710 case LTTNG_STOP_TRACE:
8c0faa1d 2711 {
54d01ffb 2712 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
2713 break;
2714 }
5e16da05
MD
2715 case LTTNG_CREATE_SESSION:
2716 {
a4b92340
DG
2717 size_t nb_uri, len;
2718 struct lttng_uri *uris = NULL;
2719
2720 nb_uri = cmd_ctx->lsm->u.uri.size;
2721 len = nb_uri * sizeof(struct lttng_uri);
2722
2723 if (nb_uri > 0) {
2724 uris = zmalloc(len);
2725 if (uris == NULL) {
f73fabfd 2726 ret = LTTNG_ERR_FATAL;
a4b92340
DG
2727 goto error;
2728 }
2729
2730 /* Receive variable len data */
77c7c900 2731 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
2732 ret = lttcomm_recv_unix_sock(sock, uris, len);
2733 if (ret <= 0) {
2734 DBG("No URIs received from client... continuing");
2735 *sock_error = 1;
f73fabfd 2736 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 2737 free(uris);
a4b92340
DG
2738 goto error;
2739 }
2740
2741 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
2742 DBG("Creating session with ONE network URI is a bad call");
f73fabfd 2743 ret = LTTNG_ERR_SESSION_FAIL;
b1d41407 2744 free(uris);
a4b92340
DG
2745 goto error;
2746 }
2747 }
2748
2749 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
2750 &cmd_ctx->creds);
2751
b1d41407
DG
2752 free(uris);
2753
00e2e675
DG
2754 break;
2755 }
5e16da05
MD
2756 case LTTNG_DESTROY_SESSION:
2757 {
2f77fc4b 2758 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
a4b92340
DG
2759
2760 /* Set session to NULL so we do not unlock it after free. */
256a5576 2761 cmd_ctx->session = NULL;
5461b305 2762 break;
5e16da05 2763 }
9f19cc17 2764 case LTTNG_LIST_DOMAINS:
5e16da05 2765 {
54d01ffb
DG
2766 ssize_t nb_dom;
2767 struct lttng_domain *domains;
5461b305 2768
54d01ffb
DG
2769 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
2770 if (nb_dom < 0) {
f73fabfd 2771 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2772 ret = -nb_dom;
2773 goto error;
ce3d728c 2774 }
520ff687 2775
54d01ffb 2776 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305
DG
2777 if (ret < 0) {
2778 goto setup_error;
520ff687 2779 }
57167058 2780
54d01ffb
DG
2781 /* Copy event list into message payload */
2782 memcpy(cmd_ctx->llm->payload, domains,
2783 nb_dom * sizeof(struct lttng_domain));
2784
2785 free(domains);
5e16da05 2786
f73fabfd 2787 ret = LTTNG_OK;
5e16da05
MD
2788 break;
2789 }
9f19cc17 2790 case LTTNG_LIST_CHANNELS:
5e16da05 2791 {
6775595e 2792 int nb_chan;
54d01ffb
DG
2793 struct lttng_channel *channels;
2794
b551a063
DG
2795 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
2796 cmd_ctx->session, &channels);
54d01ffb 2797 if (nb_chan < 0) {
f73fabfd 2798 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2799 ret = -nb_chan;
2800 goto error;
5461b305 2801 }
ca95a216 2802
54d01ffb 2803 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305
DG
2804 if (ret < 0) {
2805 goto setup_error;
2806 }
9f19cc17 2807
54d01ffb
DG
2808 /* Copy event list into message payload */
2809 memcpy(cmd_ctx->llm->payload, channels,
2810 nb_chan * sizeof(struct lttng_channel));
2811
2812 free(channels);
9f19cc17 2813
f73fabfd 2814 ret = LTTNG_OK;
5461b305 2815 break;
5e16da05 2816 }
9f19cc17 2817 case LTTNG_LIST_EVENTS:
5e16da05 2818 {
b551a063 2819 ssize_t nb_event;
684d34d2 2820 struct lttng_event *events = NULL;
9f19cc17 2821
b551a063 2822 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
2823 cmd_ctx->lsm->u.list.channel_name, &events);
2824 if (nb_event < 0) {
f73fabfd 2825 /* Return value is a negative lttng_error_code. */
54d01ffb
DG
2826 ret = -nb_event;
2827 goto error;
5461b305 2828 }
ca95a216 2829
54d01ffb 2830 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305
DG
2831 if (ret < 0) {
2832 goto setup_error;
2833 }
9f19cc17 2834
54d01ffb
DG
2835 /* Copy event list into message payload */
2836 memcpy(cmd_ctx->llm->payload, events,
2837 nb_event * sizeof(struct lttng_event));
9f19cc17 2838
54d01ffb 2839 free(events);
9f19cc17 2840
f73fabfd 2841 ret = LTTNG_OK;
5461b305 2842 break;
5e16da05
MD
2843 }
2844 case LTTNG_LIST_SESSIONS:
2845 {
8e0af1b4 2846 unsigned int nr_sessions;
5461b305 2847
8e0af1b4 2848 session_lock_list();
730389d9
DG
2849 nr_sessions = lttng_sessions_count(
2850 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2851 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 2852
8e0af1b4 2853 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 2854 if (ret < 0) {
54d01ffb 2855 session_unlock_list();
5461b305 2856 goto setup_error;
e065084a 2857 }
5e16da05 2858
6c9cc2ab 2859 /* Filled the session array */
2f77fc4b 2860 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
2861 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2862 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 2863
54d01ffb 2864 session_unlock_list();
5e16da05 2865
f73fabfd 2866 ret = LTTNG_OK;
5e16da05
MD
2867 break;
2868 }
d0254c7c
MD
2869 case LTTNG_CALIBRATE:
2870 {
54d01ffb
DG
2871 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
2872 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
2873 break;
2874 }
d9800920
DG
2875 case LTTNG_REGISTER_CONSUMER:
2876 {
2f77fc4b
DG
2877 struct consumer_data *cdata;
2878
2879 switch (cmd_ctx->lsm->domain.type) {
2880 case LTTNG_DOMAIN_KERNEL:
2881 cdata = &kconsumer_data;
2882 break;
2883 default:
f73fabfd 2884 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2885 goto error;
2886 }
2887
54d01ffb 2888 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2f77fc4b 2889 cmd_ctx->lsm->u.reg.path, cdata);
d9800920
DG
2890 break;
2891 }
025faf73 2892 case LTTNG_ENABLE_EVENT_WITH_FILTER:
53a80697
MD
2893 {
2894 struct lttng_filter_bytecode *bytecode;
2895
025faf73 2896 if (cmd_ctx->lsm->u.enable.bytecode_len > LTTNG_FILTER_MAX_LEN) {
f73fabfd 2897 ret = LTTNG_ERR_FILTER_INVAL;
53a80697
MD
2898 goto error;
2899 }
52df2401
MD
2900 if (cmd_ctx->lsm->u.enable.bytecode_len == 0) {
2901 ret = LTTNG_ERR_FILTER_INVAL;
2902 goto error;
2903 }
025faf73 2904 bytecode = zmalloc(cmd_ctx->lsm->u.enable.bytecode_len);
53a80697 2905 if (!bytecode) {
f73fabfd 2906 ret = LTTNG_ERR_FILTER_NOMEM;
53a80697
MD
2907 goto error;
2908 }
2909 /* Receive var. len. data */
2910 DBG("Receiving var len data from client ...");
2911 ret = lttcomm_recv_unix_sock(sock, bytecode,
025faf73 2912 cmd_ctx->lsm->u.enable.bytecode_len);
53a80697
MD
2913 if (ret <= 0) {
2914 DBG("Nothing recv() from client var len data... continuing");
2915 *sock_error = 1;
f73fabfd 2916 ret = LTTNG_ERR_FILTER_INVAL;
53a80697
MD
2917 goto error;
2918 }
2919
2920 if (bytecode->len + sizeof(*bytecode)
025faf73 2921 != cmd_ctx->lsm->u.enable.bytecode_len) {
53a80697 2922 free(bytecode);
f73fabfd 2923 ret = LTTNG_ERR_FILTER_INVAL;
53a80697
MD
2924 goto error;
2925 }
2926
025faf73
DG
2927 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
2928 cmd_ctx->lsm->u.enable.channel_name,
2929 &cmd_ctx->lsm->u.enable.event, bytecode, kernel_poll_pipe[1]);
53a80697
MD
2930 break;
2931 }
6d805429 2932 case LTTNG_DATA_PENDING:
806e2684 2933 {
6d805429 2934 ret = cmd_data_pending(cmd_ctx->session);
806e2684
DG
2935 break;
2936 }
5e16da05 2937 default:
f73fabfd 2938 ret = LTTNG_ERR_UND;
5461b305 2939 break;
fac6795d
DG
2940 }
2941
5461b305 2942error:
5461b305
DG
2943 if (cmd_ctx->llm == NULL) {
2944 DBG("Missing llm structure. Allocating one.");
894be886 2945 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
2946 goto setup_error;
2947 }
2948 }
54d01ffb 2949 /* Set return code */
5461b305 2950 cmd_ctx->llm->ret_code = ret;
5461b305 2951setup_error:
b5541356 2952 if (cmd_ctx->session) {
54d01ffb 2953 session_unlock(cmd_ctx->session);
b5541356 2954 }
256a5576
MD
2955 if (need_tracing_session) {
2956 session_unlock_list();
2957 }
54d01ffb 2958init_setup_error:
8028d920 2959 return ret;
fac6795d
DG
2960}
2961
44a5e5eb
DG
2962/*
2963 * Thread managing health check socket.
2964 */
2965static void *thread_manage_health(void *data)
2966{
eb4a2943 2967 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
2968 uint32_t revents, nb_fd;
2969 struct lttng_poll_event events;
2970 struct lttcomm_health_msg msg;
2971 struct lttcomm_health_data reply;
2972
2973 DBG("[thread] Manage health check started");
2974
2975 rcu_register_thread();
2976
2977 /* Create unix socket */
2978 sock = lttcomm_create_unix_sock(health_unix_sock_path);
2979 if (sock < 0) {
2980 ERR("Unable to create health check Unix socket");
2981 ret = -1;
2982 goto error;
2983 }
2984
b662582b
DG
2985 /*
2986 * Set the CLOEXEC flag. Return code is useless because either way, the
2987 * show must go on.
2988 */
2989 (void) utils_set_fd_cloexec(sock);
2990
44a5e5eb
DG
2991 ret = lttcomm_listen_unix_sock(sock);
2992 if (ret < 0) {
2993 goto error;
2994 }
2995
2996 /*
2997 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2998 * more will be added to this poll set.
2999 */
3000 ret = create_thread_poll_set(&events, 2);
3001 if (ret < 0) {
3002 goto error;
3003 }
3004
3005 /* Add the application registration socket */
3006 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3007 if (ret < 0) {
3008 goto error;
3009 }
3010
3011 while (1) {
3012 DBG("Health check ready");
3013
44a5e5eb
DG
3014 /* Inifinite blocking call, waiting for transmission */
3015restart:
3016 ret = lttng_poll_wait(&events, -1);
3017 if (ret < 0) {
3018 /*
3019 * Restart interrupted system call.
3020 */
3021 if (errno == EINTR) {
3022 goto restart;
3023 }
3024 goto error;
3025 }
3026
0d9c5d77
DG
3027 nb_fd = ret;
3028
44a5e5eb
DG
3029 for (i = 0; i < nb_fd; i++) {
3030 /* Fetch once the poll data */
3031 revents = LTTNG_POLL_GETEV(&events, i);
3032 pollfd = LTTNG_POLL_GETFD(&events, i);
3033
3034 /* Thread quit pipe has been closed. Killing thread. */
3035 ret = check_thread_quit_pipe(pollfd, revents);
3036 if (ret) {
139ac872
MD
3037 err = 0;
3038 goto exit;
44a5e5eb
DG
3039 }
3040
3041 /* Event on the registration socket */
3042 if (pollfd == sock) {
3043 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3044 ERR("Health socket poll error");
3045 goto error;
3046 }
3047 }
3048 }
3049
3050 new_sock = lttcomm_accept_unix_sock(sock);
3051 if (new_sock < 0) {
3052 goto error;
3053 }
3054
b662582b
DG
3055 /*
3056 * Set the CLOEXEC flag. Return code is useless because either way, the
3057 * show must go on.
3058 */
3059 (void) utils_set_fd_cloexec(new_sock);
3060
44a5e5eb
DG
3061 DBG("Receiving data from client for health...");
3062 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3063 if (ret <= 0) {
3064 DBG("Nothing recv() from client... continuing");
3065 ret = close(new_sock);
3066 if (ret) {
3067 PERROR("close");
3068 }
3069 new_sock = -1;
3070 continue;
3071 }
3072
3073 rcu_thread_online();
3074
3075 switch (msg.component) {
3076 case LTTNG_HEALTH_CMD:
927ca06a 3077 reply.ret_code = health_check_state(HEALTH_TYPE_CMD);
44a5e5eb 3078 break;
139ac872 3079 case LTTNG_HEALTH_APP_MANAGE:
927ca06a 3080 reply.ret_code = health_check_state(HEALTH_TYPE_APP_MANAGE);
139ac872 3081 break;
44a5e5eb 3082 case LTTNG_HEALTH_APP_REG:
927ca06a 3083 reply.ret_code = health_check_state(HEALTH_TYPE_APP_REG);
44a5e5eb
DG
3084 break;
3085 case LTTNG_HEALTH_KERNEL:
927ca06a 3086 reply.ret_code = health_check_state(HEALTH_TYPE_KERNEL);
44a5e5eb
DG
3087 break;
3088 case LTTNG_HEALTH_CONSUMER:
3089 reply.ret_code = check_consumer_health();
3090 break;
3091 case LTTNG_HEALTH_ALL:
44a5e5eb 3092 reply.ret_code =
927ca06a
DG
3093 health_check_state(HEALTH_TYPE_APP_MANAGE) &&
3094 health_check_state(HEALTH_TYPE_APP_REG) &&
3095 health_check_state(HEALTH_TYPE_CMD) &&
3096 health_check_state(HEALTH_TYPE_KERNEL) &&
139ac872 3097 check_consumer_health();
44a5e5eb
DG
3098 break;
3099 default:
f73fabfd 3100 reply.ret_code = LTTNG_ERR_UND;
44a5e5eb
DG