Commits

Amaury Forgeot d'Arc committed debd5b7

Port _posixsubprocess module.
Only very little tests, should at least ensure that the code compiles.

  • Participants
  • Parent commits afdb9dc
  • Branches py3k

Comments (0)

Files changed (4)

pypy/module/_posixsubprocess/__init__.py

+from pypy.interpreter.mixedmodule import MixedModule
+
+class Module(MixedModule):
+    applevel_name = '_posixsubprocess'
+
+    appleveldefs = {
+        }
+    
+    interpleveldefs = {
+        'fork_exec': 'interp_subprocess.fork_exec',
+        'cloexec_pipe': 'interp_subprocess.cloexec_pipe',
+        }

pypy/module/_posixsubprocess/_posixsubprocess.c

+/* This file is mostly a copy of CPython's Module/_posixsubprocess.c */
+/* modified for PyPy: Removed dependency on Python API. */
+
+/* Authors: Gregory P. Smith & Jeffrey Yasskin */
+#if defined(HAVE_PIPE2) && !defined(_GNU_SOURCE)
+# define _GNU_SOURCE
+#endif
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#if defined(HAVE_SYS_STAT_H) && defined(__FreeBSD__)
+#include <sys/stat.h>
+#endif
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#include <dirent.h>
+
+#if defined(sun)
+/* readdir64 is used to work around Solaris 9 bug 6395699. */
+# define readdir readdir64
+# define dirent dirent64
+# if !defined(HAVE_DIRFD)
+/* Some versions of Solaris lack dirfd(). */
+#  define dirfd(dirp) ((dirp)->dd_fd)
+#  define HAVE_DIRFD
+# endif
+#endif
+
+#if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
+# define FD_DIR "/dev/fd"
+#else
+# define FD_DIR "/proc/self/fd"
+#endif
+
+#define POSIX_CALL(call)   if ((call) == -1) goto error
+
+
+/* Maximum file descriptor, initialized on module load. */
+static long max_fd;
+
+
+/* Convert ASCII to a positive int, no libc call. no overflow. -1 on error. */
+static int
+_pos_int_from_ascii(char *name)
+{
+    int num = 0;
+    while (*name >= '0' && *name <= '9') {
+        num = num * 10 + (*name - '0');
+        ++name;
+    }
+    if (*name)
+        return -1;  /* Non digit found, not a number. */
+    return num;
+}
+
+
+#if defined(__FreeBSD__)
+/* When /dev/fd isn't mounted it is often a static directory populated
+ * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
+ * NetBSD and OpenBSD have a /proc fs available (though not necessarily
+ * mounted) and do not have fdescfs for /dev/fd.  MacOS X has a devfs
+ * that properly supports /dev/fd.
+ */
+static int
+_is_fdescfs_mounted_on_dev_fd()
+{
+    struct stat dev_stat;
+    struct stat dev_fd_stat;
+    if (stat("/dev", &dev_stat) != 0)
+        return 0;
+    if (stat(FD_DIR, &dev_fd_stat) != 0)
+        return 0; 
+    if (dev_stat.st_dev == dev_fd_stat.st_dev)
+        return 0;  /* / == /dev == /dev/fd means it is static. #fail */
+    return 1;
+}
+#endif
+
+
+/* Is fd found in the sorted Python Sequence? */
+static int
+_is_fd_in_sorted_fd_sequence(int fd, long *fd_sequence, size_t seq_len)
+{
+    /* Binary search. */
+    size_t search_min = 0;
+    size_t search_max = seq_len - 1;
+    if (search_max < 0)
+        return 0;
+    do {
+        size_t middle = (search_min + search_max) / 2;
+        long middle_fd = fd_sequence[middle];
+        if (fd == middle_fd)
+            return 1;
+        if (fd > middle_fd)
+            search_min = middle + 1;
+        else
+            search_max = middle - 1;
+    } while (search_min <= search_max);
+    return 0;
+}
+
+
+/* Close all file descriptors in the range start_fd inclusive to
+ * end_fd exclusive except for those in py_fds_to_keep.  If the
+ * range defined by [start_fd, end_fd) is large this will take a
+ * long time as it calls close() on EVERY possible fd.
+ */
+static void
+_close_fds_by_brute_force(int start_fd, int end_fd, long *py_fds_to_keep,
+			  size_t num_fds_to_keep)
+{
+    size_t keep_seq_idx;
+    int fd_num;
+    /* As py_fds_to_keep is sorted we can loop through the list closing
+     * fds inbetween any in the keep list falling within our range. */
+    for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) {
+        int keep_fd = py_fds_to_keep[keep_seq_idx];
+        if (keep_fd < start_fd)
+            continue;
+        for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) {
+            while (close(fd_num) < 0 && errno == EINTR);
+        }
+        start_fd = keep_fd + 1;
+    }
+    if (start_fd <= end_fd) {
+        for (fd_num = start_fd; fd_num < end_fd; ++fd_num) {
+            while (close(fd_num) < 0 && errno == EINTR);
+        }
+    }
+}
+
+
+#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
+/* It doesn't matter if d_name has room for NAME_MAX chars; we're using this
+ * only to read a directory of short file descriptor number names.  The kernel
+ * will return an error if we didn't give it enough space.  Highly Unlikely.
+ * This structure is very old and stable: It will not change unless the kernel
+ * chooses to break compatibility with all existing binaries.  Highly Unlikely.
+ */
+struct linux_dirent {
+#if defined(__x86_64__) && defined(__ILP32__)
+   /* Support the wacky x32 ABI (fake 32-bit userspace speaking to x86_64
+    * kernel interfaces) - https://sites.google.com/site/x32abi/ */
+   unsigned long long d_ino;
+   unsigned long long d_off;
+#else
+   unsigned long  d_ino;        /* Inode number */
+   unsigned long  d_off;        /* Offset to next linux_dirent */
+#endif
+   unsigned short d_reclen;     /* Length of this linux_dirent */
+   char           d_name[256];  /* Filename (null-terminated) */
+};
+
+/* Close all open file descriptors in the range start_fd inclusive to end_fd
+ * exclusive. Do not close any in the sorted py_fds_to_keep list.
+ *
+ * This version is async signal safe as it does not make any unsafe C library
+ * calls, malloc calls or handle any locks.  It is _unfortunate_ to be forced
+ * to resort to making a kernel system call directly but this is the ONLY api
+ * available that does no harm.  opendir/readdir/closedir perform memory
+ * allocation and locking so while they usually work they are not guaranteed
+ * to (especially if you have replaced your malloc implementation).  A version
+ * of this function that uses those can be found in the _maybe_unsafe variant.
+ *
+ * This is Linux specific because that is all I am ready to test it on.  It
+ * should be easy to add OS specific dirent or dirent64 structures and modify
+ * it with some cpp #define magic to work on other OSes as well if you want.
+ */
+static void
+_close_open_fd_range_safe(int start_fd, int end_fd, long *py_fds_to_keep,
+			  size_t num_fds_to_keep)
+{
+    int fd_dir_fd;
+    if (start_fd >= end_fd)
+        return;
+#ifdef O_CLOEXEC
+    fd_dir_fd = open(FD_DIR, O_RDONLY | O_CLOEXEC, 0);
+#else
+    fd_dir_fd = open(FD_DIR, O_RDONLY, 0);
+#ifdef FD_CLOEXEC
+    {
+        int old = fcntl(fd_dir_fd, F_GETFD);
+        if (old != -1)
+            fcntl(fd_dir_fd, F_SETFD, old | FD_CLOEXEC);
+    }
+#endif
+#endif
+    if (fd_dir_fd == -1) {
+        /* No way to get a list of open fds. */
+	_close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep,
+				  num_fds_to_keep);
+        return;
+    } else {
+        char buffer[sizeof(struct linux_dirent)];
+        int bytes;
+        while ((bytes = syscall(SYS_getdents, fd_dir_fd,
+                                (struct linux_dirent *)buffer,
+                                sizeof(buffer))) > 0) {
+            struct linux_dirent *entry;
+            int offset;
+            for (offset = 0; offset < bytes; offset += entry->d_reclen) {
+                int fd;
+                entry = (struct linux_dirent *)(buffer + offset);
+                if ((fd = _pos_int_from_ascii(entry->d_name)) < 0)
+                    continue;  /* Not a number. */
+                if (fd != fd_dir_fd && fd >= start_fd && fd < end_fd &&
+                    !_is_fd_in_sorted_fd_sequence(
+			fd, py_fds_to_keep, num_fds_to_keep)) {
+                    while (close(fd) < 0 && errno == EINTR);
+                }
+            }
+        }
+        close(fd_dir_fd);
+    }
+}
+
+#define _close_open_fd_range _close_open_fd_range_safe
+
+#else  /* NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
+
+
+/* Close all open file descriptors in the range start_fd inclusive to end_fd
+ * exclusive. Do not close any in the sorted py_fds_to_keep list.
+ *
+ * This function violates the strict use of async signal safe functions. :(
+ * It calls opendir(), readdir() and closedir().  Of these, the one most
+ * likely to ever cause a problem is opendir() as it performs an internal
+ * malloc().  Practically this should not be a problem.  The Java VM makes the
+ * same calls between fork and exec in its own UNIXProcess_md.c implementation.
+ *
+ * readdir_r() is not used because it provides no benefit.  It is typically
+ * implemented as readdir() followed by memcpy().  See also:
+ *   http://womble.decadent.org.uk/readdir_r-advisory.html
+ */
+static void
+_close_open_fd_range_maybe_unsafe(int start_fd, int end_fd,
+                                  long *py_fds_to_keep, size_t num_fds_to_keep)
+{
+    DIR *proc_fd_dir;
+#ifndef HAVE_DIRFD
+    while (_is_fd_in_sorted_fd_sequence(start_fd, py_fds_to_keep, num_fds_to_keep) &&
+           (start_fd < end_fd)) {
+        ++start_fd;
+    }
+    if (start_fd >= end_fd)
+        return;
+    /* Close our lowest fd before we call opendir so that it is likely to
+     * reuse that fd otherwise we might close opendir's file descriptor in
+     * our loop.  This trick assumes that fd's are allocated on a lowest
+     * available basis. */
+    while (close(start_fd) < 0 && errno == EINTR);
+    ++start_fd;
+#endif
+    if (start_fd >= end_fd)
+        return;
+
+#if defined(__FreeBSD__)
+    if (!_is_fdescfs_mounted_on_dev_fd())
+        proc_fd_dir = NULL;
+    else
+#endif
+        proc_fd_dir = opendir(FD_DIR);
+    if (!proc_fd_dir) {
+        /* No way to get a list of open fds. */
+        _close_fds_by_brute_force(start_fd, end_fd, py_fds_to_keep, num_fds_to_keep);
+    } else {
+        struct dirent *dir_entry;
+#ifdef HAVE_DIRFD
+        int fd_used_by_opendir = dirfd(proc_fd_dir);
+#else
+        int fd_used_by_opendir = start_fd - 1;
+#endif
+        errno = 0;
+        while ((dir_entry = readdir(proc_fd_dir))) {
+            int fd;
+            if ((fd = _pos_int_from_ascii(dir_entry->d_name)) < 0)
+                continue;  /* Not a number. */
+            if (fd != fd_used_by_opendir && fd >= start_fd && fd < end_fd &&
+                !_is_fd_in_sorted_fd_sequence(fd, py_fds_to_keep, num_fds_to_keep)) {
+                while (close(fd) < 0 && errno == EINTR);
+            }
+            errno = 0;
+        }
+        if (errno) {
+            /* readdir error, revert behavior. Highly Unlikely. */
+            _close_fds_by_brute_force(
+		start_fd, end_fd, py_fds_to_keep, num_fds_to_keep);
+        }
+        closedir(proc_fd_dir);
+    }
+}
+
+#define _close_open_fd_range _close_open_fd_range_maybe_unsafe
+
+#endif  /* else NOT (defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)) */
+
+/*
+ * This function is code executed in the child process immediately after fork
+ * to set things up and call exec().
+ *
+ * All of the code in this function must only use async-signal-safe functions,
+ * listed at `man 7 signal` or
+ * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
+ *
+ * This restriction is documented at
+ * http://www.opengroup.org/onlinepubs/009695399/functions/fork.html.
+ */
+void
+pypy_subprocess_child_exec(
+           char *const exec_array[],
+           char *const argv[],
+           char *const envp[],
+           const char *cwd,
+           int p2cread, int p2cwrite,
+           int c2pread, int c2pwrite,
+           int errread, int errwrite,
+           int errpipe_read, int errpipe_write,
+           int close_fds, int restore_signals,
+           int call_setsid,
+           long *py_fds_to_keep,
+	   size_t num_fds_to_keep,
+           int (*preexec_fn)(void*),
+           void *preexec_fn_arg)
+{
+    int i, saved_errno, unused, reached_preexec = 0;
+    int result;
+    const char* err_msg = "";
+    /* Buffer large enough to hold a hex integer.  We can't malloc. */
+    char hex_errno[sizeof(saved_errno)*2+1];
+
+    /* Close parent's pipe ends. */
+    if (p2cwrite != -1) {
+        POSIX_CALL(close(p2cwrite));
+    }
+    if (c2pread != -1) {
+        POSIX_CALL(close(c2pread));
+    }
+    if (errread != -1) {
+        POSIX_CALL(close(errread));
+    }
+    POSIX_CALL(close(errpipe_read));
+
+    /* When duping fds, if there arises a situation where one of the fds is
+       either 0, 1 or 2, it is possible that it is overwritten (#12607). */
+    if (c2pwrite == 0)
+        POSIX_CALL(c2pwrite = dup(c2pwrite));
+    if (errwrite == 0 || errwrite == 1)
+        POSIX_CALL(errwrite = dup(errwrite));
+
+    /* Dup fds for child.
+       dup2() removes the CLOEXEC flag but we must do it ourselves if dup2()
+       would be a no-op (issue #10806). */
+    if (p2cread == 0) {
+        int old = fcntl(p2cread, F_GETFD);
+        if (old != -1)
+            fcntl(p2cread, F_SETFD, old & ~FD_CLOEXEC);
+    } else if (p2cread != -1) {
+        POSIX_CALL(dup2(p2cread, 0));  /* stdin */
+    }
+    if (c2pwrite == 1) {
+        int old = fcntl(c2pwrite, F_GETFD);
+        if (old != -1)
+            fcntl(c2pwrite, F_SETFD, old & ~FD_CLOEXEC);
+    } else if (c2pwrite != -1) {
+        POSIX_CALL(dup2(c2pwrite, 1));  /* stdout */
+    }
+    if (errwrite == 2) {
+        int old = fcntl(errwrite, F_GETFD);
+        if (old != -1)
+            fcntl(errwrite, F_SETFD, old & ~FD_CLOEXEC);
+    } else if (errwrite != -1) {
+        POSIX_CALL(dup2(errwrite, 2));  /* stderr */
+    }
+
+    /* Close pipe fds.  Make sure we don't close the same fd more than */
+    /* once, or standard fds. */
+    if (p2cread > 2) {
+        POSIX_CALL(close(p2cread));
+    }
+    if (c2pwrite > 2 && c2pwrite != p2cread) {
+        POSIX_CALL(close(c2pwrite));
+    }
+    if (errwrite != c2pwrite && errwrite != p2cread && errwrite > 2) {
+        POSIX_CALL(close(errwrite));
+    }
+
+    if (close_fds) {
+        int local_max_fd = max_fd;
+#if defined(__NetBSD__)
+        local_max_fd = fcntl(0, F_MAXFD);
+        if (local_max_fd < 0)
+            local_max_fd = max_fd;
+#endif
+        /* TODO HP-UX could use pstat_getproc() if anyone cares about it. */
+        _close_open_fd_range(3, local_max_fd, py_fds_to_keep, num_fds_to_keep);
+    }
+
+    if (cwd)
+        POSIX_CALL(chdir(cwd));
+
+    /* PyPy change: moved this call to the preexec callback */
+    /* if (restore_signals) */
+    /*     _Py_RestoreSignals(); */
+
+#ifdef HAVE_SETSID
+    if (call_setsid)
+        POSIX_CALL(setsid());
+#endif
+
+    reached_preexec = 1;
+    if (preexec_fn != NULL) {
+        /* This is where the user has asked us to deadlock their program. */
+        result = preexec_fn(preexec_fn_arg);
+        if (result == 0) {
+            /* Stringifying the exception or traceback would involve
+             * memory allocation and thus potential for deadlock.
+             * We've already faced potential deadlock by calling back
+             * into Python in the first place, so it probably doesn't
+             * matter but we avoid it to minimize the possibility. */
+            err_msg = "Exception occurred in preexec_fn.";
+            errno = 0;  /* We don't want to report an OSError. */
+            goto error;
+        }
+    }
+
+    /* This loop matches the Lib/os.py _execvpe()'s PATH search when */
+    /* given the executable_list generated by Lib/subprocess.py.     */
+    saved_errno = 0;
+    for (i = 0; exec_array[i] != NULL; ++i) {
+        const char *executable = exec_array[i];
+        if (envp) {
+            execve(executable, argv, envp);
+        } else {
+            execv(executable, argv);
+        }
+        if (errno != ENOENT && errno != ENOTDIR && saved_errno == 0) {
+            saved_errno = errno;
+        }
+    }
+    /* Report the first exec error, not the last. */
+    if (saved_errno)
+        errno = saved_errno;
+
+error:
+    saved_errno = errno;
+    /* Report the posix error to our parent process. */
+    /* We ignore all write() return values as the total size of our writes is
+     * less than PIPEBUF and we cannot do anything about an error anyways. */
+    if (saved_errno) {
+        char *cur;
+        unused = write(errpipe_write, "OSError:", 8);
+        cur = hex_errno + sizeof(hex_errno);
+        while (saved_errno != 0 && cur > hex_errno) {
+            *--cur = "0123456789ABCDEF"[saved_errno % 16];
+            saved_errno /= 16;
+        }
+        unused = write(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
+        unused = write(errpipe_write, ":", 1);
+        if (!reached_preexec) {
+            /* Indicate to the parent that the error happened before exec(). */
+            unused = write(errpipe_write, "noexec", 6);
+        }
+        /* We can't call strerror(saved_errno).  It is not async signal safe.
+         * The parent process will look the error message up. */
+    } else {
+        unused = write(errpipe_write, "RuntimeError:0:", 15);
+        unused = write(errpipe_write, err_msg, strlen(err_msg));
+    }
+    if (unused) return;  /* silly? yes! avoids gcc compiler warning. */
+}
+
+
+int
+pypy_subprocess_cloexec_pipe(int *fds)
+{
+    int res;
+#ifdef HAVE_PIPE2
+    Py_BEGIN_ALLOW_THREADS
+    res = pipe2(fds, O_CLOEXEC);
+    Py_END_ALLOW_THREADS
+    if (res != 0 && errno == ENOSYS)
+    {
+        {
+#endif
+        /* We hold the GIL which offers some protection from other code calling
+         * fork() before the CLOEXEC flags have been set but we can't guarantee
+         * anything without pipe2(). */
+        long oldflags;
+
+        res = pipe(fds);
+
+        if (res == 0) {
+            oldflags = fcntl(fds[0], F_GETFD, 0);
+            if (oldflags < 0) res = oldflags;
+        }
+        if (res == 0)
+            res = fcntl(fds[0], F_SETFD, oldflags | FD_CLOEXEC);
+
+        if (res == 0) {
+            oldflags = fcntl(fds[1], F_GETFD, 0);
+            if (oldflags < 0) res = oldflags;
+        }
+        if (res == 0)
+            res = fcntl(fds[1], F_SETFD, oldflags | FD_CLOEXEC);
+#ifdef HAVE_PIPE2
+        }
+    }
+#endif
+    if (res != 0)
+	return res;
+    return 0;
+}
+
+
+void
+pypy_subprocess_init(void)
+{
+#ifdef _SC_OPEN_MAX
+    max_fd = sysconf(_SC_OPEN_MAX);
+    if (max_fd == -1)
+#endif
+        max_fd = 256;  /* Matches Lib/subprocess.py */
+}

pypy/module/_posixsubprocess/interp_subprocess.py

+from pypy.translator.goal import autopath
+from pypy.rpython.lltypesystem import rffi, lltype, llmemory
+from pypy.module.posix.interp_posix import fsencode_w, run_fork_hooks
+from pypy.interpreter.gateway import unwrap_spec
+from pypy.interpreter.error import (
+    OperationError, exception_from_errno, wrap_oserror)
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+import py
+import os
+
+pypydir = py.path.local(autopath.pypydir)
+thisdir = pypydir.join('module', '_posixsubprocess')
+
+eci = ExternalCompilationInfo(
+    separate_module_files=[thisdir.join('_posixsubprocess.c')],
+    export_symbols=['pypy_subprocess_child_exec',
+                    'pypy_subprocess_cloexec_pipe',
+                    'pypy_subprocess_init',
+                    ])
+c_child_exec = rffi.llexternal(
+    'pypy_subprocess_child_exec',
+    [rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARPP, rffi.CCHARP,
+     rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT, 
+     rffi.INT, rffi.INT, rffi.INT, rffi.INT, rffi.INT,
+     rffi.CArrayPtr(rffi.LONG), lltype.Signed,
+     lltype.Ptr(lltype.FuncType([rffi.VOIDP], rffi.INT)), rffi.VOIDP],
+    lltype.Void,
+    compilation_info=eci,
+    threadsafe=True)
+c_cloexec_pipe = rffi.llexternal(
+    'pypy_subprocess_cloexec_pipe',
+    [rffi.CArrayPtr(rffi.LONG)], rffi.INT,
+    compilation_info=eci,
+    threadsafe=True)     
+    
+
+class PreexecCallback:
+    def __init__(self):
+        self.space = None
+        self.w_preexec_fn = None
+    
+    @staticmethod
+    def run_function(unused):
+        self = preexec
+        if self.w_preexec_fn:
+            try:
+                self.space.call_function(self.w_preexec_fn)
+            except OperationError:
+                return 0
+        return 1
+preexec = PreexecCallback()
+
+
+def build_fd_sequence(space, w_fd_list):
+    result = [space.int_w(w_fd)
+              for w_fd in space.unpackiterable(w_fd_list)]
+    prev_fd = -1
+    for fd in result:
+        if fd < 0 or fd < prev_fd or fd > 1 << 30:
+            raise OperationError(space.w_ValueError, space.wrap(
+                    "bad value(s) in fds_to_keep"))
+    return result
+
+
+@unwrap_spec(p2cread=int, p2cwrite=int, c2pread=int, c2pwrite=int,
+             errread=int, errwrite=int, errpipe_read=int, errpipe_write=int,
+             restore_signals=int, call_setsid=int)
+def fork_exec(space, w_process_args, w_executable_list,
+              w_close_fds, w_fds_to_keep, w_cwd, w_env_list,
+              p2cread, p2cwrite, c2pread, c2pwrite,
+              errread, errwrite, errpipe_read, errpipe_write,
+              restore_signals, call_setsid, w_preexec_fn):
+    """\
+    fork_exec(args, executable_list, close_fds, cwd, env,
+              p2cread, p2cwrite, c2pread, c2pwrite,
+              errread, errwrite, errpipe_read, errpipe_write,
+              restore_signals, call_setsid, preexec_fn)
+
+    Forks a child process, closes parent file descriptors as appropriate in the
+    child and dups the few that are needed before calling exec() in the child
+    process.
+    
+    The preexec_fn, if supplied, will be called immediately before exec.
+    WARNING: preexec_fn is NOT SAFE if your application uses threads.
+             It may trigger infrequent, difficult to debug deadlocks.
+    
+    If an error occurs in the child process before the exec, it is
+    serialized and written to the errpipe_write fd per subprocess.py.
+    
+    Returns: the child process's PID.
+    
+    Raises: Only on an error in the parent process.
+    """
+    close_fds = space.is_true(w_close_fds)
+    if close_fds and errpipe_write < 3:  # precondition
+        raise OperationError(space.w_ValueError, space.wrap(
+                "errpipe_write must be >= 3"))
+    fds_to_keep = build_fd_sequence(space, w_fds_to_keep)
+
+    # No need to disable GC in PyPy:
+    # - gc.disable() only disables __del__ anyway.
+    # - appelvel __del__ are only called at specific points of the
+    #   interpreter.
+
+    l_exec_array = lltype.nullptr(rffi.CCHARPP.TO)
+    l_argv = lltype.nullptr(rffi.CCHARPP.TO)
+    l_envp = lltype.nullptr(rffi.CCHARPP.TO)
+    l_cwd = lltype.nullptr(rffi.CCHARP.TO)
+    l_fds_to_keep = lltype.nullptr(rffi.CArrayPtr(rffi.LONG).TO)
+
+    # Convert args and env into appropriate arguments for exec()
+    # These conversions are done in the parent process to avoid allocating
+    # or freeing memory in the child process.
+    try:
+        exec_array = [space.bytes_w(w_item)
+                      for w_item in space.listview(w_executable_list)]
+        l_exec_array = rffi.liststr2charpp(exec_array)
+
+        if not space.is_none(w_process_args):
+            argv = [fsencode_w(space, w_item)
+                    for w_item in space.listview(w_process_args)]
+            l_argv = rffi.liststr2charpp(argv)
+
+        if not space.is_none(w_env_list):
+            envp = [space.bytes_w(w_item)
+                    for w_item in space.listview(w_env_list)]
+            l_envp = rffi.liststr2charpp(envp)
+
+        l_fds_to_keep = lltype.malloc(rffi.CArrayPtr(rffi.LONG).TO,
+                                      len(fds_to_keep) + 1, flavor='raw')
+        for i in range(len(fds_to_keep)):
+            l_fds_to_keep[i] = fds_to_keep[i]
+
+        # if not space.is_none(w_preexec_fn):
+        #     _PyImport_AcquireLock()
+        preexec.space = space
+        preexec.w_preexec_fn = w_preexec_fn
+
+        if not space.is_none(w_cwd):
+            cwd = fsencode_w(space, w_cwd)
+            l_cwd = rffi.str2charp(cwd)
+
+        try:
+            pid = os.fork()
+        except OSError, e:
+            raise wrap_oserror(space, e)
+
+        if pid == 0:
+            # Child process
+            # Code from here to _exit() must only use
+            # async-signal-safe functions, listed at `man 7 signal`
+            # http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
+            if not space.is_none(w_preexec_fn):
+                # We'll be calling back into Python later so we need
+                # to do this. This call may not be async-signal-safe
+                # but neither is calling back into Python.  The user
+                # asked us to use hope as a strategy to avoid
+                # deadlock...
+                run_fork_hooks('child', space)
+
+            c_child_exec(
+                l_exec_array, l_argv, l_envp, l_cwd,
+                p2cread, p2cwrite, c2pread, c2pwrite,
+                errread, errwrite, errpipe_read, errpipe_write,
+                close_fds, restore_signals, call_setsid,
+                l_fds_to_keep, len(fds_to_keep),
+                PreexecCallback.run_function, None)
+            os._exit(255)
+
+    # parent process
+    finally:
+        # if not space.is_none(w_preexec_fn):
+        #     _PyImport_ReleaseLock()
+        run_fork_hooks('parent', space)
+        
+        preexec.w_preexec_fn = None
+
+        if l_cwd:
+            lltype.free(l_cwd, flavor='raw')
+        if l_envp:
+            lltype.free(l_envp, flavor='raw')
+        if l_argv:
+            lltype.free(l_argv, flavor='raw')
+        if l_exec_array:
+            lltype.free(l_exec_array, flavor='raw')
+        if l_fds_to_keep:
+            lltype.free(l_fds_to_keep, flavor='raw')
+
+    return space.wrap(pid)
+
+
+def cloexec_pipe(space):
+    """"cloexec_pipe() -> (read_end, write_end)
+    Create a pipe whose ends have the cloexec flag set."""
+
+    with lltype.scoped_alloc(rffi.CArrayPtr(rffi.LONG).TO, 2) as fds:
+        res = c_cloexec_pipe(fds)
+        if res != 0:
+            raise exception_from_errno(space, space.w_OSError)
+
+        return space.newtuple([space.wrap(fds[0]),
+                               space.wrap(fds[1]),
+                               ])

pypy/module/_posixsubprocess/test/test_ztranslation.py

+from pypy.objspace.fake.checkmodule import checkmodule
+
+def test_posixsubprocess_translates():
+    checkmodule('_posixsubprocess')