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