]> xenbits.xensource.com Git - libvirt.git/commitdiff
bye to close(), welcome to VIR_(FORCE_)CLOSE()
authorStefan Berger <stefanb@us.ibm.com>
Tue, 9 Nov 2010 20:48:48 +0000 (15:48 -0500)
committerStefan Berger <stefanb@us.ibm.com>
Tue, 9 Nov 2010 20:48:48 +0000 (15:48 -0500)
Using automated replacement with sed and editing I have now replaced all
occurrences of close() with VIR_(FORCE_)CLOSE() except for one, of
course. Some replacements were straight forward, others I needed to pay
attention. I hope I payed attention in all the right places... Please
have a look. This should have at least solved one more double-close
error.

43 files changed:
daemon/libvirtd.c
src/lxc/lxc_container.c
src/lxc/lxc_controller.c
src/lxc/lxc_driver.c
src/node_device/node_device_linux_sysfs.c
src/nwfilter/nwfilter_ebiptables_driver.c
src/openvz/openvz_conf.c
src/openvz/openvz_driver.c
src/phyp/phyp_driver.c
src/qemu/qemu_conf.c
src/qemu/qemu_driver.c
src/qemu/qemu_monitor.c
src/remote/remote_driver.c
src/secret/secret_driver.c
src/security/security_apparmor.c
src/security/security_selinux.c
src/security/virt-aa-helper.c
src/storage/storage_backend.c
src/storage/storage_backend_fs.c
src/storage/storage_backend_iscsi.c
src/storage/storage_backend_mpath.c
src/storage/storage_backend_scsi.c
src/storage/storage_driver.c
src/test/test_driver.c
src/uml/uml_conf.c
src/uml/uml_driver.c
src/util/bridge.c
src/util/conf.c
src/util/hooks.c
src/util/interface.c
src/util/logging.c
src/util/macvtap.c
src/util/pci.c
src/util/storage_file.c
src/util/util.c
src/util/uuid.c
src/util/virtaudit.c
src/xen/xen_hypervisor.c
src/xen/xen_inotify.c
src/xen/xend_internal.c
tests/testutils.c
tools/console.c
tools/virsh.c

index d3f003ebb6297a758e1c408d5112764edc475187..ab2b5797ee6d6f06d445b5f8bc5a7a02b0760fd8 100644 (file)
@@ -50,6 +50,7 @@
 
 #include "libvirt_internal.h"
 #include "virterror_internal.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
@@ -425,7 +426,7 @@ static int daemonForkIntoBackground(void) {
             int stdoutfd = -1;
             int nextpid;
 
-            close(statuspipe[0]);
+            VIR_FORCE_CLOSE(statuspipe[0]);
 
             if ((stdinfd = open("/dev/null", O_RDONLY)) < 0)
                 goto cleanup;
@@ -437,12 +438,10 @@ static int daemonForkIntoBackground(void) {
                 goto cleanup;
             if (dup2(stdoutfd, STDERR_FILENO) != STDERR_FILENO)
                 goto cleanup;
-            if (close(stdinfd) < 0)
+            if (VIR_CLOSE(stdinfd) < 0)
                 goto cleanup;
-            stdinfd = -1;
-            if (close(stdoutfd) < 0)
+            if (VIR_CLOSE(stdoutfd) < 0)
                 goto cleanup;
-            stdoutfd = -1;
 
             if (setsid() < 0)
                 goto cleanup;
@@ -458,10 +457,8 @@ static int daemonForkIntoBackground(void) {
             }
 
         cleanup:
-            if (stdoutfd != -1)
-                close(stdoutfd);
-            if (stdinfd != -1)
-                close(stdinfd);
+            VIR_FORCE_CLOSE(stdoutfd);
+            VIR_FORCE_CLOSE(stdinfd);
             return -1;
 
         }
@@ -475,7 +472,7 @@ static int daemonForkIntoBackground(void) {
             int ret;
             char status;
 
-            close(statuspipe[1]);
+            VIR_FORCE_CLOSE(statuspipe[1]);
 
             /* We wait to make sure the first child forked successfully */
             if ((got = waitpid(pid, &exitstatus, 0)) < 0 ||
@@ -518,7 +515,7 @@ static int qemudWritePidFile(const char *pidFile) {
     if (!(fh = fdopen(fd, "w"))) {
         VIR_ERROR(_("Failed to fdopen pid file '%s' : %s"),
                   pidFile, virStrerror(errno, ebuf, sizeof ebuf));
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
@@ -610,8 +607,7 @@ static int qemudListenUnix(struct qemud_server *server,
     return 0;
 
  cleanup:
-    if (sock->fd >= 0)
-        close(sock->fd);
+    VIR_FORCE_CLOSE(sock->fd);
     VIR_FREE(sock);
     return -1;
 }
@@ -665,7 +661,7 @@ remoteMakeSockets (int *fds, int max_fds, int *nfds_r, const char *node, const c
                 VIR_ERROR(_("bind: %s"), virStrerror (errno, ebuf, sizeof ebuf));
                 return -1;
             }
-            close (fds[*nfds_r]);
+            VIR_FORCE_CLOSE(fds[*nfds_r]);
         } else {
             ++*nfds_r;
         }
@@ -734,7 +730,7 @@ remoteListenTCP (struct qemud_server *server,
 
 cleanup:
     for (i = 0; i < nfds; ++i)
-        close(fds[i]);
+        VIR_FORCE_CLOSE(fds[i]);
     return -1;
 }
 
@@ -1483,7 +1479,7 @@ error:
         VIR_FREE(client);
     }
     VIR_FREE(addrstr);
-    close (fd);
+    VIR_FORCE_CLOSE(fd);
     PROBE(CLIENT_DISCONNECT, "fd=%d", fd);
     return -1;
 }
@@ -1529,8 +1525,7 @@ void qemudDispatchClientFailure(struct qemud_client *client) {
     }
     if (client->fd != -1) {
         PROBE(CLIENT_DISCONNECT, "fd=%d", client->fd);
-        close(client->fd);
-        client->fd = -1;
+        VIR_FORCE_CLOSE(client->fd);
     }
     VIR_FREE(client->addrstr);
 }
@@ -2433,17 +2428,15 @@ static int qemudStartEventLoop(struct qemud_server *server) {
 static void qemudCleanup(struct qemud_server *server) {
     struct qemud_socket *sock;
 
-    if (server->sigread != -1)
-        close(server->sigread);
-    if (server->sigwrite != -1)
-        close(server->sigwrite);
+    VIR_FORCE_CLOSE(server->sigread);
+    VIR_FORCE_CLOSE(server->sigwrite);
 
     sock = server->sockets;
     while (sock) {
         struct qemud_socket *next = sock->next;
         if (sock->watch)
             virEventRemoveHandleImpl(sock->watch);
-        close(sock->fd);
+        VIR_FORCE_CLOSE(sock->fd);
 
         /* Unlink unix domain sockets which are not in
          * the abstract namespace */
@@ -2999,8 +2992,8 @@ daemonSetupSignals(struct qemud_server *server)
     return 0;
 
 error:
-    close(sigpipe[0]);
-    close(sigpipe[1]);
+    VIR_FORCE_CLOSE(sigpipe[0]);
+    VIR_FORCE_CLOSE(sigpipe[1]);
     return -1;
 }
 
@@ -3257,8 +3250,7 @@ int main(int argc, char **argv) {
         while (write(statuswrite, &status, 1) == -1 &&
                errno == EINTR)
             ;
-        close(statuswrite);
-        statuswrite = -1;
+        VIR_FORCE_CLOSE(statuswrite);
     }
 
     /* Start the event loop in a background thread, since
@@ -3315,7 +3307,7 @@ error:
                    errno == EINTR)
                 ;
         }
-        close(statuswrite);
+        VIR_FORCE_CLOSE(statuswrite);
     }
     if (server)
         qemudCleanup(server);
index b7f025ae92d5f31af3756458bfa61a95b7f20259..0e06a2dcd095d50d81d31284c9aaa2ce5c4773de 100644 (file)
@@ -52,6 +52,7 @@
 #include "util.h"
 #include "memory.h"
 #include "veth.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -145,8 +146,10 @@ static int lxcContainerSetStdio(int control, int ttyfd)
      * close all FDs before executing the container */
     open_max = sysconf (_SC_OPEN_MAX);
     for (i = 0; i < open_max; i++)
-        if (i != ttyfd && i != control)
-            close(i);
+        if (i != ttyfd && i != control) {
+            int tmpfd = i;
+            VIR_FORCE_CLOSE(tmpfd);
+        }
 
     if (dup2(ttyfd, 0) < 0) {
         virReportSystemError(errno, "%s",
@@ -222,7 +225,7 @@ static int lxcContainerWaitForContinue(int control)
                              _("Failed to read the container continue message"));
         return -1;
     }
-    close(control);
+    VIR_FORCE_CLOSE(control);
 
     DEBUG0("Received container continue message");
 
@@ -776,10 +779,10 @@ static int lxcContainerChild( void *data )
     VIR_FREE(ttyPath);
 
     if (lxcContainerSetStdio(argv->monitor, ttyfd) < 0) {
-        close(ttyfd);
+        VIR_FORCE_CLOSE(ttyfd);
         return -1;
     }
-    close(ttyfd);
+    VIR_FORCE_CLOSE(ttyfd);
 
     if (lxcContainerSetupMounts(vmDef, root) < 0)
         return -1;
index 2a4f113315ba3173d3383832d666d3599b4da089..eb0148e4b7d773a8e98f68b2d6b028abb159dd09 100644 (file)
@@ -48,6 +48,7 @@
 #include "veth.h"
 #include "memory.h"
 #include "util.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -233,8 +234,7 @@ static int lxcMonitorServer(const char *sockpath)
     return fd;
 
 error:
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     return -1;
 }
 
@@ -409,7 +409,7 @@ static int lxcControllerMain(int monitor,
                     goto cleanup;
                 }
                 if (client != -1) { /* Already connected, so kick new one out */
-                    close(fd);
+                    VIR_FORCE_CLOSE(fd);
                     continue;
                 }
                 client = fd;
@@ -426,8 +426,7 @@ static int lxcControllerMain(int monitor,
                                          _("epoll_ctl(client) failed"));
                     goto cleanup;
                 }
-                close(client);
-                client = -1;
+                VIR_FORCE_CLOSE(client);
             } else {
                 if (epollEvent.events & EPOLLIN) {
                     curFdOff = epollEvent.data.fd == appPty ? 0 : 1;
@@ -485,9 +484,9 @@ static int lxcControllerMain(int monitor,
     rc = 0;
 
 cleanup:
-    close(appPty);
-    close(contPty);
-    close(epollFd);
+    VIR_FORCE_CLOSE(appPty);
+    VIR_FORCE_CLOSE(contPty);
+    VIR_FORCE_CLOSE(epollFd);
     return rc;
 }
 
@@ -660,8 +659,7 @@ lxcControllerRun(virDomainDefPtr def,
                                        control[1],
                                        containerPtyPath)) < 0)
         goto cleanup;
-    close(control[1]);
-    control[1] = -1;
+    VIR_FORCE_CLOSE(control[1]);
 
     if (lxcControllerMoveInterfaces(nveths, veths, container) < 0)
         goto cleanup;
@@ -679,13 +677,10 @@ lxcControllerRun(virDomainDefPtr def,
 cleanup:
     VIR_FREE(devptmx);
     VIR_FREE(devpts);
-    if (control[0] != -1)
-        close(control[0]);
-    if (control[1] != -1)
-        close(control[1]);
+    VIR_FORCE_CLOSE(control[0]);
+    VIR_FORCE_CLOSE(control[1]);
     VIR_FREE(containerPtyPath);
-    if (containerPty != -1)
-        close(containerPty);
+    VIR_FORCE_CLOSE(containerPty);
 
     if (container > 1) {
         int status;
index f7630dd200db645f2de8fe2789f735dda47d9199..2616c43f6df0dd1cd65090a49621c8c6c2a15dcb 100644 (file)
@@ -51,6 +51,7 @@
 #include "uuid.h"
 #include "stats_linux.h"
 #include "hooks.h"
+#include "files.h"
 
 
 #define VIR_FROM_THIS VIR_FROM_LXC
@@ -969,7 +970,7 @@ static int lxcVmCleanup(lxc_driver_t *driver,
     }
 
     virEventRemoveHandle(priv->monitorWatch);
-    close(priv->monitor);
+    VIR_FORCE_CLOSE(priv->monitor);
 
     virFileDeletePid(driver->stateDir, vm->def->name);
     virDomainDeleteConfig(driver->stateDir, NULL, vm);
@@ -1151,8 +1152,7 @@ static int lxcMonitorClient(lxc_driver_t * driver,
 
 error:
     VIR_FREE(sockpath);
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     return -1;
 }
 
@@ -1534,19 +1534,18 @@ static int lxcVmStart(virConnectPtr conn,
     rc = 0;
 
 cleanup:
+    if (VIR_CLOSE(logfd) < 0) {
+        virReportSystemError(errno, "%s", _("could not close logfile"));
+        rc = -1;
+    }
     for (i = 0 ; i < nveths ; i++) {
         if (rc != 0)
             vethDelete(veths[i]);
         VIR_FREE(veths[i]);
     }
-    if (rc != 0 && priv->monitor != -1) {
-        close(priv->monitor);
-        priv->monitor = -1;
-    }
-    if (parentTty != -1)
-        close(parentTty);
-    if (logfd != -1)
-        close(logfd);
+    if (rc != 0)
+        VIR_FORCE_CLOSE(priv->monitor);
+    VIR_FORCE_CLOSE(parentTty);
     VIR_FREE(logfile);
     return rc;
 }
@@ -2006,8 +2005,7 @@ lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
 
     /* Read pid from controller */
     if ((virFileReadPid(lxc_driver->stateDir, vm->def->name, &vm->pid)) != 0) {
-        close(priv->monitor);
-        priv->monitor = -1;
+        VIR_FORCE_CLOSE(priv->monitor);
         goto cleanup;
     }
 
@@ -2037,8 +2035,7 @@ lxcReconnectVM(void *payload, const char *name ATTRIBUTE_UNUSED, void *opaque)
         }
     } else {
         vm->def->id = -1;
-        close(priv->monitor);
-        priv->monitor = -1;
+        VIR_FORCE_CLOSE(priv->monitor);
     }
 
 cleanup:
index 7f09cc73e0dae2f485a886168fe1fb4d118d0fdd..ea0b30e2aa117938605d621c8672ff7fd82232c6 100644 (file)
@@ -31,6 +31,7 @@
 #include "virterror_internal.h"
 #include "memory.h"
 #include "logging.h"
+#include "files.h"
 #include <dirent.h>
 
 #define VIR_FROM_THIS VIR_FROM_NODEDEV
@@ -104,9 +105,7 @@ int read_wwn_linux(int host, const char *file, char **wwn)
     }
 
 out:
-    if (fd != -1) {
-        close(fd);
-    }
+    VIR_FORCE_CLOSE(fd);
     return retval;
 }
 
index 21b1b5122b6734d0299f30c5e9f988f3cca216f4..20d1ba3f3e678d7876b05bb2cb5fbeca94323009 100644 (file)
@@ -37,6 +37,7 @@
 #include "nwfilter_conf.h"
 #include "nwfilter_gentech_driver.h"
 #include "nwfilter_ebiptables_driver.h"
+#include "files.h"
 
 
 #define VIR_FROM_THIS VIR_FROM_NWFILTER
@@ -2493,13 +2494,12 @@ ebiptablesWriteToTempFile(const char *string) {
     }
 
     VIR_FREE(header);
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     return filnam;
 
 err_exit:
     VIR_FREE(header);
-    if (fd >= 0)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     unlink(filename);
     return NULL;
 }
@@ -3259,7 +3259,7 @@ iptablesCheckBridgeNFCallEnabled(bool isIPv6)
                         lastReport = now;
                 }
             }
-            close(fd);
+            VIR_FORCE_CLOSE(fd);
         }
     }
 }
index c84a6f3a1eb24b5e7eb890db21dd20a289bcbdce..48e17246dec006e687e5e6faeacdfa7349028b95 100644 (file)
@@ -50,6 +50,7 @@
 #include "memory.h"
 #include "util.h"
 #include "nodeinfo.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_OPENVZ
 
@@ -109,7 +110,7 @@ openvzExtractVersionInfo(const char *cmd, int *retversion)
 
 cleanup2:
     VIR_FREE(help);
-    if (close(newstdout) < 0)
+    if (VIR_CLOSE(newstdout) < 0)
         ret = -1;
 
 rewait:
@@ -569,7 +570,7 @@ openvzWriteConfigParam(const char * conf_file, const char *param, const char *va
         goto error;
     temp_fd = open(temp_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
     if (temp_fd == -1) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         goto error;
     }
 
@@ -590,12 +591,10 @@ openvzWriteConfigParam(const char * conf_file, const char *param, const char *va
         safewrite(temp_fd, "\"\n", 2) < 0)
         goto error;
 
-    if (close(fd) < 0)
+    if (VIR_CLOSE(fd) < 0)
         goto error;
-    fd = -1;
-    if (close(temp_fd) < 0)
+    if (VIR_CLOSE(temp_fd) < 0)
         goto error;
-    temp_fd = -1;
 
     if (rename(temp_file, conf_file) < 0)
         goto error;
@@ -603,10 +602,8 @@ openvzWriteConfigParam(const char * conf_file, const char *param, const char *va
     return 0;
 
 error:
-    if (fd != -1)
-        close(fd);
-    if (temp_fd != -1)
-        close(temp_fd);
+    VIR_FORCE_CLOSE(fd);
+    VIR_FORCE_CLOSE(temp_fd);
     if (temp_file)
         unlink(temp_file);
     VIR_FREE(temp_file);
@@ -662,7 +659,7 @@ openvzReadConfigParam(const char * conf_file ,const char * param, char *value, i
             }
        }
     }
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     if (ret == 0 && found)
         ret = 1;
@@ -703,7 +700,7 @@ openvz_copyfile(char* from_path, char* to_path)
         return -1;
     copy_fd = open(to_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
     if (copy_fd == -1) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
@@ -716,19 +713,16 @@ openvz_copyfile(char* from_path, char* to_path)
             goto error;
     }
 
-    if (close(fd) < 0)
+    if (VIR_CLOSE(fd) < 0)
         goto error;
-    fd = -1;
-    if (close(copy_fd) < 0)
+    if (VIR_CLOSE(copy_fd) < 0)
         goto error;
 
     return 0;
 
 error:
-    if (fd != -1)
-        close(fd);
-    if (copy_fd != -1)
-        close(copy_fd);
+    VIR_FORCE_CLOSE(fd);
+    VIR_FORCE_CLOSE(copy_fd);
     return -1;
 }
 
@@ -880,8 +874,7 @@ openvzGetVPSUUID(int vpsid, char *uuidstr, size_t len)
     }
     retval = 0;
 cleanup:
-    if (fd >= 0)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     VIR_FREE(conf_file);
 
     return retval;
index b7c2754c1340de1248dcb14d0fc653c5cbe07a32..2893f6982cf0a2a4a47d36524bb60f9f54c223b9 100644 (file)
@@ -57,6 +57,7 @@
 #include "nodeinfo.h"
 #include "memory.h"
 #include "bridge.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_OPENVZ
 
@@ -1540,7 +1541,7 @@ Version: 2.2
         }
     }
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (ret < 0)
         return -1;
 
index 3d0ed111167c9371b5404102423ffd1ef77621d0..c1252dd0e4ebcb037e0fe4f78687cb689c8a8224 100644 (file)
@@ -58,6 +58,7 @@
 #include "domain_conf.h"
 #include "storage_conf.h"
 #include "nodeinfo.h"
+#include "files.h"
 
 #include "phyp_driver.h"
 
@@ -457,11 +458,15 @@ phypUUIDTable_WriteFile(virConnectPtr conn)
         }
     }
 
-    close(fd);
+    if (VIR_CLOSE(fd) < 0) {
+        virReportSystemError(errno, _("Could not close %s"),
+                             local_file);
+        goto err;
+    }
     return 0;
 
   err:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     return -1;
 }
 
@@ -672,11 +677,11 @@ phypUUIDTable_ReadFile(virConnectPtr conn)
     } else
         virReportOOMError();
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     return 0;
 
   err:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     return -1;
 }
 
@@ -764,7 +769,11 @@ phypUUIDTable_Pull(virConnectPtr conn)
         }
         break;
     }
-    close(fd);
+    if (VIR_CLOSE(fd) < 0) {
+        virReportSystemError(errno, _("Could not close %s"),
+                             local_file);
+        goto err;
+    }
     goto exit;
 
   exit:
@@ -1001,7 +1010,7 @@ openSSHSession(virConnectPtr conn, virConnectAuthPtr auth,
             if (connect(sock, cur->ai_addr, cur->ai_addrlen) == 0) {
                 goto connected;
             }
-            close(sock);
+            VIR_FORCE_CLOSE(sock);
         }
         cur = cur->ai_next;
     }
index 9e6e16262386ed5d21eff2f3c63bdfce375eaba9..9974cf4d971c99588f4df9b68225f4be495202c0 100644 (file)
@@ -55,6 +55,7 @@
 #include "macvtap.h"
 #include "cpu/cpu.h"
 #include "domain_nwfilter.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
@@ -588,7 +589,7 @@ qemudProbeMachineTypes(const char *binary,
 cleanup2:
     VIR_FREE(output);
 cleanup:
-    if (close(newstdout) < 0)
+    if (VIR_CLOSE(newstdout) < 0)
         ret = -1;
 
 rewait:
@@ -838,7 +839,7 @@ qemudProbeCPUModels(const char *qemu,
 
 cleanup:
     VIR_FREE(output);
-    if (close(newstdout) < 0)
+    if (VIR_CLOSE(newstdout) < 0)
         ret = -1;
 
 rewait:
@@ -1495,7 +1496,7 @@ static void qemudParsePCIDeviceStrs(const char *qemu, unsigned long long *flags)
 
 cleanup:
     VIR_FREE(pciassign);
-    close(newstderr);
+    VIR_FORCE_CLOSE(newstderr);
 rewait:
     if (waitpid(child, &status, 0) != child) {
         if (errno == EINTR)
@@ -1555,7 +1556,7 @@ int qemudExtractVersionInfo(const char *qemu,
 
 cleanup2:
     VIR_FREE(help);
-    if (close(newstdout) < 0)
+    if (VIR_CLOSE(newstdout) < 0)
         ret = -1;
 
 rewait:
@@ -1670,8 +1671,7 @@ qemudPhysIfaceConnect(virConnectPtr conn,
         if ((net->filter) && (net->ifname)) {
             err = virDomainConfNWFilterInstantiate(conn, net);
             if (err) {
-                close(rc);
-                rc = -1;
+                VIR_FORCE_CLOSE(rc);
                 delMacvtap(net->ifname, net->mac, net->data.direct.linkdev,
                            &net->data.direct.virtPortProfile);
                 VIR_FREE(net->ifname);
@@ -1816,10 +1816,8 @@ qemudNetworkIfaceConnect(virConnectPtr conn,
     if (tapfd >= 0) {
         if ((net->filter) && (net->ifname)) {
             err = virDomainConfNWFilterInstantiate(conn, net);
-            if (err) {
-                close(tapfd);
-                tapfd = -1;
-            }
+            if (err)
+                VIR_FORCE_CLOSE(tapfd);
         }
     }
 
@@ -4782,7 +4780,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
                 if (VIR_REALLOC_N(*vmfds, (*nvmfds)+1) < 0) {
                     virDomainConfNWFilterTeardown(net);
-                    close(tapfd);
+                    VIR_FORCE_CLOSE(tapfd);
                     goto no_memory;
                 }
 
@@ -4801,7 +4799,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
                 if (VIR_REALLOC_N(*vmfds, (*nvmfds)+1) < 0) {
                     virDomainConfNWFilterTeardown(net);
-                    close(tapfd);
+                    VIR_FORCE_CLOSE(tapfd);
                     goto no_memory;
                 }
 
@@ -4821,7 +4819,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
                 int vhostfd = qemudOpenVhostNet(net, qemuCmdFlags);
                 if (vhostfd >= 0) {
                     if (VIR_REALLOC_N(*vmfds, (*nvmfds)+1) < 0) {
-                        close(vhostfd);
+                        VIR_FORCE_CLOSE(vhostfd);
                         goto no_memory;
                     }
 
@@ -5410,14 +5408,14 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
                     if (configfd >= 0) {
                         if (virAsprintf(&configfd_name, "%d", configfd) < 0) {
-                            close(configfd);
+                            VIR_FORCE_CLOSE(configfd);
                             virReportOOMError();
                             goto no_memory;
                         }
 
                         if (VIR_REALLOC_N(*vmfds, (*nvmfds)+1) < 0) {
                             VIR_FREE(configfd_name);
-                            close(configfd);
+                            VIR_FORCE_CLOSE(configfd);
                             goto no_memory;
                         }
 
@@ -5510,7 +5508,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
     if (vmfds &&
         *vmfds) {
         for (i = 0; i < *nvmfds; i++)
-            close((*vmfds)[i]);
+            VIR_FORCE_CLOSE((*vmfds)[i]);
         VIR_FREE(*vmfds);
         *nvmfds = 0;
     }
index 052f95dacb0cc39349766135c792577b941214ea..bb96c1f9b9f43e066225a9ce0bba55250efc909c 100644 (file)
@@ -82,6 +82,7 @@
 #include "hooks.h"
 #include "storage_file.h"
 #include "virtaudit.h"
+#include "files.h"
 
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
@@ -768,7 +769,7 @@ qemudLogFD(struct qemud_driver *driver, const char* name)
     if (virSetCloseExec(fd) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set VM logfile close-on-exec flag"));
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
     return fd;
@@ -800,14 +801,14 @@ qemudLogReadFD(const char* logDir, const char* name, off_t pos)
     if (virSetCloseExec(fd) < 0) {
         virReportSystemError(errno, "%s",
                              _("Unable to set VM logfile close-on-exec flag"));
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
     if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) {
-      virReportSystemError(pos < 0 ? 0 : errno,
+        virReportSystemError(pos < 0 ? 0 : errno,
                              _("Unable to seek to %lld in %s"),
                              (long long) pos, logfile);
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
     }
     return fd;
 }
@@ -2401,7 +2402,7 @@ cleanup:
     }
 
 closelog:
-    if (close(logfd) < 0) {
+    if (VIR_CLOSE(logfd) < 0) {
         char ebuf[4096];
         VIR_WARN("Unable to close logfile: %s",
                  virStrerror(errno, ebuf, sizeof ebuf));
@@ -2982,13 +2983,13 @@ static int qemudNextFreePort(struct qemud_driver *driver,
             return -1;
 
         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse, sizeof(reuse)) < 0) {
-            close(fd);
+            VIR_FORCE_CLOSE(fd);
             break;
         }
 
         if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
             /* Not in use, lets grab it */
-            close(fd);
+            VIR_FORCE_CLOSE(fd);
             /* Add port to bitmap of reserved ports */
             if (virBitmapSetBit(driver->reservedVNCPorts,
                                 i - QEMU_VNC_PORT_MIN) < 0) {
@@ -2997,7 +2998,7 @@ static int qemudNextFreePort(struct qemud_driver *driver,
             }
             return i;
         }
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
 
         if (errno == EADDRINUSE) {
             /* In use, try next */
@@ -3249,7 +3250,7 @@ qemuPrepareChardevDevice(virDomainDefPtr def ATTRIBUTE_UNUSED,
         return -1;
     }
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return 0;
 }
@@ -4163,7 +4164,7 @@ static int qemudStartVMDaemon(virConnectPtr conn,
 
     if (vmfds) {
         for (i = 0 ; i < nvmfds ; i++) {
-            close(vmfds[i]);
+            VIR_FORCE_CLOSE(vmfds[i]);
         }
         VIR_FREE(vmfds);
     }
@@ -4216,8 +4217,7 @@ static int qemudStartVMDaemon(virConnectPtr conn,
     if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0)
         goto cleanup;
 
-    if (logfile != -1)
-        close(logfile);
+    VIR_FORCE_CLOSE(logfile);
 
     return 0;
 
@@ -4227,8 +4227,7 @@ cleanup:
      * pretend we never started it */
     qemudShutdownVMDaemon(driver, vm, 0);
 
-    if (logfile != -1)
-        close(logfile);
+    VIR_FORCE_CLOSE(logfile);
 
     return -1;
 }
@@ -4503,7 +4502,7 @@ static int kvmGetMaxVCPUs(void) {
     if (r > 0)
         maxvcpus = r;
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     return maxvcpus;
 }
 
@@ -5608,10 +5607,10 @@ static int qemudDomainSaveFlag(struct qemud_driver *driver, virDomainPtr dom,
             goto endjob;
         }
         if (qemudDomainSaveFileOpHook(fd, &hdata) < 0) {
-            close(fd);
+            VIR_FORCE_CLOSE(fd);
             goto endjob;
         }
-        if (close(fd) < 0) {
+        if (VIR_CLOSE(fd) < 0) {
             virReportSystemError(errno, _("unable to close %s"), path);
             goto endjob;
         }
@@ -6047,7 +6046,7 @@ static int qemudDomainCoreDump(virDomainPtr dom,
         goto endjob;
     }
 
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno,
                              _("unable to save file %s"),
                              path);
@@ -6692,8 +6691,7 @@ static int qemudOpenAsUID(const char *path, uid_t uid, pid_t *child_pid) {
         /* parent */
 
         /* parent doesn't need the write side of the pipe */
-        close(pipefd[1]);
-        pipefd[1] = -1;
+        VIR_FORCE_CLOSE(pipefd[1]);
 
         if (forkRet < 0) {
             virReportSystemError(errno,
@@ -6705,10 +6703,8 @@ static int qemudOpenAsUID(const char *path, uid_t uid, pid_t *child_pid) {
         fd = pipefd[0];
         pipefd[0] = -1;
 parent_cleanup:
-        if (pipefd[0] != -1)
-            close(pipefd[0]);
-        if (pipefd[1] != -1)
-            close(pipefd[1]);
+        VIR_FORCE_CLOSE(pipefd[0]);
+        VIR_FORCE_CLOSE(pipefd[1]);
         if ((fd < 0) && (*child_pid > 0)) {
             /* a child process was started and subsequently an error
                occurred in the parent, so we need to wait for it to
@@ -6734,7 +6730,7 @@ parent_cleanup:
     struct passwd pwd, *pwd_result;
 
     /* child doesn't need the read side of the pipe */
-    close(pipefd[0]);
+    VIR_FORCE_CLOSE(pipefd[0]);
 
     if (forkRet < 0) {
         exit_code = errno;
@@ -6799,10 +6795,8 @@ parent_cleanup:
 
 child_cleanup:
     VIR_FREE(buf);
-    if (fd != -1)
-        close(fd);
-    if (pipefd[1] != -1)
-        close(pipefd[1]);
+    VIR_FORCE_CLOSE(fd);
+    VIR_FORCE_CLOSE(pipefd[1]);
     _exit(exit_code);
 }
 
@@ -6810,8 +6804,10 @@ static int qemudDomainSaveImageClose(int fd, pid_t read_pid, int *status)
 {
     int ret = 0;
 
-    if (fd != -1)
-        close(fd);
+    if (VIR_CLOSE(fd) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("cannot close file"));
+    }
 
     if (read_pid != -1) {
         /* reap the process that read the file */
@@ -6967,8 +6963,7 @@ qemudDomainSaveImageStartVM(virConnectPtr conn,
             /* empty */
         }
     }
-    if (intermediatefd != -1)
-        close(intermediatefd);
+    VIR_FORCE_CLOSE(intermediatefd);
 
     wait_ret = qemudDomainSaveImageClose(fd, read_pid, &status);
     fd = -1;
@@ -8347,9 +8342,7 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
     }
     qemuDomainObjExitMonitorWithDriver(driver, vm);
 
-    if (tapfd != -1)
-        close(tapfd);
-    tapfd = -1;
+    VIR_FORCE_CLOSE(tapfd);
 
     if (!virDomainObjIsActive(vm)) {
         qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -8403,8 +8396,7 @@ cleanup:
     VIR_FREE(nicstr);
     VIR_FREE(netstr);
     VIR_FREE(tapfd_name);
-    if (tapfd != -1)
-        close(tapfd);
+    VIR_FORCE_CLOSE(tapfd);
 
     return ret;
 
@@ -8533,8 +8525,7 @@ static int qemudDomainAttachHostPciDevice(struct qemud_driver *driver,
 
     VIR_FREE(devstr);
     VIR_FREE(configfd_name);
-    if (configfd >= 0)
-        close(configfd);
+    VIR_FORCE_CLOSE(configfd);
 
     return 0;
 
@@ -8548,8 +8539,7 @@ error:
 
     VIR_FREE(devstr);
     VIR_FREE(configfd_name);
-    if (configfd >= 0)
-        close(configfd);
+    VIR_FORCE_CLOSE(configfd);
 
     return -1;
 }
@@ -10430,8 +10420,7 @@ qemudDomainBlockPeek (virDomainPtr dom,
     }
 
 cleanup:
-    if (fd >= 0)
-        close (fd);
+    VIR_FORCE_CLOSE(fd);
     if (vm)
         virDomainObjUnlock(vm);
     return ret;
@@ -10518,7 +10507,7 @@ endjob:
 
 cleanup:
     VIR_FREE(tmp);
-    if (fd >= 0) close (fd);
+    VIR_FORCE_CLOSE(fd);
     unlink (tmp);
     if (vm)
         virDomainObjUnlock(vm);
@@ -10674,8 +10663,7 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
     }
 
 cleanup:
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (vm)
         virDomainObjUnlock(vm);
     return ret;
@@ -10990,8 +10978,7 @@ cleanup:
 
 static void qemuStreamMigFree(struct qemuStreamMigFile *qemust)
 {
-    if (qemust->fd != -1)
-        close(qemust->fd);
+    VIR_FORCE_CLOSE(qemust->fd);
     VIR_FREE(qemust);
 }
 
@@ -11832,10 +11819,8 @@ finish:
     qemuDomainObjExitRemoteWithDriver(driver, vm);
 
 cleanup:
-    if (client_sock != -1)
-        close(client_sock);
-    if (qemu_sock != -1)
-        close(qemu_sock);
+    VIR_FORCE_CLOSE(client_sock);
+    VIR_FORCE_CLOSE(qemu_sock);
 
     if (ddomain)
         virUnrefDomain(ddomain);
@@ -12616,8 +12601,7 @@ cleanup:
     VIR_FREE(snapFile);
     VIR_FREE(snapDir);
     VIR_FREE(newxml);
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     return ret;
 }
 
index 2366fdb6d6292f428e36e0f21a90e66d26abbd83..f65512dd122fad07f6dacb783695c8c1f20cfce3 100644 (file)
@@ -36,6 +36,7 @@
 #include "virterror_internal.h"
 #include "memory.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
@@ -283,7 +284,7 @@ qemuMonitorOpenUnix(const char *monitor)
     return monfd;
 
 error:
-    close(monfd);
+    VIR_FORCE_CLOSE(monfd);
     return -1;
 }
 
index 1c874b22e796769ad353ab14f508fbf760533064..69768596769b270c472612841af1e651b6288df0 100644 (file)
@@ -82,6 +82,7 @@
 #include "util.h"
 #include "event.h"
 #include "ignore-value.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_REMOTE
 
@@ -621,7 +622,7 @@ doRemoteOpen (virConnectPtr conn,
 
             if (connect (priv->sock, r->ai_addr, r->ai_addrlen) == -1) {
                 saved_errno = errno;
-                close (priv->sock);
+                VIR_FORCE_CLOSE(priv->sock);
                 continue;
             }
 
@@ -630,8 +631,7 @@ doRemoteOpen (virConnectPtr conn,
                     negotiate_gnutls_on_connection
                       (conn, priv, no_verify);
                 if (!priv->session) {
-                    close (priv->sock);
-                    priv->sock = -1;
+                    VIR_FORCE_CLOSE(priv->sock);
                     goto failed;
                 }
             }
@@ -711,8 +711,7 @@ doRemoteOpen (virConnectPtr conn,
             if (errno == ECONNREFUSED &&
                 flags & VIR_DRV_OPEN_REMOTE_AUTOSTART &&
                 trials < 20) {
-                close(priv->sock);
-                priv->sock = -1;
+                VIR_FORCE_CLOSE(priv->sock);
                 if (trials > 0 ||
                     remoteForkDaemon() == 0) {
                     trials++;
@@ -806,8 +805,8 @@ doRemoteOpen (virConnectPtr conn,
             goto failed;
 
         /* Parent continues here. */
-        close (sv[1]);
-        close (errfd[1]);
+        VIR_FORCE_CLOSE(sv[1]);
+        VIR_FORCE_CLOSE(errfd[1]);
         priv->sock = sv[0];
         priv->errfd = errfd[0];
         priv->pid = pid;
@@ -955,15 +954,14 @@ doRemoteOpen (virConnectPtr conn,
 
  failed:
     /* Close the socket if we failed. */
-    if (priv->errfd >= 0)
-        close(priv->errfd);
+    VIR_FORCE_CLOSE(priv->errfd);
 
     if (priv->sock >= 0) {
         if (priv->uses_tls && priv->session) {
             gnutls_bye (priv->session, GNUTLS_SHUT_RDWR);
             gnutls_deinit (priv->session);
         }
-        close (priv->sock);
+        VIR_FORCE_CLOSE(priv->sock);
 #ifndef WIN32
         if (priv->pid > 0) {
             pid_t reap;
@@ -977,10 +975,8 @@ retry:
 #endif
     }
 
-    if (wakeupFD[0] >= 0) {
-        close(wakeupFD[0]);
-        close(wakeupFD[1]);
-    }
+    VIR_FORCE_CLOSE(wakeupFD[0]);
+    VIR_FORCE_CLOSE(wakeupFD[1]);
 
     VIR_FREE(priv->hostname);
     goto cleanup;
@@ -1442,8 +1438,8 @@ doRemoteClose (virConnectPtr conn, struct private_data *priv)
     if (priv->saslconn)
         sasl_dispose (&priv->saslconn);
 #endif
-    close (priv->sock);
-    close (priv->errfd);
+    VIR_FORCE_CLOSE(priv->sock);
+    VIR_FORCE_CLOSE(priv->errfd);
 
 #ifndef WIN32
     if (priv->pid > 0) {
@@ -1456,10 +1452,8 @@ retry:
         } while (reap != -1 && reap != priv->pid);
     }
 #endif
-    if (priv->wakeupReadFD >= 0) {
-        close(priv->wakeupReadFD);
-        close(priv->wakeupSendFD);
-    }
+    VIR_FORCE_CLOSE(priv->wakeupReadFD);
+    VIR_FORCE_CLOSE(priv->wakeupSendFD);
 
 
     /* Free hostname copy */
index 01c0034343104ccb09417231d2e580a9bca55255..2b5a7c6e75c45d0bf982c4dd21a9c7f534153360 100644 (file)
@@ -41,6 +41,7 @@
 #include "util.h"
 #include "uuid.h"
 #include "virterror_internal.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_SECRET
 
@@ -181,7 +182,7 @@ replaceFile(const char *filename, void *data, size_t size)
                               tmp_path);
         goto cleanup;
     }
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno, _("error closing '%s'"), tmp_path);
         goto cleanup;
     }
@@ -196,8 +197,7 @@ replaceFile(const char *filename, void *data, size_t size)
     ret = 0;
 
 cleanup:
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (tmp_path != NULL) {
         unlink(tmp_path);
         VIR_FREE(tmp_path);
@@ -394,8 +394,7 @@ secretLoadValue(virSecretDriverStatePtr driver,
         virReportSystemError(errno, _("cannot read '%s'"), filename);
         goto cleanup;
     }
-    close(fd);
-    fd = -1;
+    VIR_FORCE_CLOSE(fd);
 
     if (!base64_decode_alloc(contents, st.st_size, &value, &value_size)) {
         virSecretReportError(VIR_ERR_INTERNAL_ERROR,
@@ -422,8 +421,7 @@ cleanup:
         memset(contents, 0, st.st_size);
         VIR_FREE(contents);
     }
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     VIR_FREE(filename);
     return ret;
 }
index 16bba2a11104b30c47ad486702b70edc33bb9704..be39f758fced109321803eb7a7232d4e87ff66e2 100644 (file)
@@ -37,6 +37,7 @@
 #include "uuid.h"
 #include "pci.h"
 #include "hostusb.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_SECURITY
 #define SECURITY_APPARMOR_VOID_DOI      "0"
@@ -215,7 +216,7 @@ load_profile(virSecurityDriverPtr drv,
         virReportSystemError(errno, "%s", _("unable to write to pipe"));
         goto clean;
     }
-    close(pipefd[1]);
+    VIR_FORCE_CLOSE(pipefd[1]);
     rc = 0;
 
   rewait:
@@ -233,10 +234,8 @@ load_profile(virSecurityDriverPtr drv,
   clean:
     VIR_FREE(xml);
 
-    if (pipefd[0] > 0)
-        close(pipefd[0]);
-    if (pipefd[1] > 0)
-        close(pipefd[1]);
+    VIR_FORCE_CLOSE(pipefd[0]);
+    VIR_FORCE_CLOSE(pipefd[1]);
 
     return rc;
 }
index edeff100597a01697746df8f9ffb7000a3d7a0ea..7dd9b14ea57364b18e65a833b7ff8a25017c60b9 100644 (file)
@@ -28,6 +28,7 @@
 #include "pci.h"
 #include "hostusb.h"
 #include "storage_file.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_SECURITY
 
@@ -120,10 +121,10 @@ SELinuxInitialize(void)
         virReportSystemError(errno,
                              _("cannot read SELinux virtual domain context file %s"),
                              selinux_virtual_domain_context_path());
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     ptr = strchrnul(default_domain_context, '\n');
     *ptr = '\0';
@@ -139,10 +140,10 @@ SELinuxInitialize(void)
         virReportSystemError(errno,
                              _("cannot read SELinux virtual image context file %s"),
                              selinux_virtual_image_context_path());
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     ptr = strchrnul(default_image_context, '\n');
     if (*ptr == '\n') {
index eba34b0a0b0399ecb283df095b7ac28aae2235a5..0b56178a9b2fab636036765c019beaf72257dddd 100644 (file)
@@ -37,6 +37,7 @@
 #include "uuid.h"
 #include "hostusb.h"
 #include "pci.h"
+#include "files.h"
 
 static char *progname;
 
@@ -278,12 +279,12 @@ update_include_file(const char *include_file, const char *included_files,
     }
 
     if (safewrite(fd, pcontent, plen) < 0) { /* don't write the '\0' */
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         vah_error(NULL, 0, "failed to write to profile");
         goto clean;
     }
 
-    if (close(fd) != 0) {
+    if (VIR_CLOSE(fd) != 0) {
         vah_error(NULL, 0, "failed to close or write to profile");
         goto clean;
     }
@@ -385,12 +386,12 @@ create_profile(const char *profile, const char *profile_name,
     }
 
     if (safewrite(fd, pcontent, plen - 1) < 0) { /* don't write the '\0' */
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         vah_error(NULL, 0, "failed to write to profile");
         goto clean_all;
     }
 
-    if (close(fd) != 0) {
+    if (VIR_CLOSE(fd) != 0) {
         vah_error(NULL, 0, "failed to close or write to profile");
         goto clean_all;
     }
index 580d859dc40ecbc4a58b9666326ded5843030b82..5eba786fe97fc4e81f235a1d38a4f15cd22e7e77 100644 (file)
@@ -51,6 +51,7 @@
 #include "storage_file.h"
 #include "storage_backend.h"
 #include "logging.h"
+#include "files.h"
 
 #if WITH_STORAGE_LVM
 # include "storage_backend_logical.h"
@@ -181,7 +182,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
         } while ((amtleft -= 512) > 0);
     }
 
-    if (inputfd != -1 && close(inputfd) < 0) {
+    if (VIR_CLOSE(inputfd) < 0) {
         ret = -errno;
         virReportSystemError(errno,
                              _("cannot close file '%s'"),
@@ -193,8 +194,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
     *total -= remain;
 
 cleanup:
-    if (inputfd != -1)
-        close(inputfd);
+    VIR_FORCE_CLOSE(inputfd);
 
     VIR_FREE(buf);
 
@@ -251,7 +251,7 @@ virStorageBackendCreateBlockFrom(virConnectPtr conn ATTRIBUTE_UNUSED,
                              vol->target.path, vol->target.perms.mode);
         goto cleanup;
     }
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno,
                              _("cannot close file '%s'"),
                              vol->target.path);
@@ -261,8 +261,7 @@ virStorageBackendCreateBlockFrom(virConnectPtr conn ATTRIBUTE_UNUSED,
 
     ret = 0;
 cleanup:
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return ret;
 }
@@ -608,7 +607,7 @@ static int virStorageBackendQEMUImgBackingFormat(const char *qemuimg)
 
 cleanup:
     VIR_FREE(help);
-    close(newstdout);
+    VIR_FORCE_CLOSE(newstdout);
 rewait:
     if (child) {
         if (waitpid(child, &status, 0) != child) {
@@ -997,7 +996,7 @@ virStorageBackendVolOpenCheckMode(const char *path, unsigned int flags)
         virReportSystemError(errno,
                              _("cannot stat file '%s'"),
                              path);
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
@@ -1009,7 +1008,7 @@ virStorageBackendVolOpenCheckMode(const char *path, unsigned int flags)
         mode = VIR_STORAGE_VOL_OPEN_BLOCK;
 
     if (!(mode & flags)) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
 
         if (mode & VIR_STORAGE_VOL_OPEN_ERROR) {
             virStorageReportError(VIR_ERR_INTERNAL_ERROR,
@@ -1045,7 +1044,7 @@ virStorageBackendUpdateVolTargetInfo(virStorageVolTargetPtr target,
                                                  allocation,
                                                  capacity);
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return ret;
 }
@@ -1461,10 +1460,8 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
 
     if (list)
         fclose(list);
-    else {
-        if (fd >= 0)
-            close(fd);
-    }
+    else
+        VIR_FORCE_CLOSE(fd);
 
     while ((err = waitpid(child, &exitstatus, 0) == -1) && errno == EINTR);
 
@@ -1579,7 +1576,7 @@ virStorageBackendRunProgNul(virStoragePoolObjPtr pool,
     if (fp)
         fclose (fp);
     else
-        close (fd);
+        VIR_FORCE_CLOSE(fd);
 
     while ((w_err = waitpid (child, &exitstatus, 0) == -1) && errno == EINTR)
         /* empty */ ;
index b6b8fdd2bb4d07a860b1170ba6825adbc70292a6..c2bc6c463de12844d8ce6a43a03f8730f6601242 100644 (file)
@@ -45,6 +45,7 @@
 #include "util.h"
 #include "memory.h"
 #include "xml.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -72,25 +73,25 @@ virStorageBackendProbeTarget(virStorageVolTargetPtr target,
     if ((ret = virStorageBackendUpdateVolTargetInfoFD(target, fd,
                                                       allocation,
                                                       capacity)) < 0) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return ret;
     }
 
     memset(&meta, 0, sizeof(meta));
 
     if ((target->format = virStorageFileProbeFormatFromFD(target->path, fd)) < 0) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
     if (virStorageFileGetMetadataFromFD(target->path, fd,
                                         target->format,
                                         &meta) < 0) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return -1;
     }
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     if (meta.backingStore) {
         *backingStore = meta.backingStore;
@@ -98,7 +99,7 @@ virStorageBackendProbeTarget(virStorageVolTargetPtr target,
         if (meta.backingStoreFormat == VIR_STORAGE_FILE_AUTO) {
             if ((*backingStoreFormat
                  = virStorageFileProbeFormat(*backingStore)) < 0) {
-                close(fd);
+                VIR_FORCE_CLOSE(fd);
                 goto cleanup;
             }
         } else {
index 46dc1be9199dc11907b2f6f1da01e1c216692b85..06a89eca8cae431d8636d0d3bed9318ab5a573c8 100644 (file)
@@ -41,6 +41,7 @@
 #include "util.h"
 #include "memory.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -237,9 +238,7 @@ out:
     if (fp != NULL) {
         fclose(fp);
     } else {
-        if (fd != -1) {
-            close(fd);
-        }
+        VIR_FORCE_CLOSE(fd);
     }
 
     return ret;
index 3d7dfcc072ef805f6a9f588c29e91c4c7603f025..79ad4b8e1c9cfee2de48bbe2be416e213c753790 100644 (file)
@@ -35,6 +35,7 @@
 #include "storage_backend.h"
 #include "memory.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -61,9 +62,7 @@ virStorageBackendMpathUpdateVolTargetInfo(virStorageVolTargetPtr target,
 
     ret = 0;
 out:
-    if (fd != -1) {
-        close(fd);
-    }
+    VIR_FORCE_CLOSE(fd);
     return ret;
 }
 
index 71492cf68fe8ce9d11fc5372a4fab9c4e5e60665..28d6ac648f1612336eb485cebb6f1afe64023006 100644 (file)
@@ -32,6 +32,7 @@
 #include "storage_backend_scsi.h"
 #include "memory.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -154,8 +155,7 @@ virStorageBackendSCSIUpdateVolTargetInfo(virStorageVolTargetPtr target,
     ret = 0;
 
 cleanup:
-    if (fd >= 0)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return ret;
 }
@@ -572,14 +572,14 @@ virStorageBackendSCSITriggerRescan(uint32_t host)
     if (safewrite(fd,
                   LINUX_SYSFS_SCSI_HOST_SCAN_STRING,
                   sizeof(LINUX_SYSFS_SCSI_HOST_SCAN_STRING)) < 0) {
-
+        VIR_FORCE_CLOSE(fd);
         virReportSystemError(errno,
                              _("Write to '%s' to trigger host scan failed"),
                              path);
         retval = -1;
     }
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 free_path:
     VIR_FREE(path);
 out:
index 4ebbced6dabd75457fc1a6ee49cefd27c6f8f806..f6672d922995a83edd33bd3edce0aa217e50d1b1 100644 (file)
@@ -45,6 +45,7 @@
 #include "memory.h"
 #include "storage_backend.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -1664,9 +1665,7 @@ storageVolumeWipeInternal(virStorageVolDefPtr def)
 out:
     VIR_FREE(writebuf);
 
-    if (fd != -1) {
-        close(fd);
-    }
+    VIR_FORCE_CLOSE(fd);
 
     return ret;
 }
index a9d3d891daab1a74fc749001e0442e2ca9122309..e74e8d117c860877d6c2d0c03f8993b5f9ace1fc 100644 (file)
@@ -50,6 +50,7 @@
 #include "xml.h"
 #include "threads.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_TEST
 
@@ -788,8 +789,7 @@ static int testOpenFromFile(virConnectPtr conn,
                   _("Invalid XML in file '%s'"), file);
         goto error;
     }
-    close(fd);
-    fd = -1;
+    VIR_FORCE_CLOSE(fd);
 
     root = xmlDocGetRootElement(xml);
     if ((root == NULL) || (!xmlStrEqual(root->name, BAD_CAST "node"))) {
@@ -1101,8 +1101,7 @@ static int testOpenFromFile(virConnectPtr conn,
     VIR_FREE(networks);
     VIR_FREE(ifaces);
     VIR_FREE(pools);
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     virDomainObjListDeinit(&privconn->domains);
     virNetworkObjListFree(&privconn->networks);
     virInterfaceObjListFree(&privconn->ifaces);
@@ -1752,7 +1751,7 @@ static int testDomainSave(virDomainPtr domain,
         goto cleanup;
     }
 
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno,
                              _("saving domain '%s' to '%s': write failed"),
                              domain->name, path);
@@ -1779,8 +1778,7 @@ cleanup:
      * in either case we're already in a failure scenario
      * and have reported a earlier error */
     if (ret != 0) {
-        if (fd != -1)
-            close(fd);
+        VIR_FORCE_CLOSE(fd);
         unlink(path);
     }
     if (privdom)
@@ -1870,8 +1868,7 @@ static int testDomainRestore(virConnectPtr conn,
 cleanup:
     virDomainDefFree(def);
     VIR_FREE(xml);
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (dom)
         virDomainObjUnlock(dom);
     if (event)
@@ -1911,7 +1908,7 @@ static int testDomainCoreDump(virDomainPtr domain,
                              domain->name, to);
         goto cleanup;
     }
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno,
                              _("domain '%s' coredump: write failed: %s"),
                              domain->name, to);
@@ -1932,8 +1929,7 @@ static int testDomainCoreDump(virDomainPtr domain,
 
     ret = 0;
 cleanup:
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (privdom)
         virDomainObjUnlock(privdom);
     if (event)
index 33b2b04a95c4b392d9ad006148dc46064a10a21d..0921c81014dac118a4412eed1b62ab361b391265 100644 (file)
@@ -47,6 +47,7 @@
 #include "bridge.h"
 #include "logging.h"
 #include "domain_nwfilter.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_UML
 
@@ -367,7 +368,7 @@ umlBuildCommandLineChr(virDomainChrDefPtr def,
             }
             if (virAsprintf(&ret, "%s%d=null,fd:%d", dev, def->target.port, fd_out) < 0) {
                 virReportOOMError();
-                close(fd_out);
+                VIR_FORCE_CLOSE(fd_out);
                 return NULL;
             }
             FD_SET(fd_out, keepfd);
index 5161012739016fba0c19d930380ddce0fff2664d..348f2994a2a11e161a94f63d10ff50719a0d1759 100644 (file)
@@ -59,6 +59,7 @@
 #include "datatypes.h"
 #include "logging.h"
 #include "domain_nwfilter.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_UML
 
@@ -533,7 +534,7 @@ umlShutdown(void) {
     umlDriverLock(uml_driver);
     if (uml_driver->inotifyWatch != -1)
         virEventRemoveHandle(uml_driver->inotifyWatch);
-    close(uml_driver->inotifyFD);
+    VIR_FORCE_CLOSE(uml_driver->inotifyFD);
     virCapabilitiesFree(uml_driver->caps);
 
     /* shutdown active VMs
@@ -659,8 +660,7 @@ restat:
     if (bind(priv->monitor, (struct sockaddr *)&addr, sizeof addr) < 0) {
         virReportSystemError(errno,
                              "%s", _("cannot bind socket"));
-        close(priv->monitor);
-        priv->monitor = -1;
+        VIR_FORCE_CLOSE(priv->monitor);
         return -1;
     }
 
@@ -870,13 +870,13 @@ static int umlStartVMDaemon(virConnectPtr conn,
     if (umlSetCloseExec(logfd) < 0) {
         virReportSystemError(errno,
                              "%s", _("Unable to set VM logfile close-on-exec flag"));
-        close(logfd);
+        VIR_FORCE_CLOSE(logfd);
         return -1;
     }
 
     if (umlBuildCommandLine(conn, driver, vm, &keepfd,
                             &argv, &progenv) < 0) {
-        close(logfd);
+        VIR_FORCE_CLOSE(logfd);
         virDomainConfVMNWFilterTeardown(vm);
         umlCleanupTapDevices(conn, vm);
         return -1;
@@ -912,15 +912,17 @@ static int umlStartVMDaemon(virConnectPtr conn,
                            -1, &logfd, &logfd,
                            VIR_EXEC_CLEAR_CAPS,
                            NULL, NULL, NULL);
-    close(logfd);
+    VIR_FORCE_CLOSE(logfd);
 
     /*
      * At the moment, the only thing that populates keepfd is
      * umlBuildCommandLineChr. We want to close every fd it opens.
      */
     for (i = 0; i < FD_SETSIZE; i++)
-        if (FD_ISSET(i, &keepfd))
-            close(i);
+        if (FD_ISSET(i, &keepfd)) {
+            int tmpfd = i;
+            VIR_FORCE_CLOSE(tmpfd);
+        }
 
     for (i = 0 ; argv[i] ; i++)
         VIR_FREE(argv[i]);
@@ -957,9 +959,7 @@ static void umlShutdownVMDaemon(virConnectPtr conn ATTRIBUTE_UNUSED,
 
     virKillProcess(vm->pid, SIGTERM);
 
-    if (priv->monitor != -1)
-        close(priv->monitor);
-    priv->monitor = -1;
+    VIR_FORCE_CLOSE(priv->monitor);
 
     if ((ret = waitpid(vm->pid, NULL, 0)) != vm->pid) {
         VIR_WARN("Got unexpected pid %d != %d",
@@ -2088,7 +2088,7 @@ umlDomainBlockPeek (virDomainPtr dom,
     }
 
 cleanup:
-    if (fd >= 0) close (fd);
+    VIR_FORCE_CLOSE(fd);
     if (vm)
         virDomainObjUnlock(vm);
     return ret;
index 6a94ec0ec4d6a508e8c12dcb7232339533505e8a..97cf73fb8e0feca6aa7dca8f909404f8ccda6e3f 100644 (file)
@@ -24,6 +24,7 @@
 #if defined(WITH_BRIDGE)
 
 # include "bridge.h"
+# include "files.h"
 
 # include <stdlib.h>
 # include <stdio.h>
@@ -82,12 +83,12 @@ brInit(brControl **ctlp)
     if ((flags = fcntl(fd, F_GETFD)) < 0 ||
         fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
         int err = errno;
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return err;
     }
 
     if (VIR_ALLOC(*ctlp) < 0) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return ENOMEM;
     }
 
@@ -108,8 +109,7 @@ brShutdown(brControl *ctl)
     if (!ctl)
         return;
 
-    close(ctl->fd);
-    ctl->fd = 0;
+    VIR_FORCE_CLOSE(ctl->fd);
 
     VIR_FREE(ctl);
 }
@@ -540,11 +540,11 @@ brAddTap(brControl *ctl,
     if (tapfd)
         *tapfd = fd;
     else
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
     return 0;
 
  error:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return errno;
 }
@@ -575,7 +575,7 @@ int brDeleteTap(brControl *ctl,
     }
 
  error:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return errno;
 }
index 0aef2c2ca8c9fa0c2860c3a46cc608660b54995a..ba1a384a6736d44b625d6438db72a5383b992c05 100644 (file)
@@ -24,6 +24,7 @@
 #include "util.h"
 #include "c-ctype.h"
 #include "memory.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_CONF
 
@@ -954,7 +955,7 @@ virConfWriteFile(const char *filename, virConfPtr conf)
     content = virBufferContentAndReset(&buf);
     ret = safewrite(fd, content, use);
     VIR_FREE(content);
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (ret != (int)use) {
         virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"));
         return -1;
index 482480dbd194fd45b20771c52ce83db0d88638cf..6e4e2b9b1d7ee2f5a19459062bfe6841aec70221 100644 (file)
@@ -36,6 +36,7 @@
 #include "conf/domain_conf.h"
 #include "logging.h"
 #include "memory.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_HOOK
 
@@ -368,11 +369,10 @@ virHookCall(int driver, const char *id, int op, int sub_op, const char *extra,
         }
         ret = virExec(argv, env, NULL, &pid, pipefd[0], &outfd, &errfd,
                       VIR_EXEC_NONE | VIR_EXEC_NONBLOCK);
-        if (close(pipefd[1]) < 0) {
+        if (VIR_CLOSE(pipefd[1]) < 0) {
             virReportSystemError(errno, "%s",
                              _("unable to close pipe for hook input"));
         }
-        pipefd[1] = -1;
     } else {
         ret = virExec(argv, env, NULL, &pid, -1, &outfd, &errfd,
                       VIR_EXEC_NONE | VIR_EXEC_NONBLOCK);
@@ -418,17 +418,15 @@ virHookCall(int driver, const char *id, int op, int sub_op, const char *extra,
     }
 
 cleanup:
-    if (pipefd[0] >= 0) {
-        if (close(pipefd[0]) < 0) {
-            virReportSystemError(errno, "%s",
-                             _("unable to close pipe for hook input"));
-        }
+    if (VIR_CLOSE(pipefd[0]) < 0) {
+        virReportSystemError(errno, "%s",
+                         _("unable to close pipe for hook input"));
+        ret = 1;
     }
-    if (pipefd[1] >= 0) {
-        if (close(pipefd[1]) < 0) {
-            virReportSystemError(errno, "%s",
-                             _("unable to close pipe for hook input"));
-        }
+    if (VIR_CLOSE(pipefd[1]) < 0) {
+        virReportSystemError(errno, "%s",
+                         _("unable to close pipe for hook input"));
+        ret = 1;
     }
     if (argv) {
         for (i = 0 ; i < argc ; i++)
index eec437a3a6b5e03f21c29fe72a487782e9a4d3ff..fe58823be17fb9449cbc15b5ead59be3e5a2eb30 100644 (file)
@@ -39,6 +39,7 @@
 #include "util.h"
 #include "interface.h"
 #include "virterror_internal.h"
+#include "files.h"
 
 #define ifaceError(code, ...) \
         virReportErrorHelper(NULL, VIR_FROM_NET, code, __FILE__, \
@@ -82,7 +83,7 @@ ifaceGetFlags(const char *ifname, short *flags) {
 
     *flags = ifr.ifr_flags;
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return rc;
 }
@@ -161,7 +162,7 @@ static int chgIfaceFlags(const char *ifname, short flagclear, short flagset) {
     }
 
 err_exit:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     return rc;
 }
 
@@ -259,8 +260,7 @@ ifaceCheck(bool reportError, const char *ifname,
     }
 
  err_exit:
-    if (fd >= 0)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return rc;
 }
@@ -326,7 +326,7 @@ ifaceGetIndex(bool reportError, const char *ifname, int *ifindex)
     }
 
 err_exit:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return rc;
 }
@@ -373,7 +373,7 @@ ifaceGetVlanID(const char *vlanifname, int *vlanid) {
     *vlanid = vlanargs.u.VID;
 
  err_exit:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return rc;
 }
index 248c47e41eae6419e9c36b1eb42259665b19c984..ac38d4d98d802ad97956181aed69d80b2797466b 100644 (file)
@@ -40,6 +40,7 @@
 #include "util.h"
 #include "buf.h"
 #include "threads.h"
+#include "files.h"
 
 /*
  * Macro used to format the message as a string in virLogMessage
@@ -603,8 +604,7 @@ static int virLogOutputToFd(const char *category ATTRIBUTE_UNUSED,
 static void virLogCloseFd(void *data) {
     int fd = (long) data;
 
-    if (fd >= 0)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
 }
 
 static int virLogAddOutputToStderr(int priority) {
@@ -622,7 +622,7 @@ static int virLogAddOutputToFile(int priority, const char *file) {
         return(-1);
     if (virLogDefineOutput(virLogOutputToFd, virLogCloseFd, (void *)(long)fd,
                            priority, VIR_LOG_TO_FILE, file, 0) < 0) {
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         return(-1);
     }
     return(0);
index 79a540524ad84e741ce134162a78fa658538a3df..88c38c86f37755096e19510e9fef4df8d0ab81f8 100644 (file)
@@ -52,6 +52,7 @@
 # include "conf/domain_conf.h"
 # include "virterror_internal.h"
 # include "uuid.h"
+# include "files.h"
 
 # define VIR_FROM_THIS VIR_FROM_NET
 
@@ -92,12 +93,6 @@ static int nlOpen(void)
 }
 
 
-static void nlClose(int fd)
-{
-    close(fd);
-}
-
-
 /**
  * nlComm:
  * @nlmsg: pointer to netlink message
@@ -191,7 +186,7 @@ err_exit:
         *respbuflen = 0;
     }
 
-    nlClose(fd);
+    VIR_FORCE_CLOSE(fd);
     return rc;
 }
 
@@ -689,8 +684,7 @@ create_name:
 
     if (rc >= 0) {
         if (configMacvtapTap(rc, vnet_hdr) < 0) {
-            close(rc);
-            rc = -1;
+            VIR_FORCE_CLOSE(rc); /* sets rc to -1 */
             goto disassociate_exit;
         }
         *res_ifname = strdup(cr_ifname);
@@ -778,8 +772,7 @@ getLldpadPid(void) {
                              _("Error opening file %s"), LLDPAD_PID_FILE);
     }
 
-    if (fd >= 0)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return pid;
 }
index 22c66fa42d952734a96dddd1f8cee7958d77bcf6..bd8c6c5442309a26ba523e0506e2b3a25fd539c6 100644 (file)
@@ -37,6 +37,7 @@
 #include "memory.h"
 #include "util.h"
 #include "virterror_internal.h"
+#include "files.h"
 
 /* avoid compilation breakage on some systems */
 #ifndef MODPROBE
@@ -188,10 +189,7 @@ pciCloseConfig(pciDevice *dev)
     if (!dev)
         return;
 
-    if (dev->fd >= 0) {
-        close(dev->fd);
-        dev->fd = -1;
-    }
+    VIR_FORCE_CLOSE(dev->fd);
 }
 
 static int
index e4f1eaa071de404e2f09e798163cca189c64c0d8..403b0c089276c94be0a762d0a8afa871b507edf6 100644 (file)
@@ -36,6 +36,7 @@
 #include "memory.h"
 #include "virterror_internal.h"
 #include "logging.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -688,7 +689,7 @@ virStorageFileProbeFormat(const char *path)
 
     ret = virStorageFileProbeFormatFromFD(path, fd);
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return ret;
 }
@@ -782,7 +783,7 @@ virStorageFileGetMetadata(const char *path,
 
     ret = virStorageFileGetMetadataFromFD(path, fd, format, meta);
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return ret;
 }
index 4827292bc536a7485b98425fb7d7caa7a53af514..2f90e8db18470614cc1197171654f74d037bdbb4 100644 (file)
@@ -72,6 +72,7 @@
 #include "memory.h"
 #include "threads.h"
 #include "verify.h"
+#include "files.h"
 
 #ifndef NSIG
 # define NSIG 32
@@ -460,6 +461,7 @@ __virExec(const char *const*argv,
     int pipeerr[2] = {-1,-1};
     int childout = -1;
     int childerr = -1;
+    int tmpfd;
 
     if ((null = open("/dev/null", O_RDONLY)) < 0) {
         virReportSystemError(errno,
@@ -533,13 +535,13 @@ __virExec(const char *const*argv,
     }
 
     if (pid) { /* parent */
-        close(null);
+        VIR_FORCE_CLOSE(null);
         if (outfd && *outfd == -1) {
-            close(pipeout[1]);
+            VIR_FORCE_CLOSE(pipeout[1]);
             *outfd = pipeout[0];
         }
         if (errfd && *errfd == -1) {
-            close(pipeerr[1]);
+            VIR_FORCE_CLOSE(pipeerr[1]);
             *errfd = pipeerr[0];
         }
 
@@ -567,8 +569,10 @@ __virExec(const char *const*argv,
             i != childout &&
             i != childerr &&
             (!keepfd ||
-             !FD_ISSET(i, keepfd)))
-            close(i);
+             !FD_ISSET(i, keepfd))) {
+            tmpfd = i;
+            VIR_FORCE_CLOSE(tmpfd);
+        }
 
     if (dup2(infd >= 0 ? infd : null, STDIN_FILENO) < 0) {
         virReportSystemError(errno,
@@ -588,14 +592,15 @@ __virExec(const char *const*argv,
         goto fork_error;
     }
 
-    if (infd > 0)
-        close(infd);
-    close(null);
-    if (childout > 0)
-        close(childout);
+    VIR_FORCE_CLOSE(infd);
+    VIR_FORCE_CLOSE(null);
+    tmpfd = childout;   /* preserve childout value */
+    VIR_FORCE_CLOSE(tmpfd);
     if (childerr > 0 &&
-        childerr != childout)
-        close(childerr);
+        childerr != childout) {
+        VIR_FORCE_CLOSE(childerr);
+        childout = -1;
+    }
 
     /* Daemonize as late as possible, so the parent process can detect
      * the above errors with wait* */
@@ -665,16 +670,11 @@ __virExec(const char *const*argv,
     /* NB we don't virUtilError() on any failures here
        because the code which jumped hre already raised
        an error condition which we must not overwrite */
-    if (pipeerr[0] > 0)
-        close(pipeerr[0]);
-    if (pipeerr[1] > 0)
-        close(pipeerr[1]);
-    if (pipeout[0] > 0)
-        close(pipeout[0]);
-    if (pipeout[1] > 0)
-        close(pipeout[1]);
-    if (null > 0)
-        close(null);
+    VIR_FORCE_CLOSE(pipeerr[0]);
+    VIR_FORCE_CLOSE(pipeerr[1]);
+    VIR_FORCE_CLOSE(pipeout[0]);
+    VIR_FORCE_CLOSE(pipeout[1]);
+    VIR_FORCE_CLOSE(null);
     return -1;
 }
 
@@ -864,10 +864,8 @@ virRunWithHook(const char *const*argv,
     VIR_FREE(outbuf);
     VIR_FREE(errbuf);
     VIR_FREE(argv_str);
-    if (outfd != -1)
-        close(outfd);
-    if (errfd != -1)
-        close(errfd);
+    VIR_FORCE_CLOSE(outfd);
+    VIR_FORCE_CLOSE(errfd);
     return ret;
 }
 
@@ -1110,7 +1108,7 @@ int virFileReadAll(const char *path, int maxlen, char **buf)
     }
 
     int len = virFileReadLimFD(fd, maxlen, buf);
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     if (len < 0) {
         virReportSystemError(errno, _("Failed to read file '%s'"), path);
         return -1;
@@ -1131,13 +1129,13 @@ int virFileWriteStr(const char *path, const char *str)
 
     if (safewrite(fd, str, strlen(str)) < 0) {
         int saved_errno = errno;
-        close (fd);
+        VIR_FORCE_CLOSE(fd);
         errno = saved_errno;
         return -1;
     }
 
     /* Use errno from failed close only if there was no write error.  */
-    if (close (fd) != 0)
+    if (VIR_CLOSE(fd) != 0)
         return -1;
 
     return 0;
@@ -1316,7 +1314,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode,
     if ((hook) && ((ret = hook(fd, hookdata)) != 0)) {
         goto error;
     }
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         ret = -errno;
         virReportSystemError(errno, _("failed to close new file '%s'"),
                              path);
@@ -1325,8 +1323,7 @@ static int virFileOperationNoFork(const char *path, int openflags, mode_t mode,
     }
     fd = -1;
 error:
-    if (fd != -1)
-       close(fd);
+    VIR_FORCE_CLOSE(fd);
     return ret;
 }
 
@@ -1477,7 +1474,7 @@ parenterror:
     if ((hook) && ((ret = hook(fd, hookdata)) != 0)) {
         goto childerror;
     }
-    if (close(fd) < 0) {
+    if (VIR_CLOSE(fd) < 0) {
         ret = -errno;
         virReportSystemError(errno, _("child failed to close new file '%s'"),
                              path);
@@ -1754,10 +1751,8 @@ int virFileOpenTtyAt(const char *ptmx,
     rc = 0;
 
 cleanup:
-    if (rc != 0 &&
-        *ttymaster != -1) {
-        close(*ttymaster);
-    }
+    if (rc != 0)
+        VIR_FORCE_CLOSE(*ttymaster);
 
     return rc;
 
@@ -1823,7 +1818,7 @@ int virFileWritePidPath(const char *pidfile,
 
     if (!(file = fdopen(fd, "w"))) {
         rc = errno;
-        close(fd);
+        VIR_FORCE_CLOSE(fd);
         goto cleanup;
     }
 
index 969ac6d275a7706a910c5c61222a93c9209c63d7..805c20f3abd66cb8faafa239bed359518b16ca9f 100644 (file)
@@ -39,6 +39,7 @@
 #include "virterror_internal.h"
 #include "logging.h"
 #include "memory.h"
+#include "files.h"
 
 #ifndef ENODATA
 # define ENODATA EIO
@@ -61,7 +62,7 @@ virUUIDGenerateRandomBytes(unsigned char *buf,
         if ((n = read(fd, buf, buflen)) <= 0) {
             if (errno == EINTR)
                 continue;
-            close(fd);
+            VIR_FORCE_CLOSE(fd);
             return n < 0 ? errno : ENODATA;
         }
 
@@ -69,7 +70,7 @@ virUUIDGenerateRandomBytes(unsigned char *buf,
         buflen -= n;
     }
 
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
 
     return 0;
 }
@@ -240,10 +241,10 @@ getDMISystemUUID(char *uuid, int len)
         int fd = open(paths[i], O_RDONLY);
         if (fd > 0) {
             if (saferead(fd, uuid, len) == len) {
-                close(fd);
+                VIR_FORCE_CLOSE(fd);
                 return 0;
             }
-            close(fd);
+            VIR_FORCE_CLOSE(fd);
         }
         i++;
     }
index 6af7af0abf1913b6b31ccf80c0406d7431dcb06f..b630fcec1a13f6324266a4b92f8de482cf237c8f 100644 (file)
@@ -31,6 +31,7 @@
 #include "logging.h"
 #include "virtaudit.h"
 #include "util.h"
+#include "files.h"
 
 /* Provide the macros in case the header file is old.
    FIXME: should be removed. */
@@ -134,7 +135,7 @@ void virAuditSend(const char *file ATTRIBUTE_UNUSED, const char *func,
 void virAuditClose(void)
 {
 #if HAVE_AUDIT
-    close(auditfd);
+    VIR_FORCE_CLOSE(auditfd);
 #endif
 }
 
index c5cc880d818c532c321617c4c3f6d9eab9fed801..c35b6d844fe514c311f01bd0d2f2462569680de3 100644 (file)
@@ -65,6 +65,7 @@
 #include "buf.h"
 #include "capabilities.h"
 #include "memory.h"
+#include "files.h"
 
 #define VIR_FROM_THIS VIR_FROM_XEN
 
@@ -2019,7 +2020,7 @@ xenHypervisorInit(void)
     hypervisor_version = -1;
     virXenError(VIR_ERR_XEN_CALL, " ioctl %lu",
                 (unsigned long) IOCTL_PRIVCMD_HYPERCALL);
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     in_init = 0;
     return(-1);
 
@@ -2105,13 +2106,13 @@ xenHypervisorInit(void)
     hypervisor_version = -1;
     virXenError(VIR_ERR_XEN_CALL, " ioctl %lu",
                 (unsigned long)IOCTL_PRIVCMD_HYPERCALL);
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     in_init = 0;
     VIR_FREE(ipt);
     return(-1);
 
  done:
-    close(fd);
+    VIR_FORCE_CLOSE(fd);
     in_init = 0;
     VIR_FREE(ipt);
     return(0);
@@ -2174,7 +2175,7 @@ xenHypervisorClose(virConnectPtr conn)
     if (priv->handle < 0)
         return -1;
 
-    ret = close(priv->handle);
+    ret = VIR_CLOSE(priv->handle);
     if (ret < 0)
         return (-1);
 
@@ -2379,8 +2380,7 @@ get_cpu_flags(virConnectPtr conn, const char **hvm, int *pae, int *longmode)
     ret = 1;
 
 out:
-    if (fd != -1)
-        close(fd);
+    VIR_FORCE_CLOSE(fd);
     return ret;
 }
 
index 950706162d8e7fa5fc154da591c63e3129589a86..106776974b3c3259a03c7468bceb73a8417a85cb 100644 (file)
@@ -39,6 +39,7 @@
 #include "xend_internal.h"
 #include "logging.h"
 #include "uuid.h"
+#include "files.h"
 
 #include "xm_internal.h" /* for xenXMDomainConfigParse */
 
@@ -483,7 +484,7 @@ xenInotifyClose(virConnectPtr conn)
 
     if (priv->inotifyWatch != -1)
         virEventRemoveHandle(priv->inotifyWatch);
-    close(priv->inotifyFD);
+    VIR_FORCE_CLOSE(priv->inotifyFD);
 
     return 0;
 }
index 81967d438fc38abbadace499da2a2b9c1a26e437..d6d66bdb5c264ed3b490e4b3ad25b6ca8374b570 100644 (file)
@@ -45,6 +45,7 @@
 #include "xs_internal.h" /* To extract VNC port & Serial console TTY */
 #include "memory.h"
 #include "count-one-bits.h"
+#include "files.h"
 
 /* required for cpumap_t */
 #include <xen/dom0_ops.h>
@@ -112,7 +113,6 @@ static int
 do_connect(virConnectPtr xend)
 {
     int s;
-    int serrno;
     int no_slow_start = 1;
     xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) xend->privateData;
 
@@ -131,10 +131,7 @@ do_connect(virConnectPtr xend)
 
 
     if (connect(s, (struct sockaddr *)&priv->addr, priv->addrlen) == -1) {
-        serrno = errno;
-        close(s);
-        errno = serrno;
-        s = -1;
+        VIR_FORCE_CLOSE(s); /* preserves errno */
 
         /*
          * Connecting to XenD when privileged is mandatory, so log this
@@ -381,7 +378,7 @@ xend_get(virConnectPtr xend, const char *path,
             "Content-Type: application/x-www-form-urlencoded\r\n" "\r\n");
 
     ret = xend_req(s, content);
-    close(s);
+    VIR_FORCE_CLOSE(s);
 
     if (((ret < 0) || (ret >= 300)) &&
         ((ret != 404) || (!STRPREFIX(path, "/xend/domain/")))) {
@@ -430,7 +427,7 @@ xend_post(virConnectPtr xend, const char *path, const char *ops)
     swrites(s, ops);
 
     ret = xend_req(s, &err_buf);
-    close(s);
+    VIR_FORCE_CLOSE(s);
 
     if ((ret < 0) || (ret >= 300)) {
         virXendError(VIR_ERR_POST_FAILED,
@@ -821,7 +818,7 @@ xenDaemonOpen_tcp(virConnectPtr conn, const char *host, const char *port)
 
         if (connect (sock, r->ai_addr, r->ai_addrlen) == -1) {
             saved_errno = errno;
-            close (sock);
+            VIR_FORCE_CLOSE(sock);
             continue;
         }
 
@@ -831,7 +828,7 @@ xenDaemonOpen_tcp(virConnectPtr conn, const char *host, const char *port)
         memcpy(&priv->addr,
                r->ai_addr,
                r->ai_addrlen);
-        close(sock);
+        VIR_FORCE_CLOSE(sock);
         break;
     }
 
@@ -5125,7 +5122,7 @@ xenDaemonDomainBlockPeek (virDomainPtr domain, const char *path,
 
     ret = 0;
  cleanup:
-    if (fd >= 0) close (fd);
+    VIR_FORCE_CLOSE(fd);
     sexpr_free(root);
     virDomainDefFree(def);
     return ret;
index 6e693c558cff8d76043afff2ddb503aad63b220a..206937207394642610d3f60f115e235f032ad079 100644 (file)
@@ -47,6 +47,8 @@
     ((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 +        \
       ((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
 
+#include "files.h"
+
 static unsigned int testDebug = -1;
 static unsigned int testVerbose = -1;
 
@@ -222,8 +224,10 @@ void virtTestCaptureProgramExecChild(const char *const argv[],
     open_max = sysconf (_SC_OPEN_MAX);
     for (i = 0; i < open_max; i++) {
         if (i != stdinfd &&
-            i != pipefd)
-            close(i);
+            i != pipefd) {
+            int tmpfd = i;
+            VIR_FORCE_CLOSE(tmpfd);
+        }
     }
 
     if (dup2(stdinfd, STDIN_FILENO) != STDIN_FILENO)
@@ -237,8 +241,7 @@ void virtTestCaptureProgramExecChild(const char *const argv[],
     execve(argv[0], (char *const*)argv, (char *const*)env);
 
  cleanup:
-    if (stdinfd != -1)
-        close(stdinfd);
+    VIR_FORCE_CLOSE(stdinfd);
 }
 
 int virtTestCaptureProgramOutput(const char *const argv[],
@@ -252,10 +255,10 @@ int virtTestCaptureProgramOutput(const char *const argv[],
     int pid = fork();
     switch (pid) {
     case 0:
-        close(pipefd[0]);
+        VIR_FORCE_CLOSE(pipefd[0]);
         virtTestCaptureProgramExecChild(argv, pipefd[1]);
 
-        close(pipefd[1]);
+        VIR_FORCE_CLOSE(pipefd[1]);
         _exit(1);
 
     case -1:
@@ -267,7 +270,7 @@ int virtTestCaptureProgramOutput(const char *const argv[],
             int ret = -1;
             int want = buflen-1;
 
-            close(pipefd[1]);
+            VIR_FORCE_CLOSE(pipefd[1]);
 
             while (want) {
                 if ((ret = read(pipefd[0], (*buf)+got, want)) <= 0)
@@ -275,7 +278,7 @@ int virtTestCaptureProgramOutput(const char *const argv[],
                 got += ret;
                 want -= ret;
             }
-            close(pipefd[0]);
+            VIR_FORCE_CLOSE(pipefd[0]);
 
             if (!ret)
                 (*buf)[got] = '\0';
index 60e62e2c06d49a79125713ef87648f4a18b0715a..278dff303a961f1ed808aa1f9441353117d5c541 100644 (file)
@@ -39,6 +39,7 @@
 # include "internal.h"
 # include "logging.h"
 # include "util.h"
+# include "files.h"
 
 /* ie  Ctrl-]  as per telnet */
 # define CTRL_CLOSE_BRACKET '\35'
@@ -192,7 +193,7 @@ int vshRunConsole(const char *tty) {
     tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr);
 
  closetty:
-    close(ttyfd);
+    VIR_FORCE_CLOSE(ttyfd);
 
     return ret;
 }
index 26d7f5ac758c092705695c177bde99a9dfd62fba..6a9b5d9bd6c2aed17d14356cf432b525f64eb5eb 100644 (file)
@@ -51,6 +51,7 @@
 #include "memory.h"
 #include "xml.h"
 #include "libvirt/libvirt-qemu.h"
+#include "files.h"
 
 static char *progname;
 
@@ -11269,11 +11270,9 @@ static void
 vshCloseLogFile(vshControl *ctl)
 {
     /* log file close */
-    if (ctl->log_fd >= 0) {
-        if (close(ctl->log_fd) < 0)
-            vshError(ctl, _("%s: failed to write log file: %s"),
-                     ctl->logfile ? ctl->logfile : "?", strerror (errno));
-        ctl->log_fd = -1;
+    if (VIR_CLOSE(ctl->log_fd) < 0) {
+        vshError(ctl, _("%s: failed to write log file: %s"),
+                 ctl->logfile ? ctl->logfile : "?", strerror (errno));
     }
 
     if (ctl->logfile) {