null.c revision 111119
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 111119 2003-02-19 05:47:46Z imp $ 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#include <machine/resource.h> 40#include <sys/rman.h> 41 42/* For use with destroy_dev(9). */ 43static dev_t null_dev; 44static dev_t zero_dev; 45 46static d_write_t null_write; 47static d_ioctl_t null_ioctl; 48static d_read_t zero_read; 49 50#define CDEV_MAJOR 2 51#define NULL_MINOR 2 52#define ZERO_MINOR 12 53 54static struct cdevsw null_cdevsw = { 55 /* open */ (d_open_t *)nullop, 56 /* close */ (d_close_t *)nullop, 57 /* read */ (d_read_t *)nullop, 58 /* write */ null_write, 59 /* ioctl */ null_ioctl, 60 /* poll */ nopoll, 61 /* mmap */ nommap, 62 /* strategy */ nostrategy, 63 /* name */ "null", 64 /* maj */ CDEV_MAJOR, 65 /* dump */ nodump, 66 /* psize */ nopsize, 67 /* flags */ 0, 68 /* kqfilter */ NULL 69}; 70 71static struct cdevsw zero_cdevsw = { 72 /* open */ (d_open_t *)nullop, 73 /* close */ (d_close_t *)nullop, 74 /* read */ zero_read, 75 /* write */ null_write, 76 /* ioctl */ noioctl, 77 /* poll */ nopoll, 78 /* mmap */ nommap, 79 /* strategy */ nostrategy, 80 /* name */ "zero", 81 /* maj */ CDEV_MAJOR, 82 /* dump */ nodump, 83 /* psize */ nopsize, 84 /* flags */ D_MMAP_ANON, 85 /* kqfilter */ NULL 86}; 87 88static void *zbuf; 89 90static int 91null_write(dev_t dev, struct uio *uio, int flags) 92{ 93 uio->uio_resid = 0; 94 return 0; 95} 96 97static int 98null_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 99{ 100 int error; 101 102 if (cmd != DIOCSKERNELDUMP) 103 return (noioctl(dev, cmd, data, flags, td)); 104 error = suser(td); 105 if (error) 106 return (error); 107 return (set_dumper(NULL)); 108} 109 110static int 111zero_read(dev_t dev, struct uio *uio, int flags) 112{ 113 u_int c; 114 int error = 0; 115 116 while (uio->uio_resid > 0 && error == 0) { 117 c = min(uio->uio_resid, PAGE_SIZE); 118 error = uiomove(zbuf, c, uio); 119 } 120 return error; 121} 122 123static int 124null_modevent(module_t mod, int type, void *data) 125{ 126 switch(type) { 127 case MOD_LOAD: 128 if (bootverbose) 129 printf("null: <null device, zero device>\n"); 130 zbuf = (void *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK | M_ZERO); 131 zero_dev = make_dev(&zero_cdevsw, ZERO_MINOR, UID_ROOT, 132 GID_WHEEL, 0666, "zero"); 133 null_dev = make_dev(&null_cdevsw, NULL_MINOR, UID_ROOT, 134 GID_WHEEL, 0666, "null"); 135 return 0; 136 137 case MOD_UNLOAD: 138 destroy_dev(null_dev); 139 destroy_dev(zero_dev); 140 free(zbuf, M_TEMP); 141 return 0; 142 143 case MOD_SHUTDOWN: 144 return 0; 145 146 default: 147 return EOPNOTSUPP; 148 } 149} 150 151DEV_MODULE(null, null_modevent, NULL); 152