From de21c6ec21e1a745fd5d9c4878218fb4d07f0737 Mon Sep 17 00:00:00 2001 From: guenther <> Date: Sun, 20 Jul 2014 01:38:40 +0000 Subject: Make sure the correct errno is reported by warn* or err* and not the errno of an intervening cleanup operation like close/unlink/etc. Diff from Doug Hogan (doug (at) acyclic.org) --- src/regress/lib/libc/stdio_threading/fgetln/fgetln_test.c | 3 ++- src/regress/lib/libc/stdio_threading/fgets/fgets_test.c | 3 ++- src/regress/lib/libc/stdio_threading/fputs/fputs_test.c | 3 ++- src/regress/lib/libc/stdio_threading/fread/fread_test.c | 3 ++- src/regress/lib/libc/stdio_threading/fwrite/fwrite_test.c | 3 ++- src/regress/lib/libc/stdio_threading/include/local.h | 1 + src/usr.bin/nc/netcat.c | 5 +++-- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/regress/lib/libc/stdio_threading/fgetln/fgetln_test.c b/src/regress/lib/libc/stdio_threading/fgetln/fgetln_test.c index 0c815838d0..76d154bb2a 100755 --- a/src/regress/lib/libc/stdio_threading/fgetln/fgetln_test.c +++ b/src/regress/lib/libc/stdio_threading/fgetln/fgetln_test.c @@ -49,11 +49,12 @@ main(void) strlcpy(sfn, "/tmp/barnacles.XXXXXXXX", sizeof(sfn)); if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) { + int saved_errno = errno; if (fd != -1) { unlink(sfn); close(fd); } - err(1, "could not open temporary file"); + errc(1, saved_errno, "could not open temporary file"); } for (i = 0; i < 4096 * THREAD_COUNT; i++) diff --git a/src/regress/lib/libc/stdio_threading/fgets/fgets_test.c b/src/regress/lib/libc/stdio_threading/fgets/fgets_test.c index c53abbc06b..7c5008e2ad 100755 --- a/src/regress/lib/libc/stdio_threading/fgets/fgets_test.c +++ b/src/regress/lib/libc/stdio_threading/fgets/fgets_test.c @@ -48,11 +48,12 @@ main(void) strlcpy(sfn, "/tmp/barnacles.XXXXXXXX", sizeof(sfn)); if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) { + int saved_errno = errno; if (fd != -1) { unlink(sfn); close(fd); } - err(1, "could not open temporary file"); + errc(1, saved_errno, "could not open temporary file"); } for (i = 0; i < 4096 * THREAD_COUNT; i++) diff --git a/src/regress/lib/libc/stdio_threading/fputs/fputs_test.c b/src/regress/lib/libc/stdio_threading/fputs/fputs_test.c index 90b6179fd5..93d0f0b6c7 100755 --- a/src/regress/lib/libc/stdio_threading/fputs/fputs_test.c +++ b/src/regress/lib/libc/stdio_threading/fputs/fputs_test.c @@ -46,11 +46,12 @@ main(void) strlcpy(sfn, "/tmp/barnacles.XXXXXXXX", sizeof(sfn)); if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) { + int saved_errno = errno; if (fd != -1) { unlink(sfn); close(fd); } - err(1, "could not open temporary file"); + errc(1, saved_errno, "could not open temporary file"); } run_threads(fputs_thread, sfp); diff --git a/src/regress/lib/libc/stdio_threading/fread/fread_test.c b/src/regress/lib/libc/stdio_threading/fread/fread_test.c index c45a64d14e..6bd734b470 100755 --- a/src/regress/lib/libc/stdio_threading/fread/fread_test.c +++ b/src/regress/lib/libc/stdio_threading/fread/fread_test.c @@ -50,11 +50,12 @@ main(void) strlcpy(sfn, "/tmp/barnacles.XXXXXXXX", sizeof(sfn)); if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) { + int saved_errno = errno; if (fd != -1) { unlink(sfn); close(fd); } - err(1, "could not open temporary file"); + errc(1, saved_errno, "could not open temporary file"); } for (i = 0; i < 4096 * THREAD_COUNT; i++) diff --git a/src/regress/lib/libc/stdio_threading/fwrite/fwrite_test.c b/src/regress/lib/libc/stdio_threading/fwrite/fwrite_test.c index 621c5cb6e8..86c450cbc9 100755 --- a/src/regress/lib/libc/stdio_threading/fwrite/fwrite_test.c +++ b/src/regress/lib/libc/stdio_threading/fwrite/fwrite_test.c @@ -46,11 +46,12 @@ main(void) strlcpy(sfn, "/tmp/barnacles.XXXXXXXX", sizeof(sfn)); if ((fd = mkstemp(sfn)) == -1 || (sfp = fdopen(fd, "w+")) == NULL) { + int saved_errno = errno; if (fd != -1) { unlink(sfn); close(fd); } - err(1, "could not open temporary file"); + errc(1, saved_errno, "could not open temporary file"); } run_threads(fwrite_thread, sfp); diff --git a/src/regress/lib/libc/stdio_threading/include/local.h b/src/regress/lib/libc/stdio_threading/include/local.h index 8d0e628cf6..7a7822a452 100644 --- a/src/regress/lib/libc/stdio_threading/include/local.h +++ b/src/regress/lib/libc/stdio_threading/include/local.h @@ -15,6 +15,7 @@ */ #include +#include #include #include #include diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 76794df6c0..58454595b2 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.121 2014/06/10 16:35:42 tedu Exp $ */ +/* $OpenBSD: netcat.c,v 1.122 2014/07/20 01:38:40 guenther Exp $ */ /* * Copyright (c) 2001 Eric Jackson * @@ -753,8 +753,9 @@ readwrite(int nfd) sleep(iflag); if ((n = poll(pfd, 2 - dflag, timeout)) < 0) { + int saved_errno = errno; close(nfd); - err(1, "Polling Error"); + errc(1, saved_errno, "Polling Error"); } if (n == 0) -- cgit v1.2.3-55-g6feb