summaryrefslogtreecommitdiffstats
path: root/usr.bin/doscmd/AsyncIO.c
blob: e92fc55a9348cfa22d3c4a2bb4cf49c368e1e729 (plain)
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
/*
 * Copyright (c) 1992, 1993, 1996
 *	Berkeley Software Design, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Berkeley Software
 *	Design, Inc.
 *
 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	BSDI AsyncIO.c,v 2.2 1996/04/08 19:32:10 bostic Exp
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>

#include "doscmd.h"
#include "AsyncIO.h"

#define FD_ISZERO(p)	((p)->fds_bits[0] == 0)

/*
 * Set or Clear the Async nature of an FD
 */

#define	SETASYNC(fd)	fcntl(fd, F_SETFL, handlers[fd].flag | FASYNC)
#define	CLRASYNC(fd)	fcntl(fd, F_SETFL, handlers[fd].flag & ~FASYNC)

/*
 * Request that ``func'' be called everytime data is available on ``fd''
 */

static	fd_set	fdset;		/* File Descriptors to select on */

typedef	struct {
	void	(*func)(int, int, void *, regcontext_t *);
					/* Function to call on data arrival */
	void	(*failure)(void *);	/* Function to call on failure */
	void	*arg;			/* Argument to above functions */
	int	lockcnt;		/* Nested level of lock */
	fd_set	members;		/* Set of FD's to disable on SIGIO */
	int	flag;			/* The flag from F_GETFL (we own it) */
} Async;

static Async	handlers[OPEN_MAX];

static void	CleanIO(void);
static void	HandleIO(struct sigframe *sf);

void
_RegisterIO(int fd, void (*func)(int, int, void *, regcontext_t *),
    void *arg, void (*failure)(void *))
{
	static int firsttime = 1;
	Async *as;

	if (fd < 0 || fd > OPEN_MAX) {
printf("%d: Invalid FD\n", fd);
		return;
	}

	as = &handlers[fd];

        if ((as->flag = fcntl(fd, F_GETFL, 0)) == -1) {
		if (func) {
/*@*/			perror("get fcntl");
/*@*/			abort();
			return;
		}
        }

	if (firsttime) {
		firsttime = 0;
    	    	setsignal(SIGIO, HandleIO);
	}

	if ((handlers[fd].func = func) != 0) {
		as->lockcnt = 0;
		as->arg = arg;
		as->failure = failure;

		FD_SET(fd, &fdset);
		FD_ZERO(&handlers[fd].members);
		FD_SET(fd, &handlers[fd].members);
		if (fcntl(fd, F_SETOWN, getpid()) < 0) {
/*@*/			perror("SETOWN");
		}
		SETASYNC(fd);
	} else {
		as->arg = 0;
		as->failure = 0;
		as->lockcnt = 0;

		CLRASYNC(fd);
		FD_CLR(fd, &fdset);
	}
}

static void
CleanIO()
{
	int x;
	static struct timeval tv;

	/*
	 * For every file des in fd_set, we check to see if it
	 * causes a fault on select().  If so, we unregister it
	 * for the user.
	 */
	for (x = 0; x < OPEN_MAX; ++x) {
		fd_set set;

		if (!FD_ISSET(x, &fdset))
			continue;

		FD_ZERO(&set);
		FD_SET(x, &set);
		errno = 0;
		if (select(FD_SETSIZE, &set, 0, 0, &tv) < 0 &&
		    errno == EBADF) {
			void (*f)(void *);
			void *a;
printf("Closed file descriptor %d\n", x);

			f = handlers[x].failure;
			a = handlers[x].arg;
			handlers[x].failure = 0;
			handlers[x].func = 0;
			handlers[x].arg = 0;
			handlers[x].lockcnt = 0;
			FD_CLR(x, &fdset);
			if (f)
				(*f)(a);
		}
	}
}

static void HandleIO(struct sigframe *sf)
{
	static struct timeval tv;
	fd_set readset, writeset;
	int x, fd;

again:
	readset = writeset = fdset;
	if ((x = select(FD_SETSIZE, &readset, &writeset, 0, &tv)) < 0) {
		/*
		 * If we failed because of a BADFiledes, go find
		 * which one(s), fail them out and then try a
		 * new select to see if any of the good ones are
		 * okay.
		 */
		if (errno == EBADF) {
			CleanIO();
			if (FD_ISZERO(&fdset))
				return;
			goto again;
		}
		perror("select");
		return;
	}

	/*
	 * If we run out of fds to look at, break out of the loop
	 * and exit the handler.
	 */
	if (x == 0)
		return;

	/*
	 * If there is at least 1 fd saying it has something for
	 * us, then loop through the sets looking for those
	 * bits, stopping when we have handleed the number it has
	 * asked for.
	 */
	for (fd = 0; x && fd < OPEN_MAX; fd ++) {
		Async *as;
		int cond;  				     
		
		cond = 0;
		
		if (FD_ISSET(fd, &readset)) {
			cond |= AS_RD;
			x --;
		}
		if (FD_ISSET(fd, &writeset)) {
			cond |= AS_WR;
			x --;
		}

		if (cond == 0)
			continue;

		/*
		 * Is suppose it is possible that one of the previous
		 * I/O requests changed the fdset.
		 * We do know that SIGIO is turned off right now,
		 * so it is safe to checkit.
		 */
		if (!FD_ISSET(fd, &fdset)) {
			continue;
		}
		as = &handlers[fd];
		
		/*
		 * as in above, maybe someone locked us...
		 * we are in dangerous water now if we are
		 * multi-tasked
		 */
		if (as->lockcnt) {
			fprintf(stderr, "Selected IO on locked %d\n",fd);
			continue;
		}
		/*
		 * Okay, now if there exists a handler, we should
		 * call it.  We must turn back on SIGIO if there
		 * are possibly other people waiting for it.
		 */
		if (as->func) {
			(*handlers[fd].func)(fd, cond, handlers[fd].arg, 
			    (regcontext_t*)&sf->sf_uc);
		} else {
			/*
			 * Otherwise deregister this guy.
			 */
			_RegisterIO(fd, 0, 0, 0);
		}
	}
	/*
	 * If we did not process all the fd's, then we should
	 * break out of the probable infinite loop.
	 */
}
OpenPOWER on IntegriCloud