os161-1.99
 All Data Structures
devnull.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Implementation of the null device, "null:", which generates an
00032  * immediate EOF on read and throws away anything written to it.
00033  */
00034 #include <types.h>
00035 #include <kern/errno.h>
00036 #include <lib.h>
00037 #include <uio.h>
00038 #include <vfs.h>
00039 #include <device.h>
00040 
00041 /* For open() */
00042 static
00043 int
00044 nullopen(struct device *dev, int openflags)
00045 {
00046         (void)dev;
00047         (void)openflags;
00048 
00049         return 0;
00050 }
00051 
00052 /* For close() */
00053 static
00054 int
00055 nullclose(struct device *dev)
00056 {
00057         (void)dev;
00058         return 0;
00059 }
00060 
00061 /* For d_io() */
00062 static
00063 int
00064 nullio(struct device *dev, struct uio *uio)
00065 {
00066         /*
00067          * On write, discard everything without looking at it.
00068          * On read, do nothing, generating an immediate EOF.
00069          */
00070 
00071         (void)dev; // unused
00072 
00073         if (uio->uio_rw == UIO_WRITE) {
00074                 uio->uio_resid = 0;
00075         }
00076 
00077         return 0;
00078 }
00079 
00080 /* For ioctl() */
00081 static
00082 int
00083 nullioctl(struct device *dev, int op, userptr_t data)
00084 {
00085         /*
00086          * No ioctls.
00087          */
00088 
00089         (void)dev;
00090         (void)op;
00091         (void)data;
00092 
00093         return EINVAL;
00094 }
00095 
00096 /*
00097  * Function to create and attach null:
00098  */
00099 void
00100 devnull_create(void)
00101 {
00102         int result;
00103         struct device *dev;
00104 
00105         dev = kmalloc(sizeof(*dev));
00106         if (dev==NULL) {
00107                 panic("Could not add null device: out of memory\n");
00108         }
00109 
00110         
00111         dev->d_open = nullopen;
00112         dev->d_close = nullclose;
00113         dev->d_io = nullio;
00114         dev->d_ioctl = nullioctl;
00115 
00116         dev->d_blocks = 0;
00117         dev->d_blocksize = 1;
00118 
00119         dev->d_devnumber = 0; /* assigned by vfs_adddev */
00120 
00121         dev->d_data = NULL;
00122 
00123         result = vfs_adddev("null", dev, 0);
00124         if (result) {
00125                 panic("Could not add null device: %s\n", strerror(result));
00126         }
00127 }
 All Data Structures