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 #if defined(USE_PTHREADS) || defined(USE_SOLARIS_THREADS)
66 void IOMGR_Initialize() /* noop */
69 void IOMGR_Sleep (seconds)
76 assert(pthread_mutex_unlock(&lwp_mutex) == 0);
77 assert(pthread_delay_np(&itv) == 0);
78 assert(pthread_mutex_lock(&lwp_mutex) == 0);
84 extern void *malloc();
85 #endif /* AFS_DECOSF_ENV */
87 typedef unsigned char bool;
92 #define MIN(a,b) (((a)>(b)) ? (b) : (a))
96 #define NSIG 8*sizeof(sigset_t)
99 static int SignalSignals(void);
101 /********************************\
103 * Stuff for managing IoRequests *
105 \********************************/
109 /* Pid of process making request (for use in IOMGR_Cancel */
112 /* Descriptor masks for requests */
118 struct TM_Elem timeout;
120 /* Result of select call */
123 struct IoRequest *next; /* for iorFreeList */
127 /********************************\
129 * Stuff for managing signals *
131 \********************************/
133 #define badsig(signo) (((signo) <= 0) || ((signo) >= NSIG))
134 #define mysigmask(signo) (1 << ((signo)-1))
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. */
141 static struct sigaction oldActions[NSIG]; /* the old signal vectors */
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
148 /* software 'signals' */
150 static void *(*sigProc[NSOFTSIG])(void *);
151 static void *sigRock[NSOFTSIG];
154 static struct IoRequest *iorFreeList = 0;
156 static struct TM_Elem *Requests; /* List of requests */
157 static struct timeval iomgr_timeout; /* global so signal handler can zap it */
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,
165 static void SignalTimeout(int code, struct timeval *timeout);
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
172 struct IOMGR_fd_set {
173 struct IOMGR_fd_set *next;
174 } *iomgrFreeFDSets = (struct IOMGR_fd_set*)0;
177 * Return fd_set to the free list.
179 void IOMGR_FreeFDSet(fd_set *s)
181 struct IOMGR_fd_set *t = (struct IOMGR_fd_set *)s;
183 t->next = iomgrFreeFDSets;
188 * returns a zeroed fd_set or null if could not malloc one.
190 fd_set *IOMGR_AllocFDSet(void)
192 struct IOMGR_fd_set *t;
193 if (iomgrFreeFDSets) {
195 iomgrFreeFDSets = iomgrFreeFDSets->next;
198 t = (struct IOMGR_fd_set *)malloc(sizeof(fd_set));
208 #define FreeRequest(x) ((x)->next = iorFreeList, iorFreeList = (x))
210 static struct IoRequest *NewRequest(void)
212 struct IoRequest *request;
214 if ((request=iorFreeList))
215 iorFreeList = (struct IoRequest *) (request->next);
216 else request = (struct IoRequest *) malloc(sizeof(struct IoRequest));
218 memset((char*)request, 0, sizeof(struct IoRequest));
222 #define Purge(list) FOR_ALL_ELTS(req, list, { free(req->BackPointer); })
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.
228 * For each routine, nfds is the highest bit set in either fd_set, starting
232 #define FD_N_ZERO(A, x) FD_ZERO(x)
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))
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
243 #define FDS_BITS fds_bits
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).
250 static int FDSetCmp(int nfds, fd_set *fd_set1, fd_set *fd_set2)
254 if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
258 if (fd_set1->fd_count == 0 || fd_set2->fd_count == 0)
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])
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])
280 /* FDSetSet - set bits from fd_set2 into fd_set1
282 static void FDSetSet(int nfds, fd_set *fd_set1, fd_set *fd_set2)
289 if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
293 if (fd_set2->fd_count==0)
296 for (i=0; i<fd_set2->fd_count; i++)
297 FD_SET(fd_set2->fd_array[i], fd_set1);
302 for (i = 0, n = INTS_PER_FDS(nfds); i < n; i++) {
303 fd_set1->FDS_BITS[i] |= fd_set2->FDS_BITS[i];
308 /* FDSetAnd - fd_set1 <- fd_set1 & fd_set2.
311 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
316 if (fd_set1 == NULL || fd_set1->fd_count == 0)
319 if (fd_set2 == NULL || fd_set2->fd_count == 0) {
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);
332 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
336 if (nfds == 0 || fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
339 n = INTS_PER_FDS(nfds);
340 for (i=0; i<n; i++) {
341 fd_set1->FDS_BITS[i] &= fd_set2->FDS_BITS[i];
346 /* FDSetEmpty - return true if fd_set is empty
348 static int FDSetEmpty(int nfds, fd_set *fds)
356 if (fds == (fd_set*)0)
360 if (fds->fd_count == 0)
365 n = INTS_PER_FDS(nfds);
367 for (i=n-1; i>=0; i--) {
368 if (fds->FDS_BITS[i])
379 /* The IOMGR process */
382 * Important invariant: process->iomgrRequest is null iff request not in timer
384 * also, request->pid is valid while request is in queue,
385 * also, don't signal selector while request in queue, since selector frees
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;
393 static void *IOMGR(void *dummy)
397 struct TM_Elem *earliest;
398 struct timeval timeout, junk;
401 FD_ZERO(&IOMGR_readfds);
402 FD_ZERO(&IOMGR_writefds);
403 FD_ZERO(&IOMGR_exceptfds);
406 /* Wake up anyone who has expired or who has received a
407 Unix signal between executions. Keep going until we
410 woke_someone = FALSE;
411 /* Wake up anyone waiting on signals. */
412 /* Note: SignalSignals() may yield! */
413 if (anySigsDelivered && SignalSignals ())
415 FT_GetTimeOfDay(&junk, 0); /* force accurate time check */
418 register struct IoRequest *req;
419 struct TM_Elem *expired;
420 expired = TM_GetExpired(Requests);
421 if (expired == NULL) break;
423 req = (struct IoRequest *) expired -> BackPointer;
425 if (lwp_debug != 0) puts("[Polling SELECT]");
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);
432 req->result = 0; /* no fds ready */
433 TM_Remove(Requests, &req->timeout);
435 req -> timeout.Next = (struct TM_Elem *) 2;
436 req -> timeout.Prev = (struct TM_Elem *) 2;
438 LWP_QSignal(req->pid);
439 req->pid->iomgrRequest = 0;
442 if (woke_someone) LWP_DispatchProcess();
443 } while (woke_someone);
445 /* Collect requests & update times */
446 FD_ZERO(&IOMGR_readfds);
447 FD_ZERO(&IOMGR_writefds);
448 FD_ZERO(&IOMGR_exceptfds);
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;
460 earliest = TM_GetEarliest(Requests);
461 if (earliest != NULL) {
462 timeout = earliest -> TimeLeft;
467 if (lwp_debug != 0) {
470 printf("[Read Select:");
471 if (IOMGR_readfds.fd_count == 0)
474 for (idbg=0; idbg<IOMGR_readfds.fd_count; idbg++)
475 printf(" %d", IOMGR_readfds.fd_array[idbg]);
478 printf("[Write Select:");
479 if (IOMGR_writefds.fd_count == 0)
482 for (idbg=0; idbg<IOMGR_writefds.fd_count; idbg++)
483 printf(" %d", IOMGR_writefds.fd_array[idbg]);
486 printf("[Except Select:");
487 if (IOMGR_exceptfds.fd_count == 0)
490 for (idbg=0; idbg<IOMGR_exceptfds.fd_count; idbg++)
491 printf(" %d", IOMGR_exceptfds.fd_array[idbg]);
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)
503 printf("<%d, %d>)]\n", timeout.tv_sec, timeout.tv_usec);
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;
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;
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.
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. */
534 if (IOMGR_readfds.fd_count == 0 && IOMGR_writefds.fd_count == 0
535 && IOMGR_exceptfds.fd_count == 0) {
538 if (iomgr_timeout.tv_sec || iomgr_timeout.tv_usec) {
539 stime = iomgr_timeout.tv_sec * 1000
540 + iomgr_timeout.tv_usec/1000;
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,
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;
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;
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
576 if (errno==EINVAL && anySigsDelivered)
578 #endif /* AFS_SUN5_ENV */
580 if ((errno != EINTR) && e) {
583 for(i=0; i<FD_SETSIZE; i++) {
584 if (fcntl(i, F_GETFD, 0) < 0 && errno == EBADF)
585 FD_SET(i, &openMask);
593 /* See what happened */
595 /* Action -- wake up everyone involved */
596 SignalIO(IOMGR_nfds, &IOMGR_readfds, &IOMGR_writefds,
597 &IOMGR_exceptfds, code);
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. */
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).
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 */
615 #endif /* AFS_NT40_ENV */
616 FT_GetTimeOfDay(&junk, 0);
617 SignalTimeout(code, &timeout);
620 LWP_DispatchProcess();
622 return (void *)-1; /* keeps compilers happy. */
625 /************************\
627 * Signalling routines *
629 \************************/
631 static void SignalIO(int fds, fd_set *readfds, fd_set *writefds,
632 fd_set *exceptfds, int code)
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;
656 static void SignalTimeout(int code, struct timeval *timeout)
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;
673 /*****************************************************\
675 * Signal handling routine (not to be confused with *
676 * signalling routines, above). *
678 \*****************************************************/
679 static void SigHandler (int signo)
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;
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)
696 register void *(*p)(void *);
699 anySigsDelivered = FALSE;
701 /* handle software signals */
702 stackSize = (AFS_LWP_MINSTACKSIZE < lwp_MaxStackSeen? lwp_MaxStackSeen : AFS_LWP_MINSTACKSIZE);
703 for (i=0; i < NSOFTSIG; i++) {
705 if ((p=sigProc[i])) /* This yields!!! */
706 LWP_CreateProcess2(p, stackSize, LWP_NORMAL_PRIORITY,
707 sigRock[i], "SignalHandler", &pid);
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]);
721 /***************************\
723 * User-callable routines *
725 \***************************/
728 /* Keep IOMGR process id */
729 static PROCESS IOMGR_Id = NULL;
731 int IOMGR_SoftSig(void *(*aproc)(void *), void *arock)
734 for (i=0;i<NSOFTSIG;i++) {
735 if (sigProc[i] == 0) {
739 anySigsDelivered = TRUE;
740 iomgr_timeout.tv_sec = 0;
741 iomgr_timeout.tv_usec = 0;
749 int IOMGR_Initialize(void)
753 /* If lready initialized, just return */
754 if (IOMGR_Id != NULL) return LWP_SUCCESS;
756 /* Init LWP if someone hasn't yet. */
757 if (LWP_InitializeProcessSupport (LWP_NORMAL_PRIORITY, &pid) != LWP_SUCCESS)
760 /* Initialize request lists */
761 if (TM_Init(&Requests) < 0) return -1;
763 /* Initialize signal handling stuff. */
765 anySigsDelivered = TRUE; /* A soft signal may have happened before
766 IOMGR_Initialize: so force a check for signals regardless */
768 return LWP_CreateProcess(IOMGR, AFS_LWP_MINSTACKSIZE, 0, (void *) 0,
769 "IO MANAGER", &IOMGR_Id);
772 int IOMGR_Finalize(void)
778 status = LWP_DestroyProcess(IOMGR_Id);
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;
791 FT_GetTimeOfDay(&tv, 0); /* force accurate time check */
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;
800 if (lwp_debug != 0) puts("[Polling SELECT]");
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);
807 req->result = 0; /* no fds ready */
808 TM_Remove(Requests, &req->timeout);
810 req -> timeout.Next = (struct TM_Elem *) 2;
811 req -> timeout.Prev = (struct TM_Elem *) 2;
813 LWP_QSignal(req->pid);
814 req->pid->iomgrRequest = 0;
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");
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);
842 if (readfds->fd_count == 0 && writefds->fd_count == 0
843 && exceptfds->fd_count == 0)
845 code = select(fds, readfds, writefds, exceptfds, &tv);
847 SignalIO(fds, readfds, writefds, exceptfds, code);
850 if (readfds) IOMGR_FreeFDSet(readfds);
851 if (writefds) IOMGR_FreeFDSet(writefds);
852 if (exceptfds) IOMGR_FreeFDSet(exceptfds);
855 LWP_DispatchProcess(); /* make sure others run */
856 LWP_DispatchProcess();
860 int IOMGR_Select(int fds, fd_set *readfds, fd_set *writefds,
861 fd_set *exceptfds, struct timeval *timeout)
863 register struct IoRequest *request;
867 if(fds > FD_SETSIZE) {
868 fprintf(stderr, "IOMGR_Select: fds=%d, more than max %d\n",
875 /* See if polling request. If so, handle right here */
876 if (timeout != NULL) {
877 if (timeout->tv_sec == 0 && timeout->tv_usec == 0) {
880 if (lwp_debug != 0) puts("[Polling SELECT]");
882 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV) || defined(AFS_NT40_ENV)
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)
888 * For SGI and SVR4 - poll & select can return EAGAIN ...
891 * this is basically for SGI, but I believe stock SVR4 (Solaris?)
892 * can also get this error return
894 if (code < 0 && errno == EAGAIN)
898 if (code == SOCKET_ERROR) {
899 if (WSAGetLastError() == WSAEINPROGRESS)
905 return (code > 1 ? 1 : code);
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;
919 if (timeout == NULL) {
920 request -> timeout.TotalTime.tv_sec = -1;
921 request -> timeout.TotalTime.tv_usec = -1;
923 request -> timeout.TotalTime = *timeout;
924 /* check for bad request */
925 if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec > 999999) {
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;
936 request -> timeout.BackPointer = (char *) request;
938 /* Insert my PID in case of IOMGR_Cancel */
939 request -> pid = LWP_ActiveProcess;
940 LWP_ActiveProcess -> iomgrRequest = request;
943 request -> timeout.Next = (struct TM_Elem *) 1;
944 request -> timeout.Prev = (struct TM_Elem *) 1;
946 TM_Insert(Requests, &request->timeout);
948 /* Wait for action */
951 /* Update parameters & return */
952 result = request -> result;
954 FreeRequest(request);
955 return (result > 1 ? 1 : result);
958 int IOMGR_Cancel(PROCESS pid)
960 register struct IoRequest *request;
962 if ((request = pid->iomgrRequest) == 0) return -1; /* Pid not found */
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);
969 request -> result = -2;
970 TM_Remove(Requests, &request->timeout);
972 request -> timeout.Next = (struct TM_Elem *) 5;
973 request -> timeout.Prev = (struct TM_Elem *) 5;
975 LWP_QSignal(request->pid);
976 pid->iomgrRequest = 0;
982 /* Cause delivery of signal signo to result in a LWP_SignalProcess of
984 int IOMGR_Signal (int signo, char *event)
991 return LWP_EBADEVENT;
992 sa.sa_handler = SigHandler;
993 sigfillset(&sa.sa_mask); /* mask all signals */
995 sigsHandled |= mysigmask(signo);
996 sigEvents[signo] = event;
997 sigDelivered[signo] = FALSE;
998 if (sigaction (signo, &sa, &oldActions[signo]) == -1)
1003 /* Stop handling occurrences of signo. */
1004 int IOMGR_CancelSignal (int signo)
1006 if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
1008 sigaction (signo, &oldActions[signo], NULL);
1009 sigsHandled &= ~mysigmask(signo);
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)
1016 struct timeval timeout;
1018 timeout.tv_sec = seconds;
1019 timeout.tv_usec = 0;
1020 IOMGR_Select(0, 0, 0, 0, &timeout);
1022 #endif /* USE_PTHREADS */