xref: /openbsd-src/lib/libc/sys/chmod.2 (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1.\"	$OpenBSD: chmod.2,v 1.11 2003/06/02 20:18:39 millert Exp $
2.\"	$NetBSD: chmod.2,v 1.7 1995/02/27 12:32:06 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)chmod.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd June 4, 1993
34.Dt CHMOD 2
35.Os
36.Sh NAME
37.Nm chmod ,
38.Nm fchmod
39.Nd change mode of file
40.Sh SYNOPSIS
41.Fd #include <sys/types.h>
42.Fd #include <sys/stat.h>
43.Ft int
44.Fn chmod "const char *path" "mode_t mode"
45.Ft int
46.Fn fchmod "int fd" "mode_t mode"
47.Sh DESCRIPTION
48The function
49.Fn chmod
50sets the file permission bits of the file specified by the pathname
51.Fa path
52to
53.Fa mode .
54.Fn fchmod
55sets the permission bits of the specified file descriptor
56.Fa fd .
57.Fn chmod
58verifies that the process owner (user) either owns the file specified by
59.Fa path
60(or
61.Fa fd ) ,
62or is the superuser.
63A mode is created from
64.Em or'd
65permission bit masks defined in
66.Aq Pa sys/stat.h :
67.Pp
68.Bd -literal -offset indent -compact
69#define S_IRWXU 0000700    /* RWX mask for owner */
70#define S_IRUSR 0000400    /* R for owner */
71#define S_IWUSR 0000200    /* W for owner */
72#define S_IXUSR 0000100    /* X for owner */
73
74#define S_IRWXG 0000070    /* RWX mask for group */
75#define S_IRGRP 0000040    /* R for group */
76#define S_IWGRP 0000020    /* W for group */
77#define S_IXGRP 0000010    /* X for group */
78
79#define S_IRWXO 0000007    /* RWX mask for other */
80#define S_IROTH 0000004    /* R for other */
81#define S_IWOTH 0000002    /* W for other */
82#define S_IXOTH 0000001    /* X for other */
83
84#define S_ISUID 0004000    /* set user id on execution */
85#define S_ISGID 0002000    /* set group id on execution */
86#define S_ISVTX 0001000    /* save swapped text even after use */
87.Ed
88.Pp
89The
90.Dv ISVTX
91(the
92.Em sticky bit )
93indicates to the system which executable files are shareable (the
94default) and the system maintains the program text of the files
95in the swap area.
96The sticky bit may only be set by the superuser on shareable executable files.
97.Pp
98If mode
99.Dv ISVTX
100(the
101.Dq sticky bit )
102is set on a directory, an unprivileged user may not delete or rename
103files of other users in that directory.
104The sticky bit may be set by any user on a directory which the user owns
105or has appropriate permissions.
106For more details of the properties of the sticky bit, see
107.Xr sticky 8 .
108.Pp
109Writing or changing the owner of a file turns off the set-user-ID and
110set-group-ID bits unless the user is the superuser.
111This makes the system somewhat more secure by protecting
112set-user-ID (set-group-ID) files from remaining set-user-ID (set-group-ID)
113if they are modified, at the expense of a degree of compatibility.
114.Sh RETURN VALUES
115Upon successful completion, a value of 0 is returned.
116Otherwise, a value of \-1 is returned and
117.Va errno
118is set to indicate the error.
119.Sh ERRORS
120.Fn chmod
121will fail and the file mode will be unchanged if:
122.Bl -tag -width Er
123.It Bq Er ENOTDIR
124A component of the path prefix is not a directory.
125.It Bq Er ENAMETOOLONG
126A component of a pathname exceeded
127.Dv {NAME_MAX}
128characters, or an entire path name exceeded
129.Dv {PATH_MAX}
130characters.
131.It Bq Er ENOENT
132The named file does not exist.
133.It Bq Er EACCES
134Search permission is denied for a component of the path prefix.
135.It Bq Er EINVAL
136.Fa mode
137contains bits other than the file type and those described above.
138.It Bq Er ELOOP
139Too many symbolic links were encountered in translating the pathname.
140.It Bq Er EPERM
141The effective user ID does not match the owner of the file and
142the effective user ID is not the superuser.
143.It Bq Er EROFS
144The named file resides on a read-only file system.
145.It Bq Er EFAULT
146.Fa path
147points outside the process's allocated address space.
148.It Bq Er EIO
149An I/O error occurred while reading from or writing to the file system.
150.El
151.Pp
152.Fn fchmod
153will fail and the file mode will be unchanged if:
154.Bl -tag -width Er
155.It Bq Er EBADF
156The descriptor is not valid.
157.It Bq Er EINVAL
158.Fa fd
159refers to a socket, not to a file.
160.It Bq Er EINVAL
161.Fa mode
162contains bits other than the file type and those described above.
163.It Bq Er EROFS
164The file resides on a read-only file system.
165.It Bq Er EIO
166An I/O error occurred while reading from or writing to the file system.
167.El
168.Sh SEE ALSO
169.Xr chmod 1 ,
170.Xr chown 2 ,
171.Xr open 2 ,
172.Xr stat 2 ,
173.Xr sticky 8
174.Sh STANDARDS
175The
176.Fn chmod
177function is expected to conform to
178.St -p1003.1-88 .
179.Sh HISTORY
180The
181.Fn fchmod
182function call appeared in
183.Bx 4.2 .
184