diff mbox

[1/9,v2] xl: Improve return and exit codes of memory related functions.

Message ID 1457463730-4712-2-git-send-email-write.harmandeep@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Harmandeep Kaur March 8, 2016, 7:02 p.m. UTC
Return and exit codes of freemem(), set_memory_max(), main_memmax(),
set_memory_target(), main_memset() and main_sharing() in the file
xl_cmdimpl are fixed.

Add comment to explain return vaule of parse_mem_size_kb().

Signed-off-by: Harmandeep Kaur <write.harmandeep@gmail.com>
---
v2: Add comment to explain return vaule of parse_mem_size_kb().
    Add freemem() and main_sharing().
    Remove find_domain().
---
 tools/libxl/xl_cmdimpl.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
diff mbox

Patch

diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 990d3c9..088971e 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -2581,30 +2581,30 @@  static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
 
     rc = libxl_domain_need_memory(ctx, b_info, &need_memkb);
     if (rc < 0)
-        return rc;
+        return -1;
 
     do {
         rc = libxl_get_free_memory(ctx, &free_memkb);
         if (rc < 0)
-            return rc;
+            return -1;
 
         if (free_memkb >= need_memkb)
             return 0;
 
         rc = libxl_set_memory_target(ctx, 0, free_memkb - need_memkb, 1, 0);
         if (rc < 0)
-            return rc;
+            return -1;
 
         /* wait until dom0 reaches its target, as long as we are making
          * progress */
         rc = libxl_wait_for_memory_target(ctx, 0, 10);
         if (rc < 0)
-            return rc;
+            return -1;
 
         retries--;
     } while (retries > 0);
 
-    return ERROR_NOMEM;
+    return -1;
 }
 
 static void autoconnect_console(libxl_ctx *ctx_ignored,
@@ -3133,6 +3133,10 @@  void help(const char *command)
     }
 }
 
+/*
+ * parse_mem_size_kb() returns -1 on failure, or the amount of memory,
+ * on success.
+ */
 static int64_t parse_mem_size_kb(const char *mem)
 {
     char *endptr;
@@ -3275,17 +3279,17 @@  static int def_getopt(int argc, char * const argv[],
 static int set_memory_max(uint32_t domid, const char *mem)
 {
     int64_t memorykb;
-    int rc;
 
     memorykb = parse_mem_size_kb(mem);
     if (memorykb == -1) {
         fprintf(stderr, "invalid memory size: %s\n", mem);
-        exit(3);
+        exit(EXIT_FAILURE);
     }
 
-    rc = libxl_domain_setmaxmem(ctx, domid, memorykb);
+    if (libxl_domain_setmaxmem(ctx, domid, memorykb))
+	return 1;
 
-    return rc;
+    return 0;
 }
 
 int main_memmax(int argc, char **argv)
@@ -3305,10 +3309,10 @@  int main_memmax(int argc, char **argv)
     rc = set_memory_max(domid, mem);
     if (rc) {
         fprintf(stderr, "cannot set domid %d static max memory to : %s\n", domid, mem);
-        return 1;
+        return EXIT_FAILURE;
     }
 
-    return 0;
+    return EXIT_SUCCESS;
 }
 
 static void set_memory_target(uint32_t domid, const char *mem)
@@ -3318,7 +3322,7 @@  static void set_memory_target(uint32_t domid, const char *mem)
     memorykb = parse_mem_size_kb(mem);
     if (memorykb == -1)  {
         fprintf(stderr, "invalid memory size: %s\n", mem);
-        exit(3);
+        exit(EXIT_FAILURE);
     }
 
     libxl_set_memory_target(ctx, domid, memorykb, 0, /* enforce */ 1);
@@ -3338,7 +3342,7 @@  int main_memset(int argc, char **argv)
     mem = argv[optind + 1];
 
     set_memory_target(domid, mem);
-    return 0;
+    return EXIT_SUCCESS;
 }
 
 static int cd_insert(uint32_t domid, const char *virtdev, char *phys)
@@ -5783,7 +5787,7 @@  int main_sharing(int argc, char **argv)
         info = libxl_list_domain(ctx, &nb_domain);
         if (!info) {
             fprintf(stderr, "libxl_list_domain failed.\n");
-            return 1;
+            return EXIT_FAILURE;
         }
         info_free = info;
     } else if (optind == argc-1) {
@@ -5792,17 +5796,17 @@  int main_sharing(int argc, char **argv)
         if (rc == ERROR_DOMAIN_NOTFOUND) {
             fprintf(stderr, "Error: Domain \'%s\' does not exist.\n",
                 argv[optind]);
-            return -rc;
+            return EXIT_FAILURE;
         }
         if (rc) {
             fprintf(stderr, "libxl_domain_info failed (code %d).\n", rc);
-            return -rc;
+            return EXIT_FAILURE;
         }
         info = &info_buf;
         nb_domain = 1;
     } else {
         help("sharing");
-        return 2;
+        return EXIT_FAILURE;
     }
 
     sharing(info, nb_domain);
@@ -5812,7 +5816,7 @@  int main_sharing(int argc, char **argv)
     else
         libxl_dominfo_dispose(info);
 
-    return 0;
+    return EXIT_SUCCESS;
 }
 
 static int sched_domain_get(libxl_scheduler sched, int domid,