CHMOD(2) System Calls CHMOD(2) NNAAMMEE chmod, fchmod, fchmodat - change access permission mode of file SSYYNNOOPPSSIISS #include #include iinntt cchhmmoodd(ccoonnsstt cchhaarr **_p_a_t_h, mmooddee__tt _m_o_d_e); iinntt ffcchhmmoodd(iinntt _f_i_l_d_e_s, mmooddee__tt _m_o_d_e); iinntt ffcchhmmooddaatt(iinntt _f_i_l_d_e_s, ccoonnsstt cchhaarr **_p_a_t_h, mmooddee__tt _m_o_d_e, iinntt _f_l_a_g); DDEESSCCRRIIPPTTIIOONN The cchhmmoodd(()), ffcchhmmoodd(()), and ffcchhmmooddaatt(()) functions set the access permission portion of the mode of the file whose name is given by _p_a_t_h or referenced by the open file descriptor _f_i_l_d_e_s to the bit pattern contained in _m_o_d_e. Access permission bits are interpreted as follows: SS__IISSUUIIDD 04000 Set user ID on execution. SS__IISSGGIIDD 020#0 Set group ID on execution if # is 77, 55, 33, or 11. Enable mandatory file/record locking if # is 66, 44, 22, or 00. SS__IISSVVTTXX 01000 Sticky bit. SS__IIRRWWXXUU 00700 Read, write, execute by owner. SS__IIRRUUSSRR 00400 Read by owner. SS__IIWWUUSSRR 00200 Write by owner. SS__IIXXUUSSRR 00100 Execute (search if a directory) by owner. SS__IIRRWWXXGG 00070 Read, write, execute by group. SS__IIRRGGRRPP 00040 Read by group. SS__IIWWGGRRPP 00020 Write by group. SS__IIXXGGRRPP 00010 Execute by group. SS__IIRRWWXXOO 00007 Read, write, execute (search) by others. SS__IIRROOTTHH 00004 Read by others. SS__IIWWOOTTHH 00002 Write by others. SS__IIXXOOTTHH 00001 Execute by others. Modes are constructed by the bitwise OR operation of the access permission bits. The effective user ID of the process must match the owner of the file or the process must have the appropriate privilege to change the mode of a file. If the process is not a privileged process and the file is not a directory, mode bit 01000 (save text image on execution) is cleared. If neither the process is privileged nor the file's group is a member of the process's supplementary group list, and the effective group ID of the process does not match the group ID of the file, mode bit 02000 (set group ID on execution) is cleared. If a directory is writable and has SS__IISSVVTTXX (the sticky bit) set, files within that directory can be removed or renamed only if one or more of the following is true (see uunnlliinnkk(2) and rreennaammee(2)): o the user owns the file o the user owns the directory o the file is writable by the user o the user is a privileged user If a regular file is not executable and has SS__IISSVVTTXX set, the file is assumed to be a swap file. In this case, the system's page cache will not be used to hold the file's data. If the SS__IISSVVTTXX bit is set on any other file, the results are unspecified. If a directory has the set group ID bit set, a given file created within that directory will have the same group ID as the directory. Otherwise, the newly created file's group ID will be set to the effective group ID of the creating process. If the mode bit 02000 (set group ID on execution) is set and the mode bit 00010 (execute or search by group) is not set, mandatory file/record locking will exist on a regular file, possibly affecting future calls to ooppeenn(2), ccrreeaatt(2), rreeaadd(2), and wwrriittee(2) on this file. If _f_i_l_d_e_s references a shared memory object, ffcchhmmoodd(()) need only affect the SS__IIRRUUSSRR, SS__IIRRGGRRPP, SS__IIRROOTTHH, SS__IIWWUUSSRR, SS__IIWWGGRRPP, SS__IIWWOOTTHH, SS__IIXXUUSSRR, SS__IIXXGGRRPP, and SS__IIXXOOTTHH file permission bits. If _f_i_l_d_e_s refers to a socket, ffcchhmmoodd(()) does not fail but no action is taken. If _f_i_l_d_e_s refers to a stream that is attached to an object in the file system name space with ffaattttaacchh(3C), the ffcchhmmoodd(()) call performs no action and returns successfully. The ffcchhmmooddaatt(()) function behaves similarly to cchhmmoodd(()), except when _p_a_t_h is a relative path, it is resolved relative to the directory specified by _f_i_l_e_d_e_s. If ffiilleeddeess has the value AATT__FFDDCCWWDD, then ppaatthh will be resolved relative to the current working directory. The argument _f_l_a_g should be zero, but may include the value AATT__SSYYMMLLIINNKK__NNOOFFOOLLLLOOWW, which indicates that if _p_a_t_h refers to a symbolic link, then permissions should be changed on the symbolic link itself. However, changing permissions of symbolic links is not supported on illumos, and will result in an error. Upon successful completion, cchhmmoodd(()), ffcchhmmoodd(()), ffcchhmmooddaatt(()) mark for update the sstt__ccttiimmee field of the file. RREETTUURRNN VVAALLUUEESS Upon successful completion, 00 is returned. Otherwise, --11 is returned, the file mode is unchanged, and eerrrrnnoo is set to indicate the error. EERRRROORRSS The cchhmmoodd(()), ffcchhmmoodd(()), and ffcchhmmooddaatt(()) functions will fail if: EEIIOO An I/O error occurred while reading from or writing to the file system. EEPPEERRMM The effective user ID does not match the owner of the file and the process does not have appropriate privilege. The {PPRRIIVV__FFIILLEE__OOWWNNEERR} privilege overrides constraints on ownership when changing permissions on a file. The {PPRRIIVV__FFIILLEE__SSEETTIIDD} privilege overrides constraints on ownership when adding the setuid or setgid bits to an executable file or a directory. When adding the setuid bit to a root owned executable, additional restrictions apply. See pprriivviilleeggeess(5). The cchhmmoodd(()) and ffcchhmmooddaatt(()) functions will fail if: EEAACCCCEESS Search permission is denied on a component of the path prefix of _p_a_t_h and for ffcchhmmooddaatt(()), ffiilleeddeess was not opened with OO__SSEEAARRCCHH requested. The privilege {FFIILLEE__DDAACC__SSEEAARRCCHH} overrides file permissions restrictions in that case. EEFFAAUULLTT The _p_a_t_h argument points to an illegal address. EELLOOOOPP A loop exists in symbolic links encountered during the resolution of the _p_a_t_h argument. EENNAAMMEETTOOOOLLOONNGG The length of the _p_a_t_h argument exceeds PPAATTHH__MMAAXX, or the length of a _p_a_t_h component exceeds NNAAMMEE__MMAAXX while __PPOOSSIIXX__NNOO__TTRRUUNNCC is in effect. EENNOOEENNTT Either a component of the path prefix or the file referred to by _p_a_t_h does not exist or is a null pathname. EENNOOLLIINNKK The _f_i_l_d_e_s argument points to a remote machine and the link to that machine is no longer active. EENNOOTTDDIIRR A component of the prefix of _p_a_t_h is not a directory. EERROOFFSS The file referred to by _p_a_t_h resides on a read-only file system. The ffcchhmmoodd(()) function will fail if: EEBBAADDFF The _f_i_l_d_e_s argument is not an open file descriptor EENNOOLLIINNKK The _p_a_t_h argument points to a remote machine and the link to that machine is no longer active. EERROOFFSS The file referred to by _f_i_l_d_e_s resides on a read-only file system. The cchhmmoodd(()) and ffcchhmmoodd(()) functions may fail if: EEIINNTTRR A signal was caught during execution of the function. EEIINNVVAALL The value of the _m_o_d_e argument is invalid. The ffcchhmmooddaatt(()) will fail if: EEBBAADDFF The argument _p_a_t_h is a relative path and _f_i_l_e_d_e_s is not an open file descriptor or the value AATT__FFDDCCWWDD. EEIINNVVAALL The argument _f_l_a_g_s has a non-zero value other than AATT__SSYYMMLLIINNKK__NNOOFFOOLLLLOOWW. EENNOOTTDDIIRR The argument _p_a_t_h is a relative path and _f_i_l_e_d_e_s is a valid file descriptor which does not refer to a file. EEOOPPNNOOTTSSUUPPPP The AATT__SSYYMMLLIINNKK__NNOOFFOOLLLLOOWW bit is set in the _f_l_a_g_s argument. The cchhmmoodd(()) and ffcchhmmooddaatt(()) functions may fail if: EELLOOOOPP More than {SSYYMMLLOOOOPP__MMAAXX} symbolic links were encountered during the resolution of the _p_a_t_h argument. EENNAAMMEETTOOOOLLOONNGG As a result of encountering a symbolic link in resolution of the _p_a_t_h argument, the length of the substituted pathname strings exceeds {PPAATTHH__MMAAXX}. The ffcchhmmoodd(()) function may fail if: EEIINNVVAALL The _f_i_l_d_e_s argument refers to a pipe and the system disallows execution of this function on a pipe. EEXXAAMMPPLLEESS EExxaammppllee 11 Set Read Permissions for User, Group, and Others The following example sets read permissions for the owner, group, and others. #include const char *path; ... chmod(path, S_IRUSR|S_IRGRP|S_IROTH); EExxaammppllee 22 Set Read, Write, and Execute Permissions for the Owner Only The following example sets read, write, and execute permissions for the owner, and no permissions for group and others. #include const char *path; ... chmod(path, S_IRWXU); EExxaammppllee 33 Set Different Permissions for Owner, Group, and Other The following example sets owner permissions for CHANGEFILE to read, write, and execute, group permissions to read and execute, and other permissions to read. #include #define CHANGEFILE "/etc/myfile" ... chmod(CHANGEFILE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH); EExxaammppllee 44 Set and Checking File Permissions The following example sets the file permission bits for a file named //hhoommee//ccnndd//mmoodd11, then calls the ssttaatt(2) function to verify the permissions. #include #include int status; struct stat buffer ... chmod("home/cnd/mod1", S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH); status = stat("home/cnd/mod1", &buffer;); UUSSAAGGEE If cchhmmoodd(()) or ffcchhmmoodd(()) is used to change the file group owner permissions on a file with non-trivial ACL entries, only the ACL mask is set to the new permissions and the group owner permission bits in the file's mode field (defined in mmkknnoodd(2)) are unchanged. A non- trivial ACL entry is one whose meaning cannot be represented in the file's mode field alone. The new ACL mask permissions might change the effective permissions for additional users and groups that have ACL entries on the file. AATTTTRRIIBBUUTTEESS See aattttrriibbuutteess(5) for descriptions of the following attributes: +--------------------+-------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +--------------------+-------------------+ |Interface Stability | Standard | +--------------------+-------------------+ |MT-Level | Async-Signal-Safe | +--------------------+-------------------+ SSEEEE AALLSSOO cchhmmoodd(1), cchhoowwnn(2), ccrreeaatt(2), ffccnnttll(2), mmkknnoodd(2), ooppeenn(2), rreeaadd(2), rreennaammee(2), ssttaatt(2), wwrriittee(2), ffaattttaacchh(3C), mmkkffiiffoo(3C), ssttaatt..hh(3HEAD), aattttrriibbuutteess(5), pprriivviilleeggeess(5), ssttaannddaarrddss(5) _P_r_o_g_r_a_m_m_i_n_g _I_n_t_e_r_f_a_c_e_s _G_u_i_d_e December 22, 2014 CHMOD(2)