xref: /titanic_41/usr/src/uts/common/syscall/mknod.c (revision 3eae19d9cf3390cf5b75e10c9c1945fd36ad856a)
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/user.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/stat.h>
44 #include <sys/vnode.h>
45 #include <sys/mode.h>
46 #include <sys/uio.h>
47 #include <sys/mkdev.h>
48 #include <sys/policy.h>
49 #include <sys/debug.h>
50 
51 /*
52  * Create a special file, a regular file, or a FIFO.
53  * fname - pathname passed by user
54  * fmode - mode of pathname
55  * dev = device number - b/c specials only
56  */
57 int
58 mknod(char *fname, mode_t fmode, dev_t dev)
59 {
60 	vnode_t *vp;
61 	struct vattr vattr;
62 	int error;
63 	enum create why;
64 
65 	/*
66 	 * Zero type is equivalent to a regular file.
67 	 */
68 	if ((fmode & S_IFMT) == 0)
69 		fmode |= S_IFREG;
70 
71 	/*
72 	 * Must be privileged unless making a FIFO node.
73 	 */
74 	if (((fmode & S_IFMT) != S_IFIFO) && secpolicy_sys_devices(CRED()) != 0)
75 		return (set_errno(EPERM));
76 	/*
77 	 * Set up desired attributes and vn_create the file.
78 	 */
79 	vattr.va_type = IFTOVT(fmode);
80 	vattr.va_mode = fmode & MODEMASK;
81 	vattr.va_mask = AT_TYPE|AT_MODE;
82 	if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
83 		if (get_udatamodel() != DATAMODEL_NATIVE)
84 			dev = expldev(dev);
85 		if (dev == NODEV || (getemajor(dev)) == (major_t)NODEV)
86 			return (set_errno(EINVAL));
87 		vattr.va_rdev = dev;
88 		vattr.va_mask |= AT_RDEV;
89 	}
90 	why = ((fmode & S_IFMT) == S_IFDIR) ? CRMKDIR : CRMKNOD;
91 	if (error = vn_create(fname, UIO_USERSPACE, &vattr, EXCL, 0, &vp,
92 	    why, 0,  PTOU(curproc)->u_cmask))
93 		return (set_errno(error));
94 	VN_RELE(vp);
95 	return (0);
96 }
97 
98 #if defined(__i386) || defined(__i386_COMPAT)
99 
100 /*ARGSUSED*/
101 int
102 xmknod(int version, char *fname, mode_t fmode, dev_t dev)
103 {
104 	return (mknod(fname, fmode, dev));
105 }
106 
107 #endif
108