xref: /openbsd-src/lib/libc/sys/execve.2 (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1.\"	$OpenBSD: execve.2,v 1.41 2014/05/30 04:38:43 guenther Exp $
2.\"	$NetBSD: execve.2,v 1.9 1995/02/27 12:32:25 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)execve.2	8.3 (Berkeley) 1/24/94
32.\"
33.Dd $Mdocdate: May 30 2014 $
34.Dt EXECVE 2
35.Os
36.Sh NAME
37.Nm execve ,
38.Nm exect
39.Nd execute a file
40.Sh SYNOPSIS
41.Fd #include <unistd.h>
42.Ft int
43.Fn execve "const char *path" "char *const argv[]" "char *const envp[]"
44.Ft int
45.Fn exect "const char *path" "char *const argv[]" "char *const envp[]"
46.Sh DESCRIPTION
47.Fn execve
48transforms the calling process into a new process.
49The new process is constructed from an ordinary file,
50whose name is pointed to by
51.Fa path ,
52called the
53.Em new process file .
54This file is either an executable object file,
55or a file of data for an interpreter.
56An executable object file consists of an identifying header,
57followed by pages of data representing the initial program (text)
58and initialized data pages.
59Additional pages may be specified by the header to be initialized
60with zero data; see
61.Xr elf 5 .
62.Pp
63An interpreter file begins with a line of the form:
64.Bd -filled -offset indent
65.Sy #!\&
66.Em interpreter
67.Bq Em arg
68.Ed
69.Pp
70When an interpreter file is passed to
71.Fn execve
72the system instead calls
73.Fn execve
74with the specified
75.Em interpreter .
76If the optional
77.Em arg
78is specified, it becomes the first argument to the
79.Em interpreter ,
80and the original
81.Ar path
82becomes the second argument;
83otherwise,
84.Ar path
85becomes the first argument.
86The original arguments are shifted over to become the subsequent arguments.
87The zeroth argument, normally the name of the file being executed, is left
88unchanged.
89.Pp
90The argument
91.Fa argv
92is a pointer to a null-terminated array of
93character pointers to NUL-terminated character strings.
94These strings construct the argument list to be made available to the new
95process.
96At least one argument must be present in the array;
97by custom, the first element should be
98the name of the executed program (for example, the last component of
99.Fa path ) .
100.Pp
101The argument
102.Fa envp
103is also a pointer to a null-terminated array of
104character pointers to NUL-terminated strings.
105A pointer to this array is normally stored in the global variable
106.Va environ .
107These strings pass information to the
108new process that is not directly an argument to the command (see
109.Xr environ 7 ) .
110.Pp
111File descriptors open in the calling process image remain open in
112the new process image, except for those for which the close-on-exec
113flag is set (see
114.Xr close 2
115and
116.Xr fcntl 2 ) .
117Descriptors that remain open are unaffected by
118.Fn execve .
119In the case of a new setuid or setgid executable being executed, if
120file descriptors 0, 1, or 2 (representing stdin, stdout, and stderr)
121are currently unallocated, these descriptors will be opened to point to
122some system file like
123.Pa /dev/null .
124The intent is to ensure these descriptors are not unallocated, since
125many libraries make assumptions about the use of these 3 file descriptors.
126.Pp
127Signals set to be ignored in the calling process are set to be ignored in
128the
129new process.
130Signals which are set to be caught in the calling process image
131are set to default action in the new process image.
132Blocked signals remain blocked regardless of changes to the signal action.
133The signal stack is reset to be undefined (see
134.Xr sigaction 2
135for more information).
136.Pp
137If the set-user-ID mode bit of the new process image file is set
138(see
139.Xr chmod 2 ) ,
140the effective user ID of the new process image is set to the owner ID
141of the new process image file.
142If the set-group-ID mode bit of the new process image file is set,
143the effective group ID of the new process image is set to the group ID
144of the new process image file.
145(The effective group ID is the first element of the group list.)
146The real user ID, real group ID and
147other group IDs of the new process image remain the same as the calling
148process image.
149After any set-user-ID and set-group-ID processing,
150the effective user ID is recorded as the saved set-user-ID,
151and the effective group ID is recorded as the saved set-group-ID.
152These values may be used in changing the effective IDs later (see
153.Xr setuid 2 ) .
154The set-user-ID and set-group-ID bits have no effect if the
155new process image file is located on a file system mounted with
156the nosuid flag.
157The process will be started without the new permissions.
158.Pp
159The new process also inherits the following attributes from
160the calling process:
161.Pp
162.Bl -tag -width parent_process_ID -offset indent -compact
163.It process ID
164see
165.Xr getpid 2
166.It parent process ID
167see
168.Xr getppid 2
169.It process group ID
170see
171.Xr getpgrp 2
172.It session ID
173see
174.Xr getsid 2
175.It access groups
176see
177.Xr getgroups 2
178.It working directory
179see
180.Xr chdir 2
181.It root directory
182see
183.Xr chroot 2
184.It control terminal
185see
186.Xr termios 4
187.It resource usages
188see
189.Xr getrusage 2
190.It interval timers
191see
192.Xr getitimer 2
193(unless process image file is setuid or setgid,
194in which case all timers are disabled)
195.It resource limits
196see
197.Xr getrlimit 2
198.It file mode mask
199see
200.Xr umask 2
201.It signal mask
202see
203.Xr sigaction 2 ,
204.Xr sigsetmask 3
205.El
206.Pp
207When a program is executed as a result of an
208.Fn execve
209call, it is entered as follows:
210.Bd -literal -offset indent
211main(int argc, char **argv, char **envp)
212.Ed
213.Pp
214where
215.Fa argc
216is the number of elements in
217.Fa argv
218(the
219.Dq arg count )
220and
221.Fa argv
222points to the array of character pointers
223to the arguments themselves.
224.Pp
225The
226.Fn exect
227function is equivalent to
228.Fn execve
229with the additional property that it executes the file with the process
230tracing facilities enabled (see
231.Xr ptrace 2 ) .
232.Sh RETURN VALUES
233As the
234.Fn execve
235function overlays the current process image
236with a new process image the successful call
237has no process to return to.
238If
239.Fn execve
240does return to the calling process an error has occurred; the
241return value will be \-1 and the global variable
242.Va errno
243is set to indicate the error.
244.Sh ERRORS
245.Fn execve
246will fail and return to the calling process if:
247.Bl -tag -width Er
248.It Bq Er ENOTDIR
249A component of the path prefix is not a directory.
250.It Bq Er ENAMETOOLONG
251A component of a pathname exceeded
252.Dv {NAME_MAX}
253characters, or an entire path name exceeded
254.Dv {PATH_MAX}
255characters.
256.It Bq Er ENOENT
257The new process file does not exist.
258.It Bq Er ELOOP
259Too many symbolic links were encountered in translating the pathname.
260.It Bq Er EACCES
261Search permission is denied for a component of the path prefix.
262.It Bq Er EACCES
263The new process file is not an ordinary file.
264.It Bq Er EACCES
265The new process file mode denies execute permission.
266.It Bq Er EACCES
267The new process file is on a filesystem mounted with execution
268disabled
269.Pf ( Dv MNT_NOEXEC
270in
271.In sys/mount.h ) .
272.It Bq Er ENOEXEC
273The new process file has the appropriate access
274permission, but has an invalid magic number in its header.
275.It Bq Er ETXTBSY
276The new process file is a pure procedure (shared text)
277file that is currently open for writing or reading by some process.
278.It Bq Er ENOMEM
279The new process requires more virtual memory than
280is allowed by the imposed maximum
281.Pq Xr getrlimit 2 .
282.It Bq Er E2BIG
283The number of bytes in the new process's argument list
284is larger than the system-imposed limit.
285The limit in the system as released is 262144 bytes
286.Pf ( Dv NCARGS
287in
288.In sys/param.h ) .
289.It Bq Er EFAULT
290The new process file is not as long as indicated by
291the size values in its header.
292.It Bq Er EFAULT
293.Fa path ,
294.Fa argv ,
295or
296.Fa envp
297point
298to an illegal address.
299.It Bq Er EIO
300An I/O error occurred while reading from the file system.
301.It Bq Er ENFILE
302During startup of an
303.Em interpreter ,
304the system file table was found to be full.
305.El
306.Sh SEE ALSO
307.Xr _exit 2 ,
308.Xr fork 2 ,
309.Xr execl 3 ,
310.Xr exit 3 ,
311.Xr elf 5 ,
312.Xr environ 7
313.Sh STANDARDS
314The
315.Fn execve
316function is expected to conform to
317.St -p1003.1-2008 .
318The
319.Fn exect
320function should not be used in portable applications.
321.Sh HISTORY
322The predecessor of these functions, the former
323.Fn exec
324system call, first appeared in
325.At v1 .
326The
327.Fn execve
328function first appeared in
329.At v7 .
330.Sh CAVEATS
331If a program is
332.Em setuid
333to a non-superuser, but is executed when the real
334.Em uid
335is
336.Dq root ,
337then the process has some of the powers of a superuser as well.
338