xref: /original-bsd/lib/libc/sys/execve.2 (revision deff14a8)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)execve.2	8.5 (Berkeley) 06/01/94
7.\"
8.Dd
9.Dt EXECVE 2
10.Os BSD 4
11.Sh NAME
12.Nm execve
13.Nd execute a file
14.Sh SYNOPSIS
15.Fd #include <unistd.h>
16.Ft int
17.Fn execve "const char *path" "char *const argv[]" "char *const envp[]"
18.Sh DESCRIPTION
19.Fn Execve
20transforms the calling process into a new process.
21The new process is constructed from an ordinary file,
22whose name is pointed to by
23.Fa path ,
24called the
25.Em new process file .
26This file is either an executable object file,
27or a file of data for an interpreter.
28An executable object file consists of an identifying header,
29followed by pages of data representing the initial program (text)
30and initialized data pages.  Additional pages may be specified
31by the header to be initialized with zero data;  see
32.Xr a.out 5 .
33.Pp
34An interpreter file begins with a line of the form:
35.Pp
36.Bd -filled -offset indent -compact
37.Sy \&#!
38.Em interpreter
39.Bq Em arg
40.Ed
41.Pp
42When an interpreter file is
43.\" was .Fn execve Ap d ,
44\fBexecve\fP'd,
45the system
46.\" was .Fn execve Ap s
47\fBexecve\fP's
48runs the specified
49.Em interpreter .
50If the optional
51.Em arg
52is specified, it becomes the first argument to the
53.Em interpreter ,
54and the name of the originally
55.\" was .Fn execve Ap d
56\fBexecve\fP'd
57file becomes the second argument;
58otherwise, the name of the originally
59.\" was .Fn execve Ap d
60\fBexecve\fP'd
61file becomes the first argument.  The original arguments are shifted over to
62become the subsequent arguments.  The zeroth argument, normally the name of the
63.\" was .Fn execve Ap d
64\fBexecve\fP'd
65file, is left unchanged.
66.Pp
67The argument
68.Fa argv
69is a pointer to a null-terminated array of
70character pointers to null-terminated character strings.
71These strings construct the argument list to be made available to the new
72process.  At least one argument must be present in
73the array; by custom, the first element should be
74the name of the executed program (for example, the last component of
75.Fa path ) .
76.Pp
77The argument
78.Fa envp
79is also a pointer to a null-terminated array of
80character pointers to null-terminated strings.
81A pointer to this array is normally stored in the global variable
82.Va environ.
83These strings pass information to the
84new process that is not directly an argument to the command (see
85.Xr environ 7 ) .
86.Pp
87File descriptors open in the calling process image remain open in
88the new process image, except for those for which the close-on-exec
89flag is set (see
90.Xr close 2
91and
92.Xr fcntl 2 ) .
93Descriptors that remain open are unaffected by
94.Fn execve .
95.Pp
96Signals set to be ignored in the calling process are set to be ignored in
97the
98new process. Signals which are set to be caught in the calling process image
99are set to default action in the new process image.
100Blocked signals remain blocked regardless of changes to the signal action.
101The signal stack is reset to be undefined (see
102.Xr sigaction 2
103for more information).
104.Pp
105If the set-user-ID mode bit of the new process image file is set
106(see
107.Xr chmod 2 ) ,
108the effective user ID of the new process image is set to the owner ID
109of the new process image file.
110If the set-group-ID mode bit of the new process image file is set,
111the effective group ID of the new process image is set to the group ID
112of the new process image file.
113(The effective group ID is the first element of the group list.)
114The real user ID, real group ID and
115other group IDs of the new process image remain the same as the calling
116process image.
117After any set-user-ID and set-group-ID processing,
118the effective user ID is recorded as the saved set-user-ID,
119and the effective group ID is recorded as the saved set-group-ID.
120These values may be used in changing the effective IDs later (see
121.Xr setuid 2 ) .
122.ne 1i
123.Pp
124The new process also inherits the following attributes from
125the calling process:
126.Pp
127.Bl -column parent_process_ID -offset indent -compact
128.It process ID Ta see Xr getpid 2
129.It parent process ID Ta see Xr getppid 2
130.It process group ID Ta see Xr getpgrp 2
131.It access groups Ta see Xr getgroups 2
132.It working directory Ta see Xr chdir 2
133.It root directory Ta see Xr chroot 2
134.It control terminal Ta see Xr termios 4
135.It resource usages Ta see Xr getrusage 2
136.It interval timers Ta see Xr getitimer 2
137.It resource limits Ta see Xr getrlimit 2
138.It file mode mask Ta see Xr umask 2
139.It signal mask Ta see Xr sigvec 2 ,
140.Xr sigsetmask 2
141.El
142.Pp
143When a program is executed as a result of an
144.Fn execve
145call, it is entered as follows:
146.Bd -literal -offset indent
147main(argc, argv, envp)
148int argc;
149char **argv, **envp;
150.Ed
151.Pp
152where
153.Fa argc
154is the number of elements in
155.Fa argv
156(the ``arg count'')
157and
158.Fa argv
159points to the array of character pointers
160to the arguments themselves.
161.Sh RETURN VALUES
162As the
163.Fn execve
164function overlays the current process image
165with a new process image the successful call
166has no process to return to.
167If
168.Fn execve
169does return to the calling process an error has occurred; the
170return value will be -1 and the global variable
171.Va errno
172is set to indicate the error.
173.Sh ERRORS
174.Fn Execve
175will fail and return to the calling process if:
176.Bl -tag -width [ENAMETOOLONG]
177.It Bq Er ENOTDIR
178A component of the path prefix is not a directory.
179.It Bq Er EINVAL
180The pathname contains a character with the high-order bit set.
181.It Bq Er ENAMETOOLONG
182A component of a pathname exceeded 255 characters,
183or an entire path name exceeded 1023 characters.
184.It Bq Er ENOENT
185The new process file does not exist.
186.It Bq Er ELOOP
187Too many symbolic links were encountered in translating the pathname.
188.It Bq Er EACCES
189Search permission is denied for a component of the path prefix.
190.It Bq Er EACCES
191The new process file is not an ordinary file.
192.It Bq Er EACCES
193The new process file mode denies execute permission.
194.It Bq Er ENOEXEC
195The new process file has the appropriate access
196permission, but has an invalid magic number in its header.
197.It Bq Er ETXTBSY
198The new process file is a pure procedure (shared text)
199file that is currently open for writing or reading by some process.
200.ne 1i
201.It Bq Er ENOMEM
202The new process requires more virtual memory than
203is allowed by the imposed maximum
204.Pq Xr getrlimit 2 .
205.It Bq Er E2BIG
206The number of bytes in the new process's argument list
207is larger than the system-imposed limit.
208The limit in the system as released is 20480 bytes
209.Pf ( Dv NCARGS
210in
211.Ao Pa sys/param.h Ac .
212.It Bq Er EFAULT
213The new process file is not as long as indicated by
214the size values in its header.
215.It Bq Er EFAULT
216.Fa Path ,
217.Fa argv ,
218or
219.Fa envp
220point
221to an illegal address.
222.It Bq Er EIO
223An I/O error occurred while reading from the file system.
224.El
225.Sh CAVEAT
226If a program is
227.Em setuid
228to a non-super-user, but is executed when
229the real
230.Em uid
231is ``root'', then the program has some of the powers
232of a super-user as well.
233.Sh SEE ALSO
234.Xr exit 2 ,
235.Xr fork 2 ,
236.Xr execl 3 ,
237.Xr environ 7
238.Sh HISTORY
239The
240.Nm
241function call appeared in
242.Bx 4.2 .
243