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