From: Mathieu Desnoyers Date: Sun, 18 Aug 2019 19:12:02 +0000 (-0400) Subject: relayd comm: add base path to create session X-Git-Tag: v2.12.0-rc1~472 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=46ef21888c975515837d5dab104c938c424792ed;p=lttng-tools.git relayd comm: add base path to create session Add base path and session_name_contains_creation_time flag to session creation 2.11 message. Use it to ensure the "auto-" session names don't get duplicate timestamps for their output paths. Signed-off-by: Mathieu Desnoyers Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng-relayd/cmd-2-11.c b/src/bin/lttng-relayd/cmd-2-11.c index c513130ab..0da8d2834 100644 --- a/src/bin/lttng-relayd/cmd-2-11.c +++ b/src/bin/lttng-relayd/cmd-2-11.c @@ -34,7 +34,8 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload, uint32_t *live_timer, bool *snapshot, uint64_t *id_sessiond, lttng_uuid sessiond_uuid, bool *has_current_chunk, uint64_t *current_chunk_id, - time_t *creation_time) + time_t *creation_time, + bool *session_name_contains_creation_time) { int ret; struct lttcomm_relayd_create_session_2_11 header; @@ -132,6 +133,8 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload, *current_chunk_id = header.current_chunk_id.value; *has_current_chunk = header.current_chunk_id.is_set; *creation_time = (time_t) header.creation_time; + *session_name_contains_creation_time = + header.session_name_contains_creation_time; ret = 0; diff --git a/src/bin/lttng-relayd/cmd-2-11.h b/src/bin/lttng-relayd/cmd-2-11.h index 48841d26b..fc9021024 100644 --- a/src/bin/lttng-relayd/cmd-2-11.h +++ b/src/bin/lttng-relayd/cmd-2-11.h @@ -26,7 +26,8 @@ int cmd_create_session_2_11(const struct lttng_buffer_view *payload, uint32_t *live_timer, bool *snapshot, uint64_t *id_sessiond, lttng_uuid sessiond_uuid, bool *has_current_chunk, uint64_t *current_chunk_id, - time_t *creation_time); + time_t *creation_time, + bool *session_name_contains_creation_time); int cmd_recv_stream_2_11(const struct lttng_buffer_view *payload, char **ret_path_name, char **ret_channel_name, diff --git a/src/bin/lttng-relayd/cmd-2-4.c b/src/bin/lttng-relayd/cmd-2-4.c index aeb760a04..f420ad928 100644 --- a/src/bin/lttng-relayd/cmd-2-4.c +++ b/src/bin/lttng-relayd/cmd-2-4.c @@ -64,6 +64,7 @@ int cmd_create_session_2_4(const struct lttng_buffer_view *payload, } strncpy(hostname, session_info.hostname, sizeof(session_info.hostname)); + *live_timer = be32toh(session_info.live_timer); *snapshot = be32toh(session_info.snapshot); diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index d164bbd8c..a2904eb08 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -1073,6 +1073,7 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, char hostname[LTTNG_HOST_NAME_MAX] = {}; uint32_t live_timer = 0; bool snapshot = false; + bool session_name_contains_creation_timestamp = false; /* Left nil for peers < 2.11. */ char base_path[LTTNG_PATH_MAX] = {}; lttng_uuid sessiond_uuid = {}; @@ -1097,7 +1098,8 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, ret = cmd_create_session_2_11(payload, session_name, hostname, base_path, &live_timer, &snapshot, &id_sessiond_value, sessiond_uuid, &has_current_chunk, - ¤t_chunk_id_value, &creation_time_value); + ¤t_chunk_id_value, &creation_time_value, + &session_name_contains_creation_timestamp); if (lttng_uuid_is_nil(sessiond_uuid)) { /* The nil UUID is reserved for pre-2.11 clients. */ ERR("Illegal nil UUID announced by peer in create session command"); @@ -1121,7 +1123,8 @@ static int relay_create_session(const struct lttcomm_relayd_hdr *recv_hdr, id_sessiond.is_set ? &id_sessiond.value : NULL, current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, creation_time.is_set ? &creation_time.value : NULL, - conn->major, conn->minor); + conn->major, conn->minor, + session_name_contains_creation_timestamp); if (!session) { ret = -1; goto send_reply; @@ -2266,14 +2269,14 @@ static int init_session_output_directory_handle(struct relay_session *session, /* * session_directory: * - * if base_path is NULL + * if base_path is \0' * hostname/session_name * else * hostname/base_path */ char *session_directory = NULL; /* - * base path + session_directory + * relayd_output_path/session_directory * e.g. /home/user/lttng-traces/hostname/session_name */ char *full_session_path = NULL; @@ -2282,28 +2285,60 @@ static int init_session_output_directory_handle(struct relay_session *session, * If base path is set, it overrides the session name for the * session relative base path. No timestamp is appended if the * base path is overridden. + * + * If the session name already contains the creation time (e.g. + * auto-, don't append yet another timestamp after + * the session name in the generated path. + * + * Otherwise, generate the path with session_name-. */ - if (session->base_path[0] == '\0') { - char creation_time_str[16]; + if (session->base_path[0] != '\0') { + pthread_mutex_lock(&session->lock); + ret = asprintf(&session_directory, "%s/%s", session->hostname, + session->base_path); + pthread_mutex_unlock(&session->lock); + } else if (session->session_name_contains_creation_time) { + pthread_mutex_lock(&session->lock); + ret = asprintf(&session_directory, "%s/%s", session->hostname, + session->session_name); + pthread_mutex_unlock(&session->lock); + } else { + char session_creation_datetime[16]; + size_t strftime_ret; struct tm *timeinfo; + time_t creation_time; - assert(session->creation_time.is_set); - timeinfo = localtime(&session->creation_time.value); - if (!timeinfo) { + /* + * The 2.11+ protocol guarantees that a creation time + * is provided for a session. This would indicate a + * protocol error or an improper use of this util. + */ + if (!session->creation_time.is_set) { + ERR("Creation time missing for session \"%s\" (protocol error)", + session->session_name); ret = -1; goto end; } - strftime(creation_time_str, sizeof(creation_time_str), "%Y%m%d-%H%M%S", - timeinfo); + creation_time = LTTNG_OPTIONAL_GET(session->creation_time); + timeinfo = localtime(&creation_time); + if (!timeinfo) { + ERR("Failed to get timeinfo while initializing session output directory handle"); + ret = -1; + goto end; + } + strftime_ret = strftime(session_creation_datetime, + sizeof(session_creation_datetime), + "%Y%m%d-%H%M%S", timeinfo); + if (strftime_ret == 0) { + ERR("Failed to format session creation timestamp while initializing session output directory handle"); + ret = -1; + goto end; + } pthread_mutex_lock(&session->lock); - ret = asprintf(&session_directory, "%s/%s-%s", session->hostname, - session->session_name, creation_time_str); - pthread_mutex_unlock(&session->lock); - } else { - pthread_mutex_lock(&session->lock); - ret = asprintf(&session_directory, "%s/%s", session->hostname, - session->base_path); + ret = asprintf(&session_directory, "%s/%s-%s", + session->hostname, session->session_name, + session_creation_datetime); pthread_mutex_unlock(&session->lock); } if (ret < 0) { diff --git a/src/bin/lttng-relayd/session.c b/src/bin/lttng-relayd/session.c index de059aae2..605931d5b 100644 --- a/src/bin/lttng-relayd/session.c +++ b/src/bin/lttng-relayd/session.c @@ -95,7 +95,8 @@ struct relay_session *session_create(const char *session_name, const uint64_t *current_chunk_id, const time_t *creation_time, uint32_t major, - uint32_t minor) + uint32_t minor, + bool session_name_contains_creation_time) { int ret; struct relay_session *session = NULL; @@ -136,6 +137,11 @@ struct relay_session *session_create(const char *session_name, WARN("Base path exceeds maximal allowed length"); goto error; } + if (creation_time) { + LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); + } + session->session_name_contains_creation_time = + session_name_contains_creation_time; session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); if (!session->ctf_traces_ht) { @@ -196,9 +202,6 @@ struct relay_session *session_create(const char *session_name, } lttng_ht_add_unique_u64(sessions_ht, &session->session_n); - if (creation_time) { - LTTNG_OPTIONAL_SET(&session->creation_time, *creation_time); - } return session; error: diff --git a/src/bin/lttng-relayd/session.h b/src/bin/lttng-relayd/session.h index 228873300..b3d99012d 100644 --- a/src/bin/lttng-relayd/session.h +++ b/src/bin/lttng-relayd/session.h @@ -87,6 +87,8 @@ struct relay_session { */ bool aborted; + bool session_name_contains_creation_time; + /* Contains ctf_trace object of that session indexed by path name. */ struct lttng_ht *ctf_traces_ht; @@ -134,7 +136,8 @@ struct relay_session *session_create(const char *session_name, const uint64_t *current_chunk_id, const time_t *creation_time, uint32_t major, - uint32_t minor); + uint32_t minor, + bool session_name_contains_creation_timestamp); struct relay_session *session_get_by_id(uint64_t id); bool session_get(struct relay_session *session); void session_put(struct relay_session *session); diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 0596033b1..577647a1c 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -1040,7 +1040,8 @@ static enum lttng_error_code send_consumer_relayd_socket( const char *session_name, const char *hostname, const char *base_path, int session_live_timer, const uint64_t *current_chunk_id, - time_t session_creation_time) + time_t session_creation_time, + bool session_name_contains_creation_time) { int ret; struct lttcomm_relayd_sock *rsock = NULL; @@ -1070,7 +1071,7 @@ static enum lttng_error_code send_consumer_relayd_socket( relayd_uri->stype, session_id, session_name, hostname, base_path, session_live_timer, current_chunk_id, - session_creation_time); + session_creation_time, session_name_contains_creation_time); if (ret < 0) { status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; goto close_sock; @@ -1118,7 +1119,8 @@ static enum lttng_error_code send_consumer_relayd_sockets( unsigned int session_id, struct consumer_output *consumer, struct consumer_socket *sock, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, - const uint64_t *current_chunk_id, time_t session_creation_time) + const uint64_t *current_chunk_id, time_t session_creation_time, + bool session_name_contains_creation_time) { enum lttng_error_code status = LTTNG_OK; @@ -1130,7 +1132,8 @@ static enum lttng_error_code send_consumer_relayd_sockets( status = send_consumer_relayd_socket(session_id, &consumer->dst.net.control, consumer, sock, session_name, hostname, base_path, session_live_timer, - current_chunk_id, session_creation_time); + current_chunk_id, session_creation_time, + session_name_contains_creation_time); if (status != LTTNG_OK) { goto error; } @@ -1141,7 +1144,8 @@ static enum lttng_error_code send_consumer_relayd_sockets( status = send_consumer_relayd_socket(session_id, &consumer->dst.net.data, consumer, sock, session_name, hostname, base_path, session_live_timer, - current_chunk_id, session_creation_time); + current_chunk_id, session_creation_time, + session_name_contains_creation_time); if (status != LTTNG_OK) { goto error; } @@ -1199,7 +1203,8 @@ int cmd_setup_relayd(struct ltt_session *session) session->base_path, session->live_timer, current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, - session->creation_time); + session->creation_time, + session->name_contains_creation_time); pthread_mutex_unlock(socket->lock); if (ret != LTTNG_OK) { goto error; @@ -1224,7 +1229,8 @@ int cmd_setup_relayd(struct ltt_session *session) session->base_path, session->live_timer, current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, - session->creation_time); + session->creation_time, + session->name_contains_creation_time); pthread_mutex_unlock(socket->lock); if (ret != LTTNG_OK) { goto error; @@ -4289,7 +4295,8 @@ static enum lttng_error_code set_relayd_for_snapshot( base_path, session->live_timer, current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, - session->creation_time); + session->creation_time, + session->name_contains_creation_time); pthread_mutex_unlock(socket->lock); if (status != LTTNG_OK) { rcu_read_unlock(); diff --git a/src/bin/lttng-sessiond/consumer.c b/src/bin/lttng-sessiond/consumer.c index 3e23d3255..4480da368 100644 --- a/src/bin/lttng-sessiond/consumer.c +++ b/src/bin/lttng-sessiond/consumer.c @@ -1076,7 +1076,8 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, enum lttng_stream_type type, uint64_t session_id, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, - const uint64_t *current_chunk_id, time_t session_creation_time) + const uint64_t *current_chunk_id, time_t session_creation_time, + bool session_name_contains_creation_time) { int ret; struct lttcomm_consumer_msg msg; @@ -1100,7 +1101,8 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, session_live_timer, consumer->snapshot, session_id, sessiond_uuid, current_chunk_id, - session_creation_time); + session_creation_time, + session_name_contains_creation_time); if (ret < 0) { /* Close the control socket. */ (void) relayd_close(rsock); diff --git a/src/bin/lttng-sessiond/consumer.h b/src/bin/lttng-sessiond/consumer.h index 37b522a91..af07f5ccf 100644 --- a/src/bin/lttng-sessiond/consumer.h +++ b/src/bin/lttng-sessiond/consumer.h @@ -220,7 +220,8 @@ int consumer_send_relayd_socket(struct consumer_socket *consumer_sock, enum lttng_stream_type type, uint64_t session_id, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, - const uint64_t *current_chunk_id, time_t session_creation_time); + const uint64_t *current_chunk_id, time_t session_creation_time, + bool session_name_contains_creation_time); int consumer_send_channel_monitor_pipe(struct consumer_socket *consumer_sock, int pipe); int consumer_send_destroy_relayd(struct consumer_socket *sock, diff --git a/src/bin/lttng-sessiond/session.c b/src/bin/lttng-sessiond/session.c index 2a7b8b83b..602059424 100644 --- a/src/bin/lttng-sessiond/session.c +++ b/src/bin/lttng-sessiond/session.c @@ -1065,6 +1065,7 @@ enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid, DEFAULT_SESSION_NAME, i, datetime); } + new_session->name_contains_creation_time = true; if (ret == -1 || ret >= sizeof(new_session->name)) { /* * Null-terminate in case the name is used diff --git a/src/bin/lttng-sessiond/session.h b/src/bin/lttng-sessiond/session.h index cd0f99968..204701429 100644 --- a/src/bin/lttng-sessiond/session.h +++ b/src/bin/lttng-sessiond/session.h @@ -75,6 +75,7 @@ struct ltt_session_list { struct ltt_session { char name[NAME_MAX]; bool has_auto_generated_name; + bool name_contains_creation_time; char hostname[HOST_NAME_MAX]; /* Local hostname. */ time_t creation_time; struct ltt_kernel_session *kernel_session; diff --git a/src/common/relayd/relayd.c b/src/common/relayd/relayd.c index 51de7b82f..ed5e45607 100644 --- a/src/common/relayd/relayd.c +++ b/src/common/relayd/relayd.c @@ -132,7 +132,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, const char *base_path, int session_live_timer, unsigned int snapshot, uint64_t sessiond_session_id, const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id, - time_t creation_time) + time_t creation_time, bool session_name_contains_creation_time) { int ret; struct lttcomm_relayd_create_session_2_11 *msg = NULL; @@ -142,7 +142,10 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, size_t msg_length; char *dst; - /* The two names are sent with a '\0' delimiter between them. */ + if (!base_path) { + base_path = ""; + } + /* The three names are sent with a '\0' delimiter between them. */ session_name_len = strlen(session_name) + 1; hostname_len = strlen(hostname) + 1; base_path_len = base_path ? strlen(base_path) + 1 : 0; @@ -185,7 +188,7 @@ static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, lttng_uuid_copy(msg->sessiond_uuid, sessiond_uuid); msg->session_id = htobe64(sessiond_session_id); - + msg->session_name_contains_creation_time = session_name_contains_creation_time; if (current_chunk_id) { LTTNG_OPTIONAL_SET(&msg->current_chunk_id, htobe64(*current_chunk_id)); @@ -266,7 +269,7 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock, unsigned int snapshot, uint64_t sessiond_session_id, const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id, - time_t creation_time) + time_t creation_time, bool session_name_contains_creation_time) { int ret; struct lttcomm_relayd_status_session reply; @@ -288,7 +291,8 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock, ret = relayd_create_session_2_11(rsock, session_name, hostname, base_path, session_live_timer, snapshot, sessiond_session_id, sessiond_uuid, - current_chunk_id, creation_time); + current_chunk_id, creation_time, + session_name_contains_creation_time); } if (ret < 0) { diff --git a/src/common/relayd/relayd.h b/src/common/relayd/relayd.h index 6dc5d0044..8d4c9da13 100644 --- a/src/common/relayd/relayd.h +++ b/src/common/relayd/relayd.h @@ -46,7 +46,7 @@ int relayd_create_session(struct lttcomm_relayd_sock *rsock, unsigned int snapshot, uint64_t sessiond_session_id, const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id, - time_t creation_time); + time_t creation_time, bool session_name_contains_creation_time); int relayd_add_stream(struct lttcomm_relayd_sock *sock, const char *channel_name, const char *pathname, uint64_t *stream_id, uint64_t tracefile_size, uint64_t tracefile_count, diff --git a/src/common/sessiond-comm/relayd.h b/src/common/sessiond-comm/relayd.h index 3bdff8705..cc26a2c6b 100644 --- a/src/common/sessiond-comm/relayd.h +++ b/src/common/sessiond-comm/relayd.h @@ -210,6 +210,7 @@ struct lttcomm_relayd_create_session_2_11 { uint32_t base_path_len; uint32_t live_timer; uint8_t snapshot; + uint8_t session_name_contains_creation_time; /* Sessiond instance UUID */ lttng_uuid sessiond_uuid; /* Sessiond session id */