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