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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/* $OpenBSD: file.c,v 1.2 2008/08/14 09:48:50 ratchov Exp $ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* non-blocking file i/o module: each file can be read or written (or
* both). To achieve non-blocking io, we simply use the poll() syscall
* in an event loop. If a read() or write() syscall return EAGAIN
* (operation will block), then the file is marked as "for polling", else
* the file is not polled again.
*
*/
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "conf.h"
#include "file.h"
#include "aproc.h"
#include "abuf.h"
#define MAXFDS 100
struct filelist file_list;
struct file *
file_new(int fd, char *name)
{
unsigned i;
struct file *f;
i = 0;
LIST_FOREACH(f, &file_list, entry)
i++;
if (i >= MAXFDS)
err(1, "%s: too many polled files", name);
f = malloc(sizeof(struct file));
if (f == NULL)
err(1, "%s", name);
f->fd = fd;
f->events = 0;
f->rbytes = -1;
f->wbytes = -1;
f->name = name;
f->state = 0;
f->rproc = NULL;
f->wproc = NULL;
LIST_INSERT_HEAD(&file_list, f, entry);
DPRINTF("file_new: %s\n", f->name);
return f;
}
void
file_del(struct file *f)
{
DPRINTF("file_del: %s|%x\n", f->name, f->state);
}
int
file_poll(void)
{
#ifdef DEBUG
int ndead;
#endif
nfds_t nfds;
struct pollfd pfds[MAXFDS];
struct pollfd *pfd;
struct file *f, *fnext;
struct aproc *p;
nfds = 0;
#ifdef DEBUG
ndead = 0;
#endif
LIST_FOREACH(f, &file_list, entry) {
if (!f->events) {
#ifdef DEBUG
if (f->state & (FILE_EOF | FILE_HUP))
ndead++;
#endif
f->pfd = NULL;
continue;
}
pfd = &pfds[nfds++];
f->pfd = pfd;
pfd->fd = f->fd;
pfd->events = f->events;
}
#ifdef DEBUG
if (debug_level >= 4) {
fprintf(stderr, "file_poll:");
LIST_FOREACH(f, &file_list, entry) {
fprintf(stderr, " %s(%x)", f->name, f->events);
}
fprintf(stderr, "\n");
}
if (nfds == 0 && ndead == 0 && !LIST_EMPTY(&file_list)) {
fprintf(stderr, "file_poll: deadlock\n");
abort();
}
#endif
if (LIST_EMPTY(&file_list)) {
DPRINTF("file_poll: nothing to do...\n");
return 0;
}
if (nfds) {
while (poll(pfds, nfds, -1) < 0) {
if (errno != EINTR)
err(1, "file_poll: poll failed");
}
}
LIST_FOREACH(f, &file_list, entry) {
pfd = f->pfd;
if (pfd == NULL)
continue;
if ((f->events & POLLIN) && (pfd->revents & POLLIN)) {
f->events &= ~POLLIN;
f->state |= FILE_ROK;
DPRINTFN(3, "file_poll: %s rok\n", f->name);
while (f->state & FILE_ROK) {
p = f->rproc;
if (!p || !p->ops->in(p, NULL))
break;
}
}
if ((f->events & POLLOUT) && (pfd->revents & POLLOUT)) {
f->events &= ~POLLOUT;
f->state |= FILE_WOK;
DPRINTFN(3, "file_poll: %s wok\n", f->name);
while (f->state & FILE_WOK) {
p = f->wproc;
if (!p || !p->ops->out(p, NULL))
break;
}
}
}
LIST_FOREACH(f, &file_list, entry) {
if (f->state & FILE_EOF) {
DPRINTFN(2, "file_poll: %s: eof\n", f->name);
p = f->rproc;
if (p)
p->ops->eof(p, NULL);
f->state &= ~FILE_EOF;
}
if (f->state & FILE_HUP) {
DPRINTFN(2, "file_poll: %s hup\n", f->name);
p = f->wproc;
if (p)
p->ops->hup(p, NULL);
f->state &= ~FILE_HUP;
}
}
for (f = LIST_FIRST(&file_list); f != NULL; f = fnext) {
fnext = LIST_NEXT(f, entry);
if (f->rproc == NULL && f->wproc == NULL) {
LIST_REMOVE(f, entry);
DPRINTF("file_poll: %s: deleted\n", f->name);
free(f);
}
}
if (LIST_EMPTY(&file_list)) {
DPRINTFN(2, "file_poll: terminated\n");
return 0;
}
return 1;
}
void
file_start(void)
{
sigset_t set;
sigemptyset(&set);
(void)sigaddset(&set, SIGPIPE);
if (sigprocmask(SIG_BLOCK, &set, NULL))
err(1, "sigprocmask");
LIST_INIT(&file_list);
}
void
file_stop(void)
{
struct file *f;
if (!LIST_EMPTY(&file_list)) {
fprintf(stderr, "file_stop:");
LIST_FOREACH(f, &file_list, entry) {
fprintf(stderr, " %s(%x)", f->name, f->events);
}
fprintf(stderr, "\nfile_stop: list not empty\n");
exit(1);
}
}
unsigned
file_read(struct file *file, unsigned char *data, unsigned count)
{
int n;
if (file->rbytes >= 0 && count > file->rbytes) {
count = file->rbytes; /* file->rbytes fits in count */
if (count == 0) {
DPRINTFN(2, "file_read: %s: complete\n", file->name);
file->state &= ~FILE_ROK;
file->state |= FILE_EOF;
return 0;
}
}
while ((n = read(file->fd, data, count)) < 0) {
if (errno == EINTR)
continue;
file->state &= ~FILE_ROK;
if (errno == EAGAIN) {
DPRINTFN(3, "file_read: %s: blocking...\n",
file->name);
file->events |= POLLIN;
} else {
warn("%s", file->name);
file->state |= FILE_EOF;
}
return 0;
}
if (n == 0) {
DPRINTFN(2, "file_read: %s: eof\n", file->name);
file->state &= ~FILE_ROK;
file->state |= FILE_EOF;
return 0;
}
if (file->rbytes >= 0)
file->rbytes -= n;
DPRINTFN(4, "file_read: %s: got %d bytes\n", file->name, n);
return n;
}
unsigned
file_write(struct file *file, unsigned char *data, unsigned count)
{
int n;
if (file->wbytes >= 0 && count > file->wbytes) {
count = file->wbytes; /* file->wbytes fits in count */
if (count == 0) {
DPRINTFN(2, "file_write: %s: complete\n", file->name);
file->state &= ~FILE_WOK;
file->state |= FILE_HUP;
return 0;
}
}
while ((n = write(file->fd, data, count)) < 0) {
if (errno == EINTR)
continue;
file->state &= ~FILE_WOK;
if (errno == EAGAIN) {
DPRINTFN(3, "file_write: %s: blocking...\n",
file->name);
file->events |= POLLOUT;
} else {
warn("%s", file->name);
file->state |= FILE_HUP;
}
return 0;
}
if (file->wbytes >= 0)
file->wbytes -= n;
DPRINTFN(4, "file_write: %s: wrote %d bytes\n", file->name, n);
return n;
}
void
file_eof(struct file *f)
{
DPRINTFN(2, "file_eof: %s: scheduled for eof\n", f->name);
f->events &= ~POLLIN;
f->state &= ~FILE_ROK;
f->state |= FILE_EOF;
}
void
file_hup(struct file *f)
{
DPRINTFN(2, "file_hup: %s: scheduled for hup\n", f->name);
f->events &= ~POLLOUT;
f->state &= ~FILE_WOK;
f->state |= FILE_HUP;
}
|