diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-01-25 20:48:01 -0500 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2014-01-27 23:17:02 -0500 |
commit | 8e33886ec582336564ae11b80023abe93d7599c0 (patch) | |
tree | 9d2c70e1a3ba876e78a16e0497a9908ace4f7781 /src/shared/util.c | |
parent | 847657c7690598f1c77d0ba9e86ef58916bc58a7 (diff) |
Replace mkostemp+unlink with open(O_TMPFILE)
This will only work on Linux >= 3.11, and probably not on all
filesystems. Fallback code is provided.
Diffstat (limited to 'src/shared/util.c')
-rw-r--r-- | src/shared/util.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index cdd9a48866..27fc9594cd 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -6108,3 +6108,25 @@ int getpeersec(int fd, char **ret) { *ret = s; return 0; } + +int open_tmpfile(const char *path, int flags) { + int fd; + char *p; + +#ifdef O_TMPFILE + fd = open(path, flags|O_TMPFILE, S_IRUSR|S_IWUSR); + if (fd >= 0) + return fd; +#endif + p = strappenda(path, "/systemd-tmp-XXXXXX"); + + RUN_WITH_UMASK(0077) { + fd = mkostemp(p, O_RDWR|O_CLOEXEC); + } + + if (fd < 0) + return -errno; + + unlink(p); + return fd; +} |