xref: /openbsd-src/lib/libc/sys/execve.2 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: execve.2,v 1.33 2007/05/31 19:19:32 jmc 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 31 2007 $
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 a.out 5
62and
63.Xr elf 5 .
64.Pp
65An interpreter file begins with a line of the form:
66.Bd -filled -offset indent
67.Sy #!\&
68.Em interpreter
69.Bq Em arg
70.Ed
71.Pp
72When an interpreter file is
73.Fn execve Ap d ,
74the system
75.Fn execve Ap s
76runs the specified
77.Em interpreter .
78If the optional
79.Em arg
80is specified, it becomes the first argument to the
81.Em interpreter ,
82and the name of the originally
83.Fn execve Ap d
84file becomes the second argument;
85otherwise, the name of the originally
86.Fn execve Ap d
87file becomes the first argument.
88The original arguments are shifted over to become the subsequent arguments.
89The zeroth argument, normally the name of the
90.Fn execve Ap d
91file, is left unchanged.
92.Pp
93The argument
94.Fa argv
95is a pointer to a null-terminated array of
96character pointers to NUL-terminated character strings.
97These strings construct the argument list to be made available to the new
98process.
99At least one argument must be present in the array;
100by custom, the first element should be
101the name of the executed program (for example, the last component of
102.Fa path ) .
103.Pp
104The argument
105.Fa envp
106is also a pointer to a null-terminated array of
107character pointers to NUL-terminated strings.
108A pointer to this array is normally stored in the global variable
109.Va environ .
110These strings pass information to the
111new process that is not directly an argument to the command (see
112.Xr environ 7 ) .
113.Pp
114File descriptors open in the calling process image remain open in
115the new process image, except for those for which the close-on-exec
116flag is set (see
117.Xr close 2
118and
119.Xr fcntl 2 ) .
120Descriptors that remain open are unaffected by
121.Fn execve .
122In the case of a new setuid or setgid executable being executed, if
123file descriptors 0, 1, or 2 (representing stdin, stdout, and stderr)
124are currently unallocated, these descriptors will be opened to point to
125some system file like
126.Pa /dev/null .
127The intent is to ensure these descriptors are not unallocated, since
128many libraries make assumptions about the use of these 3 file descriptors.
129.Pp
130Signals set to be ignored in the calling process are set to be ignored in
131the
132new process.
133Signals which are set to be caught in the calling process image
134are set to default action in the new process image.
135Blocked signals remain blocked regardless of changes to the signal action.
136The signal stack is reset to be undefined (see
137.Xr sigaction 2
138for more information).
139.Pp
140If the set-user-ID mode bit of the new process image file is set
141(see
142.Xr chmod 2 ) ,
143the effective user ID of the new process image is set to the owner ID
144of the new process image file.
145If the set-group-ID mode bit of the new process image file is set,
146the effective group ID of the new process image is set to the group ID
147of the new process image file.
148(The effective group ID is the first element of the group list.)
149The real user ID, real group ID and
150other group IDs of the new process image remain the same as the calling
151process image.
152After any set-user-ID and set-group-ID processing,
153the effective user ID is recorded as the saved set-user-ID,
154and the effective group ID is recorded as the saved set-group-ID.
155These values may be used in changing the effective IDs later (see
156.Xr setuid 2 ) .
157The set-user-ID and set-group-ID bits have no effect if the
158new process image file is located on a file system mounted with
159the nosuid flag.
160The process will be started without the new permissions.
161.Pp
162The new process also inherits the following attributes from
163the calling process:
164.Pp
165.Bl -column parent_process_ID -offset indent -compact
166.It process ID Ta see Xr getpid 2
167.It parent process ID Ta see Xr getppid 2
168.It process group ID Ta see Xr getpgrp 2
169.It session ID Ta see Xr getsid 2
170.It access groups Ta see Xr getgroups 2
171.It working directory Ta see Xr chdir 2
172.It root directory Ta see Xr chroot 2
173.It control terminal Ta see Xr termios 4
174.It resource usages Ta see Xr getrusage 2
175.It interval timers Ta see Xr getitimer 2
176(unless process image file is setuid or setgid,
177in which case all timers are disabled)
178.It resource limits Ta see Xr getrlimit 2
179.It file mode mask Ta see Xr umask 2
180.It signal mask Ta see Xr sigaction 2 ,
181.Xr sigsetmask 3
182.El
183.Pp
184When a program is executed as a result of an
185.Fn execve
186call, it is entered as follows:
187.Bd -literal -offset indent
188main(int argc, char **argv, char **envp)
189.Ed
190.Pp
191where
192.Fa argc
193is the number of elements in
194.Fa argv
195(the
196.Dq arg count )
197and
198.Fa argv
199points to the array of character pointers
200to the arguments themselves.
201.Pp
202The
203.Fn exect
204function is equivalent to
205.Fn execve
206with the additional property that it executes the file with the program
207tracing facilities enabled (see
208.Xr ptrace 2 ) .
209.Sh RETURN VALUES
210As the
211.Fn execve
212function overlays the current process image
213with a new process image the successful call
214has no process to return to.
215If
216.Fn execve
217does return to the calling process an error has occurred; the
218return value will be \-1 and the global variable
219.Va errno
220is set to indicate the error.
221.Sh ERRORS
222.Fn execve
223will fail and return to the calling process if:
224.Bl -tag -width Er
225.It Bq Er ENOTDIR
226A component of the path prefix is not a directory.
227.It Bq Er ENAMETOOLONG
228A component of a pathname exceeded
229.Dv {NAME_MAX}
230characters, or an entire path name exceeded
231.Dv {PATH_MAX}
232characters.
233.It Bq Er ENOENT
234The new process file does not exist.
235.It Bq Er ELOOP
236Too many symbolic links were encountered in translating the pathname.
237.It Bq Er EACCES
238Search permission is denied for a component of the path prefix.
239.It Bq Er EACCES
240The new process file is not an ordinary file.
241.It Bq Er EACCES
242The new process file mode denies execute permission.
243.It Bq Er EACCES
244The new process file is on a filesystem mounted with execution
245disabled
246.Pf ( Dv MNT_NOEXEC
247in
248.Ao Pa sys/mount.h Ac ) .
249.It Bq Er ENOEXEC
250The new process file has the appropriate access
251permission, but has an invalid magic number in its header.
252.It Bq Er ETXTBSY
253The new process file is a pure procedure (shared text)
254file that is currently open for writing or reading by some process.
255.It Bq Er ENOMEM
256The new process requires more virtual memory than
257is allowed by the imposed maximum
258.Pq Xr getrlimit 2 .
259.It Bq Er E2BIG
260The number of bytes in the new process's argument list
261is larger than the system-imposed limit.
262The limit in the system as released is 262144 bytes
263.Pf ( Dv NCARGS
264in
265.Ao Pa sys/param.h Ac ) .
266.It Bq Er EFAULT
267The new process file is not as long as indicated by
268the size values in its header.
269.It Bq Er EFAULT
270.Fa path ,
271.Fa argv ,
272or
273.Fa envp
274point
275to an illegal address.
276.It Bq Er EIO
277An I/O error occurred while reading from the file system.
278.It Bq Er ENFILE
279During startup of an
280.Em interpreter ,
281the system file table was found to be full.
282.El
283.Sh SEE ALSO
284.Xr _exit 2 ,
285.Xr fork 2 ,
286.Xr execl 3 ,
287.Xr exit 3 ,
288.Xr a.out 5 ,
289.Xr elf 5 ,
290.Xr environ 7
291.Sh STANDARDS
292The
293.Fn exect
294function should not be used in portable applications.
295.Sh HISTORY
296The
297.Fn execve
298function call first appeared in
299.At v7
300and
301.Bx 3 .
302.Sh CAVEATS
303If a program is
304.Em setuid
305to a non-superuser, but is executed when the real
306.Em uid
307is
308.Dq root ,
309then the program has some of the powers of a superuser as well.
310