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