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;
}
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
*/
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;
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.
*
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);
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)
{
LTTNG_CONSUMER_LOST_PACKETS,
LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL,
LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE,
+ LTTNG_CONSUMER_ROTATE_RENAME,
LTTNG_CONSUMER_MKDIR,
};
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);
}
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,
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;
error:
free(msg);
return ret;
-
}
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 */
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;
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,
};
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. */
}
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,