]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/posix_spawn.2
xnu-1228.tar.gz
[apple/xnu.git] / bsd / man / man2 / posix_spawn.2
CommitLineData
2d21ac55
A
1.\"
2.\" Copyright (c) 2000-2007 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 August 9, 2007
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 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 a file action; see
112.Xr posix_spawn_file_actions_init 3
113for more information.
114.Pp
115The argument
116.Fa attrp
117is either NULL, or it is a pointer to an attributes object that was
118initialized by a call to
119.Xr posix_spawnattr_init 3
120and represents a set of spawn attributes to apply. If NULL, then the
121default attributes are applied; otherwise, these attributes can control
122various aspects of the spawned process, and are applied prior to the
123spawned process beginning execution; see
124.Xr posix_spawnattr_init 3
125for more information.
126.Pp
127The argument
128.Fa argv
129is a pointer to a null-terminated array of
130character pointers to null-terminated character strings.
131These strings construct the argument list to be made available to the new
132process. At least
133.Fa argv[0]
134must be present in the array, and should contain the file name of the
135program being spawned, e.g. the last component of the
136.Em path
137or
138.Em file
139argument.
140.Pp
141The argument
142.Fa envp
143is a pointer to a null-terminated array of character pointers to
144null-terminated strings. A pointer to this array is normally stored
145in the global variable
146.Va environ.
147These strings pass information to the
148new process that is not directly an argument to the command (see
149.Xr environ 7 ) .
150.Pp
151Signals set to be ignored in the calling process are set to be ignored in
152the new process, unless the behaviour is modified by user specified
153.Em spawn attributes .
154Signals which are set to be caught in the calling process image are set to
155default action in the new process image.
156Blocked signals remain blocked regardless of changes to the signal action,
157unless the mask is overridden by user specified
158.Em spawn attributes .
159The signal stack is reset to be undefined (see
160.Xr sigaction 2
161for more information).
162.Pp
163By default, the effective user ID and group ID will be the same as those of
164the calling process image; however, this may be overridden to force them to
165be the real user ID and group ID of the parent process by user specified
166.Em spawn attributes
167(see
168.Xr posix_spawnattr_init 3
169for more information).
170.Pp
171If the set-user-ID mode bit of the new process image file is set
172(see
173.Xr chmod 2 ) ,
174the effective user ID of the new process image is set to the owner ID
175of the new process image file.
176If the set-group-ID mode bit of the new process image file is set,
177the effective group ID of the new process image is set to the group ID
178of the new process image file.
179(The effective group ID is the first element of the group list.)
180The real user ID, real group ID and supplementary group IDs of the new
181process image remain the same as the calling process image.
182After any set-user-ID and set-group-ID processing,
183the effective user ID is recorded as the saved set-user-ID,
184and the effective group ID is recorded as the saved set-group-ID.
185These values may be used in changing the effective IDs later (see
186.Xr setuid 2 ) .
187.Pp
188The new process also inherits the following attributes from
189the calling process:
190.Pp
191.Bl -column parent_process_ID -offset indent -compact
192.It parent process ID Ta see Xr getppid 2
193.It process group ID Ta see Xr getpgrp 2 , Xr posix_spawnattr_init 3
194.It access groups Ta see Xr getgroups 2
195.It working directory Ta see Xr chdir 2
196.It root directory Ta see Xr chroot 2
197.It control terminal Ta see Xr termios 4
198.It resource usages Ta see Xr getrusage 2
199.It interval timers Ta see Xr getitimer 2
200.It resource limits Ta see Xr getrlimit 2
201.It file mode mask Ta see Xr umask 2
202.It signal mask Ta see Xr sigaction 2 , Xr sigsetmask 2 ,
203.Xr posix_spawnattr_init 3
204.El
205.Pp
206When a program is executed as a result of a
207.Fn posix_spawn
208or
209.Fn posix_spawnp
210call, it is entered as follows:
211.Bd -literal -offset indent
212main(argc, argv, envp)
213int argc;
214char **argv, **envp;
215.Ed
216.Pp
217where
218.Fa argc
219is the number of elements in
220.Fa argv
221(the ``arg count'')
222and
223.Fa argv
224points to the array of character pointers
225to the arguments themselves.
226.Sh RETURN VALUES
227If the
228.Em pid
229argument is NULL, no pid is returned to the calling process; if it is
230non-NULL, then
231.Fn posix_spawn
232and
233.Fn posix_spawnp
234functions return the process ID of the child process into the pid_t
235variable pointed to by the
236.Em pid
237argument and return a 0 on success. If an error occurs, they return
238a non-zero error code as the function return value, and no child process
239is created.
240.Sh ERRORS
241The
242.Fn posix_spawn
243and
244.Fn posix_spawnp
245functions will fail and return to the calling process if:
246.Bl -tag -width Er
247.\" ==========
248.It Bq Er EINVAL
249The value specified by
250.Fa file_actions
251or
252.Fa attrp
253is invalid.
254.\" ==========
255.It Bq Er E2BIG
256The number of bytes in the new process's argument list
257is larger than the system-imposed limit.
258This limit is specified by the
259.Xr sysctl 3
260MIB variable
261.Dv KERN_ARGMAX .
262.\" ==========
263.It Bq Er EACCES
264Search permission is denied for a component of the path prefix.
265.\" ==========
266.It Bq Er EACCES
267The new process file is not an ordinary file.
268.\" ==========
269.It Bq Er EACCES
270The new process file mode denies execute permission.
271.\" ==========
272.It Bq Er EACCES
273The new process file is on a filesystem mounted
274with execution disabled
275.Pf ( Dv MNT_NOEXEC
276in
277.Ao Pa sys/mount.h Ac ) .
278.\" ==========
279.It Bq Er EFAULT
280The new process file is not as long as indicated by
281the size values in its header.
282.\" ==========
283.It Bq Er EFAULT
284.Fa Path ,
285.Fa argv ,
286or
287.Fa envp
288point
289to an illegal address.
290.\" ==========
291.It Bq Er EIO
292An I/O error occurred while reading from the file system.
293.\" ==========
294.It Bq Er ELOOP
295Too many symbolic links were encountered in translating the pathname.
296This is taken to be indicative of a looping symbolic link.
297.\" ==========
298.It Bq Er ENAMETOOLONG
299A component of a pathname exceeded
300.Dv {NAME_MAX}
301characters, or an entire path name exceeded
302.Dv {PATH_MAX}
303characters.
304.\" ==========
305.It Bq Er ENOENT
306The new process file does not exist.
307.\" ==========
308.It Bq Er ENOEXEC
309The new process file has the appropriate access
310permission, but has an unrecognized format
311(e.g., an invalid magic number in its header).
312.\" ==========
313.It Bq Er ENOMEM
314The new process requires more virtual memory than
315is allowed by the imposed maximum
316.Pq Xr getrlimit 2 .
317.\" ==========
318.It Bq Er ENOTDIR
319A component of the path prefix is not a directory.
320.\" ==========
321.It Bq Er ETXTBSY
322The new process file is a pure procedure (shared text)
323file that is currently open for writing or reading by some process.
324.El
325.Sh CAVEAT
326If a program is
327.Em setuid
328to a non-super-user, but is executed when
329the real
330.Em uid
331is ``root'', then the program has some of the powers
332of a super-user as well.
333.Sh SEE ALSO
334.Xr exit 2 ,
335.Xr fork 2 ,
336.Xr execl 3 ,
337.Xr sysctl 3 ,
338.Xr environ 7 ,
339.Xr posix_spawnattr_init 3 ,
340.Xr posix_file_actions_init 3
341.Sh STANDARDS
342.St -susv3 [SPN]
343.Sh HISTORY
344The
345.Fn posix_spawn
346and
347.Fn posix_spawnp
348function calls appeared in
349.St -susv3 [SPN] .