diff options
Diffstat (limited to 'portability/mknodat.c')
-rw-r--r-- | portability/mknodat.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/portability/mknodat.c b/portability/mknodat.c new file mode 100644 index 0000000..0d5c459 --- /dev/null +++ b/portability/mknodat.c @@ -0,0 +1,30 @@ +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> + +int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev) +{ + int ret = 0; + int curdir_fd = open(".", O_DIRECTORY | O_CLOEXEC); + if (curdir_fd < 0) + return -1; + + if (fchdir(dirfd) < 0) { + ret = -1; + goto cleanup; + } + + /* if mknod fails, fall through and restore the original dirfd */ + if (mknod(pathname, mode, dev) < 0) { + ret = -1; + } + + if (fchdir(curdir_fd) < 0) { + ret = -1; + goto cleanup; + } + +cleanup: + close(curdir_fd); + return ret; +} |