From 0f907de1f20c91a2bd1a08ee4d50332d1958754b Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Thu, 28 Mar 2013 13:25:08 -0400 Subject: [PATCH] Relayd add_stream command handle tracefile rotation Extend the relayd_add_stream structure to pass the tracefile_size and tracefile_count parameters required to handle the tracefile rotation. Signed-off-by: Julien Desfossez Signed-off-by: David Goulet --- src/bin/lttng-consumerd/Makefile.am | 2 +- src/bin/lttng-relayd/Makefile.am | 5 +- src/bin/lttng-relayd/cmd-2-1.c | 61 +++++++ src/bin/lttng-relayd/cmd-2-1.h | 26 +++ src/bin/lttng-relayd/cmd-2-2.c | 64 +++++++ src/bin/lttng-relayd/cmd-2-2.h | 26 +++ src/bin/lttng-relayd/cmd-generic.c | 48 +++++ src/bin/lttng-relayd/cmd-generic.h | 26 +++ src/bin/lttng-relayd/cmd.h | 26 +++ src/bin/lttng-relayd/lttng-relayd.h | 17 +- src/bin/lttng-relayd/main.c | 183 ++++++------------- src/bin/lttng-relayd/utils.c | 104 +++++++++++ src/bin/lttng-relayd/utils.h | 24 +++ src/common/kernel-consumer/kernel-consumer.c | 4 +- src/common/relayd/relayd.c | 31 +++- src/common/relayd/relayd.h | 3 +- src/common/sessiond-comm/relayd.h | 11 ++ src/common/ust-consumer/ust-consumer.c | 4 +- src/common/utils.c | 3 +- 19 files changed, 527 insertions(+), 141 deletions(-) create mode 100644 src/bin/lttng-relayd/cmd-2-1.c create mode 100644 src/bin/lttng-relayd/cmd-2-1.h create mode 100644 src/bin/lttng-relayd/cmd-2-2.c create mode 100644 src/bin/lttng-relayd/cmd-2-2.h create mode 100644 src/bin/lttng-relayd/cmd-generic.c create mode 100644 src/bin/lttng-relayd/cmd-generic.h create mode 100644 src/bin/lttng-relayd/cmd.h create mode 100644 src/bin/lttng-relayd/utils.c create mode 100644 src/bin/lttng-relayd/utils.h diff --git a/src/bin/lttng-consumerd/Makefile.am b/src/bin/lttng-consumerd/Makefile.am index a395c0b37..a418eb4ac 100644 --- a/src/bin/lttng-consumerd/Makefile.am +++ b/src/bin/lttng-consumerd/Makefile.am @@ -4,7 +4,7 @@ lttnglibexec_PROGRAMS = lttng-consumerd lttng_consumerd_SOURCES = lttng-consumerd.c lttng-consumerd.h -lttng_consumerd_LDADD = \ +lttng_consumerd_LDADD = -lrt \ $(top_builddir)/src/common/libconsumer.la \ $(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la \ $(top_builddir)/src/common/libcommon.la diff --git a/src/bin/lttng-relayd/Makefile.am b/src/bin/lttng-relayd/Makefile.am index a04b910f6..57045463a 100644 --- a/src/bin/lttng-relayd/Makefile.am +++ b/src/bin/lttng-relayd/Makefile.am @@ -6,7 +6,10 @@ AM_CFLAGS = -fno-strict-aliasing bin_PROGRAMS = lttng-relayd -lttng_relayd_SOURCES = main.c lttng-relayd.h +lttng_relayd_SOURCES = main.c lttng-relayd.h utils.h utils.c \ + cmd-generic.c cmd-generic.h \ + cmd-2-1.c cmd-2-1.h \ + cmd-2-2.c cmd-2-2.h # link on liblttngctl for check if relayd is already alive. lttng_relayd_LDADD = -lrt -lurcu-common -lurcu \ diff --git a/src/bin/lttng-relayd/cmd-2-1.c b/src/bin/lttng-relayd/cmd-2-1.c new file mode 100644 index 000000000..1942dee0a --- /dev/null +++ b/src/bin/lttng-relayd/cmd-2-1.c @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _GNU_SOURCE +#include +#include + +#include +#include + +#include "cmd-generic.h" +#include "cmd-2-1.h" +#include "utils.h" + +int cmd_recv_stream_2_1(struct relay_command *cmd, struct relay_stream *stream) +{ + int ret; + struct lttcomm_relayd_add_stream stream_info; + + assert(cmd); + assert(stream); + + ret = cmd_recv(cmd->sock, &stream_info, sizeof(stream_info)); + if (ret < 0) { + ERR("Unable to recv stream version 2.1"); + goto error; + } + + stream->path_name = create_output_path(stream_info.pathname); + if (stream->path_name == NULL) { + PERROR("Path name allocation"); + ret = -ENOMEM; + goto error; + } + + stream->channel_name = strdup(stream_info.channel_name); + if (stream->channel_name == NULL) { + ret = -errno; + PERROR("Path name allocation"); + goto error; + } + ret = 0; + +error: + return ret; +} diff --git a/src/bin/lttng-relayd/cmd-2-1.h b/src/bin/lttng-relayd/cmd-2-1.h new file mode 100644 index 000000000..26bc0c1ba --- /dev/null +++ b/src/bin/lttng-relayd/cmd-2-1.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef RELAYD_CMD_2_1_H +#define RELAYD_CMD_2_1_H + +#include "lttng-relayd.h" + +int cmd_recv_stream_2_1(struct relay_command *cmd, struct relay_stream *stream); + +#endif /* RELAYD_CMD_2_1_H */ diff --git a/src/bin/lttng-relayd/cmd-2-2.c b/src/bin/lttng-relayd/cmd-2-2.c new file mode 100644 index 000000000..0450fbf9d --- /dev/null +++ b/src/bin/lttng-relayd/cmd-2-2.c @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _GNU_SOURCE +#include +#include + +#include +#include + +#include "cmd-generic.h" +#include "cmd-2-1.h" +#include "utils.h" + +int cmd_recv_stream_2_2(struct relay_command *cmd, struct relay_stream *stream) +{ + int ret; + struct lttcomm_relayd_add_stream_2_2 stream_info; + + assert(cmd); + assert(stream); + + ret = cmd_recv(cmd->sock, &stream_info, sizeof(stream_info)); + if (ret < 0) { + ERR("Unable to recv stream version 2.2"); + goto error; + } + + stream->path_name = create_output_path(stream_info.pathname); + if (stream->path_name == NULL) { + PERROR("Path name allocation"); + ret = -ENOMEM; + goto error; + } + + stream->channel_name = strdup(stream_info.channel_name); + if (stream->channel_name == NULL) { + ret = -errno; + PERROR("Path name allocation"); + goto error; + } + + stream->tracefile_size = be64toh(stream_info.tracefile_size); + stream->tracefile_count = be64toh(stream_info.tracefile_count); + ret = 0; + +error: + return ret; +} diff --git a/src/bin/lttng-relayd/cmd-2-2.h b/src/bin/lttng-relayd/cmd-2-2.h new file mode 100644 index 000000000..9a6312777 --- /dev/null +++ b/src/bin/lttng-relayd/cmd-2-2.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef RELAYD_CMD_2_2_H +#define RELAYD_CMD_2_2_H + +#include "lttng-relayd.h" + +int cmd_recv_stream_2_2(struct relay_command *cmd, struct relay_stream *stream); + +#endif /* RELAYD_CMD_2_2_H */ diff --git a/src/bin/lttng-relayd/cmd-generic.c b/src/bin/lttng-relayd/cmd-generic.c new file mode 100644 index 000000000..1122bfe20 --- /dev/null +++ b/src/bin/lttng-relayd/cmd-generic.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _GNU_SOURCE +#include + +#include + +#include "cmd-generic.h" + +int cmd_recv(struct lttcomm_sock *sock, void *buf, size_t len) +{ + int ret; + + assert(sock); + assert(buf); + + ret = sock->ops->recvmsg(sock, buf, len, 0); + if (ret < len) { + if (ret == 0) { + /* Orderly shutdown. Not necessary to print an error. */ + DBG("Socket %d did an orderly shutdown", sock->fd); + } else { + ERR("Relay didn't receive valid add_stream struct size. " + "Expected %lu, got %d", len, ret); + } + ret = -1; + goto error; + } + +error: + return ret; +} diff --git a/src/bin/lttng-relayd/cmd-generic.h b/src/bin/lttng-relayd/cmd-generic.h new file mode 100644 index 000000000..30b96b421 --- /dev/null +++ b/src/bin/lttng-relayd/cmd-generic.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef RELAYD_CMD_GENERIC_H +#define RELAYD_CMD_GENERIC_H + +#include + +int cmd_recv(struct lttcomm_sock *sock, void *buf, size_t len); + +#endif /* RELAYD_CMD_GENERIC_H */ diff --git a/src/bin/lttng-relayd/cmd.h b/src/bin/lttng-relayd/cmd.h new file mode 100644 index 000000000..14634954b --- /dev/null +++ b/src/bin/lttng-relayd/cmd.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef RELAYD_CMD_H +#define RELAYD_CMD_H + +#include "cmd-generic.h" +#include "cmd-2-1.h" +#include "cmd-2-2.h" + +#endif /* RELAYD_CMD_H */ diff --git a/src/bin/lttng-relayd/lttng-relayd.h b/src/bin/lttng-relayd/lttng-relayd.h index e6ca5ebbb..9bbafe1dd 100644 --- a/src/bin/lttng-relayd/lttng-relayd.h +++ b/src/bin/lttng-relayd/lttng-relayd.h @@ -22,6 +22,7 @@ #define _LGPL_SOURCE #include #include +#include /* * Queue used to enqueue relay requests @@ -47,9 +48,6 @@ struct relay_session { */ uint64_t id; struct lttcomm_sock *sock; - /* protocol version to use for this session */ - uint32_t major; - uint32_t minor; }; /* @@ -63,6 +61,14 @@ struct relay_stream { struct rcu_head rcu_node; int fd; + char *path_name; + char *channel_name; + /* on-disk circular buffer of tracefiles */ + uint64_t tracefile_size; + uint64_t tracefile_size_current; + uint64_t tracefile_count; + uint64_t tracefile_count_current; + /* Information telling us when to close the stream */ unsigned int close_flag:1; uint64_t last_net_seq_num; @@ -82,6 +88,11 @@ struct relay_command { struct rcu_head rcu_node; enum connection_type type; unsigned int version_check_done:1; + /* protocol version to use for this session */ + uint32_t major; + uint32_t minor; }; +extern char *opt_output_path; + #endif /* LTTNG_RELAYD_H */ diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index 5f6f9cfd8..9a2b3bda7 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -47,16 +47,17 @@ #include #include #include -#include #include #include #include +#include "cmd.h" +#include "utils.h" #include "lttng-relayd.h" /* command line options */ +char *opt_output_path; static int opt_daemon; -static char *opt_output_path; static struct lttng_uri *control_uri; static struct lttng_uri *data_uri; @@ -685,82 +686,6 @@ error: return NULL; } -/* - * config_get_default_path - * - * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer. - */ -static -char *config_get_default_path(void) -{ - return getenv("HOME"); -} - -static -char *create_output_path_auto(char *path_name) -{ - int ret; - char *traces_path = NULL; - char *alloc_path = NULL; - char *default_path; - - default_path = config_get_default_path(); - if (default_path == NULL) { - ERR("Home path not found.\n \ - Please specify an output path using -o, --output PATH"); - goto exit; - } - alloc_path = strdup(default_path); - if (alloc_path == NULL) { - PERROR("Path allocation"); - goto exit; - } - ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME - "/%s", alloc_path, path_name); - if (ret < 0) { - PERROR("asprintf trace dir name"); - goto exit; - } -exit: - free(alloc_path); - return traces_path; -} - -static -char *create_output_path_noauto(char *path_name) -{ - int ret; - char *traces_path = NULL; - char *full_path; - - full_path = utils_expand_path(opt_output_path); - if (!full_path) { - goto exit; - } - - ret = asprintf(&traces_path, "%s/%s", full_path, path_name); - if (ret < 0) { - PERROR("asprintf trace dir name"); - goto exit; - } -exit: - free(full_path); - return traces_path; -} - -/* - * create_output_path: create the output trace directory - */ -static -char *create_output_path(char *path_name) -{ - if (opt_output_path == NULL) { - return create_output_path_auto(path_name); - } else { - return create_output_path_noauto(path_name); - } -} - /* * Get stream from stream id. * Need to be called with RCU read-side lock held. @@ -794,6 +719,8 @@ void deferred_free_stream(struct rcu_head *head) { struct relay_stream *stream = caa_container_of(head, struct relay_stream, rcu_node); + free(stream->path_name); + free(stream->channel_name); free(stream); } @@ -895,10 +822,8 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, struct relay_command *cmd, struct lttng_ht *streams_ht) { struct relay_session *session = cmd->session; - struct lttcomm_relayd_add_stream stream_info; struct relay_stream *stream = NULL; struct lttcomm_relayd_status_stream reply; - char *path = NULL, *root_path = NULL; int ret, send_ret; if (!session || cmd->version_check_done == 0) { @@ -907,18 +832,6 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, goto end_no_session; } - ret = cmd->sock->ops->recvmsg(cmd->sock, &stream_info, - sizeof(struct lttcomm_relayd_add_stream), 0); - if (ret < sizeof(struct lttcomm_relayd_add_stream)) { - if (ret == 0) { - /* Orderly shutdown. Not necessary to print an error. */ - DBG("Socket %d did an orderly shutdown", cmd->sock->fd); - } else { - ERR("Relay didn't receive valid add_stream struct size : %d", ret); - } - ret = -1; - goto end_no_session; - } stream = zmalloc(sizeof(struct relay_stream)); if (stream == NULL) { PERROR("relay stream zmalloc"); @@ -926,49 +839,51 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr, goto end_no_session; } + switch (cmd->minor) { + case 1: /* LTTng sessiond 2.1 */ + ret = cmd_recv_stream_2_1(cmd, stream); + break; + case 2: /* LTTng sessiond 2.2 */ + default: + ret = cmd_recv_stream_2_2(cmd, stream); + break; + } + if (ret < 0) { + goto err_free_stream; + } + rcu_read_lock(); stream->stream_handle = ++last_relay_stream_id; stream->prev_seq = -1ULL; stream->session = session; - root_path = create_output_path(stream_info.pathname); - if (!root_path) { - ret = -1; - goto end; - } - ret = utils_mkdir_recursive(root_path, S_IRWXU | S_IRWXG); + ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG); if (ret < 0) { ERR("relay creating output directory"); goto end; } - ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name); - if (ret < 0) { - PERROR("asprintf stream path"); - path = NULL; - goto end; - } - - ret = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); + ret = utils_create_stream_file(stream->path_name, stream->channel_name, + stream->tracefile_size, 0, getuid(), getgid()); if (ret < 0) { - PERROR("Relay creating trace file"); + ERR("Create output file"); goto end; } - stream->fd = ret; - DBG("Tracefile %s created", path); + if (stream->tracefile_size) { + DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); + } else { + DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); + } lttng_ht_node_init_ulong(&stream->stream_n, (unsigned long) stream->stream_handle); lttng_ht_add_unique_ulong(streams_ht, &stream->stream_n); - DBG("Relay new stream added %s", stream_info.channel_name); + DBG("Relay new stream added %s", stream->channel_name); end: - free(path); - free(root_path); - reply.handle = htobe64(stream->stream_handle); /* send the session id to the client or a negative return code on error */ if (ret < 0) { @@ -989,6 +904,12 @@ end: end_no_session: return ret; + +err_free_stream: + free(stream->path_name); + free(stream->channel_name); + free(stream); + return ret; } /* @@ -1278,6 +1199,14 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, goto end; } + cmd->major = reply.major; + /* We adapt to the lowest compatible version */ + if (reply.minor <= be32toh(msg.minor)) { + cmd->minor = reply.minor; + } else { + cmd->minor = be32toh(msg.minor); + } + reply.major = htobe32(reply.major); reply.minor = htobe32(reply.minor); ret = cmd->sock->ops->sendmsg(cmd->sock, &reply, @@ -1286,18 +1215,8 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr, ERR("Relay sending version"); } -#if 0 - cmd->session->major = reply.major; - /* We adapt to the lowest compatible version */ - if (reply.minor <= be32toh(msg.minor)) { - cmd->session->minor = reply.minor; - } else { - cmd->session->minor = be32toh(msg.minor); - } - - DBG("Version check done using protocol %u.%u", cmd->session->major, - cmd->session->minor); -#endif + DBG("Version check done using protocol %u.%u", cmd->major, + cmd->minor); end: return ret; @@ -1701,6 +1620,20 @@ int relay_process_data(struct relay_command *cmd, struct lttng_ht *streams_ht) goto end_unlock; } + if (stream->tracefile_size > 0 && + (stream->tracefile_size_current + data_size) > + stream->tracefile_size) { + ret = utils_rotate_stream_file(stream->path_name, + stream->channel_name, stream->tracefile_size, + stream->tracefile_count, getuid(), getgid(), + stream->fd, &(stream->tracefile_count_current)); + if (ret < 0) { + ERR("Rotating output file"); + goto end; + } + stream->fd = ret; + } + stream->tracefile_size_current += data_size; do { ret = write(stream->fd, data_buffer, data_size); } while (ret < 0 && errno == EINTR); diff --git a/src/bin/lttng-relayd/utils.c b/src/bin/lttng-relayd/utils.c new file mode 100644 index 000000000..ad13d3277 --- /dev/null +++ b/src/bin/lttng-relayd/utils.c @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2013 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +#include +#include +#include + +#include "lttng-relayd.h" +#include "utils.h" + +/* + * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer. + */ +static char *get_default_path(void) +{ + return getenv("HOME"); +} + +static char *create_output_path_auto(char *path_name) +{ + int ret; + char *traces_path = NULL; + char *alloc_path = NULL; + char *default_path; + + default_path = get_default_path(); + if (default_path == NULL) { + ERR("Home path not found.\n \ + Please specify an output path using -o, --output PATH"); + goto exit; + } + alloc_path = strdup(default_path); + if (alloc_path == NULL) { + PERROR("Path allocation"); + goto exit; + } + ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME + "/%s", alloc_path, path_name); + if (ret < 0) { + PERROR("asprintf trace dir name"); + goto exit; + } +exit: + free(alloc_path); + return traces_path; +} + +static char *create_output_path_noauto(char *path_name) +{ + int ret; + char *traces_path = NULL; + char *full_path; + + full_path = utils_expand_path(opt_output_path); + if (!full_path) { + goto exit; + } + + ret = asprintf(&traces_path, "%s/%s", full_path, path_name); + if (ret < 0) { + PERROR("asprintf trace dir name"); + goto exit; + } +exit: + free(full_path); + return traces_path; +} + +/* + * Create the output trace directory path name string. + * + * Return the allocated string containing the path name or else NULL. + */ +char *create_output_path(char *path_name) +{ + assert(path_name); + + if (opt_output_path == NULL) { + return create_output_path_auto(path_name); + } else { + return create_output_path_noauto(path_name); + } +} diff --git a/src/bin/lttng-relayd/utils.h b/src/bin/lttng-relayd/utils.h new file mode 100644 index 000000000..de1521d6f --- /dev/null +++ b/src/bin/lttng-relayd/utils.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2012 - Julien Desfossez + * David Goulet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2 only, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef RELAYD_UTILS_H +#define RELAYD_UTILS_H + +char *create_output_path(char *path_name); + +#endif /* RELAYD_UTILS_H */ diff --git a/src/common/kernel-consumer/kernel-consumer.c b/src/common/kernel-consumer/kernel-consumer.c index bc3ddc930..2cf9ac1a8 100644 --- a/src/common/kernel-consumer/kernel-consumer.c +++ b/src/common/kernel-consumer/kernel-consumer.c @@ -262,7 +262,9 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, pthread_mutex_lock(&relayd->ctrl_sock_mutex); ret = relayd_add_stream(&relayd->control_sock, new_stream->name, new_stream->chan->pathname, - &new_stream->relayd_stream_id); + &new_stream->relayd_stream_id, + new_stream->chan->tracefile_size, + new_stream->chan->tracefile_count); pthread_mutex_unlock(&relayd->ctrl_sock_mutex); if (ret < 0) { consumer_del_stream(new_stream, NULL); diff --git a/src/common/relayd/relayd.c b/src/common/relayd/relayd.c index bed0933d1..82bcf5ff6 100644 --- a/src/common/relayd/relayd.c +++ b/src/common/relayd/relayd.c @@ -161,10 +161,12 @@ error: * On success return 0 else return ret_code negative value. */ int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, - const char *pathname, uint64_t *stream_id) + const char *pathname, uint64_t *stream_id, + uint64_t tracefile_size, uint64_t tracefile_count) { int ret; struct lttcomm_relayd_add_stream msg; + struct lttcomm_relayd_add_stream_2_2 msg_2_2; struct lttcomm_relayd_status_stream reply; /* Code flow error. Safety net. */ @@ -174,13 +176,28 @@ int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_nam DBG("Relayd adding stream for channel name %s", channel_name); - strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name)); - strncpy(msg.pathname, pathname, sizeof(msg.pathname)); + /* Compat with relayd 2.1 */ + if (rsock->minor == 1) { + strncpy(msg.channel_name, channel_name, sizeof(msg.channel_name)); + strncpy(msg.pathname, pathname, sizeof(msg.pathname)); - /* Send command */ - ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); - if (ret < 0) { - goto error; + /* Send command */ + ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); + if (ret < 0) { + goto error; + } + } else { + /* Compat with relayd 2.2+ */ + strncpy(msg_2_2.channel_name, channel_name, sizeof(msg_2_2.channel_name)); + strncpy(msg_2_2.pathname, pathname, sizeof(msg_2_2.pathname)); + msg_2_2.tracefile_size = htobe64(tracefile_size); + msg_2_2.tracefile_count = htobe64(tracefile_count); + + /* Send command */ + ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg_2_2, sizeof(msg_2_2), 0); + if (ret < 0) { + goto error; + } } /* Waiting for reply */ diff --git a/src/common/relayd/relayd.h b/src/common/relayd/relayd.h index bbb6f9385..dd435e905 100644 --- a/src/common/relayd/relayd.h +++ b/src/common/relayd/relayd.h @@ -27,7 +27,8 @@ int relayd_connect(struct lttcomm_relayd_sock *sock); int relayd_close(struct lttcomm_relayd_sock *sock); int relayd_create_session(struct lttcomm_relayd_sock *sock, uint64_t *session_id); int relayd_add_stream(struct lttcomm_relayd_sock *sock, const char *channel_name, - const char *pathname, uint64_t *stream_id); + const char *pathname, uint64_t *stream_id, + uint64_t tracefile_size, uint64_t tracefile_count); int relayd_send_close_stream(struct lttcomm_relayd_sock *sock, uint64_t stream_id, uint64_t last_net_seq_num); int relayd_version_check(struct lttcomm_relayd_sock *sock); diff --git a/src/common/sessiond-comm/relayd.h b/src/common/sessiond-comm/relayd.h index 0a8f53b4a..624d1b27d 100644 --- a/src/common/sessiond-comm/relayd.h +++ b/src/common/sessiond-comm/relayd.h @@ -69,6 +69,17 @@ struct lttcomm_relayd_add_stream { char pathname[PATH_MAX]; } LTTNG_PACKED; +/* + * Used to add a stream on the relay daemon. + * Protocol version 2.2 + */ +struct lttcomm_relayd_add_stream_2_2 { + char channel_name[DEFAULT_STREAM_NAME_LEN]; + char pathname[PATH_MAX]; + uint64_t tracefile_size; + uint64_t tracefile_count; +} LTTNG_PACKED; + /* * Answer from an add stream command. */ diff --git a/src/common/ust-consumer/ust-consumer.c b/src/common/ust-consumer/ust-consumer.c index bc0f58538..5581dce19 100644 --- a/src/common/ust-consumer/ust-consumer.c +++ b/src/common/ust-consumer/ust-consumer.c @@ -223,7 +223,9 @@ static int send_stream_to_relayd(struct lttng_consumer_stream *stream) pthread_mutex_lock(&relayd->ctrl_sock_mutex); /* Add stream on the relayd */ ret = relayd_add_stream(&relayd->control_sock, stream->name, - stream->chan->pathname, &stream->relayd_stream_id); + stream->chan->pathname, &stream->relayd_stream_id, + stream->chan->tracefile_size, + stream->chan->tracefile_count); pthread_mutex_unlock(&relayd->ctrl_sock_mutex); if (ret < 0) { goto error; diff --git a/src/common/utils.c b/src/common/utils.c index 9f3a8f942..b16cdc97c 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -340,8 +340,9 @@ int utils_create_stream_file(char *path_name, char *file_name, uint64_t size, path = full_path; } + /* Open with 660 mode */ out_fd = run_as_open(path, O_WRONLY | O_CREAT | O_TRUNC, - S_IRWXU | S_IRWXG | S_IRWXO, uid, gid); + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, uid, gid); if (out_fd < 0) { PERROR("open stream path %s", path); goto error_open; -- 2.34.1