1MKNOD(3P) POSIX Programmer's Manual MKNOD(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 mknod, mknodat — make directory, special file, or regular file
13
15 #include <sys/stat.h>
16
17 int mknod(const char *path, mode_t mode, dev_t dev);
18
19 #include <fcntl.h>
20
21 int mknodat(int fd, const char *path, mode_t mode, dev_t dev);
22
24 The mknod() function shall create a new file named by the pathname to
25 which the argument path points.
26
27 The file type for path is OR'ed into the mode argument, and the appli‐
28 cation shall select one of the following symbolic constants:
29
30 ┌───────────┬──────────────────────────────────┐
31 │ Name │ Description │
32 ├───────────┼──────────────────────────────────┤
33 │S_IFIFO │ FIFO-special │
34 │S_IFCHR │ Character-special (non-portable) │
35 │S_IFDIR │ Directory (non-portable) │
36 │S_IFBLK │ Block-special (non-portable) │
37 │S_IFREG │ Regular (non-portable) │
38 └───────────┴──────────────────────────────────┘
39 The only portable use of mknod() is to create a FIFO-special file. If
40 mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspec‐
41 ified.
42
43 The permissions for the new file are OR'ed into the mode argument, and
44 may be selected from any combination of the following symbolic con‐
45 stants:
46
47 ┌───────────┬─────────────────────────────────────────────┐
48 │ Name │ Description │
49 ├───────────┼─────────────────────────────────────────────┤
50 │S_ISUID │ Set user ID on execution. │
51 │S_ISGID │ Set group ID on execution. │
52 │S_IRWXU │ Read, write, or execute (search) by owner. │
53 │S_IRUSR │ Read by owner. │
54 │S_IWUSR │ Write by owner. │
55 │S_IXUSR │ Execute (search) by owner. │
56 │S_IRWXG │ Read, write, or execute (search) by group. │
57 │S_IRGRP │ Read by group. │
58 │S_IWGRP │ Write by group. │
59 │S_IXGRP │ Execute (search) by group. │
60 │S_IRWXO │ Read, write, or execute (search) by others. │
61 │S_IROTH │ Read by others. │
62 │S_IWOTH │ Write by others. │
63 │S_IXOTH │ Execute (search) by others. │
64 │S_ISVTX │ On directories, restricted deletion flag. │
65 └───────────┴─────────────────────────────────────────────┘
66 The user ID of the file shall be initialized to the effective user ID
67 of the process. The group ID of the file shall be initialized to either
68 the effective group ID of the process or the group ID of the parent
69 directory. Implementations shall provide a way to initialize the file's
70 group ID to the group ID of the parent directory. Implementations may,
71 but need not, provide an implementation-defined way to initialize the
72 file's group ID to the effective group ID of the calling process. The
73 owner, group, and other permission bits of mode shall be modified by
74 the file mode creation mask of the process. The mknod() function shall
75 clear each bit whose corresponding bit in the file mode creation mask
76 of the process is set.
77
78 If path names a symbolic link, mknod() shall fail and set errno to
79 [EEXIST].
80
81 Upon successful completion, mknod() shall mark for update the last data
82 access, last data modification, and last file status change timestamps
83 of the file. Also, the last data modification and last file status
84 change timestamps of the directory that contains the new entry shall be
85 marked for update.
86
87 Only a process with appropriate privileges may invoke mknod() for file
88 types other than FIFO-special.
89
90 The mknodat() function shall be equivalent to the mknod() function
91 except in the case where path specifies a relative path. In this case
92 the newly created directory, special file, or regular file is located
93 relative to the directory associated with the file descriptor fd
94 instead of the current working directory. If the access mode of the
95 open file description associated with the file descriptor is not
96 O_SEARCH, the function shall check whether directory searches are per‐
97 mitted using the current permissions of the directory underlying the
98 file descriptor. If the access mode is O_SEARCH, the function shall not
99 perform the check.
100
101 If mknodat() is passed the special value AT_FDCWD in the fd parameter,
102 the current working directory shall be used and the behavior shall be
103 identical to a call to mknod().
104
106 Upon successful completion, these functions shall return 0. Otherwise,
107 these functions shall return -1 and set errno to indicate the error. If
108 -1 is returned, the new file shall not be created.
109
111 These functions shall fail if:
112
113 EACCES A component of the path prefix denies search permission, or
114 write permission is denied on the parent directory.
115
116 EEXIST The named file exists.
117
118 EINVAL An invalid argument exists.
119
120 EIO An I/O error occurred while accessing the file system.
121
122 ELOOP A loop exists in symbolic links encountered during resolution of
123 the path argument.
124
125 ENAMETOOLONG
126 The length of a component of a pathname is longer than
127 {NAME_MAX}.
128
129 ENOENT A component of the path prefix of path does not name an existing
130 file or path is an empty string.
131
132 ENOENT or ENOTDIR
133 The path argument contains at least one non-<slash> character
134 and ends with one or more trailing <slash> characters. If path
135 without the trailing <slash> characters would name an existing
136 file, an [ENOENT] error shall not occur.
137
138 ENOSPC The directory that would contain the new file cannot be extended
139 or the file system is out of file allocation resources.
140
141 ENOTDIR
142 A component of the path prefix names an existing file that is
143 neither a directory nor a symbolic link to a directory.
144
145 EPERM The invoking process does not have appropriate privileges and
146 the file type is not FIFO-special.
147
148 EROFS The directory in which the file is to be created is located on a
149 read-only file system.
150
151 The mknodat() function shall fail if:
152
153 EACCES The access mode of the open file description associated with fd
154 is not O_SEARCH and the permissions of the directory underlying
155 fd do not permit directory searches.
156
157 EBADF The path argument does not specify an absolute path and the fd
158 argument is neither AT_FDCWD nor a valid file descriptor open
159 for reading or searching.
160
161 ENOTDIR
162 The path argument is not an absolute path and fd is a file
163 descriptor associated with a non-directory file.
164
165 These functions may fail if:
166
167 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
168 resolution of the path argument.
169
170 ENAMETOOLONG
171 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
172 tion of a symbolic link produced an intermediate result with a
173 length that exceeds {PATH_MAX}.
174
175 The following sections are informative.
176
178 Creating a FIFO Special File
179 The following example shows how to create a FIFO special file named
180 /home/cnd/mod_done, with read/write permissions for owner, and with
181 read permissions for group and others.
182
183
184 #include <sys/types.h>
185 #include <sys/stat.h>
186
187 dev_t dev;
188 int status;
189 ...
190 status = mknod("/home/cnd/mod_done", S_IFIFO | S_IWUSR |
191 S_IRUSR | S_IRGRP | S_IROTH, dev);
192
194 The mkfifo() function is preferred over this function for making FIFO
195 special files.
196
198 The POSIX.1‐1990 standard required that the group ID of a newly created
199 file be set to the group ID of its parent directory or to the effective
200 group ID of the creating process. FIPS 151‐2 required that implementa‐
201 tions provide a way to have the group ID be set to the group ID of the
202 containing directory, but did not prohibit implementations also sup‐
203 porting a way to set the group ID to the effective group ID of the cre‐
204 ating process. Conforming applications should not assume which group
205 ID will be used. If it matters, an application can use chown() to set
206 the group ID after the file is created, or determine under what condi‐
207 tions the implementation will set the desired group ID.
208
209 The purpose of the mknodat() function is to create directories, special
210 files, or regular files in directories other than the current working
211 directory without exposure to race conditions. Any part of the path of
212 a file could be changed in parallel to a call to mknod(), resulting in
213 unspecified behavior. By opening a file descriptor for the target
214 directory and using the mknodat() function it can be guaranteed that
215 the newly created directory, special file, or regular file is located
216 relative to the desired directory.
217
219 None.
220
222 chmod(), creat(), exec, fstatat(), mkdir(), mkfifo(), open(), umask()
223
224 The Base Definitions volume of POSIX.1‐2017, <fcntl.h>, <sys_stat.h>
225
227 Portions of this text are reprinted and reproduced in electronic form
228 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
229 table Operating System Interface (POSIX), The Open Group Base Specifi‐
230 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
231 Electrical and Electronics Engineers, Inc and The Open Group. In the
232 event of any discrepancy between this version and the original IEEE and
233 The Open Group Standard, the original IEEE and The Open Group Standard
234 is the referee document. The original Standard can be obtained online
235 at http://www.opengroup.org/unix/online.html .
236
237 Any typographical or formatting errors that appear in this page are
238 most likely to have been introduced during the conversion of the source
239 files to man page format. To report such errors, see https://www.ker‐
240 nel.org/doc/man-pages/reporting_bugs.html .
241
242
243
244IEEE/The Open Group 2017 MKNOD(3P)