chiark / gitweb /
fileio: simplify mkostemp_safe() (#4090)
authorTopi Miettinen <topimiettinen@users.noreply.github.com>
Tue, 13 Sep 2016 06:20:38 +0000 (06:20 +0000)
committerSven Eden <yamakuzure@gmx.net>
Wed, 5 Jul 2017 06:50:53 +0000 (08:50 +0200)
According to its manual page, flags given to mkostemp(3) shouldn't include
O_RDWR, O_CREAT or O_EXCL flags as these are always included. Beyond
those, the only flag that all callers (except a few tests where it
probably doesn't matter) use is O_CLOEXEC, so set that unconditionally.

src/basic/fileio.c
src/basic/fileio.h

index d60cd76208e9aa5a5a49977d90d35f8f39c70c7f..4ad2acf3bd7d9f5a404c1b04f89fe378f4a0d6f5 100644 (file)
@@ -1046,7 +1046,7 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
         if (r < 0)
                 return r;
 
         if (r < 0)
                 return r;
 
-        fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
+        fd = mkostemp_safe(t);
         if (fd < 0) {
                 free(t);
                 return -errno;
         if (fd < 0) {
                 free(t);
                 return -errno;
@@ -1079,7 +1079,7 @@ int fflush_and_check(FILE *f) {
 }
 
 /* This is much like mkostemp() but is subject to umask(). */
 }
 
 /* This is much like mkostemp() but is subject to umask(). */
-int mkostemp_safe(char *pattern, int flags) {
+int mkostemp_safe(char *pattern) {
         _cleanup_umask_ mode_t u = 0;
         int fd;
 
         _cleanup_umask_ mode_t u = 0;
         int fd;
 
@@ -1087,7 +1087,7 @@ int mkostemp_safe(char *pattern, int flags) {
 
         u = umask(077);
 
 
         u = umask(077);
 
-        fd = mkostemp(pattern, flags);
+        fd = mkostemp(pattern, O_CLOEXEC);
         if (fd < 0)
                 return -errno;
 
         if (fd < 0)
                 return -errno;
 
@@ -1293,7 +1293,7 @@ int open_tmpfile_unlinkable(const char *directory, int flags) {
         /* Fall back to unguessable name + unlinking */
         p = strjoina(directory, "/systemd-tmp-XXXXXX");
 
         /* Fall back to unguessable name + unlinking */
         p = strjoina(directory, "/systemd-tmp-XXXXXX");
 
-        fd = mkostemp_safe(p, flags);
+        fd = mkostemp_safe(p);
         if (fd < 0)
                 return fd;
 
         if (fd < 0)
                 return fd;
 
index 2469b4ca4d9fff3b78577c73d06015806c6fbbd2..fe9506c3d3717f4b971e8b6d439c45deda81a5c0 100644 (file)
@@ -75,7 +75,7 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *root
 int fflush_and_check(FILE *f);
 
 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
 int fflush_and_check(FILE *f);
 
 int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
-int mkostemp_safe(char *pattern, int flags);
+int mkostemp_safe(char *pattern);
 
 int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
 int tempfn_random(const char *p, const char *extra, char **ret);
 
 int tempfn_xxxxxx(const char *p, const char *extra, char **ret);
 int tempfn_random(const char *p, const char *extra, char **ret);