1.\" Copyright (c) 1980, 1991 Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" %sccs.include.redist.man% 5.\" 6.\" @(#)execve.2 6.9 (Berkeley) 03/10/91 7.\" 8.Dd 9.Dt EXECVE 2 10.Os BSD 4 11.Sh NAME 12.Nm execve 13.Nd execute a file 14.Sh SYNOPSIS 15.Fd #include <unistd.h> 16.Ft int 17.Fn execve "const char *path" "const * char *argv" "const * char *envp" 18.Sh DESCRIPTION 19.Fn Execve 20transforms the calling process into a new process. 21The new process is constructed from an ordinary file, 22whose name is pointed to by 23.Fa path , 24called the 25.Em new process file . 26This file is either an executable object file, 27or a file of data for an interpreter. 28An executable object file consists of an identifying header, 29followed by pages of data representing the initial program (text) 30and initialized data pages. Additional pages may be specified 31by the header to be initialized with zero data; see 32.Xr a.out 5 . 33.Pp 34An interpreter file begins with a line of the form: 35.Pp 36.Bd -filled -offset indent -compact 37.Sy \&#! 38.Em interpreter 39.Bq Em arg 40.Ed 41.Pp 42When an interpreter file is 43.Fn execve Ap d , 44the system 45.Fn execve Ap s 46the specified 47.Em interpreter . 48If the optional 49.Em arg 50is specified, it becomes the first argument to the 51.Em interpreter , 52and the name of the originally 53.Fn execve Ap d 54file becomes the second argument; 55otherwise, the name of the originally 56.Fn execve Ap d 57file becomes the first argument. The original arguments are shifted over to 58become the subsequent arguments. The zeroth argument, normally the name of the 59.Fn execve Ap d 60file, is left unchanged. 61.Pp 62The argument 63.Fa argv 64is a pointer to a null-terminated array of 65character pointers to null-terminated character strings. 66These strings construct the argument list to be made available to the new 67process. At least one argument must be present in 68the array; by custom, the first element should be 69the name of the executed program (for example, the last component of 70.Fa path ) . 71.Pp 72The argument 73.Fa envp 74is also a pointer to a null-terminated array of 75character pointers to null-terminated strings. 76A pointer to this array is normally stored in the global variable 77.Va environ. 78These strings pass information to the 79new process that is not directly an argument to the command (see 80.Xr environ 7 ) . 81.Pp 82File descriptors open in the calling process image remain open in 83the new process image, except for those for which the close-on-exec 84flag is set (see 85.Xr close 2 86and 87.Xr fcntl 2 ) . 88Descriptors that remain open are unaffected by 89.Fn execve . 90.Pp 91Signals set to be ignored in the calling process are set to be ignored in 92the 93new process. Signals which are set to be caught in the calling process image 94are set to default action in the new process image. 95Blocked signals remain blocked regardless of changes to the signal action. 96The signal stack is reset to be undefined (see 97.Xr sigaction 2 98for more information). 99.Pp 100If the set-user-ID mode bit of the new process image file is set 101(see 102.Xr chmod 2 ) , 103the effective user ID of the new process image is set to the owner ID 104of the new process image file. 105If the set-group-ID mode bit of the new process image file is set, 106the effective group ID of the new process image is set to the group ID 107of the new process image file. 108The real user ID, real group ID and 109supplementary group IDs of the new process image remain the same as the calling 110process image. 111.Pp 112The new process also inherits the following attributes from 113the calling process: 114.Pp 115.Bl -column parent_process_ID -offset indent -compact 116.It process ID Ta see Xr getpid 2 117.It parent process ID Ta see Xr getppid 2 118.It process group ID Ta see Xr getpgrp 2 119.It access groups Ta see Xr getgroups 2 120.It working directory Ta see Xr chdir 2 121.It root directory Ta see Xr chroot 2 122.It control terminal Ta see Xr termios 4 123.It resource usages Ta see Xr getrusage 2 124.It interval timers Ta see Xr getitimer 2 125.It resource limits Ta see Xr getrlimit 2 126.It file mode mask Ta see Xr umask 2 127.It signal mask Ta see Xr sigvec 2 , 128.Xr sigsetmask 2 129.El 130.Pp 131When a program is executed as a result of an 132.Fn execve 133call, it is entered as follows: 134.Bd -literal -offset indent 135main(argc, argv, envp) 136int argc; 137char **argv, **envp; 138.Ed 139.Pp 140where 141.Fa argc 142is the number of elements in 143.Fa argv 144(the ``arg count'') 145and 146.Fa argv 147points to the array of character pointers 148to the arguments themselves. 149.Sh RETURN VALUES 150As the 151.Fn execve 152function overlays the current process image 153with a new process image the successful call 154has no process to return to. 155If 156.Fn execve 157does return to the calling process an error has occurred; the 158return value will be -1 and the global variable 159.Va errno 160is set to indicate the error. 161.Sh ERRORS 162.Fn Execve 163will fail and return to the calling process if: 164.Bl -tag -width [ENAMETOOLONG] 165.It Bq Er ENOTDIR 166A component of the path prefix is not a directory. 167.It Bq Er EINVAL 168The pathname contains a character with the high-order bit set. 169.It Bq Er ENAMETOOLONG 170A component of a pathname exceeded 255 characters, 171or an entire path name exceeded 1023 characters. 172.It Bq Er ENOENT 173The new process file does not exist. 174.It Bq Er ELOOP 175Too many symbolic links were encountered in translating the pathname. 176.It Bq Er EACCES 177Search permission is denied for a component of the path prefix. 178.It Bq Er EACCES 179The new process file is not an ordinary file. 180.It Bq Er EACCES 181The new process file mode denies execute permission. 182.It Bq Er ENOEXEC 183The new process file has the appropriate access 184permission, but has an invalid magic number in its header. 185.It Bq Er ETXTBSY 186The new process file is a pure procedure (shared text) 187file that is currently open for writing or reading by some process. 188.It Bq Er ENOMEM 189The new process requires more virtual memory than 190is allowed by the imposed maximum 191.Pq Xr getrlimit 2 . 192.It Bq Er E2BIG 193The number of bytes in the new process's argument list 194is larger than the system-imposed limit. 195The limit in the system as released is 20480 bytes 196.Pf ( Dv NCARGS 197in 198.Ao Pa sys/param.h Ac . 199.It Bq Er EFAULT 200The new process file is not as long as indicated by 201the size values in its header. 202.It Bq Er EFAULT 203.Fa Path , 204.Fa argv , 205or 206.Fa envp 207point 208to an illegal address. 209.It Bq Er EIO 210An I/O error occurred while reading from the file system. 211.El 212.Sh CAVEAT 213If a program is 214.Em setuid 215to a non-super-user, but is executed when 216the real 217.Em uid 218is ``root'', then the program has some of the powers 219of a super-user as well. 220.Sh SEE ALSO 221.Xr exit 2 , 222.Xr fork 2 , 223.Xr execl 3 , 224.Xr environ 7 225.Sh HISTORY 226The 227.Nm 228function call appeared in 229.Bx 4.2 . 230