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