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