1UNLINK(3P)                 POSIX Programmer's Manual                UNLINK(3P)
2
3
4

PROLOG

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

NAME

12       unlink, unlinkat — remove a directory entry
13

SYNOPSIS

15       #include <unistd.h>
16
17       int unlink(const char *path);
18
19       #include <fcntl.h>
20
21       int unlinkat(int fd, const char *path, int flag);
22

DESCRIPTION

24       The unlink() function shall remove a link to a file. If  path  names  a
25       symbolic  link,  unlink()  shall remove the symbolic link named by path
26       and shall not affect any file or directory named by the contents of the
27       symbolic  link.  Otherwise, unlink() shall remove the link named by the
28       pathname pointed to by path and shall decrement the link count  of  the
29       file referenced by the link.
30
31       When  the file's link count becomes 0 and no process has the file open,
32       the space occupied by the file shall be freed and  the  file  shall  no
33       longer  be accessible. If one or more processes have the file open when
34       the last link is removed, the link shall  be  removed  before  unlink()
35       returns,  but the removal of the file contents shall be postponed until
36       all references to the file are closed.
37
38       The path argument shall not name a directory  unless  the  process  has
39       appropriate  privileges  and the implementation supports using unlink()
40       on directories.
41
42       Upon successful completion, unlink() shall mark  for  update  the  last
43       data  modification and last file status change timestamps of the parent
44       directory. Also, if the file's link count is not 0, the last file  sta‐
45       tus change timestamp of the file shall be marked for update.
46
47       The  unlinkat() function shall be equivalent to the unlink() or rmdir()
48       function except in the case where path specifies a  relative  path.  In
49       this  case  the directory entry to be removed is determined relative to
50       the directory associated with the file descriptor  fd  instead  of  the
51       current working directory. If the access mode of the open file descrip‐
52       tion associated with the file descriptor is not O_SEARCH, the  function
53       shall  check whether directory searches are permitted using the current
54       permissions of the directory underlying the  file  descriptor.  If  the
55       access mode is O_SEARCH, the function shall not perform the check.
56
57       Values for flag are constructed by a bitwise-inclusive OR of flags from
58       the following list, defined in <fcntl.h>:
59
60       AT_REMOVEDIR
61             Remove the directory entry specified by fd and path as  a  direc‐
62             tory, not a normal file.
63
64       If unlinkat() is passed the special value AT_FDCWD in the fd parameter,
65       the current working directory shall be used and the behavior  shall  be
66       identical  to  a call to unlink() or rmdir() respectively, depending on
67       whether or not the AT_REMOVEDIR bit is set in flag.
68

RETURN VALUE

70       Upon successful completion, these functions shall return 0.  Otherwise,
71       these functions shall return -1 and set errno to indicate the error. If
72       -1 is returned, the named file shall not be changed.
73

ERRORS

75       These functions shall fail and shall not unlink the file if:
76
77       EACCES Search permission is denied for a component of the path  prefix,
78              or  write  permission  is denied on the directory containing the
79              directory entry to be removed.
80
81       EBUSY  The file named by the path argument cannot be  unlinked  because
82              it is being used by the system or another process and the imple‐
83              mentation considers this an error.
84
85       ELOOP  A loop exists in symbolic links encountered during resolution of
86              the path argument.
87
88       ENAMETOOLONG
89              The  length  of  a  component  of  a  pathname  is  longer  than
90              {NAME_MAX}.
91
92       ENOENT A component of path does not name an existing file or path is an
93              empty string.
94
95       ENOTDIR
96              A  component  of  the path prefix names an existing file that is
97              neither a directory nor a symbolic link to a directory,  or  the
98              path  argument  contains  at least one non-<slash> character and
99              ends with one or more trailing <slash> characters and  the  last
100              pathname  component  names  an  existing  file that is neither a
101              directory nor a symbolic link to a directory.
102
103       EPERM  The file named by path is a directory, and  either  the  calling
104              process does not have appropriate privileges, or the implementa‐
105              tion prohibits using unlink() on directories.
106
107       EPERM or EACCES
108              The S_ISVTX flag is set on the  directory  containing  the  file
109              referred  to  by the path argument and the process does not sat‐
110              isfy the criteria specified in the Base  Definitions  volume  of
111              POSIX.1‐2017, Section 4.3, Directory Protection.
112
113       EROFS  The  directory  entry to be unlinked is part of a read-only file
114              system.
115
116       The unlinkat() function shall fail if:
117
118       EACCES The access mode of the open file description associated with  fd
119              is  not O_SEARCH and the permissions of the directory underlying
120              fd do not permit directory searches.
121
122       EBADF  The path argument does not specify an absolute path and  the  fd
123              argument  is  neither  AT_FDCWD nor a valid file descriptor open
124              for reading or searching.
125
126       ENOTDIR
127              The path argument is not an absolute  path  and  fd  is  a  file
128              descriptor associated with a non-directory file.
129
130       EEXIST or ENOTEMPTY
131              The  flag  parameter  has  the AT_REMOVEDIR bit set and the path
132              argument names a directory that is not an  empty  directory,  or
133              there are hard links to the directory other than dot or a single
134              entry in dot-dot.
135
136       ENOTDIR
137              The flag parameter has the AT_REMOVEDIR bit set  and  path  does
138              not name a directory.
139
140       These functions may fail and not unlink the file if:
141
142       EBUSY  The file named by path is a named STREAM.
143
144       ELOOP  More  than  {SYMLOOP_MAX} symbolic links were encountered during
145              resolution of the path argument.
146
147       ENAMETOOLONG
148              The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
149              tion  of  a symbolic link produced an intermediate result with a
150              length that exceeds {PATH_MAX}.
151
152       ETXTBSY
153              The entry to be unlinked is the last directory entry to  a  pure
154              procedure (shared text) file that is being executed.
155
156       The unlinkat() function may fail if:
157
158       EINVAL The value of the flag argument is not valid.
159
160       The following sections are informative.
161

EXAMPLES

163   Removing a Link to a File
164       The  following  example  shows  how  to  remove  a link to a file named
165       /home/cnd/mod1 by removing the entry named /modules/pass1.
166
167
168           #include <unistd.h>
169
170           char *path = "/modules/pass1";
171           int   status;
172           ...
173           status = unlink(path);
174
175   Checking for an Error
176       The following example fragment creates a temporary password  lock  file
177       named LOCKFILE, which is defined as /etc/ptmp, and gets a file descrip‐
178       tor for it. If the file cannot be opened for writing, unlink() is  used
179       to remove the link between the file descriptor and LOCKFILE.
180
181
182           #include <sys/types.h>
183           #include <stdio.h>
184           #include <fcntl.h>
185           #include <errno.h>
186           #include <unistd.h>
187           #include <sys/stat.h>
188
189           #define LOCKFILE "/etc/ptmp"
190
191           int pfd;  /* Integer for file descriptor returned by open call. */
192           FILE *fpfd;  /* File pointer for use in putpwent(). */
193           ...
194           /* Open password Lock file. If it exists, this is an error. */
195           if ((pfd = open(LOCKFILE, O_WRONLY| O_CREAT | O_EXCL, S_IRUSR
196               | S_IWUSR | S_IRGRP | S_IROTH)) == -1)  {
197               fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n");
198               exit(1);
199           }
200
201           /* Lock file created; proceed with fdopen of lock file so that
202              putpwent() can be used.
203            */
204           if ((fpfd = fdopen(pfd, "w")) == NULL) {
205               close(pfd);
206               unlink(LOCKFILE);
207               exit(1);
208           }
209
210   Replacing Files
211       The following example fragment uses unlink() to discard links to files,
212       so that they can be replaced with new versions of the files. The  first
213       call  removes the link to LOCKFILE if an error occurs. Successive calls
214       remove the links to SAVEFILE and PASSWDFILE so that new  links  can  be
215       created, then removes the link to LOCKFILE when it is no longer needed.
216
217
218           #include <sys/types.h>
219           #include <stdio.h>
220           #include <fcntl.h>
221           #include <errno.h>
222           #include <unistd.h>
223           #include <sys/stat.h>
224
225           #define LOCKFILE "/etc/ptmp"
226           #define PASSWDFILE "/etc/passwd"
227           #define SAVEFILE "/etc/opasswd"
228           ...
229           /* If no change was made, assume error and leave passwd unchanged. */
230           if (!valid_change) {
231               fprintf(stderr, "Could not change password for user %s\n", user);
232               unlink(LOCKFILE);
233               exit(1);
234           }
235
236           /* Change permissions on new password file. */
237           chmod(LOCKFILE, S_IRUSR | S_IRGRP | S_IROTH);
238
239           /* Remove saved password file. */
240           unlink(SAVEFILE);
241
242           /* Save current password file. */
243           link(PASSWDFILE, SAVEFILE);
244
245           /* Remove current password file. */
246           unlink(PASSWDFILE);
247
248           /* Save new password file as current password file. */
249           link(LOCKFILE,PASSWDFILE);
250
251           /* Remove lock file. */
252           unlink(LOCKFILE);
253
254           exit(0);
255

APPLICATION USAGE

257       Applications should use rmdir() to remove a directory.
258

RATIONALE

260       Unlinking a directory is restricted to the superuser in many historical
261       implementations for reasons given in link() (see also rename()).
262
263       The meaning of [EBUSY] in historical implementations is  ``mount  point
264       busy''.  Since  this  volume  of POSIX.1‐2017 does not cover the system
265       administration concepts of mounting and unmounting, the description  of
266       the  error  was  changed to ``resource busy''. (This meaning is used by
267       some device drivers when a second process tries to  open  an  exclusive
268       use  device.)  The wording is also intended to allow implementations to
269       refuse to remove a directory if it  is  the  root  or  current  working
270       directory of any process.
271
272       The  standard developers reviewed TR 24715‐2006 and noted that LSB-con‐
273       forming implementations may return [EISDIR]  instead  of  [EPERM]  when
274       unlinking a directory. A change to permit this behavior by changing the
275       requirement for [EPERM] to [EPERM]  or  [EISDIR]  was  considered,  but
276       decided  against  since it would break existing strictly conforming and
277       conforming applications. Applications written for portability  to  both
278       POSIX.1‐2008  and  the  LSB  should  be prepared to handle either error
279       code.
280
281       The purpose of the unlinkat() function is to remove  directory  entries
282       in  directories  other than the current working directory without expo‐
283       sure to race conditions. Any part of  the  path  of  a  file  could  be
284       changed  in  parallel  to  a call to unlink(), resulting in unspecified
285       behavior. By opening a file descriptor for  the  target  directory  and
286       using  the  unlinkat()  function  it can be guaranteed that the removed
287       directory entry is located relative to the desired directory.
288

FUTURE DIRECTIONS

290       None.
291

SEE ALSO

293       close(), link(), remove(), rename(), rmdir(), symlink()
294
295       The Base Definitions volume of  POSIX.1‐2017,  Section  4.3,  Directory
296       Protection, <fcntl.h>, <unistd.h>
297
299       Portions  of  this text are reprinted and reproduced in electronic form
300       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
301       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
302       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
303       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
304       event of any discrepancy between this version and the original IEEE and
305       The  Open Group Standard, the original IEEE and The Open Group Standard
306       is the referee document. The original Standard can be  obtained  online
307       at http://www.opengroup.org/unix/online.html .
308
309       Any  typographical  or  formatting  errors that appear in this page are
310       most likely to have been introduced during the conversion of the source
311       files  to  man page format. To report such errors, see https://www.ker
312       nel.org/doc/man-pages/reporting_bugs.html .
313
314
315
316IEEE/The Open Group                  2017                           UNLINK(3P)
Impressum