xref: /freebsd-13.1/lib/libc/sys/unlink.2 (revision a811237d)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)unlink.2	8.1 (Berkeley) 6/4/93
29.\" $FreeBSD$
30.\"
31.Dd June 6, 2019
32.Dt UNLINK 2
33.Os
34.Sh NAME
35.Nm unlink ,
36.Nm unlinkat ,
37.Nm funlinkat
38.Nd remove directory entry
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In unistd.h
43.Ft int
44.Fn unlink "const char *path"
45.Ft int
46.Fn unlinkat "int dfd" "const char *path" "int flag"
47.Ft int
48.Fn funlinkat "int dfd" "const char *path" "int fd" "int flag"
49.Sh DESCRIPTION
50The
51.Fn unlink
52system call
53removes the link named by
54.Fa path
55from its directory and decrements the link count of the
56file which was referenced by the link.
57If that decrement reduces the link count of the file
58to zero,
59and no process has the file open, then
60all resources associated with the file are reclaimed.
61If one or more process have the file open when the last link is removed,
62the link is removed, but the removal of the file is delayed until
63all references to it have been closed.
64The
65.Fa path
66argument
67may not be a directory.
68.Pp
69The
70.Fn unlinkat
71system call is equivalent to
72.Fn unlink
73or
74.Fn rmdir
75except in the case where
76.Fa path
77specifies a relative path.
78In this case the directory entry to be removed is determined
79relative to the directory associated with the file descriptor
80.Fa dfd
81instead of the current working directory.
82.Pp
83The values for
84.Fa flag
85are constructed by a bitwise-inclusive OR of flags from the following list,
86defined in
87.In fcntl.h :
88.Bl -tag -width indent
89.It Dv AT_REMOVEDIR
90Remove the directory entry specified by
91.Fa fd
92and
93.Fa path
94as a directory, not a normal file.
95.It Dv AT_BENEATH
96Only unlink files and directories which are beneath of the topping
97directory.
98See the description of the
99.Dv O_BENEATH
100flag in the
101.Xr open 2
102manual page.
103.El
104.Pp
105If
106.Fn unlinkat
107is passed the special value
108.Dv AT_FDCWD
109in the
110.Fa fd
111parameter, the current working directory is used and the behavior is
112identical to a call to
113.Fa unlink
114or
115.Fa rmdir
116respectively, depending on whether or not the
117.Dv AT_REMOVEDIR
118bit is set in flag.
119.Pp
120The
121.Fn funlinkat
122system call can be used to unlink an already-opened file, unless that
123file has been replaced since it was opened.
124It is equivalent to
125.Fn unlinkat
126in the case where
127.Fa path
128is already open as the file descriptor
129.Fa fd .
130Otherwise, the path will not be removed and an error will be returned.
131The
132.Fa fd
133can be set the
134.Dv FD_NONE .
135In that case
136.Fn funlinkat
137behaves exactly like
138.Fn unlinkat .
139.Sh RETURN VALUES
140.Rv -std unlink
141.Sh ERRORS
142The
143.Fn unlink
144succeeds unless:
145.Bl -tag -width Er
146.It Bq Er ENOTDIR
147A component of the path prefix is not a directory.
148.It Bq Er EISDIR
149The named file is a directory.
150.It Bq Er ENAMETOOLONG
151A component of a pathname exceeded 255 characters,
152or an entire path name exceeded 1023 characters.
153.It Bq Er ENOENT
154The named file does not exist.
155.It Bq Er EACCES
156Search permission is denied for a component of the path prefix.
157.It Bq Er EACCES
158Write permission is denied on the directory containing the link
159to be removed.
160.It Bq Er ELOOP
161Too many symbolic links were encountered in translating the pathname.
162.It Bq Er EPERM
163The named file is a directory.
164.It Bq Er EPERM
165The named file has its immutable, undeletable or append-only flag set, see the
166.Xr chflags 2
167manual page for more information.
168.It Bq Er EPERM
169The parent directory of the named file has its immutable or append-only flag
170set.
171.It Bq Er EPERM
172The directory containing the file is marked sticky,
173and neither the containing directory nor the file to be removed
174are owned by the effective user ID.
175.It Bq Er EIO
176An I/O error occurred while deleting the directory entry
177or deallocating the inode.
178.It Bq Er EROFS
179The named file resides on a read-only file system.
180.It Bq Er EFAULT
181The
182.Fa path
183argument
184points outside the process's allocated address space.
185.It Bq Er ENOSPC
186On file systems supporting copy-on-write or snapshots, there was not enough
187free space to record metadata for the delete operation of the file.
188.El
189.Pp
190In addition to the errors returned by the
191.Fn unlink ,
192the
193.Fn unlinkat
194may fail if:
195.Bl -tag -width Er
196.It Bq Er EBADF
197The
198.Fa path
199argument does not specify an absolute path and the
200.Fa fd
201argument is neither
202.Dv AT_FDCWD
203nor a valid file descriptor open for searching.
204.It Bq Er ENOTEMPTY
205The
206.Fa flag
207parameter has the
208.Dv AT_REMOVEDIR
209bit set and the
210.Fa path
211argument names a directory that is not an empty directory,
212or there are hard links to the directory other than dot or
213a single entry in dot-dot.
214.It Bq Er ENOTDIR
215The
216.Fa flag
217parameter has the
218.Dv AT_REMOVEDIR
219bit set and
220.Fa path
221does not name a directory.
222.It Bq Er EINVAL
223The value of the
224.Fa flag
225argument is not valid.
226.It Bq Er ENOTDIR
227The
228.Fa path
229argument is not an absolute path and
230.Fa fd
231is neither
232.Dv AT_FDCWD
233nor a file descriptor associated with a directory.
234.It Bq Er ENOTCAPABLE
235.Fa path
236is an absolute path,
237or contained a ".." component leading to a
238directory outside of the directory hierarchy specified by
239.Fa fd ,
240and the process is in capability mode.
241.It Bq Er ENOTCAPABLE
242The
243.Dv AT_BENEATH
244flag was provided to
245.Fn unlinkat ,
246and the absolute
247.Fa path
248does not have its tail fully contained under the topping directory,
249or the relative
250.Fa path
251escapes it.
252.El
253.Pp
254In addition to the errors returned by
255.Fn unlinkat ,
256.Fn funlinkat
257may fail if:
258.Bl -tag -width Er
259.It Bq Er EDEADLK
260The file descriptor is not associated with the path.
261.El
262.Sh SEE ALSO
263.Xr chflags 2 ,
264.Xr close 2 ,
265.Xr link 2 ,
266.Xr rmdir 2 ,
267.Xr symlink 7
268.Sh STANDARDS
269The
270.Fn unlinkat
271system call follows The Open Group Extended API Set 2 specification.
272.Sh HISTORY
273The
274.Fn unlink
275function appeared in
276.At v1 .
277The
278.Fn unlinkat
279system call appeared in
280.Fx 8.0 .
281The
282.Fn funlinkat
283system call appeared in
284.Fx 13.0 .
285.Pp
286The
287.Fn unlink
288system call traditionally allows the super-user to unlink directories which
289can damage the file system integrity.
290This implementation no longer permits it.
291