bump version to 1.0.28
[uclibc-ng.git] / include / spawn.h
blob3de375b41f88f297b602f1586ddea8232b8486e6
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.
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.
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.
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/>. */
19 #ifndef _SPAWN_H
20 #define _SPAWN_H 1
22 #include <features.h>
23 #include <sched.h>
24 #define __need_sigset_t
25 #include <signal.h>
26 #include <sys/types.h>
28 /* For the tiny inlines (errno/free/memset). */
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
34 /* Data structure to contain attributes for thread creation. */
35 typedef struct
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;
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
51 int __allocated;
52 int __used;
53 struct __spawn_action *__actions;
54 int __pad[16];
55 } posix_spawn_file_actions_t;
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
69 __BEGIN_DECLS
71 /* Spawn a new process executing PATH with the attributes describes in *ATTRP.
72 Before running the process perform the actions described in FILE-ACTIONS.
74 This function is a possible cancellation point and therefore not
75 marked with __THROW. */
76 extern int posix_spawn (pid_t *__restrict __pid,
77 const char *__restrict __path,
78 const posix_spawn_file_actions_t *__restrict
79 __file_actions,
80 const posix_spawnattr_t *__restrict __attrp,
81 char *const __argv[__restrict_arr],
82 char *const __envp[__restrict_arr]);
84 /* Similar to `posix_spawn' but search for FILE in the PATH.
86 This function is a possible cancellation point and therefore not
87 marked with __THROW. */
88 extern int posix_spawnp (pid_t *__pid, const char *__file,
89 const posix_spawn_file_actions_t *__file_actions,
90 const posix_spawnattr_t *__attrp,
91 char *const __argv[], char *const __envp[]);
94 /* Initialize data structure with attributes for `spawn' to default values. */
95 static inline
96 int posix_spawnattr_init (posix_spawnattr_t *__attr)
98 memset (__attr, 0, sizeof (*__attr));
99 return 0;
102 /* Free resources associated with ATTR. */
103 static inline
104 int posix_spawnattr_destroy (posix_spawnattr_t *__attr)
106 return 0;
109 /* Store signal mask for signals with default handling from ATTR in
110 SIGDEFAULT. */
111 static inline
112 int posix_spawnattr_getsigdefault (const posix_spawnattr_t *
113 __restrict __attr,
114 sigset_t *__restrict __sigdefault)
116 memcpy (__sigdefault, &__attr->__sd, sizeof (sigset_t));
117 return 0;
120 /* Set signal mask for signals with default handling in ATTR to SIGDEFAULT. */
121 static inline
122 int posix_spawnattr_setsigdefault (posix_spawnattr_t *__restrict __attr,
123 const sigset_t *__restrict
124 __sigdefault)
126 memcpy (&__attr->__sd, __sigdefault, sizeof (sigset_t));
127 return 0;
130 /* Store signal mask for the new process from ATTR in SIGMASK. */
131 static inline
132 int posix_spawnattr_getsigmask (const posix_spawnattr_t *__restrict
133 __attr,
134 sigset_t *__restrict __sigmask)
136 memcpy (__sigmask, &__attr->__ss, sizeof (sigset_t));
137 return 0;
140 /* Set signal mask for the new process in ATTR to SIGMASK. */
141 static inline
142 int posix_spawnattr_setsigmask (posix_spawnattr_t *__restrict __attr,
143 const sigset_t *__restrict __sigmask)
145 memcpy (&__attr->__ss, __sigmask, sizeof (sigset_t));
146 return 0;
149 /* Get flag word from the attribute structure. */
150 static inline
151 int posix_spawnattr_getflags (const posix_spawnattr_t *__restrict
152 __attr,
153 short int *__restrict __flags)
155 *__flags = __attr->__flags;
156 return 0;
159 /* Store flags in the attribute structure. */
160 static inline
161 int posix_spawnattr_setflags (posix_spawnattr_t *_attr,
162 short int __flags)
164 #ifdef POSIX_SPAWN_USEVFORK
165 # define __POSIX_SPAWN_USEVFORK POSIX_SPAWN_USEVFORK
166 #else
167 # define __POSIX_SPAWN_USEVFORK 0
168 #endif
169 #define __POSIX_SPAWN_MASK (POSIX_SPAWN_RESETIDS \
170 | POSIX_SPAWN_SETPGROUP \
171 | POSIX_SPAWN_SETSIGDEF \
172 | POSIX_SPAWN_SETSIGMASK \
173 | POSIX_SPAWN_SETSCHEDPARAM \
174 | POSIX_SPAWN_SETSCHEDULER \
175 | __POSIX_SPAWN_USEVFORK)
177 /* Check no invalid bits are set. */
178 if (__flags & ~__POSIX_SPAWN_MASK)
179 return EINVAL;
181 _attr->__flags = __flags;
182 return 0;
183 #undef __POSIX_SPAWN_USEVFORK
184 #undef __POSIX_SPAWN_MASK
187 /* Get process group ID from the attribute structure. */
188 static inline
189 int posix_spawnattr_getpgroup (const posix_spawnattr_t *__restrict
190 __attr, pid_t *__restrict __pgroup)
192 *__pgroup = __attr->__pgrp;
193 return 0;
196 /* Store process group ID in the attribute structure. */
197 static inline
198 int posix_spawnattr_setpgroup (posix_spawnattr_t *__attr,
199 pid_t __pgroup)
201 __attr->__pgrp = __pgroup;
202 return 0;
205 /* Get scheduling policy from the attribute structure. */
206 static inline
207 int posix_spawnattr_getschedpolicy (const posix_spawnattr_t *
208 __restrict __attr,
209 int *__restrict __schedpolicy)
211 *__schedpolicy = __attr->__policy;
212 return 0;
215 /* Store scheduling policy in the attribute structure. */
216 static inline
217 int posix_spawnattr_setschedpolicy (posix_spawnattr_t *__attr,
218 int __schedpolicy)
220 switch (__schedpolicy) {
221 case SCHED_OTHER:
222 case SCHED_FIFO:
223 case SCHED_RR:
224 break;
225 default:
226 return EINVAL;
229 __attr->__policy = __schedpolicy;
230 return 0;
233 /* Get scheduling parameters from the attribute structure. */
234 static inline
235 int posix_spawnattr_getschedparam (const posix_spawnattr_t *
236 __restrict __attr,
237 struct sched_param *__restrict
238 __schedparam)
240 memcpy (__schedparam, &__attr->__sp, sizeof (__attr->__sp));
241 return 0;
244 /* Store scheduling parameters in the attribute structure. */
245 static inline
246 int posix_spawnattr_setschedparam (posix_spawnattr_t *__restrict __attr,
247 const struct sched_param *
248 __restrict __schedparam)
250 __attr->__sp = *__schedparam;
251 return 0;
254 /* Initialize data structure for file attribute for `spawn' call. */
255 static inline
256 int posix_spawn_file_actions_init (posix_spawn_file_actions_t *
257 __file_actions)
259 memset (__file_actions, 0, sizeof (*__file_actions));
260 return 0;
263 /* Free resources associated with FILE-ACTIONS. */
264 static inline
265 int posix_spawn_file_actions_destroy (posix_spawn_file_actions_t *
266 __file_actions)
268 free (__file_actions->__actions);
269 return 0;
272 /* Add an action to FILE-ACTIONS which tells the implementation to call
273 `open' for the given file during the `spawn' call. */
274 extern int posix_spawn_file_actions_addopen (posix_spawn_file_actions_t *
275 __restrict __file_actions,
276 int __fd,
277 const char *__restrict __path,
278 int __oflag, mode_t __mode)
279 __THROW;
281 /* Add an action to FILE-ACTIONS which tells the implementation to call
282 `close' for the given file descriptor during the `spawn' call. */
283 extern int posix_spawn_file_actions_addclose (posix_spawn_file_actions_t *
284 __file_actions, int __fd)
285 __THROW;
287 /* Add an action to FILE-ACTIONS which tells the implementation to call
288 `dup2' for the given file descriptors during the `spawn' call. */
289 extern int posix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *
290 __file_actions,
291 int __fd, int __newfd) __THROW;
293 __END_DECLS
295 #endif /* spawn.h */