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