From c438cd3d6cc0d89b36641c4d15e65ebcceee6ded Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Sun, 26 Oct 2014 20:23:30 +0000 Subject: [PATCH] return NODATA in more cases and be more picky about children's exit codes. --- src/compress.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/compress.c b/src/compress.c index 45251b1d..5675d492 100644 --- a/src/compress.c +++ b/src/compress.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: compress.c,v 1.73 2014/01/05 15:55:21 christos Exp $") +FILE_RCSID("@(#)$File: compress.c,v 1.74 2014/10/26 20:23:30 christos Exp $") #endif #include "magic.h" @@ -377,6 +377,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, const unsigned char *old, unsigned char **newch, size_t n) { int fdin[2], fdout[2]; + int status; ssize_t r; pid_t pid; @@ -459,7 +460,17 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, /*NOTREACHED*/ default: /* parent */ - break; + if (wait(&status) == -1) { +#ifdef DEBUG + (void)fprintf(stderr, + "Wait failed (%s)\n", + strerror(errno)); +#endif + exit(1); + } + exit(WIFEXITED(status) ? + WEXITSTATUS(status) : 1); + /*NOTREACHED*/ } (void) close(fdin[1]); fdin[1] = -1; @@ -470,7 +481,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, (void)fprintf(stderr, "Malloc failed (%s)\n", strerror(errno)); #endif - n = 0; + n = NODATA; goto err; } if ((r = sread(fdout[0], *newch, HOWMANY, 0)) <= 0) { @@ -479,7 +490,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, strerror(errno)); #endif free(*newch); - n = 0; + n = NODATA; *newch = NULL; goto err; } else { @@ -491,12 +502,19 @@ err: if (fdin[1] != -1) (void) close(fdin[1]); (void) close(fdout[0]); -#ifdef WNOHANG - while (waitpid(pid, NULL, WNOHANG) != -1) - continue; -#else - (void)wait(NULL); + if (wait(&status) == -1) { +#ifdef DEBUG + (void)fprintf(stderr, "Wait failed (%s)\n", + strerror(errno)); #endif + n = NODATA; + } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { +#ifdef DEBUG + (void)fprintf(stderr, "Child status (0x%x)\n", status); +#endif + n = NODATA; + } + (void) close(fdin[0]); return n; -- 2.49.0