mips nolonger needs :errfix
[unix-history] / usr / src / lib / libc / gen / exec.3
CommitLineData
ef0a718e
KB
1.\" Copyright (c) 1991 The Regents of the University of California.
2.\" All rights reserved.
6db60273 3.\"
ef0a718e 4.\" %sccs.include.redist.man%
6db60273 5.\"
ae59e04c 6.\" @(#)exec.3 6.4 (Berkeley) %G%
ef0a718e 7.\"
ae59e04c
CL
8.Dd
9.Dt EXEC 3
10.Os
11.Sh NAME
12.Nm execl ,
13.Nm execlp ,
14.Nm execle ,
15.Nm exect ,
16.Nm execv ,
17.Nm execvp
18.Nd execute a file
19.Sh SYNOPSIS
20.Fd #include <unistd.h>
21.Vt extern char **environ;
22.Ft int
23.Fn execl "const char *path" "const char *arg" ...
24.Ft int
25.Fn execlp "const char *file" "const char *arg" ...
26.Ft int
27.Fn execle "const char *path" "const char *arg" ... "char *const envp[]"
28.Ft int
29.Fn exect "const char *path" "char *const argv[]"
30.Ft int
31.Fn execv "const char *path" "char *const argv[]"
32.Ft int
33.Fn execvp "const char *file" "char *const argv[]"
34.Sh DESCRIPTION
35The
36.Nm exec
37family of functions replaces the current process image with a
ef0a718e
KB
38new process image.
39The functions described in this manual page are front-ends for the function
ae59e04c 40.Xr execve 2 .
ef0a718e 41(See the manual page for
ae59e04c 42.Xr execve
ef0a718e 43for detailed information about the replacement of the current process.)
ae59e04c 44.Pp
ef0a718e
KB
45The initial argument for these functions is the pathname of a file which
46is to be executed.
ae59e04c
CL
47.Pp
48The
49.Fa "const char *arg"
50and subsequent ellipses in the
51.Fn execl ,
52.Fn execlp ,
ef0a718e 53and
ae59e04c 54.Fn execle
ef0a718e 55functions can be thought of as
ae59e04c
CL
56.Em arg0 ,
57.Em arg1 ,
ef0a718e 58\&...,
ae59e04c 59.Em argn .
ef0a718e
KB
60Together they describe a list of one or more pointers to null-terminated
61strings that represent the argument list available to the executed program.
62The first argument, by convention, should point to the file name associated
63with the file being executed.
64The list of arguments
ae59e04c
CL
65.Em must
66be terminated by a
67.Dv NULL
68pointer.
69.Pp
6db60273 70The
ae59e04c
CL
71.Fn exect ,
72.Fn execv ,
ef0a718e 73and
ae59e04c 74.Fn execvp
ef0a718e
KB
75functions provide an array of pointers to null-terminated strings that
76represent the argument list available to the new program.
77The first argument, by convention, should point to the file name associated
78with the file begin executed.
79The array of pointers
ae59e04c
CL
80.Sy must
81be terminated by a
82.Dv NULL
83pointer.
84.Pp
6db60273 85The
ae59e04c 86.Fn execle
ef0a718e 87and
ae59e04c 88.Fn exect
ef0a718e 89functions also specify the environment of the executed process by following
ae59e04c
CL
90the
91.Dv NULL
92pointer that terminates the list of arguments in the parameter list
ef0a718e
KB
93or the pointer to the argv array with an additional parameter.
94This additional parameter is an array of pointers to null-terminated strings
95and
ae59e04c
CL
96.Em must
97be terminated by a
98.Dv NULL
99pointer.
ef0a718e
KB
100The other functions take the environment for the new process image from the
101external variable
ae59e04c 102.Va environ
ef0a718e 103in the current process.
ae59e04c 104.Pp
ef0a718e 105Some of these functions have special semantics.
ae59e04c 106.Pp
ef0a718e 107The functions
ae59e04c 108.Fn execlp
6db60273 109and
ae59e04c 110.Fn execvp
ef0a718e 111will duplicate the actions of the shell in searching for an executable file
ae59e04c
CL
112if the specified file name does not contain a slash
113.Dq Li /
114character.
115The search path is the path specified in the environment by
116.Dq Ev PATH
117variable.
118If this variable isn't specified, the default path
119.Dq Ev /bin:/usr/bin:
120is
ef0a718e
KB
121used.
122In addtion, certain errors are treated specially.
ae59e04c 123.Pp
ef0a718e 124If permission is denied for a file (the attempted
ae59e04c
CL
125.Xr execve
126returned
127.Er EACCES ) ,
128these functions will continue searching the rest of
ef0a718e 129the search path.
ae59e04c
CL
130If no other file is found, however, they will return with the global variable
131.Va errno
132set to
133.Er EACCES .
134.Pp
ef0a718e 135If the header of a file isn't recognized (the attempted
ae59e04c
CL
136.Xr execve
137returned
138.Er ENOEXEC ) ,
139these functions will execute the shell with the path of
ef0a718e
KB
140the file as its first argument.
141(If this attempt fails, no further searching is done.)
ae59e04c 142.Pp
ef0a718e 143If the file is currently busy (the attempted
ae59e04c
CL
144.Xr execve
145returned
146.Er ETXTBUSY ) ,
147these functions will sleep for several seconds,
ef0a718e 148periodically re-attempting to execute the file.
ae59e04c 149.Pp
ef0a718e 150The function
ae59e04c 151.Fn exect
ef0a718e 152executes a file with the program tracing facilities enabled (see
ae59e04c
CL
153.Xr ptrace 2 ) .
154.Sh RETURN VALUES
ef0a718e 155If any of the
ae59e04c 156.Xr exec
ef0a718e 157functions returns, an error will have occurred.
ae59e04c
CL
158The return value is \-1, and the global variable
159.Va errno
ef0a718e 160will be set to indicate the error.
ae59e04c
CL
161.Sh FILES
162.Bl -tag -width /bin/sh - compact
163.It Pa /bin/sh
164The shell.
165.El
166.Sh ERRORS
167.Fn Execl ,
168.Fn execle ,
169.Fn execlp
6db60273 170and
ae59e04c 171.Fn execvp
ef0a718e 172may fail and set
ae59e04c 173.Va errno
ef0a718e 174for any of the errors specified for the library functions
ae59e04c 175.Xr execve 2
6db60273 176and
ae59e04c
CL
177.Xr malloc 3 .
178.Pp
179.Fn Exect
ef0a718e 180and
ae59e04c 181.Fn execv
ef0a718e 182may fail and set
ae59e04c 183.Va errno
ef0a718e 184for any of the errors specified for the library function
ae59e04c
CL
185.Xr execve 2 .
186.Sh SEE ALSO
187.Xr sh 1 ,
188.Xr execve 2 ,
189.Xr fork 2 ,
190.Xr trace 2 ,
191.Xr environ 7 ,
192.Xr ptrace 2 ,
193.Xr environ 7 ,
194.Sh COMPATIBILITY
ef0a718e 195Historically, the default path for the
ae59e04c 196.Fn execlp
ef0a718e 197and
ae59e04c
CL
198.Fn execvp
199functions was
200.Dq Pa :/bin:/usr/bin .
ef0a718e
KB
201This was changed to place the current directory last to enhance system
202security.
ae59e04c 203.Pp
ef0a718e 204The behavior of
ae59e04c 205.Fn execlp
ef0a718e 206and
ae59e04c 207.Fn execvp
ef0a718e
KB
208when errors occur while attempting to execute the file is historic
209practice, but has not traditionally been documented and is not specified
ae59e04c
CL
210by the
211.Tn POSIX
212standard.
213.Pp
ef0a718e 214Traditionally, the functions
ae59e04c
CL
215.Fn execlp
216and
217.Fn execvp
218ignored all errors except for the ones described above and
219.Er ENOMEM
ef0a718e 220and
ae59e04c
CL
221.Er E2BIG ,
222upon which they returned.
ef0a718e 223They now return if any error other than the ones described above occurs.
ae59e04c
CL
224.Sh STANDARDS
225.Fn Execl ,
226.Fn execv ,
227.Fn execle ,
228.Fn execlp
6db60273 229and
ae59e04c
CL
230.Fn execvp
231conform to
232.St -p1003.1-88 .