check for connect failure in syslog log opening
[musl] / src / misc / wordexp.c
index 7a35868..a5f1b65 100644 (file)
@@ -8,8 +8,21 @@
 #include <sys/wait.h>
 #include <signal.h>
 #include <errno.h>
+#include <fcntl.h>
 #include "pthread_impl.h"
 
+static void reap(pid_t pid)
+{
+       int status;
+       for (;;) {
+               if (waitpid(pid, &status, 0) < 0) {
+                       if (errno != EINTR) return;
+               } else {
+                       if (WIFEXITED(status)) return;
+               }
+       }
+}
+
 static char *getword(FILE *f)
 {
        char *s = 0;
@@ -23,7 +36,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        size_t np=0;
        char *w, **tmp;
        char *redir = (flags & WRDE_SHOWERR) ? "" : "2>/dev/null";
-       int err = 0, status;
+       int err = 0;
        FILE *f;
        size_t wc = 0;
        char **wv = 0;
@@ -82,25 +95,24 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        i = wc;
        if (flags & WRDE_DOOFFS) {
                if (we->we_offs > SIZE_MAX/sizeof(void *)/4)
-                       return WRDE_NOSPACE;
+                       goto nospace;
                i += we->we_offs;
        } else {
                we->we_offs = 0;
        }
 
-       if (pipe(p) < 0) return WRDE_NOSPACE;
+       if (pipe2(p, O_CLOEXEC) < 0) goto nospace;
        __block_all_sigs(&set);
        pid = fork();
        __restore_sigs(&set);
        if (pid < 0) {
                close(p[0]);
                close(p[1]);
-               return WRDE_NOSPACE;
+               goto nospace;
        }
        if (!pid) {
-               dup2(p[1], 1);
-               close(p[0]);
-               close(p[1]);
+               if (p[1] == 1) fcntl(1, F_SETFD, 0);
+               else dup2(p[1], 1);
                execl("/bin/sh", "sh", "-c",
                        "eval \"printf %s\\\\\\\\0 x $1 $2\"",
                        "sh", s, redir, (char *)0);
@@ -112,8 +124,8 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        if (!f) {
                close(p[0]);
                kill(pid, SIGKILL);
-               waitpid(pid, &status, 0);
-               return WRDE_NOSPACE;
+               reap(pid);
+               goto nospace;
        }
 
        l = wv ? i+1 : 0;
@@ -121,8 +133,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        free(getword(f));
        if (feof(f)) {
                fclose(f);
-               while ((waitpid(pid, &status, 0) < 0 && errno == EINTR)
-                       || !WIFEXITED(status));
+               reap(pid);
                return WRDE_SYNTAX;
        }
 
@@ -139,17 +150,26 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
        if (!feof(f)) err = WRDE_NOSPACE;
 
        fclose(f);
-       while ((waitpid(pid, &status, 0) < 0 && errno == EINTR)
-               || !WIFEXITED(status));
+       reap(pid);
+
+       if (!wv) wv = calloc(i+1, sizeof *wv);
 
        we->we_wordv = wv;
        we->we_wordc = i;
 
-       for (i=we->we_offs; i; i--)
-               we->we_wordv[i-1] = 0;
-
-       if (flags & WRDE_DOOFFS) we->we_wordc -= we->we_offs;
+       if (flags & WRDE_DOOFFS) {
+               if (wv) for (i=we->we_offs; i; i--)
+                       we->we_wordv[i-1] = 0;
+               we->we_wordc -= we->we_offs;
+       }
        return err;
+
+nospace:
+       if (!(flags & WRDE_APPEND)) {
+               we->we_wordc = 0;
+               we->we_wordv = 0;
+       }
+       return WRDE_NOSPACE;
 }
 
 int wordexp(const char *restrict s, wordexp_t *restrict we, int flags)