1chmod(2) System Calls chmod(2)
2
3
4
6 chmod, fchmod - change access permission mode of file
7
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 int chmod(const char *path, mode_t mode);
13
14
15 int fchmod(int fildes, mode_t mode);
16
17
19 The chmod() and fchmod() functions set the access permission portion of
20 the mode of the file whose name is given by path or referenced by the
21 open file descriptor fildes to the bit pattern contained in mode.
22 Access permission bits are interpreted as follows:
23
24
25
26
27 S_ISUID 04000 Set user ID on execution.
28 S_ISGID 020#0 Set group ID on execution if # is 7, 5,
29 3, or 1. Enable mandatory file/record
30 locking if # is 6, 4, 2, or 0.
31 S_ISVTX 01000 Sticky bit.
32 S_IRWXU 00700 Read, write, execute by owner.
33 S_IRUSR 00400 Read by owner.
34 S_IWUSR 00200 Write by owner.
35 S_IXUSR 00100 Execute (search if a directory) by
36 owner.
37 S_IRWXG 00070 Read, write, execute by group.
38 S_IRGRP 00040 Read by group.
39 S_IWGRP 00020 Write by group.
40 S_IXGRP 00010 Execute by group.
41 S_IRWXO 00007 Read, write, execute (search) by others.
42 S_IROTH 00004 Read by others.
43 S_IWOTH 00002 Write by others.
44 S_IXOTH 00001 Execute by others.
45
46
47
48 Modes are constructed by the bitwise OR operation of the access permis‐
49 sion bits.
50
51
52 The effective user ID of the process must match the owner of the file
53 or the process must have the appropriate privilege to change the mode
54 of a file.
55
56
57 If the process is not a privileged process and the file is not a direc‐
58 tory, mode bit 01000 (save text image on execution) is cleared.
59
60
61 If neither the process is privileged nor the file's group is a member
62 of the process's supplementary group list, and the effective group ID
63 of the process does not match the group ID of the file, mode bit 02000
64 (set group ID on execution) is cleared.
65
66
67 If a directory is writable and has S_ISVTX (the sticky bit) set, files
68 within that directory can be removed or renamed only if one or more of
69 the following is true (see unlink(2) and rename(2)):
70
71 o the user owns the file
72
73 o the user owns the directory
74
75 o the file is writable by the user
76
77 o the user is a privileged user
78
79
80 If a regular file is not executable and has S_ISVTX set, the file is
81 assumed to be a swap file. In this case, the system's page cache will
82 not be used to hold the file's data. If the S_ISVTX bit is set on any
83 other file, the results are unspecified.
84
85
86 If a directory has the set group ID bit set, a given file created
87 within that directory will have the same group ID as the directory.
88 Otherwise, the newly created file's group ID will be set to the effec‐
89 tive group ID of the creating process.
90
91
92 If the mode bit 02000 (set group ID on execution) is set and the mode
93 bit 00010 (execute or search by group) is not set, mandatory
94 file/record locking will exist on a regular file, possibly affecting
95 future calls to open(2), creat(2), read(2), and write(2) on this file.
96
97
98 If fildes references a shared memory object, fchmod() need only affect
99 the S_IRUSR, S_IRGRP, S_IROTH, S_IWUSR, S_IWGRP, S_IWOTH, S_IXUSR,
100 S_IXGRP, and S_IXOTH file permission bits.
101
102
103 If fildes refers to a socket, fchmod() does not fail but no action is
104 taken.
105
106
107 If fildes refers to a stream that is attached to an object in the file
108 system name space with fattach(3C), the fchmod() call performs no
109 action and returns successfully.
110
111
112 Upon successful completion, chmod() and fchmod() mark for update the
113 st_ctime field of the file.
114
116 Upon successful completion, 0 is returned. Otherwise, −1 is returned,
117 the file mode is unchanged, and errno is set to indicate the error.
118
120 The chmod() and fchmod() functions will fail if:
121
122 EIO An I/O error occurred while reading from or writing to the
123 file system.
124
125
126 EPERM The effective user ID does not match the owner of the file and
127 the process does not have appropriate privilege.
128
129 The {PRIV_FILE_OWNER} privilege overrides constraints on own‐
130 ership when changing permissions on a file.
131
132 The {PRIV_FILE_SETID} privilege overrides constraints on own‐
133 ership when adding the setuid or setgid bits to an executable
134 file or a directory. When adding the setuid bit to a root
135 owned executable, additional restrictions apply. See privi‐
136 leges(5).
137
138
139
140 The chmod() function will fail if:
141
142 EACCES Search permission is denied on a component of the path
143 prefix of path. The privilege {FILE_DAC_SEARCH} over‐
144 rides file permissions restrictions in that case.
145
146
147 EFAULT The path argument points to an illegal address.
148
149
150 ELOOP A loop exists in symbolic links encountered during the
151 resolution of the path argument.
152
153
154 ENAMETOOLONG The length of the path argument exceeds PATH_MAX, or
155 the length of a path component exceeds NAME_MAX while
156 _POSIX_NO_TRUNC is in effect.
157
158
159 ENOENT Either a component of the path prefix or the file
160 referred to by path does not exist or is a null path‐
161 name.
162
163
164 ENOLINK The fildes argument points to a remote machine and the
165 link to that machine is no longer active.
166
167
168 ENOTDIR A component of the prefix of path is not a directory.
169
170
171 EROFS The file referred to by path resides on a read-only
172 file system.
173
174
175
176 The fchmod() function will fail if:
177
178 EBADF The fildes argument is not an open file descriptor
179
180
181 ENOLINK The path argument points to a remote machine and the link to
182 that machine is no longer active.
183
184
185 EROFS The file referred to by fildes resides on a read-only file
186 system.
187
188
189
190 The chmod() and fchmod() functions may fail if:
191
192 EINTR A signal was caught during execution of the function.
193
194
195 EINVAL The value of the mode argument is invalid.
196
197
198
199 The chmod() function may fail if:
200
201 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered
202 during the resolution of the path argument.
203
204
205 ENAMETOOLONG As a result of encountering a symbolic link in resolu‐
206 tion of thepath argument, the length of the substituted
207 pathname strings exceeds {PATH_MAX}.
208
209
210
211 The fchmod() function may fail if:
212
213 EINVAL The fildes argument refers to a pipe and the system disallows
214 execution of this function on a pipe.
215
216
218 Example 1 Set Read Permissions for User, Group, and Others
219
220
221 The following example sets read permissions for the owner, group, and
222 others.
223
224
225 #include <sys/stat.h>
226 const char *path;
227 ...
228 chmod(path, S_IRUSR|S_IRGRP|S_IROTH);
229
230
231 Example 2 Set Read, Write, and Execute Permissions for the Owner Only
232
233
234 The following example sets read, write, and execute permissions for the
235 owner, and no permissions for group and others.
236
237
238 #include <sys/stat.h>
239 const char *path;
240 ...
241 chmod(path, S_IRWXU);
242
243
244 Example 3 Set Different Permissions for Owner, Group, and Other
245
246
247 The following example sets owner permissions for CHANGEFILE to read,
248 write, and execute, group permissions to read and execute, and other
249 permissions to read.
250
251
252 #include <sys/stat.h>
253 #define CHANGEFILE "/etc/myfile"
254 ...
255 chmod(CHANGEFILE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
256
257
258 Example 4 Set and Checking File Permissions
259
260
261 The following example sets the file permission bits for a file named
262 /home/cnd/mod1, then calls the stat(2) function to verify the permis‐
263 sions.
264
265
266 #include <sys/types.h>
267 #include <sys/stat.h>
268 int status;
269 struct stat buffer
270 ...
271 chmod("home/cnd/mod1", S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH);
272 status = stat("home/cnd/mod1", &buffer;);
273
274
276 If chmod() or fchmod() is used to change the file group owner permis‐
277 sions on a file with non-trivial ACL entries, only the ACL mask is set
278 to the new permissions and the group owner permission bits in the
279 file's mode field (defined in mknod(2)) are unchanged. A non-trivial
280 ACL entry is one whose meaning cannot be represented in the file's mode
281 field alone. The new ACL mask permissions might change the effective
282 permissions for additional users and groups that have ACL entries on
283 the file.
284
286 See attributes(5) for descriptions of the following attributes:
287
288
289
290
291 ┌─────────────────────────────┬─────────────────────────────┐
292 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
293 ├─────────────────────────────┼─────────────────────────────┤
294 │Interface Stability │Standard │
295 ├─────────────────────────────┼─────────────────────────────┤
296 │MT-Level │Async-Signal-Safe │
297 └─────────────────────────────┴─────────────────────────────┘
298
300 chmod(1), chown(2), creat(2), fcntl(2), mknod(2), open(2), read(2),
301 rename(2), stat(2), write(2), fattach(3C), mkfifo(3C), stat.h(3HEAD),
302 attributes(5), privileges(5), standards(5)
303
304
305 Programming Interfaces Guide
306
307
308
309SunOS 5.11 12 Sep 2005 chmod(2)