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