| 1 |
.\" Copyright 1992, Drew Eckhardt <[email protected]> |
| 2 |
.\" Copyright 1993-1994, Ian Jackson <[email protected]> |
| 3 |
.\" Copyright 2006-2014, Michael Kerrisk <[email protected]> |
| 4 |
.\" Copyright, the authors of the Linux man-pages project |
| 5 |
.\" |
| 6 |
.\" SPDX-License-Identifier: GPL-1.0-or-later |
| 7 |
.\" |
| 8 |
.TH mknod 2 2026-02-11 "Linux man-pages 6.18" |
| 9 |
.SH NAME |
| 10 |
mknod, mknodat \- create a special or ordinary file |
| 11 |
.SH LIBRARY |
| 12 |
Standard C library |
| 13 |
.RI ( libc ,\~ \-lc ) |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
.B #include <sys/stat.h> |
| 17 |
.P |
| 18 |
.BI "int mknod(const char *" path ", mode_t " mode ", dev_t " dev ); |
| 19 |
.P |
| 20 |
.BR "#include <fcntl.h> " "/* Definition of AT_* constants */" |
| 21 |
.B #include <sys/stat.h> |
| 22 |
.P |
| 23 |
.BI "int mknodat(int " dirfd ", const char *" path ", mode_t " mode \ |
| 24 |
", dev_t " dev ); |
| 25 |
.fi |
| 26 |
.P |
| 27 |
.RS -4 |
| 28 |
Feature Test Macro Requirements for glibc (see |
| 29 |
.BR feature_test_macros (7)): |
| 30 |
.RE |
| 31 |
.P |
| 32 |
.BR mknod (): |
| 33 |
.nf |
| 34 |
_XOPEN_SOURCE >= 500 |
| 35 |
.\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED |
| 36 |
|| /* Since glibc 2.19: */ _DEFAULT_SOURCE |
| 37 |
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE |
| 38 |
.fi |
| 39 |
.SH DESCRIPTION |
| 40 |
The system call |
| 41 |
.BR mknod () |
| 42 |
creates a filesystem node (file, device special file, or |
| 43 |
named pipe) named |
| 44 |
.IR path , |
| 45 |
with attributes specified by |
| 46 |
.I mode |
| 47 |
and |
| 48 |
.IR dev . |
| 49 |
.P |
| 50 |
The |
| 51 |
.I mode |
| 52 |
argument specifies both the file mode to use and the type of node |
| 53 |
to be created. |
| 54 |
It should be a combination (using bitwise OR) of one of the file types |
| 55 |
listed below and zero or more of the file mode bits listed in |
| 56 |
.BR inode (7). |
| 57 |
.P |
| 58 |
The file mode is modified by the process's |
| 59 |
.I umask |
| 60 |
in the usual way: in the absence of a default ACL, the permissions of the |
| 61 |
created node are |
| 62 |
.RI ( mode " & \[ti]" umask ). |
| 63 |
.P |
| 64 |
The file type must be one of |
| 65 |
.BR S_IFREG , |
| 66 |
.BR S_IFCHR , |
| 67 |
.BR S_IFBLK , |
| 68 |
.BR S_IFIFO , |
| 69 |
or |
| 70 |
.B S_IFSOCK |
| 71 |
.\" (S_IFSOCK since Linux 1.2.4) |
| 72 |
to specify a regular file (which will be created empty), character |
| 73 |
special file, block special file, FIFO (named pipe), or UNIX domain socket, |
| 74 |
respectively. |
| 75 |
(Zero file type is equivalent to type |
| 76 |
.BR S_IFREG .) |
| 77 |
.P |
| 78 |
If the file type is |
| 79 |
.B S_IFCHR |
| 80 |
or |
| 81 |
.BR S_IFBLK , |
| 82 |
then |
| 83 |
.I dev |
| 84 |
specifies the major and minor numbers of the newly created device |
| 85 |
special file |
| 86 |
.RB ( makedev (3) |
| 87 |
may be useful to build the value for |
| 88 |
.IR dev ); |
| 89 |
otherwise it is ignored. |
| 90 |
.P |
| 91 |
If |
| 92 |
.I path |
| 93 |
already exists, or is a symbolic link, this call fails with an |
| 94 |
.B EEXIST |
| 95 |
error. |
| 96 |
.P |
| 97 |
The newly created node will be owned by the effective user ID of the |
| 98 |
process. |
| 99 |
If the directory containing the node has the set-group-ID |
| 100 |
bit set, or if the filesystem is mounted with BSD group semantics, the |
| 101 |
new node will inherit the group ownership from its parent directory; |
| 102 |
otherwise it will be owned by the effective group ID of the process. |
| 103 |
.\" |
| 104 |
.\" |
| 105 |
.SS mknodat() |
| 106 |
The |
| 107 |
.BR mknodat () |
| 108 |
system call operates in exactly the same way as |
| 109 |
.BR mknod (), |
| 110 |
except for the differences described here. |
| 111 |
.P |
| 112 |
If |
| 113 |
.I path |
| 114 |
is relative, then it is interpreted relative to the directory |
| 115 |
referred to by the file descriptor |
| 116 |
.I dirfd |
| 117 |
(rather than relative to the current working directory of |
| 118 |
the calling process, as is done by |
| 119 |
.BR mknod () |
| 120 |
for a relative pathname). |
| 121 |
.P |
| 122 |
If |
| 123 |
.I path |
| 124 |
is relative and |
| 125 |
.I dirfd |
| 126 |
is the special value |
| 127 |
.BR AT_FDCWD , |
| 128 |
then |
| 129 |
.I path |
| 130 |
is interpreted relative to the current working |
| 131 |
directory of the calling process (like |
| 132 |
.BR mknod ()). |
| 133 |
.P |
| 134 |
If |
| 135 |
.I path |
| 136 |
is absolute, then |
| 137 |
.I dirfd |
| 138 |
is ignored. |
| 139 |
.P |
| 140 |
See |
| 141 |
.BR openat (2) |
| 142 |
for an explanation of the need for |
| 143 |
.BR mknodat (). |
| 144 |
.SH RETURN VALUE |
| 145 |
.BR mknod () |
| 146 |
and |
| 147 |
.BR mknodat () |
| 148 |
return zero on success. |
| 149 |
On error, \-1 is returned and |
| 150 |
.I errno |
| 151 |
is set to indicate the error. |
| 152 |
.SH ERRORS |
| 153 |
.TP |
| 154 |
.B EACCES |
| 155 |
The parent directory does not allow write permission to the process, |
| 156 |
or one of the directories in the path prefix of |
| 157 |
.I path |
| 158 |
did not allow search permission. |
| 159 |
(See also |
| 160 |
.BR path_resolution (7).) |
| 161 |
.TP |
| 162 |
.B EBADF |
| 163 |
.RB ( mknodat ()) |
| 164 |
.I path |
| 165 |
is relative but |
| 166 |
.I dirfd |
| 167 |
is neither |
| 168 |
.B AT_FDCWD |
| 169 |
nor a valid file descriptor. |
| 170 |
.TP |
| 171 |
.B EDQUOT |
| 172 |
The user's quota of disk blocks or inodes on the filesystem has been |
| 173 |
exhausted. |
| 174 |
.TP |
| 175 |
.B EEXIST |
| 176 |
.I path |
| 177 |
already exists. |
| 178 |
This includes the case where |
| 179 |
.I path |
| 180 |
is a symbolic link, dangling or not. |
| 181 |
.TP |
| 182 |
.B EFAULT |
| 183 |
.I path |
| 184 |
points outside your accessible address space. |
| 185 |
.TP |
| 186 |
.B EINVAL |
| 187 |
.I mode |
| 188 |
requested creation of something other than a regular file, device |
| 189 |
special file, FIFO or socket. |
| 190 |
.TP |
| 191 |
.B ELOOP |
| 192 |
Too many symbolic links were encountered in resolving |
| 193 |
.IR path . |
| 194 |
.TP |
| 195 |
.B ENAMETOOLONG |
| 196 |
.I path |
| 197 |
was too long. |
| 198 |
.TP |
| 199 |
.B ENOENT |
| 200 |
A directory component in |
| 201 |
.I path |
| 202 |
does not exist or is a dangling symbolic link. |
| 203 |
.TP |
| 204 |
.B ENOMEM |
| 205 |
Insufficient kernel memory was available. |
| 206 |
.TP |
| 207 |
.B ENOSPC |
| 208 |
The device containing |
| 209 |
.I path |
| 210 |
has no room for the new node. |
| 211 |
.TP |
| 212 |
.B ENOTDIR |
| 213 |
A component used as a directory in |
| 214 |
.I path |
| 215 |
is not, in fact, a directory. |
| 216 |
.TP |
| 217 |
.B ENOTDIR |
| 218 |
.RB ( mknodat ()) |
| 219 |
.I path |
| 220 |
is relative and |
| 221 |
.I dirfd |
| 222 |
is a file descriptor referring to a file other than a directory. |
| 223 |
.TP |
| 224 |
.B EPERM |
| 225 |
.I mode |
| 226 |
requested creation of something other than a regular file, |
| 227 |
FIFO (named pipe), or UNIX domain socket, and the caller |
| 228 |
is not privileged (Linux: does not have the |
| 229 |
.B CAP_MKNOD |
| 230 |
capability); |
| 231 |
.\" For UNIX domain sockets and regular files, EPERM is returned only in |
| 232 |
.\" Linux 2.2 and earlier; in Linux 2.4 and later, unprivileged can |
| 233 |
.\" use mknod() to make these files. |
| 234 |
also returned if the filesystem containing |
| 235 |
.I path |
| 236 |
does not support the type of node requested. |
| 237 |
.TP |
| 238 |
.B EROFS |
| 239 |
.I path |
| 240 |
refers to a file on a read-only filesystem. |
| 241 |
.SH VERSIONS |
| 242 |
POSIX.1-2001 says: "The only portable use of |
| 243 |
.BR mknod () |
| 244 |
is to create a FIFO-special file. |
| 245 |
If |
| 246 |
.I mode |
| 247 |
is not |
| 248 |
.B S_IFIFO |
| 249 |
or |
| 250 |
.I dev |
| 251 |
is not 0, the behavior of |
| 252 |
.BR mknod () |
| 253 |
is unspecified." |
| 254 |
However, nowadays one should never use |
| 255 |
.BR mknod () |
| 256 |
for this purpose; |
| 257 |
one should use |
| 258 |
.BR mkfifo (3), |
| 259 |
a function especially defined for this purpose. |
| 260 |
.P |
| 261 |
Under Linux, |
| 262 |
.BR mknod () |
| 263 |
cannot be used to create directories. |
| 264 |
One should make directories with |
| 265 |
.BR mkdir (2). |
| 266 |
.\" and one should make UNIX domain sockets with socket(2) and bind(2). |
| 267 |
.SH STANDARDS |
| 268 |
POSIX.1-2024 XSI. |
| 269 |
.SH HISTORY |
| 270 |
.TP |
| 271 |
.BR mknod () |
| 272 |
SVr4, |
| 273 |
4.4BSD, |
| 274 |
SUSv1, |
| 275 |
POSIX.1-2001 XSI (but see VERSIONS). |
| 276 |
.\" The Linux version differs from the SVr4 version in that it |
| 277 |
.\" does not require root permission to create pipes, also in that no |
| 278 |
.\" EMULTIHOP, ENOLINK, or EINTR error is documented. |
| 279 |
.TP |
| 280 |
.BR mknodat () |
| 281 |
glibc 2.4, |
| 282 |
Linux 2.6.16, |
| 283 |
POSIX.1-2008 XSI. |
| 284 |
.SH NOTES |
| 285 |
There are many infelicities in the protocol underlying NFS. |
| 286 |
Some of these affect |
| 287 |
.BR mknod () |
| 288 |
and |
| 289 |
.BR mknodat (). |
| 290 |
.SH SEE ALSO |
| 291 |
.BR mknod (1), |
| 292 |
.BR chmod (2), |
| 293 |
.BR chown (2), |
| 294 |
.BR fcntl (2), |
| 295 |
.BR mkdir (2), |
| 296 |
.BR mount (2), |
| 297 |
.BR socket (2), |
| 298 |
.BR stat (2), |
| 299 |
.BR umask (2), |
| 300 |
.BR unlink (2), |
| 301 |
.BR makedev (3), |
| 302 |
.BR mkfifo (3), |
| 303 |
.BR acl (5), |
| 304 |
.BR path_resolution (7) |