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