1.\" Copyright (c) 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd December 11, 2023 29.Dt EXEC 3 30.Os 31.Sh NAME 32.Nm execl , 33.Nm execlp , 34.Nm execle , 35.Nm exect , 36.Nm execv , 37.Nm execvp , 38.Nm execvpe , 39.Nm execvP 40.Nd execute a file 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In unistd.h 45.Vt extern char **environ ; 46.Ft int 47.Fn execl "const char *path" "const char *arg" ... NULL 48.Ft int 49.Fn execlp "const char *file" "const char *arg" ... NULL 50.Ft int 51.Fn execle "const char *path" "const char *arg" ... NULL "char *const envp[]" 52.Ft int 53.Fn exect "const char *path" "char *const argv[]" "char *const envp[]" 54.Ft int 55.Fn execv "const char *path" "char *const argv[]" 56.Ft int 57.Fn execvp "const char *file" "char *const argv[]" 58.Ft int 59.Fn execvpe "const char *file" "char *const argv[]" "char *const envp[]" 60.Ft int 61.Fn execvP "const char *file" "const char *search_path" "char *const argv[]" 62.Sh DESCRIPTION 63The 64.Nm exec 65family of functions replaces the current process image with a 66new process image. 67The functions described in this manual page are front-ends for the function 68.Xr execve 2 . 69(See the manual page for 70.Xr execve 2 71for detailed information about the replacement of the current process.) 72.Pp 73The initial argument for these functions is the pathname of a file which 74is to be executed. 75.Pp 76The 77.Fa "const char *arg" 78and subsequent ellipses in the 79.Fn execl , 80.Fn execlp , 81and 82.Fn execle 83functions can be thought of as 84.Em arg0 , 85.Em arg1 , 86\&..., 87.Em argn . 88Together they describe a list of one or more pointers to null-terminated 89strings that represent the argument list available to the executed program. 90The first argument, by convention, should point to the file name associated 91with the file being executed. 92The list of arguments 93.Em must 94be terminated by a 95.Dv NULL 96pointer. 97.Pp 98The 99.Fn exect , 100.Fn execv , 101.Fn execvp , 102.Fn execvpe , 103and 104.Fn execvP 105functions provide an array of pointers to null-terminated strings that 106represent the argument list available to the new program. 107The first argument, by convention, should point to the file name associated 108with the file being executed. 109The array of pointers 110.Sy must 111be terminated by a 112.Dv NULL 113pointer. 114.Pp 115The 116.Fn execle , 117.Fn exect , 118and 119.Fn execvpe 120functions also specify the environment of the executed process by following 121the 122.Dv NULL 123pointer that terminates the list of arguments in the argument list 124or the pointer to the argv array with an additional argument. 125This additional argument is an array of pointers to null-terminated strings 126and 127.Em must 128be terminated by a 129.Dv NULL 130pointer. 131The other functions take the environment for the new process image from the 132external variable 133.Va environ 134in the current process. 135.Pp 136Some of these functions have special semantics. 137.Pp 138The functions 139.Fn execlp , 140.Fn execvp , 141.Fn execvpe , 142and 143.Fn execvP 144will duplicate the actions of the shell in searching for an executable file 145if the specified file name does not contain a slash 146.Dq Li / 147character. 148For 149.Fn execlp 150and 151.Fn execvp , 152.Fn execvpe , 153search path is the path specified in the environment by 154.Dq Ev PATH 155variable. 156If this variable is not specified, 157the default path is set according to the 158.Dv _PATH_DEFPATH 159definition in 160.In paths.h , 161which is set to 162.Dq Ev /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin . 163For 164.Fn execvP , 165the search path is specified as an argument to the function. 166In addition, certain errors are treated specially. 167.Pp 168If an error is ambiguous (for simplicity, we shall consider all 169errors except 170.Er ENOEXEC 171as being ambiguous here, although only the critical error 172.Er EACCES 173is really ambiguous), 174then these functions will act as if they stat the file to determine 175whether the file exists and has suitable execute permissions. 176If it does, they will return immediately with the global variable 177.Va errno 178restored to the value set by 179.Fn execve . 180Otherwise, the search will be continued. 181If the search completes without performing a successful 182.Fn execve 183or terminating due to an error, 184these functions will return with the global variable 185.Va errno 186set to 187.Er EACCES 188or 189.Er ENOENT 190according to whether at least one file with suitable execute permissions 191was found. 192.Pp 193If the header of a file is not recognized (the attempted 194.Fn execve 195returned 196.Er ENOEXEC ) , 197these functions will execute the shell with the path of 198the file as its first argument. 199(If this attempt fails, no further searching is done.) 200.Pp 201The function 202.Fn exect 203executes a file with the program tracing facilities enabled (see 204.Xr ptrace 2 ) . 205.Sh RETURN VALUES 206If any of the 207.Fn exec 208functions returns, an error will have occurred. 209The return value is \-1, and the global variable 210.Va errno 211will be set to indicate the error. 212.Sh FILES 213.Bl -tag -width /bin/sh -compact 214.It Pa /bin/sh 215The shell. 216.El 217.Sh COMPATIBILITY 218Historically, the default path for the 219.Fn execlp 220and 221.Fn execvp 222functions was 223.Dq Pa :/bin:/usr/bin . 224This was changed to remove the current directory to enhance system 225security. 226.Pp 227The behavior of 228.Fn execlp 229and 230.Fn execvp 231when errors occur while attempting to execute the file is not quite historic 232practice, and has not traditionally been documented and is not specified 233by the 234.Tn POSIX 235standard. 236.Pp 237Traditionally, the functions 238.Fn execlp 239and 240.Fn execvp 241ignored all errors except for the ones described above and 242.Er ETXTBSY , 243upon which they retried after sleeping for several seconds, and 244.Er ENOMEM 245and 246.Er E2BIG , 247upon which they returned. 248They now return for 249.Er ETXTBSY , 250and determine existence and executability more carefully. 251In particular, 252.Er EACCES 253for inaccessible directories in the path prefix is no longer 254confused with 255.Er EACCES 256for files with unsuitable execute permissions. 257In 258.Bx 4.4 , 259they returned upon all errors except 260.Er EACCES , 261.Er ENOENT , 262.Er ENOEXEC 263and 264.Er ETXTBSY . 265This was inferior to the traditional error handling, 266since it breaks the ignoring of errors for path prefixes 267and only improves the handling of the unusual ambiguous error 268.Er EFAULT 269and the unusual error 270.Er EIO . 271The behaviour was changed to match the behaviour of 272.Xr sh 1 . 273.Sh ERRORS 274The 275.Fn execl , 276.Fn execle , 277.Fn execlp , 278.Fn execvp , 279.Fn execvpe , 280and 281.Fn execvP 282functions 283may fail and set 284.Va errno 285for any of the errors specified for the library functions 286.Xr execve 2 287and 288.Xr malloc 3 . 289.Pp 290The 291.Fn exect 292and 293.Fn execv 294functions 295may fail and set 296.Va errno 297for any of the errors specified for the library function 298.Xr execve 2 . 299.Sh SEE ALSO 300.Xr sh 1 , 301.Xr execve 2 , 302.Xr fork 2 , 303.Xr ktrace 2 , 304.Xr ptrace 2 , 305.Xr environ 7 306.Sh STANDARDS 307The 308.Fn execl , 309.Fn execv , 310.Fn execle , 311.Fn execlp 312and 313.Fn execvp 314functions 315conform to 316.St -p1003.1-88 . 317The 318.Fn execvpe 319function is a GNU extension. 320.Sh HISTORY 321The 322.Fn exec 323function appeared in 324.At v1 . 325The 326.Fn execl 327and 328.Fn execv 329functions appeared in 330.At v2 . 331The 332.Fn execlp , 333.Fn execle , 334.Fn execve , 335and 336.Fn execvp 337functions appeared in 338.At v7 . 339The 340.Fn execvP 341function first appeared in 342.Fx 5.2 . 343The 344.Fn execvpe 345function first appeared in 346.Fx 14.1 . 347.Sh BUGS 348The type of the 349.Fa argv 350and 351.Fa envp 352parameters to 353.Fn execle , 354.Fn exect , 355.Fn execv , 356.Fn execvp , 357.Fn execvpe , 358and 359.Fn execvP 360is a historical accident and no sane implementation should modify the provided 361strings. 362The bogus parameter types trigger false positives from 363.Li const 364correctness analyzers. 365On 366.Fx , 367the 368.Fn __DECONST 369macro may be used to work around this limitation. 370.Pp 371Due to a fluke of the C standard, on platforms other than 372.Fx 373the definition of 374.Dv NULL 375may be the untyped number zero, rather than a 376.Ad (void *)0 377expression. 378To distinguish the concepts, they are referred to as a 379.Dq null pointer constant 380and a 381.Dq null pointer , 382respectively. 383On exotic computer architectures that 384.Fx 385does not support, the null pointer constant and null pointer may have a 386different representation. 387In general, where this document and others reference a 388.Dv NULL 389value, they actually imply a null pointer. 390E.g., for portability to non-FreeBSD operating systems on exotic computer 391architectures, one may use 392.Li (char *)NULL 393in place of 394.Dv NULL 395when invoking 396.Fn execl , 397.Fn execle , 398and 399.Fn execlp . 400