3 Copyright 1996, 1997, 1998, 1999, 2000 Cygnus Solutions.
5 Written by Christopher Faylor of Cygnus Solutions
8 This file is part of Cygwin.
10 This software is a copyrighted work licensed under the terms of the
11 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
15 * The following line means that the BSD socket
16 * definitions for fd_set, FD_ISSET etc. are used in this
20 #define __INSIDE_CYGWIN_NET__
24 #include <sys/socket.h>
38 * All these defines below should be in sys/types.h
39 * but because of the includes above, they may not have
40 * been included. We create special UNIX_xxxx versions here.
44 #define NBBY 8 /* number of bits in a byte */
48 * Select uses bit masks of file descriptors in longs.
49 * These macros manipulate such bit fields (the filesystem macros use chars).
50 * FD_SETSIZE may be defined by the user, but the default here
51 * should be >= NOFILE (param.h).
55 #define UNIX_NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */
57 #define unix_howmany(x,y) (((x)+((y)-1))/(y))
60 #define unix_fd_set fd_set
62 #define NULL_fd_set ((fd_set *)NULL)
63 #define sizeof_fd_set(n) \
64 ((unsigned) (NULL_fd_set->fds_bits + unix_howmany((n), UNIX_NFDBITS)))
65 #define UNIX_FD_SET(n, p) \
66 ((p)->fds_bits[(n)/UNIX_NFDBITS] |= (1L << ((n) % UNIX_NFDBITS)))
67 #define UNIX_FD_CLR(n, p) \
68 ((p)->fds_bits[(n)/UNIX_NFDBITS] &= ~(1L << ((n) % UNIX_NFDBITS)))
69 #define UNIX_FD_ISSET(n, p) \
70 ((p)->fds_bits[(n)/UNIX_NFDBITS] & (1L << ((n) % UNIX_NFDBITS)))
71 #define UNIX_FD_ZERO(p, n) \
72 bzero ((caddr_t)(p), sizeof_fd_set ((n)))
74 #define allocfd_set(n) ((fd_set *) alloca (sizeof_fd_set (n)))
75 #define copyfd_set(to, from, n) memcpy (to, from, sizeof_fd_set (n));
77 /* Make a fhandler_foo::ready_for_ready method.
78 Assumption: The "ready_for_read" methods are called with one level of
80 #define MAKEready(what) \
82 fhandler_##what::ready_for_read (int fd, DWORD howlong, int ignra) \
84 select_record me (this); \
86 (void) select_read (&me); \
87 while (!peek_##what (&me, ignra) && howlong == INFINITE) \
88 if (fd >= 0 && dtable.not_open (fd)) \
90 else if (WaitForSingleObject (signal_arrived, 10) == WAIT_OBJECT_0) \
92 return me.read_ready; \
95 #define set_handle_or_return_if_not_open(h, s) \
96 h = (s)->fh->get_handle (); \
97 if (dtable.not_open ((s)->fd)) \
99 (s)->saw_error = TRUE; \
104 /* The main select code.
108 cygwin_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
112 fd_set *dummy_readfds = allocfd_set (n);
113 fd_set *dummy_writefds = allocfd_set (n);
114 fd_set *dummy_exceptfds = allocfd_set (n);
115 sigframe thisframe (mainthread, 0);
125 select_printf ("%d, %p, %p, %p, %p", n, readfds, writefds, exceptfds, to);
129 UNIX_FD_ZERO (dummy_readfds, n);
130 readfds = dummy_readfds;
134 UNIX_FD_ZERO (dummy_writefds, n);
135 writefds = dummy_writefds;
139 UNIX_FD_ZERO (dummy_exceptfds, n);
140 exceptfds = dummy_exceptfds;
143 for (int i = 0; i < n; i++)
144 if (!sel.test_and_set (i, readfds, writefds, exceptfds))
146 select_printf ("aborting due to test_and_set error");
147 return -1; /* Invalid fd, maybe? */
150 /* Convert to milliseconds or INFINITE if to == NULL */
151 DWORD ms = to ? (to->tv_sec * 1000) + (to->tv_usec / 1000) : INFINITE;
152 if (ms == 0 && to->tv_usec)
153 ms = 1; /* At least 1 ms granularity */
156 select_printf ("to->tv_sec %d, to->tv_usec %d, ms %d", to->tv_sec, to->tv_usec, ms);
158 select_printf ("to NULL, ms %x", ms);
160 select_printf ("sel.always_ready %d", sel.always_ready);
162 /* Degenerate case. No fds to wait for. Just wait. */
163 if (sel.start.next == NULL)
165 if (WaitForSingleObject (signal_arrived, ms) == WAIT_OBJECT_0)
167 select_printf ("signal received");
168 set_sig_errno (EINTR);
174 /* If one of the selected fds is "always ready" just poll everything and return
175 the result. There is no need to wait. */
176 if (sel.always_ready || ms == 0)
178 UNIX_FD_ZERO (readfds, n);
179 UNIX_FD_ZERO (writefds, n);
180 UNIX_FD_ZERO (exceptfds, n);
181 return sel.poll (readfds, writefds, exceptfds);
184 /* Wait for an fd to come alive */
185 return sel.wait (readfds, writefds, exceptfds, ms);
189 select_stuff::~select_stuff ()
191 select_record *s = &start;
193 select_printf ("calling cleanup routines");
194 while ((s = s->next))
196 s->cleanup (s, this);
198 select_record *snext = start.next;
200 select_printf ("deleting select records");
208 /* Add a record to the select chain */
210 select_stuff::test_and_set (int i, fd_set *readfds, fd_set *writefds,
213 select_record *s = NULL;
214 if (UNIX_FD_ISSET (i, readfds) && (s = dtable.select_read (i, s)) == NULL)
215 return 0; /* error */
216 if (UNIX_FD_ISSET (i, writefds) && (s = dtable.select_write (i, s)) == NULL)
217 return 0; /* error */
218 if (UNIX_FD_ISSET (i, exceptfds) && (s = dtable.select_except (i, s)) == NULL)
219 return 0; /* error */
221 return 1; /* nothing to do */
223 if (s->read_ready || s->write_ready || s->except_ready)
226 if (s->windows_handle || s->windows_handle || s->windows_handle)
229 s->next = start.next;
234 /* Poll every fd in the select chain. Set appropriate fd in mask. */
236 select_stuff::poll (fd_set *readfds, fd_set *writefds, fd_set *exceptfds)
239 select_record *s = &start;
240 while ((s = s->next))
241 n += s->poll (s, readfds, writefds, exceptfds);
242 select_printf ("returning %d", n);
246 /* The heart of select. Waits for an fd to do something interesting. */
248 select_stuff::wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
252 HANDLE w4[MAXIMUM_WAIT_OBJECTS];
253 select_record *s = &start;
256 w4[m++] = signal_arrived; /* Always wait for the arrival of a signal. */
257 /* Loop through the select chain, starting up anything appropriate and
258 counting the number of active fds. */
259 while ((s = s->next))
261 if (!s->startup (s, this))
268 for (int i = 1; i < m; i++)
277 DWORD start_time = GetTickCount (); /* Record the current time for later use. */
279 /* Allocate some fd_set structures using the number of fds as a guide. */
280 fd_set *r = allocfd_set (n);
281 fd_set *w = allocfd_set (n);
282 fd_set *e = allocfd_set (n);
286 debug_printf ("n %d, ms %u", n, ms);
290 wait_ret = WaitForMultipleObjects (m, w4, FALSE, ms);
292 wait_ret = MsgWaitForMultipleObjects (m, w4, FALSE, ms, QS_ALLINPUT);
297 select_printf ("signal received");
298 set_sig_errno (EINTR);
301 select_printf ("WaitForMultipleObjects failed");
305 select_printf ("timed out");
309 select_printf ("woke up. wait_ret %d. verifying", wait_ret);
312 while ((s = s->next))
314 return -1; /* Somebody detected an error */
315 else if ((((wait_ret >= m && s->windows_handle) || s->h == w4[wait_ret])) &&
316 s->verify (s, r, w, e))
319 select_printf ("gotone %d", gotone);
325 select_printf ("looping");
328 select_printf ("recalculating ms");
330 DWORD now = GetTickCount ();
331 if (now > (start_time + ms))
333 select_printf ("timed out after verification");
336 ms -= (now - start_time);
338 select_printf ("ms now %u", ms);
342 copyfd_set (readfds, r, n);
343 copyfd_set (writefds, w, n);
344 copyfd_set (exceptfds, e, n);
346 return poll (readfds, writefds, exceptfds);
350 set_bits (select_record *me, fd_set *readfds, fd_set *writefds,
354 select_printf ("me %p, testing fd %d (%s)", me, me->fd, me->fh->get_name ());
355 if (me->read_selected && me->read_ready)
357 UNIX_FD_SET (me->fd, readfds);
360 if (me->write_selected && me->write_ready)
362 UNIX_FD_SET (me->fd, writefds);
365 if (me->except_ready && me->except_ready)
367 UNIX_FD_SET (me->fd, exceptfds);
370 select_printf ("ready %d", ready);
375 verify_true (select_record *, fd_set *, fd_set *, fd_set *)
381 verify_ok (select_record *me, fd_set *readfds, fd_set *writefds,
384 return set_bits (me, readfds, writefds, exceptfds);
388 no_startup (select_record *, select_stuff *)
394 no_verify (select_record *, fd_set *, fd_set *, fd_set *)
400 peek_pipe (select_record *s, int ignra)
404 fhandler_base *fh = s->fh;
407 set_handle_or_return_if_not_open (h, s);
409 /* Don't perform complicated tests if we don't need to. */
410 if (!s->read_selected && !s->except_selected)
413 if (s->read_selected)
417 select_printf ("already ready");
421 if (fh->bg_check (SIGTTIN) <= 0)
423 gotone = s->read_ready = 1;
427 if (!ignra && fh->get_device () != FH_PTYM && fh->get_device () != FH_TTYM &&
428 fh->get_readahead_valid ())
430 select_printf ("readahead");
431 gotone = s->read_ready = 1;
436 if (fh->get_device() != FH_PIPEW &&
437 !PeekNamedPipe (h, NULL, 0, NULL, (DWORD *) &n, NULL))
439 select_printf ("%s, PeekNamedPipe failed, %E", fh->get_name ());
445 select_printf ("%s, n %d", fh->get_name (), n);
446 if (s->except_selected)
447 gotone += s->except_ready = TRUE;
448 if (s->read_selected)
449 gotone += s->read_ready = TRUE;
451 if (n > 0 && s->read_selected)
453 select_printf ("%s, ready for read", fh->get_name ());
454 gotone += s->read_ready = TRUE;
456 if (!gotone && s->fh->hit_eof ())
458 select_printf ("%s, saw EOF", fh->get_name ());
459 if (s->except_selected)
460 gotone = s->except_ready = TRUE;
461 if (s->read_selected)
462 gotone += s->read_ready = TRUE;
463 select_printf ("saw eof on '%s'", fh->get_name ());
467 return gotone || s->write_ready;
471 poll_pipe (select_record *me, fd_set *readfds, fd_set *writefds,
474 return peek_pipe (me, 0) ?
475 set_bits (me, readfds, writefds, exceptfds) :
481 static int start_thread_pipe (select_record *me, select_stuff *stuff);
486 BOOL stop_thread_pipe;
487 select_record *start;
491 thread_pipe (void *arg)
493 pipeinf *pi = (pipeinf *)arg;
498 select_record *s = pi->start;
499 while ((s = s->next))
500 if (s->startup == start_thread_pipe)
502 if (peek_pipe (s, 0))
504 if (pi->stop_thread_pipe)
506 select_printf ("stopping");
511 if (pi->stop_thread_pipe)
513 select_printf ("stopping from outer loop");
525 start_thread_pipe (select_record *me, select_stuff *stuff)
527 if (stuff->device_specific[FHDEVN(FH_PIPE)])
529 me->h = ((pipeinf *) stuff->device_specific[FHDEVN(FH_PIPE)])->thread;
532 pipeinf *pi = new pipeinf;
533 pi->start = &stuff->start;
534 pi->stop_thread_pipe = FALSE;
535 pi->thread = me->h = makethread (thread_pipe, (LPVOID)pi, 0, "select_pipe");
538 stuff->device_specific[FHDEVN(FH_PIPE)] = (void *)pi;
543 pipe_cleanup (select_record *, select_stuff *stuff)
545 pipeinf *pi = (pipeinf *)stuff->device_specific[FHDEVN(FH_PIPE)];
546 if (pi && pi->thread)
548 pi->stop_thread_pipe = TRUE;
549 WaitForSingleObject (pi->thread, INFINITE);
550 CloseHandle (pi->thread);
552 stuff->device_specific[FHDEVN(FH_PIPE)] = NULL;
557 fhandler_pipe::select_read (select_record *s)
560 s = new select_record;
561 s->startup = start_thread_pipe;
563 s->verify = verify_ok;
564 s->read_selected = TRUE;
565 s->cleanup = pipe_cleanup;
570 fhandler_pipe::select_write (select_record *s)
574 s = new select_record;
575 s->startup = no_startup;
577 s->verify = no_verify;
579 s->write_selected = TRUE;
580 s->write_ready = TRUE;
585 fhandler_pipe::select_except (select_record *s)
588 s = new select_record;
589 s->startup = start_thread_pipe;
591 s->verify = verify_ok;
592 s->cleanup = pipe_cleanup;
593 s->except_selected = TRUE;
598 peek_console (select_record *me, int ignra)
600 extern const char * get_nonascii_key (INPUT_RECORD& input_rec);
601 fhandler_console *fh = (fhandler_console *)me->fh;
603 if (!me->read_selected)
604 return me->write_ready;
606 if (!ignra && fh->get_readahead_valid ())
608 select_printf ("readahead");
609 return me->read_ready = 1;
614 select_printf ("already ready");
621 set_handle_or_return_if_not_open (h, me);
624 if (fh->bg_check (SIGTTIN) <= 0)
625 return me->read_ready = 1;
626 else if (!PeekConsoleInput (h, &irec, 1, &events_read) || !events_read)
630 if (irec.EventType == WINDOW_BUFFER_SIZE_EVENT)
631 kill_pgrp (fh->tc->getpgid (), SIGWINCH);
632 else if (irec.EventType == KEY_EVENT && irec.Event.KeyEvent.bKeyDown == TRUE &&
633 (irec.Event.KeyEvent.uChar.AsciiChar || get_nonascii_key (irec)))
634 return me->read_ready = 1;
636 /* Read and discard the event */
637 ReadConsoleInput (h, &irec, 1, &events_read);
640 return me->write_ready;
644 poll_console (select_record *me, fd_set *readfds, fd_set *writefds,
647 return peek_console (me, 0) ?
648 set_bits (me, readfds, writefds, exceptfds) :
655 fhandler_console::select_read (select_record *s)
659 s = new select_record;
660 s->startup = no_startup;
661 s->poll = poll_console;
662 s->verify = poll_console;
665 s->h = get_handle ();
666 s->read_selected = TRUE;
671 fhandler_console::select_write (select_record *s)
675 s = new select_record;
676 s->startup = no_startup;
677 s->poll = poll_console;
678 s->verify = no_verify;
681 s->write_selected = TRUE;
682 s->write_ready = TRUE;
687 fhandler_console::select_except (select_record *s)
691 s = new select_record;
692 s->startup = no_startup;
693 s->poll = poll_console;
694 s->verify = no_verify;
697 s->except_selected = TRUE;
702 fhandler_tty_common::ready_for_read (int fd, DWORD howlong, int ignra)
705 if (myself->pgid && get_ttyp ()->getpgid () != myself->pgid &&
706 myself->ctty == ttynum) // background process?
707 return 1; // Yes. Let read return an error
709 return ((fhandler_pipe*)this)->fhandler_pipe::ready_for_read (fd, howlong, ignra);
713 fhandler_tty_common::select_read (select_record *s)
715 return ((fhandler_pipe*)this)->fhandler_pipe::select_read (s);
719 fhandler_tty_common::select_write (select_record *s)
721 return ((fhandler_pipe *)this)->fhandler_pipe::select_write (s);
725 fhandler_tty_common::select_except (select_record *s)
727 return ((fhandler_pipe *)this)->fhandler_pipe::select_except (s);
731 fhandler_dev_null::select_read (select_record *s)
735 s = new select_record;
736 s->startup = no_startup;
738 s->verify = no_verify;
740 s->h = get_handle ();
741 s->read_selected = TRUE;
746 fhandler_dev_null::select_write (select_record *s)
750 s = new select_record;
751 s->startup = no_startup;
753 s->verify = no_verify;
755 s->h = get_handle ();
756 s->write_selected = TRUE;
761 fhandler_dev_null::select_except (select_record *s)
765 s = new select_record;
766 s->startup = no_startup;
768 s->verify = no_verify;
770 s->h = get_handle ();
771 s->except_selected = TRUE;
772 s->except_ready = TRUE;
776 static int start_thread_serial (select_record *me, select_stuff *stuff);
781 BOOL stop_thread_serial;
782 select_record *start;
786 peek_serial (select_record *s, int)
791 fhandler_serial *fh = (fhandler_serial *)s->fh;
793 if (fh->get_readahead_valid () || fh->overlapped_armed < 0)
794 return s->read_ready = 1;
796 select_printf ("fh->overlapped_armed %d", fh->overlapped_armed);
799 set_handle_or_return_if_not_open (h, s);
802 if (s->read_selected && s->read_ready || (s->write_selected && s->write_ready))
804 select_printf ("already ready");
809 (void) SetCommMask (h, EV_RXCHAR);
811 if (!fh->overlapped_armed)
816 ResetEvent (fh->io_status.hEvent);
818 if (!ClearCommError (h, &ev, &st))
820 debug_printf ("ClearCommError");
824 return s->read_ready = 1;
825 else if (WaitCommEvent (h, &ev, &fh->io_status))
826 return s->read_ready = 1;
827 else if (GetLastError () == ERROR_IO_PENDING)
828 fh->overlapped_armed = 1;
831 debug_printf ("WaitCommEvent");
839 w4[0] = fh->io_status.hEvent;
840 w4[1] = signal_arrived;
843 switch (WaitForMultipleObjects (2, w4, FALSE, to))
846 if (!ClearCommError (h, &ev, &st))
848 debug_printf ("ClearCommError");
851 else if (!st.cbInQue)
855 return s->read_ready = 1;
856 select_printf ("got something");
858 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
860 case WAIT_OBJECT_0 + 1:
861 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
862 select_printf ("interrupt");
863 set_sig_errno (EINTR);
867 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
870 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
871 debug_printf ("WaitForMultipleObjects");
879 if (GetLastError () == ERROR_OPERATION_ABORTED)
881 select_printf ("operation aborted");
887 select_printf ("error %E");
892 thread_serial (void *arg)
894 serialinf *si = (serialinf *)arg;
899 select_record *s = si->start;
900 while ((s = s->next))
901 if (s->startup == start_thread_serial)
903 if (peek_serial (s, 0))
906 if (si->stop_thread_serial)
908 select_printf ("stopping");
915 select_printf ("exiting");
920 start_thread_serial (select_record *me, select_stuff *stuff)
922 if (stuff->device_specific[FHDEVN(FH_SERIAL)])
924 me->h = ((pipeinf *) stuff->device_specific[FHDEVN(FH_SERIAL)])->thread;
927 serialinf *si = new serialinf;
928 si->start = &stuff->start;
929 si->stop_thread_serial = FALSE;
930 si->thread = me->h = makethread (thread_serial, (LPVOID)si, 0, "select_serial");
933 stuff->device_specific[FHDEVN(FH_SERIAL)] = (void *)si;
938 serial_cleanup (select_record *, select_stuff *stuff)
940 serialinf *si = (serialinf *)stuff->device_specific[FHDEVN(FH_SERIAL)];
941 if (si && si->thread)
943 si->stop_thread_serial = TRUE;
944 WaitForSingleObject (si->thread, INFINITE);
945 CloseHandle (si->thread);
947 stuff->device_specific[FHDEVN(FH_SERIAL)] = NULL;
952 poll_serial (select_record *me, fd_set *readfds, fd_set *writefds,
956 return peek_serial (me, 0) ?
957 set_bits (me, readfds, writefds, exceptfds) :
964 fhandler_serial::select_read (select_record *s)
968 s = new select_record;
969 s->startup = start_thread_serial;
970 s->poll = poll_serial;
971 s->verify = verify_ok;
972 s->cleanup = serial_cleanup;
974 s->read_selected = TRUE;
979 fhandler_serial::select_write (select_record *s)
983 s = new select_record;
984 s->startup = no_startup;
986 s->verify = verify_ok;
988 s->h = get_handle ();
989 s->write_selected = TRUE;
990 s->write_ready = TRUE;
995 fhandler_serial::select_except (select_record *s)
999 s = new select_record;
1000 s->startup = no_startup;
1002 s->verify = verify_ok;
1005 s->except_selected = FALSE; // Can't do this
1010 fhandler_base::ready_for_read (int, DWORD, int)
1016 fhandler_base::select_read (select_record *s)
1020 s = new select_record;
1021 s->startup = no_startup;
1023 s->verify = verify_ok;
1025 s->h = get_handle ();
1026 s->read_selected = TRUE;
1027 s->read_ready = TRUE;
1032 fhandler_base::select_write (select_record *s)
1036 s = new select_record;
1037 s->startup = no_startup;
1039 s->verify = verify_ok;
1041 s->h = get_handle ();
1042 s->write_selected = TRUE;
1043 s->write_ready = TRUE;
1048 fhandler_base::select_except (select_record *s)
1052 s = new select_record;
1053 s->startup = no_startup;
1055 s->verify = verify_ok;
1058 s->write_selected = TRUE;
1065 winsock_fd_set readfds, writefds, exceptfds;
1067 struct sockaddr_in sin;
1068 select_record *start;
1072 peek_socket (select_record *me, int)
1074 winsock_fd_set ws_readfds, ws_writefds, ws_exceptfds;
1075 struct timeval tv = {0, 0};
1076 WINSOCK_FD_ZERO (&ws_readfds);
1077 WINSOCK_FD_ZERO (&ws_writefds);
1078 WINSOCK_FD_ZERO (&ws_exceptfds);
1082 set_handle_or_return_if_not_open (h, me);
1083 select_printf ("considering handle %p", h);
1085 if (me->read_selected)
1087 select_printf ("adding read fd_set %s, fd %d", me->fh->get_name (),
1089 WINSOCK_FD_SET (h, &ws_readfds);
1091 if (me->write_selected)
1093 select_printf ("adding write fd_set %s, fd %d", me->fh->get_name (),
1095 WINSOCK_FD_SET (h, &ws_writefds);
1097 if (me->except_selected)
1099 select_printf ("adding except fd_set %s, fd %d", me->fh->get_name (),
1101 WINSOCK_FD_SET (h, &ws_exceptfds);
1103 int r = WINSOCK_SELECT (0, &ws_readfds, &ws_writefds, &ws_exceptfds, &tv);
1104 select_printf ("WINSOCK_SELECT returned %d", r);
1107 select_printf ("error %d", WSAGetLastError ());
1111 if (WINSOCK_FD_ISSET (h, &ws_readfds) || (me->read_selected && me->read_ready))
1112 gotone = me->read_ready = TRUE;
1113 if (WINSOCK_FD_ISSET (h, &ws_writefds) || (me->write_selected && me->write_ready))
1114 gotone = me->write_ready = TRUE;
1115 if (WINSOCK_FD_ISSET (h, &ws_exceptfds) || (me->except_selected && me->except_ready))
1116 gotone = me->except_ready = TRUE;
1121 poll_socket (select_record *me, fd_set *readfds, fd_set *writefds,
1124 return peek_socket (me, 0) ?
1125 set_bits (me, readfds, writefds, exceptfds) :
1131 static int start_thread_socket (select_record *, select_stuff *);
1134 thread_socket (void *arg)
1136 socketinf *si = (socketinf *)arg;
1138 select_printf ("stuff_start %p", &si->start);
1139 int r = WINSOCK_SELECT (0, &si->readfds, &si->writefds, &si->exceptfds, NULL);
1140 select_printf ("Win32 select returned %d", r);
1142 select_printf ("error %d", WSAGetLastError ());
1143 select_record *s = si->start;
1144 while ((s = s->next))
1145 if (s->startup == start_thread_socket)
1147 HANDLE h = s->fh->get_handle ();
1148 select_printf ("s %p, testing fd %d (%s)", s, s->fd, s->fh->get_name ());
1149 if (WINSOCK_FD_ISSET (h, &si->readfds))
1151 select_printf ("read_ready");
1152 s->read_ready = TRUE;
1154 if (WINSOCK_FD_ISSET (h, &si->writefds))
1156 select_printf ("write_ready");
1157 s->write_ready = TRUE;
1159 if (WINSOCK_FD_ISSET (h, &si->exceptfds))
1161 select_printf ("except_ready");
1162 s->except_ready = TRUE;
1166 if (WINSOCK_FD_ISSET (si->exitsock, &si->readfds))
1167 select_printf ("saw exitsock read");
1172 extern "C" unsigned long htonl (unsigned long);
1175 start_thread_socket (select_record *me, select_stuff *stuff)
1179 if ((si = (socketinf *)stuff->device_specific[FHDEVN(FH_SOCKET)]))
1186 WINSOCK_FD_ZERO (&si->readfds);
1187 WINSOCK_FD_ZERO (&si->writefds);
1188 WINSOCK_FD_ZERO (&si->exceptfds);
1189 select_record *s = &stuff->start;
1190 while ((s = s->next))
1191 if (s->startup == start_thread_socket)
1193 HANDLE h = s->fh->get_handle ();
1194 select_printf ("Handle %p", h);
1195 if (s->read_selected)
1197 WINSOCK_FD_SET (h, &si->readfds);
1198 select_printf ("Added to readfds");
1200 if (s->write_selected)
1202 WINSOCK_FD_SET (h, &si->writefds);
1203 select_printf ("Added to writefds");
1205 if (s->except_selected)
1207 WINSOCK_FD_SET (h, &si->exceptfds);
1208 select_printf ("Added to exceptfds");
1212 if ((si->exitsock = socket (PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
1214 set_winsock_errno ();
1215 select_printf ("cannot create socket, %E");
1218 /* Allow rapid reuse of the port. */
1220 (void) setsockopt (si->exitsock, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
1222 int sin_len = sizeof(si->sin);
1223 memset (&si->sin, 0, sizeof (si->sin));
1224 si->sin.sin_family = AF_INET;
1225 si->sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1226 if (bind (si->exitsock, (struct sockaddr *) &si->sin, sizeof (si->sin)) < 0)
1228 select_printf ("cannot bind socket, %E");
1232 if (getsockname (si->exitsock, (struct sockaddr *) &si->sin, &sin_len) < 0)
1234 select_printf ("getsockname error");
1238 if (listen (si->exitsock, 1))
1240 select_printf ("listen failed, %E");
1244 select_printf ("exitsock %p", si->exitsock);
1245 WINSOCK_FD_SET ((HANDLE) si->exitsock, &si->readfds);
1246 WINSOCK_FD_SET ((HANDLE) si->exitsock, &si->exceptfds);
1247 stuff->device_specific[FHDEVN(FH_SOCKET)] = (void *) si;
1248 si->start = &stuff->start;
1249 select_printf ("stuff_start %p", &stuff->start);
1250 si->thread = me->h = makethread (thread_socket, (LPVOID)si, 0,
1255 set_winsock_errno ();
1256 closesocket (si->exitsock);
1261 socket_cleanup (select_record *, select_stuff *stuff)
1263 socketinf *si = (socketinf *)stuff->device_specific[FHDEVN(FH_SOCKET)];
1264 select_printf ("si %p si->thread %p", si, si ? si->thread : NULL);
1265 if (si && si->thread)
1267 select_printf ("connection to si->exitsock %p", si->exitsock);
1268 SOCKET s = socket (AF_INET, SOCK_STREAM, 0);
1269 /* Connecting to si->exitsock will cause any executing select to wake
1270 up. When this happens then the exitsock condition will cause the
1271 thread to terminate. */
1272 if (connect (s, (struct sockaddr *) &si->sin, sizeof (si->sin)) < 0)
1274 set_winsock_errno ();
1275 select_printf ("connect failed");
1276 /* FIXME: now what? */
1281 /* Wait for thread to go away */
1282 WaitForSingleObject (si->thread, INFINITE);
1283 shutdown (si->exitsock, 2);
1284 closesocket (si->exitsock);
1285 CloseHandle (si->thread);
1286 stuff->device_specific[FHDEVN(FH_SOCKET)] = NULL;
1289 select_printf ("returning");
1293 fhandler_socket::select_read (select_record *s)
1297 s = new select_record;
1298 s->startup = start_thread_socket;
1299 s->poll = poll_socket;
1300 s->verify = verify_true;
1301 s->cleanup = socket_cleanup;
1303 s->read_selected = TRUE;
1308 fhandler_socket::select_write (select_record *s)
1312 s = new select_record;
1313 s->startup = start_thread_socket;
1314 s->poll = poll_socket;
1315 s->verify = verify_true;
1316 s->cleanup = socket_cleanup;
1318 s->write_selected = TRUE;
1323 fhandler_socket::select_except (select_record *s)
1327 s = new select_record;
1328 s->startup = start_thread_socket;
1329 s->poll = poll_socket;
1330 s->verify = verify_true;
1331 s->cleanup = socket_cleanup;
1333 s->except_selected = TRUE;
1338 peek_windows (select_record *me, int)
1342 set_handle_or_return_if_not_open (h, me);
1344 if (me->read_selected && me->read_ready)
1347 if (PeekMessage (&m, (HWND) h, 0, 0, PM_NOREMOVE))
1349 me->read_ready = TRUE;
1350 select_printf ("window %d(%p) ready", me->fd, me->fh->get_handle ());
1354 select_printf ("window %d(%p) not ready", me->fd, me->fh->get_handle ());
1355 return me->write_ready;
1359 poll_windows (select_record *me, fd_set *readfds, fd_set *writefds,
1363 return peek_windows (me, 0) ?
1364 set_bits (me, readfds, writefds, exceptfds) :
1371 fhandler_windows::select_read (select_record *s)
1375 s = new select_record;
1376 s->startup = no_startup;
1377 s->poll = poll_windows;
1378 s->verify = poll_windows;
1380 s->h = get_handle ();
1381 s->read_selected = TRUE;
1382 s->h = get_handle ();
1383 s->windows_handle = TRUE;
1388 fhandler_windows::select_write (select_record *s)
1392 s = new select_record;
1393 s->startup = no_startup;
1395 s->verify = verify_ok;
1397 s->h = get_handle ();
1398 s->write_selected = TRUE;
1399 s->write_ready = TRUE;
1400 s->windows_handle = TRUE;
1405 fhandler_windows::select_except (select_record *s)
1409 s = new select_record;
1410 s->startup = no_startup;
1412 s->verify = verify_ok;
1414 s->h = get_handle ();
1415 s->except_selected = TRUE;
1416 s->except_ready = TRUE;
1417 s->windows_handle = TRUE;