Implement <spawn.h>.

As described here:

  http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html

And here:

  http://man7.org/linux/man-pages/man3/posix_spawn.3.html

Bug: N/A (but mentioned in my inbox since 2013)
Test: ran tests
Change-Id: I0b27b2919b660779e3bd8a25fb429527c16dc621
diff --git a/libc/Android.bp b/libc/Android.bp
index cd01efd..a142f4e 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -1530,6 +1530,7 @@
         "bionic/sigwait.cpp",
         "bionic/sigwaitinfo.cpp",
         "bionic/socket.cpp",
+        "bionic/spawn.cpp",
         "bionic/stat.cpp",
         "bionic/statvfs.cpp",
         "bionic/stdlib_l.cpp",
diff --git a/libc/bionic/spawn.cpp b/libc/bionic/spawn.cpp
new file mode 100644
index 0000000..7015ad9
--- /dev/null
+++ b/libc/bionic/spawn.cpp
@@ -0,0 +1,316 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <spawn.h>
+
+#include <fcntl.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "private/ScopedSignalBlocker.h"
+
+enum Action {
+  kOpen,
+  kClose,
+  kDup2
+};
+
+struct __posix_spawn_file_action {
+  __posix_spawn_file_action* next;
+
+  Action what;
+  int fd;
+  int new_fd;
+  char* path;
+  int flags;
+  mode_t mode;
+
+  void Do() {
+    if (what == kOpen) {
+      fd = open(path, flags, mode);
+      if (fd == -1) _exit(127);
+      // If it didn't land where we wanted it, move it.
+      if (fd != new_fd) {
+        if (dup2(fd, new_fd) == -1) _exit(127);
+        close(fd);
+      }
+    } else if (what == kClose) {
+      // Failure to close is ignored.
+      close(fd);
+    } else {
+      if (dup2(fd, new_fd) == -1) _exit(127);
+    }
+  }
+};
+
+struct __posix_spawn_file_actions {
+  __posix_spawn_file_action* head;
+  __posix_spawn_file_action* last;
+
+  void Do() {
+    for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
+      action->Do();
+    }
+  }
+};
+
+struct __posix_spawnattr {
+  short flags;
+  pid_t pgroup;
+  sched_param schedparam;
+  int schedpolicy;
+  sigset_t sigmask;
+  sigset_t sigdefault;
+
+  void Do() {
+    bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
+
+    for (int s = 1; s < _NSIG; ++s) {
+      struct sigaction sa;
+      if (sigaction(s, nullptr, &sa) == -1) _exit(127);
+      if (sa.sa_handler == SIG_DFL) continue;
+      // POSIX: "Signals set to be caught by the calling process shall be set to the default
+      // action in the child process."
+      // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ... shall be set to
+      // their default actions in the child process."
+      if (sa.sa_handler != SIG_IGN || (use_sigdefault && sigismember(&sigdefault, s))) {
+        sa.sa_handler = SIG_DFL;
+        if (sigaction(s, &sa, nullptr) == -1) _exit(127);
+      }
+    }
+
+    if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, pgroup) == -1) _exit(127);
+    if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
+
+    // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
+    // to set both.
+    if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
+      if (sched_setscheduler(0, schedpolicy, &schedparam) == -1) _exit(127);
+    } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
+      if (sched_setparam(0, &schedparam) == -1) _exit(127);
+    }
+
+    if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
+      if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
+    }
+
+    if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
+      if (sigprocmask(SIG_SETMASK, &sigmask, nullptr)) _exit(127);
+    }
+  }
+};
+
+static int posix_spawn(pid_t* pid_ptr,
+                       const char* path,
+                       const posix_spawn_file_actions_t* actions,
+                       const posix_spawnattr_t* attr,
+                       char* const argv[],
+                       char* const env[],
+                       int exec_fn(const char* path, char* const argv[], char* const env[])) {
+  // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
+  // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
+
+  ScopedSignalBlocker ssb;
+
+  short flags = attr ? (*attr)->flags : 0;
+  bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
+
+  pid_t pid = use_vfork ? vfork() : fork();
+  if (pid == -1) return errno;
+
+  if (pid == 0) {
+    // Child.
+    if (attr) (*attr)->Do();
+    if (actions) (*actions)->Do();
+    if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
+    exec_fn(path, argv, env ? env : environ);
+    _exit(127);
+  }
+
+  // Parent.
+  if (pid_ptr) *pid_ptr = pid;
+  return 0;
+}
+
+int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
+                const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
+  return posix_spawn(pid, path, actions, attr, argv, env, execve);
+}
+
+int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
+                 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
+  return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
+}
+
+int posix_spawnattr_init(posix_spawnattr_t* attr) {
+  *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
+  return (*attr == nullptr) ? errno : 0;
+}
+
+int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
+  free(*attr);
+  *attr = nullptr;
+  return 0;
+}
+
+int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
+  if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
+                 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
+                 POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID)) != 0) {
+    return EINVAL;
+  }
+  (*attr)->flags = flags;
+  return 0;
+}
+
+int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
+  *flags = (*attr)->flags;
+  return 0;
+}
+
+int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
+  (*attr)->pgroup = pgroup;
+  return 0;
+}
+
+int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
+  *pgroup = (*attr)->pgroup;
+  return 0;
+}
+
+int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
+  (*attr)->sigmask = *mask;
+  return 0;
+}
+
+int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
+  *mask = (*attr)->sigmask;
+  return 0;
+}
+
+int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
+  (*attr)->sigdefault = *mask;
+  return 0;
+}
+
+int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
+  *mask = (*attr)->sigdefault;
+  return 0;
+}
+
+int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
+  (*attr)->schedparam = *param;
+  return 0;
+}
+
+int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
+  *param = (*attr)->schedparam;
+  return 0;
+}
+
+int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
+  (*attr)->schedpolicy = policy;
+  return 0;
+}
+
+int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
+  *policy = (*attr)->schedpolicy;
+  return 0;
+}
+
+int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
+  *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
+  return (*actions == nullptr) ? errno : 0;
+}
+
+int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
+  __posix_spawn_file_action* a = (*actions)->head;
+  while (a) {
+    __posix_spawn_file_action* last = a;
+    a = a->next;
+    free(last->path);
+    free(last);
+  }
+  free(*actions);
+  *actions = nullptr;
+  return 0;
+}
+
+static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
+                                       Action what,
+                                       int fd,
+                                       int new_fd,
+                                       const char* path,
+                                       int flags,
+                                       mode_t mode) {
+  __posix_spawn_file_action* action =
+      reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
+  if (action == nullptr) return errno;
+
+  action->next = nullptr;
+  if (path != nullptr) {
+    action->path = strdup(path);
+    if (action->path == nullptr) {
+      free(action);
+      return errno;
+    }
+  } else {
+    action->path = nullptr;
+  }
+  action->what = what;
+  action->fd = fd;
+  action->new_fd = new_fd;
+  action->flags = flags;
+  action->mode = mode;
+
+  if ((*actions)->head == nullptr) {
+    (*actions)->head = (*actions)->last = action;
+  } else {
+    (*actions)->last->next = action;
+    (*actions)->last = action;
+  }
+
+  return 0;
+}
+
+int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
+                                     int fd, const char* path, int flags, mode_t mode) {
+  if (fd < 0) return EBADF;
+  return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
+}
+
+int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
+  if (fd < 0) return EBADF;
+  return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
+}
+
+int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
+  if (fd < 0 || new_fd < 0) return EBADF;
+  return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
+}
diff --git a/libc/bionic/tmpfile.cpp b/libc/bionic/tmpfile.cpp
index dc142a9..bda3566 100644
--- a/libc/bionic/tmpfile.cpp
+++ b/libc/bionic/tmpfile.cpp
@@ -39,22 +39,7 @@
 #include <unistd.h>
 
 #include "private/ErrnoRestorer.h"
-
-class ScopedSignalBlocker {
- public:
-  ScopedSignalBlocker() {
-    sigset_t set;
-    sigfillset(&set);
-    sigprocmask(SIG_BLOCK, &set, &old_set_);
-  }
-
-  ~ScopedSignalBlocker() {
-    sigprocmask(SIG_SETMASK, &old_set_, NULL);
-  }
-
- private:
-  sigset_t old_set_;
-};
+#include "private/ScopedSignalBlocker.h"
 
 static FILE* __tmpfile_dir(const char* tmp_dir) {
   char* path = NULL;
diff --git a/libc/include/bits/posix_limits.h b/libc/include/bits/posix_limits.h
index 4038c3a..e5846d6 100644
--- a/libc/include/bits/posix_limits.h
+++ b/libc/include/bits/posix_limits.h
@@ -68,7 +68,7 @@
 #define _POSIX_SEMAPHORES _POSIX_VERSION /* sem_*. */
 #define _POSIX_SHARED_MEMORY_OBJECTS __BIONIC_POSIX_FEATURE_MISSING /* mmap/munmap are implemented, but shm_open/shm_unlink are not. */
 #define _POSIX_SHELL 1 /* system. */
-#define _POSIX_SPAWN __BIONIC_POSIX_FEATURE_MISSING /* <spawn.h> */
+#define _POSIX_SPAWN __BIONIC_POSIX_FEATURE_SINCE(28) /* <spawn.h> */
 #define _POSIX_SPIN_LOCKS __BIONIC_POSIX_FEATURE_SINCE(24) /* pthread_spin_*. */
 #define _POSIX_SPORADIC_SERVER _POSIX_VERSION /* sched_setparam/sched_setscheduler. */
 #define _POSIX_SYNCHRONIZED_IO _POSIX_VERSION
diff --git a/libc/include/spawn.h b/libc/include/spawn.h
new file mode 100644
index 0000000..ea4bb19
--- /dev/null
+++ b/libc/include/spawn.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _SPAWN_H_
+#define _SPAWN_H_
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <sched.h>
+#include <signal.h>
+
+__BEGIN_DECLS
+
+#define POSIX_SPAWN_RESETIDS 1
+#define POSIX_SPAWN_SETPGROUP 2
+#define POSIX_SPAWN_SETSIGDEF 4
+#define POSIX_SPAWN_SETSIGMASK 8
+#define POSIX_SPAWN_SETSCHEDPARAM 16
+#define POSIX_SPAWN_SETSCHEDULER 32
+#if defined(__USE_GNU)
+#define POSIX_SPAWN_USEVFORK 64
+#define POSIX_SPAWN_SETSID 128
+#endif
+
+typedef struct __posix_spawnattr* posix_spawnattr_t;
+typedef struct __posix_spawn_file_actions* posix_spawn_file_actions_t;
+
+int posix_spawn(pid_t* __pid, const char* __path, const posix_spawn_file_actions_t* __actions, const posix_spawnattr_t* __attr, char* const __argv[], char* const __env[]) __INTRODUCED_IN_FUTURE;
+int posix_spawnp(pid_t* __pid, const char* __file, const posix_spawn_file_actions_t* __actions, const posix_spawnattr_t* __attr, char* const __argv[], char* const __env[]) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_init(posix_spawnattr_t* __attr) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_destroy(posix_spawnattr_t* __attr) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_setflags(posix_spawnattr_t* __attr, short __flags) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_getflags(const posix_spawnattr_t* __attr, short* __flags) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_setpgroup(posix_spawnattr_t* __attr, pid_t __pgroup) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_getpgroup(const posix_spawnattr_t* __attr, pid_t* __pgroup) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_setsigmask(posix_spawnattr_t* __attr, const sigset_t* __mask) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_getsigmask(const posix_spawnattr_t* __attr, sigset_t* __mask) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_setsigdefault(posix_spawnattr_t* __attr, const sigset_t* __mask) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_getsigdefault(const posix_spawnattr_t* __attr, sigset_t* __mask) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_setschedparam(posix_spawnattr_t* __attr, const struct sched_param* __param) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_getschedparam(const posix_spawnattr_t* __attr, struct sched_param* __param) __INTRODUCED_IN_FUTURE;
+
+int posix_spawnattr_setschedpolicy(posix_spawnattr_t* __attr, int __policy) __INTRODUCED_IN_FUTURE;
+int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* __attr, int* __policy) __INTRODUCED_IN_FUTURE;
+
+int posix_spawn_file_actions_init(posix_spawn_file_actions_t* __actions) __INTRODUCED_IN_FUTURE;
+int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* __actions) __INTRODUCED_IN_FUTURE;
+
+int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* __actions, int __fd, const char* __path, int __flags, mode_t __mode) __INTRODUCED_IN_FUTURE;
+int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* __actions, int __fd) __INTRODUCED_IN_FUTURE;
+int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* __actions, int __fd, int __new_fd) __INTRODUCED_IN_FUTURE;
+
+__END_DECLS
+
+#endif
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index 13c267a..08ba59f 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1336,6 +1336,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 9d8c1b7..400c95f 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1256,6 +1256,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index da462d3..eb5c1e4 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1361,6 +1361,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 3e41f95..16f1209 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1320,6 +1320,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 9d8c1b7..400c95f 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1256,6 +1256,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index c3d678c..94ee319 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1318,6 +1318,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 9d8c1b7..400c95f 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1256,6 +1256,27 @@
     iconv;
     iconv_close;
     iconv_open;
+    posix_spawn;
+    posix_spawnattr_destroy;
+    posix_spawnattr_getflags;
+    posix_spawnattr_getpgroup;
+    posix_spawnattr_getschedparam;
+    posix_spawnattr_getschedpolicy;
+    posix_spawnattr_getsigdefault;
+    posix_spawnattr_getsigmask;
+    posix_spawnattr_init;
+    posix_spawnattr_setflags;
+    posix_spawnattr_setpgroup;
+    posix_spawnattr_setschedparam;
+    posix_spawnattr_setschedpolicy;
+    posix_spawnattr_setsigdefault;
+    posix_spawnattr_setsigmask;
+    posix_spawn_file_actions_addclose;
+    posix_spawn_file_actions_adddup2;
+    posix_spawn_file_actions_addopen;
+    posix_spawn_file_actions_destroy;
+    posix_spawn_file_actions_init;
+    posix_spawnp;
     syncfs;
 } LIBC_O;
 
diff --git a/libc/private/ScopedSignalBlocker.h b/libc/private/ScopedSignalBlocker.h
new file mode 100644
index 0000000..35d1c58
--- /dev/null
+++ b/libc/private/ScopedSignalBlocker.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SCOPED_SIGNAL_BLOCKER_H
+#define SCOPED_SIGNAL_BLOCKER_H
+
+#include <signal.h>
+
+#include "bionic_macros.h"
+
+class ScopedSignalBlocker {
+ public:
+  explicit ScopedSignalBlocker() {
+    sigset_t set;
+    sigfillset(&set);
+    sigprocmask(SIG_BLOCK, &set, &old_set_);
+  }
+
+  ~ScopedSignalBlocker() {
+    reset();
+  }
+
+  void reset() {
+    sigprocmask(SIG_SETMASK, &old_set_, nullptr);
+  }
+
+ private:
+  sigset_t old_set_;
+
+  DISALLOW_COPY_AND_ASSIGN(ScopedSignalBlocker);
+};
+
+#endif