#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <sys/resource.h>
#include <inttypes.h>
#include <urcu/futex.h>
#include <urcu/uatomic.h>
#include <unistd.h>
#include <fcntl.h>
+#include <ctype.h>
#include <lttng/lttng.h>
#include <common/common.h>
#include <common/config/session-config.h>
#include <common/dynamic-buffer.h>
#include <common/buffer-view.h>
+#include <common/fd-tracker/utils.h>
#include <urcu/rculist.h>
+#include "version.h"
#include "cmd.h"
#include "ctf-trace.h"
#include "index.h"
/* command line options */
char *opt_output_path, *opt_working_directory;
-static int opt_daemon, opt_background;
+static int opt_daemon, opt_background, opt_print_version;
+int opt_group_output_by_session;
+int opt_group_output_by_host;
/*
* We need to wait for listener and live listener threads, as well as
*/
static struct relay_conn_queue relay_conn_queue;
+/* Cap of file desriptors to be in simultaneous use by the relay daemon. */
+static unsigned int lttng_opt_fd_cap;
+
/* Global relay stream hash table. */
struct lttng_ht *relay_streams_ht;
/* Relayd health monitoring */
struct health_app *health_relayd;
+/* Global fd tracker. */
+struct fd_tracker *the_fd_tracker;
+
static struct option long_options[] = {
{ "control-port", 1, 0, 'C', },
{ "data-port", 1, 0, 'D', },
{ "daemonize", 0, 0, 'd', },
{ "background", 0, 0, 'b', },
{ "group", 1, 0, 'g', },
+ { "fd-cap", 1, 0, '\0', },
{ "help", 0, 0, 'h', },
{ "output", 1, 0, 'o', },
{ "verbose", 0, 0, 'v', },
{ "config", 1, 0, 'f' },
{ "version", 0, 0, 'V' },
{ "working-directory", 1, 0, 'w', },
+ { "group-output-by-session", 0, 0, 's', },
+ { "group-output-by-host", 0, 0, 'p', },
{ NULL, 0, 0, 0, },
};
static const char *config_ignore_options[] = { "help", "config", "version" };
+static void print_version(void) {
+ fprintf(stdout, "%s\n", VERSION);
+}
+
+static void relayd_config_log(void)
+{
+ DBG("LTTng-relayd " VERSION " - " VERSION_NAME "%s%s",
+ GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION,
+ EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME);
+ if (EXTRA_VERSION_DESCRIPTION[0] != '\0') {
+ DBG("LTTng-relayd extra version description:\n\t" EXTRA_VERSION_DESCRIPTION "\n");
+ }
+ if (EXTRA_VERSION_PATCHES[0] != '\0') {
+ DBG("LTTng-relayd extra patches:\n\t" EXTRA_VERSION_PATCHES "\n");
+ }
+}
+
/*
* Take an option from the getopt output and set it in the right variable to be
* used later.
switch (opt) {
case 0:
- fprintf(stderr, "option %s", optname);
- if (arg) {
- fprintf(stderr, " with arg %s\n", arg);
+ if (!strcmp(optname, "fd-cap")) {
+ unsigned long v;
+
+ errno = 0;
+ v = strtoul(arg, NULL, 0);
+ if (errno != 0 || !isdigit(arg[0])) {
+ ERR("Wrong value in --fd-cap parameter: %s", arg);
+ ret = -1;
+ goto end;
+ }
+ if (v < DEFAULT_RELAYD_MINIMAL_FD_CAP) {
+ ERR("File descriptor cap must be set to at least %d",
+ DEFAULT_RELAYD_MINIMAL_FD_CAP);
+ }
+ if (v >= UINT_MAX) {
+ ERR("File descriptor cap overflow in --fd-cap parameter: %s", arg);
+ ret = -1;
+ goto end;
+ }
+ lttng_opt_fd_cap = (unsigned int) v;
+ DBG3("File descriptor cap set to %u", lttng_opt_fd_cap);
+
+ } else {
+ fprintf(stderr, "unknown option %s", optname);
+ if (arg) {
+ fprintf(stderr, " with arg %s\n", arg);
+ }
}
break;
case 'C':
}
exit(EXIT_FAILURE);
case 'V':
- fprintf(stdout, "%s\n", VERSION);
- exit(EXIT_SUCCESS);
+ opt_print_version = 1;
+ break;
case 'o':
if (lttng_is_setuid_setgid()) {
WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
}
}
break;
+ case 's':
+ if (opt_group_output_by_host) {
+ ERR("Cannot set --group-output-by-session, --group-output-by-host already defined");
+ exit(EXIT_FAILURE);
+ }
+ opt_group_output_by_session = 1;
+ break;
+ case 'p':
+ if (opt_group_output_by_session) {
+ ERR("Cannot set --group-output-by-host, --group-output-by-session already defined");
+ exit(EXIT_FAILURE);
+ }
+ opt_group_output_by_host = 1;
+ break;
default:
/* Unknown option or other error.
* Error is printed by getopt, just return */
goto exit;
}
}
+ if (lttng_opt_fd_cap == 0) {
+ int ret;
+ struct rlimit rlimit;
+
+ ret = getrlimit(RLIMIT_NOFILE, &rlimit);
+ if (ret) {
+ PERROR("Failed to get file descriptor limit");
+ retval = -1;
+ }
+
+ lttng_opt_fd_cap = rlimit.rlim_cur;
+ }
+
+ if (!opt_group_output_by_session && !opt_group_output_by_host) {
+ /* Group by host by default */
+ opt_group_output_by_host = 1;
+ }
exit:
free(optstring);
static void print_global_objects(void)
{
- rcu_register_thread();
-
print_viewer_streams();
print_relay_streams();
print_sessions();
-
- rcu_unregister_thread();
}
/*
free(opt_output_path);
/* Close thread quit pipes */
- utils_close_pipe(thread_quit_pipe);
+ (void) fd_tracker_util_pipe_close(the_fd_tracker, thread_quit_pipe);
uri_free(control_uri);
uri_free(data_uri);
if (tracing_group_name_override) {
free((void *) tracing_group_name);
}
+ fd_tracker_log(the_fd_tracker);
}
/*
*/
static int init_thread_quit_pipe(void)
{
- int ret;
-
- ret = utils_create_pipe_cloexec(thread_quit_pipe);
+ return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
+ "Quit pipe", thread_quit_pipe);
+}
- return ret;
+/*
+ * Init health quit pipe.
+ *
+ * Return -1 on error or 0 if all pipes are created.
+ */
+static int init_health_quit_pipe(void)
+{
+ return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
+ "Health quit pipe", health_quit_pipe);
}
/*
* Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
*/
-static int create_thread_poll_set(struct lttng_poll_event *events, int size)
+static int create_named_thread_poll_set(struct lttng_poll_event *events,
+ int size, const char *name)
{
int ret;
goto error;
}
- ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
- if (ret < 0) {
- goto error;
- }
+ ret = fd_tracker_util_poll_create(the_fd_tracker,
+ name, events, 1, LTTNG_CLOEXEC);
/* Add quit pipe */
ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
* Pass 3 as size here for the thread quit pipe, control and
* data socket.
*/
- ret = create_thread_poll_set(&events, 3);
+ ret = create_named_thread_poll_set(&events, 3, "Listener thread epoll");
if (ret < 0) {
goto error_create_poll;
}
error:
error_poll_add:
error_testpoint:
- lttng_poll_clean(&events);
+ (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
error_create_poll:
if (data_sock->fd >= 0) {
ret = data_sock->ops->close(data_sock);
switch (session->minor) {
case 1: /* LTTng sessiond 2.1. Allocates path_name and channel_name. */
ret = cmd_recv_stream_2_1(payload, &path_name,
- &channel_name);
+ &channel_name, session);
break;
case 2: /* LTTng sessiond 2.2. Allocates path_name and channel_name. */
default:
ret = cmd_recv_stream_2_2(payload, &path_name,
- &channel_name, &tracefile_size, &tracefile_count);
+ &channel_name, &tracefile_size, &tracefile_count,
+ session);
break;
}
if (ret < 0) {
goto relay_connections_ht_error;
}
- ret = create_thread_poll_set(&events, 2);
+ ret = create_named_thread_poll_set(&events, 2, "Worker thread epoll");
if (ret < 0) {
goto error_poll_create;
}
}
rcu_read_unlock();
- lttng_poll_clean(&events);
+ (void) fd_tracker_util_poll_clean(the_fd_tracker, &events);
error_poll_create:
lttng_ht_destroy(relay_connections_ht);
relay_connections_ht_error:
/* Close relay conn pipes */
- utils_close_pipe(relay_conn_pipe);
+ (void) fd_tracker_util_pipe_close(the_fd_tracker,
+ relay_conn_pipe);
if (err) {
DBG("Thread exited with error");
}
*/
static int create_relay_conn_pipe(void)
{
- int ret;
-
- ret = utils_create_pipe_cloexec(relay_conn_pipe);
-
- return ret;
+ return fd_tracker_util_pipe_open_cloexec(the_fd_tracker,
+ "Relayd connection pipe", relay_conn_pipe);
}
/*
goto exit_options;
}
+ relayd_config_log();
+
+ if (opt_print_version) {
+ print_version();
+ retval = 0;
+ goto exit_options;
+ }
+
+ ret = fclose(stdin);
+ if (ret) {
+ PERROR("Failed to close stdin");
+ goto exit_options;
+ }
/* Try to create directory if -o, --output is specified. */
if (opt_output_path) {
if (*opt_output_path != '/') {
/* Daemonize */
if (opt_daemon || opt_background) {
- int i;
-
ret = lttng_daemonize(&child_ppid, &recv_child_signal,
!opt_background);
if (ret < 0) {
retval = -1;
goto exit_options;
}
-
- /*
- * We are in the child. Make sure all other file
- * descriptors are closed, in case we are called with
- * more opened file descriptors than the standard ones.
- */
- for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
- (void) close(i);
- }
}
-
if (opt_working_directory) {
ret = utils_change_working_dir(opt_working_directory);
if (ret) {
goto exit_options;
}
}
+ /*
+ * The RCU thread registration (and use, through the fd-tracker's
+ * creation) is done after the daemonization to allow us to not
+ * deal with liburcu's fork() management as the call RCU needs to
+ * be restored.
+ */
+ rcu_register_thread();
+
+ the_fd_tracker = fd_tracker_create(lttng_opt_fd_cap);
+ if (!the_fd_tracker) {
+ retval = -1;
+ goto exit_options;
+ }
/* Initialize thread health monitoring */
health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
goto exit_init_data;
}
- ret = utils_create_pipe(health_quit_pipe);
+ ret = init_health_quit_pipe();
if (ret) {
retval = -1;
goto exit_health_quit_pipe;
}
exit_health_thread:
- utils_close_pipe(health_quit_pipe);
+ (void) fd_tracker_util_pipe_close(the_fd_tracker, health_quit_pipe);
exit_health_quit_pipe:
exit_init_data:
/* Ensure all prior call_rcu are done. */
rcu_barrier();
+ fd_tracker_destroy(the_fd_tracker);
+ rcu_unregister_thread();
+
if (!retval) {
exit(EXIT_SUCCESS);
} else {