diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/util.c | 40 | ||||
-rw-r--r-- | src/shared/util.h | 2 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c index fe05820395..bce4e634c2 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -4145,6 +4145,46 @@ int symlink_atomic(const char *from, const char *to) { return 0; } +int mknod_atomic(const char *path, mode_t mode, dev_t dev) { + _cleanup_free_ char *t = NULL; + + assert(path); + + t = tempfn_random(path); + if (!t) + return -ENOMEM; + + if (mknod(t, mode, dev) < 0) + return -errno; + + if (rename(t, path) < 0) { + unlink_noerrno(t); + return -errno; + } + + return 0; +} + +int mkfifo_atomic(const char *path, mode_t mode) { + _cleanup_free_ char *t = NULL; + + assert(path); + + t = tempfn_random(path); + if (!t) + return -ENOMEM; + + if (mkfifo(t, mode) < 0) + return -errno; + + if (rename(t, path) < 0) { + unlink_noerrno(t); + return -errno; + } + + return 0; +} + bool display_is_local(const char *display) { assert(display); diff --git a/src/shared/util.h b/src/shared/util.h index 73f7c0ad4c..6ad43cd274 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -523,6 +523,8 @@ int terminal_vhangup(const char *name); int vt_disallocate(const char *name); int symlink_atomic(const char *from, const char *to); +int mknod_atomic(const char *path, mode_t mode, dev_t dev); +int mkfifo_atomic(const char *path, mode_t mode); int fchmod_umask(int fd, mode_t mode); |