xref: /csrg-svn/lib/libc/gen/exec.3 (revision 48352)
146591Sbostic.\" Copyright (c) 1991 The Regents of the University of California.
246591Sbostic.\" All rights reserved.
320494Smckusick.\"
446591Sbostic.\" %sccs.include.redist.man%
520494Smckusick.\"
6*48352Scael.\"     @(#)exec.3	6.4 (Berkeley) 04/19/91
746591Sbostic.\"
8*48352Scael.Dd
9*48352Scael.Dt EXEC 3
10*48352Scael.Os
11*48352Scael.Sh NAME
12*48352Scael.Nm execl ,
13*48352Scael.Nm execlp ,
14*48352Scael.Nm execle ,
15*48352Scael.Nm exect ,
16*48352Scael.Nm execv ,
17*48352Scael.Nm execvp
18*48352Scael.Nd execute a file
19*48352Scael.Sh SYNOPSIS
20*48352Scael.Fd #include <unistd.h>
21*48352Scael.Vt extern char **environ;
22*48352Scael.Ft int
23*48352Scael.Fn execl "const char *path" "const char *arg" ...
24*48352Scael.Ft int
25*48352Scael.Fn execlp "const char *file" "const char *arg" ...
26*48352Scael.Ft int
27*48352Scael.Fn execle "const char *path" "const char *arg" ... "char *const envp[]"
28*48352Scael.Ft int
29*48352Scael.Fn exect "const char *path" "char *const argv[]"
30*48352Scael.Ft int
31*48352Scael.Fn execv "const char *path" "char *const argv[]"
32*48352Scael.Ft int
33*48352Scael.Fn execvp "const char *file" "char *const argv[]"
34*48352Scael.Sh DESCRIPTION
35*48352ScaelThe
36*48352Scael.Nm exec
37*48352Scaelfamily of functions replaces the current process image with a
3846591Sbosticnew process image.
3946591SbosticThe functions described in this manual page are front-ends for the function
40*48352Scael.Xr execve 2 .
4146591Sbostic(See the manual page for
42*48352Scael.Xr execve
4346591Sbosticfor detailed information about the replacement of the current process.)
44*48352Scael.Pp
4546591SbosticThe initial argument for these functions is the pathname of a file which
4646591Sbosticis to be executed.
47*48352Scael.Pp
48*48352ScaelThe
49*48352Scael.Fa "const char *arg"
50*48352Scaeland subsequent ellipses in the
51*48352Scael.Fn execl ,
52*48352Scael.Fn execlp ,
5346591Sbosticand
54*48352Scael.Fn execle
5546591Sbosticfunctions can be thought of as
56*48352Scael.Em arg0 ,
57*48352Scael.Em arg1 ,
5846591Sbostic\&...,
59*48352Scael.Em argn .
6046591SbosticTogether they describe a list of one or more pointers to null-terminated
6146591Sbosticstrings that represent the argument list available to the executed program.
6246591SbosticThe first argument, by convention, should point to the file name associated
6346591Sbosticwith the file being executed.
6446591SbosticThe list of arguments
65*48352Scael.Em must
66*48352Scaelbe terminated by a
67*48352Scael.Dv NULL
68*48352Scaelpointer.
69*48352Scael.Pp
7020494SmckusickThe
71*48352Scael.Fn exect ,
72*48352Scael.Fn execv ,
7346591Sbosticand
74*48352Scael.Fn execvp
7546591Sbosticfunctions provide an array of pointers to null-terminated strings that
7646591Sbosticrepresent the argument list available to the new program.
7746591SbosticThe first argument, by convention, should point to the file name associated
7846591Sbosticwith the file begin executed.
7946591SbosticThe array of pointers
80*48352Scael.Sy must
81*48352Scaelbe terminated by a
82*48352Scael.Dv NULL
83*48352Scaelpointer.
84*48352Scael.Pp
8520494SmckusickThe
86*48352Scael.Fn execle
8746591Sbosticand
88*48352Scael.Fn exect
8946591Sbosticfunctions also specify the environment of the executed process by following
90*48352Scaelthe
91*48352Scael.Dv NULL
92*48352Scaelpointer that terminates the list of arguments in the parameter list
9346591Sbosticor the pointer to the argv array with an additional parameter.
9446591SbosticThis additional parameter is an array of pointers to null-terminated strings
9546591Sbosticand
96*48352Scael.Em must
97*48352Scaelbe terminated by a
98*48352Scael.Dv NULL
99*48352Scaelpointer.
10046591SbosticThe other functions take the environment for the new process image from the
10146591Sbosticexternal variable
102*48352Scael.Va environ
10346591Sbosticin the current process.
104*48352Scael.Pp
10546591SbosticSome of these functions have special semantics.
106*48352Scael.Pp
10746591SbosticThe functions
108*48352Scael.Fn execlp
10920494Smckusickand
110*48352Scael.Fn execvp
11146591Sbosticwill duplicate the actions of the shell in searching for an executable file
112*48352Scaelif the specified file name does not contain a slash
113*48352Scael.Dq Li /
114*48352Scaelcharacter.
115*48352ScaelThe search path is the path specified in the environment by
116*48352Scael.Dq Ev PATH
117*48352Scaelvariable.
118*48352ScaelIf this variable isn't specified, the default path
119*48352Scael.Dq Ev /bin:/usr/bin:
120*48352Scaelis
12146591Sbosticused.
12246591SbosticIn addtion, certain errors are treated specially.
123*48352Scael.Pp
12446591SbosticIf permission is denied for a file (the attempted
125*48352Scael.Xr execve
126*48352Scaelreturned
127*48352Scael.Er EACCES ) ,
128*48352Scaelthese functions will continue searching the rest of
12946591Sbosticthe search path.
130*48352ScaelIf no other file is found, however, they will return with the global variable
131*48352Scael.Va errno
132*48352Scaelset to
133*48352Scael.Er EACCES .
134*48352Scael.Pp
13546591SbosticIf the header of a file isn't recognized (the attempted
136*48352Scael.Xr execve
137*48352Scaelreturned
138*48352Scael.Er ENOEXEC ) ,
139*48352Scaelthese functions will execute the shell with the path of
14046591Sbosticthe file as its first argument.
14146591Sbostic(If this attempt fails, no further searching is done.)
142*48352Scael.Pp
14346591SbosticIf the file is currently busy (the attempted
144*48352Scael.Xr execve
145*48352Scaelreturned
146*48352Scael.Er ETXTBUSY ) ,
147*48352Scaelthese functions will sleep for several seconds,
14846591Sbosticperiodically re-attempting to execute the file.
149*48352Scael.Pp
15046591SbosticThe function
151*48352Scael.Fn exect
15246591Sbosticexecutes a file with the program tracing facilities enabled (see
153*48352Scael.Xr ptrace 2 ) .
154*48352Scael.Sh RETURN VALUES
15546591SbosticIf any of the
156*48352Scael.Xr exec
15746591Sbosticfunctions returns, an error will have occurred.
158*48352ScaelThe return value is \-1, and the global variable
159*48352Scael.Va errno
16046591Sbosticwill be set to indicate the error.
161*48352Scael.Sh FILES
162*48352Scael.Bl -tag -width /bin/sh - compact
163*48352Scael.It Pa /bin/sh
164*48352ScaelThe shell.
165*48352Scael.El
166*48352Scael.Sh ERRORS
167*48352Scael.Fn Execl ,
168*48352Scael.Fn execle ,
169*48352Scael.Fn execlp
17020494Smckusickand
171*48352Scael.Fn execvp
17246591Sbosticmay fail and set
173*48352Scael.Va errno
17446591Sbosticfor any of the errors specified for the library functions
175*48352Scael.Xr execve 2
17620494Smckusickand
177*48352Scael.Xr malloc 3 .
178*48352Scael.Pp
179*48352Scael.Fn Exect
18046591Sbosticand
181*48352Scael.Fn execv
18246591Sbosticmay fail and set
183*48352Scael.Va errno
18446591Sbosticfor any of the errors specified for the library function
185*48352Scael.Xr execve 2 .
186*48352Scael.Sh SEE ALSO
187*48352Scael.Xr sh 1 ,
188*48352Scael.Xr execve 2 ,
189*48352Scael.Xr fork 2 ,
190*48352Scael.Xr trace 2 ,
191*48352Scael.Xr environ 7 ,
192*48352Scael.Xr ptrace 2 ,
193*48352Scael.Xr environ 7 ,
194*48352Scael.Sh COMPATIBILITY
19546591SbosticHistorically, the default path for the
196*48352Scael.Fn execlp
19746591Sbosticand
198*48352Scael.Fn execvp
199*48352Scaelfunctions was
200*48352Scael.Dq Pa :/bin:/usr/bin .
20146591SbosticThis was changed to place the current directory last to enhance system
20246591Sbosticsecurity.
203*48352Scael.Pp
20446591SbosticThe behavior of
205*48352Scael.Fn execlp
20646591Sbosticand
207*48352Scael.Fn execvp
20846591Sbosticwhen errors occur while attempting to execute the file is historic
20946591Sbosticpractice, but has not traditionally been documented and is not specified
210*48352Scaelby the
211*48352Scael.Tn POSIX
212*48352Scaelstandard.
213*48352Scael.Pp
21446591SbosticTraditionally, the functions
215*48352Scael.Fn execlp
21620494Smckusickand
217*48352Scael.Fn execvp
218*48352Scaelignored all errors except for the ones described above and
219*48352Scael.Er ENOMEM
220*48352Scaeland
221*48352Scael.Er E2BIG ,
222*48352Scaelupon which they returned.
22346591SbosticThey now return if any error other than the ones described above occurs.
224*48352Scael.Sh STANDARDS
225*48352Scael.Fn Execl ,
226*48352Scael.Fn execv ,
227*48352Scael.Fn execle ,
228*48352Scael.Fn execlp
22946591Sbosticand
230*48352Scael.Fn execvp
231*48352Scaelconform to
232*48352Scael.St -p1003.1-88 .
233