xref: /netbsd-src/lib/libc/sys/chmod.2 (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. 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.\"     from: @(#)chmod.2	6.6 (Berkeley) 3/10/91
33.\"	$Id: chmod.2,v 1.6 1994/04/22 00:13:29 jtc Exp $
34.\"
35.Dd March 10, 1991
36.Dt CHMOD 2
37.Os BSD 4
38.Sh NAME
39.Nm chmod ,
40.Nm fchmod
41.Nd change mode of file
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 fchmod "int fd" "mode_t mode"
49.Sh DESCRIPTION
50The function
51.Fn chmod
52sets the file permission bits
53of the file
54specified by the pathname
55.Fa path
56to
57.Fa mode .
58.Fn Fchmod
59sets the permission bits of the specified
60file descriptor
61.Fa fd .
62.Fn Chmod
63verifies that the process owner (user) either owns
64the file specified by
65.Fa path
66(or
67.Fa fd ) ,
68or
69is the super-user.
70A mode is created from
71.Em or Ap d
72permission bit masks
73defined in
74.Aq Pa sys/stat.h :
75.Bd -literal -offset indent -compact
76#define S_IRWXU 0000700    /* RWX mask for owner */
77#define S_IRUSR 0000400    /* R for owner */
78#define S_IWUSR 0000200    /* W for owner */
79#define S_IXUSR 0000100    /* X for owner */
80
81#define S_IRWXG 0000070    /* RWX mask for group */
82#define S_IRGRP 0000040    /* R for group */
83#define S_IWGRP 0000020    /* W for group */
84#define S_IXGRP 0000010    /* X for group */
85
86#define S_IRWXO 0000007    /* RWX mask for other */
87#define S_IROTH 0000004    /* R for other */
88#define S_IWOTH 0000002    /* W for other */
89#define S_IXOTH 0000001    /* X for other */
90
91#define S_ISUID 0004000    /* set user id on execution */
92#define S_ISGID 0002000    /* set group id on execution */
93#define S_ISVTX 0001000    /* save swapped text even after use */
94.Ed
95.Pp
96The
97.Dv ISVTX
98(the
99.Em sticky bit )
100indicates to the system which executable files are shareable (the
101default) and the system maintains the program text of the files
102in the swap area. The sticky bit may only be set by the super user
103on shareable executable files.
104.Pp
105If mode
106.Dv ISVTX
107(the `sticky bit') is set on a directory,
108an unprivileged user may not delete or rename
109files of other users in that directory. The sticky bit may be
110set by any user on a directory which the user owns or has appropriate
111permissions.
112For more details of the properties of the sticky bit, see
113.Xr sticky 8 .
114.Pp
115Writing or changing the owner of a file
116turns off the set-user-id and set-group-id bits
117unless the user is the super-user.
118This makes the system somewhat more secure
119by protecting set-user-id (set-group-id) files
120from remaining set-user-id (set-group-id) if they are modified,
121at the expense of a degree of compatibility.
122.Sh RETURN VALUES
123Upon successful completion, a value of 0 is returned.
124Otherwise, a value of -1 is returned and
125.Va errno
126is set to indicate the error.
127.Sh ERRORS
128.Fn Chmod
129will fail and the file mode will be unchanged if:
130.Bl -tag -width Er
131.It Bq Er ENOTDIR
132A component of the path prefix is not a directory.
133.It Bq Er ENAMETOOLONG
134A component of a pathname exceeded
135.Dv {NAME_MAX}
136characters, or an entire path name exceeded
137.Dv {PATH_MAX}
138characters.
139.It Bq Er ENOENT
140The named file does not exist.
141.It Bq Er EACCES
142Search permission is denied for a component of the path prefix.
143.It Bq Er ELOOP
144Too many symbolic links were encountered in translating the pathname.
145.It Bq Er EPERM
146The effective user ID does not match the owner of the file and
147the effective user ID is not the super-user.
148.It Bq Er EROFS
149The named file resides on a read-only file system.
150.It Bq Er EFAULT
151.Fa Path
152points outside the process's allocated address space.
153.It Bq Er EIO
154An I/O error occurred while reading from or writing to the file system.
155.El
156.Pp
157.Fn Fchmod
158will fail if:
159.Bl -tag -width Er
160.It Bq Er EBADF
161The descriptor is not valid.
162.It Bq Er EINVAL
163.Fa Fd
164refers to a socket, not to a file.
165.It Bq Er EROFS
166The file resides on a read-only file system.
167.It Bq Er EIO
168An I/O error occurred while reading from or writing to the file system.
169.El
170.Sh SEE ALSO
171.Xr chmod 1 ,
172.Xr open 2 ,
173.Xr chown 2 ,
174.Xr stat 2 ,
175.Xr sticky 8
176.Sh STANDARDS
177The
178.Fn chmod
179function is expected to conform to
180.St -p1003.1-88 .
181.Sh HISTORY
182The
183.Fn fchmod
184function call
185appeared in
186.Bx 4.2 .
187