1.\" $OpenBSD: execve.2,v 1.34 2009/10/27 08:26:20 nicm Exp $ 2.\" $NetBSD: execve.2,v 1.9 1995/02/27 12:32:25 cgd Exp $ 3.\" 4.\" Copyright (c) 1980, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)execve.2 8.3 (Berkeley) 1/24/94 32.\" 33.Dd $Mdocdate: October 27 2009 $ 34.Dt EXECVE 2 35.Os 36.Sh NAME 37.Nm execve , 38.Nm exect 39.Nd execute a file 40.Sh SYNOPSIS 41.Fd #include <unistd.h> 42.Ft int 43.Fn execve "const char *path" "char *const argv[]" "char *const envp[]" 44.Ft int 45.Fn exect "const char *path" "char *const argv[]" "char *const envp[]" 46.Sh DESCRIPTION 47.Fn execve 48transforms the calling process into a new process. 49The new process is constructed from an ordinary file, 50whose name is pointed to by 51.Fa path , 52called the 53.Em new process file . 54This file is either an executable object file, 55or a file of data for an interpreter. 56An executable object file consists of an identifying header, 57followed by pages of data representing the initial program (text) 58and initialized data pages. 59Additional pages may be specified by the header to be initialized 60with zero data; see 61.Xr a.out 5 62and 63.Xr elf 5 . 64.Pp 65An interpreter file begins with a line of the form: 66.Bd -filled -offset indent 67.Sy #!\& 68.Em interpreter 69.Bq Em arg 70.Ed 71.Pp 72When an interpreter file is passed to 73.Fn execve 74the system instead calls 75.Fn execve 76with the specified 77.Em interpreter . 78If the optional 79.Em arg 80is specified, it becomes the first argument to the 81.Em interpreter , 82and the original 83.Ar path 84becomes the second argument; 85otherwise, 86.Ar path 87becomes the first argument. 88The original arguments are shifted over to become the subsequent arguments. 89The zeroth argument, normally the name of the file being executed, is left 90unchanged. 91.Pp 92The argument 93.Fa argv 94is a pointer to a null-terminated array of 95character pointers to NUL-terminated character strings. 96These strings construct the argument list to be made available to the new 97process. 98At least one argument must be present in the array; 99by custom, the first element should be 100the name of the executed program (for example, the last component of 101.Fa path ) . 102.Pp 103The argument 104.Fa envp 105is also a pointer to a null-terminated array of 106character pointers to NUL-terminated strings. 107A pointer to this array is normally stored in the global variable 108.Va environ . 109These strings pass information to the 110new process that is not directly an argument to the command (see 111.Xr environ 7 ) . 112.Pp 113File descriptors open in the calling process image remain open in 114the new process image, except for those for which the close-on-exec 115flag is set (see 116.Xr close 2 117and 118.Xr fcntl 2 ) . 119Descriptors that remain open are unaffected by 120.Fn execve . 121In the case of a new setuid or setgid executable being executed, if 122file descriptors 0, 1, or 2 (representing stdin, stdout, and stderr) 123are currently unallocated, these descriptors will be opened to point to 124some system file like 125.Pa /dev/null . 126The intent is to ensure these descriptors are not unallocated, since 127many libraries make assumptions about the use of these 3 file descriptors. 128.Pp 129Signals set to be ignored in the calling process are set to be ignored in 130the 131new process. 132Signals which are set to be caught in the calling process image 133are set to default action in the new process image. 134Blocked signals remain blocked regardless of changes to the signal action. 135The signal stack is reset to be undefined (see 136.Xr sigaction 2 137for more information). 138.Pp 139If the set-user-ID mode bit of the new process image file is set 140(see 141.Xr chmod 2 ) , 142the effective user ID of the new process image is set to the owner ID 143of the new process image file. 144If the set-group-ID mode bit of the new process image file is set, 145the effective group ID of the new process image is set to the group ID 146of the new process image file. 147(The effective group ID is the first element of the group list.) 148The real user ID, real group ID and 149other group IDs of the new process image remain the same as the calling 150process image. 151After any set-user-ID and set-group-ID processing, 152the effective user ID is recorded as the saved set-user-ID, 153and the effective group ID is recorded as the saved set-group-ID. 154These values may be used in changing the effective IDs later (see 155.Xr setuid 2 ) . 156The set-user-ID and set-group-ID bits have no effect if the 157new process image file is located on a file system mounted with 158the nosuid flag. 159The process will be started without the new permissions. 160.Pp 161The new process also inherits the following attributes from 162the calling process: 163.Pp 164.Bl -column parent_process_ID -offset indent -compact 165.It process ID Ta see Xr getpid 2 166.It parent process ID Ta see Xr getppid 2 167.It process group ID Ta see Xr getpgrp 2 168.It session ID Ta see Xr getsid 2 169.It access groups Ta see Xr getgroups 2 170.It working directory Ta see Xr chdir 2 171.It root directory Ta see Xr chroot 2 172.It control terminal Ta see Xr termios 4 173.It resource usages Ta see Xr getrusage 2 174.It interval timers Ta see Xr getitimer 2 175(unless process image file is setuid or setgid, 176in which case all timers are disabled) 177.It resource limits Ta see Xr getrlimit 2 178.It file mode mask Ta see Xr umask 2 179.It signal mask Ta see Xr sigaction 2 , 180.Xr sigsetmask 3 181.El 182.Pp 183When a program is executed as a result of an 184.Fn execve 185call, it is entered as follows: 186.Bd -literal -offset indent 187main(int argc, char **argv, char **envp) 188.Ed 189.Pp 190where 191.Fa argc 192is the number of elements in 193.Fa argv 194(the 195.Dq arg count ) 196and 197.Fa argv 198points to the array of character pointers 199to the arguments themselves. 200.Pp 201The 202.Fn exect 203function is equivalent to 204.Fn execve 205with the additional property that it executes the file with the program 206tracing facilities enabled (see 207.Xr ptrace 2 ) . 208.Sh RETURN VALUES 209As the 210.Fn execve 211function overlays the current process image 212with a new process image the successful call 213has no process to return to. 214If 215.Fn execve 216does return to the calling process an error has occurred; the 217return value will be \-1 and the global variable 218.Va errno 219is set to indicate the error. 220.Sh ERRORS 221.Fn execve 222will fail and return to the calling process if: 223.Bl -tag -width Er 224.It Bq Er ENOTDIR 225A component of the path prefix is not a directory. 226.It Bq Er ENAMETOOLONG 227A component of a pathname exceeded 228.Dv {NAME_MAX} 229characters, or an entire path name exceeded 230.Dv {PATH_MAX} 231characters. 232.It Bq Er ENOENT 233The new process file does not exist. 234.It Bq Er ELOOP 235Too many symbolic links were encountered in translating the pathname. 236.It Bq Er EACCES 237Search permission is denied for a component of the path prefix. 238.It Bq Er EACCES 239The new process file is not an ordinary file. 240.It Bq Er EACCES 241The new process file mode denies execute permission. 242.It Bq Er EACCES 243The new process file is on a filesystem mounted with execution 244disabled 245.Pf ( Dv MNT_NOEXEC 246in 247.Ao Pa sys/mount.h Ac ) . 248.It Bq Er ENOEXEC 249The new process file has the appropriate access 250permission, but has an invalid magic number in its header. 251.It Bq Er ETXTBSY 252The new process file is a pure procedure (shared text) 253file that is currently open for writing or reading by some process. 254.It Bq Er ENOMEM 255The new process requires more virtual memory than 256is allowed by the imposed maximum 257.Pq Xr getrlimit 2 . 258.It Bq Er E2BIG 259The number of bytes in the new process's argument list 260is larger than the system-imposed limit. 261The limit in the system as released is 262144 bytes 262.Pf ( Dv NCARGS 263in 264.Ao Pa sys/param.h Ac ) . 265.It Bq Er EFAULT 266The new process file is not as long as indicated by 267the size values in its header. 268.It Bq Er EFAULT 269.Fa path , 270.Fa argv , 271or 272.Fa envp 273point 274to an illegal address. 275.It Bq Er EIO 276An I/O error occurred while reading from the file system. 277.It Bq Er ENFILE 278During startup of an 279.Em interpreter , 280the system file table was found to be full. 281.El 282.Sh SEE ALSO 283.Xr _exit 2 , 284.Xr fork 2 , 285.Xr execl 3 , 286.Xr exit 3 , 287.Xr a.out 5 , 288.Xr elf 5 , 289.Xr environ 7 290.Sh STANDARDS 291The 292.Fn exect 293function should not be used in portable applications. 294.Sh HISTORY 295The 296.Fn execve 297function call first appeared in 298.At v7 299and 300.Bx 3 . 301.Sh CAVEATS 302If a program is 303.Em setuid 304to a non-superuser, but is executed when the real 305.Em uid 306is 307.Dq root , 308then the program has some of the powers of a superuser as well. 309