From: epg <> Date: Fri, 6 Sep 2002 07:08:49 +0000 (+0000) Subject: (save_errno): New macro. X-Git-Url: https://diplodocus.org/git/mdeliver/commitdiff_plain/4140b150adbbb1e081b4b8d5ae6980394517cc85?hp=77139f24964581ac8509190b1ca71a155d677317 (save_errno): New macro. Use save_errno to protect errno in the 101 places i was calling unlink(2) and then err(3), possibly reporting the wrong error message. --- diff --git a/mdeliver.c b/mdeliver.c index 0073c47..5d4b900 100644 --- a/mdeliver.c +++ b/mdeliver.c @@ -48,6 +48,15 @@ * LEN_ULONGs for time and pid. */ #define TMPNAMLEN 1 + 4 + HOST_NAME_MAX + LEN_ULONG + LEN_ULONG +/* Use this macro to perform some operation and saving the value errno + * had before that operation. + */ +#define save_errno(op) { \ + int olderrno = errno; \ + op; \ + errno = olderrno; \ +} + static char rcsid[] = "$Id$"; /* Write all of buf, even if write(2) is interrupted. */ @@ -146,7 +155,7 @@ strip_from(int fd, char *buf, char *fn) r = read(STDIN_FILENO, buf, 5); if (r != 5) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed read from stdin"); } @@ -159,12 +168,12 @@ strip_from(int fd, char *buf, char *fn) } if (r == -1) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed read from stdin"); } } else { if (full_write(fd, buf, r) != r) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed write(%s)", fn); } } @@ -180,23 +189,23 @@ copy_message(int fd, char *buf, char *fn) while ((r = read(STDIN_FILENO, buf, BUFSIZ)) > 0 || (r == -1 && errno == EINTR)) { if (full_write(fd, buf, r) != r) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed write(%s)", fn); } } if (r == -1) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed read from stdin"); } if (fsync(fd) < 0) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed fsync(%s)", fn); } if (close(fd) < 0) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed close(%s)", fn); } } @@ -230,7 +239,7 @@ deliver(char *maildir, char *newfn) memcpy(newfn, "new", 3); if (rename(fn, newfn) != 0) { - unlink(fn); + save_errno( unlink(fn) ); err(MDELIVER_EXITCODE, "Failed rename(%s, new)", fn); } }