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
11

NAME

13       unlink, unlinkat — remove a directory entry relative to directory  file
14       descriptor
15

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

EXAMPLES

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

APPLICATION USAGE

252       Applications should use rmdir() to remove a directory.
253

RATIONALE

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

FUTURE DIRECTIONS

285       None.
286

SEE ALSO

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