xref: /netbsd-src/lib/libc/sys/chown.2 (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1.\"	$NetBSD: chown.2,v 1.23 2001/09/16 01:39:00 wiz Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993, 1994
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)chown.2	8.4 (Berkeley) 4/19/94
35.\"
36.Dd April 19, 1994
37.Dt CHOWN 2
38.Os
39.Sh NAME
40.Nm chown ,
41.Nm lchown ,
42.Nm fchown
43.Nd change owner and group of a file
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.Fd #include <sys/types.h>
48.Fd #include <unistd.h>
49.Ft int
50.Fn chown "const char *path" "uid_t owner" "gid_t group"
51.Ft int
52.Fn lchown "const char *path" "uid_t owner" "gid_t group"
53.Ft int
54.Fn fchown "int fd" "uid_t owner" "gid_t group"
55.Sh DESCRIPTION
56The owner ID and group ID of the file
57named by
58.Fa path
59or referenced by
60.Fa fd
61is changed as specified by the arguments
62.Fa owner
63and
64.Fa group .
65The owner of a file may change the
66.Fa group
67to a group of which
68he or she is a member,
69but the change
70.Fa owner
71capability is restricted to the super-user.
72.Pp
73When called to change the owner of a file,
74.Fn chown ,
75.Fn lchown
76and
77.Fn fchown
78clear the set-user-id
79.Dv ( S_ISUID )
80bit on the file.
81When a called to change the group of a file,
82.Fn chown ,
83.Fn lchown
84and
85.Fn fchown
86clear the set-group-id
87.Dv ( S_ISGID )
88bit on the file.
89These actions are taken to prevent accidental or mischievous creation of
90set-user-id and set-group-id programs.
91.Pp
92.Fn lchown
93is like
94.Fn chown
95except in the case where the named file is a symbolic link,
96in which case
97.Fn lchown
98changes the owner and group of the link,
99while
100.Fn chown
101changes the owner and group of the file the link references.
102.Pp
103.Fn fchown
104is particularly useful when used in conjunction
105with the file locking primitives (see
106.Xr flock 2 ) .
107.Pp
108One of the owner or group id's
109may be left unchanged by specifying it as (uid_t)-1 or (gid_t)-1 respectively.
110.Sh RETURN VALUES
111Zero is returned if the operation was successful;
112-1 is returned if an error occurs, with a more specific
113error code being placed in the global variable
114.Va errno .
115.Sh ERRORS
116.Fn chown
117and
118.Fn lchown
119will fail and the file will be unchanged if:
120.Bl -tag -width Er
121.It Bq Er ENOTDIR
122A component of the path prefix is not a directory.
123.It Bq Er ENAMETOOLONG
124A component of a pathname exceeded
125.Dv {NAME_MAX}
126characters, or an entire path name exceeded
127.Dv {PATH_MAX}
128characters.
129.It Bq Er ENOENT
130The named file does not exist.
131.It Bq Er EACCES
132Search permission is denied for a component of the path prefix.
133.It Bq Er ELOOP
134Too many symbolic links were encountered in translating the pathname.
135.It Bq Er EPERM
136The effective user ID is not the super-user.
137.It Bq Er EROFS
138The named file resides on a read-only file system.
139.It Bq Er EFAULT
140.Fa path
141points outside the process's allocated address space.
142.It Bq Er EIO
143An I/O error occurred while reading from or writing to the file system.
144.El
145.Pp
146.Fn fchown
147will fail if:
148.Bl -tag -width Er
149.It Bq Er EBADF
150.Fa fd
151does not refer to a valid descriptor.
152.It Bq Er EINVAL
153.Fa fd
154refers to a socket, not a file.
155.It Bq Er EPERM
156The effective user ID is not the super-user.
157.It Bq Er EROFS
158The named file resides on a read-only file system.
159.It Bq Er EIO
160An I/O error occurred while reading from or writing to the file system.
161.El
162.Sh SEE ALSO
163.Xr chgrp 1 ,
164.Xr chmod 2 ,
165.Xr flock 2 ,
166.Xr symlink 7 ,
167.Xr chown 8
168.Sh STANDARDS
169The
170.Fn chown
171function deviates from the semantics defined in
172.St -p1003.1-90 ,
173which specifies that, unless the caller is the super-user, both the
174set-user-id and set-group-id bits on a file shall be cleared, regardless
175of the file attribute changed.
176The
177.Fn lchown
178and
179.Fn fchown
180functions, as defined by
181.St -xpg4.2 ,
182provide the same semantics.
183.Pp
184To retain conformance to these standards, compatibility interfaces
185are are provided by the
186.Lb libposix
187as follows:
188.Bl -bullet -compact
189.It
190The
191.Fn chown
192function conforms to
193.St -p1003.1-90
194and
195.St -xpg4.2 .
196.It
197The
198.Fn lchown
199and
200.Fn fchown
201functions conform to
202.St -xpg4.2 .
203.El
204.Sh HISTORY
205The
206.Fn fchown
207function call appeared in
208.Bx 4.2 .
209.Pp
210The
211.Fn chown
212and
213.Fn fchown
214functions were changed to follow symbolic links in
215.Bx 4.4 .
216The
217.Fn lchown
218function call appeared in
219.Nx 1.3 .
220