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.4 1993/11/25 00:38: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 EINVAL 134The pathname contains a character with the high-order bit set. 135.It Bq Er ENAMETOOLONG 136A component of a pathname exceeded 255 characters, 137or an entire path name exceeded 1023 characters. 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 ELOOP 143Too many symbolic links were encountered in translating the pathname. 144.It Bq Er EPERM 145The effective user ID does not match the owner of the file and 146the effective user ID is not the super-user. 147.It Bq Er EROFS 148The named file resides on a read-only file system. 149.It Bq Er EFAULT 150.Fa Path 151points outside the process's allocated address space. 152.It Bq Er EIO 153An I/O error occurred while reading from or writing to the file system. 154.El 155.Pp 156.Fn Fchmod 157will fail if: 158.Bl -tag -width Er 159.It Bq Er EBADF 160The descriptor is not valid. 161.It Bq Er EINVAL 162.Fa Fd 163refers to a socket, not to a file. 164.It Bq Er EROFS 165The file resides on a read-only file system. 166.It Bq Er EIO 167An I/O error occurred while reading from or writing to the file system. 168.El 169.Sh SEE ALSO 170.Xr chmod 1 , 171.Xr open 2 , 172.Xr chown 2 , 173.Xr stat 2 , 174.Xr sticky 8 175.Sh STANDARDS 176The 177.Fn chmod 178function is expected to conform to 179.St -p1003.1-88 . 180.Sh HISTORY 181The 182.Fn fchmod 183function call 184appeared in 185.Bx 4.2 . 186