xl: Break out flush_stream
authorIan Jackson <ian.jackson@eu.citrix.com>
Fri, 3 Jul 2015 11:36:20 +0000 (12:36 +0100)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Fri, 3 Jul 2015 13:43:11 +0000 (14:43 +0100)
We are going to want to reuse this.  Adjust the code slightly to
detect right away call sites that pass something other than stdout or
stderr.

No resulting functional change.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
---
v2: New patch in this version of the mini-series

tools/libxl/xl_cmdimpl.c

index c858068b397bd5ca964128fb620336058d2348f0..ee5578684c21dbf1bb66b8f32375f9762f0d3ffc 100644 (file)
@@ -389,6 +389,20 @@ static yajl_gen_status printf_info_one_json(yajl_gen hand, int domid,
 out:
     return s;
 }
+
+static void flush_stream(FILE *fh)
+{
+    const char *fh_name =
+        fh == stdout ? "stdout" :
+        fh == stderr ? "stderr" :
+        (abort(), (const char*)0);
+
+    if (ferror(fh) || fflush(fh)) {
+        perror(fh_name);
+        exit(-1);
+    }
+}
+
 static void printf_info(enum output_format output_format,
                         int domid,
                         libxl_domain_config *d_config, FILE *fh)
@@ -424,13 +438,7 @@ out:
         fprintf(stderr,
                 "unable to format domain config as JSON (YAJL:%d)\n", s);
 
-    if (ferror(fh) || fflush(fh)) {
-        if (fh == stdout)
-            perror("stdout");
-        else
-            perror("stderr");
-        exit(-1);
-    }
+    flush_stream(fh);
 }
 
 static int do_daemonize(char *name)