xref: /illumos-gate/usr/src/uts/common/syscall/unlink.c (revision e38a713ad4e0a9c42f8cccd9350412b2c6ccccdb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/param.h>
37 #include <sys/isa_defs.h>
38 #include <sys/types.h>
39 #include <sys/sysmacros.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/vnode.h>
43 #include <sys/uio.h>
44 #include <sys/debug.h>
45 #include <sys/file.h>
46 #include <sys/fcntl.h>
47 #include <c2/audit.h>
48 
49 /*
50  * Unlink (i.e. delete) a file.
51  */
52 int
53 unlink(char *fname)
54 {
55 	int	error;
56 
57 	if (error = vn_remove(fname, UIO_USERSPACE, RMFILE))
58 		return (set_errno(error));
59 	return (0);
60 }
61 
62 /*
63  * Unlink a file from a directory
64  */
65 int
66 unlinkat(int fd, char *name, int flags)
67 {
68 	file_t *dirfp;
69 	vnode_t *dirvp;
70 	int error;
71 	char startchar;
72 
73 	if (fd == AT_FDCWD && name == NULL)
74 		return (set_errno(EFAULT));
75 
76 	if (name != NULL) {
77 		if (copyin(name, &startchar, sizeof (char)))
78 			return (set_errno(EFAULT));
79 	} else
80 		startchar = '\0';
81 
82 	if (fd == AT_FDCWD) {
83 		dirvp = NULL;
84 	} else {
85 		if (startchar != '/') {
86 			if ((dirfp = getf(fd)) == NULL) {
87 				return (set_errno(EBADF));
88 			}
89 			dirvp = dirfp->f_vnode;
90 			VN_HOLD(dirvp);
91 			releasef(fd);
92 		} else {
93 			dirvp = NULL;
94 		}
95 	}
96 
97 	if (audit_active)
98 		audit_setfsat_path(1);
99 
100 	error = vn_removeat(dirvp, name,
101 	    UIO_USERSPACE, (flags == AT_REMOVEDIR) ? RMDIRECTORY : RMFILE);
102 	if (dirvp != NULL)
103 		VN_RELE(dirvp);
104 
105 	if (error != NULL)
106 		return (set_errno(error));
107 	return (0);
108 }
109