Skip to content

Commit e319ec4

Browse files
committed
Simplify errno generating in thread testing program.
1 parent b33f78d commit e319ec4

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/test/thread/thread_test.c

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ main(int argc, char *argv[])
263263
}
264264
}
265265

266+
/*
267+
* func_call_1
268+
*/
266269
static void
267270
func_call_1(void)
268271
{
@@ -272,23 +275,38 @@ func_call_1(void)
272275
void *p;
273276
#endif
274277
#ifdef WIN32
275-
HANDLE h1, h2;
278+
HANDLE h1;
279+
#else
280+
int fd;
276281
#endif
277282

278283
unlink(TEMP_FILENAME_1);
279284

285+
/* Set errno = EEXIST */
286+
280287
/* create, then try to fail on exclusive create open */
281288
#ifdef WIN32
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);
284-
if (h1 == INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
289+
if ((h1 = CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL)) ==
290+
INVALID_HANDLE_VALUE)
291+
#else
292+
if ((fd = open(TEMP_FILENAME_1, O_RDWR | O_CREAT, 0600)) < 0)
293+
#endif
294+
{
295+
fprintf(stderr, "Could not create file %s in current directory\n",
296+
TEMP_FILENAME_1);
297+
exit(1);
298+
}
299+
300+
#ifdef WIN32
301+
if (CreateFile(TEMP_FILENAME_1, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL)
302+
!= INVALID_HANDLE_VALUE || GetLastError() != ERROR_FILE_EXISTS)
285303
#else
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)
304+
if (open(TEMP_FILENAME_1, O_RDWR | O_CREAT | O_EXCL, 0600) >= 0)
288305
#endif
289306
{
290-
fprintf(stderr, "Could not create file in current directory or\n");
291-
fprintf(stderr, "could not generate failure for create file in current directory **\nexiting\n");
307+
fprintf(stderr,
308+
"Could not generate failure for exclusive file create of %s in current directory **\nexiting\n",
309+
TEMP_FILENAME_1);
292310
exit(1);
293311
}
294312

@@ -315,6 +333,11 @@ func_call_1(void)
315333
exit(1);
316334
}
317335

336+
#ifdef WIN32
337+
CloseHandle(h1);
338+
#else
339+
close(fd);
340+
#endif
318341
unlink(TEMP_FILENAME_1);
319342

320343
#ifndef HAVE_STRERROR_R
@@ -352,6 +375,9 @@ func_call_1(void)
352375
}
353376

354377

378+
/*
379+
* func_call_2
380+
*/
355381
static void
356382
func_call_2(void)
357383
{
@@ -363,15 +389,14 @@ func_call_2(void)
363389

364390
unlink(TEMP_FILENAME_2);
365391

366-
/* open non-existant file */
367-
#ifdef WIN32
368-
CreateFile(TEMP_FILENAME_2, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
369-
if (GetLastError() != ERROR_FILE_NOT_FOUND)
370-
#else
371-
if (open(TEMP_FILENAME_2, O_RDONLY, 0600) >= 0)
372-
#endif
392+
/* Set errno = ENOENT */
393+
394+
/* This will fail, but we can't check errno yet */
395+
if (unlink(TEMP_FILENAME_2) != -1)
373396
{
374-
fprintf(stderr, "Read-only open succeeded without create **\nexiting\n");
397+
fprintf(stderr,
398+
"Could not generate failure for unlink of %s in current directory **\nexiting\n",
399+
TEMP_FILENAME_2);
375400
exit(1);
376401
}
377402

@@ -394,12 +419,9 @@ func_call_2(void)
394419
#else
395420
fprintf(stderr, "errno not thread-safe **\nexiting\n");
396421
#endif
397-
unlink(TEMP_FILENAME_2);
398422
exit(1);
399423
}
400424

401-
unlink(TEMP_FILENAME_2);
402-
403425
#ifndef HAVE_STRERROR_R
404426
/*
405427
* If strerror() uses sys_errlist, the pointer might change for different

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy