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