From 00fb02ace5151a6546f4e97e5439512913a50e68 Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Mon, 11 Dec 2017 15:07:23 -0500 Subject: [PATCH] Command to rename a folder MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This new command allows the sessiond to ask the consumer or relay to rename a folder. This will be useful for the session rotation to rename a completed chunk. Signed-off-by: Julien Desfossez Signed-off-by: Jérémie Galarneau --- src/bin/lttng-relayd/main.c | 172 ++++++++++++++++++- src/bin/lttng-sessiond/consumer.c | 56 ++++++ src/bin/lttng-sessiond/consumer.h | 3 + src/common/consumer/consumer.c | 57 ++++++ src/common/consumer/consumer.h | 3 + src/common/kernel-consumer/kernel-consumer.c | 25 +++ src/common/relayd/relayd.c | 67 +++++++- src/common/relayd/relayd.h | 2 + src/common/sessiond-comm/relayd.h | 7 + src/common/sessiond-comm/sessiond-comm.h | 10 ++ src/common/ust-consumer/ust-consumer.c | 23 +++ 11 files changed, 423 insertions(+), 2 deletions(-) diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index c4c609a4f..4c99b56f6 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -2137,7 +2137,7 @@ static int relay_mkdir(struct lttcomm_relayd_hdr *recv_hdr, char *path = NULL; if (!session || !conn->version_check_done) { - ERR("Trying to rename before version check"); + ERR("Trying to create a directory before version check"); ret = -1; goto end_no_session; } @@ -2234,6 +2234,173 @@ end_no_session: return ret; } +static int validate_rotate_rename_path_length(const char *path_type, + uint32_t path_length) +{ + int ret = 0; + + if (path_length > LTTNG_PATH_MAX) { + ret = -ENAMETOOLONG; + ERR("rotate rename \"%s\" path name length (%" PRIu32 " bytes) exceeds the allowed size of %i bytes", + path_type, path_length, LTTNG_PATH_MAX); + } else if (path_length == 0) { + ret = -EINVAL; + ERR("rotate rename \"%s\" path name has an illegal length of 0", path_type); + } + return ret; +} + +/* + * relay_rotate_rename: rename the trace folder after a rotation is + * completed. We are not closing any fd here, just moving the folder, so it + * works even if data is still in-flight. + */ +static int relay_rotate_rename(struct lttcomm_relayd_hdr *recv_hdr, + struct relay_connection *conn) +{ + int ret; + ssize_t network_ret; + struct relay_session *session = conn->session; + struct lttcomm_relayd_generic_reply reply; + struct lttcomm_relayd_rotate_rename header; + char *received_paths = NULL; + size_t received_paths_size; + const char *received_old_path, *received_new_path; + char *complete_old_path = NULL, *complete_new_path = NULL; + + if (!session || !conn->version_check_done) { + ERR("Trying to rename a trace folder before version check"); + ret = -1; + goto end_no_reply; + } + + if (session->major == 2 && session->minor < 11) { + ERR("relay_rotate_rename command is unsupported before LTTng 2.11"); + ret = -1; + goto end_no_reply; + } + + network_ret = conn->sock->ops->recvmsg(conn->sock, &header, + sizeof(header), 0); + if (network_ret < (ssize_t) sizeof(header)) { + if (network_ret == 0) { + /* Orderly shutdown. Not necessary to print an error. */ + DBG("Socket %d did an orderly shutdown", + conn->sock->fd); + } else { + ERR("Relay didn't receive a valid rotate_rename command header: expected %zu bytes, recvmsg() returned %zi", + sizeof(header), network_ret); + } + ret = -1; + goto end_no_reply; + } + + header.old_path_length = be32toh(header.old_path_length); + header.new_path_length = be32toh(header.new_path_length); + received_paths_size = header.old_path_length + header.new_path_length; + + /* Ensure the paths don't exceed their allowed size. */ + ret = validate_rotate_rename_path_length("old", header.old_path_length); + if (ret) { + goto end; + } + ret = validate_rotate_rename_path_length("new", header.new_path_length); + if (ret) { + goto end; + } + + received_paths = zmalloc(received_paths_size); + if (!received_paths) { + PERROR("Could not allocate rotate commands paths reception buffer"); + ret = -1; + goto end; + } + + network_ret = conn->sock->ops->recvmsg(conn->sock, received_paths, + received_paths_size, 0); + if (network_ret < (ssize_t) received_paths_size) { + if (network_ret == 0) { + /* Orderly shutdown. Not necessary to print an error. */ + DBG("Socket %d did an orderly shutdown", + conn->sock->fd); + } else { + ERR("Relay failed to received rename command paths (%zu bytes): recvmsg() returned %zi", + received_paths_size, network_ret); + } + ret = -1; + goto end_no_reply; + } + + /* Validate that both paths received are NULL terminated. */ + if (received_paths[header.old_path_length - 1] != '\0') { + ERR("relay_rotate_rename command's \"old\" path is invalid (not NULL terminated)"); + ret = -1; + goto end; + } + if (received_paths[received_paths_size - 1] != '\0') { + ERR("relay_rotate_rename command's \"new\" path is invalid (not NULL terminated)"); + ret = -1; + goto end; + } + + received_old_path = received_paths; + received_new_path = received_paths + header.old_path_length; + + complete_old_path = create_output_path(received_old_path); + if (!complete_old_path) { + ERR("Failed to build old output path in rotate_rename command"); + ret = -1; + goto end; + } + + complete_new_path = create_output_path(received_new_path); + if (!complete_new_path) { + ERR("Failed to build new output path in rotate_rename command"); + ret = -1; + goto end; + } + + ret = utils_mkdir_recursive(complete_new_path, S_IRWXU | S_IRWXG, + -1, -1); + if (ret < 0) { + ERR("Failed to mkdir() rotate_rename's \"new\" output directory at \"%s\"", + complete_new_path); + goto end; + } + + /* + * If a domain has not yet created its channel, the domain-specific + * folder might not exist, but this is not an error. + */ + ret = rename(complete_old_path, complete_new_path); + if (ret < 0 && errno != ENOENT) { + PERROR("Renaming chunk in rotate_rename command from \"%s\" to \"%s\"", + complete_old_path, complete_new_path); + goto end; + } + ret = 0; + +end: + memset(&reply, 0, sizeof(reply)); + if (ret < 0) { + reply.ret_code = htobe32(LTTNG_ERR_UNK); + } else { + reply.ret_code = htobe32(LTTNG_OK); + } + network_ret = conn->sock->ops->sendmsg(conn->sock, &reply, + sizeof(struct lttcomm_relayd_generic_reply), 0); + if (network_ret < sizeof(struct lttcomm_relayd_generic_reply)) { + ERR("Relay sending stream id"); + ret = -1; + } + +end_no_reply: + free(received_paths); + free(complete_old_path); + free(complete_new_path); + return ret; +} + /* * Process the commands received on the control socket */ @@ -2282,6 +2449,9 @@ static int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr, case RELAYD_RESET_METADATA: ret = relay_reset_metadata(recv_hdr, conn); break; + case RELAYD_ROTATE_RENAME: + ret = relay_rotate_rename(recv_hdr, conn); + break; case RELAYD_MKDIR: ret = relay_mkdir(recv_hdr, conn); break; diff --git a/src/bin/lttng-sessiond/consumer.c b/src/bin/lttng-sessiond/consumer.c index 473573092..8727ce377 100644 --- a/src/bin/lttng-sessiond/consumer.c +++ b/src/bin/lttng-sessiond/consumer.c @@ -1580,6 +1580,62 @@ end: return ret; } +int consumer_rotate_rename(struct consumer_socket *socket, uint64_t session_id, + const struct consumer_output *output, const char *old_path, + const char *new_path, uid_t uid, gid_t gid) +{ + int ret; + struct lttcomm_consumer_msg msg; + size_t old_path_length, new_path_length; + + assert(socket); + assert(old_path); + assert(new_path); + + DBG("Consumer rotate rename session %" PRIu64 ", old path = \"%s\", new_path = \"%s\"", + session_id, old_path, new_path); + + old_path_length = strlen(old_path); + if (old_path_length >= sizeof(msg.u.rotate_rename.old_path)) { + ERR("consumer_rotate_rename: old path length (%zu bytes) exceeds the maximal length allowed by the consumer protocol (%zu bytes)", + old_path_length + 1, sizeof(msg.u.rotate_rename.old_path)); + ret = -1; + goto error; + } + + new_path_length = strlen(new_path); + if (new_path_length >= sizeof(msg.u.rotate_rename.new_path)) { + ERR("consumer_rotate_rename: new path length (%zu bytes) exceeds the maximal length allowed by the consumer protocol (%zu bytes)", + new_path_length + 1, sizeof(msg.u.rotate_rename.new_path)); + ret = -1; + goto error; + } + + memset(&msg, 0, sizeof(msg)); + msg.cmd_type = LTTNG_CONSUMER_ROTATE_RENAME; + msg.u.rotate_rename.session_id = session_id; + msg.u.rotate_rename.uid = uid; + msg.u.rotate_rename.gid = gid; + strcpy(msg.u.rotate_rename.old_path, old_path); + strcpy(msg.u.rotate_rename.new_path, new_path); + + if (output->type == CONSUMER_DST_NET) { + msg.u.rotate_rename.relayd_id = output->net_seq_index; + } else { + msg.u.rotate_rename.relayd_id = -1ULL; + } + + health_code_update(); + ret = consumer_send_msg(socket, &msg); + if (ret < 0) { + goto error; + } + +error: + health_code_update(); + return ret; +} + /* * Ask the consumer to create a directory. * diff --git a/src/bin/lttng-sessiond/consumer.h b/src/bin/lttng-sessiond/consumer.h index 1fe7c89b5..691aad9f6 100644 --- a/src/bin/lttng-sessiond/consumer.h +++ b/src/bin/lttng-sessiond/consumer.h @@ -315,6 +315,9 @@ int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key, struct snapshot_output *output, int metadata, uid_t uid, gid_t gid, const char *session_path, int wait, uint64_t nb_packets_per_stream); +int consumer_rotate_rename(struct consumer_socket *socket, uint64_t session_id, + const struct consumer_output *output, const char *old_path, + const char *new_path, uid_t uid, gid_t gid); int consumer_mkdir(struct consumer_socket *socket, uint64_t session_id, const struct consumer_output *output, const char *path, uid_t uid, gid_t gid); diff --git a/src/common/consumer/consumer.c b/src/common/consumer/consumer.c index b449c1637..820427aa9 100644 --- a/src/common/consumer/consumer.c +++ b/src/common/consumer/consumer.c @@ -3784,6 +3784,63 @@ unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos, return start_pos; } +static +int rotate_rename_local(const char *old_path, const char *new_path, + uid_t uid, gid_t gid) +{ + int ret; + + assert(old_path); + assert(new_path); + + ret = utils_mkdir_recursive(new_path, S_IRWXU | S_IRWXG, uid, gid); + if (ret < 0) { + ERR("Create directory on rotate"); + goto end; + } + + ret = rename(old_path, new_path); + if (ret < 0 && errno != ENOENT) { + PERROR("Rename completed rotation chunk"); + goto end; + } + + ret = 0; +end: + return ret; +} + +static +int rotate_rename_relay(const char *old_path, const char *new_path, + uint64_t relayd_id) +{ + int ret; + struct consumer_relayd_sock_pair *relayd; + + relayd = consumer_find_relayd(relayd_id); + if (!relayd) { + ERR("Failed to find relayd while running rotate_rename_relay command"); + ret = -1; + goto end; + } + + pthread_mutex_lock(&relayd->ctrl_sock_mutex); + ret = relayd_rotate_rename(&relayd->control_sock, old_path, new_path); + pthread_mutex_unlock(&relayd->ctrl_sock_mutex); +end: + return ret; +} + +int lttng_consumer_rotate_rename(const char *old_path, const char *new_path, + uid_t uid, gid_t gid, uint64_t relayd_id) +{ + if (relayd_id != -1ULL) { + return rotate_rename_relay(old_path, new_path, relayd_id); + } else { + return rotate_rename_local(old_path, new_path, uid, gid); + } +} + static int mkdir_local(const char *path, uid_t uid, gid_t gid) { diff --git a/src/common/consumer/consumer.h b/src/common/consumer/consumer.h index f5fbf3262..eb2958e20 100644 --- a/src/common/consumer/consumer.h +++ b/src/common/consumer/consumer.h @@ -62,6 +62,7 @@ enum lttng_consumer_command { LTTNG_CONSUMER_LOST_PACKETS, LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL, LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE, + LTTNG_CONSUMER_ROTATE_RENAME, LTTNG_CONSUMER_MKDIR, }; @@ -760,6 +761,8 @@ void consumer_del_stream_for_data(struct lttng_consumer_stream *stream); void consumer_add_metadata_stream(struct lttng_consumer_stream *stream); void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream); int consumer_create_index_file(struct lttng_consumer_stream *stream); +int lttng_consumer_rotate_rename(const char *current_path, const char *new_path, + uid_t uid, gid_t gid, uint64_t relayd_id); int lttng_consumer_mkdir(const char *path, uid_t uid, gid_t gid, uint64_t relayd_id); diff --git a/src/common/kernel-consumer/kernel-consumer.c b/src/common/kernel-consumer/kernel-consumer.c index 561902466..185d42320 100644 --- a/src/common/kernel-consumer/kernel-consumer.c +++ b/src/common/kernel-consumer/kernel-consumer.c @@ -1072,6 +1072,31 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, } break; } + case LTTNG_CONSUMER_ROTATE_RENAME: + { + DBG("Consumer rename session %" PRIu64 " after rotation, old path = \"%s\", new path = \"%s\"", + msg.u.rotate_rename.session_id, + msg.u.rotate_rename.old_path, + msg.u.rotate_rename.new_path); + ret = lttng_consumer_rotate_rename(msg.u.rotate_rename.old_path, + msg.u.rotate_rename.new_path, + msg.u.rotate_rename.uid, + msg.u.rotate_rename.gid, + msg.u.rotate_rename.relayd_id); + if (ret < 0) { + ERR("Rotate rename failed"); + ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; + } + + health_code_update(); + + ret = consumer_send_status_msg(sock, ret_code); + if (ret < 0) { + /* Somehow, the session daemon is not responding anymore. */ + goto end_nosignal; + } + break; + } case LTTNG_CONSUMER_MKDIR: { DBG("Consumer mkdir %s in session %" PRIu64, diff --git a/src/common/relayd/relayd.c b/src/common/relayd/relayd.c index 7e6520d29..458553bfe 100644 --- a/src/common/relayd/relayd.c +++ b/src/common/relayd/relayd.c @@ -942,6 +942,72 @@ error: return ret; } +int relayd_rotate_rename(struct lttcomm_relayd_sock *rsock, + const char *old_path, const char *new_path) +{ + int ret; + struct lttcomm_relayd_rotate_rename *msg = NULL; + struct lttcomm_relayd_generic_reply reply; + size_t old_path_length, new_path_length; + size_t msg_length; + + /* Code flow error. Safety net. */ + assert(rsock); + + DBG("Relayd rename chunk %s to %s", old_path, new_path); + + /* The two paths are sent with a '\0' delimiter between them. */ + old_path_length = strlen(old_path) + 1; + new_path_length = strlen(new_path) + 1; + + msg_length = sizeof(*msg) + old_path_length + new_path_length; + msg = zmalloc(msg_length); + if (!msg) { + PERROR("zmalloc rotate-rename command message"); + ret = -1; + goto error; + } + + assert(old_path_length <= UINT32_MAX); + msg->old_path_length = htobe32(old_path_length); + + assert(new_path_length <= UINT32_MAX); + msg->new_path_length = htobe32(new_path_length); + + strcpy(msg->paths, old_path); + strcpy(msg->paths + old_path_length, new_path); + + /* Send command */ + ret = send_command(rsock, RELAYD_ROTATE_RENAME, (const void *) msg, + msg_length, 0); + if (ret < 0) { + goto error; + } + + /* Receive response */ + ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); + if (ret < 0) { + goto error; + } + + reply.ret_code = be32toh(reply.ret_code); + + /* Return session id or negative ret code. */ + if (reply.ret_code != LTTNG_OK) { + ret = -1; + ERR("Relayd rotate rename replied error %d", reply.ret_code); + } else { + /* Success */ + ret = 0; + } + + DBG("Relayd rotate rename completed successfully"); + +error: + free(msg); + return ret; +} + int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path) { int ret; @@ -997,5 +1063,4 @@ int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path) error: free(msg); return ret; - } diff --git a/src/common/relayd/relayd.h b/src/common/relayd/relayd.h index fd308d500..5360ae7c8 100644 --- a/src/common/relayd/relayd.h +++ b/src/common/relayd/relayd.h @@ -51,6 +51,8 @@ int relayd_send_index(struct lttcomm_relayd_sock *rsock, uint64_t net_seq_num); int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, uint64_t version); +int relayd_rotate_rename(struct lttcomm_relayd_sock *sock, + const char *current_path, const char *new_path); int relayd_mkdir(struct lttcomm_relayd_sock *rsock, const char *path); #endif /* _RELAYD_H */ diff --git a/src/common/sessiond-comm/relayd.h b/src/common/sessiond-comm/relayd.h index 74d5b1176..52482e017 100644 --- a/src/common/sessiond-comm/relayd.h +++ b/src/common/sessiond-comm/relayd.h @@ -195,6 +195,13 @@ struct lttcomm_relayd_reset_metadata { uint64_t version; } LTTNG_PACKED; +struct lttcomm_relayd_rotate_rename { + uint32_t old_path_length; + uint32_t new_path_length; + /* Concatenation of the old and new paths, separated by \0. */ + char paths[]; +} LTTNG_PACKED; + struct lttcomm_relayd_mkdir { /* Includes trailing NULL */ uint32_t length; diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index 5cd926e6f..6f8249b6a 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -123,6 +123,8 @@ enum lttcomm_relayd_command { RELAYD_STREAMS_SENT = 16, /* Ask the relay to reset the metadata trace file (2.8+) */ RELAYD_RESET_METADATA = 17, + /* Rename a chunk after the rotation is completed (2.11+) */ + RELAYD_ROTATE_RENAME = 19, /* Create a folder on the relayd FS (2.11+) */ RELAYD_MKDIR = 21, }; @@ -536,6 +538,14 @@ struct lttcomm_consumer_msg { struct { uint64_t session_id; } LTTNG_PACKED regenerate_metadata; + struct { + char old_path[LTTNG_PATH_MAX]; + char new_path[LTTNG_PATH_MAX]; + uint64_t relayd_id; /* Relayd id if apply. */ + uint64_t session_id; + uint32_t uid; + uint32_t gid; + } LTTNG_PACKED rotate_rename; struct { char path[LTTNG_PATH_MAX]; uint64_t relayd_id; /* Relayd id if apply. */ diff --git a/src/common/ust-consumer/ust-consumer.c b/src/common/ust-consumer/ust-consumer.c index d0488dbc8..ec078fb47 100644 --- a/src/common/ust-consumer/ust-consumer.c +++ b/src/common/ust-consumer/ust-consumer.c @@ -1917,6 +1917,29 @@ int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, } goto end_msg_sessiond; } + case LTTNG_CONSUMER_ROTATE_RENAME: + { + DBG("Consumer rename session %" PRIu64 " after rotation", + msg.u.rotate_rename.session_id); + ret = lttng_consumer_rotate_rename(msg.u.rotate_rename.old_path, + msg.u.rotate_rename.new_path, + msg.u.rotate_rename.uid, + msg.u.rotate_rename.gid, + msg.u.rotate_rename.relayd_id); + if (ret < 0) { + ERR("Rotate rename failed"); + ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; + } + + health_code_update(); + + ret = consumer_send_status_msg(sock, ret_code); + if (ret < 0) { + /* Somehow, the session daemon is not responding anymore. */ + goto end_nosignal; + } + break; + } case LTTNG_CONSUMER_MKDIR: { DBG("Consumer mkdir %s in session %" PRIu64, -- 2.34.1