-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho-3.0.c
172 lines (144 loc) · 3.52 KB
/
echo-3.0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/ioccom.h>
MALLOC_DEFINE(M_ECHO, "echo_buffer", "buffer for echo driver");
#define ECHO_CLEAR_BUFFER _IO('E', 1)
#define ECHO_SET_BUFFER_SIZE _IOW('E', 2, int)
static d_open_t echo_open;
static d_close_t echo_close;
static d_read_t echo_read;
static d_write_t echo_write;
static d_ioctl_t echo_ioctl;
static struct cdevsw echo_cdevsw = {
.d_version = D_VERSION,
.d_open = echo_open,
.d_close = echo_close,
.d_read = echo_read,
.d_write = echo_write,
.d_ioctl = echo_ioctl,
.d_name = "echo"
};
typedef struct echo {
int buffer_size;
char *buffer;
int length;
} echo_t;
static echo_t *echo_message;
static struct cdev *echo_dev;
static int
echo_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
{
uprintf("Opening echo device.\n");
return(0);
}
static int
echo_close(struct cdev *dev, int fflags, int devtype, struct thread *td)
{
uprintf("Closing echo device.\n");
return(0);
}
static int
echo_write(struct cdev *dev, struct uio *uio, int ioflag)
{
int error = 0;
int amount;
amount = MIN(uio->uio_resid,
(echo_message->buffer_size - 1 - uio->uio_offset > 0) ?
echo_message->buffer_size - 1 - uio->uio_offset : 0);
if (amount == 0)
return (error);
error = uiomove(echo_message->buffer, amount, uio);
if (error != 0) {
uprintf("Write failed.\n");
return (error);
}
echo_message->buffer[amount] = '\0';
echo_message->length = amount;
return(error);
}
static int
echo_read(struct cdev *dev, struct uio *uio, int ioflag)
{
int error = 0;
int amount;
amount = MIN(uio->uio_resid,
(echo_message->length - uio->uio_offset > 0) ?
echo_message->length - uio->uio_offset : 0);
error = uiomove(echo_message->buffer + uio->uio_offset, amount, uio);
if (error != 0)
uprintf("Read failed.\n");
return (error);
}
static int
echo_set_buffer_size(int size)
{
int error = 0;
if (echo_message->buffer_size == size)
return (error);
if (size >= 128 && size <= 512) {
echo_message->buffer = realloc(echo_message->buffer, size,
M_ECHO, M_WAITOK);
echo_message->buffer_size = size;
if (echo_message->length >= size) {
echo_message->length = size - 1;
echo_message->buffer[size - 1] = '\0';
}
} else
error = EINVAL;
return (error);
}
static int
echo_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
struct thread *td)
{
int error = 0;
switch (cmd) {
case ECHO_CLEAR_BUFFER:
memset(echo_message->buffer, '\0',
echo_message->buffer_size);
echo_message->length = 0;
uprintf("Buffer cleared.\n");
break;
case ECHO_SET_BUFFER_SIZE:
error = echo_set_buffer_size(*(int *)data);
if (error == 0)
uprintf("Buffer resized.\n");
break;
default:
error = ENOTTY;
break;
}
return (error);
}
static int
echo_modevent(module_t mod __unused, int event, void *arg __unused)
{
int error = 0;
switch (event) {
case MOD_LOAD:
echo_message = malloc(sizeof(echo_t), M_ECHO, M_WAITOK);
echo_message->buffer_size = 256;
echo_message->buffer = malloc(echo_message->buffer_size,
M_ECHO, M_WAITOK);
echo_dev = make_dev(&echo_cdevsw, 0, UID_ROOT, GID_WHEEL,
0600, "echo");
uprintf("Echo driver loaded.\n");
break;
case MOD_UNLOAD:
destroy_dev(echo_dev);
free(echo_message->buffer, M_ECHO);
free(echo_message, M_ECHO);
uprintf("Echo driver unloaded.\n");
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}
DEV_MODULE(echo30, echo_modevent, NULL);