2 .\" Copyright (c) 2005, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright 1989 AT&T.
4 .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 .\" This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH CHMOD 2 "Sep 12, 2005"
14 chmod, fchmod \- change access permission mode of file
18 #include <sys/types.h>
21 \fBint\fR \fBchmod\fR(\fBconst char *\fR\fIpath\fR, \fBmode_t\fR \fImode\fR);
26 \fBint\fR \fBfchmod\fR(\fBint\fR \fIfildes\fR, \fBmode_t\fR \fImode\fR);
32 The \fBchmod()\fR and \fBfchmod()\fR functions set the access permission
33 portion of the mode of the file whose name is given by \fIpath\fR or referenced
34 by the open file descriptor \fIfildes\fR to the bit pattern contained in
35 \fImode\fR. Access permission bits are interpreted as follows:
42 \fBS_ISUID\fR 04000 Set user ID on execution.
43 \fBS_ISGID\fR 020#0 T{
44 Set group ID on execution if # is \fB7\fR, \fB5\fR, \fB3\fR, or \fB1\fR. Enable mandatory file/record locking if # is \fB6\fR, \fB4\fR, \fB2\fR, or \fB0\fR.
46 \fBS_ISVTX\fR 01000 Sticky bit.
47 \fBS_IRWXU\fR 00700 Read, write, execute by owner.
48 \fBS_IRUSR\fR 00400 Read by owner.
49 \fBS_IWUSR\fR 00200 Write by owner.
50 \fBS_IXUSR\fR 00100 T{
51 Execute (search if a directory) by owner.
53 \fBS_IRWXG\fR 00070 Read, write, execute by group.
54 \fBS_IRGRP\fR 00040 Read by group.
55 \fBS_IWGRP\fR 00020 Write by group.
56 \fBS_IXGRP\fR 00010 Execute by group.
57 \fBS_IRWXO\fR 00007 Read, write, execute (search) by others.
58 \fBS_IROTH\fR 00004 Read by others.
59 \fBS_IWOTH\fR 00002 Write by others.
60 \fBS_IXOTH\fR 00001 Execute by others.
65 Modes are constructed by the bitwise OR operation of the access permission
69 The effective user ID of the process must match the owner of the file or the
70 process must have the appropriate privilege to change the mode of a file.
73 If the process is not a privileged process and the file is not a directory,
74 mode bit 01000 (save text image on execution) is cleared.
77 If neither the process is privileged nor the file's group is a member of the
78 process's supplementary group list, and the effective group ID of the process
79 does not match the group ID of the file, mode bit 02000 (set group ID on
80 execution) is cleared.
83 If a directory is writable and has \fBS_ISVTX\fR (the sticky bit) set, files
84 within that directory can be removed or renamed only if one or more of the
85 following is true (see \fBunlink\fR(2) and \fBrename\fR(2)):
90 the user owns the file
96 the user owns the directory
102 the file is writable by the user
108 the user is a privileged user
112 If a regular file is not executable and has \fBS_ISVTX\fR set, the file is
113 assumed to be a swap file. In this case, the system's page cache will not be
114 used to hold the file's data. If the \fBS_ISVTX\fR bit is set on any other
115 file, the results are unspecified.
118 If a directory has the set group ID bit set, a given file created within that
119 directory will have the same group ID as the directory. Otherwise, the newly
120 created file's group ID will be set to the effective group ID of the creating
124 If the mode bit 02000 (set group ID on execution) is set and the mode bit 00010
125 (execute or search by group) is not set, mandatory file/record locking will
126 exist on a regular file, possibly affecting future calls to \fBopen\fR(2),
127 \fBcreat\fR(2), \fBread\fR(2), and \fBwrite\fR(2) on this file.
130 If \fIfildes\fR references a shared memory object, \fBfchmod()\fR need only
131 affect the \fBS_IRUSR\fR, \fBS_IRGRP\fR, \fBS_IROTH\fR, \fBS_IWUSR\fR,
132 \fBS_IWGRP\fR, \fBS_IWOTH\fR, \fBS_IXUSR\fR, \fBS_IXGRP\fR, and \fBS_IXOTH\fR
133 file permission bits.
136 If \fIfildes\fR refers to a socket, \fBfchmod()\fR does not fail but no action
140 If \fIfildes\fR refers to a stream that is attached to an object in the file
141 system name space with \fBfattach\fR(3C), the \fBfchmod()\fR call performs no
142 action and returns successfully.
145 Upon successful completion, \fBchmod()\fR and \fBfchmod()\fR mark for update
146 the \fBst_ctime\fR field of the file.
150 Upon successful completion, \fB0\fR is returned. Otherwise, \fB\(mi1\fR is
151 returned, the file mode is unchanged, and \fBerrno\fR is set to indicate the
156 The \fBchmod()\fR and \fBfchmod()\fR functions will fail if:
163 An I/O error occurred while reading from or writing to the file system.
172 The effective user ID does not match the owner of the file and the process does
173 not have appropriate privilege.
175 The {\fBPRIV_FILE_OWNER\fR} privilege overrides constraints on ownership when
176 changing permissions on a file.
178 The {\fBPRIV_FILE_SETID\fR} privilege overrides constraints on ownership when
179 adding the setuid or setgid bits to an executable file or a directory. When
180 adding the setuid bit to a root owned executable, additional restrictions
181 apply. See \fBprivileges\fR(5).
186 The \fBchmod()\fR function will fail if:
193 Search permission is denied on a component of the path prefix of \fIpath\fR.
194 The privilege {\fBFILE_DAC_SEARCH\fR} overrides file permissions restrictions
204 The \fIpath\fR argument points to an illegal address.
213 A loop exists in symbolic links encountered during the resolution of the
220 \fB\fBENAMETOOLONG\fR\fR
223 The length of the \fIpath\fR argument exceeds \fBPATH_MAX\fR, or the length of
224 a \fIpath\fR component exceeds \fBNAME_MAX\fR while \fB_POSIX_NO_TRUNC\fR is in
234 Either a component of the path prefix or the file referred to by \fIpath\fR
235 does not exist or is a null pathname.
244 The \fIfildes\fR argument points to a remote machine and the link to that
245 machine is no longer active.
254 A component of the prefix of \fIpath\fR is not a directory.
263 The file referred to by \fIpath\fR resides on a read-only file system.
268 The \fBfchmod()\fR function will fail if:
275 The \fIfildes\fR argument is not an open file descriptor
284 The \fIpath\fR argument points to a remote machine and the link to that machine
294 The file referred to by \fIfildes\fR resides on a read-only file system.
299 The \fBchmod()\fR and \fBfchmod()\fR functions may fail if:
306 A signal was caught during execution of the function.
315 The value of the \fImode\fR argument is invalid.
320 The \fBchmod()\fR function may fail if:
327 More than {\fBSYMLOOP_MAX\fR} symbolic links were encountered during the
328 resolution of the \fIpath\fR argument.
334 \fB\fBENAMETOOLONG\fR\fR
337 As a result of encountering a symbolic link in resolution of the\fIpath\fR
338 argument, the length of the substituted pathname strings exceeds
344 The \fBfchmod()\fR function may fail if:
351 The \fIfildes\fR argument refers to a pipe and the system disallows execution
352 of this function on a pipe.
357 \fBExample 1 \fRSet Read Permissions for User, Group, and Others
360 The following example sets read permissions for the owner, group, and others.
365 #include <sys/stat.h>
368 chmod(path, S_IRUSR|S_IRGRP|S_IROTH);
373 \fBExample 2 \fRSet Read, Write, and Execute Permissions for the Owner Only
376 The following example sets read, write, and execute permissions for the owner,
377 and no permissions for group and others.
382 #include <sys/stat.h>
385 chmod(path, S_IRWXU);
390 \fBExample 3 \fRSet Different Permissions for Owner, Group, and Other
393 The following example sets owner permissions for CHANGEFILE to read, write, and
394 execute, group permissions to read and execute, and other permissions to read.
399 #include <sys/stat.h>
400 #define CHANGEFILE "/etc/myfile"
402 chmod(CHANGEFILE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
407 \fBExample 4 \fRSet and Checking File Permissions
410 The following example sets the file permission bits for a file named
411 \fB/home/cnd/mod1\fR, then calls the \fBstat\fR(2) function to verify the
417 #include <sys/types.h>
418 #include <sys/stat.h>
422 chmod("home/cnd/mod1", S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH);
423 status = stat("home/cnd/mod1", &buffer;);
430 If \fBchmod()\fR or \fBfchmod()\fR is used to change the file group owner
431 permissions on a file with non-trivial ACL entries, only the ACL mask is set to
432 the new permissions and the group owner permission bits in the file's mode
433 field (defined in \fBmknod\fR(2)) are unchanged. A non-trivial ACL entry is
434 one whose meaning cannot be represented in the file's mode field alone. The new
435 ACL mask permissions might change the effective permissions for additional
436 users and groups that have ACL entries on the file.
440 See \fBattributes\fR(5) for descriptions of the following attributes:
448 ATTRIBUTE TYPE ATTRIBUTE VALUE
450 Interface Stability Standard
452 MT-Level Async-Signal-Safe
458 \fBchmod\fR(1), \fBchown\fR(2), \fBcreat\fR(2), \fBfcntl\fR(2), \fBmknod\fR(2),
459 \fBopen\fR(2), \fBread\fR(2), \fBrename\fR(2), \fBstat\fR(2), \fBwrite\fR(2),
460 \fBfattach\fR(3C), \fBmkfifo\fR(3C), \fBstat.h\fR(3HEAD), \fBattributes\fR(5),
461 \fBprivileges\fR(5), \fBstandards\fR(5)
464 \fIProgramming Interfaces Guide\fR