1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. 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.\" @(#)chmod.2 8.1 (Berkeley) 6/4/93 33.\" 34.Dd June 4, 1993 35.Dt CHMOD 2 36.Os BSD 4 37.Sh NAME 38.Nm chmod , 39.Nm fchmod 40.Nd change mode of file 41.Sh SYNOPSIS 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 51of the file 52specified by the pathname 53.Fa path 54to 55.Fa mode . 56.Fn Fchmod 57sets the permission bits of the specified 58file descriptor 59.Fa fd . 60.Fn Chmod 61verifies that the process owner (user) either owns 62the file specified by 63.Fa path 64(or 65.Fa fd ) , 66or 67is the super-user. 68A mode is created from 69.Em or'd 70permission bit masks 71defined in 72.Aq Pa sys/stat.h : 73.Bd -literal -offset indent -compact 74#define S_IRWXU 0000700 /* RWX mask for owner */ 75#define S_IRUSR 0000400 /* R for owner */ 76#define S_IWUSR 0000200 /* W for owner */ 77#define S_IXUSR 0000100 /* X for owner */ 78 79#define S_IRWXG 0000070 /* RWX mask for group */ 80#define S_IRGRP 0000040 /* R for group */ 81#define S_IWGRP 0000020 /* W for group */ 82#define S_IXGRP 0000010 /* X for group */ 83 84#define S_IRWXO 0000007 /* RWX mask for other */ 85#define S_IROTH 0000004 /* R for other */ 86#define S_IWOTH 0000002 /* W for other */ 87#define S_IXOTH 0000001 /* X for other */ 88 89#define S_ISUID 0004000 /* set user id on execution */ 90#define S_ISGID 0002000 /* set group id on execution */ 91#define S_ISVTX 0001000 /* save swapped text even after use */ 92.Ed 93.Pp 94The 95.Dv ISVTX 96(the 97.Em sticky bit ) 98indicates to the system which executable files are shareable (the 99default) and the system maintains the program text of the files 100in the swap area. The sticky bit may only be set by the super user 101on shareable executable files. 102.Pp 103If mode 104.Dv ISVTX 105(the `sticky bit') is set on a directory, 106an unprivileged user may not delete or rename 107files of other users in that directory. The sticky bit may be 108set by any user on a directory which the user owns or has appropriate 109permissions. 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 114turns off the set-user-id and set-group-id bits 115unless the user is the super-user. 116This makes the system somewhat more secure 117by protecting set-user-id (set-group-id) files 118from remaining set-user-id (set-group-id) if they are modified, 119at the expense of a degree of compatibility. 120.Sh RETURN VALUES 121Upon successful completion, a value of 0 is returned. 122Otherwise, a value of -1 is returned and 123.Va errno 124is set to indicate the error. 125.Sh ERRORS 126.Fn Chmod 127will fail and the file mode will be unchanged if: 128.Bl -tag -width Er 129.It Bq Er ENOTDIR 130A component of the path prefix is not a directory. 131.It Bq Er EINVAL 132The pathname contains a character with the high-order bit set. 133.It Bq Er ENAMETOOLONG 134A component of a pathname exceeded 255 characters, 135or an entire path name exceeded 1023 characters. 136.It Bq Er ENOENT 137The named file does not exist. 138.It Bq Er EACCES 139Search permission is denied for a component of the path prefix. 140.It Bq Er ELOOP 141Too many symbolic links were encountered in translating the pathname. 142.It Bq Er EPERM 143The effective user ID does not match the owner of the file and 144the effective user ID is not the super-user. 145.It Bq Er EROFS 146The named file resides on a read-only file system. 147.It Bq Er EFAULT 148.Fa Path 149points outside the process's allocated address space. 150.It Bq Er EIO 151An I/O error occurred while reading from or writing to the file system. 152.El 153.Pp 154.Fn Fchmod 155will fail if: 156.Bl -tag -width Er 157.It Bq Er EBADF 158The descriptor is not valid. 159.It Bq Er EINVAL 160.Fa Fd 161refers to a socket, not to a file. 162.It Bq Er EROFS 163The file resides on a read-only file system. 164.It Bq Er EIO 165An I/O error occurred while reading from or writing to the file system. 166.El 167.Sh SEE ALSO 168.Xr chmod 1 , 169.Xr open 2 , 170.Xr chown 2 , 171.Xr stat 2 , 172.Xr sticky 8 173.Sh STANDARDS 174.Fn Chmod 175is expected to conform to IEEE Std 1003.1-1988 176.Pq Dq Tn POSIX . 177.Sh HISTORY 178The 179.Fn fchmod 180function call 181appeared in 182.Bx 4.2 . 183