| 1 |
.\" Copyright 2010, Michael Kerrisk <[email protected]> |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH posix_spawn 3 2026-02-08 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
posix_spawn, posix_spawnp \- spawn a process |
| 9 |
.SH LIBRARY |
| 10 |
Standard C library |
| 11 |
.RI ( libc ,\~ \-lc ) |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
.B #include <spawn.h> |
| 15 |
.P |
| 16 |
.BI "int posix_spawn(pid_t *restrict " pid ", const char *restrict " path , |
| 17 |
.BI " const posix_spawn_file_actions_t *restrict " file_actions , |
| 18 |
.BI " const posix_spawnattr_t *restrict " attrp , |
| 19 |
.BI " char *const " argv [restrict], |
| 20 |
.BI " char *const " envp [restrict]); |
| 21 |
.BI "int posix_spawnp(pid_t *restrict " pid ", const char *restrict " file , |
| 22 |
.BI " const posix_spawn_file_actions_t *restrict " file_actions , |
| 23 |
.BI " const posix_spawnattr_t *restrict " attrp , |
| 24 |
.BI " char *const " argv [restrict], |
| 25 |
.BI " char *const " envp [restrict]); |
| 26 |
.fi |
| 27 |
.SH DESCRIPTION |
| 28 |
The |
| 29 |
.BR posix_spawn () |
| 30 |
and |
| 31 |
.BR posix_spawnp () |
| 32 |
functions are used to create a new child process that executes |
| 33 |
a specified file. |
| 34 |
These functions were specified by POSIX to provide a standardized method |
| 35 |
of creating new processes on machines that lack the capability |
| 36 |
to support the |
| 37 |
.BR fork (2) |
| 38 |
system call. |
| 39 |
These machines are generally small, embedded systems lacking MMU support. |
| 40 |
.P |
| 41 |
The |
| 42 |
.BR posix_spawn () |
| 43 |
and |
| 44 |
.BR posix_spawnp () |
| 45 |
functions provide the functionality of a combined |
| 46 |
.BR fork (2) |
| 47 |
and |
| 48 |
.BR exec (3), |
| 49 |
with some optional housekeeping steps in the child process before the |
| 50 |
.BR exec (3). |
| 51 |
These functions are not meant to replace the |
| 52 |
.BR fork (2) |
| 53 |
and |
| 54 |
.BR execve (2) |
| 55 |
system calls. |
| 56 |
In fact, they provide only a subset of the functionality |
| 57 |
that can be achieved by using the system calls. |
| 58 |
.P |
| 59 |
The only difference between |
| 60 |
.BR posix_spawn () |
| 61 |
and |
| 62 |
.BR posix_spawnp () |
| 63 |
is the manner in which they specify the file to be executed by |
| 64 |
the child process. |
| 65 |
With |
| 66 |
.BR posix_spawn (), |
| 67 |
the executable file is specified as a pathname |
| 68 |
(which can be absolute or relative). |
| 69 |
With |
| 70 |
.BR posix_spawnp (), |
| 71 |
the executable file is specified as a simple filename; |
| 72 |
the system searches for this file in the list of directories specified by |
| 73 |
.B PATH |
| 74 |
(in the same way as for |
| 75 |
.BR execvp (3)). |
| 76 |
For the remainder of this page, the discussion is phrased in terms of |
| 77 |
.BR posix_spawn (), |
| 78 |
with the understanding that |
| 79 |
.BR posix_spawnp () |
| 80 |
differs only on the point just described. |
| 81 |
.P |
| 82 |
The remaining arguments to these two functions are as follows: |
| 83 |
.TP |
| 84 |
.I pid |
| 85 |
points to a buffer that is used to return the process ID |
| 86 |
of the new child process. |
| 87 |
.TP |
| 88 |
.I file_actions |
| 89 |
points to a |
| 90 |
.I "spawn file actions object" |
| 91 |
that specifies file-related actions to be performed in the child |
| 92 |
between the |
| 93 |
.BR fork (2) |
| 94 |
and |
| 95 |
.BR exec (3) |
| 96 |
steps. |
| 97 |
This object is initialized and populated before the |
| 98 |
.BR posix_spawn () |
| 99 |
call using |
| 100 |
.BR posix_spawn_file_actions_init (3) |
| 101 |
and the |
| 102 |
.BR posix_spawn_file_actions_* () |
| 103 |
functions. |
| 104 |
.TP |
| 105 |
.I attrp |
| 106 |
points to an |
| 107 |
.I attributes objects |
| 108 |
that specifies various attributes of the created child process. |
| 109 |
This object is initialized and populated before the |
| 110 |
.BR posix_spawn () |
| 111 |
call using |
| 112 |
.BR posix_spawnattr_init (3) |
| 113 |
and the |
| 114 |
.BR posix_spawnattr_* () |
| 115 |
functions. |
| 116 |
.TP |
| 117 |
.I argv |
| 118 |
.TQ |
| 119 |
.I envp |
| 120 |
specify the argument list and environment for the program |
| 121 |
that is executed in the child process, as for |
| 122 |
.BR execve (2). |
| 123 |
.P |
| 124 |
Below, the functions are described in terms of a three-step process: the |
| 125 |
.BR fork () |
| 126 |
step, the |
| 127 |
.RB pre- exec () |
| 128 |
step (executed in the child), |
| 129 |
and the |
| 130 |
.BR exec () |
| 131 |
step (executed in the child). |
| 132 |
.SS fork() step |
| 133 |
Since glibc 2.24, the |
| 134 |
.BR posix_spawn () |
| 135 |
function commences by calling |
| 136 |
.BR clone (2) |
| 137 |
with |
| 138 |
.B CLONE_VM |
| 139 |
and |
| 140 |
.B CLONE_VFORK |
| 141 |
flags. |
| 142 |
Older implementations use |
| 143 |
.BR fork (2), |
| 144 |
or possibly |
| 145 |
.BR vfork (2) |
| 146 |
(see below). |
| 147 |
.P |
| 148 |
The PID of the new child process is placed in |
| 149 |
.IR *pid . |
| 150 |
The |
| 151 |
.BR posix_spawn () |
| 152 |
function then returns control to the parent process. |
| 153 |
.P |
| 154 |
Subsequently, the parent can use one of the system calls described in |
| 155 |
.BR wait (2) |
| 156 |
to check the status of the child process. |
| 157 |
If the child fails in any of the housekeeping steps described below, |
| 158 |
or fails to execute the desired file, |
| 159 |
it exits with a status of 127. |
| 160 |
.P |
| 161 |
Before glibc 2.24, the child process is created using |
| 162 |
.BR vfork (2) |
| 163 |
instead of |
| 164 |
.BR fork (2) |
| 165 |
when either of the following is true: |
| 166 |
.IP \[bu] 3 |
| 167 |
the |
| 168 |
.I spawn-flags |
| 169 |
element of the attributes object pointed to by |
| 170 |
.I attrp |
| 171 |
contains the GNU-specific flag |
| 172 |
.BR POSIX_SPAWN_USEVFORK ; |
| 173 |
or |
| 174 |
.IP \[bu] |
| 175 |
.I file_actions |
| 176 |
is NULL and the |
| 177 |
.I spawn-flags |
| 178 |
element of the attributes object pointed to by |
| 179 |
.I attrp |
| 180 |
does |
| 181 |
.I not |
| 182 |
contain |
| 183 |
.BR POSIX_SPAWN_SETSIGMASK , |
| 184 |
.BR POSIX_SPAWN_SETSIGDEF , |
| 185 |
.BR POSIX_SPAWN_SETSCHEDPARAM , |
| 186 |
.BR POSIX_SPAWN_SETSCHEDULER , |
| 187 |
.BR POSIX_SPAWN_SETPGROUP , |
| 188 |
or |
| 189 |
.BR POSIX_SPAWN_RESETIDS . |
| 190 |
.P |
| 191 |
In other words, |
| 192 |
.BR vfork (2) |
| 193 |
is used if the caller requests it, |
| 194 |
or if there is no cleanup expected in the child before it |
| 195 |
.BR exec (3)s |
| 196 |
the requested file. |
| 197 |
.SS pre-exec() step: housekeeping |
| 198 |
In between the |
| 199 |
.B fork() |
| 200 |
and the |
| 201 |
.B exec() |
| 202 |
steps, a child process may need to perform a set of housekeeping actions. |
| 203 |
The |
| 204 |
.BR posix_spawn () |
| 205 |
and |
| 206 |
.BR posix_spawnp () |
| 207 |
functions support a small, well-defined set of system tasks that the child |
| 208 |
process can accomplish before it executes the executable file. |
| 209 |
These operations are controlled by the attributes object pointed to by |
| 210 |
.I attrp |
| 211 |
and the file actions object pointed to by |
| 212 |
.IR file_actions . |
| 213 |
In the child, processing is done in the following sequence: |
| 214 |
.IP (1) 5 |
| 215 |
Process attribute actions: signal mask, signal default handlers, |
| 216 |
scheduling algorithm and parameters, |
| 217 |
process group, and effective user and group IDs |
| 218 |
are changed as specified by the attributes object pointed to by |
| 219 |
.IR attrp . |
| 220 |
.IP (2) |
| 221 |
File actions, as specified in the |
| 222 |
.I file_actions |
| 223 |
argument, |
| 224 |
are performed in the order that they were specified using calls to the |
| 225 |
.BR posix_spawn_file_actions_add* () |
| 226 |
functions. |
| 227 |
.IP (3) |
| 228 |
File descriptors with the |
| 229 |
.B FD_CLOEXEC |
| 230 |
flag set are closed. |
| 231 |
.P |
| 232 |
All process attributes in the child, |
| 233 |
other than those affected by attributes specified in the |
| 234 |
object pointed to by |
| 235 |
.I attrp |
| 236 |
and the file actions in the object pointed to by |
| 237 |
.IR file_actions , |
| 238 |
will be affected as though the child was created with |
| 239 |
.BR fork (2) |
| 240 |
and it executed the program with |
| 241 |
.BR execve (2). |
| 242 |
.P |
| 243 |
The process attributes actions are defined by the attributes object |
| 244 |
pointed to by |
| 245 |
.IR attrp . |
| 246 |
The |
| 247 |
.I spawn-flags |
| 248 |
attribute (set using |
| 249 |
.BR posix_spawnattr_setflags (3)) |
| 250 |
controls the general actions that occur, |
| 251 |
and other attributes in the object specify values |
| 252 |
to be used during those actions. |
| 253 |
.P |
| 254 |
The effects of the flags that may be specified in |
| 255 |
.I spawn-flags |
| 256 |
are as follows: |
| 257 |
.TP |
| 258 |
.B POSIX_SPAWN_SETSIGMASK |
| 259 |
Set the signal mask to the signal set specified in the |
| 260 |
.I spawn-sigmask |
| 261 |
attribute |
| 262 |
.\" FIXME . |
| 263 |
.\" (see |
| 264 |
.\" .BR posix_spawnattr_setsigmask (3)) |
| 265 |
of the object pointed to by |
| 266 |
.IR attrp . |
| 267 |
If the |
| 268 |
.B POSIX_SPAWN_SETSIGMASK |
| 269 |
flag is not set, then the child inherits the parent's signal mask. |
| 270 |
.TP |
| 271 |
.B POSIX_SPAWN_SETSIGDEF |
| 272 |
Reset the disposition of all signals in the set specified in the |
| 273 |
.I spawn-sigdefault |
| 274 |
attribute |
| 275 |
.\" FIXME . |
| 276 |
.\" (see |
| 277 |
.\" .BR posix_spawnattr_setsigdefault (3)) |
| 278 |
of the object pointed to by |
| 279 |
.I attrp |
| 280 |
to the default. |
| 281 |
For the treatment of the dispositions of signals not specified in the |
| 282 |
.I spawn-sigdefault |
| 283 |
attribute, or the treatment when |
| 284 |
.B POSIX_SPAWN_SETSIGDEF |
| 285 |
is not specified, see |
| 286 |
.BR execve (2). |
| 287 |
.TP |
| 288 |
.B POSIX_SPAWN_SETSCHEDPARAM |
| 289 |
.\" (POSIX_PRIORITY_SCHEDULING only) |
| 290 |
If this flag is set, and the |
| 291 |
.B POSIX_SPAWN_SETSCHEDULER |
| 292 |
flag is not set, then set the scheduling parameters |
| 293 |
to the parameters specified in the |
| 294 |
.I spawn-schedparam |
| 295 |
attribute |
| 296 |
.\" FIXME . |
| 297 |
.\" (see |
| 298 |
.\" .BR posix_spawnattr_setschedparam (3)) |
| 299 |
of the object pointed to by |
| 300 |
.IR attrp . |
| 301 |
.TP |
| 302 |
.B POSIX_SPAWN_SETSCHEDULER |
| 303 |
Set the scheduling policy algorithm and parameters of the child, |
| 304 |
as follows: |
| 305 |
.RS |
| 306 |
.IP \[bu] 3 |
| 307 |
The scheduling policy is set to the value specified in the |
| 308 |
.I spawn-schedpolicy |
| 309 |
attribute |
| 310 |
.\" FIXME . |
| 311 |
.\" (see |
| 312 |
.\" .BR posix_spawnattr_setpolicy (3)) |
| 313 |
of the object pointed to by |
| 314 |
.IR attrp . |
| 315 |
.IP \[bu] |
| 316 |
The scheduling parameters are set to the value specified in the |
| 317 |
.I spawn-schedparam |
| 318 |
attribute |
| 319 |
.\" FIXME . |
| 320 |
.\" (see |
| 321 |
.\" .BR posix_spawnattr_setschedparam (3)) |
| 322 |
of the object pointed to by |
| 323 |
.I attrp |
| 324 |
(but see BUGS). |
| 325 |
.P |
| 326 |
If the |
| 327 |
.B POSIX_SPAWN_SETSCHEDPARAM |
| 328 |
and |
| 329 |
.B POSIX_SPAWN_SETSCHEDPOLICY |
| 330 |
flags are not specified, |
| 331 |
the child inherits the corresponding scheduling attributes from the parent. |
| 332 |
.RE |
| 333 |
.TP |
| 334 |
.B POSIX_SPAWN_RESETIDS |
| 335 |
If this flag is set, |
| 336 |
reset the effective UID and GID to the |
| 337 |
real UID and GID of the parent process. |
| 338 |
If this flag is not set, |
| 339 |
then the child retains the effective UID and GID of the parent. |
| 340 |
In either case, if the set-user-ID and set-group-ID permission |
| 341 |
bits are enabled on the executable file, their effect will override |
| 342 |
the setting of the effective UID and GID (se |
| 343 |
.BR execve (2)). |
| 344 |
.TP |
| 345 |
.B POSIX_SPAWN_SETPGROUP |
| 346 |
Set the process group to the value specified in the |
| 347 |
.I spawn-pgroup |
| 348 |
attribute |
| 349 |
.\" FIXME . |
| 350 |
.\" (see |
| 351 |
.\" .BR posix_spawnattr_setpgroup (3)) |
| 352 |
of the object pointed to by |
| 353 |
.IR attrp . |
| 354 |
If the |
| 355 |
.I spawn-pgroup |
| 356 |
attribute has the value 0, |
| 357 |
the child's process group ID is made the same as its process ID. |
| 358 |
If the |
| 359 |
.B POSIX_SPAWN_SETPGROUP |
| 360 |
flag is not set, the child inherits the parent's process group ID. |
| 361 |
.TP |
| 362 |
.B POSIX_SPAWN_USEVFORK |
| 363 |
Since glibc 2.24, this flag has no effect. |
| 364 |
On older implementations, setting this flag forces the |
| 365 |
.B fork() |
| 366 |
step to use |
| 367 |
.BR vfork (2) |
| 368 |
instead of |
| 369 |
.BR fork (2). |
| 370 |
The |
| 371 |
.B _GNU_SOURCE |
| 372 |
feature test macro must be defined to obtain the definition of this constant. |
| 373 |
.TP |
| 374 |
.BR POSIX_SPAWN_SETSID " (since glibc 2.26)" |
| 375 |
If this flag is set, |
| 376 |
the child process shall create a new session and become the session leader. |
| 377 |
The child process shall also become the process group leader of the new process |
| 378 |
group in the session (see |
| 379 |
.BR setsid (2)). |
| 380 |
The |
| 381 |
.B _GNU_SOURCE |
| 382 |
feature test macro must be defined to obtain the definition of this constant. |
| 383 |
.\" This flag has been accepted in POSIX, see: |
| 384 |
.\" http://austingroupbugs.net/view.php?id=1044 |
| 385 |
.\" and has been implemented since glibc 2.26 |
| 386 |
.\" commit daeb1fa2e1b33323e719015f5f546988bd4cc73b |
| 387 |
.P |
| 388 |
If |
| 389 |
.I attrp |
| 390 |
is NULL, then the default behaviors described above for each flag apply. |
| 391 |
.\" mtk: I think we probably don't want to say the following, since it |
| 392 |
.\" could lead people to do the wrong thing |
| 393 |
.\" The POSIX standard tells you to call |
| 394 |
.\" this function to de-initialize the attributes object pointed to by |
| 395 |
.\" .I attrp |
| 396 |
.\" when you are done with it; |
| 397 |
.\" however, on Linux systems this operation is a no-op. |
| 398 |
.P |
| 399 |
The |
| 400 |
.I file_actions |
| 401 |
argument specifies a sequence of file operations |
| 402 |
that are performed in the child process after |
| 403 |
the general processing described above, and before it performs the |
| 404 |
.BR exec (3). |
| 405 |
If |
| 406 |
.I file_actions |
| 407 |
is NULL, then no special action is taken, and standard |
| 408 |
.BR exec (3) |
| 409 |
semantics apply\[em]file descriptors open before the exec |
| 410 |
remain open in the new process, |
| 411 |
except those for which the |
| 412 |
.B FD_CLOEXEC |
| 413 |
flag has been set. |
| 414 |
File locks remain in place. |
| 415 |
.P |
| 416 |
If |
| 417 |
.I file_actions |
| 418 |
is not NULL, then it contains an ordered set of requests to |
| 419 |
.BR open (2), |
| 420 |
.BR close (2), |
| 421 |
and |
| 422 |
.BR dup2 (2) |
| 423 |
files. |
| 424 |
These requests are added to the |
| 425 |
.I file_actions |
| 426 |
by |
| 427 |
.BR posix_spawn_file_actions_addopen (3), |
| 428 |
.BR posix_spawn_file_actions_addclose (3), |
| 429 |
and |
| 430 |
.BR posix_spawn_file_actions_adddup2 (3). |
| 431 |
The requested operations are performed in the order they were added to |
| 432 |
.IR file_actions . |
| 433 |
.\" FIXME . I think the following is best placed in the |
| 434 |
.\" posix_spawn_file_actions_adddup2(3) page, and a similar statement is |
| 435 |
.\" also needed in posix_spawn_file_actions_addclose(3) |
| 436 |
.\" Note that you can specify file descriptors in |
| 437 |
.\" .I posix_spawn_file_actions_adddup2 (3) |
| 438 |
.\" which would not be usable if you called |
| 439 |
.\" .BR dup2 (2) |
| 440 |
.\" at that time--i.e., file descriptors that are opened or |
| 441 |
.\" closed by the earlier operations |
| 442 |
.\" added to |
| 443 |
.\" .I file_actions . |
| 444 |
.P |
| 445 |
If any of the housekeeping actions fails |
| 446 |
(due to bogus values being passed or other reasons why signal handling, |
| 447 |
process scheduling, process group ID functions, |
| 448 |
and file descriptor operations might fail), |
| 449 |
the child process exits with exit value 127. |
| 450 |
.SS exec() step |
| 451 |
Once the child has successfully forked and performed |
| 452 |
all requested pre-exec steps, |
| 453 |
the child runs the requested executable. |
| 454 |
.P |
| 455 |
The child process takes its environment from the |
| 456 |
.I envp |
| 457 |
argument, which is interpreted as if it had been passed to |
| 458 |
.BR execve (2). |
| 459 |
The arguments to the created process come from the |
| 460 |
.I argv |
| 461 |
argument, which is processed as for |
| 462 |
.BR execve (2). |
| 463 |
.SH RETURN VALUE |
| 464 |
Upon successful completion, |
| 465 |
.BR posix_spawn () |
| 466 |
and |
| 467 |
.BR posix_spawnp () |
| 468 |
place the PID of the child process in |
| 469 |
.IR pid , |
| 470 |
and return 0. |
| 471 |
If there is an error during the |
| 472 |
.B fork() |
| 473 |
step, |
| 474 |
then no child is created, |
| 475 |
the contents of |
| 476 |
.I *pid |
| 477 |
are unspecified, |
| 478 |
and these functions return an error number as described below. |
| 479 |
.P |
| 480 |
Even when these functions return a success status, |
| 481 |
the child process may still fail for a plethora of reasons related to its |
| 482 |
.RB pre- exec () |
| 483 |
initialization. |
| 484 |
In addition, the |
| 485 |
.BR exec (3) |
| 486 |
may fail. |
| 487 |
In all of these cases, the child process will exit with the exit value of 127. |
| 488 |
.SH ERRORS |
| 489 |
The |
| 490 |
.BR posix_spawn () |
| 491 |
and |
| 492 |
.BR posix_spawnp () |
| 493 |
functions fail only in the case where the underlying |
| 494 |
.BR fork (2), |
| 495 |
.BR vfork (2), |
| 496 |
or |
| 497 |
.BR clone (2) |
| 498 |
call fails; in these cases, these functions return an error number, |
| 499 |
which will be one of the errors described for |
| 500 |
.BR fork (2), |
| 501 |
.BR vfork (2), |
| 502 |
or |
| 503 |
.BR clone (2). |
| 504 |
.P |
| 505 |
In addition, these functions fail if: |
| 506 |
.TP |
| 507 |
.B ENOSYS |
| 508 |
Function not supported on this system. |
| 509 |
.SH STANDARDS |
| 510 |
POSIX.1-2008. |
| 511 |
.SH HISTORY |
| 512 |
glibc 2.2. |
| 513 |
POSIX.1-2001. |
| 514 |
.\" FIXME . This piece belongs in spawnattr_setflags(3) |
| 515 |
.\" The |
| 516 |
.\" .B POSIX_SPAWN_USEVFORK |
| 517 |
.\" flag is a GNU extension; the |
| 518 |
.\" .B _GNU_SOURCE |
| 519 |
.\" feature test macro must be defined (before including any header files) |
| 520 |
.\" to obtain the definition of this constant. |
| 521 |
.SH NOTES |
| 522 |
The housekeeping activities in the child are controlled by |
| 523 |
the objects pointed to by |
| 524 |
.I attrp |
| 525 |
(for non-file actions) and |
| 526 |
.I file_actions |
| 527 |
In POSIX parlance, the |
| 528 |
.I posix_spawnattr_t |
| 529 |
and |
| 530 |
.I posix_spawn_file_actions_t |
| 531 |
data types are referred to as objects, |
| 532 |
and their elements are not specified by name. |
| 533 |
Portable programs should initialize these objects using |
| 534 |
only the POSIX-specified functions. |
| 535 |
(In other words, |
| 536 |
although these objects may be implemented as structures containing fields, |
| 537 |
portable programs must avoid dependence on such implementation details.) |
| 538 |
.P |
| 539 |
According to POSIX, it is unspecified whether fork handlers established with |
| 540 |
.BR pthread_atfork (3) |
| 541 |
are called when |
| 542 |
.BR posix_spawn () |
| 543 |
is invoked. |
| 544 |
Since glibc 2.24, the fork handlers are not executed in any case. |
| 545 |
.\" Tested on glibc 2.12 |
| 546 |
On older implementations, |
| 547 |
fork handlers are called only if the child is created using |
| 548 |
.BR fork (2). |
| 549 |
.P |
| 550 |
There is no "posix_fspawn" function (i.e., a function that is to |
| 551 |
.BR posix_spawn () |
| 552 |
as |
| 553 |
.BR fexecve (3) |
| 554 |
is to |
| 555 |
.BR execve (2)). |
| 556 |
However, this functionality can be obtained by specifying the |
| 557 |
.I path |
| 558 |
argument as one of the files in the caller's |
| 559 |
.I /proc/self/fd |
| 560 |
directory. |
| 561 |
.SH BUGS |
| 562 |
POSIX.1 says that when |
| 563 |
.B POSIX_SPAWN_SETSCHEDULER |
| 564 |
is specified in |
| 565 |
.IR spawn-flags , |
| 566 |
then the |
| 567 |
.B POSIX_SPAWN_SETSCHEDPARAM |
| 568 |
(if present) is ignored. |
| 569 |
However, before glibc 2.14, calls to |
| 570 |
.BR posix_spawn () |
| 571 |
failed with an error if |
| 572 |
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=12052 |
| 573 |
.B POSIX_SPAWN_SETSCHEDULER |
| 574 |
was specified without also specifying |
| 575 |
.BR POSIX_SPAWN_SETSCHEDPARAM . |
| 576 |
.SH EXAMPLES |
| 577 |
The program below demonstrates the use of various functions in the |
| 578 |
POSIX spawn API. |
| 579 |
The program accepts command-line attributes that can be used |
| 580 |
to create file actions and attributes objects. |
| 581 |
The remaining command-line arguments are used as the executable name |
| 582 |
and command-line arguments of the program that is executed in the child. |
| 583 |
.P |
| 584 |
In the first run, the |
| 585 |
.BR date (1) |
| 586 |
command is executed in the child, and the |
| 587 |
.BR posix_spawn () |
| 588 |
call employs no file actions or attributes objects. |
| 589 |
.P |
| 590 |
.in +4n |
| 591 |
.EX |
| 592 |
.RB $ " ./a.out date" ; |
| 593 |
PID of child: 7634 |
| 594 |
Tue Feb 1 19:47:50 CEST 2011 |
| 595 |
Child status: exited, status=0 |
| 596 |
.EE |
| 597 |
.in |
| 598 |
.P |
| 599 |
In the next run, the |
| 600 |
.I \-c |
| 601 |
command-line option is used to create a file actions object that closes |
| 602 |
standard output in the child. |
| 603 |
Consequently, |
| 604 |
.BR date (1) |
| 605 |
fails when trying to perform output and exits with a status of 1. |
| 606 |
.P |
| 607 |
.in +4n |
| 608 |
.EX |
| 609 |
.RB $ " ./a.out \-c date" ; |
| 610 |
PID of child: 7636 |
| 611 |
date: write error: Bad file descriptor |
| 612 |
Child status: exited, status=1 |
| 613 |
.EE |
| 614 |
.in |
| 615 |
.P |
| 616 |
In the next run, the |
| 617 |
.I \-s |
| 618 |
command-line option is used to create an attributes object that |
| 619 |
specifies that all (blockable) signals in the child should be blocked. |
| 620 |
Consequently, trying to kill child with the default signal sent by |
| 621 |
.BR kill (1) |
| 622 |
(i.e., |
| 623 |
.BR SIGTERM ) |
| 624 |
fails, because that signal is blocked. |
| 625 |
Therefore, to kill the child, |
| 626 |
.B SIGKILL |
| 627 |
is necessary |
| 628 |
.RB ( SIGKILL |
| 629 |
can't be blocked). |
| 630 |
.P |
| 631 |
.in +4n |
| 632 |
.EX |
| 633 |
.RB $ " ./a.out \-s sleep 60 &" |
| 634 |
[1] 7637 |
| 635 |
$ PID of child: 7638 |
| 636 |
.P |
| 637 |
.RB $ " kill 7638" ; |
| 638 |
.RB $ " kill \-KILL 7638" ; |
| 639 |
$ Child status: killed by signal 9 |
| 640 |
[1]+ Done ./a.out \-s sleep 60 |
| 641 |
.EE |
| 642 |
.in |
| 643 |
.P |
| 644 |
When we try to execute a nonexistent command in the child, the |
| 645 |
.BR exec (3) |
| 646 |
fails and the child exits with a status of 127. |
| 647 |
.P |
| 648 |
.in +4n |
| 649 |
.EX |
| 650 |
.RB $ " ./a.out xxxxx" ; |
| 651 |
PID of child: 10190 |
| 652 |
Child status: exited, status=127 |
| 653 |
.EE |
| 654 |
.in |
| 655 |
.SS Program source |
| 656 |
\& |
| 657 |
.\" SRC BEGIN (posix_spawn.c) |
| 658 |
.EX |
| 659 |
#include <err.h> |
| 660 |
#include <errno.h> |
| 661 |
#include <spawn.h> |
| 662 |
#include <stdint.h> |
| 663 |
#include <stdio.h> |
| 664 |
#include <stdlib.h> |
| 665 |
#include <string.h> |
| 666 |
#include <unistd.h> |
| 667 |
#include <wait.h> |
| 668 |
\& |
| 669 |
char **environ; |
| 670 |
\& |
| 671 |
int |
| 672 |
main(int argc, char *argv[]) |
| 673 |
{ |
| 674 |
pid_t child_pid; |
| 675 |
int s, opt, status; |
| 676 |
sigset_t mask; |
| 677 |
posix_spawnattr_t attr; |
| 678 |
posix_spawnattr_t *attrp; |
| 679 |
posix_spawn_file_actions_t file_actions; |
| 680 |
posix_spawn_file_actions_t *file_actionsp; |
| 681 |
\& |
| 682 |
/* Parse command\-line options, which can be used to specify an |
| 683 |
attributes object and file actions object for the child. */ |
| 684 |
\& |
| 685 |
attrp = NULL; |
| 686 |
file_actionsp = NULL; |
| 687 |
\& |
| 688 |
while ((opt = getopt(argc, argv, "sc")) != \-1) { |
| 689 |
switch (opt) { |
| 690 |
case \[aq]c\[aq]: /* \-c: close standard output in child */ |
| 691 |
\& |
| 692 |
/* Create a file actions object and add a "close" |
| 693 |
action to it. */ |
| 694 |
\& |
| 695 |
s = posix_spawn_file_actions_init(&file_actions); |
| 696 |
if (s != 0) |
| 697 |
errc(EXIT_FAILURE, s, "posix_spawn_file_actions_init"); |
| 698 |
\& |
| 699 |
s = posix_spawn_file_actions_addclose(&file_actions, |
| 700 |
STDOUT_FILENO); |
| 701 |
if (s != 0) |
| 702 |
errc(EXIT_FAILURE, s, "posix_spawn_file_actions_addclose"); |
| 703 |
\& |
| 704 |
file_actionsp = &file_actions; |
| 705 |
break; |
| 706 |
\& |
| 707 |
case \[aq]s\[aq]: /* \-s: block all signals in child */ |
| 708 |
\& |
| 709 |
/* Create an attributes object and add a "set signal mask" |
| 710 |
action to it. */ |
| 711 |
\& |
| 712 |
s = posix_spawnattr_init(&attr); |
| 713 |
if (s != 0) |
| 714 |
errc(EXIT_FAILURE, s, "posix_spawnattr_init"); |
| 715 |
s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK); |
| 716 |
if (s != 0) |
| 717 |
errc(EXIT_FAILURE, s, "posix_spawnattr_setflags"); |
| 718 |
\& |
| 719 |
sigfillset(&mask); |
| 720 |
s = posix_spawnattr_setsigmask(&attr, &mask); |
| 721 |
if (s != 0) |
| 722 |
errc(EXIT_FAILURE, s, "posix_spawnattr_setsigmask"); |
| 723 |
\& |
| 724 |
attrp = &attr; |
| 725 |
break; |
| 726 |
} |
| 727 |
} |
| 728 |
\& |
| 729 |
if (argv[optind] == NULL) { |
| 730 |
fprintf(stderr, "Usage: %s [\-cs] executable [args]\[rs]n", argv[0]); |
| 731 |
exit(EXIT_FAILURE); |
| 732 |
} |
| 733 |
\& |
| 734 |
/* Spawn the child. The name of the program to execute and the |
| 735 |
command\-line arguments are taken from the command\-line arguments |
| 736 |
of this program. The environment of the program execed in the |
| 737 |
child is made the same as the parent\[aq]s environment. */ |
| 738 |
\& |
| 739 |
s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp, |
| 740 |
&argv[optind], environ); |
| 741 |
if (s != 0) |
| 742 |
errc(EXIT_FAILURE, s, "posix_spawn"); |
| 743 |
\& |
| 744 |
/* Destroy any objects that we created earlier. */ |
| 745 |
\& |
| 746 |
if (attrp != NULL) { |
| 747 |
s = posix_spawnattr_destroy(attrp); |
| 748 |
if (s != 0) |
| 749 |
errc(EXIT_FAILURE, s, "posix_spawnattr_destroy"); |
| 750 |
} |
| 751 |
\& |
| 752 |
if (file_actionsp != NULL) { |
| 753 |
s = posix_spawn_file_actions_destroy(file_actionsp); |
| 754 |
if (s != 0) |
| 755 |
errc(EXIT_FAILURE, s, "posix_spawn_file_actions_destroy"); |
| 756 |
} |
| 757 |
\& |
| 758 |
printf("PID of child: %jd\[rs]n", (intmax_t) child_pid); |
| 759 |
\& |
| 760 |
/* Monitor status of the child until it terminates. */ |
| 761 |
\& |
| 762 |
do { |
| 763 |
s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED); |
| 764 |
if (s == \-1) |
| 765 |
err(EXIT_FAILURE, "waitpid"); |
| 766 |
\& |
| 767 |
printf("Child status: "); |
| 768 |
if (WIFEXITED(status)) { |
| 769 |
printf("exited, status=%d\[rs]n", WEXITSTATUS(status)); |
| 770 |
} else if (WIFSIGNALED(status)) { |
| 771 |
printf("killed by signal %d\[rs]n", WTERMSIG(status)); |
| 772 |
} else if (WIFSTOPPED(status)) { |
| 773 |
printf("stopped by signal %d\[rs]n", WSTOPSIG(status)); |
| 774 |
} else if (WIFCONTINUED(status)) { |
| 775 |
printf("continued\[rs]n"); |
| 776 |
} |
| 777 |
} while (!WIFEXITED(status) && !WIFSIGNALED(status)); |
| 778 |
\& |
| 779 |
exit(EXIT_SUCCESS); |
| 780 |
} |
| 781 |
.EE |
| 782 |
.\" SRC END |
| 783 |
.SH SEE ALSO |
| 784 |
.nh \" Disable hyphenation |
| 785 |
.ad l |
| 786 |
.BR close (2), |
| 787 |
.BR dup2 (2), |
| 788 |
.BR execl (2), |
| 789 |
.BR execlp (2), |
| 790 |
.BR fork (2), |
| 791 |
.BR open (2), |
| 792 |
.BR sched_setparam (2), |
| 793 |
.BR sched_setscheduler (2), |
| 794 |
.BR setpgid (2), |
| 795 |
.BR setuid (2), |
| 796 |
.BR sigaction (2), |
| 797 |
.BR sigprocmask (2), |
| 798 |
.BR posix_spawn_file_actions_addclose (3), |
| 799 |
.BR posix_spawn_file_actions_adddup2 (3), |
| 800 |
.BR posix_spawn_file_actions_addopen (3), |
| 801 |
.BR posix_spawn_file_actions_destroy (3), |
| 802 |
.BR posix_spawn_file_actions_init (3), |
| 803 |
.BR posix_spawnattr_destroy (3), |
| 804 |
.BR posix_spawnattr_getflags (3), |
| 805 |
.BR posix_spawnattr_getpgroup (3), |
| 806 |
.BR posix_spawnattr_getschedparam (3), |
| 807 |
.BR posix_spawnattr_getschedpolicy (3), |
| 808 |
.BR posix_spawnattr_getsigdefault (3), |
| 809 |
.BR posix_spawnattr_getsigmask (3), |
| 810 |
.BR posix_spawnattr_init (3), |
| 811 |
.BR posix_spawnattr_setflags (3), |
| 812 |
.BR posix_spawnattr_setpgroup (3), |
| 813 |
.BR posix_spawnattr_setschedparam (3), |
| 814 |
.BR posix_spawnattr_setschedpolicy (3), |
| 815 |
.BR posix_spawnattr_setsigdefault (3), |
| 816 |
.BR posix_spawnattr_setsigmask (3), |
| 817 |
.BR pthread_atfork (3), |
| 818 |
.IR <spawn.h> , |
| 819 |
Base Definitions volume of POSIX.1-2001, |
| 820 |
.I http://www.opengroup.org/unix/online.html |