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