FreeBSD/Linux Kernel Cross Reference
sys/dev/null/null.c
1 /*-
2 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen
3 * Copyright (c) 2001-2004 Mark R. V. Murray
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/uio.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/priv.h>
40 #include <sys/disk.h>
41 #include <sys/bus.h>
42 #include <sys/filio.h>
43
44 #include <machine/bus.h>
45 #include <machine/vmparam.h>
46
47 /* For use with destroy_dev(9). */
48 static struct cdev *null_dev;
49 static struct cdev *zero_dev;
50
51 static d_write_t null_write;
52 static d_ioctl_t null_ioctl;
53 static d_ioctl_t zero_ioctl;
54 static d_read_t zero_read;
55
56 static struct cdevsw null_cdevsw = {
57 .d_version = D_VERSION,
58 .d_read = (d_read_t *)nullop,
59 .d_write = null_write,
60 .d_ioctl = null_ioctl,
61 .d_name = "null",
62 };
63
64 static struct cdevsw zero_cdevsw = {
65 .d_version = D_VERSION,
66 .d_read = zero_read,
67 .d_write = null_write,
68 .d_ioctl = zero_ioctl,
69 .d_name = "zero",
70 .d_flags = D_MMAP_ANON,
71 };
72
73 /* ARGSUSED */
74 static int
75 null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused)
76 {
77 uio->uio_resid = 0;
78
79 return (0);
80 }
81
82 /* ARGSUSED */
83 static int
84 null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
85 int flags __unused, struct thread *td)
86 {
87 int error;
88 error = 0;
89
90 switch (cmd) {
91 case DIOCSKERNELDUMP:
92 error = priv_check(td, PRIV_SETDUMPER);
93 if (error == 0)
94 error = set_dumper(NULL);
95 break;
96 case FIONBIO:
97 break;
98 case FIOASYNC:
99 if (*(int *)data != 0)
100 error = EINVAL;
101 break;
102 default:
103 error = ENOIOCTL;
104 }
105 return (error);
106 }
107
108 /* ARGSUSED */
109 static int
110 zero_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused,
111 int flags __unused, struct thread *td)
112 {
113 int error;
114 error = 0;
115
116 switch (cmd) {
117 case FIONBIO:
118 break;
119 case FIOASYNC:
120 if (*(int *)data != 0)
121 error = EINVAL;
122 break;
123 default:
124 error = ENOIOCTL;
125 }
126 return (error);
127 }
128
129
130 /* ARGSUSED */
131 static int
132 zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused)
133 {
134 void *zbuf;
135 ssize_t len;
136 int error = 0;
137
138 KASSERT(uio->uio_rw == UIO_READ,
139 ("Can't be in %s for write", __func__));
140 zbuf = __DECONST(void *, zero_region);
141 while (uio->uio_resid > 0 && error == 0) {
142 len = uio->uio_resid;
143 if (len > ZERO_REGION_SIZE)
144 len = ZERO_REGION_SIZE;
145 error = uiomove(zbuf, len, uio);
146 }
147
148 return (error);
149 }
150
151 /* ARGSUSED */
152 static int
153 null_modevent(module_t mod __unused, int type, void *data __unused)
154 {
155 switch(type) {
156 case MOD_LOAD:
157 if (bootverbose)
158 printf("null: <null device, zero device>\n");
159 null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0,
160 NULL, UID_ROOT, GID_WHEEL, 0666, "null");
161 zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0,
162 NULL, UID_ROOT, GID_WHEEL, 0666, "zero");
163 break;
164
165 case MOD_UNLOAD:
166 destroy_dev(null_dev);
167 destroy_dev(zero_dev);
168 break;
169
170 case MOD_SHUTDOWN:
171 break;
172
173 default:
174 return (EOPNOTSUPP);
175 }
176
177 return (0);
178 }
179
180 DEV_MODULE(null, null_modevent, NULL);
181 MODULE_VERSION(null, 1);
Cache object: abef6212fe0c3580ca7fadacef0b0e10
|