namespaces.7: ffix
[man-pages.git] / man2 / unlink.2
blob6f4c187f8a9d29ddd79d505b785a1ee5a956efe8
1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\"             and Copyright (C) 1993 Ian Jackson
3 .\"             and Copyright (C) 2006, 2014 Michael Kerrisk.
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1996-09-08 by Arnt Gulbrandsen <agulbra@troll.no>
29 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 2001-05-17 by aeb
31 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
32 .\"
33 .TH UNLINK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 unlink, unlinkat \- delete a name and possibly the file it refers to
36 .SH SYNOPSIS
37 .nf
38 .B #include <unistd.h>
39 .PP
40 .BI "int unlink(const char *" pathname );
41 .PP
42 .BR "#include <fcntl.h>           " "/* Definition of " AT_* " constants */"
43 .B #include <unistd.h>
44 .PP
45 .BI "int unlinkat(int " dirfd ", const char *" pathname ", int " flags );
46 .fi
47 .PP
48 .RS -4
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
51 .RE
52 .PP
53 .BR unlinkat ():
54 .nf
55     Since glibc 2.10:
56         _POSIX_C_SOURCE >= 200809L
57     Before glibc 2.10:
58         _ATFILE_SOURCE
59 .fi
60 .SH DESCRIPTION
61 .BR unlink ()
62 deletes a name from the filesystem.
63 If that name was the
64 last link to a file and no processes have the file open, the file is
65 deleted and the space it was using is made available for reuse.
66 .PP
67 If the name was the last link to a file but any processes still have
68 the file open, the file will remain in existence until the last file
69 descriptor referring to it is closed.
70 .PP
71 If the name referred to a symbolic link, the link is removed.
72 .PP
73 If the name referred to a socket, FIFO, or device, the name for it is
74 removed but processes which have the object open may continue to use
75 it.
76 .SS unlinkat()
77 The
78 .BR unlinkat ()
79 system call operates in exactly the same way as either
80 .BR unlink ()
82 .BR rmdir (2)
83 (depending on whether or not
84 .I flags
85 includes the
86 .B AT_REMOVEDIR
87 flag)
88 except for the differences described here.
89 .PP
90 If the pathname given in
91 .I pathname
92 is relative, then it is interpreted relative to the directory
93 referred to by the file descriptor
94 .I dirfd
95 (rather than relative to the current working directory of
96 the calling process, as is done by
97 .BR unlink ()
98 and
99 .BR rmdir (2)
100 for a relative pathname).
102 If the pathname given in
103 .I pathname
104 is relative and
105 .I dirfd
106 is the special value
107 .BR AT_FDCWD ,
108 then
109 .I pathname
110 is interpreted relative to the current working
111 directory of the calling process (like
112 .BR unlink ()
114 .BR rmdir (2)).
116 If the pathname given in
117 .I pathname
118 is absolute, then
119 .I dirfd
120 is ignored.
122 .I flags
123 is a bit mask that can either be specified as 0, or by ORing
124 together flag values that control the operation of
125 .BR unlinkat ().
126 Currently, only one such flag is defined:
128 .B AT_REMOVEDIR
129 By default,
130 .BR unlinkat ()
131 performs the equivalent of
132 .BR unlink ()
134 .IR pathname .
135 If the
136 .B AT_REMOVEDIR
137 flag is specified, then
138 performs the equivalent of
139 .BR rmdir (2)
141 .IR pathname .
144 .BR openat (2)
145 for an explanation of the need for
146 .BR unlinkat ().
147 .SH RETURN VALUE
148 On success, zero is returned.
149 On error, \-1 is returned, and
150 .I errno
151 is set to indicate the error.
152 .SH ERRORS
154 .B EACCES
155 Write access to the directory containing
156 .I pathname
157 is not allowed for the process's effective UID, or one of the
158 directories in
159 .I pathname
160 did not allow search permission.
161 (See also
162 .BR path_resolution (7).)
164 .BR EBUSY
165 The file
166 .I pathname
167 cannot be unlinked because it is being used by the system
168 or another process;
169 for example, it is a mount point
170 or the NFS client software created it to represent an
171 active but otherwise nameless inode ("NFS silly renamed").
173 .B EFAULT
174 .I pathname
175 points outside your accessible address space.
177 .B EIO
178 An I/O error occurred.
180 .B EISDIR
181 .I pathname
182 refers to a directory.
183 (This is the non-POSIX value returned by Linux since 2.1.132.)
185 .B ELOOP
186 Too many symbolic links were encountered in translating
187 .IR pathname .
189 .B ENAMETOOLONG
190 .IR pathname " was too long."
192 .B ENOENT
193 A component in
194 .I pathname
195 does not exist or is a dangling symbolic link, or
196 .I pathname
197 is empty.
199 .B ENOMEM
200 Insufficient kernel memory was available.
202 .B ENOTDIR
203 A component used as a directory in
204 .I pathname
205 is not, in fact, a directory.
207 .B EPERM
208 The system does not allow unlinking of directories,
209 or unlinking of directories requires privileges that the
210 calling process doesn't have.
211 (This is the POSIX prescribed error return;
212 as noted above, Linux returns
213 .B EISDIR
214 for this case.)
216 .BR EPERM " (Linux only)"
217 The filesystem does not allow unlinking of files.
219 .BR EPERM " or " EACCES
220 The directory containing
221 .I pathname
222 has the sticky bit
223 .RB ( S_ISVTX )
224 set and the process's effective UID is neither the UID of the file to
225 be deleted nor that of the directory containing it, and
226 the process is not privileged (Linux: does not have the
227 .B CAP_FOWNER
228 capability).
230 .B EPERM
231 The file to be unlinked is marked immutable or append-only.
232 (See
233 .BR ioctl_iflags (2).)
235 .B EROFS
236 .I pathname
237 refers to a file on a read-only filesystem.
239 The same errors that occur for
240 .BR unlink ()
242 .BR rmdir (2)
243 can also occur for
244 .BR unlinkat ().
245 The following additional errors can occur for
246 .BR unlinkat ():
248 .B EBADF
249 .I dirfd
250 is not a valid file descriptor.
252 .B EINVAL
253 An invalid flag value was specified in
254 .IR flags .
256 .B EISDIR
257 .I pathname
258 refers to a directory, and
259 .B AT_REMOVEDIR
260 was not specified in
261 .IR flags .
263 .B ENOTDIR
264 .I pathname
265 is relative and
266 .I dirfd
267 is a file descriptor referring to a file other than a directory.
268 .SH VERSIONS
269 .BR unlinkat ()
270 was added to Linux in kernel 2.6.16;
271 library support was added to glibc in version 2.4.
272 .SH CONFORMING TO
273 .BR unlink ():
274 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
275 .\" SVr4 documents additional error
276 .\" conditions EINTR, EMULTIHOP, ETXTBSY, ENOLINK.
278 .BR unlinkat ():
279 POSIX.1-2008.
280 .SH NOTES
281 .SS Glibc notes
282 On older kernels where
283 .BR unlinkat ()
284 is unavailable, the glibc wrapper function falls back to the use of
285 .BR unlink ()
287 .BR rmdir (2).
288 When
289 .I pathname
290 is a relative pathname,
291 glibc constructs a pathname based on the symbolic link in
292 .IR /proc/self/fd
293 that corresponds to the
294 .IR dirfd
295 argument.
296 .SH BUGS
297 Infelicities in the protocol underlying NFS can cause the unexpected
298 disappearance of files which are still being used.
299 .SH SEE ALSO
300 .BR rm (1),
301 .BR unlink (1),
302 .BR chmod (2),
303 .BR link (2),
304 .BR mknod (2),
305 .BR open (2),
306 .BR rename (2),
307 .BR rmdir (2),
308 .BR mkfifo (3),
309 .BR remove (3),
310 .BR path_resolution (7),
311 .BR symlink (7)