Add lttng address API proposal. Initial import
[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 *
451 * At this point, we are sure that not streams data will be lost after this
452 * command is issued.
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
3bd1e081
MD
1632 if (consumer_data->pid != 0) {
1633 ret = kill(consumer_data->pid, SIGTERM);
cf3af59e 1634 if (ret) {
3bd1e081 1635 ERR("Error killing consumer daemon");
cf3af59e
MD
1636 return ret;
1637 }
3bd1e081 1638 return pthread_join(consumer_data->thread, &status);
cf3af59e
MD
1639 } else {
1640 return 0;
1641 }
1642}
1643
8c0faa1d 1644/*
3bd1e081 1645 * Fork and exec a consumer daemon (consumerd).
8c0faa1d 1646 *
d063d709 1647 * Return pid if successful else -1.
8c0faa1d 1648 */
3bd1e081 1649static pid_t spawn_consumerd(struct consumer_data *consumer_data)
8c0faa1d
DG
1650{
1651 int ret;
1652 pid_t pid;
94c55f17 1653 const char *consumer_to_use;
53086306 1654 const char *verbosity;
94c55f17 1655 struct stat st;
8c0faa1d 1656
3bd1e081 1657 DBG("Spawning consumerd");
c49dc785 1658
8c0faa1d
DG
1659 pid = fork();
1660 if (pid == 0) {
1661 /*
3bd1e081 1662 * Exec consumerd.
8c0faa1d 1663 */
daee5345 1664 if (opt_verbose_consumer) {
53086306
DG
1665 verbosity = "--verbose";
1666 } else {
1667 verbosity = "--quiet";
1668 }
3bd1e081
MD
1669 switch (consumer_data->type) {
1670 case LTTNG_CONSUMER_KERNEL:
94c55f17 1671 /*
c7704d57
DG
1672 * Find out which consumerd to execute. We will first try the
1673 * 64-bit path, then the sessiond's installation directory, and
1674 * fallback on the 32-bit one,
94c55f17 1675 */
63a799e8
AM
1676 DBG3("Looking for a kernel consumer at these locations:");
1677 DBG3(" 1) %s", consumerd64_bin);
1678 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
1679 DBG3(" 3) %s", consumerd32_bin);
94c55f17 1680 if (stat(consumerd64_bin, &st) == 0) {
63a799e8 1681 DBG3("Found location #1");
94c55f17 1682 consumer_to_use = consumerd64_bin;
94c55f17 1683 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
63a799e8 1684 DBG3("Found location #2");
94c55f17 1685 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
eb1e0bd4 1686 } else if (stat(consumerd32_bin, &st) == 0) {
63a799e8 1687 DBG3("Found location #3");
eb1e0bd4 1688 consumer_to_use = consumerd32_bin;
94c55f17 1689 } else {
63a799e8 1690 DBG("Could not find any valid consumerd executable");
94c55f17
AM
1691 break;
1692 }
1693 DBG("Using kernel consumer at: %s", consumer_to_use);
1694 execl(consumer_to_use,
1695 "lttng-consumerd", verbosity, "-k",
1696 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1697 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1698 NULL);
3bd1e081 1699 break;
7753dea8
MD
1700 case LTTNG_CONSUMER64_UST:
1701 {
b1e0b6b6 1702 char *tmpnew = NULL;
8f4905da
MD
1703
1704 if (consumerd64_libdir[0] != '\0') {
1705 char *tmp;
1706 size_t tmplen;
1707
1708 tmp = getenv("LD_LIBRARY_PATH");
1709 if (!tmp) {
1710 tmp = "";
1711 }
1712 tmplen = strlen("LD_LIBRARY_PATH=")
1713 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
1714 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1715 if (!tmpnew) {
1716 ret = -ENOMEM;
1717 goto error;
1718 }
1719 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1720 strcat(tmpnew, consumerd64_libdir);
1721 if (tmp[0] != '\0') {
1722 strcat(tmpnew, ":");
1723 strcat(tmpnew, tmp);
1724 }
1725 ret = putenv(tmpnew);
1726 if (ret) {
1727 ret = -errno;
1728 goto error;
1729 }
1730 }
94c55f17 1731 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
a5a6aff3 1732 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1733 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1734 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1735 NULL);
8f4905da
MD
1736 if (consumerd64_libdir[0] != '\0') {
1737 free(tmpnew);
1738 }
1739 if (ret) {
1740 goto error;
1741 }
3bd1e081 1742 break;
7753dea8
MD
1743 }
1744 case LTTNG_CONSUMER32_UST:
1745 {
937dde8e 1746 char *tmpnew = NULL;
8f4905da
MD
1747
1748 if (consumerd32_libdir[0] != '\0') {
1749 char *tmp;
1750 size_t tmplen;
1751
1752 tmp = getenv("LD_LIBRARY_PATH");
1753 if (!tmp) {
1754 tmp = "";
1755 }
1756 tmplen = strlen("LD_LIBRARY_PATH=")
1757 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
1758 tmpnew = zmalloc(tmplen + 1 /* \0 */);
1759 if (!tmpnew) {
1760 ret = -ENOMEM;
1761 goto error;
1762 }
1763 strcpy(tmpnew, "LD_LIBRARY_PATH=");
1764 strcat(tmpnew, consumerd32_libdir);
1765 if (tmp[0] != '\0') {
1766 strcat(tmpnew, ":");
1767 strcat(tmpnew, tmp);
1768 }
1769 ret = putenv(tmpnew);
1770 if (ret) {
1771 ret = -errno;
1772 goto error;
1773 }
1774 }
94c55f17 1775 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
a5a6aff3 1776 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
7753dea8
MD
1777 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
1778 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
1779 NULL);
8f4905da
MD
1780 if (consumerd32_libdir[0] != '\0') {
1781 free(tmpnew);
1782 }
1783 if (ret) {
1784 goto error;
1785 }
7753dea8
MD
1786 break;
1787 }
3bd1e081 1788 default:
76d7553f 1789 PERROR("unknown consumer type");
3bd1e081
MD
1790 exit(EXIT_FAILURE);
1791 }
8c0faa1d 1792 if (errno != 0) {
76d7553f 1793 PERROR("kernel start consumer exec");
8c0faa1d
DG
1794 }
1795 exit(EXIT_FAILURE);
1796 } else if (pid > 0) {
1797 ret = pid;
8c0faa1d 1798 } else {
76d7553f 1799 PERROR("start consumer fork");
8c0faa1d 1800 ret = -errno;
8c0faa1d 1801 }
8f4905da 1802error:
8c0faa1d
DG
1803 return ret;
1804}
1805
693bd40b 1806/*
3bd1e081 1807 * Spawn the consumerd daemon and session daemon thread.
693bd40b 1808 */
3bd1e081 1809static int start_consumerd(struct consumer_data *consumer_data)
693bd40b
DG
1810{
1811 int ret;
1812
3bd1e081
MD
1813 pthread_mutex_lock(&consumer_data->pid_mutex);
1814 if (consumer_data->pid != 0) {
1815 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785
DG
1816 goto end;
1817 }
693bd40b 1818
3bd1e081 1819 ret = spawn_consumerd(consumer_data);
c49dc785 1820 if (ret < 0) {
3bd1e081
MD
1821 ERR("Spawning consumerd failed");
1822 pthread_mutex_unlock(&consumer_data->pid_mutex);
c49dc785 1823 goto error;
693bd40b 1824 }
c49dc785 1825
3bd1e081
MD
1826 /* Setting up the consumer_data pid */
1827 consumer_data->pid = ret;
48842b30 1828 DBG2("Consumer pid %d", consumer_data->pid);
3bd1e081 1829 pthread_mutex_unlock(&consumer_data->pid_mutex);
693bd40b 1830
3bd1e081
MD
1831 DBG2("Spawning consumer control thread");
1832 ret = spawn_consumer_thread(consumer_data);
693bd40b 1833 if (ret < 0) {
3bd1e081 1834 ERR("Fatal error spawning consumer control thread");
693bd40b
DG
1835 goto error;
1836 }
1837
c49dc785 1838end:
693bd40b
DG
1839 return 0;
1840
1841error:
1842 return ret;
1843}
1844
44a5e5eb 1845/*
139ac872
MD
1846 * Compute health status of each consumer. If one of them is zero (bad
1847 * state), we return 0.
44a5e5eb
DG
1848 */
1849static int check_consumer_health(void)
1850{
1851 int ret;
1852
139ac872
MD
1853 ret = health_check_state(&kconsumer_data.health) &&
1854 health_check_state(&ustconsumer32_data.health) &&
44a5e5eb
DG
1855 health_check_state(&ustconsumer64_data.health);
1856
1857 DBG3("Health consumer check %d", ret);
1858
1859 return ret;
1860}
1861
b73401da 1862/*
096102bd 1863 * Check version of the lttng-modules.
b73401da 1864 */
096102bd 1865static int validate_lttng_modules_version(void)
b73401da 1866{
096102bd 1867 return kernel_validate_version(kernel_tracer_fd);
ab147185
MD
1868}
1869
b73401da 1870/*
096102bd 1871 * Setup necessary data for kernel tracer action.
b73401da 1872 */
096102bd 1873static int init_kernel_tracer(void)
b73401da
DG
1874{
1875 int ret;
b73401da 1876
096102bd
DG
1877 /* Modprobe lttng kernel modules */
1878 ret = modprobe_lttng_control();
b73401da 1879 if (ret < 0) {
b73401da
DG
1880 goto error;
1881 }
1882
096102bd
DG
1883 /* Open debugfs lttng */
1884 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1885 if (kernel_tracer_fd < 0) {
1886 DBG("Failed to open %s", module_proc_lttng);
1887 ret = -1;
1888 goto error_open;
b73401da
DG
1889 }
1890
096102bd
DG
1891 /* Validate kernel version */
1892 ret = validate_lttng_modules_version();
b73401da 1893 if (ret < 0) {
096102bd 1894 goto error_version;
b73401da
DG
1895 }
1896
096102bd 1897 ret = modprobe_lttng_data();
b73401da 1898 if (ret < 0) {
096102bd 1899 goto error_modules;
8c0faa1d
DG
1900 }
1901
f3ed775e 1902 DBG("Kernel tracer fd %d", kernel_tracer_fd);
096102bd
DG
1903 return 0;
1904
1905error_version:
1906 modprobe_remove_lttng_control();
76d7553f
MD
1907 ret = close(kernel_tracer_fd);
1908 if (ret) {
1909 PERROR("close");
1910 }
1911 kernel_tracer_fd = -1;
096102bd
DG
1912 return LTTCOMM_KERN_VERSION;
1913
1914error_modules:
76d7553f
MD
1915 ret = close(kernel_tracer_fd);
1916 if (ret) {
1917 PERROR("close");
1918 }
096102bd
DG
1919
1920error_open:
1921 modprobe_remove_lttng_control();
b73401da
DG
1922
1923error:
b73401da 1924 WARN("No kernel tracer available");
a4b35e07 1925 kernel_tracer_fd = -1;
531d29f9
MD
1926 if (!is_root) {
1927 return LTTCOMM_NEED_ROOT_SESSIOND;
1928 } else {
1929 return LTTCOMM_KERN_NA;
1930 }
f3ed775e 1931}
33a2b854 1932
f3ed775e 1933/*
54d01ffb 1934 * Init tracing by creating trace directory and sending fds kernel consumer.
f3ed775e 1935 */
54d01ffb 1936static int init_kernel_tracing(struct ltt_kernel_session *session)
f3ed775e 1937{
f40799e8 1938 int ret = 0;
173af62f
DG
1939 struct lttng_ht_iter iter;
1940 struct consumer_socket *socket;
8c0faa1d 1941
173af62f 1942 assert(session);
d9800920 1943
173af62f
DG
1944 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
1945 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
1946 socket, node.node) {
1947 /* Code flow error */
1948 assert(socket->fd >= 0);
1949
1950 pthread_mutex_lock(socket->lock);
1951 ret = kernel_consumer_send_session(socket->fd, session);
1952 pthread_mutex_unlock(socket->lock);
1953 if (ret < 0) {
1954 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1955 goto error;
1956 }
f3ed775e 1957 }
00e2e675 1958 }
1d4b027a 1959
00e2e675
DG
1960error:
1961 return ret;
1962}
1963
1964/*
1965 * Create a socket to the relayd using the URI.
1966 *
1967 * On success, the relayd_sock pointer is set to the created socket.
1968 * Else, it is untouched and an lttcomm error code is returned.
1969 */
1970static int create_connect_relayd(struct consumer_output *output,
1971 const char *session_name, struct lttng_uri *uri,
1972 struct lttcomm_sock **relayd_sock)
1973{
1974 int ret;
1975 struct lttcomm_sock *sock;
1976
1977 /* Create socket object from URI */
1978 sock = lttcomm_alloc_sock_from_uri(uri);
1979 if (sock == NULL) {
1980 ret = LTTCOMM_FATAL;
1981 goto error;
1982 }
1983
1984 ret = lttcomm_create_sock(sock);
1985 if (ret < 0) {
1986 ret = LTTCOMM_FATAL;
1987 goto error;
1988 }
1989
1990 /* Connect to relayd so we can proceed with a session creation. */
1991 ret = relayd_connect(sock);
1992 if (ret < 0) {
1993 ERR("Unable to reach lttng-relayd");
1994 ret = LTTCOMM_RELAYD_SESSION_FAIL;
1995 goto free_sock;
f3ed775e 1996 }
1d4b027a 1997
00e2e675
DG
1998 /* Create socket for control stream. */
1999 if (uri->stype == LTTNG_STREAM_CONTROL) {
2000 DBG3("Creating relayd stream socket from URI");
2001
2002 /* Check relayd version */
2003 ret = relayd_version_check(sock, LTTNG_UST_COMM_MAJOR, 0);
2004 if (ret < 0) {
2005 ret = LTTCOMM_RELAYD_VERSION_FAIL;
2006 goto close_sock;
2007 }
2008 } else if (uri->stype == LTTNG_STREAM_DATA) {
2009 DBG3("Creating relayd data socket from URI");
2010 } else {
2011 /* Command is not valid */
2012 ERR("Relayd invalid stream type: %d", uri->stype);
2013 ret = LTTCOMM_INVALID;
2014 goto close_sock;
2015 }
2016
2017 *relayd_sock = sock;
2018
2019 return LTTCOMM_OK;
2020
2021close_sock:
2022 if (sock) {
2023 (void) relayd_close(sock);
2024 }
2025free_sock:
2026 if (sock) {
2027 lttcomm_destroy_sock(sock);
2028 }
2029error:
2030 return ret;
2031}
2032
2033/*
2034 * Connect to the relayd using URI and send the socket to the right consumer.
2035 */
2036static int send_socket_relayd_consumer(int domain, struct ltt_session *session,
2037 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
2038 int consumer_fd)
2039{
2040 int ret;
2041 struct lttcomm_sock *sock = NULL;
2042
2043 /* Set the network sequence index if not set. */
2044 if (consumer->net_seq_index == -1) {
2045 /*
2046 * Increment net_seq_idx because we are about to transfer the
2047 * new relayd socket to the consumer.
2048 */
2049 uatomic_inc(&relayd_net_seq_idx);
2050 /* Assign unique key so the consumer can match streams */
2051 consumer->net_seq_index = uatomic_read(&relayd_net_seq_idx);
2052 }
2053
2054 /* Connect to relayd and make version check if uri is the control. */
2055 ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
2056 if (ret != LTTCOMM_OK) {
2057 goto close_sock;
2058 }
2059
2060 /* If the control socket is connected, network session is ready */
2061 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
2062 session->net_handle = 1;
2063 }
2064
37278a1e
DG
2065 /* Send relayd socket to consumer. */
2066 ret = consumer_send_relayd_socket(consumer_fd, sock,
2067 consumer, relayd_uri->stype);
2068 if (ret < 0) {
2069 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
2070 goto close_sock;
00e2e675
DG
2071 }
2072
2073 ret = LTTCOMM_OK;
2074
2075 /*
2076 * Close socket which was dup on the consumer side. The session daemon does
2077 * NOT keep track of the relayd socket(s) once transfer to the consumer.
2078 */
2079
2080close_sock:
2081 if (sock) {
2082 (void) relayd_close(sock);
2083 lttcomm_destroy_sock(sock);
2084 }
2085
2086 return ret;
2087}
2088
2089/*
2090 * Send both relayd sockets to a specific consumer and domain. This is a
2091 * helper function to facilitate sending the information to the consumer for a
2092 * session.
2093 */
2094static int send_sockets_relayd_consumer(int domain,
2095 struct ltt_session *session, struct consumer_output *consumer, int fd)
2096{
2097 int ret;
2098
2099 /* Sending control relayd socket. */
2100 ret = send_socket_relayd_consumer(domain, session,
2101 &consumer->dst.net.control, consumer, fd);
2102 if (ret != LTTCOMM_OK) {
2103 goto error;
2104 }
2105
2106 /* Sending data relayd socket. */
2107 ret = send_socket_relayd_consumer(domain, session,
2108 &consumer->dst.net.data, consumer, fd);
2109 if (ret != LTTCOMM_OK) {
2110 goto error;
2111 }
2112
2113error:
2114 return ret;
2115}
2116
2117/*
2118 * Setup relayd connections for a tracing session. First creates the socket to
2119 * the relayd and send them to the right domain consumer. Consumer type MUST be
2120 * network.
2121 */
2122static int setup_relayd(struct ltt_session *session)
2123{
2124 int ret = LTTCOMM_OK;
2125 struct ltt_ust_session *usess;
2126 struct ltt_kernel_session *ksess;
173af62f
DG
2127 struct consumer_socket *socket;
2128 struct lttng_ht_iter iter;
00e2e675
DG
2129
2130 assert(session);
2131
2132 usess = session->ust_session;
2133 ksess = session->kernel_session;
2134
2135 DBG2("Setting relayd for session %s", session->name);
2136
173af62f 2137 if (usess && usess->consumer->type == CONSUMER_DST_NET &&
00e2e675 2138 usess->consumer->enabled) {
173af62f
DG
2139 /* For each consumer socket, send relayd sockets */
2140 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
2141 socket, node.node) {
2142 /* Code flow error */
2143 assert(socket->fd >= 0);
2144
2145 pthread_mutex_lock(socket->lock);
00e2e675 2146 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST, session,
173af62f
DG
2147 usess->consumer, socket->fd);
2148 pthread_mutex_unlock(socket->lock);
00e2e675
DG
2149 if (ret != LTTCOMM_OK) {
2150 goto error;
2151 }
2152 }
173af62f
DG
2153 } else if (ksess && ksess->consumer->type == CONSUMER_DST_NET &&
2154 ksess->consumer->enabled) {
2155 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
2156 socket, node.node) {
2157 /* Code flow error */
2158 assert(socket->fd >= 0);
2159
2160 pthread_mutex_lock(socket->lock);
2161 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL, session,
2162 ksess->consumer, socket->fd);
2163 pthread_mutex_unlock(socket->lock);
00e2e675
DG
2164 if (ret != LTTCOMM_OK) {
2165 goto error;
2166 }
2167 }
00e2e675
DG
2168 }
2169
2170error:
2171 return ret;
2172}
2173
2174/*
2175 * Copy consumer output from the tracing session to the domain session. The
2176 * function also applies the right modification on a per domain basis for the
2177 * trace files destination directory.
2178 */
2179static int copy_session_consumer(int domain, struct ltt_session *session)
2180{
2181 int ret;
2182 const char *dir_name;
2183 struct consumer_output *consumer;
2184
2185 switch (domain) {
2186 case LTTNG_DOMAIN_KERNEL:
2187 DBG3("Copying tracing session consumer output in kernel session");
2188 session->kernel_session->consumer =
2189 consumer_copy_output(session->consumer);
2190 /* Ease our life a bit for the next part */
2191 consumer = session->kernel_session->consumer;
2192 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2193 break;
2194 case LTTNG_DOMAIN_UST:
2195 DBG3("Copying tracing session consumer output in UST session");
2196 session->ust_session->consumer =
2197 consumer_copy_output(session->consumer);
2198 /* Ease our life a bit for the next part */
2199 consumer = session->ust_session->consumer;
2200 dir_name = DEFAULT_UST_TRACE_DIR;
2201 break;
2202 default:
2203 ret = LTTCOMM_UNKNOWN_DOMAIN;
2204 goto error;
2205 }
2206
2207 /* Append correct directory to subdir */
2208 strncat(consumer->subdir, dir_name, sizeof(consumer->subdir));
2209 DBG3("Copy session consumer subdir %s", consumer->subdir);
2210
2211 /* Add default trace directory name */
2212 if (consumer->type == CONSUMER_DST_LOCAL) {
2213 strncat(consumer->dst.trace_path, dir_name,
2214 sizeof(consumer->dst.trace_path));
2215 }
2216
2217 ret = LTTCOMM_OK;
2218
1d4b027a
DG
2219error:
2220 return ret;
8c0faa1d
DG
2221}
2222
0177d773
DG
2223/*
2224 * Create an UST session and add it to the session ust list.
2225 */
44d3bd01 2226static int create_ust_session(struct ltt_session *session,
6df2e2c9 2227 struct lttng_domain *domain)
0177d773 2228{
6df2e2c9 2229 int ret;
00e2e675
DG
2230 struct ltt_ust_session *lus = NULL;
2231
2232 assert(session);
2233 assert(session->consumer);
44d3bd01
DG
2234
2235 switch (domain->type) {
48842b30 2236 case LTTNG_DOMAIN_UST:
44d3bd01
DG
2237 break;
2238 default:
00e2e675 2239 ERR("Unknown UST domain on create session %d", domain->type);
13384287 2240 ret = LTTCOMM_UNKNOWN_DOMAIN;
44d3bd01
DG
2241 goto error;
2242 }
0177d773
DG
2243
2244 DBG("Creating UST session");
2245
a991f516 2246 lus = trace_ust_create_session(session->path, session->id, domain);
0177d773 2247 if (lus == NULL) {
44d3bd01 2248 ret = LTTCOMM_UST_SESS_FAIL;
0177d773
DG
2249 goto error;
2250 }
2251
00e2e675
DG
2252 if (session->consumer->type == CONSUMER_DST_LOCAL) {
2253 ret = run_as_mkdir_recursive(lus->pathname, S_IRWXU | S_IRWXG,
2254 session->uid, session->gid);
2255 if (ret < 0) {
2256 if (ret != -EEXIST) {
2257 ERR("Trace directory creation error");
2258 ret = LTTCOMM_UST_SESS_FAIL;
2259 goto error;
2260 }
0177d773
DG
2261 }
2262 }
2263
6df2e2c9
MD
2264 lus->uid = session->uid;
2265 lus->gid = session->gid;
f6a9efaa 2266 session->ust_session = lus;
44d3bd01 2267
00e2e675
DG
2268 /* Copy session output to the newly created UST session */
2269 ret = copy_session_consumer(domain->type, session);
2270 if (ret != LTTCOMM_OK) {
2271 goto error;
2272 }
2273
44d3bd01 2274 return LTTCOMM_OK;
0177d773
DG
2275
2276error:
2277 free(lus);
00e2e675 2278 session->ust_session = NULL;
0177d773
DG
2279 return ret;
2280}
2281
333f285e 2282/*
d063d709 2283 * Create a kernel tracer session then create the default channel.
333f285e 2284 */
6df2e2c9 2285static int create_kernel_session(struct ltt_session *session)
333f285e 2286{
f3ed775e 2287 int ret;
f3ed775e
DG
2288
2289 DBG("Creating kernel session");
2290
2291 ret = kernel_create_session(session, kernel_tracer_fd);
2292 if (ret < 0) {
2293 ret = LTTCOMM_KERN_SESS_FAIL;
2294 goto error;
333f285e
DG
2295 }
2296
00e2e675
DG
2297 /* Copy session output to the newly created Kernel session */
2298 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2299 if (ret != LTTCOMM_OK) {
2300 goto error;
2301 }
2302
2303 /* Create directory(ies) on local filesystem. */
2304 if (session->consumer->type == CONSUMER_DST_LOCAL) {
2305 ret = run_as_mkdir_recursive(
2306 session->kernel_session->consumer->dst.trace_path,
2307 S_IRWXU | S_IRWXG, session->uid, session->gid);
2308 if (ret < 0) {
2309 if (ret != -EEXIST) {
2310 ERR("Trace directory creation error");
2311 goto error;
2312 }
7a485870
DG
2313 }
2314 }
00e2e675 2315
6df2e2c9
MD
2316 session->kernel_session->uid = session->uid;
2317 session->kernel_session->gid = session->gid;
7a485870 2318
00e2e675
DG
2319 return LTTCOMM_OK;
2320
f3ed775e 2321error:
00e2e675
DG
2322 trace_kernel_destroy_session(session->kernel_session);
2323 session->kernel_session = NULL;
f3ed775e 2324 return ret;
333f285e
DG
2325}
2326
8e0af1b4 2327/*
c7704d57
DG
2328 * Check if the UID or GID match the session. Root user has access to all
2329 * sessions.
8e0af1b4 2330 */
c7704d57 2331static int session_access_ok(struct ltt_session *session, uid_t uid, gid_t gid)
8e0af1b4 2332{
c7704d57 2333 if (uid != session->uid && gid != session->gid && uid != 0) {
8e0af1b4
MD
2334 return 0;
2335 } else {
2336 return 1;
2337 }
2338}
2339
00e2e675
DG
2340/*
2341 * Count number of session permitted by uid/gid.
2342 */
8e0af1b4
MD
2343static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2344{
2345 unsigned int i = 0;
2346 struct ltt_session *session;
2347
2348 DBG("Counting number of available session for UID %d GID %d",
2349 uid, gid);
2350 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2351 /*
2352 * Only list the sessions the user can control.
2353 */
2354 if (!session_access_ok(session, uid, gid)) {
2355 continue;
2356 }
2357 i++;
2358 }
2359 return i;
2360}
2361
6c9cc2ab
DG
2362/*
2363 * Using the session list, filled a lttng_session array to send back to the
2364 * client for session listing.
2365 *
2366 * The session list lock MUST be acquired before calling this function. Use
54d01ffb 2367 * session_lock_list() and session_unlock_list().
6c9cc2ab 2368 */
c7704d57
DG
2369static void list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2370 gid_t gid)
6c9cc2ab 2371{
8e0af1b4 2372 unsigned int i = 0;
6c9cc2ab
DG
2373 struct ltt_session *session;
2374
8e0af1b4
MD
2375 DBG("Getting all available session for UID %d GID %d",
2376 uid, gid);
6c9cc2ab
DG
2377 /*
2378 * Iterate over session list and append data after the control struct in
2379 * the buffer.
2380 */
2381 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
8e0af1b4
MD
2382 /*
2383 * Only list the sessions the user can control.
2384 */
2385 if (!session_access_ok(session, uid, gid)) {
2386 continue;
2387 }
6c9cc2ab 2388 strncpy(sessions[i].path, session->path, PATH_MAX);
99497cd0 2389 sessions[i].path[PATH_MAX - 1] = '\0';
6c9cc2ab 2390 strncpy(sessions[i].name, session->name, NAME_MAX);
99497cd0 2391 sessions[i].name[NAME_MAX - 1] = '\0';
464dd62d 2392 sessions[i].enabled = session->enabled;
6c9cc2ab
DG
2393 i++;
2394 }
2395}
2396
9f19cc17
DG
2397/*
2398 * Fill lttng_channel array of all channels.
2399 */
b551a063 2400static void list_lttng_channels(int domain, struct ltt_session *session,
9f19cc17
DG
2401 struct lttng_channel *channels)
2402{
2403 int i = 0;
2404 struct ltt_kernel_channel *kchan;
2405
2406 DBG("Listing channels for session %s", session->name);
2407
b551a063
DG
2408 switch (domain) {
2409 case LTTNG_DOMAIN_KERNEL:
2410 /* Kernel channels */
2411 if (session->kernel_session != NULL) {
2412 cds_list_for_each_entry(kchan,
2413 &session->kernel_session->channel_list.head, list) {
2414 /* Copy lttng_channel struct to array */
2415 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
2416 channels[i].enabled = kchan->enabled;
2417 i++;
2418 }
2419 }
2420 break;
2421 case LTTNG_DOMAIN_UST:
2422 {
bec39940 2423 struct lttng_ht_iter iter;
b551a063
DG
2424 struct ltt_ust_channel *uchan;
2425
bec39940
DG
2426 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
2427 &iter.iter, uchan, node.node) {
b551a063
DG
2428 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
2429 channels[i].attr.overwrite = uchan->attr.overwrite;
2430 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
2431 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
2432 channels[i].attr.switch_timer_interval =
2433 uchan->attr.switch_timer_interval;
2434 channels[i].attr.read_timer_interval =
2435 uchan->attr.read_timer_interval;
aea829b3 2436 channels[i].enabled = uchan->enabled;
5edd7e09
DG
2437 switch (uchan->attr.output) {
2438 case LTTNG_UST_MMAP:
2439 default:
2440 channels[i].attr.output = LTTNG_EVENT_MMAP;
2441 break;
2442 }
befe0905 2443 i++;
b551a063
DG
2444 }
2445 break;
2446 }
2447 default:
2448 break;
2449 }
2450}
2451
2452/*
2453 * Create a list of ust global domain events.
2454 */
2455static int list_lttng_ust_global_events(char *channel_name,
2456 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
2457{
2458 int i = 0, ret = 0;
2459 unsigned int nb_event = 0;
bec39940
DG
2460 struct lttng_ht_iter iter;
2461 struct lttng_ht_node_str *node;
b551a063
DG
2462 struct ltt_ust_channel *uchan;
2463 struct ltt_ust_event *uevent;
2464 struct lttng_event *tmp;
2465
2466 DBG("Listing UST global events for channel %s", channel_name);
2467
2468 rcu_read_lock();
2469
bec39940
DG
2470 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
2471 node = lttng_ht_iter_get_node_str(&iter);
d7b75b30
DG
2472 if (node == NULL) {
2473 ret = -LTTCOMM_UST_CHAN_NOT_FOUND;
2474 goto error;
b551a063
DG
2475 }
2476
bec39940 2477 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
d7b75b30 2478
bec39940 2479 nb_event += lttng_ht_get_count(uchan->events);
d7b75b30 2480
b551a063
DG
2481 if (nb_event == 0) {
2482 ret = nb_event;
2483 goto error;
2484 }
2485
2486 DBG3("Listing UST global %d events", nb_event);
2487
2488 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
2489 if (tmp == NULL) {
2490 ret = -LTTCOMM_FATAL;
2491 goto error;
2492 }
2493
bec39940 2494 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
d7b75b30
DG
2495 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
2496 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2497 tmp[i].enabled = uevent->enabled;
2498 switch (uevent->attr.instrumentation) {
2499 case LTTNG_UST_TRACEPOINT:
2500 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
2501 break;
2502 case LTTNG_UST_PROBE:
2503 tmp[i].type = LTTNG_EVENT_PROBE;
2504 break;
2505 case LTTNG_UST_FUNCTION:
2506 tmp[i].type = LTTNG_EVENT_FUNCTION;
2507 break;
0cda4f28 2508 }
8005f29a 2509 tmp[i].loglevel = uevent->attr.loglevel;
0cda4f28 2510 switch (uevent->attr.loglevel_type) {
8005f29a 2511 case LTTNG_UST_LOGLEVEL_ALL:
22e25b71 2512 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
0cda4f28 2513 break;
8005f29a 2514 case LTTNG_UST_LOGLEVEL_RANGE:
22e25b71 2515 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
8005f29a
MD
2516 break;
2517 case LTTNG_UST_LOGLEVEL_SINGLE:
22e25b71 2518 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
ac3bd9c0 2519 break;
9f19cc17 2520 }
fceb65df
MD
2521 if (uevent->filter) {
2522 tmp[i].filter = 1;
2523 }
d7b75b30 2524 i++;
9f19cc17
DG
2525 }
2526
b551a063
DG
2527 ret = nb_event;
2528 *events = tmp;
2529
2530error:
2531 rcu_read_unlock();
2532 return ret;
9f19cc17
DG
2533}
2534
2535/*
b551a063 2536 * Fill lttng_event array of all kernel events in the channel.
9f19cc17 2537 */
b551a063
DG
2538static int list_lttng_kernel_events(char *channel_name,
2539 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
9f19cc17 2540{
b551a063
DG
2541 int i = 0, ret;
2542 unsigned int nb_event;
9f19cc17 2543 struct ltt_kernel_event *event;
b551a063
DG
2544 struct ltt_kernel_channel *kchan;
2545
2546 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
2547 if (kchan == NULL) {
2548 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2549 goto error;
2550 }
2551
2552 nb_event = kchan->event_count;
9f19cc17
DG
2553
2554 DBG("Listing events for channel %s", kchan->channel->name);
2555
b551a063
DG
2556 if (nb_event == 0) {
2557 ret = nb_event;
2558 goto error;
2559 }
2560
2561 *events = zmalloc(nb_event * sizeof(struct lttng_event));
2562 if (*events == NULL) {
2563 ret = LTTCOMM_FATAL;
2564 goto error;
2565 }
2566
9f19cc17
DG
2567 /* Kernel channels */
2568 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
b551a063
DG
2569 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
2570 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2571 (*events)[i].enabled = event->enabled;
9f19cc17
DG
2572 switch (event->event->instrumentation) {
2573 case LTTNG_KERNEL_TRACEPOINT:
b551a063 2574 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
9f19cc17
DG
2575 break;
2576 case LTTNG_KERNEL_KPROBE:
2577 case LTTNG_KERNEL_KRETPROBE:
b551a063
DG
2578 (*events)[i].type = LTTNG_EVENT_PROBE;
2579 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
9f19cc17
DG
2580 sizeof(struct lttng_kernel_kprobe));
2581 break;
2582 case LTTNG_KERNEL_FUNCTION:
b551a063
DG
2583 (*events)[i].type = LTTNG_EVENT_FUNCTION;
2584 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
9f19cc17
DG
2585 sizeof(struct lttng_kernel_function));
2586 break;
0133c199 2587 case LTTNG_KERNEL_NOOP:
b551a063 2588 (*events)[i].type = LTTNG_EVENT_NOOP;
0133c199 2589 break;
a54bd42d 2590 case LTTNG_KERNEL_SYSCALL:
b551a063 2591 (*events)[i].type = LTTNG_EVENT_SYSCALL;
0133c199 2592 break;
7a3d1328
MD
2593 case LTTNG_KERNEL_ALL:
2594 assert(0);
2595 break;
9f19cc17
DG
2596 }
2597 i++;
2598 }
b551a063
DG
2599
2600 return nb_event;
2601
2602error:
2603 return ret;
9f19cc17
DG
2604}
2605
fac6795d 2606/*
54d01ffb 2607 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
fac6795d 2608 */
54d01ffb
DG
2609static int cmd_disable_channel(struct ltt_session *session,
2610 int domain, char *channel_name)
fac6795d 2611{
54d01ffb 2612 int ret;
78f0bacd
DG
2613 struct ltt_ust_session *usess;
2614
2615 usess = session->ust_session;
379473d2 2616
54d01ffb 2617 switch (domain) {
78f0bacd
DG
2618 case LTTNG_DOMAIN_KERNEL:
2619 {
2620 ret = channel_kernel_disable(session->kernel_session,
2621 channel_name);
2622 if (ret != LTTCOMM_OK) {
2623 goto error;
2624 }
54d01ffb 2625
78f0bacd
DG
2626 kernel_wait_quiescent(kernel_tracer_fd);
2627 break;
2628 }
2629 case LTTNG_DOMAIN_UST:
2630 {
2631 struct ltt_ust_channel *uchan;
bec39940 2632 struct lttng_ht *chan_ht;
78f0bacd 2633
7885e399
DG
2634 chan_ht = usess->domain_global.channels;
2635
2636 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
78f0bacd
DG
2637 if (uchan == NULL) {
2638 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
54d01ffb 2639 goto error;
78f0bacd
DG
2640 }
2641
7885e399
DG
2642 ret = channel_ust_disable(usess, domain, uchan);
2643 if (ret != LTTCOMM_OK) {
78f0bacd
DG
2644 goto error;
2645 }
78f0bacd
DG
2646 break;
2647 }
d78d6610 2648#if 0
78f0bacd
DG
2649 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2650 case LTTNG_DOMAIN_UST_EXEC_NAME:
2651 case LTTNG_DOMAIN_UST_PID:
d78d6610 2652#endif
78f0bacd
DG
2653 default:
2654 ret = LTTCOMM_UNKNOWN_DOMAIN;
2655 goto error;
20fe2104
DG
2656 }
2657
54d01ffb 2658 ret = LTTCOMM_OK;
d65106b1 2659
54d01ffb
DG
2660error:
2661 return ret;
2662}
2663
2664/*
2665 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2666 */
44d3bd01 2667static int cmd_enable_channel(struct ltt_session *session,
aea829b3 2668 int domain, struct lttng_channel *attr)
54d01ffb
DG
2669{
2670 int ret;
f6a9efaa 2671 struct ltt_ust_session *usess = session->ust_session;
bec39940 2672 struct lttng_ht *chan_ht;
f6a9efaa 2673
5d56b5aa 2674 DBG("Enabling channel %s for session %s", attr->name, session->name);
d65106b1 2675
aea829b3 2676 switch (domain) {
44d3bd01
DG
2677 case LTTNG_DOMAIN_KERNEL:
2678 {
2679 struct ltt_kernel_channel *kchan;
54d01ffb 2680
44d3bd01
DG
2681 kchan = trace_kernel_get_channel_by_name(attr->name,
2682 session->kernel_session);
2683 if (kchan == NULL) {
2684 ret = channel_kernel_create(session->kernel_session,
2685 attr, kernel_poll_pipe[1]);
2686 } else {
2687 ret = channel_kernel_enable(session->kernel_session, kchan);
2688 }
2689
2690 if (ret != LTTCOMM_OK) {
2691 goto error;
2692 }
2693
2694 kernel_wait_quiescent(kernel_tracer_fd);
2695 break;
2696 }
f6a9efaa
DG
2697 case LTTNG_DOMAIN_UST:
2698 {
2699 struct ltt_ust_channel *uchan;
2700
7885e399 2701 chan_ht = usess->domain_global.channels;
f6a9efaa 2702
7885e399 2703 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
f6a9efaa 2704 if (uchan == NULL) {
7885e399 2705 ret = channel_ust_create(usess, domain, attr);
f6a9efaa 2706 } else {
7885e399 2707 ret = channel_ust_enable(usess, domain, uchan);
48842b30 2708 }
f6a9efaa
DG
2709 break;
2710 }
d78d6610 2711#if 0
78f0bacd
DG
2712 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2713 case LTTNG_DOMAIN_UST_EXEC_NAME:
44d3bd01 2714 case LTTNG_DOMAIN_UST_PID:
d78d6610 2715#endif
44d3bd01
DG
2716 default:
2717 ret = LTTCOMM_UNKNOWN_DOMAIN;
2718 goto error;
54d01ffb
DG
2719 }
2720
54d01ffb
DG
2721error:
2722 return ret;
2723}
2724
2725/*
2726 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2727 */
2728static int cmd_disable_event(struct ltt_session *session, int domain,
2729 char *channel_name, char *event_name)
2730{
2731 int ret;
54d01ffb
DG
2732
2733 switch (domain) {
2734 case LTTNG_DOMAIN_KERNEL:
2bdd86d4
MD
2735 {
2736 struct ltt_kernel_channel *kchan;
b0a40d28 2737 struct ltt_kernel_session *ksess;
2bdd86d4 2738
b0a40d28
DG
2739 ksess = session->kernel_session;
2740
2741 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
54d01ffb
DG
2742 if (kchan == NULL) {
2743 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2744 goto error;
d65106b1
DG
2745 }
2746
b0a40d28 2747 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
54d01ffb
DG
2748 if (ret != LTTCOMM_OK) {
2749 goto error;
2750 }
2751
2752 kernel_wait_quiescent(kernel_tracer_fd);
d65106b1 2753 break;
2bdd86d4
MD
2754 }
2755 case LTTNG_DOMAIN_UST:
b0a40d28 2756 {
b0a40d28 2757 struct ltt_ust_channel *uchan;
7f79d3a1 2758 struct ltt_ust_session *usess;
b0a40d28
DG
2759
2760 usess = session->ust_session;
2761
2762 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2763 channel_name);
2764 if (uchan == NULL) {
2765 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2766 goto error;
2767 }
2768
7f79d3a1
DG
2769 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
2770 if (ret != LTTCOMM_OK) {
b0a40d28
DG
2771 goto error;
2772 }
2773
7f79d3a1 2774 DBG3("Disable UST event %s in channel %s completed", event_name,
b0a40d28 2775 channel_name);
b0a40d28
DG
2776 break;
2777 }
d78d6610 2778#if 0
2bdd86d4
MD
2779 case LTTNG_DOMAIN_UST_EXEC_NAME:
2780 case LTTNG_DOMAIN_UST_PID:
2781 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 2782#endif
54d01ffb 2783 default:
1ab1ea0b 2784 ret = LTTCOMM_UND;
54d01ffb 2785 goto error;
d65106b1 2786 }
26cc6b4e 2787
54d01ffb
DG
2788 ret = LTTCOMM_OK;
2789
2790error:
2791 return ret;
2792}
2793
2794/*
2795 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2796 */
2797static int cmd_disable_event_all(struct ltt_session *session, int domain,
2798 char *channel_name)
2799{
2800 int ret;
54d01ffb
DG
2801
2802 switch (domain) {
2803 case LTTNG_DOMAIN_KERNEL:
9730260e
DG
2804 {
2805 struct ltt_kernel_session *ksess;
2806 struct ltt_kernel_channel *kchan;
2807
2808 ksess = session->kernel_session;
2809
2810 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
54d01ffb
DG
2811 if (kchan == NULL) {
2812 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2813 goto error;
26cc6b4e
DG
2814 }
2815
9730260e 2816 ret = event_kernel_disable_all(ksess, kchan);
54d01ffb 2817 if (ret != LTTCOMM_OK) {
0b97ec54 2818 goto error;
26cc6b4e
DG
2819 }
2820
54d01ffb 2821 kernel_wait_quiescent(kernel_tracer_fd);
26cc6b4e 2822 break;
9730260e
DG
2823 }
2824 case LTTNG_DOMAIN_UST:
2825 {
2826 struct ltt_ust_session *usess;
2827 struct ltt_ust_channel *uchan;
2828
2829 usess = session->ust_session;
2830
2831 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2832 channel_name);
2833 if (uchan == NULL) {
2834 ret = LTTCOMM_UST_CHAN_NOT_FOUND;
2835 goto error;
2836 }
2837
7f79d3a1
DG
2838 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
2839 if (ret != 0) {
9730260e
DG
2840 goto error;
2841 }
2842
7f79d3a1 2843 DBG3("Disable all UST events in channel %s completed", channel_name);
9730260e
DG
2844
2845 break;
2846 }
d78d6610 2847#if 0
9730260e
DG
2848 case LTTNG_DOMAIN_UST_EXEC_NAME:
2849 case LTTNG_DOMAIN_UST_PID:
2850 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 2851#endif
54d01ffb 2852 default:
1ab1ea0b 2853 ret = LTTCOMM_UND;
54d01ffb 2854 goto error;
26cc6b4e 2855 }
e953ef25 2856
54d01ffb 2857 ret = LTTCOMM_OK;
e953ef25 2858
54d01ffb
DG
2859error:
2860 return ret;
2861}
f5177a38 2862
54d01ffb
DG
2863/*
2864 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2865 */
2866static int cmd_add_context(struct ltt_session *session, int domain,
2867 char *channel_name, char *event_name, struct lttng_event_context *ctx)
2868{
2869 int ret;
f5177a38 2870
54d01ffb
DG
2871 switch (domain) {
2872 case LTTNG_DOMAIN_KERNEL:
2873 /* Add kernel context to kernel tracer */
2874 ret = context_kernel_add(session->kernel_session, ctx,
2875 event_name, channel_name);
2876 if (ret != LTTCOMM_OK) {
f5177a38 2877 goto error;
e953ef25 2878 }
2bdd86d4
MD
2879 break;
2880 case LTTNG_DOMAIN_UST:
2881 {
55cc08a6 2882 struct ltt_ust_session *usess = session->ust_session;
e953ef25 2883
55cc08a6
DG
2884 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
2885 if (ret != LTTCOMM_OK) {
2886 goto error;
2bdd86d4 2887 }
e953ef25 2888 break;
2bdd86d4 2889 }
d78d6610 2890#if 0
55cc08a6
DG
2891 case LTTNG_DOMAIN_UST_EXEC_NAME:
2892 case LTTNG_DOMAIN_UST_PID:
2893 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 2894#endif
54d01ffb 2895 default:
1ab1ea0b 2896 ret = LTTCOMM_UND;
54d01ffb 2897 goto error;
e953ef25 2898 }
950131af 2899
54d01ffb 2900 ret = LTTCOMM_OK;
950131af 2901
54d01ffb
DG
2902error:
2903 return ret;
2904}
2905
53a80697
MD
2906/*
2907 * Command LTTNG_SET_FILTER processed by the client thread.
2908 */
2909static int cmd_set_filter(struct ltt_session *session, int domain,
2910 char *channel_name, char *event_name,
2911 struct lttng_filter_bytecode *bytecode)
2912{
2913 int ret;
2914
2915 switch (domain) {
2916 case LTTNG_DOMAIN_KERNEL:
2917 ret = LTTCOMM_FATAL;
2918 break;
2919 case LTTNG_DOMAIN_UST:
2920 {
2921 struct ltt_ust_session *usess = session->ust_session;
2922
2923 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
2924 if (ret != LTTCOMM_OK) {
2925 goto error;
2926 }
2927 break;
2928 }
2929#if 0
2930 case LTTNG_DOMAIN_UST_EXEC_NAME:
2931 case LTTNG_DOMAIN_UST_PID:
2932 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
2933#endif
2934 default:
2935 ret = LTTCOMM_UND;
2936 goto error;
2937 }
2938
2939 ret = LTTCOMM_OK;
2940
2941error:
2942 return ret;
2943
2944}
2945
54d01ffb
DG
2946/*
2947 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2948 */
2949static int cmd_enable_event(struct ltt_session *session, int domain,
2950 char *channel_name, struct lttng_event *event)
2951{
2952 int ret;
f6cd6b0f 2953 struct lttng_channel *attr;
48842b30 2954 struct ltt_ust_session *usess = session->ust_session;
54d01ffb
DG
2955
2956 switch (domain) {
2957 case LTTNG_DOMAIN_KERNEL:
2bdd86d4
MD
2958 {
2959 struct ltt_kernel_channel *kchan;
2960
54d01ffb
DG
2961 kchan = trace_kernel_get_channel_by_name(channel_name,
2962 session->kernel_session);
2963 if (kchan == NULL) {
f6cd6b0f
DG
2964 attr = channel_new_default_attr(domain);
2965 if (attr == NULL) {
2966 ret = LTTCOMM_FATAL;
2967 goto error;
2968 }
2969 snprintf(attr->name, NAME_MAX, "%s", channel_name);
2970
54d01ffb 2971 /* This call will notify the kernel thread */
44d3bd01 2972 ret = channel_kernel_create(session->kernel_session,
f6cd6b0f 2973 attr, kernel_poll_pipe[1]);
54d01ffb 2974 if (ret != LTTCOMM_OK) {
ff4d74e6 2975 free(attr);
f5177a38
DG
2976 goto error;
2977 }
ff4d74e6 2978 free(attr);
54d01ffb 2979 }
950131af 2980
54d01ffb
DG
2981 /* Get the newly created kernel channel pointer */
2982 kchan = trace_kernel_get_channel_by_name(channel_name,
2983 session->kernel_session);
2984 if (kchan == NULL) {
2985 /* This sould not happen... */
2986 ret = LTTCOMM_FATAL;
2987 goto error;
2988 }
f5177a38 2989
48842b30
DG
2990 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
2991 event);
54d01ffb 2992 if (ret != LTTCOMM_OK) {
f5177a38 2993 goto error;
950131af
DG
2994 }
2995
54d01ffb 2996 kernel_wait_quiescent(kernel_tracer_fd);
950131af 2997 break;
2bdd86d4
MD
2998 }
2999 case LTTNG_DOMAIN_UST:
3000 {
aea829b3 3001 struct lttng_channel *attr;
edb67388 3002 struct ltt_ust_channel *uchan;
2bdd86d4 3003
edb67388 3004 /* Get channel from global UST domain */
48842b30
DG
3005 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3006 channel_name);
3007 if (uchan == NULL) {
aea829b3
DG
3008 /* Create default channel */
3009 attr = channel_new_default_attr(domain);
3010 if (attr == NULL) {
3011 ret = LTTCOMM_FATAL;
3012 goto error;
3013 }
596219f7 3014 snprintf(attr->name, NAME_MAX, "%s", channel_name);
edb67388 3015 attr->name[NAME_MAX - 1] = '\0';
aea829b3 3016
7885e399 3017 ret = channel_ust_create(usess, domain, attr);
edb67388 3018 if (ret != LTTCOMM_OK) {
ff4d74e6 3019 free(attr);
aea829b3
DG
3020 goto error;
3021 }
aea829b3
DG
3022 free(attr);
3023
3024 /* Get the newly created channel reference back */
3025 uchan = trace_ust_find_channel_by_name(
3026 usess->domain_global.channels, channel_name);
3027 if (uchan == NULL) {
3028 /* Something is really wrong */
3029 ret = LTTCOMM_FATAL;
3030 goto error;
3031 }
48842b30 3032 }
2bdd86d4 3033
edb67388 3034 /* At this point, the session and channel exist on the tracer */
edb67388
DG
3035 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
3036 if (ret != LTTCOMM_OK) {
48842b30 3037 goto error;
2bdd86d4
MD
3038 }
3039 break;
3040 }
d78d6610 3041#if 0
2bdd86d4
MD
3042 case LTTNG_DOMAIN_UST_EXEC_NAME:
3043 case LTTNG_DOMAIN_UST_PID:
3044 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3045#endif
54d01ffb 3046 default:
1ab1ea0b 3047 ret = LTTCOMM_UND;
54d01ffb 3048 goto error;
950131af 3049 }
d36b8583 3050
54d01ffb 3051 ret = LTTCOMM_OK;
d36b8583 3052
54d01ffb
DG
3053error:
3054 return ret;
3055}
7d29a247 3056
54d01ffb
DG
3057/*
3058 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
3059 */
3060static int cmd_enable_event_all(struct ltt_session *session, int domain,
8c9ae521 3061 char *channel_name, int event_type)
54d01ffb
DG
3062{
3063 int ret;
3064 struct ltt_kernel_channel *kchan;
7d29a247 3065
54d01ffb
DG
3066 switch (domain) {
3067 case LTTNG_DOMAIN_KERNEL:
3068 kchan = trace_kernel_get_channel_by_name(channel_name,
3069 session->kernel_session);
3070 if (kchan == NULL) {
3071 /* This call will notify the kernel thread */
44d3bd01
DG
3072 ret = channel_kernel_create(session->kernel_session, NULL,
3073 kernel_poll_pipe[1]);
54d01ffb
DG
3074 if (ret != LTTCOMM_OK) {
3075 goto error;
d36b8583 3076 }
0d0c377a 3077
8f69e5eb
DG
3078 /* Get the newly created kernel channel pointer */
3079 kchan = trace_kernel_get_channel_by_name(channel_name,
3080 session->kernel_session);
3081 if (kchan == NULL) {
3082 /* This sould not happen... */
3083 ret = LTTCOMM_FATAL;
3084 goto error;
3085 }
3086
54d01ffb 3087 }
0fdd1e2c 3088
7a3d1328 3089 switch (event_type) {
76d45b40 3090 case LTTNG_EVENT_SYSCALL:
7a3d1328 3091 ret = event_kernel_enable_all_syscalls(session->kernel_session,
8c9ae521 3092 kchan, kernel_tracer_fd);
7a3d1328 3093 break;
76d45b40 3094 case LTTNG_EVENT_TRACEPOINT:
8c9ae521 3095 /*
7a3d1328
MD
3096 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
3097 * events already registered to the channel.
8c9ae521 3098 */
7a3d1328
MD
3099 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
3100 kchan, kernel_tracer_fd);
3101 break;
76d45b40 3102 case LTTNG_EVENT_ALL:
7a3d1328 3103 /* Enable syscalls and tracepoints */
8c9ae521
DG
3104 ret = event_kernel_enable_all(session->kernel_session,
3105 kchan, kernel_tracer_fd);
7a3d1328
MD
3106 break;
3107 default:
3108 ret = LTTCOMM_KERN_ENABLE_FAIL;
3109 goto error;
8c9ae521 3110 }
8f69e5eb
DG
3111
3112 /* Manage return value */
54d01ffb 3113 if (ret != LTTCOMM_OK) {
0d0c377a 3114 goto error;
d36b8583
DG
3115 }
3116
54d01ffb 3117 kernel_wait_quiescent(kernel_tracer_fd);
d36b8583 3118 break;
76d45b40
DG
3119 case LTTNG_DOMAIN_UST:
3120 {
3121 struct lttng_channel *attr;
3122 struct ltt_ust_channel *uchan;
3123 struct ltt_ust_session *usess = session->ust_session;
3124
3125 /* Get channel from global UST domain */
3126 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
3127 channel_name);
3128 if (uchan == NULL) {
3129 /* Create default channel */
3130 attr = channel_new_default_attr(domain);
3131 if (attr == NULL) {
3132 ret = LTTCOMM_FATAL;
3133 goto error;
3134 }
3135 snprintf(attr->name, NAME_MAX, "%s", channel_name);
3136 attr->name[NAME_MAX - 1] = '\0';
3137
3138 /* Use the internal command enable channel */
7885e399 3139 ret = channel_ust_create(usess, domain, attr);
76d45b40
DG
3140 if (ret != LTTCOMM_OK) {
3141 free(attr);
3142 goto error;
3143 }
3144 free(attr);
3145
3146 /* Get the newly created channel reference back */
3147 uchan = trace_ust_find_channel_by_name(
3148 usess->domain_global.channels, channel_name);
3149 if (uchan == NULL) {
3150 /* Something is really wrong */
3151 ret = LTTCOMM_FATAL;
3152 goto error;
3153 }
3154 }
3155
3156 /* At this point, the session and channel exist on the tracer */
3157
3158 switch (event_type) {
3159 case LTTNG_EVENT_ALL:
3160 case LTTNG_EVENT_TRACEPOINT:
3161 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
3162 if (ret != LTTCOMM_OK) {
3163 goto error;
3164 }
3165 break;
3166 default:
3167 ret = LTTCOMM_UST_ENABLE_FAIL;
3168 goto error;
3169 }
3170
3171 /* Manage return value */
3172 if (ret != LTTCOMM_OK) {
3173 goto error;
3174 }
3175
3176 break;
3177 }
d78d6610 3178#if 0
76d45b40
DG
3179 case LTTNG_DOMAIN_UST_EXEC_NAME:
3180 case LTTNG_DOMAIN_UST_PID:
3181 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 3182#endif
54d01ffb 3183 default:
1ab1ea0b 3184 ret = LTTCOMM_UND;
54d01ffb 3185 goto error;
d36b8583 3186 }
f3ed775e 3187
54d01ffb
DG
3188 ret = LTTCOMM_OK;
3189
3190error:
3191 return ret;
3192}
3193
3194/*
3195 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
3196 */
3197static ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
3198{
3199 int ret;
3200 ssize_t nb_events = 0;
3201
3202 switch (domain) {
3203 case LTTNG_DOMAIN_KERNEL:
3204 nb_events = kernel_list_events(kernel_tracer_fd, events);
3205 if (nb_events < 0) {
3206 ret = LTTCOMM_KERN_LIST_FAIL;
3207 goto error;
894be886 3208 }
54d01ffb 3209 break;
b551a063
DG
3210 case LTTNG_DOMAIN_UST:
3211 nb_events = ust_app_list_events(events);
3212 if (nb_events < 0) {
3213 ret = LTTCOMM_UST_LIST_FAIL;
3214 goto error;
3215 }
3216 break;
54d01ffb 3217 default:
1ab1ea0b 3218 ret = LTTCOMM_UND;
54d01ffb
DG
3219 goto error;
3220 }
894be886 3221
54d01ffb 3222 return nb_events;
7d29a247 3223
54d01ffb
DG
3224error:
3225 /* Return negative value to differentiate return code */
3226 return -ret;
3227}
b389abbe 3228
f37d259d
MD
3229/*
3230 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3231 */
3232static ssize_t cmd_list_tracepoint_fields(int domain,
3233 struct lttng_event_field **fields)
3234{
3235 int ret;
3236 ssize_t nb_fields = 0;
3237
3238 switch (domain) {
3239 case LTTNG_DOMAIN_UST:
3240 nb_fields = ust_app_list_event_fields(fields);
3241 if (nb_fields < 0) {
3242 ret = LTTCOMM_UST_LIST_FAIL;
3243 goto error;
3244 }
3245 break;
3246 case LTTNG_DOMAIN_KERNEL:
3247 default: /* fall-through */
3248 ret = LTTCOMM_UND;
3249 goto error;
3250 }
3251
3252 return nb_fields;
3253
3254error:
3255 /* Return negative value to differentiate return code */
3256 return -ret;
3257}
3258
54d01ffb
DG
3259/*
3260 * Command LTTNG_START_TRACE processed by the client thread.
3261 */
3262static int cmd_start_trace(struct ltt_session *session)
3263{
3264 int ret;
54d01ffb 3265 struct ltt_kernel_session *ksession;
b9d9b220 3266 struct ltt_ust_session *usess;
00e2e675 3267 struct ltt_kernel_channel *kchan;
b389abbe 3268
00e2e675 3269 /* Ease our life a bit ;) */
54d01ffb 3270 ksession = session->kernel_session;
b9d9b220 3271 usess = session->ust_session;
5eb91c98 3272
b9ef1c83 3273 if (session->enabled) {
80e327fa
DG
3274 /* Already started. */
3275 ret = LTTCOMM_TRACE_ALREADY_STARTED;
b9ef1c83
DG
3276 goto error;
3277 }
3278
464dd62d
MD
3279 session->enabled = 1;
3280
00e2e675
DG
3281 ret = setup_relayd(session);
3282 if (ret != LTTCOMM_OK) {
3283 ERR("Error setting up relayd for session %s", session->name);
3284 goto error;
3285 }
3286
54d01ffb
DG
3287 /* Kernel tracing */
3288 if (ksession != NULL) {
3289 /* Open kernel metadata */
3290 if (ksession->metadata == NULL) {
00e2e675
DG
3291 ret = kernel_open_metadata(ksession,
3292 ksession->consumer->dst.trace_path);
54d01ffb
DG
3293 if (ret < 0) {
3294 ret = LTTCOMM_KERN_META_FAIL;
3295 goto error;
b389abbe 3296 }
54d01ffb 3297 }
7d29a247 3298
54d01ffb 3299 /* Open kernel metadata stream */
03550b58 3300 if (ksession->metadata_stream_fd < 0) {
54d01ffb
DG
3301 ret = kernel_open_metadata_stream(ksession);
3302 if (ret < 0) {
3303 ERR("Kernel create metadata stream failed");
3304 ret = LTTCOMM_KERN_STREAM_FAIL;
3305 goto error;
3306 }
3307 }
3308
3309 /* For each channel */
3310 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3311 if (kchan->stream_count == 0) {
3312 ret = kernel_open_channel_stream(kchan);
3313 if (ret < 0) {
3314 ret = LTTCOMM_KERN_STREAM_FAIL;
7d29a247
DG
3315 goto error;
3316 }
54d01ffb
DG
3317 /* Update the stream global counter */
3318 ksession->stream_count_global += ret;
7d29a247 3319 }
54d01ffb
DG
3320 }
3321
3322 /* Setup kernel consumer socket and send fds to it */
3323 ret = init_kernel_tracing(ksession);
3324 if (ret < 0) {
3325 ret = LTTCOMM_KERN_START_FAIL;
3326 goto error;
3327 }
3328
3329 /* This start the kernel tracing */
3330 ret = kernel_start_session(ksession);
3331 if (ret < 0) {
3332 ret = LTTCOMM_KERN_START_FAIL;
3333 goto error;
3334 }
3335
3336 /* Quiescent wait after starting trace */
3337 kernel_wait_quiescent(kernel_tracer_fd);
3338 }
3339
36dc12cc 3340 /* Flag session that trace should start automatically */
b9d9b220
DG
3341 if (usess) {
3342 usess->start_trace = 1;
36dc12cc 3343
b9d9b220
DG
3344 ret = ust_app_start_trace_all(usess);
3345 if (ret < 0) {
3346 ret = LTTCOMM_UST_START_FAIL;
3347 goto error;
3348 }
2bdd86d4 3349 }
54d01ffb
DG
3350
3351 ret = LTTCOMM_OK;
3352
3353error:
3354 return ret;
3355}
3356
3357/*
3358 * Command LTTNG_STOP_TRACE processed by the client thread.
3359 */
3360static int cmd_stop_trace(struct ltt_session *session)
3361{
3362 int ret;
3363 struct ltt_kernel_channel *kchan;
3364 struct ltt_kernel_session *ksession;
8be98f9a 3365 struct ltt_ust_session *usess;
54d01ffb
DG
3366
3367 /* Short cut */
3368 ksession = session->kernel_session;
8be98f9a 3369 usess = session->ust_session;
54d01ffb 3370
b9ef1c83 3371 if (!session->enabled) {
80e327fa 3372 ret = LTTCOMM_TRACE_ALREADY_STOPPED;
b9ef1c83
DG
3373 goto error;
3374 }
3375
464dd62d
MD
3376 session->enabled = 0;
3377
54d01ffb
DG
3378 /* Kernel tracer */
3379 if (ksession != NULL) {
3380 DBG("Stop kernel tracing");
3381
00e2e675
DG
3382 /* Flush metadata if exist */
3383 if (ksession->metadata_stream_fd >= 0) {
3384 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
3385 if (ret < 0) {
3386 ERR("Kernel metadata flush failed");
3387 }
54d01ffb 3388 }
f34daff7 3389
00e2e675 3390 /* Flush all buffers before stopping */
54d01ffb
DG
3391 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
3392 ret = kernel_flush_buffer(kchan);
0d0c377a 3393 if (ret < 0) {
54d01ffb 3394 ERR("Kernel flush buffer error");
7d29a247 3395 }
54d01ffb 3396 }
19e70852 3397
54d01ffb
DG
3398 ret = kernel_stop_session(ksession);
3399 if (ret < 0) {
3400 ret = LTTCOMM_KERN_STOP_FAIL;
19e70852 3401 goto error;
f3ed775e 3402 }
54d01ffb
DG
3403
3404 kernel_wait_quiescent(kernel_tracer_fd);
894be886 3405 }
54d01ffb 3406
8be98f9a
MD
3407 if (usess) {
3408 usess->start_trace = 0;
2bdd86d4 3409
8be98f9a 3410 ret = ust_app_stop_trace_all(usess);
2bdd86d4 3411 if (ret < 0) {
190df5ac 3412 ret = LTTCOMM_UST_STOP_FAIL;
2bdd86d4
MD
3413 goto error;
3414 }
2bdd86d4 3415 }
54d01ffb
DG
3416
3417 ret = LTTCOMM_OK;
3418
3419error:
3420 return ret;
3421}
3422
3423/*
00e2e675 3424 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
54d01ffb 3425 */
00e2e675
DG
3426static int cmd_create_session_uri(char *name, struct lttng_uri *ctrl_uri,
3427 struct lttng_uri *data_uri, unsigned int enable_consumer,
3428 lttng_sock_cred *creds)
54d01ffb
DG
3429{
3430 int ret;
00e2e675
DG
3431 char *path = NULL;
3432 struct ltt_session *session;
3433 struct consumer_output *consumer;
3434
3435 /* Verify if the session already exist */
3436 session = session_find_by_name(name);
3437 if (session != NULL) {
3438 ret = LTTCOMM_EXIST_SESS;
3439 goto error;
3440 }
3441
3442 /* TODO: validate URIs */
3443
3444 /* Create default consumer output */
3445 consumer = consumer_create_output(CONSUMER_DST_LOCAL);
3446 if (consumer == NULL) {
3447 ret = LTTCOMM_FATAL;
3448 goto error;
3449 }
3450 strncpy(consumer->subdir, ctrl_uri->subdir, sizeof(consumer->subdir));
3451 DBG2("Consumer subdir set to %s", consumer->subdir);
3452
3453 switch (ctrl_uri->dtype) {
3454 case LTTNG_DST_IPV4:
3455 case LTTNG_DST_IPV6:
3456 /* Set control URI into consumer output object */
3457 ret = consumer_set_network_uri(consumer, ctrl_uri);
3458 if (ret < 0) {
3459 ret = LTTCOMM_FATAL;
3460 goto error;
3461 }
3462
3463 /* Set data URI into consumer output object */
3464 ret = consumer_set_network_uri(consumer, data_uri);
3465 if (ret < 0) {
3466 ret = LTTCOMM_FATAL;
3467 goto error;
3468 }
3469
3470 /* Empty path since the session is network */
3471 path = "";
3472 break;
3473 case LTTNG_DST_PATH:
3474 /* Very volatile pointer. Only used for the create session. */
3475 path = ctrl_uri->dst.path;
3476 strncpy(consumer->dst.trace_path, path,
3477 sizeof(consumer->dst.trace_path));
3478 break;
3479 }
3480
3481 /* Set if the consumer is enabled or not */
3482 consumer->enabled = enable_consumer;
54d01ffb 3483
730389d9
DG
3484 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
3485 LTTNG_SOCK_GET_GID_CRED(creds));
54d01ffb 3486 if (ret != LTTCOMM_OK) {
00e2e675 3487 goto consumer_error;
54d01ffb
DG
3488 }
3489
00e2e675
DG
3490 /* Get the newly created session pointer back */
3491 session = session_find_by_name(name);
3492 assert(session);
3493
3494 /* Assign consumer to session */
3495 session->consumer = consumer;
3496
3497 return LTTCOMM_OK;
3498
3499consumer_error:
3500 consumer_destroy_output(consumer);
3501error:
3502 return ret;
3503}
3504
3505/*
3506 * Command LTTNG_CREATE_SESSION processed by the client thread.
3507 */
3508static int cmd_create_session(char *name, char *path, lttng_sock_cred *creds)
3509{
3510 int ret;
3511 struct lttng_uri uri;
3512
3513 /* Zeroed temporary URI */
3514 memset(&uri, 0, sizeof(uri));
3515
3516 uri.dtype = LTTNG_DST_PATH;
3517 uri.utype = LTTNG_URI_DST;
3518 strncpy(uri.dst.path, path, sizeof(uri.dst.path));
3519
3520 /* TODO: Strip date-time from path and put it in uri's subdir */
3521
3522 ret = cmd_create_session_uri(name, &uri, NULL, 1, creds);
3523 if (ret != LTTCOMM_OK) {
3524 goto error;
3525 }
54d01ffb
DG
3526
3527error:
3528 return ret;
3529}
3530
3531/*
3532 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3533 */
3534static int cmd_destroy_session(struct ltt_session *session, char *name)
3535{
3536 int ret;
3537
173af62f
DG
3538 /* Safety net */
3539 assert(session);
3540
54d01ffb
DG
3541 /* Clean kernel session teardown */
3542 teardown_kernel_session(session);
84cd17c6
MD
3543 /* UST session teardown */
3544 teardown_ust_session(session);
54d01ffb
DG
3545
3546 /*
3547 * Must notify the kernel thread here to update it's poll setin order
3548 * to remove the channel(s)' fd just destroyed.
3549 */
3550 ret = notify_thread_pipe(kernel_poll_pipe[1]);
3551 if (ret < 0) {
76d7553f 3552 PERROR("write kernel poll pipe");
54d01ffb
DG
3553 }
3554
271933a4 3555 ret = session_destroy(session);
54d01ffb
DG
3556
3557 return ret;
3558}
3559
3560/*
3561 * Command LTTNG_CALIBRATE processed by the client thread.
3562 */
3563static int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
3564{
3565 int ret;
3566
3567 switch (domain) {
3568 case LTTNG_DOMAIN_KERNEL:
33a2b854 3569 {
54d01ffb 3570 struct lttng_kernel_calibrate kcalibrate;
33a2b854 3571
54d01ffb
DG
3572 kcalibrate.type = calibrate->type;
3573 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
33a2b854 3574 if (ret < 0) {
54d01ffb
DG
3575 ret = LTTCOMM_KERN_ENABLE_FAIL;
3576 goto error;
33a2b854 3577 }
54d01ffb
DG
3578 break;
3579 }
4466912f
DG
3580 case LTTNG_DOMAIN_UST:
3581 {
3582 struct lttng_ust_calibrate ucalibrate;
3583
3584 ucalibrate.type = calibrate->type;
3585 ret = ust_app_calibrate_glb(&ucalibrate);
3586 if (ret < 0) {
3587 ret = LTTCOMM_UST_CALIBRATE_FAIL;
3588 goto error;
3589 }
3590 break;
3591 }
54d01ffb 3592 default:
1ab1ea0b 3593 ret = LTTCOMM_UND;
54d01ffb
DG
3594 goto error;
3595 }
33a2b854 3596
54d01ffb 3597 ret = LTTCOMM_OK;
33a2b854 3598
54d01ffb
DG
3599error:
3600 return ret;
3601}
7d29a247 3602
54d01ffb
DG
3603/*
3604 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3605 */
3606static int cmd_register_consumer(struct ltt_session *session, int domain,
3607 char *sock_path)
3608{
3609 int ret, sock;
173af62f 3610 struct consumer_socket *socket;
b389abbe 3611
54d01ffb
DG
3612 switch (domain) {
3613 case LTTNG_DOMAIN_KERNEL:
3614 /* Can't register a consumer if there is already one */
48c4f28c 3615 if (session->kernel_session->consumer_fds_sent != 0) {
54d01ffb
DG
3616 ret = LTTCOMM_KERN_CONSUMER_FAIL;
3617 goto error;
3618 }
3619
3620 sock = lttcomm_connect_unix_sock(sock_path);
3621 if (sock < 0) {
3622 ret = LTTCOMM_CONNECT_FAIL;
3623 goto error;
3624 }
3625
173af62f
DG
3626 socket = consumer_allocate_socket(sock);
3627 if (socket == NULL) {
3628 ret = LTTCOMM_FATAL;
3629 close(sock);
3630 goto error;
3631 }
3632
3633 rcu_read_lock();
3634 consumer_add_socket(socket, session->kernel_session->consumer);
3635 rcu_read_unlock();
3636
54d01ffb
DG
3637 break;
3638 default:
3639 /* TODO: Userspace tracing */
1ab1ea0b 3640 ret = LTTCOMM_UND;
54d01ffb
DG
3641 goto error;
3642 }
3643
3644 ret = LTTCOMM_OK;
3645
3646error:
3647 return ret;
3648}
3649
3650/*
3651 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3652 */
3653static ssize_t cmd_list_domains(struct ltt_session *session,
3654 struct lttng_domain **domains)
3655{
b551a063 3656 int ret, index = 0;
54d01ffb
DG
3657 ssize_t nb_dom = 0;
3658
3659 if (session->kernel_session != NULL) {
b551a063 3660 DBG3("Listing domains found kernel domain");
54d01ffb
DG
3661 nb_dom++;
3662 }
3663
b551a063
DG
3664 if (session->ust_session != NULL) {
3665 DBG3("Listing domains found UST global domain");
3666 nb_dom++;
3667 }
54d01ffb 3668
b551a063 3669 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
54d01ffb
DG
3670 if (*domains == NULL) {
3671 ret = -LTTCOMM_FATAL;
3672 goto error;
3673 }
3674
b551a063
DG
3675 if (session->kernel_session != NULL) {
3676 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3677 index++;
3678 }
3679
3680 if (session->ust_session != NULL) {
3681 (*domains)[index].type = LTTNG_DOMAIN_UST;
3682 index++;
3683 }
54d01ffb 3684
54d01ffb
DG
3685 return nb_dom;
3686
3687error:
3688 return ret;
3689}
3690
3691/*
3692 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3693 */
b551a063 3694static ssize_t cmd_list_channels(int domain, struct ltt_session *session,
54d01ffb
DG
3695 struct lttng_channel **channels)
3696{
3697 int ret;
3698 ssize_t nb_chan = 0;
3699
b551a063
DG
3700 switch (domain) {
3701 case LTTNG_DOMAIN_KERNEL:
3702 if (session->kernel_session != NULL) {
3703 nb_chan = session->kernel_session->channel_count;
3704 }
0df502fd 3705 DBG3("Number of kernel channels %zd", nb_chan);
b551a063
DG
3706 break;
3707 case LTTNG_DOMAIN_UST:
3708 if (session->ust_session != NULL) {
bec39940 3709 nb_chan = lttng_ht_get_count(
b551a063
DG
3710 session->ust_session->domain_global.channels);
3711 }
0df502fd 3712 DBG3("Number of UST global channels %zd", nb_chan);
b551a063
DG
3713 break;
3714 default:
3715 *channels = NULL;
1ab1ea0b 3716 ret = -LTTCOMM_UND;
54d01ffb
DG
3717 goto error;
3718 }
3719
b551a063
DG
3720 if (nb_chan > 0) {
3721 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
3722 if (*channels == NULL) {
3723 ret = -LTTCOMM_FATAL;
3724 goto error;
3725 }
54d01ffb 3726
b551a063
DG
3727 list_lttng_channels(domain, session, *channels);
3728 } else {
3729 *channels = NULL;
3730 }
2bdd86d4 3731
54d01ffb
DG
3732 return nb_chan;
3733
3734error:
3735 return ret;
3736}
3737
3738/*
3739 * Command LTTNG_LIST_EVENTS processed by the client thread.
3740 */
b551a063 3741static ssize_t cmd_list_events(int domain, struct ltt_session *session,
54d01ffb
DG
3742 char *channel_name, struct lttng_event **events)
3743{
b551a063 3744 int ret = 0;
54d01ffb 3745 ssize_t nb_event = 0;
54d01ffb 3746
b551a063
DG
3747 switch (domain) {
3748 case LTTNG_DOMAIN_KERNEL:
3749 if (session->kernel_session != NULL) {
3750 nb_event = list_lttng_kernel_events(channel_name,
3751 session->kernel_session, events);
54d01ffb 3752 }
b551a063
DG
3753 break;
3754 case LTTNG_DOMAIN_UST:
3755 {
3756 if (session->ust_session != NULL) {
3757 nb_event = list_lttng_ust_global_events(channel_name,
3758 &session->ust_session->domain_global, events);
3759 }
3760 break;
54d01ffb 3761 }
b551a063 3762 default:
1ab1ea0b 3763 ret = -LTTCOMM_UND;
54d01ffb
DG
3764 goto error;
3765 }
3766
b551a063 3767 ret = nb_event;
54d01ffb
DG
3768
3769error:
3770 return ret;
3771}
3772
00e2e675
DG
3773/*
3774 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3775 */
3776static int cmd_set_consumer_uri(int domain, struct ltt_session *session,
3777 struct lttng_uri *uri)
3778{
3779 int ret;
3780 struct ltt_kernel_session *ksess = session->kernel_session;
3781 struct ltt_ust_session *usess = session->ust_session;
3782 struct consumer_output *consumer;
3783
3784 /* Can't enable consumer after session started. */
3785 if (session->enabled) {
3786 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3787 goto error;
3788 }
3789
3790 switch (domain) {
3791 case LTTNG_DOMAIN_KERNEL:
173af62f
DG
3792 {
3793 struct lttng_ht_iter iter;
3794 struct consumer_socket *socket;
3795
00e2e675
DG
3796 /* Code flow error if we don't have a kernel session here. */
3797 assert(ksess);
3798
3799 /* Create consumer output if none exists */
3800 consumer = ksess->tmp_consumer;
3801 if (consumer == NULL) {
3802 consumer = consumer_copy_output(ksess->consumer);
3803 if (consumer == NULL) {
3804 ret = LTTCOMM_FATAL;
3805 goto error;
3806 }
3807 /* Reassign new pointer */
3808 ksess->tmp_consumer = consumer;
3809 }
3810
3811 switch (uri->dtype) {
3812 case LTTNG_DST_IPV4:
3813 case LTTNG_DST_IPV6:
3814 DBG2("Setting network URI for kernel session %s", session->name);
3815
3816 /* Set URI into consumer output object */
3817 ret = consumer_set_network_uri(consumer, uri);
3818 if (ret < 0) {
3819 ret = LTTCOMM_FATAL;
3820 goto error;
3821 }
3822
3823 /* On a new subdir, reappend the default trace dir. */
3824 if (strlen(uri->subdir) != 0) {
3825 strncat(consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
3826 sizeof(consumer->subdir));
3827 }
3828
173af62f
DG
3829 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter,
3830 socket, node.node) {
3831 /* Code flow error */
3832 assert(socket->fd >= 0);
3833
3834 pthread_mutex_lock(socket->lock);
3835 ret = send_socket_relayd_consumer(domain, session, uri, consumer,
3836 socket->fd);
3837 pthread_mutex_unlock(socket->lock);
3838 if (ret != LTTCOMM_OK) {
3839 goto error;
3840 }
00e2e675 3841 }
173af62f 3842
00e2e675
DG
3843 break;
3844 case LTTNG_DST_PATH:
3845 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
3846 memset(consumer->dst.trace_path, 0,
3847 sizeof(consumer->dst.trace_path));
3848 strncpy(consumer->dst.trace_path, uri->dst.path,
3849 sizeof(consumer->dst.trace_path));
3850 /* Append default kernel trace dir */
3851 strncat(consumer->dst.trace_path, DEFAULT_KERNEL_TRACE_DIR,
3852 sizeof(consumer->dst.trace_path));
3853 break;
3854 }
3855
3856 /* All good! */
3857 break;
173af62f 3858 }
00e2e675
DG
3859 case LTTNG_DOMAIN_UST:
3860 /* Code flow error if we don't have a kernel session here. */
3861 assert(usess);
3862
3863 /* Create consumer output if none exists */
3864 consumer = usess->tmp_consumer;
3865 if (consumer == NULL) {
3866 consumer = consumer_copy_output(usess->consumer);
3867 if (consumer == NULL) {
3868 ret = LTTCOMM_FATAL;
3869 goto error;
3870 }
3871 /* Reassign new pointer */
3872 usess->tmp_consumer = consumer;
3873 }
3874
3875 switch (uri->dtype) {
3876 case LTTNG_DST_IPV4:
3877 case LTTNG_DST_IPV6:
3878 {
173af62f
DG
3879 struct consumer_socket *socket;
3880
00e2e675
DG
3881 DBG2("Setting network URI for UST session %s", session->name);
3882
3883 /* Set URI into consumer object */
3884 ret = consumer_set_network_uri(consumer, uri);
3885 if (ret < 0) {
3886 ret = LTTCOMM_FATAL;
3887 goto error;
3888 }
3889
3890 /* On a new subdir, reappend the default trace dir. */
3891 if (strlen(uri->subdir) != 0) {
3892 strncat(consumer->subdir, DEFAULT_UST_TRACE_DIR,
3893 sizeof(consumer->subdir));
3894 }
3895
173af62f
DG
3896 rcu_read_lock();
3897 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
3898 consumer);
3899 if (socket != NULL) {
3900 pthread_mutex_lock(socket->lock);
00e2e675 3901 ret = send_socket_relayd_consumer(domain, session, uri,
173af62f
DG
3902 consumer, socket->fd);
3903 pthread_mutex_unlock(socket->lock);
00e2e675
DG
3904 if (ret != LTTCOMM_OK) {
3905 goto error;
3906 }
3907 }
3908
173af62f
DG
3909 socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd),
3910 consumer);
3911 if (socket != NULL) {
3912 pthread_mutex_lock(socket->lock);
00e2e675 3913 ret = send_socket_relayd_consumer(domain, session, uri,
173af62f
DG
3914 consumer, socket->fd);
3915 pthread_mutex_unlock(socket->lock);
00e2e675
DG
3916 if (ret != LTTCOMM_OK) {
3917 goto error;
3918 }
3919 }
173af62f 3920 rcu_read_unlock();
00e2e675
DG
3921 break;
3922 }
3923 case LTTNG_DST_PATH:
3924 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
3925 memset(consumer->dst.trace_path, 0,
3926 sizeof(consumer->dst.trace_path));
3927 strncpy(consumer->dst.trace_path, uri->dst.path,
3928 sizeof(consumer->dst.trace_path));
3929 /* Append default UST trace dir */
3930 strncat(consumer->dst.trace_path, DEFAULT_UST_TRACE_DIR,
3931 sizeof(consumer->dst.trace_path));
3932 break;
3933 }
3934 break;
3935 }
3936
3937 /* All good! */
3938 ret = LTTCOMM_OK;
3939
3940error:
3941 return ret;
3942}
3943
3944/*
3945 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
3946 */
3947static int cmd_disable_consumer(int domain, struct ltt_session *session)
3948{
3949 int ret;
3950 struct ltt_kernel_session *ksess = session->kernel_session;
3951 struct ltt_ust_session *usess = session->ust_session;
3952 struct consumer_output *consumer;
3953
3954 if (session->enabled) {
3955 /* Can't disable consumer on an already started session */
3956 ret = LTTCOMM_TRACE_ALREADY_STARTED;
3957 goto error;
3958 }
3959
3960 switch (domain) {
3961 case LTTNG_DOMAIN_KERNEL:
3962 /* Code flow error if we don't have a kernel session here. */
3963 assert(ksess);
3964
3965 DBG("Disabling kernel consumer");
3966 consumer = ksess->consumer;
3967
3968 break;
3969 case LTTNG_DOMAIN_UST:
3970 /* Code flow error if we don't have a UST session here. */
3971 assert(usess);
3972
3973 DBG("Disabling UST consumer");
3974 consumer = usess->consumer;
3975
3976 break;
3977 default:
3978 ret = LTTCOMM_UNKNOWN_DOMAIN;
3979 goto error;
3980 }
3981
3982 assert(consumer);
3983 consumer->enabled = 0;
3984
3985 /* Success at this point */
3986 ret = LTTCOMM_OK;
3987
3988error:
3989 return ret;
3990}
3991
3992/*
3993 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
3994 */
3995static int cmd_enable_consumer(int domain, struct ltt_session *session)
3996{
3997 int ret;
3998 struct ltt_kernel_session *ksess = session->kernel_session;
3999 struct ltt_ust_session *usess = session->ust_session;
4000 struct consumer_output *tmp_out;
4001
4002 /* Can't enable consumer after session started. */
4003 if (session->enabled) {
4004 ret = LTTCOMM_TRACE_ALREADY_STARTED;
4005 goto error;
4006 }
4007
4008 switch (domain) {
4009 case LTTNG_DOMAIN_KERNEL:
4010 /* Code flow error if we don't have a kernel session here. */
4011 assert(ksess);
4012
4013 /*
4014 * Check if we have already sent fds to the consumer. In that case,
4015 * the enable-consumer command can't be used because a start trace
4016 * had previously occured.
4017 */
4018 if (ksess->consumer_fds_sent) {
4019 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4020 goto error;
4021 }
4022
4023 tmp_out = ksess->tmp_consumer;
4024 if (tmp_out == NULL) {
4025 /* No temp. consumer output exists. Using the current one. */
4026 DBG3("No temporary consumer. Using default");
4027 ret = LTTCOMM_OK;
4028 goto error;
4029 }
4030
4031 switch (tmp_out->type) {
4032 case CONSUMER_DST_LOCAL:
4033 DBG2("Consumer output is local. Creating directory(ies)");
4034
4035 /* Create directory(ies) */
4036 ret = run_as_mkdir_recursive(tmp_out->dst.trace_path,
4037 S_IRWXU | S_IRWXG, session->uid, session->gid);
4038 if (ret < 0) {
4039 if (ret != -EEXIST) {
4040 ERR("Trace directory creation error");
4041 ret = LTTCOMM_FATAL;
4042 goto error;
4043 }
4044 }
4045 break;
4046 case CONSUMER_DST_NET:
4047 DBG2("Consumer output is network. Validating URIs");
4048 /* Validate if we have both control and data path set. */
4049 if (!tmp_out->dst.net.control_isset) {
4050 ret = LTTCOMM_URI_CTRL_MISS;
4051 goto error;
4052 }
4053
4054 if (!tmp_out->dst.net.data_isset) {
4055 ret = LTTCOMM_URI_DATA_MISS;
4056 goto error;
4057 }
4058
4059 /* Check established network session state */
4060 if (session->net_handle == 0) {
4061 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4062 ERR("Session network handle is not set on enable-consumer");
4063 goto error;
4064 }
4065
4066 /* Append default kernel trace dir to subdir */
4067 strncat(ksess->consumer->subdir, DEFAULT_KERNEL_TRACE_DIR,
4068 sizeof(ksess->consumer->subdir));
4069
4070 break;
4071 }
4072
4073 /*
4074 * @session-lock
4075 * This is race free for now since the session lock is acquired before
4076 * ending up in this function. No other threads can access this kernel
4077 * session without this lock hence freeing the consumer output object
4078 * is valid.
4079 */
4080 consumer_destroy_output(ksess->consumer);
4081 ksess->consumer = tmp_out;
4082 ksess->tmp_consumer = NULL;
4083
4084 break;
4085 case LTTNG_DOMAIN_UST:
4086 /* Code flow error if we don't have a UST session here. */
4087 assert(usess);
4088
4089 /*
4090 * Check if we have already sent fds to the consumer. In that case,
4091 * the enable-consumer command can't be used because a start trace
4092 * had previously occured.
4093 */
4094 if (usess->start_trace) {
4095 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4096 goto error;
4097 }
4098
4099 tmp_out = usess->tmp_consumer;
4100 if (tmp_out == NULL) {
4101 /* No temp. consumer output exists. Using the current one. */
4102 DBG3("No temporary consumer. Using default");
4103 ret = LTTCOMM_OK;
4104 goto error;
4105 }
4106
4107 switch (tmp_out->type) {
4108 case CONSUMER_DST_LOCAL:
4109 DBG2("Consumer output is local. Creating directory(ies)");
4110
4111 /* Create directory(ies) */
4112 ret = run_as_mkdir_recursive(tmp_out->dst.trace_path,
4113 S_IRWXU | S_IRWXG, session->uid, session->gid);
4114 if (ret < 0) {
4115 if (ret != -EEXIST) {
4116 ERR("Trace directory creation error");
4117 ret = LTTCOMM_FATAL;
4118 goto error;
4119 }
4120 }
4121 break;
4122 case CONSUMER_DST_NET:
4123 DBG2("Consumer output is network. Validating URIs");
4124 /* Validate if we have both control and data path set. */
4125 if (!tmp_out->dst.net.control_isset) {
4126 ret = LTTCOMM_URI_CTRL_MISS;
4127 goto error;
4128 }
4129
4130 if (!tmp_out->dst.net.data_isset) {
4131 ret = LTTCOMM_URI_DATA_MISS;
4132 goto error;
4133 }
4134
4135 /* Check established network session state */
4136 if (session->net_handle == 0) {
4137 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4138 DBG2("Session network handle is not set on enable-consumer");
4139 goto error;
4140 }
4141
4142 if (tmp_out->net_seq_index == -1) {
4143 ret = LTTCOMM_ENABLE_CONSUMER_FAIL;
4144 DBG2("Network index is not set on the consumer");
4145 goto error;
4146 }
4147
4148 /* Append default kernel trace dir to subdir */
4149 strncat(usess->consumer->subdir, DEFAULT_UST_TRACE_DIR,
4150 sizeof(usess->consumer->subdir));
4151
4152 break;
4153 }
4154
4155 /*
4156 * @session-lock
4157 * This is race free for now since the session lock is acquired before
4158 * ending up in this function. No other threads can access this kernel
4159 * session without this lock hence freeing the consumer output object
4160 * is valid.
4161 */
4162 consumer_destroy_output(usess->consumer);
4163 usess->consumer = tmp_out;
4164 usess->tmp_consumer = NULL;
4165
4166 break;
4167 }
4168
4169 /* Success at this point */
4170 ret = LTTCOMM_OK;
4171
4172error:
4173 return ret;
4174}
4175
54d01ffb
DG
4176/*
4177 * Process the command requested by the lttng client within the command
4178 * context structure. This function make sure that the return structure (llm)
4179 * is set and ready for transmission before returning.
4180 *
4181 * Return any error encountered or 0 for success.
53a80697
MD
4182 *
4183 * "sock" is only used for special-case var. len data.
54d01ffb 4184 */
53a80697
MD
4185static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
4186 int *sock_error)
54d01ffb
DG
4187{
4188 int ret = LTTCOMM_OK;
44d3bd01 4189 int need_tracing_session = 1;
2e09ba09 4190 int need_domain;
54d01ffb
DG
4191
4192 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
4193
53a80697
MD
4194 *sock_error = 0;
4195
2e09ba09
MD
4196 switch (cmd_ctx->lsm->cmd_type) {
4197 case LTTNG_CREATE_SESSION:
00e2e675 4198 case LTTNG_CREATE_SESSION_URI:
2e09ba09
MD
4199 case LTTNG_DESTROY_SESSION:
4200 case LTTNG_LIST_SESSIONS:
4201 case LTTNG_LIST_DOMAINS:
4202 case LTTNG_START_TRACE:
4203 case LTTNG_STOP_TRACE:
4204 need_domain = 0;
3aace903 4205 break;
2e09ba09
MD
4206 default:
4207 need_domain = 1;
4208 }
4209
4210 if (opt_no_kernel && need_domain
4211 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
531d29f9
MD
4212 if (!is_root) {
4213 ret = LTTCOMM_NEED_ROOT_SESSIOND;
4214 } else {
4215 ret = LTTCOMM_KERN_NA;
4216 }
4fba7219
DG
4217 goto error;
4218 }
4219
54d01ffb
DG
4220 /*
4221 * Check for command that don't needs to allocate a returned payload. We do
44d3bd01 4222 * this here so we don't have to make the call for no payload at each
54d01ffb
DG
4223 * command.
4224 */
4225 switch(cmd_ctx->lsm->cmd_type) {
4226 case LTTNG_LIST_SESSIONS:
4227 case LTTNG_LIST_TRACEPOINTS:
f37d259d 4228 case LTTNG_LIST_TRACEPOINT_FIELDS:
54d01ffb
DG
4229 case LTTNG_LIST_DOMAINS:
4230 case LTTNG_LIST_CHANNELS:
4231 case LTTNG_LIST_EVENTS:
4232 break;
4233 default:
4234 /* Setup lttng message with no payload */
4235 ret = setup_lttng_msg(cmd_ctx, 0);
4236 if (ret < 0) {
4237 /* This label does not try to unlock the session */
4238 goto init_setup_error;
4239 }
4240 }
4241
4242 /* Commands that DO NOT need a session. */
4243 switch (cmd_ctx->lsm->cmd_type) {
54d01ffb 4244 case LTTNG_CREATE_SESSION:
00e2e675 4245 case LTTNG_CREATE_SESSION_URI:
2e09ba09 4246 case LTTNG_CALIBRATE:
54d01ffb
DG
4247 case LTTNG_LIST_SESSIONS:
4248 case LTTNG_LIST_TRACEPOINTS:
f37d259d 4249 case LTTNG_LIST_TRACEPOINT_FIELDS:
44d3bd01 4250 need_tracing_session = 0;
54d01ffb
DG
4251 break;
4252 default:
4253 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
256a5576
MD
4254 /*
4255 * We keep the session list lock across _all_ commands
4256 * for now, because the per-session lock does not
4257 * handle teardown properly.
4258 */
74babd95 4259 session_lock_list();
54d01ffb
DG
4260 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
4261 if (cmd_ctx->session == NULL) {
4262 if (cmd_ctx->lsm->session.name != NULL) {
4263 ret = LTTCOMM_SESS_NOT_FOUND;
4264 } else {
4265 /* If no session name specified */
4266 ret = LTTCOMM_SELECT_SESS;
4267 }
4268 goto error;
4269 } else {
4270 /* Acquire lock for the session */
4271 session_lock(cmd_ctx->session);
4272 }
4273 break;
4274 }
b389abbe 4275
2e09ba09
MD
4276 if (!need_domain) {
4277 goto skip_domain;
4278 }
54d01ffb
DG
4279 /*
4280 * Check domain type for specific "pre-action".
4281 */
4282 switch (cmd_ctx->lsm->domain.type) {
4283 case LTTNG_DOMAIN_KERNEL:
d1f1c568 4284 if (!is_root) {
531d29f9 4285 ret = LTTCOMM_NEED_ROOT_SESSIOND;
d1f1c568
DG
4286 goto error;
4287 }
4288
54d01ffb 4289 /* Kernel tracer check */
a4b35e07 4290 if (kernel_tracer_fd == -1) {
54d01ffb 4291 /* Basically, load kernel tracer modules */
096102bd
DG
4292 ret = init_kernel_tracer();
4293 if (ret != 0) {
54d01ffb
DG
4294 goto error;
4295 }
4296 }
5eb91c98 4297
5c827ce0
DG
4298 /* Consumer is in an ERROR state. Report back to client */
4299 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
4300 ret = LTTCOMM_NO_KERNCONSUMERD;
4301 goto error;
4302 }
4303
54d01ffb 4304 /* Need a session for kernel command */
44d3bd01 4305 if (need_tracing_session) {
173af62f
DG
4306 struct consumer_socket *socket;
4307
54d01ffb 4308 if (cmd_ctx->session->kernel_session == NULL) {
6df2e2c9 4309 ret = create_kernel_session(cmd_ctx->session);
5eb91c98 4310 if (ret < 0) {
54d01ffb 4311 ret = LTTCOMM_KERN_SESS_FAIL;
5eb91c98
DG
4312 goto error;
4313 }
b389abbe 4314 }
7d29a247 4315
54d01ffb 4316 /* Start the kernel consumer daemon */
3bd1e081
MD
4317 pthread_mutex_lock(&kconsumer_data.pid_mutex);
4318 if (kconsumer_data.pid == 0 &&
54d01ffb 4319 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
3bd1e081
MD
4320 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
4321 ret = start_consumerd(&kconsumer_data);
7d29a247 4322 if (ret < 0) {
54d01ffb
DG
4323 ret = LTTCOMM_KERN_CONSUMER_FAIL;
4324 goto error;
950131af 4325 }
5c827ce0 4326 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
3ff2ecac
MD
4327 } else {
4328 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
33a2b854 4329 }
173af62f
DG
4330
4331 /* Set kernel consumer socket fd */
4332 if (kconsumer_data.cmd_sock >= 0) {
4333 rcu_read_lock();
4334 socket = consumer_find_socket(kconsumer_data.cmd_sock,
4335 cmd_ctx->session->kernel_session->consumer);
4336 rcu_read_unlock();
4337 if (socket == NULL) {
4338 socket = consumer_allocate_socket(kconsumer_data.cmd_sock);
4339 if (socket == NULL) {
4340 goto error;
4341 }
4342
4343 socket->lock = &kconsumer_data.lock;
4344 rcu_read_lock();
4345 consumer_add_socket(socket,
4346 cmd_ctx->session->kernel_session->consumer);
4347 rcu_read_unlock();
4348 }
4349 }
0d0c377a 4350 }
5c827ce0 4351
54d01ffb 4352 break;
2bdd86d4 4353 case LTTNG_DOMAIN_UST:
44d3bd01 4354 {
5c827ce0
DG
4355 /* Consumer is in an ERROR state. Report back to client */
4356 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
4357 ret = LTTCOMM_NO_USTCONSUMERD;
4358 goto error;
4359 }
4360
44d3bd01 4361 if (need_tracing_session) {
173af62f
DG
4362 struct consumer_socket *socket;
4363
f6a9efaa 4364 if (cmd_ctx->session->ust_session == NULL) {
44d3bd01 4365 ret = create_ust_session(cmd_ctx->session,
6df2e2c9 4366 &cmd_ctx->lsm->domain);
44d3bd01
DG
4367 if (ret != LTTCOMM_OK) {
4368 goto error;
4369 }
4370 }
00e2e675 4371
7753dea8
MD
4372 /* Start the UST consumer daemons */
4373 /* 64-bit */
4374 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
fc7a59ce 4375 if (consumerd64_bin[0] != '\0' &&
7753dea8 4376 ustconsumer64_data.pid == 0 &&
2bdd86d4 4377 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
7753dea8
MD
4378 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4379 ret = start_consumerd(&ustconsumer64_data);
2bdd86d4 4380 if (ret < 0) {
7753dea8 4381 ret = LTTCOMM_UST_CONSUMER64_FAIL;
173af62f 4382 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2bdd86d4
MD
4383 goto error;
4384 }
48842b30 4385
173af62f 4386 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
5c827ce0 4387 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
3ff2ecac 4388 } else {
7753dea8
MD
4389 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
4390 }
173af62f
DG
4391
4392 /*
4393 * Setup socket for consumer 64 bit. No need for atomic access
4394 * since it was set above and can ONLY be set in this thread.
4395 */
4396 if (ust_consumerd64_fd >= 0) {
4397 rcu_read_lock();
4398 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
4399 cmd_ctx->session->ust_session->consumer);
4400 rcu_read_unlock();
4401 if (socket == NULL) {
4402 socket = consumer_allocate_socket(ust_consumerd64_fd);
4403 if (socket == NULL) {
4404 goto error;
4405 }
4406 socket->lock = &ustconsumer32_data.lock;
4407
4408 rcu_read_lock();
4409 consumer_add_socket(socket,
4410 cmd_ctx->session->ust_session->consumer);
4411 rcu_read_unlock();
4412 }
4413 DBG3("UST consumer 64 bit socket set to %d", socket->fd);
4414 }
4415
7753dea8 4416 /* 32-bit */
fc7a59ce 4417 if (consumerd32_bin[0] != '\0' &&
7753dea8
MD
4418 ustconsumer32_data.pid == 0 &&
4419 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
4420 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
4421 ret = start_consumerd(&ustconsumer32_data);
4422 if (ret < 0) {
4423 ret = LTTCOMM_UST_CONSUMER32_FAIL;
173af62f 4424 uatomic_set(&ust_consumerd32_fd, -EINVAL);
7753dea8
MD
4425 goto error;
4426 }
5c827ce0 4427
173af62f 4428 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
5c827ce0 4429 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
7753dea8
MD
4430 } else {
4431 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2bdd86d4 4432 }
173af62f
DG
4433
4434 /*
4435 * Setup socket for consumer 64 bit. No need for atomic access
4436 * since it was set above and can ONLY be set in this thread.
4437 */
4438 if (ust_consumerd32_fd >= 0) {
4439 rcu_read_lock();
4440 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
4441 cmd_ctx->session->ust_session->consumer);
4442 rcu_read_unlock();
4443 if (socket == NULL) {
4444 socket = consumer_allocate_socket(ust_consumerd32_fd);
4445 if (socket == NULL) {
4446 goto error;
4447 }
4448 socket->lock = &ustconsumer32_data.lock;
4449
4450 rcu_read_lock();
4451 consumer_add_socket(socket,
4452 cmd_ctx->session->ust_session->consumer);
4453 rcu_read_unlock();
4454 }
4455 DBG3("UST consumer 32 bit socket set to %d", socket->fd);
4456 }
44d3bd01
DG
4457 }
4458 break;
48842b30 4459 }
54d01ffb 4460 default:
54d01ffb
DG
4461 break;
4462 }
2e09ba09 4463skip_domain:
33a2b854 4464
5c827ce0
DG
4465 /* Validate consumer daemon state when start/stop trace command */
4466 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
4467 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
4468 switch (cmd_ctx->lsm->domain.type) {
4469 case LTTNG_DOMAIN_UST:
4470 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
4471 ret = LTTCOMM_NO_USTCONSUMERD;
4472 goto error;
4473 }
4474 break;
4475 case LTTNG_DOMAIN_KERNEL:
4476 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
4477 ret = LTTCOMM_NO_KERNCONSUMERD;
4478 goto error;
4479 }
4480 break;
4481 }
4482 }
4483
8e0af1b4
MD
4484 /*
4485 * Check that the UID or GID match that of the tracing session.
4486 * The root user can interact with all sessions.
4487 */
4488 if (need_tracing_session) {
4489 if (!session_access_ok(cmd_ctx->session,
730389d9
DG
4490 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4491 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
8e0af1b4
MD
4492 ret = LTTCOMM_EPERM;
4493 goto error;
4494 }
4495 }
4496
54d01ffb
DG
4497 /* Process by command type */
4498 switch (cmd_ctx->lsm->cmd_type) {
4499 case LTTNG_ADD_CONTEXT:
4500 {
4501 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4502 cmd_ctx->lsm->u.context.channel_name,
4503 cmd_ctx->lsm->u.context.event_name,
4504 &cmd_ctx->lsm->u.context.ctx);
4505 break;
4506 }
4507 case LTTNG_DISABLE_CHANNEL:
4508 {
4509 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4510 cmd_ctx->lsm->u.disable.channel_name);
4511 break;
4512 }
4513 case LTTNG_DISABLE_EVENT:
4514 {
4515 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4516 cmd_ctx->lsm->u.disable.channel_name,
4517 cmd_ctx->lsm->u.disable.name);
33a2b854
DG
4518 break;
4519 }
54d01ffb
DG
4520 case LTTNG_DISABLE_ALL_EVENT:
4521 {
2bdd86d4 4522 DBG("Disabling all events");
54d01ffb
DG
4523
4524 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4525 cmd_ctx->lsm->u.disable.channel_name);
4526 break;
4527 }
00e2e675
DG
4528 case LTTNG_DISABLE_CONSUMER:
4529 {
4530 ret = cmd_disable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4531 break;
4532 }
54d01ffb
DG
4533 case LTTNG_ENABLE_CHANNEL:
4534 {
aea829b3 4535 ret = cmd_enable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
54d01ffb
DG
4536 &cmd_ctx->lsm->u.channel.chan);
4537 break;
4538 }
00e2e675
DG
4539 case LTTNG_ENABLE_CONSUMER:
4540 {
4541 ret = cmd_enable_consumer(cmd_ctx->lsm->domain.type, cmd_ctx->session);
4542 break;
4543 }
54d01ffb
DG
4544 case LTTNG_ENABLE_EVENT:
4545 {
4546 ret = cmd_enable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4547 cmd_ctx->lsm->u.enable.channel_name,
4548 &cmd_ctx->lsm->u.enable.event);
4549 break;
4550 }
4551 case LTTNG_ENABLE_ALL_EVENT:
4552 {
2bdd86d4 4553 DBG("Enabling all events");
54d01ffb
DG
4554
4555 ret = cmd_enable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
8c9ae521
DG
4556 cmd_ctx->lsm->u.enable.channel_name,
4557 cmd_ctx->lsm->u.enable.event.type);
54d01ffb
DG
4558 break;
4559 }
052da939 4560 case LTTNG_LIST_TRACEPOINTS:
2ef84c95 4561 {
9f19cc17 4562 struct lttng_event *events;
54d01ffb 4563 ssize_t nb_events;
052da939 4564
54d01ffb
DG
4565 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
4566 if (nb_events < 0) {
4567 ret = -nb_events;
4568 goto error;
2ef84c95
DG
4569 }
4570
4571 /*
4572 * Setup lttng message with payload size set to the event list size in
4573 * bytes and then copy list into the llm payload.
4574 */
052da939 4575 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
2ef84c95 4576 if (ret < 0) {
052da939 4577 free(events);
2ef84c95
DG
4578 goto setup_error;
4579 }
4580
4581 /* Copy event list into message payload */
9f19cc17 4582 memcpy(cmd_ctx->llm->payload, events,
052da939 4583 sizeof(struct lttng_event) * nb_events);
2ef84c95 4584
9f19cc17 4585 free(events);
2ef84c95
DG
4586
4587 ret = LTTCOMM_OK;
4588 break;
4589 }
f37d259d
MD
4590 case LTTNG_LIST_TRACEPOINT_FIELDS:
4591 {
4592 struct lttng_event_field *fields;
4593 ssize_t nb_fields;
4594
4595 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type, &fields);
4596 if (nb_fields < 0) {
4597 ret = -nb_fields;
4598 goto error;
4599 }
4600
4601 /*
4602 * Setup lttng message with payload size set to the event list size in
4603 * bytes and then copy list into the llm payload.
4604 */
4605 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event_field) * nb_fields);
4606 if (ret < 0) {
4607 free(fields);
4608 goto setup_error;
4609 }
4610
4611 /* Copy event list into message payload */
4612 memcpy(cmd_ctx->llm->payload, fields,
4613 sizeof(struct lttng_event_field) * nb_fields);
4614
4615 free(fields);
4616
4617 ret = LTTCOMM_OK;
4618 break;
4619 }
00e2e675
DG
4620 case LTTNG_SET_CONSUMER_URI:
4621 {
4622 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
4623 &cmd_ctx->lsm->u.uri);
4624 break;
4625 }
f3ed775e 4626 case LTTNG_START_TRACE:
8c0faa1d 4627 {
54d01ffb 4628 ret = cmd_start_trace(cmd_ctx->session);
8c0faa1d
DG
4629 break;
4630 }
f3ed775e 4631 case LTTNG_STOP_TRACE:
8c0faa1d 4632 {
54d01ffb 4633 ret = cmd_stop_trace(cmd_ctx->session);
8c0faa1d
DG
4634 break;
4635 }
5e16da05
MD
4636 case LTTNG_CREATE_SESSION:
4637 {
54d01ffb 4638 ret = cmd_create_session(cmd_ctx->lsm->session.name,
6df2e2c9 4639 cmd_ctx->lsm->session.path, &cmd_ctx->creds);
5e16da05
MD
4640 break;
4641 }
00e2e675
DG
4642 case LTTNG_CREATE_SESSION_URI:
4643 {
4644 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name,
4645 &cmd_ctx->lsm->u.create_uri.ctrl_uri,
4646 &cmd_ctx->lsm->u.create_uri.data_uri,
4647 cmd_ctx->lsm->u.create_uri.enable_consumer, &cmd_ctx->creds);
4648 break;
4649 }
5e16da05
MD
4650 case LTTNG_DESTROY_SESSION:
4651 {
54d01ffb
DG
4652 ret = cmd_destroy_session(cmd_ctx->session,
4653 cmd_ctx->lsm->session.name);
256a5576
MD
4654 /*
4655 * Set session to NULL so we do not unlock it after
4656 * free.
4657 */
4658 cmd_ctx->session = NULL;
5461b305 4659 break;
5e16da05 4660 }
9f19cc17 4661 case LTTNG_LIST_DOMAINS:
5e16da05 4662 {
54d01ffb
DG
4663 ssize_t nb_dom;
4664 struct lttng_domain *domains;
5461b305 4665
54d01ffb
DG
4666 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
4667 if (nb_dom < 0) {
4668 ret = -nb_dom;
4669 goto error;
ce3d728c 4670 }
520ff687 4671
54d01ffb 4672 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
5461b305
DG
4673 if (ret < 0) {
4674 goto setup_error;
520ff687 4675 }
57167058 4676
54d01ffb
DG
4677 /* Copy event list into message payload */
4678 memcpy(cmd_ctx->llm->payload, domains,
4679 nb_dom * sizeof(struct lttng_domain));
4680
4681 free(domains);
5e16da05 4682
5461b305 4683 ret = LTTCOMM_OK;
5e16da05
MD
4684 break;
4685 }
9f19cc17 4686 case LTTNG_LIST_CHANNELS:
5e16da05 4687 {
6775595e 4688 int nb_chan;
54d01ffb
DG
4689 struct lttng_channel *channels;
4690
b551a063
DG
4691 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
4692 cmd_ctx->session, &channels);
54d01ffb
DG
4693 if (nb_chan < 0) {
4694 ret = -nb_chan;
4695 goto error;
5461b305 4696 }
ca95a216 4697
54d01ffb 4698 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
5461b305
DG
4699 if (ret < 0) {
4700 goto setup_error;
4701 }
9f19cc17 4702
54d01ffb
DG
4703 /* Copy event list into message payload */
4704 memcpy(cmd_ctx->llm->payload, channels,
4705 nb_chan * sizeof(struct lttng_channel));
4706
4707 free(channels);
9f19cc17
DG
4708
4709 ret = LTTCOMM_OK;
5461b305 4710 break;
5e16da05 4711 }
9f19cc17 4712 case LTTNG_LIST_EVENTS:
5e16da05 4713 {
b551a063 4714 ssize_t nb_event;
684d34d2 4715 struct lttng_event *events = NULL;
9f19cc17 4716
b551a063 4717 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
54d01ffb
DG
4718 cmd_ctx->lsm->u.list.channel_name, &events);
4719 if (nb_event < 0) {
4720 ret = -nb_event;
4721 goto error;
5461b305 4722 }
ca95a216 4723
54d01ffb 4724 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
5461b305
DG
4725 if (ret < 0) {
4726 goto setup_error;
4727 }
9f19cc17 4728
54d01ffb
DG
4729 /* Copy event list into message payload */
4730 memcpy(cmd_ctx->llm->payload, events,
4731 nb_event * sizeof(struct lttng_event));
9f19cc17 4732
54d01ffb 4733 free(events);
9f19cc17
DG
4734
4735 ret = LTTCOMM_OK;
5461b305 4736 break;
5e16da05
MD
4737 }
4738 case LTTNG_LIST_SESSIONS:
4739 {
8e0af1b4 4740 unsigned int nr_sessions;
5461b305 4741
8e0af1b4 4742 session_lock_list();
730389d9
DG
4743 nr_sessions = lttng_sessions_count(
4744 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4745 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
d32fb093 4746
8e0af1b4 4747 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
5461b305 4748 if (ret < 0) {
54d01ffb 4749 session_unlock_list();
5461b305 4750 goto setup_error;
e065084a 4751 }
5e16da05 4752
6c9cc2ab 4753 /* Filled the session array */
8e0af1b4 4754 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
730389d9
DG
4755 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
4756 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
6c9cc2ab 4757
54d01ffb 4758 session_unlock_list();
5e16da05 4759
5461b305 4760 ret = LTTCOMM_OK;
5e16da05
MD
4761 break;
4762 }
d0254c7c
MD
4763 case LTTNG_CALIBRATE:
4764 {
54d01ffb
DG
4765 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
4766 &cmd_ctx->lsm->u.calibrate);
d0254c7c
MD
4767 break;
4768 }
d9800920
DG
4769 case LTTNG_REGISTER_CONSUMER:
4770 {
54d01ffb
DG
4771 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4772 cmd_ctx->lsm->u.reg.path);
d9800920
DG
4773 break;
4774 }
53a80697
MD
4775 case LTTNG_SET_FILTER:
4776 {
4777 struct lttng_filter_bytecode *bytecode;
4778
4779 if (cmd_ctx->lsm->u.filter.bytecode_len > 65336) {
4780 ret = LTTCOMM_FILTER_INVAL;
4781 goto error;
4782 }
4783 bytecode = zmalloc(cmd_ctx->lsm->u.filter.bytecode_len);
4784 if (!bytecode) {
4785 ret = LTTCOMM_FILTER_NOMEM;
4786 goto error;
4787 }
4788 /* Receive var. len. data */
4789 DBG("Receiving var len data from client ...");
4790 ret = lttcomm_recv_unix_sock(sock, bytecode,
4791 cmd_ctx->lsm->u.filter.bytecode_len);
4792 if (ret <= 0) {
4793 DBG("Nothing recv() from client var len data... continuing");
4794 *sock_error = 1;
4795 ret = LTTCOMM_FILTER_INVAL;
4796 goto error;
4797 }
4798
4799 if (bytecode->len + sizeof(*bytecode)
4800 != cmd_ctx->lsm->u.filter.bytecode_len) {
4801 free(bytecode);
4802 ret = LTTCOMM_FILTER_INVAL;
4803 goto error;
4804 }
4805
4806 ret = cmd_set_filter(cmd_ctx->session, cmd_ctx->lsm->domain.type,
4807 cmd_ctx->lsm->u.filter.channel_name,
4808 cmd_ctx->lsm->u.filter.event_name,
4809 bytecode);
4810 break;
4811 }
5e16da05 4812 default:
5e16da05 4813 ret = LTTCOMM_UND;
5461b305 4814 break;
fac6795d
DG
4815 }
4816
5461b305 4817error:
5461b305
DG
4818 if (cmd_ctx->llm == NULL) {
4819 DBG("Missing llm structure. Allocating one.");
894be886 4820 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
5461b305
DG
4821 goto setup_error;
4822 }
4823 }
54d01ffb 4824 /* Set return code */
5461b305 4825 cmd_ctx->llm->ret_code = ret;
5461b305 4826setup_error:
b5541356 4827 if (cmd_ctx->session) {
54d01ffb 4828 session_unlock(cmd_ctx->session);
b5541356 4829 }
256a5576
MD
4830 if (need_tracing_session) {
4831 session_unlock_list();
4832 }
54d01ffb 4833init_setup_error:
8028d920 4834 return ret;
fac6795d
DG
4835}
4836
44a5e5eb
DG
4837/*
4838 * Thread managing health check socket.
4839 */
4840static void *thread_manage_health(void *data)
4841{
139ac872 4842 int sock = -1, new_sock, ret, i, pollfd, err = -1;
44a5e5eb
DG
4843 uint32_t revents, nb_fd;
4844 struct lttng_poll_event events;
4845 struct lttcomm_health_msg msg;
4846 struct lttcomm_health_data reply;
4847
4848 DBG("[thread] Manage health check started");
4849
4850 rcu_register_thread();
4851
4852 /* Create unix socket */
4853 sock = lttcomm_create_unix_sock(health_unix_sock_path);
4854 if (sock < 0) {
4855 ERR("Unable to create health check Unix socket");
4856 ret = -1;
4857 goto error;
4858 }
4859
4860 ret = lttcomm_listen_unix_sock(sock);
4861 if (ret < 0) {
4862 goto error;
4863 }
4864
4865 /*
4866 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4867 * more will be added to this poll set.
4868 */
4869 ret = create_thread_poll_set(&events, 2);
4870 if (ret < 0) {
4871 goto error;
4872 }
4873
4874 /* Add the application registration socket */
4875 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
4876 if (ret < 0) {
4877 goto error;
4878 }
4879
4880 while (1) {
4881 DBG("Health check ready");
4882
4883 nb_fd = LTTNG_POLL_GETNB(&events);
4884
4885 /* Inifinite blocking call, waiting for transmission */
4886restart:
4887 ret = lttng_poll_wait(&events, -1);
4888 if (ret < 0) {
4889 /*
4890 * Restart interrupted system call.
4891 */
4892 if (errno == EINTR) {
4893 goto restart;
4894 }
4895 goto error;
4896 }
4897
4898 for (i = 0; i < nb_fd; i++) {
4899 /* Fetch once the poll data */
4900 revents = LTTNG_POLL_GETEV(&events, i);
4901 pollfd = LTTNG_POLL_GETFD(&events, i);
4902
4903 /* Thread quit pipe has been closed. Killing thread. */
4904 ret = check_thread_quit_pipe(pollfd, revents);
4905 if (ret) {
139ac872
MD
4906 err = 0;
4907 goto exit;
44a5e5eb
DG
4908 }
4909
4910 /* Event on the registration socket */
4911 if (pollfd == sock) {
4912 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
4913 ERR("Health socket poll error");
4914 goto error;
4915 }
4916 }
4917 }
4918
4919 new_sock = lttcomm_accept_unix_sock(sock);
4920 if (new_sock < 0) {
4921 goto error;
4922 }
4923
4924 DBG("Receiving data from client for health...");
4925 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
4926 if (ret <= 0) {
4927 DBG("Nothing recv() from client... continuing");
4928 ret = close(new_sock);
4929 if (ret) {
4930 PERROR("close");
4931 }
4932 new_sock = -1;
4933 continue;
4934 }
4935
4936 rcu_thread_online();
4937
4938 switch (msg.component) {
4939 case LTTNG_HEALTH_CMD:
4940 reply.ret_code = health_check_state(&health_thread_cmd);
4941 break;
139ac872
MD
4942 case LTTNG_HEALTH_APP_MANAGE:
4943 reply.ret_code = health_check_state(&health_thread_app_manage);
4944 break;
44a5e5eb
DG
4945 case LTTNG_HEALTH_APP_REG:
4946 reply.ret_code = health_check_state(&health_thread_app_reg);
4947 break;
4948 case LTTNG_HEALTH_KERNEL:
4949 reply.ret_code = health_check_state(&health_thread_kernel);
4950 break;
4951 case LTTNG_HEALTH_CONSUMER:
4952 reply.ret_code = check_consumer_health();
4953 break;
4954 case LTTNG_HEALTH_ALL:
44a5e5eb 4955 reply.ret_code =
139ac872
MD
4956 health_check_state(&health_thread_app_manage) &&
4957 health_check_state(&health_thread_app_reg) &&
4958 health_check_state(&health_thread_cmd) &&
4959 health_check_state(&health_thread_kernel) &&
4960 check_consumer_health();
44a5e5eb
DG
4961 break;
4962 default:
4963 reply.ret_code = LTTCOMM_UND;
4964 break;
4965 }
4966
4967 /*
4968 * Flip ret value since 0 is a success and 1 indicates a bad health for
4969 * the client where in the sessiond it is the opposite. Again, this is
4970 * just to make things easier for us poor developer which enjoy a lot
4971 * lazyness.
4972 */
4973 if (reply.ret_code == 0 || reply.ret_code == 1) {
4974 reply.ret_code = !reply.ret_code;
4975 }
4976
4977 DBG2("Health check return value %d", reply.ret_code);
4978
4979 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
4980 if (ret < 0) {
4981 ERR("Failed to send health data back to client");
4982 }
4983
4984 /* End of transmission */
4985 ret = close(new_sock);
4986 if (ret) {
4987 PERROR("close");
4988 }
4989 new_sock = -1;
4990 }
4991
139ac872 4992exit:
44a5e5eb 4993error:
139ac872
MD
4994 if (err) {
4995 ERR("Health error occurred in %s", __func__);
4996 }
44a5e5eb
DG
4997 DBG("Health check thread dying");
4998 unlink(health_unix_sock_path);
4999 if (sock >= 0) {
5000 ret = close(sock);
5001 if (ret) {
5002 PERROR("close");
5003 }
5004 }
5005 if (new_sock >= 0) {
5006 ret = close(new_sock);
5007 if (ret) {
5008 PERROR("close");
5009 }
5010 }
5011
5012 lttng_poll_clean(&events);
5013
5014 rcu_unregister_thread();
5015 return NULL;
5016}
5017
1d4b027a 5018/*
d063d709
DG
5019 * This thread manage all clients request using the unix client socket for
5020 * communication.
1d4b027a
DG
5021 */
5022static void *thread_manage_clients(void *data)
5023{
139ac872 5024 int sock = -1, ret, i, pollfd, err = -1;
53a80697 5025 int sock_error;
5eb91c98 5026 uint32_t revents, nb_fd;
273ea72c 5027 struct command_ctx *cmd_ctx = NULL;
5eb91c98 5028 struct lttng_poll_event events;
1d4b027a
DG
5029
5030 DBG("[thread] Manage client started");
5031
f6a9efaa
DG
5032 rcu_register_thread();
5033
44a5e5eb
DG
5034 health_code_update(&health_thread_cmd);
5035
1d4b027a
DG
5036 ret = lttcomm_listen_unix_sock(client_sock);
5037 if (ret < 0) {
5038 goto error;
5039 }
5040
5eb91c98
DG
5041 /*
5042 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
5043 * more will be added to this poll set.
5044 */
5045 ret = create_thread_poll_set(&events, 2);
5046 if (ret < 0) {
5047 goto error;
5048 }
273ea72c 5049
5eb91c98
DG
5050 /* Add the application registration socket */
5051 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
5052 if (ret < 0) {
5053 goto error;
5054 }
273ea72c 5055
bbd973c2
DG
5056 /*
5057 * Notify parent pid that we are ready to accept command for client side.
1d4b027a
DG
5058 */
5059 if (opt_sig_parent) {
8db8d1dc 5060 kill(ppid, SIGUSR1);
1d4b027a
DG
5061 }
5062
44a5e5eb
DG
5063 health_code_update(&health_thread_cmd);
5064
1d4b027a 5065 while (1) {
1d4b027a 5066 DBG("Accepting client command ...");
273ea72c 5067
5eb91c98
DG
5068 nb_fd = LTTNG_POLL_GETNB(&events);
5069
273ea72c 5070 /* Inifinite blocking call, waiting for transmission */
88f2b785 5071 restart:
44a5e5eb 5072 health_poll_update(&health_thread_cmd);
5eb91c98 5073 ret = lttng_poll_wait(&events, -1);
44a5e5eb 5074 health_poll_update(&health_thread_cmd);
273ea72c 5075 if (ret < 0) {
88f2b785
MD
5076 /*
5077 * Restart interrupted system call.
5078 */
5079 if (errno == EINTR) {
5080 goto restart;
5081 }
273ea72c
DG
5082 goto error;
5083 }
5084
5eb91c98
DG
5085 for (i = 0; i < nb_fd; i++) {
5086 /* Fetch once the poll data */
5087 revents = LTTNG_POLL_GETEV(&events, i);
5088 pollfd = LTTNG_POLL_GETFD(&events, i);
5089
44a5e5eb
DG
5090 health_code_update(&health_thread_cmd);
5091
5eb91c98
DG
5092 /* Thread quit pipe has been closed. Killing thread. */
5093 ret = check_thread_quit_pipe(pollfd, revents);
5094 if (ret) {
139ac872
MD
5095 err = 0;
5096 goto exit;
5eb91c98
DG
5097 }
5098
5099 /* Event on the registration socket */
5100 if (pollfd == client_sock) {
5101 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
5102 ERR("Client socket poll error");
5103 goto error;
5104 }
5105 }
5106 }
5107
5108 DBG("Wait for client response");
5109
44a5e5eb
DG
5110 health_code_update(&health_thread_cmd);
5111
5eb91c98
DG
5112 sock = lttcomm_accept_unix_sock(client_sock);
5113 if (sock < 0) {
273ea72c 5114 goto error;
273ea72c
DG
5115 }
5116
be040666
DG
5117 /* Set socket option for credentials retrieval */
5118 ret = lttcomm_setsockopt_creds_unix_sock(sock);
5119 if (ret < 0) {
5120 goto error;
5121 }
5122
5eb91c98 5123 /* Allocate context command to process the client request */
ba7f0ae5 5124 cmd_ctx = zmalloc(sizeof(struct command_ctx));
5eb91c98 5125 if (cmd_ctx == NULL) {
76d7553f 5126 PERROR("zmalloc cmd_ctx");
1d4b027a 5127 goto error;
5eb91c98 5128 }
1d4b027a 5129
5eb91c98 5130 /* Allocate data buffer for reception */
ba7f0ae5 5131 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
5eb91c98 5132 if (cmd_ctx->lsm == NULL) {
76d7553f 5133 PERROR("zmalloc cmd_ctx->lsm");
5eb91c98
DG
5134 goto error;
5135 }
1d4b027a 5136
5eb91c98
DG
5137 cmd_ctx->llm = NULL;
5138 cmd_ctx->session = NULL;
1d4b027a 5139
44a5e5eb
DG
5140 health_code_update(&health_thread_cmd);
5141
5eb91c98
DG
5142 /*
5143 * Data is received from the lttng client. The struct
5144 * lttcomm_session_msg (lsm) contains the command and data request of
5145 * the client.
5146 */
5147 DBG("Receiving data from client ...");
be040666
DG
5148 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
5149 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
5eb91c98
DG
5150 if (ret <= 0) {
5151 DBG("Nothing recv() from client... continuing");
76d7553f
MD
5152 ret = close(sock);
5153 if (ret) {
5154 PERROR("close");
5155 }
5156 sock = -1;
a2c0da86 5157 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
5158 continue;
5159 }
1d4b027a 5160
44a5e5eb
DG
5161 health_code_update(&health_thread_cmd);
5162
5eb91c98
DG
5163 // TODO: Validate cmd_ctx including sanity check for
5164 // security purpose.
f7776ea7 5165
f6a9efaa 5166 rcu_thread_online();
5eb91c98
DG
5167 /*
5168 * This function dispatch the work to the kernel or userspace tracer
5169 * libs and fill the lttcomm_lttng_msg data structure of all the needed
5170 * informations for the client. The command context struct contains
5171 * everything this function may needs.
5172 */
53a80697 5173 ret = process_client_msg(cmd_ctx, sock, &sock_error);
f6a9efaa 5174 rcu_thread_offline();
5eb91c98 5175 if (ret < 0) {
53a80697
MD
5176 if (sock_error) {
5177 ret = close(sock);
5178 if (ret) {
5179 PERROR("close");
5180 }
5181 sock = -1;
5182 }
bbd973c2 5183 /*
5eb91c98 5184 * TODO: Inform client somehow of the fatal error. At
ba7f0ae5 5185 * this point, ret < 0 means that a zmalloc failed
53a80697
MD
5186 * (ENOMEM). Error detected but still accept
5187 * command, unless a socket error has been
5188 * detected.
bbd973c2 5189 */
bbd973c2 5190 clean_command_ctx(&cmd_ctx);
5eb91c98
DG
5191 continue;
5192 }
d6e4fca4 5193
44a5e5eb
DG
5194 health_code_update(&health_thread_cmd);
5195
54d01ffb
DG
5196 DBG("Sending response (size: %d, retcode: %s)",
5197 cmd_ctx->lttng_msg_size,
9a745bc7 5198 lttng_strerror(-cmd_ctx->llm->ret_code));
54d01ffb 5199 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
5eb91c98
DG
5200 if (ret < 0) {
5201 ERR("Failed to send data back to client");
bbd973c2 5202 }
1d4b027a 5203
5eb91c98 5204 /* End of transmission */
76d7553f
MD
5205 ret = close(sock);
5206 if (ret) {
5207 PERROR("close");
5208 }
5209 sock = -1;
be040666
DG
5210
5211 clean_command_ctx(&cmd_ctx);
44a5e5eb
DG
5212
5213 health_code_update(&health_thread_cmd);
273ea72c
DG
5214 }
5215
139ac872 5216exit:
5eb91c98 5217error:
139ac872
MD
5218 if (err) {
5219 health_error(&health_thread_cmd);
5220 ERR("Health error occurred in %s", __func__);
5221 }
5222 health_exit(&health_thread_cmd);
44a5e5eb 5223
5eb91c98 5224 DBG("Client thread dying");
273ea72c 5225 unlink(client_unix_sock_path);
76d7553f
MD
5226 if (client_sock >= 0) {
5227 ret = close(client_sock);
5228 if (ret) {
5229 PERROR("close");
5230 }
a4b35e07
MD
5231 }
5232 if (sock >= 0) {
76d7553f
MD
5233 ret = close(sock);
5234 if (ret) {
5235 PERROR("close");
5236 }
a4b35e07 5237 }
273ea72c 5238
5eb91c98 5239 lttng_poll_clean(&events);
a2fb29a5 5240 clean_command_ctx(&cmd_ctx);
f6a9efaa
DG
5241
5242 rcu_unregister_thread();
1d4b027a
DG
5243 return NULL;
5244}
5245
5246
fac6795d
DG
5247/*
5248 * usage function on stderr
5249 */
5250static void usage(void)
5251{
b716ce68 5252 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
d6f42150
DG
5253 fprintf(stderr, " -h, --help Display this usage.\n");
5254 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
5255 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
5256 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
5257 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
7753dea8
MD
5258 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
5259 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
5260 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
5261 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
ebaeda94
MD
5262 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
5263 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
5264 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
5265 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
d6f42150
DG
5266 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
5267 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
5268 fprintf(stderr, " -V, --version Show version number.\n");
5269 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
5270 fprintf(stderr, " -q, --quiet No output at all.\n");
5271 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
3bd1e081 5272 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4fba7219 5273 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
fac6795d
DG
5274}
5275
5276/*
5277 * daemon argument parsing
5278 */
5279static int parse_args(int argc, char **argv)
5280{
5281 int c;
5282
5283 static struct option long_options[] = {
5284 { "client-sock", 1, 0, 'c' },
5285 { "apps-sock", 1, 0, 'a' },
3bd1e081
MD
5286 { "kconsumerd-cmd-sock", 1, 0, 'C' },
5287 { "kconsumerd-err-sock", 1, 0, 'E' },
7753dea8
MD
5288 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
5289 { "ustconsumerd32-err-sock", 1, 0, 'H' },
ebaeda94
MD
5290 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
5291 { "ustconsumerd64-err-sock", 1, 0, 'F' },
5292 { "consumerd32-path", 1, 0, 'u' },
5293 { "consumerd32-libdir", 1, 0, 'U' },
5294 { "consumerd64-path", 1, 0, 't' },
5295 { "consumerd64-libdir", 1, 0, 'T' },
fac6795d 5296 { "daemonize", 0, 0, 'd' },
5b8719f5 5297 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
5298 { "help", 0, 0, 'h' },
5299 { "group", 1, 0, 'g' },
5300 { "version", 0, 0, 'V' },
75462a81 5301 { "quiet", 0, 0, 'q' },
3f9947db 5302 { "verbose", 0, 0, 'v' },
3bd1e081 5303 { "verbose-consumer", 0, 0, 'Z' },
4fba7219 5304 { "no-kernel", 0, 0, 'N' },
fac6795d
DG
5305 { NULL, 0, 0, 0 }
5306 };
5307
5308 while (1) {
5309 int option_index = 0;
4fba7219 5310 c = getopt_long(argc, argv, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t",
54d01ffb 5311 long_options, &option_index);
fac6795d
DG
5312 if (c == -1) {
5313 break;
5314 }
5315
5316 switch (c) {
5317 case 0:
5318 fprintf(stderr, "option %s", long_options[option_index].name);
5319 if (optarg) {
5320 fprintf(stderr, " with arg %s\n", optarg);
5321 }
5322 break;
b716ce68 5323 case 'c':
fac6795d
DG
5324 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
5325 break;
5326 case 'a':
5327 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
5328 break;
5329 case 'd':
5330 opt_daemon = 1;
5331 break;
5332 case 'g':
fb09408a 5333 opt_tracing_group = optarg;
fac6795d
DG
5334 break;
5335 case 'h':
5336 usage();
5337 exit(EXIT_FAILURE);
5338 case 'V':
5339 fprintf(stdout, "%s\n", VERSION);
5340 exit(EXIT_SUCCESS);
5b8719f5
DG
5341 case 'S':
5342 opt_sig_parent = 1;
5343 break;
d6f42150 5344 case 'E':
3bd1e081 5345 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150
DG
5346 break;
5347 case 'C':
3bd1e081
MD
5348 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5349 break;
5350 case 'F':
7753dea8 5351 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
3bd1e081
MD
5352 break;
5353 case 'D':
7753dea8
MD
5354 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
5355 break;
5356 case 'H':
5357 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", optarg);
5358 break;
5359 case 'G':
5360 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", optarg);
d6f42150 5361 break;
4fba7219
DG
5362 case 'N':
5363 opt_no_kernel = 1;
5364 break;
75462a81 5365 case 'q':
97e19046 5366 lttng_opt_quiet = 1;
75462a81 5367 break;
3f9947db 5368 case 'v':
53086306 5369 /* Verbose level can increase using multiple -v */
97e19046 5370 lttng_opt_verbose += 1;
3f9947db 5371 break;
31f73cc9 5372 case 'Z':
3bd1e081 5373 opt_verbose_consumer += 1;
31f73cc9 5374 break;
fb09408a 5375 case 'u':
fc7a59ce 5376 consumerd32_bin= optarg;
ebaeda94
MD
5377 break;
5378 case 'U':
5379 consumerd32_libdir = optarg;
7753dea8
MD
5380 break;
5381 case 't':
fc7a59ce 5382 consumerd64_bin = optarg;
ebaeda94
MD
5383 break;
5384 case 'T':
5385 consumerd64_libdir = optarg;
fb09408a 5386 break;
fac6795d
DG
5387 default:
5388 /* Unknown option or other error.
5389 * Error is printed by getopt, just return */
5390 return -1;
5391 }
5392 }
5393
5394 return 0;
5395}
5396
5397/*
d063d709 5398 * Creates the two needed socket by the daemon.
d6f42150
DG
5399 * apps_sock - The communication socket for all UST apps.
5400 * client_sock - The communication of the cli tool (lttng).
fac6795d 5401 */
cf3af59e 5402static int init_daemon_socket(void)
fac6795d
DG
5403{
5404 int ret = 0;
5405 mode_t old_umask;
5406
5407 old_umask = umask(0);
5408
5409 /* Create client tool unix socket */
d6f42150
DG
5410 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
5411 if (client_sock < 0) {
5412 ERR("Create unix sock failed: %s", client_unix_sock_path);
fac6795d
DG
5413 ret = -1;
5414 goto end;
5415 }
5416
5417 /* File permission MUST be 660 */
5418 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
5419 if (ret < 0) {
d6f42150 5420 ERR("Set file permissions failed: %s", client_unix_sock_path);
76d7553f 5421 PERROR("chmod");
fac6795d
DG
5422 goto end;
5423 }
5424
5425 /* Create the application unix socket */
d6f42150
DG
5426 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
5427 if (apps_sock < 0) {
5428 ERR("Create unix sock failed: %s", apps_unix_sock_path);
fac6795d
DG
5429 ret = -1;
5430 goto end;
5431 }
5432
d6f42150 5433 /* File permission MUST be 666 */
54d01ffb
DG
5434 ret = chmod(apps_unix_sock_path,
5435 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
fac6795d 5436 if (ret < 0) {
d6f42150 5437 ERR("Set file permissions failed: %s", apps_unix_sock_path);
76d7553f 5438 PERROR("chmod");
fac6795d
DG
5439 goto end;
5440 }
5441
5442end:
5443 umask(old_umask);
5444 return ret;
5445}
5446
5447/*
54d01ffb
DG
5448 * Check if the global socket is available, and if a daemon is answering at the
5449 * other side. If yes, error is returned.
fac6795d 5450 */
cf3af59e 5451static int check_existing_daemon(void)
fac6795d 5452{
7d8234d9 5453 /* Is there anybody out there ? */
099e26bd 5454 if (lttng_session_daemon_alive()) {
7d8234d9 5455 return -EEXIST;
099e26bd 5456 }
b09c7c76
DG
5457
5458 return 0;
fac6795d
DG
5459}
5460
fac6795d 5461/*
d063d709 5462 * Set the tracing group gid onto the client socket.
5e16da05 5463 *
d063d709 5464 * Race window between mkdir and chown is OK because we are going from more
d1613cf5 5465 * permissive (root.root) to less permissive (root.tracing).
fac6795d 5466 */
be040666 5467static int set_permissions(char *rundir)
fac6795d
DG
5468{
5469 int ret;
996b65c8 5470 gid_t gid;
fac6795d 5471
6775595e
DG
5472 ret = allowed_group();
5473 if (ret < 0) {
be040666
DG
5474 WARN("No tracing group detected");
5475 ret = 0;
fac6795d
DG
5476 goto end;
5477 }
5478
6775595e
DG
5479 gid = ret;
5480
d6f42150 5481 /* Set lttng run dir */
be040666 5482 ret = chown(rundir, 0, gid);
d6f42150 5483 if (ret < 0) {
be040666 5484 ERR("Unable to set group on %s", rundir);
76d7553f 5485 PERROR("chown");
d6f42150
DG
5486 }
5487
d1613cf5 5488 /* Ensure tracing group can search the run dir */
61076f74 5489 ret = chmod(rundir, S_IRWXU | S_IXGRP | S_IXOTH);
d1613cf5
JN
5490 if (ret < 0) {
5491 ERR("Unable to set permissions on %s", rundir);
76d7553f 5492 PERROR("chmod");
d1613cf5
JN
5493 }
5494
d6f42150 5495 /* lttng client socket path */
996b65c8 5496 ret = chown(client_unix_sock_path, 0, gid);
fac6795d 5497 if (ret < 0) {
d6f42150 5498 ERR("Unable to set group on %s", client_unix_sock_path);
76d7553f 5499 PERROR("chown");
d6f42150
DG
5500 }
5501
3bd1e081
MD
5502 /* kconsumer error socket path */
5503 ret = chown(kconsumer_data.err_unix_sock_path, 0, gid);
d6f42150 5504 if (ret < 0) {
3bd1e081 5505 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
76d7553f 5506 PERROR("chown");
3bd1e081
MD
5507 }
5508
7753dea8
MD
5509 /* 64-bit ustconsumer error socket path */
5510 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, gid);
5511 if (ret < 0) {
5512 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
76d7553f 5513 PERROR("chown");
7753dea8
MD
5514 }
5515
5516 /* 32-bit ustconsumer compat32 error socket path */
5517 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, gid);
3bd1e081 5518 if (ret < 0) {
7753dea8 5519 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
76d7553f 5520 PERROR("chown");
fac6795d
DG
5521 }
5522
d6f42150 5523 DBG("All permissions are set");
e07ae692 5524
fac6795d
DG
5525end:
5526 return ret;
5527}
5528
d6f42150 5529/*
d063d709 5530 * Create the lttng run directory needed for all global sockets and pipe.
d6f42150 5531 */
67e40797 5532static int create_lttng_rundir(const char *rundir)
d6f42150
DG
5533{
5534 int ret;
5535
67e40797
DG
5536 DBG3("Creating LTTng run directory: %s", rundir);
5537
d1613cf5 5538 ret = mkdir(rundir, S_IRWXU);
d6f42150 5539 if (ret < 0) {
b1f11e69 5540 if (errno != EEXIST) {
67e40797 5541 ERR("Unable to create %s", rundir);
b1f11e69
DG
5542 goto error;
5543 } else {
5544 ret = 0;
5545 }
d6f42150
DG
5546 }
5547
5548error:
5549 return ret;
5550}
5551
5552/*
d063d709
DG
5553 * Setup sockets and directory needed by the kconsumerd communication with the
5554 * session daemon.
d6f42150 5555 */
67e40797
DG
5556static int set_consumer_sockets(struct consumer_data *consumer_data,
5557 const char *rundir)
d6f42150
DG
5558{
5559 int ret;
67e40797 5560 char path[PATH_MAX];
d6f42150 5561
67e40797 5562 switch (consumer_data->type) {
7753dea8 5563 case LTTNG_CONSUMER_KERNEL:
60922cb0 5564 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
7753dea8
MD
5565 break;
5566 case LTTNG_CONSUMER64_UST:
60922cb0 5567 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
7753dea8
MD
5568 break;
5569 case LTTNG_CONSUMER32_UST:
60922cb0 5570 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
7753dea8
MD
5571 break;
5572 default:
5573 ERR("Consumer type unknown");
5574 ret = -EINVAL;
5575 goto error;
d6f42150
DG
5576 }
5577
67e40797
DG
5578 DBG2("Creating consumer directory: %s", path);
5579
d1613cf5 5580 ret = mkdir(path, S_IRWXU);
d6f42150 5581 if (ret < 0) {
6beb2242 5582 if (errno != EEXIST) {
f11e84c2 5583 PERROR("mkdir");
3bd1e081 5584 ERR("Failed to create %s", path);
6beb2242
DG
5585 goto error;
5586 }
f11e84c2 5587 ret = -1;
d6f42150
DG
5588 }
5589
5590 /* Create the kconsumerd error unix socket */
3bd1e081
MD
5591 consumer_data->err_sock =
5592 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
5593 if (consumer_data->err_sock < 0) {
5594 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
d6f42150
DG
5595 ret = -1;
5596 goto error;
5597 }
5598
5599 /* File permission MUST be 660 */
3bd1e081 5600 ret = chmod(consumer_data->err_unix_sock_path,
54d01ffb 5601 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
d6f42150 5602 if (ret < 0) {
3bd1e081 5603 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
67e40797 5604 PERROR("chmod");
d6f42150
DG
5605 goto error;
5606 }
5607
5608error:
5609 return ret;
5610}
5611
fac6795d 5612/*
d063d709 5613 * Signal handler for the daemon
cf3af59e 5614 *
54d01ffb
DG
5615 * Simply stop all worker threads, leaving main() return gracefully after
5616 * joining all threads and calling cleanup().
fac6795d
DG
5617 */
5618static void sighandler(int sig)
5619{
5620 switch (sig) {
cf3af59e 5621 case SIGPIPE:
af87c45a 5622 DBG("SIGPIPE caught");
cf3af59e
MD
5623 return;
5624 case SIGINT:
af87c45a 5625 DBG("SIGINT caught");
cf3af59e
MD
5626 stop_threads();
5627 break;
5628 case SIGTERM:
af87c45a 5629 DBG("SIGTERM caught");
cf3af59e
MD
5630 stop_threads();
5631 break;
5632 default:
5633 break;
fac6795d 5634 }
fac6795d
DG
5635}
5636
5637/*
d063d709 5638 * Setup signal handler for :
1d4b027a 5639 * SIGINT, SIGTERM, SIGPIPE
fac6795d 5640 */
1d4b027a 5641static int set_signal_handler(void)
fac6795d 5642{
1d4b027a
DG
5643 int ret = 0;
5644 struct sigaction sa;
5645 sigset_t sigset;
fac6795d 5646
1d4b027a 5647 if ((ret = sigemptyset(&sigset)) < 0) {
76d7553f 5648 PERROR("sigemptyset");
1d4b027a
DG
5649 return ret;
5650 }
d6f42150 5651
1d4b027a
DG
5652 sa.sa_handler = sighandler;
5653 sa.sa_mask = sigset;
5654 sa.sa_flags = 0;
5655 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
76d7553f 5656 PERROR("sigaction");
1d4b027a 5657 return ret;
d6f42150
DG
5658 }
5659
1d4b027a 5660 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
76d7553f 5661 PERROR("sigaction");
1d4b027a 5662 return ret;
d6f42150 5663 }
aaf26714 5664
1d4b027a 5665 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
76d7553f 5666 PERROR("sigaction");
1d4b027a 5667 return ret;
8c0faa1d
DG
5668 }
5669
1d4b027a
DG
5670 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
5671
5672 return ret;
fac6795d
DG
5673}
5674
f3ed775e 5675/*
d063d709
DG
5676 * Set open files limit to unlimited. This daemon can open a large number of
5677 * file descriptors in order to consumer multiple kernel traces.
f3ed775e
DG
5678 */
5679static void set_ulimit(void)
5680{
5681 int ret;
5682 struct rlimit lim;
5683
a88df331 5684 /* The kernel does not allowed an infinite limit for open files */
f3ed775e
DG
5685 lim.rlim_cur = 65535;
5686 lim.rlim_max = 65535;
5687
5688 ret = setrlimit(RLIMIT_NOFILE, &lim);
5689 if (ret < 0) {
76d7553f 5690 PERROR("failed to set open files limit");
f3ed775e
DG
5691 }
5692}
5693
fac6795d
DG
5694/*
5695 * main
5696 */
5697int main(int argc, char **argv)
5698{
fac6795d
DG
5699 int ret = 0;
5700 void *status;
b082db07 5701 const char *home_path;
fac6795d 5702
335a95b7
MD
5703 init_kernel_workarounds();
5704
f6a9efaa
DG
5705 rcu_register_thread();
5706
7753dea8 5707 setup_consumerd_path();
fb09408a 5708
fac6795d
DG
5709 /* Parse arguments */
5710 progname = argv[0];
5711 if ((ret = parse_args(argc, argv) < 0)) {
cf3af59e 5712 goto error;
fac6795d
DG
5713 }
5714
5715 /* Daemonize */
5716 if (opt_daemon) {
ceed52b5
MD
5717 int i;
5718
5719 /*
5720 * fork
5721 * child: setsid, close FD 0, 1, 2, chdir /
5722 * parent: exit (if fork is successful)
5723 */
53094c05
DG
5724 ret = daemon(0, 0);
5725 if (ret < 0) {
76d7553f 5726 PERROR("daemon");
cf3af59e 5727 goto error;
53094c05 5728 }
ceed52b5
MD
5729 /*
5730 * We are in the child. Make sure all other file
5731 * descriptors are closed, in case we are called with
5732 * more opened file descriptors than the standard ones.
5733 */
5734 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
5735 (void) close(i);
5736 }
5737 }
5738
5739 /* Create thread quit pipe */
5740 if ((ret = init_thread_quit_pipe()) < 0) {
5741 goto error;
fac6795d
DG
5742 }
5743
5744 /* Check if daemon is UID = 0 */
5745 is_root = !getuid();
5746
fac6795d 5747 if (is_root) {
990570ed 5748 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
67e40797
DG
5749
5750 /* Create global run dir with root access */
5751 ret = create_lttng_rundir(rundir);
d6f42150 5752 if (ret < 0) {
cf3af59e 5753 goto error;
d6f42150
DG
5754 }
5755
fac6795d 5756 if (strlen(apps_unix_sock_path) == 0) {
d6f42150
DG
5757 snprintf(apps_unix_sock_path, PATH_MAX,
5758 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
fac6795d
DG
5759 }
5760
5761 if (strlen(client_unix_sock_path) == 0) {
d6f42150
DG
5762 snprintf(client_unix_sock_path, PATH_MAX,
5763 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
5764 }
0fdd1e2c
DG
5765
5766 /* Set global SHM for ust */
5767 if (strlen(wait_shm_path) == 0) {
5768 snprintf(wait_shm_path, PATH_MAX,
5769 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
5770 }
67e40797 5771
44a5e5eb
DG
5772 if (strlen(health_unix_sock_path) == 0) {
5773 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5774 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
5775 }
5776
67e40797
DG
5777 /* Setup kernel consumerd path */
5778 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
60922cb0 5779 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
67e40797 5780 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5781 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
67e40797
DG
5782
5783 DBG2("Kernel consumer err path: %s",
5784 kconsumer_data.err_unix_sock_path);
5785 DBG2("Kernel consumer cmd path: %s",
5786 kconsumer_data.cmd_unix_sock_path);
fac6795d 5787 } else {
b082db07
DG
5788 home_path = get_home_dir();
5789 if (home_path == NULL) {
273ea72c
DG
5790 /* TODO: Add --socket PATH option */
5791 ERR("Can't get HOME directory for sockets creation.");
cf3af59e
MD
5792 ret = -EPERM;
5793 goto error;
b082db07
DG
5794 }
5795
67e40797
DG
5796 /*
5797 * Create rundir from home path. This will create something like
5798 * $HOME/.lttng
5799 */
990570ed 5800 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
67e40797
DG
5801 if (ret < 0) {
5802 ret = -ENOMEM;
5803 goto error;
5804 }
5805
5806 ret = create_lttng_rundir(rundir);
5807 if (ret < 0) {
5808 goto error;
5809 }
5810
fac6795d 5811 if (strlen(apps_unix_sock_path) == 0) {
d6f42150 5812 snprintf(apps_unix_sock_path, PATH_MAX,
b082db07 5813 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
fac6795d
DG
5814 }
5815
5816 /* Set the cli tool unix socket path */
5817 if (strlen(client_unix_sock_path) == 0) {
d6f42150 5818 snprintf(client_unix_sock_path, PATH_MAX,
b082db07 5819 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
fac6795d 5820 }
0fdd1e2c
DG
5821
5822 /* Set global SHM for ust */
5823 if (strlen(wait_shm_path) == 0) {
5824 snprintf(wait_shm_path, PATH_MAX,
5825 DEFAULT_HOME_APPS_WAIT_SHM_PATH, geteuid());
5826 }
44a5e5eb
DG
5827
5828 /* Set health check Unix path */
5829 if (strlen(health_unix_sock_path) == 0) {
5830 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
5831 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
5832 }
fac6795d
DG
5833 }
5834
5c827ce0
DG
5835 /* Set consumer initial state */
5836 kernel_consumerd_state = CONSUMER_STOPPED;
5837 ust_consumerd_state = CONSUMER_STOPPED;
5838
847177cd
DG
5839 DBG("Client socket path %s", client_unix_sock_path);
5840 DBG("Application socket path %s", apps_unix_sock_path);
67e40797
DG
5841 DBG("LTTng run directory path: %s", rundir);
5842
5843 /* 32 bits consumerd path setup */
5844 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
60922cb0 5845 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
67e40797 5846 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5847 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
67e40797
DG
5848
5849 DBG2("UST consumer 32 bits err path: %s",
5850 ustconsumer32_data.err_unix_sock_path);
5851 DBG2("UST consumer 32 bits cmd path: %s",
5852 ustconsumer32_data.cmd_unix_sock_path);
5853
5854 /* 64 bits consumerd path setup */
5855 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
60922cb0 5856 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
67e40797 5857 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
60922cb0 5858 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
67e40797
DG
5859
5860 DBG2("UST consumer 64 bits err path: %s",
5861 ustconsumer64_data.err_unix_sock_path);
5862 DBG2("UST consumer 64 bits cmd path: %s",
5863 ustconsumer64_data.cmd_unix_sock_path);
847177cd 5864
273ea72c 5865 /*
7d8234d9 5866 * See if daemon already exist.
fac6795d 5867 */
7d8234d9 5868 if ((ret = check_existing_daemon()) < 0) {
75462a81 5869 ERR("Already running daemon.\n");
273ea72c 5870 /*
cf3af59e
MD
5871 * We do not goto exit because we must not cleanup()
5872 * because a daemon is already running.
ab118b20 5873 */
cf3af59e 5874 goto error;
a88df331
DG
5875 }
5876
1427f9b2
DG
5877 /*
5878 * Init UST app hash table. Alloc hash table before this point since
5879 * cleanup() can get called after that point.
5880 */
5881 ust_app_ht_alloc();
5882
54d01ffb 5883 /* After this point, we can safely call cleanup() with "goto exit" */
a88df331
DG
5884
5885 /*
5886 * These actions must be executed as root. We do that *after* setting up
5887 * the sockets path because we MUST make the check for another daemon using
5888 * those paths *before* trying to set the kernel consumer sockets and init
5889 * kernel tracer.
5890 */
5891 if (is_root) {
67e40797 5892 ret = set_consumer_sockets(&kconsumer_data, rundir);
7753dea8
MD
5893 if (ret < 0) {
5894 goto exit;
5895 }
5896
a88df331 5897 /* Setup kernel tracer */
4fba7219
DG
5898 if (!opt_no_kernel) {
5899 init_kernel_tracer();
5900 }
a88df331
DG
5901
5902 /* Set ulimit for open files */
5903 set_ulimit();
fac6795d 5904 }
4063050c
MD
5905 /* init lttng_fd tracking must be done after set_ulimit. */
5906 lttng_fd_init();
fac6795d 5907
67e40797
DG
5908 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
5909 if (ret < 0) {
5910 goto exit;
5911 }
5912
5913 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
5914 if (ret < 0) {
5915 goto exit;
5916 }
5917
cf3af59e
MD
5918 if ((ret = set_signal_handler()) < 0) {
5919 goto exit;
fac6795d
DG
5920 }
5921
d6f42150 5922 /* Setup the needed unix socket */
cf3af59e
MD
5923 if ((ret = init_daemon_socket()) < 0) {
5924 goto exit;
fac6795d
DG
5925 }
5926
5927 /* Set credentials to socket */
be040666 5928 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
cf3af59e 5929 goto exit;
fac6795d
DG
5930 }
5931
5b8719f5
DG
5932 /* Get parent pid if -S, --sig-parent is specified. */
5933 if (opt_sig_parent) {
5934 ppid = getppid();
5935 }
5936
7a485870 5937 /* Setup the kernel pipe for waking up the kernel thread */
ef599319 5938 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
cf3af59e 5939 goto exit;
7a485870
DG
5940 }
5941
099e26bd 5942 /* Setup the thread apps communication pipe. */
ef599319 5943 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
099e26bd
DG
5944 goto exit;
5945 }
5946
5947 /* Init UST command queue. */
5948 cds_wfq_init(&ust_cmd_queue.queue);
5949
273ea72c 5950 /*
54d01ffb
DG
5951 * Get session list pointer. This pointer MUST NOT be free(). This list is
5952 * statically declared in session.c
273ea72c 5953 */
54d01ffb 5954 session_list_ptr = session_get_list();
b5541356 5955
5eb91c98
DG
5956 /* Set up max poll set size */
5957 lttng_poll_set_max_size();
5958
00e2e675
DG
5959 /*
5960 * Set network sequence index to 1 for streams to match a relayd socket on
5961 * the consumer side.
5962 */
5963 uatomic_set(&relayd_net_seq_idx, 1);
5964
44a5e5eb
DG
5965 /* Init all health thread counters. */
5966 health_init(&health_thread_cmd);
5967 health_init(&health_thread_kernel);
139ac872 5968 health_init(&health_thread_app_manage);
44a5e5eb
DG
5969 health_init(&health_thread_app_reg);
5970
5971 /*
5972 * Init health counters of the consumer thread. We do a quick hack here to
5973 * the state of the consumer health is fine even if the thread is not
5974 * started. This is simply to ease our life and has no cost what so ever.
5975 */
5976 health_init(&kconsumer_data.health);
5977 health_poll_update(&kconsumer_data.health);
5978 health_init(&ustconsumer32_data.health);
5979 health_poll_update(&ustconsumer32_data.health);
5980 health_init(&ustconsumer64_data.health);
5981 health_poll_update(&ustconsumer64_data.health);
5982
5983 /* Create thread to manage the client socket */
5984 ret = pthread_create(&health_thread, NULL,
5985 thread_manage_health, (void *) NULL);
5986 if (ret != 0) {
5987 PERROR("pthread_create health");
5988 goto exit_health;
5989 }
5990
cf3af59e 5991 /* Create thread to manage the client socket */
099e26bd
DG
5992 ret = pthread_create(&client_thread, NULL,
5993 thread_manage_clients, (void *) NULL);
cf3af59e 5994 if (ret != 0) {
76d7553f 5995 PERROR("pthread_create clients");
cf3af59e
MD
5996 goto exit_client;
5997 }
fac6795d 5998
099e26bd
DG
5999 /* Create thread to dispatch registration */
6000 ret = pthread_create(&dispatch_thread, NULL,
6001 thread_dispatch_ust_registration, (void *) NULL);
6002 if (ret != 0) {
76d7553f 6003 PERROR("pthread_create dispatch");
099e26bd
DG
6004 goto exit_dispatch;
6005 }
6006
6007 /* Create thread to manage application registration. */
6008 ret = pthread_create(&reg_apps_thread, NULL,
6009 thread_registration_apps, (void *) NULL);
6010 if (ret != 0) {
76d7553f 6011 PERROR("pthread_create registration");
099e26bd
DG
6012 goto exit_reg_apps;
6013 }
6014
cf3af59e 6015 /* Create thread to manage application socket */
54d01ffb
DG
6016 ret = pthread_create(&apps_thread, NULL,
6017 thread_manage_apps, (void *) NULL);
cf3af59e 6018 if (ret != 0) {
76d7553f 6019 PERROR("pthread_create apps");
cf3af59e
MD
6020 goto exit_apps;
6021 }
fac6795d 6022
cf3af59e 6023 /* Create kernel thread to manage kernel event */
54d01ffb
DG
6024 ret = pthread_create(&kernel_thread, NULL,
6025 thread_manage_kernel, (void *) NULL);
cf3af59e 6026 if (ret != 0) {
76d7553f 6027 PERROR("pthread_create kernel");
cf3af59e
MD
6028 goto exit_kernel;
6029 }
7a485870 6030
cf3af59e
MD
6031 ret = pthread_join(kernel_thread, &status);
6032 if (ret != 0) {
76d7553f 6033 PERROR("pthread_join");
cf3af59e 6034 goto error; /* join error, exit without cleanup */
fac6795d
DG
6035 }
6036
cf3af59e
MD
6037exit_kernel:
6038 ret = pthread_join(apps_thread, &status);
6039 if (ret != 0) {
76d7553f 6040 PERROR("pthread_join");
cf3af59e
MD
6041 goto error; /* join error, exit without cleanup */
6042 }
fac6795d 6043
cf3af59e 6044exit_apps:
099e26bd
DG
6045 ret = pthread_join(reg_apps_thread, &status);
6046 if (ret != 0) {
76d7553f 6047 PERROR("pthread_join");
099e26bd
DG
6048 goto error; /* join error, exit without cleanup */
6049 }
6050
6051exit_reg_apps:
6052 ret = pthread_join(dispatch_thread, &status);
6053 if (ret != 0) {
76d7553f 6054 PERROR("pthread_join");
099e26bd
DG
6055 goto error; /* join error, exit without cleanup */
6056 }
6057
6058exit_dispatch:
cf3af59e
MD
6059 ret = pthread_join(client_thread, &status);
6060 if (ret != 0) {
76d7553f 6061 PERROR("pthread_join");
cf3af59e
MD
6062 goto error; /* join error, exit without cleanup */
6063 }
6064
3bd1e081 6065 ret = join_consumer_thread(&kconsumer_data);
cf3af59e 6066 if (ret != 0) {
76d7553f 6067 PERROR("join_consumer");
cf3af59e
MD
6068 goto error; /* join error, exit without cleanup */
6069 }
a88df331 6070
cf3af59e 6071exit_client:
44a5e5eb 6072exit_health:
a88df331 6073exit:
cf3af59e
MD
6074 /*
6075 * cleanup() is called when no other thread is running.
6076 */
f6a9efaa 6077 rcu_thread_online();
cf3af59e 6078 cleanup();
f6a9efaa
DG
6079 rcu_thread_offline();
6080 rcu_unregister_thread();
67e40797 6081 if (!ret) {
cf3af59e 6082 exit(EXIT_SUCCESS);
67e40797 6083 }
cf3af59e 6084error:
5e16da05 6085 exit(EXIT_FAILURE);
fac6795d 6086}
This page took 0.41495 seconds and 4 git commands to generate.