Created
August 3, 2011 21:11
-
-
Save bnoordhuis/1123781 to your computer and use it in GitHub Desktop.
race-free exec-after-fork
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2011, Ben Noordhuis <[email protected]> | |
* | |
* Permission to use, copy, modify, and/or distribute this software for any | |
* purpose with or without fee is hereby granted, provided that the above | |
* copyright notice and this permission notice appear in all copies. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
*/ | |
#ifndef _GNU_SOURCE | |
#define _GNU_SOURCE /* pipe2 */ | |
#endif | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <signal.h> | |
#include <fcntl.h> | |
#include <poll.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/wait.h> | |
#ifdef __linux__ | |
#include <linux/version.h> | |
/* pipe2() requires linux >= 2.6.27 and glibc >= 2.9 */ | |
#define HAVE_PIPE2 \ | |
defined(LINUX_VERSION_CODE) && defined(__GLIBC_PREREQ) && LINUX_VERSION_CODE >= 0x2061B && __GLIBC_PREREQ(2, 9)) | |
#endif | |
#define assert_errno() \ | |
do { \ | |
if (errno != 0) { \ | |
fprintf(stderr, \ | |
"%s:%d: %s\n", \ | |
__FILE__, \ | |
__LINE__, \ | |
strerror(errno)); \ | |
abort(); \ | |
} \ | |
} \ | |
while (0); | |
#define E(expr) (expr); assert_errno(); | |
#ifndef HAVE_PIPE2 | |
static void cloexec(int fd) { | |
int flags; | |
E(flags = fcntl(fd, F_GETFD)); | |
E(fcntl(fd, F_SETFD, FD_CLOEXEC | flags)); | |
} | |
#endif | |
static void nonblock(int fd) { | |
int flags; | |
E(flags = fcntl(fd, F_GETFL)); | |
E(fcntl(fd, F_SETFL, O_NONBLOCK | flags)); | |
} | |
static void sigchld(int signum) { | |
(void) signum; | |
} | |
static void sigterm(int signum) { | |
(void) signum; | |
E(write(2, "SIGTERM received\n", sizeof("SIGTERM received\n") - 1)); | |
} | |
int main(void) { | |
int pipefd[2]; | |
pid_t pid; | |
/* need to catch SIGCHLD */ | |
E(signal(SIGCHLD, sigchld)); | |
/* we'll send SIGTERM to the child process */ | |
E(signal(SIGTERM, sigterm)); | |
#ifdef HAVE_PIPE2 | |
E(pipe2(pipefd, O_CLOEXEC)); | |
#else | |
E(pipe(pipefd)); | |
cloexec(pipefd[0]); | |
cloexec(pipefd[1]); | |
#endif | |
E(pid = fork()); | |
if (pid) { | |
/* parent */ | |
struct pollfd pfd; | |
char buf[1]; | |
int status; | |
nonblock(pipefd[0]); | |
E(close(pipefd[1])); | |
do { | |
pfd.fd = pipefd[0]; | |
pfd.events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL; | |
pfd.revents = 0; | |
errno = 0, status = poll(&pfd, 1, -1); | |
} | |
while (status == -1 && status == EINTR); | |
assert_errno(); | |
assert(status == 1); | |
assert(pfd.revents & POLLHUP); | |
do { | |
errno = 0, E(status = read(pipefd[0], buf, sizeof buf)); | |
} | |
while (status == -1 && status == EINTR); | |
assert_errno(); | |
assert(status == 0); | |
E(close(pipefd[0])); | |
E(kill(pid, SIGINT)); | |
E(waitpid(pid, &status, 0)); | |
assert(WIFSIGNALED(status)); | |
assert(!WIFEXITED(status)); | |
} | |
else { | |
/* child */ | |
char *argv[] = { "/bin/cat", NULL }; | |
/* idle for a bit */ | |
E(usleep(500 * 1000)); | |
E(execvp(argv[0], argv)); | |
abort(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment