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