Mi set-session command: support and validation
authorJonathan Rajotte Julien <jonathan.r.julien@gmail.com>
Wed, 18 Jun 2014 21:26:15 +0000 (17:26 -0400)
committerJonathan Rajotte Julien <jonathan.r.julien@gmail.com>
Tue, 22 Jul 2014 20:14:57 +0000 (16:14 -0400)
Signed-off-by: Jonathan Rajotte Julien <jonathan.r.julien@gmail.com>
src/bin/lttng/commands/set_session.c
src/common/mi-lttng.c
src/common/mi-lttng.h
src/common/mi_lttng.xsd

index 54320df2a42a0988a1255eddbf19252ea8f36d7d..fd3d9a11813a3a7fdd9bb9f8b47afb8a74b0e4ca 100644 (file)
@@ -23,6 +23,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <assert.h>
+
+#include <common/mi-lttng.h>
 
 #include "../command.h"
 
@@ -33,6 +36,8 @@ enum {
        OPT_LIST_OPTIONS,
 };
 
+static struct mi_writer *writer;
+
 static struct poptOption long_options[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
        {"help",           'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
@@ -53,6 +58,47 @@ static void usage(FILE *ofp)
        fprintf(ofp, "\n");
 }
 
+/*
+ * Print the necessary mi for a session and name.
+ */
+static int mi_print(char *session_name)
+{
+       int ret;
+
+       assert(writer);
+       assert(session_name);
+
+       /*
+        * Open a sessions element
+        * This is purely for validation purpose
+        */
+       ret = mi_lttng_sessions_open(writer);
+       if (ret) {
+               goto end;
+       }
+
+       /* Open a session element */
+       ret = mi_lttng_writer_open_element(writer, config_element_session);
+       if (ret) {
+               goto end;
+       }
+
+       /* Session name */
+       ret = mi_lttng_writer_write_element_string(writer , config_element_name,
+                       session_name);
+       if (ret) {
+               goto end;
+       }
+
+       /* Close session and sessions element */
+       ret = mi_lttng_close_multi_element(writer, 2);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret;
+}
+
 /*
  *  set_session
  */
@@ -75,6 +121,14 @@ static int set_session(void)
        }
 
        MSG("Session set to %s", opt_session_name);
+       if (lttng_opt_mi) {
+               ret = mi_print(opt_session_name);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto error;
+               }
+       }
+
        ret = CMD_SUCCESS;
 
 error:
@@ -86,19 +140,12 @@ error:
  */
 int cmd_set_session(int argc, const char **argv)
 {
-       int opt, ret = CMD_SUCCESS;
+       int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
 
-       /* TODO: mi support */
-       if (lttng_opt_mi) {
-               ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
-               ERR("mi option not supported");
-               goto end;
-       }
-
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
@@ -122,9 +169,71 @@ int cmd_set_session(int argc, const char **argv)
                goto end;
        }
 
-       ret = set_session();
+       /* Mi check */
+       if (lttng_opt_mi) {
+               writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
+               if (!writer) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
+
+               /* Open command element */
+               ret = mi_lttng_writer_command_open(writer,
+                               mi_lttng_element_command_set_session);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Open output element */
+               ret = mi_lttng_writer_open_element(writer,
+                               mi_lttng_element_command_output);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
+       command_ret = set_session();
+       if (command_ret) {
+               success = 0;
+       }
+
+       /* Mi closing */
+       if (lttng_opt_mi) {
+               /* Close  output element */
+               ret = mi_lttng_writer_close_element(writer);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Success ? */
+               ret = mi_lttng_writer_write_element_bool(writer,
+                               mi_lttng_element_command_success, success);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+
+               /* Command element close */
+               ret = mi_lttng_writer_command_close(writer);
+               if (ret) {
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
 
 end:
+       /* Mi clean-up */
+       if (writer && mi_lttng_writer_destroy(writer)) {
+               /* Preserve original error code */
+               ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
+       }
+
+       /* Overwrite ret if an error occured during set_session() */
+       ret = command_ret ? command_ret : ret;
+
        poptFreeContext(pc);
        return ret;
 }
index 0149204f53074e232807e18af66bce0577203f1c..a825f55946b3615e84c13f2646eefa786053664f 100644 (file)
@@ -38,6 +38,7 @@ const char * const mi_lttng_element_command_destroy = "destroy";
 const char * const mi_lttng_element_command_calibrate = "calibrate";
 const char * const mi_lttng_element_command_add_context = "add-context";
 const char * const mi_lttng_element_command_enable_channels = "enable-channel";
+const char * const mi_lttng_element_command_set_session = "set-session";
 const char * const mi_lttng_element_command_output = "output";
 const char * const mi_lttng_element_command_success = "success";
 
index d621f36a56839aab527643d06398e56a0fe73b8d..6a002751a36235d941e2f4d70a51c84c258fe7ab 100644 (file)
@@ -60,6 +60,7 @@ const char * const mi_lttng_element_command_destroy;
 const char * const mi_lttng_element_command_calibrate;
 const char * const mi_lttng_element_command_add_context;
 const char * const mi_lttng_element_command_enable_channels;
+const char * const mi_lttng_element_command_set_session;
 const char * const mi_lttng_element_command_output;
 const char * const mi_lttng_element_command_success;
 
index 02aab41067320e27ea4d579424f23fcc1a0eff1c..6520dfd5a6b34614a73df5246d7fcd0337d48c40 100644 (file)
@@ -407,6 +407,7 @@ THE SOFTWARE.
                        <xs:enumeration value="add-context" />
                        <xs:enumeration value="enable-channel" />
                        <xs:enumeration value="enable-event" />
+                       <xs:enumeration value="set-session" />
                </xs:restriction>
        </xs:simpleType>
 
This page took 0.02831 seconds and 4 git commands to generate.