aboutsummaryrefslogtreecommitdiffstats
path: root/ports/unix/guts/mknodat.c
blob: 9e86c934e3b46955f6978d69bdc2b0d32115b520 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2011 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * SPDX-License-Identifier: LGPL-2.1-only
 *
 * int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev)
 *	int rc = -1;
 */

 	pseudo_msg_t *msg;
	PSEUDO_STATBUF buf;
        int save_errno = errno;

	/* mask out mode bits appropriately */
	mode = mode & ~pseudo_umask;
        /* if you don't specify a type, assume regular file */
        if (!(mode & S_IFMT)) {
                mode |= S_IFREG;
        }
        pseudo_debug(PDBGF_FILE, "mknodat creating '%s', mode 0%o\n",
                path ? path : "<no name>", (int) mode);

#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	if (dirfd != AT_FDCWD) {
		errno = ENOSYS;
		return -1;
	}
	rc = base_stat(path, &buf);
#else
	rc = base_fstatat(dirfd, path, &buf, AT_SYMLINK_NOFOLLOW);
#endif
	if (rc != -1) {
		/* if we can stat the file, you can't mknod it */
		errno = EEXIST;
		return -1;
	}
#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
	rc = real_open(path, O_CREAT | O_WRONLY | O_EXCL,
		PSEUDO_FS_MODE(mode, 0));
#else
	rc = real_openat(dirfd, path, O_CREAT | O_WRONLY | O_EXCL,
		PSEUDO_FS_MODE(mode, 0));
#endif
	if (rc == -1) {
		return -1;
	}
	real_fchmod(rc, PSEUDO_FS_MODE(mode, 0));
	base_fstat(rc, &buf);
	/* mknod does not really open the file.  We don't have
	 * to use wrap_close because we've never exposed this file
	 * descriptor to the client code.
	 */
	real_close(rc);

	/* mask in the mode type bits again */
	buf.st_mode = (PSEUDO_DB_MODE(buf.st_mode, mode) & 07777) |
			(mode & ~07777);
	buf.st_rdev = dev;
	msg = pseudo_client_op(OP_MKNOD, 0, -1, dirfd, path, &buf);
	if (msg && msg->result != RESULT_SUCCEED) {
		errno = EPERM;
		rc = -1;
	} else {
		/* just pretend we worked */
                errno = save_errno;
		rc = 0;
	}
	if (rc == -1) {
                save_errno = errno;
#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS
		real_unlink(path);
#else
		real_unlinkat(dirfd, path, AT_SYMLINK_NOFOLLOW);
#endif
		errno = save_errno;
	}

/*	return rc;
 * }
 */