xref: /openbsd-src/lib/libc/sys/unlink.2 (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1.\"	$OpenBSD: unlink.2,v 1.23 2014/02/13 07:30:39 guenther Exp $
2.\"	$NetBSD: unlink.2,v 1.7 1995/02/27 12:39:13 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)unlink.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: February 13 2014 $
34.Dt UNLINK 2
35.Os
36.Sh NAME
37.Nm unlink ,
38.Nm unlinkat
39.Nd remove directory entry
40.Sh SYNOPSIS
41.Fd #include <unistd.h>
42.Ft int
43.Fn unlink "const char *path"
44.Fd #include <fcntl.h>
45.Fd #include <unistd.h>
46.Ft int
47.Fn unlinkat "int fd" "const char *path" "int flag"
48.Sh DESCRIPTION
49The
50.Fn unlink
51function removes the link named by
52.Fa path
53from its directory and decrements the link count of the
54file which was referenced by the link.
55If that decrement reduces the link count of the file
56to zero,
57and no process has the file open, then
58all resources associated with the file are reclaimed.
59If one or more processes have the file open when the last link is removed,
60the link is removed, but the removal of the file is delayed until
61all references to it have been closed.
62.Pp
63The
64.Fn unlinkat
65function is equivalent to either the
66.Fn unlink
67or
68.Xr rmdir 2
69function depending on the value of
70.Fa flag
71(see below), except that where
72.Fa path
73specifies a relative path,
74the directory entry to be removed is determined relative to
75the directory associated with file descriptor
76.Fa fd
77instead of the current working directory.
78.Pp
79If
80.Fn unlinkat
81is passed the special value
82.Dv AT_FDCWD
83(defined in
84.In fcntl.h )
85in the
86.Fa fd
87parameter, the current working directory is used
88and the behavior is identical to a call to
89.Fn unlink
90or
91.Xr rmdir 2 ,
92depending on whether or not the
93.Dv AT_REMOVEDIR
94bit is set in
95.Fa flag .
96.Pp
97The
98.Fa flag
99argument is the bitwise OR of zero or more of the following values:
100.Pp
101.Bl -tag -width AT_REMOVEDIR -offset indent -compact
102.It Dv AT_REMOVEDIR
103Remove the directory entry specified by
104.Fa path
105as a directory, not a normal file.
106.El
107.Sh RETURN VALUES
108Upon successful completion, a value of 0 is returned.
109Otherwise, a value of \-1 is returned and
110.Va errno
111is set to indicate the error.
112.Sh ERRORS
113The
114.Fn unlink
115and
116.Fn unlinkat
117functions will fail if:
118.Bl -tag -width Er
119.It Bq Er ENOTDIR
120A component of the path prefix is not a directory.
121.It Bq Er ENAMETOOLONG
122A component of a pathname exceeded
123.Dv {NAME_MAX}
124characters, or an entire path name exceeded
125.Dv {PATH_MAX}
126characters.
127.It Bq Er ENOENT
128The named file does not exist.
129.It Bq Er EACCES
130Search permission is denied for a component of the path prefix.
131.It Bq Er EACCES
132Write permission is denied on the directory containing the link
133to be removed.
134.It Bq Er ELOOP
135Too many symbolic links were encountered in translating the pathname.
136.It Bq Er EPERM
137The named file is a directory and the effective user ID
138of the process is not the superuser, or the file system
139containing the file does not permit the use of
140.Fn unlink
141on a directory.
142.It Bq Er EPERM
143The directory containing the file is marked sticky,
144and neither the containing directory nor the file to be removed
145are owned by the effective user ID.
146.It Bq Er EPERM
147The named file has its immutable or append-only flag set (see
148.Xr chflags 2 ) .
149.It Bq Er EBUSY
150The entry to be unlinked is the mount point for a
151mounted file system.
152.It Bq Er EIO
153An I/O error occurred while deleting the directory entry
154or deallocating the inode.
155.It Bq Er EROFS
156The named file resides on a read-only file system.
157.It Bq Er EFAULT
158.Fa path
159points outside the process's allocated address space.
160.El
161.Pp
162Additionally,
163.Fn unlinkat
164will fail if:
165.Bl -tag -width Er
166.It Bq Er ENOTDIR
167The
168.Dv AT_REMOVEDIR
169flag bit is set and
170.Fa path
171does not name a directory.
172.It Bq Er ENOTEMPTY
173The
174.Dv AT_REMOVEDIR
175flag bit is set and the named directory contains files other than
176.Ql \&.
177and
178.Ql \&..
179in it.
180.It Bq Er EINVAL
181The value of the
182.Fa flag
183argument was neither zero nor
184.Dv AT_REMOVEDIR .
185.It Bq Er EBADF
186The
187.Fa path
188argument specifies a relative path and the
189.Fa fd
190argument is neither
191.Dv AT_FDCWD
192nor a valid file descriptor.
193.It Bq Er ENOTDIR
194The
195.Fa path
196argument specifies a relative path and the
197.Fa fd
198argument is a valid file descriptor but it does not reference a directory.
199.It Bq Er EACCES
200The
201.Fa path
202argument specifies a relative path but search permission is denied
203for the directory which the
204.Fa fd
205file descriptor references.
206.El
207.Sh SEE ALSO
208.Xr rm 1 ,
209.Xr close 2 ,
210.Xr link 2 ,
211.Xr rmdir 2 ,
212.Xr symlink 7
213.Sh STANDARDS
214The
215.Fn unlink
216and
217.Fn unlinkat
218functions conform to
219.St -p1003.1-2008 .
220.Sh HISTORY
221The
222.Fn unlink
223system call first appeared in
224.At v1 .
225The
226.Fn unlinkat
227function appeared in
228.Ox 5.0 .
229