summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/posix_spawn.3c
blob: 20d56f8ead9b9b64d1aee1e31bb6c770ff797d05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright (c) 2001, The IEEE and The Open Group.  All Rights Reserved.
.\" Portions Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 2011 by Delphix.  All rights reserved.
.\"
.TH POSIX_SPAWN 3C "Feb 20, 2009"
.SH NAME
posix_spawn, posix_spawnp \- spawn a process
.SH SYNOPSIS
.LP
.nf
#include <spawn.h>

\fBint\fR \fBposix_spawn\fR(\fBpid_t *restrict\fR \fIpid\fR, \fBconst char *restrict\fR \fIpath\fR,
     \fBconst posix_spawn_file_actions_t *\fR\fIfile_actions\fR,
     \fBconst posix_spawnattr_t *restrict\fR \fIattrp\fR,
     \fBchar *const\fR \fIargv\fR[restrict], \fBchar *const\fR \fIenvp\fR[restrict]);
.fi

.LP
.nf
\fBint\fR \fBposix_spawnp\fR(\fBpid_t *restrict\fR \fIpid\fR, \fBconst char *restrict\fR \fIfile\fR,
     \fBconst posix_spawn_file_actions_t *\fR\fIfile_actions\fR,
     \fBconst posix_spawnattr_t *restrict\fR \fIattrp\fR,
     \fBchar *const\fR \fIargv\fR[restrict], \fBchar *const\fR \fIenvp\fR[restrict]);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBposix_spawn()\fR and \fBposix_spawnp()\fR functions create a new process
(child process) from the specified process image. The new process image is
constructed from a regular executable file called the new process image file.
.sp
.LP
When a C program is executed as the result of this call, it is entered as a C
language function call as follows:
.sp
.in +2
.nf
int main(int \fIargc\fR, char *\fIargv\fR[]);
.fi
.in -2

.sp
.LP
where \fIargc\fR is the argument count and \fIargv\fR is an array of character
pointers to the arguments themselves. In addition, the following variable
.sp
.in +2
.nf
extern char **environ;
.fi
.in -2

.sp
.LP
is initialized as a pointer to an array of character pointers to the
environment strings.
.sp
.LP
The argument \fIargv\fR is an array of character pointers to null-terminated
strings. The last member of this array is a null pointer and is not counted in
\fIargc\fR. These strings constitute the argument list available to the new
process image. The value in \fIargv\fR[0] should point to a filename that is
associated with the process image being started by the \fBposix_spawn()\fR or
\fBposix_spawnp()\fR function.
.sp
.LP
The argument \fIenvp\fR is an array of character pointers to null-terminated
strings. These strings constitute the environment for the new process image.
The environment array is terminated by a null pointer.
.sp
.LP
The number of bytes available for the child process's combined argument and
environment lists is {\fBARG_MAX\fR}, counting all character pointers, the
strings they point to, the trailing null bytes in the strings, and the
list-terminating null pointers. There is no additional system overhead included
in this total.
.sp
.LP
The \fIpath\fR argument to \fBposix_spawn()\fR is a pathname that identifies
the new process image file to execute.
.sp
.LP
The \fIfile\fR parameter to \fBposix_spawnp()\fR is used to construct a
pathname that identifies the new process image file. If the file parameter
contains a slash character, the file parameter is used as the pathname for the
new process image file. Otherwise, the path prefix for this file is obtained by
a search of the directories passed as the environment variable \fBPATH\fR. If
this environment variable is not defined, the results of the search are
implementation-defined.
.sp
.LP
If \fIfile_actions\fR is a null pointer, then file descriptors open in the
calling process remain open in the child process, except for those whose
close-on-exec flag \fBFD_CLOEXEC\fR is set (see \fBfcntl\fR(2)).  For those
file descriptors that remain open, all attributes of the corresponding open
file descriptions, including file locks (see \fBfcntl\fR(2)), remain unchanged.
.sp
.LP
If \fIfile_actions\fR is not \fINULL\fR, then the file descriptors open in the
child process are those open in the calling process as modified by the spawn
file actions object pointed to by \fIfile_actions\fR and the \fBFD_CLOEXEC\fR
flag of each remaining open file descriptor after the spawn file actions have
been processed. The effective order of processing the spawn file actions are:
.RS +4
.TP
1.
The set of open file descriptors for the child process are initially the
same set as is open for the calling process. All attributes of the
corresponding open file descriptions, including file locks (see
\fBfcntl\fR(2)), remain unchanged.
.RE
.RS +4
.TP
2.
The signal mask, signal default or ignore actions, and the effective user
and group IDs for the child process are changed as specified in the attributes
object referenced by \fIattrp\fR.
.RE
.RS +4
.TP
3.
The file actions specified by the spawn file actions object are performed in
the order in which they were added to the spawn file actions object.
.RE
.RS +4
.TP
4.
Any file descriptor that has its \fBFD_CLOEXEC\fR flag set (see
\fBfcntl\fR(2)) is closed.
.RE
.sp
.LP
The \fBposix_spawnattr_t\fR spawn attributes object type is defined in
<\fBspawn.h\fR>. It contains at least the attributes defined below.
.sp
.LP
If the \fBPOSIX_SPAWN_SETPGROUP\fR flag is set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, and the \fIspawn-pgroup\fR
attribute of the same object is non-zero, then the child's process group is as
specified in the \fIspawn-pgroup\fR attribute of the object referenced by
\fIattrp\fR.
.sp
.LP
As a special case, if the \fBPOSIX_SPAWN_SETPGROUP\fR flag is set in the
\fIspawn-flags\fR attribute of the object referenced by \fIattrp\fR, and the
\fIspawn-pgroup\fR attribute of the same object is set to zero, then the child
will be in a new process group with a process group ID equal to its process ID.
.sp
.LP
If the \fBPOSIX_SPAWN_SETPGROUP\fR flag is not set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, the new child process
inherits the parent's process group.
.sp
.LP
If the \fBPOSIX_SPAWN_SETSCHEDPARAM\fR flag is set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, but
\fBPOSIX_SPAWN_SETSCHEDULER\fR is not set, the new process image initially has
the scheduling policy of the calling process with the scheduling parameters
specified in the \fIspawn-schedparam\fR attribute of the object referenced by
\fIattrp\fR.
.sp
.LP
If the \fBPOSIX_SPAWN_SETSCHEDULER\fR flag is set in \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR (regardless of the setting of
the \fBPOSIX_SPAWN_SETSCHEDPARAM\fR flag), the new process image initially has
the scheduling policy specified in the \fIspawn-schedpolicy\fR attribute of the
object referenced by \fIattrp\fR and the scheduling parameters specified in the
\fIspawn-schedparam\fR attribute of the same object.
.sp
.LP
The \fBPOSIX_SPAWN_RESETIDS\fR flag in the \fIspawn-flags\fR attribute of the
object referenced by \fIattrp\fR governs the effective user ID of the child
process. If this flag is not set, the child process inherits the parent
process's effective user ID. If this flag is set, the child process's effective
user ID is reset to the parent's real user ID. In either case, if the
set-user-ID mode bit of the new process image file is set, the effective user
ID of the child process becomes that file's owner ID before the new process
image begins execution. If this flag is set, the child process's effective user
ID is reset to the parent's real user ID. In either case, if the set-user-ID
mode bit of the new process image file is set, the effective user ID of the
child process becomes that file's owner ID before the new process image begins
execution.
.sp
.LP
The \fBPOSIX_SPAWN_RESETIDS\fR flag in the \fIspawn-flags\fR attribute of the
object referenced by \fIattrp\fR also governs the effective group ID of the
child process. If this flag is not set, the child process inherits the parent
process's effective group ID. If this flag is set, the child process's
effective group ID is reset to the parent's real group ID. In either case, if
the set-group-ID mode bit of the new process image file is set, the effective
group ID of the child process becomes that file's group ID before the new
process image begins execution.
.sp
.LP
If the \fBPOSIX_SPAWN_SETSIGMASK\fR flag is set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, the child process initially
has the signal mask specified in the \fIspawn-sigmask\fR attribute of the
object referenced by \fIattrp\fR.
.sp
.LP
If the \fBPOSIX_SPAWN_SETSIGDEF\fR flag is set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, the signals specified in the
\fIspawn-sigdefault\fR attribute of the same object is set to their default
actions in the child process.
.sp
.LP
If the \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flag is set in the spawn-flags attribute
of the object referenced by \fIattrp\fR, the signals specified in the
\fIspawn-sigignore\fR attribute of the same object are set to be ignored in the
child process.
.sp
.LP
If both \fBPOSIX_SPAWN_SETSIGDEF\fR and \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flags
are set in the spawn-flags attribute of the object referenced by \fIattrp\fR,
the actions for \fBPOSIX_SPAWN_SETSIGDEF\fR take precedence over the actions
for \fBPOSIX_SPAWN_SETSIGIGN_NP\fR.
.sp
.LP
If the \fBPOSIX_SPAWN_NOSIGCHLD_NP\fR flag is set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, no \fBSIGCHLD\fR signal will
be posted to the parent process when the child process terminates, regardless
of the disposition of the \fBSIGCHLD\fR signal in the parent.  \fBSIGCHLD\fR
signals are still possible for job control stop and continue actions if the
parent has requested them.
.sp
.LP
If the \fBPOSIX_SPAWN_WAITPID_NP\fR flag is set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR, no wait-for-multiple-pids
operation by the parent, as in \fBwait()\fR, \fBwaitid\fR(\fBP_ALL\fR), or
\fBwaitid\fR(\fBP_PGID\fR), will succeed in reaping the child, and the child
will not be reaped automatically due the disposition of the \fBSIGCHLD\fR
signal being set to be ignored in the parent.  Only a specific wait for the
child, as in \fBwaitid\fR(\fBP_PID\fR, \fBpid\fR), is allowed and it is
required, else when the child exits it will remain a zombie until the parent
exits.
.sp
.LP
If the \fBPOSIX_SPAWN_NOEXECERR_NP\fR flag is set in the spawn-flags attribute
of the object referenced by \fIattrp\fR, and if the specified process image
file cannot be executed, then the \fBposix_spawn()\fR and \fBposix_spawnp()\fR
functions do not fail with one of the \fBexec\fR(2) error codes, as is normal,
but rather return successfully having created a child process that exits
immediately with exit status 127. This flag permits \fBsystem\fR(3C) and
\fBpopen\fR(3C) to be implemented with \fBposix_spawn()\fR and still conform
strictly to their POSIX specifications.
.sp
.LP
Signals set to be caught or set to the default action in the calling process
are set to the default action in the child process, unless the
\fBPOSIX_SPAWN_SETSIGIGN_NP\fR flag is set in the spawn-flags attribute of the
object referenced by \fIattrp\fR and the signals are specified in the
\fIspawn-sigignore\fR attribute of the same object.
.sp
.LP
Except for \fBSIGCHLD\fR, signals set to be ignored by the calling process
image are set to be ignored by the child process, unless otherwise specified by
the \fBPOSIX_SPAWN_SETSIGDEF\fR flag being set in the \fIspawn-flags\fR
attribute of the object referenced by \fIattrp\fR and the signals being
indicated in the \fIspawn-sigdefault\fR attribute of the object referenced by
\fIattrp\fR.
.sp
.LP
If the \fBSIGCHLD\fR signal is set to be ignored by the calling process, it is
unspecified whether the \fBSIGCHLD\fR signal is set to be ignored or to the
default action in the child process, unless otherwise specified by the
\fBPOSIX_SPAWN_SETSIGDEF\fR flag being set in the \fIspawn-flags\fR attribute
of the object referenced by \fIattrp\fR and the \fBSIGCHLD\fR signal being
indicated in the \fIspawn-sigdefault\fR attribute of the object referenced by
\fIattrp\fR.
.sp
.LP
If the value of the \fIattrp\fR pointer is \fINULL\fR, then the default values
are used.
.sp
.LP
All process attributes, other than those influenced by the attributes set in
the object referenced by \fIattrp\fR as specified above or by the file
descriptor manipulations specified in \fIfile_actions\fR appear in the new
process image as though \fBfork()\fR had been called to create a child process
and then a member of the \fBexec\fR family of functions had been called by the
child process to execute the new process image.
.sp
.LP
The fork handlers are not run when \fBposix_spawn()\fR or \fBposix_spawnp()\fR
is called.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fBposix_spawn()\fR and \fBposix_spawnp()\fR return
the process ID of the child process to the parent process in the variable
pointed to by a non-null \fIpid\fR argument, and return zero as the function
return value. Otherwise, no child process is created, the value stored into the
variable pointed to by a non-null \fIpid\fR is unspecified, and an error number
is returned as the function return value to indicate the error. If the
\fIpid\fR argument is a null pointer, the process ID of the child is not
returned to the caller.
.SH ERRORS
.sp
.LP
The \fBposix_spawn()\fR and \fBposix_spawnp()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The value specified by \fIfile_actions\fR or \fIattrp\fR is invalid.
.RE

.sp
.LP
If \fBposix_spawn()\fR or \fBposix_spawnp()\fR fails for any of the reasons
that would cause \fBfork()\fR or one of the \fBexec\fR family of functions to
fail, an error value is returned as described by \fBfork\fR(2) and
\fBexec\fR(2), respectively
.sp
.LP
If \fBPOSIX_SPAWN_SETPGROUP\fR is set in the \fIspawn-flags\fR attribute of the
object referenced by \fIattrp\fR, and \fBposix_spawn()\fR or
\fBposix_spawnp()\fR fails while changing the child's process group, an error
value is returned as described by \fBsetpgid\fR(2).
.sp
.LP
If \fBPOSIX_SPAWN_SETSCHEDPARAM\fR is set and \fBPOSIX_SPAWN_SETSCHEDULER\fR is
not set in the \fIspawn-flags\fR attribute of the object referenced by
\fIattrp\fR, then if \fBposix_spawn()\fR or \fBposix_spawnp()\fR fails for any
of the reasons that would cause \fBsched_setparam()\fR to fail, an error value
is returned as described by \fBsched_setparam\fR(3C).
.sp
.LP
If \fBPOSIX_SPAWN_SETSCHEDULER\fR is set in the \fIspawn-flags\fR attribute of
the object referenced by \fIattrp\fR, and if \fBposix_spawn()\fR or
\fBposix_spawnp()\fR fails for any of the reasons that would cause
\fBsched_setscheduler()\fR to fail, an error value is returned as described by
\fBsched_setscheduler\fR(3C).
.sp
.LP
If the \fIfile_actions\fR argument is not \fINULL\fR and specifies any
\fBclose()\fR, \fBdup2()\fR, or \fBopen()\fR actions to be performed, and if
\fBposix_spawn()\fR or \fBposix_spawnp()\fR fails for any of the reasons that
would cause \fBclose()\fR, \fBdup2()\fR, or \fBopen()\fR to fail, an error
value is returned as described by \fBclose\fR(2), \fBdup2\fR(3C), or
\fBopen\fR(2), respectively. An open file action might, by itself, result in
any of the errors described by \fBclose()\fR or \fBdup2()\fR, in addition to
those described by \fBopen()\fR.
.sp
.LP
If a \fBclose\fR(2) operation is specified to be performed for a file
descriptor that is not open at the time of the call to \fBposix_spawn()\fR or
\fBposix_spawnp()\fR, the action does not cause \fBposix_spawn()\fR or
\fBposix_spawnp()\fR to fail.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
_
Standard	See \fBstandards\fR(5).
.TE

.SH SEE ALSO
.sp
.LP
\fBalarm\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBdup\fR(2), \fBexec\fR(2),
\fBexit\fR(2), \fBfcntl\fR(2), \fBfork\fR(2), \fBkill\fR(2), \fBopen\fR(2),
\fBsetpgid\fR(2), \fBsetuid\fR(2), \fBstat\fR(2), \fBtimes\fR(2),
\fBdup2\fR(3C), \fBpopen\fR(3C), \fBposix_spawn_file_actions_addclose\fR(3C),
\fBposix_spawn_file_actions_adddup2\fR(3C),
\fBposix_spawn_file_actions_addopen\fR(3C),
\fBposix_spawn_file_actions_destroy\fR(3C),
\fBposix_spawn_file_actions_init\fR(3C),
\fBposix_spawn_pipe_np\fR(3C),
\fBposix_spawnattr_destroy\fR(3C),
\fBposix_spawnattr_getflags\fR(3C), \fBposix_spawnattr_getpgroup\fR(3C),
\fBposix_spawnattr_getschedparam\fR(3C),
\fBposix_spawnattr_getschedpolicy\fR(3C),
\fBposix_spawnattr_getsigdefault\fR(3C),
\fBposix_spawnattr_getsigignore_np\fR(3C),
\fBposix_spawnattr_getsigmask\fR(3C), \fBposix_spawnattr_init\fR(3C),
\fBposix_spawnattr_setflags\fR(3C), \fBposix_spawnattr_setpgroup\fR(3C),
\fBposix_spawnattr_setschedparam\fR(3C),
\fBposix_spawnattr_setschedpolicy\fR(3C),
\fBposix_spawnattr_setsigdefault\fR(3C),
\fBposix_spawnattr_setsigignore_np\fR(3C),
\fBposix_spawnattr_setsigmask\fR(3C), \fBsched_setparam\fR(3C),
\fBsched_setscheduler\fR(3C), \fBsystem\fR(3C), \fBwait\fR(3C),
\fBattributes\fR(5), \fBstandards\fR(5)
.SH NOTES
.sp
.LP
The SUSv3 POSIX standard (The Open Group Base Specifications Issue 6, IEEE Std
1003.1-2001) permits the \fBposix_spawn()\fR and \fBposix_spawnp()\fR functions
to return successfully before some of the above-described errors are detected,
allowing the child process to fail instead:
.sp
.in +2
.nf
\&... if the error occurs after the calling process
successfully returns, the child process exits with
exit status 127.
.fi
.in -2

.sp
.LP
With the one exception of when the \fBPOSIX_SPAWN_NOEXECERR_NP\fR flag is
passed in the attributes structure, this behavior is not present in the Solaris
implementation. Any error that occurs before the new process image is
successfully constructed causes the \fBposix_spawn()\fR and
\fBposix_spawnp()\fR functions to return the corresponding non-zero error value
without creating a child process.
.sp
.LP
The \fBPOSIX_SPAWN_NOSIGCHLD_NP\fR, \fBPOSIX_SPAWN_WAITPID_NP\fR,
\fBPOSIX_SPAWN_NOEXECERR_NP\fR, and \fBPOSIX_SPAWN_SETSIGIGN_NP\fR flags and
the \fBposix_spawnattr_getsigignore_np()\fR and
\fBposix_spawnattr_setsigignore_np()\fR functions are non-portable Solaris
extensions to the \fBposix_spawn()\fR and \fBposix_spawnp()\fR interfaces.