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: stable/12/lib/libc/sys/unlink.2 369500 2021-03-21 17:56:44Z kib $
30.\"
31.Dd February 23, 2021
32.Dt UNLINK 2
33.Os
34.Sh NAME
35.Nm unlink ,
36.Nm unlinkat
37.Nd remove directory entry
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn unlink "const char *path"
44.Ft int
45.Fn unlinkat "int fd" "const char *path" "int flag"
46.Sh DESCRIPTION
47The
48.Fn unlink
49system call
50removes the link named by
51.Fa path
52from its directory and decrements the link count of the
53file which was referenced by the link.
54If that decrement reduces the link count of the file
55to zero,
56and no process has the file open, then
57all resources associated with the file are reclaimed.
58If one or more process have the file open when the last link is removed,
59the link is removed, but the removal of the file is delayed until
60all references to it have been closed.
61The
62.Fa path
63argument
64may not be a directory.
65.Pp
66The
67.Fn unlinkat
68system call is equivalent to
69.Fn unlink
70or
71.Fn rmdir
72except in the case where
73.Fa path
74specifies a relative path.
75In this case the directory entry to be removed is determined
76relative to the directory associated with the file descriptor
77.Fa fd
78instead of the current working directory.
79.Pp
80The values for
81.Fa flag
82are constructed by a bitwise-inclusive OR of flags from the following list,
83defined in
84.In fcntl.h :
85.Bl -tag -width indent
86.It Dv AT_REMOVEDIR
87Remove the directory entry specified by
88.Fa fd
89and
90.Fa path
91as a directory, not a normal file.
92.It Dv AT_RESOLVE_BENEATH
93Only walk paths below the directory specified by the
94.Ar fd
95descriptor.
96See the description of the
97.Dv O_RESOLVE_BENEATH
98flag in the
99.Xr open 2
100manual page.
101.El
102.Pp
103If
104.Fn unlinkat
105is passed the special value
106.Dv AT_FDCWD
107in the
108.Fa fd
109parameter, the current working directory is used and the behavior is
110identical to a call to
111.Fa unlink
112or
113.Fa rmdir
114respectively, depending on whether or not the
115.Dv AT_REMOVEDIR
116bit is set in flag.
117.Sh RETURN VALUES
118.Rv -std unlink
119.Sh ERRORS
120The
121.Fn unlink
122succeeds unless:
123.Bl -tag -width Er
124.It Bq Er ENOTDIR
125A component of the path prefix is not a directory.
126.It Bq Er EISDIR
127The named file is a directory.
128.It Bq Er ENAMETOOLONG
129A component of a pathname exceeded 255 characters,
130or an entire path name exceeded 1023 characters.
131.It Bq Er ENOENT
132The named file does not exist.
133.It Bq Er EACCES
134Search permission is denied for a component of the path prefix.
135.It Bq Er EACCES
136Write permission is denied on the directory containing the link
137to be removed.
138.It Bq Er ELOOP
139Too many symbolic links were encountered in translating the pathname.
140.It Bq Er EPERM
141The named file is a directory.
142.It Bq Er EPERM
143The named file has its immutable, undeletable or append-only flag set, see the
144.Xr chflags 2
145manual page for more information.
146.It Bq Er EPERM
147The parent directory of the named file has its immutable or append-only flag
148set.
149.It Bq Er EPERM
150The directory containing the file is marked sticky,
151and neither the containing directory nor the file to be removed
152are owned by the effective user ID.
153.It Bq Er EIO
154An I/O error occurred while deleting the directory entry
155or deallocating the inode.
156.It Bq Er EINTEGRITY
157Corrupted data was detected while reading from the file system.
158.It Bq Er EROFS
159The named file resides on a read-only file system.
160.It Bq Er EFAULT
161The
162.Fa path
163argument
164points outside the process's allocated address space.
165.It Bq Er ENOSPC
166On file systems supporting copy-on-write or snapshots, there was not enough
167free space to record metadata for the delete operation of the file.
168.El
169.Pp
170In addition to the errors returned by the
171.Fn unlink ,
172the
173.Fn unlinkat
174may fail if:
175.Bl -tag -width Er
176.It Bq Er EBADF
177The
178.Fa path
179argument does not specify an absolute path and the
180.Fa fd
181argument is neither
182.Dv AT_FDCWD
183nor a valid file descriptor open for searching.
184.It Bq Er ENOTEMPTY
185The
186.Fa flag
187parameter has the
188.Dv AT_REMOVEDIR
189bit set and the
190.Fa path
191argument names a directory that is not an empty directory,
192or there are hard links to the directory other than dot or
193a single entry in dot-dot.
194.It Bq Er ENOTDIR
195The
196.Fa flag
197parameter has the
198.Dv AT_REMOVEDIR
199bit set and
200.Fa path
201does not name a directory.
202.It Bq Er EINVAL
203The value of the
204.Fa flag
205argument is not valid.
206.It Bq Er ENOTDIR
207The
208.Fa path
209argument is not an absolute path and
210.Fa fd
211is neither
212.Dv AT_FDCWD
213nor a file descriptor associated with a directory.
214.It Bq Er ENOTCAPABLE
215.Fa path
216is an absolute path,
217or contained a ".." component leading to a
218directory outside of the directory hierarchy specified by
219.Fa fd ,
220and the process is in capability mode or the
221.Dv AT_RESOLVE_BENEATH
222flag was specified.
223.El
224.Sh SEE ALSO
225.Xr chflags 2 ,
226.Xr close 2 ,
227.Xr link 2 ,
228.Xr rmdir 2 ,
229.Xr symlink 7
230.Sh STANDARDS
231The
232.Fn unlinkat
233system call follows The Open Group Extended API Set 2 specification.
234.Sh HISTORY
235The
236.Fn unlink
237function appeared in
238.At v1 .
239The
240.Fn unlinkat
241system call appeared in
242.Fx 8.0 .
243.Pp
244The
245.Fn unlink
246system call traditionally allows the super-user to unlink directories which
247can damage the file system integrity.
248This implementation no longer permits it.
249