2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /*******************************************************************\
12 * Information Technology Center *
13 * Carnegie-Mellon University *
17 \*******************************************************************/
21 IO Manager routines & server process for VICE server.
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) */
33 #define FD_SETSIZE 65536
36 #include <afsconfig.h>
37 #include <afs/param.h>
46 extern void lwp_abort(void);
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 */
52 # include <sys/select.h> /* fd_set on newer platforms */
55 #endif /* AFS_NT40_ENV */
64 typedef unsigned char bool;
69 #define MIN(a,b) (((a)>(b)) ? (b) : (a))
73 #define NSIG 8*sizeof(sigset_t)
76 static int SignalSignals(void);
78 /********************************\
80 * Stuff for managing IoRequests *
82 \********************************/
86 /* Pid of process making request (for use in IOMGR_Cancel */
89 /* Descriptor masks for requests */
95 struct TM_Elem timeout;
97 /* Result of select call */
100 struct IoRequest *next; /* for iorFreeList */
104 /********************************\
106 * Stuff for managing signals *
108 \********************************/
110 #define badsig(signo) (((signo) <= 0) || ((signo) >= NSIG))
111 #define mysigmask(signo) (1 << ((signo)-1))
114 fd_set openMask; /* mask of open files on an IOMGR abort */
115 static afs_int32 sigsHandled; /* sigmask(signo) is on if we handle signo */
116 static int anySigsDelivered; /* true if any have been delivered. */
118 static struct sigaction oldActions[NSIG]; /* the old signal vectors */
120 static char *sigEvents[NSIG]; /* the event to do an LWP signal on */
121 static int sigDelivered[NSIG]; /* True for signals delivered so far.
122 This is an int array to make sure
123 there are no conflicts when trying
125 /* software 'signals' */
127 static void *(*sigProc[NSOFTSIG])(void *);
128 static void *sigRock[NSOFTSIG];
131 static struct IoRequest *iorFreeList = 0;
133 static struct TM_Elem *Requests; /* List of requests */
134 static struct timeval iomgr_timeout; /* global so signal handler can zap it */
136 /* stuff for debugging */
137 static int iomgr_errno;
138 static struct timeval iomgr_badtv;
139 static PROCESS iomgr_badpid;
140 static void SignalIO(int fds, fd_set *rfds, fd_set *wfds, fd_set *efs,
142 static void SignalTimeout(int code, struct timeval *timeout);
144 /* fd_set pool managment.
145 * Use the pool instead of creating fd_set's on the stack. fd_set's can be
146 * 8K in size, so making three could put 24K in the limited space of an LWP
149 struct IOMGR_fd_set {
150 struct IOMGR_fd_set *next;
151 } *iomgrFreeFDSets = (struct IOMGR_fd_set*)0;
154 * Return fd_set to the free list.
156 void IOMGR_FreeFDSet(fd_set *s)
158 struct IOMGR_fd_set *t = (struct IOMGR_fd_set *)s;
160 t->next = iomgrFreeFDSets;
165 * returns a zeroed fd_set or null if could not malloc one.
167 fd_set *IOMGR_AllocFDSet(void)
169 struct IOMGR_fd_set *t;
170 if (iomgrFreeFDSets) {
172 iomgrFreeFDSets = iomgrFreeFDSets->next;
175 t = (struct IOMGR_fd_set *)malloc(sizeof(fd_set));
185 #define FreeRequest(x) ((x)->next = iorFreeList, iorFreeList = (x))
187 static struct IoRequest *NewRequest(void)
189 struct IoRequest *request;
191 if ((request=iorFreeList))
192 iorFreeList = (struct IoRequest *) (request->next);
193 else request = (struct IoRequest *) malloc(sizeof(struct IoRequest));
195 memset((char*)request, 0, sizeof(struct IoRequest));
199 #define Purge(list) FOR_ALL_ELTS(req, list, { free(req->BackPointer); })
202 /* FD_SET support routines. All assume the fd_set size is a multiple of an int
203 * so we can at least do logical operations on ints instead of chars.
205 * For each routine, nfds is the highest bit set in either fd_set, starting
209 #define FD_N_ZERO(A, x) FD_ZERO(x)
211 #define FDS_P_POS (sizeof(int)*8)
212 #define INTS_PER_FDS(x) (((x)+(FDS_P_POS-1)) / FDS_P_POS)
213 #define FD_N_ZERO(nfds, x) memset((char*)(x), 0, (INTS_PER_FDS(nfds))*sizeof(int))
216 /* On Linux without __USE_XOPEN, we have __fds_bits. With __USE_XOPEN, or
217 * non-Linux, we have fds_bits. */
218 #if defined(AFS_LINUX22_ENV) && (__GLIBC_MINOR__ > 0) && !defined(__USE_XOPEN)
219 # define FDS_BITS __fds_bits
221 # define FDS_BITS fds_bits
224 /* FDSetCmp - returns 1 if any bits in fd_set1 are also set in fd_set2.
225 * If nfds is 0, or one of the fd_sets is null return 0 (since there is no bit
226 * set in both fd_sets).
228 static int FDSetCmp(int nfds, fd_set *fd_set1, fd_set *fd_set2)
232 if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
236 if (fd_set1->fd_count == 0 || fd_set2->fd_count == 0)
239 for (i=0; i<fd_set1->fd_count; i++) {
240 for (j=0; j<fd_set2->fd_count; j++) {
241 if (fd_set1->fd_array[i] == fd_set2->fd_array[j])
249 j = INTS_PER_FDS(nfds);
250 for (i=0; i<j; i++) {
251 if (fd_set1->FDS_BITS[i] & fd_set2->FDS_BITS[i])
258 /* FDSetSet - set bits from fd_set2 into fd_set1
260 static void FDSetSet(int nfds, fd_set *fd_set1, fd_set *fd_set2)
267 if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
271 if (fd_set2->fd_count==0)
274 for (i=0; i<fd_set2->fd_count; i++)
275 FD_SET(fd_set2->fd_array[i], fd_set1);
280 for (i = 0, n = INTS_PER_FDS(nfds); i < n; i++) {
281 fd_set1->FDS_BITS[i] |= fd_set2->FDS_BITS[i];
286 /* FDSetAnd - fd_set1 <- fd_set1 & fd_set2.
289 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
294 if (fd_set1 == NULL || fd_set1->fd_count == 0)
297 if (fd_set2 == NULL || fd_set2->fd_count == 0) {
302 for (i=0; i<fd_set2->fd_count; i++) {
303 if (FD_ISSET(fd_set2->fd_array[i], fd_set1))
304 FD_SET(fd_set2->fd_array[i], &tmpset);
310 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
314 if (nfds == 0 || fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
317 n = INTS_PER_FDS(nfds);
318 for (i=0; i<n; i++) {
319 fd_set1->FDS_BITS[i] &= fd_set2->FDS_BITS[i];
324 /* FDSetEmpty - return true if fd_set is empty
326 static int FDSetEmpty(int nfds, fd_set *fds)
334 if (fds == (fd_set*)0)
338 if (fds->fd_count == 0)
343 n = INTS_PER_FDS(nfds);
345 for (i=n-1; i>=0; i--) {
346 if (fds->FDS_BITS[i])
357 /* The IOMGR process */
360 * Important invariant: process->iomgrRequest is null iff request not in timer
362 * also, request->pid is valid while request is in queue,
363 * also, don't signal selector while request in queue, since selector frees
367 /* These are not declared in IOMGR so that they don't use up 6K of stack. */
368 static fd_set IOMGR_readfds, IOMGR_writefds, IOMGR_exceptfds;
369 static int IOMGR_nfds = 0;
371 static void *IOMGR(void *dummy)
375 struct TM_Elem *earliest;
376 struct timeval timeout, junk;
379 FD_ZERO(&IOMGR_readfds);
380 FD_ZERO(&IOMGR_writefds);
381 FD_ZERO(&IOMGR_exceptfds);
384 /* Wake up anyone who has expired or who has received a
385 Unix signal between executions. Keep going until we
388 woke_someone = FALSE;
389 /* Wake up anyone waiting on signals. */
390 /* Note: SignalSignals() may yield! */
391 if (anySigsDelivered && SignalSignals ())
393 FT_GetTimeOfDay(&junk, 0); /* force accurate time check */
396 struct IoRequest *req;
397 struct TM_Elem *expired;
398 expired = TM_GetExpired(Requests);
399 if (expired == NULL) break;
401 req = (struct IoRequest *) expired -> BackPointer;
403 if (lwp_debug != 0) puts("[Polling SELECT]");
406 if (req->readfds) FD_N_ZERO(req->nfds, req->readfds);
407 if (req->writefds) FD_N_ZERO(req->nfds, req->writefds);
408 if (req->exceptfds) FD_N_ZERO(req->nfds, req->exceptfds);
410 req->result = 0; /* no fds ready */
411 TM_Remove(Requests, &req->timeout);
413 req -> timeout.Next = (struct TM_Elem *) 2;
414 req -> timeout.Prev = (struct TM_Elem *) 2;
416 LWP_QSignal(req->pid);
417 req->pid->iomgrRequest = 0;
420 if (woke_someone) LWP_DispatchProcess();
421 } while (woke_someone);
423 /* Collect requests & update times */
424 FD_ZERO(&IOMGR_readfds);
425 FD_ZERO(&IOMGR_writefds);
426 FD_ZERO(&IOMGR_exceptfds);
429 FOR_ALL_ELTS(r, Requests, {
430 struct IoRequest *req;
431 req = (struct IoRequest *) r -> BackPointer;
432 FDSetSet(req->nfds, &IOMGR_readfds, req->readfds);
433 FDSetSet(req->nfds, &IOMGR_writefds, req->writefds);
434 FDSetSet(req->nfds, &IOMGR_exceptfds, req->exceptfds);
435 if (req->nfds > IOMGR_nfds)
436 IOMGR_nfds = req->nfds;
438 earliest = TM_GetEarliest(Requests);
439 if (earliest != NULL) {
440 timeout = earliest -> TimeLeft;
445 if (lwp_debug != 0) {
448 printf("[Read Select:");
449 if (IOMGR_readfds.fd_count == 0)
452 for (idbg=0; idbg<IOMGR_readfds.fd_count; idbg++)
453 printf(" %d", IOMGR_readfds.fd_array[idbg]);
456 printf("[Write Select:");
457 if (IOMGR_writefds.fd_count == 0)
460 for (idbg=0; idbg<IOMGR_writefds.fd_count; idbg++)
461 printf(" %d", IOMGR_writefds.fd_array[idbg]);
464 printf("[Except Select:");
465 if (IOMGR_exceptfds.fd_count == 0)
468 for (idbg=0; idbg<IOMGR_exceptfds.fd_count; idbg++)
469 printf(" %d", IOMGR_exceptfds.fd_array[idbg]);
473 /* Only prints first 32. */
474 printf("[select(%d, 0x%x, 0x%x, 0x%x, ", IOMGR_nfds,
475 *(int*)&IOMGR_readfds, *(int*)&IOMGR_writefds,
476 *(int*)&IOMGR_exceptfds);
477 #endif /* AFS_NT40_ENV */
478 if (timeout.tv_sec == -1 && timeout.tv_usec == -1)
481 printf("<%d, %d>)]\n", timeout.tv_sec, timeout.tv_usec);
484 iomgr_timeout = timeout;
485 if (timeout.tv_sec == -1 && timeout.tv_usec == -1) {
486 /* infinite, sort of */
487 iomgr_timeout.tv_sec = 100000000;
488 iomgr_timeout.tv_usec = 0;
490 #if defined(AFS_NT40_ENV) || defined(AFS_LINUX24_ENV)
491 /* On NT, signals don't interrupt a select call. So this can potentially
492 * lead to long wait times before a signal is honored. To avoid this we
493 * dont do select() for longer than IOMGR_MAXWAITTIME (5 secs) */
494 /* Whereas Linux seems to sometimes "lose" signals */
495 if (iomgr_timeout.tv_sec > (IOMGR_MAXWAITTIME - 1)) {
496 iomgr_timeout.tv_sec = IOMGR_MAXWAITTIME;
497 iomgr_timeout.tv_usec = 0;
501 /* Check one last time for a signal delivery. If one comes after
502 this, the signal handler will set iomgr_timeout to zero, causing
503 the select to return immediately. The timer package won't return
504 a zero timeval because all of those guys were handled above.
506 I'm assuming that the kernel masks signals while it's picking up
507 the parameters to select. This may a bad assumption. -DN */
508 if (anySigsDelivered)
509 continue; /* go to the top and handle them. */
512 if (IOMGR_readfds.fd_count == 0 && IOMGR_writefds.fd_count == 0
513 && IOMGR_exceptfds.fd_count == 0) {
516 if (iomgr_timeout.tv_sec || iomgr_timeout.tv_usec) {
517 stime = iomgr_timeout.tv_sec * 1000
518 + iomgr_timeout.tv_usec/1000;
526 { /* select runs much faster if 0's are passed instead of &0s */
527 code = select(IOMGR_nfds,
528 (FDSetEmpty(IOMGR_nfds, &IOMGR_readfds)) ?
529 (fd_set*)0 : &IOMGR_readfds,
530 (FDSetEmpty(IOMGR_nfds, &IOMGR_writefds)) ?
531 (fd_set*)0 : &IOMGR_writefds,
532 (FDSetEmpty(IOMGR_nfds, &IOMGR_exceptfds)) ?
533 (fd_set*)0 : &IOMGR_exceptfds,
540 #if defined(AFS_SUN_ENV)
541 /* Tape drives on Sun boxes do not support select and return ENXIO */
542 if (errno == ENXIO) e=0, code=1;
544 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV)
545 /* For SGI and SVR4 - poll & select can return EAGAIN ... */
546 if (errno == EAGAIN) e=0;
548 #if defined(AFS_SUN5_ENV)
549 /* On sun4x_55, select doesn't block signal. It could be
550 interupted by a signal that changes iomgr_timeout, and
551 then select returns with EINVAL. In this case, we need
554 if (errno==EINVAL && anySigsDelivered)
556 #endif /* AFS_SUN5_ENV */
558 if ((errno != EINTR) && e) {
561 for(i=0; i<FD_SETSIZE; i++) {
562 if (fcntl(i, F_GETFD, 0) < 0 && errno == EBADF)
563 FD_SET(i, &openMask);
571 /* See what happened */
573 /* Action -- wake up everyone involved */
574 SignalIO(IOMGR_nfds, &IOMGR_readfds, &IOMGR_writefds,
575 &IOMGR_exceptfds, code);
578 && (iomgr_timeout.tv_sec != 0 || iomgr_timeout.tv_usec != 0)) {
579 /* Real timeout only if signal handler hasn't set
580 iomgr_timeout to zero. */
582 #if defined(AFS_NT40_ENV) || defined(AFS_LINUX24_ENV)
583 /* On NT, real timeout only if above and if iomgr_timeout
584 * interval is equal to timeout interval (i.e., not adjusted
585 * to check for pseudo-signals).
587 /* And also for Linux as above */
588 if (iomgr_timeout.tv_sec != timeout.tv_sec ||
589 iomgr_timeout.tv_usec != timeout.tv_usec) {
590 /* signal check interval timed out; not real timeout */
593 #endif /* AFS_NT40_ENV */
594 FT_GetTimeOfDay(&junk, 0);
595 SignalTimeout(code, &timeout);
598 LWP_DispatchProcess();
600 return (void *)-1; /* keeps compilers happy. */
603 /************************\
605 * Signalling routines *
607 \************************/
609 static void SignalIO(int fds, fd_set *readfds, fd_set *writefds,
610 fd_set *exceptfds, int code)
613 /* Look at everyone who's bit mask was affected */
614 FOR_ALL_ELTS(r, Requests, {
615 struct IoRequest *req;
617 req = (struct IoRequest *) r -> BackPointer;
618 nfds = MIN(fds, req->nfds);
619 if (FDSetCmp(nfds, req->readfds, readfds) ||
620 FDSetCmp(nfds, req->writefds, writefds) ||
621 FDSetCmp(nfds, req->exceptfds, exceptfds)) {
622 /* put ready fd's into request. */
623 FDSetAnd(nfds, req->readfds, readfds);
624 FDSetAnd(nfds, req->writefds, writefds);
625 FDSetAnd(nfds, req->exceptfds, exceptfds);
626 req -> result = code;
627 TM_Remove(Requests, &req->timeout);
628 LWP_QSignal(pid=req->pid);
629 pid->iomgrRequest = 0;
634 static void SignalTimeout(int code, struct timeval *timeout)
636 /* Find everyone who has specified timeout */
637 FOR_ALL_ELTS(r, Requests, {
638 struct IoRequest *req;
640 req = (struct IoRequest *) r -> BackPointer;
641 if (TM_eql(&r->TimeLeft, timeout)) {
642 req -> result = code;
643 TM_Remove(Requests, &req->timeout);
644 LWP_QSignal(pid=req->pid);
645 pid->iomgrRequest = 0;
651 /*****************************************************\
653 * Signal handling routine (not to be confused with *
654 * signalling routines, above). *
656 \*****************************************************/
657 static void SigHandler (int signo)
659 if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
660 return; /* can't happen. */
661 sigDelivered[signo] = TRUE;
662 anySigsDelivered = TRUE;
663 /* Make sure that the IOMGR process doesn't pause on the select. */
664 iomgr_timeout.tv_sec = 0;
665 iomgr_timeout.tv_usec = 0;
668 /* Alright, this is the signal signalling routine. It delivers LWP signals
669 to LWPs waiting on Unix signals. NOW ALSO CAN YIELD!! */
670 static int SignalSignals (void)
677 anySigsDelivered = FALSE;
679 /* handle software signals */
680 stackSize = (AFS_LWP_MINSTACKSIZE < lwp_MaxStackSeen? lwp_MaxStackSeen : AFS_LWP_MINSTACKSIZE);
681 for (i=0; i < NSOFTSIG; i++) {
683 if ((p=sigProc[i])) /* This yields!!! */
684 LWP_CreateProcess2(p, stackSize, LWP_NORMAL_PRIORITY,
685 sigRock[i], "SignalHandler", &pid);
689 for (i = 1; i <= NSIG; ++i) /* forall !badsig(i) */
690 if ((sigsHandled & mysigmask(i)) && sigDelivered[i] == TRUE) {
691 sigDelivered[i] = FALSE;
692 LWP_NoYieldSignal (sigEvents[i]);
699 /***************************\
701 * User-callable routines *
703 \***************************/
706 /* Keep IOMGR process id */
707 static PROCESS IOMGR_Id = NULL;
709 int IOMGR_SoftSig(void *(*aproc)(void *), void *arock)
712 for (i=0;i<NSOFTSIG;i++) {
713 if (sigProc[i] == 0) {
717 anySigsDelivered = TRUE;
718 iomgr_timeout.tv_sec = 0;
719 iomgr_timeout.tv_usec = 0;
727 int IOMGR_Initialize(void)
731 /* If lready initialized, just return */
732 if (IOMGR_Id != NULL) return LWP_SUCCESS;
734 /* Init LWP if someone hasn't yet. */
735 if (LWP_InitializeProcessSupport (LWP_NORMAL_PRIORITY, &pid) != LWP_SUCCESS)
738 /* Initialize request lists */
739 if (TM_Init(&Requests) < 0) return -1;
741 /* Initialize signal handling stuff. */
743 anySigsDelivered = TRUE; /* A soft signal may have happened before
744 IOMGR_Initialize: so force a check for signals regardless */
746 return LWP_CreateProcess(IOMGR, AFS_LWP_MINSTACKSIZE, 0, (void *) 0,
747 "IO MANAGER", &IOMGR_Id);
750 int IOMGR_Finalize(void)
756 status = LWP_DestroyProcess(IOMGR_Id);
761 /* signal I/O for anyone who is waiting for a FD or a timeout; not too cheap,
762 * since forces select and timeofday check */
763 int IOMGR_Poll(void) {
764 fd_set *readfds, *writefds, *exceptfds;
769 FT_GetTimeOfDay(&tv, 0); /* force accurate time check */
772 struct IoRequest *req;
773 struct TM_Elem *expired;
774 expired = TM_GetExpired(Requests);
775 if (expired == NULL) break;
776 req = (struct IoRequest *) expired -> BackPointer;
778 if (lwp_debug != 0) puts("[Polling SELECT]");
781 if (req->readfds) FD_N_ZERO(req->nfds, req->readfds);
782 if (req->writefds) FD_N_ZERO(req->nfds, req->writefds);
783 if (req->exceptfds) FD_N_ZERO(req->nfds, req->exceptfds);
785 req->result = 0; /* no fds ready */
786 TM_Remove(Requests, &req->timeout);
788 req -> timeout.Next = (struct TM_Elem *) 2;
789 req -> timeout.Prev = (struct TM_Elem *) 2;
791 LWP_QSignal(req->pid);
792 req->pid->iomgrRequest = 0;
795 /* Collect requests & update times */
796 readfds = IOMGR_AllocFDSet();
797 writefds = IOMGR_AllocFDSet();
798 exceptfds = IOMGR_AllocFDSet();
799 if (!(readfds && writefds && exceptfds)) {
800 fprintf(stderr, "IOMGR_Poll: Could not malloc space for fd_sets.\n");
806 FOR_ALL_ELTS(r, Requests, {
807 struct IoRequest *req;
808 req = (struct IoRequest *) r -> BackPointer;
809 FDSetSet(req->nfds, readfds, req->readfds);
810 FDSetSet(req->nfds, writefds, req->writefds);
811 FDSetSet(req->nfds, exceptfds, req->exceptfds);
820 if (readfds->fd_count == 0 && writefds->fd_count == 0
821 && exceptfds->fd_count == 0)
823 code = select(fds, readfds, writefds, exceptfds, &tv);
825 SignalIO(fds, readfds, writefds, exceptfds, code);
828 if (readfds) IOMGR_FreeFDSet(readfds);
829 if (writefds) IOMGR_FreeFDSet(writefds);
830 if (exceptfds) IOMGR_FreeFDSet(exceptfds);
833 LWP_DispatchProcess(); /* make sure others run */
834 LWP_DispatchProcess();
838 int IOMGR_Select(int fds, fd_set *readfds, fd_set *writefds,
839 fd_set *exceptfds, struct timeval *timeout)
841 struct IoRequest *request;
845 if(fds > FD_SETSIZE) {
846 fprintf(stderr, "IOMGR_Select: fds=%d, more than max %d\n",
853 /* See if polling request. If so, handle right here */
854 if (timeout != NULL) {
855 if (timeout->tv_sec == 0 && timeout->tv_usec == 0) {
858 if (lwp_debug != 0) puts("[Polling SELECT]");
860 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_NT40_ENV)
863 code = select(fds, readfds, writefds, exceptfds, timeout);
864 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV)
866 * For SGI and SVR4 - poll & select can return EAGAIN ...
869 * this is basically for SGI, but I believe stock SVR4 (Solaris?)
870 * can also get this error return
872 if (code < 0 && errno == EAGAIN)
876 if (code == SOCKET_ERROR) {
877 if (WSAGetLastError() == WSAEINPROGRESS)
883 return (code > 1 ? 1 : code);
887 /* Construct request block & insert */
888 request = NewRequest(); /* zeroes fd_set's */
889 if (readfds && !FDSetEmpty(fds, readfds))
890 request->readfds = readfds;
891 if (writefds && !FDSetEmpty(fds, writefds))
892 request->writefds = writefds;
893 if (exceptfds && !FDSetEmpty(fds, exceptfds))
894 request->exceptfds = exceptfds;
897 if (timeout == NULL) {
898 request -> timeout.TotalTime.tv_sec = -1;
899 request -> timeout.TotalTime.tv_usec = -1;
901 request -> timeout.TotalTime = *timeout;
902 /* check for bad request */
903 if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec > 999999) {
905 iomgr_badtv = *timeout;
906 iomgr_badpid = LWP_ActiveProcess;
907 /* now fixup request */
908 if(request->timeout.TotalTime.tv_sec < 0)
909 request->timeout.TotalTime.tv_sec = 1;
910 request->timeout.TotalTime.tv_usec = 100000;
914 request -> timeout.BackPointer = (char *) request;
916 /* Insert my PID in case of IOMGR_Cancel */
917 request -> pid = LWP_ActiveProcess;
918 LWP_ActiveProcess -> iomgrRequest = request;
921 request -> timeout.Next = (struct TM_Elem *) 1;
922 request -> timeout.Prev = (struct TM_Elem *) 1;
924 TM_Insert(Requests, &request->timeout);
926 /* Wait for action */
929 /* Update parameters & return */
930 result = request -> result;
932 FreeRequest(request);
933 return (result > 1 ? 1 : result);
936 int IOMGR_Cancel(PROCESS pid)
938 struct IoRequest *request;
940 if ((request = pid->iomgrRequest) == 0) return -1; /* Pid not found */
942 if (request->readfds) FD_N_ZERO(request->nfds, request->readfds);
943 if (request->writefds) FD_N_ZERO(request->nfds, request->writefds);
944 if (request->exceptfds) FD_N_ZERO(request->nfds, request->exceptfds);
947 request -> result = -2;
948 TM_Remove(Requests, &request->timeout);
950 request -> timeout.Next = (struct TM_Elem *) 5;
951 request -> timeout.Prev = (struct TM_Elem *) 5;
953 LWP_QSignal(request->pid);
954 pid->iomgrRequest = 0;
960 /* Cause delivery of signal signo to result in a LWP_SignalProcess of
962 int IOMGR_Signal (int signo, char *event)
969 return LWP_EBADEVENT;
970 sa.sa_handler = SigHandler;
971 sigfillset(&sa.sa_mask); /* mask all signals */
973 sigsHandled |= mysigmask(signo);
974 sigEvents[signo] = event;
975 sigDelivered[signo] = FALSE;
976 if (sigaction (signo, &sa, &oldActions[signo]) == -1)
981 /* Stop handling occurrences of signo. */
982 int IOMGR_CancelSignal (int signo)
984 if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
986 sigaction (signo, &oldActions[signo], NULL);
987 sigsHandled &= ~mysigmask(signo);
990 #endif /* AFS_NT40_ENV */
991 /* This routine calls select is a fashion that simulates the standard sleep routine */
992 void IOMGR_Sleep (int seconds)
994 struct timeval timeout;
996 timeout.tv_sec = seconds;
998 IOMGR_Select(0, 0, 0, 0, &timeout);