// procutil.c // PUBLIC DOMAIN // http:///www.purposeful.co.uk/software/yabaplib /* I, Tom Vajzovic, am the author of this software and its documentation. I permanently abandon all intellectual property rights in them, including copyright, trademarks, design rights, database right, patents, and the right to be identified as the author. I am fairly certain that the software does what the documentation says it does, but I do not guarantee that it does, or that it does what you think it should. I do not guarantee that it will not have undesirable side effects. You are free to use, modify and distribute this software as you please, but you do so at your own risk. If you do not pass on this warning then you may be responsible for any problems encountered by those who obtain the software through you. */ #include #include #include #include #include #include #include #include #include "procutil.h" void kill_preserving_errno(pid_t pid, int sig) { int save_errno = errno; (void)kill(pid, sig); errno = save_errno; } static pid_t restore(pid_t pid, int read_fd, int write_fd, sigset_t *original_mask, int save_errno) { if (pid > 0) { (void)kill(pid, SIGKILL); for (;;) { if (waitpid(pid, NULL, 0) < 0) { if (errno == EINTR) { continue; } } break; } } if (read_fd >= 0) { (void)close(read_fd); } if (write_fd >= 0) { (void)close(write_fd); } if (original_mask != NULL) { (void)sigprocmask(SIG_SETMASK, original_mask, NULL); } errno = save_errno; return -1; } static void send_errno(int fd, int save_errno) { for (;;) { ssize_t written = write(fd, &save_errno, sizeof save_errno); if ((written >= 0) || (errno != EINTR)) { return; } } } pid_t fork_exec(const char *filename, char *const argv[], char *const envp[]) { struct pipe_fds { int read_fd; int write_fd; } fds; if (pipe(&fds.read_fd) != 0) { return -1; } int flags = fcntl(fds.write_fd, F_GETFD); if (flags < 0) { return restore(-1, fds.read_fd, fds.write_fd, NULL, errno); } if (fcntl(fds.write_fd, F_SETFD, (flags | FD_CLOEXEC)) != 0) { return restore(-1, fds.read_fd, fds.write_fd, NULL, errno); } sigset_t original_mask; sigset_t all; sigfillset(&all); if (sigprocmask(SIG_BLOCK, &all, &original_mask) != 0) { return restore(-1, fds.read_fd, fds.write_fd, NULL, errno); } pid_t pid = fork(); if (pid < 0) { return restore(-1, fds.read_fd, fds.write_fd, &original_mask, errno); } if (pid > 0) { // Parent if (close(fds.write_fd) != 0) { return restore(pid, fds.read_fd, -1, &original_mask, errno); } for (;;) { int fetch_errno; ssize_t readed = read(fds.read_fd, &fetch_errno, sizeof fetch_errno); if ((readed < 0) && (errno == EINTR)) { continue; } else if (readed == 0) { break; } else if (readed < 0) { return restore(pid, fds.read_fd, -1, &original_mask, errno); } else if ((size_t)readed == sizeof fetch_errno) { return restore(pid, fds.read_fd, -1, &original_mask, fetch_errno); } else { return restore(pid, fds.read_fd, -1, &original_mask, EPIPE); } } if (close(fds.read_fd) != 0) { return restore(pid, -1, -1, &original_mask, errno); } if (sigprocmask(SIG_SETMASK, &original_mask, NULL) != 0) { return restore(pid, -1, -1, NULL, errno); } return pid; } else { // Child if (close(fds.read_fd) != 0) { send_errno(fds.write_fd, errno); _Exit(EXIT_FAILURE); } int i; for (i = (STDERR_FILENO + 1); i < FD_SETSIZE; i++) { if ((i != fds.read_fd) && (i != fds.write_fd)) { (void)close(i); } } for (i = 1; i < SIGRTMAX; i++) { (void)signal(i, SIG_DFL); } sigfillset(&all); if (sigprocmask(SIG_UNBLOCK, &all, NULL) != 0) { send_errno(fds.write_fd, errno); _Exit(EXIT_FAILURE); } (void)execve(filename, argv, envp); send_errno(fds.write_fd, errno); _Exit(EXIT_FAILURE); } } int waitpid_status(pid_t pid) { for (;;) { int status; pid_t result = waitpid(pid, &status, 0); if (status < 0) { if (errno == EINTR) { continue; } else { return -1; } } else if (result != pid) { return 128; // undocumented return } else if (WIFEXITED(status)) { return WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { return (128 + WTERMSIG(status)); } else { return 128; // undocumented status } } } int fork_exec_wait(const char *filename, char *const argv[], char *const envp[]) { pid_t pid = fork_exec(filename, argv, envp); if (pid < 0) { return -1; } return waitpid_status(pid); }