]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/unlink.2
grantpt.3: SYNOPSIS: Explicitly show #define _XOPEN_SOURCE requirement
[thirdparty/man-pages.git] / man2 / unlink.2
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 2017-09-15 "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
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 .in -4n
49 Feature Test Macro Requirements for glibc (see
50 .BR feature_test_macros (7)):
51 .in
52 .PP
53 .BR unlinkat ():
54 .PD 0
55 .ad l
56 .RS 4
57 .TP 4
58 Since glibc 2.10:
59 _POSIX_C_SOURCE\ >=\ 200809L
60 .TP
61 Before glibc 2.10:
62 _ATFILE_SOURCE
63 .RE
64 .ad
65 .PD
66 .SH DESCRIPTION
67 .BR unlink ()
68 deletes a name from the filesystem.
69 If that name was the
70 last link to a file and no processes have the file open, the file is
71 deleted and the space it was using is made available for reuse.
72 .PP
73 If the name was the last link to a file but any processes still have
74 the file open, the file will remain in existence until the last file
75 descriptor referring to it is closed.
76 .PP
77 If the name referred to a symbolic link, the link is removed.
78 .PP
79 If the name referred to a socket, FIFO, or device, the name for it is
80 removed but processes which have the object open may continue to use
81 it.
82 .SS unlinkat()
83 The
84 .BR unlinkat ()
85 system call operates in exactly the same way as either
86 .BR unlink ()
87 or
88 .BR rmdir (2)
89 (depending on whether or not
90 .I flags
91 includes the
92 .B AT_REMOVEDIR
93 flag)
94 except for the differences described here.
95 .PP
96 If the pathname given in
97 .I pathname
98 is relative, then it is interpreted relative to the directory
99 referred to by the file descriptor
100 .I dirfd
101 (rather than relative to the current working directory of
102 the calling process, as is done by
103 .BR unlink ()
104 and
105 .BR rmdir (2)
106 for a relative pathname).
107 .PP
108 If the pathname given in
109 .I pathname
110 is relative and
111 .I dirfd
112 is the special value
113 .BR AT_FDCWD ,
114 then
115 .I pathname
116 is interpreted relative to the current working
117 directory of the calling process (like
118 .BR unlink ()
119 and
120 .BR rmdir (2)).
121 .PP
122 If the pathname given in
123 .I pathname
124 is absolute, then
125 .I dirfd
126 is ignored.
127 .PP
128 .I flags
129 is a bit mask that can either be specified as 0, or by ORing
130 together flag values that control the operation of
131 .BR unlinkat ().
132 Currently, only one such flag is defined:
133 .TP
134 .B AT_REMOVEDIR
135 By default,
136 .BR unlinkat ()
137 performs the equivalent of
138 .BR unlink ()
139 on
140 .IR pathname .
141 If the
142 .B AT_REMOVEDIR
143 flag is specified, then
144 performs the equivalent of
145 .BR rmdir (2)
146 on
147 .IR pathname .
148 .PP
149 See
150 .BR openat (2)
151 for an explanation of the need for
152 .BR unlinkat ().
153 .SH RETURN VALUE
154 On success, zero is returned.
155 On error, \-1 is returned, and
156 .I errno
157 is set appropriately.
158 .SH ERRORS
159 .TP
160 .B EACCES
161 Write access to the directory containing
162 .I pathname
163 is not allowed for the process's effective UID, or one of the
164 directories in
165 .I pathname
166 did not allow search permission.
167 (See also
168 .BR path_resolution (7).)
169 .TP
170 .BR EBUSY
171 The file
172 .I pathname
173 cannot be unlinked because it is being used by the system
174 or another process;
175 for example, it is a mount point
176 or the NFS client software created it to represent an
177 active but otherwise nameless inode ("NFS silly renamed").
178 .TP
179 .B EFAULT
180 .I pathname
181 points outside your accessible address space.
182 .TP
183 .B EIO
184 An I/O error occurred.
185 .TP
186 .B EISDIR
187 .I pathname
188 refers to a directory.
189 (This is the non-POSIX value returned by Linux since 2.1.132.)
190 .TP
191 .B ELOOP
192 Too many symbolic links were encountered in translating
193 .IR pathname .
194 .TP
195 .B ENAMETOOLONG
196 .IR pathname " was too long."
197 .TP
198 .B ENOENT
199 A component in
200 .I pathname
201 does not exist or is a dangling symbolic link, or
202 .I pathname
203 is empty.
204 .TP
205 .B ENOMEM
206 Insufficient kernel memory was available.
207 .TP
208 .B ENOTDIR
209 A component used as a directory in
210 .I pathname
211 is not, in fact, a directory.
212 .TP
213 .B EPERM
214 The system does not allow unlinking of directories,
215 or unlinking of directories requires privileges that the
216 calling process doesn't have.
217 (This is the POSIX prescribed error return;
218 as noted above, Linux returns
219 .B EISDIR
220 for this case.)
221 .TP
222 .BR EPERM " (Linux only)"
223 The filesystem does not allow unlinking of files.
224 .TP
225 .BR EPERM " or " EACCES
226 The directory containing
227 .I pathname
228 has the sticky bit
229 .RB ( S_ISVTX )
230 set and the process's effective UID is neither the UID of the file to
231 be deleted nor that of the directory containing it, and
232 the process is not privileged (Linux: does not have the
233 .B CAP_FOWNER
234 capability).
235 .TP
236 .B EPERM
237 The file to be unlinked is marked immutable or append-only.
238 (See
239 .BR ioctl_iflags (2).)
240 .TP
241 .B EROFS
242 .I pathname
243 refers to a file on a read-only filesystem.
244 .PP
245 The same errors that occur for
246 .BR unlink ()
247 and
248 .BR rmdir (2)
249 can also occur for
250 .BR unlinkat ().
251 The following additional errors can occur for
252 .BR unlinkat ():
253 .TP
254 .B EBADF
255 .I dirfd
256 is not a valid file descriptor.
257 .TP
258 .B EINVAL
259 An invalid flag value was specified in
260 .IR flags .
261 .TP
262 .B EISDIR
263 .I pathname
264 refers to a directory, and
265 .B AT_REMOVEDIR
266 was not specified in
267 .IR flags .
268 .TP
269 .B ENOTDIR
270 .I pathname
271 is relative and
272 .I dirfd
273 is a file descriptor referring to a file other than a directory.
274 .SH VERSIONS
275 .BR unlinkat ()
276 was added to Linux in kernel 2.6.16;
277 library support was added to glibc in version 2.4.
278 .SH CONFORMING TO
279 .BR unlink ():
280 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
281 .\" SVr4 documents additional error
282 .\" conditions EINTR, EMULTIHOP, ETXTBSY, ENOLINK.
283 .PP
284 .BR unlinkat ():
285 POSIX.1-2008.
286 .SH NOTES
287 .SS Glibc notes
288 On older kernels where
289 .BR unlinkat ()
290 is unavailable, the glibc wrapper function falls back to the use of
291 .BR unlink ()
292 or
293 .BR rmdir (2).
294 When
295 .I pathname
296 is a relative pathname,
297 glibc constructs a pathname based on the symbolic link in
298 .IR /proc/self/fd
299 that corresponds to the
300 .IR dirfd
301 argument.
302 .SH BUGS
303 Infelicities in the protocol underlying NFS can cause the unexpected
304 disappearance of files which are still being used.
305 .SH SEE ALSO
306 .BR rm (1),
307 .BR unlink (1),
308 .BR chmod (2),
309 .BR link (2),
310 .BR mknod (2),
311 .BR open (2),
312 .BR rename (2),
313 .BR rmdir (2),
314 .BR mkfifo (3),
315 .BR remove (3),
316 .BR path_resolution (7),
317 .BR symlink (7)