161181Sbostic.\" Copyright (c) 1980, 1991, 1993 261181Sbostic.\" The Regents of the University of California. All rights reserved. 320107Smckusick.\" 447208Scael.\" %sccs.include.redist.man% 520107Smckusick.\" 6*67351Sah.\" @(#)execve.2 8.5 (Berkeley) 06/01/94 747208Scael.\" 847208Scael.Dd 947208Scael.Dt EXECVE 2 1047208Scael.Os BSD 4 1147208Scael.Sh NAME 1247208Scael.Nm execve 1347208Scael.Nd execute a file 1447208Scael.Sh SYNOPSIS 1547208Scael.Fd #include <unistd.h> 1647208Scael.Ft int 1765872Sbostic.Fn execve "const char *path" "char *const argv[]" "char *const envp[]" 1847208Scael.Sh DESCRIPTION 1947208Scael.Fn Execve 2020108Smckusicktransforms the calling process into a new process. 2136256SbosticThe new process is constructed from an ordinary file, 2236256Sbosticwhose name is pointed to by 2347208Scael.Fa path , 2447208Scaelcalled the 2547208Scael.Em new process file . 2620108SmckusickThis file is either an executable object file, 2720108Smckusickor a file of data for an interpreter. 2820108SmckusickAn executable object file consists of an identifying header, 2920108Smckusickfollowed by pages of data representing the initial program (text) 3020108Smckusickand initialized data pages. Additional pages may be specified 3147208Scaelby the header to be initialized with zero data; see 3247208Scael.Xr a.out 5 . 3347208Scael.Pp 3447208ScaelAn interpreter file begins with a line of the form: 3547208Scael.Pp 3647208Scael.Bd -filled -offset indent -compact 3747208Scael.Sy \&#! 3847208Scael.Em interpreter 3947208Scael.Bq Em arg 4047208Scael.Ed 4147208Scael.Pp 4220108SmckusickWhen an interpreter file is 4367102Smckusick.\" was .Fn execve Ap d , 4467102Smckusick\fBexecve\fP'd, 4547208Scaelthe system 4667102Smckusick.\" was .Fn execve Ap s 4767102Smckusick\fBexecve\fP's 4865100Smckusickruns the specified 4947208Scael.Em interpreter . 5036256SbosticIf the optional 5147208Scael.Em arg 5236256Sbosticis specified, it becomes the first argument to the 5347208Scael.Em interpreter , 5436256Sbosticand the name of the originally 5567102Smckusick.\" was .Fn execve Ap d 5667102Smckusick\fBexecve\fP'd 5736256Sbosticfile becomes the second argument; 5836256Sbosticotherwise, the name of the originally 5967102Smckusick.\" was .Fn execve Ap d 6067102Smckusick\fBexecve\fP'd 6136256Sbosticfile becomes the first argument. The original arguments are shifted over to 6236256Sbosticbecome the subsequent arguments. The zeroth argument, normally the name of the 6367102Smckusick.\" was .Fn execve Ap d 6467102Smckusick\fBexecve\fP'd 6536256Sbosticfile, is left unchanged. 6647208Scael.Pp 6747208ScaelThe argument 6847208Scael.Fa argv 6947208Scaelis a pointer to a null-terminated array of 7036256Sbosticcharacter pointers to null-terminated character strings. 7147208ScaelThese strings construct the argument list to be made available to the new 7247208Scaelprocess. At least one argument must be present in 7347208Scaelthe array; by custom, the first element should be 7447208Scaelthe name of the executed program (for example, the last component of 7547208Scael.Fa path ) . 7647208Scael.Pp 7747208ScaelThe argument 7847208Scael.Fa envp 7947208Scaelis also a pointer to a null-terminated array of 8036256Sbosticcharacter pointers to null-terminated strings. 8147208ScaelA pointer to this array is normally stored in the global variable 8247208Scael.Va environ. 8336256SbosticThese strings pass information to the 8428364Sannenew process that is not directly an argument to the command (see 8547208Scael.Xr environ 7 ) . 8647208Scael.Pp 8747208ScaelFile descriptors open in the calling process image remain open in 8847208Scaelthe new process image, except for those for which the close-on-exec 8928364Sanneflag is set (see 9047208Scael.Xr close 2 9136256Sbosticand 9247208Scael.Xr fcntl 2 ) . 9323798SsechrestDescriptors that remain open are unaffected by 9447208Scael.Fn execve . 9547208Scael.Pp 9647208ScaelSignals set to be ignored in the calling process are set to be ignored in 9747208Scaelthe 9847208Scaelnew process. Signals which are set to be caught in the calling process image 9947208Scaelare set to default action in the new process image. 10028068SkarelsBlocked signals remain blocked regardless of changes to the signal action. 10128364SanneThe signal stack is reset to be undefined (see 10247208Scael.Xr sigaction 2 10328364Sannefor more information). 10447208Scael.Pp 10547208ScaelIf the set-user-ID mode bit of the new process image file is set 10647208Scael(see 10747208Scael.Xr chmod 2 ) , 10847208Scaelthe effective user ID of the new process image is set to the owner ID 10947208Scaelof the new process image file. 11047208ScaelIf the set-group-ID mode bit of the new process image file is set, 11147208Scaelthe effective group ID of the new process image is set to the group ID 11247208Scaelof the new process image file. 11358265Smckusick(The effective group ID is the first element of the group list.) 11447208ScaelThe real user ID, real group ID and 11558265Smckusickother group IDs of the new process image remain the same as the calling 11647208Scaelprocess image. 11758265SmckusickAfter any set-user-ID and set-group-ID processing, 11858265Smckusickthe effective user ID is recorded as the saved set-user-ID, 11958265Smckusickand the effective group ID is recorded as the saved set-group-ID. 12058265SmckusickThese values may be used in changing the effective IDs later (see 12158473Smckusick.Xr setuid 2 ) . 122*67351Sah.ne 1i 12347208Scael.Pp 12420108SmckusickThe new process also inherits the following attributes from 12520108Smckusickthe calling process: 12647208Scael.Pp 12747208Scael.Bl -column parent_process_ID -offset indent -compact 12847208Scael.It process ID Ta see Xr getpid 2 12947208Scael.It parent process ID Ta see Xr getppid 2 13047208Scael.It process group ID Ta see Xr getpgrp 2 13147208Scael.It access groups Ta see Xr getgroups 2 13247208Scael.It working directory Ta see Xr chdir 2 13347208Scael.It root directory Ta see Xr chroot 2 13447208Scael.It control terminal Ta see Xr termios 4 13547208Scael.It resource usages Ta see Xr getrusage 2 13647208Scael.It interval timers Ta see Xr getitimer 2 13747208Scael.It resource limits Ta see Xr getrlimit 2 13847208Scael.It file mode mask Ta see Xr umask 2 13947208Scael.It signal mask Ta see Xr sigvec 2 , 14047208Scael.Xr sigsetmask 2 14147208Scael.El 14247208Scael.Pp 14347208ScaelWhen a program is executed as a result of an 14447208Scael.Fn execve 14547208Scaelcall, it is entered as follows: 14647208Scael.Bd -literal -offset indent 14747208Scaelmain(argc, argv, envp) 14847208Scaelint argc; 14947208Scaelchar **argv, **envp; 15047208Scael.Ed 15147208Scael.Pp 15220107Smckusickwhere 15347208Scael.Fa argc 15447208Scaelis the number of elements in 15547208Scael.Fa argv 15620108Smckusick(the ``arg count'') 15720107Smckusickand 15847208Scael.Fa argv 15936256Sbosticpoints to the array of character pointers 16020107Smckusickto the arguments themselves. 16147208Scael.Sh RETURN VALUES 16247208ScaelAs the 16347208Scael.Fn execve 16447208Scaelfunction overlays the current process image 16547208Scaelwith a new process image the successful call 16647208Scaelhas no process to return to. 16720108SmckusickIf 16847208Scael.Fn execve 16947208Scaeldoes return to the calling process an error has occurred; the 17047208Scaelreturn value will be -1 and the global variable 17147208Scael.Va errno 17247208Scaelis set to indicate the error. 17347208Scael.Sh ERRORS 17447208Scael.Fn Execve 17547208Scaelwill fail and return to the calling process if: 17647208Scael.Bl -tag -width [ENAMETOOLONG] 17747208Scael.It Bq Er ENOTDIR 17821003SmckusickA component of the path prefix is not a directory. 17947208Scael.It Bq Er EINVAL 18021003SmckusickThe pathname contains a character with the high-order bit set. 18147208Scael.It Bq Er ENAMETOOLONG 18221003SmckusickA component of a pathname exceeded 255 characters, 18321003Smckusickor an entire path name exceeded 1023 characters. 18447208Scael.It Bq Er ENOENT 18521003SmckusickThe new process file does not exist. 18647208Scael.It Bq Er ELOOP 18721003SmckusickToo many symbolic links were encountered in translating the pathname. 18847208Scael.It Bq Er EACCES 18921003SmckusickSearch permission is denied for a component of the path prefix. 19047208Scael.It Bq Er EACCES 19120108SmckusickThe new process file is not an ordinary file. 19247208Scael.It Bq Er EACCES 19320108SmckusickThe new process file mode denies execute permission. 19447208Scael.It Bq Er ENOEXEC 19520108SmckusickThe new process file has the appropriate access 19620108Smckusickpermission, but has an invalid magic number in its header. 19747208Scael.It Bq Er ETXTBSY 19820108SmckusickThe new process file is a pure procedure (shared text) 19920108Smckusickfile that is currently open for writing or reading by some process. 200*67351Sah.ne 1i 20147208Scael.It Bq Er ENOMEM 20220108SmckusickThe new process requires more virtual memory than 20320108Smckusickis allowed by the imposed maximum 20447208Scael.Pq Xr getrlimit 2 . 20547208Scael.It Bq Er E2BIG 20620108SmckusickThe number of bytes in the new process's argument list 20728151Skarelsis larger than the system-imposed limit. 20828151SkarelsThe limit in the system as released is 20480 bytes 20947208Scael.Pf ( Dv NCARGS 21047208Scaelin 21147208Scael.Ao Pa sys/param.h Ac . 21247208Scael.It Bq Er EFAULT 21320108SmckusickThe new process file is not as long as indicated by 21420108Smckusickthe size values in its header. 21547208Scael.It Bq Er EFAULT 21647208Scael.Fa Path , 21747208Scael.Fa argv , 21847208Scaelor 21947208Scael.Fa envp 22047208Scaelpoint 22120108Smckusickto an illegal address. 22247208Scael.It Bq Er EIO 22324439SmckusickAn I/O error occurred while reading from the file system. 22447208Scael.El 22547208Scael.Sh CAVEAT 22620108SmckusickIf a program is 22747208Scael.Em setuid 22820108Smckusickto a non-super-user, but is executed when 22947208Scaelthe real 23047208Scael.Em uid 23147208Scaelis ``root'', then the program has some of the powers 23220108Smckusickof a super-user as well. 23347208Scael.Sh SEE ALSO 23447208Scael.Xr exit 2 , 23547208Scael.Xr fork 2 , 23647208Scael.Xr execl 3 , 23747208Scael.Xr environ 7 23847208Scael.Sh HISTORY 23947208ScaelThe 24047208Scael.Nm 24147208Scaelfunction call appeared in 24247208Scael.Bx 4.2 . 243