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