summaryrefslogtreecommitdiff
path: root/portability/mknodat.c
blob: 0d5c4591d8631863e797c98047d3dbbabb1449d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
}