Fix lttng view using lttng list sessions
[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
ad20f474
DG
2421/*
2422 * Create a session path used by list_lttng_sessions for the case that the
2423 * session consumer is on the network.
2424 */
2425static int build_network_session_path(char *dst, size_t size,
2426 struct ltt_session *session)
2427{
2428 int ret, kdata_port, udata_port;
2429 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
2430 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
2431
2432 assert(session);
2433 assert(dst);
2434
2435 memset(tmp_urls, 0, sizeof(tmp_urls));
2436 memset(tmp_uurl, 0, sizeof(tmp_uurl));
2437
2438 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
2439
2440 if (session->kernel_session && session->kernel_session->consumer) {
2441 kuri = &session->kernel_session->consumer->dst.net.control;
2442 kdata_port = session->kernel_session->consumer->dst.net.data.port;
2443 }
2444
2445 if (session->ust_session && session->ust_session->consumer) {
2446 uuri = &session->ust_session->consumer->dst.net.control;
2447 udata_port = session->ust_session->consumer->dst.net.data.port;
2448 }
2449
2450 if (uuri == NULL && kuri == NULL) {
2451 uri = &session->consumer->dst.net.control;
2452 kdata_port = session->consumer->dst.net.data.port;
2453 } else if (kuri && uuri) {
2454 ret = uri_compare(kuri, uuri);
2455 if (ret) {
2456 /* Not Equal */
2457 uri = kuri;
2458 /* Build uuri URL string */
2459 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
2460 if (ret < 0) {
2461 goto error;
2462 }
2463 } else {
2464 uri = kuri;
2465 }
2466 } else if (kuri && uuri == NULL) {
2467 uri = kuri;
2468 } else if (uuri && kuri == NULL) {
2469 uri = uuri;
2470 }
2471
2472 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
2473 if (ret < 0) {
2474 goto error;
2475 }
2476
2477 if (strlen(tmp_uurl) > 0) {
2478 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
2479 tmp_urls, kdata_port, tmp_uurl, udata_port);
2480 } else {
2481 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, kdata_port);
2482 }
2483
2484error:
2485 return ret;
2486}
2487
6c9cc2ab
DG
2488/*
2489 * Using the session list, filled a lttng_session array to send back to the
2490 * client for session listing.
2491 *
2492 * The session list lock MUST be acquired before calling this function. Use
54d01ffb 2493 * session_lock_list() and session_unlock_list().
6c9cc2ab 2494 */
c7704d57
DG
2495static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2496 gid_t gid)
6c9cc2ab 2497{
ad20f474 2498 int ret;
8e0af1b4 2499 unsigned int i = 0;
6c9cc2ab
DG
2500 struct ltt_session *session;
2501
8e0af1b4
MD
2502 DBG("Getting all available session for UID %d GID %d",
2503 uid, gid);
6c9cc2ab
DG
2504 /*
2505 * Iterate over session list and append data after the control struct in
2506 * the buffer.
2507 */
2508 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
8e0af1b4
MD
2509 /*
2510 * Only list the sessions the user can control.
2511 */
2512 if (!session_access_ok(session, uid, gid)) {
2513 continue;
2514 }
ad20f474 2515
6f636768
DG
2516 struct ltt_kernel_session *ksess = session->kernel_session;
2517 struct ltt_ust_session *usess = session->ust_session;
2518
2519 if (session->consumer->type == CONSUMER_DST_NET ||
2520 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2521 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
ad20f474
DG
2522 ret = build_network_session_path(sessions[i].path,
2523 sizeof(session[i].path), session);
6f636768
DG
2524 } else {
2525 ret = snprintf(sessions[i].path, sizeof(session[i].path), "%s",
2526 session->consumer->dst.trace_path);
ad20f474
DG
2527 }
2528 if (ret < 0) {
2529 PERROR("snprintf session path");
2530 continue;
2531 }
2532
6c9cc2ab 2533 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 2534 sessions[i].name[NAME_MAX - 1] = '\0';
464dd62d 2535 sessions[i].enabled = session->enabled;
6c9cc2ab
DG
2536 i++;
2537 }
2538}
2539
9f19cc17
DG
2540/*
2541 * Fill lttng_channel array of all channels.
2542 */
b551a063 2543static void list_lttng_channels(int domain, struct ltt_session *session,
9f19cc17
DG
2544 struct lttng_channel *channels)
2545{
2546 int i = 0;
2547 struct ltt_kernel_channel *kchan;
2548
2549 DBG("Listing channels for session %s", session->name);
2550
b551a063
DG
2551 switch (domain) {
2552 case LTTNG_DOMAIN_KERNEL:
2553 /* Kernel channels */
2554 if (session->kernel_session != NULL) {
2555 cds_list_for_each_entry(kchan,
2556 &session->kernel_session->channel_list.head, list) {
2557 /* Copy lttng_channel struct to array */
2558 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
2559 channels[i].enabled = kchan->enabled;
2560 i++;
2561 }
2562 }
2563 break;
2564 case LTTNG_DOMAIN_UST:
2565 {
bec39940 2566 struct lttng_ht_iter iter;
b551a063
DG
2567 struct ltt_ust_channel *uchan;
2568
bec39940
DG
2569 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
2570 &iter.iter, uchan, node.node) {
b551a063
DG
2571 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
2572 channels[i].attr.overwrite = uchan->attr.overwrite;
2573 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
2574 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
2575 channels[i].attr.switch_timer_interval =
2576 uchan->attr.switch_timer_interval;
2577 channels[i].attr.read_timer_interval =
2578 uchan->attr.read_timer_interval;
aea829b3 2579 channels[i].enabled = uchan->enabled;
5edd7e09
DG
2580 switch (uchan->attr.output) {
2581 case LTTNG_UST_MMAP:
2582 default:
2583 channels[i].attr.output = LTTNG_EVENT_MMAP;
2584 break;
2585 }
befe0905 2586 i++;
b551a063
DG
2587 }
2588 break;
2589 }
2590 default:
2591 break;
2592 }
2593}
2594
2595/*
2596 * Create a list of ust global domain events.
2597 */
2598static int list_lttng_ust_global_events(char *channel_name,
2599 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
2600{
2601 int i = 0, ret = 0;
2602 unsigned int nb_event = 0;
bec39940
DG
2603 struct lttng_ht_iter iter;
2604 struct lttng_ht_node_str *node;
b551a063
DG
2605 struct ltt_ust_channel *uchan;
2606 struct ltt_ust_event *uevent;
2607 struct lttng_event *tmp;
2608
2609 DBG("Listing UST global events for channel %s", channel_name);
2610
2611 rcu_read_lock();
2612
bec39940
DG
2613 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
2614 node = lttng_ht_iter_get_node_str(&iter);
d7b75b30
DG
2615 if (node == NULL) {
2616 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
2617 goto error;
b551a063
DG
2618 }
2619
bec39940 2620 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
d7b75b30 2621
bec39940 2622 nb_event += lttng_ht_get_count(uchan->events);
d7b75b30 2623
b551a063
DG
2624 if (nb_event == 0) {
2625 ret = nb_event;
2626 goto error;
2627 }
2628
2629 DBG3("Listing UST global %d events", nb_event);
2630
2631 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
2632 if (tmp == NULL) {
2633 ret = -LTTCOMM_FATAL;
2634 goto error;
2635 }
2636
bec39940 2637 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
d7b75b30
DG
2638 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
2639 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2640 tmp[i].enabled = uevent->enabled;
2641 switch (uevent->attr.instrumentation) {
2642 case LTTNG_UST_TRACEPOINT:
2643 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
2644 break;
2645 case LTTNG_UST_PROBE:
2646 tmp[i].type = LTTNG_EVENT_PROBE;
2647 break;
2648 case LTTNG_UST_FUNCTION:
2649 tmp[i].type = LTTNG_EVENT_FUNCTION;
2650 break;
0cda4f28 2651 }
8005f29a 2652 tmp[i].loglevel = uevent->attr.loglevel;
0cda4f28 2653 switch (uevent->attr.loglevel_type) {
8005f29a 2654 case LTTNG_UST_LOGLEVEL_ALL:
22e25b71 2655 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
0cda4f28 2656 break;
8005f29a 2657 case LTTNG_UST_LOGLEVEL_RANGE:
22e25b71 2658 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
8005f29a
MD
2659 break;
2660 case LTTNG_UST_LOGLEVEL_SINGLE:
22e25b71 2661 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
ac3bd9c0 2662 break;
9f19cc17 2663 }
fceb65df
MD
2664 if (uevent->filter) {
2665 tmp[i].filter = 1;
2666 }
d7b75b30 2667 i++;
9f19cc17
DG
2668 }
2669
b551a063
DG
2670 ret = nb_event;
2671 *events = tmp;
2672
2673error:
2674 rcu_read_unlock();
2675 return ret;
9f19cc17
DG
2676}
2677
2678/*
b551a063 2679 * Fill lttng_event array of all kernel events in the channel.
9f19cc17 2680 */
b551a063
DG
2681static int list_lttng_kernel_events(char *channel_name,
2682 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
9f19cc17 2683{
b551a063
DG
2684 int i = 0, ret;
2685 unsigned int nb_event;
9f19cc17 2686 struct ltt_kernel_event *event;
b551a063
DG
2687 struct ltt_kernel_channel *kchan;
2688
2689 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
2690 if (kchan == NULL) {
2691 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2692 goto error;
2693 }
2694
2695 nb_event = kchan->event_count;
9f19cc17
DG
2696
2697 DBG("Listing events for channel %s", kchan->channel->name);
2698
b551a063
DG
2699 if (nb_event == 0) {
2700 ret = nb_event;
2701 goto error;
2702 }
2703
2704 *events = zmalloc(nb_event * sizeof(struct lttng_event));
2705 if (*events == NULL) {
2706 ret = LTTCOMM_FATAL;
2707 goto error;
2708 }
2709
9f19cc17
DG
2710 /* Kernel channels */
2711 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
b551a063
DG
2712 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
2713 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2714 (*events)[i].enabled = event->enabled;
9f19cc17
DG
2715 switch (event->event->instrumentation) {
2716 case LTTNG_KERNEL_TRACEPOINT:
b551a063 2717 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
9f19cc17
DG
2718 break;
2719 case LTTNG_KERNEL_KPROBE:
2720 case LTTNG_KERNEL_KRETPROBE:
b551a063
DG
2721 (*events)[i].type = LTTNG_EVENT_PROBE;
2722 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
9f19cc17
DG
2723 sizeof(struct lttng_kernel_kprobe));
2724 break;
2725 case LTTNG_KERNEL_FUNCTION:
b551a063
DG
2726 (*events)[i].type = LTTNG_EVENT_FUNCTION;
2727 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
9f19cc17
DG
2728 sizeof(struct lttng_kernel_function));
2729 break;
0133c199 2730 case LTTNG_KERNEL_NOOP:
b551a063 2731 (*events)[i].type = LTTNG_EVENT_NOOP;
0133c199 2732 break;
a54bd42d 2733 case LTTNG_KERNEL_SYSCALL:
b551a063 2734 (*events)[i].type = LTTNG_EVENT_SYSCALL;
0133c199 2735 break;
7a3d1328
MD
2736 case LTTNG_KERNEL_ALL:
2737 assert(0);
2738 break;
9f19cc17
DG
2739 }
2740 i++;
2741 }
b551a063
DG
2742
2743 return nb_event;
2744
2745error:
2746 return ret;
9f19cc17
DG
2747}
2748
a4b92340
DG
2749
2750/*
2751 * Add URI so the consumer output object. Set the correct path depending on the
2752 * domain adding the default trace directory.
2753 */
2754static int add_uri_to_consumer(struct consumer_output *consumer,
07424f16 2755 struct lttng_uri *uri, int domain, const char *session_name)
a4b92340 2756{
07424f16 2757 int ret = LTTCOMM_OK;
a4b92340
DG
2758 const char *default_trace_dir;
2759
2760 assert(uri);
2761
2762 if (consumer == NULL) {
2763 DBG("No consumer detected. Don't add URI. Stopping.");
2764 ret = LTTCOMM_NO_CONSUMER;
2765 goto error;
2766 }
2767
2768 switch (domain) {
2769 case LTTNG_DOMAIN_KERNEL:
2770 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
2771 break;
2772 case LTTNG_DOMAIN_UST:
2773 default_trace_dir = DEFAULT_UST_TRACE_DIR;
2774 break;
2775 default:
2776 /*
2777 * This case is possible is we try to add the URI to the global tracing
2778 * session consumer object which in this case there is no subdir.
2779 */
2780 default_trace_dir = "";
2781 }
2782
2783 switch (uri->dtype) {
2784 case LTTNG_DST_IPV4:
2785 case LTTNG_DST_IPV6:
2786 DBG2("Setting network URI to consumer");
2787
2788 /* Set URI into consumer output object */
2789 ret = consumer_set_network_uri(consumer, uri);
2790 if (ret < 0) {
2791 ret = LTTCOMM_FATAL;
2792 goto error;
ad20f474
DG
2793 } else if (ret == 1) {
2794 /*
2795 * URI was the same in the consumer so we do not append the subdir
2796 * again so to not duplicate output dir.
2797 */
2798 goto error;
a4b92340
DG
2799 }
2800
07424f16
DG
2801 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
2802 ret = set_consumer_subdir(consumer, session_name);
2803 if (ret < 0) {
2804 ret = LTTCOMM_FATAL;
2805 goto error;
2806 }
2807 }
2808
2809 if (uri->stype == LTTNG_STREAM_CONTROL) {
2810 /* On a new subdir, reappend the default trace dir. */
2811 strncat(consumer->subdir, default_trace_dir, sizeof(consumer->subdir));
2812 DBG3("Append domain trace name to subdir %s", consumer->subdir);
a4b92340
DG
2813 }
2814
2815 break;
2816 case LTTNG_DST_PATH:
2817 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
2818 memset(consumer->dst.trace_path, 0,
2819 sizeof(consumer->dst.trace_path));
2820 strncpy(consumer->dst.trace_path, uri->dst.path,
2821 sizeof(consumer->dst.trace_path));
2822 /* Append default trace dir */
2823 strncat(consumer->dst.trace_path, default_trace_dir,
2824 sizeof(consumer->dst.trace_path));
2825 /* Flag consumer as local. */
2826 consumer->type = CONSUMER_DST_LOCAL;
2827 break;
2828 }
2829
2830error:
2831 return ret;
2832}
2833
fac6795d 2834/*
54d01ffb 2835 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
fac6795d 2836 */
54d01ffb
DG
2837static int cmd_disable_channel(struct ltt_session *session,
2838 int domain, char *channel_name)
fac6795d 2839{
54d01ffb 2840 int ret;
78f0bacd
DG
2841 struct ltt_ust_session *usess;
2842
2843 usess = session->ust_session;
379473d2 2844
54d01ffb 2845 switch (domain) {
78f0bacd
DG
2846 case LTTNG_DOMAIN_KERNEL:
2847 {
2848 ret = channel_kernel_disable(session->kernel_session,
2849 channel_name);
2850 if (ret != LTTCOMM_OK) {
2851 goto error;
2852 }
54d01ffb 2853
78f0bacd
DG
2854 kernel_wait_quiescent(kernel_tracer_fd);
2855 break;
2856 }
2857 case LTTNG_DOMAIN_UST:
2858 {
2859 struct ltt_ust_channel *uchan;
bec39940 2860 struct lttng_ht *chan_ht;
78f0bacd 2861
7885e399
DG
2862 chan_ht = usess->domain_global.channels;
2863
2864 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
78f0bacd
DG
2865 if (uchan == NULL) {
2866 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
54d01ffb 2867 goto error;
78f0bacd
DG
2868 }
2869
7885e399
DG
2870 ret = channel_ust_disable(usess, domain, uchan);
2871 if (ret != LTTCOMM_OK) {
78f0bacd
DG
2872 goto error;
2873 }
78f0bacd
DG
2874 break;
2875 }
d78d6610 2876#if 0
78f0bacd
DG
2877 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2878 case LTTNG_DOMAIN_UST_EXEC_NAME:
2879 case LTTNG_DOMAIN_UST_PID:
d78d6610 2880#endif
78f0bacd
DG
2881 default:
2882 ret = LTTCOMM_UNKNOWN_DOMAIN;
2883 goto error;
20fe2104
DG
2884 }
2885
54d01ffb 2886 ret = LTTCOMM_OK;
d65106b1 2887
54d01ffb
DG
2888error:
2889 return ret;
2890}
2891
2892/*
2893 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2894 */
44d3bd01 2895static int cmd_enable_channel(struct ltt_session *session,
aea829b3 2896 int domain, struct lttng_channel *attr)
54d01ffb
DG
2897{
2898 int ret;
f6a9efaa 2899 struct ltt_ust_session *usess = session->ust_session;
bec39940 2900 struct lttng_ht *chan_ht;
f6a9efaa 2901
5d56b5aa 2902 DBG("Enabling channel %s for session %s", attr->name, session->name);
d65106b1 2903
aea829b3 2904 switch (domain) {
44d3bd01
DG
2905 case LTTNG_DOMAIN_KERNEL:
2906 {
2907 struct ltt_kernel_channel *kchan;
54d01ffb 2908
44d3bd01
DG
2909 kchan = trace_kernel_get_channel_by_name(attr->name,
2910 session->kernel_session);
2911 if (kchan == NULL) {
2912 ret = channel_kernel_create(session->kernel_session,
2913 attr, kernel_poll_pipe[1]);
2914 } else {
2915 ret = channel_kernel_enable(session->kernel_session, kchan);
2916 }
2917
2918 if (ret != LTTCOMM_OK) {
2919 goto error;
2920 }
2921
2922 kernel_wait_quiescent(kernel_tracer_fd);
2923 break;
2924 }
f6a9efaa
DG
2925 case LTTNG_DOMAIN_UST:
2926 {
2927 struct ltt_ust_channel *uchan;
2928
7885e399 2929 chan_ht = usess->domain_global.channels;
f6a9efaa 2930
7885e399 2931 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
f6a9efaa 2932 if (uchan == NULL) {
7885e399 2933 ret = channel_ust_create(usess, domain, attr);
f6a9efaa 2934 } else {
7885e399 2935 ret = channel_ust_enable(usess, domain, uchan);
48842b30 2936 }
f6a9efaa
DG
2937 break;
2938 }
d78d6610 2939#if 0
78f0bacd
DG
2940 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2941 case LTTNG_DOMAIN_UST_EXEC_NAME:
44d3bd01 2942 case LTTNG_DOMAIN_UST_PID:
d78d6610 2943#endif
44d3bd01
DG
2944 default:
2945 ret = LTTCOMM_UNKNOWN_DOMAIN;
2946 goto error;
54d01ffb
DG
2947 }
2948
54d01ffb
DG
2949error:
2950 return ret;
2951}
2952
2953/*
2954 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2955 */
2956static int cmd_disable_event(struct ltt_session *session, int domain,
2957 char *channel_name, char *event_name)
2958{
2959 int ret;
54d01ffb
DG
2960
2961 switch (domain) {
2962 case LTTNG_DOMAIN_KERNEL:
2bdd86d4
MD
2963 {
2964 struct ltt_kernel_channel *kchan;
b0a40d28 2965 struct ltt_kernel_session *ksess;
2bdd86d4 2966
b0a40d28
DG
2967 ksess = session->kernel_session;
2968
2969 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
54d01ffb
DG
2970 if (kchan == NULL) {
2971 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2972 goto error;
d65106b1
DG
2973 }
2974
b0a40d28 2975 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
54d01ffb
DG
2976 if (ret != LTTCOMM_OK) {
2977 goto error;
2978 }
2979
2980 kernel_wait_quiescent(kernel_tracer_fd);
d65106b1 2981 break;
2bdd86d4
MD
2982 }
2983 case LTTNG_DOMAIN_UST:
b0a40d28 2984 {
b0a40d28 2985 struct ltt_ust_channel *uchan;
7f79d3a1 2986 struct ltt_ust_session *usess;
b0a40d28
DG
2987
2988 usess = session->ust_session;
2989
2990 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2991 channel_name);
2992 if (uchan == NULL) {
2993 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2994 goto error;
2995 }
2996
7f79d3a1
DG
2997 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
2998 if (ret != LTTCOMM_OK) {
b0a40d28
DG
2999 goto error;
3000 }
3001
7f79d3a1 3002 DBG3("Disable UST event %s in channel %s completed", event_name,
b0a40d28 3003 channel_name);
b0a40d28
DG
3004 break;
3005 }
d78d6610 3006#if 0
2bdd86d4
MD
3007 case LTTNG_DOMAIN_UST_EXEC_NAME:
3008 case LTTNG_DOMAIN_UST_PID:
3009 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3010#endif
54d01ffb 3011 default:
1ab1ea0b 3012 ret = LTTCOMM_UND;
54d01ffb 3013 goto error;
d65106b1 3014 }
26cc6b4e 3015
54d01ffb
DG
3016 ret = LTTCOMM_OK;
3017
3018error:
3019 return ret;
3020}
3021
3022/*
3023 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
3024 */
3025static int cmd_disable_event_all(struct ltt_session *session, int domain,
3026 char *channel_name)
3027{
3028 int ret;
54d01ffb
DG
3029
3030 switch (domain) {
3031 case LTTNG_DOMAIN_KERNEL:
9730260e
DG
3032 {
3033 struct ltt_kernel_session *ksess;
3034 struct ltt_kernel_channel *kchan;
3035
3036 ksess = session->kernel_session;
3037
3038 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
54d01ffb
DG
3039 if (kchan == NULL) {
3040 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
3041 goto error;
26cc6b4e
DG
3042 }
3043
9730260e 3044 ret = event_kernel_disable_all(ksess, kchan);
54d01ffb 3045 if (ret != LTTCOMM_OK) {
0b97ec54 3046 goto error;
26cc6b4e
DG
3047 }
3048
54d01ffb 3049 kernel_wait_quiescent(kernel_tracer_fd);
26cc6b4e 3050 break;
9730260e
DG
3051 }
3052 case LTTNG_DOMAIN_UST:
3053 {
3054 struct ltt_ust_session *usess;
3055 struct ltt_ust_channel *uchan;
3056
3057 usess = session->ust_session;
3058
3059 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3060 channel_name);
3061 if (uchan == NULL) {
3062 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
3063 goto error;
3064 }
3065
7f79d3a1
DG
3066 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
3067 if (ret != 0) {
9730260e
DG
3068 goto error;
3069 }
3070
7f79d3a1 3071 DBG3("Disable all UST events in channel %s completed", channel_name);
9730260e
DG
3072
3073 break;
3074 }
d78d6610 3075#if 0
9730260e
DG
3076 case LTTNG_DOMAIN_UST_EXEC_NAME:
3077 case LTTNG_DOMAIN_UST_PID:
3078 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3079#endif
54d01ffb 3080 default:
1ab1ea0b 3081 ret = LTTCOMM_UND;
54d01ffb 3082 goto error;
26cc6b4e 3083 }
e953ef25 3084
54d01ffb 3085 ret = LTTCOMM_OK;
e953ef25 3086
54d01ffb
DG
3087error:
3088 return ret;
3089}
f5177a38 3090
54d01ffb
DG
3091/*
3092 * Command LTTNG_ADD_CONTEXT processed by the client thread.
3093 */
3094static int cmd_add_context(struct ltt_session *session, int domain,
3095 char *channel_name, char *event_name, struct lttng_event_context *ctx)
3096{
3097 int ret;
f5177a38 3098
54d01ffb
DG
3099 switch (domain) {
3100 case LTTNG_DOMAIN_KERNEL:
3101 /* Add kernel context to kernel tracer */
3102 ret = context_kernel_add(session->kernel_session, ctx,
3103 event_name, channel_name);
3104 if (ret != LTTCOMM_OK) {
f5177a38 3105 goto error;
e953ef25 3106 }
2bdd86d4
MD
3107 break;
3108 case LTTNG_DOMAIN_UST:
3109 {
55cc08a6 3110 struct ltt_ust_session *usess = session->ust_session;
e953ef25 3111
55cc08a6
DG
3112 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
3113 if (ret != LTTCOMM_OK) {
3114 goto error;
2bdd86d4 3115 }
e953ef25 3116 break;
2bdd86d4 3117 }
d78d6610 3118#if 0
55cc08a6
DG
3119 case LTTNG_DOMAIN_UST_EXEC_NAME:
3120 case LTTNG_DOMAIN_UST_PID:
3121 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3122#endif
54d01ffb 3123 default:
1ab1ea0b 3124 ret = LTTCOMM_UND;
54d01ffb 3125 goto error;
e953ef25 3126 }
950131af 3127
54d01ffb 3128 ret = LTTCOMM_OK;
950131af 3129
54d01ffb
DG
3130error:
3131 return ret;
3132}
3133
53a80697
MD
3134/*
3135 * Command LTTNG_SET_FILTER processed by the client thread.
3136 */
3137static int cmd_set_filter(struct ltt_session *session, int domain,
3138 char *channel_name, char *event_name,
3139 struct lttng_filter_bytecode *bytecode)
3140{
3141 int ret;
3142
3143 switch (domain) {
3144 case LTTNG_DOMAIN_KERNEL:
3145 ret = LTTCOMM_FATAL;
3146 break;
3147 case LTTNG_DOMAIN_UST:
3148 {
3149 struct ltt_ust_session *usess = session->ust_session;
3150
3151 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
3152 if (ret != LTTCOMM_OK) {
3153 goto error;
3154 }
3155 break;
3156 }
3157#if 0
3158 case LTTNG_DOMAIN_UST_EXEC_NAME:
3159 case LTTNG_DOMAIN_UST_PID:
3160 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
3161#endif
3162 default:
3163 ret = LTTCOMM_UND;
3164 goto error;
3165 }
3166
3167 ret = LTTCOMM_OK;
3168
3169error:
3170 return ret;
3171
3172}
3173
54d01ffb
DG
3174/*
3175 * Command LTTNG_ENABLE_EVENT processed by the client thread.
3176 */
3177static int cmd_enable_event(struct ltt_session *session, int domain,
3178 char *channel_name, struct lttng_event *event)
3179{
3180 int ret;
f6cd6b0f 3181 struct lttng_channel *attr;
48842b30 3182 struct ltt_ust_session *usess = session->ust_session;
54d01ffb
DG
3183
3184 switch (domain) {
3185 case LTTNG_DOMAIN_KERNEL:
2bdd86d4
MD
3186 {
3187 struct ltt_kernel_channel *kchan;
3188
54d01ffb
DG
3189 kchan = trace_kernel_get_channel_by_name(channel_name,
3190 session->kernel_session);
3191 if (kchan == NULL) {
f6cd6b0f
DG
3192 attr = channel_new_default_attr(domain);
3193 if (attr == NULL) {
3194 ret = LTTCOMM_FATAL;
3195 goto error;
3196 }
3197 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3198
54d01ffb 3199 /* This call will notify the kernel thread */
44d3bd01 3200 ret = channel_kernel_create(session->kernel_session,
f6cd6b0f 3201 attr, kernel_poll_pipe[1]);
54d01ffb 3202 if (ret != LTTCOMM_OK) {
ff4d74e6 3203 free(attr);
f5177a38
DG
3204 goto error;
3205 }
ff4d74e6 3206 free(attr);
54d01ffb 3207 }
950131af 3208
54d01ffb
DG
3209 /* Get the newly created kernel channel pointer */
3210 kchan = trace_kernel_get_channel_by_name(channel_name,
3211 session->kernel_session);
3212 if (kchan == NULL) {
3213 /* This sould not happen... */
3214 ret = LTTCOMM_FATAL;
3215 goto error;
3216 }
f5177a38 3217
48842b30
DG
3218 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
3219 event);
54d01ffb 3220 if (ret != LTTCOMM_OK) {
f5177a38 3221 goto error;
950131af
DG
3222 }
3223
54d01ffb 3224 kernel_wait_quiescent(kernel_tracer_fd);
950131af 3225 break;
2bdd86d4
MD
3226 }
3227 case LTTNG_DOMAIN_UST:
3228 {
aea829b3 3229 struct lttng_channel *attr;
edb67388 3230 struct ltt_ust_channel *uchan;
2bdd86d4 3231
edb67388 3232 /* Get channel from global UST domain */
48842b30
DG
3233 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3234 channel_name);
3235 if (uchan == NULL) {
aea829b3
DG
3236 /* Create default channel */
3237 attr = channel_new_default_attr(domain);
3238 if (attr == NULL) {
3239 ret = LTTCOMM_FATAL;
3240 goto error;
3241 }
596219f7 3242 snprintf(attr->name, NAME_MAX, "%s", channel_name);
edb67388 3243 attr->name[NAME_MAX - 1] = '\0';
aea829b3 3244
7885e399 3245 ret = channel_ust_create(usess, domain, attr);
edb67388 3246 if (ret != LTTCOMM_OK) {
ff4d74e6 3247 free(attr);
aea829b3
DG
3248 goto error;
3249 }
aea829b3
DG
3250 free(attr);
3251
3252 /* Get the newly created channel reference back */
3253 uchan = trace_ust_find_channel_by_name(
3254 usess->domain_global.channels, channel_name);
3255 if (uchan == NULL) {
3256 /* Something is really wrong */
3257 ret = LTTCOMM_FATAL;
3258 goto error;
3259 }
48842b30 3260 }
2bdd86d4 3261
edb67388 3262 /* At this point, the session and channel exist on the tracer */
edb67388
DG
3263 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
3264 if (ret != LTTCOMM_OK) {
48842b30 3265 goto error;
2bdd86d4
MD
3266 }
3267 break;
3268 }
d78d6610 3269#if 0
2bdd86d4
MD
3270 case LTTNG_DOMAIN_UST_EXEC_NAME:
3271 case LTTNG_DOMAIN_UST_PID:
3272 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3273#endif
54d01ffb 3274 default:
1ab1ea0b 3275 ret = LTTCOMM_UND;
54d01ffb 3276 goto error;
950131af 3277 }
d36b8583 3278
54d01ffb 3279 ret = LTTCOMM_OK;
d36b8583 3280
54d01ffb
DG
3281error:
3282 return ret;
3283}
7d29a247 3284
54d01ffb
DG
3285/*
3286 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
3287 */
3288static int cmd_enable_event_all(struct ltt_session *session, int domain,
8c9ae521 3289 char *channel_name, int event_type)
54d01ffb
DG
3290{
3291 int ret;
3292 struct ltt_kernel_channel *kchan;
7d29a247 3293
54d01ffb
DG
3294 switch (domain) {
3295 case LTTNG_DOMAIN_KERNEL:
3296 kchan = trace_kernel_get_channel_by_name(channel_name,
3297 session->kernel_session);
3298 if (kchan == NULL) {
3299 /* This call will notify the kernel thread */
44d3bd01
DG
3300 ret = channel_kernel_create(session->kernel_session, NULL,
3301 kernel_poll_pipe[1]);
54d01ffb
DG
3302 if (ret != LTTCOMM_OK) {
3303 goto error;
d36b8583 3304 }
0d0c377a 3305
8f69e5eb
DG
3306 /* Get the newly created kernel channel pointer */
3307 kchan = trace_kernel_get_channel_by_name(channel_name,
3308 session->kernel_session);
3309 if (kchan == NULL) {
3310 /* This sould not happen... */
3311 ret = LTTCOMM_FATAL;
3312 goto error;
3313 }
3314
54d01ffb 3315 }
0fdd1e2c 3316
7a3d1328 3317 switch (event_type) {
76d45b40 3318 case LTTNG_EVENT_SYSCALL:
7a3d1328 3319 ret = event_kernel_enable_all_syscalls(session->kernel_session,
8c9ae521 3320 kchan, kernel_tracer_fd);
7a3d1328 3321 break;
76d45b40 3322 case LTTNG_EVENT_TRACEPOINT:
8c9ae521 3323 /*
7a3d1328
MD
3324 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3325 * events already registered to the channel.
8c9ae521 3326 */
7a3d1328
MD
3327 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
3328 kchan, kernel_tracer_fd);
3329 break;
76d45b40 3330 case LTTNG_EVENT_ALL:
7a3d1328 3331 /* Enable syscalls and tracepoints */
8c9ae521
DG
3332 ret = event_kernel_enable_all(session->kernel_session,
3333 kchan, kernel_tracer_fd);
7a3d1328
MD
3334 break;
3335 default:
3336 ret = LTTCOMM_KERN_ENABLE_FAIL;
3337 goto error;
8c9ae521 3338 }
8f69e5eb
DG
3339
3340 /* Manage return value */
54d01ffb 3341 if (ret != LTTCOMM_OK) {
0d0c377a 3342 goto error;
d36b8583
DG
3343 }
3344
54d01ffb 3345 kernel_wait_quiescent(kernel_tracer_fd);
d36b8583 3346 break;
76d45b40
DG
3347 case LTTNG_DOMAIN_UST:
3348 {
3349 struct lttng_channel *attr;
3350 struct ltt_ust_channel *uchan;
3351 struct ltt_ust_session *usess = session->ust_session;
3352
3353 /* Get channel from global UST domain */
3354 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3355 channel_name);
3356 if (uchan == NULL) {
3357 /* Create default channel */
3358 attr = channel_new_default_attr(domain);
3359 if (attr == NULL) {
3360 ret = LTTCOMM_FATAL;
3361 goto error;
3362 }
3363 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3364 attr->name[NAME_MAX - 1] = '\0';
3365
3366 /* Use the internal command enable channel */
7885e399 3367 ret = channel_ust_create(usess, domain, attr);
76d45b40
DG
3368 if (ret != LTTCOMM_OK) {
3369 free(attr);
3370 goto error;
3371 }
3372 free(attr);
3373
3374 /* Get the newly created channel reference back */
3375 uchan = trace_ust_find_channel_by_name(
3376 usess->domain_global.channels, channel_name);
3377 if (uchan == NULL) {
3378 /* Something is really wrong */
3379 ret = LTTCOMM_FATAL;
3380 goto error;
3381 }
3382 }
3383
3384 /* At this point, the session and channel exist on the tracer */
3385
3386 switch (event_type) {
3387 case LTTNG_EVENT_ALL:
3388 case LTTNG_EVENT_TRACEPOINT:
3389 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
3390 if (ret != LTTCOMM_OK) {
3391 goto error;
3392 }
3393 break;
3394 default:
3395 ret = LTTCOMM_UST_ENABLE_FAIL;
3396 goto error;
3397 }
3398
3399 /* Manage return value */
3400 if (ret != LTTCOMM_OK) {
3401 goto error;
3402 }
3403
3404 break;
3405 }
d78d6610 3406#if 0
76d45b40
DG
3407 case LTTNG_DOMAIN_UST_EXEC_NAME:
3408 case LTTNG_DOMAIN_UST_PID:
3409 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3410#endif
54d01ffb 3411 default:
1ab1ea0b 3412 ret = LTTCOMM_UND;
54d01ffb 3413 goto error;
d36b8583 3414 }
f3ed775e 3415
54d01ffb
DG
3416 ret = LTTCOMM_OK;
3417
3418error:
3419 return ret;
3420}
3421
3422/*
3423 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3424 */
3425static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
3426{
3427 int ret;
3428 ssize_t nb_events = 0;
3429
3430 switch (domain) {
3431 case LTTNG_DOMAIN_KERNEL:
3432 nb_events = kernel_list_events(kernel_tracer_fd, events);
3433 if (nb_events < 0) {
3434 ret = LTTCOMM_KERN_LIST_FAIL;
3435 goto error;
894be886 3436 }
54d01ffb 3437 break;
b551a063
DG
3438 case LTTNG_DOMAIN_UST:
3439 nb_events = ust_app_list_events(events);
3440 if (nb_events < 0) {
3441 ret = LTTCOMM_UST_LIST_FAIL;
3442 goto error;
3443 }
3444 break;
54d01ffb 3445 default:
1ab1ea0b 3446 ret = LTTCOMM_UND;
54d01ffb
DG
3447 goto error;
3448 }
894be886 3449
54d01ffb 3450 return nb_events;
7d29a247 3451
54d01ffb
DG
3452error:
3453 /* Return negative value to differentiate return code */
3454 return -ret;
3455}
b389abbe 3456
f37d259d
MD
3457/*
3458 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3459 */
3460static ssize_t cmd_list_tracepoint_fields(int domain,
3461 struct lttng_event_field **fields)
3462{
3463 int ret;
3464 ssize_t nb_fields = 0;
3465
3466 switch (domain) {
3467 case LTTNG_DOMAIN_UST:
3468 nb_fields = ust_app_list_event_fields(fields);
3469 if (nb_fields < 0) {
3470 ret = LTTCOMM_UST_LIST_FAIL;
3471 goto error;
3472 }
3473 break;
3474 case LTTNG_DOMAIN_KERNEL:
3475 default: /* fall-through */
3476 ret = LTTCOMM_UND;
3477 goto error;
3478 }
3479
3480 return nb_fields;
3481
3482error:
3483 /* Return negative value to differentiate return code */
3484 return -ret;
3485}
3486
54d01ffb
DG
3487/*
3488 * Command LTTNG_START_TRACE processed by the client thread.
3489 */
3490static int cmd_start_trace(struct ltt_session *session)
3491{
3492 int ret;
54d01ffb 3493 struct ltt_kernel_session *ksession;
b9d9b220 3494 struct ltt_ust_session *usess;
00e2e675 3495 struct ltt_kernel_channel *kchan;
b389abbe 3496
00e2e675 3497 /* Ease our life a bit ;) */
54d01ffb 3498 ksession = session->kernel_session;
b9d9b220 3499 usess = session->ust_session;
5eb91c98 3500
b9ef1c83 3501 if (session->enabled) {
80e327fa
DG
3502 /* Already started. */
3503 ret = LTTCOMM_TRACE_ALREADY_STARTED;
b9ef1c83
DG
3504 goto error;
3505 }
3506
464dd62d
MD
3507 session->enabled = 1;
3508
00e2e675
DG
3509 ret = setup_relayd(session);
3510 if (ret != LTTCOMM_OK) {
3511 ERR("Error setting up relayd for session %s", session->name);
3512 goto error;
3513 }
3514
54d01ffb
DG
3515 /* Kernel tracing */
3516 if (ksession != NULL) {
3517 /* Open kernel metadata */
3518 if (ksession->metadata == NULL) {
a4b92340 3519 ret = kernel_open_metadata(ksession);
54d01ffb
DG
3520 if (ret < 0) {
3521 ret = LTTCOMM_KERN_META_FAIL;
3522 goto error;
b389abbe 3523 }
54d01ffb 3524 }
7d29a247 3525
54d01ffb 3526 /* Open kernel metadata stream */
03550b58 3527 if (ksession->metadata_stream_fd < 0) {
54d01ffb
DG
3528 ret = kernel_open_metadata_stream(ksession);
3529 if (ret < 0) {
3530 ERR("Kernel create metadata stream failed");
3531 ret = LTTCOMM_KERN_STREAM_FAIL;
3532 goto error;
3533 }
3534 }
3535
3536 /* For each channel */
3537 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3538 if (kchan->stream_count == 0) {
3539 ret = kernel_open_channel_stream(kchan);
3540 if (ret < 0) {
3541 ret = LTTCOMM_KERN_STREAM_FAIL;
7d29a247
DG
3542 goto error;
3543 }
54d01ffb
DG
3544 /* Update the stream global counter */
3545 ksession->stream_count_global += ret;
7d29a247 3546 }
54d01ffb
DG
3547 }
3548
3549 /* Setup kernel consumer socket and send fds to it */
3550 ret = init_kernel_tracing(ksession);
3551 if (ret < 0) {
3552 ret = LTTCOMM_KERN_START_FAIL;
3553 goto error;
3554 }
3555
3556 /* This start the kernel tracing */
3557 ret = kernel_start_session(ksession);
3558 if (ret < 0) {
3559 ret = LTTCOMM_KERN_START_FAIL;
3560 goto error;
3561 }
3562
3563 /* Quiescent wait after starting trace */
3564 kernel_wait_quiescent(kernel_tracer_fd);
3565 }
3566
36dc12cc 3567 /* Flag session that trace should start automatically */
b9d9b220
DG
3568 if (usess) {
3569 usess->start_trace = 1;
36dc12cc 3570
b9d9b220
DG
3571 ret = ust_app_start_trace_all(usess);
3572 if (ret < 0) {
3573 ret = LTTCOMM_UST_START_FAIL;
3574 goto error;
3575 }
2bdd86d4 3576 }
54d01ffb
DG
3577
3578 ret = LTTCOMM_OK;
3579
3580error:
3581 return ret;
3582}
3583
3584/*
3585 * Command LTTNG_STOP_TRACE processed by the client thread.
3586 */
3587static int cmd_stop_trace(struct ltt_session *session)
3588{
3589 int ret;
3590 struct ltt_kernel_channel *kchan;
3591 struct ltt_kernel_session *ksession;
8be98f9a 3592 struct ltt_ust_session *usess;
54d01ffb
DG
3593
3594 /* Short cut */
3595 ksession = session->kernel_session;
8be98f9a 3596 usess = session->ust_session;
54d01ffb 3597
b9ef1c83 3598 if (!session->enabled) {
80e327fa 3599 ret = LTTCOMM_TRACE_ALREADY_STOPPED;
b9ef1c83
DG
3600 goto error;
3601 }
3602
464dd62d
MD
3603 session->enabled = 0;
3604
54d01ffb
DG
3605 /* Kernel tracer */
3606 if (ksession != NULL) {
3607 DBG("Stop kernel tracing");
3608
00e2e675
DG
3609 /* Flush metadata if exist */
3610 if (ksession->metadata_stream_fd >= 0) {
3611 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
3612 if (ret < 0) {
3613 ERR("Kernel metadata flush failed");
3614 }
54d01ffb 3615 }
f34daff7 3616
00e2e675 3617 /* Flush all buffers before stopping */
54d01ffb
DG
3618 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3619 ret = kernel_flush_buffer(kchan);
0d0c377a 3620 if (ret < 0) {
54d01ffb 3621 ERR("Kernel flush buffer error");
7d29a247 3622 }
54d01ffb 3623 }
19e70852 3624
54d01ffb
DG
3625 ret = kernel_stop_session(ksession);
3626 if (ret < 0) {
3627 ret = LTTCOMM_KERN_STOP_FAIL;
19e70852 3628 goto error;
f3ed775e 3629 }
54d01ffb
DG
3630
3631 kernel_wait_quiescent(kernel_tracer_fd);
894be886 3632 }
54d01ffb 3633
8be98f9a
MD
3634 if (usess) {
3635 usess->start_trace = 0;
2bdd86d4 3636
8be98f9a 3637 ret = ust_app_stop_trace_all(usess);
2bdd86d4 3638 if (ret < 0) {
190df5ac 3639 ret = LTTCOMM_UST_STOP_FAIL;
2bdd86d4
MD
3640 goto error;
3641 }
2bdd86d4 3642 }
54d01ffb
DG
3643
3644 ret = LTTCOMM_OK;
3645
3646error:
3647 return ret;
3648}
3649
3650/*
07424f16 3651 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
54d01ffb 3652 */
07424f16
DG
3653static int cmd_set_consumer_uri(int domain, struct ltt_session *session,
3654 size_t nb_uri, struct lttng_uri *uris)
54d01ffb 3655{
07424f16
DG
3656 int ret, i;
3657 struct ltt_kernel_session *ksess = session->kernel_session;
3658 struct ltt_ust_session *usess = session->ust_session;
a4b92340 3659 struct consumer_output *consumer = NULL;
a4b92340 3660
07424f16
DG
3661 assert(session);
3662 assert(uris);
3663 assert(nb_uri > 0);
00e2e675 3664
07424f16
DG
3665 /* Can't enable consumer after session started. */
3666 if (session->enabled) {
3667 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3668 goto error;
a4b92340
DG
3669 }
3670
07424f16
DG
3671 if (!session->start_consumer) {
3672 ret = LTTCOMM_NO_CONSUMER;
00e2e675
DG
3673 goto error;
3674 }
a4b92340
DG
3675
3676 /*
07424f16
DG
3677 * This case switch makes sure the domain session has a temporary consumer
3678 * so the URL can be set.
a4b92340 3679 */
07424f16
DG
3680 switch (domain) {
3681 case 0:
3682 /* Code flow error. A session MUST always have a consumer object */
3683 assert(session->consumer);
a4b92340 3684 /*
07424f16
DG
3685 * The URL will be added to the tracing session consumer instead of a
3686 * specific domain consumer.
a4b92340 3687 */
07424f16
DG
3688 consumer = session->consumer;
3689 break;
3690 case LTTNG_DOMAIN_KERNEL:
3691 /* Code flow error if we don't have a kernel session here. */
3692 assert(ksess);
a4b92340 3693
07424f16
DG
3694 /* Create consumer output if none exists */
3695 consumer = ksess->tmp_consumer;
3696 if (consumer == NULL) {
3697 consumer = consumer_copy_output(ksess->consumer);
3698 if (consumer == NULL) {
3699 ret = LTTCOMM_FATAL;
3700 goto error;
3701 }
3702 /* Trash the consumer subdir, we are about to set a new one. */
3703 memset(consumer->subdir, 0, sizeof(consumer->subdir));
3704 ksess->tmp_consumer = consumer;
3705 }
a4b92340 3706
07424f16
DG
3707 break;
3708 case LTTNG_DOMAIN_UST:
3709 /* Code flow error if we don't have a kernel session here. */
3710 assert(usess);
a4b92340 3711
07424f16
DG
3712 /* Create consumer output if none exists */
3713 consumer = usess->tmp_consumer;
3714 if (consumer == NULL) {
3715 consumer = consumer_copy_output(usess->consumer);
3716 if (consumer == NULL) {
3717 ret = LTTCOMM_FATAL;
3718 goto error;
3719 }
3720 /* Trash the consumer subdir, we are about to set a new one. */
3721 memset(consumer->subdir, 0, sizeof(consumer->subdir));
3722 usess->tmp_consumer = consumer;
3723 }
3724
3725 break;
a4b92340 3726 }
00e2e675 3727
07424f16
DG
3728 for (i = 0; i < nb_uri; i++) {
3729 struct consumer_socket *socket;
3730 struct lttng_ht_iter iter;
a4b92340 3731
07424f16 3732 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
00e2e675 3733 if (ret < 0) {
00e2e675
DG
3734 goto error;
3735 }
3736
07424f16
DG
3737 /*
3738 * Don't send relayd socket if URI is NOT remote or if the relayd
3739 * sockets for the session are already sent.
3740 */
3741 if (uris[i].dtype == LTTNG_DST_PATH ||
3742 consumer->dst.net.relayd_socks_sent) {
3743 continue;
00e2e675
DG
3744 }
3745
07424f16
DG
3746 /* Try to send relayd URI to the consumer if exist. */
3747 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter,
3748 socket, node.node) {
3749
3750 /* A socket in the HT should never have a negative fd */
3751 assert(socket->fd >= 0);
3752
3753 pthread_mutex_lock(socket->lock);
3754 ret = send_socket_relayd_consumer(domain, session, &uris[i],
3755 consumer, socket->fd);
3756 pthread_mutex_unlock(socket->lock);
3757 if (ret != LTTCOMM_OK) {
3758 goto error;
3759 }
3760 }
00e2e675
DG
3761 }
3762
07424f16
DG
3763 /* All good! */
3764 ret = LTTCOMM_OK;
3765
3766error:
3767 return ret;
3768}
3769
3770
3771/*
3772 * Command LTTNG_CREATE_SESSION processed by the client thread.
3773 */
3774static int cmd_create_session_uri(char *name, struct lttng_uri *uris,
3775 size_t nb_uri, lttng_sock_cred *creds)
3776{
3777 int ret;
3778 char *path = NULL;
3779 struct ltt_session *session;
07424f16
DG
3780
3781 assert(name);
3782
3783 /*
3784 * Verify if the session already exist
3785 *
3786 * XXX: There is no need for the session lock list here since the caller
3787 * (process_client_msg) is holding it. We might want to change that so a
3788 * single command does not lock the entire session list.
3789 */
3790 session = session_find_by_name(name);
3791 if (session != NULL) {
3792 ret = LTTCOMM_EXIST_SESS;
3793 goto find_error;
3794 }
54d01ffb 3795
a4b92340 3796 /* Create tracing session in the registry */
730389d9
DG
3797 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
3798 LTTNG_SOCK_GET_GID_CRED(creds));
54d01ffb 3799 if (ret != LTTCOMM_OK) {
07424f16 3800 goto session_error;
54d01ffb
DG
3801 }
3802
a4b92340
DG
3803 /*
3804 * Get the newly created session pointer back
3805 *
3806 * XXX: There is no need for the session lock list here since the caller
3807 * (process_client_msg) is holding it. We might want to change that so a
3808 * single command does not lock the entire session list.
3809 */
00e2e675
DG
3810 session = session_find_by_name(name);
3811 assert(session);
3812
07424f16
DG
3813 /* Create default consumer output for the session not yet created. */
3814 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
3815 if (session->consumer == NULL) {
3816 ret = LTTCOMM_FATAL;
3817 goto consumer_error;
3818 }
00e2e675 3819
07424f16
DG
3820 /*
3821 * This means that the lttng_create_session call was called with the _path_
3822 * argument set to NULL.
3823 */
3824 if (uris == NULL) {
3825 /*
3826 * At this point, we'll skip the consumer URI setup and create a
3827 * session with a NULL path which will flag the session to NOT spawn a
3828 * consumer.
3829 */
3830 DBG("Create session %s with NO uri, skipping consumer setup", name);
3831 goto end;
a4b92340 3832 }
07424f16
DG
3833
3834 session->start_consumer = 1;
3835
3836 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
3837 if (ret != LTTCOMM_OK) {
3838 goto consumer_error;
00e2e675 3839 }
54d01ffb 3840
07424f16
DG
3841 session->consumer->enabled = 1;
3842
3843end:
a4b92340
DG
3844 return LTTCOMM_OK;
3845
a4b92340 3846consumer_error:
07424f16
DG
3847 session_destroy(session);
3848session_error:
3849find_error:
54d01ffb
DG
3850 return ret;
3851}
3852
3853/*
3854 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3855 */
3856static int cmd_destroy_session(struct ltt_session *session, char *name)
3857{
3858 int ret;
3859
173af62f
DG
3860 /* Safety net */
3861 assert(session);
3862
54d01ffb
DG
3863 /* Clean kernel session teardown */
3864 teardown_kernel_session(session);
84cd17c6
MD
3865 /* UST session teardown */
3866 teardown_ust_session(session);
54d01ffb
DG
3867
3868 /*
3869 * Must notify the kernel thread here to update it's poll setin order
3870 * to remove the channel(s)' fd just destroyed.
3871 */
3872 ret = notify_thread_pipe(kernel_poll_pipe[1]);
3873 if (ret < 0) {
76d7553f 3874 PERROR("write kernel poll pipe");
54d01ffb
DG
3875 }
3876
271933a4 3877 ret = session_destroy(session);
54d01ffb
DG
3878
3879 return ret;
3880}
3881
3882/*
3883 * Command LTTNG_CALIBRATE processed by the client thread.
3884 */
3885static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
3886{
3887 int ret;
3888
3889 switch (domain) {
3890 case LTTNG_DOMAIN_KERNEL:
33a2b854 3891 {
54d01ffb 3892 struct lttng_kernel_calibrate kcalibrate;
33a2b854 3893
54d01ffb
DG
3894 kcalibrate.type = calibrate->type;
3895 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
33a2b854 3896 if (ret < 0) {
54d01ffb
DG
3897 ret = LTTCOMM_KERN_ENABLE_FAIL;
3898 goto error;
33a2b854 3899 }
54d01ffb
DG
3900 break;
3901 }
4466912f
DG
3902 case LTTNG_DOMAIN_UST:
3903 {
3904 struct lttng_ust_calibrate ucalibrate;
3905
3906 ucalibrate.type = calibrate->type;
3907 ret = ust_app_calibrate_glb(&ucalibrate);
3908 if (ret < 0) {
3909 ret = LTTCOMM_UST_CALIBRATE_FAIL;
3910 goto error;
3911 }
3912 break;
3913 }
54d01ffb 3914 default:
1ab1ea0b 3915 ret = LTTCOMM_UND;
54d01ffb
DG
3916 goto error;
3917 }
33a2b854 3918
54d01ffb 3919 ret = LTTCOMM_OK;
33a2b854 3920
54d01ffb
DG
3921error:
3922 return ret;
3923}
7d29a247 3924
54d01ffb
DG
3925/*
3926 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3927 */
3928static int cmd_register_consumer(struct ltt_session *session, int domain,
3929 char *sock_path)
3930{
3931 int ret, sock;
173af62f 3932 struct consumer_socket *socket;
b389abbe 3933
54d01ffb
DG
3934 switch (domain) {
3935 case LTTNG_DOMAIN_KERNEL:
3936 /* Can't register a consumer if there is already one */
48c4f28c 3937 if (session->kernel_session->consumer_fds_sent != 0) {
54d01ffb
DG
3938 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3939 goto error;
3940 }
3941
3942 sock = lttcomm_connect_unix_sock(sock_path);
3943 if (sock < 0) {
3944 ret = LTTCOMM_CONNECT_FAIL;
3945 goto error;
3946 }
3947
173af62f
DG
3948 socket = consumer_allocate_socket(sock);
3949 if (socket == NULL) {
3950 ret = LTTCOMM_FATAL;
3951 close(sock);
3952 goto error;
3953 }
3954
e8209f6b
DG
3955 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3956 if (socket->lock == NULL) {
3957 PERROR("zmalloc pthread mutex");
3958 ret = LTTCOMM_FATAL;
3959 goto error;
3960 }
3961 pthread_mutex_init(socket->lock, NULL);
3962
173af62f
DG
3963 rcu_read_lock();
3964 consumer_add_socket(socket, session->kernel_session->consumer);
3965 rcu_read_unlock();
3966
e8209f6b
DG
3967 pthread_mutex_lock(&kconsumer_data.pid_mutex);
3968 kconsumer_data.pid = -1;
3969 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
3970
54d01ffb
DG
3971 break;
3972 default:
3973 /* TODO: Userspace tracing */
1ab1ea0b 3974 ret = LTTCOMM_UND;
54d01ffb
DG
3975 goto error;
3976 }
3977
3978 ret = LTTCOMM_OK;
3979
3980error:
3981 return ret;
3982}
3983
3984/*
3985 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3986 */
3987static ssize_t cmd_list_domains(struct ltt_session *session,
3988 struct lttng_domain **domains)
3989{
b551a063 3990 int ret, index = 0;
54d01ffb
DG
3991 ssize_t nb_dom = 0;
3992
3993 if (session->kernel_session != NULL) {
b551a063 3994 DBG3("Listing domains found kernel domain");
54d01ffb
DG
3995 nb_dom++;
3996 }
3997
b551a063
DG
3998 if (session->ust_session != NULL) {
3999 DBG3("Listing domains found UST global domain");
4000 nb_dom++;
4001 }
54d01ffb 4002
b551a063 4003 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
54d01ffb
DG
4004 if (*domains == NULL) {
4005 ret = -LTTCOMM_FATAL;
4006 goto error;
4007 }
4008
b551a063
DG
4009 if (session->kernel_session != NULL) {
4010 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
4011 index++;
4012 }
4013
4014 if (session->ust_session != NULL) {
4015 (*domains)[index].type = LTTNG_DOMAIN_UST;
4016 index++;
4017 }
54d01ffb 4018
54d01ffb
DG
4019 return nb_dom;
4020
4021error:
4022 return ret;
4023}
4024
4025/*
4026 * Command LTTNG_LIST_CHANNELS processed by the client thread.
4027 */
b551a063 4028static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
54d01ffb
DG
4029 struct lttng_channel **channels)
4030{
4031 int ret;
4032 ssize_t nb_chan = 0;
4033
b551a063
DG
4034 switch (domain) {
4035 case LTTNG_DOMAIN_KERNEL:
4036 if (session->kernel_session != NULL) {
4037 nb_chan = session->kernel_session->channel_count;
4038 }
0df502fd 4039 DBG3("Number of kernel channels %zd", nb_chan);
b551a063
DG
4040 break;
4041 case LTTNG_DOMAIN_UST:
4042 if (session->ust_session != NULL) {
bec39940 4043 nb_chan = lttng_ht_get_count(
b551a063
DG
4044 session->ust_session->domain_global.channels);
4045 }
0df502fd 4046 DBG3("Number of UST global channels %zd", nb_chan);
b551a063
DG
4047 break;
4048 default:
4049 *channels = NULL;
1ab1ea0b 4050 ret = -LTTCOMM_UND;
54d01ffb
DG
4051 goto error;
4052 }
4053
b551a063
DG
4054 if (nb_chan > 0) {
4055 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
4056 if (*channels == NULL) {
4057 ret = -LTTCOMM_FATAL;
4058 goto error;
4059 }
54d01ffb 4060
b551a063
DG
4061 list_lttng_channels(domain, session, *channels);
4062 } else {
4063 *channels = NULL;
4064 }
2bdd86d4 4065
54d01ffb
DG
4066 return nb_chan;
4067
4068error:
4069 return ret;
4070}
4071
4072/*
4073 * Command LTTNG_LIST_EVENTS processed by the client thread.
4074 */
b551a063 4075static ssize_t cmd_list_events(int domain, struct ltt_session *session,
54d01ffb
DG
4076 char *channel_name, struct lttng_event **events)
4077{
b551a063 4078 int ret = 0;
54d01ffb 4079 ssize_t nb_event = 0;
54d01ffb 4080
b551a063
DG
4081 switch (domain) {
4082 case LTTNG_DOMAIN_KERNEL:
4083 if (session->kernel_session != NULL) {
4084 nb_event = list_lttng_kernel_events(channel_name,
4085 session->kernel_session, events);
54d01ffb 4086 }
b551a063
DG
4087 break;
4088 case LTTNG_DOMAIN_UST:
4089 {
4090 if (session->ust_session != NULL) {
4091 nb_event = list_lttng_ust_global_events(channel_name,
4092 &session->ust_session->domain_global, events);
4093 }
4094 break;
54d01ffb 4095 }
b551a063 4096 default:
1ab1ea0b 4097 ret = -LTTCOMM_UND;
54d01ffb
DG
4098 goto error;
4099 }
4100
b551a063 4101 ret = nb_event;
54d01ffb
DG
4102
4103error:
4104 return ret;
4105}
4106
00e2e675
DG
4107/*
4108 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
4109 */
4110static int cmd_disable_consumer(int domain, struct ltt_session *session)
4111{
4112 int ret;
4113 struct ltt_kernel_session *ksess = session->kernel_session;
4114 struct ltt_ust_session *usess = session->ust_session;
4115 struct consumer_output *consumer;
4116
a4b92340
DG
4117 assert(session);
4118
00e2e675
DG
4119 if (session->enabled) {
4120 /* Can't disable consumer on an already started session */
4121 ret = LTTCOMM_TRACE_ALREADY_STARTED;
4122 goto error;
4123 }
4124
a4b92340
DG
4125 if (!session->start_consumer) {
4126 ret = LTTCOMM_NO_CONSUMER;
4127 goto error;
4128 }
4129
00e2e675 4130 switch (domain) {
a4b92340
DG
4131 case 0:
4132 DBG("Disable tracing session %s consumer", session->name);
4133 consumer = session->consumer;
4134 break;
00e2e675
DG
4135 case LTTNG_DOMAIN_KERNEL:
4136 /* Code flow error if we don't have a kernel session here. */
4137 assert(ksess);
4138
4139 DBG("Disabling kernel consumer");
4140 consumer = ksess->consumer;
4141
4142 break;
4143 case LTTNG_DOMAIN_UST:
4144 /* Code flow error if we don't have a UST session here. */
4145 assert(usess);
4146
4147 DBG("Disabling UST consumer");
4148 consumer = usess->consumer;
4149
4150 break;
4151 default:
4152 ret = LTTCOMM_UNKNOWN_DOMAIN;
4153 goto error;
4154 }
4155
a4b92340
DG
4156 if (consumer) {
4157 consumer->enabled = 0;
4158 /* Success at this point */
4159 ret = LTTCOMM_OK;
4160 } else {
4161 ret = LTTCOMM_NO_CONSUMER;
4162 }
00e2e675
DG
4163
4164error:
4165 return ret;
4166}
4167
4168/*
4169 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
4170 */
4171static int cmd_enable_consumer(int domain, struct ltt_session *session)
4172{
4173 int ret;
4174 struct ltt_kernel_session *ksess = session->kernel_session;
4175 struct ltt_ust_session *usess = session->ust_session;
a4b92340
DG
4176 struct consumer_output *consumer = NULL;
4177
4178 assert(session);
00e2e675
DG
4179
4180 /* Can't enable consumer after session started. */
4181 if (session->enabled) {
4182 ret = LTTCOMM_TRACE_ALREADY_STARTED;
4183 goto error;
4184 }
4185
a4b92340
DG
4186 if (!session->start_consumer) {
4187 ret = LTTCOMM_NO_CONSUMER;
4188 goto error;
4189 }
4190
00e2e675 4191 switch (domain) {
a4b92340
DG
4192 case 0:
4193 assert(session->consumer);
4194 consumer = session->consumer;
4195 break;
00e2e675
DG
4196 case LTTNG_DOMAIN_KERNEL:
4197 /* Code flow error if we don't have a kernel session here. */
4198 assert(ksess);
4199
4200 /*
4201 * Check if we have already sent fds to the consumer. In that case,
4202 * the enable-consumer command can't be used because a start trace
4203 * had previously occured.
4204 */
4205 if (ksess->consumer_fds_sent) {
4206 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4207 goto error;
4208 }
4209
a4b92340
DG
4210 consumer = ksess->tmp_consumer;
4211 if (consumer == NULL) {
4212 ret = LTTCOMM_OK;
00e2e675
DG
4213 /* No temp. consumer output exists. Using the current one. */
4214 DBG3("No temporary consumer. Using default");
a4b92340 4215 consumer = ksess->consumer;
00e2e675
DG
4216 goto error;
4217 }
4218
a4b92340 4219 switch (consumer->type) {
00e2e675
DG
4220 case CONSUMER_DST_LOCAL:
4221 DBG2("Consumer output is local. Creating directory(ies)");
4222
4223 /* Create directory(ies) */
a4b92340 4224 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
00e2e675
DG
4225 S_IRWXU | S_IRWXG, session->uid, session->gid);
4226 if (ret < 0) {
4227 if (ret != -EEXIST) {
4228 ERR("Trace directory creation error");
4229 ret = LTTCOMM_FATAL;
4230 goto error;
4231 }
4232 }
4233 break;
4234 case CONSUMER_DST_NET:
4235 DBG2("Consumer output is network. Validating URIs");
4236 /* Validate if we have both control and data path set. */
a4b92340
DG
4237 if (!consumer->dst.net.control_isset) {
4238 ret = LTTCOMM_URL_CTRL_MISS;
00e2e675
DG
4239 goto error;
4240 }
4241
a4b92340
DG
4242 if (!consumer->dst.net.data_isset) {
4243 ret = LTTCOMM_URL_DATA_MISS;
00e2e675
DG
4244 goto error;
4245 }
4246
4247 /* Check established network session state */
4248 if (session->net_handle == 0) {
4249 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4250 ERR("Session network handle is not set on enable-consumer");
4251 goto error;
4252 }
4253
00e2e675
DG
4254 break;
4255 }
4256
07424f16
DG
4257 /* Append default kernel trace dir to subdir */
4258 strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
4259 sizeof(ksess->consumer->subdir));
4260
00e2e675
DG
4261 /*
4262 * @session-lock
4263 * This is race free for now since the session lock is acquired before
4264 * ending up in this function. No other threads can access this kernel
4265 * session without this lock hence freeing the consumer output object
4266 * is valid.
4267 */
3f8e211f 4268 rcu_read_lock();
00e2e675 4269 consumer_destroy_output(ksess->consumer);
3f8e211f 4270 rcu_read_unlock();
a4b92340 4271 ksess->consumer = consumer;
00e2e675
DG
4272 ksess->tmp_consumer = NULL;
4273
4274 break;
4275 case LTTNG_DOMAIN_UST:
4276 /* Code flow error if we don't have a UST session here. */
4277 assert(usess);
4278
4279 /*
4280 * Check if we have already sent fds to the consumer. In that case,
4281 * the enable-consumer command can't be used because a start trace
4282 * had previously occured.
4283 */
4284 if (usess->start_trace) {
4285 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4286 goto error;
4287 }
4288
a4b92340
DG
4289 consumer = usess->tmp_consumer;
4290 if (consumer == NULL) {
4291 ret = LTTCOMM_OK;
00e2e675
DG
4292 /* No temp. consumer output exists. Using the current one. */
4293 DBG3("No temporary consumer. Using default");
a4b92340 4294 consumer = usess->consumer;
00e2e675
DG
4295 goto error;
4296 }
4297
a4b92340 4298 switch (consumer->type) {
00e2e675
DG
4299 case CONSUMER_DST_LOCAL:
4300 DBG2("Consumer output is local. Creating directory(ies)");
4301
4302 /* Create directory(ies) */
a4b92340 4303 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
00e2e675
DG
4304 S_IRWXU | S_IRWXG, session->uid, session->gid);
4305 if (ret < 0) {
4306 if (ret != -EEXIST) {
4307 ERR("Trace directory creation error");
4308 ret = LTTCOMM_FATAL;
4309 goto error;
4310 }
4311 }
4312 break;
4313 case CONSUMER_DST_NET:
4314 DBG2("Consumer output is network. Validating URIs");
4315 /* Validate if we have both control and data path set. */
a4b92340
DG
4316 if (!consumer->dst.net.control_isset) {
4317 ret = LTTCOMM_URL_CTRL_MISS;
00e2e675
DG
4318 goto error;
4319 }
4320
a4b92340
DG
4321 if (!consumer->dst.net.data_isset) {
4322 ret = LTTCOMM_URL_DATA_MISS;
00e2e675
DG
4323 goto error;
4324 }
4325
4326 /* Check established network session state */
4327 if (session->net_handle == 0) {
4328 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4329 DBG2("Session network handle is not set on enable-consumer");
4330 goto error;
4331 }
4332
a4b92340 4333 if (consumer->net_seq_index == -1) {
00e2e675
DG
4334 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4335 DBG2("Network index is not set on the consumer");
4336 goto error;
4337 }
4338
00e2e675
DG
4339 break;
4340 }
4341
07424f16
DG
4342 /* Append default kernel trace dir to subdir */
4343 strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
4344 sizeof(usess->consumer->subdir));
4345
00e2e675
DG
4346 /*
4347 * @session-lock
4348 * This is race free for now since the session lock is acquired before
4349 * ending up in this function. No other threads can access this kernel
4350 * session without this lock hence freeing the consumer output object
4351 * is valid.
4352 */
3f8e211f 4353 rcu_read_lock();
00e2e675 4354 consumer_destroy_output(usess->consumer);
3f8e211f 4355 rcu_read_unlock();
a4b92340 4356 usess->consumer = consumer;
00e2e675
DG
4357 usess->tmp_consumer = NULL;
4358
4359 break;
4360 }
4361
a4b92340
DG
4362 /* Enable it */
4363 if (consumer) {
4364 consumer->enabled = 1;
4365 /* Success at this point */
4366 ret = LTTCOMM_OK;
4367 } else {
4368 /* Should not really happend... */
4369 ret = LTTCOMM_NO_CONSUMER;
4370 }
00e2e675
DG
4371
4372error:
4373 return ret;
4374}
4375
54d01ffb
DG
4376/*
4377 * Process the command requested by the lttng client within the command
4378 * context structure. This function make sure that the return structure (llm)
4379 * is set and ready for transmission before returning.
4380 *
4381 * Return any error encountered or 0 for success.
53a80697
MD
4382 *
4383 * "sock" is only used for special-case var. len data.
54d01ffb 4384 */
53a80697
MD
4385static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
4386 int *sock_error)
54d01ffb
DG
4387{
4388 int ret = LTTCOMM_OK;
44d3bd01 4389 int need_tracing_session = 1;
2e09ba09 4390 int need_domain;
54d01ffb
DG
4391
4392 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
4393
53a80697
MD
4394 *sock_error = 0;
4395
2e09ba09
MD
4396 switch (cmd_ctx->lsm->cmd_type) {
4397 case LTTNG_CREATE_SESSION:
4398 case LTTNG_DESTROY_SESSION:
4399 case LTTNG_LIST_SESSIONS:
4400 case LTTNG_LIST_DOMAINS:
4401 case LTTNG_START_TRACE:
4402 case LTTNG_STOP_TRACE:
4403 need_domain = 0;
3aace903 4404 break;
2e09ba09
MD
4405 default:
4406 need_domain = 1;
4407 }
4408
4409 if (opt_no_kernel && need_domain
4410 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9
MD
4411 if (!is_root) {
4412 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4413 } else {
4414 ret = LTTCOMM_KERN_NA;
4415 }
4fba7219
DG
4416 goto error;
4417 }
4418
8d3113b2
DG
4419 /* Deny register consumer if we already have a spawned consumer. */
4420 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
4421 pthread_mutex_lock(&kconsumer_data.pid_mutex);
4422 if (kconsumer_data.pid > 0) {
4423 ret = LTTCOMM_KERN_CONSUMER_FAIL;
4424 goto error;
4425 }
4426 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4427 }
4428
54d01ffb
DG
4429 /*
4430 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 4431 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
4432 * command.
4433 */
4434 switch(cmd_ctx->lsm->cmd_type) {
4435 case LTTNG_LIST_SESSIONS:
4436 case LTTNG_LIST_TRACEPOINTS:
f37d259d 4437 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
4438 case LTTNG_LIST_DOMAINS:
4439 case LTTNG_LIST_CHANNELS:
4440 case LTTNG_LIST_EVENTS:
4441 break;
4442 default:
4443 /* Setup lttng message with no payload */
4444 ret = setup_lttng_msg(cmd_ctx, 0);
4445 if (ret < 0) {
4446 /* This label does not try to unlock the session */
4447 goto init_setup_error;
4448 }
4449 }
4450
4451 /* Commands that DO NOT need a session. */
4452 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 4453 case LTTNG_CREATE_SESSION:
2e09ba09 4454 case LTTNG_CALIBRATE:
54d01ffb
DG
4455 case LTTNG_LIST_SESSIONS:
4456 case LTTNG_LIST_TRACEPOINTS:
f37d259d 4457 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 4458 need_tracing_session = 0;
54d01ffb
DG
4459 break;
4460 default:
4461 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
4462 /*
4463 * We keep the session list lock across _all_ commands
4464 * for now, because the per-session lock does not
4465 * handle teardown properly.
4466 */
74babd95 4467 session_lock_list();
54d01ffb
DG
4468 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
4469 if (cmd_ctx->session == NULL) {
4470 if (cmd_ctx->lsm->session.name != NULL) {
4471 ret = LTTCOMM_SESS_NOT_FOUND;
4472 } else {
4473 /* If no session name specified */
4474 ret = LTTCOMM_SELECT_SESS;
4475 }
4476 goto error;
4477 } else {
4478 /* Acquire lock for the session */
4479 session_lock(cmd_ctx->session);
4480 }
4481 break;
4482 }
b389abbe 4483
2e09ba09
MD
4484 if (!need_domain) {
4485 goto skip_domain;
4486 }
a4b92340 4487
54d01ffb
DG
4488 /*
4489 * Check domain type for specific "pre-action".
4490 */
4491 switch (cmd_ctx->lsm->domain.type) {
4492 case LTTNG_DOMAIN_KERNEL:
d1f1c568 4493 if (!is_root) {
531d29f9 4494 ret = LTTCOMM_NEED_ROOT_SESSIOND;
d1f1c568
DG
4495 goto error;
4496 }
4497
54d01ffb 4498 /* Kernel tracer check */
a4b35e07 4499 if (kernel_tracer_fd == -1) {
54d01ffb 4500 /* Basically, load kernel tracer modules */
096102bd
DG
4501 ret = init_kernel_tracer();
4502 if (ret != 0) {
54d01ffb
DG
4503 goto error;
4504 }
4505 }
5eb91c98 4506
5c827ce0
DG
4507 /* Consumer is in an ERROR state. Report back to client */
4508 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
4509 ret = LTTCOMM_NO_KERNCONSUMERD;
4510 goto error;
4511 }
4512
54d01ffb 4513 /* Need a session for kernel command */
44d3bd01 4514 if (need_tracing_session) {
54d01ffb 4515 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 4516 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 4517 if (ret < 0) {
54d01ffb 4518 ret = LTTCOMM_KERN_SESS_FAIL;
5eb91c98
DG
4519 goto error;
4520 }
b389abbe 4521 }
7d29a247 4522
54d01ffb 4523 /* Start the kernel consumer daemon */
3bd1e081
MD
4524 pthread_mutex_lock(&kconsumer_data.pid_mutex);
4525 if (kconsumer_data.pid == 0 &&
a4b92340
DG
4526 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
4527 cmd_ctx->session->start_consumer) {
3bd1e081
MD
4528 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4529 ret = start_consumerd(&kconsumer_data);
7d29a247 4530 if (ret < 0) {
54d01ffb
DG
4531 ret = LTTCOMM_KERN_CONSUMER_FAIL;
4532 goto error;
950131af 4533 }
5c827ce0 4534 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
4535 } else {
4536 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 4537 }
173af62f 4538
a4b92340
DG
4539 /*
4540 * The consumer was just spawned so we need to add the socket to
4541 * the consumer output of the session if exist.
4542 */
4543 ret = consumer_create_socket(&kconsumer_data,
4544 cmd_ctx->session->kernel_session->consumer);
4545 if (ret < 0) {
4546 goto error;
173af62f 4547 }
0d0c377a 4548 }
5c827ce0 4549
54d01ffb 4550 break;
2bdd86d4 4551 case LTTNG_DOMAIN_UST:
44d3bd01 4552 {
5c827ce0
DG
4553 /* Consumer is in an ERROR state. Report back to client */
4554 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
4555 ret = LTTCOMM_NO_USTCONSUMERD;
4556 goto error;
4557 }
4558
44d3bd01 4559 if (need_tracing_session) {
a4b92340 4560 /* Create UST session if none exist. */
f6a9efaa 4561 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 4562 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 4563 &cmd_ctx->lsm->domain);
44d3bd01
DG
4564 if (ret != LTTCOMM_OK) {
4565 goto error;
4566 }
4567 }
00e2e675 4568
7753dea8
MD
4569 /* Start the UST consumer daemons */
4570 /* 64-bit */
4571 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 4572 if (consumerd64_bin[0] != '\0' &&
7753dea8 4573 ustconsumer64_data.pid == 0 &&
a4b92340
DG
4574 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
4575 cmd_ctx->session->start_consumer) {
7753dea8
MD
4576 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4577 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 4578 if (ret < 0) {
7753dea8 4579 ret = LTTCOMM_UST_CONSUMER64_FAIL;
173af62f 4580 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
4581 goto error;
4582 }
48842b30 4583
173af62f 4584 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 4585 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 4586 } else {
7753dea8
MD
4587 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4588 }
173af62f
DG
4589
4590 /*
4591 * Setup socket for consumer 64 bit. No need for atomic access
4592 * since it was set above and can ONLY be set in this thread.
4593 */
a4b92340
DG
4594 ret = consumer_create_socket(&ustconsumer64_data,
4595 cmd_ctx->session->ust_session->consumer);
4596 if (ret < 0) {
4597 goto error;
173af62f
DG
4598 }
4599
7753dea8 4600 /* 32-bit */
fc7a59ce 4601 if (consumerd32_bin[0] != '\0' &&
7753dea8 4602 ustconsumer32_data.pid == 0 &&
a4b92340
DG
4603 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER &&
4604 cmd_ctx->session->start_consumer) {
7753dea8
MD
4605 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4606 ret = start_consumerd(&ustconsumer32_data);
4607 if (ret < 0) {
4608 ret = LTTCOMM_UST_CONSUMER32_FAIL;
173af62f 4609 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
4610 goto error;
4611 }
5c827ce0 4612
173af62f 4613 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 4614 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
4615 } else {
4616 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 4617 }
173af62f
DG
4618
4619 /*
4620 * Setup socket for consumer 64 bit. No need for atomic access
4621 * since it was set above and can ONLY be set in this thread.
4622 */
a4b92340
DG
4623 ret = consumer_create_socket(&ustconsumer32_data,
4624 cmd_ctx->session->ust_session->consumer);
4625 if (ret < 0) {
4626 goto error;
173af62f 4627 }
44d3bd01
DG
4628 }
4629 break;
48842b30 4630 }
54d01ffb 4631 default:
54d01ffb
DG
4632 break;
4633 }
2e09ba09 4634skip_domain:
33a2b854 4635
5c827ce0
DG
4636 /* Validate consumer daemon state when start/stop trace command */
4637 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
4638 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
4639 switch (cmd_ctx->lsm->domain.type) {
4640 case LTTNG_DOMAIN_UST:
4641 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
4642 ret = LTTCOMM_NO_USTCONSUMERD;
4643 goto error;
4644 }
4645 break;
4646 case LTTNG_DOMAIN_KERNEL:
4647 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
4648 ret = LTTCOMM_NO_KERNCONSUMERD;
4649 goto error;
4650 }
4651 break;
4652 }
4653 }
4654
8e0af1b4
MD
4655 /*
4656 * Check that the UID or GID match that of the tracing session.
4657 * The root user can interact with all sessions.
4658 */
4659 if (need_tracing_session) {
4660 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
4661 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4662 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
8e0af1b4
MD
4663 ret = LTTCOMM_EPERM;
4664 goto error;
4665 }
4666 }
4667
54d01ffb
DG
4668 /* Process by command type */
4669 switch (cmd_ctx->lsm->cmd_type) {
4670 case LTTNG_ADD_CONTEXT:
4671 {
4672 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4673 cmd_ctx->lsm->u.context.channel_name,
4674 cmd_ctx->lsm->u.context.event_name,
4675 &cmd_ctx->lsm->u.context.ctx);
4676 break;
4677 }
4678 case LTTNG_DISABLE_CHANNEL:
4679 {
4680 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4681 cmd_ctx->lsm->u.disable.channel_name);
4682 break;
4683 }
4684 case LTTNG_DISABLE_EVENT:
4685 {
4686 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4687 cmd_ctx->lsm->u.disable.channel_name,
4688 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
4689 break;
4690 }
54d01ffb
DG
4691 case LTTNG_DISABLE_ALL_EVENT:
4692 {
2bdd86d4 4693 DBG("Disabling all events");
54d01ffb
DG
4694
4695 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4696 cmd_ctx->lsm->u.disable.channel_name);
4697 break;
4698 }
00e2e675
DG
4699 case LTTNG_DISABLE_CONSUMER:
4700 {
4701 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4702 break;
4703 }
54d01ffb
DG
4704 case LTTNG_ENABLE_CHANNEL:
4705 {
aea829b3 4706 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
54d01ffb
DG
4707 &cmd_ctx->lsm->u.channel.chan);
4708 break;
4709 }
00e2e675
DG
4710 case LTTNG_ENABLE_CONSUMER:
4711 {
a4b92340
DG
4712 /*
4713 * XXX: 0 means that this URI should be applied on the session. Should
4714 * be a DOMAIN enuam.
4715 */
00e2e675 4716 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
a4b92340
DG
4717 if (ret != LTTCOMM_OK) {
4718 goto error;
4719 }
4720
4721 if (cmd_ctx->lsm->domain.type == 0) {
4722 /* Add the URI for the UST session if a consumer is present. */
4723 if (cmd_ctx->session->ust_session &&
4724 cmd_ctx->session->ust_session->consumer) {
4725 ret = cmd_enable_consumer(LTTNG_DOMAIN_UST, cmd_ctx->session);
4726 } else if (cmd_ctx->session->kernel_session &&
4727 cmd_ctx->session->kernel_session->consumer) {
4728 ret = cmd_enable_consumer(LTTNG_DOMAIN_KERNEL,
4729 cmd_ctx->session);
4730 }
4731 }
00e2e675
DG
4732 break;
4733 }
54d01ffb
DG
4734 case LTTNG_ENABLE_EVENT:
4735 {
4736 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4737 cmd_ctx->lsm->u.enable.channel_name,
4738 &cmd_ctx->lsm->u.enable.event);
4739 break;
4740 }
4741 case LTTNG_ENABLE_ALL_EVENT:
4742 {
2bdd86d4 4743 DBG("Enabling all events");
54d01ffb
DG
4744
4745 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
8c9ae521
DG
4746 cmd_ctx->lsm->u.enable.channel_name,
4747 cmd_ctx->lsm->u.enable.event.type);
54d01ffb
DG
4748 break;
4749 }
052da939 4750 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 4751 {
9f19cc17 4752 struct lttng_event *events;
54d01ffb 4753 ssize_t nb_events;
052da939 4754
54d01ffb
DG
4755 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
4756 if (nb_events < 0) {
4757 ret = -nb_events;
4758 goto error;
2ef84c95
DG
4759 }
4760
4761 /*
4762 * Setup lttng message with payload size set to the event list size in
4763 * bytes and then copy list into the llm payload.
4764 */
052da939 4765 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 4766 if (ret < 0) {
052da939 4767 free(events);
2ef84c95
DG
4768 goto setup_error;
4769 }
4770
4771 /* Copy event list into message payload */
9f19cc17 4772 memcpy(cmd_ctx->llm->payload, events,
052da939 4773 sizeof(struct lttng_event) * nb_events);
2ef84c95 4774
9f19cc17 4775 free(events);
2ef84c95
DG
4776
4777 ret = LTTCOMM_OK;
4778 break;
4779 }
f37d259d
MD
4780 case LTTNG_LIST_TRACEPOINT_FIELDS:
4781 {
4782 struct lttng_event_field *fields;
4783 ssize_t nb_fields;
4784
a4b92340
DG
4785 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
4786 &fields);
f37d259d
MD
4787 if (nb_fields < 0) {
4788 ret = -nb_fields;
4789 goto error;
4790 }
4791
4792 /*
4793 * Setup lttng message with payload size set to the event list size in
4794 * bytes and then copy list into the llm payload.
4795 */
a4b92340
DG
4796 ret = setup_lttng_msg(cmd_ctx,
4797 sizeof(struct lttng_event_field) * nb_fields);
f37d259d
MD
4798 if (ret < 0) {
4799 free(fields);
4800 goto setup_error;
4801 }
4802
4803 /* Copy event list into message payload */
4804 memcpy(cmd_ctx->llm->payload, fields,
4805 sizeof(struct lttng_event_field) * nb_fields);
4806
4807 free(fields);
4808
4809 ret = LTTCOMM_OK;
4810 break;
4811 }
00e2e675
DG
4812 case LTTNG_SET_CONSUMER_URI:
4813 {
a4b92340
DG
4814 size_t nb_uri, len;
4815 struct lttng_uri *uris;
4816
4817 nb_uri = cmd_ctx->lsm->u.uri.size;
4818 len = nb_uri * sizeof(struct lttng_uri);
4819
4820 if (nb_uri == 0) {
4821 ret = LTTCOMM_INVALID;
4822 goto error;
4823 }
4824
4825 uris = zmalloc(len);
4826 if (uris == NULL) {
4827 ret = LTTCOMM_FATAL;
4828 goto error;
4829 }
4830
4831 /* Receive variable len data */
77c7c900 4832 DBG("Receiving %zu URI(s) from client ...", nb_uri);
a4b92340
DG
4833 ret = lttcomm_recv_unix_sock(sock, uris, len);
4834 if (ret <= 0) {
4835 DBG("No URIs received from client... continuing");
4836 *sock_error = 1;
4837 ret = LTTCOMM_SESSION_FAIL;
4838 goto error;
4839 }
4840
00e2e675 4841 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
a4b92340
DG
4842 nb_uri, uris);
4843 if (ret != LTTCOMM_OK) {
4844 goto error;
4845 }
4846
4847 /*
4848 * XXX: 0 means that this URI should be applied on the session. Should
4849 * be a DOMAIN enuam.
4850 */
4851 if (cmd_ctx->lsm->domain.type == 0) {
4852 /* Add the URI for the UST session if a consumer is present. */
4853 if (cmd_ctx->session->ust_session &&
4854 cmd_ctx->session->ust_session->consumer) {
4855 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
4856 nb_uri, uris);
4857 } else if (cmd_ctx->session->kernel_session &&
4858 cmd_ctx->session->kernel_session->consumer) {
4859 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
4860 cmd_ctx->session, nb_uri, uris);
4861 }
4862 }
4863
00e2e675
DG
4864 break;
4865 }
f3ed775e 4866 case LTTNG_START_TRACE:
8c0faa1d 4867 {
54d01ffb 4868 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
4869 break;
4870 }
f3ed775e 4871 case LTTNG_STOP_TRACE:
8c0faa1d 4872 {
54d01ffb 4873 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
4874 break;
4875 }
5e16da05
MD
4876 case LTTNG_CREATE_SESSION:
4877 {
a4b92340
DG
4878 size_t nb_uri, len;
4879 struct lttng_uri *uris = NULL;
4880
4881 nb_uri = cmd_ctx->lsm->u.uri.size;
4882 len = nb_uri * sizeof(struct lttng_uri);
4883
4884 if (nb_uri > 0) {
4885 uris = zmalloc(len);
4886 if (uris == NULL) {
4887 ret = LTTCOMM_FATAL;
4888 goto error;
4889 }
4890
4891 /* Receive variable len data */
77c7c900 4892 DBG("Waiting for %zu URIs from client ...", nb_uri);
a4b92340
DG
4893 ret = lttcomm_recv_unix_sock(sock, uris, len);
4894 if (ret <= 0) {
4895 DBG("No URIs received from client... continuing");
4896 *sock_error = 1;
4897 ret = LTTCOMM_SESSION_FAIL;
4898 goto error;
4899 }
4900
4901 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
4902 DBG("Creating session with ONE network URI is a bad call");
4903 ret = LTTCOMM_SESSION_FAIL;
4904 goto error;
4905 }
4906 }
4907
4908 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
4909 &cmd_ctx->creds);
4910
00e2e675
DG
4911 break;
4912 }
5e16da05
MD
4913 case LTTNG_DESTROY_SESSION:
4914 {
54d01ffb
DG
4915 ret = cmd_destroy_session(cmd_ctx->session,
4916 cmd_ctx->lsm->session.name);
a4b92340
DG
4917
4918 /* Set session to NULL so we do not unlock it after free. */
256a5576 4919 cmd_ctx->session = NULL;
5461b305 4920 break;
5e16da05 4921 }
9f19cc17 4922 case LTTNG_LIST_DOMAINS:
5e16da05 4923 {
54d01ffb
DG
4924 ssize_t nb_dom;
4925 struct lttng_domain *domains;
5461b305 4926
54d01ffb
DG
4927 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
4928 if (nb_dom < 0) {
4929 ret = -nb_dom;
4930 goto error;
ce3d728c 4931 }
520ff687 4932
54d01ffb 4933 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305
DG
4934 if (ret < 0) {
4935 goto setup_error;
520ff687 4936 }
57167058 4937
54d01ffb
DG
4938 /* Copy event list into message payload */
4939 memcpy(cmd_ctx->llm->payload, domains,
4940 nb_dom * sizeof(struct lttng_domain));
4941
4942 free(domains);
5e16da05 4943
5461b305 4944 ret = LTTCOMM_OK;
5e16da05
MD
4945 break;
4946 }
9f19cc17 4947 case LTTNG_LIST_CHANNELS:
5e16da05 4948 {
6775595e 4949 int nb_chan;
54d01ffb
DG
4950 struct lttng_channel *channels;
4951
b551a063
DG
4952 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
4953 cmd_ctx->session, &channels);
54d01ffb
DG
4954 if (nb_chan < 0) {
4955 ret = -nb_chan;
4956 goto error;
5461b305 4957 }
ca95a216 4958
54d01ffb 4959 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305
DG
4960 if (ret < 0) {
4961 goto setup_error;
4962 }
9f19cc17 4963
54d01ffb
DG
4964 /* Copy event list into message payload */
4965 memcpy(cmd_ctx->llm->payload, channels,
4966 nb_chan * sizeof(struct lttng_channel));
4967
4968 free(channels);
9f19cc17
DG
4969
4970 ret = LTTCOMM_OK;
5461b305 4971 break;
5e16da05 4972 }
9f19cc17 4973 case LTTNG_LIST_EVENTS:
5e16da05 4974 {
b551a063 4975 ssize_t nb_event;
684d34d2 4976 struct lttng_event *events = NULL;
9f19cc17 4977
b551a063 4978 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
4979 cmd_ctx->lsm->u.list.channel_name, &events);
4980 if (nb_event < 0) {
4981 ret = -nb_event;
4982 goto error;
5461b305 4983 }
ca95a216 4984
54d01ffb 4985 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305
DG
4986 if (ret < 0) {
4987 goto setup_error;
4988 }
9f19cc17 4989
54d01ffb
DG
4990 /* Copy event list into message payload */
4991 memcpy(cmd_ctx->llm->payload, events,
4992 nb_event * sizeof(struct lttng_event));
9f19cc17 4993
54d01ffb 4994 free(events);
9f19cc17
DG
4995
4996 ret = LTTCOMM_OK;
5461b305 4997 break;
5e16da05
MD
4998 }
4999 case LTTNG_LIST_SESSIONS:
5000 {
8e0af1b4 5001 unsigned int nr_sessions;
5461b305 5002
8e0af1b4 5003 session_lock_list();
730389d9
DG
5004 nr_sessions = lttng_sessions_count(
5005 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
5006 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 5007
8e0af1b4 5008 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 5009 if (ret < 0) {
54d01ffb 5010 session_unlock_list();
5461b305 5011 goto setup_error;
e065084a 5012 }
5e16da05 5013
6c9cc2ab 5014 /* Filled the session array */
8e0af1b4 5015 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
5016 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
5017 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 5018
54d01ffb 5019 session_unlock_list();
5e16da05 5020
5461b305 5021 ret = LTTCOMM_OK;
5e16da05
MD
5022 break;
5023 }
d0254c7c
MD
5024 case LTTNG_CALIBRATE:
5025 {
54d01ffb
DG
5026 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
5027 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
5028 break;
5029 }
d9800920
DG
5030 case LTTNG_REGISTER_CONSUMER:
5031 {
54d01ffb
DG
5032 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
5033 cmd_ctx->lsm->u.reg.path);
d9800920
DG
5034 break;
5035 }
53a80697
MD
5036 case LTTNG_SET_FILTER:
5037 {
5038 struct lttng_filter_bytecode *bytecode;
5039
5040 if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) {
5041 ret = LTTCOMM_FILTER_INVAL;
5042 goto error;
5043 }
5044 bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len);
5045 if (!bytecode) {
5046 ret = LTTCOMM_FILTER_NOMEM;
5047 goto error;
5048 }
5049 /* Receive var. len. data */
5050 DBG("Receiving var len data from client ...");
5051 ret = lttcomm_recv_unix_sock(sock, bytecode,
5052 cmd_ctx->lsm->u.filter.bytecode_len);
5053 if (ret <= 0) {
5054 DBG("Nothing recv() from client var len data... continuing");
5055 *sock_error = 1;
5056 ret = LTTCOMM_FILTER_INVAL;
5057 goto error;
5058 }
5059
5060 if (bytecode->len + sizeof(*bytecode)
5061 != cmd_ctx->lsm->u.filter.bytecode_len) {
5062 free(bytecode);
5063 ret = LTTCOMM_FILTER_INVAL;
5064 goto error;
5065 }
5066
5067 ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type,
5068 cmd_ctx->lsm->u.filter.channel_name,
5069 cmd_ctx->lsm->u.filter.event_name,
5070 bytecode);
5071 break;
5072 }
5e16da05 5073 default:
5e16da05 5074 ret = LTTCOMM_UND;
5461b305 5075 break;
fac6795d
DG
5076 }
5077
5461b305 5078error:
5461b305
DG
5079 if (cmd_ctx->llm == NULL) {
5080 DBG("Missing llm structure. Allocating one.");
894be886 5081 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
5082 goto setup_error;
5083 }
5084 }
54d01ffb 5085 /* Set return code */
5461b305 5086 cmd_ctx->llm->ret_code = ret;
5461b305 5087setup_error:
b5541356 5088 if (cmd_ctx->session) {
54d01ffb 5089 session_unlock(cmd_ctx->session);
b5541356 5090 }
256a5576
MD
5091 if (need_tracing_session) {
5092 session_unlock_list();
5093 }
54d01ffb 5094init_setup_error:
8028d920 5095 return ret;
fac6795d
DG
5096}
5097
44a5e5eb
DG
5098/*
5099 * Thread managing health check socket.
5100 */
5101static void *thread_manage_health(void *data)
5102{
eb4a2943 5103 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
44a5e5eb
DG
5104 uint32_t revents, nb_fd;
5105 struct lttng_poll_event events;
5106 struct lttcomm_health_msg msg;
5107 struct lttcomm_health_data reply;
5108
5109 DBG("[thread] Manage health check started");
5110
5111 rcu_register_thread();
5112
5113 /* Create unix socket */
5114 sock = lttcomm_create_unix_sock(health_unix_sock_path);
5115 if (sock < 0) {
5116 ERR("Unable to create health check Unix socket");
5117 ret = -1;
5118 goto error;
5119 }
5120
5121 ret = lttcomm_listen_unix_sock(sock);
5122 if (ret < 0) {
5123 goto error;
5124 }
5125
5126 /*
5127 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5128 * more will be added to this poll set.
5129 */
5130 ret = create_thread_poll_set(&events, 2);
5131 if (ret < 0) {
5132 goto error;
5133 }
5134
5135 /* Add the application registration socket */
5136 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
5137 if (ret < 0) {
5138 goto error;
5139 }
5140
5141 while (1) {
5142 DBG("Health check ready");
5143
5144 nb_fd = LTTNG_POLL_GETNB(&events);
5145
5146 /* Inifinite blocking call, waiting for transmission */
5147restart:
5148 ret = lttng_poll_wait(&events, -1);
5149 if (ret < 0) {
5150 /*
5151 * Restart interrupted system call.
5152 */
5153 if (errno == EINTR) {
5154 goto restart;
5155 }
5156 goto error;
5157 }
5158
5159 for (i = 0; i < nb_fd; i++) {
5160 /* Fetch once the poll data */
5161 revents = LTTNG_POLL_GETEV(&events, i);
5162 pollfd = LTTNG_POLL_GETFD(&events, i);
5163
5164 /* Thread quit pipe has been closed. Killing thread. */
5165 ret = check_thread_quit_pipe(pollfd, revents);
5166 if (ret) {
139ac872
MD
5167 err = 0;
5168 goto exit;
44a5e5eb
DG
5169 }
5170
5171 /* Event on the registration socket */
5172 if (pollfd == sock) {
5173 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5174 ERR("Health socket poll error");
5175 goto error;
5176 }
5177 }
5178 }
5179
5180 new_sock = lttcomm_accept_unix_sock(sock);
5181 if (new_sock < 0) {
5182 goto error;
5183 }
5184
5185 DBG("Receiving data from client for health...");
5186 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
5187 if (ret <= 0) {
5188 DBG("Nothing recv() from client... continuing");
5189 ret = close(new_sock);
5190 if (ret) {
5191 PERROR("close");
5192 }
5193 new_sock = -1;
5194 continue;
5195 }
5196
5197 rcu_thread_online();
5198
5199 switch (msg.component) {
5200 case LTTNG_HEALTH_CMD:
5201 reply.ret_code = health_check_state(&health_thread_cmd);
5202 break;
139ac872
MD
5203 case LTTNG_HEALTH_APP_MANAGE:
5204 reply.ret_code = health_check_state(&health_thread_app_manage);
5205 break;
44a5e5eb
DG
5206 case LTTNG_HEALTH_APP_REG:
5207 reply.ret_code = health_check_state(&health_thread_app_reg);
5208 break;
5209 case LTTNG_HEALTH_KERNEL:
5210 reply.ret_code = health_check_state(&health_thread_kernel);
5211 break;
5212 case LTTNG_HEALTH_CONSUMER:
5213 reply.ret_code = check_consumer_health();
5214 break;
5215 case LTTNG_HEALTH_ALL:
44a5e5eb 5216 reply.ret_code =
139ac872
MD
5217 health_check_state(&health_thread_app_manage) &&
5218 health_check_state(&health_thread_app_reg) &&
5219 health_check_state(&health_thread_cmd) &&
5220 health_check_state(&health_thread_kernel) &&
5221 check_consumer_health();
44a5e5eb
DG
5222 break;
5223 default:
5224 reply.ret_code = LTTCOMM_UND;
5225 break;
5226 }
5227
5228 /*
5229 * Flip ret value since 0 is a success and 1 indicates a bad health for
5230 * the client where in the sessiond it is the opposite. Again, this is
5231 * just to make things easier for us poor developer which enjoy a lot
5232 * lazyness.
5233 */
5234 if (reply.ret_code == 0 || reply.ret_code == 1) {
5235 reply.ret_code = !reply.ret_code;
5236 }
5237
5238 DBG2("Health check return value %d", reply.ret_code);
5239
5240 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
5241 if (ret < 0) {
5242 ERR("Failed to send health data back to client");
5243 }
5244
5245 /* End of transmission */
5246 ret = close(new_sock);
5247 if (ret) {
5248 PERROR("close");
5249 }
5250 new_sock = -1;
5251 }
5252
139ac872 5253exit:
44a5e5eb 5254error:
139ac872
MD
5255 if (err) {
5256 ERR("Health error occurred in %s", __func__);
5257 }
44a5e5eb
DG
5258 DBG("Health check thread dying");
5259 unlink(health_unix_sock_path);
5260 if (sock >= 0) {
5261 ret = close(sock);
5262 if (ret) {
5263 PERROR("close");
5264 }
5265 }
5266 if (new_sock >= 0) {
5267 ret = close(new_sock);
5268 if (ret) {
5269 PERROR("close");
5270 }
5271 }
5272
5273 lttng_poll_clean(&events);
5274
5275 rcu_unregister_thread();
5276 return NULL;
5277}
5278
1d4b027a 5279/*
d063d709
DG
5280 * This thread manage all clients request using the unix client socket for
5281 * communication.
1d4b027a
DG
5282 */
5283static void *thread_manage_clients(void *data)
5284{
139ac872 5285 int sock = -1, ret, i, pollfd, err = -1;
53a80697 5286 int sock_error;
5eb91c98 5287 uint32_t revents, nb_fd;
273ea72c 5288 struct command_ctx *cmd_ctx = NULL;
5eb91c98 5289 struct lttng_poll_event events;
1d4b027a
DG
5290
5291 DBG("[thread] Manage client started");
5292
f6a9efaa
DG
5293 rcu_register_thread();
5294
44a5e5eb
DG
5295 health_code_update(&health_thread_cmd);
5296
1d4b027a
DG
5297 ret = lttcomm_listen_unix_sock(client_sock);
5298 if (ret < 0) {
5299 goto error;
5300 }
5301
5eb91c98
DG
5302 /*
5303 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5304 * more will be added to this poll set.
5305 */
5306 ret = create_thread_poll_set(&events, 2);
5307 if (ret < 0) {
5308 goto error;
5309 }
273ea72c 5310
5eb91c98
DG
5311 /* Add the application registration socket */
5312 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
5313 if (ret < 0) {
5314 goto error;
5315 }
273ea72c 5316
bbd973c2
DG
5317 /*
5318 * Notify parent pid that we are ready to accept command for client side.
1d4b027a
DG
5319 */
5320 if (opt_sig_parent) {
8db8d1dc 5321 kill(ppid, SIGUSR1);
1d4b027a
DG
5322 }
5323
44a5e5eb
DG
5324 health_code_update(&health_thread_cmd);
5325
1d4b027a 5326 while (1) {
1d4b027a 5327 DBG("Accepting client command ...");
273ea72c 5328
5eb91c98
DG
5329 nb_fd = LTTNG_POLL_GETNB(&events);
5330
273ea72c 5331 /* Inifinite blocking call, waiting for transmission */
88f2b785 5332 restart:
44a5e5eb 5333 health_poll_update(&health_thread_cmd);
5eb91c98 5334 ret = lttng_poll_wait(&events, -1);
44a5e5eb 5335 health_poll_update(&health_thread_cmd);
273ea72c 5336 if (ret < 0) {
88f2b785
MD
5337 /*
5338 * Restart interrupted system call.
5339 */
5340 if (errno == EINTR) {
5341 goto restart;
5342 }
273ea72c
DG
5343 goto error;
5344 }
5345
5eb91c98
DG
5346 for (i = 0; i < nb_fd; i++) {
5347 /* Fetch once the poll data */
5348 revents = LTTNG_POLL_GETEV(&events, i);
5349 pollfd = LTTNG_POLL_GETFD(&events, i);
5350
44a5e5eb
DG
5351 health_code_update(&health_thread_cmd);
5352
5eb91c98
DG
5353 /* Thread quit pipe has been closed. Killing thread. */
5354 ret = check_thread_quit_pipe(pollfd, revents);
5355 if (ret) {
139ac872
MD
5356 err = 0;
5357 goto exit;
5eb91c98
DG
5358 }
5359
5360 /* Event on the registration socket */
5361 if (pollfd == client_sock) {
5362 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5363 ERR("Client socket poll error");
5364 goto error;
5365 }
5366 }
5367 }
5368
5369 DBG("Wait for client response");
5370
44a5e5eb
DG
5371 health_code_update(&health_thread_cmd);
5372
5eb91c98
DG
5373 sock = lttcomm_accept_unix_sock(client_sock);
5374 if (sock < 0) {
273ea72c 5375 goto error;
273ea72c
DG
5376 }
5377
be040666
DG
5378 /* Set socket option for credentials retrieval */
5379 ret = lttcomm_setsockopt_creds_unix_sock(sock);
5380 if (ret < 0) {
5381 goto error;
5382 }
5383
5eb91c98 5384 /* Allocate context command to process the client request */
ba7f0ae5 5385 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 5386 if (cmd_ctx == NULL) {
76d7553f 5387 PERROR("zmalloc cmd_ctx");
1d4b027a 5388 goto error;
5eb91c98 5389 }
1d4b027a 5390
5eb91c98 5391 /* Allocate data buffer for reception */
ba7f0ae5 5392 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 5393 if (cmd_ctx->lsm == NULL) {
76d7553f 5394 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
5395 goto error;
5396 }
1d4b027a 5397
5eb91c98
DG
5398 cmd_ctx->llm = NULL;
5399 cmd_ctx->session = NULL;
1d4b027a 5400
44a5e5eb
DG
5401 health_code_update(&health_thread_cmd);
5402
5eb91c98
DG
5403 /*
5404 * Data is received from the lttng client. The struct
5405 * lttcomm_session_msg (lsm) contains the command and data request of
5406 * the client.
5407 */
5408 DBG("Receiving data from client ...");
be040666
DG
5409 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
5410 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
5411 if (ret <= 0) {
5412 DBG("Nothing recv() from client... continuing");
76d7553f
MD
5413 ret = close(sock);
5414 if (ret) {
5415 PERROR("close");
5416 }
5417 sock = -1;
a2c0da86 5418 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
5419 continue;
5420 }
1d4b027a 5421
44a5e5eb
DG
5422 health_code_update(&health_thread_cmd);
5423
5eb91c98
DG
5424 // TODO: Validate cmd_ctx including sanity check for
5425 // security purpose.
f7776ea7 5426
f6a9efaa 5427 rcu_thread_online();
5eb91c98
DG
5428 /*
5429 * This function dispatch the work to the kernel or userspace tracer
5430 * libs and fill the lttcomm_lttng_msg data structure of all the needed
5431 * informations for the client. The command context struct contains
5432 * everything this function may needs.
5433 */
53a80697 5434 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 5435 rcu_thread_offline();
5eb91c98 5436 if (ret < 0) {
53a80697
MD
5437 if (sock_error) {
5438 ret = close(sock);
5439 if (ret) {
5440 PERROR("close");
5441 }
5442 sock = -1;
5443 }
bbd973c2 5444 /*
5eb91c98 5445 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 5446 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
5447 * (ENOMEM). Error detected but still accept
5448 * command, unless a socket error has been
5449 * detected.
bbd973c2 5450 */
bbd973c2 5451 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
5452 continue;
5453 }
d6e4fca4 5454
44a5e5eb
DG
5455 health_code_update(&health_thread_cmd);
5456
54d01ffb
DG
5457 DBG("Sending response (size: %d, retcode: %s)",
5458 cmd_ctx->lttng_msg_size,
9a745bc7 5459 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 5460 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
5461 if (ret < 0) {
5462 ERR("Failed to send data back to client");
bbd973c2 5463 }
1d4b027a 5464
5eb91c98 5465 /* End of transmission */
76d7553f
MD
5466 ret = close(sock);
5467 if (ret) {
5468 PERROR("close");
5469 }
5470 sock = -1;
be040666
DG
5471
5472 clean_command_ctx(&cmd_ctx);
44a5e5eb
DG
5473
5474 health_code_update(&health_thread_cmd);
273ea72c
DG
5475 }
5476
139ac872 5477exit:
5eb91c98 5478error:
139ac872
MD
5479 if (err) {
5480 health_error(&health_thread_cmd);
5481 ERR("Health error occurred in %s", __func__);
5482 }
5483 health_exit(&health_thread_cmd);
44a5e5eb 5484
5eb91c98 5485 DBG("Client thread dying");
273ea72c 5486 unlink(client_unix_sock_path);
76d7553f
MD
5487 if (client_sock >= 0) {
5488 ret = close(client_sock);
5489 if (ret) {
5490 PERROR("close");
5491 }
a4b35e07
MD
5492 }
5493 if (sock >= 0) {
76d7553f
MD
5494 ret = close(sock);
5495 if (ret) {
5496 PERROR("close");
5497 }
a4b35e07 5498 }
273ea72c 5499
5eb91c98 5500 lttng_poll_clean(&events);
a2fb29a5 5501 clean_command_ctx(&cmd_ctx);
f6a9efaa
DG
5502
5503 rcu_unregister_thread();
1d4b027a
DG
5504 return NULL;
5505}
5506
5507
fac6795d
DG
5508/*
5509 * usage function on stderr
5510 */
5511static void usage(void)
5512{
b716ce68 5513 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
5514 fprintf(stderr, " -h, --help Display this usage.\n");
5515 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
5516 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5517 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5518 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
5519 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5520 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5521 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5522 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
5523 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5524 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5525 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5526 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150
DG
5527 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
5528 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5529 fprintf(stderr, " -V, --version Show version number.\n");
5530 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5531 fprintf(stderr, " -q, --quiet No output at all.\n");
5532 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3bd1e081 5533 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 5534 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
fac6795d
DG
5535}
5536
5537/*
5538 * daemon argument parsing
5539 */
5540static int parse_args(int argc, char **argv)
5541{
5542 int c;
5543
5544 static struct option long_options[] = {
5545 { "client-sock", 1, 0, 'c' },
5546 { "apps-sock", 1, 0, 'a' },
3bd1e081
MD
5547 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5548 { "kconsumerd-err-sock", 1, 0, 'E' },
7753dea8
MD
5549 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5550 { "ustconsumerd32-err-sock", 1, 0, 'H' },
ebaeda94
MD
5551 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5552 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5553 { "consumerd32-path", 1, 0, 'u' },
5554 { "consumerd32-libdir", 1, 0, 'U' },
5555 { "consumerd64-path", 1, 0, 't' },
5556 { "consumerd64-libdir", 1, 0, 'T' },
fac6795d 5557 { "daemonize", 0, 0, 'd' },
5b8719f5 5558 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
5559 { "help", 0, 0, 'h' },
5560 { "group", 1, 0, 'g' },
5561 { "version", 0, 0, 'V' },
75462a81 5562 { "quiet", 0, 0, 'q' },
3f9947db 5563 { "verbose", 0, 0, 'v' },
3bd1e081 5564 { "verbose-consumer", 0, 0, 'Z' },
4fba7219 5565 { "no-kernel", 0, 0, 'N' },
fac6795d
DG
5566 { NULL, 0, 0, 0 }
5567 };
5568
5569 while (1) {
5570 int option_index = 0;
4fba7219 5571 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
54d01ffb 5572 long_options, &option_index);
fac6795d
DG
5573 if (c == -1) {
5574 break;
5575 }
5576
5577 switch (c) {
5578 case 0:
5579 fprintf(stderr, "option %s", long_options[option_index].name);
5580 if (optarg) {
5581 fprintf(stderr, " with arg %s\n", optarg);
5582 }
5583 break;
b716ce68 5584 case 'c':
fac6795d
DG
5585 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
5586 break;
5587 case 'a':
5588 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
5589 break;
5590 case 'd':
5591 opt_daemon = 1;
5592 break;
5593 case 'g':
fb09408a 5594 opt_tracing_group = optarg;
fac6795d
DG
5595 break;
5596 case 'h':
5597 usage();
5598 exit(EXIT_FAILURE);
5599 case 'V':
5600 fprintf(stdout, "%s\n", VERSION);
5601 exit(EXIT_SUCCESS);
5b8719f5
DG
5602 case 'S':
5603 opt_sig_parent = 1;
5604 break;
d6f42150 5605 case 'E':
3bd1e081 5606 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150
DG
5607 break;
5608 case 'C':
3bd1e081
MD
5609 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5610 break;
5611 case 'F':
7753dea8 5612 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3bd1e081
MD
5613 break;
5614 case 'D':
7753dea8
MD
5615 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5616 break;
5617 case 'H':
5618 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5619 break;
5620 case 'G':
5621 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150 5622 break;
4fba7219
DG
5623 case 'N':
5624 opt_no_kernel = 1;
5625 break;
75462a81 5626 case 'q':
97e19046 5627 lttng_opt_quiet = 1;
75462a81 5628 break;
3f9947db 5629 case 'v':
53086306 5630 /* Verbose level can increase using multiple -v */
97e19046 5631 lttng_opt_verbose += 1;
3f9947db 5632 break;
31f73cc9 5633 case 'Z':
3bd1e081 5634 opt_verbose_consumer += 1;
31f73cc9 5635 break;
fb09408a 5636 case 'u':
fc7a59ce 5637 consumerd32_bin= optarg;
ebaeda94
MD
5638 break;
5639 case 'U':
5640 consumerd32_libdir = optarg;
7753dea8
MD
5641 break;
5642 case 't':
fc7a59ce 5643 consumerd64_bin = optarg;
ebaeda94
MD
5644 break;
5645 case 'T':
5646 consumerd64_libdir = optarg;
fb09408a 5647 break;
fac6795d
DG
5648 default:
5649 /* Unknown option or other error.
5650 * Error is printed by getopt, just return */
5651 return -1;
5652 }
5653 }
5654
5655 return 0;
5656}
5657
5658/*
d063d709 5659 * Creates the two needed socket by the daemon.
d6f42150
DG
5660 * apps_sock - The communication socket for all UST apps.
5661 * client_sock - The communication of the cli tool (lttng).
fac6795d 5662 */
cf3af59e 5663static int init_daemon_socket(void)
fac6795d
DG
5664{
5665 int ret = 0;
5666 mode_t old_umask;
5667
5668 old_umask = umask(0);
5669
5670 /* Create client tool unix socket */
d6f42150
DG
5671 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5672 if (client_sock < 0) {
5673 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
5674 ret = -1;
5675 goto end;
5676 }
5677
5678 /* File permission MUST be 660 */
5679 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5680 if (ret < 0) {
d6f42150 5681 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 5682 PERROR("chmod");
fac6795d
DG
5683 goto end;
5684 }
5685
5686 /* Create the application unix socket */
d6f42150
DG
5687 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5688 if (apps_sock < 0) {
5689 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
5690 ret = -1;
5691 goto end;
5692 }
5693
d6f42150 5694 /* File permission MUST be 666 */
54d01ffb
DG
5695 ret = chmod(apps_unix_sock_path,
5696 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 5697 if (ret < 0) {
d6f42150 5698 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 5699 PERROR("chmod");
fac6795d
DG
5700 goto end;
5701 }
5702
5703end:
5704 umask(old_umask);
5705 return ret;
5706}
5707
5708/*
54d01ffb
DG
5709 * Check if the global socket is available, and if a daemon is answering at the
5710 * other side. If yes, error is returned.
fac6795d 5711 */
cf3af59e 5712static int check_existing_daemon(void)
fac6795d 5713{
7d8234d9 5714 /* Is there anybody out there ? */
099e26bd 5715 if (lttng_session_daemon_alive()) {
7d8234d9 5716 return -EEXIST;
099e26bd 5717 }
b09c7c76
DG
5718
5719 return 0;
fac6795d
DG
5720}
5721
fac6795d 5722/*
d063d709 5723 * Set the tracing group gid onto the client socket.
5e16da05 5724 *
d063d709 5725 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 5726 * permissive (root.root) to less permissive (root.tracing).
fac6795d 5727 */
be040666 5728static int set_permissions(char *rundir)
fac6795d
DG
5729{
5730 int ret;
996b65c8 5731 gid_t gid;
fac6795d 5732
6775595e
DG
5733 ret = allowed_group();
5734 if (ret < 0) {
be040666
DG
5735 WARN("No tracing group detected");
5736 ret = 0;
fac6795d
DG
5737 goto end;
5738 }
5739
6775595e
DG
5740 gid = ret;
5741
d6f42150 5742 /* Set lttng run dir */
be040666 5743 ret = chown(rundir, 0, gid);
d6f42150 5744 if (ret < 0) {
be040666 5745 ERR("Unable to set group on %s", rundir);
76d7553f 5746 PERROR("chown");
d6f42150
DG
5747 }
5748
d1613cf5 5749 /* Ensure tracing group can search the run dir */
61076f74 5750 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
d1613cf5
JN
5751 if (ret < 0) {
5752 ERR("Unable to set permissions on %s", rundir);
76d7553f 5753 PERROR("chmod");
d1613cf5
JN
5754 }
5755
d6f42150 5756 /* lttng client socket path */
996b65c8 5757 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 5758 if (ret < 0) {
d6f42150 5759 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 5760 PERROR("chown");
d6f42150
DG
5761 }
5762
3bd1e081
MD
5763 /* kconsumer error socket path */
5764 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
d6f42150 5765 if (ret < 0) {
3bd1e081 5766 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 5767 PERROR("chown");
3bd1e081
MD
5768 }
5769
7753dea8
MD
5770 /* 64-bit ustconsumer error socket path */
5771 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
5772 if (ret < 0) {
5773 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 5774 PERROR("chown");
7753dea8
MD
5775 }
5776
5777 /* 32-bit ustconsumer compat32 error socket path */
5778 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3bd1e081 5779 if (ret < 0) {
7753dea8 5780 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5781 PERROR("chown");
fac6795d
DG
5782 }
5783
d6f42150 5784 DBG("All permissions are set");
e07ae692 5785
fac6795d
DG
5786end:
5787 return ret;
5788}
5789
d6f42150 5790/*
d063d709 5791 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5792 */
67e40797 5793static int create_lttng_rundir(const char *rundir)
d6f42150
DG
5794{
5795 int ret;
5796
67e40797
DG
5797 DBG3("Creating LTTng run directory: %s", rundir);
5798
d1613cf5 5799 ret = mkdir(rundir, S_IRWXU);
d6f42150 5800 if (ret < 0) {
b1f11e69 5801 if (errno != EEXIST) {
67e40797 5802 ERR("Unable to create %s", rundir);
b1f11e69
DG
5803 goto error;
5804 } else {
5805 ret = 0;
5806 }
d6f42150
DG
5807 }
5808
5809error:
5810 return ret;
5811}
5812
5813/*
d063d709
DG
5814 * Setup sockets and directory needed by the kconsumerd communication with the
5815 * session daemon.
d6f42150 5816 */
67e40797
DG
5817static int set_consumer_sockets(struct consumer_data *consumer_data,
5818 const char *rundir)
d6f42150
DG
5819{
5820 int ret;
67e40797 5821 char path[PATH_MAX];
d6f42150 5822
67e40797 5823 switch (consumer_data->type) {
7753dea8 5824 case LTTNG_CONSUMER_KERNEL:
60922cb0 5825 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
5826 break;
5827 case LTTNG_CONSUMER64_UST:
60922cb0 5828 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
5829 break;
5830 case LTTNG_CONSUMER32_UST:
60922cb0 5831 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
5832 break;
5833 default:
5834 ERR("Consumer type unknown");
5835 ret = -EINVAL;
5836 goto error;
d6f42150
DG
5837 }
5838
67e40797
DG
5839 DBG2("Creating consumer directory: %s", path);
5840
d1613cf5 5841 ret = mkdir(path, S_IRWXU);
d6f42150 5842 if (ret < 0) {
6beb2242 5843 if (errno != EEXIST) {
f11e84c2 5844 PERROR("mkdir");
3bd1e081 5845 ERR("Failed to create %s", path);
6beb2242
DG
5846 goto error;
5847 }
f11e84c2 5848 ret = -1;
d6f42150
DG
5849 }
5850
5851 /* Create the kconsumerd error unix socket */
3bd1e081
MD
5852 consumer_data->err_sock =
5853 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5854 if (consumer_data->err_sock < 0) {
5855 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5856 ret = -1;
5857 goto error;
5858 }
5859
5860 /* File permission MUST be 660 */
3bd1e081 5861 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5862 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5863 if (ret < 0) {
3bd1e081 5864 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5865 PERROR("chmod");
d6f42150
DG
5866 goto error;
5867 }
5868
5869error:
5870 return ret;
5871}
5872
fac6795d 5873/*
d063d709 5874 * Signal handler for the daemon
cf3af59e 5875 *
54d01ffb
DG
5876 * Simply stop all worker threads, leaving main() return gracefully after
5877 * joining all threads and calling cleanup().
fac6795d
DG
5878 */
5879static void sighandler(int sig)
5880{
5881 switch (sig) {
cf3af59e 5882 case SIGPIPE:
af87c45a 5883 DBG("SIGPIPE caught");
cf3af59e
MD
5884 return;
5885 case SIGINT:
af87c45a 5886 DBG("SIGINT caught");
cf3af59e
MD
5887 stop_threads();
5888 break;
5889 case SIGTERM:
af87c45a 5890 DBG("SIGTERM caught");
cf3af59e
MD
5891 stop_threads();
5892 break;
5893 default:
5894 break;
fac6795d 5895 }
fac6795d
DG
5896}
5897
5898/*
d063d709 5899 * Setup signal handler for :
1d4b027a 5900 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5901 */
1d4b027a 5902static int set_signal_handler(void)
fac6795d 5903{
1d4b027a
DG
5904 int ret = 0;
5905 struct sigaction sa;
5906 sigset_t sigset;
fac6795d 5907
1d4b027a 5908 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5909 PERROR("sigemptyset");
1d4b027a
DG
5910 return ret;
5911 }
d6f42150 5912
1d4b027a
DG
5913 sa.sa_handler = sighandler;
5914 sa.sa_mask = sigset;
5915 sa.sa_flags = 0;
5916 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5917 PERROR("sigaction");
1d4b027a 5918 return ret;
d6f42150
DG
5919 }
5920
1d4b027a 5921 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5922 PERROR("sigaction");
1d4b027a 5923 return ret;
d6f42150 5924 }
aaf26714 5925
1d4b027a 5926 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 5927 PERROR("sigaction");
1d4b027a 5928 return ret;
8c0faa1d
DG
5929 }
5930
1d4b027a
DG
5931 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5932
5933 return ret;
fac6795d
DG
5934}
5935
f3ed775e 5936/*
d063d709
DG
5937 * Set open files limit to unlimited. This daemon can open a large number of
5938 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
5939 */
5940static void set_ulimit(void)
5941{
5942 int ret;
5943 struct rlimit lim;
5944
a88df331 5945 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
5946 lim.rlim_cur = 65535;
5947 lim.rlim_max = 65535;
5948
5949 ret = setrlimit(RLIMIT_NOFILE, &lim);
5950 if (ret < 0) {
76d7553f 5951 PERROR("failed to set open files limit");
f3ed775e
DG
5952 }
5953}
5954
fac6795d
DG
5955/*
5956 * main
5957 */
5958int main(int argc, char **argv)
5959{
fac6795d
DG
5960 int ret = 0;
5961 void *status;
b082db07 5962 const char *home_path;
fac6795d 5963
335a95b7
MD
5964 init_kernel_workarounds();
5965
f6a9efaa
DG
5966 rcu_register_thread();
5967
7753dea8 5968 setup_consumerd_path();
fb09408a 5969
fac6795d
DG
5970 /* Parse arguments */
5971 progname = argv[0];
5972 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 5973 goto error;
fac6795d
DG
5974 }
5975
5976 /* Daemonize */
5977 if (opt_daemon) {
ceed52b5
MD
5978 int i;
5979
5980 /*
5981 * fork
5982 * child: setsid, close FD 0, 1, 2, chdir /
5983 * parent: exit (if fork is successful)
5984 */
53094c05
DG
5985 ret = daemon(0, 0);
5986 if (ret < 0) {
76d7553f 5987 PERROR("daemon");
cf3af59e 5988 goto error;
53094c05 5989 }
ceed52b5
MD
5990 /*
5991 * We are in the child. Make sure all other file
5992 * descriptors are closed, in case we are called with
5993 * more opened file descriptors than the standard ones.
5994 */
5995 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5996 (void) close(i);
5997 }
5998 }
5999
6000 /* Create thread quit pipe */
6001 if ((ret = init_thread_quit_pipe()) < 0) {
6002 goto error;
fac6795d
DG
6003 }
6004
6005 /* Check if daemon is UID = 0 */
6006 is_root = !getuid();
6007
fac6795d 6008 if (is_root) {
990570ed 6009 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
67e40797
DG
6010
6011 /* Create global run dir with root access */
6012 ret = create_lttng_rundir(rundir);
d6f42150 6013 if (ret < 0) {
cf3af59e 6014 goto error;
d6f42150
DG
6015 }
6016
fac6795d 6017 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
6018 snprintf(apps_unix_sock_path, PATH_MAX,
6019 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
6020 }
6021
6022 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
6023 snprintf(client_unix_sock_path, PATH_MAX,
6024 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
6025 }
0fdd1e2c
DG
6026
6027 /* Set global SHM for ust */
6028 if (strlen(wait_shm_path) == 0) {
6029 snprintf(wait_shm_path, PATH_MAX,
6030 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
6031 }
67e40797 6032
44a5e5eb
DG
6033 if (strlen(health_unix_sock_path) == 0) {
6034 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
6035 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
6036 }
6037
67e40797
DG
6038 /* Setup kernel consumerd path */
6039 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 6040 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
67e40797 6041 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 6042 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
67e40797
DG
6043
6044 DBG2("Kernel consumer err path: %s",
6045 kconsumer_data.err_unix_sock_path);
6046 DBG2("Kernel consumer cmd path: %s",
6047 kconsumer_data.cmd_unix_sock_path);
fac6795d 6048 } else {
b082db07
DG
6049 home_path = get_home_dir();
6050 if (home_path == NULL) {
273ea72c
DG
6051 /* TODO: Add --socket PATH option */
6052 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
6053 ret = -EPERM;
6054 goto error;
b082db07
DG
6055 }
6056
67e40797
DG
6057 /*
6058 * Create rundir from home path. This will create something like
6059 * $HOME/.lttng
6060 */
990570ed 6061 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797
DG
6062 if (ret < 0) {
6063 ret = -ENOMEM;
6064 goto error;
6065 }
6066
6067 ret = create_lttng_rundir(rundir);
6068 if (ret < 0) {
6069 goto error;
6070 }
6071
fac6795d 6072 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 6073 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 6074 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
6075 }
6076
6077 /* Set the cli tool unix socket path */
6078 if (strlen(client_unix_sock_path) == 0) {
d6f42150 6079 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 6080 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 6081 }
0fdd1e2c
DG
6082
6083 /* Set global SHM for ust */
6084 if (strlen(wait_shm_path) == 0) {
6085 snprintf(wait_shm_path, PATH_MAX,
6086 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
6087 }
44a5e5eb
DG
6088
6089 /* Set health check Unix path */
6090 if (strlen(health_unix_sock_path) == 0) {
6091 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
6092 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
6093 }
fac6795d
DG
6094 }
6095
5c827ce0
DG
6096 /* Set consumer initial state */
6097 kernel_consumerd_state = CONSUMER_STOPPED;
6098 ust_consumerd_state = CONSUMER_STOPPED;
6099
847177cd
DG
6100 DBG("Client socket path %s", client_unix_sock_path);
6101 DBG("Application socket path %s", apps_unix_sock_path);
67e40797
DG
6102 DBG("LTTng run directory path: %s", rundir);
6103
6104 /* 32 bits consumerd path setup */
6105 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 6106 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
67e40797 6107 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 6108 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
67e40797
DG
6109
6110 DBG2("UST consumer 32 bits err path: %s",
6111 ustconsumer32_data.err_unix_sock_path);
6112 DBG2("UST consumer 32 bits cmd path: %s",
6113 ustconsumer32_data.cmd_unix_sock_path);
6114
6115 /* 64 bits consumerd path setup */
6116 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 6117 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
67e40797 6118 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 6119 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
67e40797
DG
6120
6121 DBG2("UST consumer 64 bits err path: %s",
6122 ustconsumer64_data.err_unix_sock_path);
6123 DBG2("UST consumer 64 bits cmd path: %s",
6124 ustconsumer64_data.cmd_unix_sock_path);
847177cd 6125
273ea72c 6126 /*
7d8234d9 6127 * See if daemon already exist.
fac6795d 6128 */
7d8234d9 6129 if ((ret = check_existing_daemon()) < 0) {
75462a81 6130 ERR("Already running daemon.\n");
273ea72c 6131 /*
cf3af59e
MD
6132 * We do not goto exit because we must not cleanup()
6133 * because a daemon is already running.
ab118b20 6134 */
cf3af59e 6135 goto error;
a88df331
DG
6136 }
6137
1427f9b2
DG
6138 /*
6139 * Init UST app hash table. Alloc hash table before this point since
6140 * cleanup() can get called after that point.
6141 */
6142 ust_app_ht_alloc();
6143
54d01ffb 6144 /* After this point, we can safely call cleanup() with "goto exit" */
a88df331
DG
6145
6146 /*
6147 * These actions must be executed as root. We do that *after* setting up
6148 * the sockets path because we MUST make the check for another daemon using
6149 * those paths *before* trying to set the kernel consumer sockets and init
6150 * kernel tracer.
6151 */
6152 if (is_root) {
67e40797 6153 ret = set_consumer_sockets(&kconsumer_data, rundir);
7753dea8
MD
6154 if (ret < 0) {
6155 goto exit;
6156 }
6157
a88df331 6158 /* Setup kernel tracer */
4fba7219
DG
6159 if (!opt_no_kernel) {
6160 init_kernel_tracer();
6161 }
a88df331
DG
6162
6163 /* Set ulimit for open files */
6164 set_ulimit();
fac6795d 6165 }
4063050c
MD
6166 /* init lttng_fd tracking must be done after set_ulimit. */
6167 lttng_fd_init();
fac6795d 6168
67e40797
DG
6169 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
6170 if (ret < 0) {
6171 goto exit;
6172 }
6173
6174 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
6175 if (ret < 0) {
6176 goto exit;
6177 }
6178
cf3af59e
MD
6179 if ((ret = set_signal_handler()) < 0) {
6180 goto exit;
fac6795d
DG
6181 }
6182
d6f42150 6183 /* Setup the needed unix socket */
cf3af59e
MD
6184 if ((ret = init_daemon_socket()) < 0) {
6185 goto exit;
fac6795d
DG
6186 }
6187
6188 /* Set credentials to socket */
be040666 6189 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
cf3af59e 6190 goto exit;
fac6795d
DG
6191 }
6192
5b8719f5
DG
6193 /* Get parent pid if -S, --sig-parent is specified. */
6194 if (opt_sig_parent) {
6195 ppid = getppid();
6196 }
6197
7a485870 6198 /* Setup the kernel pipe for waking up the kernel thread */
ef599319 6199 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
cf3af59e 6200 goto exit;
7a485870
DG
6201 }
6202
099e26bd 6203 /* Setup the thread apps communication pipe. */
ef599319 6204 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
099e26bd
DG
6205 goto exit;
6206 }
6207
6208 /* Init UST command queue. */
6209 cds_wfq_init(&ust_cmd_queue.queue);
6210
273ea72c 6211 /*
54d01ffb
DG
6212 * Get session list pointer. This pointer MUST NOT be free(). This list is
6213 * statically declared in session.c
273ea72c 6214 */
54d01ffb 6215 session_list_ptr = session_get_list();
b5541356 6216
5eb91c98
DG
6217 /* Set up max poll set size */
6218 lttng_poll_set_max_size();
6219
00e2e675
DG
6220 /*
6221 * Set network sequence index to 1 for streams to match a relayd socket on
6222 * the consumer side.
6223 */
6224 uatomic_set(&relayd_net_seq_idx, 1);
6225
44a5e5eb
DG
6226 /* Init all health thread counters. */
6227 health_init(&health_thread_cmd);
6228 health_init(&health_thread_kernel);
139ac872 6229 health_init(&health_thread_app_manage);
44a5e5eb
DG
6230 health_init(&health_thread_app_reg);
6231
6232 /*
6233 * Init health counters of the consumer thread. We do a quick hack here to
6234 * the state of the consumer health is fine even if the thread is not
6235 * started. This is simply to ease our life and has no cost what so ever.
6236 */
6237 health_init(&kconsumer_data.health);
6238 health_poll_update(&kconsumer_data.health);
6239 health_init(&ustconsumer32_data.health);
6240 health_poll_update(&ustconsumer32_data.health);
6241 health_init(&ustconsumer64_data.health);
6242 health_poll_update(&ustconsumer64_data.health);
6243
6244 /* Create thread to manage the client socket */
6245 ret = pthread_create(&health_thread, NULL,
6246 thread_manage_health, (void *) NULL);
6247 if (ret != 0) {
6248 PERROR("pthread_create health");
6249 goto exit_health;
6250 }
6251
cf3af59e 6252 /* Create thread to manage the client socket */
099e26bd
DG
6253 ret = pthread_create(&client_thread, NULL,
6254 thread_manage_clients, (void *) NULL);
cf3af59e 6255 if (ret != 0) {
76d7553f 6256 PERROR("pthread_create clients");
cf3af59e
MD
6257 goto exit_client;
6258 }
fac6795d 6259
099e26bd
DG
6260 /* Create thread to dispatch registration */
6261 ret = pthread_create(&dispatch_thread, NULL,
6262 thread_dispatch_ust_registration, (void *) NULL);
6263 if (ret != 0) {
76d7553f 6264 PERROR("pthread_create dispatch");
099e26bd
DG
6265 goto exit_dispatch;
6266 }
6267
6268 /* Create thread to manage application registration. */
6269 ret = pthread_create(&reg_apps_thread, NULL,
6270 thread_registration_apps, (void *) NULL);
6271 if (ret != 0) {
76d7553f 6272 PERROR("pthread_create registration");
099e26bd
DG
6273 goto exit_reg_apps;
6274 }
6275
cf3af59e 6276 /* Create thread to manage application socket */
54d01ffb
DG
6277 ret = pthread_create(&apps_thread, NULL,
6278 thread_manage_apps, (void *) NULL);
cf3af59e 6279 if (ret != 0) {
76d7553f 6280 PERROR("pthread_create apps");
cf3af59e
MD
6281 goto exit_apps;
6282 }
fac6795d 6283
cf3af59e 6284 /* Create kernel thread to manage kernel event */
54d01ffb
DG
6285 ret = pthread_create(&kernel_thread, NULL,
6286 thread_manage_kernel, (void *) NULL);
cf3af59e 6287 if (ret != 0) {
76d7553f 6288 PERROR("pthread_create kernel");
cf3af59e
MD
6289 goto exit_kernel;
6290 }
7a485870 6291
cf3af59e
MD
6292 ret = pthread_join(kernel_thread, &status);
6293 if (ret != 0) {
76d7553f 6294 PERROR("pthread_join");
cf3af59e 6295 goto error; /* join error, exit without cleanup */
fac6795d
DG
6296 }
6297
cf3af59e
MD
6298exit_kernel:
6299 ret = pthread_join(apps_thread, &status);
6300 if (ret != 0) {
76d7553f 6301 PERROR("pthread_join");
cf3af59e
MD
6302 goto error; /* join error, exit without cleanup */
6303 }
fac6795d 6304
cf3af59e 6305exit_apps:
099e26bd
DG
6306 ret = pthread_join(reg_apps_thread, &status);
6307 if (ret != 0) {
76d7553f 6308 PERROR("pthread_join");
099e26bd
DG
6309 goto error; /* join error, exit without cleanup */
6310 }
6311
6312exit_reg_apps:
6313 ret = pthread_join(dispatch_thread, &status);
6314 if (ret != 0) {
76d7553f 6315 PERROR("pthread_join");
099e26bd
DG
6316 goto error; /* join error, exit without cleanup */
6317 }
6318
6319exit_dispatch:
cf3af59e
MD
6320 ret = pthread_join(client_thread, &status);
6321 if (ret != 0) {
76d7553f 6322 PERROR("pthread_join");
cf3af59e
MD
6323 goto error; /* join error, exit without cleanup */
6324 }
6325
3bd1e081 6326 ret = join_consumer_thread(&kconsumer_data);
cf3af59e 6327 if (ret != 0) {
76d7553f 6328 PERROR("join_consumer");
cf3af59e
MD
6329 goto error; /* join error, exit without cleanup */
6330 }
a88df331 6331
cf3af59e 6332exit_client:
44a5e5eb 6333exit_health:
a88df331 6334exit:
cf3af59e
MD
6335 /*
6336 * cleanup() is called when no other thread is running.
6337 */
f6a9efaa 6338 rcu_thread_online();
cf3af59e 6339 cleanup();
f6a9efaa
DG
6340 rcu_thread_offline();
6341 rcu_unregister_thread();
67e40797 6342 if (!ret) {
cf3af59e 6343 exit(EXIT_SUCCESS);
67e40797 6344 }
cf3af59e 6345error:
5e16da05 6346 exit(EXIT_FAILURE);
fac6795d 6347}
This page took 0.413986 seconds and 4 git commands to generate.