return NODATA in more cases and be more picky about children's exit codes.
authorChristos Zoulas <christos@zoulas.com>
Sun, 26 Oct 2014 20:23:30 +0000 (20:23 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sun, 26 Oct 2014 20:23:30 +0000 (20:23 +0000)
src/compress.c

index 45251b1d40c2046c2383294b4717c8cd35023dc5..5675d492f2a2a525e72c6a3a7a0f260718dd8d0b 100644 (file)
@@ -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;