161111Sbostic.\" Copyright (c) 1991, 1993 261111Sbostic.\" The Regents of the University of California. All rights reserved. 320494Smckusick.\" 446591Sbostic.\" %sccs.include.redist.man% 520494Smckusick.\" 6*64884Sbostic.\" @(#)exec.3 8.2 (Berkeley) 11/16/93 746591Sbostic.\" 848352Scael.Dd 948352Scael.Dt EXEC 3 1048352Scael.Os 1148352Scael.Sh NAME 1248352Scael.Nm execl , 1348352Scael.Nm execlp , 1448352Scael.Nm execle , 1548352Scael.Nm exect , 1648352Scael.Nm execv , 1748352Scael.Nm execvp 1848352Scael.Nd execute a file 1948352Scael.Sh SYNOPSIS 2048352Scael.Fd #include <unistd.h> 2148352Scael.Vt extern char **environ; 2248352Scael.Ft int 2348352Scael.Fn execl "const char *path" "const char *arg" ... 2448352Scael.Ft int 2548352Scael.Fn execlp "const char *file" "const char *arg" ... 2648352Scael.Ft int 2748352Scael.Fn execle "const char *path" "const char *arg" ... "char *const envp[]" 2848352Scael.Ft int 2948352Scael.Fn exect "const char *path" "char *const argv[]" 3048352Scael.Ft int 3148352Scael.Fn execv "const char *path" "char *const argv[]" 3248352Scael.Ft int 3348352Scael.Fn execvp "const char *file" "char *const argv[]" 3448352Scael.Sh DESCRIPTION 3548352ScaelThe 3648352Scael.Nm exec 3748352Scaelfamily 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 4048352Scael.Xr execve 2 . 4146591Sbostic(See the manual page for 4248352Scael.Xr execve 4346591Sbosticfor detailed information about the replacement of the current process.) 4448352Scael.Pp 4546591SbosticThe initial argument for these functions is the pathname of a file which 4646591Sbosticis to be executed. 4748352Scael.Pp 4848352ScaelThe 4948352Scael.Fa "const char *arg" 5048352Scaeland subsequent ellipses in the 5148352Scael.Fn execl , 5248352Scael.Fn execlp , 5346591Sbosticand 5448352Scael.Fn execle 5546591Sbosticfunctions can be thought of as 5648352Scael.Em arg0 , 5748352Scael.Em arg1 , 5846591Sbostic\&..., 5948352Scael.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 6548352Scael.Em must 6648352Scaelbe terminated by a 6748352Scael.Dv NULL 6848352Scaelpointer. 6948352Scael.Pp 7020494SmckusickThe 7148352Scael.Fn exect , 7248352Scael.Fn execv , 7346591Sbosticand 7448352Scael.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 8048352Scael.Sy must 8148352Scaelbe terminated by a 8248352Scael.Dv NULL 8348352Scaelpointer. 8448352Scael.Pp 8520494SmckusickThe 8648352Scael.Fn execle 8746591Sbosticand 8848352Scael.Fn exect 8946591Sbosticfunctions also specify the environment of the executed process by following 9048352Scaelthe 9148352Scael.Dv NULL 9248352Scaelpointer 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 9648352Scael.Em must 9748352Scaelbe terminated by a 9848352Scael.Dv NULL 9948352Scaelpointer. 10046591SbosticThe other functions take the environment for the new process image from the 10146591Sbosticexternal variable 10248352Scael.Va environ 10346591Sbosticin the current process. 10448352Scael.Pp 10546591SbosticSome of these functions have special semantics. 10648352Scael.Pp 10746591SbosticThe functions 10848352Scael.Fn execlp 10920494Smckusickand 11048352Scael.Fn execvp 11146591Sbosticwill duplicate the actions of the shell in searching for an executable file 11248352Scaelif the specified file name does not contain a slash 11348352Scael.Dq Li / 11448352Scaelcharacter. 11548352ScaelThe search path is the path specified in the environment by 11648352Scael.Dq Ev PATH 11748352Scaelvariable. 11848352ScaelIf this variable isn't specified, the default path 11948352Scael.Dq Ev /bin:/usr/bin: 12048352Scaelis 12146591Sbosticused. 122*64884SbosticIn addition, certain errors are treated specially. 12348352Scael.Pp 12446591SbosticIf permission is denied for a file (the attempted 12548352Scael.Xr execve 12648352Scaelreturned 12748352Scael.Er EACCES ) , 12848352Scaelthese functions will continue searching the rest of 12946591Sbosticthe search path. 13048352ScaelIf no other file is found, however, they will return with the global variable 13148352Scael.Va errno 13248352Scaelset to 13348352Scael.Er EACCES . 13448352Scael.Pp 13546591SbosticIf the header of a file isn't recognized (the attempted 13648352Scael.Xr execve 13748352Scaelreturned 13848352Scael.Er ENOEXEC ) , 13948352Scaelthese 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.) 14248352Scael.Pp 14346591SbosticIf the file is currently busy (the attempted 14448352Scael.Xr execve 14548352Scaelreturned 14648352Scael.Er ETXTBUSY ) , 14748352Scaelthese functions will sleep for several seconds, 14846591Sbosticperiodically re-attempting to execute the file. 14948352Scael.Pp 15046591SbosticThe function 15148352Scael.Fn exect 15246591Sbosticexecutes a file with the program tracing facilities enabled (see 15348352Scael.Xr ptrace 2 ) . 15448352Scael.Sh RETURN VALUES 15546591SbosticIf any of the 15648352Scael.Xr exec 15746591Sbosticfunctions returns, an error will have occurred. 15848352ScaelThe return value is \-1, and the global variable 15948352Scael.Va errno 16046591Sbosticwill be set to indicate the error. 16148352Scael.Sh FILES 16248352Scael.Bl -tag -width /bin/sh - compact 16348352Scael.It Pa /bin/sh 16448352ScaelThe shell. 16548352Scael.El 16648352Scael.Sh ERRORS 16748352Scael.Fn Execl , 16848352Scael.Fn execle , 16948352Scael.Fn execlp 17020494Smckusickand 17148352Scael.Fn execvp 17246591Sbosticmay fail and set 17348352Scael.Va errno 17446591Sbosticfor any of the errors specified for the library functions 17548352Scael.Xr execve 2 17620494Smckusickand 17748352Scael.Xr malloc 3 . 17848352Scael.Pp 17948352Scael.Fn Exect 18046591Sbosticand 18148352Scael.Fn execv 18246591Sbosticmay fail and set 18348352Scael.Va errno 18446591Sbosticfor any of the errors specified for the library function 18548352Scael.Xr execve 2 . 18648352Scael.Sh SEE ALSO 18748352Scael.Xr sh 1 , 18848352Scael.Xr execve 2 , 18948352Scael.Xr fork 2 , 19048352Scael.Xr trace 2 , 19148352Scael.Xr environ 7 , 19248352Scael.Xr ptrace 2 , 19348352Scael.Xr environ 7 , 19448352Scael.Sh COMPATIBILITY 19546591SbosticHistorically, the default path for the 19648352Scael.Fn execlp 19746591Sbosticand 19848352Scael.Fn execvp 19948352Scaelfunctions was 20048352Scael.Dq Pa :/bin:/usr/bin . 20146591SbosticThis was changed to place the current directory last to enhance system 20246591Sbosticsecurity. 20348352Scael.Pp 20446591SbosticThe behavior of 20548352Scael.Fn execlp 20646591Sbosticand 20748352Scael.Fn execvp 20846591Sbosticwhen errors occur while attempting to execute the file is historic 20946591Sbosticpractice, but has not traditionally been documented and is not specified 21048352Scaelby the 21148352Scael.Tn POSIX 21248352Scaelstandard. 21348352Scael.Pp 21446591SbosticTraditionally, the functions 21548352Scael.Fn execlp 21620494Smckusickand 21748352Scael.Fn execvp 21848352Scaelignored all errors except for the ones described above and 21948352Scael.Er ENOMEM 22048352Scaeland 22148352Scael.Er E2BIG , 22248352Scaelupon which they returned. 22346591SbosticThey now return if any error other than the ones described above occurs. 22448352Scael.Sh STANDARDS 22548352Scael.Fn Execl , 22648352Scael.Fn execv , 22748352Scael.Fn execle , 22848352Scael.Fn execlp 22946591Sbosticand 23048352Scael.Fn execvp 23148352Scaelconform to 23248352Scael.St -p1003.1-88 . 233