logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

chmod.3p (10036B)


  1. '\" et
  2. .TH CHMOD "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. chmod, fchmodat
  12. \(em change mode of a file
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <sys/stat.h>
  17. .P
  18. int chmod(const char *\fIpath\fP, mode_t \fImode\fP);
  19. .P
  20. #include <fcntl.h>
  21. .P
  22. int fchmodat(int \fIfd\fP, const char *\fIpath\fP, mode_t \fImode\fP, int \fIflag\fP);
  23. .fi
  24. .SH DESCRIPTION
  25. The
  26. \fIchmod\fR()
  27. function shall change S_ISUID, S_ISGID,
  28. S_ISVTX,
  29. and the file permission bits of the file named by the pathname pointed
  30. to by the
  31. .IR path
  32. argument to the corresponding bits in the
  33. .IR mode
  34. argument. The application shall ensure that the effective user ID
  35. of the process matches the owner of the file or the process has
  36. appropriate privileges in order to do this.
  37. .P
  38. S_ISUID, S_ISGID,
  39. S_ISVTX,
  40. and the file permission bits
  41. are described in
  42. .IR <sys/stat.h> .
  43. .P
  44. If the calling process does not have appropriate privileges, and if the
  45. group ID of the file does not match the effective group ID or one of
  46. the supplementary group IDs and if the file is a regular file, bit
  47. S_ISGID (set-group-ID on execution) in the file's mode shall be cleared
  48. upon successful return from
  49. \fIchmod\fR().
  50. .P
  51. Additional implementation-defined restrictions may cause the S_ISUID
  52. and S_ISGID bits in
  53. .IR mode
  54. to be ignored.
  55. .P
  56. Upon successful completion,
  57. \fIchmod\fR()
  58. shall mark for update the last file status change timestamp of the file.
  59. .P
  60. The
  61. \fIfchmodat\fR()
  62. function shall be equivalent to the
  63. \fIchmod\fR()
  64. function except in the case where
  65. .IR path
  66. specifies a relative path. In this case the file to be changed is
  67. determined relative to the directory associated with the file
  68. descriptor
  69. .IR fd
  70. instead of the current working directory. If the access mode of the
  71. open file description associated with the file descriptor is not
  72. O_SEARCH, the function shall check whether directory searches
  73. are permitted using the current permissions of the directory
  74. underlying the file descriptor. If the access mode is
  75. O_SEARCH, the function shall not perform the check.
  76. .P
  77. Values for
  78. .IR flag
  79. are constructed by a bitwise-inclusive OR of flags from the following
  80. list, defined in
  81. .IR <fcntl.h> :
  82. .IP AT_SYMLINK_NOFOLLOW 6
  83. .br
  84. If
  85. .IR path
  86. names a symbolic link, then the mode of the symbolic link is changed.
  87. .P
  88. If
  89. \fIfchmodat\fR()
  90. is passed the special value AT_FDCWD in the
  91. .IR fd
  92. parameter, the current working directory shall be used. If also
  93. .IR flag
  94. is zero, the behavior shall be identical to a call to
  95. \fIchmod\fR().
  96. .SH "RETURN VALUE"
  97. Upon successful completion, these functions shall return 0.
  98. Otherwise, these functions shall return \-1 and set
  99. .IR errno
  100. to indicate the error. If \-1 is returned, no change to the
  101. file mode occurs.
  102. .SH ERRORS
  103. These functions shall fail if:
  104. .TP
  105. .BR EACCES
  106. Search permission is denied on a component of the path prefix.
  107. .TP
  108. .BR ELOOP
  109. A loop exists in symbolic links encountered during resolution of the
  110. .IR path
  111. argument.
  112. .TP
  113. .BR ENAMETOOLONG
  114. .br
  115. The length of a component of a pathname is longer than
  116. {NAME_MAX}.
  117. .TP
  118. .BR ENOENT
  119. A component of
  120. .IR path
  121. does not name an existing file or
  122. .IR path
  123. is an empty string.
  124. .TP
  125. .BR ENOTDIR
  126. A component of the path prefix names an existing file that is neither
  127. a directory nor a symbolic link to a directory, or the
  128. .IR path
  129. argument contains at least one non-\c
  130. <slash>
  131. character and ends with one or more trailing
  132. <slash>
  133. characters and the last pathname component names an existing file
  134. that is neither a directory nor a symbolic link to a directory.
  135. .TP
  136. .BR EPERM
  137. The effective user ID does not match the owner of the file and the
  138. process does not have appropriate privileges.
  139. .TP
  140. .BR EROFS
  141. The named file resides on a read-only file system.
  142. .P
  143. The
  144. \fIfchmodat\fR()
  145. function shall fail if:
  146. .TP
  147. .BR EACCES
  148. The access mode of the open file description associated with
  149. .IR fd
  150. is not O_SEARCH and the permissions of the directory underlying
  151. .IR fd
  152. do not permit directory searches.
  153. .TP
  154. .BR EBADF
  155. The
  156. .IR path
  157. argument does not specify an absolute path and the
  158. .IR fd
  159. argument is neither AT_FDCWD nor a valid file descriptor open
  160. for reading or searching.
  161. .TP
  162. .BR ENOTDIR
  163. The
  164. .IR path
  165. argument is not an absolute path and
  166. .IR fd
  167. is a file descriptor associated with a non-directory file.
  168. .P
  169. These functions may fail if:
  170. .TP
  171. .BR EINTR
  172. A signal was caught during execution of the function.
  173. .TP
  174. .BR EINVAL
  175. The value of the
  176. .IR mode
  177. argument is invalid.
  178. .TP
  179. .BR ELOOP
  180. More than
  181. {SYMLOOP_MAX}
  182. symbolic links were encountered during resolution of the
  183. .IR path
  184. argument.
  185. .TP
  186. .BR ENAMETOOLONG
  187. .br
  188. The length of a pathname exceeds
  189. {PATH_MAX},
  190. or pathname resolution of a symbolic link produced an intermediate
  191. result with a length that exceeds
  192. {PATH_MAX}.
  193. .P
  194. The
  195. \fIfchmodat\fR()
  196. function may fail if:
  197. .TP
  198. .BR EINVAL
  199. The value of the
  200. .IR flag
  201. argument is invalid.
  202. .TP
  203. .BR EOPNOTSUPP
  204. The AT_SYMLINK_NOFOLLOW bit is set in the
  205. .IR flag
  206. argument,
  207. .IR path
  208. names a symbolic link, and the system does not support changing the
  209. mode of a symbolic link.
  210. .br
  211. .LP
  212. .IR "The following sections are informative."
  213. .SH EXAMPLES
  214. .SS "Setting Read Permissions for User, Group, and Others"
  215. .P
  216. The following example sets read permissions for the owner, group, and
  217. others.
  218. .sp
  219. .RS 4
  220. .nf
  221. #include <sys/stat.h>
  222. .P
  223. const char *path;
  224. \&...
  225. chmod(path, S_IRUSR|S_IRGRP|S_IROTH);
  226. .fi
  227. .P
  228. .RE
  229. .SS "Setting Read, Write, and Execute Permissions for the Owner Only"
  230. .P
  231. The following example sets read, write, and execute permissions for the
  232. owner, and no permissions for group and others.
  233. .sp
  234. .RS 4
  235. .nf
  236. #include <sys/stat.h>
  237. .P
  238. const char *path;
  239. \&...
  240. chmod(path, S_IRWXU);
  241. .fi
  242. .P
  243. .RE
  244. .SS "Setting Different Permissions for Owner, Group, and Other"
  245. .P
  246. The following example sets owner permissions for CHANGEFILE to read,
  247. write, and execute, group permissions to read and execute, and other
  248. permissions to read.
  249. .sp
  250. .RS 4
  251. .nf
  252. #include <sys/stat.h>
  253. .P
  254. #define CHANGEFILE "/etc/myfile"
  255. \&...
  256. chmod(CHANGEFILE, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
  257. .fi
  258. .P
  259. .RE
  260. .SS "Setting and Checking File Permissions"
  261. .P
  262. The following example sets the file permission bits for a file named
  263. .BR /home/cnd/mod1 ,
  264. then calls the
  265. \fIstat\fR()
  266. function to verify the permissions.
  267. .sp
  268. .RS 4
  269. .nf
  270. #include <sys/types.h>
  271. #include <sys/stat.h>
  272. .P
  273. int status;
  274. struct stat buffer
  275. \&...
  276. chmod("/home/cnd/mod1", S_IRWXU|S_IRWXG|S_IROTH|S_IWOTH);
  277. status = stat("/home/cnd/mod1", &buffer);
  278. .fi
  279. .P
  280. .RE
  281. .SH "APPLICATION USAGE"
  282. In order to ensure that the S_ISUID and S_ISGID
  283. bits are set, an application requiring this should use
  284. \fIstat\fR()
  285. after a successful
  286. \fIchmod\fR()
  287. to verify this.
  288. .P
  289. Any file descriptors currently open by any process on the file could
  290. possibly become invalid if the mode of the file is changed to a value
  291. which would deny access to that process. One situation where this could
  292. occur is on a stateless file system. This behavior will not occur in a
  293. conforming environment.
  294. .SH RATIONALE
  295. This volume of POSIX.1\(hy2017 specifies that the S_ISGID bit is cleared by
  296. \fIchmod\fR()
  297. on a regular file under certain conditions. This is specified on the
  298. assumption that regular files may be executed, and the system should
  299. prevent users from making executable
  300. \fIsetgid\fR()
  301. files perform with privileges that the caller does not have. On
  302. implementations that support execution of other file types, the S_ISGID
  303. bit should be cleared for those file types under the same
  304. circumstances.
  305. .P
  306. Implementations that use the S_ISUID bit to indicate some other
  307. function (for example, mandatory record locking) on non-executable
  308. files need not clear this bit on writing. They should clear the bit
  309. for executable files and any other cases where the bit grants special
  310. powers to processes that change the file contents. Similar comments
  311. apply to the S_ISGID bit.
  312. .P
  313. The purpose of the
  314. \fIfchmodat\fR()
  315. function is to enable changing the mode of files in directories other
  316. than the current working directory without exposure to race conditions.
  317. Any part of the path of a file could be changed in parallel to a call
  318. to
  319. \fIchmod\fR(),
  320. resulting in unspecified behavior. By opening a file descriptor for
  321. the target directory and using the
  322. \fIfchmodat\fR()
  323. function it can be guaranteed that the changed file is located relative
  324. to the desired directory. Some implementations might allow changing
  325. the mode of symbolic links. This is not supported by the interfaces in
  326. the POSIX specification. Systems with such support provide an
  327. interface named
  328. .IR lchmod (\|).
  329. To support such implementations
  330. \fIfchmodat\fR()
  331. has a
  332. .IR flag
  333. parameter.
  334. .SH "FUTURE DIRECTIONS"
  335. None.
  336. .SH "SEE ALSO"
  337. .IR "\fIaccess\fR\^(\|)",
  338. .IR "\fIchown\fR\^(\|)",
  339. .IR "\fIexec\fR\^",
  340. .IR "\fIfstatat\fR\^(\|)",
  341. .IR "\fIfstatvfs\fR\^(\|)",
  342. .IR "\fImkdir\fR\^(\|)",
  343. .IR "\fImkfifo\fR\^(\|)",
  344. .IR "\fImknod\fR\^(\|)",
  345. .IR "\fIopen\fR\^(\|)"
  346. .P
  347. The Base Definitions volume of POSIX.1\(hy2017,
  348. .IR "\fB<fcntl.h>\fP",
  349. .IR "\fB<sys_stat.h>\fP",
  350. .IR "\fB<sys_types.h>\fP"
  351. .\"
  352. .SH COPYRIGHT
  353. Portions of this text are reprinted and reproduced in electronic form
  354. from IEEE Std 1003.1-2017, Standard for Information Technology
  355. -- Portable Operating System Interface (POSIX), The Open Group Base
  356. Specifications Issue 7, 2018 Edition,
  357. Copyright (C) 2018 by the Institute of
  358. Electrical and Electronics Engineers, Inc and The Open Group.
  359. In the event of any discrepancy between this version and the original IEEE and
  360. The Open Group Standard, the original IEEE and The Open Group Standard
  361. is the referee document. The original Standard can be obtained online at
  362. http://www.opengroup.org/unix/online.html .
  363. .PP
  364. Any typographical or formatting errors that appear
  365. in this page are most likely
  366. to have been introduced during the conversion of the source files to
  367. man page format. To report such errors, see
  368. https://www.kernel.org/doc/man-pages/reporting_bugs.html .