diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.c | 22 | ||||
-rw-r--r-- | src/shared/util.h | 2 |
2 files changed, 24 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; +} diff --git a/src/shared/util.h b/src/shared/util.h index d6d746bc6a..630137a53a 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -849,3 +849,5 @@ bool pid_valid(pid_t pid); int getpeercred(int fd, struct ucred *ucred); int getpeersec(int fd, char **ret); + +int open_tmpfile(const char *path, int flags); |