1MKNOD(P) POSIX Programmer's Manual MKNOD(P)
2
3
4
6 mknod - make a directory, a special file, or a regular file
7
9 #include <sys/stat.h>
10
11 int mknod(const char *path, mode_t mode, dev_t dev);
12
13
15 The mknod() function shall create a new file named by the pathname to
16 which the argument path points.
17
18 The file type for path is OR'ed into the mode argument, and the appli‐
19 cation shall select one of the following symbolic constants:
20
21 Name Description
22 S_IFIFO FIFO-special
23 S_IFCHR Character-special (non-portable)
24 S_IFDIR Directory (non-portable)
25 S_IFBLK Block-special (non-portable)
26 S_IFREG Regular (non-portable)
27
28 The only portable use of mknod() is to create a FIFO-special file. If
29 mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspec‐
30 ified.
31
32 The permissions for the new file are OR'ed into the mode argument, and
33 may be selected from any combination of the following symbolic con‐
34 stants:
35
36 Name Description
37 S_ISUID Set user ID on execution.
38 S_ISGID Set group ID on execution.
39 S_IRWXU Read, write, or execute (search) by owner.
40 S_IRUSR Read by owner.
41 S_IWUSR Write by owner.
42 S_IXUSR Execute (search) by owner.
43 S_IRWXG Read, write, or execute (search) by group.
44 S_IRGRP Read by group.
45 S_IWGRP Write by group.
46 S_IXGRP Execute (search) by group.
47 S_IRWXO Read, write, or execute (search) by others.
48 S_IROTH Read by others.
49 S_IWOTH Write by others.
50 S_IXOTH Execute (search) by others.
51 S_ISVTX On directories, restricted deletion flag.
52
53 The user ID of the file shall be initialized to the effective user ID
54 of the process. The group ID of the file shall be initialized to either
55 the effective group ID of the process or the group ID of the parent
56 directory. Implementations shall provide a way to initialize the file's
57 group ID to the group ID of the parent directory. Implementations may,
58 but need not, provide an implementation-defined way to initialize the
59 file's group ID to the effective group ID of the calling process. The
60 owner, group, and other permission bits of mode shall be modified by
61 the file mode creation mask of the process. The mknod() function shall
62 clear each bit whose corresponding bit in the file mode creation mask
63 of the process is set.
64
65 If path names a symbolic link, mknod() shall fail and set errno to
66 [EEXIST].
67
68 Upon successful completion, mknod() shall mark for update the st_atime,
69 st_ctime, and st_mtime fields of the file. Also, the st_ctime and
70 st_mtime fields of the directory that contains the new entry shall be
71 marked for update.
72
73 Only a process with appropriate privileges may invoke mknod() for file
74 types other than FIFO-special.
75
77 Upon successful completion, mknod() shall return 0. Otherwise, it shall
78 return -1, the new file shall not be created, and errno shall be set to
79 indicate the error.
80
82 The mknod() function shall fail if:
83
84 EACCES A component of the path prefix denies search permission, or
85 write permission is denied on the parent directory.
86
87 EEXIST The named file exists.
88
89 EINVAL An invalid argument exists.
90
91 EIO An I/O error occurred while accessing the file system.
92
93 ELOOP A loop exists in symbolic links encountered during resolution of
94 the path argument.
95
96 ENAMETOOLONG
97 The length of a pathname exceeds {PATH_MAX} or a pathname compo‐
98 nent is longer than {NAME_MAX}.
99
100 ENOENT A component of the path prefix specified by path does not name
101 an existing directory or path is an empty string.
102
103 ENOSPC The directory that would contain the new file cannot be extended
104 or the file system is out of file allocation resources.
105
106 ENOTDIR
107 A component of the path prefix is not a directory.
108
109 EPERM The invoking process does not have appropriate privileges and
110 the file type is not FIFO-special.
111
112 EROFS The directory in which the file is to be created is located on a
113 read-only file system.
114
115
116 The mknod() function may fail if:
117
118 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
119 resolution of the path argument.
120
121 ENAMETOOLONG
122 Pathname resolution of a symbolic link produced an intermediate
123 result whose length exceeds {PATH_MAX}.
124
125
126 The following sections are informative.
127
129 Creating a FIFO Special File
130 The following example shows how to create a FIFO special file named
131 /home/cnd/mod_done, with read/write permissions for owner, and with
132 read permissions for group and others.
133
134
135 #include <sys/types.h>
136 #include <sys/stat.h>
137
138
139 dev_t dev;
140 int status;
141 ...
142 status = mknod("/home/cnd/mod_done", S_IFIFO | S_IWUSR |
143 S_IRUSR | S_IRGRP | S_IROTH, dev);
144
146 The mkfifo() function is preferred over this function for making FIFO
147 special files.
148
150 The POSIX.1-1990 standard required that the group ID of a newly created
151 file be set to the group ID of its parent directory or to the effective
152 group ID of the creating process. FIPS 151-2 required that implementa‐
153 tions provide a way to have the group ID be set to the group ID of the
154 containing directory, but did not prohibit implementations also sup‐
155 porting a way to set the group ID to the effective group ID of the cre‐
156 ating process. Conforming applications should not assume which group ID
157 will be used. If it matters, an application can use chown() to set the
158 group ID after the file is created, or determine under what conditions
159 the implementation will set the desired group ID.
160
162 None.
163
165 chmod() , creat() , exec() , mkdir() , mkfifo() , open() , stat() ,
166 umask() , the Base Definitions volume of IEEE Std 1003.1-2001,
167 <sys/stat.h>
168
170 Portions of this text are reprinted and reproduced in electronic form
171 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
172 -- Portable Operating System Interface (POSIX), The Open Group Base
173 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
174 Electrical and Electronics Engineers, Inc and The Open Group. In the
175 event of any discrepancy between this version and the original IEEE and
176 The Open Group Standard, the original IEEE and The Open Group Standard
177 is the referee document. The original Standard can be obtained online
178 at http://www.opengroup.org/unix/online.html .
179
180
181
182IEEE/The Open Group 2003 MKNOD(P)