X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmisc%2Fwordexp.c;h=db39b5b8a3e3dee78bf6a54c1af3a1e23013c72b;hb=04b8360adbb6487f61aa0c00e53ec3a90a5a0d29;hp=8f0d42f59af0ca267496169486cdf541aa5535b6;hpb=8253f59eae7bdb8b5a0f5b87212671564882d1f0;p=musl diff --git a/src/misc/wordexp.c b/src/misc/wordexp.c index 8f0d42f5..db39b5b8 100644 --- a/src/misc/wordexp.c +++ b/src/misc/wordexp.c @@ -8,8 +8,21 @@ #include #include #include +#include #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; @@ -64,6 +77,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags) if (!(sq|dq|np)) return WRDE_BADCHAR; break; case '$': + if (sq) break; if (s[i+1]=='(' && s[i+2]=='(') { i += 2; np += 2; @@ -88,7 +102,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags) we->we_offs = 0; } - if (pipe(p) < 0) goto nospace; + if (pipe2(p, O_CLOEXEC) < 0) goto nospace; __block_all_sigs(&set); pid = fork(); __restore_sigs(&set); @@ -98,9 +112,8 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags) 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,7 +125,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags) if (!f) { close(p[0]); kill(pid, SIGKILL); - waitpid(pid, &status, 0); + reap(pid); goto nospace; } @@ -121,8 +134,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,8 +151,7 @@ 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);