my previous version was wrong; this one is right
[unix-history] / usr / src / lib / libc / sys / chmod.2
CommitLineData
931b8415
CL
1.\" Copyright (c) 1980, 1991 Regents of the University of California.
2.\" All rights reserved.
d149e2aa 3.\"
931b8415 4.\" %sccs.include.redist.man%
d149e2aa 5.\"
931b8415
CL
6.\" @(#)chmod.2 6.6 (Berkeley) %G%
7.\"
8.Dd
9.Dt CHMOD 2
10.Os BSD 4
11.Sh NAME
12.Nm chmod ,
13.Nm fchmod
14.Nd change mode of file
15.Sh SYNOPSIS
16.Fd #include <sys/stat.h>
17.Ft int
18.Fn chmod "const char *path" "mode_t mode"
19.Ft int
20.Fn fchmod "int fd" "mode_t mode"
21.Sh DESCRIPTION
22The function
23.Fn chmod
24sets the file permission bits
25of the file
26specified by the pathname
27.Fa path
28to
29.Fa mode .
30.Fn Fchmod
31sets the permission bits of the specified
32file descriptor
33.Fa fd .
34.Fn Chmod
35verifies that the process owner (user) either owns
36the file specified by
37.Fa path
38(or
39.Fa fd ) ,
40or
41is the super-user.
42A mode is created from
43.Em or'd
44permission bit masks
45defined in
46.Aq Pa sys/stat.h :
47.Bd -literal -offset indent -compact
48#define S_IRWXU 0000700 /* RWX mask for owner */
49#define S_IRUSR 0000400 /* R for owner */
50#define S_IWUSR 0000200 /* W for owner */
51#define S_IXUSR 0000100 /* X for owner */
52
53#define S_IRWXG 0000070 /* RWX mask for group */
54#define S_IRGRP 0000040 /* R for group */
55#define S_IWGRP 0000020 /* W for group */
56#define S_IXGRP 0000010 /* X for group */
57
58#define S_IRWXO 0000007 /* RWX mask for other */
59#define S_IROTH 0000004 /* R for other */
60#define S_IWOTH 0000002 /* W for other */
61#define S_IXOTH 0000001 /* X for other */
62
63#define S_ISUID 0004000 /* set user id on execution */
64#define S_ISGID 0002000 /* set group id on execution */
65#define S_ISVTX 0001000 /* save swapped text even after use */
66.Ed
67.Pp
68The
69.Dv ISVTX
70(the
71.Em sticky bit )
72indicates to the system which executable files are shareable (the
73default) and the system maintains the program text of the files
74in the swap area. The sticky bit may only be set by the super user
75on shareable executable files.
76.Pp
77If mode
78.Dv ISVTX
79(the `sticky bit') is set on a directory,
31d564b9 80an unprivileged user may not delete or rename
931b8415
CL
81files of other users in that directory. The sticky bit may be
82set by any user on a directory which the user owns or has appropriate
83permissions.
31d564b9 84For more details of the properties of the sticky bit, see
931b8415
CL
85.Xr sticky 8 .
86.Pp
7656e522 87Writing or changing the owner of a file
ed77f7df
MK
88turns off the set-user-id and set-group-id bits
89unless the user is the super-user.
d149e2aa 90This makes the system somewhat more secure
7656e522
KM
91by protecting set-user-id (set-group-id) files
92from remaining set-user-id (set-group-id) if they are modified,
d149e2aa 93at the expense of a degree of compatibility.
931b8415 94.Sh RETURN VALUES
7656e522 95Upon successful completion, a value of 0 is returned.
931b8415
CL
96Otherwise, a value of -1 is returned and
97.Va errno
7656e522 98is set to indicate the error.
931b8415
CL
99.Sh ERRORS
100.Fn Chmod
7656e522 101will fail and the file mode will be unchanged if:
931b8415
CL
102.Bl -tag -width Er
103.It Bq Er ENOTDIR
7656e522 104A component of the path prefix is not a directory.
931b8415 105.It Bq Er EINVAL
b5984ffe 106The pathname contains a character with the high-order bit set.
931b8415 107.It Bq Er ENAMETOOLONG
b5984ffe
KM
108A component of a pathname exceeded 255 characters,
109or an entire path name exceeded 1023 characters.
931b8415 110.It Bq Er ENOENT
7656e522 111The named file does not exist.
931b8415 112.It Bq Er EACCES
b5984ffe 113Search permission is denied for a component of the path prefix.
931b8415 114.It Bq Er ELOOP
b5984ffe 115Too many symbolic links were encountered in translating the pathname.
931b8415 116.It Bq Er EPERM
7656e522
KM
117The effective user ID does not match the owner of the file and
118the effective user ID is not the super-user.
931b8415 119.It Bq Er EROFS
7656e522 120The named file resides on a read-only file system.
931b8415
CL
121.It Bq Er EFAULT
122.Fa Path
7656e522 123points outside the process's allocated address space.
931b8415 124.It Bq Er EIO
fd690c8b 125An I/O error occurred while reading from or writing to the file system.
931b8415
CL
126.El
127.Pp
128.Fn Fchmod
7656e522 129will fail if:
931b8415
CL
130.Bl -tag -width Er
131.It Bq Er EBADF
7656e522 132The descriptor is not valid.
931b8415
CL
133.It Bq Er EINVAL
134.Fa Fd
7656e522 135refers to a socket, not to a file.
931b8415 136.It Bq Er EROFS
7656e522 137The file resides on a read-only file system.
931b8415 138.It Bq Er EIO
fd690c8b 139An I/O error occurred while reading from or writing to the file system.
931b8415
CL
140.El
141.Sh SEE ALSO
142.Xr chmod 1 ,
143.Xr open 2 ,
144.Xr chown 2 ,
145.Xr stat 2 ,
146.Xr sticky 8
147.Sh STANDARDS
148.Fn Chmod
149is expected to conform to IEEE Std 1003.1-1988
150.Pq Dq Tn POSIX .
151.Sh HISTORY
152The
153.Fn fchmod
154function call
155appeared in
156.Bx 4.2 .