@@ -52,41 +52,18 @@ typedef char bool;
52
52
#include <sys/param.h>
53
53
#endif
54
54
55
- /******************************************************************
56
- * Windows Hacks
57
- *****************************************************************/
58
-
59
55
#ifdef WIN32
60
56
#define MAXHOSTNAMELEN 63
61
57
#include <winsock2.h>
62
-
63
- int mkstemp (char * template );
64
-
65
- int
66
- mkstemp (char * template )
67
- {
68
- FILE * foo ;
69
-
70
- mktemp (template );
71
- foo = fopen (template , "rw" );
72
- if (!foo )
73
- return -1 ;
74
- else
75
- return (int ) foo ;
76
- }
77
58
#endif
78
59
79
- /******************************************************************
80
- * End Windows Hacks
81
- *****************************************************************/
82
-
83
60
84
61
/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
85
62
#include <signal.h>
86
63
int sigwait (const sigset_t * set , int * sig );
87
64
88
65
89
- #if !defined(ENABLE_THREAD_SAFETY ) && !defined(IN_CONFIGURE ) && !( defined(WIN32 ) )
66
+ #if !defined(ENABLE_THREAD_SAFETY ) && !defined(IN_CONFIGURE ) && !defined(WIN32 )
90
67
int
91
68
main (int argc , char * argv [])
92
69
{
@@ -99,20 +76,12 @@ main(int argc, char *argv[])
99
76
/* This must be down here because this is the code that uses threads. */
100
77
#include <pthread.h>
101
78
79
+ #define TEMP_FILENAME_1 "thread_test.1"
80
+ #define TEMP_FILENAME_2 "thread_test.2"
81
+
102
82
static void func_call_1 (void );
103
83
static void func_call_2 (void );
104
84
105
- #ifdef WIN32
106
- #define TEMP_FILENAME_1 "thread_test.1.XXXXXX"
107
- #define TEMP_FILENAME_2 "thread_test.2.XXXXXX"
108
- #else
109
- #define TEMP_FILENAME_1 "/tmp/thread_test.1.XXXXXX"
110
- #define TEMP_FILENAME_2 "/tmp/thread_test.2.XXXXXX"
111
- #endif
112
-
113
- static char * temp_filename_1 ;
114
- static char * temp_filename_2 ;
115
-
116
85
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER ;
117
86
118
87
static volatile int thread1_done = 0 ;
@@ -127,13 +96,11 @@ static char *strerror_p2;
127
96
static bool strerror_threadsafe = false;
128
97
#endif
129
98
130
- #ifndef WIN32
131
- #ifndef HAVE_GETPWUID_R
99
+ #if !defined(WIN32 ) && !defined(HAVE_GETPWUID_R )
132
100
static struct passwd * passwd_p1 ;
133
101
static struct passwd * passwd_p2 ;
134
102
static bool getpwuid_threadsafe = false;
135
103
#endif
136
- #endif
137
104
138
105
#if !defined(HAVE_GETADDRINFO ) && !defined(HAVE_GETHOSTBYNAME_R )
139
106
static struct hostent * hostent_p1 ;
@@ -147,11 +114,8 @@ static bool platform_is_threadsafe = true;
147
114
int
148
115
main (int argc , char * argv [])
149
116
{
150
- pthread_t thread1 ,
151
- thread2 ;
152
- int fd ;
117
+ pthread_t thread1 , thread2 ;
153
118
int rc ;
154
-
155
119
#ifdef WIN32
156
120
WSADATA wsaData ;
157
121
int err ;
@@ -178,17 +142,6 @@ main(int argc, char *argv[])
178
142
}
179
143
#endif
180
144
181
- /* Make temp filenames, might not have strdup() */
182
- temp_filename_1 = malloc (strlen (TEMP_FILENAME_1 ) + 1 );
183
- strcpy (temp_filename_1 , TEMP_FILENAME_1 );
184
- fd = mkstemp (temp_filename_1 );
185
- close (fd );
186
-
187
- temp_filename_2 = malloc (strlen (TEMP_FILENAME_2 ) + 1 );
188
- strcpy (temp_filename_2 , TEMP_FILENAME_2 );
189
- fd = mkstemp (temp_filename_2 );
190
- close (fd );
191
-
192
145
#if !defined(HAVE_GETADDRINFO ) && !defined(HAVE_GETHOSTBYNAME_R )
193
146
if (gethostname (myhostname , MAXHOSTNAMELEN ) != 0 )
194
147
{
@@ -212,14 +165,18 @@ main(int argc, char *argv[])
212
165
{
213
166
/*
214
167
* strerror() might not be thread-safe, and we already spawned thread
215
- * 1 that uses it
168
+ * 1 that uses it, so avoid using it.
216
169
*/
217
170
fprintf (stderr , "Failed to create thread 2 **\nexiting\n" );
218
171
exit (1 );
219
172
}
220
173
221
174
while (thread1_done == 0 || thread2_done == 0 )
222
175
sched_yield (); /* if this is a portability problem, remove it */
176
+
177
+ /* Test things while we have thread-local storage */
178
+
179
+ /* If we got here, we didn't exit() from a thread */
223
180
#ifdef WIN32
224
181
printf ("Your GetLastError() is thread-safe.\n" );
225
182
#else
@@ -231,23 +188,25 @@ main(int argc, char *argv[])
231
188
strerror_threadsafe = true;
232
189
#endif
233
190
234
- #ifndef WIN32
235
- #ifndef HAVE_GETPWUID_R
191
+ #if !defined(WIN32 ) && !defined(HAVE_GETPWUID_R )
236
192
if (passwd_p1 != passwd_p2 )
237
193
getpwuid_threadsafe = true;
238
194
#endif
239
- #endif
240
195
241
196
#if !defined(HAVE_GETADDRINFO ) && !defined(HAVE_GETHOSTBYNAME_R )
242
197
if (hostent_p1 != hostent_p2 )
243
198
gethostbyname_threadsafe = true;
244
199
#endif
245
200
201
+ /* close down threads */
202
+
246
203
pthread_mutex_unlock (& init_mutex ); /* let children exit */
247
204
248
205
pthread_join (thread1 , NULL ); /* clean up children */
249
206
pthread_join (thread2 , NULL );
250
207
208
+ /* report results */
209
+
251
210
#ifdef HAVE_STRERROR_R
252
211
printf ("Your system has sterror_r(); it does not need strerror().\n" );
253
212
#else
@@ -261,8 +220,9 @@ main(int argc, char *argv[])
261
220
}
262
221
#endif
263
222
264
- #ifndef WIN32
265
- #ifdef HAVE_GETPWUID_R
223
+ #ifdef WIN32
224
+ printf ("getpwuid_r()/getpwuid() are not applicable to Win32 platforms.\n" );
225
+ #elif defined(HAVE_GETPWUID_R )
266
226
printf ("Your system has getpwuid_r(); it does not need getpwuid().\n" );
267
227
#else
268
228
printf ("Your system uses getpwuid() which is " );
@@ -274,15 +234,11 @@ main(int argc, char *argv[])
274
234
platform_is_threadsafe = false;
275
235
}
276
236
#endif
277
- #else
278
- printf ("getpwuid_r()/getpwuid() are not applicable to Win32 platforms.\n" );
279
- #endif
280
237
281
238
#ifdef HAVE_GETADDRINFO
282
239
printf ("Your system has getaddrinfo(); it does not need gethostbyname()\n"
283
240
" or gethostbyname_r().\n" );
284
- #else
285
- #ifdef HAVE_GETHOSTBYNAME_R
241
+ #elif defined(HAVE_GETHOSTBYNAME_R )
286
242
printf ("Your system has gethostbyname_r(); it does not need gethostbyname().\n" );
287
243
#else
288
244
printf ("Your system uses gethostbyname which is " );
@@ -293,7 +249,6 @@ main(int argc, char *argv[])
293
249
printf ("not thread-safe. **\n" );
294
250
platform_is_threadsafe = false;
295
251
}
296
- #endif
297
252
#endif
298
253
299
254
if (platform_is_threadsafe )
@@ -317,29 +272,23 @@ func_call_1(void)
317
272
void * p ;
318
273
#endif
319
274
#ifdef WIN32
320
- HANDLE h1 ;
321
- HANDLE h2 ;
275
+ HANDLE h1 , h2 ;
322
276
#endif
323
- unlink (temp_filename_1 );
324
277
278
+ unlink (TEMP_FILENAME_1 );
325
279
326
280
/* create, then try to fail on exclusive create open */
327
281
#ifdef WIN32
328
- h1 = CreateFile (temp_filename_1 , GENERIC_WRITE , 0 , NULL , OPEN_ALWAYS , 0 , NULL );
329
- h2 = CreateFile (temp_filename_1 , GENERIC_WRITE , 0 , NULL , CREATE_NEW , 0 , NULL );
282
+ h1 = CreateFile (TEMP_FILENAME_1 , GENERIC_WRITE , 0 , NULL , OPEN_ALWAYS , 0 , NULL );
283
+ h2 = CreateFile (TEMP_FILENAME_1 , GENERIC_WRITE , 0 , NULL , CREATE_NEW , 0 , NULL );
330
284
if (h1 == INVALID_HANDLE_VALUE || GetLastError () != ERROR_FILE_EXISTS )
331
285
#else
332
- if (open (temp_filename_1 , O_RDWR | O_CREAT , 0600 ) < 0 ||
333
- open (temp_filename_1 , O_RDWR | O_CREAT | O_EXCL , 0600 ) >= 0 )
286
+ if (open (TEMP_FILENAME_1 , O_RDWR | O_CREAT , 0600 ) < 0 ||
287
+ open (TEMP_FILENAME_1 , O_RDWR | O_CREAT | O_EXCL , 0600 ) >= 0 )
334
288
#endif
335
289
{
336
- #ifdef WIN32
337
290
fprintf (stderr , "Could not create file in current directory or\n" );
338
- fprintf (stderr , "Could not generate failure for create file in current directory **\nexiting\n" );
339
- #else
340
- fprintf (stderr , "Could not create file in /tmp or\n" );
341
- fprintf (stderr , "Could not generate failure for create file in /tmp **\nexiting\n" );
342
- #endif
291
+ fprintf (stderr , "could not generate failure for create file in current directory **\nexiting\n" );
343
292
exit (1 );
344
293
}
345
294
@@ -350,6 +299,7 @@ func_call_1(void)
350
299
errno1_set = 1 ;
351
300
while (errno2_set == 0 )
352
301
sched_yield ();
302
+
353
303
#ifdef WIN32
354
304
if (GetLastError () != ERROR_FILE_EXISTS )
355
305
#else
@@ -361,22 +311,21 @@ func_call_1(void)
361
311
#else
362
312
fprintf (stderr , "errno not thread-safe **\nexiting\n" );
363
313
#endif
364
- unlink (temp_filename_1 );
314
+ unlink (TEMP_FILENAME_1 );
365
315
exit (1 );
366
316
}
367
- unlink (temp_filename_1 );
368
317
369
- #ifndef HAVE_STRERROR_R
370
- strerror_p1 = strerror (EACCES );
318
+ unlink (TEMP_FILENAME_1 );
371
319
320
+ #ifndef HAVE_STRERROR_R
372
321
/*
373
322
* If strerror() uses sys_errlist, the pointer might change for different
374
323
* errno values, so we don't check to see if it varies within the thread.
375
324
*/
325
+ strerror_p1 = strerror (EACCES );
376
326
#endif
377
327
378
- #ifndef WIN32
379
- #ifndef HAVE_GETPWUID_R
328
+ #if !defined(WIN32 ) && !defined(HAVE_GETPWUID_R )
380
329
passwd_p1 = getpwuid (0 );
381
330
p = getpwuid (1 );
382
331
if (passwd_p1 != p )
@@ -385,7 +334,6 @@ func_call_1(void)
385
334
passwd_p1 = NULL ; /* force thread-safe failure report */
386
335
}
387
336
#endif
388
- #endif
389
337
390
338
#if !defined(HAVE_GETADDRINFO ) && !defined(HAVE_GETHOSTBYNAME_R )
391
339
/* threads do this in opposite order */
@@ -413,13 +361,14 @@ func_call_2(void)
413
361
void * p ;
414
362
#endif
415
363
416
- unlink (temp_filename_2 );
364
+ unlink (TEMP_FILENAME_2 );
365
+
417
366
/* open non-existant file */
418
367
#ifdef WIN32
419
- CreateFile (temp_filename_2 , GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , NULL );
368
+ CreateFile (TEMP_FILENAME_2 , GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , NULL );
420
369
if (GetLastError () != ERROR_FILE_NOT_FOUND )
421
370
#else
422
- if (open (temp_filename_2 , O_RDONLY , 0600 ) >= 0 )
371
+ if (open (TEMP_FILENAME_2 , O_RDONLY , 0600 ) >= 0 )
423
372
#endif
424
373
{
425
374
fprintf (stderr , "Read-only open succeeded without create **\nexiting\n" );
@@ -433,6 +382,7 @@ func_call_2(void)
433
382
errno2_set = 1 ;
434
383
while (errno1_set == 0 )
435
384
sched_yield ();
385
+
436
386
#ifdef WIN32
437
387
if (GetLastError () != ENOENT )
438
388
#else
@@ -444,22 +394,21 @@ func_call_2(void)
444
394
#else
445
395
fprintf (stderr , "errno not thread-safe **\nexiting\n" );
446
396
#endif
447
- unlink (temp_filename_2 );
397
+ unlink (TEMP_FILENAME_2 );
448
398
exit (1 );
449
399
}
450
- unlink (temp_filename_2 );
451
400
452
- #ifndef HAVE_STRERROR_R
453
- strerror_p2 = strerror (EINVAL );
401
+ unlink (TEMP_FILENAME_2 );
454
402
403
+ #ifndef HAVE_STRERROR_R
455
404
/*
456
405
* If strerror() uses sys_errlist, the pointer might change for different
457
406
* errno values, so we don't check to see if it varies within the thread.
458
407
*/
408
+ strerror_p2 = strerror (EINVAL );
459
409
#endif
460
410
461
- #ifndef WIN32
462
- #ifndef HAVE_GETPWUID_R
411
+ #if !defined(WIN32 ) && !defined(HAVE_GETPWUID_R )
463
412
passwd_p2 = getpwuid (2 );
464
413
p = getpwuid (3 );
465
414
if (passwd_p2 != p )
@@ -468,7 +417,6 @@ func_call_2(void)
468
417
passwd_p2 = NULL ; /* force thread-safe failure report */
469
418
}
470
419
#endif
471
- #endif
472
420
473
421
#if !defined(HAVE_GETADDRINFO ) && !defined(HAVE_GETHOSTBYNAME_R )
474
422
/* threads do this in opposite order */
0 commit comments