Command to rename a folder
authorJulien Desfossez <jdesfossez@efficios.com>
Mon, 11 Dec 2017 20:07:23 +0000 (15:07 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 6 Mar 2018 22:09:42 +0000 (17:09 -0500)
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 <jdesfossez@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-relayd/main.c
src/bin/lttng-sessiond/consumer.c
src/bin/lttng-sessiond/consumer.h
src/common/consumer/consumer.c
src/common/consumer/consumer.h
src/common/kernel-consumer/kernel-consumer.c
src/common/relayd/relayd.c
src/common/relayd/relayd.h
src/common/sessiond-comm/relayd.h
src/common/sessiond-comm/sessiond-comm.h
src/common/ust-consumer/ust-consumer.c

index c4c609a4f2ecf0c5d20d3960d3395e44fbdf7e51..4c99b56f67dcb23ee726b361e96d81d6ba0f0967 100644 (file)
@@ -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;
index 47357309246becaea233fdc068906a9cd99bdd27..8727ce3776a1d85d80a9bdef7ae09110cedb256e 100644 (file)
@@ -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.
  *
index 1fe7c89b5554a250981a43907ed77bc5fbcf9e95..691aad9f6a1dd7f0992c16147c4dcc55f1bb4f2a 100644 (file)
@@ -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);
index b449c1637875ec3d9fd480e49982975ca17ab193..820427aa99ed168d24f30cb28856fa87a6a4f950 100644 (file)
@@ -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)
 {
index f5fbf326219efada17fe347b9fa3ffa6cfd46fe6..eb2958e20669593ed4c8cb7b199c3c3f3fa06039 100644 (file)
@@ -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);
 
index 5619024661d462dc56c79bacfb9fa433b66049a3..185d42320731d026e26a167a3809a731ce597b05 100644 (file)
@@ -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,
index 7e6520d29f51d3c9f8dbd4781d9383fa4fbbb829..458553bfe997069706aa9b43987a99d1cfcf0ece 100644 (file)
@@ -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;
-
 }
index fd308d5007f3b35b4d10d81a2308d4e4aeb5d7bb..5360ae7c8f0697ae48c81346815288cee4608da8 100644 (file)
@@ -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 */
index 74d5b1176be637601edac2e342d08a0fd56ae11f..52482e017a4b58ce527c1bc8275ad568f0d9d108 100644 (file)
@@ -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;
index 5cd926e6f10ce8e857ecd75dc73de027d89a407b..6f8249b6ab0801450a81196fce2d8acca239e196 100644 (file)
@@ -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. */
index d0488dbc837851ec3a23162a2595954aa2c02a87..ec078fb477a39eec0988c3075d8f3d86cad91ea4 100644 (file)
@@ -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,
This page took 0.051364 seconds and 4 git commands to generate.