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 March 22, 2020 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 execvP 39.Nd execute a file 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In unistd.h 44.Vt extern char **environ ; 45.Ft int 46.Fn execl "const char *path" "const char *arg" ... NULL 47.Ft int 48.Fn execlp "const char *file" "const char *arg" ... NULL 49.Ft int 50.Fn execle "const char *path" "const char *arg" ... NULL "char *const envp[]" 51.Fc 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 execvP "const char *file" "const char *search_path" "char *const argv[]" 60.Sh DESCRIPTION 61The 62.Nm exec 63family of functions replaces the current process image with a 64new process image. 65The functions described in this manual page are front-ends for the function 66.Xr execve 2 . 67(See the manual page for 68.Xr execve 2 69for detailed information about the replacement of the current process.) 70.Pp 71The initial argument for these functions is the pathname of a file which 72is to be executed. 73.Pp 74The 75.Fa "const char *arg" 76and subsequent ellipses in the 77.Fn execl , 78.Fn execlp , 79and 80.Fn execle 81functions can be thought of as 82.Em arg0 , 83.Em arg1 , 84\&..., 85.Em argn . 86Together they describe a list of one or more pointers to null-terminated 87strings that represent the argument list available to the executed program. 88The first argument, by convention, should point to the file name associated 89with the file being executed. 90The list of arguments 91.Em must 92be terminated by a 93.Dv NULL 94pointer. 95.Pp 96The 97.Fn exect , 98.Fn execv , 99.Fn execvp , 100and 101.Fn execvP 102functions provide an array of pointers to null-terminated strings that 103represent the argument list available to the new program. 104The first argument, by convention, should point to the file name associated 105with the file being executed. 106The array of pointers 107.Sy must 108be terminated by a 109.Dv NULL 110pointer. 111.Pp 112The 113.Fn execle 114and 115.Fn exect 116functions also specify the environment of the executed process by following 117the 118.Dv NULL 119pointer that terminates the list of arguments in the argument list 120or the pointer to the argv array with an additional argument. 121This additional argument is an array of pointers to null-terminated strings 122and 123.Em must 124be terminated by a 125.Dv NULL 126pointer. 127The other functions take the environment for the new process image from the 128external variable 129.Va environ 130in the current process. 131.Pp 132Some of these functions have special semantics. 133.Pp 134The functions 135.Fn execlp , 136.Fn execvp , 137and 138.Fn execvP 139will duplicate the actions of the shell in searching for an executable file 140if the specified file name does not contain a slash 141.Dq Li / 142character. 143For 144.Fn execlp 145and 146.Fn execvp , 147search path is the path specified in the environment by 148.Dq Ev PATH 149variable. 150If this variable is not specified, 151the default path is set according to the 152.Dv _PATH_DEFPATH 153definition in 154.In paths.h , 155which is set to 156.Dq Ev /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin . 157For 158.Fn execvP , 159the search path is specified as an argument to the function. 160In addition, certain errors are treated specially. 161.Pp 162If an error is ambiguous (for simplicity, we shall consider all 163errors except 164.Er ENOEXEC 165as being ambiguous here, although only the critical error 166.Er EACCES 167is really ambiguous), 168then these functions will act as if they stat the file to determine 169whether the file exists and has suitable execute permissions. 170If it does, they will return immediately with the global variable 171.Va errno 172restored to the value set by 173.Fn execve . 174Otherwise, the search will be continued. 175If the search completes without performing a successful 176.Fn execve 177or terminating due to an error, 178these functions will return with the global variable 179.Va errno 180set to 181.Er EACCES 182or 183.Er ENOENT 184according to whether at least one file with suitable execute permissions 185was found. 186.Pp 187If the header of a file is not recognized (the attempted 188.Fn execve 189returned 190.Er ENOEXEC ) , 191these functions will execute the shell with the path of 192the file as its first argument. 193(If this attempt fails, no further searching is done.) 194.Pp 195The function 196.Fn exect 197executes a file with the program tracing facilities enabled (see 198.Xr ptrace 2 ) . 199.Sh RETURN VALUES 200If any of the 201.Fn exec 202functions returns, an error will have occurred. 203The return value is \-1, and the global variable 204.Va errno 205will be set to indicate the error. 206.Sh FILES 207.Bl -tag -width /bin/sh -compact 208.It Pa /bin/sh 209The shell. 210.El 211.Sh COMPATIBILITY 212Historically, the default path for the 213.Fn execlp 214and 215.Fn execvp 216functions was 217.Dq Pa :/bin:/usr/bin . 218This was changed to remove the current directory to enhance system 219security. 220.Pp 221The behavior of 222.Fn execlp 223and 224.Fn execvp 225when errors occur while attempting to execute the file is not quite historic 226practice, and has not traditionally been documented and is not specified 227by the 228.Tn POSIX 229standard. 230.Pp 231Traditionally, the functions 232.Fn execlp 233and 234.Fn execvp 235ignored all errors except for the ones described above and 236.Er ETXTBSY , 237upon which they retried after sleeping for several seconds, and 238.Er ENOMEM 239and 240.Er E2BIG , 241upon which they returned. 242They now return for 243.Er ETXTBSY , 244and determine existence and executability more carefully. 245In particular, 246.Er EACCES 247for inaccessible directories in the path prefix is no longer 248confused with 249.Er EACCES 250for files with unsuitable execute permissions. 251In 252.Bx 4.4 , 253they returned upon all errors except 254.Er EACCES , 255.Er ENOENT , 256.Er ENOEXEC 257and 258.Er ETXTBSY . 259This was inferior to the traditional error handling, 260since it breaks the ignoring of errors for path prefixes 261and only improves the handling of the unusual ambiguous error 262.Er EFAULT 263and the unusual error 264.Er EIO . 265The behaviour was changed to match the behaviour of 266.Xr sh 1 . 267.Sh ERRORS 268The 269.Fn execl , 270.Fn execle , 271.Fn execlp , 272.Fn execvp 273and 274.Fn execvP 275functions 276may fail and set 277.Va errno 278for any of the errors specified for the library functions 279.Xr execve 2 280and 281.Xr malloc 3 . 282.Pp 283The 284.Fn exect 285and 286.Fn execv 287functions 288may fail and set 289.Va errno 290for any of the errors specified for the library function 291.Xr execve 2 . 292.Sh SEE ALSO 293.Xr sh 1 , 294.Xr execve 2 , 295.Xr fork 2 , 296.Xr ktrace 2 , 297.Xr ptrace 2 , 298.Xr environ 7 299.Sh STANDARDS 300The 301.Fn execl , 302.Fn execv , 303.Fn execle , 304.Fn execlp 305and 306.Fn execvp 307functions 308conform to 309.St -p1003.1-88 . 310.Sh HISTORY 311The 312.Fn exec 313function appeared in 314.At v1 . 315The 316.Fn execl 317and 318.Fn execv 319functions appeared in 320.At v2 . 321The 322.Fn execlp , 323.Fn execle , 324.Fn execve , 325and 326.Fn execvp 327functions appeared in 328.At v7 . 329The 330.Fn execvP 331function first appeared in 332.Fx 5.2 . 333.Sh BUGS 334The type of the 335.Fa argv 336and 337.Fa envp 338parameters to 339.Fn execle , 340.Fn exect , 341.Fn execv , 342.Fn execvp , 343and 344.Fn execvP 345is a historical accident and no sane implementation should modify the provided 346strings. 347The bogus parameter types trigger false positives from 348.Li const 349correctness analyzers. 350On 351.Fx , 352the 353.Fn __DECONST 354macro may be used to work around this limitation. 355.Pp 356Due to a fluke of the C standard, on platforms other than 357.Fx 358the definition of 359.Dv NULL 360may be the untyped number zero, rather than a 361.Ad (void *)0 362expression. 363To distinguish the concepts, they are referred to as a 364.Dq null pointer constant 365and a 366.Dq null pointer , 367respectively. 368On exotic computer architectures that 369.Fx 370does not support, the null pointer constant and null pointer may have a 371different representation. 372In general, where this document and others reference a 373.Dv NULL 374value, they actually imply a null pointer. 375E.g., for portability to non-FreeBSD operating systems on exotic computer 376architectures, one may use 377.Li (char *)NULL 378in place of 379.Dv NULL 380when invoking 381.Fn execl , 382.Fn execle , 383and 384.Fn execlp . 385