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