1.\" $NetBSD: chmod.2,v 1.23 2002/10/01 18:10:43 wiz Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)chmod.2 8.1 (Berkeley) 6/4/93 35.\" 36.Dd June 4, 1993 37.Dt CHMOD 2 38.Os 39.Sh NAME 40.Nm chmod , 41.Nm lchmod , 42.Nm fchmod 43.Nd change mode of file 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.Fd #include \*[Lt]sys/stat.h\*[Gt] 48.Ft int 49.Fn chmod "const char *path" "mode_t mode" 50.Ft int 51.Fn lchmod "const char *path" "mode_t mode" 52.Ft int 53.Fn fchmod "int fd" "mode_t mode" 54.Sh DESCRIPTION 55The function 56.Fn chmod 57sets the file permission bits 58of the file 59specified by the pathname 60.Fa path 61to 62.Fa mode . 63.Fn fchmod 64sets the permission bits of the specified 65file descriptor 66.Fa fd . 67.Fn lchmod 68is like 69.Fn chmod 70except in the case where the named file is a symbolic link, 71in which case 72.Fn lchmod 73sets the permission bits of the link, 74while 75.Fn chmod 76sets the bits of the file the link references. 77.Fn chmod 78verifies that the process owner (user) either owns 79the file specified by 80.Fa path 81(or 82.Fa fd ) , 83or 84is the super-user. 85A mode is created from 86.Em or'd 87permission bit masks 88defined in 89.Aq Pa sys/stat.h : 90.Bd -literal -offset indent -compact 91#define S_IRWXU 0000700 /* RWX mask for owner */ 92#define S_IRUSR 0000400 /* R for owner */ 93#define S_IWUSR 0000200 /* W for owner */ 94#define S_IXUSR 0000100 /* X for owner */ 95 96#define S_IRWXG 0000070 /* RWX mask for group */ 97#define S_IRGRP 0000040 /* R for group */ 98#define S_IWGRP 0000020 /* W for group */ 99#define S_IXGRP 0000010 /* X for group */ 100 101#define S_IRWXO 0000007 /* RWX mask for other */ 102#define S_IROTH 0000004 /* R for other */ 103#define S_IWOTH 0000002 /* W for other */ 104#define S_IXOTH 0000001 /* X for other */ 105 106#define S_ISUID 0004000 /* set user id on execution */ 107#define S_ISGID 0002000 /* set group id on execution */ 108#define S_ISVTX 0001000 /* save swapped text even after use */ 109.Ed 110.Pp 111The 112.Dv ISVTX 113(the 114.Em sticky bit ) 115indicates to the system which executable files are shareable (the 116default) and the system maintains the program text of the files 117in the swap area. 118The sticky bit may only be set by the super user 119on shareable executable files. 120.Pp 121If mode 122.Dv ISVTX 123(the `sticky bit') is set on a directory, 124an unprivileged user may not delete or rename 125files of other users in that directory. 126The sticky bit may be set by any user on a directory which the user 127owns or has appropriate permissions. 128For more details of the properties of the sticky bit, see 129.Xr sticky 8 . 130.Pp 131Changing the owner of a file 132turns off the set-user-id and set-group-id bits; 133writing to a file 134turns off the set-user-id and set-group-id bits 135unless the user is the super-user. 136This makes the system somewhat more secure 137by protecting set-user-id (set-group-id) files 138from remaining set-user-id (set-group-id) if they are modified, 139at the expense of a degree of compatibility. 140.Sh RETURN VALUES 141Upon successful completion, a value of 0 is returned. 142Otherwise, a value of -1 is returned and 143.Va errno 144is set to indicate the error. 145.Sh ERRORS 146.Fn chmod 147and 148.Fn lchmod 149will fail and the file mode will be unchanged if: 150.Bl -tag -width Er 151.It Bq Er ENOTDIR 152A component of the path prefix is not a directory. 153.It Bq Er ENAMETOOLONG 154A component of a pathname exceeded 155.Dv {NAME_MAX} 156characters, or an entire path name exceeded 157.Dv {PATH_MAX} 158characters. 159.It Bq Er ENOENT 160The named file does not exist. 161.It Bq Er EACCES 162Search permission is denied for a component of the path prefix. 163.It Bq Er ELOOP 164Too many symbolic links were encountered in translating the pathname. 165.It Bq Er EPERM 166The effective user ID does not match the owner of the file and 167the effective user ID is not the super-user. 168.It Bq Er EROFS 169The named file resides on a read-only file system. 170.It Bq Er EFAULT 171.Fa path 172points outside the process's allocated address space. 173.It Bq Er EIO 174An I/O error occurred while reading from or writing to the file system. 175.It Bq Er EFTYPE 176The effective user ID is not the super-user, the 177.Fa mode 178includes the sticky bit 179.Pq Dv S_ISVTX , 180and 181.Fa path 182does not refer to a directory. 183.El 184.Pp 185.Fn fchmod 186will fail if: 187.Bl -tag -width Er 188.It Bq Er EBADF 189The descriptor is not valid. 190.It Bq Er EINVAL 191.Fa fd 192refers to a socket, not to a file. 193.It Bq Er EROFS 194The file resides on a read-only file system. 195.It Bq Er EIO 196An I/O error occurred while reading from or writing to the file system. 197.It Bq Er EFTYPE 198The effective user ID is not the super-user, the 199.Fa mode 200includes the sticky bit 201.Pq Dv S_ISVTX , 202and 203.Fa fd 204does not refer to a directory. 205.El 206.Sh SEE ALSO 207.Xr chmod 1 , 208.Xr chflags 2 , 209.Xr chown 2 , 210.Xr open 2 , 211.Xr stat 2 , 212.Xr symlink 7 , 213.Xr sticky 8 214.Sh STANDARDS 215The 216.Fn chmod 217function conforms to 218.St -p1003.1-90 . 219.Sh HISTORY 220The 221.Fn fchmod 222function call 223appeared in 224.Bx 4.2 . 225The 226.Fn lchmod 227function call appeared in 228.Nx 1.3 . 229