]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/chown.2
zfs: merge openzfs/zfs@41e55b476
[FreeBSD/FreeBSD.git] / lib / libc / sys / chown.2
1 .\" Copyright (c) 1980, 1991, 1993, 1994
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. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)chown.2     8.4 (Berkeley) 4/19/94
29 .\"
30 .Dd March 30, 2021
31 .Dt CHOWN 2
32 .Os
33 .Sh NAME
34 .Nm chown ,
35 .Nm fchown ,
36 .Nm lchown ,
37 .Nm fchownat
38 .Nd change owner and group of a file
39 .Sh LIBRARY
40 .Lb libc
41 .Sh SYNOPSIS
42 .In unistd.h
43 .Ft int
44 .Fn chown "const char *path" "uid_t owner" "gid_t group"
45 .Ft int
46 .Fn fchown "int fd" "uid_t owner" "gid_t group"
47 .Ft int
48 .Fn lchown "const char *path" "uid_t owner" "gid_t group"
49 .Ft int
50 .Fn fchownat "int fd" "const char *path" "uid_t owner" "gid_t group" "int flag"
51 .Sh DESCRIPTION
52 The owner ID and group ID of the file
53 named by
54 .Fa path
55 or referenced by
56 .Fa fd
57 is changed as specified by the arguments
58 .Fa owner
59 and
60 .Fa group .
61 The owner of a file may change the
62 .Fa group
63 to a group of which
64 he or she is a member,
65 but the change
66 .Fa owner
67 capability is restricted to the super-user.
68 .Pp
69 The
70 .Fn chown
71 system call
72 clears the set-user-id and set-group-id bits
73 on the file
74 to prevent accidental or mischievous creation of
75 set-user-id and set-group-id programs if not executed
76 by the super-user.
77 The
78 .Fn chown
79 system call
80 follows symbolic links to operate on the target of the link
81 rather than the link itself.
82 .Pp
83 The
84 .Fn fchown
85 system call
86 is particularly useful when used in conjunction
87 with the file locking primitives (see
88 .Xr flock 2 ) .
89 .Pp
90 The
91 .Fn lchown
92 system call is similar to
93 .Fn chown
94 but does not follow symbolic links.
95 .Pp
96 The
97 .Fn fchownat
98 system call is equivalent to the
99 .Fn chown
100 and
101 .Fn lchown
102 except in the case where
103 .Fa path
104 specifies a relative path.
105 In this case the file to be changed is determined relative to the directory
106 associated with the file descriptor
107 .Fa fd
108 instead of the current working directory.
109 .Pp
110 Values for
111 .Fa flag
112 are constructed by a bitwise-inclusive OR of flags from the following
113 list, defined in
114 .In fcntl.h :
115 .Bl -tag -width indent
116 .It Dv AT_SYMLINK_NOFOLLOW
117 If
118 .Fa path
119 names a symbolic link, ownership of the symbolic link is changed.
120 .It Dv AT_RESOLVE_BENEATH
121 Only walk paths below the directory specified by the
122 .Ar fd
123 descriptor.
124 See the description of the
125 .Dv O_RESOLVE_BENEATH
126 flag in the
127 .Xr open 2
128 manual page.
129 .It Dv AT_EMPTY_PATH
130 If the
131 .Fa path
132 argument is an empty string, operate on the file or directory
133 referenced by the descriptor
134 .Fa fd .
135 If
136 .Fa fd
137 is equal to
138 .Dv AT_FDCWD ,
139 operate on the current working directory.
140 .El
141 .Pp
142 If
143 .Fn fchownat
144 is passed the special value
145 .Dv AT_FDCWD
146 in the
147 .Fa fd
148 parameter, the current working directory is used and the behavior is identical
149 to a call to
150 .Fn chown
151 or
152 .Fn lchown
153 respectively, depending on whether or not the
154 .Dv AT_SYMLINK_NOFOLLOW
155 bit is set in the
156 .Fa flag
157 argument.
158 .Pp
159 One of the owner or group id's
160 may be left unchanged by specifying it as -1.
161 .Sh RETURN VALUES
162 .Rv -std
163 .Sh ERRORS
164 The
165 .Fn chown
166 and
167 .Fn lchown
168 will fail and the file will be unchanged if:
169 .Bl -tag -width Er
170 .It Bq Er ENOTDIR
171 A component of the path prefix is not a directory.
172 .It Bq Er ENAMETOOLONG
173 A component of a pathname exceeded 255 characters,
174 or an entire path name exceeded 1023 characters.
175 .It Bq Er ENOENT
176 The named file does not exist.
177 .It Bq Er EACCES
178 Search permission is denied for a component of the path prefix.
179 .It Bq Er ELOOP
180 Too many symbolic links were encountered in translating the pathname.
181 .It Bq Er EPERM
182 The operation would change the ownership, but the effective user ID is not the
183 super-user.
184 .It Bq Er EPERM
185 The named file has its immutable or append-only flag set, see the
186 .Xr chflags 2
187 manual page for more information.
188 .It Bq Er EROFS
189 The named file resides on a read-only file system.
190 .It Bq Er EFAULT
191 The
192 .Fa path
193 argument
194 points outside the process's allocated address space.
195 .It Bq Er EIO
196 An I/O error occurred while reading from or writing to the file system.
197 .It Bq Er EINTEGRITY
198 Corrupted data was detected while reading from the file system.
199 .El
200 .Pp
201 The
202 .Fn fchown
203 system call will fail if:
204 .Bl -tag -width Er
205 .It Bq Er EBADF
206 The
207 .Fa fd
208 argument
209 does not refer to a valid descriptor.
210 .It Bq Er EINVAL
211 The
212 .Fa fd
213 argument
214 refers to a socket, not a file.
215 .It Bq Er EPERM
216 The effective user ID is not the super-user.
217 .It Bq Er EROFS
218 The named file resides on a read-only file system.
219 .It Bq Er EIO
220 An I/O error occurred while reading from or writing to the file system.
221 .It Bq Er EINTEGRITY
222 Corrupted data was detected while reading from the file system.
223 .El
224 .Pp
225 In addition to the errors specified for
226 .Fn chown
227 and
228 .Fn lchown ,
229 the
230 .Fn fchownat
231 system call may fail if:
232 .Bl -tag -width Er
233 .It Bq Er EBADF
234 The
235 .Fa path
236 argument does not specify an absolute path and the
237 .Fa fd
238 argument is neither
239 .Dv AT_FDCWD
240 nor a valid file descriptor open for searching.
241 .It Bq Er EINVAL
242 The value of the
243 .Fa flag
244 argument is not valid.
245 .It Bq Er ENOTDIR
246 The
247 .Fa path
248 argument is not an absolute path and
249 .Fa fd
250 is neither
251 .Dv AT_FDCWD
252 nor a file descriptor associated with a directory.
253 .It Bq Er ENOTCAPABLE
254 .Fa path
255 is an absolute path,
256 or contained a ".." component leading to a
257 directory outside of the directory hierarchy specified by
258 .Fa fd ,
259 and the process is in capability mode or the
260 .Dv AT_RESOLVE_BENEATH
261 flag was specified.
262 .El
263 .Sh SEE ALSO
264 .Xr chgrp 1 ,
265 .Xr chflags 2 ,
266 .Xr chmod 2 ,
267 .Xr flock 2 ,
268 .Xr chown 8
269 .Sh STANDARDS
270 The
271 .Fn chown
272 system call is expected to conform to
273 .St -p1003.1-90 .
274 The
275 .Fn fchownat
276 system call follows The Open Group Extended API Set 2 specification.
277 .Sh HISTORY
278 The
279 .Fn chown
280 function appeared in
281 .At v1 .
282 The
283 .Fn fchown
284 system call appeared in
285 .Bx 4.2 .
286 .Pp
287 The
288 .Fn chown
289 system call was changed to follow symbolic links in
290 .Bx 4.4 .
291 The
292 .Fn lchown
293 system call was added in
294 .Fx 3.0
295 to compensate for the loss of functionality.
296 .Pp
297 The
298 .Fn fchownat
299 system call appeared in
300 .Fx 8.0 .