Fix: set session should not set non-existent session
[lttng-tools.git] / src / bin / lttng / commands / set_session.c
index fdeeaafb5ded5ab535c131046af90f179d19e684..423421deb2bdba337a2e20cae03951bc16cda605 100644 (file)
@@ -1,19 +1,18 @@
 /*
  * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _GNU_SOURCE
@@ -46,7 +45,7 @@ static struct poptOption long_options[] = {
  */
 static void usage(FILE *ofp)
 {
-       fprintf(ofp, "usage: lttng set-session NAME\n");
+       fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n");
        fprintf(ofp, "\n");
        fprintf(ofp, "Options:\n");
        fprintf(ofp, "  -h, --help               Show this help\n");
@@ -60,6 +59,36 @@ 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;
+       }
 
        ret = config_init(opt_session_name);
        if (ret < 0) {
@@ -72,6 +101,8 @@ static int set_session(void)
        ret = CMD_SUCCESS;
 
 error:
+       free(sessions);
+end:
        return ret;
 }
 
This page took 0.024856 seconds and 4 git commands to generate.