Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6cc08e7

Browse files
committed
Have thread_test create its test files in the current directory, rather
than /tmp. Also cleanup C defines and add comments. Per report by Alex Soto
1 parent 08e1eed commit 6cc08e7

File tree

1 file changed

+43
-95
lines changed

1 file changed

+43
-95
lines changed

src/test/thread/thread_test.c

Lines changed: 43 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,18 @@ typedef char bool;
5252
#include <sys/param.h>
5353
#endif
5454

55-
/******************************************************************
56-
* Windows Hacks
57-
*****************************************************************/
58-
5955
#ifdef WIN32
6056
#define MAXHOSTNAMELEN 63
6157
#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-
}
7758
#endif
7859

79-
/******************************************************************
80-
* End Windows Hacks
81-
*****************************************************************/
82-
8360

8461
/* Test for POSIX.1c 2-arg sigwait() and fail on single-arg version */
8562
#include <signal.h>
8663
int sigwait(const sigset_t *set, int *sig);
8764

8865

89-
#if !defined(ENABLE_THREAD_SAFETY) && !defined(IN_CONFIGURE) && !(defined(WIN32))
66+
#if !defined(ENABLE_THREAD_SAFETY) && !defined(IN_CONFIGURE) && !defined(WIN32)
9067
int
9168
main(int argc, char *argv[])
9269
{
@@ -99,20 +76,12 @@ main(int argc, char *argv[])
9976
/* This must be down here because this is the code that uses threads. */
10077
#include <pthread.h>
10178

79+
#define TEMP_FILENAME_1 "thread_test.1"
80+
#define TEMP_FILENAME_2 "thread_test.2"
81+
10282
static void func_call_1(void);
10383
static void func_call_2(void);
10484

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-
11685
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
11786

11887
static volatile int thread1_done = 0;
@@ -127,13 +96,11 @@ static char *strerror_p2;
12796
static bool strerror_threadsafe = false;
12897
#endif
12998

130-
#ifndef WIN32
131-
#ifndef HAVE_GETPWUID_R
99+
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
132100
static struct passwd *passwd_p1;
133101
static struct passwd *passwd_p2;
134102
static bool getpwuid_threadsafe = false;
135103
#endif
136-
#endif
137104

138105
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
139106
static struct hostent *hostent_p1;
@@ -147,11 +114,8 @@ static bool platform_is_threadsafe = true;
147114
int
148115
main(int argc, char *argv[])
149116
{
150-
pthread_t thread1,
151-
thread2;
152-
int fd;
117+
pthread_t thread1, thread2;
153118
int rc;
154-
155119
#ifdef WIN32
156120
WSADATA wsaData;
157121
int err;
@@ -178,17 +142,6 @@ main(int argc, char *argv[])
178142
}
179143
#endif
180144

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-
192145
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
193146
if (gethostname(myhostname, MAXHOSTNAMELEN) != 0)
194147
{
@@ -212,14 +165,18 @@ main(int argc, char *argv[])
212165
{
213166
/*
214167
* 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.
216169
*/
217170
fprintf(stderr, "Failed to create thread 2 **\nexiting\n");
218171
exit(1);
219172
}
220173

221174
while (thread1_done == 0 || thread2_done == 0)
222175
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 */
223180
#ifdef WIN32
224181
printf("Your GetLastError() is thread-safe.\n");
225182
#else
@@ -231,23 +188,25 @@ main(int argc, char *argv[])
231188
strerror_threadsafe = true;
232189
#endif
233190

234-
#ifndef WIN32
235-
#ifndef HAVE_GETPWUID_R
191+
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
236192
if (passwd_p1 != passwd_p2)
237193
getpwuid_threadsafe = true;
238194
#endif
239-
#endif
240195

241196
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
242197
if (hostent_p1 != hostent_p2)
243198
gethostbyname_threadsafe = true;
244199
#endif
245200

201+
/* close down threads */
202+
246203
pthread_mutex_unlock(&init_mutex); /* let children exit */
247204

248205
pthread_join(thread1, NULL); /* clean up children */
249206
pthread_join(thread2, NULL);
250207

208+
/* report results */
209+
251210
#ifdef HAVE_STRERROR_R
252211
printf("Your system has sterror_r(); it does not need strerror().\n");
253212
#else
@@ -261,8 +220,9 @@ main(int argc, char *argv[])
261220
}
262221
#endif
263222

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)
266226
printf("Your system has getpwuid_r(); it does not need getpwuid().\n");
267227
#else
268228
printf("Your system uses getpwuid() which is ");
@@ -274,15 +234,11 @@ main(int argc, char *argv[])
274234
platform_is_threadsafe = false;
275235
}
276236
#endif
277-
#else
278-
printf("getpwuid_r()/getpwuid() are not applicable to Win32 platforms.\n");
279-
#endif
280237

281238
#ifdef HAVE_GETADDRINFO
282239
printf("Your system has getaddrinfo(); it does not need gethostbyname()\n"
283240
" or gethostbyname_r().\n");
284-
#else
285-
#ifdef HAVE_GETHOSTBYNAME_R
241+
#elif defined(HAVE_GETHOSTBYNAME_R)
286242
printf("Your system has gethostbyname_r(); it does not need gethostbyname().\n");
287243
#else
288244
printf("Your system uses gethostbyname which is ");
@@ -293,7 +249,6 @@ main(int argc, char *argv[])
293249
printf("not thread-safe. **\n");
294250
platform_is_threadsafe = false;
295251
}
296-
#endif
297252
#endif
298253

299254
if (platform_is_threadsafe)
@@ -317,29 +272,23 @@ func_call_1(void)
317272
void *p;
318273
#endif
319274
#ifdef WIN32
320-
HANDLE h1;
321-
HANDLE h2;
275+
HANDLE h1, h2;
322276
#endif
323-
unlink(temp_filename_1);
324277

278+
unlink(TEMP_FILENAME_1);
325279

326280
/* create, then try to fail on exclusive create open */
327281
#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);
330284
if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
331285
#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)
334288
#endif
335289
{
336-
#ifdef WIN32
337290
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");
343292
exit(1);
344293
}
345294

@@ -350,6 +299,7 @@ func_call_1(void)
350299
errno1_set = 1;
351300
while (errno2_set == 0)
352301
sched_yield();
302+
353303
#ifdef WIN32
354304
if (GetLastError() != ERROR_FILE_EXISTS)
355305
#else
@@ -361,22 +311,21 @@ func_call_1(void)
361311
#else
362312
fprintf(stderr, "errno not thread-safe **\nexiting\n");
363313
#endif
364-
unlink(temp_filename_1);
314+
unlink(TEMP_FILENAME_1);
365315
exit(1);
366316
}
367-
unlink(temp_filename_1);
368317

369-
#ifndef HAVE_STRERROR_R
370-
strerror_p1 = strerror(EACCES);
318+
unlink(TEMP_FILENAME_1);
371319

320+
#ifndef HAVE_STRERROR_R
372321
/*
373322
* If strerror() uses sys_errlist, the pointer might change for different
374323
* errno values, so we don't check to see if it varies within the thread.
375324
*/
325+
strerror_p1 = strerror(EACCES);
376326
#endif
377327

378-
#ifndef WIN32
379-
#ifndef HAVE_GETPWUID_R
328+
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
380329
passwd_p1 = getpwuid(0);
381330
p = getpwuid(1);
382331
if (passwd_p1 != p)
@@ -385,7 +334,6 @@ func_call_1(void)
385334
passwd_p1 = NULL; /* force thread-safe failure report */
386335
}
387336
#endif
388-
#endif
389337

390338
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
391339
/* threads do this in opposite order */
@@ -413,13 +361,14 @@ func_call_2(void)
413361
void *p;
414362
#endif
415363

416-
unlink(temp_filename_2);
364+
unlink(TEMP_FILENAME_2);
365+
417366
/* open non-existant file */
418367
#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);
420369
if (GetLastError() != ERROR_FILE_NOT_FOUND)
421370
#else
422-
if (open(temp_filename_2, O_RDONLY, 0600) >= 0)
371+
if (open(TEMP_FILENAME_2, O_RDONLY, 0600) >= 0)
423372
#endif
424373
{
425374
fprintf(stderr, "Read-only open succeeded without create **\nexiting\n");
@@ -433,6 +382,7 @@ func_call_2(void)
433382
errno2_set = 1;
434383
while (errno1_set == 0)
435384
sched_yield();
385+
436386
#ifdef WIN32
437387
if (GetLastError() != ENOENT)
438388
#else
@@ -444,22 +394,21 @@ func_call_2(void)
444394
#else
445395
fprintf(stderr, "errno not thread-safe **\nexiting\n");
446396
#endif
447-
unlink(temp_filename_2);
397+
unlink(TEMP_FILENAME_2);
448398
exit(1);
449399
}
450-
unlink(temp_filename_2);
451400

452-
#ifndef HAVE_STRERROR_R
453-
strerror_p2 = strerror(EINVAL);
401+
unlink(TEMP_FILENAME_2);
454402

403+
#ifndef HAVE_STRERROR_R
455404
/*
456405
* If strerror() uses sys_errlist, the pointer might change for different
457406
* errno values, so we don't check to see if it varies within the thread.
458407
*/
408+
strerror_p2 = strerror(EINVAL);
459409
#endif
460410

461-
#ifndef WIN32
462-
#ifndef HAVE_GETPWUID_R
411+
#if !defined(WIN32) && !defined(HAVE_GETPWUID_R)
463412
passwd_p2 = getpwuid(2);
464413
p = getpwuid(3);
465414
if (passwd_p2 != p)
@@ -468,7 +417,6 @@ func_call_2(void)
468417
passwd_p2 = NULL; /* force thread-safe failure report */
469418
}
470419
#endif
471-
#endif
472420

473421
#if !defined(HAVE_GETADDRINFO) && !defined(HAVE_GETHOSTBYNAME_R)
474422
/* threads do this in opposite order */

0 commit comments

Comments
 (0)