1 /* w32-io.c - W32 API I/O functions.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GPGME; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include <sys/types.h>
41 /* We assume that a HANDLE can be represented by an int which should
42 be true for all i386 systems (HANDLE is defined as void *) and
43 these are the only systems for which Windows is available. Further
44 we assume that -1 denotes an invalid handle. */
46 #define fd_to_handle(a) ((HANDLE)(a))
47 #define handle_to_fd(a) ((int)(a))
48 #define pid_to_handle(a) ((HANDLE)(a))
49 #define handle_to_pid(a) ((int)(a))
51 #define READBUF_SIZE 4096
52 #define WRITEBUF_SIZE 4096
53 #define MAX_READERS 20
54 #define MAX_WRITERS 20
59 void (*handler)(int,void*);
62 DEFINE_STATIC_LOCK (notify_table_lock);
65 struct reader_context_s {
76 HANDLE have_data_ev; /* manually reset */
77 HANDLE have_space_ev; /* auto reset */
79 size_t readpos, writepos;
80 char buffer[READBUF_SIZE];
87 struct reader_context_s *context;
88 } reader_table[MAX_READERS];
89 static int reader_table_size= MAX_READERS;
90 DEFINE_STATIC_LOCK (reader_table_lock);
93 struct writer_context_s {
102 HANDLE have_data; /* manually reset */
106 char buffer[WRITEBUF_SIZE];
113 struct writer_context_s *context;
114 } writer_table[MAX_WRITERS];
115 static int writer_table_size= MAX_WRITERS;
116 DEFINE_STATIC_LOCK (writer_table_lock);
121 set_synchronize (HANDLE h)
125 /* For NT we have to set the sync flag. It seems that the only
126 * way to do it is by duplicating the handle. Tsss.. */
127 if (!DuplicateHandle( GetCurrentProcess(), h,
128 GetCurrentProcess(), &tmp,
129 EVENT_MODIFY_STATE|SYNCHRONIZE, FALSE, 0 ) ) {
130 DEBUG1 ("** Set SYNCRONIZE failed: ec=%d\n", (int)GetLastError());
141 static DWORD CALLBACK
144 struct reader_context_s *c = arg;
148 DEBUG2 ("reader thread %p for file %p started", c->thread_hd, c->file_hd );
151 /* leave a 1 byte gap so that we can see whether it is empty or full*/
152 if ((c->writepos + 1) % READBUF_SIZE == c->readpos) {
154 if (!ResetEvent (c->have_space_ev) )
155 DEBUG1 ("ResetEvent failed: ec=%d", (int)GetLastError ());
157 DEBUG1 ("reader thread %p: waiting for space ...", c->thread_hd );
158 WaitForSingleObject (c->have_space_ev, INFINITE);
159 DEBUG1 ("reader thread %p: got space", c->thread_hd );
166 nbytes = (c->readpos + READBUF_SIZE - c->writepos-1) % READBUF_SIZE;
167 if ( nbytes > READBUF_SIZE - c->writepos )
168 nbytes = READBUF_SIZE - c->writepos;
171 DEBUG2 ("reader thread %p: reading %d bytes", c->thread_hd, nbytes );
172 if ( !ReadFile ( c->file_hd,
173 c->buffer+c->writepos, nbytes, &nread, NULL) ) {
174 c->error_code = (int)GetLastError ();
175 if (c->error_code == ERROR_BROKEN_PIPE ) {
177 DEBUG1 ("reader thread %p: got eof (broken pipe)",
182 DEBUG2 ("reader thread %p: read error: ec=%d",
183 c->thread_hd, c->error_code );
189 DEBUG1 ("reader thread %p: got eof", c->thread_hd );
192 DEBUG2 ("reader thread %p: got %d bytes", c->thread_hd, (int)nread );
199 c->writepos = (c->writepos + nread) % READBUF_SIZE;
200 if ( !SetEvent (c->have_data_ev) )
201 DEBUG1 ("SetEvent failed: ec=%d", (int)GetLastError ());
204 /* indicate that we have an error or eof */
205 if ( !SetEvent (c->have_data_ev) )
206 DEBUG1 ("SetEvent failed: ec=%d", (int)GetLastError ());
207 DEBUG1 ("reader thread %p ended", c->thread_hd );
208 SetEvent (c->stopped);
214 static struct reader_context_s *
215 create_reader (HANDLE fd)
217 struct reader_context_s *c;
218 SECURITY_ATTRIBUTES sec_attr;
221 DEBUG1 ("creating new read thread for file handle %p", fd );
222 memset (&sec_attr, 0, sizeof sec_attr );
223 sec_attr.nLength = sizeof sec_attr;
224 sec_attr.bInheritHandle = FALSE;
226 c = calloc (1, sizeof *c );
231 c->have_data_ev = CreateEvent (&sec_attr, TRUE, FALSE, NULL);
232 c->have_space_ev = CreateEvent (&sec_attr, FALSE, TRUE, NULL);
233 c->stopped = CreateEvent (&sec_attr, TRUE, FALSE, NULL);
234 if (!c->have_data_ev || !c->have_space_ev || !c->stopped ) {
235 DEBUG1 ("** CreateEvent failed: ec=%d\n", (int)GetLastError ());
237 CloseHandle (c->have_data_ev);
238 if (c->have_space_ev)
239 CloseHandle (c->have_space_ev);
241 CloseHandle (c->stopped);
246 c->have_data_ev = set_synchronize (c->have_data_ev);
247 INIT_LOCK (c->mutex);
249 c->thread_hd = CreateThread (&sec_attr, 0, reader, c, 0, &tid );
251 DEBUG1 ("** failed to create reader thread: ec=%d\n",
252 (int)GetLastError ());
253 DESTROY_LOCK (c->mutex);
255 CloseHandle (c->have_data_ev);
256 if (c->have_space_ev)
257 CloseHandle (c->have_space_ev);
259 CloseHandle (c->stopped);
268 destroy_reader (struct reader_context_s *c)
272 if (c->have_space_ev)
273 SetEvent (c->have_space_ev);
276 DEBUG1 ("waiting for thread %p termination ...", c->thread_hd );
277 WaitForSingleObject (c->stopped, INFINITE);
278 DEBUG1 ("thread %p has terminated", c->thread_hd );
281 CloseHandle (c->stopped);
283 CloseHandle (c->have_data_ev);
284 if (c->have_space_ev)
285 CloseHandle (c->have_space_ev);
286 CloseHandle (c->thread_hd);
287 DESTROY_LOCK (c->mutex);
293 * Find a reader context or create a new one
294 * Note that the reader context will last until a io_close.
296 static struct reader_context_s *
297 find_reader (int fd, int start_it)
301 for (i=0; i < reader_table_size ; i++ ) {
302 if ( reader_table[i].used && reader_table[i].fd == fd )
303 return reader_table[i].context;
308 LOCK (reader_table_lock);
309 for (i=0; i < reader_table_size; i++ ) {
310 if (!reader_table[i].used) {
311 reader_table[i].fd = fd;
312 reader_table[i].context = create_reader (fd_to_handle (fd));
313 reader_table[i].used = 1;
314 UNLOCK (reader_table_lock);
315 return reader_table[i].context;
318 UNLOCK (reader_table_lock);
328 LOCK (reader_table_lock);
329 for (i=0; i < reader_table_size; i++ ) {
330 if (reader_table[i].used && reader_table[i].fd == fd ) {
331 destroy_reader (reader_table[i].context);
332 reader_table[i].context = NULL;
333 reader_table[i].used = 0;
337 UNLOCK (reader_table_lock);
343 _gpgme_io_read ( int fd, void *buffer, size_t count )
346 struct reader_context_s *c = find_reader (fd,1);
348 DEBUG2 ("fd %d: about to read %d bytes\n", fd, (int)count );
350 DEBUG0 ( "no reader thread\n");
353 if (c->eof_shortcut) {
354 DEBUG1 ("fd %d: EOF (again)", fd );
359 if (c->readpos == c->writepos && !c->error) { /*no data avail*/
361 DEBUG2 ("fd %d: waiting for data from thread %p", fd, c->thread_hd);
362 WaitForSingleObject (c->have_data_ev, INFINITE);
363 DEBUG2 ("fd %d: data from thread %p available", fd, c->thread_hd);
367 if (c->readpos == c->writepos || c->error) {
371 DEBUG1 ("fd %d: EOF", fd );
375 DEBUG1 ("fd %d: EOF but eof flag not set", fd );
378 DEBUG1 ("fd %d: read error", fd );
382 nread = c->readpos < c->writepos? c->writepos - c->readpos
383 : READBUF_SIZE - c->readpos;
386 memcpy (buffer, c->buffer+c->readpos, nread);
387 c->readpos = (c->readpos + nread) % READBUF_SIZE;
388 if (c->readpos == c->writepos && !c->eof) {
389 if ( !ResetEvent (c->have_data_ev) )
390 DEBUG1 ("ResetEvent failed: ec=%d", (int)GetLastError ());
392 if (!SetEvent (c->have_space_ev))
393 DEBUG1 ("SetEvent failed: ec=%d", (int)GetLastError ());
396 DEBUG2 ("fd %d: got %d bytes\n", fd, nread );
404 * The writer does use a simple buffering strategy so that we are
405 * informed about write errors as soon as possible (i.e. with the the
406 * next call to the write function
408 static DWORD CALLBACK
411 struct writer_context_s *c = arg;
414 DEBUG2 ("writer thread %p for file %p started", c->thread_hd, c->file_hd );
418 if (!ResetEvent (c->have_data) )
419 DEBUG1 ("ResetEvent failed: ec=%d", (int)GetLastError ());
421 DEBUG1 ("writer thread %p: idle ...", c->thread_hd );
422 WaitForSingleObject (c->have_data, INFINITE);
423 DEBUG1 ("writer thread %p: got data to send", c->thread_hd );
432 DEBUG2 ("writer thread %p: writing %d bytes",
433 c->thread_hd, c->nbytes );
434 if ( c->nbytes && !WriteFile ( c->file_hd, c->buffer, c->nbytes,
436 c->error_code = (int)GetLastError ();
438 DEBUG2 ("writer thread %p: write error: ec=%d",
439 c->thread_hd, c->error_code );
442 DEBUG2 ("writer thread %p: wrote %d bytes",
443 c->thread_hd, (int)nwritten );
446 c->nbytes -= nwritten;
452 if ( !SetEvent (c->is_empty) )
453 DEBUG1 ("SetEvent failed: ec=%d", (int)GetLastError ());
457 /* indicate that we have an error */
458 if ( !SetEvent (c->is_empty) )
459 DEBUG1 ("SetEvent failed: ec=%d", (int)GetLastError ());
460 DEBUG1 ("writer thread %p ended", c->thread_hd );
461 SetEvent (c->stopped);
467 static struct writer_context_s *
468 create_writer (HANDLE fd)
470 struct writer_context_s *c;
471 SECURITY_ATTRIBUTES sec_attr;
474 DEBUG1 ("creating new write thread for file handle %p", fd );
475 memset (&sec_attr, 0, sizeof sec_attr );
476 sec_attr.nLength = sizeof sec_attr;
477 sec_attr.bInheritHandle = FALSE;
479 c = calloc (1, sizeof *c );
484 c->have_data = CreateEvent (&sec_attr, FALSE, FALSE, NULL);
485 c->is_empty = CreateEvent (&sec_attr, TRUE, TRUE, NULL);
486 c->stopped = CreateEvent (&sec_attr, TRUE, FALSE, NULL);
487 if (!c->have_data || !c->is_empty || !c->stopped ) {
488 DEBUG1 ("** CreateEvent failed: ec=%d\n", (int)GetLastError ());
490 CloseHandle (c->have_data);
492 CloseHandle (c->is_empty);
494 CloseHandle (c->stopped);
499 c->is_empty = set_synchronize (c->is_empty);
500 INIT_LOCK (c->mutex);
502 c->thread_hd = CreateThread (&sec_attr, 0, writer, c, 0, &tid );
504 DEBUG1 ("** failed to create writer thread: ec=%d\n",
505 (int)GetLastError ());
506 DESTROY_LOCK (c->mutex);
508 CloseHandle (c->have_data);
510 CloseHandle (c->is_empty);
512 CloseHandle (c->stopped);
521 destroy_writer (struct writer_context_s *c)
526 SetEvent (c->have_data);
529 DEBUG1 ("waiting for thread %p termination ...", c->thread_hd );
530 WaitForSingleObject (c->stopped, INFINITE);
531 DEBUG1 ("thread %p has terminated", c->thread_hd );
534 CloseHandle (c->stopped);
536 CloseHandle (c->have_data);
538 CloseHandle (c->is_empty);
539 CloseHandle (c->thread_hd);
540 DESTROY_LOCK (c->mutex);
546 * Find a writer context or create a new one
547 * Note that the writer context will last until a io_close.
549 static struct writer_context_s *
550 find_writer (int fd, int start_it)
554 for (i=0; i < writer_table_size ; i++ ) {
555 if ( writer_table[i].used && writer_table[i].fd == fd )
556 return writer_table[i].context;
561 LOCK (writer_table_lock);
562 for (i=0; i < writer_table_size; i++ ) {
563 if (!writer_table[i].used) {
564 writer_table[i].fd = fd;
565 writer_table[i].context = create_writer (fd_to_handle (fd));
566 writer_table[i].used = 1;
567 UNLOCK (writer_table_lock);
568 return writer_table[i].context;
571 UNLOCK (writer_table_lock);
581 LOCK (writer_table_lock);
582 for (i=0; i < writer_table_size; i++ ) {
583 if (writer_table[i].used && writer_table[i].fd == fd ) {
584 destroy_writer (writer_table[i].context);
585 writer_table[i].context = NULL;
586 writer_table[i].used = 0;
590 UNLOCK (writer_table_lock);
597 _gpgme_io_write ( int fd, const void *buffer, size_t count )
599 struct writer_context_s *c = find_writer (fd,1);
601 DEBUG2 ("fd %d: about to write %d bytes\n", fd, (int)count );
603 DEBUG0 ( "no writer thread\n");
608 if ( c->nbytes ) { /* bytes are pending for send */
610 DEBUG2 ("fd %d: waiting for empty buffer in thread %p",
612 WaitForSingleObject (c->is_empty, INFINITE);
613 DEBUG2 ("fd %d: thread %p buffer is empty", fd, c->thread_hd);
620 DEBUG1 ("fd %d: write error", fd );
624 if (count > WRITEBUF_SIZE)
625 count = WRITEBUF_SIZE;
626 memcpy (c->buffer, buffer, count);
628 if (!SetEvent (c->have_data))
629 DEBUG1 ("SetEvent failed: ec=%d", (int)GetLastError ());
632 DEBUG2 ("fd %d: copied %d bytes\n",
639 _gpgme_io_pipe ( int filedes[2], int inherit_idx )
642 SECURITY_ATTRIBUTES sec_attr;
644 memset (&sec_attr, 0, sizeof sec_attr );
645 sec_attr.nLength = sizeof sec_attr;
646 sec_attr.bInheritHandle = FALSE;
648 if (!CreatePipe ( &r, &w, &sec_attr, 0))
650 /* make one end inheritable */
651 if ( inherit_idx == 0 ) {
653 if (!DuplicateHandle( GetCurrentProcess(), r,
654 GetCurrentProcess(), &h, 0,
655 TRUE, DUPLICATE_SAME_ACCESS ) ) {
656 DEBUG1 ("DuplicateHandle failed: ec=%d\n", (int)GetLastError());
664 else if ( inherit_idx == 1 ) {
666 if (!DuplicateHandle( GetCurrentProcess(), w,
667 GetCurrentProcess(), &h, 0,
668 TRUE, DUPLICATE_SAME_ACCESS ) ) {
669 DEBUG1 ("DuplicateHandle failed: ec=%d\n", (int)GetLastError());
678 filedes[0] = handle_to_fd (r);
679 filedes[1] = handle_to_fd (w);
680 DEBUG5 ("CreatePipe %p %p %d %d inherit=%d\n", r, w,
681 filedes[0], filedes[1], inherit_idx );
686 _gpgme_io_close ( int fd )
689 void (*handler)(int, void*) = NULL;
695 DEBUG1 ("** closing handle for fd %d\n", fd);
698 LOCK (notify_table_lock);
699 for ( i=0; i < DIM (notify_table); i++ ) {
700 if (notify_table[i].inuse && notify_table[i].fd == fd) {
701 handler = notify_table[i].handler;
702 value = notify_table[i].value;
703 notify_table[i].handler = NULL;
704 notify_table[i].value = NULL;
705 notify_table[i].inuse = 0;
709 UNLOCK (notify_table_lock);
713 if ( !CloseHandle (fd_to_handle (fd)) ) {
714 DEBUG2 ("CloseHandle for fd %d failed: ec=%d\n",
715 fd, (int)GetLastError ());
723 _gpgme_io_set_close_notify (int fd, void (*handler)(int, void*), void *value)
729 LOCK (notify_table_lock);
730 for (i=0; i < DIM (notify_table); i++ ) {
731 if ( notify_table[i].inuse && notify_table[i].fd == fd )
734 if ( i == DIM (notify_table) ) {
735 for (i=0; i < DIM (notify_table); i++ ) {
736 if ( !notify_table[i].inuse )
740 if ( i == DIM (notify_table) ) {
741 UNLOCK (notify_table_lock);
744 notify_table[i].fd = fd;
745 notify_table[i].handler = handler;
746 notify_table[i].value = value;
747 notify_table[i].inuse = 1;
748 UNLOCK (notify_table_lock);
749 DEBUG2 ("set notification for fd %d (idx=%d)", fd, i );
755 _gpgme_io_set_nonblocking ( int fd )
762 build_commandline ( char **argv )
767 /* FIXME: we have to quote some things because under Windows the
768 * program parses the commandline and does some unquoting */
769 for (i=0; argv[i]; i++)
770 n += strlen (argv[i]) + 2 + 1; /* 2 extra bytes for possible quoting */
771 buf = p = malloc (n);
776 p = stpcpy (p, argv[0]);
777 for (i = 1; argv[i]; i++) {
779 p = stpcpy (p, " \"\"");
781 p = stpcpy (stpcpy (p, " "), argv[i]);
789 _gpgme_io_spawn ( const char *path, char **argv,
790 struct spawn_fd_item_s *fd_child_list,
791 struct spawn_fd_item_s *fd_parent_list )
793 SECURITY_ATTRIBUTES sec_attr;
794 PROCESS_INFORMATION pi = {
795 NULL, /* returns process handle */
796 0, /* returns primary thread handle */
801 char *envblock = NULL;
802 int cr_flags = CREATE_DEFAULT_ERROR_MODE
803 | GetPriorityClass (GetCurrentProcess ());
807 int duped_stderr = 0;
808 HANDLE hnul = INVALID_HANDLE_VALUE;
812 memset (&sec_attr, 0, sizeof sec_attr );
813 sec_attr.nLength = sizeof sec_attr;
814 sec_attr.bInheritHandle = FALSE;
816 arg_string = build_commandline ( argv );
820 memset (&si, 0, sizeof si);
822 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
823 si.wShowWindow = debug_me? SW_SHOW : SW_MINIMIZE;
824 si.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
825 si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
826 si.hStdError = GetStdHandle (STD_ERROR_HANDLE);
828 for (i=0; fd_child_list[i].fd != -1; i++ ) {
829 if (fd_child_list[i].dup_to == 0 ) {
830 si.hStdInput = fd_to_handle (fd_child_list[i].fd);
831 DEBUG1 ("using %d for stdin", fd_child_list[i].fd );
834 else if (fd_child_list[i].dup_to == 1 ) {
835 si.hStdOutput = fd_to_handle (fd_child_list[i].fd);
836 DEBUG1 ("using %d for stdout", fd_child_list[i].fd );
838 else if (fd_child_list[i].dup_to == 2 ) {
839 si.hStdError = fd_to_handle (fd_child_list[i].fd);
840 DEBUG1 ("using %d for stderr", fd_child_list[i].fd );
845 if( !duped_stdin || !duped_stderr ) {
846 SECURITY_ATTRIBUTES sa;
848 memset (&sa, 0, sizeof sa );
849 sa.nLength = sizeof sa;
850 sa.bInheritHandle = TRUE;
851 hnul = CreateFile ( "nul",
852 GENERIC_READ|GENERIC_WRITE,
853 FILE_SHARE_READ|FILE_SHARE_WRITE,
856 FILE_ATTRIBUTE_NORMAL,
858 if ( hnul == INVALID_HANDLE_VALUE ) {
859 DEBUG1 ("can't open `nul': ec=%d\n", (int)GetLastError ());
863 /* Make sure that the process has a connected stdin */
864 if ( !duped_stdin ) {
866 DEBUG1 ("using %d for dummy stdin", (int)hnul );
868 /* We normally don't want all the normal output */
869 if ( !duped_stderr ) {
871 DEBUG1 ("using %d for dummy stderr", (int)hnul );
875 DEBUG2 ("CreateProcess, path=`%s' args=`%s'", path, arg_string);
876 cr_flags |= CREATE_SUSPENDED;
877 if ( !CreateProcessA (path,
879 &sec_attr, /* process security attributes */
880 &sec_attr, /* thread security attributes */
881 TRUE, /* inherit handles */
882 cr_flags, /* creation flags */
883 envblock, /* environment */
884 NULL, /* use current drive/directory */
885 &si, /* startup information */
886 &pi /* returns process information */
888 DEBUG1 ("CreateProcess failed: ec=%d\n", (int) GetLastError ());
893 /* close the /dev/nul handle if used */
894 if (hnul != INVALID_HANDLE_VALUE ) {
895 if ( !CloseHandle ( hnul ) )
896 DEBUG1 ("CloseHandle(hnul) failed: ec=%d\n", (int)GetLastError());
899 /* Close the other ends of the pipes */
900 for (i = 0; fd_parent_list[i].fd != -1; i++)
901 _gpgme_io_close (fd_parent_list[i].fd);
903 DEBUG4 ("CreateProcess ready\n"
904 "- hProcess=%p hThread=%p\n"
905 "- dwProcessID=%d dwThreadId=%d\n",
906 pi.hProcess, pi.hThread,
907 (int) pi.dwProcessId, (int) pi.dwThreadId);
909 if ( ResumeThread ( pi.hThread ) < 0 ) {
910 DEBUG1 ("ResumeThread failed: ec=%d\n", (int)GetLastError ());
913 if ( !CloseHandle (pi.hThread) ) {
914 DEBUG1 ("CloseHandle of thread failed: ec=%d\n",
915 (int)GetLastError ());
918 return handle_to_pid (pi.hProcess);
925 _gpgme_io_waitpid ( int pid, int hang, int *r_status, int *r_signal )
927 HANDLE proc = fd_to_handle (pid);
933 code = WaitForSingleObject ( proc, hang? INFINITE : 0 );
936 DEBUG2 ("WFSO pid=%d failed: %d\n", (int)pid, (int)GetLastError () );
940 if (!GetExitCodeProcess (proc, &exc)) {
941 DEBUG2 ("** GECP pid=%d failed: ec=%d\n",
942 (int)pid, (int)GetLastError () );
946 DEBUG2 ("GECP pid=%d exit code=%d\n", (int)pid, exc);
954 DEBUG1 ("WFSO pid=%d timed out\n", (int)pid);
958 DEBUG2 ("WFSO pid=%d returned %d\n", (int)pid, code );
965 _gpgme_io_kill ( int pid, int hard )
967 HANDLE proc = fd_to_handle (pid);
969 #warning I am not sure how to kill a process
970 /* fixme: figure out how this can be done */
977 * Select on the list of fds.
978 * Returns: -1 = error
979 * 0 = timeout or nothing to select
980 * >0 = number of signaled fds
983 _gpgme_io_select ( struct io_select_fd_s *fds, size_t nfds, int nonblock )
985 HANDLE waitbuf[MAXIMUM_WAIT_OBJECTS];
986 int waitidx[MAXIMUM_WAIT_OBJECTS];
993 DEBUG_BEGIN (dbg_help, 3, "select on [ ");
997 for ( i=0; i < nfds; i++ ) {
998 if ( fds[i].fd == -1 )
1000 fds[i].signaled = 0;
1001 if ( fds[i].for_read || fds[i].for_write ) {
1002 if ( fds[i].frozen ) {
1003 DEBUG_ADD1 (dbg_help, "f%d ", fds[i].fd );
1005 else if ( fds[i].for_read ) {
1006 struct reader_context_s *c = find_reader (fds[i].fd,1);
1009 DEBUG1 ("oops: no reader thread for fd %d", fds[i].fd);
1012 if ( nwait >= DIM (waitbuf) ) {
1013 DEBUG_END (dbg_help, "oops ]");
1014 DEBUG0 ("Too many objects for WFMO!" );
1018 waitbuf[nwait++] = c->have_data_ev;
1020 DEBUG_ADD1 (dbg_help, "r%d ", fds[i].fd );
1023 else if ( fds[i].for_write ) {
1024 struct writer_context_s *c = find_writer (fds[i].fd,1);
1027 DEBUG1 ("oops: no writer thread for fd %d", fds[i].fd);
1030 if ( nwait >= DIM (waitbuf) ) {
1031 DEBUG_END (dbg_help, "oops ]");
1032 DEBUG0 ("Too many objects for WFMO!" );
1038 waitbuf[nwait++] = c->is_empty;
1039 DEBUG_ADD1 (dbg_help, "w%d ", fds[i].fd );
1043 DEBUG_ADD1 (dbg_help, "w%d(ignored) ", fds[i].fd );
1050 DEBUG_END (dbg_help, "]");
1054 code = WaitForMultipleObjects ( nwait, waitbuf, 0, nonblock ? 0 : 1000);
1055 if ( code >= WAIT_OBJECT_0 && code < WAIT_OBJECT_0 + nwait ) {
1056 /* This WFMO is a really silly function: It does return either
1057 * the index of the signaled object or if 2 objects have been
1058 * signalled at the same time, the index of the object with the
1059 * lowest object is returned - so and how do we find out
1060 * how many objects have been signaled???.
1061 * The only solution I can imagine is to test each object starting
1062 * with the returned index individually - how dull.
1065 for (i=code - WAIT_OBJECT_0; i < nwait; i++ ) {
1066 if (WaitForSingleObject ( waitbuf[i], NULL ) == WAIT_OBJECT_0) {
1067 assert (waitidx[i] >=0 && waitidx[i] < nfds);
1068 fds[waitidx[i]].signaled = 1;
1074 DEBUG0 ("Oops: No signaled objects found after WFMO");
1078 else if ( code == WAIT_TIMEOUT ) {
1079 DEBUG0 ("WFMO timed out\n" );
1081 else if (code == WAIT_FAILED ) {
1082 int le = (int)GetLastError ();
1083 if ( le == ERROR_INVALID_HANDLE ) {
1084 int k, j = handle_to_fd (waitbuf[i]);
1086 DEBUG1 ("WFMO invalid handle %d removed\n", j);
1087 for (k=0 ; k < nfds; i++ ) {
1088 if ( fds[k].fd == j ) {
1089 fds[k].for_read = fds[k].for_write = 0;
1093 DEBUG0 (" oops, or not???\n");
1095 DEBUG1 ("WFMO failed: %d\n", le );
1099 DEBUG1 ("WFMO returned %d\n", code );
1104 DEBUG_BEGIN (dbg_help, 3, " signaled [ ");
1105 for ( i=0; i < nfds; i++ ) {
1106 if ( fds[i].fd == -1 )
1108 if ( (fds[i].for_read || fds[i].for_write) && fds[i].signaled ) {
1109 DEBUG_ADD2 (dbg_help, "%c%d ",
1110 fds[i].for_read? 'r':'w',fds[i].fd );
1113 DEBUG_END (dbg_help, "]");