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