/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Implementation of the null device, "null:", which generates an31* immediate EOF on read and throws away anything written to it.32*/33#include <types.h>34#include <kern/errno.h>35#include <lib.h>36#include <uio.h>37#include <vfs.h>38#include <device.h>3940/* For open() */41static42int43nullopen(struct device *dev, int openflags)44{45(void)dev;46(void)openflags;4748return 0;49}5051/* For close() */52static53int54nullclose(struct device *dev)55{56(void)dev;57return 0;58}5960/* For d_io() */61static62int63nullio(struct device *dev, struct uio *uio)64{65/*66* On write, discard everything without looking at it.67* On read, do nothing, generating an immediate EOF.68*/6970(void)dev; // unused7172if (uio->uio_rw == UIO_WRITE) {73uio->uio_resid = 0;74}7576return 0;77}7879/* For ioctl() */80static81int82nullioctl(struct device *dev, int op, userptr_t data)83{84/*85* No ioctls.86*/8788(void)dev;89(void)op;90(void)data;9192return EINVAL;93}9495/*96* Function to create and attach null:97*/98void99devnull_create(void)100{101int result;102struct device *dev;103104dev = kmalloc(sizeof(*dev));105if (dev==NULL) {106panic("Could not add null device: out of memory\n");107}108109110dev->d_open = nullopen;111dev->d_close = nullclose;112dev->d_io = nullio;113dev->d_ioctl = nullioctl;114115dev->d_blocks = 0;116dev->d_blocksize = 1;117118dev->d_devnumber = 0; /* assigned by vfs_adddev */119120dev->d_data = NULL;121122result = vfs_adddev("null", dev, 0);123if (result) {124panic("Could not add null device: %s\n", strerror(result));125}126}127128129