spawn.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <errno.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <features.h>
  21. #include <sched.h>
  22. #define __need_sigset_t
  23. #include <signal.h>
  24. #include <sys/types.h>
  25. /* Data structure to contain attributes for thread creation. */
  26. typedef struct {
  27. short int __flags;
  28. pid_t __pgrp;
  29. sigset_t __sd;
  30. sigset_t __ss;
  31. struct sched_param __sp;
  32. int __policy;
  33. int __pad[16];
  34. } posix_spawnattr_t;
  35. /* Data structure to contain information about the actions to be
  36. performed in the new process with respect to file descriptors. */
  37. typedef struct {
  38. int __allocated;
  39. int __used;
  40. struct __spawn_action *__actions;
  41. int __pad[16];
  42. } posix_spawn_file_actions_t;
  43. /* Flags to be set in the `posix_spawnattr_t'. */
  44. #define POSIX_SPAWN_RESETIDS 0x01
  45. #define POSIX_SPAWN_SETPGROUP 0x02
  46. #define POSIX_SPAWN_SETSIGDEF 0x04
  47. #define POSIX_SPAWN_SETSIGMASK 0x08
  48. #define POSIX_SPAWN_SETSCHEDPARAM 0x10
  49. #define POSIX_SPAWN_SETSCHEDULER 0x20
  50. #define POSIX_SPAWN_USEVFORK 0x40 /* GNU extension */
  51. #define __POSIX_SPAWN_MASK (POSIX_SPAWN_RESETIDS \
  52. | POSIX_SPAWN_SETPGROUP \
  53. | POSIX_SPAWN_SETSIGDEF \
  54. | POSIX_SPAWN_SETSIGMASK \
  55. | POSIX_SPAWN_SETSCHEDPARAM \
  56. | POSIX_SPAWN_SETSCHEDULER \
  57. | POSIX_SPAWN_USEVFORK)
  58. __BEGIN_DECLS
  59. /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
  60. Before running the process perform the actions described in FILE-ACTIONS.
  61. This function is a possible cancellation point and therefore not
  62. marked with __THROW. */
  63. int posix_spawn(pid_t * restrict pid, const char * restrict path,
  64. const posix_spawn_file_actions_t * restrict file_actions,
  65. const posix_spawnattr_t * restrict attrp,
  66. char * const argv[restrict],
  67. char * const envp[restrict]);
  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. 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. inline static int
  77. 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. inline static int
  84. 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. inline static int
  91. posix_spawnattr_getsigdefault(const posix_spawnattr_t *attr,
  92. sigset_t *sigdefault)
  93. {
  94. memcpy(sigdefault, &attr->__sd, sizeof(sigset_t));
  95. return 0;
  96. }
  97. /* Set signal mask for signals with default handling in ATTR to SIGDEFAULT. */
  98. inline static int
  99. posix_spawnattr_setsigdefault(posix_spawnattr_t *attr,
  100. const sigset_t *sigdefault)
  101. {
  102. memcpy(&attr->__sd, sigdefault, sizeof(sigset_t));
  103. return 0;
  104. }
  105. /* Store signal mask for the new process from ATTR in SIGMASK. */
  106. inline static int
  107. posix_spawnattr_getsigmask(const posix_spawnattr_t *attr,
  108. sigset_t *sigmask)
  109. {
  110. memcpy(sigmask, &attr->__ss, sizeof(sigset_t));
  111. return 0;
  112. }
  113. /* Set signal mask for the new process in ATTR to SIGMASK. */
  114. inline static int
  115. posix_spawnattr_setsigmask(posix_spawnattr_t *attr,
  116. const sigset_t *sigmask)
  117. {
  118. memcpy(&attr->__ss, sigmask, sizeof(sigset_t));
  119. return 0;
  120. }
  121. /* Get flag word from the attribute structure. */
  122. inline static int
  123. posix_spawnattr_getflags(const posix_spawnattr_t *attr, short int *flags)
  124. {
  125. *flags = attr->__flags;
  126. return 0;
  127. }
  128. /* Store flags in the attribute structure. */
  129. inline static int
  130. posix_spawnattr_setflags(posix_spawnattr_t *attr, short int flags)
  131. {
  132. /* Check no invalid bits are set. */
  133. if (flags & ~__POSIX_SPAWN_MASK)
  134. return EINVAL;
  135. attr->__flags = flags;
  136. return 0;
  137. }
  138. /* Get process group ID from the attribute structure. */
  139. inline static int
  140. posix_spawnattr_getpgroup(const posix_spawnattr_t *attr, pid_t *pgroup)
  141. {
  142. *pgroup = attr->__pgrp;
  143. return 0;
  144. }
  145. /* Store process group ID in the attribute structure. */
  146. inline static int
  147. posix_spawnattr_setpgroup(posix_spawnattr_t *attr, pid_t pgroup)
  148. {
  149. attr->__pgrp = pgroup;
  150. return 0;
  151. }
  152. /* Get scheduling policy from the attribute structure. */
  153. inline static int
  154. posix_spawnattr_getschedpolicy(const posix_spawnattr_t *attr,
  155. int *schedpolicy)
  156. {
  157. *schedpolicy = attr->__policy;
  158. return 0;
  159. }
  160. /* Store scheduling policy in the attribute structure. */
  161. inline static int
  162. posix_spawnattr_setschedpolicy(posix_spawnattr_t *attr, int schedpolicy)
  163. {
  164. switch (schedpolicy) {
  165. case SCHED_OTHER:
  166. case SCHED_FIFO:
  167. case SCHED_RR:
  168. break;
  169. default:
  170. return EINVAL;
  171. }
  172. attr->__policy = schedpolicy;
  173. return 0;
  174. }
  175. /* Get scheduling parameters from the attribute structure. */
  176. static inline int
  177. posix_spawnattr_getschedparam(const posix_spawnattr_t *attr,
  178. struct sched_param *schedparam)
  179. {
  180. memcpy(schedparam, &attr->__sp, sizeof(attr->__sp));
  181. return 0;
  182. }
  183. /* Store scheduling parameters in the attribute structure. */
  184. static inline int
  185. posix_spawnattr_setschedparam(posix_spawnattr_t *attr,
  186. const struct sched_param *schedparam)
  187. {
  188. attr->__sp = *schedparam;
  189. return 0;
  190. }
  191. /* Initialize data structure for file attribute for `spawn' call. */
  192. inline static int
  193. posix_spawn_file_actions_init(posix_spawn_file_actions_t *file_actions)
  194. {
  195. memset(file_actions, 0, sizeof(*file_actions));
  196. return 0;
  197. }
  198. /* Free resources associated with FILE-ACTIONS. */
  199. inline static int
  200. posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *file_actions)
  201. {
  202. free(file_actions->__actions);
  203. return 0;
  204. }
  205. /* Add an action to FILE-ACTIONS which tells the implementation to call
  206. `open' for the given file during the `spawn' call. */
  207. int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * restrict
  208. file_actions, int fd,
  209. const char * restrict path,
  210. int oflag, mode_t mode)
  211. __THROW;
  212. /* Add an action to FILE-ACTIONS which tells the implementation to call
  213. `close' for the given file descriptor during the `spawn' call. */
  214. int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *file_actions,
  215. int fd)
  216. __THROW;
  217. /* Add an action to FILE-ACTIONS which tells the implementation to call
  218. `dup2' for the given file descriptors during the `spawn' call. */
  219. int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *file_actions,
  220. int fd, int newfd) __THROW;
  221. __END_DECLS
  222. #endif /* spawn.h */