1CHMOD(P)                   POSIX Programmer's Manual                  CHMOD(P)
2
3
4

NAME

6       chmod - change mode of a file
7

SYNOPSIS

9       #include <sys/stat.h>
10
11       int chmod(const char *path, mode_t mode);
12
13

DESCRIPTION

15       The chmod() function shall change S_ISUID, S_ISGID,    S_ISVTX, and the
16       file permission bits of the file named by the pathname  pointed  to  by
17       the  path  argument to the corresponding bits in the mode argument. The
18       application shall ensure that the effective  user  ID  of  the  process
19       matches the owner of the file or the process has appropriate privileges
20       in order to do this.
21
22       S_ISUID,  S_ISGID,     S_ISVTX,   and  the  file  permission  bits  are
23       described in <sys/stat.h>.
24
25       If the calling process does not have appropriate privileges, and if the
26       group ID of the file does not match the effective group ID  or  one  of
27       the  supplementary  group  IDs  and  if the file is a regular file, bit
28       S_ISGID (set-group-ID on execution) in the file's mode shall be cleared
29       upon successful return from chmod().
30
31       Additional  implementation-defined  restrictions  may cause the S_ISUID
32       and S_ISGID bits in mode to be ignored.
33
34       The effect on file descriptors for files open at the time of a call  to
35       chmod() is implementation-defined.
36
37       Upon  successful completion, chmod() shall mark for update the st_ctime
38       field of the file.
39

RETURN VALUE

41       Upon successful completion, 0 shall be returned; otherwise, -1 shall be
42       returned  and  errno  set  to indicate the error. If -1 is returned, no
43       change to the file mode occurs.
44

ERRORS

46       The chmod() function shall fail if:
47
48       EACCES Search permission is denied on a component of the path prefix.
49
50       ELOOP  A loop exists in symbolic links encountered during resolution of
51              the path argument.
52
53       ENAMETOOLONG
54              The length of the path argument exceeds {PATH_MAX} or a pathname
55              component is longer than {NAME_MAX}.
56
57       ENOTDIR
58              A component of the path prefix is not a directory.
59
60       ENOENT A component of path does not name an existing file or path is an
61              empty string.
62
63       EPERM  The  effective  user ID does not match the owner of the file and
64              the process does not have appropriate privileges.
65
66       EROFS  The named file resides on a read-only file system.
67
68
69       The chmod() function may fail if:
70
71       EINTR  A signal was caught during execution of the function.
72
73       EINVAL The value of the mode argument is invalid.
74
75       ELOOP  More than {SYMLOOP_MAX} symbolic links were  encountered  during
76              resolution of the path argument.
77
78       ENAMETOOLONG
79              As a result of encountering a symbolic link in resolution of the
80              path argument, the length of the  substituted  pathname  strings
81              exceeded {PATH_MAX}.
82
83
84       The following sections are informative.
85

EXAMPLES

87   Setting Read Permissions for User, Group, and Others
88       The  following  example sets read permissions for the owner, group, and
89       others.
90
91
92              #include <sys/stat.h>
93
94
95              const char *path;
96              ...
97              chmod(path, S_IRUSR|S_IRGRP|S_IROTH);
98
99   Setting Read, Write, and Execute Permissions for the Owner Only
100       The following example sets read, write, and execute permissions for the
101       owner, and no permissions for group and others.
102
103
104              #include <sys/stat.h>
105
106
107              const char *path;
108              ...
109              chmod(path, S_IRWXU);
110
111   Setting Different Permissions for Owner, Group, and Other
112       The  following  example  sets owner permissions for CHANGEFILE to read,
113       write, and execute, group permissions to read and  execute,  and  other
114       permissions to read.
115
116
117              #include <sys/stat.h>
118
119
120              #define CHANGEFILE "/etc/myfile"
121              ...
122              chmod(CHANGEFILE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
123
124   Setting and Checking File Permissions
125       The  following  example  sets the file permission bits for a file named
126       /home/cnd/mod1, then calls the stat() function to  verify  the  permis‐
127       sions.
128
129
130              #include <sys/types.h>
131              #include <sys/stat.h>
132
133
134              int status;
135              struct stat buffer
136              ...
137              chmod("home/cnd/mod1", S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH);
138              status = stat("home/cnd/mod1", &buffer;);
139

APPLICATION USAGE

141       In order to ensure that the S_ISUID and S_ISGID bits are set, an appli‐
142       cation requiring this should use stat() after a successful  chmod()  to
143       verify this.
144
145       Any  file  descriptors  currently open by any process on the file could
146       possibly become invalid if the mode of the file is changed to  a  value
147       which would deny access to that process. One situation where this could
148       occur is on a stateless file system. This behavior will not occur in  a
149       conforming environment.
150

RATIONALE

152       This  volume  of IEEE Std 1003.1-2001 specifies that the S_ISGID bit is
153       cleared by chmod() on a regular file under certain conditions. This  is
154       specified on the assumption that regular files may be executed, and the
155       system should prevent users from making executable setgid() files  per‐
156       form  with privileges that the caller does not have. On implementations
157       that support execution of other file types, the S_ISGID bit  should  be
158       cleared for those file types under the same circumstances.
159
160       Implementations  that  use the S_ISUID bit to indicate some other func‐
161       tion (for example, mandatory record locking)  on  non-executable  files
162       need  not clear this bit on writing. They should clear the bit for exe‐
163       cutable files and any other cases where the bit grants  special  powers
164       to  processes that change the file contents.  Similar comments apply to
165       the S_ISGID bit.
166

FUTURE DIRECTIONS

168       None.
169

SEE ALSO

171       chown() , mkdir() , mkfifo() , open() , stat() , statvfs() ,  the  Base
172       Definitions volume of IEEE Std 1003.1-2001, <sys/stat.h>, <sys/types.h>
173
175       Portions  of  this text are reprinted and reproduced in electronic form
176       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
177       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
178       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
179       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
180       event of any discrepancy between this version and the original IEEE and
181       The  Open Group Standard, the original IEEE and The Open Group Standard
182       is the referee document. The original Standard can be  obtained  online
183       at http://www.opengroup.org/unix/online.html .
184
185
186
187IEEE/The Open Group                  2003                             CHMOD(P)
Impressum