devnull.c revision 1.7.2.1 1 /* $NetBSD: devnull.c,v 1.7.2.1 2016/07/18 03:50:00 pgoyette 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.7.2.1 2016/07/18 03:50:00 pgoyette Exp $");
37
38 #include <sys/param.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/stat.h>
42 #include <sys/localcount.h>
43
44 #include <rump-sys/vfs.h>
45
46 static dev_type_open(rump_devnullopen);
47 static dev_type_read(rump_devnullrw);
48
49 #ifdef _MODULE
50 struct localcount null_localcount;
51 #endif
52
53 static struct cdevsw null_cdevsw = {
54 .d_open = rump_devnullopen,
55 .d_close = nullclose,
56 .d_read = rump_devnullrw,
57 .d_write = rump_devnullrw,
58 .d_ioctl = noioctl,
59 .d_stop = nostop,
60 .d_tty = notty,
61 .d_poll = nopoll,
62 .d_mmap = nommap,
63 .d_kqfilter = nokqfilter,
64 .d_discard = nodiscard,
65 #ifdef _MODULE
66 .d_localcount = &null_localcount,
67 #endif
68 .d_flag = D_OTHER | D_MPSAFE
69 };
70
71 int
72 rump_devnull_init()
73 {
74 devmajor_t null_bmaj, null_cmaj;
75 int error;
76
77 null_bmaj = null_cmaj = NODEVMAJOR;
78 error = devsw_attach("null", NULL, &null_bmaj, &null_cdevsw,&null_cmaj);
79 KASSERT(error || null_cmaj == 2);
80
81 error = rump_vfs_makeonedevnode(S_IFCHR,
82 "/dev/null", null_cmaj, DEV_NULL);
83 if (error)
84 return error;
85 return rump_vfs_makeonedevnode(S_IFCHR,
86 "/dev/zero", null_cmaj, DEV_ZERO);
87 }
88
89 static int
90 rump_devnullopen(dev_t dev, int flag, int mode, struct lwp *l)
91 {
92
93 if (minor(dev) == DEV_ZERO || minor(dev) == DEV_NULL)
94 return 0;
95 return ENXIO;
96 }
97
98 static int
99 rump_devnullrw(dev_t dev, struct uio *uio, int flags)
100 {
101 char zeros[512];
102 int error;
103
104 switch (minor(dev)) {
105 case DEV_NULL:
106 if (uio->uio_rw == UIO_WRITE)
107 uio->uio_resid = 0;
108 break;
109 case DEV_ZERO:
110 if (uio->uio_rw == UIO_WRITE) {
111 uio->uio_resid = 0;
112 break;
113 }
114 memset(zeros, 0, sizeof(zeros));
115 while (uio->uio_resid > 0) {
116 error = uiomove(zeros,
117 min(sizeof(zeros), uio->uio_resid), uio);
118 if (error)
119 return error;
120 }
121 break;
122 default:
123 return ENXIO; /* how? */
124 }
125
126 return 0;
127 }
128