tools/libxl: Introduce new helper function dup_fd_helper()
authorWen Congyang <wency@cn.fujitsu.com>
Tue, 16 Feb 2016 08:06:31 +0000 (16:06 +0800)
committerChanglong Xie <xiecl.fnst@cn.fujitsu.com>
Fri, 1 Apr 2016 03:07:28 +0000 (11:07 +0800)
It is pure refactoring and no functional changes.

Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
Signed-off-by: Changlong Xie <xiecl.fnst@cn.fujitsu.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
tools/libxl/libxl_save_callout.c

index 7f1f5d444454256e4ea584cb94e3d6ad9ef67ce0..06967df49c8ac9ef9d3cdccd5d84bb88f4ec38ab 100644 (file)
@@ -119,6 +119,23 @@ void libxl__save_helper_init(libxl__save_helper_state *shs)
 
 /*----- helper execution -----*/
 
+/* This function can not fail. */
+static int dup_cloexec(libxl__gc *gc, int fd, const char *what)
+{
+    int dup_fd = fd;
+
+    if (fd <= 2) {
+        dup_fd = dup(fd);
+        if (dup_fd < 0) {
+            LOGE(ERROR,"dup %s", what);
+            exit(-1);
+        }
+    }
+    libxl_fd_set_cloexec(CTX, dup_fd, 0);
+
+    return dup_fd;
+}
+
 /*
  * Both save and restore share four parameters:
  * 1) Path to libxl-save-helper.
@@ -186,14 +203,7 @@ static void run_helper(libxl__egc *egc, libxl__save_helper_state *shs,
 
     pid_t pid = libxl__ev_child_fork(gc, &shs->child, helper_exited);
     if (!pid) {
-        if (stream_fd <= 2) {
-            stream_fd = dup(stream_fd);
-            if (stream_fd < 0) {
-                LOGE(ERROR,"dup migration stream fd");
-                exit(-1);
-            }
-        }
-        libxl_fd_set_cloexec(CTX, stream_fd, 0);
+        stream_fd = dup_cloexec(gc, stream_fd, "migration stream fd");
         *stream_fd_arg = GCSPRINTF("%d", stream_fd);
 
         for (i=0; i<num_preserve_fds; i++)