logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0001-libiberty-Implement-pex_unix_exec_child-using-posix_.patch (8740B)


  1. From 8e578ea68fa570aa46f04d4cf1c593ac11732bdf Mon Sep 17 00:00:00 2001
  2. From: Michael Forney <mforney@mforney.org>
  3. Date: Wed, 17 Apr 2019 16:44:05 -0700
  4. Subject: [PATCH] libiberty: Implement pex_unix_exec_child using posix_spawn
  5. ---
  6. libiberty/config.in | 6 +++
  7. libiberty/configure | 10 ++---
  8. libiberty/configure.ac | 10 ++---
  9. libiberty/pex-unix.c | 95 +++++++++++++++++++++++++++++++++++++++++-
  10. 4 files changed, 110 insertions(+), 11 deletions(-)
  11. diff --git a/libiberty/config.in b/libiberty/config.in
  12. index f7052b5d958..ce79a9e77f2 100644
  13. --- a/libiberty/config.in
  14. +++ b/libiberty/config.in
  15. @@ -198,6 +198,9 @@
  16. /* Define to 1 if you have the `pipe2' function. */
  17. #undef HAVE_PIPE2
  18. +/* Define to 1 if you have the `posix_spawn' function. */
  19. +#undef HAVE_POSIX_SPAWN
  20. +
  21. /* Define to 1 if you have the <process.h> header file. */
  22. #undef HAVE_PROCESS_H
  23. @@ -249,6 +252,9 @@
  24. /* Define to 1 if you have the `spawnvpe' function. */
  25. #undef HAVE_SPAWNVPE
  26. +/* Define to 1 if you have the <spawn.h> header file. */
  27. +#undef HAVE_SPAWN_H
  28. +
  29. /* Define to 1 if you have the <stdint.h> header file. */
  30. #undef HAVE_STDINT_H
  31. diff --git a/libiberty/configure b/libiberty/configure
  32. index 306c07bd37e..6f85b1fd529 100755
  33. --- a/libiberty/configure
  34. +++ b/libiberty/configure
  35. @@ -5652,7 +5652,7 @@ host_makefile_frag=${frag}
  36. # It's OK to check for header files. Although the compiler may not be
  37. # able to link anything, it had better be able to at least compile
  38. # something.
  39. -for ac_header in sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h
  40. +for ac_header in sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h spawn.h
  41. do :
  42. as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
  43. ac_fn_c_check_header_preproc "$LINENO" "$ac_header" "$as_ac_Header"
  44. @@ -6161,9 +6161,9 @@ funcs="$funcs setproctitle"
  45. vars="sys_errlist sys_nerr sys_siglist"
  46. checkfuncs="__fsetlocking canonicalize_file_name dup3 getrlimit getrusage \
  47. - getsysinfo gettimeofday on_exit pipe2 psignal pstat_getdynamic pstat_getstatic \
  48. - realpath setrlimit spawnve spawnvpe strerror strsignal sysconf sysctl \
  49. - sysmp table times wait3 wait4"
  50. + getsysinfo gettimeofday on_exit pipe2 posix_spawn psignal pstat_getdynamic \
  51. + pstat_getstatic realpath setrlimit spawnve spawnvpe strerror strsignal \
  52. + sysconf sysctl sysmp table times wait3 wait4"
  53. # Darwin has sbrk, but it is deprecated and that produces build-time warnings
  54. # so do not check for it.
  55. @@ -6184,7 +6184,7 @@ if test "x" = "y"; then
  56. index insque \
  57. memchr memcmp memcpy memmem memmove memset mkstemps \
  58. on_exit \
  59. - pipe2 psignal pstat_getdynamic pstat_getstatic putenv \
  60. + pipe2 posix_spawn psignal pstat_getdynamic pstat_getstatic putenv \
  61. random realpath rename rindex \
  62. sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \
  63. stpcpy stpncpy strcasecmp strchr strdup \
  64. diff --git a/libiberty/configure.ac b/libiberty/configure.ac
  65. index 6c1ff9c6093..5d21f383e7d 100644
  66. --- a/libiberty/configure.ac
  67. +++ b/libiberty/configure.ac
  68. @@ -289,7 +289,7 @@ AC_SUBST_FILE(host_makefile_frag)
  69. # It's OK to check for header files. Although the compiler may not be
  70. # able to link anything, it had better be able to at least compile
  71. # something.
  72. -AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h)
  73. +AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h malloc.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h sys/systemcfg.h stdint.h stdio_ext.h process.h sys/prctl.h spawn.h)
  74. AC_HEADER_SYS_WAIT
  75. AC_HEADER_TIME
  76. @@ -412,9 +412,9 @@ funcs="$funcs setproctitle"
  77. vars="sys_errlist sys_nerr sys_siglist"
  78. checkfuncs="__fsetlocking canonicalize_file_name dup3 getrlimit getrusage \
  79. - getsysinfo gettimeofday on_exit pipe2 psignal pstat_getdynamic pstat_getstatic \
  80. - realpath setrlimit spawnve spawnvpe strerror strsignal sysconf sysctl \
  81. - sysmp table times wait3 wait4"
  82. + getsysinfo gettimeofday on_exit pipe2 posix_spawn psignal pstat_getdynamic \
  83. + pstat_getstatic realpath setrlimit spawnve spawnvpe strerror strsignal \
  84. + sysconf sysctl sysmp table times wait3 wait4"
  85. # Darwin has sbrk, but it is deprecated and that produces build-time warnings
  86. # so do not check for it.
  87. @@ -435,7 +435,7 @@ if test "x" = "y"; then
  88. index insque \
  89. memchr memcmp memcpy memmem memmove memset mkstemps \
  90. on_exit \
  91. - pipe2 psignal pstat_getdynamic pstat_getstatic putenv \
  92. + pipe2 posix_spawn psignal pstat_getdynamic pstat_getstatic putenv \
  93. random realpath rename rindex \
  94. sbrk setenv setproctitle setrlimit sigsetmask snprintf spawnve spawnvpe \
  95. stpcpy stpncpy strcasecmp strchr strdup \
  96. diff --git a/libiberty/pex-unix.c b/libiberty/pex-unix.c
  97. index 850d344ba58..ba3a6d5d38f 100644
  98. --- a/libiberty/pex-unix.c
  99. +++ b/libiberty/pex-unix.c
  100. @@ -58,6 +58,9 @@ extern int errno;
  101. #ifdef HAVE_PROCESS_H
  102. #include <process.h>
  103. #endif
  104. +#ifdef HAVE_SPAWN_H
  105. +#include <spawn.h>
  106. +#endif
  107. #ifdef vfork /* Autoconf may define this to fork for us. */
  108. # define VFORK_STRING "fork"
  109. @@ -366,7 +369,97 @@ pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
  110. /* Execute a child. */
  111. -#if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
  112. +#if defined(HAVE_POSIX_SPAWN)
  113. +/* Implementation of pex->exec_child using the posix_spawn operation. */
  114. +
  115. +static pid_t
  116. +pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
  117. + char * const * argv, char * const * env,
  118. + int in, int out, int errdes,
  119. + int toclose, const char **errmsg, int *err)
  120. +{
  121. + posix_spawn_file_actions_t file_actions;
  122. + pid_t pid;
  123. + int ret;
  124. +
  125. + *errmsg = "posix_spawn";
  126. + ret = posix_spawn_file_actions_init (&file_actions);
  127. + if (ret != 0)
  128. + goto error;
  129. + if (in != STDIN_FILE_NO)
  130. + {
  131. + ret = posix_spawn_file_actions_adddup2 (&file_actions, in, STDIN_FILE_NO);
  132. + if (ret != 0)
  133. + goto error;
  134. + ret = posix_spawn_file_actions_addclose (&file_actions, in);
  135. + if (ret != 0)
  136. + goto error;
  137. + }
  138. + if (out != STDOUT_FILE_NO)
  139. + {
  140. + ret = posix_spawn_file_actions_adddup2 (&file_actions, out, STDOUT_FILE_NO);
  141. + if (ret != 0)
  142. + goto error;
  143. + ret = posix_spawn_file_actions_addclose (&file_actions, out);
  144. + if (ret != 0)
  145. + goto error;
  146. + }
  147. + if ((flags & PEX_STDERR_TO_STDOUT) != 0)
  148. + {
  149. + ret = posix_spawn_file_actions_adddup2 (&file_actions, STDOUT_FILE_NO, STDERR_FILE_NO);
  150. + if (ret != 0)
  151. + goto error;
  152. + }
  153. + else if (errdes != STDERR_FILE_NO)
  154. + {
  155. + ret = posix_spawn_file_actions_adddup2 (&file_actions, errdes, STDERR_FILE_NO);
  156. + if (ret != 0)
  157. + goto error;
  158. + ret = posix_spawn_file_actions_addclose (&file_actions, errdes);
  159. + if (ret != 0)
  160. + goto error;
  161. + }
  162. + if (toclose >= 0)
  163. + {
  164. + ret = posix_spawn_file_actions_addclose (&file_actions, toclose);
  165. + if (ret != 0)
  166. + goto error;
  167. + }
  168. + if (env == NULL)
  169. + env = environ;
  170. + if ((flags & PEX_SEARCH) != 0)
  171. + ret = posix_spawnp (&pid, executable, &file_actions, NULL, argv, env);
  172. + else
  173. + ret = posix_spawn (&pid, executable, &file_actions, NULL, argv, env);
  174. + if (ret != 0)
  175. + goto error;
  176. + posix_spawn_file_actions_destroy(&file_actions);
  177. +
  178. + *errmsg = "close";
  179. + if (in != STDIN_FILE_NO && close (in) < 0)
  180. + {
  181. + ret = errno;
  182. + goto error;
  183. + }
  184. + if (out != STDOUT_FILE_NO && close (out) < 0)
  185. + {
  186. + ret = errno;
  187. + goto error;
  188. + }
  189. + if (errdes != STDERR_FILE_NO && close (errdes) < 0)
  190. + {
  191. + ret = errno;
  192. + goto error;
  193. + }
  194. +
  195. + return pid;
  196. +
  197. + error:
  198. + *err = ret;
  199. + return (pid_t) -1;
  200. +}
  201. +
  202. +#elif defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
  203. /* Implementation of pex->exec_child using the Cygwin spawn operation. */
  204. /* Subroutine of pex_unix_exec_child. Move OLD_FD to a new file descriptor
  205. --
  206. 2.35.1