]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: provide fallback if mkostemp(3) missing
authorRuediger Meier <ruediger.meier@ga-group.nl>
Sat, 27 Feb 2016 12:28:04 +0000 (13:28 +0100)
committerRuediger Meier <ruediger.meier@ga-group.nl>
Mon, 29 Feb 2016 12:05:18 +0000 (13:05 +0100)
It's missing on OSX.

CC: Yuriy M. Kaminskiy <yumkam@gmail.com>
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
configure.ac
include/fileutils.h
lib/fileutils.c
libblkid/src/save.c

index 68b1a342d27de0abf67487b5e78edec8c45871a3..0c5537faa43ee0cc5d5b8936d9d9eb56e56ee808 100644 (file)
@@ -368,6 +368,7 @@ AC_CHECK_FUNCS([ \
        llseek \
        lseek64 \
        mempcpy \
+       mkostemp \
        nanosleep \
        ntp_gettime \
        personality \
index ba8da7fe60137d8d8f023001ebc295db96c7aadc..79dd012377dca538890fc7caeacf3cd4c1857ea5 100644 (file)
@@ -8,6 +8,8 @@
 
 #include "c.h"
 
+extern int mkstemp_cloexec(char *template);
+
 extern int xmkstemp(char **tmpname, const char *dir, const char *prefix);
 
 static inline FILE *xfmkstemp(char **tmpname, const char *dir, const char *prefix)
index bf8e60a4b3e6682a64407541fbcaf386bfeaf318..15cc9258302d7cb531c620b0c5acae83fd1065a3 100644 (file)
 #include "fileutils.h"
 #include "pathnames.h"
 
+int mkstemp_cloexec(char *template)
+{
+#ifdef HAVE_MKOSTEMP
+       return mkostemp(template, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
+#else
+       int fd, old_flags, errno_save;
+
+       fd = mkstemp(template);
+       if (fd < 0)
+               return fd;
+
+       old_flags = fcntl(fd, F_GETFD, 0);
+       if (old_flags < 0)
+               goto unwind;
+       if (fcntl(fd, F_SETFD, old_flags | O_CLOEXEC) < 0)
+               goto unwind;
+
+       return fd;
+
+unwind:
+       errno_save = errno;
+       unlink(template);
+       close(fd);
+       errno = errno_save;
+
+       return -1;
+#endif
+}
+
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
 int xmkstemp(char **tmpname, const char *dir, const char *prefix)
@@ -33,7 +62,7 @@ int xmkstemp(char **tmpname, const char *dir, const char *prefix)
                return -1;
 
        old_mode = umask(077);
-       fd = mkostemp(localtmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
+       fd = mkstemp_cloexec(localtmp);
        umask(old_mode);
        if (fd == -1) {
                free(localtmp);
index 5e8bbee8f96c475e0c9a330cb22e73820a1aabac..307053094860e2a85035d47d455647ad81fc0956 100644 (file)
@@ -23,6 +23,7 @@
 #endif
 
 #include "closestream.h"
+#include "fileutils.h"
 
 #include "blkidP.h"
 
@@ -133,7 +134,7 @@ int blkid_flush_cache(blkid_cache cache)
                tmp = malloc(strlen(filename) + 8);
                if (tmp) {
                        sprintf(tmp, "%s-XXXXXX", filename);
-                       fd = mkostemp(tmp, O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC);
+                       fd = mkstemp_cloexec(tmp);
                        if (fd >= 0) {
                                if (fchmod(fd, 0644) != 0)
                                        DBG(SAVE, ul_debug("%s: fchmod failed", filename));