spawn.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. (void)__attr;
  87. return 0;
  88. }
  89. /* Store signal mask for signals with default handling from ATTR in
  90. SIGDEFAULT. */
  91. static inline
  92. int posix_spawnattr_getsigdefault (const posix_spawnattr_t *
  93. __restrict __attr,
  94. sigset_t *__restrict __sigdefault)
  95. {
  96. memcpy (__sigdefault, &__attr->__sd, sizeof (sigset_t));
  97. return 0;
  98. }
  99. /* Set signal mask for signals with default handling in ATTR to SIGDEFAULT. */
  100. static inline
  101. int posix_spawnattr_setsigdefault (posix_spawnattr_t *__restrict __attr,
  102. const sigset_t *__restrict
  103. __sigdefault)
  104. {
  105. memcpy (&__attr->__sd, __sigdefault, sizeof (sigset_t));
  106. return 0;
  107. }
  108. /* Store signal mask for the new process from ATTR in SIGMASK. */
  109. static inline
  110. int posix_spawnattr_getsigmask (const posix_spawnattr_t *__restrict
  111. __attr,
  112. sigset_t *__restrict __sigmask)
  113. {
  114. memcpy (__sigmask, &__attr->__ss, sizeof (sigset_t));
  115. return 0;
  116. }
  117. /* Set signal mask for the new process in ATTR to SIGMASK. */
  118. static inline
  119. int posix_spawnattr_setsigmask (posix_spawnattr_t *__restrict __attr,
  120. const sigset_t *__restrict __sigmask)
  121. {
  122. memcpy (&__attr->__ss, __sigmask, sizeof (sigset_t));
  123. return 0;
  124. }
  125. /* Get flag word from the attribute structure. */
  126. static inline
  127. int posix_spawnattr_getflags (const posix_spawnattr_t *__restrict
  128. __attr,
  129. short int *__restrict __flags)
  130. {
  131. *__flags = __attr->__flags;
  132. return 0;
  133. }
  134. /* Store flags in the attribute structure. */
  135. static inline
  136. int posix_spawnattr_setflags (posix_spawnattr_t *_attr,
  137. short int __flags)
  138. {
  139. #ifdef POSIX_SPAWN_USEVFORK
  140. # define __POSIX_SPAWN_USEVFORK POSIX_SPAWN_USEVFORK
  141. #else
  142. # define __POSIX_SPAWN_USEVFORK 0
  143. #endif
  144. #define __POSIX_SPAWN_MASK (POSIX_SPAWN_RESETIDS \
  145. | POSIX_SPAWN_SETPGROUP \
  146. | POSIX_SPAWN_SETSIGDEF \
  147. | POSIX_SPAWN_SETSIGMASK \
  148. | POSIX_SPAWN_SETSCHEDPARAM \
  149. | POSIX_SPAWN_SETSCHEDULER \
  150. | __POSIX_SPAWN_USEVFORK)
  151. /* Check no invalid bits are set. */
  152. if (__flags & ~__POSIX_SPAWN_MASK)
  153. return EINVAL;
  154. _attr->__flags = __flags;
  155. return 0;
  156. #undef __POSIX_SPAWN_USEVFORK
  157. #undef __POSIX_SPAWN_MASK
  158. }
  159. /* Get process group ID from the attribute structure. */
  160. static inline
  161. int posix_spawnattr_getpgroup (const posix_spawnattr_t *__restrict
  162. __attr, pid_t *__restrict __pgroup)
  163. {
  164. *__pgroup = __attr->__pgrp;
  165. return 0;
  166. }
  167. /* Store process group ID in the attribute structure. */
  168. static inline
  169. int posix_spawnattr_setpgroup (posix_spawnattr_t *__attr,
  170. pid_t __pgroup)
  171. {
  172. __attr->__pgrp = __pgroup;
  173. return 0;
  174. }
  175. /* Get scheduling policy from the attribute structure. */
  176. static inline
  177. int posix_spawnattr_getschedpolicy (const posix_spawnattr_t *
  178. __restrict __attr,
  179. int *__restrict __schedpolicy)
  180. {
  181. *__schedpolicy = __attr->__policy;
  182. return 0;
  183. }
  184. /* Store scheduling policy in the attribute structure. */
  185. static inline
  186. int posix_spawnattr_setschedpolicy (posix_spawnattr_t *__attr,
  187. int __schedpolicy)
  188. {
  189. switch (__schedpolicy) {
  190. case SCHED_OTHER:
  191. case SCHED_FIFO:
  192. case SCHED_RR:
  193. break;
  194. default:
  195. return EINVAL;
  196. }
  197. __attr->__policy = __schedpolicy;
  198. return 0;
  199. }
  200. /* Get scheduling parameters from the attribute structure. */
  201. static inline
  202. int posix_spawnattr_getschedparam (const posix_spawnattr_t *
  203. __restrict __attr,
  204. struct sched_param *__restrict
  205. __schedparam)
  206. {
  207. memcpy (__schedparam, &__attr->__sp, sizeof (__attr->__sp));
  208. return 0;
  209. }
  210. /* Store scheduling parameters in the attribute structure. */
  211. static inline
  212. int posix_spawnattr_setschedparam (posix_spawnattr_t *__restrict __attr,
  213. const struct sched_param *
  214. __restrict __schedparam)
  215. {
  216. __attr->__sp = *__schedparam;
  217. return 0;
  218. }
  219. /* Initialize data structure for file attribute for `spawn' call. */
  220. static inline
  221. int posix_spawn_file_actions_init (posix_spawn_file_actions_t *
  222. __file_actions)
  223. {
  224. memset (__file_actions, 0, sizeof (*__file_actions));
  225. return 0;
  226. }
  227. /* Free resources associated with FILE-ACTIONS. */
  228. static inline
  229. int posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *
  230. __file_actions)
  231. {
  232. free (__file_actions->__actions);
  233. return 0;
  234. }
  235. /* Add an action to FILE-ACTIONS which tells the implementation to call
  236. `open' for the given file during the `spawn' call. */
  237. extern int posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *
  238. __restrict __file_actions,
  239. int __fd,
  240. const char *__restrict __path,
  241. int __oflag, mode_t __mode)
  242. __THROW;
  243. /* Add an action to FILE-ACTIONS which tells the implementation to call
  244. `close' for the given file descriptor during the `spawn' call. */
  245. extern int posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *
  246. __file_actions, int __fd)
  247. __THROW;
  248. /* Add an action to FILE-ACTIONS which tells the implementation to call
  249. `dup2' for the given file descriptors during the `spawn' call. */
  250. extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *
  251. __file_actions,
  252. int __fd, int __newfd) __THROW;
  253. __END_DECLS
  254. #endif /* spawn.h */