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