xref: /xnu-12377.1.9/bsd/man/man2/posix_spawn.2 (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1.\"
2.\" Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3.\"
4.\" @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5.\"
6.\" This file contains Original Code and/or Modifications of Original Code
7.\" as defined in and that are subject to the Apple Public Source License
8.\" Version 2.0 (the 'License'). You may not use this file except in
9.\" compliance with the License. The rights granted to you under the License
10.\" may not be used to create, or enable the creation or redistribution of,
11.\" unlawful or unlicensed copies of an Apple operating system, or to
12.\" circumvent, violate, or enable the circumvention or violation of, any
13.\" terms of an Apple operating system software license agreement.
14.\"
15.\" Please obtain a copy of the License at
16.\" http://www.opensource.apple.com/apsl/ and read it before using this file.
17.\"
18.\" The Original Code and all software distributed under the License are
19.\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22.\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23.\" Please see the License for the specific language governing rights and
24.\" limitations under the License.
25.\"
26.\" @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27.\"
28.\"     @(#)posix_spawn.2
29.
30.Dd November 2, 2010
31.Dt POSIX_SPAWN 2
32.Os "Mac OS X"
33.Sh NAME
34.Nm posix_spawn
35.Nm posix_spawnp
36.Nd spawn a process
37.Sh SYNOPSIS
38.Fd #include <spawn.h>
39.Ft int
40.Fo posix_spawn
41.Fa "pid_t *restrict pid"
42.Fa "const char *restrict path"
43.Fa "const posix_spawn_file_actions_t *file_actions"
44.Fa "const posix_spawnattr_t *restrict attrp"
45.Fa "char *const argv[restrict]"
46.Fa "char *const envp[restrict]"
47.Fc
48.Ft int
49.Fo posix_spawnp
50.Fa "pid_t *restrict pid"
51.Fa "const char *restrict file"
52.Fa "const posix_spawn_file_actions_t *file_actions"
53.Fa "const posix_spawnattr_t *restrict attrp"
54.Fa "char *const argv[restrict]"
55.Fa "char *const envp[restrict]"
56.Fc
57.Sh DESCRIPTION
58The
59.Fn posix_spawn
60function creates a new process from the executable file, called the
61.Em new process file ,
62specified by
63.Em path ,
64which is an absolute or relative path to the file.
65The
66.Fn posix_spawnp
67function is identical to the
68.Fn posix_spawn
69function if the
70.Em file
71specified contains a slash character; otherwise, the
72.Em file
73parameter is used to construct a pathname, with its path prefix being
74obtained by a search of the path specified in the environment by the
75.Dq Ev PATH variable .
76If this variable isn't specified, the default path is set according
77to the
78.Dv _PATH_DEFPATH
79definition in
80.In paths.h ,
81which is set to
82.Dq Ev /usr/bin:/bin .
83This pathname either refers to an executable object file,
84or a file of data for an interpreter;
85.Xr execve 2
86for more details.
87.Pp
88The argument
89.Fa pid
90is a pointer to a pid_t variable to receive the pid of the spawned
91process; if this is NULL, then the pid of the spawned process is
92not returned.  If this pointer is non-NULL, then on successful
93completion, the variable will be modified to contain the pid of the
94spawned process.  The value is undefined in the case of a failure.
95.Pp
96The argument
97.Fa file_actions
98is either NULL, or it is a pointer to a file actions object that was
99initialized by a call to
100.Xr posix_spawn_file_actions_init 3
101and represents zero or more file actions.
102.Pp
103File descriptors open in the calling process image remain open in
104the new process image, except for those for which the close-on-exec
105flag is set (see
106.Xr close 2
107and
108.Xr fcntl 2 ) .
109Descriptors that remain open are unaffected by
110.Fn posix_spawn
111unless their behaviour is modified by particular spawn flags
112or a file action; see
113.Xr posix_spawnattr_setflags 3
114and
115.Xr posix_spawn_file_actions_init 3
116for additional information.
117.Pp
118The argument
119.Fa attrp
120is either NULL, or it is a pointer to an attributes object that was
121initialized by a call to
122.Xr posix_spawnattr_init 3
123and represents a set of spawn attributes to apply.  If NULL, then the
124default attributes are applied; otherwise, these attributes can control
125various aspects of the spawned process, and are applied prior to the
126spawned process beginning execution; see
127.Xr posix_spawnattr_init 3
128for more information.
129.Pp
130The argument
131.Fa argv
132is a pointer to a null-terminated array of
133character pointers to null-terminated character strings.
134These strings construct the argument list to be made available to the new
135process.  At least
136.Fa argv[0]
137must be present in the array, and should contain the file name of the
138program being spawned, e.g. the last component of the
139.Em path
140or
141.Em file
142argument.
143.Pp
144The argument
145.Fa envp
146is a pointer to a null-terminated array of character pointers to
147null-terminated strings.  A pointer to this array is normally stored
148in the global variable
149.Va environ.
150These strings pass information to the
151new process that is not directly an argument to the command (see
152.Xr environ 7 ) .
153.Pp
154Signals set to be ignored in the calling process are set to be ignored in
155the new process, unless the behaviour is modified by user specified
156.Em spawn attributes .
157Signals which are set to be caught in the calling process image are set to
158default action in the new process image.
159Blocked signals remain blocked regardless of changes to the signal action,
160unless the mask is overridden by user specified
161.Em spawn attributes .
162The signal stack is reset to be undefined (see
163.Xr sigaction 2
164for more information).
165.Pp
166By default, the effective user ID and group ID will be the same as those of
167the calling process image; however, this may be overridden to force them to
168be the real user ID and group ID of the parent process by user specified
169.Em spawn attributes
170(see
171.Xr posix_spawnattr_init 3
172for more information).
173.Pp
174If the set-user-ID mode bit of the new process image file is set
175(see
176.Xr chmod 2 ) ,
177the effective user ID of the new process image is set to the owner ID
178of the new process image file.
179If the set-group-ID mode bit of the new process image file is set,
180the effective group ID of the new process image is set to the group ID
181of the new process image file.
182(The effective group ID is the first element of the group list.)
183The real user ID, real group ID and supplementary group IDs of the new
184process image remain the same as the calling process image.
185After any set-user-ID and set-group-ID processing,
186the effective user ID is recorded as the saved set-user-ID,
187and the effective group ID is recorded as the saved set-group-ID.
188These values may be used in changing the effective IDs later (see
189.Xr setuid 2 ) .
190.Pp
191The new process also inherits the following attributes from
192the calling process:
193.Pp
194.Bl -column parent_process_ID -offset indent -compact
195.It parent process ID Ta see Xr getppid 2
196.It process group ID Ta see Xr getpgrp 2 , Xr posix_spawnattr_init 3
197.It access groups Ta see Xr getgroups 2
198.It working directory Ta see Xr chdir 2
199.It root directory Ta see Xr chroot 2
200.It control terminal Ta see Xr termios 4
201.It resource usages Ta see Xr getrusage 2
202.It interval timers Ta see Xr getitimer 2
203.It resource limits Ta see Xr getrlimit 2
204.It file mode mask Ta see Xr umask 2
205.It signal mask Ta see Xr sigaction 2 , Xr sigsetmask 2 ,
206.Xr posix_spawnattr_init 3
207.El
208.Pp
209When a program is executed as a result of a
210.Fn posix_spawn
211or
212.Fn posix_spawnp
213call, it is entered as follows:
214.Bd -literal -offset indent
215.Ft int
216.Fn main "int argc" "char **argv" "char **envp";
217.Ed
218.Pp
219where
220.Fa argc
221is the number of elements in
222.Fa argv
223(the ``arg count'')
224and
225.Fa argv
226points to the array of character pointers
227to the arguments themselves.
228.Sh RETURN VALUES
229If the
230.Em pid
231argument is NULL, no pid is returned to the calling process; if it is
232non-NULL, then
233.Fn posix_spawn
234and
235.Fn posix_spawnp
236functions return the process ID of the child process into the pid_t
237variable pointed to by the
238.Em pid
239argument and return a 0 on success.  If an error occurs, they return
240a non-zero error code as the function return value, and no child process
241is created.
242.Sh ERRORS
243The
244.Fn posix_spawn
245and
246.Fn posix_spawnp
247functions will fail and return to the calling process if:
248.Bl -tag -width Er
249.\" ==========
250.It Bq Er EINVAL
251The value specified by
252.Fa file_actions
253or
254.Fa attrp
255is invalid.
256.\" ==========
257.It Bq Er E2BIG
258The number of bytes in the new process's argument list
259is larger than the system-imposed limit.
260This limit is specified by the
261.Xr sysctl 3
262MIB variable
263.Dv KERN_ARGMAX .
264.\" ==========
265.It Bq Er EACCES
266Search permission is denied for a component of the path prefix.
267.\" ==========
268.It Bq Er EACCES
269The new process file is not an ordinary file.
270.\" ==========
271.It Bq Er EACCES
272The new process file mode denies execute permission.
273.\" ==========
274.It Bq Er EACCES
275The new process file is on a filesystem mounted
276with execution disabled
277.Pf ( Dv MNT_NOEXEC
278in
279.Ao Pa sys/mount.h Ac ) .
280.\" ==========
281.It Bq Er EFAULT
282The new process file is not as long as indicated by
283the size values in its header.
284.\" ==========
285.It Bq Er EFAULT
286.Fa Path ,
287.Fa argv ,
288or
289.Fa envp
290point
291to an illegal address.
292.\" ==========
293.It Bq Er EIO
294An I/O error occurred while reading from the file system.
295.\" ==========
296.It Bq Er ELOOP
297Too many symbolic links were encountered in translating the pathname.
298This is taken to be indicative of a looping symbolic link.
299.\" ==========
300.It Bq Er ENAMETOOLONG
301A component of a pathname exceeded
302.Dv {NAME_MAX}
303characters, or an entire path name exceeded
304.Dv {PATH_MAX}
305characters.
306.\" ==========
307.It Bq Er ENOENT
308The new process file does not exist.
309.\" ==========
310.It Bq Er ENOEXEC
311The new process file has the appropriate access
312permission, but has an unrecognized format
313(e.g., an invalid magic number in its header).
314.\" ==========
315.It Bq Er ENOMEM
316The new process requires more virtual memory than
317is allowed by the imposed maximum
318.Pq Xr getrlimit 2 .
319.\" ==========
320.It Bq Er ENOTDIR
321A component of the path prefix is not a directory.
322.\" ==========
323.It Bq Er ETXTBSY
324The new process file is a pure procedure (shared text)
325file that is currently open for writing or reading by some process.
326.\" ==========
327.It Bq Er EBADARCH
328The new process file has no architectures appropriate for the current system.
329.\" ==========
330.It Bq Er EBADF
331Bad file descriptor for one or more
332.Fa file_actions .
333.El
334.Pp
335Additionally, they may fail for any of the reasons listed in
336.Xr fork 2 or
337.Xr exec 3 .
338.Sh CAVEAT
339If a program is
340.Em setuid
341to a non-super-user, but is executed when
342the real
343.Em uid
344is ``root'', then the program has some of the powers
345of a super-user as well.
346.Sh SEE ALSO
347.Xr exit 2 ,
348.Xr fork 2 ,
349.Xr execl 3 ,
350.Xr sysctl 3 ,
351.Xr environ 7 ,
352.Xr posix_spawnattr_init 3 ,
353.Xr posix_spawn_file_actions_init 3 ,
354.Sh STANDARDS
355.St -susv3 [SPN]
356.Sh HISTORY
357The
358.Fn posix_spawn
359and
360.Fn posix_spawnp
361function calls appeared in
362.St -susv3 [SPN] .
363