Use sigset_t and sigfillset instead of memset
[openafs.git] / src / lwp / iomgr.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*******************************************************************\
11 *                                                                   *
12 *       Information Technology Center                               *
13 *       Carnegie-Mellon University                                  *
14 *                                                                   *
15 *                                                                   *
16 *                                                                   *
17 \*******************************************************************/
18
19
20 /*
21         IO Manager routines & server process for VICE server.
22 */
23
24 /* This controls the size of an fd_set; it must be defined early before
25  * the system headers define that type and the macros that operate on it.
26  * Its value should be as large as the maximum file descriptor limit we
27  * are likely to run into on any platform.  Right now, that is 65536
28  * which is the default hard fd limit on Solaris 9 */
29 /* We don't do this on Windows because on that platform there is code
30  * which allocates fd_set's on the stack (IOMGR_Sleep on Win9x, and
31  * FDSetAnd on WinNT) */
32 #ifndef _WIN32
33 #define FD_SETSIZE 65536
34 #endif
35
36 #include <afsconfig.h>
37 #include <afs/param.h>
38
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #ifdef AFS_NT40_ENV
44 #include <winsock2.h>
45 #include <malloc.h>
46 extern void lwp_abort(void);
47 #else
48 #include <unistd.h>             /* select() prototype */
49 #include <sys/types.h>          /* fd_set on older platforms */
50 #include <sys/time.h>           /* struct timeval, select() prototype */
51 #ifndef FD_SET
52 # include <sys/select.h>        /* fd_set on newer platforms */
53 #endif
54 #include <sys/file.h>
55 #endif /* AFS_NT40_ENV */
56 #include "lwp.h"
57 #include "timer.h"
58 #include <signal.h>
59 #include <errno.h>
60 #ifdef AFS_SUN5_ENV
61 #include <fcntl.h>
62 #endif
63
64 #if     defined(USE_PTHREADS) || defined(USE_SOLARIS_THREADS)
65
66 void IOMGR_Initialize() /* noop */
67 { }
68
69 void IOMGR_Sleep (seconds)
70   unsigned seconds;
71 {
72     struct timespec itv;
73
74     itv.tv_sec = seconds;
75     itv.tv_nsec = 0;
76     assert(pthread_mutex_unlock(&lwp_mutex) == 0);
77     assert(pthread_delay_np(&itv) == 0);
78     assert(pthread_mutex_lock(&lwp_mutex) == 0);
79 }
80
81 #else
82
83 #ifdef  AFS_DECOSF_ENV
84 extern void *malloc();
85 #endif  /* AFS_DECOSF_ENV */
86
87 typedef unsigned char bool;
88 #define FALSE   0
89 #define TRUE    1
90
91 #ifndef MIN
92 #define MIN(a,b) (((a)>(b)) ? (b) : (a))
93 #endif
94
95 #ifndef NSIG
96 #define NSIG 8*sizeof(sigset_t)
97 #endif
98
99 static int SignalSignals(void);
100 \f
101 /********************************\
102 *                                *
103 *  Stuff for managing IoRequests *
104 *                                *
105 \********************************/
106
107 struct IoRequest {
108
109     /* Pid of process making request (for use in IOMGR_Cancel */
110     PROCESS             pid;
111
112     /* Descriptor masks for requests */
113     int                 nfds;
114     fd_set              *readfds;
115     fd_set              *writefds;
116     fd_set              *exceptfds;
117
118     struct TM_Elem      timeout;
119
120     /* Result of select call */
121     int                 result;
122
123     struct IoRequest    *next;  /* for iorFreeList */
124
125 };
126
127 /********************************\
128 *                                *
129 *  Stuff for managing signals    *
130 *                                *
131 \********************************/
132
133 #define badsig(signo)           (((signo) <= 0) || ((signo) >= NSIG))
134 #define mysigmask(signo)                (1 << ((signo)-1))
135
136
137 fd_set openMask;                /* mask of open files on an IOMGR abort */
138 static afs_int32 sigsHandled;   /* sigmask(signo) is on if we handle signo */
139 static int anySigsDelivered;            /* true if any have been delivered. */
140 #ifndef AFS_NT40_ENV
141 static struct sigaction oldActions[NSIG];       /* the old signal vectors */
142 #endif
143 static char *sigEvents[NSIG];           /* the event to do an LWP signal on */
144 static int sigDelivered[NSIG];          /* True for signals delivered so far.
145                                            This is an int array to make sure
146                                            there are no conflicts when trying
147                                            to write it */
148 /* software 'signals' */
149 #define NSOFTSIG                4
150 static void *(*sigProc[NSOFTSIG])(void *);
151 static void *sigRock[NSOFTSIG];
152
153 \f
154 static struct IoRequest *iorFreeList = 0;
155
156 static struct TM_Elem *Requests;        /* List of requests */
157 static struct timeval iomgr_timeout;    /* global so signal handler can zap it */
158
159 /* stuff for debugging */
160 static int iomgr_errno;
161 static struct timeval iomgr_badtv;
162 static PROCESS iomgr_badpid;
163 static void SignalIO(int fds, fd_set *rfds, fd_set *wfds, fd_set *efs,
164                     int code);
165 static void SignalTimeout(int code, struct timeval *timeout);
166
167 /* fd_set pool managment. 
168  * Use the pool instead of creating fd_set's on the stack. fd_set's can be
169  * 8K in size, so making three could put 24K in the limited space of an LWP
170  * stack.
171  */
172 struct IOMGR_fd_set {
173     struct IOMGR_fd_set *next;
174 } *iomgrFreeFDSets = (struct IOMGR_fd_set*)0;
175
176 /* IOMGR_FreeFDSet
177  * Return fd_set to the free list.
178  */
179 void IOMGR_FreeFDSet(fd_set *s)
180 {
181     struct IOMGR_fd_set *t = (struct IOMGR_fd_set *)s;
182
183     t->next = iomgrFreeFDSets;
184     iomgrFreeFDSets = t;
185 }
186
187 /* IOMGR_AllocFDSet
188  * returns a zeroed fd_set or null if could not malloc one.
189  */
190 fd_set *IOMGR_AllocFDSet(void)
191 {
192     struct IOMGR_fd_set *t;
193     if (iomgrFreeFDSets) {
194         t =  iomgrFreeFDSets;
195         iomgrFreeFDSets = iomgrFreeFDSets->next;
196     }
197     else {
198         t = (struct IOMGR_fd_set *)malloc(sizeof(fd_set));
199     }
200     if (!t)
201         return (fd_set*)0;
202     else {
203         FD_ZERO((fd_set*)t);
204         return (fd_set*)t;
205     }
206 }
207
208 #define FreeRequest(x) ((x)->next = iorFreeList, iorFreeList = (x))
209
210 static struct IoRequest *NewRequest(void)
211 {
212     struct IoRequest *request;
213
214     if ((request=iorFreeList))
215         iorFreeList = (struct IoRequest *) (request->next);
216     else request = (struct IoRequest *) malloc(sizeof(struct IoRequest));
217
218     memset((char*)request, 0, sizeof(struct IoRequest));
219     return request;
220 }
221
222 #define Purge(list) FOR_ALL_ELTS(req, list, { free(req->BackPointer); })
223
224 \f
225 /* FD_SET support routines. All assume the fd_set size is a multiple of an int
226  * so we can at least do logical operations on ints instead of chars.
227  *
228  * For each routine, nfds is the highest bit set in either fd_set, starting
229  * with no bits == 0.
230  */
231 #ifdef AFS_NT40_ENV
232 #define FD_N_ZERO(A, x) FD_ZERO(x)
233 #else
234 #define FDS_P_POS (sizeof(int)*8)
235 #define INTS_PER_FDS(x) (((x)+(FDS_P_POS-1)) / FDS_P_POS)
236 #define FD_N_ZERO(nfds, x) memset((char*)(x), 0, (INTS_PER_FDS(nfds))*sizeof(int))
237 #endif
238
239 #if defined(AFS_LINUX22_ENV) && (__GLIBC_MINOR__ > 0)
240 /* Build for both glibc 2.0.x and 2.1.x */
241 #define FDS_BITS __fds_bits
242 #else
243 #define FDS_BITS fds_bits
244 #endif
245
246 /* FDSetCmp - returns 1 if any bits in fd_set1 are also set in fd_set2.
247  * If nfds is 0, or one of the fd_sets is null return 0 (since there is no bit
248  * set in both fd_sets).
249  */
250 static int FDSetCmp(int nfds, fd_set *fd_set1, fd_set *fd_set2)
251 {
252     unsigned int i, j;
253
254     if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
255         return 0;
256
257 #ifdef AFS_NT40_ENV
258     if (fd_set1->fd_count == 0 || fd_set2->fd_count == 0)
259         return 0;
260
261     for (i=0; i<fd_set1->fd_count; i++) {
262         for (j=0; j<fd_set2->fd_count; j++) {
263         if (fd_set1->fd_array[i] == fd_set2->fd_array[j])
264             return 1;
265         }
266     }
267 #else
268     if (nfds == 0)
269         return 0;
270
271     j = INTS_PER_FDS(nfds);
272     for (i=0; i<j; i++) {
273         if (fd_set1->FDS_BITS[i] & fd_set2->FDS_BITS[i])
274             return 1;
275     }
276 #endif
277     return 0;
278 }
279
280 /* FDSetSet - set bits from fd_set2 into fd_set1
281  */
282 static void FDSetSet(int nfds, fd_set *fd_set1, fd_set *fd_set2)
283 {
284     unsigned int i;
285 #ifndef AFS_NT40_ENV
286     unsigned int n;
287 #endif
288
289     if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
290         return;
291
292 #ifdef AFS_NT40_ENV
293     if (fd_set2->fd_count==0)
294         return;
295
296     for (i=0; i<fd_set2->fd_count; i++)
297         FD_SET(fd_set2->fd_array[i], fd_set1);
298 #else
299     if (nfds == 0)
300         return;
301
302     for (i = 0, n = INTS_PER_FDS(nfds); i < n; i++) {
303         fd_set1->FDS_BITS[i] |= fd_set2->FDS_BITS[i];
304     }
305 #endif
306 }
307
308 /* FDSetAnd - fd_set1  <- fd_set1 & fd_set2. 
309  */
310 #ifdef AFS_NT40_ENV
311 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
312 {
313     unsigned int i;
314     fd_set tmpset;
315
316     if (fd_set1 == NULL || fd_set1->fd_count == 0)
317         return;
318     
319     if (fd_set2 == NULL || fd_set2->fd_count == 0) {
320         FD_ZERO(fd_set1);
321     }
322     else {
323         FD_ZERO(&tmpset);
324         for (i=0; i<fd_set2->fd_count; i++) {
325             if (FD_ISSET(fd_set2->fd_array[i], fd_set1))
326                 FD_SET(fd_set2->fd_array[i], &tmpset);
327         }
328         *fd_set1 = tmpset;
329     }
330 }
331 #else
332 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
333 {
334     int i, n;
335
336     if (nfds == 0 || fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
337         return;
338
339     n = INTS_PER_FDS(nfds);
340     for (i=0; i<n; i++) {
341         fd_set1->FDS_BITS[i] &= fd_set2->FDS_BITS[i];
342     }
343 }
344 #endif
345             
346 /* FDSetEmpty - return true if fd_set is empty 
347  */
348 static int FDSetEmpty(int nfds, fd_set *fds)
349 {
350 #ifndef AFS_NT40_ENV
351     int i, n;
352
353     if (nfds == 0)
354         return 1;
355 #endif
356     if (fds == (fd_set*)0)
357         return 1;
358
359 #ifdef AFS_NT40_ENV
360     if (fds->fd_count == 0)
361         return 1;
362     else
363         return 0;
364 #else
365     n = INTS_PER_FDS(nfds);
366
367     for (i=n-1; i>=0; i--) {
368         if (fds->FDS_BITS[i])
369             break;
370     }
371
372     if (i>=0)
373         return 0;
374     else
375         return 1;
376 #endif
377 }
378
379 /* The IOMGR process */
380
381 /*
382  * Important invariant: process->iomgrRequest is null iff request not in timer
383  * queue.
384  * also, request->pid is valid while request is in queue,
385  * also, don't signal selector while request in queue, since selector frees
386  *  request.
387  */
388
389 /* These are not declared in IOMGR so that they don't use up 6K of stack. */
390 static fd_set IOMGR_readfds, IOMGR_writefds, IOMGR_exceptfds;
391 static int IOMGR_nfds = 0;
392
393 static void *IOMGR(void *dummy)
394 {
395     for (;;) {
396         int code;
397         struct TM_Elem *earliest;
398         struct timeval timeout, junk;
399         bool woke_someone;
400
401         FD_ZERO(&IOMGR_readfds);
402         FD_ZERO(&IOMGR_writefds);
403         FD_ZERO(&IOMGR_exceptfds);
404         IOMGR_nfds = 0;
405
406         /* Wake up anyone who has expired or who has received a
407            Unix signal between executions.  Keep going until we
408            run out. */
409         do {
410             woke_someone = FALSE;
411             /* Wake up anyone waiting on signals. */
412             /* Note: SignalSignals() may yield! */
413             if (anySigsDelivered && SignalSignals ())
414                 woke_someone = TRUE;
415             FT_GetTimeOfDay(&junk, 0);    /* force accurate time check */
416             TM_Rescan(Requests);
417             for (;;) {
418                 register struct IoRequest *req;
419                 struct TM_Elem *expired;
420                 expired = TM_GetExpired(Requests);
421                 if (expired == NULL) break;
422                 woke_someone = TRUE;
423                 req = (struct IoRequest *) expired -> BackPointer;
424 #ifdef DEBUG
425                 if (lwp_debug != 0) puts("[Polling SELECT]");
426 #endif /* DEBUG */
427                 /* no data ready */
428                 if (req->readfds)   FD_N_ZERO(req->nfds, req->readfds);
429                 if (req->writefds)  FD_N_ZERO(req->nfds, req->writefds);
430                 if (req->exceptfds) FD_N_ZERO(req->nfds, req->exceptfds);
431                 req->nfds = 0;
432                 req->result = 0; /* no fds ready */
433                 TM_Remove(Requests, &req->timeout);
434 #ifdef DEBUG
435                 req -> timeout.Next = (struct TM_Elem *) 2;
436                 req -> timeout.Prev = (struct TM_Elem *) 2;
437 #endif /* DEBUG */
438                 LWP_QSignal(req->pid);
439                 req->pid->iomgrRequest = 0;
440             }
441
442             if (woke_someone) LWP_DispatchProcess();
443         } while (woke_someone);
444
445         /* Collect requests & update times */
446         FD_ZERO(&IOMGR_readfds);
447         FD_ZERO(&IOMGR_writefds);
448         FD_ZERO(&IOMGR_exceptfds);
449         IOMGR_nfds = 0;
450
451         FOR_ALL_ELTS(r, Requests, {
452             register struct IoRequest *req;
453             req = (struct IoRequest *) r -> BackPointer;
454             FDSetSet(req->nfds, &IOMGR_readfds,   req->readfds);
455             FDSetSet(req->nfds, &IOMGR_writefds,  req->writefds);
456             FDSetSet(req->nfds, &IOMGR_exceptfds, req->exceptfds);
457             if (req->nfds > IOMGR_nfds)
458                 IOMGR_nfds = req->nfds;
459         })
460         earliest = TM_GetEarliest(Requests);
461         if (earliest != NULL) {
462             timeout = earliest -> TimeLeft;
463
464
465             /* Do select */
466 #ifdef DEBUG
467             if (lwp_debug != 0) {
468 #ifdef AFS_NT40_ENV
469                 int idbg;
470                 printf("[Read Select:");
471                 if (IOMGR_readfds.fd_count == 0)
472                     printf(" none]\n");
473                 else {
474                     for (idbg=0; idbg<IOMGR_readfds.fd_count; idbg++)
475                         printf(" %d", IOMGR_readfds.fd_array[idbg]);
476                     printf("]\n");
477                 }
478                 printf("[Write Select:");
479                 if (IOMGR_writefds.fd_count == 0)
480                     printf(" none]\n");
481                 else {
482                     for (idbg=0; idbg<IOMGR_writefds.fd_count; idbg++)
483                         printf(" %d", IOMGR_writefds.fd_array[idbg]);
484                     printf("]\n");
485                 }
486                 printf("[Except Select:");
487                 if (IOMGR_exceptfds.fd_count == 0)
488                     printf(" none]\n");
489                 else {
490                     for (idbg=0; idbg<IOMGR_exceptfds.fd_count; idbg++)
491                         printf(" %d", IOMGR_exceptfds.fd_array[idbg]);
492                     printf("]\n");
493                 }
494 #else
495                 /* Only prints first 32. */
496                 printf("[select(%d, 0x%x, 0x%x, 0x%x, ", IOMGR_nfds,
497                        *(int*)&IOMGR_readfds, *(int*)&IOMGR_writefds,
498                        *(int*)&IOMGR_exceptfds);
499 #endif /* AFS_NT40_ENV */
500                 if (timeout.tv_sec == -1 && timeout.tv_usec == -1)
501                     puts("INFINITE)]");
502                 else
503                     printf("<%d, %d>)]\n", timeout.tv_sec, timeout.tv_usec);
504             }
505 #endif /* DEBUG */
506             iomgr_timeout = timeout;
507             if (timeout.tv_sec == -1 && timeout.tv_usec == -1) {
508                 /* infinite, sort of */
509                 iomgr_timeout.tv_sec = 100000000;
510                 iomgr_timeout.tv_usec = 0;
511             }
512 #if defined(AFS_NT40_ENV) || defined(AFS_LINUX24_ENV)
513             /* On NT, signals don't interrupt a select call. So this can potentially
514              * lead to long wait times before a signal is honored. To avoid this we
515              * dont do select() for longer than IOMGR_MAXWAITTIME (5 secs) */
516             /* Whereas Linux seems to sometimes "lose" signals */
517             if (iomgr_timeout.tv_sec > (IOMGR_MAXWAITTIME - 1)) {
518               iomgr_timeout.tv_sec = IOMGR_MAXWAITTIME;
519               iomgr_timeout.tv_usec = 0;
520             }
521 #endif /* NT40 */
522
523             /* Check one last time for a signal delivery.  If one comes after
524                this, the signal handler will set iomgr_timeout to zero, causing
525                the select to return immediately.  The timer package won't return
526                a zero timeval because all of those guys were handled above.
527
528                I'm assuming that the kernel masks signals while it's picking up
529                the parameters to select.  This may a bad assumption.  -DN */
530             if (anySigsDelivered)
531                 continue;       /* go to the top and handle them. */
532
533 #ifdef AFS_NT40_ENV
534             if (IOMGR_readfds.fd_count == 0 && IOMGR_writefds.fd_count == 0
535                 && IOMGR_exceptfds.fd_count == 0) {
536                 DWORD stime;
537                 code = 0;
538                 if (iomgr_timeout.tv_sec || iomgr_timeout.tv_usec) {
539                     stime = iomgr_timeout.tv_sec * 1000
540                         + iomgr_timeout.tv_usec/1000;
541                     if (!stime)
542                         stime = 1;
543                     Sleep(stime);
544                 }
545             }
546             else
547 #endif
548                 {    /* select runs much faster if 0's are passed instead of &0s */
549                     code = select(IOMGR_nfds,
550                                   (FDSetEmpty(IOMGR_nfds, &IOMGR_readfds)) ?
551                                   (fd_set*)0 : &IOMGR_readfds,
552                                   (FDSetEmpty(IOMGR_nfds, &IOMGR_writefds)) ?
553                                   (fd_set*)0 : &IOMGR_writefds,
554                                   (FDSetEmpty(IOMGR_nfds, &IOMGR_exceptfds)) ?
555                                   (fd_set*)0 : &IOMGR_exceptfds,
556                                   &iomgr_timeout);
557                 }
558
559             if (code < 0) {
560                int e=1;
561
562 #if defined(AFS_SUN_ENV)
563                /* Tape drives on Sun boxes do not support select and return ENXIO */
564                if (errno == ENXIO) e=0, code=1;
565 #endif
566 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV)
567                /* For SGI and SVR4 - poll & select can return EAGAIN ... */
568                if (errno == EAGAIN) e=0;
569 #endif
570 #if defined(AFS_SUN5_ENV)
571                /* On sun4x_55, select doesn't block signal. It could be
572                   interupted by a signal that changes iomgr_timeout, and
573                   then select returns with EINVAL. In this case, we need
574                   to retry.
575                 */
576                if (errno==EINVAL && anySigsDelivered)
577                    e = 0;
578 #endif /* AFS_SUN5_ENV */
579
580                if ((errno != EINTR) && e) {
581 #ifndef AFS_NT40_ENV
582                   int i;
583                   for(i=0; i<FD_SETSIZE; i++) {
584                      if (fcntl(i, F_GETFD, 0) < 0 && errno == EBADF)
585                          FD_SET(i, &openMask);
586                   }
587 #endif
588                   iomgr_errno = errno;
589                   lwp_abort();
590                }
591             }
592
593             /* See what happened */
594             if (code > 0) {
595                 /* Action -- wake up everyone involved */
596                 SignalIO(IOMGR_nfds, &IOMGR_readfds, &IOMGR_writefds,
597                          &IOMGR_exceptfds, code);
598             }
599             else if (code == 0
600                 && (iomgr_timeout.tv_sec != 0 || iomgr_timeout.tv_usec != 0)) {
601                 /* Real timeout only if signal handler hasn't set
602                    iomgr_timeout to zero. */
603
604 #if defined(AFS_NT40_ENV) || defined(AFS_LINUX24_ENV)
605                 /* On NT, real timeout only if above and if iomgr_timeout
606                  * interval is equal to timeout interval (i.e., not adjusted
607                  * to check for pseudo-signals).
608                  */
609                 /* And also for Linux as above */
610                 if (iomgr_timeout.tv_sec  != timeout.tv_sec ||
611                     iomgr_timeout.tv_usec != timeout.tv_usec) {
612                     /* signal check interval timed out; not real timeout */
613                     continue;
614                 }
615 #endif /* AFS_NT40_ENV */
616                 FT_GetTimeOfDay(&junk, 0);
617                 SignalTimeout(code, &timeout);
618             }
619         }
620         LWP_DispatchProcess();
621     }
622     return (void *)-1; /* keeps compilers happy. */
623 }
624 \f
625 /************************\
626 *                        *
627 *  Signalling routines   *
628 *                        *
629 \************************/
630
631 static void SignalIO(int fds, fd_set *readfds, fd_set *writefds,
632                      fd_set *exceptfds, int code)
633 {
634     int nfds;
635     /* Look at everyone who's bit mask was affected */
636     FOR_ALL_ELTS(r, Requests, {
637         register struct IoRequest *req;
638         register PROCESS pid;
639         req = (struct IoRequest *) r -> BackPointer;
640         nfds = MIN(fds, req->nfds);
641         if (FDSetCmp(nfds, req->readfds, readfds) ||
642             FDSetCmp(nfds, req->writefds, writefds) ||
643             FDSetCmp(nfds, req->exceptfds, exceptfds)) {
644             /* put ready fd's into request. */
645             FDSetAnd(nfds, req->readfds, readfds);
646             FDSetAnd(nfds, req->writefds, writefds);
647             FDSetAnd(nfds, req->exceptfds, exceptfds);
648             req -> result = code;
649             TM_Remove(Requests, &req->timeout);
650             LWP_QSignal(pid=req->pid);
651             pid->iomgrRequest = 0;
652         }
653     })
654 }
655
656 static void SignalTimeout(int code, struct timeval *timeout)
657 {
658     /* Find everyone who has specified timeout */
659     FOR_ALL_ELTS(r, Requests, {
660         register struct IoRequest *req;
661         register PROCESS pid;
662         req = (struct IoRequest *) r -> BackPointer;
663         if (TM_eql(&r->TimeLeft, timeout)) {
664             req -> result = code;
665             TM_Remove(Requests, &req->timeout);
666             LWP_QSignal(pid=req->pid);
667             pid->iomgrRequest = 0;
668         } else
669             return;
670     })
671 }
672 \f
673 /*****************************************************\
674 *                                                     *
675 *  Signal handling routine (not to be confused with   *
676 *  signalling routines, above).                       *
677 *                                                     *
678 \*****************************************************/
679 static void SigHandler (int signo)
680 {
681     if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
682         return;         /* can't happen. */
683     sigDelivered[signo] = TRUE;
684     anySigsDelivered = TRUE;
685     /* Make sure that the IOMGR process doesn't pause on the select. */
686     iomgr_timeout.tv_sec = 0;
687     iomgr_timeout.tv_usec = 0;
688 }
689
690 /* Alright, this is the signal signalling routine.  It delivers LWP signals
691    to LWPs waiting on Unix signals. NOW ALSO CAN YIELD!! */
692 static int SignalSignals (void)
693 {
694     bool gotone = FALSE;
695     register int i;
696     register void *(*p)(void *);
697     afs_int32 stackSize;
698
699     anySigsDelivered = FALSE;
700
701     /* handle software signals */
702     stackSize = (AFS_LWP_MINSTACKSIZE < lwp_MaxStackSeen? lwp_MaxStackSeen : AFS_LWP_MINSTACKSIZE);
703     for (i=0; i < NSOFTSIG; i++) {
704         PROCESS pid;
705         if ((p=sigProc[i])) /* This yields!!! */
706             LWP_CreateProcess2(p, stackSize, LWP_NORMAL_PRIORITY, 
707                                sigRock[i], "SignalHandler", &pid);
708         sigProc[i] = 0;
709     }
710
711     for (i = 1; i <= NSIG; ++i)  /* forall !badsig(i) */
712         if ((sigsHandled & mysigmask(i)) && sigDelivered[i] == TRUE) {
713             sigDelivered[i] = FALSE;
714             LWP_NoYieldSignal (sigEvents[i]);
715             gotone = TRUE;
716         }
717     return gotone;
718 }
719
720 \f
721 /***************************\
722 *                           *
723 *  User-callable routines   *
724 *                           *
725 \***************************/
726
727
728 /* Keep IOMGR process id */
729 static PROCESS IOMGR_Id = NULL;
730
731 int IOMGR_SoftSig(void *(*aproc)(void *), void *arock)
732 {
733     register int i;
734     for (i=0;i<NSOFTSIG;i++) {
735         if (sigProc[i] == 0) {
736             /* a free entry */
737             sigProc[i] = aproc;
738             sigRock[i] = arock;
739             anySigsDelivered = TRUE;
740             iomgr_timeout.tv_sec = 0;
741             iomgr_timeout.tv_usec = 0;
742             return 0;
743         }
744     }
745     return -1;
746 }
747
748
749 int IOMGR_Initialize(void)
750 {
751     PROCESS pid;
752
753     /* If lready initialized, just return */
754     if (IOMGR_Id != NULL) return LWP_SUCCESS;
755
756     /* Init LWP if someone hasn't yet. */
757     if (LWP_InitializeProcessSupport (LWP_NORMAL_PRIORITY, &pid) != LWP_SUCCESS)
758         return -1;
759
760     /* Initialize request lists */
761     if (TM_Init(&Requests) < 0) return -1;
762
763     /* Initialize signal handling stuff. */
764     sigsHandled = 0;
765     anySigsDelivered = TRUE; /* A soft signal may have happened before
766         IOMGR_Initialize:  so force a check for signals regardless */
767
768     return LWP_CreateProcess(IOMGR, AFS_LWP_MINSTACKSIZE, 0, (void *) 0, 
769                              "IO MANAGER", &IOMGR_Id);
770 }
771
772 int IOMGR_Finalize(void)
773 {
774     int status;
775
776     Purge(Requests)
777     TM_Final(&Requests);
778     status = LWP_DestroyProcess(IOMGR_Id);
779     IOMGR_Id = NULL;
780     return status;
781 }
782 \f
783 /* signal I/O for anyone who is waiting for a FD or a timeout; not too cheap,
784  * since forces select and timeofday check */
785 int IOMGR_Poll(void) {
786     fd_set *readfds, *writefds, *exceptfds;
787     afs_int32 code;
788     struct timeval tv;
789     int fds;
790
791     FT_GetTimeOfDay(&tv, 0);    /* force accurate time check */
792     TM_Rescan(Requests);
793     for (;;) {
794         register struct IoRequest *req;
795         struct TM_Elem *expired;
796         expired = TM_GetExpired(Requests);
797         if (expired == NULL) break;
798         req = (struct IoRequest *) expired -> BackPointer;
799 #ifdef DEBUG
800         if (lwp_debug != 0) puts("[Polling SELECT]");
801 #endif /* DEBUG */
802         /* no data ready */
803         if (req->readfds)   FD_N_ZERO(req->nfds, req->readfds);
804         if (req->writefds)  FD_N_ZERO(req->nfds, req->writefds);
805         if (req->exceptfds) FD_N_ZERO(req->nfds, req->exceptfds);
806         req->nfds = 0;
807         req->result = 0; /* no fds ready */
808         TM_Remove(Requests, &req->timeout);
809 #ifdef DEBUG
810         req -> timeout.Next = (struct TM_Elem *) 2;
811         req -> timeout.Prev = (struct TM_Elem *) 2;
812 #endif /* DEBUG */
813         LWP_QSignal(req->pid);
814         req->pid->iomgrRequest = 0;
815     }
816
817     /* Collect requests & update times */
818     readfds = IOMGR_AllocFDSet();
819     writefds = IOMGR_AllocFDSet();
820     exceptfds = IOMGR_AllocFDSet();
821     if (!(readfds && writefds && exceptfds)) {
822         fprintf(stderr, "IOMGR_Poll: Could not malloc space for fd_sets.\n");
823         fflush(stderr);
824     }
825
826     fds = 0;
827
828     FOR_ALL_ELTS(r, Requests, {
829         register struct IoRequest *req;
830         req = (struct IoRequest *) r -> BackPointer;
831         FDSetSet(req->nfds, readfds,   req->readfds);
832         FDSetSet(req->nfds, writefds,  req->writefds);
833         FDSetSet(req->nfds, exceptfds, req->exceptfds);
834         if (fds < req->nfds)
835             fds = req->nfds;
836     })
837         
838     tv.tv_sec = 0;
839     tv.tv_usec = 0;
840 #ifdef AFS_NT40_ENV
841     code = -1;
842     if (readfds->fd_count == 0 && writefds->fd_count == 0
843         && exceptfds->fd_count == 0)
844 #endif
845         code = select(fds, readfds, writefds, exceptfds, &tv);
846     if (code > 0) {
847         SignalIO(fds, readfds, writefds, exceptfds, code);
848     }
849
850     if (readfds) IOMGR_FreeFDSet(readfds);
851     if (writefds) IOMGR_FreeFDSet(writefds);
852     if (exceptfds) IOMGR_FreeFDSet(exceptfds);
853
854
855     LWP_DispatchProcess();  /* make sure others run */
856     LWP_DispatchProcess();
857     return 0;
858 }
859
860 int IOMGR_Select(int fds, fd_set *readfds, fd_set *writefds, 
861                  fd_set *exceptfds, struct timeval *timeout)
862 {
863     register struct IoRequest *request;
864     int result;
865
866 #ifndef AFS_NT40_ENV
867     if(fds > FD_SETSIZE) {
868         fprintf(stderr, "IOMGR_Select: fds=%d, more than max %d\n",
869                 fds, FD_SETSIZE);
870         fflush(stderr);
871         lwp_abort();
872     }
873 #endif
874
875     /* See if polling request. If so, handle right here */
876     if (timeout != NULL) {
877         if (timeout->tv_sec == 0 && timeout->tv_usec == 0) {
878             int code;
879 #ifdef DEBUG
880             if (lwp_debug != 0) puts("[Polling SELECT]");
881 #endif /* DEBUG */
882 #if     defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_NT40_ENV)
883 again:
884 #endif
885             code = select(fds, readfds, writefds, exceptfds, timeout);
886 #if     defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV)
887             /*
888              * For SGI and SVR4 - poll & select can return EAGAIN ...
889              */
890             /*
891              * this is basically for SGI, but I believe stock SVR4 (Solaris?)
892              * can also get this error return
893              */
894             if (code < 0 && errno == EAGAIN)
895                 goto again;     
896 #endif
897 #ifdef AFS_NT40_ENV
898             if (code == SOCKET_ERROR) {
899                 if (WSAGetLastError() == WSAEINPROGRESS)
900                     goto again;
901
902                 code = -1;
903             }
904 #endif
905             return (code > 1 ? 1 : code);
906         }
907     }
908
909     /* Construct request block & insert */
910     request = NewRequest(); /* zeroes fd_set's */
911     if (readfds && !FDSetEmpty(fds, readfds))
912         request->readfds = readfds;
913     if (writefds && !FDSetEmpty(fds, writefds))
914         request->writefds = writefds;
915     if (exceptfds && !FDSetEmpty(fds, exceptfds))
916         request->exceptfds = exceptfds;
917     request->nfds = fds;
918
919     if (timeout == NULL) {
920         request -> timeout.TotalTime.tv_sec = -1;
921         request -> timeout.TotalTime.tv_usec = -1;
922     } else {
923         request -> timeout.TotalTime = *timeout;
924         /* check for bad request */
925         if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec > 999999) {
926             /* invalid arg */
927             iomgr_badtv = *timeout;
928             iomgr_badpid = LWP_ActiveProcess;
929             /* now fixup request */
930             if(request->timeout.TotalTime.tv_sec < 0)
931                 request->timeout.TotalTime.tv_sec = 1;
932             request->timeout.TotalTime.tv_usec = 100000;
933         }
934     }
935
936     request -> timeout.BackPointer = (char *) request;
937
938     /* Insert my PID in case of IOMGR_Cancel */
939     request -> pid = LWP_ActiveProcess;
940     LWP_ActiveProcess -> iomgrRequest = request;
941
942 #ifdef DEBUG
943     request -> timeout.Next = (struct TM_Elem *) 1;
944     request -> timeout.Prev = (struct TM_Elem *) 1;
945 #endif /* DEBUG */
946     TM_Insert(Requests, &request->timeout);
947
948     /* Wait for action */
949     LWP_QWait();
950
951     /* Update parameters & return */
952     result = request -> result;
953
954     FreeRequest(request);
955     return (result > 1 ? 1 : result);
956 }
957 \f
958 int IOMGR_Cancel(PROCESS pid)
959 {
960     register struct IoRequest *request;
961
962     if ((request = pid->iomgrRequest) == 0) return -1;  /* Pid not found */
963
964     if (request->readfds)   FD_N_ZERO(request->nfds, request->readfds);
965     if (request->writefds)  FD_N_ZERO(request->nfds, request->writefds);
966     if (request->exceptfds) FD_N_ZERO(request->nfds, request->exceptfds);
967     request->nfds = 0;
968
969     request -> result = -2;
970     TM_Remove(Requests, &request->timeout);
971 #ifdef DEBUG
972     request -> timeout.Next = (struct TM_Elem *) 5;
973     request -> timeout.Prev = (struct TM_Elem *) 5;
974 #endif /* DEBUG */
975     LWP_QSignal(request->pid);
976     pid->iomgrRequest = 0;
977
978     return 0;
979 }
980 \f
981 #ifndef AFS_NT40_ENV
982 /* Cause delivery of signal signo to result in a LWP_SignalProcess of
983    event. */
984 int IOMGR_Signal (int signo, char *event)
985 {
986     struct sigaction sa;
987
988     if (badsig(signo))
989         return LWP_EBADSIG;
990     if (event == NULL)
991         return LWP_EBADEVENT;
992     sa.sa_handler = SigHandler;
993     sigfillset(&sa.sa_mask);    /* mask all signals */
994     sa.sa_flags = 0;
995     sigsHandled |= mysigmask(signo);
996     sigEvents[signo] = event;
997     sigDelivered[signo] = FALSE;
998     if (sigaction (signo, &sa, &oldActions[signo]) == -1)
999         return LWP_ESYSTEM;
1000     return LWP_SUCCESS;
1001 }
1002
1003 /* Stop handling occurrences of signo. */
1004 int IOMGR_CancelSignal (int signo)
1005 {
1006     if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
1007         return LWP_EBADSIG;
1008     sigaction (signo, &oldActions[signo], NULL);
1009     sigsHandled &= ~mysigmask(signo);
1010     return LWP_SUCCESS;
1011 }
1012 #endif /* AFS_NT40_ENV */
1013 /* This routine calls select is a fashion that simulates the standard sleep routine */
1014 void IOMGR_Sleep (int seconds)
1015 {
1016     struct timeval timeout;
1017
1018     timeout.tv_sec = seconds;
1019     timeout.tv_usec = 0;
1020     IOMGR_Select(0, 0, 0, 0, &timeout);
1021 }
1022 #endif  /* USE_PTHREADS */