xref: /illumos-gate/usr/src/uts/common/syscall/unlink.c (revision 8fd04b83)
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 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/isa_defs.h>
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/vnode.h>
42 #include <sys/uio.h>
43 #include <sys/debug.h>
44 #include <sys/file.h>
45 #include <sys/fcntl.h>
46 #include <c2/audit.h>
47 
48 /*
49  * Unlink a file from a directory
50  */
51 int
52 unlinkat(int fd, char *name, int flags)
53 {
54 	file_t *dirfp;
55 	vnode_t *dirvp;
56 	int error;
57 	char startchar;
58 
59 	if (fd == AT_FDCWD && name == NULL)
60 		return (set_errno(EFAULT));
61 
62 	if (name != NULL) {
63 		if (copyin(name, &startchar, sizeof (char)))
64 			return (set_errno(EFAULT));
65 	} else
66 		startchar = '\0';
67 
68 	if (fd == AT_FDCWD) {
69 		dirvp = NULL;
70 	} else {
71 		if (startchar != '/') {
72 			if ((dirfp = getf(fd)) == NULL) {
73 				return (set_errno(EBADF));
74 			}
75 			dirvp = dirfp->f_vnode;
76 			VN_HOLD(dirvp);
77 			releasef(fd);
78 		} else {
79 			dirvp = NULL;
80 		}
81 	}
82 
83 	if (audit_active)
84 		audit_setfsat_path(1);
85 
86 	error = vn_removeat(dirvp, name,
87 	    UIO_USERSPACE, (flags == AT_REMOVEDIR) ? RMDIRECTORY : RMFILE);
88 	if (dirvp != NULL)
89 		VN_RELE(dirvp);
90 
91 	if (error != NULL)
92 		return (set_errno(error));
93 	return (0);
94 }
95 
96 int
97 unlink(char *name)
98 {
99 	return (unlinkat(AT_FDCWD, name, 0));
100 }
101 
102 int
103 rmdir(char *name)
104 {
105 	return (unlinkat(AT_FDCWD, name, AT_REMOVEDIR));
106 }
107