-
Notifications
You must be signed in to change notification settings - Fork 2
/
w32-pth.c
2822 lines (2383 loc) · 60.9 KB
/
w32-pth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* w32-pth.c - GNU Pth emulation for W32 (MS Windows).
* Copyright (c) 1999-2003 Ralf S. Engelschall <[email protected]>
* Copyright (C) 2004, 2006, 2007, 2008, 2010 g10 Code GmbH
*
* This file is part of W32PTH.
*
* W32PTH is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* W32PTH is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* ------------------------------------------------------------------
* This code is based on Ralf Engelschall's GNU Pth, a non-preemptive
* thread scheduling library which can be found at
* http://www.gnu.org/software/pth/. MS Windows (W32) specific code
* written by Timo Schulz, g10 Code.
*/
#include <config.h>
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif
#include <errno.h>
#include "utils.h"
#include "debug.h"
#include "w32-io.h"
/* We don't want to have any Windows specific code in the header, thus
we use a macro which defaults to a compatible type in w32-pth.h. */
#define W32_PTH_HANDLE_INTERNAL HANDLE
#include "pth.h"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#if FALSE != 0 || TRUE != 1
#error TRUE or FALSE defined to wrong values
#endif
#if SIZEOF_LONG_LONG != 8
#error long long is not 64 bit
#endif
/* FIXME: We can only undefine this when we have static thread-local
event allocation. */
#define NO_PTH_MODE_STATIC 1
/* States whether this module has been initialized. */
static int pth_initialized;
/* Debug helpers. */
int debug_level;
#ifdef HAVE_W32CE_SYSTEM
HANDLE dbghd = INVALID_HANDLE_VALUE;
#else
FILE *dbgfp;
#endif
/* Variables to support event handling. */
static int pth_signo;
static HANDLE pth_signo_ev;
/* Mutex to make sure only one thread is running. */
static CRITICAL_SECTION pth_shd;
/* A sentinel to catch bogus use of pth_enter/pth_leave. */
static int enter_leave_api_sentinel;
/* Counter to track the number of PTH threads. */
static int thread_counter;
/* Object used by update_fdarray. */
struct fdarray_item_s
{
int fd;
long netevents;
};
#ifdef HAVE_W32CE_SYSTEM
/* W32CE timer implementation. */
static HANDLE w32ce_timer_ev; /* Event to kick the timer thread. */
static CRITICAL_SECTION w32ce_timer_cs; /* Protect the objects below. */
static int w32ce_timer_thread_lauchend; /* True if the timer thread has
been launched. */
static struct /* The timer array. */
{
HANDLE event; /* Event to be signaled for this timer or
NULL for an unused slot. */
int active; /* Timer is active. */
unsigned long remaining; /* Remaining time in milliseconds. */
} w32ce_timer[32];
#endif /*HAVE_W32CE_SYSTEM*/
/* Pth events are store in a double linked event ring. */
struct pth_event_s
{
struct pth_event_s *next;
struct pth_event_s *prev;
HANDLE hd; /* The event object. Note that this is
also used directly for the
PTH_EVENT_HANDLE event. */
int u_type; /* The type of the event. */
union
{
int fd; /* Used for PTH_EVENT_FD. */
struct
{
int *rc;
fd_set *rfds;
fd_set *wfds;
fd_set *efds;
} sel; /* Used for PTH_EVENT_SELECT. */
struct
{
struct sigset_s *set;
int *signo;
} sig; /* Used for PTH_EVENT_SIGS. */
struct timeval tv; /* Used for PTH_EVENT_TIME. */
pth_mutex_t *mx; /* Used for PTH_EVENT_MUTEX. */
} u;
unsigned int flags; /* Flags used to further describe an event.
This is the bit wise combination of
PTH_MODE_* or PTH_UNTIL_*. */
pth_status_t status; /* Current status of the event. */
};
/* Attribute object for threads. */
struct pth_attr_s
{
unsigned int flags;
unsigned int stack_size;
char *name;
};
/* Object to keep information about a thread. This may eventually be
used to implement a scheduler queue. */
struct thread_info_s
{
void *(*thread)(void *); /* The actual thread fucntion. */
void * arg; /* The argument passed to that fucntion. */
int joinable; /* True if this Thread is joinable. */
HANDLE th; /* Handle of this thread. Used by non-joinable
threads to close the handle. */
};
/* Convenience macro to startup the system. */
#define implicit_init() do { if (!pth_initialized) pth_init(); } while (0)
/* Prototypes. */
static pth_event_t do_pth_event (unsigned long spec, ...);
static unsigned int do_pth_waitpid (unsigned pid, int * status, int options);
static int do_pth_wait (pth_event_t ev);
static void *launch_thread (void * ctx);
static int do_pth_event_free (pth_event_t ev, int mode);
/* Our own malloc function. Eventually we will use HeapCreate to use
a private heap here. */
void *
_pth_malloc (size_t n)
{
void *p;
p = malloc (n);
return p;
}
void *
_pth_calloc (size_t n, size_t m)
{
void *p;
p = calloc (n, m);
return p;
}
void
_pth_free (void *p)
{
if (p)
free (p);
}
/* Return a string from the Win32 Registry or NULL in case of error.
The returned string is allocated using a plain malloc and thus the
caller needs to call the standard free(). The string is looked up
under HKEY_LOCAL_MACHINE. */
#ifdef HAVE_W32CE_SYSTEM
static char *
w32_read_registry (const wchar_t *dir, const wchar_t *name)
{
HKEY handle;
DWORD n, nbytes;
wchar_t *buffer = NULL;
char *result = NULL;
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &handle))
{
return NULL; /* No need for a RegClose, so return immediately. */
}
nbytes = 1;
if (RegQueryValueEx (handle, name, 0, NULL, NULL, &nbytes))
{
goto leave;
}
buffer = malloc ((n=nbytes+2));
if (!buffer)
{
goto leave;
}
if (RegQueryValueEx (handle, name, 0, NULL, (PBYTE)buffer, &n))
{
free (buffer);
buffer = NULL;
goto leave;
}
n = WideCharToMultiByte (CP_UTF8, 0, buffer, nbytes, NULL, 0, NULL, NULL);
if (n < 0 || (n+1) <= 0)
{
goto leave;
}
result = malloc (n+1);
if (!result)
{
goto leave;
}
n = WideCharToMultiByte (CP_UTF8, 0, buffer, nbytes, result, n, NULL, NULL);
if (n < 0)
{
free (result);
result = NULL;
goto leave;
}
result[n] = 0;
leave:
free (buffer);
RegCloseKey (handle);
return result;
}
#endif /*HAVE_W32CE_SYSTEM*/
#ifdef HAVE_W32CE_SYSTEM
/* Replacement for getenv which takes care of the our use of getenv.
The code is not thread safe but we expect it to work in all cases
because it is called for the first time early enough. */
static char *
getenv (const char *name)
{
static int initialized;
static char *val_debug;
if (!initialized)
{
val_debug = w32_read_registry (L"\\Software\\GNU\\w32pth",
L"debug");
initialized = 1;
}
if (!strcmp (name, "PTH_DEBUG"))
return val_debug;
else
return NULL;
}
#endif /*HAVE_W32CE_SYSTEM*/
static char *
w32_strerror (char *strerr, size_t strerrsize)
{
#ifdef HAVE_W32CE_SYSTEM
/* There is only a wchar_t FormatMessage. It does not make much
sense to play the conversion game; we print only the code. */
snprintf (strerr, strerrsize, "ec=%d", (int)GetLastError ());
#else
if (strerrsize > 1)
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, (int)GetLastError (),
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
strerr, strerrsize, NULL);
#endif
return strerr;
}
static char *
wsa_strerror (char *strerr, size_t strerrsize)
{
#ifdef HAVE_W32CE_SYSTEM
snprintf (strerr, strerrsize, "ec=%d", (int)WSAGetLastError ());
#else
if (strerrsize > 1)
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, (int)WSAGetLastError (),
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
strerr, strerrsize, NULL);
#endif
return strerr;
}
int
map_wsa_to_errno (long wsa_err)
{
switch (wsa_err)
{
case 0:
return 0;
case WSAEINTR:
return EINTR;
case WSAEBADF:
return EBADF;
case WSAEACCES:
return EACCES;
case WSAEFAULT:
return EFAULT;
case WSAEINVAL:
return EINVAL;
case WSAEMFILE:
return EMFILE;
case WSAEWOULDBLOCK:
return EAGAIN;
case WSAENAMETOOLONG:
return ENAMETOOLONG;
case WSAENOTEMPTY:
return ENOTEMPTY;
default:
return EIO;
}
}
int
map_w32_to_errno (DWORD w32_err)
{
switch (w32_err)
{
case 0:
return 0;
case ERROR_FILE_NOT_FOUND:
return ENOENT;
case ERROR_PATH_NOT_FOUND:
return ENOENT;
case ERROR_ACCESS_DENIED:
return EPERM;
case ERROR_INVALID_HANDLE:
case ERROR_INVALID_BLOCK:
return EINVAL;
case ERROR_NOT_ENOUGH_MEMORY:
return ENOMEM;
case ERROR_NO_DATA:
return EPIPE;
default:
return EIO;
}
}
static int
fd_is_socket (int fd)
{
int is_socket;
int optval;
int optlen;
if (_pth_get_reader_ev (fd) != INVALID_HANDLE_VALUE
|| _pth_get_writer_ev (fd) != INVALID_HANDLE_VALUE)
is_socket = 0;
else
{
/* This implemenation strategy is taken from glib.
Unfortunately, it does not work with pipes, as getsockopt can
block on those. So we test for pipes above first. */
optlen = sizeof (optval);
is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE,
(char *) &optval, &optlen) != SOCKET_ERROR);
}
if (DBG_INFO)
_pth_debug (0, "fd_is_socket: fd %i is a %s.\n",
fd, is_socket ? "socket" : "file");
return is_socket;
}
/* Create a manual resetable event object useable in WFMO. */
static HANDLE
create_event (void)
{
SECURITY_ATTRIBUTES sa;
HANDLE h, h2;
char strerr[256];
memset (&sa, 0, sizeof sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof sa;
h = CreateEvent (&sa, TRUE, FALSE, NULL);
if (!h)
{
if (DBG_ERROR)
_pth_debug (0, "CreateEvent failed: %s\n",
w32_strerror (strerr, sizeof strerr));
return NULL;
}
#ifdef HAVE_W32CE_SYSTEM
h2 = h;
#else
if (!DuplicateHandle (GetCurrentProcess(), h,
GetCurrentProcess(), &h2,
EVENT_MODIFY_STATE|SYNCHRONIZE, FALSE, 0 ) )
{
if (DBG_ERROR)
_pth_debug (0, "setting synchronize for event object %p failed: %s\n",
h, w32_strerror (strerr, sizeof strerr));
CloseHandle (h);
return NULL;
}
CloseHandle (h);
#endif
if (DBG_INFO)
{
_pth_debug (0, "CreateEvent(%p) succeeded\n", h2);
}
return h2;
}
#ifdef TEST
static void
set_event (HANDLE h)
{
char strerr[256];
if (!SetEvent (h))
{
if (DBG_ERROR)
_pth_debug (0, "SetEvent(%p) failed: %s\n",
h, w32_strerror (strerr, sizeof strerr));
}
else if (DBG_INFO)
{
_pth_debug (0, "SetEvent(%p) succeeded\n", h);
}
}
#endif
static void
reset_event (HANDLE h)
{
char strerr[256];
if (!ResetEvent (h))
{
if (DBG_ERROR)
_pth_debug (0, "ResetEvent(%p) failed: %s\n",
h, w32_strerror (strerr, sizeof strerr));
}
else if (DBG_INFO)
{
_pth_debug (0, "ResetEvent(%p) succeeded\n", h);
}
}
#ifdef HAVE_W32CE_SYSTEM
/* WindowsCE does not provide CreateWaitableTimer et al. Thus we have
to roll our own. The timer is not fully correct because we use
GetTickCount and don't adjust for the time it takes to call it. */
static DWORD CALLBACK
w32ce_timer_thread (void *arg)
{
int idx;
DWORD timeout, elapsed, lasttick;
(void)arg;
lasttick = GetTickCount ();
for (;;)
{
elapsed = lasttick; /* Get start time. */
timeout = INFINITE;
EnterCriticalSection (&w32ce_timer_cs);
for (idx=0; idx < DIM (w32ce_timer); idx++)
{
if (w32ce_timer[idx].event && w32ce_timer[idx].active)
{
if (w32ce_timer[idx].remaining < timeout)
timeout = w32ce_timer[idx].remaining;
}
}
LeaveCriticalSection (&w32ce_timer_cs);
if (timeout != INFINITE && timeout > 0x7fffffff)
timeout = 0x7fffffff;
switch (WaitForSingleObject (w32ce_timer_ev, (DWORD)timeout))
{
case WAIT_OBJECT_0:
case WAIT_TIMEOUT:
break;
default:
if (DBG_ERROR)
_pth_debug (0,
"w32ce_timer_thread: WFSO failed: rc=%d\n",
(int)GetLastError ());
Sleep (500); /* Failsafe pause. */
break;
}
EnterCriticalSection (&w32ce_timer_cs);
lasttick = GetTickCount ();
elapsed = lasttick - elapsed;
for (idx=0; idx < DIM (w32ce_timer); idx++)
{
if (w32ce_timer[idx].event && w32ce_timer[idx].active)
{
if (w32ce_timer[idx].remaining > elapsed)
w32ce_timer[idx].remaining -= elapsed;
else
{
w32ce_timer[idx].remaining = 0;
w32ce_timer[idx].active = 0;
if (!SetEvent (w32ce_timer[idx].event))
{
if (DBG_ERROR)
_pth_debug (0, "w32ce_timer_thread: SetEvent(%p) "
"failed: rc=%d\n",
(int)GetLastError ());
}
}
}
}
LeaveCriticalSection (&w32ce_timer_cs);
}
return 0; /*NOTREACHED*/
}
#endif /*HAVE_W32CE_SYSTEM*/
/* Create a timer event. */
static HANDLE
create_timer (void)
{
HANDLE h;
#ifdef HAVE_W32CE_SYSTEM
int idx;
EnterCriticalSection (&w32ce_timer_cs);
if (!w32ce_timer_thread_lauchend)
{
h = CreateThread (NULL, 0, w32ce_timer_thread, NULL, 0, NULL);
if (!h)
{
if (DBG_ERROR)
_pth_debug (0, "create_timer: CreateThread failed: rc=%d\n",
(int)GetLastError ());
goto leave;
}
CloseHandle (h);
w32ce_timer_thread_lauchend = 1;
}
h = NULL;
for (idx = 0; idx < DIM (w32ce_timer); idx++)
if (!w32ce_timer[idx].event)
break;
if (idx == DIM (w32ce_timer))
{
SetLastError (ERROR_TOO_MANY_OPEN_FILES);
goto leave;
}
/* We create a manual reset event. */
w32ce_timer[idx].event = CreateEvent (NULL, TRUE, FALSE, NULL);
if (!w32ce_timer[idx].event)
goto leave;
w32ce_timer[idx].active = 0;
h = w32ce_timer[idx].event;
leave:
LeaveCriticalSection (&w32ce_timer_cs);
#else /* Plain Windows. */
SECURITY_ATTRIBUTES sa;
memset (&sa, 0, sizeof sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof sa;
h = CreateWaitableTimer (&sa, TRUE, NULL);
#endif /* Plain Windows. */
if (!h)
{
if (DBG_ERROR)
_pth_debug (0, "CreateWaitableTimer failed: rc=%d\n",
(int)GetLastError ());
}
else if (DBG_INFO)
{
_pth_debug (0, "CreateWaitableTimer(%p) succeeded\n",
h);
}
return h;
}
static int
set_timer (HANDLE hd, DWORD milliseconds)
{
#ifdef HAVE_W32CE_SYSTEM
int idx;
int result = -1;
EnterCriticalSection (&w32ce_timer_cs);
for (idx = 0; idx < DIM (w32ce_timer); idx++)
if (hd && w32ce_timer[idx].event == hd)
{
w32ce_timer[idx].remaining = milliseconds;
if (!ResetEvent (w32ce_timer[idx].event))
{
if (DBG_ERROR)
_pth_debug (0, "set_timer: ResetEvent(%p) failed: rc=%d\n",
(int)GetLastError ());
}
else if (!SetEvent (w32ce_timer_ev))
{
if (DBG_ERROR)
_pth_debug (0, "set_timer: SetEvent(%p) failed: rc=%d\n",
(int)GetLastError ());
}
else
{
w32ce_timer[idx].active = 1;
result = 0;
}
break;
}
if (idx == DIM (w32ce_timer))
SetLastError (ERROR_INVALID_HANDLE);
LeaveCriticalSection (&w32ce_timer_cs);
return result;
#else /* Plain Windows. */
LARGE_INTEGER ll;
char strerr[256];
if (DBG_CALLS)
_pth_debug (DEBUG_CALLS, "set_timer hd=%p ms=%lu\n",
hd, (unsigned long)milliseconds);
ll.QuadPart = ((unsigned long)milliseconds * ((long long)-10000LL));
if (!SetWaitableTimer (hd, &ll, 0, NULL, NULL, FALSE))
{
if (DBG_ERROR)
_pth_debug (0,"%s: SetWaitableTimer failed: %s\n",
__func__,
w32_strerror (strerr, sizeof strerr));
return -1;
}
return 0;
#endif /* Plain Windows. */
}
static void
destroy_timer (HANDLE h)
{
#ifdef HAVE_W32CE_SYSTEM
int idx;
EnterCriticalSection (&w32ce_timer_cs);
for (idx = 0; idx < DIM (w32ce_timer); idx++)
if (w32ce_timer[idx].event == h)
{
CloseHandle (w32ce_timer[idx].event);
w32ce_timer[idx].event = NULL;
break;
}
LeaveCriticalSection (&w32ce_timer_cs);
#else /* Plain Windows. */
CloseHandle (h);
#endif /* Plain Windows. */
}
int
pth_init (void)
{
WSADATA wsadat;
const char *s;
if (pth_initialized)
return TRUE;
_pth_sema_subsystem_init ();
s = getenv ("PTH_DEBUG");
if (s && (debug_level = atoi (s)))
{
#ifdef HAVE_W32CE_SYSTEM
ActivateDevice (L"Drivers\\GnuPG_Log", 0);
/* Ignore a filename and write the debug output to the GPG2:
device. */
dbghd = CreateFile (L"GPG2:", GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
#else
const char *s1, *s2;
char *p;
s1 = strchr (s, ';');
if (s1)
{
s1++;
if (!(s2 = strchr (s1, ';')))
s2 = s1 + strlen (s1);
p = _pth_malloc (s2 - s1 + 1);
if (p)
{
memcpy (p, s1, s2-s1);
p[s2-s1] = 0;
dbgfp = fopen (p, "a");
if (dbgfp)
setvbuf (dbgfp, NULL, _IOLBF, 0);
_pth_free (p);
}
}
#endif
}
#ifndef HAVE_W32CE_SYSTEM
if (!dbgfp)
dbgfp = stderr;
#endif
if (debug_level)
_pth_debug (DEBUG_ERROR, "pth_init called (level=%d)\n", debug_level);
if (WSAStartup (0x202, &wsadat))
return FALSE;
pth_signo = 0;
InitializeCriticalSection (&pth_shd);
if (pth_signo_ev)
CloseHandle (pth_signo_ev);
pth_signo_ev = create_event ();
if (!pth_signo_ev)
return FALSE;
#ifdef HAVE_W32CE_SYSTEM
InitializeCriticalSection (&w32ce_timer_cs);
w32ce_timer_ev = CreateEvent (NULL, FALSE, FALSE, NULL);
if (!w32ce_timer_ev)
return FALSE;
#endif /*HAVE_W32CE_SYSTEM*/
pth_initialized = 1;
thread_counter = 1;
EnterCriticalSection (&pth_shd);
return TRUE;
}
int
pth_kill (void)
{
pth_signo = 0;
if (pth_signo_ev)
{
CloseHandle (pth_signo_ev);
pth_signo_ev = NULL;
}
if (pth_initialized)
{
#ifdef HAVE_W32CE_SYSTEM
DeleteCriticalSection (&w32ce_timer_cs);
#endif /*HAVE_W32CE_SYSTEM*/
DeleteCriticalSection (&pth_shd);
}
WSACleanup ();
pth_initialized = 0;
return TRUE;
}
static void
enter_pth (const char *function)
{
/* Fixme: I am not sure whether the same thread my enter a critical
section twice. */
if (DBG_CALLS)
_pth_debug (DEBUG_CALLS, "enter_pth (%s)\n", function? function:"");
LeaveCriticalSection (&pth_shd);
}
static void
leave_pth (const char *function)
{
EnterCriticalSection (&pth_shd);
if (DBG_CALLS)
_pth_debug (DEBUG_CALLS, "leave_pth (%s)\n", function? function:"");
}
void
pth_enter (void)
{
implicit_init ();
if (enter_leave_api_sentinel)
{
fprintf (stderr, "pth_enter called while already in pth\n");
abort ();
}
enter_leave_api_sentinel++;
enter_pth (__FUNCTION__);
}
void
pth_leave (void)
{
leave_pth (__FUNCTION__);
if (enter_leave_api_sentinel != 1)
{
fprintf (stderr, "pth_leave was called while not in pth\n");
abort ();
}
enter_leave_api_sentinel--;
}
long
pth_ctrl (unsigned long query, ...)
{
implicit_init ();
switch (query)
{
case PTH_CTRL_GETAVLOAD:
case PTH_CTRL_GETPRIO:
case PTH_CTRL_GETNAME:
return -1;
case PTH_CTRL_GETTHREADS_NEW:
return 0; /* Not strictly correct. */
case PTH_CTRL_GETTHREADS_READY:
return thread_counter? (thread_counter-1):0;
case PTH_CTRL_GETTHREADS_RUNNING:
return thread_counter? 1:0;
case PTH_CTRL_GETTHREADS_WAITING:
return -1; /* We don't have this info. */
case PTH_CTRL_GETTHREADS_SUSPENDED:
return -1; /* We don't have this info. */
case PTH_CTRL_GETTHREADS_DEAD:
return 0;
case PTH_CTRL_GETTHREADS:
return thread_counter;
default:
return -1;
}
return 0;
}
pth_time_t
pth_timeout (long sec, long usec)
{
pth_time_t tvd;
tvd.tv_sec = sec;
tvd.tv_usec = usec;
return tvd;
}
/* Return true if HD refers to a socket. */
static int
is_socket_2 (int hd)
{
#ifdef HAVE_W32CE_SYSTEM
(void)hd;
return 1; /* Assume socket. */
#else
/* We need to figure out whether we are working on a socket or on a
handle. A trivial way would be to check for the return code of
recv and see if it is WSAENOTSOCK. However the recv may block
after the server process died and thus the destroy_reader will
hang. Another option is to use getsockopt to test whether it is
a socket. The bug here is that once a socket with a certain
values has been opened, closed and later a CreatePipe returned
the same value (i.e. handle), getsockopt still believes it is a
socket. What we do now is to use a combination of GetFileType
and GetNamedPipeInfo. The specs say that the latter may be used
on anonymous pipes as well. Note that there are claims that
since winsocket version 2 ReadFile may be used on a socket but
only if it is supported by the service provider. Tests on a
stock XP using a local TCP socket show that it does not work. */
DWORD dummyflags, dummyoutsize, dummyinsize, dummyinst;
if (GetFileType ((HANDLE)hd) == FILE_TYPE_PIPE
&& !GetNamedPipeInfo ((HANDLE)hd, &dummyflags, &dummyoutsize,
&dummyinsize, &dummyinst))
return 1; /* Function failed; thus we assume it is a socket. */
else
return 0; /* Success; this is not a socket. */
#endif
}
static int
pipe_is_not_connected (void)
{
#ifdef HAVE_W32CE_SYSTEM
switch (GetLastError ())
{
case ERROR_PIPE_NOT_CONNECTED:
/* This may happen while one pipe end is still dangling
because the child process has not yet completed the
pipe creation. */
case ERROR_BUSY:
/* Returned by the device manager? */
Sleep (100);
return 1;
}
#endif
return 0;
}
static int
do_pth_read (int fd, void * buffer, size_t size)
{
int n;
HANDLE hd;
int use_readfile = 0;
TRACE_BEG (DEBUG_INFO, "do_pth_read", fd);
/* We have to check for internal pipes first, as socket operations
can block on these. */
hd = _pth_get_reader_ev (fd);
TRACE_LOG1 (" hd=%p", hd);
if (hd != INVALID_HANDLE_VALUE)
n = _pth_io_read (fd, buffer, size);
else
{
if (is_socket_2 (fd))