man-pages/man2/unlink.2.html
2021-03-31 01:06:50 +01:00

479 lines
9.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of UNLINK</TITLE>
</HEAD><BODY>
<H1>UNLINK</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
unlink, unlinkat - delete a name and possibly the file it refers to
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>int unlink(const char *</B><I>pathname</I><B>);</B>
<B>#include &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt; </B>/* Definition of AT_* constants */
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>int unlinkat(int </B><I>dirfd</I><B>, const char *</B><I>pathname</I><B>, int </B><I>flags</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>unlinkat</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<DT id="3">Before glibc 2.10:<DD>
_ATFILE_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>unlink</B>()
deletes a name from the filesystem.
If that name was the
last link to a file and no processes have the file open, the file is
deleted and the space it was using is made available for reuse.
<P>
If the name was the last link to a file but any processes still have
the file open, the file will remain in existence until the last file
descriptor referring to it is closed.
<P>
If the name referred to a symbolic link, the link is removed.
<P>
If the name referred to a socket, FIFO, or device, the name for it is
removed but processes which have the object open may continue to use
it.
<A NAME="lbAE">&nbsp;</A>
<H3>unlinkat()</H3>
The
<B>unlinkat</B>()
system call operates in exactly the same way as either
<B>unlink</B>()
or
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2)
(depending on whether or not
<I>flags</I>
includes the
<B>AT_REMOVEDIR</B>
flag)
except for the differences described here.
<P>
If the pathname given in
<I>pathname</I>
is relative, then it is interpreted relative to the directory
referred to by the file descriptor
<I>dirfd</I>
(rather than relative to the current working directory of
the calling process, as is done by
<B>unlink</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2)
for a relative pathname).
<P>
If the pathname given in
<I>pathname</I>
is relative and
<I>dirfd</I>
is the special value
<B>AT_FDCWD</B>,
then
<I>pathname</I>
is interpreted relative to the current working
directory of the calling process (like
<B>unlink</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2)).
<P>
If the pathname given in
<I>pathname</I>
is absolute, then
<I>dirfd</I>
is ignored.
<P>
<I>flags</I>
is a bit mask that can either be specified as 0, or by ORing
together flag values that control the operation of
<B>unlinkat</B>().
Currently, only one such flag is defined:
<DL COMPACT>
<DT id="4"><B>AT_REMOVEDIR</B>
<DD>
By default,
<B>unlinkat</B>()
performs the equivalent of
<B>unlink</B>()
on
<I>pathname</I>.
If the
<B>AT_REMOVEDIR</B>
flag is specified, then
performs the equivalent of
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2)
on
<I>pathname</I>.
</DL>
<P>
See
<B><A HREF="/cgi-bin/man/man2html?2+openat">openat</A></B>(2)
for an explanation of the need for
<B>unlinkat</B>().
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, zero is returned.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAG">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="5"><B>EACCES</B>
<DD>
Write access to the directory containing
<I>pathname</I>
is not allowed for the process's effective UID, or one of the
directories in
<I>pathname</I>
did not allow search permission.
(See also
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
<DT id="6"><B>EBUSY</B>
<DD>
The file
<I>pathname</I>
cannot be unlinked because it is being used by the system
or another process;
for example, it is a mount point
or the NFS client software created it to represent an
active but otherwise nameless inode (&quot;NFS silly renamed&quot;).
<DT id="7"><B>EFAULT</B>
<DD>
<I>pathname</I>
points outside your accessible address space.
<DT id="8"><B>EIO</B>
<DD>
An I/O error occurred.
<DT id="9"><B>EISDIR</B>
<DD>
<I>pathname</I>
refers to a directory.
(This is the non-POSIX value returned by Linux since 2.1.132.)
<DT id="10"><B>ELOOP</B>
<DD>
Too many symbolic links were encountered in translating
<I>pathname</I>.
<DT id="11"><B>ENAMETOOLONG</B>
<DD>
<I>pathname</I> was too long.
<DT id="12"><B>ENOENT</B>
<DD>
A component in
<I>pathname</I>
does not exist or is a dangling symbolic link, or
<I>pathname</I>
is empty.
<DT id="13"><B>ENOMEM</B>
<DD>
Insufficient kernel memory was available.
<DT id="14"><B>ENOTDIR</B>
<DD>
A component used as a directory in
<I>pathname</I>
is not, in fact, a directory.
<DT id="15"><B>EPERM</B>
<DD>
The system does not allow unlinking of directories,
or unlinking of directories requires privileges that the
calling process doesn't have.
(This is the POSIX prescribed error return;
as noted above, Linux returns
<B>EISDIR</B>
for this case.)
<DT id="16"><B>EPERM</B> (Linux only)
<DD>
The filesystem does not allow unlinking of files.
<DT id="17"><B>EPERM</B> or <B>EACCES</B>
<DD>
The directory containing
<I>pathname</I>
has the sticky bit
(<B>S_ISVTX</B>)
set and the process's effective UID is neither the UID of the file to
be deleted nor that of the directory containing it, and
the process is not privileged (Linux: does not have the
<B>CAP_FOWNER</B>
capability).
<DT id="18"><B>EPERM</B>
<DD>
The file to be unlinked is marked immutable or append-only.
(See
<B><A HREF="/cgi-bin/man/man2html?2+ioctl_iflags">ioctl_iflags</A></B>(2).)
<DT id="19"><B>EROFS</B>
<DD>
<I>pathname</I>
refers to a file on a read-only filesystem.
</DL>
<P>
The same errors that occur for
<B>unlink</B>()
and
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2)
can also occur for
<B>unlinkat</B>().
The following additional errors can occur for
<B>unlinkat</B>():
<DL COMPACT>
<DT id="20"><B>EBADF</B>
<DD>
<I>dirfd</I>
is not a valid file descriptor.
<DT id="21"><B>EINVAL</B>
<DD>
An invalid flag value was specified in
<I>flags</I>.
<DT id="22"><B>EISDIR</B>
<DD>
<I>pathname</I>
refers to a directory, and
<B>AT_REMOVEDIR</B>
was not specified in
<I>flags</I>.
<DT id="23"><B>ENOTDIR</B>
<DD>
<I>pathname</I>
is relative and
<I>dirfd</I>
is a file descriptor referring to a file other than a directory.
</DL>
<A NAME="lbAH">&nbsp;</A>
<H2>VERSIONS</H2>
<B>unlinkat</B>()
was added to Linux in kernel 2.6.16;
library support was added to glibc in version 2.4.
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>unlink</B>():
SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
<P>
<B>unlinkat</B>():
POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
<A NAME="lbAK">&nbsp;</A>
<H3>Glibc notes</H3>
On older kernels where
<B>unlinkat</B>()
is unavailable, the glibc wrapper function falls back to the use of
<B>unlink</B>()
or
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2).
When
<I>pathname</I>
is a relative pathname,
glibc constructs a pathname based on the symbolic link in
<I>/proc/self/fd</I>
that corresponds to the
<I>dirfd</I>
argument.
<A NAME="lbAL">&nbsp;</A>
<H2>BUGS</H2>
Infelicities in the protocol underlying NFS can cause the unexpected
disappearance of files which are still being used.
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+rm">rm</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?1+unlink">unlink</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+chmod">chmod</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+link">link</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mknod">mknod</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+rename">rename</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+rmdir">rmdir</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+mkfifo">mkfifo</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+remove">remove</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+symlink">symlink</A></B>(7)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="24"><A HREF="#lbAB">NAME</A><DD>
<DT id="25"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="26"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="27"><A HREF="#lbAE">unlinkat()</A><DD>
</DL>
<DT id="28"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="29"><A HREF="#lbAG">ERRORS</A><DD>
<DT id="30"><A HREF="#lbAH">VERSIONS</A><DD>
<DT id="31"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="32"><A HREF="#lbAJ">NOTES</A><DD>
<DL>
<DT id="33"><A HREF="#lbAK">Glibc notes</A><DD>
</DL>
<DT id="34"><A HREF="#lbAL">BUGS</A><DD>
<DT id="35"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="36"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:35 GMT, March 31, 2021
</BODY>
</HTML>