From: Partha Pratim Mukherjee Date: Sat, 25 Jul 2015 07:55:19 +0000 (+0530) Subject: Fix: set session should not set non-existent session X-Git-Tag: v2.5.5~10 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=88fd7d62599f1e64a2cd5175ec0d832fc8de2bf9;p=lttng-tools.git Fix: set session should not set non-existent session set-session does not check the existence of a session before setting it as the current session. Fix it so that it gives error for a non-existent session. Fixes #885 Signed-off-by: Partha Pratim Mukherjee Signed-off-by: Jérémie Galarneau --- diff --git a/src/bin/lttng/commands/set_session.c b/src/bin/lttng/commands/set_session.c index 4238016e4..423421deb 100644 --- a/src/bin/lttng/commands/set_session.c +++ b/src/bin/lttng/commands/set_session.c @@ -59,11 +59,34 @@ static void usage(FILE *ofp) static int set_session(void) { int ret = CMD_SUCCESS; + int count, i; + unsigned int session_found = 0; + struct lttng_session *sessions; if (opt_session_name && strlen(opt_session_name) > NAME_MAX) { ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX); ret = CMD_ERROR; + goto end; + } + + count = lttng_list_sessions(&sessions); + if (count < 0) { + ret = CMD_ERROR; + ERR("%s", lttng_strerror(count)); + goto end; + } + + for (i = 0; i < count; i++) { + if (strncmp(sessions[i].name, opt_session_name, NAME_MAX) == 0) { + session_found = 1; + break; + } + } + + if (!session_found) { + ERR("Session '%s' not found", opt_session_name); + ret = CMD_ERROR; goto error; } @@ -78,6 +101,8 @@ static int set_session(void) ret = CMD_SUCCESS; error: + free(sessions); +end: return ret; }