xref: /illumos-gate/usr/src/uts/common/syscall/unlink.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2001 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/param.h>
38 #include <sys/isa_defs.h>
39 #include <sys/types.h>
40 #include <sys/sysmacros.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/vnode.h>
44 #include <sys/uio.h>
45 #include <sys/debug.h>
46 #include <sys/file.h>
47 #include <sys/fcntl.h>
48 #include <c2/audit.h>
49 
50 /*
51  * Unlink (i.e. delete) a file.
52  */
53 int
54 unlink(char *fname)
55 {
56 	int	error;
57 
58 	if (error = vn_remove(fname, UIO_USERSPACE, RMFILE))
59 		return (set_errno(error));
60 	return (0);
61 }
62 
63 /*
64  * Unlink a file from a directory
65  */
66 int
67 unlinkat(int fd, char *name, int flags)
68 {
69 	file_t *dirfp;
70 	vnode_t *dirvp;
71 	int error;
72 	char startchar;
73 
74 	if (fd == AT_FDCWD && name == NULL)
75 		return (set_errno(EFAULT));
76 
77 	if (name != NULL) {
78 		if (copyin(name, &startchar, sizeof (char)))
79 			return (set_errno(EFAULT));
80 	} else
81 		startchar = '\0';
82 
83 	if (fd == AT_FDCWD) {
84 		dirvp = NULL;
85 	} else {
86 		if (startchar != '/') {
87 			if ((dirfp = getf(fd)) == NULL) {
88 				return (set_errno(EBADF));
89 			}
90 			dirvp = dirfp->f_vnode;
91 			VN_HOLD(dirvp);
92 			releasef(fd);
93 		} else {
94 			dirvp = NULL;
95 		}
96 	}
97 
98 #ifdef C2_AUDIT
99 	if (audit_active)
100 		audit_setfsat_path(1);
101 #endif /* C2_AUDIT */
102 
103 	error = vn_removeat(dirvp, name,
104 		UIO_USERSPACE, (flags == AT_REMOVEDIR) ? RMDIRECTORY : RMFILE);
105 	if (dirvp != NULL)
106 		VN_RELE(dirvp);
107 
108 	if (error != NULL)
109 		return (set_errno(error));
110 	return (0);
111 }
112