]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/chmod.2
mmap.2: srcfix: note kernel commit that caused MAP_POPULATE | MAP_NONBLOCK to be...
[thirdparty/man-pages.git] / man2 / chmod.2
1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\" and Copyright (C) 2006, 2014 Michael Kerrisk
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" Modified by Michael Haardt <michael@moria.de>
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1997-01-12 by Michael Haardt
29 .\" <michael@cantor.informatik.rwth-aachen.de>: NFS details
30 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
31 .\"
32 .TH CHMOD 2 2017-09-15 "Linux" "Linux Programmer's Manual"
33 .SH NAME
34 chmod, fchmod, fchmodat \- change permissions of a file
35 .SH SYNOPSIS
36 .nf
37 .B #include <sys/stat.h>
38 .PP
39 .BI "int chmod(const char *" pathname ", mode_t " mode );
40 .br
41 .BI "int fchmod(int " fd ", mode_t " mode );
42
43 .BR "#include <fcntl.h>" " /* Definition of AT_* constants */"
44 .B #include <sys/stat.h>
45 .PP
46 .BI "int fchmodat(int " dirfd ", const char *" pathname ", mode_t " \
47 mode ", int " flags );
48 .fi
49 .PP
50 .in -4n
51 Feature Test Macro Requirements for glibc (see
52 .BR feature_test_macros (7)):
53 .in
54 .PP
55 .ad l
56 .PD 0
57 .BR fchmod ():
58 .RS 4
59 Since glibc 2.24:
60 _POSIX_C_SOURCE\ >=\ 199309L
61 .\" || (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)
62 .PP
63 Glibc 2.19 to 2.23
64 _POSIX_C_SOURCE
65 .PP
66 Glibc 2.16 to 2.19:
67 _BSD_SOURCE || _POSIX_C_SOURCE
68 .PP
69 Glibc 2.12 to 2.16:
70 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
71 _POSIX_C_SOURCE >= 200809L
72 .PP
73 Glibc 2.11 and earlier:
74 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
75 .\" || (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED)
76 .RE
77 .PD
78 .PP
79 .BR fchmodat ():
80 .PD 0
81 .ad l
82 .RS 4
83 .TP 4
84 Since glibc 2.10:
85 _POSIX_C_SOURCE\ >=\ 200809L
86 .TP
87 Before glibc 2.10:
88 _ATFILE_SOURCE
89 .RE
90 .ad
91 .PD
92 .ad
93 .SH DESCRIPTION
94 The
95 .BR chmod ()
96 and
97 .BR fchmod ()
98 system calls change a files mode bits.
99 (The file mode consists of the file permission bits plus the set-user-ID,
100 set-group-ID, and sticky bits.)
101 These system calls differ only in how the file is specified:
102 .IP * 2
103 .BR chmod ()
104 changes the mode of the file specified whose pathname is given in
105 .IR pathname ,
106 which is dereferenced if it is a symbolic link.
107 .IP *
108 .BR fchmod ()
109 changes the mode of the file referred to by the open file descriptor
110 .IR fd .
111 .PP
112 The new file mode is specified in
113 .IR mode ,
114 which is a bit mask created by ORing together zero or
115 more of the following:
116 .TP 18
117 .BR S_ISUID " (04000)"
118 set-user-ID (set process effective user ID on
119 .BR execve (2))
120 .TP
121 .BR S_ISGID " (02000)"
122 set-group-ID (set process effective group ID on
123 .BR execve (2);
124 mandatory locking, as described in
125 .BR fcntl (2);
126 take a new file's group from parent directory, as described in
127 .BR chown (2)
128 and
129 .BR mkdir (2))
130 .TP
131 .BR S_ISVTX " (01000)"
132 sticky bit (restricted deletion flag, as described in
133 .BR unlink (2))
134 .TP
135 .BR S_IRUSR " (00400)"
136 read by owner
137 .TP
138 .BR S_IWUSR " (00200)"
139 write by owner
140 .TP
141 .BR S_IXUSR " (00100)"
142 execute/search by owner ("search" applies for directories,
143 and means that entries within the directory can be accessed)
144 .TP
145 .BR S_IRGRP " (00040)"
146 read by group
147 .TP
148 .BR S_IWGRP " (00020)"
149 write by group
150 .TP
151 .BR S_IXGRP " (00010)"
152 execute/search by group
153 .TP
154 .BR S_IROTH " (00004)"
155 read by others
156 .TP
157 .BR S_IWOTH " (00002)"
158 write by others
159 .TP
160 .BR S_IXOTH " (00001)"
161 execute/search by others
162 .PP
163 The effective UID of the calling process must match the owner of the file,
164 or the process must be privileged (Linux: it must have the
165 .B CAP_FOWNER
166 capability).
167 .PP
168 If the calling process is not privileged (Linux: does not have the
169 .B CAP_FSETID
170 capability), and the group of the file does not match
171 the effective group ID of the process or one of its
172 supplementary group IDs, the
173 .B S_ISGID
174 bit will be turned off,
175 but this will not cause an error to be returned.
176 .PP
177 As a security measure, depending on the filesystem,
178 the set-user-ID and set-group-ID execution bits
179 may be turned off if a file is written.
180 (On Linux, this occurs if the writing process does not have the
181 .B CAP_FSETID
182 capability.)
183 On some filesystems, only the superuser can set the sticky bit,
184 which may have a special meaning.
185 For the sticky bit, and for set-user-ID and set-group-ID bits on
186 directories, see
187 .BR inode (7).
188 .PP
189 On NFS filesystems, restricting the permissions will immediately influence
190 already open files, because the access control is done on the server, but
191 open files are maintained by the client.
192 Widening the permissions may be
193 delayed for other clients if attribute caching is enabled on them.
194 .\"
195 .\"
196 .SS fchmodat()
197 The
198 .BR fchmodat ()
199 system call operates in exactly the same way as
200 .BR chmod (),
201 except for the differences described here.
202 .PP
203 If the pathname given in
204 .I pathname
205 is relative, then it is interpreted relative to the directory
206 referred to by the file descriptor
207 .I dirfd
208 (rather than relative to the current working directory of
209 the calling process, as is done by
210 .BR chmod ()
211 for a relative pathname).
212 .PP
213 If
214 .I pathname
215 is relative and
216 .I dirfd
217 is the special value
218 .BR AT_FDCWD ,
219 then
220 .I pathname
221 is interpreted relative to the current working
222 directory of the calling process (like
223 .BR chmod ()).
224 .PP
225 If
226 .I pathname
227 is absolute, then
228 .I dirfd
229 is ignored.
230 .PP
231 .I flags
232 can either be 0, or include the following flag:
233 .TP
234 .B AT_SYMLINK_NOFOLLOW
235 If
236 .I pathname
237 is a symbolic link, do not dereference it:
238 instead operate on the link itself.
239 This flag is not currently implemented.
240 .PP
241 See
242 .BR openat (2)
243 for an explanation of the need for
244 .BR fchmodat ().
245 .SH RETURN VALUE
246 On success, zero is returned.
247 On error, \-1 is returned, and
248 .I errno
249 is set appropriately.
250 .SH ERRORS
251 Depending on the filesystem,
252 errors other than those listed below can be returned.
253 .PP
254 The more general errors for
255 .BR chmod ()
256 are listed below:
257 .TP
258 .B EACCES
259 Search permission is denied on a component of the path prefix.
260 (See also
261 .BR path_resolution (7).)
262 .TP
263 .B EFAULT
264 .I pathname
265 points outside your accessible address space.
266 .TP
267 .B EIO
268 An I/O error occurred.
269 .TP
270 .B ELOOP
271 Too many symbolic links were encountered in resolving
272 .IR pathname .
273 .TP
274 .B ENAMETOOLONG
275 .I pathname
276 is too long.
277 .TP
278 .B ENOENT
279 The file does not exist.
280 .TP
281 .B ENOMEM
282 Insufficient kernel memory was available.
283 .TP
284 .B ENOTDIR
285 A component of the path prefix is not a directory.
286 .TP
287 .B EPERM
288 The effective UID does not match the owner of the file,
289 and the process is not privileged (Linux: it does not have the
290 .B CAP_FOWNER
291 capability).
292 .TP
293 .B EPERM
294 The file is marked immutable or append-only.
295 (See
296 .BR ioctl_iflags (2).)
297 .TP
298 .B EROFS
299 The named file resides on a read-only filesystem.
300 .PP
301 The general errors for
302 .BR fchmod ()
303 are listed below:
304 .TP
305 .B EBADF
306 The file descriptor
307 .I fd
308 is not valid.
309 .TP
310 .B EIO
311 See above.
312 .TP
313 .B EPERM
314 See above.
315 .TP
316 .B EROFS
317 See above.
318 .PP
319 The same errors that occur for
320 .BR chmod ()
321 can also occur for
322 .BR fchmodat ().
323 The following additional errors can occur for
324 .BR fchmodat ():
325 .TP
326 .B EBADF
327 .I dirfd
328 is not a valid file descriptor.
329 .TP
330 .B EINVAL
331 Invalid flag specified in
332 .IR flags .
333 .TP
334 .B ENOTDIR
335 .I pathname
336 is relative and
337 .I dirfd
338 is a file descriptor referring to a file other than a directory.
339 .TP
340 .B ENOTSUP
341 .I flags
342 specified
343 .BR AT_SYMLINK_NOFOLLOW ,
344 which is not supported.
345 .SH VERSIONS
346 .BR fchmodat ()
347 was added to Linux in kernel 2.6.16;
348 library support was added to glibc in version 2.4.
349 .SH CONFORMING TO
350 .BR chmod (),
351 .BR fchmod ():
352 4.4BSD, SVr4, POSIX.1-2001i, POSIX.1-2008.
353 .PP
354 .BR fchmodat ():
355 POSIX.1-2008.
356 .SH NOTES
357 .SS C library/kernel differences
358 The GNU C library
359 .BR fchmodat ()
360 wrapper function implements the POSIX-specified
361 interface described in this page.
362 This interface differs from the underlying Linux system call, which does
363 .I not
364 have a
365 .I flags
366 argument.
367 .SS Glibc notes
368 On older kernels where
369 .BR fchmodat ()
370 is unavailable, the glibc wrapper function falls back to the use of
371 .BR chmod ().
372 When
373 .I pathname
374 is a relative pathname,
375 glibc constructs a pathname based on the symbolic link in
376 .IR /proc/self/fd
377 that corresponds to the
378 .IR dirfd
379 argument.
380 .SH SEE ALSO
381 .BR chmod (1),
382 .BR chown (2),
383 .BR execve (2),
384 .BR open (2),
385 .BR stat (2),
386 .BR inode (7),
387 .BR path_resolution (7),
388 .BR symlink (7)