null.c revision 116794
1/*- 2 * Copyright (c) 2000 Mark R. V. Murray & Jeroen C. van Gelderen 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/dev/null/null.c 116794 2003-06-24 19:50:48Z phk $ 27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/conf.h> 32#include <sys/uio.h> 33#include <sys/kernel.h> 34#include <sys/malloc.h> 35#include <sys/module.h> 36#include <sys/disk.h> 37#include <sys/bus.h> 38#include <machine/bus.h> 39 40/* For use with destroy_dev(9). */ 41static dev_t null_dev; 42static dev_t zero_dev; 43 44static d_write_t null_write; 45static d_ioctl_t null_ioctl; 46static d_read_t zero_read; 47static d_read_t null_read; 48 49#define CDEV_MAJOR 2 50#define NULL_MINOR 2 51#define ZERO_MINOR 12 52 53static struct cdevsw null_cdevsw = { 54 .d_open = nullopen, 55 .d_close = nullclose, 56 .d_read = null_read, 57 .d_write = null_write, 58 .d_ioctl = null_ioctl, 59 .d_name = "null", 60 .d_maj = CDEV_MAJOR, 61 .d_flags = D_NOGIANT, 62}; 63 64static struct cdevsw zero_cdevsw = { 65 .d_open = nullopen, 66 .d_close = nullclose, 67 .d_read = zero_read, 68 .d_write = null_write, 69 .d_name = "zero", 70 .d_maj = CDEV_MAJOR, 71 .d_flags = D_MMAP_ANON | D_NOGIANT, 72}; 73 74static void *zbuf; 75 76static int 77null_read(dev_t dev __unused, struct uio *uio, int flags __unused) 78{ 79 80 return 0; 81} 82 83/* ARGSUSED */ 84static int 85null_write(dev_t dev __unused, struct uio *uio, int flags __unused) 86{ 87 uio->uio_resid = 0; 88 return 0; 89} 90 91static int 92null_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 93{ 94 int error; 95 96 if (cmd != DIOCSKERNELDUMP) 97 return (noioctl(dev, cmd, data, flags, td)); 98 error = suser(td); 99 if (error) 100 return (error); 101 return (set_dumper(NULL)); 102} 103 104/* ARGSUSED */ 105static int 106zero_read(dev_t dev __unused, struct uio *uio, int flags __unused) 107{ 108 int c; 109 int error = 0; 110 111 while (uio->uio_resid > 0 && error == 0) { 112 c = uio->uio_resid < PAGE_SIZE ? uio->uio_resid : PAGE_SIZE; 113 error = uiomove(zbuf, c, uio); 114 } 115 return error; 116} 117 118/* ARGSUSED */ 119static int 120null_modevent(module_t mod __unused, int type, void *data __unused) 121{ 122 switch(type) { 123 case MOD_LOAD: 124 if (bootverbose) 125 printf("null: <null device, zero device>\n"); 126 zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK | M_ZERO); 127 zero_dev = make_dev(&zero_cdevsw, ZERO_MINOR, UID_ROOT, 128 GID_WHEEL, 0666, "zero"); 129 null_dev = make_dev(&null_cdevsw, NULL_MINOR, UID_ROOT, 130 GID_WHEEL, 0666, "null"); 131 return 0; 132 133 case MOD_UNLOAD: 134 destroy_dev(null_dev); 135 destroy_dev(zero_dev); 136 free(zbuf, M_TEMP); 137 return 0; 138 139 case MOD_SHUTDOWN: 140 return 0; 141 142 default: 143 return EOPNOTSUPP; 144 } 145} 146 147DEV_MODULE(null, null_modevent, NULL); 148