xref: /openbsd/lib/libc/sys/chmod.2 (revision 3d8817e4)
1.\"	$OpenBSD: chmod.2,v 1.14 2007/05/31 19:19:32 jmc 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 $Mdocdate: May 31 2007 $
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.Bd -literal -offset indent
68#define S_IRWXU 0000700    /* RWX mask for owner */
69#define S_IRUSR 0000400    /* R for owner */
70#define S_IWUSR 0000200    /* W for owner */
71#define S_IXUSR 0000100    /* X for owner */
72
73#define S_IRWXG 0000070    /* RWX mask for group */
74#define S_IRGRP 0000040    /* R for group */
75#define S_IWGRP 0000020    /* W for group */
76#define S_IXGRP 0000010    /* X for group */
77
78#define S_IRWXO 0000007    /* RWX mask for other */
79#define S_IROTH 0000004    /* R for other */
80#define S_IWOTH 0000002    /* W for other */
81#define S_IXOTH 0000001    /* X for other */
82
83#define S_ISUID 0004000    /* set user id on execution */
84#define S_ISGID 0002000    /* set group id on execution */
85#define S_ISVTX 0001000    /* save swapped text even after use */
86.Ed
87.Pp
88If mode
89.Dv ISVTX
90(the
91.Em sticky bit )
92is set on a file, it is ignored.
93.Pp
94If mode
95.Dv ISVTX
96(the
97.Em sticky bit )
98is set on a directory, an unprivileged user may not delete or rename
99files of other users in that directory.
100The sticky bit may be set by any user on a directory which the user owns
101or has appropriate permissions.
102For more details of the properties of the sticky bit, see
103.Xr sticky 8 .
104.Pp
105Writing or changing the owner of a file turns off the set-user-ID and
106set-group-ID bits unless the user is the superuser.
107This makes the system somewhat more secure by protecting
108set-user-ID (set-group-ID) files from remaining set-user-ID (set-group-ID)
109if they are modified, at the expense of a degree of compatibility.
110.Sh RETURN VALUES
111Upon successful completion, a value of 0 is returned.
112Otherwise, a value of \-1 is returned and
113.Va errno
114is set to indicate the error.
115.Sh ERRORS
116.Fn chmod
117will fail and the file mode will be unchanged if:
118.Bl -tag -width Er
119.It Bq Er ENOTDIR
120A component of the path prefix is not a directory.
121.It Bq Er ENAMETOOLONG
122A component of a pathname exceeded
123.Dv {NAME_MAX}
124characters, or an entire path name exceeded
125.Dv {PATH_MAX}
126characters.
127.It Bq Er ENOENT
128The named file does not exist.
129.It Bq Er EACCES
130Search permission is denied for a component of the path prefix.
131.It Bq Er EINVAL
132.Fa mode
133contains bits other than the file type and those described above.
134.It Bq Er ELOOP
135Too many symbolic links were encountered in translating the pathname.
136.It Bq Er EPERM
137The effective user ID does not match the owner of the file and
138the effective user ID is not the superuser.
139.It Bq Er EROFS
140The named file resides on a read-only file system.
141.It Bq Er EFAULT
142.Fa path
143points outside the process's allocated address space.
144.It Bq Er EIO
145An I/O error occurred while reading from or writing to the file system.
146.El
147.Pp
148.Fn fchmod
149will fail and the file mode will be unchanged if:
150.Bl -tag -width Er
151.It Bq Er EBADF
152The descriptor is not valid.
153.It Bq Er EINVAL
154.Fa fd
155refers to a socket, not to a file.
156.It Bq Er EINVAL
157.Fa mode
158contains bits other than the file type and those described above.
159.It Bq Er EROFS
160The file resides on a read-only file system.
161.It Bq Er EIO
162An I/O error occurred while reading from or writing to the file system.
163.El
164.Sh SEE ALSO
165.Xr chmod 1 ,
166.Xr chown 2 ,
167.Xr open 2 ,
168.Xr stat 2 ,
169.Xr sticky 8
170.Sh STANDARDS
171The
172.Fn chmod
173function is expected to conform to
174.St -p1003.1-88 .
175.Sh HISTORY
176The
177.Fn fchmod
178function call appeared in
179.Bx 4.2 .
180