xref: /netbsd-src/lib/libc/sys/chmod.2 (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1.\"	$NetBSD: chmod.2,v 1.15 1997/10/11 03:34:00 enami Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
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.\"     @(#)chmod.2	8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993
37.Dt CHMOD 2
38.Os BSD 4
39.Sh NAME
40.Nm chmod ,
41.Nm lchmod ,
42.Nm fchmod
43.Nd change mode of file
44.Sh SYNOPSIS
45.Fd #include <sys/types.h>
46.Fd #include <sys/stat.h>
47.Ft int
48.Fn chmod "const char *path" "mode_t mode"
49.Ft int
50.Fn lchmod "const char *path" "mode_t mode"
51.Ft int
52.Fn fchmod "int fd" "mode_t mode"
53.Sh DESCRIPTION
54The function
55.Fn chmod
56sets the file permission bits
57of the file
58specified by the pathname
59.Fa path
60to
61.Fa mode .
62.Fn fchmod
63sets the permission bits of the specified
64file descriptor
65.Fa fd .
66.Fn lchmod
67is like
68.Fn chmod
69except in the case where the named file is a symbolic link,
70in which case
71.Fn lchmod
72sets the permission bits of the link,
73while
74.Fn chmod
75sets the bits of the file the link references.
76.Fn chmod
77verifies that the process owner (user) either owns
78the file specified by
79.Fa path
80(or
81.Fa fd ) ,
82or
83is the super-user.
84A mode is created from
85.Em or'd
86permission bit masks
87defined in
88.Aq Pa sys/stat.h :
89.Bd -literal -offset indent -compact
90#define S_IRWXU 0000700    /* RWX mask for owner */
91#define S_IRUSR 0000400    /* R for owner */
92#define S_IWUSR 0000200    /* W for owner */
93#define S_IXUSR 0000100    /* X for owner */
94
95#define S_IRWXG 0000070    /* RWX mask for group */
96#define S_IRGRP 0000040    /* R for group */
97#define S_IWGRP 0000020    /* W for group */
98#define S_IXGRP 0000010    /* X for group */
99
100#define S_IRWXO 0000007    /* RWX mask for other */
101#define S_IROTH 0000004    /* R for other */
102#define S_IWOTH 0000002    /* W for other */
103#define S_IXOTH 0000001    /* X for other */
104
105#define S_ISUID 0004000    /* set user id on execution */
106#define S_ISGID 0002000    /* set group id on execution */
107#define S_ISVTX 0001000    /* save swapped text even after use */
108.Ed
109.Pp
110The
111.Dv ISVTX
112(the
113.Em sticky bit )
114indicates to the system which executable files are shareable (the
115default) and the system maintains the program text of the files
116in the swap area. The sticky bit may only be set by the super user
117on shareable executable files.
118.Pp
119If mode
120.Dv ISVTX
121(the `sticky bit') is set on a directory,
122an unprivileged user may not delete or rename
123files of other users in that directory. The sticky bit may be
124set by any user on a directory which the user owns or has appropriate
125permissions.
126For more details of the properties of the sticky bit, see
127.Xr sticky 8 .
128.Pp
129Changing the owner of a file
130turns off the set-user-id and set-group-id bits;
131writing to a file
132turns off the set-user-id and set-group-id bits
133unless the user is the super-user.
134This makes the system somewhat more secure
135by protecting set-user-id (set-group-id) files
136from remaining set-user-id (set-group-id) if they are modified,
137at the expense of a degree of compatibility.
138.Sh RETURN VALUES
139Upon successful completion, a value of 0 is returned.
140Otherwise, a value of -1 is returned and
141.Va errno
142is set to indicate the error.
143.Sh ERRORS
144.Fn chmod
145and
146.Fn lchmod
147will fail and the file mode will be unchanged if:
148.Bl -tag -width Er
149.It Bq Er ENOTDIR
150A component of the path prefix is not a directory.
151.It Bq Er ENAMETOOLONG
152A component of a pathname exceeded
153.Dv {NAME_MAX}
154characters, or an entire path name exceeded
155.Dv {PATH_MAX}
156characters.
157.It Bq Er ENOENT
158The named file does not exist.
159.It Bq Er EACCES
160Search permission is denied for a component of the path prefix.
161.It Bq Er ELOOP
162Too many symbolic links were encountered in translating the pathname.
163.It Bq Er EPERM
164The effective user ID does not match the owner of the file and
165the effective user ID is not the super-user.
166.It Bq Er EROFS
167The named file resides on a read-only file system.
168.It Bq Er EFAULT
169.Fa path
170points outside the process's allocated address space.
171.It Bq Er EIO
172An I/O error occurred while reading from or writing to the file system.
173.It Bq Er EFTYPE
174The effective user ID is not the super-user, the
175.Fa mode
176includes the sticky bit
177.Pq Dv S_ISVTX ,
178and
179.Fa path
180does not refer to a directory.
181.El
182.Pp
183.Fn fchmod
184will fail if:
185.Bl -tag -width Er
186.It Bq Er EBADF
187The descriptor is not valid.
188.It Bq Er EINVAL
189.Fa fd
190refers to a socket, not to a file.
191.It Bq Er EROFS
192The file resides on a read-only file system.
193.It Bq Er EIO
194An I/O error occurred while reading from or writing to the file system.
195.It Bq Er EFTYPE
196The effective user ID is not the super-user, the
197.Fa mode
198includes the sticky bit
199.Pq Dv S_ISVTX ,
200and
201.Fa fd
202does not refer to a directory.
203.El
204.Sh SEE ALSO
205.Xr chmod 1 ,
206.Xr chown 2 ,
207.Xr open 2 ,
208.Xr stat 2 ,
209.Xr sticky 8
210.Sh STANDARDS
211The
212.Fn chmod
213function conforms to
214.St -p1003.1-90 .
215.Sh HISTORY
216The
217.Fn fchmod
218function call
219appeared in
220.Bx 4.2 .
221The
222.Fn lchmod
223function call appeared in
224.Nx 1.3 .
225