1.\" Copyright (c) 1980, 1991 Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" %sccs.include.redist.man% 5.\" 6.\" @(#)chmod.2 6.6 (Berkeley) 03/10/91 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, 80an unprivileged user may not delete or rename 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. 84For more details of the properties of the sticky bit, see 85.Xr sticky 8 . 86.Pp 87Writing or changing the owner of a file 88turns off the set-user-id and set-group-id bits 89unless the user is the super-user. 90This makes the system somewhat more secure 91by protecting set-user-id (set-group-id) files 92from remaining set-user-id (set-group-id) if they are modified, 93at the expense of a degree of compatibility. 94.Sh RETURN VALUES 95Upon successful completion, a value of 0 is returned. 96Otherwise, a value of -1 is returned and 97.Va errno 98is set to indicate the error. 99.Sh ERRORS 100.Fn Chmod 101will fail and the file mode will be unchanged if: 102.Bl -tag -width Er 103.It Bq Er ENOTDIR 104A component of the path prefix is not a directory. 105.It Bq Er EINVAL 106The pathname contains a character with the high-order bit set. 107.It Bq Er ENAMETOOLONG 108A component of a pathname exceeded 255 characters, 109or an entire path name exceeded 1023 characters. 110.It Bq Er ENOENT 111The named file does not exist. 112.It Bq Er EACCES 113Search permission is denied for a component of the path prefix. 114.It Bq Er ELOOP 115Too many symbolic links were encountered in translating the pathname. 116.It Bq Er EPERM 117The effective user ID does not match the owner of the file and 118the effective user ID is not the super-user. 119.It Bq Er EROFS 120The named file resides on a read-only file system. 121.It Bq Er EFAULT 122.Fa Path 123points outside the process's allocated address space. 124.It Bq Er EIO 125An I/O error occurred while reading from or writing to the file system. 126.El 127.Pp 128.Fn Fchmod 129will fail if: 130.Bl -tag -width Er 131.It Bq Er EBADF 132The descriptor is not valid. 133.It Bq Er EINVAL 134.Fa Fd 135refers to a socket, not to a file. 136.It Bq Er EROFS 137The file resides on a read-only file system. 138.It Bq Er EIO 139An I/O error occurred while reading from or writing to the file system. 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 . 157