From dbd512ea8aa5349849aeeccda6130aa9e28cf23c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Wed, 1 Aug 2018 16:49:46 -0400 Subject: [PATCH] rotation-api: pass session name explicitly MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The session name is currently passed to the rotation API commands through the attribute descriptors. However, the session name is an attribute that is always required and passing it explicitly allows users to perform immediate rotations without allocating an immediate rotation attribute descriptor. Signed-off-by: Jérémie Galarneau --- include/lttng/rotate-internal.h | 4 - include/lttng/rotation.h | 53 ++++---------- src/bin/lttng/commands/disable_rotation.c | 7 +- src/bin/lttng/commands/enable_rotation.c | 7 +- src/bin/lttng/commands/rotate.c | 15 +--- src/common/config/session-config.c | 14 ++-- src/lib/lttng-ctl/rotate.c | 89 ++++++----------------- 7 files changed, 46 insertions(+), 143 deletions(-) diff --git a/include/lttng/rotate-internal.h b/include/lttng/rotate-internal.h index e359cd741..8d0823b7a 100644 --- a/include/lttng/rotate-internal.h +++ b/include/lttng/rotate-internal.h @@ -32,8 +32,6 @@ * This is opaque to the public library. */ struct lttng_rotation_immediate_attr { - /* Session name to rotate. */ - char session_name[LTTNG_NAME_MAX]; /* For the rotate pending request. */ uint64_t rotate_id; }; @@ -44,8 +42,6 @@ struct lttng_rotation_immediate_attr { * This is opaque to the public library. */ struct lttng_rotation_schedule_attr { - /* Session name to rotate. */ - char session_name[LTTNG_NAME_MAX]; /* > 0 if a timer is set. */ uint64_t timer_us; /* > 0 if the session should rotate when it has written that many bytes. */ diff --git a/include/lttng/rotation.h b/include/lttng/rotation.h index dbc0183da..6091a7b98 100644 --- a/include/lttng/rotation.h +++ b/include/lttng/rotation.h @@ -88,25 +88,14 @@ struct lttng_rotation_schedule_attr; struct lttng_rotation_handle; /* - * Return a newly allocated immediate session rotation descriptor object or NULL - * on error. - */ -extern struct lttng_rotation_immediate_attr * -lttng_rotation_immediate_attr_create(void); - -/* - * Return a newly allocated scheduled rotate session descriptor object or NULL + * Return a newly allocated session rotation schedule descriptor object or NULL * on error. + * + * The rotation schedule may be expressed as a size or as a time period. */ extern struct lttng_rotation_schedule_attr * lttng_rotation_schedule_attr_create(void); -/* - * Destroy a given immediate session rotation descriptor object. - */ -extern void lttng_rotation_immediate_attr_destroy( - struct lttng_rotation_immediate_attr *attr); - /* * Destroy a given scheduled rotate session descriptor object. */ @@ -114,33 +103,13 @@ extern void lttng_rotation_schedule_attr_destroy( struct lttng_rotation_schedule_attr *attr); /* - * Set the name of the session to rotate immediately. - * - * The session_name parameter is copied to the immediate session rotation - * attributes. - */ -extern enum lttng_rotation_status lttng_rotation_immediate_attr_set_session_name( - struct lttng_rotation_immediate_attr *attr, - const char *session_name); - -/* - * Set the name of the session to rotate automatically. - * - * The session_name parameter is copied to the immediate session rotation - * attributes. - */ -extern enum lttng_rotation_status lttng_rotation_schedule_attr_set_session_name( - struct lttng_rotation_schedule_attr *attr, - const char *session_name); - -/* - * Set the timer to periodically rotate the session (µs, -1ULL to disable). + * Set the timer to periodically rotate the session (in µs). */ extern enum lttng_rotation_status lttng_rotation_schedule_attr_set_timer_period( struct lttng_rotation_schedule_attr *attr, uint64_t timer); /* - * Set the size to rotate the session (bytes, -1ULL to disable). + * Set the size to rotate the session (in bytes). */ void lttng_rotation_schedule_attr_set_size( struct lttng_rotation_schedule_attr *attr, uint64_t size); @@ -179,22 +148,26 @@ extern void lttng_rotation_handle_destroy( struct lttng_rotation_handle *rotation_handle); /* - * Rotate the output folder of the session + * Rotate the output folder of the session. * * On success, handle is allocated and can be used to monitor the progress * of the rotation with lttng_rotation_get_state(). The handle must be freed * by the caller with lttng_rotation_handle_destroy(). * + * Passing NULL as the immediate rotation attribute results in the default + * options being used. + * * Return 0 if the rotate action was successfully launched or a negative * LTTng error code on error. */ -extern int lttng_rotate_session(struct lttng_rotation_immediate_attr *attr, +extern int lttng_rotate_session(const char *session_name, + struct lttng_rotation_immediate_attr *attr, struct lttng_rotation_handle **rotation_handle); /* - * Configure a session to rotate periodically or based on the size written. + * Configure a session to rotate according to a given schedule. */ -extern int lttng_rotation_set_schedule( +extern int lttng_rotation_set_schedule(const char *session_name, struct lttng_rotation_schedule_attr *attr); /* diff --git a/src/bin/lttng/commands/disable_rotation.c b/src/bin/lttng/commands/disable_rotation.c index a4a979030..b179e91ce 100644 --- a/src/bin/lttng/commands/disable_rotation.c +++ b/src/bin/lttng/commands/disable_rotation.c @@ -77,11 +77,6 @@ static int setup_rotate(char *session_name, uint64_t timer, uint64_t size) } } - ret = lttng_rotation_schedule_attr_set_session_name(attr, session_name); - if (ret < 0) { - goto error; - } - if (lttng_opt_mi) { ret = mi_lttng_writer_write_element_string(writer, mi_lttng_element_session_name, session_name); @@ -99,7 +94,7 @@ static int setup_rotate(char *session_name, uint64_t timer, uint64_t size) MSG("Disabling rotation based on size on session %s", session_name); } - ret = lttng_rotation_set_schedule(attr); + ret = lttng_rotation_set_schedule(session_name, attr); if (ret) { ERR("%s", lttng_strerror(ret)); if (lttng_opt_mi) { diff --git a/src/bin/lttng/commands/enable_rotation.c b/src/bin/lttng/commands/enable_rotation.c index 42aa45017..81a55b24c 100644 --- a/src/bin/lttng/commands/enable_rotation.c +++ b/src/bin/lttng/commands/enable_rotation.c @@ -69,11 +69,6 @@ static int setup_rotate(char *session_name, uint64_t timer, uint64_t size) goto error; } - ret = lttng_rotation_schedule_attr_set_session_name(attr, session_name); - if (ret < 0) { - goto error; - } - if (lttng_opt_mi) { /* Open rotation_schedule element */ ret = mi_lttng_writer_open_element(writer, @@ -114,7 +109,7 @@ static int setup_rotate(char *session_name, uint64_t timer, uint64_t size) } } - ret = lttng_rotation_set_schedule(attr); + ret = lttng_rotation_set_schedule(session_name, attr); if (ret) { ERR("%s", lttng_strerror(ret)); if (lttng_opt_mi) { diff --git a/src/bin/lttng/commands/rotate.c b/src/bin/lttng/commands/rotate.c index 05e55bf3c..68bc59a33 100644 --- a/src/bin/lttng/commands/rotate.c +++ b/src/bin/lttng/commands/rotate.c @@ -209,25 +209,13 @@ end: static int rotate_tracing(char *session_name) { int ret; - struct lttng_rotation_immediate_attr *attr = NULL; struct lttng_rotation_handle *handle = NULL; enum lttng_rotation_status rotation_status; enum lttng_rotation_state rotation_state = LTTNG_ROTATION_STATE_ONGOING; DBG("Rotating the output files of session %s", session_name); - attr = lttng_rotation_immediate_attr_create(); - if (!attr) { - goto error; - } - - ret = lttng_rotation_immediate_attr_set_session_name(attr, session_name); - if (ret < 0) { - ERR("Session name exceeds the maximal allowed length"); - goto error; - } - - ret = lttng_rotate_session(attr, &handle); + ret = lttng_rotate_session(session_name, NULL, &handle); if (ret < 0) { switch (-ret) { case LTTNG_ERR_SESSION_NOT_STARTED: @@ -314,7 +302,6 @@ error: ret = CMD_ERROR; end: lttng_rotation_handle_destroy(handle); - lttng_rotation_immediate_attr_destroy(attr); return ret; } diff --git a/src/common/config/session-config.c b/src/common/config/session-config.c index 1b0db12cc..244fbd5cc 100644 --- a/src/common/config/session-config.c +++ b/src/common/config/session-config.c @@ -2831,20 +2831,18 @@ domain_init_error: } if (rotation_timer_interval || rotation_size) { - struct lttng_rotation_schedule_attr *rotation_attr = lttng_rotation_schedule_attr_create(); + struct lttng_rotation_schedule_attr *rotation_attr = + lttng_rotation_schedule_attr_create(); if (!rotation_attr) { goto error; } - ret = lttng_rotation_schedule_attr_set_session_name(rotation_attr, (const char *) name); - if (ret) { - lttng_rotation_schedule_attr_destroy(rotation_attr); - goto error; - } lttng_rotation_schedule_attr_set_timer_period(rotation_attr, rotation_timer_interval); - lttng_rotation_schedule_attr_set_size(rotation_attr, rotation_size); - ret = lttng_rotation_set_schedule(rotation_attr); + lttng_rotation_schedule_attr_set_size(rotation_attr, + rotation_size); + ret = lttng_rotation_set_schedule((const char *) name, + rotation_attr); lttng_rotation_schedule_attr_destroy(rotation_attr); if (ret) { goto error; diff --git a/src/lib/lttng-ctl/rotate.c b/src/lib/lttng-ctl/rotate.c index 26cc5f94f..f32c69be1 100644 --- a/src/lib/lttng-ctl/rotate.c +++ b/src/lib/lttng-ctl/rotate.c @@ -28,22 +28,11 @@ #include "lttng-ctl-helper.h" -struct lttng_rotation_immediate_attr *lttng_rotation_immediate_attr_create(void) -{ - return zmalloc(sizeof(struct lttng_rotation_immediate_attr)); -} - struct lttng_rotation_schedule_attr *lttng_rotation_schedule_attr_create(void) { return zmalloc(sizeof(struct lttng_rotation_schedule_attr)); } -void lttng_rotation_immediate_attr_destroy( - struct lttng_rotation_immediate_attr *attr) -{ - free(attr); -} - void lttng_rotation_schedule_attr_destroy(struct lttng_rotation_schedule_attr *attr) { if (attr) { @@ -52,29 +41,6 @@ void lttng_rotation_schedule_attr_destroy(struct lttng_rotation_schedule_attr *a } } -enum lttng_rotation_status lttng_rotation_immediate_attr_set_session_name( - struct lttng_rotation_immediate_attr *attr, - const char *session_name) -{ - enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK; - int ret; - - if (!attr || !session_name) { - status = LTTNG_ROTATION_STATUS_INVALID; - goto error; - } - - ret = lttng_strncpy(attr->session_name, session_name, - sizeof(attr->session_name)); - if (ret) { - status = LTTNG_ROTATION_STATUS_INVALID; - goto error; - } - -error: - return status; -} - static enum lttng_rotation_status ask_rotation_info( struct lttng_rotation_handle *rotation_handle, @@ -111,29 +77,6 @@ end: } -enum lttng_rotation_status lttng_rotation_schedule_attr_set_session_name( - struct lttng_rotation_schedule_attr *attr, - const char *session_name) -{ - enum lttng_rotation_status status = LTTNG_ROTATION_STATUS_OK; - int ret; - - if (!attr || !session_name) { - status = LTTNG_ROTATION_STATUS_INVALID; - goto error; - } - - ret = lttng_strncpy(attr->session_name, session_name, - sizeof(attr->session_name)); - if (ret) { - status = LTTNG_ROTATION_STATUS_INVALID; - goto error; - } - -error: - return status; -} - enum lttng_rotation_status lttng_rotation_schedule_attr_set_timer_period( struct lttng_rotation_schedule_attr *attr, uint64_t timer) @@ -277,12 +220,13 @@ void lttng_rotation_handle_destroy( static int init_rotation_handle(struct lttng_rotation_handle *rotation_handle, + const char *session_name, struct lttng_rotate_session_return *rotate_return, struct lttng_rotation_immediate_attr *attr) { int ret; - ret = lttng_strncpy(rotation_handle->session_name, attr->session_name, + ret = lttng_strncpy(rotation_handle->session_name, session_name, sizeof(rotation_handle->session_name)); if (ret) { goto end; @@ -298,21 +242,30 @@ end: * * Return 0 on success else a negative LTTng error code. */ -int lttng_rotate_session(struct lttng_rotation_immediate_attr *attr, +int lttng_rotate_session(const char *session_name, + struct lttng_rotation_immediate_attr *attr, struct lttng_rotation_handle **rotation_handle) { struct lttcomm_session_msg lsm; struct lttng_rotate_session_return *rotate_return = NULL; int ret; + size_t session_name_len; - if (!attr) { + if (!session_name) { + ret = -LTTNG_ERR_INVALID; + goto end; + } + + session_name_len = strlen(session_name); + if (session_name_len >= sizeof(lsm.session.name) || + session_name_len >= member_sizeof(struct lttng_rotation_handle, session_name)) { ret = -LTTNG_ERR_INVALID; goto end; } memset(&lsm, 0, sizeof(lsm)); lsm.cmd_type = LTTNG_ROTATE_SESSION; - lttng_ctl_copy_string(lsm.session.name, attr->session_name, + lttng_ctl_copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); ret = lttng_ctl_ask_sessiond(&lsm, (void **) &rotate_return); @@ -327,7 +280,8 @@ int lttng_rotate_session(struct lttng_rotation_immediate_attr *attr, goto end; } - init_rotation_handle(*rotation_handle, rotate_return, attr); + init_rotation_handle(*rotation_handle, session_name, rotate_return, + attr); ret = 0; @@ -339,20 +293,25 @@ end: /* * Configure the automatic rotate parameters. */ -int lttng_rotation_set_schedule( +int lttng_rotation_set_schedule(const char *session_name, struct lttng_rotation_schedule_attr *attr) { struct lttcomm_session_msg lsm; int ret; - if (!attr) { + if (!attr || !session_name) { + ret = -LTTNG_ERR_INVALID; + goto end; + } + + if (strlen(session_name) >= sizeof(lsm.session.name)) { ret = -LTTNG_ERR_INVALID; goto end; } memset(&lsm, 0, sizeof(lsm)); lsm.cmd_type = LTTNG_ROTATION_SET_SCHEDULE; - lttng_ctl_copy_string(lsm.session.name, attr->session_name, + lttng_ctl_copy_string(lsm.session.name, session_name, sizeof(lsm.session.name)); lsm.u.rotate_setup.timer_us = attr->timer_us; lsm.u.rotate_setup.size = attr->size; -- 2.34.1