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