spawn.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #define __POSIX_SPAWN_MASK (POSIX_SPAWN_RESETIDS \
  57. | POSIX_SPAWN_SETPGROUP \
  58. | POSIX_SPAWN_SETSIGDEF \
  59. | POSIX_SPAWN_SETSIGMASK \
  60. | POSIX_SPAWN_SETSCHEDPARAM \
  61. | POSIX_SPAWN_SETSCHEDULER \
  62. | POSIX_SPAWN_USEVFORK)
  63. __BEGIN_DECLS
  64. /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
  65. Before running the process perform the actions described in FILE-ACTIONS.
  66. This function is a possible cancellation point and therefore not
  67. marked with __THROW. */
  68. extern int posix_spawn (pid_t *__restrict __pid,
  69. const char *__restrict __path,
  70. const posix_spawn_file_actions_t *__restrict
  71. __file_actions,
  72. const posix_spawnattr_t *__restrict __attrp,
  73. char *const __argv[__restrict_arr],
  74. char *const __envp[__restrict_arr]);
  75. /* Similar to `posix_spawn' but search for FILE in the PATH.
  76. This function is a possible cancellation point and therefore not
  77. marked with __THROW. */
  78. extern int posix_spawnp (pid_t *__pid, const char *__file,
  79. const posix_spawn_file_actions_t *__file_actions,
  80. const posix_spawnattr_t *__attrp,
  81. char *const __argv[], char *const __envp[]);
  82. /* Initialize data structure with attributes for `spawn' to default values. */
  83. static inline
  84. int posix_spawnattr_init (posix_spawnattr_t *__attr)
  85. {
  86. memset (__attr, 0, sizeof (*__attr));
  87. return 0;
  88. }
  89. /* Free resources associated with ATTR. */
  90. static inline
  91. int posix_spawnattr_destroy (posix_spawnattr_t *__attr)
  92. {
  93. return 0;
  94. }
  95. /* Store signal mask for signals with default handling from ATTR in
  96. SIGDEFAULT. */
  97. static inline
  98. int posix_spawnattr_getsigdefault (const posix_spawnattr_t *
  99. __restrict __attr,
  100. sigset_t *__restrict __sigdefault)
  101. {
  102. memcpy (__sigdefault, &__attr->__sd, sizeof (sigset_t));
  103. return 0;
  104. }
  105. /* Set signal mask for signals with default handling in ATTR to SIGDEFAULT. */
  106. static inline
  107. int posix_spawnattr_setsigdefault (posix_spawnattr_t *__restrict __attr,
  108. const sigset_t *__restrict
  109. __sigdefault)
  110. {
  111. memcpy (&__attr->__sd, __sigdefault, sizeof (sigset_t));
  112. return 0;
  113. }
  114. /* Store signal mask for the new process from ATTR in SIGMASK. */
  115. static inline
  116. int posix_spawnattr_getsigmask (const posix_spawnattr_t *__restrict
  117. __attr,
  118. sigset_t *__restrict __sigmask)
  119. {
  120. memcpy (__sigmask, &__attr->__ss, sizeof (sigset_t));
  121. return 0;
  122. }
  123. /* Set signal mask for the new process in ATTR to SIGMASK. */
  124. static inline
  125. int posix_spawnattr_setsigmask (posix_spawnattr_t *__restrict __attr,
  126. const sigset_t *__restrict __sigmask)
  127. {
  128. memcpy (&__attr->__ss, __sigmask, sizeof (sigset_t));
  129. return 0;
  130. }
  131. /* Get flag word from the attribute structure. */
  132. static inline
  133. int posix_spawnattr_getflags (const posix_spawnattr_t *__restrict
  134. __attr,
  135. short int *__restrict __flags)
  136. {
  137. *__flags = __attr->__flags;
  138. return 0;
  139. }
  140. /* Store flags in the attribute structure. */
  141. static inline
  142. int posix_spawnattr_setflags (posix_spawnattr_t *_attr,
  143. short int __flags)
  144. {
  145. /* Check no invalid bits are set. */
  146. if (__flags & ~__POSIX_SPAWN_MASK)
  147. return EINVAL;
  148. _attr->__flags = __flags;
  149. return 0;
  150. }
  151. /* Get process group ID from the attribute structure. */
  152. static inline
  153. int posix_spawnattr_getpgroup (const posix_spawnattr_t *__restrict
  154. __attr, pid_t *__restrict __pgroup)
  155. {
  156. *__pgroup = __attr->__pgrp;
  157. return 0;
  158. }
  159. /* Store process group ID in the attribute structure. */
  160. static inline
  161. int posix_spawnattr_setpgroup (posix_spawnattr_t *__attr,
  162. pid_t __pgroup)
  163. {
  164. __attr->__pgrp = __pgroup;
  165. return 0;
  166. }
  167. /* Get scheduling policy from the attribute structure. */
  168. static inline
  169. int posix_spawnattr_getschedpolicy (const posix_spawnattr_t *
  170. __restrict __attr,
  171. int *__restrict __schedpolicy)
  172. {
  173. *__schedpolicy = __attr->__policy;
  174. return 0;
  175. }
  176. /* Store scheduling policy in the attribute structure. */
  177. static inline
  178. int posix_spawnattr_setschedpolicy (posix_spawnattr_t *__attr,
  179. int __schedpolicy)
  180. {
  181. switch (__schedpolicy) {
  182. case SCHED_OTHER:
  183. case SCHED_FIFO:
  184. case SCHED_RR:
  185. break;
  186. default:
  187. return EINVAL;
  188. }
  189. __attr->__policy = __schedpolicy;
  190. return 0;
  191. }
  192. /* Get scheduling parameters from the attribute structure. */
  193. static inline
  194. int posix_spawnattr_getschedparam (const posix_spawnattr_t *
  195. __restrict __attr,
  196. struct sched_param *__restrict
  197. __schedparam)
  198. {
  199. memcpy (__schedparam, &__attr->__sp, sizeof (__attr->__sp));
  200. return 0;
  201. }
  202. /* Store scheduling parameters in the attribute structure. */
  203. static inline
  204. int posix_spawnattr_setschedparam (posix_spawnattr_t *__restrict __attr,
  205. const struct sched_param *
  206. __restrict __schedparam)
  207. {
  208. __attr->__sp = *__schedparam;
  209. return 0;
  210. }
  211. /* Initialize data structure for file attribute for `spawn' call. */
  212. static inline
  213. int posix_spawn_file_actions_init (posix_spawn_file_actions_t *
  214. __file_actions)
  215. {
  216. memset (__file_actions, 0, sizeof (*__file_actions));
  217. return 0;
  218. }
  219. /* Free resources associated with FILE-ACTIONS. */
  220. static inline
  221. int posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *
  222. __file_actions)
  223. {
  224. free (__file_actions->__actions);
  225. return 0;
  226. }
  227. /* Add an action to FILE-ACTIONS which tells the implementation to call
  228. `open' for the given file during the `spawn' call. */
  229. extern int posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *
  230. __restrict __file_actions,
  231. int __fd,
  232. const char *__restrict __path,
  233. int __oflag, mode_t __mode)
  234. __THROW;
  235. /* Add an action to FILE-ACTIONS which tells the implementation to call
  236. `close' for the given file descriptor during the `spawn' call. */
  237. extern int posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *
  238. __file_actions, int __fd)
  239. __THROW;
  240. /* Add an action to FILE-ACTIONS which tells the implementation to call
  241. `dup2' for the given file descriptors during the `spawn' call. */
  242. extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *
  243. __file_actions,
  244. int __fd, int __newfd) __THROW;
  245. __END_DECLS
  246. #endif /* spawn.h */