Home | History | Annotate | Line # | Download | only in rumpvfs
devnull.c revision 1.4
      1 /*	$NetBSD: devnull.c,v 1.4 2013/04/04 01:29:55 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009 Antti Kantee.  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  * 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 AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * /dev/null, the infamous bytesink.
     30  *
     31  * I can't imagine it being very different in the rump kernel than in
     32  * the host kernel.  But nonetheless it serves as a simple example.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: devnull.c,v 1.4 2013/04/04 01:29:55 pooka Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/stat.h>
     42 
     43 #include "rump_vfs_private.h"
     44 
     45 static dev_type_open(rump_devnullopen);
     46 static dev_type_read(rump_devnullrw);
     47 
     48 static struct cdevsw null_cdevsw = {
     49 	rump_devnullopen, nullclose, rump_devnullrw, rump_devnullrw, noioctl,
     50 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE,
     51 };
     52 
     53 int
     54 rump_devnull_init()
     55 {
     56 	devmajor_t null_bmaj, null_cmaj;
     57 	int error;
     58 
     59 	null_bmaj = null_cmaj = NODEVMAJOR;
     60 	error = devsw_attach("null", NULL, &null_bmaj, &null_cdevsw,&null_cmaj);
     61 	KASSERT(error || null_cmaj == 2);
     62 
     63 	error = rump_vfs_makeonedevnode(S_IFCHR,
     64 	    "/dev/null", null_cmaj, DEV_NULL);
     65 	if (error)
     66 		return error;
     67 	return rump_vfs_makeonedevnode(S_IFCHR,
     68 	    "/dev/zero", null_cmaj, DEV_ZERO);
     69 }
     70 
     71 static int
     72 rump_devnullopen(dev_t dev, int flag, int mode, struct lwp *l)
     73 {
     74 
     75 	if (minor(dev) == DEV_ZERO || minor(dev) == DEV_NULL)
     76 		return 0;
     77 	return ENXIO;
     78 }
     79 
     80 static int
     81 rump_devnullrw(dev_t dev, struct uio *uio, int flags)
     82 {
     83 	char zeros[512];
     84 	int error;
     85 
     86 	switch (minor(dev)) {
     87 	case DEV_NULL:
     88 		if (uio->uio_rw == UIO_WRITE)
     89 			uio->uio_resid = 0;
     90 		break;
     91 	case DEV_ZERO:
     92 		if (uio->uio_rw == UIO_WRITE) {
     93 			uio->uio_resid = 0;
     94 			break;
     95 		}
     96 		memset(zeros, 0, sizeof(zeros));
     97 		while (uio->uio_resid > 0) {
     98 			error = uiomove(zeros,
     99 			    min(sizeof(zeros), uio->uio_resid), uio);
    100 			if (error)
    101 				return error;
    102 		}
    103 		break;
    104 	default:
    105 		return ENXIO; /* how? */
    106 	}
    107 
    108 	return 0;
    109 }
    110