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