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