null.c revision 221853
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: head/sys/dev/null/null.c 221853 2011-05-13 18:48:00Z mdf $"); 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 <machine/bus.h> 43 44/* For use with destroy_dev(9). */ 45static struct cdev *null_dev; 46static struct cdev *zero_dev; 47 48static d_write_t null_write; 49static d_ioctl_t null_ioctl; 50static d_read_t zero_read; 51 52static struct cdevsw null_cdevsw = { 53 .d_version = D_VERSION, 54 .d_read = (d_read_t *)nullop, 55 .d_write = null_write, 56 .d_ioctl = null_ioctl, 57 .d_name = "null", 58}; 59 60static struct cdevsw zero_cdevsw = { 61 .d_version = D_VERSION, 62 .d_read = zero_read, 63 .d_write = null_write, 64 .d_name = "zero", 65 .d_flags = D_MMAP_ANON, 66}; 67 68/* ARGSUSED */ 69static int 70null_write(struct cdev *dev __unused, struct uio *uio, int flags __unused) 71{ 72 uio->uio_resid = 0; 73 74 return (0); 75} 76 77/* ARGSUSED */ 78static int 79null_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data __unused, 80 int flags __unused, struct thread *td) 81{ 82 int error; 83 84 if (cmd != DIOCSKERNELDUMP) 85 return (ENOIOCTL); 86 error = priv_check(td, PRIV_SETDUMPER); 87 if (error) 88 return (error); 89 return (set_dumper(NULL)); 90} 91 92/* ARGSUSED */ 93static int 94zero_read(struct cdev *dev __unused, struct uio *uio, int flags __unused) 95{ 96 void *zbuf; 97 ssize_t len; 98 int error = 0; 99 100 KASSERT(uio->uio_rw == UIO_READ, 101 ("Can't be in %s for write", __func__)); 102 zbuf = __DECONST(void *, zero_region); 103 while (uio->uio_resid > 0 && error == 0) { 104 len = uio->uio_resid; 105 if (len > ZERO_REGION_SIZE) 106 len = ZERO_REGION_SIZE; 107 error = uiomove(zbuf, len, uio); 108 } 109 110 return (error); 111} 112 113/* ARGSUSED */ 114static int 115null_modevent(module_t mod __unused, int type, void *data __unused) 116{ 117 switch(type) { 118 case MOD_LOAD: 119 if (bootverbose) 120 printf("null: <null device, zero device>\n"); 121 null_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &null_cdevsw, 0, 122 NULL, UID_ROOT, GID_WHEEL, 0666, "null"); 123 zero_dev = make_dev_credf(MAKEDEV_ETERNAL_KLD, &zero_cdevsw, 0, 124 NULL, UID_ROOT, GID_WHEEL, 0666, "zero"); 125 break; 126 127 case MOD_UNLOAD: 128 destroy_dev(null_dev); 129 destroy_dev(zero_dev); 130 break; 131 132 case MOD_SHUTDOWN: 133 break; 134 135 default: 136 return (EOPNOTSUPP); 137 } 138 139 return (0); 140} 141 142DEV_MODULE(null, null_modevent, NULL); 143MODULE_VERSION(null, 1); 144