Skip to contentSkip to conversation

libxl: libxl__spawn_qdisk_backend closes fds

PATCH In review Ian Jackson 3 participants 4 messages Mar 17, 2014 – Apr 23, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: libxl__spawn_qdisk_backend closes fds

This function needs to close both null and logfile_w on both error and normal exits. (The child gets its own copy during the fork, and the parent doesn't need them any more.)

Use the standard initialise-to-unallocated, always-free style; the label "out" only makes the callback if rc is nonzero.

Coverity-ID: 1130517 and 1130518
Cc: Roger Pau Monne <[email protected]>
Cc: Ian Campbell <[email protected]>
Cc: [email protected]

---
 tools/libxl/libxl_dm.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
tools/libxl/libxl_dm.c +12 -3
index 8abed7b..d28ea4d 100644
@@ -1392,7 +1392,7 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     flexarray_t *dm_args;
     char **args;
     const char *dm;
-    int logfile_w, null, rc;
+    int logfile_w = -1, null = -1, rc;
     uint32_t domid = dmss->guest_domid;
 
     /* Always use qemu-xen as device model */
@@ -1448,12 +1448,21 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
         libxl__exec(gc, null, logfile_w, logfile_w, dm, args, NULL);
     }
 
+    rc = 0;
+
+ out:
+    if (logfile_w >= 0) close(logfile_w);
+    if (null >= 0) close(null);
+
+    /* rc is nonzero iff we had an error; if we had no error then
+     * spawn succeeded and we will continue in a further callback */
+    if (rc)
+        dmss->callback(egc, dmss, rc);
     return;
 
 error:
     assert(rc);
-    dmss->callback(egc, dmss, rc);
-    return;
+    goto out;
 }
 
 /* Generic function to signal a Qemu instance to exit */
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 9 lines · Ian Jackson

This function needs to close both null and logfile_w on both error and normal exits. (The child gets its own copy during the fork, and the parent doesn't need them any more.)

Use the standard initialise-to-unallocated, always-free style; the label "out" only makes the callback if rc is nonzero.

Coverity-ID: 1130517 and 1130518

There's a similar patch in <[email protected]>.

Quoted 24 lines
Cc Roger Pau Monne <[email protected]>
Cc Ian Campbell <[email protected]>
---
 tools/libxl/libxl_dm.c |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
tools/libxl/libxl_dm.c +1 -1
index 8abed7b..d28ea4d 100644
@@ -1392,7 +1392,7 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
     flexarray_t *dm_args;
     char **args;
     const char *dm;
-    int logfile_w, null, rc;
+    int logfile_w = -1, null = -1, rc;
     uint32_t domid = dmss->guest_domid;
 
     /* Always use qemu-xen as device model */
@@ -1448,12 +1448,21 @@ void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss)
         libxl__exec(gc, null, logfile_w, logfile_w, dm, args, NULL);
     }
 

tools/libxl/libxl_dm.c void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss) · line 1460

Quoted 10 lines
tools/libxl/libxl_dm.c +10 -0
+    rc = 0;
+
+ out:
+    if (logfile_w >= 0) close(logfile_w);
+    if (null >= 0) close(null);
+
+    /* rc is nonzero iff we had an error; if we had no error then
+     * spawn succeeded and we will continue in a further callback */
+    if (rc)
+        dmss->callback(egc, dmss, rc);

Using the device_model_spawn_outcome helper instead of directly calling the callback will print a nice error message in case spawn has failed.

tools/libxl/libxl_dm.c void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss) · line 1465

Quoted 7 lines
tools/libxl/libxl_dm.c +1 -2
     return;
 
 error:
     assert(rc);
-    dmss->callback(egc, dmss, rc);
-    return;
+    goto out;

This jump backwards is kind of strange IMHO, why not just rename the error label to out and use it instead for both the error and non-error exit paths?

Roger.

#2 lore raw

Roger Pau Monné writes ("Re: [PATCH] libxl: libxl__spawn_qdisk_backend closes fds"):

Quoted 5 lines · Ian Jackson
Quoted 4 lines

This function needs to close both null and logfile_w on both error and normal exits. (The child gets its own copy during the fork, and the parent doesn't need them any more.)

Quoted 8 lines
Quoted 5 lines

Use the standard initialise-to-unallocated, always-free style; the label "out" only makes the callback if rc is nonzero.

Coverity-ID: 1130517 and 1130518

There's a similar patch in <[email protected]>.

Yes. This posting is an updated version of it.

Quoted 7 lines

tools/libxl/libxl_dm.c void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss) · line 1460

Quoted 4 lines
tools/libxl/libxl_dm.c +4 -0
+    /* rc is nonzero iff we had an error; if we had no error then
+     * spawn succeeded and we will continue in a further callback */
+    if (rc)
+        dmss->callback(egc, dmss, rc);

Using the device_model_spawn_outcome helper instead of directly calling the callback will print a nice error message in case spawn has failed.

Good idea. I will do that as a separate patch.

Quoted 11 lines

tools/libxl/libxl_dm.c void libxl__spawn_qdisk_backend(libxl__egc *egc, libxl__dm_spawn_state *dmss) · line 1465

Quoted 7 lines
     return;

error:

     assert(rc);
tools/libxl/libxl_dm.c +1 -2
-    dmss->callback(egc, dmss, rc);
-    return;
+    goto out;

This jump backwards is kind of strange IMHO, why not just rename the error label to out and use it instead for both the error and non-error exit paths?

That would be possible, but would lose the assert(rc). An error handling bug elsewhere in the function, which failed to set rc, would result in it losing the thread of control. If we don't think the assert is important I don't mind losing it.

Ian.

#3 lore raw
Quoted 8 lines · Ian Jackson
Quoted 8 lines
Quoted 7 lines
     return;

error:

     assert(rc);
tools/libxl/libxl_dm.c +1 -2
-    dmss->callback(egc, dmss, rc);
-    return;
+    goto out;
Quoted 8 lines
Quoted 3 lines

This jump backwards is kind of strange IMHO, why not just rename the error label to out and use it instead for both the error and non-error exit paths?

That would be possible, but would lose the assert(rc). An error handling bug elsewhere in the function, which failed to set rc, would result in it losing the thread of control. If we don't think the assert is important I don't mind losing it.

The assert is only there to check that we jumped to error on an error, with a common exit path the assert would be a bit unnecessary I think.

Ian.