]> xenbits.xensource.com Git - libvirt.git/commitdiff
build: prefer mkostemp for multi-thread safety
authorEric Blake <eblake@redhat.com>
Wed, 31 Oct 2012 14:13:47 +0000 (08:13 -0600)
committerEric Blake <eblake@redhat.com>
Wed, 31 Oct 2012 16:06:10 +0000 (10:06 -0600)
https://bugzilla.redhat.com/show_bug.cgi?id=871756

Commit cd1e8d1 assumed that systems new enough to have journald
also have mkostemp; but this is not true for uclibc.

For that matter, use of mkstemp[s] is unsafe in a multi-threaded
program.  We should prefer mkostemp[s] in the first place.

* bootstrap.conf (gnulib_modules): Add mkostemp, mkostemps; drop
mkstemp and mkstemps.
* cfg.mk (sc_prohibit_mkstemp): New syntax check.
* tools/virsh.c (vshEditWriteToTempFile): Adjust caller.
* src/qemu/qemu_driver.c (qemuDomainScreenshot)
(qemudDomainMemoryPeek): Likewise.
* src/secret/secret_driver.c (replaceFile): Likewise.
* src/vbox/vbox_tmpl.c (vboxDomainScreenshot): Likewise.

bootstrap.conf
cfg.mk
src/qemu/qemu_driver.c
src/secret/secret_driver.c
src/vbox/vbox_tmpl.c
tools/virsh.c

index 5d391fdadc8ca611ca0b0ae464500126f82457b7..59dd2580f1ffd77b297583e95465fa5cd9a3be04 100644 (file)
@@ -69,8 +69,8 @@ listen
 localeconv
 maintainer-makefile
 manywarnings
-mkstemp
-mkstemps
+mkostemp
+mkostemps
 mktempd
 net_if
 netdb
diff --git a/cfg.mk b/cfg.mk
index 50e6a50c69b015f424ef2b14949e7f420be28526..cda04e418aee5591e62ec7a287ea0b6aa40e1afc 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -339,6 +339,12 @@ sc_prohibit_fork_wrappers:
        halt='use virCommand for child processes'                       \
          $(_sc_search_regexp)
 
+# Prefer mkostemp with O_CLOEXEC.
+sc_prohibit_mkstemp:
+       @prohibit='[^"]\<mkstemps? *\('                                 \
+       halt='use mkostemp with O_CLOEXEC instead of mkstemp'           \
+         $(_sc_search_regexp)
+
 # access with X_OK accepts directories, but we can't exec() those.
 # access with F_OK or R_OK is okay, though.
 sc_prohibit_access_xok:
index 8b5f06a896065cccdaece0d499cc76980a4f992f..267bbf175c6c178bac97c91c12a73249d22c095e 100644 (file)
@@ -3485,8 +3485,8 @@ qemuDomainScreenshot(virDomainPtr dom,
         goto endjob;
     }
 
-    if ((tmp_fd = mkstemp(tmp)) == -1) {
-        virReportSystemError(errno, _("mkstemp(\"%s\") failed"), tmp);
+    if ((tmp_fd = mkostemp(tmp, O_CLOEXEC)) == -1) {
+        virReportSystemError(errno, _("mkostemp(\"%s\") failed"), tmp);
         goto endjob;
     }
     unlink_tmp = true;
@@ -9230,9 +9230,9 @@ qemudDomainMemoryPeek (virDomainPtr dom,
     }
 
     /* Create a temporary filename. */
-    if ((fd = mkstemp (tmp)) == -1) {
+    if ((fd = mkostemp(tmp, O_CLOEXEC)) == -1) {
         virReportSystemError(errno,
-                             _("mkstemp(\"%s\") failed"), tmp);
+                             _("mkostemp(\"%s\") failed"), tmp);
         goto endjob;
     }
 
index 9ce1e3360a3d97b76dab4f6308129f15fd6b91ed..51e1e46485747c412a0e9b45dd625c17a4c5ce9a 100644 (file)
@@ -171,9 +171,9 @@ replaceFile(const char *filename, void *data, size_t size)
         virReportOOMError();
         goto cleanup;
     }
-    fd = mkstemp (tmp_path);
+    fd = mkostemp(tmp_path, O_CLOEXEC);
     if (fd == -1) {
-        virReportSystemError(errno, _("mkstemp('%s') failed"), tmp_path);
+        virReportSystemError(errno, _("mkostemp('%s') failed"), tmp_path);
         goto cleanup;
     }
     if (fchmod(fd, S_IRUSR | S_IWUSR) != 0) {
index 32a903e8feba41864dff5eb35e71da1afb187672..6f245daebaf0846fb9dfc936774b8f3fda7ed829 100644 (file)
@@ -9157,8 +9157,8 @@ vboxDomainScreenshot(virDomainPtr dom,
         return NULL;
     }
 
-    if ((tmp_fd = mkstemp(tmp)) == -1) {
-        virReportSystemError(errno, _("mkstemp(\"%s\") failed"), tmp);
+    if ((tmp_fd = mkostemp(tmp, O_CLOEXEC)) == -1) {
+        virReportSystemError(errno, _("mkostemp(\"%s\") failed"), tmp);
         VIR_FREE(tmp);
         VBOX_RELEASE(machine);
         return NULL;
index f0ec62537fc995bf4d9a6c9ea2b3754314b14288..7bb7781af6075a037bac2f0df7e0139ee7e2cbb8 100644 (file)
@@ -565,9 +565,9 @@ vshEditWriteToTempFile(vshControl *ctl, const char *doc)
         vshError(ctl, "%s", _("out of memory"));
         return NULL;
     }
-    fd = mkstemps(ret, 4);
+    fd = mkostemps(ret, 4, O_CLOEXEC);
     if (fd == -1) {
-        vshError(ctl, _("mkstemps: failed to create temporary file: %s"),
+        vshError(ctl, _("mkostemps: failed to create temporary file: %s"),
                  virStrerror(errno, ebuf, sizeof(ebuf)));
         VIR_FREE(ret);
         return NULL;