Merge branch 'vendor/GCC44'
[dragonfly.git] / lib / libc / sys / chmod.2
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)chmod.2     8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: src/lib/libc/sys/chmod.2,v 1.16.2.7 2001/12/14 18:34:00 ru Exp $
34 .\" $DragonFly: src/lib/libc/sys/chmod.2,v 1.4 2006/05/26 19:39:37 swildner Exp $
35 .\"
36 .Dd August 4, 2009
37 .Dt CHMOD 2
38 .Os
39 .Sh NAME
40 .Nm chmod ,
41 .Nm fchmod ,
42 .Nm lchmod ,
43 .Nm fchmodat
44 .Nd change mode of file
45 .Sh LIBRARY
46 .Lb libc
47 .Sh SYNOPSIS
48 .In sys/stat.h
49 .Ft int
50 .Fn chmod "const char *path" "mode_t mode"
51 .Ft int
52 .Fn fchmod "int fd" "mode_t mode"
53 .Ft int
54 .Fn lchmod "const char *path" "mode_t mode"
55 .Ft int
56 .Fn fchmodat "int dirfd" "const char *path" "mode_t mode" "int flags"
57 .Sh DESCRIPTION
58 The file permission bits of the file named specified by
59 .Fa path
60 or referenced by the file descriptor
61 .Fa fd
62 are changed to
63 .Fa mode .
64 The
65 .Fn chmod
66 function verifies that the process owner (user) either owns
67 the file specified by
68 .Fa path
69 (or
70 .Fa fd ) ,
71 or
72 is the super-user.
73 The
74 .Fn chmod
75 function follows symbolic links to operate on the target of the link
76 rather than the link itself.
77 .Pp
78 The
79 .Fa lchmod
80 function is similar to
81 .Fn chmod
82 but does not follow symbolic links.
83 .Pp
84 The
85 .Fn fchmodat
86 function is equivalent to the
87 .Fn chmod
88 or
89 .Fn lchmod
90 functions except in the case where the
91 .Fa path
92 specifies a relative path.
93 In this case the file to be opened is determined relative to the directory
94 associated with the file descriptor
95 .Fa dirfd
96 instead of the current working directory.
97 If
98 .Fn fchmodat
99 is passed the special value
100 .Dv AT_FDCWD
101 in the
102 .Fa dirfd
103 parameter, the current working directory is used
104 and the behavior is identical to a call to
105 .Fn chmod
106 or
107 .Fn lchmod .
108 .Pp
109 A mode is created from
110 .Em or'd
111 permission bit masks
112 defined in
113 .In sys/stat.h :
114 .Pp
115 .Bd -literal -offset indent -compact
116 #define S_IRWXU 0000700    /* RWX mask for owner */
117 #define S_IRUSR 0000400    /* R for owner */
118 #define S_IWUSR 0000200    /* W for owner */
119 #define S_IXUSR 0000100    /* X for owner */
120
121 #define S_IRWXG 0000070    /* RWX mask for group */
122 #define S_IRGRP 0000040    /* R for group */
123 #define S_IWGRP 0000020    /* W for group */
124 #define S_IXGRP 0000010    /* X for group */
125
126 #define S_IRWXO 0000007    /* RWX mask for other */
127 #define S_IROTH 0000004    /* R for other */
128 #define S_IWOTH 0000002    /* W for other */
129 #define S_IXOTH 0000001    /* X for other */
130
131 #define S_ISUID 0004000    /* set user id on execution */
132 #define S_ISGID 0002000    /* set group id on execution */
133 #define S_ISVTX 0001000    /* sticky bit */
134 #ifndef _POSIX_SOURCE
135 #define S_ISTXT 0001000
136 #endif
137 .Ed
138 .Pp
139 The
140 .Dx
141 VM system totally ignores the sticky bit
142 .Pq Dv ISVTX
143 for executables.
144 On UFS-based filesystems (FFS, MFS, LFS) the sticky
145 bit may only be set upon directories.
146 .Pp
147 If mode
148 .Dv ISVTX
149 (the `sticky bit') is set on a directory,
150 an unprivileged user may not delete or rename
151 files of other users in that directory.
152 The sticky bit may be
153 set by any user on a directory which the user owns or has appropriate
154 permissions.
155 For more details of the properties of the sticky bit, see
156 .Xr sticky 8 .
157 .Pp
158 If mode ISUID (set UID) is set on a directory,
159 and the MNT_SUIDDIR option was used in the mount of the filesystem,
160 then the owner of any new files and sub-directories
161 created within this directory are set
162 to be the same as the owner of that directory.
163 If this function is enabled, new directories will inherit
164 the bit from their parents.  Execute bits are removed from
165 the file, and it will not be given to root.
166 This behavior does not change the
167 requirements for the user to be allowed to write the file, but only the eventual
168 owner after it has been created.
169 Group inheritance is not affected.
170 .Pp
171 This feature is designed for use on fileservers serving PC users via
172 ftp, SAMBA, or netatalk.
173 It provides security holes for shell users and as
174 such should not be used on shell machines, especially on home directories.
175 This option requires the SUIDDIR
176 option in the kernel to work.
177 Only UFS filesystems support this option.
178 For more details of the suiddir mount option, see
179 .Xr mount 8 .
180 .Pp
181 Writing or changing the owner of a file
182 turns off the set-user-id and set-group-id bits
183 unless the user is the super-user.
184 This makes the system somewhat more secure
185 by protecting set-user-id (set-group-id) files
186 from remaining set-user-id (set-group-id) if they are modified,
187 at the expense of a degree of compatibility.
188 .Pp
189 The values for the
190 .Fa flags
191 are constructed by a bitwise-inclusive OR of flags from the following list,
192 defined in
193 .In fcntl.h :
194 .Bl -tag -width indent
195 .It Dv AT_SYMLINK_NOFOLLOW
196 If
197 .Fa path
198 names a symbolic link, the mode of the symbolic link is changed.
199 .El
200 .Sh RETURN VALUES
201 .Rv -std
202 .Sh ERRORS
203 .Fn Chmod
204 will fail and the file mode will be unchanged if:
205 .Bl -tag -width Er
206 .It Bq Er ENOTDIR
207 A component of the path prefix is not a directory.
208 .It Bq Er ENAMETOOLONG
209 A component of a pathname exceeded 255 characters,
210 or an entire path name exceeded 1023 characters.
211 .It Bq Er ENOENT
212 The named file does not exist.
213 .It Bq Er EACCES
214 Search permission is denied for a component of the path prefix.
215 .It Bq Er ELOOP
216 Too many symbolic links were encountered in translating the pathname.
217 .It Bq Er EPERM
218 The effective user ID does not match the owner of the file and
219 the effective user ID is not the super-user.
220 .It Bq Er EROFS
221 The named file resides on a read-only file system.
222 .It Bq Er EFAULT
223 .Fa Path
224 points outside the process's allocated address space.
225 .It Bq Er EIO
226 An I/O error occurred while reading from or writing to the file system.
227 .It Bq Er EFTYPE
228 An attempt was made to set the sticky bit upon an executable.
229 .El
230 .Pp
231 .Fn Fchmod
232 will fail if:
233 .Bl -tag -width Er
234 .It Bq Er EBADF
235 The descriptor is not valid.
236 .It Bq Er EINVAL
237 .Fa fd
238 refers to a socket, not to a file.
239 .It Bq Er EROFS
240 The file resides on a read-only file system.
241 .It Bq Er EIO
242 An I/O error occurred while reading from or writing to the file system.
243 .El
244 .Sh SEE ALSO
245 .Xr chmod 1 ,
246 .Xr chown 2 ,
247 .Xr open 2 ,
248 .Xr stat 2 ,
249 .Xr sticky 8
250 .Sh STANDARDS
251 The
252 .Fn chmod
253 function call is expected to conform to
254 .St -p1003.1-90 ,
255 except for the return of
256 .Er EFTYPE
257 and the use of
258 .Dv S_ISTXT .
259 .Sh HISTORY
260 A
261 .Fn chmod
262 function call appeared in
263 .At v7 .
264 The
265 .Fn fchmod
266 function call
267 appeared in
268 .Bx 4.2 .
269 The
270 .Fn lchmod
271 function call appeared in
272 .Fx 3.0 .