cleanup from Guy Harris; use "path" everywhere, not "name"; argv and
[unix-history] / usr / src / lib / libc / sys / execve.2
CommitLineData
2fe0358e
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
2d025b69 5.\" @(#)execve.2 6.8 (Berkeley) %G%
2fe0358e 6.\"
547837f0 7.TH EXECVE 2 ""
2fe0358e
KM
8.UC 4
9.SH NAME
34aad5a6 10execve \- execute a file
2fe0358e 11.SH SYNOPSIS
34aad5a6 12.ft B
2d025b69 13execve(path, argv, envp)
34aad5a6 14.br
2d025b69 15char *path, **argv, **envp;
2fe0358e
KM
16.fi
17.SH DESCRIPTION
34aad5a6
KM
18.I Execve
19transforms the calling process into a new process.
2d025b69
KB
20The new process is constructed from an ordinary file,
21whose name is pointed to by
22.IR path ,
34aad5a6
KM
23called the \fInew process file\fP.
24This file is either an executable object file,
25or a file of data for an interpreter.
26An executable object file consists of an identifying header,
27followed by pages of data representing the initial program (text)
28and initialized data pages. Additional pages may be specified
d4bad45b 29by the header to be initialized with zero data. See
34aad5a6
KM
30.IR a.out (5).
31.PP
2d025b69
KB
32An interpreter file begins with a line of the form
33.RB `` "#! \fIinterpreter\fP [\fIarg\fP]" ''.
34aad5a6
KM
34When an interpreter file is
35.IR execve\| 'd,
2d025b69
KB
36the system \fIexecve\fP\|'s the specified \fIinterpreter\fP.
37If the optional
38.I arg
39is specified, it becomes the first argument to the
40.IR interpreter ,
41and the name of the originally
42.BR execve 'd
43file becomes the second argument;
44otherwise, the name of the originally
45.BR execve 'd
46file becomes the first argument. The original arguments are shifted over to
47become the subsequent arguments. The zeroth argument, normally the name of the
48.BR execve 'd
49file, is left unchanged.
34aad5a6
KM
50.PP
51There can be no return from a successful \fIexecve\fP because the calling
2fe0358e 52core image is lost.
34aad5a6
KM
53This is the mechanism whereby different process images become active.
54.PP
2d025b69
KB
55The argument \fIargv\fP is a pointer to a null-terminated array of
56character pointers to null-terminated character strings.
57These strings constitute the argument list to be made available to the new
34aad5a6
KM
58process. By convention, at least one argument must be present in
59this array, and the first element of this array should be
2d025b69 60the name of the executed program (i.e., the last component of \fIpath\fP).
34aad5a6 61.PP
2d025b69
KB
62The argument \fIenvp\fP is also a pointer to a null-terminated array of
63character pointers to null-terminated strings.
64These strings pass information to the
d4bad45b
AH
65new process that is not directly an argument to the command (see
66.IR environ (7)).
34aad5a6
KM
67.PP
68Descriptors open in the calling process remain open in
69the new process, except for those for which the close-on-exec
d4bad45b 70flag is set (see
2d025b69
KB
71.IR close (2)
72and
73.IR fcntl (2)).
b5f0627a 74Descriptors that remain open are unaffected by
34aad5a6
KM
75.IR execve .
76.PP
77Ignored signals remain ignored across an
78.IR execve ,
79but signals that are caught are reset to their default values.
fa6928f4 80Blocked signals remain blocked regardless of changes to the signal action.
d4bad45b
AH
81The signal stack is reset to be undefined (see
82.IR sigvec (2)
83for more information).
34aad5a6
KM
84.PP
85Each process has
2fe0358e 86.I real
d4bad45b 87user and group IDs and an
2fe0358e 88.I effective
34aad5a6
KM
89user and group IDs. The
90.I real
91ID identifies the person using the system; the
92.I effective
93ID determines his access privileges.
94.I Execve
2fe0358e 95changes the effective user and group ID to
34aad5a6
KM
96the owner of the executed file if the file has the \*(lqset-user-ID\*(rq
97or \*(lqset-group-ID\*(rq modes. The
98.I real
2d025b69 99user and group IDs are not affected.
2fe0358e 100.PP
34aad5a6
KM
101The new process also inherits the following attributes from
102the calling process:
2fe0358e 103.PP
34aad5a6
KM
104.in +5n
105.nf
106.ta +2i
107process ID see \fIgetpid\fP\|(2)
108parent process ID see \fIgetppid\fP\|(2)
109process group ID see \fIgetpgrp\fP\|(2)
110access groups see \fIgetgroups\fP\|(2)
111working directory see \fIchdir\fP\|(2)
112root directory see \fIchroot\fP\|(2)
113control terminal see \fItty\fP\|(4)
114resource usages see \fIgetrusage\fP\|(2)
115interval timers see \fIgetitimer\fP\|(2)
116resource limits see \fIgetrlimit\fP\|(2)
117file mode mask see \fIumask\fP\|(2)
2d025b69 118signal mask see \fIsigvec\fP\|(2), \fIsigsetmask\fP\|(2)
34aad5a6
KM
119.in -5n
120.fi
2fe0358e 121.PP
34aad5a6 122When the executed program begins, it is called as follows:
2fe0358e 123.PP
34aad5a6 124.DT
2fe0358e
KM
125.nf
126 main(argc, argv, envp)
127 int argc;
128 char **argv, **envp;
129.fi
130.PP
131where
34aad5a6
KM
132.I argc
133is the number of elements in \fIargv\fP
134(the ``arg count'')
2fe0358e 135and
34aad5a6 136.I argv
2d025b69 137points to the array of character pointers
2fe0358e 138to the arguments themselves.
2fe0358e
KM
139.PP
140.I Envp
141is a pointer to an array of strings that constitute
142the
143.I environment
144of the process.
34aad5a6 145A pointer to this array is also stored in the global variable ``environ''.
2fe0358e
KM
146Each string consists of a name, an \*(lq=\*(rq, and a null-terminated value.
147The array of pointers is terminated by a null pointer.
148The shell
149.IR sh (1)
150passes an environment entry for each global shell variable
151defined when the program is called.
152See
34aad5a6 153.IR environ (7)
2fe0358e
KM
154for some conventionally
155used names.
34aad5a6 156.SH "RETURN VALUE
2fe0358e 157If
34aad5a6
KM
158.I execve
159returns to the calling process an error has occurred; the
160return value will be \-1 and the global variable
161.I errno
162will contain an error code.
163.SH ERRORS
164.I Execve
165will fail and return to the calling process if one or more
166of the following are true:
167.TP 15
b5984ffe
KM
168[ENOTDIR]
169A component of the path prefix is not a directory.
170.TP 15
171[EINVAL]
172The pathname contains a character with the high-order bit set.
173.TP 15
174[ENAMETOOLONG]
175A component of a pathname exceeded 255 characters,
176or an entire path name exceeded 1023 characters.
177.TP 15
34aad5a6 178[ENOENT]
b5984ffe 179The new process file does not exist.
34aad5a6 180.TP 15
b5984ffe
KM
181[ELOOP]
182Too many symbolic links were encountered in translating the pathname.
34aad5a6
KM
183.TP 15
184[EACCES]
b5984ffe 185Search permission is denied for a component of the path prefix.
34aad5a6
KM
186.TP 15
187[EACCES]
188The new process file is not an ordinary file.
189.TP 15
190[EACCES]
191The new process file mode denies execute permission.
192.TP 15
193[ENOEXEC]
194The new process file has the appropriate access
195permission, but has an invalid magic number in its header.
196.TP 15
197[ETXTBSY]
198The new process file is a pure procedure (shared text)
199file that is currently open for writing or reading by some process.
200.TP 15
201[ENOMEM]
202The new process requires more virtual memory than
203is allowed by the imposed maximum
204.RI ( getrlimit (2)).
205.TP 15
206[E2BIG]
207The number of bytes in the new process's argument list
95aab6a2
MK
208is larger than the system-imposed limit.
209The limit in the system as released is 20480 bytes
210(NCARGS in
211.IR <sys/param.h> .
34aad5a6
KM
212.TP 15
213[EFAULT]
214The new process file is not as long as indicated by
215the size values in its header.
216.TP 15
217[EFAULT]
218\fIPath\fP\|, \fIargv\fP\|, or \fIenvp\fP point
219to an illegal address.
fd690c8b
KM
220.TP 15
221[EIO]
222An I/O error occurred while reading from the file system.
34aad5a6
KM
223.SH CAVEATS
224If a program is
225.I setuid
226to a non-super-user, but is executed when
fa6928f4 227the real \fIuid\fP is ``root'', then the program has some of the powers
34aad5a6
KM
228of a super-user as well.
229.SH "SEE ALSO"
230exit(2), fork(2), execl(3), environ(7)