Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/vfs/devnull.c
2093 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
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
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* Implementation of the null device, "null:", which generates an
32
* immediate EOF on read and throws away anything written to it.
33
*/
34
#include <types.h>
35
#include <kern/errno.h>
36
#include <lib.h>
37
#include <uio.h>
38
#include <vfs.h>
39
#include <device.h>
40
41
/* For open() */
42
static
43
int
44
nullopen(struct device *dev, int openflags)
45
{
46
(void)dev;
47
(void)openflags;
48
49
return 0;
50
}
51
52
/* For close() */
53
static
54
int
55
nullclose(struct device *dev)
56
{
57
(void)dev;
58
return 0;
59
}
60
61
/* For d_io() */
62
static
63
int
64
nullio(struct device *dev, struct uio *uio)
65
{
66
/*
67
* On write, discard everything without looking at it.
68
* On read, do nothing, generating an immediate EOF.
69
*/
70
71
(void)dev; // unused
72
73
if (uio->uio_rw == UIO_WRITE) {
74
uio->uio_resid = 0;
75
}
76
77
return 0;
78
}
79
80
/* For ioctl() */
81
static
82
int
83
nullioctl(struct device *dev, int op, userptr_t data)
84
{
85
/*
86
* No ioctls.
87
*/
88
89
(void)dev;
90
(void)op;
91
(void)data;
92
93
return EINVAL;
94
}
95
96
/*
97
* Function to create and attach null:
98
*/
99
void
100
devnull_create(void)
101
{
102
int result;
103
struct device *dev;
104
105
dev = kmalloc(sizeof(*dev));
106
if (dev==NULL) {
107
panic("Could not add null device: out of memory\n");
108
}
109
110
111
dev->d_open = nullopen;
112
dev->d_close = nullclose;
113
dev->d_io = nullio;
114
dev->d_ioctl = nullioctl;
115
116
dev->d_blocks = 0;
117
dev->d_blocksize = 1;
118
119
dev->d_devnumber = 0; /* assigned by vfs_adddev */
120
121
dev->d_data = NULL;
122
123
result = vfs_adddev("null", dev, 0);
124
if (result) {
125
panic("Could not add null device: %s\n", strerror(result));
126
}
127
}
128
129