1MKNOD(2) Linux Programmer's Manual MKNOD(2)
2
3
4
6 mknod, mknodat - create a special or ordinary file
7
9 #include <sys/stat.h>
10
11 int mknod(const char *pathname, mode_t mode, dev_t dev);
12
13 #include <fcntl.h> /* Definition of AT_* constants */
14 #include <sys/stat.h>
15
16 int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 mknod():
21 _XOPEN_SOURCE >= 500
22 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
23 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
24
26 The system call mknod() creates a filesystem node (file, device special
27 file, or named pipe) named pathname, with attributes specified by mode
28 and dev.
29
30 The mode argument specifies both the file mode to use and the type of
31 node to be created. It should be a combination (using bitwise OR) of
32 one of the file types listed below and zero or more of the file mode
33 bits listed in inode(7).
34
35 The file mode is modified by the process's umask in the usual way: in
36 the absence of a default ACL, the permissions of the created node are
37 (mode & ~umask).
38
39 The file type must be one of S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO, or
40 S_IFSOCK to specify a regular file (which will be created empty), char‐
41 acter special file, block special file, FIFO (named pipe), or UNIX do‐
42 main socket, respectively. (Zero file type is equivalent to type
43 S_IFREG.)
44
45 If the file type is S_IFCHR or S_IFBLK, then dev specifies the major
46 and minor numbers of the newly created device special file (makedev(3)
47 may be useful to build the value for dev); otherwise it is ignored.
48
49 If pathname already exists, or is a symbolic link, this call fails with
50 an EEXIST error.
51
52 The newly created node will be owned by the effective user ID of the
53 process. If the directory containing the node has the set-group-ID bit
54 set, or if the filesystem is mounted with BSD group semantics, the new
55 node will inherit the group ownership from its parent directory; other‐
56 wise it will be owned by the effective group ID of the process.
57
58 mknodat()
59 The mknodat() system call operates in exactly the same way as mknod(),
60 except for the differences described here.
61
62 If the pathname given in pathname is relative, then it is interpreted
63 relative to the directory referred to by the file descriptor dirfd
64 (rather than relative to the current working directory of the calling
65 process, as is done by mknod() for a relative pathname).
66
67 If pathname is relative and dirfd is the special value AT_FDCWD, then
68 pathname is interpreted relative to the current working directory of
69 the calling process (like mknod()).
70
71 If pathname is absolute, then dirfd is ignored.
72
73 See openat(2) for an explanation of the need for mknodat().
74
76 mknod() and mknodat() return zero on success. On error, -1 is returned
77 and errno is set to indicate the error.
78
80 EACCES The parent directory does not allow write permission to the
81 process, or one of the directories in the path prefix of path‐
82 name did not allow search permission. (See also path_resolu‐
83 tion(7).)
84
85 EDQUOT The user's quota of disk blocks or inodes on the filesystem has
86 been exhausted.
87
88 EEXIST pathname already exists. This includes the case where pathname
89 is a symbolic link, dangling or not.
90
91 EFAULT pathname points outside your accessible address space.
92
93 EINVAL mode requested creation of something other than a regular file,
94 device special file, FIFO or socket.
95
96 ELOOP Too many symbolic links were encountered in resolving pathname.
97
98 ENAMETOOLONG
99 pathname was too long.
100
101 ENOENT A directory component in pathname does not exist or is a dan‐
102 gling symbolic link.
103
104 ENOMEM Insufficient kernel memory was available.
105
106 ENOSPC The device containing pathname has no room for the new node.
107
108 ENOTDIR
109 A component used as a directory in pathname is not, in fact, a
110 directory.
111
112 EPERM mode requested creation of something other than a regular file,
113 FIFO (named pipe), or UNIX domain socket, and the caller is not
114 privileged (Linux: does not have the CAP_MKNOD capability); also
115 returned if the filesystem containing pathname does not support
116 the type of node requested.
117
118 EROFS pathname refers to a file on a read-only filesystem.
119
120 The following additional errors can occur for mknodat():
121
122 EBADF dirfd is not a valid file descriptor.
123
124 ENOTDIR
125 pathname is relative and dirfd is a file descriptor referring to
126 a file other than a directory.
127
129 mknodat() was added to Linux in kernel 2.6.16; library support was
130 added to glibc in version 2.4.
131
133 mknod(): SVr4, 4.4BSD, POSIX.1-2001 (but see below), POSIX.1-2008.
134
135 mknodat(): POSIX.1-2008.
136
138 POSIX.1-2001 says: "The only portable use of mknod() is to create a
139 FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behav‐
140 ior of mknod() is unspecified." However, nowadays one should never use
141 mknod() for this purpose; one should use mkfifo(3), a function espe‐
142 cially defined for this purpose.
143
144 Under Linux, mknod() cannot be used to create directories. One should
145 make directories with mkdir(2).
146
147 There are many infelicities in the protocol underlying NFS. Some of
148 these affect mknod() and mknodat().
149
151 mknod(1), chmod(2), chown(2), fcntl(2), mkdir(2), mount(2), socket(2),
152 stat(2), umask(2), unlink(2), makedev(3), mkfifo(3), acl(5), path_reso‐
153 lution(7)
154
156 This page is part of release 5.12 of the Linux man-pages project. A
157 description of the project, information about reporting bugs, and the
158 latest version of this page, can be found at
159 https://www.kernel.org/doc/man-pages/.
160
161
162
163Linux 2021-03-22 MKNOD(2)