summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-01-28 18:25:31 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2014-01-28 19:07:13 -0500
commit0f010ef2130e887347212d4a3f81abafc78985a0 (patch)
tree31bbf47c4d9c043b6adcbfc7f4eeba5517b90e1d /src/shared
parent87b0284327e34a4b96c22085fa2cdb3219294991 (diff)
Base mkostemp_safe on mkostemp
It is nice to wrap umask handling and return convention, but glibc's mkostemp is async-signal-safe already.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/missing.h4
-rw-r--r--src/shared/util.c35
2 files changed, 6 insertions, 33 deletions
diff --git a/src/shared/missing.h b/src/shared/missing.h
index 939f81d9c4..ac6f5bf6af 100644
--- a/src/shared/missing.h
+++ b/src/shared/missing.h
@@ -324,10 +324,6 @@ static inline int name_to_handle_at(int fd, const char *name, struct file_handle
# define DRM_IOCTL_DROP_MASTER _IO('d', 0x1f)
#endif
-#ifndef TMP_MAX
-# define TMP_MAX 238328
-#endif
-
#if defined(__i386__) || defined(__x86_64__)
/* The precise definition of __O_TMPFILE is arch specific, so let's
diff --git a/src/shared/util.c b/src/shared/util.c
index 30512d1646..4c5b048286 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6093,43 +6093,20 @@ int getpeersec(int fd, char **ret) {
return 0;
}
+/* This is much like like mkostemp() but is subject to umask(). */
int mkostemp_safe(char *pattern, int flags) {
- unsigned long tries = TMP_MAX;
- char *s;
- int r;
_cleanup_umask_ mode_t u;
+ int fd;
assert(pattern);
u = umask(077);
- /* This is much like like mkostemp() but avoids using any
- * static variables, thus is async signal safe. Also, it's not
- * subject to umask(). */
-
- s = endswith(pattern, "XXXXXX");
- if (!s)
- return -EINVAL;
-
- while (tries--) {
- unsigned i;
- int fd;
-
- r = dev_urandom(s, 6);
- if (r < 0)
- return r;
-
- for (i = 0; i < 6; i++)
- s[i] = ALPHANUMERICAL[(unsigned) s[i] % (sizeof(ALPHANUMERICAL)-1)];
-
- fd = open(pattern, flags|O_EXCL|O_CREAT|O_NOCTTY|O_NOFOLLOW, S_IRUSR|S_IWUSR);
- if (fd >= 0)
- return fd;
- if (!IN_SET(errno, EEXIST, EINTR))
- return -errno;
- }
+ fd = mkostemp(pattern, flags);
+ if (fd < 0)
+ return -errno;
- return -EEXIST;
+ return fd;
}
int open_tmpfile(const char *path, int flags) {