18269e767SBrooks Davis.\" Copyright (c) 1980, 1991, 1993 28269e767SBrooks Davis.\" The Regents of the University of California. All rights reserved. 38269e767SBrooks Davis.\" 48269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without 58269e767SBrooks Davis.\" modification, are permitted provided that the following conditions 68269e767SBrooks Davis.\" are met: 78269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright 88269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer. 98269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright 108269e767SBrooks Davis.\" notice, this list of conditions and the following disclaimer in the 118269e767SBrooks Davis.\" documentation and/or other materials provided with the distribution. 128269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors 138269e767SBrooks Davis.\" may be used to endorse or promote products derived from this software 148269e767SBrooks Davis.\" without specific prior written permission. 158269e767SBrooks Davis.\" 168269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 178269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 188269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 198269e767SBrooks Davis.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 208269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 218269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 228269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 238269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 248269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 258269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 268269e767SBrooks Davis.\" SUCH DAMAGE. 278269e767SBrooks Davis.\" 288269e767SBrooks Davis.Dd January 26, 2022 298269e767SBrooks Davis.Dt EXECVE 2 308269e767SBrooks Davis.Os 318269e767SBrooks Davis.Sh NAME 328269e767SBrooks Davis.Nm execve , 338269e767SBrooks Davis.Nm fexecve 348269e767SBrooks Davis.Nd execute a file 358269e767SBrooks Davis.Sh LIBRARY 368269e767SBrooks Davis.Lb libc 378269e767SBrooks Davis.Sh SYNOPSIS 388269e767SBrooks Davis.In unistd.h 398269e767SBrooks Davis.Ft int 408269e767SBrooks Davis.Fn execve "const char *path" "char *const argv[]" "char *const envp[]" 418269e767SBrooks Davis.Ft int 428269e767SBrooks Davis.Fn fexecve "int fd" "char *const argv[]" "char *const envp[]" 438269e767SBrooks Davis.Sh DESCRIPTION 448269e767SBrooks DavisThe 458269e767SBrooks Davis.Fn execve 468269e767SBrooks Davissystem call 478269e767SBrooks Davistransforms the calling process into a new process. 488269e767SBrooks DavisThe new process is constructed from an ordinary file, 498269e767SBrooks Daviswhose name is pointed to by 508269e767SBrooks Davis.Fa path , 518269e767SBrooks Daviscalled the 528269e767SBrooks Davis.Em new process file . 538269e767SBrooks DavisThe 548269e767SBrooks Davis.Fn fexecve 558269e767SBrooks Davissystem call is equivalent to 568269e767SBrooks Davis.Fn execve 578269e767SBrooks Davisexcept that the file to be executed is determined by the file 588269e767SBrooks Davisdescriptor 598269e767SBrooks Davis.Fa fd 608269e767SBrooks Davisinstead of a 618269e767SBrooks Davis.Fa path . 628269e767SBrooks DavisThis file is either an executable object file, 638269e767SBrooks Davisor a file of data for an interpreter. 648269e767SBrooks DavisAn executable object file consists of an identifying header, 658269e767SBrooks Davisfollowed by pages of data representing the initial program (text) 668269e767SBrooks Davisand initialized data pages. 678269e767SBrooks DavisAdditional pages may be specified 688269e767SBrooks Davisby the header to be initialized with zero data; see 698269e767SBrooks Davis.Xr elf 5 708269e767SBrooks Davisand 718269e767SBrooks Davis.Xr a.out 5 . 728269e767SBrooks Davis.Pp 738269e767SBrooks DavisAn interpreter file begins with a line of the form: 748269e767SBrooks Davis.Pp 758269e767SBrooks Davis.Bd -ragged -offset indent -compact 768269e767SBrooks Davis.Sy \&#! 778269e767SBrooks Davis.Em interpreter 788269e767SBrooks Davis.Bq Em arg 798269e767SBrooks Davis.Ed 808269e767SBrooks Davis.Pp 818269e767SBrooks DavisWhen an interpreter file is 828269e767SBrooks Davis.Sy execve Ap d , 838269e767SBrooks Davisthe system actually 848269e767SBrooks Davis.Sy execve Ap s 858269e767SBrooks Davisthe specified 868269e767SBrooks Davis.Em interpreter . 878269e767SBrooks DavisIf the optional 888269e767SBrooks Davis.Em arg 898269e767SBrooks Davisis specified, it becomes the first argument to the 908269e767SBrooks Davis.Em interpreter , 918269e767SBrooks Davisand the name of the originally 928269e767SBrooks Davis.Sy execve Ap d 938269e767SBrooks Davisfile becomes the second argument; 948269e767SBrooks Davisotherwise, the name of the originally 958269e767SBrooks Davis.Sy execve Ap d 968269e767SBrooks Davisfile becomes the first argument. 978269e767SBrooks DavisThe original arguments are shifted over to 988269e767SBrooks Davisbecome the subsequent arguments. 998269e767SBrooks DavisThe zeroth argument is set to the specified 1008269e767SBrooks Davis.Em interpreter . 1018269e767SBrooks Davis.Pp 1028269e767SBrooks DavisThe argument 1038269e767SBrooks Davis.Fa argv 1048269e767SBrooks Davisis a pointer to a null-terminated array of 1058269e767SBrooks Davischaracter pointers to null-terminated character strings. 1068269e767SBrooks DavisThese strings construct the argument list to be made available to the new 1078269e767SBrooks Davisprocess. 1088269e767SBrooks DavisAt least one argument must be present in 1098269e767SBrooks Davisthe array; by custom, the first element should be 1108269e767SBrooks Davisthe name of the executed program (for example, the last component of 1118269e767SBrooks Davis.Fa path ) . 1128269e767SBrooks Davis.Pp 1138269e767SBrooks DavisThe argument 1148269e767SBrooks Davis.Fa envp 1158269e767SBrooks Davisis also a pointer to a null-terminated array of 1168269e767SBrooks Davischaracter pointers to null-terminated strings. 1178269e767SBrooks DavisA pointer to this array is normally stored in the global variable 1188269e767SBrooks Davis.Va environ . 1198269e767SBrooks DavisThese strings pass information to the 1208269e767SBrooks Davisnew process that is not directly an argument to the command (see 1218269e767SBrooks Davis.Xr environ 7 ) . 1228269e767SBrooks Davis.Pp 1238269e767SBrooks DavisFile descriptors open in the calling process image remain open in 1248269e767SBrooks Davisthe new process image, except for those for which the close-on-exec 1258269e767SBrooks Davisflag is set (see 1268269e767SBrooks Davis.Xr close 2 1278269e767SBrooks Davisand 1288269e767SBrooks Davis.Xr fcntl 2 ) . 1298269e767SBrooks DavisDescriptors that remain open are unaffected by 1308269e767SBrooks Davis.Fn execve . 1318269e767SBrooks DavisIf any of the standard descriptors (0, 1, and/or 2) are closed at the 1328269e767SBrooks Davistime 1338269e767SBrooks Davis.Fn execve 1348269e767SBrooks Davisis called, and the process will gain privilege as a result of set-id 1358269e767SBrooks Davissemantics, those descriptors will be re-opened automatically. 1368269e767SBrooks DavisNo programs, whether privileged or not, should assume that these descriptors 1378269e767SBrooks Daviswill remain closed across a call to 1388269e767SBrooks Davis.Fn execve . 1398269e767SBrooks Davis.Pp 1408269e767SBrooks DavisSignals set to be ignored in the calling process are set to be ignored in 1418269e767SBrooks Davisthe 1428269e767SBrooks Davisnew process. 1438269e767SBrooks DavisSignals which are set to be caught in the calling process image 1448269e767SBrooks Davisare set to default action in the new process image. 1458269e767SBrooks DavisBlocked signals remain blocked regardless of changes to the signal action. 1468269e767SBrooks DavisThe signal stack is reset to be undefined (see 1478269e767SBrooks Davis.Xr sigaction 2 1488269e767SBrooks Davisfor more information). 1498269e767SBrooks Davis.Pp 1508269e767SBrooks DavisIf the set-user-ID mode bit of the new process image file is set 1518269e767SBrooks Davis(see 1528269e767SBrooks Davis.Xr chmod 2 ) , 1538269e767SBrooks Davisthe effective user ID of the new process image is set to the owner ID 1548269e767SBrooks Davisof the new process image file. 1558269e767SBrooks DavisIf the set-group-ID mode bit of the new process image file is set, 1568269e767SBrooks Davisthe effective group ID of the new process image is set to the group ID 1578269e767SBrooks Davisof the new process image file. 1588269e767SBrooks Davis(The effective group ID is the first element of the group list.) 1598269e767SBrooks DavisThe real user ID, real group ID and 1608269e767SBrooks Davisother group IDs of the new process image remain the same as the calling 1618269e767SBrooks Davisprocess image. 1628269e767SBrooks DavisAfter any set-user-ID and set-group-ID processing, 1638269e767SBrooks Davisthe effective user ID is recorded as the saved set-user-ID, 1648269e767SBrooks Davisand the effective group ID is recorded as the saved set-group-ID. 1658269e767SBrooks DavisThese values may be used in changing the effective IDs later (see 1668269e767SBrooks Davis.Xr setuid 2 ) . 1678269e767SBrooks Davis.Pp 1688269e767SBrooks DavisThe set-ID bits are not honored if the respective file system has the 1698269e767SBrooks Davis.Cm nosuid 1708269e767SBrooks Davisoption enabled or if the new process file is an interpreter file. 1718269e767SBrooks DavisSyscall 1728269e767SBrooks Davistracing is disabled if effective IDs are changed. 1738269e767SBrooks Davis.Pp 1748269e767SBrooks DavisThe new process also inherits the following attributes from 1758269e767SBrooks Davisthe calling process: 1768269e767SBrooks Davis.Pp 1778269e767SBrooks Davis.Bl -column parent_process_ID -offset indent -compact 1788269e767SBrooks Davis.It process ID Ta see Xr getpid 2 1798269e767SBrooks Davis.It parent process ID Ta see Xr getppid 2 1808269e767SBrooks Davis.It process group ID Ta see Xr getpgrp 2 1818269e767SBrooks Davis.It access groups Ta see Xr getgroups 2 1828269e767SBrooks Davis.It working directory Ta see Xr chdir 2 1838269e767SBrooks Davis.It root directory Ta see Xr chroot 2 1848269e767SBrooks Davis.It control terminal Ta see Xr termios 4 1858269e767SBrooks Davis.It resource usages Ta see Xr getrusage 2 1868269e767SBrooks Davis.It interval timers Ta see Xr getitimer 2 1878269e767SBrooks Davis.It resource limits Ta see Xr getrlimit 2 1888269e767SBrooks Davis.It file mode mask Ta see Xr umask 2 1898269e767SBrooks Davis.It signal mask Ta see Xr sigaction 2 , 1908269e767SBrooks Davis.Xr sigprocmask 2 1918269e767SBrooks Davis.El 1928269e767SBrooks Davis.Pp 1938269e767SBrooks DavisWhen a program is executed as a result of an 1948269e767SBrooks Davis.Fn execve 1958269e767SBrooks Davissystem call, it is entered as follows: 1968269e767SBrooks Davis.Bd -literal -offset indent 1978269e767SBrooks Davismain(argc, argv, envp) 1988269e767SBrooks Davisint argc; 1998269e767SBrooks Davischar **argv, **envp; 2008269e767SBrooks Davis.Ed 2018269e767SBrooks Davis.Pp 2028269e767SBrooks Daviswhere 2038269e767SBrooks Davis.Fa argc 2048269e767SBrooks Davisis the number of elements in 2058269e767SBrooks Davis.Fa argv 2068269e767SBrooks Davis(the ``arg count'') 2078269e767SBrooks Davisand 2088269e767SBrooks Davis.Fa argv 2098269e767SBrooks Davispoints to the array of character pointers 2108269e767SBrooks Davisto the arguments themselves. 2118269e767SBrooks Davis.Pp 2128269e767SBrooks DavisThe 2138269e767SBrooks Davis.Fn fexecve 2148269e767SBrooks Davisignores the file offset of 2158269e767SBrooks Davis.Fa fd . 2168269e767SBrooks DavisSince execute permission is checked by 2178269e767SBrooks Davis.Fn fexecve , 2188269e767SBrooks Davisthe file descriptor 2198269e767SBrooks Davis.Fa fd 2208269e767SBrooks Davisneed not have been opened with the 2218269e767SBrooks Davis.Dv O_EXEC 2228269e767SBrooks Davisflag. 2238269e767SBrooks DavisHowever, if the file to be executed denies read permission for the process 2248269e767SBrooks Davispreparing to do the exec, the only way to provide the 2258269e767SBrooks Davis.Fa fd 2268269e767SBrooks Davisto 2278269e767SBrooks Davis.Fn fexecve 2288269e767SBrooks Davisis to use the 2298269e767SBrooks Davis.Dv O_EXEC 2308269e767SBrooks Davisflag when opening 2318269e767SBrooks Davis.Fa fd . 2328269e767SBrooks DavisNote that the file to be executed can not be open for writing. 2338269e767SBrooks Davis.Sh RETURN VALUES 2348269e767SBrooks DavisAs the 2358269e767SBrooks Davis.Fn execve 2368269e767SBrooks Davissystem call overlays the current process image 2378269e767SBrooks Daviswith a new process image the successful call 2388269e767SBrooks Davishas no process to return to. 2398269e767SBrooks DavisIf 2408269e767SBrooks Davis.Fn execve 2418269e767SBrooks Davisdoes return to the calling process an error has occurred; the 2428269e767SBrooks Davisreturn value will be -1 and the global variable 2438269e767SBrooks Davis.Va errno 2448269e767SBrooks Davisis set to indicate the error. 2458269e767SBrooks Davis.Sh ERRORS 2468269e767SBrooks DavisThe 2478269e767SBrooks Davis.Fn execve 2488269e767SBrooks Davissystem call 2498269e767SBrooks Daviswill fail and return to the calling process if: 2508269e767SBrooks Davis.Bl -tag -width Er 2518269e767SBrooks Davis.It Bq Er ENOTDIR 2528269e767SBrooks DavisA component of the path prefix is not a directory. 2538269e767SBrooks Davis.It Bq Er ENAMETOOLONG 2548269e767SBrooks DavisA component of a pathname exceeded 255 characters, 2558269e767SBrooks Davisor an entire path name exceeded 1023 characters. 2568269e767SBrooks Davis.It Bq Er ENOEXEC 2578269e767SBrooks DavisWhen invoking an interpreted script, the length of the first line, 2588269e767SBrooks Davisinclusive of the 2598269e767SBrooks Davis.Sy \&#! 2608269e767SBrooks Davisprefix and terminating newline, exceeds 2618269e767SBrooks Davis.Dv MAXSHELLCMDLEN 2628269e767SBrooks Davischaracters. 2638269e767SBrooks Davis.It Bq Er ENOENT 2648269e767SBrooks DavisThe new process file does not exist. 2658269e767SBrooks Davis.It Bq Er ELOOP 2668269e767SBrooks DavisToo many symbolic links were encountered in translating the pathname. 2678269e767SBrooks Davis.It Bq Er EACCES 2688269e767SBrooks DavisSearch permission is denied for a component of the path prefix. 2698269e767SBrooks Davis.It Bq Er EACCES 2708269e767SBrooks DavisThe new process file is not an ordinary file. 2718269e767SBrooks Davis.It Bq Er EACCES 2728269e767SBrooks DavisThe new process file mode denies execute permission. 2738269e767SBrooks Davis.It Bq Er EINVAL 2748269e767SBrooks Davis.Fa argv 2758269e767SBrooks Davisdid not contain at least one element. 2768269e767SBrooks Davis.It Bq Er ENOEXEC 2778269e767SBrooks DavisThe new process file has the appropriate access 2788269e767SBrooks Davispermission, but has an invalid magic number in its header. 2798269e767SBrooks Davis.It Bq Er ETXTBSY 2808269e767SBrooks DavisThe new process file is a pure procedure (shared text) 2818269e767SBrooks Davisfile that is currently open for writing by some process. 2828269e767SBrooks Davis.It Bq Er ENOMEM 2838269e767SBrooks DavisThe new process requires more virtual memory than 2848269e767SBrooks Davisis allowed by the imposed maximum 2858269e767SBrooks Davis.Pq Xr getrlimit 2 . 2868269e767SBrooks Davis.It Bq Er E2BIG 2878269e767SBrooks DavisThe number of bytes in the new process' argument list 2888269e767SBrooks Davisis larger than the system-imposed limit. 2898269e767SBrooks DavisThis limit is specified by the 2908269e767SBrooks Davis.Xr sysctl 3 2918269e767SBrooks DavisMIB variable 2928269e767SBrooks Davis.Dv KERN_ARGMAX . 2938269e767SBrooks Davis.It Bq Er EFAULT 2948269e767SBrooks DavisThe new process file is not as long as indicated by 2958269e767SBrooks Davisthe size values in its header. 2968269e767SBrooks Davis.It Bq Er EFAULT 2978269e767SBrooks DavisThe 2988269e767SBrooks Davis.Fa path , 2998269e767SBrooks Davis.Fa argv , 3008269e767SBrooks Davisor 3018269e767SBrooks Davis.Fa envp 3028269e767SBrooks Davisarguments 3038269e767SBrooks Davispoint 3048269e767SBrooks Davisto an illegal address. 3058269e767SBrooks Davis.It Bq Er EIO 3068269e767SBrooks DavisAn I/O error occurred while reading from the file system. 3078269e767SBrooks Davis.It Bq Er EINTEGRITY 3088269e767SBrooks DavisCorrupted data was detected while reading from the file system. 3098269e767SBrooks Davis.El 3108269e767SBrooks Davis.Pp 3118269e767SBrooks DavisIn addition, the 3128269e767SBrooks Davis.Fn fexecve 3138269e767SBrooks Daviswill fail and return to the calling process if: 3148269e767SBrooks Davis.Bl -tag -width Er 3158269e767SBrooks Davis.It Bq Er EBADF 3168269e767SBrooks DavisThe 3178269e767SBrooks Davis.Fa fd 3188269e767SBrooks Davisargument is not a valid file descriptor open for executing. 3198269e767SBrooks Davis.El 3208269e767SBrooks Davis.Sh SEE ALSO 3218269e767SBrooks Davis.Xr ktrace 1 , 3228269e767SBrooks Davis.Xr _exit 2 , 3238269e767SBrooks Davis.Xr fork 2 , 3248269e767SBrooks Davis.Xr open 2 , 3258269e767SBrooks Davis.Xr execl 3 , 3268269e767SBrooks Davis.Xr exit 3 , 3278269e767SBrooks Davis.Xr sysctl 3 , 328*1a720cbeSAlexander Ziaee.Xr fdescfs 4 , 3298269e767SBrooks Davis.Xr a.out 5 , 3308269e767SBrooks Davis.Xr elf 5 , 3318269e767SBrooks Davis.Xr environ 7 , 3328269e767SBrooks Davis.Xr mount 8 3338269e767SBrooks Davis.Sh STANDARDS 3348269e767SBrooks DavisThe 3358269e767SBrooks Davis.Fn execve 3368269e767SBrooks Davissystem call conforms to 3378269e767SBrooks Davis.St -p1003.1-2001 , 3388269e767SBrooks Daviswith the exception of reopening descriptors 0, 1, and/or 2 in certain 3398269e767SBrooks Daviscircumstances. 3408269e767SBrooks DavisA future update of the Standard is expected to require this behavior, 3418269e767SBrooks Davisand it may become the default for non-privileged processes as well. 3428269e767SBrooks Davis.\" NB: update this caveat when TC1 is blessed. 3438269e767SBrooks DavisThe support for executing interpreted programs is an extension. 3448269e767SBrooks DavisThe 3458269e767SBrooks Davis.Fn fexecve 3468269e767SBrooks Davissystem call conforms to The Open Group Extended API Set 2 specification. 3478269e767SBrooks Davis.Sh HISTORY 3488269e767SBrooks DavisThe 3498269e767SBrooks Davis.Fn execve 3508269e767SBrooks Davissystem call appeared in 3518269e767SBrooks Davis.At v7 . 3528269e767SBrooks DavisThe 3538269e767SBrooks Davis.Fn fexecve 3548269e767SBrooks Davissystem call appeared in 3558269e767SBrooks Davis.Fx 8.0 . 3568269e767SBrooks Davis.Sh CAVEATS 3578269e767SBrooks DavisIf a program is 3588269e767SBrooks Davis.Em setuid 3598269e767SBrooks Davisto a non-super-user, but is executed when 3608269e767SBrooks Davisthe real 3618269e767SBrooks Davis.Em uid 3628269e767SBrooks Davisis ``root'', then the program has some of the powers 3638269e767SBrooks Davisof a super-user as well. 3648269e767SBrooks Davis.Pp 3658269e767SBrooks DavisWhen executing an interpreted program through 3668269e767SBrooks Davis.Fn fexecve , 3678269e767SBrooks Daviskernel supplies 3688269e767SBrooks Davis.Pa /dev/fd/n 3698269e767SBrooks Davisas a second argument to the interpreter, 3708269e767SBrooks Daviswhere 3718269e767SBrooks Davis.Ar n 3728269e767SBrooks Davisis the file descriptor passed in the 3738269e767SBrooks Davis.Fa fd 3748269e767SBrooks Davisargument to 3758269e767SBrooks Davis.Fn fexecve . 3768269e767SBrooks DavisFor this construction to work correctly, the 377*1a720cbeSAlexander Ziaee.Xr fdescfs 4 3788269e767SBrooks Davisfilesystem shall be mounted on 3798269e767SBrooks Davis.Pa /dev/fd . 380