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

unlink.3p (12175B)


  1. '\" et
  2. .TH UNLINK "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. unlink, unlinkat
  12. \(em remove a directory entry
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <unistd.h>
  17. .P
  18. int unlink(const char *\fIpath\fP);
  19. .P
  20. #include <fcntl.h>
  21. .P
  22. int unlinkat(int \fIfd\fP, const char *\fIpath\fP, int \fIflag\fP);
  23. .fi
  24. .SH DESCRIPTION
  25. The
  26. \fIunlink\fR()
  27. function shall remove a link to a file. If
  28. .IR path
  29. names a symbolic link,
  30. \fIunlink\fR()
  31. shall remove the symbolic link named by
  32. .IR path
  33. and shall not affect any file or directory named by the contents of the
  34. symbolic link. Otherwise,
  35. \fIunlink\fR()
  36. shall remove the link named by the pathname pointed to by
  37. .IR path
  38. and shall decrement the link count of the file referenced by the link.
  39. .P
  40. When the file's link count becomes 0 and no process has the file open,
  41. the space occupied by the file shall be freed and the file shall no
  42. longer be accessible. If one or more processes have the file open when
  43. the last link is removed, the link shall be removed before
  44. \fIunlink\fR()
  45. returns, but the removal of the file contents shall be postponed until
  46. all references to the file are closed.
  47. .P
  48. The
  49. .IR path
  50. argument shall not name a directory unless the process has appropriate
  51. privileges and the implementation supports using
  52. \fIunlink\fR()
  53. on directories.
  54. .P
  55. Upon successful completion,
  56. \fIunlink\fR()
  57. shall mark for update the last data modification and last file status
  58. change timestamps of the parent directory. Also, if the file's link
  59. count is not 0, the last file status change timestamp of the file shall
  60. be marked for update.
  61. .P
  62. The
  63. \fIunlinkat\fR()
  64. function shall be equivalent to the
  65. \fIunlink\fR()
  66. or
  67. \fIrmdir\fR()
  68. function except in the case where
  69. .IR path
  70. specifies a relative path. In this case the directory entry to be
  71. removed is determined relative to the directory associated with the
  72. file descriptor
  73. .IR fd
  74. instead of the current working directory. If the access mode of the
  75. open file description associated with the file descriptor is not
  76. O_SEARCH, the function shall check whether directory searches are
  77. permitted using the current permissions of the directory underlying
  78. the file descriptor. If the access mode is O_SEARCH, the function
  79. shall not perform the check.
  80. .P
  81. Values for
  82. .IR flag
  83. are constructed by a bitwise-inclusive OR of flags from the following
  84. list, defined in
  85. .IR <fcntl.h> :
  86. .IP AT_REMOVEDIR 6
  87. .br
  88. Remove the directory entry specified by
  89. .IR fd
  90. and
  91. .IR path
  92. as a directory, not a normal file.
  93. .P
  94. If
  95. \fIunlinkat\fR()
  96. is passed the special value AT_FDCWD in the
  97. .IR fd
  98. parameter, the current working directory shall be used and the behavior
  99. shall be identical to a call to
  100. \fIunlink\fR()
  101. or
  102. \fIrmdir\fR()
  103. respectively, depending on whether or not the AT_REMOVEDIR bit is set in
  104. .IR flag .
  105. .SH "RETURN VALUE"
  106. Upon successful completion, these functions shall return 0. Otherwise,
  107. these functions shall return \-1 and set
  108. .IR errno
  109. to indicate the error. If \-1 is returned, the named file shall not
  110. be changed.
  111. .SH ERRORS
  112. These functions shall fail and shall not unlink the file if:
  113. .TP
  114. .BR EACCES
  115. Search permission is denied for a component of the path prefix, or
  116. write permission is denied on the directory containing the directory
  117. entry to be removed.
  118. .TP
  119. .BR EBUSY
  120. The file named by the
  121. .IR path
  122. argument cannot be unlinked because it is being used by the system or
  123. another process and the implementation considers this an error.
  124. .TP
  125. .BR ELOOP
  126. A loop exists in symbolic links encountered during resolution of the
  127. .IR path
  128. argument.
  129. .TP
  130. .BR ENAMETOOLONG
  131. .br
  132. The length of a component of a pathname is longer than
  133. {NAME_MAX}.
  134. .TP
  135. .BR ENOENT
  136. A component of
  137. .IR path
  138. does not name an existing file or
  139. .IR path
  140. is an empty string.
  141. .TP
  142. .BR ENOTDIR
  143. A component of the path prefix names an existing file that is neither
  144. a directory nor a symbolic link to a directory, or the
  145. .IR path
  146. argument contains at least one non-\c
  147. <slash>
  148. character and ends with one or more trailing
  149. <slash>
  150. characters and the last pathname component names an existing file
  151. that is neither a directory nor a symbolic link to a directory.
  152. .TP
  153. .BR EPERM
  154. The file named by
  155. .IR path
  156. is a directory, and either the calling process does not have
  157. appropriate privileges, or the implementation prohibits using
  158. \fIunlink\fR()
  159. on directories.
  160. .TP
  161. .BR EPERM " or " EACCES
  162. .br
  163. The S_ISVTX flag is set on the directory containing the file referred
  164. to by the
  165. .IR path
  166. argument and the process does not satisfy the criteria specified in the Base Definitions volume of POSIX.1\(hy2017,
  167. .IR "Section 4.3" ", " "Directory Protection".
  168. .TP
  169. .BR EROFS
  170. The directory entry to be unlinked is part of a read-only file system.
  171. .P
  172. The
  173. \fIunlinkat\fR()
  174. function shall fail if:
  175. .TP
  176. .BR EACCES
  177. The access mode of the open file description associated with
  178. .IR fd
  179. is not O_SEARCH and the permissions of the directory underlying
  180. .IR fd
  181. do not permit directory searches.
  182. .TP
  183. .BR EBADF
  184. The
  185. .IR path
  186. argument does not specify an absolute path and the
  187. .IR fd
  188. argument is neither AT_FDCWD nor a valid file descriptor open for reading
  189. or searching.
  190. .TP
  191. .BR ENOTDIR
  192. The
  193. .IR path
  194. argument is not an absolute path and
  195. .IR fd
  196. is a file descriptor associated with a non-directory file.
  197. .TP
  198. .BR EEXIST " or " ENOTEMPTY
  199. .br
  200. The
  201. .IR flag
  202. parameter has the AT_REMOVEDIR bit set and the
  203. .IR path
  204. argument names a directory that is not an empty directory, or there are
  205. hard links to the directory other than dot or a single entry in dot-dot.
  206. .TP
  207. .BR ENOTDIR
  208. The
  209. .IR flag
  210. parameter has the AT_REMOVEDIR bit set and
  211. .IR path
  212. does not name a directory.
  213. .P
  214. These functions may fail and not unlink the file if:
  215. .TP
  216. .BR EBUSY
  217. The file named by
  218. .IR path
  219. is a named STREAM.
  220. .TP
  221. .BR ELOOP
  222. More than
  223. {SYMLOOP_MAX}
  224. symbolic links were encountered during resolution of the
  225. .IR path
  226. argument.
  227. .TP
  228. .BR ENAMETOOLONG
  229. .br
  230. The length of a pathname exceeds
  231. {PATH_MAX},
  232. or pathname resolution of a symbolic link produced an intermediate
  233. result with a length that exceeds
  234. {PATH_MAX}.
  235. .TP
  236. .BR ETXTBSY
  237. The entry to be unlinked is the last directory entry to a pure
  238. procedure (shared text) file that is being executed.
  239. .br
  240. .P
  241. The
  242. \fIunlinkat\fR()
  243. function may fail if:
  244. .TP
  245. .BR EINVAL
  246. The value of the
  247. .IR flag
  248. argument is not valid.
  249. .LP
  250. .IR "The following sections are informative."
  251. .SH EXAMPLES
  252. .SS "Removing a Link to a File"
  253. .P
  254. The following example shows how to remove a link to a file named
  255. .BR /home/cnd/mod1
  256. by removing the entry named
  257. .BR /modules/pass1 .
  258. .sp
  259. .RS 4
  260. .nf
  261. #include <unistd.h>
  262. .P
  263. char *path = "/modules/pass1";
  264. int status;
  265. \&...
  266. status = unlink(path);
  267. .fi
  268. .P
  269. .RE
  270. .SS "Checking for an Error"
  271. .P
  272. The following example fragment creates a temporary password lock file
  273. named
  274. .BR LOCKFILE ,
  275. which is defined as
  276. .BR /etc/ptmp ,
  277. and gets a file descriptor for it. If the file cannot be opened for
  278. writing,
  279. \fIunlink\fR()
  280. is used to remove the link between the file descriptor and
  281. .BR LOCKFILE .
  282. .sp
  283. .RS 4
  284. .nf
  285. #include <sys/types.h>
  286. #include <stdio.h>
  287. #include <fcntl.h>
  288. #include <errno.h>
  289. #include <unistd.h>
  290. #include <sys/stat.h>
  291. .P
  292. #define LOCKFILE "/etc/ptmp"
  293. .P
  294. int pfd; /* Integer for file descriptor returned by open call. */
  295. FILE *fpfd; /* File pointer for use in putpwent(). */
  296. \&...
  297. /* Open password Lock file. If it exists, this is an error. */
  298. if ((pfd = open(LOCKFILE, O_WRONLY| O_CREAT | O_EXCL, S_IRUSR
  299. | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
  300. fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
  301. exit(1);
  302. }
  303. .P
  304. /* Lock file created; proceed with fdopen of lock file so that
  305. putpwent() can be used.
  306. */
  307. if ((fpfd = fdopen(pfd, "w")) == NULL) {
  308. close(pfd);
  309. unlink(LOCKFILE);
  310. exit(1);
  311. }
  312. .fi
  313. .P
  314. .RE
  315. .SS "Replacing Files"
  316. .P
  317. The following example fragment uses
  318. \fIunlink\fR()
  319. to discard links to files, so that they can be replaced with new
  320. versions of the files. The first call removes the link to
  321. .BR LOCKFILE
  322. if an error occurs. Successive calls remove the links to
  323. .BR SAVEFILE
  324. and
  325. .BR PASSWDFILE
  326. so that new links can be created, then removes the link to
  327. .BR LOCKFILE
  328. when it is no longer needed.
  329. .sp
  330. .RS 4
  331. .nf
  332. #include <sys/types.h>
  333. #include <stdio.h>
  334. #include <fcntl.h>
  335. #include <errno.h>
  336. #include <unistd.h>
  337. #include <sys/stat.h>
  338. .P
  339. #define LOCKFILE "/etc/ptmp"
  340. #define PASSWDFILE "/etc/passwd"
  341. #define SAVEFILE "/etc/opasswd"
  342. \&...
  343. /* If no change was made, assume error and leave passwd unchanged. */
  344. if (!valid_change) {
  345. fprintf(stderr, "Could not change password for user %s\en", user);
  346. unlink(LOCKFILE);
  347. exit(1);
  348. }
  349. .P
  350. /* Change permissions on new password file. */
  351. chmod(LOCKFILE, S_IRUSR | S_IRGRP | S_IROTH);
  352. .P
  353. /* Remove saved password file. */
  354. unlink(SAVEFILE);
  355. .P
  356. /* Save current password file. */
  357. link(PASSWDFILE, SAVEFILE);
  358. .P
  359. /* Remove current password file. */
  360. unlink(PASSWDFILE);
  361. .P
  362. /* Save new password file as current password file. */
  363. link(LOCKFILE,PASSWDFILE);
  364. .P
  365. /* Remove lock file. */
  366. unlink(LOCKFILE);
  367. .P
  368. exit(0);
  369. .fi
  370. .P
  371. .RE
  372. .SH "APPLICATION USAGE"
  373. Applications should use
  374. \fIrmdir\fR()
  375. to remove a directory.
  376. .SH RATIONALE
  377. Unlinking a directory is restricted to the superuser
  378. in many historical implementations for reasons given in
  379. \fIlink\fR()
  380. (see also
  381. \fIrename\fR()).
  382. .P
  383. The meaning of
  384. .BR [EBUSY]
  385. in historical implementations is ``mount point busy''. Since this volume of POSIX.1\(hy2017 does
  386. not cover the system administration concepts of mounting and unmounting,
  387. the description of the error was changed to ``resource busy''. (This
  388. meaning is used by some device drivers when a second process tries to
  389. open an exclusive use device.) The wording is also intended to allow
  390. implementations to refuse to remove a directory if it is the root or
  391. current working directory of any process.
  392. .P
  393. The standard developers reviewed TR 24715\(hy2006 and noted that
  394. LSB-conforming implementations may return
  395. .BR [EISDIR]
  396. instead of
  397. .BR [EPERM]
  398. when unlinking a directory. A change to permit this behavior by
  399. changing the requirement for
  400. .BR [EPERM]
  401. to
  402. .BR [EPERM]
  403. or
  404. .BR [EISDIR]
  405. was considered, but decided against since it would break existing
  406. strictly conforming and conforming applications. Applications written
  407. for portability to both POSIX.1\(hy2008 and the LSB should be prepared to
  408. handle either error code.
  409. .P
  410. The purpose of the
  411. \fIunlinkat\fR()
  412. function is to remove directory entries in directories other than the
  413. current working directory without exposure to race conditions. Any part
  414. of the path of a file could be changed in parallel to a call to
  415. \fIunlink\fR(),
  416. resulting in unspecified behavior. By opening a file descriptor for
  417. the target directory and using the
  418. \fIunlinkat\fR()
  419. function it can be guaranteed that the removed directory entry is
  420. located relative to the desired directory.
  421. .SH "FUTURE DIRECTIONS"
  422. None.
  423. .SH "SEE ALSO"
  424. .IR "\fIclose\fR\^(\|)",
  425. .IR "\fIlink\fR\^(\|)",
  426. .IR "\fIremove\fR\^(\|)",
  427. .IR "\fIrename\fR\^(\|)",
  428. .IR "\fIrmdir\fR\^(\|)",
  429. .IR "\fIsymlink\fR\^(\|)"
  430. .P
  431. The Base Definitions volume of POSIX.1\(hy2017,
  432. .IR "Section 4.3" ", " "Directory Protection",
  433. .IR "\fB<fcntl.h>\fP",
  434. .IR "\fB<unistd.h>\fP"
  435. .\"
  436. .SH COPYRIGHT
  437. Portions of this text are reprinted and reproduced in electronic form
  438. from IEEE Std 1003.1-2017, Standard for Information Technology
  439. -- Portable Operating System Interface (POSIX), The Open Group Base
  440. Specifications Issue 7, 2018 Edition,
  441. Copyright (C) 2018 by the Institute of
  442. Electrical and Electronics Engineers, Inc and The Open Group.
  443. In the event of any discrepancy between this version and the original IEEE and
  444. The Open Group Standard, the original IEEE and The Open Group Standard
  445. is the referee document. The original Standard can be obtained online at
  446. http://www.opengroup.org/unix/online.html .
  447. .PP
  448. Any typographical or formatting errors that appear
  449. in this page are most likely
  450. to have been introduced during the conversion of the source files to
  451. man page format. To report such errors, see
  452. https://www.kernel.org/doc/man-pages/reporting_bugs.html .