spawn.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Definitions for POSIX spawn interface.
  2. Copyright (C) 2000,2003,2004,2009,2011,2012 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _SPAWN_H
  16. #define _SPAWN_H 1
  17. #include <features.h>
  18. #include <sched.h>
  19. #define __need_sigset_t
  20. #include <signal.h>
  21. #include <sys/types.h>
  22. /* For the tiny inlines (errno/free/memset). */
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. /* Data structure to contain attributes for thread creation. */
  27. typedef struct
  28. {
  29. short int __flags;
  30. pid_t __pgrp;
  31. sigset_t __sd;
  32. sigset_t __ss;
  33. struct sched_param __sp;
  34. int __policy;
  35. int __pad[16];
  36. } posix_spawnattr_t;
  37. /* Data structure to contain information about the actions to be
  38. performed in the new process with respect to file descriptors. */
  39. typedef struct
  40. {
  41. int __allocated;
  42. int __used;
  43. struct __spawn_action *__actions;
  44. int __pad[16];
  45. } posix_spawn_file_actions_t;
  46. /* Flags to be set in the `posix_spawnattr_t'. */
  47. #define POSIX_SPAWN_RESETIDS 0x01
  48. #define POSIX_SPAWN_SETPGROUP 0x02
  49. #define POSIX_SPAWN_SETSIGDEF 0x04
  50. #define POSIX_SPAWN_SETSIGMASK 0x08
  51. #define POSIX_SPAWN_SETSCHEDPARAM 0x10
  52. #define POSIX_SPAWN_SETSCHEDULER 0x20
  53. #ifdef __USE_GNU
  54. # define POSIX_SPAWN_USEVFORK 0x40
  55. #endif
  56. __BEGIN_DECLS
  57. /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
  58. Before running the process perform the actions described in FILE-ACTIONS.
  59. This function is a possible cancellation point and therefore not
  60. marked with __THROW. */
  61. extern int posix_spawn (pid_t *__restrict __pid,
  62. const char *__restrict __path,
  63. const posix_spawn_file_actions_t *__restrict
  64. __file_actions,
  65. const posix_spawnattr_t *__restrict __attrp,
  66. char *const __argv[__restrict_arr],
  67. char *const __envp[__restrict_arr]);
  68. /* Similar to `posix_spawn' but search for FILE in the PATH.
  69. This function is a possible cancellation point and therefore not
  70. marked with __THROW. */
  71. extern int posix_spawnp (pid_t *__pid, const char *__file,
  72. const posix_spawn_file_actions_t *__file_actions,
  73. const posix_spawnattr_t *__attrp,
  74. char *const __argv[], char *const __envp[]);
  75. /* Initialize data structure with attributes for `spawn' to default values. */
  76. static inline
  77. int posix_spawnattr_init (posix_spawnattr_t *__attr)
  78. {
  79. memset (__attr, 0, sizeof (*__attr));
  80. return 0;
  81. }
  82. /* Free resources associated with ATTR. */
  83. static inline
  84. int posix_spawnattr_destroy (posix_spawnattr_t *__attr)
  85. {
  86. return 0;
  87. }
  88. /* Store signal mask for signals with default handling from ATTR in
  89. SIGDEFAULT. */
  90. static inline
  91. int posix_spawnattr_getsigdefault (const posix_spawnattr_t *
  92. __restrict __attr,
  93. sigset_t *__restrict __sigdefault)
  94. {
  95. memcpy (__sigdefault, &__attr->__sd, sizeof (sigset_t));
  96. return 0;
  97. }
  98. /* Set signal mask for signals with default handling in ATTR to SIGDEFAULT. */
  99. static inline
  100. int posix_spawnattr_setsigdefault (posix_spawnattr_t *__restrict __attr,
  101. const sigset_t *__restrict
  102. __sigdefault)
  103. {
  104. memcpy (&__attr->__sd, __sigdefault, sizeof (sigset_t));
  105. return 0;
  106. }
  107. /* Store signal mask for the new process from ATTR in SIGMASK. */
  108. static inline
  109. int posix_spawnattr_getsigmask (const posix_spawnattr_t *__restrict
  110. __attr,
  111. sigset_t *__restrict __sigmask)
  112. {
  113. memcpy (__sigmask, &__attr->__ss, sizeof (sigset_t));
  114. return 0;
  115. }
  116. /* Set signal mask for the new process in ATTR to SIGMASK. */
  117. static inline
  118. int posix_spawnattr_setsigmask (posix_spawnattr_t *__restrict __attr,
  119. const sigset_t *__restrict __sigmask)
  120. {
  121. memcpy (&__attr->__ss, __sigmask, sizeof (sigset_t));
  122. return 0;
  123. }
  124. /* Get flag word from the attribute structure. */
  125. static inline
  126. int posix_spawnattr_getflags (const posix_spawnattr_t *__restrict
  127. __attr,
  128. short int *__restrict __flags)
  129. {
  130. *__flags = __attr->__flags;
  131. return 0;
  132. }
  133. /* Store flags in the attribute structure. */
  134. static inline
  135. int posix_spawnattr_setflags (posix_spawnattr_t *_attr,
  136. short int __flags)
  137. {
  138. #ifdef POSIX_SPAWN_USEVFORK
  139. # define __POSIX_SPAWN_USEVFORK POSIX_SPAWN_USEVFORK
  140. #else
  141. # define __POSIX_SPAWN_USEVFORK 0
  142. #endif
  143. #define __POSIX_SPAWN_MASK (POSIX_SPAWN_RESETIDS \
  144. | POSIX_SPAWN_SETPGROUP \
  145. | POSIX_SPAWN_SETSIGDEF \
  146. | POSIX_SPAWN_SETSIGMASK \
  147. | POSIX_SPAWN_SETSCHEDPARAM \
  148. | POSIX_SPAWN_SETSCHEDULER \
  149. | __POSIX_SPAWN_USEVFORK)
  150. /* Check no invalid bits are set. */
  151. if (__flags & ~__POSIX_SPAWN_MASK)
  152. return EINVAL;
  153. _attr->__flags = __flags;
  154. return 0;
  155. #undef __POSIX_SPAWN_USEVFORK
  156. #undef __POSIX_SPAWN_MASK
  157. }
  158. /* Get process group ID from the attribute structure. */
  159. static inline
  160. int posix_spawnattr_getpgroup (const posix_spawnattr_t *__restrict
  161. __attr, pid_t *__restrict __pgroup)
  162. {
  163. *__pgroup = __attr->__pgrp;
  164. return 0;
  165. }
  166. /* Store process group ID in the attribute structure. */
  167. static inline
  168. int posix_spawnattr_setpgroup (posix_spawnattr_t *__attr,
  169. pid_t __pgroup)
  170. {
  171. __attr->__pgrp = __pgroup;
  172. return 0;
  173. }
  174. /* Get scheduling policy from the attribute structure. */
  175. static inline
  176. int posix_spawnattr_getschedpolicy (const posix_spawnattr_t *
  177. __restrict __attr,
  178. int *__restrict __schedpolicy)
  179. {
  180. *__schedpolicy = __attr->__policy;
  181. return 0;
  182. }
  183. /* Store scheduling policy in the attribute structure. */
  184. static inline
  185. int posix_spawnattr_setschedpolicy (posix_spawnattr_t *__attr,
  186. int __schedpolicy)
  187. {
  188. switch (__schedpolicy) {
  189. case SCHED_OTHER:
  190. case SCHED_FIFO:
  191. case SCHED_RR:
  192. break;
  193. default:
  194. return EINVAL;
  195. }
  196. __attr->__policy = __schedpolicy;
  197. return 0;
  198. }
  199. /* Get scheduling parameters from the attribute structure. */
  200. static inline
  201. int posix_spawnattr_getschedparam (const posix_spawnattr_t *
  202. __restrict __attr,
  203. struct sched_param *__restrict
  204. __schedparam)
  205. {
  206. memcpy (__schedparam, &__attr->__sp, sizeof (__attr->__sp));
  207. return 0;
  208. }
  209. /* Store scheduling parameters in the attribute structure. */
  210. static inline
  211. int posix_spawnattr_setschedparam (posix_spawnattr_t *__restrict __attr,
  212. const struct sched_param *
  213. __restrict __schedparam)
  214. {
  215. __attr->__sp = *__schedparam;
  216. return 0;
  217. }
  218. /* Initialize data structure for file attribute for `spawn' call. */
  219. static inline
  220. int posix_spawn_file_actions_init (posix_spawn_file_actions_t *
  221. __file_actions)
  222. {
  223. memset (__file_actions, 0, sizeof (*__file_actions));
  224. return 0;
  225. }
  226. /* Free resources associated with FILE-ACTIONS. */
  227. static inline
  228. int posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *
  229. __file_actions)
  230. {
  231. free (__file_actions->__actions);
  232. return 0;
  233. }
  234. /* Add an action to FILE-ACTIONS which tells the implementation to call
  235. `open' for the given file during the `spawn' call. */
  236. extern int posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *
  237. __restrict __file_actions,
  238. int __fd,
  239. const char *__restrict __path,
  240. int __oflag, mode_t __mode)
  241. __THROW;
  242. /* Add an action to FILE-ACTIONS which tells the implementation to call
  243. `close' for the given file descriptor during the `spawn' call. */
  244. extern int posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *
  245. __file_actions, int __fd)
  246. __THROW;
  247. /* Add an action to FILE-ACTIONS which tells the implementation to call
  248. `dup2' for the given file descriptors during the `spawn' call. */
  249. extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *
  250. __file_actions,
  251. int __fd, int __newfd) __THROW;
  252. __END_DECLS
  253. #endif /* spawn.h */