1CHMOD(3P) POSIX Programmer's Manual CHMOD(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 chmod - change mode of a file
13
15 #include <sys/stat.h>
16
17 int chmod(const char *path, mode_t mode);
18
19
21 The chmod() function shall change S_ISUID, S_ISGID, S_ISVTX, and the
22 file permission bits of the file named by the pathname pointed to by
23 the path argument to the corresponding bits in the mode argument. The
24 application shall ensure that the effective user ID of the process
25 matches the owner of the file or the process has appropriate privileges
26 in order to do this.
27
28 S_ISUID, S_ISGID, S_ISVTX, and the file permission bits are described
29 in <sys/stat.h>.
30
31 If the calling process does not have appropriate privileges, and if the
32 group ID of the file does not match the effective group ID or one of
33 the supplementary group IDs and if the file is a regular file, bit
34 S_ISGID (set-group-ID on execution) in the file's mode shall be cleared
35 upon successful return from chmod().
36
37 Additional implementation-defined restrictions may cause the S_ISUID
38 and S_ISGID bits in mode to be ignored.
39
40 The effect on file descriptors for files open at the time of a call to
41 chmod() is implementation-defined.
42
43 Upon successful completion, chmod() shall mark for update the st_ctime
44 field of the file.
45
47 Upon successful completion, 0 shall be returned; otherwise, -1 shall be
48 returned and errno set to indicate the error. If -1 is returned, no
49 change to the file mode occurs.
50
52 The chmod() function shall fail if:
53
54 EACCES Search permission is denied on a component of the path prefix.
55
56 ELOOP A loop exists in symbolic links encountered during resolution of
57 the path argument.
58
59 ENAMETOOLONG
60 The length of the path argument exceeds {PATH_MAX} or a pathname
61 component is longer than {NAME_MAX}.
62
63 ENOTDIR
64 A component of the path prefix is not a directory.
65
66 ENOENT A component of path does not name an existing file or path is an
67 empty string.
68
69 EPERM The effective user ID does not match the owner of the file and
70 the process does not have appropriate privileges.
71
72 EROFS The named file resides on a read-only file system.
73
74
75 The chmod() function may fail if:
76
77 EINTR A signal was caught during execution of the function.
78
79 EINVAL The value of the mode argument is invalid.
80
81 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
82 resolution of the path argument.
83
84 ENAMETOOLONG
85 As a result of encountering a symbolic link in resolution of the
86 path argument, the length of the substituted pathname strings
87 exceeded {PATH_MAX}.
88
89
90 The following sections are informative.
91
93 Setting Read Permissions for User, Group, and Others
94 The following example sets read permissions for the owner, group, and
95 others.
96
97
98 #include <sys/stat.h>
99
100
101 const char *path;
102 ...
103 chmod(path, S_IRUSR|S_IRGRP|S_IROTH);
104
105 Setting Read, Write, and Execute Permissions for the Owner Only
106 The following example sets read, write, and execute permissions for the
107 owner, and no permissions for group and others.
108
109
110 #include <sys/stat.h>
111
112
113 const char *path;
114 ...
115 chmod(path, S_IRWXU);
116
117 Setting Different Permissions for Owner, Group, and Other
118 The following example sets owner permissions for CHANGEFILE to read,
119 write, and execute, group permissions to read and execute, and other
120 permissions to read.
121
122
123 #include <sys/stat.h>
124
125
126 #define CHANGEFILE "/etc/myfile"
127 ...
128 chmod(CHANGEFILE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
129
130 Setting and Checking File Permissions
131 The following example sets the file permission bits for a file named
132 /home/cnd/mod1, then calls the stat() function to verify the permis‐
133 sions.
134
135
136 #include <sys/types.h>
137 #include <sys/stat.h>
138
139
140 int status;
141 struct stat buffer
142 ...
143 chmod("home/cnd/mod1", S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH);
144 status = stat("home/cnd/mod1", &buffer;);
145
147 In order to ensure that the S_ISUID and S_ISGID bits are set, an appli‐
148 cation requiring this should use stat() after a successful chmod() to
149 verify this.
150
151 Any file descriptors currently open by any process on the file could
152 possibly become invalid if the mode of the file is changed to a value
153 which would deny access to that process. One situation where this could
154 occur is on a stateless file system. This behavior will not occur in a
155 conforming environment.
156
158 This volume of IEEE Std 1003.1-2001 specifies that the S_ISGID bit is
159 cleared by chmod() on a regular file under certain conditions. This is
160 specified on the assumption that regular files may be executed, and the
161 system should prevent users from making executable setgid() files per‐
162 form with privileges that the caller does not have. On implementations
163 that support execution of other file types, the S_ISGID bit should be
164 cleared for those file types under the same circumstances.
165
166 Implementations that use the S_ISUID bit to indicate some other func‐
167 tion (for example, mandatory record locking) on non-executable files
168 need not clear this bit on writing. They should clear the bit for exe‐
169 cutable files and any other cases where the bit grants special powers
170 to processes that change the file contents. Similar comments apply to
171 the S_ISGID bit.
172
174 None.
175
177 chown(), mkdir(), mkfifo(), open(), stat(), statvfs(), the Base Defini‐
178 tions volume of IEEE Std 1003.1-2001, <sys/stat.h>, <sys/types.h>
179
181 Portions of this text are reprinted and reproduced in electronic form
182 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
183 -- Portable Operating System Interface (POSIX), The Open Group Base
184 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
185 Electrical and Electronics Engineers, Inc and The Open Group. In the
186 event of any discrepancy between this version and the original IEEE and
187 The Open Group Standard, the original IEEE and The Open Group Standard
188 is the referee document. The original Standard can be obtained online
189 at http://www.opengroup.org/unix/online.html .
190
191
192
193IEEE/The Open Group 2003 CHMOD(3P)