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