static void exec_cmd(int fdin, int fdout, const char** argv, const str* env, const struct passwd* pw) { selfpipe_close(); dup2(fdin, 0); dup2(fdout, 1); dup2(fdout, 2); close(fdin); close(fdout); if (!testmode) { if (initgroups(pw->pw_name, pw->pw_gid) != 0) die1sys(111, "Could not initgroups"); if (setgid(pw->pw_gid) != 0) die1sys(111, "Could not setgid"); if (setuid(pw->pw_uid) != 0) die1sys(111, "Could not setuid"); } if (chdir(pw->pw_dir) != 0) die1sys(111, "Could not change directory"); if (env) if ((environ = envstr_make_array(env)) == 0) die_oom(111); execv(argv[0], (char**)argv); die3sys(111, "Could not execute '", argv[0], "'"); exit(111); }
int selfpipe_init (void) { if (selfpipe[0] >= 0) return (errno = EBUSY, -1); if (pipe(selfpipe) == -1) return -1; if ((ndelay_on(selfpipe[1]) == -1) || (coe(selfpipe[1]) == -1) || (ndelay_on(selfpipe[0]) == -1) || (coe(selfpipe[0]) == -1)) selfpipe_close(); return selfpipe[0]; }
void selfpipe_finish (void) { selfpipe_close(); }