Fix: Subdirectory handling for lttng and sessiond
[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,
173af62f
DG
90 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
91 .lock = PTHREAD_MUTEX_INITIALIZER,
3bd1e081 92};
7753dea8
MD
93static struct consumer_data ustconsumer64_data = {
94 .type = LTTNG_CONSUMER64_UST,
60922cb0
DG
95 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
96 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
03550b58
MD
97 .err_sock = -1,
98 .cmd_sock = -1,
173af62f
DG
99 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
100 .lock = PTHREAD_MUTEX_INITIALIZER,
7753dea8
MD
101};
102static struct consumer_data ustconsumer32_data = {
103 .type = LTTNG_CONSUMER32_UST,
60922cb0
DG
104 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
105 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
03550b58
MD
106 .err_sock = -1,
107 .cmd_sock = -1,
173af62f
DG
108 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
109 .lock = PTHREAD_MUTEX_INITIALIZER,
3bd1e081
MD
110};
111
26c9d55e 112/* Shared between threads */
099e26bd 113static int dispatch_thread_exit;
fac6795d 114
54d01ffb
DG
115/* Global application Unix socket path */
116static char apps_unix_sock_path[PATH_MAX];
117/* Global client Unix socket path */
118static char client_unix_sock_path[PATH_MAX];
54d01ffb
DG
119/* global wait shm path for UST */
120static char wait_shm_path[PATH_MAX];
44a5e5eb
DG
121/* Global health check unix path */
122static char health_unix_sock_path[PATH_MAX];
fac6795d 123
1d4b027a 124/* Sockets and FDs */
a4b35e07
MD
125static int client_sock = -1;
126static int apps_sock = -1;
127static int kernel_tracer_fd = -1;
76d7553f 128static int kernel_poll_pipe[2] = { -1, -1 };
1d4b027a 129
273ea72c
DG
130/*
131 * Quit pipe for all threads. This permits a single cancellation point
132 * for all threads when receiving an event on the pipe.
133 */
76d7553f 134static int thread_quit_pipe[2] = { -1, -1 };
273ea72c 135
099e26bd
DG
136/*
137 * This pipe is used to inform the thread managing application communication
138 * that a command is queued and ready to be processed.
139 */
76d7553f 140static int apps_cmd_pipe[2] = { -1, -1 };
099e26bd 141
1d4b027a 142/* Pthread, Mutexes and Semaphores */
1d4b027a 143static pthread_t apps_thread;
099e26bd 144static pthread_t reg_apps_thread;
1d4b027a 145static pthread_t client_thread;
7a485870 146static pthread_t kernel_thread;
099e26bd 147static pthread_t dispatch_thread;
44a5e5eb 148static pthread_t health_thread;
5eb91c98 149
099e26bd
DG
150/*
151 * UST registration command queue. This queue is tied with a futex and uses a N
152 * wakers / 1 waiter implemented and detailed in futex.c/.h
153 *
154 * The thread_manage_apps and thread_dispatch_ust_registration interact with
155 * this queue and the wait/wake scheme.
156 */
157static struct ust_cmd_queue ust_cmd_queue;
158
b5541356
DG
159/*
160 * Pointer initialized before thread creation.
161 *
162 * This points to the tracing session list containing the session count and a
163 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
164 * MUST NOT be taken if you call a public function in session.c.
04ea676f 165 *
d063d709 166 * The lock is nested inside the structure: session_list_ptr->lock. Please use
54d01ffb 167 * session_lock_list and session_unlock_list for lock acquisition.
b5541356
DG
168 */
169static struct ltt_session_list *session_list_ptr;
170
7753dea8
MD
171int ust_consumerd64_fd = -1;
172int ust_consumerd32_fd = -1;
173
fb6f1fa2
YB
174static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
175static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
176static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
177static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
fb09408a 178
5c827ce0
DG
179/*
180 * Consumer daemon state which is changed when spawning it, killing it or in
181 * case of a fatal error.
182 */
183enum consumerd_state {
184 CONSUMER_STARTED = 1,
185 CONSUMER_STOPPED = 2,
186 CONSUMER_ERROR = 3,
187};
188
189/*
190 * This consumer daemon state is used to validate if a client command will be
191 * able to reach the consumer. If not, the client is informed. For instance,
192 * doing a "lttng start" when the consumer state is set to ERROR will return an
193 * error to the client.
194 *
195 * The following example shows a possible race condition of this scheme:
196 *
197 * consumer thread error happens
198 * client cmd arrives
199 * client cmd checks state -> still OK
200 * consumer thread exit, sets error
201 * client cmd try to talk to consumer
202 * ...
203 *
204 * However, since the consumer is a different daemon, we have no way of making
205 * sure the command will reach it safely even with this state flag. This is why
206 * we consider that up to the state validation during command processing, the
207 * command is safe. After that, we can not guarantee the correctness of the
208 * client request vis-a-vis the consumer.
209 */
210static enum consumerd_state ust_consumerd_state;
211static enum consumerd_state kernel_consumerd_state;
212
00e2e675
DG
213/*
214 * Used to keep a unique index for each relayd socket created where this value
215 * is associated with streams on the consumer so it can match the right relayd
216 * to send to.
217 *
218 * This value should be incremented atomically for safety purposes and future
219 * possible concurrent access.
220 */
221static unsigned int relayd_net_seq_idx;
222
44a5e5eb
DG
223/* Used for the health monitoring of the session daemon. See health.h */
224struct health_state health_thread_cmd;
139ac872 225struct health_state health_thread_app_manage;
44a5e5eb
DG
226struct health_state health_thread_app_reg;
227struct health_state health_thread_kernel;
228
fb09408a 229static
7753dea8 230void setup_consumerd_path(void)
fb09408a 231{
fc7a59ce 232 const char *bin, *libdir;
fb09408a 233
7753dea8
MD
234 /*
235 * Allow INSTALL_BIN_PATH to be used as a target path for the
ebaeda94
MD
236 * native architecture size consumer if CONFIG_CONSUMER*_PATH
237 * has not been defined.
7753dea8 238 */
ebaeda94 239#if (CAA_BITS_PER_LONG == 32)
fc7a59ce
AM
240 if (!consumerd32_bin[0]) {
241 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
ebaeda94
MD
242 }
243 if (!consumerd32_libdir[0]) {
244 consumerd32_libdir = INSTALL_LIB_PATH;
245 }
246#elif (CAA_BITS_PER_LONG == 64)
fc7a59ce
AM
247 if (!consumerd64_bin[0]) {
248 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
7753dea8 249 }
ebaeda94
MD
250 if (!consumerd64_libdir[0]) {
251 consumerd64_libdir = INSTALL_LIB_PATH;
7753dea8
MD
252 }
253#else
254#error "Unknown bitness"
255#endif
256
fb09408a
MD
257 /*
258 * runtime env. var. overrides the build default.
259 */
fc7a59ce
AM
260 bin = getenv("LTTNG_CONSUMERD32_BIN");
261 if (bin) {
262 consumerd32_bin = bin;
7753dea8 263 }
fc7a59ce
AM
264 bin = getenv("LTTNG_CONSUMERD64_BIN");
265 if (bin) {
266 consumerd64_bin = bin;
ebaeda94 267 }
72f579ee 268 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
ebaeda94
MD
269 if (libdir) {
270 consumerd32_libdir = libdir;
271 }
72f579ee 272 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
ebaeda94
MD
273 if (libdir) {
274 consumerd64_libdir = libdir;
fb09408a
MD
275 }
276}
277
5eb91c98
DG
278/*
279 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
280 */
281static int create_thread_poll_set(struct lttng_poll_event *events,
282 unsigned int size)
283{
284 int ret;
285
286 if (events == NULL || size == 0) {
287 ret = -1;
288 goto error;
289 }
290
291 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
292 if (ret < 0) {
293 goto error;
294 }
295
296 /* Add quit pipe */
297 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN);
298 if (ret < 0) {
299 goto error;
300 }
301
302 return 0;
303
304error:
305 return ret;
306}
307
308/*
309 * Check if the thread quit pipe was triggered.
310 *
311 * Return 1 if it was triggered else 0;
312 */
313static int check_thread_quit_pipe(int fd, uint32_t events)
314{
315 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
316 return 1;
317 }
318
319 return 0;
320}
321
0fdd1e2c
DG
322/*
323 * Return group ID of the tracing group or -1 if not found.
324 */
996b65c8
MD
325static gid_t allowed_group(void)
326{
327 struct group *grp;
328
274e143b
MD
329 if (opt_tracing_group) {
330 grp = getgrnam(opt_tracing_group);
331 } else {
332 grp = getgrnam(default_tracing_group);
333 }
996b65c8
MD
334 if (!grp) {
335 return -1;
336 } else {
337 return grp->gr_gid;
338 }
339}
340
273ea72c 341/*
5eb91c98 342 * Init thread quit pipe.
273ea72c
DG
343 *
344 * Return -1 on error or 0 if all pipes are created.
345 */
346static int init_thread_quit_pipe(void)
347{
730389d9 348 int ret, i;
273ea72c 349
730389d9 350 ret = pipe(thread_quit_pipe);
273ea72c 351 if (ret < 0) {
730389d9 352 PERROR("thread quit pipe");
273ea72c
DG
353 goto error;
354 }
355
730389d9
DG
356 for (i = 0; i < 2; i++) {
357 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
358 if (ret < 0) {
359 PERROR("fcntl");
360 goto error;
361 }
362 }
363
273ea72c
DG
364error:
365 return ret;
366}
367
fac6795d 368/*
d063d709
DG
369 * Complete teardown of a kernel session. This free all data structure related
370 * to a kernel session and update counter.
fac6795d 371 */
1d4b027a 372static void teardown_kernel_session(struct ltt_session *session)
fac6795d 373{
173af62f
DG
374 int ret;
375 struct lttng_ht_iter iter;
376 struct ltt_kernel_session *ksess;
377 struct consumer_socket *socket;
378
8f7a84dd 379 if (!session->kernel_session) {
99bab54f 380 DBG3("No kernel session when tearing down session");
84ea9c99 381 return;
8f7a84dd
DG
382 }
383
173af62f
DG
384 ksess = session->kernel_session;
385
84ea9c99 386 DBG("Tearing down kernel session");
d9800920 387
173af62f
DG
388 /*
389 * Destroy relayd associated with the session consumer. This action is
390 * valid since in order to destroy a session we must acquire the session
391 * lock. This means that there CAN NOT be stream(s) being sent to a
392 * consumer since this action also requires the session lock at any time.
393 *
394 * At this point, we are sure that not streams data will be lost after this
395 * command is issued.
396 */
397 if (ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET) {
398 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, socket,
399 node.node) {
400 ret = consumer_send_destroy_relayd(socket, ksess->consumer);
401 if (ret < 0) {
402 ERR("Unable to send destroy relayd command to consumer");
403 /* Continue since we MUST delete everything at this point. */
404 }
405 }
406 }
407
84ea9c99
MD
408 /*
409 * If a custom kernel consumer was registered, close the socket before
410 * tearing down the complete kernel session structure
411 */
173af62f
DG
412 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, socket,
413 node.node) {
414 if (socket->fd != kconsumer_data.cmd_sock) {
415 rcu_read_lock();
416 consumer_del_socket(socket, ksess->consumer);
417 lttcomm_close_unix_sock(socket->fd);
418 consumer_destroy_socket(socket);
419 rcu_read_unlock();
420 }
fac6795d 421 }
84ea9c99 422
173af62f 423 trace_kernel_destroy_session(ksess);
fac6795d
DG
424}
425
f6a9efaa
DG
426/*
427 * Complete teardown of all UST sessions. This will free everything on his path
428 * and destroy the core essence of all ust sessions :)
429 */
430static void teardown_ust_session(struct ltt_session *session)
431{
84cd17c6 432 int ret;
173af62f
DG
433 struct lttng_ht_iter iter;
434 struct ltt_ust_session *usess;
435 struct consumer_socket *socket;
84cd17c6 436
8f7a84dd 437 if (!session->ust_session) {
99bab54f 438 DBG3("No UST session when tearing down session");
f470a390 439 return;
8f7a84dd 440 }
173af62f 441 usess = session->ust_session;
8f7a84dd 442
84ea9c99 443 DBG("Tearing down UST session(s)");
8f7a84dd 444
173af62f
DG
445 /*
446 * Destroy relayd associated with the session consumer. This action is
447 * valid since in order to destroy a session we must acquire the session
448 * lock. This means that there CAN NOT be stream(s) being sent to a
449 * consumer since this action also requires the session lock at any time.
450 *
3f8e211f
DG
451 * At this point, we are sure that no data will be lost after this command
452 * is issued.
173af62f
DG
453 */
454 if (usess->consumer && usess->consumer->type == CONSUMER_DST_NET) {
455 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, socket,
456 node.node) {
457 ret = consumer_send_destroy_relayd(socket, usess->consumer);
458 if (ret < 0) {
459 ERR("Unable to send destroy relayd command to consumer");
460 /* Continue since we MUST delete everything at this point. */
461 }
462 }
463 }
464
465 ret = ust_app_destroy_trace_all(usess);
84cd17c6
MD
466 if (ret) {
467 ERR("Error in ust_app_destroy_trace_all");
468 }
8f7a84dd 469
173af62f 470 trace_ust_destroy_session(usess);
f6a9efaa
DG
471}
472
099e26bd
DG
473/*
474 * Stop all threads by closing the thread quit pipe.
475 */
cf3af59e
MD
476static void stop_threads(void)
477{
5eb91c98
DG
478 int ret;
479
cf3af59e
MD
480 /* Stopping all threads */
481 DBG("Terminating all threads");
54d01ffb 482 ret = notify_thread_pipe(thread_quit_pipe[1]);
5eb91c98
DG
483 if (ret < 0) {
484 ERR("write error on thread quit pipe");
485 }
486
099e26bd 487 /* Dispatch thread */
26c9d55e 488 CMM_STORE_SHARED(dispatch_thread_exit, 1);
099e26bd 489 futex_nto1_wake(&ust_cmd_queue.futex);
cf3af59e
MD
490}
491
fac6795d 492/*
d063d709 493 * Cleanup the daemon
fac6795d 494 */
cf3af59e 495static void cleanup(void)
fac6795d 496{
ef599319 497 int ret;
1d4b027a 498 char *cmd;
af9737e9 499 struct ltt_session *sess, *stmp;
fac6795d 500
1d4b027a 501 DBG("Cleaning up");
e07ae692 502
67e40797
DG
503 DBG("Removing %s directory", rundir);
504 ret = asprintf(&cmd, "rm -rf %s", rundir);
505 if (ret < 0) {
506 ERR("asprintf failed. Something is really wrong!");
507 }
5461b305 508
67e40797
DG
509 /* Remove lttng run directory */
510 ret = system(cmd);
511 if (ret < 0) {
512 ERR("Unable to clean %s", rundir);
1d4b027a 513 }
67e40797 514 free(cmd);
5461b305 515
99bab54f 516 DBG("Cleaning up all sessions");
fac6795d 517
b5541356 518 /* Destroy session list mutex */
273ea72c
DG
519 if (session_list_ptr != NULL) {
520 pthread_mutex_destroy(&session_list_ptr->lock);
521
522 /* Cleanup ALL session */
54d01ffb
DG
523 cds_list_for_each_entry_safe(sess, stmp,
524 &session_list_ptr->head, list) {
273ea72c 525 teardown_kernel_session(sess);
f6a9efaa
DG
526 teardown_ust_session(sess);
527 free(sess);
273ea72c
DG
528 }
529 }
530
099e26bd 531 DBG("Closing all UST sockets");
56fff090 532 ust_app_clean_list();
099e26bd 533
4fba7219
DG
534 if (is_root && !opt_no_kernel) {
535 DBG2("Closing kernel fd");
a4b35e07 536 if (kernel_tracer_fd >= 0) {
76d7553f
MD
537 ret = close(kernel_tracer_fd);
538 if (ret) {
539 PERROR("close");
540 }
a4b35e07 541 }
2f50c8a3 542 DBG("Unloading kernel modules");
096102bd 543 modprobe_remove_lttng_all();
2f50c8a3 544 }
ef599319
DG
545 utils_close_pipe(kernel_poll_pipe);
546 utils_close_pipe(thread_quit_pipe);
547 utils_close_pipe(apps_cmd_pipe);
421cb601
DG
548
549 /* <fun> */
f56a39af 550 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
421cb601
DG
551 "Matthew, BEET driven development works!%c[%dm",
552 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
553 /* </fun> */
fac6795d
DG
554}
555
e065084a 556/*
d063d709 557 * Send data on a unix socket using the liblttsessiondcomm API.
e065084a 558 *
d063d709 559 * Return lttcomm error code.
e065084a
DG
560 */
561static int send_unix_sock(int sock, void *buf, size_t len)
562{
563 /* Check valid length */
564 if (len <= 0) {
565 return -1;
566 }
567
568 return lttcomm_send_unix_sock(sock, buf, len);
569}
570
5461b305 571/*
d063d709 572 * Free memory of a command context structure.
5461b305 573 */
a2fb29a5 574static void clean_command_ctx(struct command_ctx **cmd_ctx)
5461b305 575{
a2fb29a5
DG
576 DBG("Clean command context structure");
577 if (*cmd_ctx) {
578 if ((*cmd_ctx)->llm) {
579 free((*cmd_ctx)->llm);
5461b305 580 }
a2fb29a5
DG
581 if ((*cmd_ctx)->lsm) {
582 free((*cmd_ctx)->lsm);
5461b305 583 }
a2fb29a5
DG
584 free(*cmd_ctx);
585 *cmd_ctx = NULL;
5461b305
DG
586 }
587}
588
fac6795d 589/*
0fdd1e2c 590 * Notify UST applications using the shm mmap futex.
fac6795d 591 */
0fdd1e2c 592static int notify_ust_apps(int active)
fac6795d 593{
0fdd1e2c 594 char *wait_shm_mmap;
fac6795d 595
0fdd1e2c 596 DBG("Notifying applications of session daemon state: %d", active);
e07ae692 597
0fdd1e2c
DG
598 /* See shm.c for this call implying mmap, shm and futex calls */
599 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
600 if (wait_shm_mmap == NULL) {
fac6795d
DG
601 goto error;
602 }
603
0fdd1e2c
DG
604 /* Wake waiting process */
605 futex_wait_update((int32_t *) wait_shm_mmap, active);
606
607 /* Apps notified successfully */
608 return 0;
fac6795d
DG
609
610error:
0fdd1e2c 611 return -1;
fac6795d
DG
612}
613
e065084a 614/*
d063d709
DG
615 * Setup the outgoing data buffer for the response (llm) by allocating the
616 * right amount of memory and copying the original information from the lsm
617 * structure.
ca95a216 618 *
d063d709 619 * Return total size of the buffer pointed by buf.
ca95a216 620 */
5461b305 621static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
ca95a216 622{
f3ed775e 623 int ret, buf_size;
ca95a216 624
f3ed775e 625 buf_size = size;
5461b305 626
ba7f0ae5 627 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
5461b305 628 if (cmd_ctx->llm == NULL) {
76d7553f 629 PERROR("zmalloc");
5461b305 630 ret = -ENOMEM;
ca95a216
DG
631 goto error;
632 }
633
5461b305
DG
634 /* Copy common data */
635 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
9f19cc17 636 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
5461b305 637
5461b305
DG
638 cmd_ctx->llm->data_size = size;
639 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
640
ca95a216
DG
641 return buf_size;
642
643error:
644 return ret;
645}
646
7a485870 647/*
5eb91c98 648 * Update the kernel poll set of all channel fd available over all tracing
d063d709 649 * session. Add the wakeup pipe at the end of the set.
7a485870 650 */
5eb91c98 651static int update_kernel_poll(struct lttng_poll_event *events)
7a485870 652{
5eb91c98 653 int ret;
7a485870
DG
654 struct ltt_session *session;
655 struct ltt_kernel_channel *channel;
656
5eb91c98 657 DBG("Updating kernel poll set");
7a485870 658
54d01ffb 659 session_lock_list();
b5541356 660 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 661 session_lock(session);
7a485870 662 if (session->kernel_session == NULL) {
54d01ffb 663 session_unlock(session);
7a485870
DG
664 continue;
665 }
7a485870 666
54d01ffb
DG
667 cds_list_for_each_entry(channel,
668 &session->kernel_session->channel_list.head, list) {
5eb91c98
DG
669 /* Add channel fd to the kernel poll set */
670 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
671 if (ret < 0) {
54d01ffb 672 session_unlock(session);
5eb91c98
DG
673 goto error;
674 }
675 DBG("Channel fd %d added to kernel set", channel->fd);
7a485870 676 }
54d01ffb 677 session_unlock(session);
7a485870 678 }
54d01ffb 679 session_unlock_list();
7a485870 680
5eb91c98 681 return 0;
7a485870
DG
682
683error:
54d01ffb 684 session_unlock_list();
7a485870
DG
685 return -1;
686}
687
688/*
54d01ffb 689 * Find the channel fd from 'fd' over all tracing session. When found, check
d063d709 690 * for new channel stream and send those stream fds to the kernel consumer.
7a485870 691 *
d063d709 692 * Useful for CPU hotplug feature.
7a485870 693 */
2bdd86d4 694static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
7a485870
DG
695{
696 int ret = 0;
697 struct ltt_session *session;
173af62f 698 struct ltt_kernel_session *ksess;
7a485870
DG
699 struct ltt_kernel_channel *channel;
700
701 DBG("Updating kernel streams for channel fd %d", fd);
702
54d01ffb 703 session_lock_list();
b5541356 704 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
54d01ffb 705 session_lock(session);
7a485870 706 if (session->kernel_session == NULL) {
54d01ffb 707 session_unlock(session);
7a485870
DG
708 continue;
709 }
173af62f 710 ksess = session->kernel_session;
d9800920 711
173af62f 712 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
7a485870
DG
713 if (channel->fd == fd) {
714 DBG("Channel found, updating kernel streams");
715 ret = kernel_open_channel_stream(channel);
716 if (ret < 0) {
b3c750d2 717 goto error;
7a485870 718 }
d9800920 719
7a485870 720 /*
5eb91c98
DG
721 * Have we already sent fds to the consumer? If yes, it means
722 * that tracing is started so it is safe to send our updated
723 * stream fds.
7a485870 724 */
173af62f
DG
725 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
726 struct lttng_ht_iter iter;
727 struct consumer_socket *socket;
728
729
730 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
731 &iter.iter, socket, node.node) {
732 /* Code flow error */
733 assert(socket->fd >= 0);
734
735 pthread_mutex_lock(socket->lock);
736 ret = kernel_consumer_send_channel_stream(socket->fd,
737 channel, ksess);
738 pthread_mutex_unlock(socket->lock);
739 if (ret < 0) {
740 goto error;
741 }
7a485870
DG
742 }
743 }
b3c750d2 744 goto error;
7a485870
DG
745 }
746 }
54d01ffb 747 session_unlock(session);
7a485870 748 }
54d01ffb 749 session_unlock_list();
b3c750d2 750 return ret;
7a485870 751
b3c750d2 752error:
54d01ffb
DG
753 session_unlock(session);
754 session_unlock_list();
7a485870
DG
755 return ret;
756}
757
487cf67c
DG
758/*
759 * For each tracing session, update newly registered apps.
760 */
761static void update_ust_app(int app_sock)
762{
763 struct ltt_session *sess, *stmp;
764
4ee14516
DG
765 session_lock_list();
766
487cf67c
DG
767 /* For all tracing session(s) */
768 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
4ee14516 769 session_lock(sess);
421cb601
DG
770 if (sess->ust_session) {
771 ust_app_global_update(sess->ust_session, app_sock);
772 }
4ee14516 773 session_unlock(sess);
487cf67c 774 }
4ee14516
DG
775
776 session_unlock_list();
487cf67c
DG
777}
778
7a485870 779/*
d063d709 780 * This thread manage event coming from the kernel.
7a485870 781 *
d063d709
DG
782 * Features supported in this thread:
783 * -) CPU Hotplug
7a485870
DG
784 */
785static void *thread_manage_kernel(void *data)
786{
139ac872 787 int ret, i, pollfd, update_poll_flag = 1, err = -1;
5eb91c98 788 uint32_t revents, nb_fd;
7a485870 789 char tmp;
5eb91c98 790 struct lttng_poll_event events;
7a485870
DG
791
792 DBG("Thread manage kernel started");
793
44a5e5eb
DG
794 health_code_update(&health_thread_kernel);
795
5eb91c98
DG
796 ret = create_thread_poll_set(&events, 2);
797 if (ret < 0) {
76d7553f 798 goto error_poll_create;
5eb91c98
DG
799 }
800
801 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
802 if (ret < 0) {
803 goto error;
804 }
805
7a485870 806 while (1) {
44a5e5eb
DG
807 health_code_update(&health_thread_kernel);
808
7a485870 809 if (update_poll_flag == 1) {
5f822d0a
DG
810 /*
811 * Reset number of fd in the poll set. Always 2 since there is the thread
812 * quit pipe and the kernel pipe.
813 */
814 events.nb_fd = 2;
815
5eb91c98
DG
816 ret = update_kernel_poll(&events);
817 if (ret < 0) {
7a485870
DG
818 goto error;
819 }
820 update_poll_flag = 0;
821 }
822
5eb91c98
DG
823 nb_fd = LTTNG_POLL_GETNB(&events);
824
825 DBG("Thread kernel polling on %d fds", nb_fd);
826
827 /* Zeroed the poll events */
828 lttng_poll_reset(&events);
7a485870
DG
829
830 /* Poll infinite value of time */
88f2b785 831 restart:
44a5e5eb 832 health_poll_update(&health_thread_kernel);
5eb91c98 833 ret = lttng_poll_wait(&events, -1);
44a5e5eb 834 health_poll_update(&health_thread_kernel);
7a485870 835 if (ret < 0) {
88f2b785
MD
836 /*
837 * Restart interrupted system call.
838 */
839 if (errno == EINTR) {
840 goto restart;
841 }
7a485870
DG
842 goto error;
843 } else if (ret == 0) {
844 /* Should not happen since timeout is infinite */
85611738
DG
845 ERR("Return value of poll is 0 with an infinite timeout.\n"
846 "This should not have happened! Continuing...");
7a485870
DG
847 continue;
848 }
849
5eb91c98
DG
850 for (i = 0; i < nb_fd; i++) {
851 /* Fetch once the poll data */
852 revents = LTTNG_POLL_GETEV(&events, i);
853 pollfd = LTTNG_POLL_GETFD(&events, i);
7a485870 854
44a5e5eb
DG
855 health_code_update(&health_thread_kernel);
856
5eb91c98
DG
857 /* Thread quit pipe has been closed. Killing thread. */
858 ret = check_thread_quit_pipe(pollfd, revents);
859 if (ret) {
139ac872
MD
860 err = 0;
861 goto exit;
5eb91c98 862 }
7a485870 863
5eb91c98
DG
864 /* Check for data on kernel pipe */
865 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
866 ret = read(kernel_poll_pipe[0], &tmp, 1);
867 update_poll_flag = 1;
868 continue;
869 } else {
870 /*
871 * New CPU detected by the kernel. Adding kernel stream to
872 * kernel session and updating the kernel consumer
873 */
874 if (revents & LPOLLIN) {
2bdd86d4 875 ret = update_kernel_stream(&kconsumer_data, pollfd);
5eb91c98
DG
876 if (ret < 0) {
877 continue;
878 }
879 break;
880 /*
881 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
882 * and unregister kernel stream at this point.
883 */
7a485870 884 }
7a485870
DG
885 }
886 }
887 }
888
139ac872 889exit:
7a485870 890error:
5eb91c98 891 lttng_poll_clean(&events);
76d7553f 892error_poll_create:
139ac872
MD
893 if (err) {
894 health_error(&health_thread_kernel);
895 ERR("Health error occurred in %s", __func__);
896 }
897 health_exit(&health_thread_kernel);
76d7553f 898 DBG("Kernel thread dying");
7a485870
DG
899 return NULL;
900}
901
1d4b027a 902/*
3bd1e081 903 * This thread manage the consumer error sent back to the session daemon.
1d4b027a 904 */
3bd1e081 905static void *thread_manage_consumer(void *data)
1d4b027a 906{
139ac872 907 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98 908 uint32_t revents, nb_fd;
1d4b027a 909 enum lttcomm_return_code code;
5eb91c98 910 struct lttng_poll_event events;
3bd1e081 911 struct consumer_data *consumer_data = data;
1d4b027a 912
3bd1e081 913 DBG("[thread] Manage consumer started");
1d4b027a 914
44a5e5eb
DG
915 health_code_update(&consumer_data->health);
916
3bd1e081 917 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
1d4b027a 918 if (ret < 0) {
76d7553f 919 goto error_listen;
1d4b027a
DG
920 }
921
5eb91c98
DG
922 /*
923 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
924 * Nothing more will be added to this poll set.
925 */
926 ret = create_thread_poll_set(&events, 2);
927 if (ret < 0) {
76d7553f 928 goto error_poll;
5eb91c98 929 }
273ea72c 930
3bd1e081 931 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
5eb91c98
DG
932 if (ret < 0) {
933 goto error;
934 }
935
936 nb_fd = LTTNG_POLL_GETNB(&events);
273ea72c 937
44a5e5eb
DG
938 health_code_update(&consumer_data->health);
939
273ea72c 940 /* Inifinite blocking call, waiting for transmission */
88f2b785 941restart:
44a5e5eb 942 health_poll_update(&consumer_data->health);
5eb91c98 943 ret = lttng_poll_wait(&events, -1);
44a5e5eb 944 health_poll_update(&consumer_data->health);
273ea72c 945 if (ret < 0) {
88f2b785
MD
946 /*
947 * Restart interrupted system call.
948 */
949 if (errno == EINTR) {
950 goto restart;
951 }
273ea72c
DG
952 goto error;
953 }
954
5eb91c98
DG
955 for (i = 0; i < nb_fd; i++) {
956 /* Fetch once the poll data */
957 revents = LTTNG_POLL_GETEV(&events, i);
958 pollfd = LTTNG_POLL_GETFD(&events, i);
959
44a5e5eb
DG
960 health_code_update(&consumer_data->health);
961
5eb91c98
DG
962 /* Thread quit pipe has been closed. Killing thread. */
963 ret = check_thread_quit_pipe(pollfd, revents);
964 if (ret) {
139ac872
MD
965 err = 0;
966 goto exit;
5eb91c98
DG
967 }
968
969 /* Event on the registration socket */
3bd1e081 970 if (pollfd == consumer_data->err_sock) {
5eb91c98 971 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 972 ERR("consumer err socket poll error");
5eb91c98
DG
973 goto error;
974 }
975 }
273ea72c
DG
976 }
977
3bd1e081 978 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1d4b027a
DG
979 if (sock < 0) {
980 goto error;
981 }
982
44a5e5eb
DG
983 health_code_update(&consumer_data->health);
984
3bd1e081 985 DBG2("Receiving code from consumer err_sock");
ee0b0061 986
712ea556 987 /* Getting status code from kconsumerd */
54d01ffb
DG
988 ret = lttcomm_recv_unix_sock(sock, &code,
989 sizeof(enum lttcomm_return_code));
1d4b027a
DG
990 if (ret <= 0) {
991 goto error;
992 }
993
44a5e5eb
DG
994 health_code_update(&consumer_data->health);
995
3bd1e081
MD
996 if (code == CONSUMERD_COMMAND_SOCK_READY) {
997 consumer_data->cmd_sock =
998 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
999 if (consumer_data->cmd_sock < 0) {
1000 sem_post(&consumer_data->sem);
48842b30 1001 PERROR("consumer connect");
1d4b027a
DG
1002 goto error;
1003 }
1004 /* Signal condition to tell that the kconsumerd is ready */
3bd1e081
MD
1005 sem_post(&consumer_data->sem);
1006 DBG("consumer command socket ready");
1d4b027a 1007 } else {
3bd1e081 1008 ERR("consumer error when waiting for SOCK_READY : %s",
1d4b027a
DG
1009 lttcomm_get_readable_code(-code));
1010 goto error;
1011 }
1012
54d01ffb 1013 /* Remove the kconsumerd error sock since we've established a connexion */
3bd1e081 1014 ret = lttng_poll_del(&events, consumer_data->err_sock);
72079cae 1015 if (ret < 0) {
72079cae
DG
1016 goto error;
1017 }
1018
5eb91c98
DG
1019 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1020 if (ret < 0) {
72079cae 1021 goto error;
5eb91c98
DG
1022 }
1023
44a5e5eb
DG
1024 health_code_update(&consumer_data->health);
1025
5eb91c98
DG
1026 /* Update number of fd */
1027 nb_fd = LTTNG_POLL_GETNB(&events);
1028
1029 /* Inifinite blocking call, waiting for transmission */
88f2b785 1030restart_poll:
44a5e5eb 1031 health_poll_update(&consumer_data->health);
5eb91c98 1032 ret = lttng_poll_wait(&events, -1);
44a5e5eb 1033 health_poll_update(&consumer_data->health);
5eb91c98 1034 if (ret < 0) {
88f2b785
MD
1035 /*
1036 * Restart interrupted system call.
1037 */
1038 if (errno == EINTR) {
1039 goto restart_poll;
1040 }
72079cae
DG
1041 goto error;
1042 }
1043
5eb91c98
DG
1044 for (i = 0; i < nb_fd; i++) {
1045 /* Fetch once the poll data */
1046 revents = LTTNG_POLL_GETEV(&events, i);
1047 pollfd = LTTNG_POLL_GETFD(&events, i);
1048
44a5e5eb
DG
1049 health_code_update(&consumer_data->health);
1050
5eb91c98
DG
1051 /* Thread quit pipe has been closed. Killing thread. */
1052 ret = check_thread_quit_pipe(pollfd, revents);
1053 if (ret) {
139ac872
MD
1054 err = 0;
1055 goto exit;
5eb91c98
DG
1056 }
1057
1058 /* Event on the kconsumerd socket */
1059 if (pollfd == sock) {
1060 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3bd1e081 1061 ERR("consumer err socket second poll error");
5eb91c98
DG
1062 goto error;
1063 }
1064 }
1065 }
1066
44a5e5eb
DG
1067 health_code_update(&consumer_data->health);
1068
712ea556 1069 /* Wait for any kconsumerd error */
54d01ffb
DG
1070 ret = lttcomm_recv_unix_sock(sock, &code,
1071 sizeof(enum lttcomm_return_code));
712ea556 1072 if (ret <= 0) {
3bd1e081 1073 ERR("consumer closed the command socket");
712ea556 1074 goto error;
6f61d021 1075 }
1d4b027a 1076
3bd1e081 1077 ERR("consumer return code : %s", lttcomm_get_readable_code(-code));
712ea556 1078
139ac872 1079exit:
1d4b027a 1080error:
5c827ce0
DG
1081 /* Immediately set the consumerd state to stopped */
1082 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1083 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1084 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1085 consumer_data->type == LTTNG_CONSUMER32_UST) {
1086 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1087 } else {
1088 /* Code flow error... */
1089 assert(0);
1090 }
1091
76d7553f
MD
1092 if (consumer_data->err_sock >= 0) {
1093 ret = close(consumer_data->err_sock);
1094 if (ret) {
1095 PERROR("close");
1096 }
1097 }
1098 if (consumer_data->cmd_sock >= 0) {
1099 ret = close(consumer_data->cmd_sock);
1100 if (ret) {
1101 PERROR("close");
1102 }
1103 }
1104 if (sock >= 0) {
1105 ret = close(sock);
1106 if (ret) {
1107 PERROR("close");
1108 }
1109 }
273ea72c 1110
3bd1e081
MD
1111 unlink(consumer_data->err_unix_sock_path);
1112 unlink(consumer_data->cmd_unix_sock_path);
1113 consumer_data->pid = 0;
1d4b027a 1114
5eb91c98 1115 lttng_poll_clean(&events);
76d7553f
MD
1116error_poll:
1117error_listen:
139ac872
MD
1118 if (err) {
1119 health_error(&consumer_data->health);
1120 ERR("Health error occurred in %s", __func__);
1121 }
1122 health_exit(&consumer_data->health);
76d7553f 1123 DBG("consumer thread cleanup completed");
0177d773 1124
5eb91c98 1125 return NULL;
099e26bd
DG
1126}
1127
099e26bd
DG
1128/*
1129 * This thread manage application communication.
1d4b027a
DG
1130 */
1131static void *thread_manage_apps(void *data)
099e26bd 1132{
139ac872 1133 int i, ret, pollfd, err = -1;
5eb91c98 1134 uint32_t revents, nb_fd;
099e26bd 1135 struct ust_command ust_cmd;
5eb91c98 1136 struct lttng_poll_event events;
099e26bd
DG
1137
1138 DBG("[thread] Manage application started");
1139
f6a9efaa
DG
1140 rcu_register_thread();
1141 rcu_thread_online();
1142
139ac872 1143 health_code_update(&health_thread_app_manage);
44a5e5eb 1144
5eb91c98
DG
1145 ret = create_thread_poll_set(&events, 2);
1146 if (ret < 0) {
76d7553f 1147 goto error_poll_create;
5eb91c98 1148 }
099e26bd 1149
5eb91c98
DG
1150 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1151 if (ret < 0) {
1152 goto error;
1153 }
099e26bd 1154
139ac872 1155 health_code_update(&health_thread_app_manage);
44a5e5eb 1156
5eb91c98
DG
1157 while (1) {
1158 /* Zeroed the events structure */
1159 lttng_poll_reset(&events);
0177d773 1160
5eb91c98 1161 nb_fd = LTTNG_POLL_GETNB(&events);
099e26bd
DG
1162
1163 DBG("Apps thread polling on %d fds", nb_fd);
1164
1165 /* Inifinite blocking call, waiting for transmission */
88f2b785 1166 restart:
139ac872 1167 health_poll_update(&health_thread_app_manage);
5eb91c98 1168 ret = lttng_poll_wait(&events, -1);
139ac872 1169 health_poll_update(&health_thread_app_manage);
099e26bd 1170 if (ret < 0) {
88f2b785
MD
1171 /*
1172 * Restart interrupted system call.
1173 */
1174 if (errno == EINTR) {
1175 goto restart;
1176 }
099e26bd
DG
1177 goto error;
1178 }
1179
5eb91c98
DG
1180 for (i = 0; i < nb_fd; i++) {
1181 /* Fetch once the poll data */
1182 revents = LTTNG_POLL_GETEV(&events, i);
1183 pollfd = LTTNG_POLL_GETFD(&events, i);
1184
139ac872 1185 health_code_update(&health_thread_app_manage);
44a5e5eb 1186
5eb91c98
DG
1187 /* Thread quit pipe has been closed. Killing thread. */
1188 ret = check_thread_quit_pipe(pollfd, revents);
1189 if (ret) {
139ac872
MD
1190 err = 0;
1191 goto exit;
5eb91c98 1192 }
099e26bd 1193
5eb91c98
DG
1194 /* Inspect the apps cmd pipe */
1195 if (pollfd == apps_cmd_pipe[0]) {
1196 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1197 ERR("Apps command pipe error");
0177d773 1198 goto error;
5eb91c98
DG
1199 } else if (revents & LPOLLIN) {
1200 /* Empty pipe */
1201 ret = read(apps_cmd_pipe[0], &ust_cmd, sizeof(ust_cmd));
1202 if (ret < 0 || ret < sizeof(ust_cmd)) {
76d7553f 1203 PERROR("read apps cmd pipe");
5eb91c98
DG
1204 goto error;
1205 }
099e26bd 1206
139ac872 1207 health_code_update(&health_thread_app_manage);
44a5e5eb 1208
5eb91c98 1209 /* Register applicaton to the session daemon */
56fff090 1210 ret = ust_app_register(&ust_cmd.reg_msg,
54d01ffb 1211 ust_cmd.sock);
88ff5b7f 1212 if (ret == -ENOMEM) {
5eb91c98 1213 goto error;
88ff5b7f
MD
1214 } else if (ret < 0) {
1215 break;
5eb91c98
DG
1216 }
1217
139ac872 1218 health_code_update(&health_thread_app_manage);
44a5e5eb 1219
acc7b41b 1220 /*
e0c7ec2b 1221 * Validate UST version compatibility.
acc7b41b 1222 */
e0c7ec2b
DG
1223 ret = ust_app_validate_version(ust_cmd.sock);
1224 if (ret >= 0) {
1225 /*
1226 * Add channel(s) and event(s) to newly registered apps
1227 * from lttng global UST domain.
1228 */
1229 update_ust_app(ust_cmd.sock);
1230 }
acc7b41b 1231
139ac872 1232 health_code_update(&health_thread_app_manage);
44a5e5eb 1233
f2ca2e25 1234 ret = ust_app_register_done(ust_cmd.sock);
5eb91c98
DG
1235 if (ret < 0) {
1236 /*
1237 * If the registration is not possible, we simply
1238 * unregister the apps and continue
1239 */
56fff090 1240 ust_app_unregister(ust_cmd.sock);
5eb91c98
DG
1241 } else {
1242 /*
1243 * We just need here to monitor the close of the UST
1244 * socket and poll set monitor those by default.
a9ade966
MD
1245 * Listen on POLLIN (even if we never expect any
1246 * data) to ensure that hangup wakes us.
5eb91c98 1247 */
a9ade966 1248 ret = lttng_poll_add(&events, ust_cmd.sock, LPOLLIN);
5eb91c98
DG
1249 if (ret < 0) {
1250 goto error;
1251 }
1252
54d01ffb
DG
1253 DBG("Apps with sock %d added to poll set",
1254 ust_cmd.sock);
5eb91c98 1255 }
487cf67c 1256
139ac872 1257 health_code_update(&health_thread_app_manage);
44a5e5eb 1258
5eb91c98 1259 break;
0177d773 1260 }
5eb91c98
DG
1261 } else {
1262 /*
54d01ffb
DG
1263 * At this point, we know that a registered application made
1264 * the event at poll_wait.
5eb91c98
DG
1265 */
1266 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1267 /* Removing from the poll set */
1268 ret = lttng_poll_del(&events, pollfd);
1269 if (ret < 0) {
1270 goto error;
1271 }
099e26bd 1272
b9d9b220 1273 /* Socket closed on remote end. */
56fff090 1274 ust_app_unregister(pollfd);
5eb91c98
DG
1275 break;
1276 }
099e26bd 1277 }
44a5e5eb 1278
139ac872 1279 health_code_update(&health_thread_app_manage);
099e26bd 1280 }
099e26bd
DG
1281 }
1282
139ac872 1283exit:
099e26bd 1284error:
5eb91c98 1285 lttng_poll_clean(&events);
76d7553f 1286error_poll_create:
139ac872
MD
1287 if (err) {
1288 health_error(&health_thread_app_manage);
1289 ERR("Health error occurred in %s", __func__);
1290 }
1291 health_exit(&health_thread_app_manage);
76d7553f 1292 DBG("Application communication apps thread cleanup complete");
f6a9efaa
DG
1293 rcu_thread_offline();
1294 rcu_unregister_thread();
099e26bd
DG
1295 return NULL;
1296}
1297
1298/*
1299 * Dispatch request from the registration threads to the application
1300 * communication thread.
1301 */
1302static void *thread_dispatch_ust_registration(void *data)
1303{
1304 int ret;
1305 struct cds_wfq_node *node;
1306 struct ust_command *ust_cmd = NULL;
1307
1308 DBG("[thread] Dispatch UST command started");
1309
26c9d55e 1310 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
099e26bd
DG
1311 /* Atomically prepare the queue futex */
1312 futex_nto1_prepare(&ust_cmd_queue.futex);
1313
1314 do {
1315 /* Dequeue command for registration */
1316 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1317 if (node == NULL) {
00a17c97 1318 DBG("Woken up but nothing in the UST command queue");
099e26bd
DG
1319 /* Continue thread execution */
1320 break;
1321 }
1322
1323 ust_cmd = caa_container_of(node, struct ust_command, node);
1324
2f50c8a3
DG
1325 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1326 " gid:%d sock:%d name:%s (version %d.%d)",
1327 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1328 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1329 ust_cmd->sock, ust_cmd->reg_msg.name,
1330 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
099e26bd
DG
1331 /*
1332 * Inform apps thread of the new application registration. This
1333 * call is blocking so we can be assured that the data will be read
1334 * at some point in time or wait to the end of the world :)
1335 */
1336 ret = write(apps_cmd_pipe[1], ust_cmd,
1337 sizeof(struct ust_command));
1338 if (ret < 0) {
76d7553f 1339 PERROR("write apps cmd pipe");
099e26bd
DG
1340 if (errno == EBADF) {
1341 /*
1342 * We can't inform the application thread to process
1343 * registration. We will exit or else application
1344 * registration will not occur and tracing will never
1345 * start.
1346 */
1347 goto error;
1348 }
1349 }
1350 free(ust_cmd);
1351 } while (node != NULL);
1352
1353 /* Futex wait on queue. Blocking call on futex() */
1354 futex_nto1_wait(&ust_cmd_queue.futex);
1355 }
1356
1357error:
1358 DBG("Dispatch thread dying");
1359 return NULL;
1360}
1361
1362/*
1363 * This thread manage application registration.
1364 */
1365static void *thread_registration_apps(void *data)
1d4b027a 1366{
139ac872 1367 int sock = -1, i, ret, pollfd, err = -1;
5eb91c98
DG
1368 uint32_t revents, nb_fd;
1369 struct lttng_poll_event events;
099e26bd
DG
1370 /*
1371 * Get allocated in this thread, enqueued to a global queue, dequeued and
1372 * freed in the manage apps thread.
1373 */
1374 struct ust_command *ust_cmd = NULL;
1d4b027a 1375
099e26bd 1376 DBG("[thread] Manage application registration started");
1d4b027a
DG
1377
1378 ret = lttcomm_listen_unix_sock(apps_sock);
1379 if (ret < 0) {
76d7553f 1380 goto error_listen;
1d4b027a
DG
1381 }
1382
5eb91c98
DG
1383 /*
1384 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1385 * more will be added to this poll set.
1386 */
1387 ret = create_thread_poll_set(&events, 2);
1388 if (ret < 0) {
76d7553f 1389 goto error_create_poll;
5eb91c98 1390 }
273ea72c 1391
5eb91c98
DG
1392 /* Add the application registration socket */
1393 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1394 if (ret < 0) {
76d7553f 1395 goto error_poll_add;
5eb91c98 1396 }
273ea72c 1397
1d4b027a 1398 /* Notify all applications to register */
0fdd1e2c
DG
1399 ret = notify_ust_apps(1);
1400 if (ret < 0) {
1401 ERR("Failed to notify applications or create the wait shared memory.\n"
54d01ffb
DG
1402 "Execution continues but there might be problem for already\n"
1403 "running applications that wishes to register.");
0fdd1e2c 1404 }
1d4b027a
DG
1405
1406 while (1) {
1407 DBG("Accepting application registration");
273ea72c 1408
5eb91c98
DG
1409 nb_fd = LTTNG_POLL_GETNB(&events);
1410
273ea72c 1411 /* Inifinite blocking call, waiting for transmission */
88f2b785 1412 restart:
139ac872 1413 health_poll_update(&health_thread_app_reg);
5eb91c98 1414 ret = lttng_poll_wait(&events, -1);
139ac872 1415 health_poll_update(&health_thread_app_reg);
273ea72c 1416 if (ret < 0) {
88f2b785
MD
1417 /*
1418 * Restart interrupted system call.
1419 */
1420 if (errno == EINTR) {
1421 goto restart;
1422 }
273ea72c
DG
1423 goto error;
1424 }
1425
5eb91c98 1426 for (i = 0; i < nb_fd; i++) {
139ac872
MD
1427 health_code_update(&health_thread_app_reg);
1428
5eb91c98
DG
1429 /* Fetch once the poll data */
1430 revents = LTTNG_POLL_GETEV(&events, i);
1431 pollfd = LTTNG_POLL_GETFD(&events, i);
273ea72c 1432
5eb91c98
DG
1433 /* Thread quit pipe has been closed. Killing thread. */
1434 ret = check_thread_quit_pipe(pollfd, revents);
1435 if (ret) {
139ac872
MD
1436 err = 0;
1437 goto exit;
90014c57 1438 }
1d4b027a 1439
5eb91c98
DG
1440 /* Event on the registration socket */
1441 if (pollfd == apps_sock) {
1442 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1443 ERR("Register apps socket poll error");
1444 goto error;
1445 } else if (revents & LPOLLIN) {
1446 sock = lttcomm_accept_unix_sock(apps_sock);
1447 if (sock < 0) {
1448 goto error;
1449 }
099e26bd 1450
5eb91c98 1451 /* Create UST registration command for enqueuing */
ba7f0ae5 1452 ust_cmd = zmalloc(sizeof(struct ust_command));
5eb91c98 1453 if (ust_cmd == NULL) {
76d7553f 1454 PERROR("ust command zmalloc");
5eb91c98
DG
1455 goto error;
1456 }
1d4b027a 1457
5eb91c98
DG
1458 /*
1459 * Using message-based transmissions to ensure we don't
1460 * have to deal with partially received messages.
1461 */
4063050c
MD
1462 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1463 if (ret < 0) {
1464 ERR("Exhausted file descriptors allowed for applications.");
1465 free(ust_cmd);
1466 ret = close(sock);
1467 if (ret) {
1468 PERROR("close");
1469 }
1470 sock = -1;
1471 continue;
1472 }
139ac872 1473 health_code_update(&health_thread_app_reg);
5eb91c98
DG
1474 ret = lttcomm_recv_unix_sock(sock, &ust_cmd->reg_msg,
1475 sizeof(struct ust_register_msg));
1476 if (ret < 0 || ret < sizeof(struct ust_register_msg)) {
1477 if (ret < 0) {
76d7553f 1478 PERROR("lttcomm_recv_unix_sock register apps");
5eb91c98
DG
1479 } else {
1480 ERR("Wrong size received on apps register");
1481 }
1482 free(ust_cmd);
76d7553f
MD
1483 ret = close(sock);
1484 if (ret) {
1485 PERROR("close");
1486 }
4063050c 1487 lttng_fd_put(LTTNG_FD_APPS, 1);
76d7553f 1488 sock = -1;
5eb91c98
DG
1489 continue;
1490 }
139ac872 1491 health_code_update(&health_thread_app_reg);
099e26bd 1492
5eb91c98 1493 ust_cmd->sock = sock;
34a2494f 1494 sock = -1;
099e26bd 1495
5eb91c98
DG
1496 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1497 " gid:%d sock:%d name:%s (version %d.%d)",
1498 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1499 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1500 ust_cmd->sock, ust_cmd->reg_msg.name,
1501 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
54d01ffb 1502
5eb91c98
DG
1503 /*
1504 * Lock free enqueue the registration request. The red pill
54d01ffb 1505 * has been taken! This apps will be part of the *system*.
5eb91c98
DG
1506 */
1507 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
1508
1509 /*
1510 * Wake the registration queue futex. Implicit memory
1511 * barrier with the exchange in cds_wfq_enqueue.
1512 */
1513 futex_nto1_wake(&ust_cmd_queue.futex);
1514 }
1515 }
90014c57 1516 }
1d4b027a
DG
1517 }
1518
139ac872 1519exit:
1d4b027a 1520error:
139ac872
MD
1521 if (err) {
1522 health_error(&health_thread_app_reg);
1523 ERR("Health error occurred in %s", __func__);
1524 }
1525 health_exit(&health_thread_app_reg);
1526
0fdd1e2c
DG
1527 /* Notify that the registration thread is gone */
1528 notify_ust_apps(0);
1529
a4b35e07 1530 if (apps_sock >= 0) {
76d7553f
MD
1531 ret = close(apps_sock);
1532 if (ret) {
1533 PERROR("close");
1534 }
a4b35e07 1535 }
46c3f085 1536 if (sock >= 0) {
76d7553f
MD
1537 ret = close(sock);
1538 if (ret) {
1539 PERROR("close");
1540 }
4063050c 1541 lttng_fd_put(LTTNG_FD_APPS, 1);
a4b35e07 1542 }
273ea72c 1543 unlink(apps_unix_sock_path);
0fdd1e2c 1544
76d7553f 1545error_poll_add:
5eb91c98 1546 lttng_poll_clean(&events);
76d7553f
MD
1547error_listen:
1548error_create_poll:
1549 DBG("UST Registration thread cleanup complete");
5eb91c98 1550
1d4b027a
DG
1551 return NULL;
1552}
1553
8c0faa1d 1554/*
3bd1e081 1555 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
d063d709 1556 * exec or it will fails.
8c0faa1d 1557 */
3bd1e081 1558static int spawn_consumer_thread(struct consumer_data *consumer_data)
8c0faa1d
DG
1559{
1560 int ret;
ee0b0061
DG
1561 struct timespec timeout;
1562
1563 timeout.tv_sec = DEFAULT_SEM_WAIT_TIMEOUT;
1564 timeout.tv_nsec = 0;
8c0faa1d
DG
1565
1566 /* Setup semaphore */
3bd1e081 1567 ret = sem_init(&consumer_data->sem, 0, 0);
ee0b0061 1568 if (ret < 0) {
3bd1e081 1569 PERROR("sem_init consumer semaphore");
ee0b0061
DG
1570 goto error;
1571 }
8c0faa1d 1572
3bd1e081
MD
1573 ret = pthread_create(&consumer_data->thread, NULL,
1574 thread_manage_consumer, consumer_data);
8c0faa1d 1575 if (ret != 0) {
3bd1e081 1576 PERROR("pthread_create consumer");
ee0b0061 1577 ret = -1;
8c0faa1d
DG
1578 goto error;
1579 }
1580
ee0b0061
DG
1581 /* Get time for sem_timedwait absolute timeout */
1582 ret = clock_gettime(CLOCK_REALTIME, &timeout);
1583 if (ret < 0) {
3bd1e081 1584 PERROR("clock_gettime spawn consumer");
ee0b0061 1585 /* Infinite wait for the kconsumerd thread to be ready */
3bd1e081 1586 ret = sem_wait(&consumer_data->sem);
ee0b0061
DG
1587 } else {
1588 /* Normal timeout if the gettime was successful */
1589 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
3bd1e081 1590 ret = sem_timedwait(&consumer_data->sem, &timeout);
ee0b0061 1591 }
8c0faa1d 1592
ee0b0061
DG
1593 if (ret < 0) {
1594 if (errno == ETIMEDOUT) {
1595 /*
1596 * Call has timed out so we kill the kconsumerd_thread and return
1597 * an error.
1598 */
3bd1e081
MD
1599 ERR("The consumer thread was never ready. Killing it");
1600 ret = pthread_cancel(consumer_data->thread);
ee0b0061 1601 if (ret < 0) {
3bd1e081 1602 PERROR("pthread_cancel consumer thread");
ee0b0061
DG
1603 }
1604 } else {
3bd1e081 1605 PERROR("semaphore wait failed consumer thread");
ee0b0061
DG
1606 }
1607 goto error;
1608 }
1609
3bd1e081
MD
1610 pthread_mutex_lock(&consumer_data->pid_mutex);
1611 if (consumer_data->pid == 0) {
712ea556 1612 ERR("Kconsumerd did not start");
3bd1e081 1613 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556
DG
1614 goto error;
1615 }
3bd1e081 1616 pthread_mutex_unlock(&consumer_data->pid_mutex);
712ea556 1617
8c0faa1d
DG
1618 return 0;
1619
1620error:
1621 return ret;
1622}
1623
d9800920 1624/*
3bd1e081 1625 * Join consumer thread
d9800920 1626 */
3bd1e081 1627static int join_consumer_thread(struct consumer_data *consumer_data)
cf3af59e
MD
1628{
1629 void *status;
1630 int ret;
1631
e8209f6b
DG
1632 /* Consumer pid must be a real one. */
1633 if (consumer_data->pid > 0) {
3bd1e081 1634 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 1635 if (ret) {
3bd1e081 1636 ERR("Error killing consumer daemon");
cf3af59e
MD
1637 return ret;
1638 }
3bd1e081 1639 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
1640 } else {
1641 return 0;
1642 }
1643}
1644
8c0faa1d 1645/*
3bd1e081 1646 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 1647 *
d063d709 1648 * Return pid if successful else -1.
8c0faa1d 1649 */
3bd1e081 1650static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
1651{
1652 int ret;
1653 pid_t pid;
94c55f17 1654 const char *consumer_to_use;
53086306 1655 const char *verbosity;
94c55f17 1656 struct stat st;
8c0faa1d 1657
3bd1e081 1658 DBG("Spawning consumerd");
c49dc785 1659
8c0faa1d
DG
1660 pid = fork();
1661 if (pid == 0) {
1662 /*
3bd1e081 1663 * Exec consumerd.
8c0faa1d 1664 */
daee5345 1665 if (opt_verbose_consumer) {
53086306
DG
1666 verbosity = "--verbose";
1667 } else {
1668 verbosity = "--quiet";
1669 }
3bd1e081
MD
1670 switch (consumer_data->type) {
1671 case LTTNG_CONSUMER_KERNEL:
94c55f17 1672 /*
c7704d57
DG
1673 * Find out which consumerd to execute. We will first try the
1674 * 64-bit path, then the sessiond's installation directory, and
1675 * fallback on the 32-bit one,
94c55f17 1676 */
63a799e8
AM
1677 DBG3("Looking for a kernel consumer at these locations:");
1678 DBG3(" 1) %s", consumerd64_bin);
1679 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1680 DBG3(" 3) %s", consumerd32_bin);
94c55f17 1681 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 1682 DBG3("Found location #1");
94c55f17 1683 consumer_to_use = consumerd64_bin;
94c55f17 1684 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 1685 DBG3("Found location #2");
94c55f17 1686 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 1687 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 1688 DBG3("Found location #3");
eb1e0bd4 1689 consumer_to_use = consumerd32_bin;
94c55f17 1690 } else {
63a799e8 1691 DBG("Could not find any valid consumerd executable");
94c55f17
AM
1692 break;
1693 }
1694 DBG("Using kernel consumer at: %s", consumer_to_use);
1695 execl(consumer_to_use,
1696 "lttng-consumerd", verbosity, "-k",
1697 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1698 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1699 NULL);
3bd1e081 1700 break;
7753dea8
MD
1701 case LTTNG_CONSUMER64_UST:
1702 {
b1e0b6b6 1703 char *tmpnew = NULL;
8f4905da
MD
1704
1705 if (consumerd64_libdir[0] != '\0') {
1706 char *tmp;
1707 size_t tmplen;
1708
1709 tmp = getenv("LD_LIBRARY_PATH");
1710 if (!tmp) {
1711 tmp = "";
1712 }
1713 tmplen = strlen("LD_LIBRARY_PATH=")
1714 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1715 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1716 if (!tmpnew) {
1717 ret = -ENOMEM;
1718 goto error;
1719 }
1720 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1721 strcat(tmpnew, consumerd64_libdir);
1722 if (tmp[0] != '\0') {
1723 strcat(tmpnew, ":");
1724 strcat(tmpnew, tmp);
1725 }
1726 ret = putenv(tmpnew);
1727 if (ret) {
1728 ret = -errno;
1729 goto error;
1730 }
1731 }
94c55f17 1732 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 1733 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1734 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1735 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1736 NULL);
8f4905da
MD
1737 if (consumerd64_libdir[0] != '\0') {
1738 free(tmpnew);
1739 }
1740 if (ret) {
1741 goto error;
1742 }
3bd1e081 1743 break;
7753dea8
MD
1744 }
1745 case LTTNG_CONSUMER32_UST:
1746 {
937dde8e 1747 char *tmpnew = NULL;
8f4905da
MD
1748
1749 if (consumerd32_libdir[0] != '\0') {
1750 char *tmp;
1751 size_t tmplen;
1752
1753 tmp = getenv("LD_LIBRARY_PATH");
1754 if (!tmp) {
1755 tmp = "";
1756 }
1757 tmplen = strlen("LD_LIBRARY_PATH=")
1758 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1759 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1760 if (!tmpnew) {
1761 ret = -ENOMEM;
1762 goto error;
1763 }
1764 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1765 strcat(tmpnew, consumerd32_libdir);
1766 if (tmp[0] != '\0') {
1767 strcat(tmpnew, ":");
1768 strcat(tmpnew, tmp);
1769 }
1770 ret = putenv(tmpnew);
1771 if (ret) {
1772 ret = -errno;
1773 goto error;
1774 }
1775 }
94c55f17 1776 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 1777 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1778 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1779 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1780 NULL);
8f4905da
MD
1781 if (consumerd32_libdir[0] != '\0') {
1782 free(tmpnew);
1783 }
1784 if (ret) {
1785 goto error;
1786 }
7753dea8
MD
1787 break;
1788 }
3bd1e081 1789 default:
76d7553f 1790 PERROR("unknown consumer type");
3bd1e081
MD
1791 exit(EXIT_FAILURE);
1792 }
8c0faa1d 1793 if (errno != 0) {
76d7553f 1794 PERROR("kernel start consumer exec");
8c0faa1d
DG
1795 }
1796 exit(EXIT_FAILURE);
1797 } else if (pid > 0) {
1798 ret = pid;
8c0faa1d 1799 } else {
76d7553f 1800 PERROR("start consumer fork");
8c0faa1d 1801 ret = -errno;
8c0faa1d 1802 }
8f4905da 1803error:
8c0faa1d
DG
1804 return ret;
1805}
1806
693bd40b 1807/*
3bd1e081 1808 * Spawn the consumerd daemon and session daemon thread.
693bd40b 1809 */
3bd1e081 1810static int start_consumerd(struct consumer_data *consumer_data)
693bd40b
DG
1811{
1812 int ret;
1813
3bd1e081
MD
1814 pthread_mutex_lock(&consumer_data->pid_mutex);
1815 if (consumer_data->pid != 0) {
1816 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
1817 goto end;
1818 }
693bd40b 1819
3bd1e081 1820 ret = spawn_consumerd(consumer_data);
c49dc785 1821 if (ret < 0) {
3bd1e081
MD
1822 ERR("Spawning consumerd failed");
1823 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 1824 goto error;
693bd40b 1825 }
c49dc785 1826
3bd1e081
MD
1827 /* Setting up the consumer_data pid */
1828 consumer_data->pid = ret;
48842b30 1829 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 1830 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 1831
3bd1e081
MD
1832 DBG2("Spawning consumer control thread");
1833 ret = spawn_consumer_thread(consumer_data);
693bd40b 1834 if (ret < 0) {
3bd1e081 1835 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
1836 goto error;
1837 }
1838
c49dc785 1839end:
693bd40b
DG
1840 return 0;
1841
1842error:
1843 return ret;
1844}
1845
44a5e5eb 1846/*
139ac872
MD
1847 * Compute health status of each consumer. If one of them is zero (bad
1848 * state), we return 0.
44a5e5eb
DG
1849 */
1850static int check_consumer_health(void)
1851{
1852 int ret;
1853
139ac872
MD
1854 ret = health_check_state(&kconsumer_data.health) &&
1855 health_check_state(&ustconsumer32_data.health) &&
44a5e5eb
DG
1856 health_check_state(&ustconsumer64_data.health);
1857
1858 DBG3("Health consumer check %d", ret);
1859
1860 return ret;
1861}
1862
b73401da 1863/*
096102bd 1864 * Check version of the lttng-modules.
b73401da 1865 */
096102bd 1866static int validate_lttng_modules_version(void)
b73401da 1867{
096102bd 1868 return kernel_validate_version(kernel_tracer_fd);
ab147185
MD
1869}
1870
b73401da 1871/*
096102bd 1872 * Setup necessary data for kernel tracer action.
b73401da 1873 */
096102bd 1874static int init_kernel_tracer(void)
b73401da
DG
1875{
1876 int ret;
b73401da 1877
096102bd
DG
1878 /* Modprobe lttng kernel modules */
1879 ret = modprobe_lttng_control();
b73401da 1880 if (ret < 0) {
b73401da
DG
1881 goto error;
1882 }
1883
096102bd
DG
1884 /* Open debugfs lttng */
1885 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1886 if (kernel_tracer_fd < 0) {
1887 DBG("Failed to open %s", module_proc_lttng);
1888 ret = -1;
1889 goto error_open;
b73401da
DG
1890 }
1891
096102bd
DG
1892 /* Validate kernel version */
1893 ret = validate_lttng_modules_version();
b73401da 1894 if (ret < 0) {
096102bd 1895 goto error_version;
b73401da
DG
1896 }
1897
096102bd 1898 ret = modprobe_lttng_data();
b73401da 1899 if (ret < 0) {
096102bd 1900 goto error_modules;
8c0faa1d
DG
1901 }
1902
f3ed775e 1903 DBG("Kernel tracer fd %d", kernel_tracer_fd);
096102bd
DG
1904 return 0;
1905
1906error_version:
1907 modprobe_remove_lttng_control();
76d7553f
MD
1908 ret = close(kernel_tracer_fd);
1909 if (ret) {
1910 PERROR("close");
1911 }
1912 kernel_tracer_fd = -1;
096102bd
DG
1913 return LTTCOMM_KERN_VERSION;
1914
1915error_modules:
76d7553f
MD
1916 ret = close(kernel_tracer_fd);
1917 if (ret) {
1918 PERROR("close");
1919 }
096102bd
DG
1920
1921error_open:
1922 modprobe_remove_lttng_control();
b73401da
DG
1923
1924error:
b73401da 1925 WARN("No kernel tracer available");
a4b35e07 1926 kernel_tracer_fd = -1;
531d29f9
MD
1927 if (!is_root) {
1928 return LTTCOMM_NEED_ROOT_SESSIOND;
1929 } else {
1930 return LTTCOMM_KERN_NA;
1931 }
f3ed775e 1932}
33a2b854 1933
f3ed775e 1934/*
54d01ffb 1935 * Init tracing by creating trace directory and sending fds kernel consumer.
f3ed775e 1936 */
54d01ffb 1937static int init_kernel_tracing(struct ltt_kernel_session *session)
f3ed775e 1938{
f40799e8 1939 int ret = 0;
173af62f
DG
1940 struct lttng_ht_iter iter;
1941 struct consumer_socket *socket;
8c0faa1d 1942
173af62f 1943 assert(session);
d9800920 1944
173af62f
DG
1945 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
1946 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
1947 socket, node.node) {
1948 /* Code flow error */
1949 assert(socket->fd >= 0);
1950
1951 pthread_mutex_lock(socket->lock);
1952 ret = kernel_consumer_send_session(socket->fd, session);
1953 pthread_mutex_unlock(socket->lock);
1954 if (ret < 0) {
1955 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1956 goto error;
1957 }
f3ed775e 1958 }
00e2e675 1959 }
1d4b027a 1960
00e2e675
DG
1961error:
1962 return ret;
1963}
1964
1965/*
1966 * Create a socket to the relayd using the URI.
1967 *
1968 * On success, the relayd_sock pointer is set to the created socket.
1969 * Else, it is untouched and an lttcomm error code is returned.
1970 */
1971static int create_connect_relayd(struct consumer_output *output,
1972 const char *session_name, struct lttng_uri *uri,
1973 struct lttcomm_sock **relayd_sock)
1974{
1975 int ret;
1976 struct lttcomm_sock *sock;
1977
1978 /* Create socket object from URI */
1979 sock = lttcomm_alloc_sock_from_uri(uri);
1980 if (sock == NULL) {
1981 ret = LTTCOMM_FATAL;
1982 goto error;
1983 }
1984
1985 ret = lttcomm_create_sock(sock);
1986 if (ret < 0) {
1987 ret = LTTCOMM_FATAL;
1988 goto error;
1989 }
1990
1991 /* Connect to relayd so we can proceed with a session creation. */
1992 ret = relayd_connect(sock);
1993 if (ret < 0) {
1994 ERR("Unable to reach lttng-relayd");
1995 ret = LTTCOMM_RELAYD_SESSION_FAIL;
1996 goto free_sock;
f3ed775e 1997 }
1d4b027a 1998
00e2e675
DG
1999 /* Create socket for control stream. */
2000 if (uri->stype == LTTNG_STREAM_CONTROL) {
2001 DBG3("Creating relayd stream socket from URI");
2002
2003 /* Check relayd version */
2004 ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0);
2005 if (ret < 0) {
2006 ret = LTTCOMM_RELAYD_VERSION_FAIL;
2007 goto close_sock;
2008 }
2009 } else if (uri->stype == LTTNG_STREAM_DATA) {
2010 DBG3("Creating relayd data socket from URI");
2011 } else {
2012 /* Command is not valid */
2013 ERR("Relayd invalid stream type: %d", uri->stype);
2014 ret = LTTCOMM_INVALID;
2015 goto close_sock;
2016 }
2017
2018 *relayd_sock = sock;
2019
2020 return LTTCOMM_OK;
2021
2022close_sock:
2023 if (sock) {
2024 (void) relayd_close(sock);
2025 }
2026free_sock:
2027 if (sock) {
2028 lttcomm_destroy_sock(sock);
2029 }
2030error:
2031 return ret;
2032}
2033
2034/*
2035 * Connect to the relayd using URI and send the socket to the right consumer.
2036 */
2037static int send_socket_relayd_consumer(int domain, struct ltt_session *session,
2038 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
2039 int consumer_fd)
2040{
2041 int ret;
2042 struct lttcomm_sock *sock = NULL;
2043
2044 /* Set the network sequence index if not set. */
2045 if (consumer->net_seq_index == -1) {
2046 /*
2047 * Increment net_seq_idx because we are about to transfer the
2048 * new relayd socket to the consumer.
2049 */
2050 uatomic_inc(&relayd_net_seq_idx);
2051 /* Assign unique key so the consumer can match streams */
2052 consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx);
2053 }
2054
2055 /* Connect to relayd and make version check if uri is the control. */
2056 ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
2057 if (ret != LTTCOMM_OK) {
2058 goto close_sock;
2059 }
2060
2061 /* If the control socket is connected, network session is ready */
2062 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
2063 session->net_handle = 1;
2064 }
2065
37278a1e
DG
2066 /* Send relayd socket to consumer. */
2067 ret = consumer_send_relayd_socket(consumer_fd, sock,
2068 consumer, relayd_uri->stype);
2069 if (ret < 0) {
2070 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2071 goto close_sock;
00e2e675
DG
2072 }
2073
2074 ret = LTTCOMM_OK;
2075
2076 /*
2077 * Close socket which was dup on the consumer side. The session daemon does
2078 * NOT keep track of the relayd socket(s) once transfer to the consumer.
2079 */
2080
2081close_sock:
2082 if (sock) {
2083 (void) relayd_close(sock);
2084 lttcomm_destroy_sock(sock);
2085 }
2086
2087 return ret;
2088}
2089
2090/*
2091 * Send both relayd sockets to a specific consumer and domain. This is a
2092 * helper function to facilitate sending the information to the consumer for a
2093 * session.
2094 */
2095static int send_sockets_relayd_consumer(int domain,
2096 struct ltt_session *session, struct consumer_output *consumer, int fd)
2097{
2098 int ret;
2099
3f8e211f
DG
2100 assert(session);
2101 assert(consumer);
2102
2103 /* Don't resend the sockets to the consumer. */
2104 if (consumer->dst.net.relayd_socks_sent) {
2105 ret = LTTCOMM_OK;
2106 goto error;
2107 }
2108
00e2e675
DG
2109 /* Sending control relayd socket. */
2110 ret = send_socket_relayd_consumer(domain, session,
2111 &consumer->dst.net.control, consumer, fd);
2112 if (ret != LTTCOMM_OK) {
2113 goto error;
2114 }
2115
2116 /* Sending data relayd socket. */
2117 ret = send_socket_relayd_consumer(domain, session,
2118 &consumer->dst.net.data, consumer, fd);
2119 if (ret != LTTCOMM_OK) {
2120 goto error;
2121 }
2122
3f8e211f
DG
2123 /* Flag that all relayd sockets were sent to the consumer. */
2124 consumer->dst.net.relayd_socks_sent = 1;
2125
00e2e675
DG
2126error:
2127 return ret;
2128}
2129
2130/*
2131 * Setup relayd connections for a tracing session. First creates the socket to
2132 * the relayd and send them to the right domain consumer. Consumer type MUST be
2133 * network.
2134 */
2135static int setup_relayd(struct ltt_session *session)
2136{
2137 int ret = LTTCOMM_OK;
2138 struct ltt_ust_session *usess;
2139 struct ltt_kernel_session *ksess;
173af62f
DG
2140 struct consumer_socket *socket;
2141 struct lttng_ht_iter iter;
00e2e675
DG
2142
2143 assert(session);
2144
2145 usess = session->ust_session;
2146 ksess = session->kernel_session;
2147
2148 DBG2("Setting relayd for session %s", session->name);
2149
a4b92340
DG
2150 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
2151 && usess->consumer->enabled) {
173af62f
DG
2152 /* For each consumer socket, send relayd sockets */
2153 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
2154 socket, node.node) {
2155 /* Code flow error */
2156 assert(socket->fd >= 0);
2157
2158 pthread_mutex_lock(socket->lock);
00e2e675 2159 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
173af62f
DG
2160 usess->consumer, socket->fd);
2161 pthread_mutex_unlock(socket->lock);
00e2e675
DG
2162 if (ret != LTTCOMM_OK) {
2163 goto error;
2164 }
2165 }
a4b92340
DG
2166 }
2167
2168 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
2169 && ksess->consumer->enabled) {
173af62f
DG
2170 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
2171 socket, node.node) {
2172 /* Code flow error */
2173 assert(socket->fd >= 0);
2174
2175 pthread_mutex_lock(socket->lock);
2176 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session,
2177 ksess->consumer, socket->fd);
2178 pthread_mutex_unlock(socket->lock);
00e2e675
DG
2179 if (ret != LTTCOMM_OK) {
2180 goto error;
2181 }
2182 }
00e2e675
DG
2183 }
2184
2185error:
2186 return ret;
2187}
2188
07424f16
DG
2189/*
2190 * Set consumer subdirectory using the session name and a generated datetime if
2191 * needed. This is appended to the current subdirectory.
2192 */
2193static int set_consumer_subdir(struct consumer_output *consumer,
2194 const char *session_name)
2195{
2196 int ret = 0;
2197 unsigned int have_default_name = 0;
2198 char datetime[16], tmp_path[PATH_MAX];
2199 time_t rawtime;
2200 struct tm *timeinfo;
2201
2202 assert(consumer);
2203 assert(session_name);
2204
2205 memset(tmp_path, 0, sizeof(tmp_path));
2206
2207 /* Flag if we have a default session. */
2208 if (strncmp(session_name, DEFAULT_SESSION_NAME "-",
2209 strlen(DEFAULT_SESSION_NAME) + 1) == 0) {
2210 have_default_name = 1;
2211 } else {
2212 /* Get date and time for session path */
2213 time(&rawtime);
2214 timeinfo = localtime(&rawtime);
2215 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
2216 }
2217
2218 if (have_default_name) {
2219 ret = snprintf(tmp_path, sizeof(tmp_path),
2220 "%s/%s", consumer->subdir, session_name);
2221 } else {
2222 ret = snprintf(tmp_path, sizeof(tmp_path),
2223 "%s/%s-%s/", consumer->subdir, session_name, datetime);
2224 }
2225 if (ret < 0) {
2226 PERROR("snprintf session name date");
2227 goto error;
2228 }
2229
2230 strncpy(consumer->subdir, tmp_path, sizeof(consumer->subdir));
2231 DBG2("Consumer subdir set to %s", consumer->subdir);
2232
2233error:
2234 return ret;
2235}
2236
00e2e675
DG
2237/*
2238 * Copy consumer output from the tracing session to the domain session. The
2239 * function also applies the right modification on a per domain basis for the
2240 * trace files destination directory.
2241 */
2242static int copy_session_consumer(int domain, struct ltt_session *session)
2243{
2244 int ret;
2245 const char *dir_name;
2246 struct consumer_output *consumer;
2247
a4b92340
DG
2248 assert(session);
2249 assert(session->consumer);
2250
00e2e675
DG
2251 switch (domain) {
2252 case LTTNG_DOMAIN_KERNEL:
2253 DBG3("Copying tracing session consumer output in kernel session");
2254 session->kernel_session->consumer =
2255 consumer_copy_output(session->consumer);
2256 /* Ease our life a bit for the next part */
2257 consumer = session->kernel_session->consumer;
2258 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2259 break;
2260 case LTTNG_DOMAIN_UST:
2261 DBG3("Copying tracing session consumer output in UST session");
2262 session->ust_session->consumer =
2263 consumer_copy_output(session->consumer);
2264 /* Ease our life a bit for the next part */
2265 consumer = session->ust_session->consumer;
2266 dir_name = DEFAULT_UST_TRACE_DIR;
2267 break;
2268 default:
2269 ret = LTTCOMM_UNKNOWN_DOMAIN;
2270 goto error;
2271 }
2272
07424f16
DG
2273 ret = set_consumer_subdir(session->consumer, session->name);
2274 if (ret < 0) {
2275 ret = LTTCOMM_FATAL;
2276 goto error;
2277 }
2278
00e2e675
DG
2279 /* Append correct directory to subdir */
2280 strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
2281 DBG3("Copy session consumer subdir %s", consumer->subdir);
2282
00e2e675
DG
2283 ret = LTTCOMM_OK;
2284
1d4b027a
DG
2285error:
2286 return ret;
8c0faa1d
DG
2287}
2288
0177d773
DG
2289/*
2290 * Create an UST session and add it to the session ust list.
2291 */
44d3bd01 2292static int create_ust_session(struct ltt_session *session,
6df2e2c9 2293 struct lttng_domain *domain)
0177d773 2294{
6df2e2c9 2295 int ret;
00e2e675
DG
2296 struct ltt_ust_session *lus = NULL;
2297
2298 assert(session);
a4b92340 2299 assert(domain);
00e2e675 2300 assert(session->consumer);
44d3bd01
DG
2301
2302 switch (domain->type) {
48842b30 2303 case LTTNG_DOMAIN_UST:
44d3bd01
DG
2304 break;
2305 default:
00e2e675 2306 ERR("Unknown UST domain on create session %d", domain->type);
13384287 2307 ret = LTTCOMM_UNKNOWN_DOMAIN;
44d3bd01
DG
2308 goto error;
2309 }
0177d773
DG
2310
2311 DBG("Creating UST session");
2312
a991f516 2313 lus = trace_ust_create_session(session->path, session->id, domain);
0177d773 2314 if (lus == NULL) {
44d3bd01 2315 ret = LTTCOMM_UST_SESS_FAIL;
0177d773
DG
2316 goto error;
2317 }
2318
6df2e2c9
MD
2319 lus->uid = session->uid;
2320 lus->gid = session->gid;
f6a9efaa 2321 session->ust_session = lus;
44d3bd01 2322
00e2e675
DG
2323 /* Copy session output to the newly created UST session */
2324 ret = copy_session_consumer(domain->type, session);
2325 if (ret != LTTCOMM_OK) {
2326 goto error;
2327 }
2328
44d3bd01 2329 return LTTCOMM_OK;
0177d773
DG
2330
2331error:
2332 free(lus);
00e2e675 2333 session->ust_session = NULL;
0177d773
DG
2334 return ret;
2335}
2336
333f285e 2337/*
d063d709 2338 * Create a kernel tracer session then create the default channel.
333f285e 2339 */
6df2e2c9 2340static int create_kernel_session(struct ltt_session *session)
333f285e 2341{
f3ed775e 2342 int ret;
f3ed775e
DG
2343
2344 DBG("Creating kernel session");
2345
2346 ret = kernel_create_session(session, kernel_tracer_fd);
2347 if (ret < 0) {
2348 ret = LTTCOMM_KERN_SESS_FAIL;
2349 goto error;
333f285e
DG
2350 }
2351
a4b92340
DG
2352 /* Code flow safety */
2353 assert(session->kernel_session);
2354
00e2e675
DG
2355 /* Copy session output to the newly created Kernel session */
2356 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2357 if (ret != LTTCOMM_OK) {
2358 goto error;
2359 }
2360
2361 /* Create directory(ies) on local filesystem. */
a4b92340
DG
2362 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2363 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
00e2e675
DG
2364 ret = run_as_mkdir_recursive(
2365 session->kernel_session->consumer->dst.trace_path,
2366 S_IRWXU | S_IRWXG, session->uid, session->gid);
2367 if (ret < 0) {
2368 if (ret != -EEXIST) {
2369 ERR("Trace directory creation error");
2370 goto error;
2371 }
7a485870
DG
2372 }
2373 }
00e2e675 2374
6df2e2c9
MD
2375 session->kernel_session->uid = session->uid;
2376 session->kernel_session->gid = session->gid;
7a485870 2377
00e2e675
DG
2378 return LTTCOMM_OK;
2379
f3ed775e 2380error:
00e2e675
DG
2381 trace_kernel_destroy_session(session->kernel_session);
2382 session->kernel_session = NULL;
f3ed775e 2383 return ret;
333f285e
DG
2384}
2385
8e0af1b4 2386/*
c7704d57
DG
2387 * Check if the UID or GID match the session. Root user has access to all
2388 * sessions.
8e0af1b4 2389 */
c7704d57 2390static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
8e0af1b4 2391{
c7704d57 2392 if (uid != session->uid && gid != session->gid && uid != 0) {
8e0af1b4
MD
2393 return 0;
2394 } else {
2395 return 1;
2396 }
2397}
2398
00e2e675
DG
2399/*
2400 * Count number of session permitted by uid/gid.
2401 */
8e0af1b4
MD
2402static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2403{
2404 unsigned int i = 0;
2405 struct ltt_session *session;
2406
2407 DBG("Counting number of available session for UID %d GID %d",
2408 uid, gid);
2409 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2410 /*
2411 * Only list the sessions the user can control.
2412 */
2413 if (!session_access_ok(session, uid, gid)) {
2414 continue;
2415 }
2416 i++;
2417 }
2418 return i;
2419}
2420
6c9cc2ab
DG
2421/*
2422 * Using the session list, filled a lttng_session array to send back to the
2423 * client for session listing.
2424 *
2425 * The session list lock MUST be acquired before calling this function. Use
54d01ffb 2426 * session_lock_list() and session_unlock_list().
6c9cc2ab 2427 */
c7704d57
DG
2428static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2429 gid_t gid)
6c9cc2ab 2430{
8e0af1b4 2431 unsigned int i = 0;
6c9cc2ab
DG
2432 struct ltt_session *session;
2433
8e0af1b4
MD
2434 DBG("Getting all available session for UID %d GID %d",
2435 uid, gid);
6c9cc2ab
DG
2436 /*
2437 * Iterate over session list and append data after the control struct in
2438 * the buffer.
2439 */
2440 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
8e0af1b4
MD
2441 /*
2442 * Only list the sessions the user can control.
2443 */
2444 if (!session_access_ok(session, uid, gid)) {
2445 continue;
2446 }
6c9cc2ab 2447 strncpy(sessions[i].path, session->path, PATH_MAX);
99497cd0 2448 sessions[i].path[PATH_MAX - 1] = '\0';
6c9cc2ab 2449 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 2450 sessions[i].name[NAME_MAX - 1] = '\0';
464dd62d 2451 sessions[i].enabled = session->enabled;
6c9cc2ab
DG
2452 i++;
2453 }
2454}
2455
9f19cc17
DG
2456/*
2457 * Fill lttng_channel array of all channels.
2458 */
b551a063 2459static void list_lttng_channels(int domain, struct ltt_session *session,
9f19cc17
DG
2460 struct lttng_channel *channels)
2461{
2462 int i = 0;
2463 struct ltt_kernel_channel *kchan;
2464
2465 DBG("Listing channels for session %s", session->name);
2466
b551a063
DG
2467 switch (domain) {
2468 case LTTNG_DOMAIN_KERNEL:
2469 /* Kernel channels */
2470 if (session->kernel_session != NULL) {
2471 cds_list_for_each_entry(kchan,
2472 &session->kernel_session->channel_list.head, list) {
2473 /* Copy lttng_channel struct to array */
2474 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
2475 channels[i].enabled = kchan->enabled;
2476 i++;
2477 }
2478 }
2479 break;
2480 case LTTNG_DOMAIN_UST:
2481 {
bec39940 2482 struct lttng_ht_iter iter;
b551a063
DG
2483 struct ltt_ust_channel *uchan;
2484
bec39940
DG
2485 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
2486 &iter.iter, uchan, node.node) {
b551a063
DG
2487 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
2488 channels[i].attr.overwrite = uchan->attr.overwrite;
2489 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
2490 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
2491 channels[i].attr.switch_timer_interval =
2492 uchan->attr.switch_timer_interval;
2493 channels[i].attr.read_timer_interval =
2494 uchan->attr.read_timer_interval;
aea829b3 2495 channels[i].enabled = uchan->enabled;
5edd7e09
DG
2496 switch (uchan->attr.output) {
2497 case LTTNG_UST_MMAP:
2498 default:
2499 channels[i].attr.output = LTTNG_EVENT_MMAP;
2500 break;
2501 }
befe0905 2502 i++;
b551a063
DG
2503 }
2504 break;
2505 }
2506 default:
2507 break;
2508 }
2509}
2510
2511/*
2512 * Create a list of ust global domain events.
2513 */
2514static int list_lttng_ust_global_events(char *channel_name,
2515 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
2516{
2517 int i = 0, ret = 0;
2518 unsigned int nb_event = 0;
bec39940
DG
2519 struct lttng_ht_iter iter;
2520 struct lttng_ht_node_str *node;
b551a063
DG
2521 struct ltt_ust_channel *uchan;
2522 struct ltt_ust_event *uevent;
2523 struct lttng_event *tmp;
2524
2525 DBG("Listing UST global events for channel %s", channel_name);
2526
2527 rcu_read_lock();
2528
bec39940
DG
2529 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
2530 node = lttng_ht_iter_get_node_str(&iter);
d7b75b30
DG
2531 if (node == NULL) {
2532 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
2533 goto error;
b551a063
DG
2534 }
2535
bec39940 2536 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
d7b75b30 2537
bec39940 2538 nb_event += lttng_ht_get_count(uchan->events);
d7b75b30 2539
b551a063
DG
2540 if (nb_event == 0) {
2541 ret = nb_event;
2542 goto error;
2543 }
2544
2545 DBG3("Listing UST global %d events", nb_event);
2546
2547 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
2548 if (tmp == NULL) {
2549 ret = -LTTCOMM_FATAL;
2550 goto error;
2551 }
2552
bec39940 2553 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
d7b75b30
DG
2554 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
2555 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2556 tmp[i].enabled = uevent->enabled;
2557 switch (uevent->attr.instrumentation) {
2558 case LTTNG_UST_TRACEPOINT:
2559 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
2560 break;
2561 case LTTNG_UST_PROBE:
2562 tmp[i].type = LTTNG_EVENT_PROBE;
2563 break;
2564 case LTTNG_UST_FUNCTION:
2565 tmp[i].type = LTTNG_EVENT_FUNCTION;
2566 break;
0cda4f28 2567 }
8005f29a 2568 tmp[i].loglevel = uevent->attr.loglevel;
0cda4f28 2569 switch (uevent->attr.loglevel_type) {
8005f29a 2570 case LTTNG_UST_LOGLEVEL_ALL:
22e25b71 2571 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
0cda4f28 2572 break;
8005f29a 2573 case LTTNG_UST_LOGLEVEL_RANGE:
22e25b71 2574 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
8005f29a
MD
2575 break;
2576 case LTTNG_UST_LOGLEVEL_SINGLE:
22e25b71 2577 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
ac3bd9c0 2578 break;
9f19cc17 2579 }
fceb65df
MD
2580 if (uevent->filter) {
2581 tmp[i].filter = 1;
2582 }
d7b75b30 2583 i++;
9f19cc17
DG
2584 }
2585
b551a063
DG
2586 ret = nb_event;
2587 *events = tmp;
2588
2589error:
2590 rcu_read_unlock();
2591 return ret;
9f19cc17
DG
2592}
2593
2594/*
b551a063 2595 * Fill lttng_event array of all kernel events in the channel.
9f19cc17 2596 */
b551a063
DG
2597static int list_lttng_kernel_events(char *channel_name,
2598 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
9f19cc17 2599{
b551a063
DG
2600 int i = 0, ret;
2601 unsigned int nb_event;
9f19cc17 2602 struct ltt_kernel_event *event;
b551a063
DG
2603 struct ltt_kernel_channel *kchan;
2604
2605 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
2606 if (kchan == NULL) {
2607 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2608 goto error;
2609 }
2610
2611 nb_event = kchan->event_count;
9f19cc17
DG
2612
2613 DBG("Listing events for channel %s", kchan->channel->name);
2614
b551a063
DG
2615 if (nb_event == 0) {
2616 ret = nb_event;
2617 goto error;
2618 }
2619
2620 *events = zmalloc(nb_event * sizeof(struct lttng_event));
2621 if (*events == NULL) {
2622 ret = LTTCOMM_FATAL;
2623 goto error;
2624 }
2625
9f19cc17
DG
2626 /* Kernel channels */
2627 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
b551a063
DG
2628 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
2629 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2630 (*events)[i].enabled = event->enabled;
9f19cc17
DG
2631 switch (event->event->instrumentation) {
2632 case LTTNG_KERNEL_TRACEPOINT:
b551a063 2633 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
9f19cc17
DG
2634 break;
2635 case LTTNG_KERNEL_KPROBE:
2636 case LTTNG_KERNEL_KRETPROBE:
b551a063
DG
2637 (*events)[i].type = LTTNG_EVENT_PROBE;
2638 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
9f19cc17
DG
2639 sizeof(struct lttng_kernel_kprobe));
2640 break;
2641 case LTTNG_KERNEL_FUNCTION:
b551a063
DG
2642 (*events)[i].type = LTTNG_EVENT_FUNCTION;
2643 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
9f19cc17
DG
2644 sizeof(struct lttng_kernel_function));
2645 break;
0133c199 2646 case LTTNG_KERNEL_NOOP:
b551a063 2647 (*events)[i].type = LTTNG_EVENT_NOOP;
0133c199 2648 break;
a54bd42d 2649 case LTTNG_KERNEL_SYSCALL:
b551a063 2650 (*events)[i].type = LTTNG_EVENT_SYSCALL;
0133c199 2651 break;
7a3d1328
MD
2652 case LTTNG_KERNEL_ALL:
2653 assert(0);
2654 break;
9f19cc17
DG
2655 }
2656 i++;
2657 }
b551a063
DG
2658
2659 return nb_event;
2660
2661error:
2662 return ret;
9f19cc17
DG
2663}
2664
a4b92340
DG
2665
2666/*
2667 * Add URI so the consumer output object. Set the correct path depending on the
2668 * domain adding the default trace directory.
2669 */
2670static int add_uri_to_consumer(struct consumer_output *consumer,
07424f16 2671 struct lttng_uri *uri, int domain, const char *session_name)
a4b92340 2672{
07424f16 2673 int ret = LTTCOMM_OK;
a4b92340
DG
2674 const char *default_trace_dir;
2675
2676 assert(uri);
2677
2678 if (consumer == NULL) {
2679 DBG("No consumer detected. Don't add URI. Stopping.");
2680 ret = LTTCOMM_NO_CONSUMER;
2681 goto error;
2682 }
2683
2684 switch (domain) {
2685 case LTTNG_DOMAIN_KERNEL:
2686 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
2687 break;
2688 case LTTNG_DOMAIN_UST:
2689 default_trace_dir = DEFAULT_UST_TRACE_DIR;
2690 break;
2691 default:
2692 /*
2693 * This case is possible is we try to add the URI to the global tracing
2694 * session consumer object which in this case there is no subdir.
2695 */
2696 default_trace_dir = "";
2697 }
2698
2699 switch (uri->dtype) {
2700 case LTTNG_DST_IPV4:
2701 case LTTNG_DST_IPV6:
2702 DBG2("Setting network URI to consumer");
2703
2704 /* Set URI into consumer output object */
2705 ret = consumer_set_network_uri(consumer, uri);
2706 if (ret < 0) {
2707 ret = LTTCOMM_FATAL;
2708 goto error;
2709 }
2710
07424f16
DG
2711 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
2712 ret = set_consumer_subdir(consumer, session_name);
2713 if (ret < 0) {
2714 ret = LTTCOMM_FATAL;
2715 goto error;
2716 }
2717 }
2718
2719 if (uri->stype == LTTNG_STREAM_CONTROL) {
2720 /* On a new subdir, reappend the default trace dir. */
2721 strncat(consumer->subdir, default_trace_dir, sizeof(consumer->subdir));
2722 DBG3("Append domain trace name to subdir %s", consumer->subdir);
a4b92340
DG
2723 }
2724
2725 break;
2726 case LTTNG_DST_PATH:
2727 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
2728 memset(consumer->dst.trace_path, 0,
2729 sizeof(consumer->dst.trace_path));
2730 strncpy(consumer->dst.trace_path, uri->dst.path,
2731 sizeof(consumer->dst.trace_path));
2732 /* Append default trace dir */
2733 strncat(consumer->dst.trace_path, default_trace_dir,
2734 sizeof(consumer->dst.trace_path));
2735 /* Flag consumer as local. */
2736 consumer->type = CONSUMER_DST_LOCAL;
2737 break;
2738 }
2739
2740error:
2741 return ret;
2742}
2743
fac6795d 2744/*
54d01ffb 2745 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
fac6795d 2746 */
54d01ffb
DG
2747static int cmd_disable_channel(struct ltt_session *session,
2748 int domain, char *channel_name)
fac6795d 2749{
54d01ffb 2750 int ret;
78f0bacd
DG
2751 struct ltt_ust_session *usess;
2752
2753 usess = session->ust_session;
379473d2 2754
54d01ffb 2755 switch (domain) {
78f0bacd
DG
2756 case LTTNG_DOMAIN_KERNEL:
2757 {
2758 ret = channel_kernel_disable(session->kernel_session,
2759 channel_name);
2760 if (ret != LTTCOMM_OK) {
2761 goto error;
2762 }
54d01ffb 2763
78f0bacd
DG
2764 kernel_wait_quiescent(kernel_tracer_fd);
2765 break;
2766 }
2767 case LTTNG_DOMAIN_UST:
2768 {
2769 struct ltt_ust_channel *uchan;
bec39940 2770 struct lttng_ht *chan_ht;
78f0bacd 2771
7885e399
DG
2772 chan_ht = usess->domain_global.channels;
2773
2774 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
78f0bacd
DG
2775 if (uchan == NULL) {
2776 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
54d01ffb 2777 goto error;
78f0bacd
DG
2778 }
2779
7885e399
DG
2780 ret = channel_ust_disable(usess, domain, uchan);
2781 if (ret != LTTCOMM_OK) {
78f0bacd
DG
2782 goto error;
2783 }
78f0bacd
DG
2784 break;
2785 }
d78d6610 2786#if 0
78f0bacd
DG
2787 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2788 case LTTNG_DOMAIN_UST_EXEC_NAME:
2789 case LTTNG_DOMAIN_UST_PID:
d78d6610 2790#endif
78f0bacd
DG
2791 default:
2792 ret = LTTCOMM_UNKNOWN_DOMAIN;
2793 goto error;
20fe2104
DG
2794 }
2795
54d01ffb 2796 ret = LTTCOMM_OK;
d65106b1 2797
54d01ffb
DG
2798error:
2799 return ret;
2800}
2801
2802/*
2803 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2804 */
44d3bd01 2805static int cmd_enable_channel(struct ltt_session *session,
aea829b3 2806 int domain, struct lttng_channel *attr)
54d01ffb
DG
2807{
2808 int ret;
f6a9efaa 2809 struct ltt_ust_session *usess = session->ust_session;
bec39940 2810 struct lttng_ht *chan_ht;
f6a9efaa 2811
5d56b5aa 2812 DBG("Enabling channel %s for session %s", attr->name, session->name);
d65106b1 2813
aea829b3 2814 switch (domain) {
44d3bd01
DG
2815 case LTTNG_DOMAIN_KERNEL:
2816 {
2817 struct ltt_kernel_channel *kchan;
54d01ffb 2818
44d3bd01
DG
2819 kchan = trace_kernel_get_channel_by_name(attr->name,
2820 session->kernel_session);
2821 if (kchan == NULL) {
2822 ret = channel_kernel_create(session->kernel_session,
2823 attr, kernel_poll_pipe[1]);
2824 } else {
2825 ret = channel_kernel_enable(session->kernel_session, kchan);
2826 }
2827
2828 if (ret != LTTCOMM_OK) {
2829 goto error;
2830 }
2831
2832 kernel_wait_quiescent(kernel_tracer_fd);
2833 break;
2834 }
f6a9efaa
DG
2835 case LTTNG_DOMAIN_UST:
2836 {
2837 struct ltt_ust_channel *uchan;
2838
7885e399 2839 chan_ht = usess->domain_global.channels;
f6a9efaa 2840
7885e399 2841 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
f6a9efaa 2842 if (uchan == NULL) {
7885e399 2843 ret = channel_ust_create(usess, domain, attr);
f6a9efaa 2844 } else {
7885e399 2845 ret = channel_ust_enable(usess, domain, uchan);
48842b30 2846 }
f6a9efaa
DG
2847 break;
2848 }
d78d6610 2849#if 0
78f0bacd
DG
2850 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2851 case LTTNG_DOMAIN_UST_EXEC_NAME:
44d3bd01 2852 case LTTNG_DOMAIN_UST_PID:
d78d6610 2853#endif
44d3bd01
DG
2854 default:
2855 ret = LTTCOMM_UNKNOWN_DOMAIN;
2856 goto error;
54d01ffb
DG
2857 }
2858
54d01ffb
DG
2859error:
2860 return ret;
2861}
2862
2863/*
2864 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2865 */
2866static int cmd_disable_event(struct ltt_session *session, int domain,
2867 char *channel_name, char *event_name)
2868{
2869 int ret;
54d01ffb
DG
2870
2871 switch (domain) {
2872 case LTTNG_DOMAIN_KERNEL:
2bdd86d4
MD
2873 {
2874 struct ltt_kernel_channel *kchan;
b0a40d28 2875 struct ltt_kernel_session *ksess;
2bdd86d4 2876
b0a40d28
DG
2877 ksess = session->kernel_session;
2878
2879 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
54d01ffb
DG
2880 if (kchan == NULL) {
2881 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2882 goto error;
d65106b1
DG
2883 }
2884
b0a40d28 2885 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
54d01ffb
DG
2886 if (ret != LTTCOMM_OK) {
2887 goto error;
2888 }
2889
2890 kernel_wait_quiescent(kernel_tracer_fd);
d65106b1 2891 break;
2bdd86d4
MD
2892 }
2893 case LTTNG_DOMAIN_UST:
b0a40d28 2894 {
b0a40d28 2895 struct ltt_ust_channel *uchan;
7f79d3a1 2896 struct ltt_ust_session *usess;
b0a40d28
DG
2897
2898 usess = session->ust_session;
2899
2900 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2901 channel_name);
2902 if (uchan == NULL) {
2903 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2904 goto error;
2905 }
2906
7f79d3a1
DG
2907 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
2908 if (ret != LTTCOMM_OK) {
b0a40d28
DG
2909 goto error;
2910 }
2911
7f79d3a1 2912 DBG3("Disable UST event %s in channel %s completed", event_name,
b0a40d28 2913 channel_name);
b0a40d28
DG
2914 break;
2915 }
d78d6610 2916#if 0
2bdd86d4
MD
2917 case LTTNG_DOMAIN_UST_EXEC_NAME:
2918 case LTTNG_DOMAIN_UST_PID:
2919 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 2920#endif
54d01ffb 2921 default:
1ab1ea0b 2922 ret = LTTCOMM_UND;
54d01ffb 2923 goto error;
d65106b1 2924 }
26cc6b4e 2925
54d01ffb
DG
2926 ret = LTTCOMM_OK;
2927
2928error:
2929 return ret;
2930}
2931
2932/*
2933 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2934 */
2935static int cmd_disable_event_all(struct ltt_session *session, int domain,
2936 char *channel_name)
2937{
2938 int ret;
54d01ffb
DG
2939
2940 switch (domain) {
2941 case LTTNG_DOMAIN_KERNEL:
9730260e
DG
2942 {
2943 struct ltt_kernel_session *ksess;
2944 struct ltt_kernel_channel *kchan;
2945
2946 ksess = session->kernel_session;
2947
2948 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
54d01ffb
DG
2949 if (kchan == NULL) {
2950 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2951 goto error;
26cc6b4e
DG
2952 }
2953
9730260e 2954 ret = event_kernel_disable_all(ksess, kchan);
54d01ffb 2955 if (ret != LTTCOMM_OK) {
0b97ec54 2956 goto error;
26cc6b4e
DG
2957 }
2958
54d01ffb 2959 kernel_wait_quiescent(kernel_tracer_fd);
26cc6b4e 2960 break;
9730260e
DG
2961 }
2962 case LTTNG_DOMAIN_UST:
2963 {
2964 struct ltt_ust_session *usess;
2965 struct ltt_ust_channel *uchan;
2966
2967 usess = session->ust_session;
2968
2969 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2970 channel_name);
2971 if (uchan == NULL) {
2972 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2973 goto error;
2974 }
2975
7f79d3a1
DG
2976 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
2977 if (ret != 0) {
9730260e
DG
2978 goto error;
2979 }
2980
7f79d3a1 2981 DBG3("Disable all UST events in channel %s completed", channel_name);
9730260e
DG
2982
2983 break;
2984 }
d78d6610 2985#if 0
9730260e
DG
2986 case LTTNG_DOMAIN_UST_EXEC_NAME:
2987 case LTTNG_DOMAIN_UST_PID:
2988 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 2989#endif
54d01ffb 2990 default:
1ab1ea0b 2991 ret = LTTCOMM_UND;
54d01ffb 2992 goto error;
26cc6b4e 2993 }
e953ef25 2994
54d01ffb 2995 ret = LTTCOMM_OK;
e953ef25 2996
54d01ffb
DG
2997error:
2998 return ret;
2999}
f5177a38 3000
54d01ffb
DG
3001/*
3002 * Command LTTNG_ADD_CONTEXT processed by the client thread.
3003 */
3004static int cmd_add_context(struct ltt_session *session, int domain,
3005 char *channel_name, char *event_name, struct lttng_event_context *ctx)
3006{
3007 int ret;
f5177a38 3008
54d01ffb
DG
3009 switch (domain) {
3010 case LTTNG_DOMAIN_KERNEL:
3011 /* Add kernel context to kernel tracer */
3012 ret = context_kernel_add(session->kernel_session, ctx,
3013 event_name, channel_name);
3014 if (ret != LTTCOMM_OK) {
f5177a38 3015 goto error;
e953ef25 3016 }
2bdd86d4
MD
3017 break;
3018 case LTTNG_DOMAIN_UST:
3019 {
55cc08a6 3020 struct ltt_ust_session *usess = session->ust_session;
e953ef25 3021
55cc08a6
DG
3022 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
3023 if (ret != LTTCOMM_OK) {
3024 goto error;
2bdd86d4 3025 }
e953ef25 3026 break;
2bdd86d4 3027 }
d78d6610 3028#if 0
55cc08a6
DG
3029 case LTTNG_DOMAIN_UST_EXEC_NAME:
3030 case LTTNG_DOMAIN_UST_PID:
3031 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3032#endif
54d01ffb 3033 default:
1ab1ea0b 3034 ret = LTTCOMM_UND;
54d01ffb 3035 goto error;
e953ef25 3036 }
950131af 3037
54d01ffb 3038 ret = LTTCOMM_OK;
950131af 3039
54d01ffb
DG
3040error:
3041 return ret;
3042}
3043
53a80697
MD
3044/*
3045 * Command LTTNG_SET_FILTER processed by the client thread.
3046 */
3047static int cmd_set_filter(struct ltt_session *session, int domain,
3048 char *channel_name, char *event_name,
3049 struct lttng_filter_bytecode *bytecode)
3050{
3051 int ret;
3052
3053 switch (domain) {
3054 case LTTNG_DOMAIN_KERNEL:
3055 ret = LTTCOMM_FATAL;
3056 break;
3057 case LTTNG_DOMAIN_UST:
3058 {
3059 struct ltt_ust_session *usess = session->ust_session;
3060
3061 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
3062 if (ret != LTTCOMM_OK) {
3063 goto error;
3064 }
3065 break;
3066 }
3067#if 0
3068 case LTTNG_DOMAIN_UST_EXEC_NAME:
3069 case LTTNG_DOMAIN_UST_PID:
3070 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
3071#endif
3072 default:
3073 ret = LTTCOMM_UND;
3074 goto error;
3075 }
3076
3077 ret = LTTCOMM_OK;
3078
3079error:
3080 return ret;
3081
3082}
3083
54d01ffb
DG
3084/*
3085 * Command LTTNG_ENABLE_EVENT processed by the client thread.
3086 */
3087static int cmd_enable_event(struct ltt_session *session, int domain,
3088 char *channel_name, struct lttng_event *event)
3089{
3090 int ret;
f6cd6b0f 3091 struct lttng_channel *attr;
48842b30 3092 struct ltt_ust_session *usess = session->ust_session;
54d01ffb
DG
3093
3094 switch (domain) {
3095 case LTTNG_DOMAIN_KERNEL:
2bdd86d4
MD
3096 {
3097 struct ltt_kernel_channel *kchan;
3098
54d01ffb
DG
3099 kchan = trace_kernel_get_channel_by_name(channel_name,
3100 session->kernel_session);
3101 if (kchan == NULL) {
f6cd6b0f
DG
3102 attr = channel_new_default_attr(domain);
3103 if (attr == NULL) {
3104 ret = LTTCOMM_FATAL;
3105 goto error;
3106 }
3107 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3108
54d01ffb 3109 /* This call will notify the kernel thread */
44d3bd01 3110 ret = channel_kernel_create(session->kernel_session,
f6cd6b0f 3111 attr, kernel_poll_pipe[1]);
54d01ffb 3112 if (ret != LTTCOMM_OK) {
ff4d74e6 3113 free(attr);
f5177a38
DG
3114 goto error;
3115 }
ff4d74e6 3116 free(attr);
54d01ffb 3117 }
950131af 3118
54d01ffb
DG
3119 /* Get the newly created kernel channel pointer */
3120 kchan = trace_kernel_get_channel_by_name(channel_name,
3121 session->kernel_session);
3122 if (kchan == NULL) {
3123 /* This sould not happen... */
3124 ret = LTTCOMM_FATAL;
3125 goto error;
3126 }
f5177a38 3127
48842b30
DG
3128 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
3129 event);
54d01ffb 3130 if (ret != LTTCOMM_OK) {
f5177a38 3131 goto error;
950131af
DG
3132 }
3133
54d01ffb 3134 kernel_wait_quiescent(kernel_tracer_fd);
950131af 3135 break;
2bdd86d4
MD
3136 }
3137 case LTTNG_DOMAIN_UST:
3138 {
aea829b3 3139 struct lttng_channel *attr;
edb67388 3140 struct ltt_ust_channel *uchan;
2bdd86d4 3141
edb67388 3142 /* Get channel from global UST domain */
48842b30
DG
3143 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3144 channel_name);
3145 if (uchan == NULL) {
aea829b3
DG
3146 /* Create default channel */
3147 attr = channel_new_default_attr(domain);
3148 if (attr == NULL) {
3149 ret = LTTCOMM_FATAL;
3150 goto error;
3151 }
596219f7 3152 snprintf(attr->name, NAME_MAX, "%s", channel_name);
edb67388 3153 attr->name[NAME_MAX - 1] = '\0';
aea829b3 3154
7885e399 3155 ret = channel_ust_create(usess, domain, attr);
edb67388 3156 if (ret != LTTCOMM_OK) {
ff4d74e6 3157 free(attr);
aea829b3
DG
3158 goto error;
3159 }
aea829b3
DG
3160 free(attr);
3161
3162 /* Get the newly created channel reference back */
3163 uchan = trace_ust_find_channel_by_name(
3164 usess->domain_global.channels, channel_name);
3165 if (uchan == NULL) {
3166 /* Something is really wrong */
3167 ret = LTTCOMM_FATAL;
3168 goto error;
3169 }
48842b30 3170 }
2bdd86d4 3171
edb67388 3172 /* At this point, the session and channel exist on the tracer */
edb67388
DG
3173 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
3174 if (ret != LTTCOMM_OK) {
48842b30 3175 goto error;
2bdd86d4
MD
3176 }
3177 break;
3178 }
d78d6610 3179#if 0
2bdd86d4
MD
3180 case LTTNG_DOMAIN_UST_EXEC_NAME:
3181 case LTTNG_DOMAIN_UST_PID:
3182 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3183#endif
54d01ffb 3184 default:
1ab1ea0b 3185 ret = LTTCOMM_UND;
54d01ffb 3186 goto error;
950131af 3187 }
d36b8583 3188
54d01ffb 3189 ret = LTTCOMM_OK;
d36b8583 3190
54d01ffb
DG
3191error:
3192 return ret;
3193}
7d29a247 3194
54d01ffb
DG
3195/*
3196 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
3197 */
3198static int cmd_enable_event_all(struct ltt_session *session, int domain,
8c9ae521 3199 char *channel_name, int event_type)
54d01ffb
DG
3200{
3201 int ret;
3202 struct ltt_kernel_channel *kchan;
7d29a247 3203
54d01ffb
DG
3204 switch (domain) {
3205 case LTTNG_DOMAIN_KERNEL:
3206 kchan = trace_kernel_get_channel_by_name(channel_name,
3207 session->kernel_session);
3208 if (kchan == NULL) {
3209 /* This call will notify the kernel thread */
44d3bd01
DG
3210 ret = channel_kernel_create(session->kernel_session, NULL,
3211 kernel_poll_pipe[1]);
54d01ffb
DG
3212 if (ret != LTTCOMM_OK) {
3213 goto error;
d36b8583 3214 }
0d0c377a 3215
8f69e5eb
DG
3216 /* Get the newly created kernel channel pointer */
3217 kchan = trace_kernel_get_channel_by_name(channel_name,
3218 session->kernel_session);
3219 if (kchan == NULL) {
3220 /* This sould not happen... */
3221 ret = LTTCOMM_FATAL;
3222 goto error;
3223 }
3224
54d01ffb 3225 }
0fdd1e2c 3226
7a3d1328 3227 switch (event_type) {
76d45b40 3228 case LTTNG_EVENT_SYSCALL:
7a3d1328 3229 ret = event_kernel_enable_all_syscalls(session->kernel_session,
8c9ae521 3230 kchan, kernel_tracer_fd);
7a3d1328 3231 break;
76d45b40 3232 case LTTNG_EVENT_TRACEPOINT:
8c9ae521 3233 /*
7a3d1328
MD
3234 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3235 * events already registered to the channel.
8c9ae521 3236 */
7a3d1328
MD
3237 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
3238 kchan, kernel_tracer_fd);
3239 break;
76d45b40 3240 case LTTNG_EVENT_ALL:
7a3d1328 3241 /* Enable syscalls and tracepoints */
8c9ae521
DG
3242 ret = event_kernel_enable_all(session->kernel_session,
3243 kchan, kernel_tracer_fd);
7a3d1328
MD
3244 break;
3245 default:
3246 ret = LTTCOMM_KERN_ENABLE_FAIL;
3247 goto error;
8c9ae521 3248 }
8f69e5eb
DG
3249
3250 /* Manage return value */
54d01ffb 3251 if (ret != LTTCOMM_OK) {
0d0c377a 3252 goto error;
d36b8583
DG
3253 }
3254
54d01ffb 3255 kernel_wait_quiescent(kernel_tracer_fd);
d36b8583 3256 break;
76d45b40
DG
3257 case LTTNG_DOMAIN_UST:
3258 {
3259 struct lttng_channel *attr;
3260 struct ltt_ust_channel *uchan;
3261 struct ltt_ust_session *usess = session->ust_session;
3262
3263 /* Get channel from global UST domain */
3264 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3265 channel_name);
3266 if (uchan == NULL) {
3267 /* Create default channel */
3268 attr = channel_new_default_attr(domain);
3269 if (attr == NULL) {
3270 ret = LTTCOMM_FATAL;
3271 goto error;
3272 }
3273 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3274 attr->name[NAME_MAX - 1] = '\0';
3275
3276 /* Use the internal command enable channel */
7885e399 3277 ret = channel_ust_create(usess, domain, attr);
76d45b40
DG
3278 if (ret != LTTCOMM_OK) {
3279 free(attr);
3280 goto error;
3281 }
3282 free(attr);
3283
3284 /* Get the newly created channel reference back */
3285 uchan = trace_ust_find_channel_by_name(
3286 usess->domain_global.channels, channel_name);
3287 if (uchan == NULL) {
3288 /* Something is really wrong */
3289 ret = LTTCOMM_FATAL;
3290 goto error;
3291 }
3292 }
3293
3294 /* At this point, the session and channel exist on the tracer */
3295
3296 switch (event_type) {
3297 case LTTNG_EVENT_ALL:
3298 case LTTNG_EVENT_TRACEPOINT:
3299 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
3300 if (ret != LTTCOMM_OK) {
3301 goto error;
3302 }
3303 break;
3304 default:
3305 ret = LTTCOMM_UST_ENABLE_FAIL;
3306 goto error;
3307 }
3308
3309 /* Manage return value */
3310 if (ret != LTTCOMM_OK) {
3311 goto error;
3312 }
3313
3314 break;
3315 }
d78d6610 3316#if 0
76d45b40
DG
3317 case LTTNG_DOMAIN_UST_EXEC_NAME:
3318 case LTTNG_DOMAIN_UST_PID:
3319 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3320#endif
54d01ffb 3321 default:
1ab1ea0b 3322 ret = LTTCOMM_UND;
54d01ffb 3323 goto error;
d36b8583 3324 }
f3ed775e 3325
54d01ffb
DG
3326 ret = LTTCOMM_OK;
3327
3328error:
3329 return ret;
3330}
3331
3332/*
3333 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3334 */
3335static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
3336{
3337 int ret;
3338 ssize_t nb_events = 0;
3339
3340 switch (domain) {
3341 case LTTNG_DOMAIN_KERNEL:
3342 nb_events = kernel_list_events(kernel_tracer_fd, events);
3343 if (nb_events < 0) {
3344 ret = LTTCOMM_KERN_LIST_FAIL;
3345 goto error;
894be886 3346 }
54d01ffb 3347 break;
b551a063
DG
3348 case LTTNG_DOMAIN_UST:
3349 nb_events = ust_app_list_events(events);
3350 if (nb_events < 0) {
3351 ret = LTTCOMM_UST_LIST_FAIL;
3352 goto error;
3353 }
3354 break;
54d01ffb 3355 default:
1ab1ea0b 3356 ret = LTTCOMM_UND;
54d01ffb
DG
3357 goto error;
3358 }
894be886 3359
54d01ffb 3360 return nb_events;
7d29a247 3361
54d01ffb
DG
3362error:
3363 /* Return negative value to differentiate return code */
3364 return -ret;
3365}
b389abbe 3366
f37d259d
MD
3367/*
3368 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3369 */
3370static ssize_t cmd_list_tracepoint_fields(int domain,
3371 struct lttng_event_field **fields)
3372{
3373 int ret;
3374 ssize_t nb_fields = 0;
3375
3376 switch (domain) {
3377 case LTTNG_DOMAIN_UST:
3378 nb_fields = ust_app_list_event_fields(fields);
3379 if (nb_fields < 0) {
3380 ret = LTTCOMM_UST_LIST_FAIL;
3381 goto error;
3382 }
3383 break;
3384 case LTTNG_DOMAIN_KERNEL:
3385 default: /* fall-through */
3386 ret = LTTCOMM_UND;
3387 goto error;
3388 }
3389
3390 return nb_fields;
3391
3392error:
3393 /* Return negative value to differentiate return code */
3394 return -ret;
3395}
3396
54d01ffb
DG
3397/*
3398 * Command LTTNG_START_TRACE processed by the client thread.
3399 */
3400static int cmd_start_trace(struct ltt_session *session)
3401{
3402 int ret;
54d01ffb 3403 struct ltt_kernel_session *ksession;
b9d9b220 3404 struct ltt_ust_session *usess;
00e2e675 3405 struct ltt_kernel_channel *kchan;
b389abbe 3406
00e2e675 3407 /* Ease our life a bit ;) */
54d01ffb 3408 ksession = session->kernel_session;
b9d9b220 3409 usess = session->ust_session;
5eb91c98 3410
b9ef1c83 3411 if (session->enabled) {
80e327fa
DG
3412 /* Already started. */
3413 ret = LTTCOMM_TRACE_ALREADY_STARTED;
b9ef1c83
DG
3414 goto error;
3415 }
3416
464dd62d
MD
3417 session->enabled = 1;
3418
00e2e675
DG
3419 ret = setup_relayd(session);
3420 if (ret != LTTCOMM_OK) {
3421 ERR("Error setting up relayd for session %s", session->name);
3422 goto error;
3423 }
3424
54d01ffb
DG
3425 /* Kernel tracing */
3426 if (ksession != NULL) {
3427 /* Open kernel metadata */
3428 if (ksession->metadata == NULL) {
a4b92340 3429 ret = kernel_open_metadata(ksession);
54d01ffb
DG
3430 if (ret < 0) {
3431 ret = LTTCOMM_KERN_META_FAIL;
3432 goto error;
b389abbe 3433 }
54d01ffb 3434 }
7d29a247 3435
54d01ffb 3436 /* Open kernel metadata stream */
03550b58 3437 if (ksession->metadata_stream_fd < 0) {
54d01ffb
DG
3438 ret = kernel_open_metadata_stream(ksession);
3439 if (ret < 0) {
3440 ERR("Kernel create metadata stream failed");
3441 ret = LTTCOMM_KERN_STREAM_FAIL;
3442 goto error;
3443 }
3444 }
3445
3446 /* For each channel */
3447 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3448 if (kchan->stream_count == 0) {
3449 ret = kernel_open_channel_stream(kchan);
3450 if (ret < 0) {
3451 ret = LTTCOMM_KERN_STREAM_FAIL;
7d29a247
DG
3452 goto error;
3453 }
54d01ffb
DG
3454 /* Update the stream global counter */
3455 ksession->stream_count_global += ret;
7d29a247 3456 }
54d01ffb
DG
3457 }
3458
3459 /* Setup kernel consumer socket and send fds to it */
3460 ret = init_kernel_tracing(ksession);
3461 if (ret < 0) {
3462 ret = LTTCOMM_KERN_START_FAIL;
3463 goto error;
3464 }
3465
3466 /* This start the kernel tracing */
3467 ret = kernel_start_session(ksession);
3468 if (ret < 0) {
3469 ret = LTTCOMM_KERN_START_FAIL;
3470 goto error;
3471 }
3472
3473 /* Quiescent wait after starting trace */
3474 kernel_wait_quiescent(kernel_tracer_fd);
3475 }
3476
36dc12cc 3477 /* Flag session that trace should start automatically */
b9d9b220
DG
3478 if (usess) {
3479 usess->start_trace = 1;
36dc12cc 3480
b9d9b220
DG
3481 ret = ust_app_start_trace_all(usess);
3482 if (ret < 0) {
3483 ret = LTTCOMM_UST_START_FAIL;
3484 goto error;
3485 }
2bdd86d4 3486 }
54d01ffb
DG
3487
3488 ret = LTTCOMM_OK;
3489
3490error:
3491 return ret;
3492}
3493
3494/*
3495 * Command LTTNG_STOP_TRACE processed by the client thread.
3496 */
3497static int cmd_stop_trace(struct ltt_session *session)
3498{
3499 int ret;
3500 struct ltt_kernel_channel *kchan;
3501 struct ltt_kernel_session *ksession;
8be98f9a 3502 struct ltt_ust_session *usess;
54d01ffb
DG
3503
3504 /* Short cut */
3505 ksession = session->kernel_session;
8be98f9a 3506 usess = session->ust_session;
54d01ffb 3507
b9ef1c83 3508 if (!session->enabled) {
80e327fa 3509 ret = LTTCOMM_TRACE_ALREADY_STOPPED;
b9ef1c83
DG
3510 goto error;
3511 }
3512
464dd62d
MD
3513 session->enabled = 0;
3514
54d01ffb
DG
3515 /* Kernel tracer */
3516 if (ksession != NULL) {
3517 DBG("Stop kernel tracing");
3518
00e2e675
DG
3519 /* Flush metadata if exist */
3520 if (ksession->metadata_stream_fd >= 0) {
3521 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
3522 if (ret < 0) {
3523 ERR("Kernel metadata flush failed");
3524 }
54d01ffb 3525 }
f34daff7 3526
00e2e675 3527 /* Flush all buffers before stopping */
54d01ffb
DG
3528 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3529 ret = kernel_flush_buffer(kchan);
0d0c377a 3530 if (ret < 0) {
54d01ffb 3531 ERR("Kernel flush buffer error");
7d29a247 3532 }
54d01ffb 3533 }
19e70852 3534
54d01ffb
DG
3535 ret = kernel_stop_session(ksession);
3536 if (ret < 0) {
3537 ret = LTTCOMM_KERN_STOP_FAIL;
19e70852 3538 goto error;
f3ed775e 3539 }
54d01ffb
DG
3540
3541 kernel_wait_quiescent(kernel_tracer_fd);
894be886 3542 }
54d01ffb 3543
8be98f9a
MD
3544 if (usess) {
3545 usess->start_trace = 0;
2bdd86d4 3546
8be98f9a 3547 ret = ust_app_stop_trace_all(usess);
2bdd86d4 3548 if (ret < 0) {
190df5ac 3549 ret = LTTCOMM_UST_STOP_FAIL;
2bdd86d4
MD
3550 goto error;
3551 }
2bdd86d4 3552 }
54d01ffb
DG
3553
3554 ret = LTTCOMM_OK;
3555
3556error:
3557 return ret;
3558}
3559
3560/*
07424f16 3561 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
54d01ffb 3562 */
07424f16
DG
3563static int cmd_set_consumer_uri(int domain, struct ltt_session *session,
3564 size_t nb_uri, struct lttng_uri *uris)
54d01ffb 3565{
07424f16
DG
3566 int ret, i;
3567 struct ltt_kernel_session *ksess = session->kernel_session;
3568 struct ltt_ust_session *usess = session->ust_session;
a4b92340 3569 struct consumer_output *consumer = NULL;
a4b92340 3570
07424f16
DG
3571 assert(session);
3572 assert(uris);
3573 assert(nb_uri > 0);
00e2e675 3574
07424f16
DG
3575 /* Can't enable consumer after session started. */
3576 if (session->enabled) {
3577 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3578 goto error;
a4b92340
DG
3579 }
3580
07424f16
DG
3581 if (!session->start_consumer) {
3582 ret = LTTCOMM_NO_CONSUMER;
00e2e675
DG
3583 goto error;
3584 }
a4b92340
DG
3585
3586 /*
07424f16
DG
3587 * This case switch makes sure the domain session has a temporary consumer
3588 * so the URL can be set.
a4b92340 3589 */
07424f16
DG
3590 switch (domain) {
3591 case 0:
3592 /* Code flow error. A session MUST always have a consumer object */
3593 assert(session->consumer);
a4b92340 3594 /*
07424f16
DG
3595 * The URL will be added to the tracing session consumer instead of a
3596 * specific domain consumer.
a4b92340 3597 */
07424f16
DG
3598 consumer = session->consumer;
3599 break;
3600 case LTTNG_DOMAIN_KERNEL:
3601 /* Code flow error if we don't have a kernel session here. */
3602 assert(ksess);
a4b92340 3603
07424f16
DG
3604 /* Create consumer output if none exists */
3605 consumer = ksess->tmp_consumer;
3606 if (consumer == NULL) {
3607 consumer = consumer_copy_output(ksess->consumer);
3608 if (consumer == NULL) {
3609 ret = LTTCOMM_FATAL;
3610 goto error;
3611 }
3612 /* Trash the consumer subdir, we are about to set a new one. */
3613 memset(consumer->subdir, 0, sizeof(consumer->subdir));
3614 ksess->tmp_consumer = consumer;
3615 }
a4b92340 3616
07424f16
DG
3617 break;
3618 case LTTNG_DOMAIN_UST:
3619 /* Code flow error if we don't have a kernel session here. */
3620 assert(usess);
a4b92340 3621
07424f16
DG
3622 /* Create consumer output if none exists */
3623 consumer = usess->tmp_consumer;
3624 if (consumer == NULL) {
3625 consumer = consumer_copy_output(usess->consumer);
3626 if (consumer == NULL) {
3627 ret = LTTCOMM_FATAL;
3628 goto error;
3629 }
3630 /* Trash the consumer subdir, we are about to set a new one. */
3631 memset(consumer->subdir, 0, sizeof(consumer->subdir));
3632 usess->tmp_consumer = consumer;
3633 }
3634
3635 break;
a4b92340 3636 }
00e2e675 3637
07424f16
DG
3638 for (i = 0; i < nb_uri; i++) {
3639 struct consumer_socket *socket;
3640 struct lttng_ht_iter iter;
a4b92340 3641
07424f16 3642 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
00e2e675 3643 if (ret < 0) {
00e2e675
DG
3644 goto error;
3645 }
3646
07424f16
DG
3647 /*
3648 * Don't send relayd socket if URI is NOT remote or if the relayd
3649 * sockets for the session are already sent.
3650 */
3651 if (uris[i].dtype == LTTNG_DST_PATH ||
3652 consumer->dst.net.relayd_socks_sent) {
3653 continue;
00e2e675
DG
3654 }
3655
07424f16
DG
3656 /* Try to send relayd URI to the consumer if exist. */
3657 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter,
3658 socket, node.node) {
3659
3660 /* A socket in the HT should never have a negative fd */
3661 assert(socket->fd >= 0);
3662
3663 pthread_mutex_lock(socket->lock);
3664 ret = send_socket_relayd_consumer(domain, session, &uris[i],
3665 consumer, socket->fd);
3666 pthread_mutex_unlock(socket->lock);
3667 if (ret != LTTCOMM_OK) {
3668 goto error;
3669 }
3670 }
00e2e675
DG
3671 }
3672
07424f16
DG
3673 /* All good! */
3674 ret = LTTCOMM_OK;
3675
3676error:
3677 return ret;
3678}
3679
3680
3681/*
3682 * Command LTTNG_CREATE_SESSION processed by the client thread.
3683 */
3684static int cmd_create_session_uri(char *name, struct lttng_uri *uris,
3685 size_t nb_uri, lttng_sock_cred *creds)
3686{
3687 int ret;
3688 char *path = NULL;
3689 struct ltt_session *session;
3690 //struct consumer_output *consumer = NULL;
3691 //struct lttng_uri *ctrl_uri, *data_uri = NULL;
3692
3693 assert(name);
3694
3695 /*
3696 * Verify if the session already exist
3697 *
3698 * XXX: There is no need for the session lock list here since the caller
3699 * (process_client_msg) is holding it. We might want to change that so a
3700 * single command does not lock the entire session list.
3701 */
3702 session = session_find_by_name(name);
3703 if (session != NULL) {
3704 ret = LTTCOMM_EXIST_SESS;
3705 goto find_error;
3706 }
54d01ffb 3707
a4b92340 3708 /* Create tracing session in the registry */
730389d9
DG
3709 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
3710 LTTNG_SOCK_GET_GID_CRED(creds));
54d01ffb 3711 if (ret != LTTCOMM_OK) {
07424f16 3712 goto session_error;
54d01ffb
DG
3713 }
3714
a4b92340
DG
3715 /*
3716 * Get the newly created session pointer back
3717 *
3718 * XXX: There is no need for the session lock list here since the caller
3719 * (process_client_msg) is holding it. We might want to change that so a
3720 * single command does not lock the entire session list.
3721 */
00e2e675
DG
3722 session = session_find_by_name(name);
3723 assert(session);
3724
07424f16
DG
3725 /* Create default consumer output for the session not yet created. */
3726 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
3727 if (session->consumer == NULL) {
3728 ret = LTTCOMM_FATAL;
3729 goto consumer_error;
3730 }
00e2e675 3731
07424f16
DG
3732 /*
3733 * This means that the lttng_create_session call was called with the _path_
3734 * argument set to NULL.
3735 */
3736 if (uris == NULL) {
3737 /*
3738 * At this point, we'll skip the consumer URI setup and create a
3739 * session with a NULL path which will flag the session to NOT spawn a
3740 * consumer.
3741 */
3742 DBG("Create session %s with NO uri, skipping consumer setup", name);
3743 goto end;
a4b92340 3744 }
07424f16
DG
3745
3746 session->start_consumer = 1;
3747
3748 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
3749 if (ret != LTTCOMM_OK) {
3750 goto consumer_error;
00e2e675 3751 }
54d01ffb 3752
07424f16
DG
3753 session->consumer->enabled = 1;
3754
3755end:
a4b92340
DG
3756 return LTTCOMM_OK;
3757
a4b92340 3758consumer_error:
07424f16
DG
3759 session_destroy(session);
3760session_error:
3761find_error:
54d01ffb
DG
3762 return ret;
3763}
3764
3765/*
3766 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3767 */
3768static int cmd_destroy_session(struct ltt_session *session, char *name)
3769{
3770 int ret;
3771
173af62f
DG
3772 /* Safety net */
3773 assert(session);
3774
54d01ffb
DG
3775 /* Clean kernel session teardown */
3776 teardown_kernel_session(session);
84cd17c6
MD
3777 /* UST session teardown */
3778 teardown_ust_session(session);
54d01ffb
DG
3779
3780 /*
3781 * Must notify the kernel thread here to update it's poll setin order
3782 * to remove the channel(s)' fd just destroyed.
3783 */
3784 ret = notify_thread_pipe(kernel_poll_pipe[1]);
3785 if (ret < 0) {
76d7553f 3786 PERROR("write kernel poll pipe");
54d01ffb
DG
3787 }
3788
271933a4 3789 ret = session_destroy(session);
54d01ffb
DG
3790
3791 return ret;
3792}
3793
3794/*
3795 * Command LTTNG_CALIBRATE processed by the client thread.
3796 */
3797static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
3798{
3799 int ret;
3800
3801 switch (domain) {
3802 case LTTNG_DOMAIN_KERNEL:
33a2b854 3803 {
54d01ffb 3804 struct lttng_kernel_calibrate kcalibrate;
33a2b854 3805
54d01ffb
DG
3806 kcalibrate.type = calibrate->type;
3807 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
33a2b854 3808 if (ret < 0) {
54d01ffb
DG
3809 ret = LTTCOMM_KERN_ENABLE_FAIL;
3810 goto error;
33a2b854 3811 }
54d01ffb
DG
3812 break;
3813 }
4466912f
DG
3814 case LTTNG_DOMAIN_UST:
3815 {
3816 struct lttng_ust_calibrate ucalibrate;
3817
3818 ucalibrate.type = calibrate->type;
3819 ret = ust_app_calibrate_glb(&ucalibrate);
3820 if (ret < 0) {
3821 ret = LTTCOMM_UST_CALIBRATE_FAIL;
3822 goto error;
3823 }
3824 break;
3825 }
54d01ffb 3826 default:
1ab1ea0b 3827 ret = LTTCOMM_UND;
54d01ffb
DG
3828 goto error;
3829 }
33a2b854 3830
54d01ffb 3831 ret = LTTCOMM_OK;
33a2b854 3832
54d01ffb
DG
3833error:
3834 return ret;
3835}
7d29a247 3836
54d01ffb
DG
3837/*
3838 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3839 */
3840static int cmd_register_consumer(struct ltt_session *session, int domain,
3841 char *sock_path)
3842{
3843 int ret, sock;
173af62f 3844 struct consumer_socket *socket;
b389abbe 3845
54d01ffb
DG
3846 switch (domain) {
3847 case LTTNG_DOMAIN_KERNEL:
3848 /* Can't register a consumer if there is already one */
48c4f28c 3849 if (session->kernel_session->consumer_fds_sent != 0) {
54d01ffb
DG
3850 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3851 goto error;
3852 }
3853
3854 sock = lttcomm_connect_unix_sock(sock_path);
3855 if (sock < 0) {
3856 ret = LTTCOMM_CONNECT_FAIL;
3857 goto error;
3858 }
3859
173af62f
DG
3860 socket = consumer_allocate_socket(sock);
3861 if (socket == NULL) {
3862 ret = LTTCOMM_FATAL;
3863 close(sock);
3864 goto error;
3865 }
3866
e8209f6b
DG
3867 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3868 if (socket->lock == NULL) {
3869 PERROR("zmalloc pthread mutex");
3870 ret = LTTCOMM_FATAL;
3871 goto error;
3872 }
3873 pthread_mutex_init(socket->lock, NULL);
3874
173af62f
DG
3875 rcu_read_lock();
3876 consumer_add_socket(socket, session->kernel_session->consumer);
3877 rcu_read_unlock();
3878
e8209f6b
DG
3879 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3880 kconsumer_data.pid = -1;
3881 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3882
54d01ffb
DG
3883 break;
3884 default:
3885 /* TODO: Userspace tracing */
1ab1ea0b 3886 ret = LTTCOMM_UND;
54d01ffb
DG
3887 goto error;
3888 }
3889
3890 ret = LTTCOMM_OK;
3891
3892error:
3893 return ret;
3894}
3895
3896/*
3897 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3898 */
3899static ssize_t cmd_list_domains(struct ltt_session *session,
3900 struct lttng_domain **domains)
3901{
b551a063 3902 int ret, index = 0;
54d01ffb
DG
3903 ssize_t nb_dom = 0;
3904
3905 if (session->kernel_session != NULL) {
b551a063 3906 DBG3("Listing domains found kernel domain");
54d01ffb
DG
3907 nb_dom++;
3908 }
3909
b551a063
DG
3910 if (session->ust_session != NULL) {
3911 DBG3("Listing domains found UST global domain");
3912 nb_dom++;
3913 }
54d01ffb 3914
b551a063 3915 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
54d01ffb
DG
3916 if (*domains == NULL) {
3917 ret = -LTTCOMM_FATAL;
3918 goto error;
3919 }
3920
b551a063
DG
3921 if (session->kernel_session != NULL) {
3922 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3923 index++;
3924 }
3925
3926 if (session->ust_session != NULL) {
3927 (*domains)[index].type = LTTNG_DOMAIN_UST;
3928 index++;
3929 }
54d01ffb 3930
54d01ffb
DG
3931 return nb_dom;
3932
3933error:
3934 return ret;
3935}
3936
3937/*
3938 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3939 */
b551a063 3940static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
54d01ffb
DG
3941 struct lttng_channel **channels)
3942{
3943 int ret;
3944 ssize_t nb_chan = 0;
3945
b551a063
DG
3946 switch (domain) {
3947 case LTTNG_DOMAIN_KERNEL:
3948 if (session->kernel_session != NULL) {
3949 nb_chan = session->kernel_session->channel_count;
3950 }
0df502fd 3951 DBG3("Number of kernel channels %zd", nb_chan);
b551a063
DG
3952 break;
3953 case LTTNG_DOMAIN_UST:
3954 if (session->ust_session != NULL) {
bec39940 3955 nb_chan = lttng_ht_get_count(
b551a063
DG
3956 session->ust_session->domain_global.channels);
3957 }
0df502fd 3958 DBG3("Number of UST global channels %zd", nb_chan);
b551a063
DG
3959 break;
3960 default:
3961 *channels = NULL;
1ab1ea0b 3962 ret = -LTTCOMM_UND;
54d01ffb
DG
3963 goto error;
3964 }
3965
b551a063
DG
3966 if (nb_chan > 0) {
3967 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
3968 if (*channels == NULL) {
3969 ret = -LTTCOMM_FATAL;
3970 goto error;
3971 }
54d01ffb 3972
b551a063
DG
3973 list_lttng_channels(domain, session, *channels);
3974 } else {
3975 *channels = NULL;
3976 }
2bdd86d4 3977
54d01ffb
DG
3978 return nb_chan;
3979
3980error:
3981 return ret;
3982}
3983
3984/*
3985 * Command LTTNG_LIST_EVENTS processed by the client thread.
3986 */
b551a063 3987static ssize_t cmd_list_events(int domain, struct ltt_session *session,
54d01ffb
DG
3988 char *channel_name, struct lttng_event **events)
3989{
b551a063 3990 int ret = 0;
54d01ffb 3991 ssize_t nb_event = 0;
54d01ffb 3992
b551a063
DG
3993 switch (domain) {
3994 case LTTNG_DOMAIN_KERNEL:
3995 if (session->kernel_session != NULL) {
3996 nb_event = list_lttng_kernel_events(channel_name,
3997 session->kernel_session, events);
54d01ffb 3998 }
b551a063
DG
3999 break;
4000 case LTTNG_DOMAIN_UST:
4001 {
4002 if (session->ust_session != NULL) {
4003 nb_event = list_lttng_ust_global_events(channel_name,
4004 &session->ust_session->domain_global, events);
4005 }
4006 break;
54d01ffb 4007 }
b551a063 4008 default:
1ab1ea0b 4009 ret = -LTTCOMM_UND;
54d01ffb
DG
4010 goto error;
4011 }
4012
b551a063 4013 ret = nb_event;
54d01ffb
DG
4014
4015error:
4016 return ret;
4017}
4018
00e2e675
DG
4019/*
4020 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
4021 */
4022static int cmd_disable_consumer(int domain, struct ltt_session *session)
4023{
4024 int ret;
4025 struct ltt_kernel_session *ksess = session->kernel_session;
4026 struct ltt_ust_session *usess = session->ust_session;
4027 struct consumer_output *consumer;
4028
a4b92340
DG
4029 assert(session);
4030
00e2e675
DG
4031 if (session->enabled) {
4032 /* Can't disable consumer on an already started session */
4033 ret = LTTCOMM_TRACE_ALREADY_STARTED;
4034 goto error;
4035 }
4036
a4b92340
DG
4037 if (!session->start_consumer) {
4038 ret = LTTCOMM_NO_CONSUMER;
4039 goto error;
4040 }
4041
00e2e675 4042 switch (domain) {
a4b92340
DG
4043 case 0:
4044 DBG("Disable tracing session %s consumer", session->name);
4045 consumer = session->consumer;
4046 break;
00e2e675
DG
4047 case LTTNG_DOMAIN_KERNEL:
4048 /* Code flow error if we don't have a kernel session here. */
4049 assert(ksess);
4050
4051 DBG("Disabling kernel consumer");
4052 consumer = ksess->consumer;
4053
4054 break;
4055 case LTTNG_DOMAIN_UST:
4056 /* Code flow error if we don't have a UST session here. */
4057 assert(usess);
4058
4059 DBG("Disabling UST consumer");
4060 consumer = usess->consumer;
4061
4062 break;
4063 default:
4064 ret = LTTCOMM_UNKNOWN_DOMAIN;
4065 goto error;
4066 }
4067
a4b92340
DG
4068 if (consumer) {
4069 consumer->enabled = 0;
4070 /* Success at this point */
4071 ret = LTTCOMM_OK;
4072 } else {
4073 ret = LTTCOMM_NO_CONSUMER;
4074 }
00e2e675
DG
4075
4076error:
4077 return ret;
4078}
4079
4080/*
4081 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
4082 */
4083static int cmd_enable_consumer(int domain, struct ltt_session *session)
4084{
4085 int ret;
4086 struct ltt_kernel_session *ksess = session->kernel_session;
4087 struct ltt_ust_session *usess = session->ust_session;
a4b92340
DG
4088 struct consumer_output *consumer = NULL;
4089
4090 assert(session);
00e2e675
DG
4091
4092 /* Can't enable consumer after session started. */
4093 if (session->enabled) {
4094 ret = LTTCOMM_TRACE_ALREADY_STARTED;
4095 goto error;
4096 }
4097
a4b92340
DG
4098 if (!session->start_consumer) {
4099 ret = LTTCOMM_NO_CONSUMER;
4100 goto error;
4101 }
4102
00e2e675 4103 switch (domain) {
a4b92340
DG
4104 case 0:
4105 assert(session->consumer);
4106 consumer = session->consumer;
4107 break;
00e2e675
DG
4108 case LTTNG_DOMAIN_KERNEL:
4109 /* Code flow error if we don't have a kernel session here. */
4110 assert(ksess);
4111
4112 /*
4113 * Check if we have already sent fds to the consumer. In that case,
4114 * the enable-consumer command can't be used because a start trace
4115 * had previously occured.
4116 */
4117 if (ksess->consumer_fds_sent) {
4118 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4119 goto error;
4120 }
4121
a4b92340
DG
4122 consumer = ksess->tmp_consumer;
4123 if (consumer == NULL) {
4124 ret = LTTCOMM_OK;
00e2e675
DG
4125 /* No temp. consumer output exists. Using the current one. */
4126 DBG3("No temporary consumer. Using default");
a4b92340 4127 consumer = ksess->consumer;
00e2e675
DG
4128 goto error;
4129 }
4130
a4b92340 4131 switch (consumer->type) {
00e2e675
DG
4132 case CONSUMER_DST_LOCAL:
4133 DBG2("Consumer output is local. Creating directory(ies)");
4134
4135 /* Create directory(ies) */
a4b92340 4136 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
00e2e675
DG
4137 S_IRWXU | S_IRWXG, session->uid, session->gid);
4138 if (ret < 0) {
4139 if (ret != -EEXIST) {
4140 ERR("Trace directory creation error");
4141 ret = LTTCOMM_FATAL;
4142 goto error;
4143 }
4144 }
4145 break;
4146 case CONSUMER_DST_NET:
4147 DBG2("Consumer output is network. Validating URIs");
4148 /* Validate if we have both control and data path set. */
a4b92340
DG
4149 if (!consumer->dst.net.control_isset) {
4150 ret = LTTCOMM_URL_CTRL_MISS;
00e2e675
DG
4151 goto error;
4152 }
4153
a4b92340
DG
4154 if (!consumer->dst.net.data_isset) {
4155 ret = LTTCOMM_URL_DATA_MISS;
00e2e675
DG
4156 goto error;
4157 }
4158
4159 /* Check established network session state */
4160 if (session->net_handle == 0) {
4161 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4162 ERR("Session network handle is not set on enable-consumer");
4163 goto error;
4164 }
4165
00e2e675
DG
4166 break;
4167 }
4168
07424f16
DG
4169 /* Append default kernel trace dir to subdir */
4170 strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
4171 sizeof(ksess->consumer->subdir));
4172
00e2e675
DG
4173 /*
4174 * @session-lock
4175 * This is race free for now since the session lock is acquired before
4176 * ending up in this function. No other threads can access this kernel
4177 * session without this lock hence freeing the consumer output object
4178 * is valid.
4179 */
3f8e211f 4180 rcu_read_lock();
00e2e675 4181 consumer_destroy_output(ksess->consumer);
3f8e211f 4182 rcu_read_unlock();
a4b92340 4183 ksess->consumer = consumer;
00e2e675
DG
4184 ksess->tmp_consumer = NULL;
4185
4186 break;
4187 case LTTNG_DOMAIN_UST:
4188 /* Code flow error if we don't have a UST session here. */
4189 assert(usess);
4190
4191 /*
4192 * Check if we have already sent fds to the consumer. In that case,
4193 * the enable-consumer command can't be used because a start trace
4194 * had previously occured.
4195 */
4196 if (usess->start_trace) {
4197 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4198 goto error;
4199 }
4200
a4b92340
DG
4201 consumer = usess->tmp_consumer;
4202 if (consumer == NULL) {
4203 ret = LTTCOMM_OK;
00e2e675
DG
4204 /* No temp. consumer output exists. Using the current one. */
4205 DBG3("No temporary consumer. Using default");
a4b92340 4206 consumer = usess->consumer;
00e2e675
DG
4207 goto error;
4208 }
4209
a4b92340 4210 switch (consumer->type) {
00e2e675
DG
4211 case CONSUMER_DST_LOCAL:
4212 DBG2("Consumer output is local. Creating directory(ies)");
4213
4214 /* Create directory(ies) */
a4b92340 4215 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
00e2e675
DG
4216 S_IRWXU | S_IRWXG, session->uid, session->gid);
4217 if (ret < 0) {
4218 if (ret != -EEXIST) {
4219 ERR("Trace directory creation error");
4220 ret = LTTCOMM_FATAL;
4221 goto error;
4222 }
4223 }
4224 break;
4225 case CONSUMER_DST_NET:
4226 DBG2("Consumer output is network. Validating URIs");
4227 /* Validate if we have both control and data path set. */
a4b92340
DG
4228 if (!consumer->dst.net.control_isset) {
4229 ret = LTTCOMM_URL_CTRL_MISS;
00e2e675
DG
4230 goto error;
4231 }
4232
a4b92340
DG
4233 if (!consumer->dst.net.data_isset) {
4234 ret = LTTCOMM_URL_DATA_MISS;
00e2e675
DG
4235 goto error;
4236 }
4237
4238 /* Check established network session state */
4239 if (session->net_handle == 0) {
4240 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4241 DBG2("Session network handle is not set on enable-consumer");
4242 goto error;
4243 }
4244
a4b92340 4245 if (consumer->net_seq_index == -1) {
00e2e675
DG
4246 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4247 DBG2("Network index is not set on the consumer");
4248 goto error;
4249 }
4250
00e2e675
DG
4251 break;
4252 }
4253
07424f16
DG
4254 /* Append default kernel trace dir to subdir */
4255 strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
4256 sizeof(usess->consumer->subdir));
4257
00e2e675
DG
4258 /*
4259 * @session-lock
4260 * This is race free for now since the session lock is acquired before
4261 * ending up in this function. No other threads can access this kernel
4262 * session without this lock hence freeing the consumer output object
4263 * is valid.
4264 */
3f8e211f 4265 rcu_read_lock();
00e2e675 4266 consumer_destroy_output(usess->consumer);
3f8e211f 4267 rcu_read_unlock();
a4b92340 4268 usess->consumer = consumer;
00e2e675
DG
4269 usess->tmp_consumer = NULL;
4270
4271 break;
4272 }
4273
a4b92340
DG
4274 /* Enable it */
4275 if (consumer) {
4276 consumer->enabled = 1;
4277 /* Success at this point */
4278 ret = LTTCOMM_OK;
4279 } else {
4280 /* Should not really happend... */
4281 ret = LTTCOMM_NO_CONSUMER;
4282 }
00e2e675
DG
4283
4284error:
4285 return ret;
4286}
4287
54d01ffb
DG
4288/*
4289 * Process the command requested by the lttng client within the command
4290 * context structure. This function make sure that the return structure (llm)
4291 * is set and ready for transmission before returning.
4292 *
4293 * Return any error encountered or 0 for success.
53a80697
MD
4294 *
4295 * "sock" is only used for special-case var. len data.
54d01ffb 4296 */
53a80697
MD
4297static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
4298 int *sock_error)
54d01ffb
DG
4299{
4300 int ret = LTTCOMM_OK;
44d3bd01 4301 int need_tracing_session = 1;
2e09ba09 4302 int need_domain;
54d01ffb
DG
4303
4304 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
4305
53a80697
MD
4306 *sock_error = 0;
4307
2e09ba09
MD
4308 switch (cmd_ctx->lsm->cmd_type) {
4309 case LTTNG_CREATE_SESSION:
4310 case LTTNG_DESTROY_SESSION:
4311 case LTTNG_LIST_SESSIONS:
4312 case LTTNG_LIST_DOMAINS:
4313 case LTTNG_START_TRACE:
4314 case LTTNG_STOP_TRACE:
4315 need_domain = 0;
3aace903 4316 break;
2e09ba09
MD
4317 default:
4318 need_domain = 1;
4319 }
4320
4321 if (opt_no_kernel && need_domain
4322 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9
MD
4323 if (!is_root) {
4324 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4325 } else {
4326 ret = LTTCOMM_KERN_NA;
4327 }
4fba7219
DG
4328 goto error;
4329 }
4330
54d01ffb
DG
4331 /*
4332 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 4333 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
4334 * command.
4335 */
4336 switch(cmd_ctx->lsm->cmd_type) {
4337 case LTTNG_LIST_SESSIONS:
4338 case LTTNG_LIST_TRACEPOINTS:
f37d259d 4339 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
4340 case LTTNG_LIST_DOMAINS:
4341 case LTTNG_LIST_CHANNELS:
4342 case LTTNG_LIST_EVENTS:
4343 break;
4344 default:
4345 /* Setup lttng message with no payload */
4346 ret = setup_lttng_msg(cmd_ctx, 0);
4347 if (ret < 0) {
4348 /* This label does not try to unlock the session */
4349 goto init_setup_error;
4350 }
4351 }
4352
4353 /* Commands that DO NOT need a session. */
4354 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 4355 case LTTNG_CREATE_SESSION:
2e09ba09 4356 case LTTNG_CALIBRATE:
54d01ffb
DG
4357 case LTTNG_LIST_SESSIONS:
4358 case LTTNG_LIST_TRACEPOINTS:
f37d259d 4359 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 4360 need_tracing_session = 0;
54d01ffb
DG
4361 break;
4362 default:
4363 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
4364 /*
4365 * We keep the session list lock across _all_ commands
4366 * for now, because the per-session lock does not
4367 * handle teardown properly.
4368 */
74babd95 4369 session_lock_list();
54d01ffb
DG
4370 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
4371 if (cmd_ctx->session == NULL) {
4372 if (cmd_ctx->lsm->session.name != NULL) {
4373 ret = LTTCOMM_SESS_NOT_FOUND;
4374 } else {
4375 /* If no session name specified */
4376 ret = LTTCOMM_SELECT_SESS;
4377 }
4378 goto error;
4379 } else {
4380 /* Acquire lock for the session */
4381 session_lock(cmd_ctx->session);
4382 }
4383 break;
4384 }
b389abbe 4385
2e09ba09
MD
4386 if (!need_domain) {
4387 goto skip_domain;
4388 }
a4b92340 4389
54d01ffb
DG
4390 /*
4391 * Check domain type for specific "pre-action".
4392 */
4393 switch (cmd_ctx->lsm->domain.type) {
4394 case LTTNG_DOMAIN_KERNEL:
d1f1c568 4395 if (!is_root) {
531d29f9 4396 ret = LTTCOMM_NEED_ROOT_SESSIOND;
d1f1c568
DG
4397 goto error;
4398 }
4399
54d01ffb 4400 /* Kernel tracer check */
a4b35e07 4401 if (kernel_tracer_fd == -1) {
54d01ffb 4402 /* Basically, load kernel tracer modules */
096102bd
DG
4403 ret = init_kernel_tracer();
4404 if (ret != 0) {
54d01ffb
DG
4405 goto error;
4406 }
4407 }
5eb91c98 4408
5c827ce0
DG
4409 /* Consumer is in an ERROR state. Report back to client */
4410 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
4411 ret = LTTCOMM_NO_KERNCONSUMERD;
4412 goto error;
4413 }
4414
54d01ffb 4415 /* Need a session for kernel command */
44d3bd01 4416 if (need_tracing_session) {
54d01ffb 4417 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 4418 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 4419 if (ret < 0) {
54d01ffb 4420 ret = LTTCOMM_KERN_SESS_FAIL;
5eb91c98
DG
4421 goto error;
4422 }
b389abbe 4423 }
7d29a247 4424
54d01ffb 4425 /* Start the kernel consumer daemon */
3bd1e081
MD
4426 pthread_mutex_lock(&kconsumer_data.pid_mutex);
4427 if (kconsumer_data.pid == 0 &&
a4b92340
DG
4428 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
4429 cmd_ctx->session->start_consumer) {
3bd1e081
MD
4430 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4431 ret = start_consumerd(&kconsumer_data);
7d29a247 4432 if (ret < 0) {
54d01ffb
DG
4433 ret = LTTCOMM_KERN_CONSUMER_FAIL;
4434 goto error;
950131af 4435 }
5c827ce0 4436 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
4437 } else {
4438 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 4439 }
173af62f 4440
a4b92340
DG
4441 /*
4442 * The consumer was just spawned so we need to add the socket to
4443 * the consumer output of the session if exist.
4444 */
4445 ret = consumer_create_socket(&kconsumer_data,
4446 cmd_ctx->session->kernel_session->consumer);
4447 if (ret < 0) {
4448 goto error;
173af62f 4449 }
0d0c377a 4450 }
5c827ce0 4451
54d01ffb 4452 break;
2bdd86d4 4453 case LTTNG_DOMAIN_UST:
44d3bd01 4454 {
5c827ce0
DG
4455 /* Consumer is in an ERROR state. Report back to client */
4456 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
4457 ret = LTTCOMM_NO_USTCONSUMERD;
4458 goto error;
4459 }
4460
44d3bd01 4461 if (need_tracing_session) {
a4b92340 4462 /* Create UST session if none exist. */
f6a9efaa 4463 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 4464 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 4465 &cmd_ctx->lsm->domain);
44d3bd01
DG
4466 if (ret != LTTCOMM_OK) {
4467 goto error;
4468 }
4469 }
00e2e675 4470
7753dea8
MD
4471 /* Start the UST consumer daemons */
4472 /* 64-bit */
4473 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 4474 if (consumerd64_bin[0] != '\0' &&
7753dea8 4475 ustconsumer64_data.pid == 0 &&
a4b92340
DG
4476 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
4477 cmd_ctx->session->start_consumer) {
7753dea8
MD
4478 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4479 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 4480 if (ret < 0) {
7753dea8 4481 ret = LTTCOMM_UST_CONSUMER64_FAIL;
173af62f 4482 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
4483 goto error;
4484 }
48842b30 4485
173af62f 4486 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 4487 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 4488 } else {
7753dea8
MD
4489 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4490 }
173af62f
DG
4491
4492 /*
4493 * Setup socket for consumer 64 bit. No need for atomic access
4494 * since it was set above and can ONLY be set in this thread.
4495 */
a4b92340
DG
4496 ret = consumer_create_socket(&ustconsumer64_data,
4497 cmd_ctx->session->ust_session->consumer);
4498 if (ret < 0) {
4499 goto error;
173af62f
DG
4500 }
4501
7753dea8 4502 /* 32-bit */
fc7a59ce 4503 if (consumerd32_bin[0] != '\0' &&
7753dea8 4504 ustconsumer32_data.pid == 0 &&
a4b92340
DG
4505 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
4506 cmd_ctx->session->start_consumer) {
7753dea8
MD
4507 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4508 ret = start_consumerd(&ustconsumer32_data);
4509 if (ret < 0) {
4510 ret = LTTCOMM_UST_CONSUMER32_FAIL;
173af62f 4511 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
4512 goto error;
4513 }
5c827ce0 4514
173af62f 4515 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 4516 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
4517 } else {
4518 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 4519 }
173af62f
DG
4520
4521 /*
4522 * Setup socket for consumer 64 bit. No need for atomic access
4523 * since it was set above and can ONLY be set in this thread.
4524 */
a4b92340
DG
4525 ret = consumer_create_socket(&ustconsumer32_data,
4526 cmd_ctx->session->ust_session->consumer);
4527 if (ret < 0) {
4528 goto error;
173af62f 4529 }
44d3bd01
DG
4530 }
4531 break;
48842b30 4532 }
54d01ffb 4533 default:
54d01ffb
DG
4534 break;
4535 }
2e09ba09 4536skip_domain:
33a2b854 4537
5c827ce0
DG
4538 /* Validate consumer daemon state when start/stop trace command */
4539 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
4540 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
4541 switch (cmd_ctx->lsm->domain.type) {
4542 case LTTNG_DOMAIN_UST:
4543 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
4544 ret = LTTCOMM_NO_USTCONSUMERD;
4545 goto error;
4546 }
4547 break;
4548 case LTTNG_DOMAIN_KERNEL:
4549 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
4550 ret = LTTCOMM_NO_KERNCONSUMERD;
4551 goto error;
4552 }
4553 break;
4554 }
4555 }
4556
8e0af1b4
MD
4557 /*
4558 * Check that the UID or GID match that of the tracing session.
4559 * The root user can interact with all sessions.
4560 */
4561 if (need_tracing_session) {
4562 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
4563 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4564 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
8e0af1b4
MD
4565 ret = LTTCOMM_EPERM;
4566 goto error;
4567 }
4568 }
4569
54d01ffb
DG
4570 /* Process by command type */
4571 switch (cmd_ctx->lsm->cmd_type) {
4572 case LTTNG_ADD_CONTEXT:
4573 {
4574 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4575 cmd_ctx->lsm->u.context.channel_name,
4576 cmd_ctx->lsm->u.context.event_name,
4577 &cmd_ctx->lsm->u.context.ctx);
4578 break;
4579 }
4580 case LTTNG_DISABLE_CHANNEL:
4581 {
4582 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4583 cmd_ctx->lsm->u.disable.channel_name);
4584 break;
4585 }
4586 case LTTNG_DISABLE_EVENT:
4587 {
4588 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4589 cmd_ctx->lsm->u.disable.channel_name,
4590 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
4591 break;
4592 }
54d01ffb
DG
4593 case LTTNG_DISABLE_ALL_EVENT:
4594 {
2bdd86d4 4595 DBG("Disabling all events");
54d01ffb
DG
4596
4597 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4598 cmd_ctx->lsm->u.disable.channel_name);
4599 break;
4600 }
00e2e675
DG
4601 case LTTNG_DISABLE_CONSUMER:
4602 {
4603 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4604 break;
4605 }
54d01ffb
DG
4606 case LTTNG_ENABLE_CHANNEL:
4607 {
aea829b3 4608 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
54d01ffb
DG
4609 &cmd_ctx->lsm->u.channel.chan);
4610 break;
4611 }
00e2e675
DG
4612 case LTTNG_ENABLE_CONSUMER:
4613 {
a4b92340
DG
4614 /*
4615 * XXX: 0 means that this URI should be applied on the session. Should
4616 * be a DOMAIN enuam.
4617 */
00e2e675 4618 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
a4b92340
DG
4619 if (ret != LTTCOMM_OK) {
4620 goto error;
4621 }
4622
4623 if (cmd_ctx->lsm->domain.type == 0) {
4624 /* Add the URI for the UST session if a consumer is present. */
4625 if (cmd_ctx->session->ust_session &&
4626 cmd_ctx->session->ust_session->consumer) {
4627 ret = cmd_enable_consumer(LTTNG_DOMAIN_UST, cmd_ctx->session);
4628 } else if (cmd_ctx->session->kernel_session &&
4629 cmd_ctx->session->kernel_session->consumer) {
4630 ret = cmd_enable_consumer(LTTNG_DOMAIN_KERNEL,
4631 cmd_ctx->session);
4632 }
4633 }
00e2e675
DG
4634 break;
4635 }
54d01ffb
DG
4636 case LTTNG_ENABLE_EVENT:
4637 {
4638 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4639 cmd_ctx->lsm->u.enable.channel_name,
4640 &cmd_ctx->lsm->u.enable.event);
4641 break;
4642 }
4643 case LTTNG_ENABLE_ALL_EVENT:
4644 {
2bdd86d4 4645 DBG("Enabling all events");
54d01ffb
DG
4646
4647 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
8c9ae521
DG
4648 cmd_ctx->lsm->u.enable.channel_name,
4649 cmd_ctx->lsm->u.enable.event.type);
54d01ffb
DG
4650 break;
4651 }
052da939 4652 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 4653 {
9f19cc17 4654 struct lttng_event *events;
54d01ffb 4655 ssize_t nb_events;
052da939 4656
54d01ffb
DG
4657 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
4658 if (nb_events < 0) {
4659 ret = -nb_events;
4660 goto error;
2ef84c95
DG
4661 }
4662
4663 /*
4664 * Setup lttng message with payload size set to the event list size in
4665 * bytes and then copy list into the llm payload.
4666 */
052da939 4667 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 4668 if (ret < 0) {
052da939 4669 free(events);
2ef84c95
DG
4670 goto setup_error;
4671 }
4672
4673 /* Copy event list into message payload */
9f19cc17 4674 memcpy(cmd_ctx->llm->payload, events,
052da939 4675 sizeof(struct lttng_event) * nb_events);
2ef84c95 4676
9f19cc17 4677 free(events);
2ef84c95
DG
4678
4679 ret = LTTCOMM_OK;
4680 break;
4681 }
f37d259d
MD
4682 case LTTNG_LIST_TRACEPOINT_FIELDS:
4683 {
4684 struct lttng_event_field *fields;
4685 ssize_t nb_fields;
4686
a4b92340
DG
4687 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
4688 &fields);
f37d259d
MD
4689 if (nb_fields < 0) {
4690 ret = -nb_fields;
4691 goto error;
4692 }
4693
4694 /*
4695 * Setup lttng message with payload size set to the event list size in
4696 * bytes and then copy list into the llm payload.
4697 */
a4b92340
DG
4698 ret = setup_lttng_msg(cmd_ctx,
4699 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
4700 if (ret < 0) {
4701 free(fields);
4702 goto setup_error;
4703 }
4704
4705 /* Copy event list into message payload */
4706 memcpy(cmd_ctx->llm->payload, fields,
4707 sizeof(struct lttng_event_field) * nb_fields);
4708
4709 free(fields);
4710
4711 ret = LTTCOMM_OK;
4712 break;
4713 }
00e2e675
DG
4714 case LTTNG_SET_CONSUMER_URI:
4715 {
a4b92340
DG
4716 size_t nb_uri, len;
4717 struct lttng_uri *uris;
4718
4719 nb_uri = cmd_ctx->lsm->u.uri.size;
4720 len = nb_uri * sizeof(struct lttng_uri);
4721
4722 if (nb_uri == 0) {
4723 ret = LTTCOMM_INVALID;
4724 goto error;
4725 }
4726
4727 uris = zmalloc(len);
4728 if (uris == NULL) {
4729 ret = LTTCOMM_FATAL;
4730 goto error;
4731 }
4732
4733 /* Receive variable len data */
77c7c900 4734 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
4735 ret = lttcomm_recv_unix_sock(sock, uris, len);
4736 if (ret <= 0) {
4737 DBG("No URIs received from client... continuing");
4738 *sock_error = 1;
4739 ret = LTTCOMM_SESSION_FAIL;
4740 goto error;
4741 }
4742
00e2e675 4743 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
a4b92340
DG
4744 nb_uri, uris);
4745 if (ret != LTTCOMM_OK) {
4746 goto error;
4747 }
4748
4749 /*
4750 * XXX: 0 means that this URI should be applied on the session. Should
4751 * be a DOMAIN enuam.
4752 */
4753 if (cmd_ctx->lsm->domain.type == 0) {
4754 /* Add the URI for the UST session if a consumer is present. */
4755 if (cmd_ctx->session->ust_session &&
4756 cmd_ctx->session->ust_session->consumer) {
4757 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
4758 nb_uri, uris);
4759 } else if (cmd_ctx->session->kernel_session &&
4760 cmd_ctx->session->kernel_session->consumer) {
4761 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
4762 cmd_ctx->session, nb_uri, uris);
4763 }
4764 }
4765
00e2e675
DG
4766 break;
4767 }
f3ed775e 4768 case LTTNG_START_TRACE:
8c0faa1d 4769 {
54d01ffb 4770 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
4771 break;
4772 }
f3ed775e 4773 case LTTNG_STOP_TRACE:
8c0faa1d 4774 {
54d01ffb 4775 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
4776 break;
4777 }
5e16da05
MD
4778 case LTTNG_CREATE_SESSION:
4779 {
a4b92340
DG
4780 size_t nb_uri, len;
4781 struct lttng_uri *uris = NULL;
4782
4783 nb_uri = cmd_ctx->lsm->u.uri.size;
4784 len = nb_uri * sizeof(struct lttng_uri);
4785
4786 if (nb_uri > 0) {
4787 uris = zmalloc(len);
4788 if (uris == NULL) {
4789 ret = LTTCOMM_FATAL;
4790 goto error;
4791 }
4792
4793 /* Receive variable len data */
77c7c900 4794 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
4795 ret = lttcomm_recv_unix_sock(sock, uris, len);
4796 if (ret <= 0) {
4797 DBG("No URIs received from client... continuing");
4798 *sock_error = 1;
4799 ret = LTTCOMM_SESSION_FAIL;
4800 goto error;
4801 }
4802
4803 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4804 DBG("Creating session with ONE network URI is a bad call");
4805 ret = LTTCOMM_SESSION_FAIL;
4806 goto error;
4807 }
4808 }
4809
4810 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
4811 &cmd_ctx->creds);
4812
00e2e675
DG
4813 break;
4814 }
5e16da05
MD
4815 case LTTNG_DESTROY_SESSION:
4816 {
54d01ffb
DG
4817 ret = cmd_destroy_session(cmd_ctx->session,
4818 cmd_ctx->lsm->session.name);
a4b92340
DG
4819
4820 /* Set session to NULL so we do not unlock it after free. */
256a5576 4821 cmd_ctx->session = NULL;
5461b305 4822 break;
5e16da05 4823 }
9f19cc17 4824 case LTTNG_LIST_DOMAINS:
5e16da05 4825 {
54d01ffb
DG
4826 ssize_t nb_dom;
4827 struct lttng_domain *domains;
5461b305 4828
54d01ffb
DG
4829 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
4830 if (nb_dom < 0) {
4831 ret = -nb_dom;
4832 goto error;
ce3d728c 4833 }
520ff687 4834
54d01ffb 4835 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305
DG
4836 if (ret < 0) {
4837 goto setup_error;
520ff687 4838 }
57167058 4839
54d01ffb
DG
4840 /* Copy event list into message payload */
4841 memcpy(cmd_ctx->llm->payload, domains,
4842 nb_dom * sizeof(struct lttng_domain));
4843
4844 free(domains);
5e16da05 4845
5461b305 4846 ret = LTTCOMM_OK;
5e16da05
MD
4847 break;
4848 }
9f19cc17 4849 case LTTNG_LIST_CHANNELS:
5e16da05 4850 {
6775595e 4851 int nb_chan;
54d01ffb
DG
4852 struct lttng_channel *channels;
4853
b551a063
DG
4854 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
4855 cmd_ctx->session, &channels);
54d01ffb
DG
4856 if (nb_chan < 0) {
4857 ret = -nb_chan;
4858 goto error;
5461b305 4859 }
ca95a216 4860
54d01ffb 4861 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305
DG
4862 if (ret < 0) {
4863 goto setup_error;
4864 }
9f19cc17 4865
54d01ffb
DG
4866 /* Copy event list into message payload */
4867 memcpy(cmd_ctx->llm->payload, channels,
4868 nb_chan * sizeof(struct lttng_channel));
4869
4870 free(channels);
9f19cc17
DG
4871
4872 ret = LTTCOMM_OK;
5461b305 4873 break;
5e16da05 4874 }
9f19cc17 4875 case LTTNG_LIST_EVENTS:
5e16da05 4876 {
b551a063 4877 ssize_t nb_event;
684d34d2 4878 struct lttng_event *events = NULL;
9f19cc17 4879
b551a063 4880 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
4881 cmd_ctx->lsm->u.list.channel_name, &events);
4882 if (nb_event < 0) {
4883 ret = -nb_event;
4884 goto error;
5461b305 4885 }
ca95a216 4886
54d01ffb 4887 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305
DG
4888 if (ret < 0) {
4889 goto setup_error;
4890 }
9f19cc17 4891
54d01ffb
DG
4892 /* Copy event list into message payload */
4893 memcpy(cmd_ctx->llm->payload, events,
4894 nb_event * sizeof(struct lttng_event));
9f19cc17 4895
54d01ffb 4896 free(events);
9f19cc17
DG
4897
4898 ret = LTTCOMM_OK;
5461b305 4899 break;
5e16da05
MD
4900 }
4901 case LTTNG_LIST_SESSIONS:
4902 {
8e0af1b4 4903 unsigned int nr_sessions;
5461b305 4904
8e0af1b4 4905 session_lock_list();
730389d9
DG
4906 nr_sessions = lttng_sessions_count(
4907 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4908 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 4909
8e0af1b4 4910 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 4911 if (ret < 0) {
54d01ffb 4912 session_unlock_list();
5461b305 4913 goto setup_error;
e065084a 4914 }
5e16da05 4915
6c9cc2ab 4916 /* Filled the session array */
8e0af1b4 4917 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
4918 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4919 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 4920
54d01ffb 4921 session_unlock_list();
5e16da05 4922
5461b305 4923 ret = LTTCOMM_OK;
5e16da05
MD
4924 break;
4925 }
d0254c7c
MD
4926 case LTTNG_CALIBRATE:
4927 {
54d01ffb
DG
4928 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
4929 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
4930 break;
4931 }
d9800920
DG
4932 case LTTNG_REGISTER_CONSUMER:
4933 {
54d01ffb
DG
4934 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4935 cmd_ctx->lsm->u.reg.path);
d9800920
DG
4936 break;
4937 }
53a80697
MD
4938 case LTTNG_SET_FILTER:
4939 {
4940 struct lttng_filter_bytecode *bytecode;
4941
4942 if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) {
4943 ret = LTTCOMM_FILTER_INVAL;
4944 goto error;
4945 }
4946 bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len);
4947 if (!bytecode) {
4948 ret = LTTCOMM_FILTER_NOMEM;
4949 goto error;
4950 }
4951 /* Receive var. len. data */
4952 DBG("Receiving var len data from client ...");
4953 ret = lttcomm_recv_unix_sock(sock, bytecode,
4954 cmd_ctx->lsm->u.filter.bytecode_len);
4955 if (ret <= 0) {
4956 DBG("Nothing recv() from client var len data... continuing");
4957 *sock_error = 1;
4958 ret = LTTCOMM_FILTER_INVAL;
4959 goto error;
4960 }
4961
4962 if (bytecode->len + sizeof(*bytecode)
4963 != cmd_ctx->lsm->u.filter.bytecode_len) {
4964 free(bytecode);
4965 ret = LTTCOMM_FILTER_INVAL;
4966 goto error;
4967 }
4968
4969 ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4970 cmd_ctx->lsm->u.filter.channel_name,
4971 cmd_ctx->lsm->u.filter.event_name,
4972 bytecode);
4973 break;
4974 }
5e16da05 4975 default:
5e16da05 4976 ret = LTTCOMM_UND;
5461b305 4977 break;
fac6795d
DG
4978 }
4979
5461b305 4980error:
5461b305
DG
4981 if (cmd_ctx->llm == NULL) {
4982 DBG("Missing llm structure. Allocating one.");
894be886 4983 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
4984 goto setup_error;
4985 }
4986 }
54d01ffb 4987 /* Set return code */
5461b305 4988 cmd_ctx->llm->ret_code = ret;
5461b305 4989setup_error:
b5541356 4990 if (cmd_ctx->session) {
54d01ffb 4991 session_unlock(cmd_ctx->session);
b5541356 4992 }
256a5576
MD
4993 if (need_tracing_session) {
4994 session_unlock_list();
4995 }
54d01ffb 4996init_setup_error:
8028d920 4997 return ret;
fac6795d
DG
4998}
4999
44a5e5eb
DG
5000/*
5001 * Thread managing health check socket.
5002 */
5003static void *thread_manage_health(void *data)
5004{
eb4a2943 5005 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
5006 uint32_t revents, nb_fd;
5007 struct lttng_poll_event events;
5008 struct lttcomm_health_msg msg;
5009 struct lttcomm_health_data reply;
5010
5011 DBG("[thread] Manage health check started");
5012
5013 rcu_register_thread();
5014
5015 /* Create unix socket */
5016 sock = lttcomm_create_unix_sock(health_unix_sock_path);
5017 if (sock < 0) {
5018 ERR("Unable to create health check Unix socket");
5019 ret = -1;
5020 goto error;
5021 }
5022
5023 ret = lttcomm_listen_unix_sock(sock);
5024 if (ret < 0) {
5025 goto error;
5026 }
5027
5028 /*
5029 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5030 * more will be added to this poll set.
5031 */
5032 ret = create_thread_poll_set(&events, 2);
5033 if (ret < 0) {
5034 goto error;
5035 }
5036
5037 /* Add the application registration socket */
5038 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
5039 if (ret < 0) {
5040 goto error;
5041 }
5042
5043 while (1) {
5044 DBG("Health check ready");
5045
5046 nb_fd = LTTNG_POLL_GETNB(&events);
5047
5048 /* Inifinite blocking call, waiting for transmission */
5049restart:
5050 ret = lttng_poll_wait(&events, -1);
5051 if (ret < 0) {
5052 /*
5053 * Restart interrupted system call.
5054 */
5055 if (errno == EINTR) {
5056 goto restart;
5057 }
5058 goto error;
5059 }
5060
5061 for (i = 0; i < nb_fd; i++) {
5062 /* Fetch once the poll data */
5063 revents = LTTNG_POLL_GETEV(&events, i);
5064 pollfd = LTTNG_POLL_GETFD(&events, i);
5065
5066 /* Thread quit pipe has been closed. Killing thread. */
5067 ret = check_thread_quit_pipe(pollfd, revents);
5068 if (ret) {
139ac872
MD
5069 err = 0;
5070 goto exit;
44a5e5eb
DG
5071 }
5072
5073 /* Event on the registration socket */
5074 if (pollfd == sock) {
5075 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5076 ERR("Health socket poll error");
5077 goto error;
5078 }
5079 }
5080 }
5081
5082 new_sock = lttcomm_accept_unix_sock(sock);
5083 if (new_sock < 0) {
5084 goto error;
5085 }
5086
5087 DBG("Receiving data from client for health...");
5088 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
5089 if (ret <= 0) {
5090 DBG("Nothing recv() from client... continuing");
5091 ret = close(new_sock);
5092 if (ret) {
5093 PERROR("close");
5094 }
5095 new_sock = -1;
5096 continue;
5097 }
5098
5099 rcu_thread_online();
5100
5101 switch (msg.component) {
5102 case LTTNG_HEALTH_CMD:
5103 reply.ret_code = health_check_state(&health_thread_cmd);
5104 break;
139ac872
MD
5105 case LTTNG_HEALTH_APP_MANAGE:
5106 reply.ret_code = health_check_state(&health_thread_app_manage);
5107 break;
44a5e5eb
DG
5108 case LTTNG_HEALTH_APP_REG:
5109 reply.ret_code = health_check_state(&health_thread_app_reg);
5110 break;
5111 case LTTNG_HEALTH_KERNEL:
5112 reply.ret_code = health_check_state(&health_thread_kernel);
5113 break;
5114 case LTTNG_HEALTH_CONSUMER:
5115 reply.ret_code = check_consumer_health();
5116 break;
5117 case LTTNG_HEALTH_ALL:
44a5e5eb 5118 reply.ret_code =
139ac872
MD
5119 health_check_state(&health_thread_app_manage) &&
5120 health_check_state(&health_thread_app_reg) &&
5121 health_check_state(&health_thread_cmd) &&
5122 health_check_state(&health_thread_kernel) &&
5123 check_consumer_health();
44a5e5eb
DG
5124 break;
5125 default:
5126 reply.ret_code = LTTCOMM_UND;
5127 break;
5128 }
5129
5130 /*
5131 * Flip ret value since 0 is a success and 1 indicates a bad health for
5132 * the client where in the sessiond it is the opposite. Again, this is
5133 * just to make things easier for us poor developer which enjoy a lot
5134 * lazyness.
5135 */
5136 if (reply.ret_code == 0 || reply.ret_code == 1) {
5137 reply.ret_code = !reply.ret_code;
5138 }
5139
5140 DBG2("Health check return value %d", reply.ret_code);
5141
5142 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
5143 if (ret < 0) {
5144 ERR("Failed to send health data back to client");
5145 }
5146
5147 /* End of transmission */
5148 ret = close(new_sock);
5149 if (ret) {
5150 PERROR("close");
5151 }
5152 new_sock = -1;
5153 }
5154
139ac872 5155exit:
44a5e5eb 5156error:
139ac872
MD
5157 if (err) {
5158 ERR("Health error occurred in %s", __func__);
5159 }
44a5e5eb
DG
5160 DBG("Health check thread dying");
5161 unlink(health_unix_sock_path);
5162 if (sock >= 0) {
5163 ret = close(sock);
5164 if (ret) {
5165 PERROR("close");
5166 }
5167 }
5168 if (new_sock >= 0) {
5169 ret = close(new_sock);
5170 if (ret) {
5171 PERROR("close");
5172 }
5173 }
5174
5175 lttng_poll_clean(&events);
5176
5177 rcu_unregister_thread();
5178 return NULL;
5179}
5180
1d4b027a 5181/*
d063d709
DG
5182 * This thread manage all clients request using the unix client socket for
5183 * communication.
1d4b027a
DG
5184 */
5185static void *thread_manage_clients(void *data)
5186{
139ac872 5187 int sock = -1, ret, i, pollfd, err = -1;
53a80697 5188 int sock_error;
5eb91c98 5189 uint32_t revents, nb_fd;
273ea72c 5190 struct command_ctx *cmd_ctx = NULL;
5eb91c98 5191 struct lttng_poll_event events;
1d4b027a
DG
5192
5193 DBG("[thread] Manage client started");
5194
f6a9efaa
DG
5195 rcu_register_thread();
5196
44a5e5eb
DG
5197 health_code_update(&health_thread_cmd);
5198
1d4b027a
DG
5199 ret = lttcomm_listen_unix_sock(client_sock);
5200 if (ret < 0) {
5201 goto error;
5202 }
5203
5eb91c98
DG
5204 /*
5205 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5206 * more will be added to this poll set.
5207 */
5208 ret = create_thread_poll_set(&events, 2);
5209 if (ret < 0) {
5210 goto error;
5211 }
273ea72c 5212
5eb91c98
DG
5213 /* Add the application registration socket */
5214 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
5215 if (ret < 0) {
5216 goto error;
5217 }
273ea72c 5218
bbd973c2
DG
5219 /*
5220 * Notify parent pid that we are ready to accept command for client side.
1d4b027a
DG
5221 */
5222 if (opt_sig_parent) {
8db8d1dc 5223 kill(ppid, SIGUSR1);
1d4b027a
DG
5224 }
5225
44a5e5eb
DG
5226 health_code_update(&health_thread_cmd);
5227
1d4b027a 5228 while (1) {
1d4b027a 5229 DBG("Accepting client command ...");
273ea72c 5230
5eb91c98
DG
5231 nb_fd = LTTNG_POLL_GETNB(&events);
5232
273ea72c 5233 /* Inifinite blocking call, waiting for transmission */
88f2b785 5234 restart:
44a5e5eb 5235 health_poll_update(&health_thread_cmd);
5eb91c98 5236 ret = lttng_poll_wait(&events, -1);
44a5e5eb 5237 health_poll_update(&health_thread_cmd);
273ea72c 5238 if (ret < 0) {
88f2b785
MD
5239 /*
5240 * Restart interrupted system call.
5241 */
5242 if (errno == EINTR) {
5243 goto restart;
5244 }
273ea72c
DG
5245 goto error;
5246 }
5247
5eb91c98
DG
5248 for (i = 0; i < nb_fd; i++) {
5249 /* Fetch once the poll data */
5250 revents = LTTNG_POLL_GETEV(&events, i);
5251 pollfd = LTTNG_POLL_GETFD(&events, i);
5252
44a5e5eb
DG
5253 health_code_update(&health_thread_cmd);
5254
5eb91c98
DG
5255 /* Thread quit pipe has been closed. Killing thread. */
5256 ret = check_thread_quit_pipe(pollfd, revents);
5257 if (ret) {
139ac872
MD
5258 err = 0;
5259 goto exit;
5eb91c98
DG
5260 }
5261
5262 /* Event on the registration socket */
5263 if (pollfd == client_sock) {
5264 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5265 ERR("Client socket poll error");
5266 goto error;
5267 }
5268 }
5269 }
5270
5271 DBG("Wait for client response");
5272
44a5e5eb
DG
5273 health_code_update(&health_thread_cmd);
5274
5eb91c98
DG
5275 sock = lttcomm_accept_unix_sock(client_sock);
5276 if (sock < 0) {
273ea72c 5277 goto error;
273ea72c
DG
5278 }
5279
be040666
DG
5280 /* Set socket option for credentials retrieval */
5281 ret = lttcomm_setsockopt_creds_unix_sock(sock);
5282 if (ret < 0) {
5283 goto error;
5284 }
5285
5eb91c98 5286 /* Allocate context command to process the client request */
ba7f0ae5 5287 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 5288 if (cmd_ctx == NULL) {
76d7553f 5289 PERROR("zmalloc cmd_ctx");
1d4b027a 5290 goto error;
5eb91c98 5291 }
1d4b027a 5292
5eb91c98 5293 /* Allocate data buffer for reception */
ba7f0ae5 5294 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 5295 if (cmd_ctx->lsm == NULL) {
76d7553f 5296 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
5297 goto error;
5298 }
1d4b027a 5299
5eb91c98
DG
5300 cmd_ctx->llm = NULL;
5301 cmd_ctx->session = NULL;
1d4b027a 5302
44a5e5eb
DG
5303 health_code_update(&health_thread_cmd);
5304
5eb91c98
DG
5305 /*
5306 * Data is received from the lttng client. The struct
5307 * lttcomm_session_msg (lsm) contains the command and data request of
5308 * the client.
5309 */
5310 DBG("Receiving data from client ...");
be040666
DG
5311 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
5312 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
5313 if (ret <= 0) {
5314 DBG("Nothing recv() from client... continuing");
76d7553f
MD
5315 ret = close(sock);
5316 if (ret) {
5317 PERROR("close");
5318 }
5319 sock = -1;
a2c0da86 5320 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
5321 continue;
5322 }
1d4b027a 5323
44a5e5eb
DG
5324 health_code_update(&health_thread_cmd);
5325
5eb91c98
DG
5326 // TODO: Validate cmd_ctx including sanity check for
5327 // security purpose.
f7776ea7 5328
f6a9efaa 5329 rcu_thread_online();
5eb91c98
DG
5330 /*
5331 * This function dispatch the work to the kernel or userspace tracer
5332 * libs and fill the lttcomm_lttng_msg data structure of all the needed
5333 * informations for the client. The command context struct contains
5334 * everything this function may needs.
5335 */
53a80697 5336 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 5337 rcu_thread_offline();
5eb91c98 5338 if (ret < 0) {
53a80697
MD
5339 if (sock_error) {
5340 ret = close(sock);
5341 if (ret) {
5342 PERROR("close");
5343 }
5344 sock = -1;
5345 }
bbd973c2 5346 /*
5eb91c98 5347 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 5348 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
5349 * (ENOMEM). Error detected but still accept
5350 * command, unless a socket error has been
5351 * detected.
bbd973c2 5352 */
bbd973c2 5353 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
5354 continue;
5355 }
d6e4fca4 5356
44a5e5eb
DG
5357 health_code_update(&health_thread_cmd);
5358
54d01ffb
DG
5359 DBG("Sending response (size: %d, retcode: %s)",
5360 cmd_ctx->lttng_msg_size,
9a745bc7 5361 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 5362 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
5363 if (ret < 0) {
5364 ERR("Failed to send data back to client");
bbd973c2 5365 }
1d4b027a 5366
5eb91c98 5367 /* End of transmission */
76d7553f
MD
5368 ret = close(sock);
5369 if (ret) {
5370 PERROR("close");
5371 }
5372 sock = -1;
be040666
DG
5373
5374 clean_command_ctx(&cmd_ctx);
44a5e5eb
DG
5375
5376 health_code_update(&health_thread_cmd);
273ea72c
DG
5377 }
5378
139ac872 5379exit:
5eb91c98 5380error:
139ac872
MD
5381 if (err) {
5382 health_error(&health_thread_cmd);
5383 ERR("Health error occurred in %s", __func__);
5384 }
5385 health_exit(&health_thread_cmd);
44a5e5eb 5386
5eb91c98 5387 DBG("Client thread dying");
273ea72c 5388 unlink(client_unix_sock_path);
76d7553f
MD
5389 if (client_sock >= 0) {
5390 ret = close(client_sock);
5391 if (ret) {
5392 PERROR("close");
5393 }
a4b35e07
MD
5394 }
5395 if (sock >= 0) {
76d7553f
MD
5396 ret = close(sock);
5397 if (ret) {
5398 PERROR("close");
5399 }
a4b35e07 5400 }
273ea72c 5401
5eb91c98 5402 lttng_poll_clean(&events);
a2fb29a5 5403 clean_command_ctx(&cmd_ctx);
f6a9efaa
DG
5404
5405 rcu_unregister_thread();
1d4b027a
DG
5406 return NULL;
5407}
5408
5409
fac6795d
DG
5410/*
5411 * usage function on stderr
5412 */
5413static void usage(void)
5414{
b716ce68 5415 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
5416 fprintf(stderr, " -h, --help Display this usage.\n");
5417 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
5418 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5419 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5420 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
5421 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5422 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5423 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5424 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
5425 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5426 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5427 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5428 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150
DG
5429 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
5430 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5431 fprintf(stderr, " -V, --version Show version number.\n");
5432 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5433 fprintf(stderr, " -q, --quiet No output at all.\n");
5434 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3bd1e081 5435 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 5436 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
fac6795d
DG
5437}
5438
5439/*
5440 * daemon argument parsing
5441 */
5442static int parse_args(int argc, char **argv)
5443{
5444 int c;
5445
5446 static struct option long_options[] = {
5447 { "client-sock", 1, 0, 'c' },
5448 { "apps-sock", 1, 0, 'a' },
3bd1e081
MD
5449 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5450 { "kconsumerd-err-sock", 1, 0, 'E' },
7753dea8
MD
5451 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5452 { "ustconsumerd32-err-sock", 1, 0, 'H' },
ebaeda94
MD
5453 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5454 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5455 { "consumerd32-path", 1, 0, 'u' },
5456 { "consumerd32-libdir", 1, 0, 'U' },
5457 { "consumerd64-path", 1, 0, 't' },
5458 { "consumerd64-libdir", 1, 0, 'T' },
fac6795d 5459 { "daemonize", 0, 0, 'd' },
5b8719f5 5460 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
5461 { "help", 0, 0, 'h' },
5462 { "group", 1, 0, 'g' },
5463 { "version", 0, 0, 'V' },
75462a81 5464 { "quiet", 0, 0, 'q' },
3f9947db 5465 { "verbose", 0, 0, 'v' },
3bd1e081 5466 { "verbose-consumer", 0, 0, 'Z' },
4fba7219 5467 { "no-kernel", 0, 0, 'N' },
fac6795d
DG
5468 { NULL, 0, 0, 0 }
5469 };
5470
5471 while (1) {
5472 int option_index = 0;
4fba7219 5473 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
54d01ffb 5474 long_options, &option_index);
fac6795d
DG
5475 if (c == -1) {
5476 break;
5477 }
5478
5479 switch (c) {
5480 case 0:
5481 fprintf(stderr, "option %s", long_options[option_index].name);
5482 if (optarg) {
5483 fprintf(stderr, " with arg %s\n", optarg);
5484 }
5485 break;
b716ce68 5486 case 'c':
fac6795d
DG
5487 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
5488 break;
5489 case 'a':
5490 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
5491 break;
5492 case 'd':
5493 opt_daemon = 1;
5494 break;
5495 case 'g':
fb09408a 5496 opt_tracing_group = optarg;
fac6795d
DG
5497 break;
5498 case 'h':
5499 usage();
5500 exit(EXIT_FAILURE);
5501 case 'V':
5502 fprintf(stdout, "%s\n", VERSION);
5503 exit(EXIT_SUCCESS);
5b8719f5
DG
5504 case 'S':
5505 opt_sig_parent = 1;
5506 break;
d6f42150 5507 case 'E':
3bd1e081 5508 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150
DG
5509 break;
5510 case 'C':
3bd1e081
MD
5511 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5512 break;
5513 case 'F':
7753dea8 5514 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3bd1e081
MD
5515 break;
5516 case 'D':
7753dea8
MD
5517 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5518 break;
5519 case 'H':
5520 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5521 break;
5522 case 'G':
5523 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150 5524 break;
4fba7219
DG
5525 case 'N':
5526 opt_no_kernel = 1;
5527 break;
75462a81 5528 case 'q':
97e19046 5529 lttng_opt_quiet = 1;
75462a81 5530 break;
3f9947db 5531 case 'v':
53086306 5532 /* Verbose level can increase using multiple -v */
97e19046 5533 lttng_opt_verbose += 1;
3f9947db 5534 break;
31f73cc9 5535 case 'Z':
3bd1e081 5536 opt_verbose_consumer += 1;
31f73cc9 5537 break;
fb09408a 5538 case 'u':
fc7a59ce 5539 consumerd32_bin= optarg;
ebaeda94
MD
5540 break;
5541 case 'U':
5542 consumerd32_libdir = optarg;
7753dea8
MD
5543 break;
5544 case 't':
fc7a59ce 5545 consumerd64_bin = optarg;
ebaeda94
MD
5546 break;
5547 case 'T':
5548 consumerd64_libdir = optarg;
fb09408a 5549 break;
fac6795d
DG
5550 default:
5551 /* Unknown option or other error.
5552 * Error is printed by getopt, just return */
5553 return -1;
5554 }
5555 }
5556
5557 return 0;
5558}
5559
5560/*
d063d709 5561 * Creates the two needed socket by the daemon.
d6f42150
DG
5562 * apps_sock - The communication socket for all UST apps.
5563 * client_sock - The communication of the cli tool (lttng).
fac6795d 5564 */
cf3af59e 5565static int init_daemon_socket(void)
fac6795d
DG
5566{
5567 int ret = 0;
5568 mode_t old_umask;
5569
5570 old_umask = umask(0);
5571
5572 /* Create client tool unix socket */
d6f42150
DG
5573 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5574 if (client_sock < 0) {
5575 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
5576 ret = -1;
5577 goto end;
5578 }
5579
5580 /* File permission MUST be 660 */
5581 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5582 if (ret < 0) {
d6f42150 5583 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 5584 PERROR("chmod");
fac6795d
DG
5585 goto end;
5586 }
5587
5588 /* Create the application unix socket */
d6f42150
DG
5589 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5590 if (apps_sock < 0) {
5591 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
5592 ret = -1;
5593 goto end;
5594 }
5595
d6f42150 5596 /* File permission MUST be 666 */
54d01ffb
DG
5597 ret = chmod(apps_unix_sock_path,
5598 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 5599 if (ret < 0) {
d6f42150 5600 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 5601 PERROR("chmod");
fac6795d
DG
5602 goto end;
5603 }
5604
5605end:
5606 umask(old_umask);
5607 return ret;
5608}
5609
5610/*
54d01ffb
DG
5611 * Check if the global socket is available, and if a daemon is answering at the
5612 * other side. If yes, error is returned.
fac6795d 5613 */
cf3af59e 5614static int check_existing_daemon(void)
fac6795d 5615{
7d8234d9 5616 /* Is there anybody out there ? */
099e26bd 5617 if (lttng_session_daemon_alive()) {
7d8234d9 5618 return -EEXIST;
099e26bd 5619 }
b09c7c76
DG
5620
5621 return 0;
fac6795d
DG
5622}
5623
fac6795d 5624/*
d063d709 5625 * Set the tracing group gid onto the client socket.
5e16da05 5626 *
d063d709 5627 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 5628 * permissive (root.root) to less permissive (root.tracing).
fac6795d 5629 */
be040666 5630static int set_permissions(char *rundir)
fac6795d
DG
5631{
5632 int ret;
996b65c8 5633 gid_t gid;
fac6795d 5634
6775595e
DG
5635 ret = allowed_group();
5636 if (ret < 0) {
be040666
DG
5637 WARN("No tracing group detected");
5638 ret = 0;
fac6795d
DG
5639 goto end;
5640 }
5641
6775595e
DG
5642 gid = ret;
5643
d6f42150 5644 /* Set lttng run dir */
be040666 5645 ret = chown(rundir, 0, gid);
d6f42150 5646 if (ret < 0) {
be040666 5647 ERR("Unable to set group on %s", rundir);
76d7553f 5648 PERROR("chown");
d6f42150
DG
5649 }
5650
d1613cf5 5651 /* Ensure tracing group can search the run dir */
61076f74 5652 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
d1613cf5
JN
5653 if (ret < 0) {
5654 ERR("Unable to set permissions on %s", rundir);
76d7553f 5655 PERROR("chmod");
d1613cf5
JN
5656 }
5657
d6f42150 5658 /* lttng client socket path */
996b65c8 5659 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 5660 if (ret < 0) {
d6f42150 5661 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 5662 PERROR("chown");
d6f42150
DG
5663 }
5664
3bd1e081
MD
5665 /* kconsumer error socket path */
5666 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
d6f42150 5667 if (ret < 0) {
3bd1e081 5668 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 5669 PERROR("chown");
3bd1e081
MD
5670 }
5671
7753dea8
MD
5672 /* 64-bit ustconsumer error socket path */
5673 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
5674 if (ret < 0) {
5675 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 5676 PERROR("chown");
7753dea8
MD
5677 }
5678
5679 /* 32-bit ustconsumer compat32 error socket path */
5680 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3bd1e081 5681 if (ret < 0) {
7753dea8 5682 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5683 PERROR("chown");
fac6795d
DG
5684 }
5685
d6f42150 5686 DBG("All permissions are set");
e07ae692 5687
fac6795d
DG
5688end:
5689 return ret;
5690}
5691
d6f42150 5692/*
d063d709 5693 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5694 */
67e40797 5695static int create_lttng_rundir(const char *rundir)
d6f42150
DG
5696{
5697 int ret;
5698
67e40797
DG
5699 DBG3("Creating LTTng run directory: %s", rundir);
5700
d1613cf5 5701 ret = mkdir(rundir, S_IRWXU);
d6f42150 5702 if (ret < 0) {
b1f11e69 5703 if (errno != EEXIST) {
67e40797 5704 ERR("Unable to create %s", rundir);
b1f11e69
DG
5705 goto error;
5706 } else {
5707 ret = 0;
5708 }
d6f42150
DG
5709 }
5710
5711error:
5712 return ret;
5713}
5714
5715/*
d063d709
DG
5716 * Setup sockets and directory needed by the kconsumerd communication with the
5717 * session daemon.
d6f42150 5718 */
67e40797
DG
5719static int set_consumer_sockets(struct consumer_data *consumer_data,
5720 const char *rundir)
d6f42150
DG
5721{
5722 int ret;
67e40797 5723 char path[PATH_MAX];
d6f42150 5724
67e40797 5725 switch (consumer_data->type) {
7753dea8 5726 case LTTNG_CONSUMER_KERNEL:
60922cb0 5727 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
5728 break;
5729 case LTTNG_CONSUMER64_UST:
60922cb0 5730 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
5731 break;
5732 case LTTNG_CONSUMER32_UST:
60922cb0 5733 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
5734 break;
5735 default:
5736 ERR("Consumer type unknown");
5737 ret = -EINVAL;
5738 goto error;
d6f42150
DG
5739 }
5740
67e40797
DG
5741 DBG2("Creating consumer directory: %s", path);
5742
d1613cf5 5743 ret = mkdir(path, S_IRWXU);
d6f42150 5744 if (ret < 0) {
6beb2242 5745 if (errno != EEXIST) {
f11e84c2 5746 PERROR("mkdir");
3bd1e081 5747 ERR("Failed to create %s", path);
6beb2242
DG
5748 goto error;
5749 }
f11e84c2 5750 ret = -1;
d6f42150
DG
5751 }
5752
5753 /* Create the kconsumerd error unix socket */
3bd1e081
MD
5754 consumer_data->err_sock =
5755 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5756 if (consumer_data->err_sock < 0) {
5757 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5758 ret = -1;
5759 goto error;
5760 }
5761
5762 /* File permission MUST be 660 */
3bd1e081 5763 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5764 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5765 if (ret < 0) {
3bd1e081 5766 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5767 PERROR("chmod");
d6f42150
DG
5768 goto error;
5769 }
5770
5771error:
5772 return ret;
5773}
5774
fac6795d 5775/*
d063d709 5776 * Signal handler for the daemon
cf3af59e 5777 *
54d01ffb
DG
5778 * Simply stop all worker threads, leaving main() return gracefully after
5779 * joining all threads and calling cleanup().
fac6795d
DG
5780 */
5781static void sighandler(int sig)
5782{
5783 switch (sig) {
cf3af59e 5784 case SIGPIPE:
af87c45a 5785 DBG("SIGPIPE caught");
cf3af59e
MD
5786 return;
5787 case SIGINT:
af87c45a 5788 DBG("SIGINT caught");
cf3af59e
MD
5789 stop_threads();
5790 break;
5791 case SIGTERM:
af87c45a 5792 DBG("SIGTERM caught");
cf3af59e
MD
5793 stop_threads();
5794 break;
5795 default:
5796 break;
fac6795d 5797 }
fac6795d
DG
5798}
5799
5800/*
d063d709 5801 * Setup signal handler for :
1d4b027a 5802 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5803 */
1d4b027a 5804static int set_signal_handler(void)
fac6795d 5805{
1d4b027a
DG
5806 int ret = 0;
5807 struct sigaction sa;
5808 sigset_t sigset;
fac6795d 5809
1d4b027a 5810 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5811 PERROR("sigemptyset");
1d4b027a
DG
5812 return ret;
5813 }
d6f42150 5814
1d4b027a
DG
5815 sa.sa_handler = sighandler;
5816 sa.sa_mask = sigset;
5817 sa.sa_flags = 0;
5818 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5819 PERROR("sigaction");
1d4b027a 5820 return ret;
d6f42150
DG
5821 }
5822
1d4b027a 5823 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5824 PERROR("sigaction");
1d4b027a 5825 return ret;
d6f42150 5826 }
aaf26714 5827
1d4b027a 5828 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 5829 PERROR("sigaction");
1d4b027a 5830 return ret;
8c0faa1d
DG
5831 }
5832
1d4b027a
DG
5833 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5834
5835 return ret;
fac6795d
DG
5836}
5837
f3ed775e 5838/*
d063d709
DG
5839 * Set open files limit to unlimited. This daemon can open a large number of
5840 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
5841 */
5842static void set_ulimit(void)
5843{
5844 int ret;
5845 struct rlimit lim;
5846
a88df331 5847 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
5848 lim.rlim_cur = 65535;
5849 lim.rlim_max = 65535;
5850
5851 ret = setrlimit(RLIMIT_NOFILE, &lim);
5852 if (ret < 0) {
76d7553f 5853 PERROR("failed to set open files limit");
f3ed775e
DG
5854 }
5855}
5856
fac6795d
DG
5857/*
5858 * main
5859 */
5860int main(int argc, char **argv)
5861{
fac6795d
DG
5862 int ret = 0;
5863 void *status;
b082db07 5864 const char *home_path;
fac6795d 5865
335a95b7
MD
5866 init_kernel_workarounds();
5867
f6a9efaa
DG
5868 rcu_register_thread();
5869
7753dea8 5870 setup_consumerd_path();
fb09408a 5871
fac6795d
DG
5872 /* Parse arguments */
5873 progname = argv[0];
5874 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 5875 goto error;
fac6795d
DG
5876 }
5877
5878 /* Daemonize */
5879 if (opt_daemon) {
ceed52b5
MD
5880 int i;
5881
5882 /*
5883 * fork
5884 * child: setsid, close FD 0, 1, 2, chdir /
5885 * parent: exit (if fork is successful)
5886 */
53094c05
DG
5887 ret = daemon(0, 0);
5888 if (ret < 0) {
76d7553f 5889 PERROR("daemon");
cf3af59e 5890 goto error;
53094c05 5891 }
ceed52b5
MD
5892 /*
5893 * We are in the child. Make sure all other file
5894 * descriptors are closed, in case we are called with
5895 * more opened file descriptors than the standard ones.
5896 */
5897 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5898 (void) close(i);
5899 }
5900 }
5901
5902 /* Create thread quit pipe */
5903 if ((ret = init_thread_quit_pipe()) < 0) {
5904 goto error;
fac6795d
DG
5905 }
5906
5907 /* Check if daemon is UID = 0 */
5908 is_root = !getuid();
5909
fac6795d 5910 if (is_root) {
990570ed 5911 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
67e40797
DG
5912
5913 /* Create global run dir with root access */
5914 ret = create_lttng_rundir(rundir);
d6f42150 5915 if (ret < 0) {
cf3af59e 5916 goto error;
d6f42150
DG
5917 }
5918
fac6795d 5919 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
5920 snprintf(apps_unix_sock_path, PATH_MAX,
5921 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
5922 }
5923
5924 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
5925 snprintf(client_unix_sock_path, PATH_MAX,
5926 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
5927 }
0fdd1e2c
DG
5928
5929 /* Set global SHM for ust */
5930 if (strlen(wait_shm_path) == 0) {
5931 snprintf(wait_shm_path, PATH_MAX,
5932 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
5933 }
67e40797 5934
44a5e5eb
DG
5935 if (strlen(health_unix_sock_path) == 0) {
5936 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5937 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
5938 }
5939
67e40797
DG
5940 /* Setup kernel consumerd path */
5941 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 5942 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
67e40797 5943 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5944 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
67e40797
DG
5945
5946 DBG2("Kernel consumer err path: %s",
5947 kconsumer_data.err_unix_sock_path);
5948 DBG2("Kernel consumer cmd path: %s",
5949 kconsumer_data.cmd_unix_sock_path);
fac6795d 5950 } else {
b082db07
DG
5951 home_path = get_home_dir();
5952 if (home_path == NULL) {
273ea72c
DG
5953 /* TODO: Add --socket PATH option */
5954 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
5955 ret = -EPERM;
5956 goto error;
b082db07
DG
5957 }
5958
67e40797
DG
5959 /*
5960 * Create rundir from home path. This will create something like
5961 * $HOME/.lttng
5962 */
990570ed 5963 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797
DG
5964 if (ret < 0) {
5965 ret = -ENOMEM;
5966 goto error;
5967 }
5968
5969 ret = create_lttng_rundir(rundir);
5970 if (ret < 0) {
5971 goto error;
5972 }
5973
fac6795d 5974 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 5975 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 5976 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
5977 }
5978
5979 /* Set the cli tool unix socket path */
5980 if (strlen(client_unix_sock_path) == 0) {
d6f42150 5981 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 5982 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 5983 }
0fdd1e2c
DG
5984
5985 /* Set global SHM for ust */
5986 if (strlen(wait_shm_path) == 0) {
5987 snprintf(wait_shm_path, PATH_MAX,
5988 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
5989 }
44a5e5eb
DG
5990
5991 /* Set health check Unix path */
5992 if (strlen(health_unix_sock_path) == 0) {
5993 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5994 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
5995 }
fac6795d
DG
5996 }
5997
5c827ce0
DG
5998 /* Set consumer initial state */
5999 kernel_consumerd_state = CONSUMER_STOPPED;
6000 ust_consumerd_state = CONSUMER_STOPPED;
6001
847177cd
DG
6002 DBG("Client socket path %s", client_unix_sock_path);
6003 DBG("Application socket path %s", apps_unix_sock_path);
67e40797
DG
6004 DBG("LTTng run directory path: %s", rundir);
6005
6006 /* 32 bits consumerd path setup */
6007 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 6008 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
67e40797 6009 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 6010 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
67e40797
DG
6011
6012 DBG2("UST consumer 32 bits err path: %s",
6013 ustconsumer32_data.err_unix_sock_path);
6014 DBG2("UST consumer 32 bits cmd path: %s",
6015 ustconsumer32_data.cmd_unix_sock_path);
6016
6017 /* 64 bits consumerd path setup */
6018 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 6019 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
67e40797 6020 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 6021 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
67e40797
DG
6022
6023 DBG2("UST consumer 64 bits err path: %s",
6024 ustconsumer64_data.err_unix_sock_path);
6025 DBG2("UST consumer 64 bits cmd path: %s",
6026 ustconsumer64_data.cmd_unix_sock_path);
847177cd 6027
273ea72c 6028 /*
7d8234d9 6029 * See if daemon already exist.
fac6795d 6030 */
7d8234d9 6031 if ((ret = check_existing_daemon()) < 0) {
75462a81 6032 ERR("Already running daemon.\n");
273ea72c 6033 /*
cf3af59e
MD
6034 * We do not goto exit because we must not cleanup()
6035 * because a daemon is already running.
ab118b20 6036 */
cf3af59e 6037 goto error;
a88df331
DG
6038 }
6039
1427f9b2
DG
6040 /*
6041 * Init UST app hash table. Alloc hash table before this point since
6042 * cleanup() can get called after that point.
6043 */
6044 ust_app_ht_alloc();
6045
54d01ffb 6046 /* After this point, we can safely call cleanup() with "goto exit" */
a88df331
DG
6047
6048 /*
6049 * These actions must be executed as root. We do that *after* setting up
6050 * the sockets path because we MUST make the check for another daemon using
6051 * those paths *before* trying to set the kernel consumer sockets and init
6052 * kernel tracer.
6053 */
6054 if (is_root) {
67e40797 6055 ret = set_consumer_sockets(&kconsumer_data, rundir);
7753dea8
MD
6056 if (ret < 0) {
6057 goto exit;
6058 }
6059
a88df331 6060 /* Setup kernel tracer */
4fba7219
DG
6061 if (!opt_no_kernel) {
6062 init_kernel_tracer();
6063 }
a88df331
DG
6064
6065 /* Set ulimit for open files */
6066 set_ulimit();
fac6795d 6067 }
4063050c
MD
6068 /* init lttng_fd tracking must be done after set_ulimit. */
6069 lttng_fd_init();
fac6795d 6070
67e40797
DG
6071 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
6072 if (ret < 0) {
6073 goto exit;
6074 }
6075
6076 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
6077 if (ret < 0) {
6078 goto exit;
6079 }
6080
cf3af59e
MD
6081 if ((ret = set_signal_handler()) < 0) {
6082 goto exit;
fac6795d
DG
6083 }
6084
d6f42150 6085 /* Setup the needed unix socket */
cf3af59e
MD
6086 if ((ret = init_daemon_socket()) < 0) {
6087 goto exit;
fac6795d
DG
6088 }
6089
6090 /* Set credentials to socket */
be040666 6091 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
cf3af59e 6092 goto exit;
fac6795d
DG
6093 }
6094
5b8719f5
DG
6095 /* Get parent pid if -S, --sig-parent is specified. */
6096 if (opt_sig_parent) {
6097 ppid = getppid();
6098 }
6099
7a485870 6100 /* Setup the kernel pipe for waking up the kernel thread */
ef599319 6101 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
cf3af59e 6102 goto exit;
7a485870
DG
6103 }
6104
099e26bd 6105 /* Setup the thread apps communication pipe. */
ef599319 6106 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
099e26bd
DG
6107 goto exit;
6108 }
6109
6110 /* Init UST command queue. */
6111 cds_wfq_init(&ust_cmd_queue.queue);
6112
273ea72c 6113 /*
54d01ffb
DG
6114 * Get session list pointer. This pointer MUST NOT be free(). This list is
6115 * statically declared in session.c
273ea72c 6116 */
54d01ffb 6117 session_list_ptr = session_get_list();
b5541356 6118
5eb91c98
DG
6119 /* Set up max poll set size */
6120 lttng_poll_set_max_size();
6121
00e2e675
DG
6122 /*
6123 * Set network sequence index to 1 for streams to match a relayd socket on
6124 * the consumer side.
6125 */
6126 uatomic_set(&relayd_net_seq_idx, 1);
6127
44a5e5eb
DG
6128 /* Init all health thread counters. */
6129 health_init(&health_thread_cmd);
6130 health_init(&health_thread_kernel);
139ac872 6131 health_init(&health_thread_app_manage);
44a5e5eb
DG
6132 health_init(&health_thread_app_reg);
6133
6134 /*
6135 * Init health counters of the consumer thread. We do a quick hack here to
6136 * the state of the consumer health is fine even if the thread is not
6137 * started. This is simply to ease our life and has no cost what so ever.
6138 */
6139 health_init(&kconsumer_data.health);
6140 health_poll_update(&kconsumer_data.health);
6141 health_init(&ustconsumer32_data.health);
6142 health_poll_update(&ustconsumer32_data.health);
6143 health_init(&ustconsumer64_data.health);
6144 health_poll_update(&ustconsumer64_data.health);
6145
6146 /* Create thread to manage the client socket */
6147 ret = pthread_create(&health_thread, NULL,
6148 thread_manage_health, (void *) NULL);
6149 if (ret != 0) {
6150 PERROR("pthread_create health");
6151 goto exit_health;
6152 }
6153
cf3af59e 6154 /* Create thread to manage the client socket */
099e26bd
DG
6155 ret = pthread_create(&client_thread, NULL,
6156 thread_manage_clients, (void *) NULL);
cf3af59e 6157 if (ret != 0) {
76d7553f 6158 PERROR("pthread_create clients");
cf3af59e
MD
6159 goto exit_client;
6160 }
fac6795d 6161
099e26bd
DG
6162 /* Create thread to dispatch registration */
6163 ret = pthread_create(&dispatch_thread, NULL,
6164 thread_dispatch_ust_registration, (void *) NULL);
6165 if (ret != 0) {
76d7553f 6166 PERROR("pthread_create dispatch");
099e26bd
DG
6167 goto exit_dispatch;
6168 }
6169
6170 /* Create thread to manage application registration. */
6171 ret = pthread_create(&reg_apps_thread, NULL,
6172 thread_registration_apps, (void *) NULL);
6173 if (ret != 0) {
76d7553f 6174 PERROR("pthread_create registration");
099e26bd
DG
6175 goto exit_reg_apps;
6176 }
6177
cf3af59e 6178 /* Create thread to manage application socket */
54d01ffb
DG
6179 ret = pthread_create(&apps_thread, NULL,
6180 thread_manage_apps, (void *) NULL);
cf3af59e 6181 if (ret != 0) {
76d7553f 6182 PERROR("pthread_create apps");
cf3af59e
MD
6183 goto exit_apps;
6184 }
fac6795d 6185
cf3af59e 6186 /* Create kernel thread to manage kernel event */
54d01ffb
DG
6187 ret = pthread_create(&kernel_thread, NULL,
6188 thread_manage_kernel, (void *) NULL);
cf3af59e 6189 if (ret != 0) {
76d7553f 6190 PERROR("pthread_create kernel");
cf3af59e
MD
6191 goto exit_kernel;
6192 }
7a485870 6193
cf3af59e
MD
6194 ret = pthread_join(kernel_thread, &status);
6195 if (ret != 0) {
76d7553f 6196 PERROR("pthread_join");
cf3af59e 6197 goto error; /* join error, exit without cleanup */
fac6795d
DG
6198 }
6199
cf3af59e
MD
6200exit_kernel:
6201 ret = pthread_join(apps_thread, &status);
6202 if (ret != 0) {
76d7553f 6203 PERROR("pthread_join");
cf3af59e
MD
6204 goto error; /* join error, exit without cleanup */
6205 }
fac6795d 6206
cf3af59e 6207exit_apps:
099e26bd
DG
6208 ret = pthread_join(reg_apps_thread, &status);
6209 if (ret != 0) {
76d7553f 6210 PERROR("pthread_join");
099e26bd
DG
6211 goto error; /* join error, exit without cleanup */
6212 }
6213
6214exit_reg_apps:
6215 ret = pthread_join(dispatch_thread, &status);
6216 if (ret != 0) {
76d7553f 6217 PERROR("pthread_join");
099e26bd
DG
6218 goto error; /* join error, exit without cleanup */
6219 }
6220
6221exit_dispatch:
cf3af59e
MD
6222 ret = pthread_join(client_thread, &status);
6223 if (ret != 0) {
76d7553f 6224 PERROR("pthread_join");
cf3af59e
MD
6225 goto error; /* join error, exit without cleanup */
6226 }
6227
3bd1e081 6228 ret = join_consumer_thread(&kconsumer_data);
cf3af59e 6229 if (ret != 0) {
76d7553f 6230 PERROR("join_consumer");
cf3af59e
MD
6231 goto error; /* join error, exit without cleanup */
6232 }
a88df331 6233
cf3af59e 6234exit_client:
44a5e5eb 6235exit_health:
a88df331 6236exit:
cf3af59e
MD
6237 /*
6238 * cleanup() is called when no other thread is running.
6239 */
f6a9efaa 6240 rcu_thread_online();
cf3af59e 6241 cleanup();
f6a9efaa
DG
6242 rcu_thread_offline();
6243 rcu_unregister_thread();
67e40797 6244 if (!ret) {
cf3af59e 6245 exit(EXIT_SUCCESS);
67e40797 6246 }
cf3af59e 6247error:
5e16da05 6248 exit(EXIT_FAILURE);
fac6795d 6249}
This page took 0.408029 seconds and 4 git commands to generate.