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