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
12 /*******************************************************************\
14 * Information Technology Center *
15 * Carnegie-Mellon University *
19 \*******************************************************************/
23 IO Manager routines & server process for VICE server.
26 #include <afs/param.h>
31 extern void lwp_abort(void);
33 #include <unistd.h> /* select() prototype */
34 #include <sys/types.h> /* fd_set on older platforms */
35 #include <sys/time.h> /* struct timeval, select() prototype */
37 # include <sys/select.h> /* fd_set on newer platforms */
40 #endif /* AFS_NT40_ENV */
49 #include "dosdefs95.h"
50 #include "netbios95.h"
51 #include <sys/socket.h>
52 #include <sys/farptr.h>
56 int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY;
57 #endif /* AFS_DJGPP_ENV */
59 #if defined(USE_PTHREADS) || defined(USE_SOLARIS_THREADS)
61 void IOMGR_Initialize() /* noop */
64 void IOMGR_Sleep (seconds)
71 assert(pthread_mutex_unlock(&lwp_mutex) == 0);
72 assert(pthread_delay_np(&itv) == 0);
73 assert(pthread_mutex_lock(&lwp_mutex) == 0);
79 extern void *malloc();
80 #endif /* AFS_DECOSF_ENV */
82 typedef unsigned char bool;
87 #define MIN(a,b) ((a)>(b)) ? b : a
91 #define NSIG 8*sizeof(sigset_t)
94 static int SignalSignals();
96 /********************************\
98 * Stuff for managing IoRequests *
100 \********************************/
104 /* Pid of process making request (for use in IOMGR_Cancel */
107 /* Descriptor masks for requests */
113 struct TM_Elem timeout;
115 /* Result of select call */
121 #endif /* AFS_DJGPP_ENV */
125 /********************************\
127 * Stuff for managing signals *
129 \********************************/
131 #define badsig(signo) (((signo) <= 0) || ((signo) >= NSIG))
132 #define mysigmask(signo) (1 << ((signo)-1))
135 fd_set openMask; /* mask of open files on an IOMGR abort */
136 static afs_int32 sigsHandled; /* sigmask(signo) is on if we handle signo */
137 static int anySigsDelivered; /* true if any have been delivered. */
139 static struct sigaction oldActions[NSIG]; /* the old signal vectors */
141 static char *sigEvents[NSIG]; /* the event to do an LWP signal on */
142 static int sigDelivered[NSIG]; /* True for signals delivered so far.
143 This is an int array to make sure
144 there are no conflicts when trying
146 /* software 'signals' */
148 static int (*sigProc[NSOFTSIG])();
149 static char *sigRock[NSOFTSIG];
152 static struct IoRequest *iorFreeList = 0;
154 static struct TM_Elem *Requests; /* List of requests */
155 static struct timeval iomgr_timeout; /* global so signal handler can zap it */
157 /* stuff for debugging */
158 static int iomgr_errno;
159 static struct timeval iomgr_badtv;
160 static PROCESS iomgr_badpid;
161 static void SignalIO(int fds, fd_set *rfds, fd_set *wfds, fd_set *efs,
165 /* handle Netbios NCB completion */
167 int anyNCBComplete = FALSE;
168 int handler_seg, handler_off; /* seg:off of NCB completion handler */
169 static __dpmi_regs callback_regs;
170 static _go32_dpmi_seginfo callback_info;
171 #endif /* AFS_DJGPP_ENV */
173 /* fd_set pool managment.
174 * Use the pool instead of creating fd_set's on the stack. fd_set's can be
175 * 2K in size, so making three could put 6K in the limited space of an LWP
178 struct IOMGR_fd_set {
179 struct IOMGR_fd_set *next;
180 } *iomgrFreeFDSets = (struct IOMGR_fd_set*)0;
183 * Return fd_set to the free list.
185 void IOMGR_FreeFDSet(fd_set *s)
187 struct IOMGR_fd_set *t = (struct IOMGR_fd_set *)s;
189 t->next = iomgrFreeFDSets;
194 * returns a zeroed fd_set or null if could not malloc one.
196 fd_set *IOMGR_AllocFDSet(void)
198 struct IOMGR_fd_set *t;
199 if (iomgrFreeFDSets) {
201 iomgrFreeFDSets = iomgrFreeFDSets->next;
204 t = (struct IOMGR_fd_set *)malloc(sizeof(fd_set));
214 #define FreeRequest(x) ((x)->result = (long) iorFreeList, iorFreeList = (x))
216 static struct IoRequest *NewRequest()
218 struct IoRequest *request;
220 if (request=iorFreeList)
221 iorFreeList = (struct IoRequest *) (request->result);
222 else request = (struct IoRequest *) malloc(sizeof(struct IoRequest));
224 memset((char*)request, 0, sizeof(struct IoRequest));
228 #define Purge(list) FOR_ALL_ELTS(req, list, { free(req->BackPointer); })
231 /* FD_SET support routines. All assume the fd_set size is a multiple of an int
232 * so we can at least do logical operations on ints instead of chars.
234 * For each routine, nfds is the highest bit set in either fd_set, starting
238 #define FD_N_ZERO(A, x) FD_ZERO(x)
240 #define FDS_P_POS (sizeof(int)*8)
241 #define INTS_PER_FDS(x) (((x)+(FDS_P_POS-1)) / FDS_P_POS)
242 #define FD_N_ZERO(nfds, x) bzero((char*)(x), (INTS_PER_FDS(nfds))*sizeof(int))
245 #if defined(AFS_LINUX22_ENV) && (__GLIBC_MINOR__ > 0)
246 /* Build for both glibc 2.0.x and 2.1.x */
247 #define FDS_BITS __fds_bits
249 #define FDS_BITS fds_bits
252 /* FDSetCmp - returns 1 if any bits in fd_set1 are also set in fd_set2.
253 * If nfds is 0, or one of the fd_sets is null return 0 (since there is no bit
254 * set in both fd_sets).
256 static int FDSetCmp(int nfds, fd_set *fd_set1, fd_set *fd_set2)
260 if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
264 if (fd_set1->fd_count == 0 || fd_set2->fd_count == 0)
267 for (i=0; i<fd_set1->fd_count; i++) {
268 for (j=0; j<fd_set2->fd_count; j++) {
269 if (fd_set1->fd_array[i] == fd_set2->fd_array[j])
277 j = INTS_PER_FDS(nfds);
278 for (i=0; i<j; i++) {
279 if (fd_set1->FDS_BITS[i] & fd_set2->FDS_BITS[i])
286 /* FDSetSet - set bits from fd_set2 into fd_set1
288 static void FDSetSet(int nfds, fd_set *fd_set1, fd_set *fd_set2)
292 if (fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
296 if (fd_set2->fd_count==0)
299 for (i=0; i<fd_set2->fd_count; i++)
300 FD_SET(fd_set2->fd_array[i], fd_set1);
305 n = INTS_PER_FDS(nfds);
306 for (i=0; i<n; i++) {
307 fd_set1->FDS_BITS[i] |= fd_set2->FDS_BITS[i];
312 /* FDSetAnd - fd_set1 <- fd_set1 & fd_set2.
315 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
320 if (fd_set1 == NULL || fd_set1->fd_count == 0)
323 if (fd_set2 == NULL || fd_set2->fd_count == 0) {
328 for (i=0; i<fd_set2->fd_count; i++) {
329 if (FD_ISSET(fd_set2->fd_array[i], fd_set1))
330 FD_SET(fd_set2->fd_array[i], &tmpset);
336 static void FDSetAnd(int nfds, fd_set *fd_set1, fd_set *fd_set2)
340 if (nfds == 0 || fd_set1 == (fd_set*)0 || fd_set2 == (fd_set*)0)
343 n = INTS_PER_FDS(nfds);
344 for (i=0; i<n; i++) {
345 fd_set1->FDS_BITS[i] &= fd_set2->FDS_BITS[i];
350 /* FDSetEmpty - return true if fd_set is empty
352 static int FDSetEmpty(int nfds, fd_set *fds)
360 if (fds == (fd_set*)0)
364 if (fds->fd_count == 0)
369 n = INTS_PER_FDS(nfds);
371 for (i=n-1; i>=0; i--) {
372 if (fds->FDS_BITS[i])
383 /* The IOMGR process */
386 * Important invariant: process->iomgrRequest is null iff request not in timer
388 * also, request->pid is valid while request is in queue,
389 * also, don't signal selector while request in queue, since selector frees
393 /* These are not declared in IOMGR so that they don't use up 6K of stack. */
394 static fd_set IOMGR_readfds, IOMGR_writefds, IOMGR_exceptfds;
395 static int IOMGR_nfds = 0;
397 static int IOMGR(void *dummy)
401 struct TM_Elem *earliest;
402 struct timeval timeout, junk;
408 FD_ZERO(&IOMGR_readfds);
409 FD_ZERO(&IOMGR_writefds);
410 FD_ZERO(&IOMGR_exceptfds);
413 /* Wake up anyone who has expired or who has received a
414 Unix signal between executions. Keep going until we
417 woke_someone = FALSE;
418 /* Wake up anyone waiting on signals. */
419 /* Note: SignalSignals() may yield! */
420 if (anySigsDelivered && SignalSignals ())
422 #ifndef AFS_DJGPP_ENV
423 FT_GetTimeOfDay(&junk, 0); /* force accurate time check */
427 register struct IoRequest *req;
428 struct TM_Elem *expired;
429 expired = TM_GetExpired(Requests);
430 if (expired == NULL) break;
432 req = (struct IoRequest *) expired -> BackPointer;
434 if (lwp_debug != 0) puts("[Polling SELECT]");
437 if (req->readfds) FD_N_ZERO(req->nfds, req->readfds);
438 if (req->writefds) FD_N_ZERO(req->nfds, req->writefds);
439 if (req->exceptfds) FD_N_ZERO(req->nfds, req->exceptfds);
441 req->result = 0; /* no fds ready */
442 TM_Remove(Requests, &req->timeout);
444 req -> timeout.Next = (struct TM_Elem *) 2;
445 req -> timeout.Prev = (struct TM_Elem *) 2;
447 LWP_QSignal(req->pid);
448 req->pid->iomgrRequest = 0;
452 if (IOMGR_CheckNCB()) /* check for completed netbios requests */
454 #endif /* AFS_DJGPP_ENV */
456 if (woke_someone) LWP_DispatchProcess();
457 } while (woke_someone);
459 /* Collect requests & update times */
460 FD_ZERO(&IOMGR_readfds);
461 FD_ZERO(&IOMGR_writefds);
462 FD_ZERO(&IOMGR_exceptfds);
465 FOR_ALL_ELTS(r, Requests, {
466 register struct IoRequest *req;
467 req = (struct IoRequest *) r -> BackPointer;
468 FDSetSet(req->nfds, &IOMGR_readfds, req->readfds);
469 FDSetSet(req->nfds, &IOMGR_writefds, req->writefds);
470 FDSetSet(req->nfds, &IOMGR_exceptfds, req->exceptfds);
471 if (req->nfds > IOMGR_nfds)
472 IOMGR_nfds = req->nfds;
474 earliest = TM_GetEarliest(Requests);
475 if (earliest != NULL) {
476 timeout = earliest -> TimeLeft;
481 if (lwp_debug != 0) {
484 printf("[Read Select:");
485 if (IOMGR_readfds.fd_count == 0)
488 for (idbg=0; idbg<IOMGR_readfds.fd_count; idbg++)
489 printf(" %d", IOMGR_readfds.fd_array[idbg]);
492 printf("[Write Select:");
493 if (IOMGR_writefds.fd_count == 0)
496 for (idbg=0; idbg<IOMGR_writefds.fd_count; idbg++)
497 printf(" %d", IOMGR_writefds.fd_array[idbg]);
500 printf("[Except Select:");
501 if (IOMGR_exceptfds.fd_count == 0)
504 for (idbg=0; idbg<IOMGR_exceptfds.fd_count; idbg++)
505 printf(" %d", IOMGR_exceptfds.fd_array[idbg]);
509 /* Only prints first 32. */
510 printf("[select(%d, 0x%x, 0x%x, 0x%x, ", IOMGR_nfds,
511 *(int*)&IOMGR_readfds, *(int*)&IOMGR_writefds,
512 *(int*)&IOMGR_exceptfds);
513 #endif /* AFS_NT40_ENV */
514 if (timeout.tv_sec == -1 && timeout.tv_usec == -1)
517 printf("<%d, %d>)]\n", timeout.tv_sec, timeout.tv_usec);
520 iomgr_timeout = timeout;
521 if (timeout.tv_sec == -1 && timeout.tv_usec == -1) {
522 /* infinite, sort of */
523 iomgr_timeout.tv_sec = 100000000;
524 iomgr_timeout.tv_usec = 0;
527 /* On NT, signals don't interrupt a select call. So this can potentially
528 * lead to long wait times before a signal is honored. To avoid this we
529 * dont do select() for longer than IOMGR_MAXWAITTIME (5 secs) */
530 if (iomgr_timeout.tv_sec > (IOMGR_MAXWAITTIME - 1)) {
531 iomgr_timeout.tv_sec = IOMGR_MAXWAITTIME;
532 iomgr_timeout.tv_usec = 0;
537 /* We do this also for the DOS-box Win95 client, since
538 NCB calls don't interrupt a select, but we want to catch them
539 in a reasonable amount of time (say, half a second). */
540 iomgr_timeout.tv_sec = 0;
541 iomgr_timeout.tv_usec = IOMGR_WIN95WAITTIME;
544 /* Check one last time for a signal delivery. If one comes after
545 this, the signal handler will set iomgr_timeout to zero, causing
546 the select to return immediately. The timer package won't return
547 a zero timeval because all of those guys were handled above.
549 I'm assuming that the kernel masks signals while it's picking up
550 the parameters to select. This may a bad assumption. -DN */
551 if (anySigsDelivered)
552 continue; /* go to the top and handle them. */
555 if (IOMGR_CheckNCB()) /* check for completed netbios requests */
556 LWP_DispatchProcess();
557 #endif /* AFS_DJGPP_ENV */
560 if (IOMGR_readfds.fd_count == 0 && IOMGR_writefds.fd_count == 0
561 && IOMGR_exceptfds.fd_count == 0) {
564 if (iomgr_timeout.tv_sec || iomgr_timeout.tv_usec) {
565 stime = iomgr_timeout.tv_sec * 1000
566 + iomgr_timeout.tv_usec/1000;
574 { /* select runs much faster if 0's are passed instead of &0s */
575 code = select(IOMGR_nfds,
576 (FDSetEmpty(IOMGR_nfds, &IOMGR_readfds)) ?
577 (fd_set*)0 : &IOMGR_readfds,
578 (FDSetEmpty(IOMGR_nfds, &IOMGR_writefds)) ?
579 (fd_set*)0 : &IOMGR_writefds,
580 (FDSetEmpty(IOMGR_nfds, &IOMGR_exceptfds)) ?
581 (fd_set*)0 : &IOMGR_exceptfds,
588 #if defined(AFS_SUN_ENV)
589 /* Tape drives on Sun boxes do not support select and return ENXIO */
590 if (errno == ENXIO) e=0, code=1;
592 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV)
593 /* For SGI and SVR4 - poll & select can return EAGAIN ... */
594 if (errno == EAGAIN) e=0;
596 #if defined(AFS_SUN5_ENV)
597 /* On sun4x_55, select doesn't block signal. It could be
598 interupted by a signal that changes iomgr_timeout, and
599 then select returns with EINVAL. In this case, we need
602 if (errno==EINVAL && anySigsDelivered)
604 #endif /* AFS_SUN5_ENV */
606 if ((errno != EINTR) && e) {
609 for(i=0; i<FD_SETSIZE; i++) {
610 if (fcntl(i, F_GETFD, 0) < 0 && errno == EBADF)
611 FD_SET(i, &openMask);
619 /* See what happened */
621 /* Action -- wake up everyone involved */
622 SignalIO(IOMGR_nfds, &IOMGR_readfds, &IOMGR_writefds,
623 &IOMGR_exceptfds, code);
626 && (iomgr_timeout.tv_sec != 0 || iomgr_timeout.tv_usec != 0)) {
627 /* Real timeout only if signal handler hasn't set
628 iomgr_timeout to zero. */
631 /* On NT, real timeout only if above and if iomgr_timeout
632 * interval is equal to timeout interval (i.e., not adjusted
633 * to check for pseudo-signals).
635 if (iomgr_timeout.tv_sec != timeout.tv_sec ||
636 iomgr_timeout.tv_usec != timeout.tv_usec) {
637 /* signal check interval timed out; not real timeout */
640 #endif /* AFS_NT40_ENV */
641 #ifndef AFS_DJGPP_ENV
642 FT_GetTimeOfDay(&junk, 0);
644 SignalTimeout(code, &timeout);
648 #endif /* AFS_DJGPP_ENV */
650 LWP_DispatchProcess();
652 return -1; /* keeps compilers happy. */
655 /************************\
657 * Signalling routines *
659 \************************/
661 static void SignalIO(int fds, fd_set *readfds, fd_set *writefds,
662 fd_set *exceptfds, int code)
665 /* Look at everyone who's bit mask was affected */
666 FOR_ALL_ELTS(r, Requests, {
667 register struct IoRequest *req;
668 register PROCESS pid;
669 req = (struct IoRequest *) r -> BackPointer;
670 nfds = MIN(fds, req->nfds);
671 if (FDSetCmp(nfds, req->readfds, readfds) ||
672 FDSetCmp(nfds, req->writefds, writefds) ||
673 FDSetCmp(nfds, req->exceptfds, exceptfds)) {
674 /* put ready fd's into request. */
675 FDSetAnd(nfds, req->readfds, readfds);
676 FDSetAnd(nfds, req->writefds, writefds);
677 FDSetAnd(nfds, req->exceptfds, exceptfds);
678 req -> result = code;
679 TM_Remove(Requests, &req->timeout);
680 LWP_QSignal(pid=req->pid);
681 pid->iomgrRequest = 0;
686 static void SignalTimeout(int code, struct timeval *timeout)
688 /* Find everyone who has specified timeout */
689 FOR_ALL_ELTS(r, Requests, {
690 register struct IoRequest *req;
691 register PROCESS pid;
692 req = (struct IoRequest *) r -> BackPointer;
693 if (TM_eql(&r->TimeLeft, timeout)) {
694 req -> result = code;
695 TM_Remove(Requests, &req->timeout);
696 LWP_QSignal(pid=req->pid);
697 pid->iomgrRequest = 0;
703 /*****************************************************\
705 * Signal handling routine (not to be confused with *
706 * signalling routines, above). *
708 \*****************************************************/
709 static void SigHandler (signo)
712 if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
713 return; /* can't happen. */
714 sigDelivered[signo] = TRUE;
715 anySigsDelivered = TRUE;
716 /* Make sure that the IOMGR process doesn't pause on the select. */
717 iomgr_timeout.tv_sec = 0;
718 iomgr_timeout.tv_usec = 0;
721 /* Alright, this is the signal signalling routine. It delivers LWP signals
722 to LWPs waiting on Unix signals. NOW ALSO CAN YIELD!! */
723 static int SignalSignals ()
730 anySigsDelivered = FALSE;
732 /* handle software signals */
733 stackSize = (AFS_LWP_MINSTACKSIZE < lwp_MaxStackSeen? lwp_MaxStackSeen : AFS_LWP_MINSTACKSIZE);
734 for (i=0; i < NSOFTSIG; i++) {
736 if (p=sigProc[i]) /* This yields!!! */
737 LWP_CreateProcess2(p, stackSize, LWP_NORMAL_PRIORITY, sigRock[i],
738 "SignalHandler", &pid);
742 for (i = 1; i <= NSIG; ++i) /* forall !badsig(i) */
743 if ((sigsHandled & mysigmask(i)) && sigDelivered[i] == TRUE) {
744 sigDelivered[i] = FALSE;
745 LWP_NoYieldSignal (sigEvents[i]);
752 /***************************\
754 * User-callable routines *
756 \***************************/
759 /* Keep IOMGR process id */
760 static PROCESS IOMGR_Id = NULL;
762 int IOMGR_SoftSig(aproc, arock)
766 for (i=0;i<NSOFTSIG;i++) {
767 if (sigProc[i] == 0) {
771 anySigsDelivered = TRUE;
772 iomgr_timeout.tv_sec = 0;
773 iomgr_timeout.tv_usec = 0;
781 unsigned char allOnes[100];
783 int IOMGR_Initialize(void)
785 extern int TM_Init();
788 /* If lready initialized, just return */
789 if (IOMGR_Id != NULL) return LWP_SUCCESS;
791 /* Init LWP if someone hasn't yet. */
792 if (LWP_InitializeProcessSupport (LWP_NORMAL_PRIORITY, &pid) != LWP_SUCCESS)
795 /* Initialize request lists */
796 if (TM_Init(&Requests) < 0) return -1;
798 /* Initialize signal handling stuff. */
800 anySigsDelivered = TRUE; /* A soft signal may have happened before
801 IOMGR_Initialize: so force a check for signals regardless */
802 memset(allOnes, 0xff, sizeof(allOnes));
805 install_ncb_handler();
806 #endif /* AFS_DJGPP_ENV */
808 return LWP_CreateProcess(IOMGR, AFS_LWP_MINSTACKSIZE, 0, 0, "IO MANAGER",
818 status = LWP_DestroyProcess(IOMGR_Id);
823 /* signal I/O for anyone who is waiting for a FD or a timeout; not too cheap,
824 * since forces select and timeofday check */
825 int IOMGR_Poll(void) {
826 fd_set *readfds, *writefds, *exceptfds;
831 FT_GetTimeOfDay(&tv, 0); /* force accurate time check */
834 register struct IoRequest *req;
835 struct TM_Elem *expired;
836 expired = TM_GetExpired(Requests);
837 if (expired == NULL) break;
838 req = (struct IoRequest *) expired -> BackPointer;
840 if (lwp_debug != 0) puts("[Polling SELECT]");
843 if (req->readfds) FD_N_ZERO(req->nfds, req->readfds);
844 if (req->writefds) FD_N_ZERO(req->nfds, req->writefds);
845 if (req->exceptfds) FD_N_ZERO(req->nfds, req->exceptfds);
847 req->result = 0; /* no fds ready */
848 TM_Remove(Requests, &req->timeout);
850 req -> timeout.Next = (struct TM_Elem *) 2;
851 req -> timeout.Prev = (struct TM_Elem *) 2;
853 LWP_QSignal(req->pid);
854 req->pid->iomgrRequest = 0;
857 /* Collect requests & update times */
858 readfds = IOMGR_AllocFDSet();
859 writefds = IOMGR_AllocFDSet();
860 exceptfds = IOMGR_AllocFDSet();
861 if (!(readfds && writefds && exceptfds)) {
862 fprintf(stderr, "IOMGR_Poll: Could not malloc space for fd_sets.\n");
868 FOR_ALL_ELTS(r, Requests, {
869 register struct IoRequest *req;
870 req = (struct IoRequest *) r -> BackPointer;
871 FDSetSet(req->nfds, readfds, req->readfds);
872 FDSetSet(req->nfds, writefds, req->writefds);
873 FDSetSet(req->nfds, exceptfds, req->exceptfds);
882 if (readfds->fd_count == 0 && writefds->fd_count == 0
883 && exceptfds->fd_count == 0)
885 code = select(fds, readfds, writefds, exceptfds, &tv);
887 SignalIO(fds, readfds, writefds, exceptfds, code);
890 if (readfds) IOMGR_FreeFDSet(readfds);
891 if (writefds) IOMGR_FreeFDSet(writefds);
892 if (exceptfds) IOMGR_FreeFDSet(exceptfds);
895 LWP_DispatchProcess(); /* make sure others run */
896 LWP_DispatchProcess();
900 int IOMGR_Select(fds, readfds, writefds, exceptfds, timeout)
902 fd_set *readfds, *writefds, *exceptfds;
903 struct timeval *timeout;
905 register struct IoRequest *request;
909 if(fds > FD_SETSIZE) {
910 fprintf(stderr, "IOMGR_Select: fds=%d, more than max %d\n",
917 /* See if polling request. If so, handle right here */
918 if (timeout != NULL) {
919 if (timeout->tv_sec == 0 && timeout->tv_usec == 0) {
922 if (lwp_debug != 0) puts("[Polling SELECT]");
925 code = select(fds, readfds, writefds, exceptfds, timeout);
926 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_AIX32_ENV)
928 * For SGI and SVR4 - poll & select can return EAGAIN ...
931 * this is basically for SGI, but I believe stock SVR4 (Solaris?)
932 * can also get this error return
934 if (code < 0 && errno == EAGAIN)
938 if (code == SOCKET_ERROR) {
939 if (WSAGetLastError() == WSAEINPROGRESS)
945 return (code > 1 ? 1 : code);
949 /* Construct request block & insert */
950 request = NewRequest(); /* zeroes fd_set's */
951 if (readfds && !FDSetEmpty(fds, readfds))
952 request->readfds = readfds;
953 if (writefds && !FDSetEmpty(fds, writefds))
954 request->writefds = writefds;
955 if (exceptfds && !FDSetEmpty(fds, exceptfds))
956 request->exceptfds = exceptfds;
959 if (timeout == NULL) {
960 request -> timeout.TotalTime.tv_sec = -1;
961 request -> timeout.TotalTime.tv_usec = -1;
963 request -> timeout.TotalTime = *timeout;
964 /* check for bad request */
965 if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec > 999999) {
967 iomgr_badtv = *timeout;
968 iomgr_badpid = LWP_ActiveProcess;
969 /* now fixup request */
970 if(request->timeout.TotalTime.tv_sec < 0)
971 request->timeout.TotalTime.tv_sec = 1;
972 request->timeout.TotalTime.tv_usec = 100000;
976 request -> timeout.BackPointer = (char *) request;
978 /* Insert my PID in case of IOMGR_Cancel */
979 request -> pid = LWP_ActiveProcess;
980 LWP_ActiveProcess -> iomgrRequest = request;
983 request -> timeout.Next = (struct TM_Elem *) 1;
984 request -> timeout.Prev = (struct TM_Elem *) 1;
986 TM_Insert(Requests, &request->timeout);
988 /* Wait for action */
991 /* Update parameters & return */
992 result = request -> result;
994 FreeRequest(request);
995 return (result > 1 ? 1 : result);
998 int IOMGR_Cancel(PROCESS pid)
1000 register struct IoRequest *request;
1002 if ((request = pid->iomgrRequest) == 0) return -1; /* Pid not found */
1004 if (request->readfds) FD_N_ZERO(request->nfds, request->readfds);
1005 if (request->writefds) FD_N_ZERO(request->nfds, request->writefds);
1006 if (request->exceptfds) FD_N_ZERO(request->nfds, request->exceptfds);
1009 request -> result = -2;
1010 TM_Remove(Requests, &request->timeout);
1012 request -> timeout.Next = (struct TM_Elem *) 5;
1013 request -> timeout.Prev = (struct TM_Elem *) 5;
1015 LWP_QSignal(request->pid);
1016 pid->iomgrRequest = 0;
1021 #ifndef AFS_NT40_ENV
1022 /* Cause delivery of signal signo to result in a LWP_SignalProcess of
1024 IOMGR_Signal (signo, event)
1028 struct sigaction sa;
1033 return LWP_EBADEVENT;
1034 sa.sa_handler = SigHandler;
1035 sa.sa_mask = *((sigset_t *) allOnes); /* mask all signals */
1037 sigsHandled |= mysigmask(signo);
1038 sigEvents[signo] = event;
1039 sigDelivered[signo] = FALSE;
1040 if (sigaction (signo, &sa, &oldActions[signo]) == -1)
1045 /* Stop handling occurrences of signo. */
1046 IOMGR_CancelSignal (signo)
1049 if (badsig(signo) || (sigsHandled & mysigmask(signo)) == 0)
1051 sigaction (signo, &oldActions[signo], (struct sigaction *)0);
1052 sigsHandled &= ~mysigmask(signo);
1055 #endif /* AFS_NT40_ENV */
1056 /* This routine calls select is a fashion that simulates the standard sleep routine */
1057 void IOMGR_Sleep (int seconds)
1059 #ifndef AFS_DJGPP_ENV
1060 struct timeval timeout;
1062 timeout.tv_sec = seconds;
1063 timeout.tv_usec = 0;
1064 IOMGR_Select(0, 0, 0, 0, &timeout);
1066 struct timeval timeout;
1071 s = socket(AF_INET,SOCK_STREAM,0);
1074 timeout.tv_sec = seconds;
1075 timeout.tv_usec = 0;
1076 IOMGR_Select(1,&set,&empty,&empty,&timeout);
1080 #endif /* USE_PTHREADS */
1083 #ifdef AFS_DJGPP_ENV
1085 /* Netbios code for djgpp port */
1087 int IOMGR_NCBSelect(ncbp, dos_ncb, timeout)
1090 struct timeval *timeout;
1092 struct IoRequest *request;
1095 if (timeout != NULL && timeout->tv_sec == 0 && timeout->tv_usec == 0)
1098 if (ncbp->ncb_event != NULL)
1104 if (get_dos_member_b(NCB, dos_ncb, ncb_cmd_cplt) != 0xff)
1113 /* Construct request block & insert */
1114 request = NewRequest();
1115 request->ncbp = ncbp;
1116 request->dos_ncb = dos_ncb;
1118 if (timeout == NULL)
1120 request->timeout.TotalTime.tv_sec = -1;
1121 request->timeout.TotalTime.tv_usec = -1;
1125 request -> timeout.TotalTime = *timeout;
1126 /* check for bad request */
1127 if (timeout->tv_sec < 0 || timeout->tv_usec < 0 || timeout->tv_usec > 999999)
1130 iomgr_badtv = *timeout;
1131 iomgr_badpid = LWP_ActiveProcess;
1132 /* now fixup request */
1133 if(request->timeout.TotalTime.tv_sec < 0)
1134 request->timeout.TotalTime.tv_sec = 1;
1135 request->timeout.TotalTime.tv_usec = 100000;
1139 request->timeout.BackPointer = (char *)request;
1141 /* Insert my PID in case of IOMGR_Cancel */
1142 request -> pid = LWP_ActiveProcess;
1143 LWP_ActiveProcess -> iomgrRequest = request;
1146 request -> timeout.Next = (struct TM_Elem *) 1;
1147 request -> timeout.Prev = (struct TM_Elem *) 1;
1149 TM_Insert(Requests, &request->timeout);
1151 if (ncbp->ncb_event != NULL)
1153 /* since we were given an event, we can return immediately and just
1154 signal the event once the request completes. */
1159 /* Wait for action */
1163 /* Update parameters & return */
1164 result = request -> result;
1166 FreeRequest(request);
1167 return (result > 1 ? 1 : result);
1171 int IOMGR_CheckNCB()
1173 int woke_someone = FALSE;
1177 anyNCBComplete = FALSE;
1178 FOR_ALL_ELTS(r, Requests, {
1179 register struct IoRequest *req;
1180 req = (struct IoRequest *) r -> BackPointer;
1182 if (req->dos_ncb && get_dos_member_b(NCB, req->dos_ncb, ncb_cmd_cplt) != 0xff)
1184 /* this NCB has completed */
1185 TM_Remove(Requests, &req->timeout);
1187 /* copy out NCB from DOS to virtual space */
1188 dosmemget(req->dos_ncb, sizeof(NCB), (char *) req->ncbp);
1190 if (ev = req->ncbp->ncb_event)
1196 woke_someone = TRUE;
1197 LWP_QSignal(pid=req->pid);
1198 pid->iomgrRequest = 0;
1202 return woke_someone;
1205 int ncb_handler(__dpmi_regs *r)
1207 anyNCBComplete = TRUE; /* NCB completed */
1208 /* Make sure that the IOMGR process doesn't pause on the select. */
1209 iomgr_timeout.tv_sec = 0;
1210 iomgr_timeout.tv_usec = 0;
1214 int install_ncb_handler()
1216 callback_info.pm_offset = (long) ncb_handler;
1217 if (_go32_dpmi_allocate_real_mode_callback_retf(&callback_info,
1220 fprintf(stderr, "error, allocate_real_mode_callback_retf failed\n");
1224 handler_seg = callback_info.rm_segment;
1225 handler_off = callback_info.rm_offset;
1227 /*printf("NCB handler_seg=0x%x, off=0x%x\n", handler_seg, handler_off);*/
1229 #endif /* AFS_DJGPP_ENV */