xref: /csrg-svn/lib/libc/sys/execve.2 (revision 67102)
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*67102Smckusick.\"     @(#)execve.2	8.4 (Berkeley) 05/06/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
43*67102Smckusick.\" was .Fn execve Ap d ,
44*67102Smckusick\fBexecve\fP'd,
4547208Scaelthe system
46*67102Smckusick.\" was .Fn execve Ap s
47*67102Smckusick\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
55*67102Smckusick.\" was .Fn execve Ap d
56*67102Smckusick\fBexecve\fP'd
5736256Sbosticfile becomes the second argument;
5836256Sbosticotherwise, the name of the originally
59*67102Smckusick.\" was .Fn execve Ap d
60*67102Smckusick\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
63*67102Smckusick.\" was .Fn execve Ap d
64*67102Smckusick\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 ) .
12247208Scael.Pp
12320108SmckusickThe new process also inherits the following attributes from
12420108Smckusickthe calling process:
12547208Scael.Pp
12647208Scael.Bl -column parent_process_ID -offset indent -compact
12747208Scael.It process ID Ta see Xr getpid 2
12847208Scael.It parent process ID Ta see Xr getppid 2
12947208Scael.It process group ID Ta see Xr getpgrp 2
13047208Scael.It access groups Ta see Xr getgroups 2
13147208Scael.It working directory Ta see Xr chdir 2
13247208Scael.It root directory Ta see Xr chroot 2
13347208Scael.It control terminal Ta see Xr termios 4
13447208Scael.It resource usages Ta see Xr getrusage 2
13547208Scael.It interval timers Ta see Xr getitimer 2
13647208Scael.It resource limits Ta see Xr getrlimit 2
13747208Scael.It file mode mask Ta see Xr umask 2
13847208Scael.It signal mask Ta see Xr sigvec 2 ,
13947208Scael.Xr sigsetmask 2
14047208Scael.El
14147208Scael.Pp
14247208ScaelWhen a program is executed as a result of an
14347208Scael.Fn execve
14447208Scaelcall, it is entered as follows:
14547208Scael.Bd -literal -offset indent
14647208Scaelmain(argc, argv, envp)
14747208Scaelint argc;
14847208Scaelchar **argv, **envp;
14947208Scael.Ed
15047208Scael.Pp
15120107Smckusickwhere
15247208Scael.Fa argc
15347208Scaelis the number of elements in
15447208Scael.Fa argv
15520108Smckusick(the ``arg count'')
15620107Smckusickand
15747208Scael.Fa argv
15836256Sbosticpoints to the array of character pointers
15920107Smckusickto the arguments themselves.
16047208Scael.Sh RETURN VALUES
16147208ScaelAs the
16247208Scael.Fn execve
16347208Scaelfunction overlays the current process image
16447208Scaelwith a new process image the successful call
16547208Scaelhas no process to return to.
16620108SmckusickIf
16747208Scael.Fn execve
16847208Scaeldoes return to the calling process an error has occurred; the
16947208Scaelreturn value will be -1 and the global variable
17047208Scael.Va errno
17147208Scaelis set to indicate the error.
17247208Scael.Sh ERRORS
17347208Scael.Fn Execve
17447208Scaelwill fail and return to the calling process if:
17547208Scael.Bl -tag -width [ENAMETOOLONG]
17647208Scael.It Bq Er ENOTDIR
17721003SmckusickA component of the path prefix is not a directory.
17847208Scael.It Bq Er EINVAL
17921003SmckusickThe pathname contains a character with the high-order bit set.
18047208Scael.It Bq Er ENAMETOOLONG
18121003SmckusickA component of a pathname exceeded 255 characters,
18221003Smckusickor an entire path name exceeded 1023 characters.
18347208Scael.It Bq Er ENOENT
18421003SmckusickThe new process file does not exist.
18547208Scael.It Bq Er ELOOP
18621003SmckusickToo many symbolic links were encountered in translating the pathname.
18747208Scael.It Bq Er EACCES
18821003SmckusickSearch permission is denied for a component of the path prefix.
18947208Scael.It Bq Er EACCES
19020108SmckusickThe new process file is not an ordinary file.
19147208Scael.It Bq Er EACCES
19220108SmckusickThe new process file mode denies execute permission.
19347208Scael.It Bq Er ENOEXEC
19420108SmckusickThe new process file has the appropriate access
19520108Smckusickpermission, but has an invalid magic number in its header.
19647208Scael.It Bq Er ETXTBSY
19720108SmckusickThe new process file is a pure procedure (shared text)
19820108Smckusickfile that is currently open for writing or reading by some process.
19947208Scael.It Bq Er ENOMEM
20020108SmckusickThe new process requires more virtual memory than
20120108Smckusickis allowed by the imposed maximum
20247208Scael.Pq Xr getrlimit 2 .
20347208Scael.It Bq Er E2BIG
20420108SmckusickThe number of bytes in the new process's argument list
20528151Skarelsis larger than the system-imposed limit.
20628151SkarelsThe limit in the system as released is 20480 bytes
20747208Scael.Pf ( Dv NCARGS
20847208Scaelin
20947208Scael.Ao Pa sys/param.h Ac .
21047208Scael.It Bq Er EFAULT
21120108SmckusickThe new process file is not as long as indicated by
21220108Smckusickthe size values in its header.
21347208Scael.It Bq Er EFAULT
21447208Scael.Fa Path ,
21547208Scael.Fa argv ,
21647208Scaelor
21747208Scael.Fa envp
21847208Scaelpoint
21920108Smckusickto an illegal address.
22047208Scael.It Bq Er EIO
22124439SmckusickAn I/O error occurred while reading from the file system.
22247208Scael.El
22347208Scael.Sh CAVEAT
22420108SmckusickIf a program is
22547208Scael.Em setuid
22620108Smckusickto a non-super-user, but is executed when
22747208Scaelthe real
22847208Scael.Em uid
22947208Scaelis ``root'', then the program has some of the powers
23020108Smckusickof a super-user as well.
23147208Scael.Sh SEE ALSO
23247208Scael.Xr exit 2 ,
23347208Scael.Xr fork 2 ,
23447208Scael.Xr execl 3 ,
23547208Scael.Xr environ 7
23647208Scael.Sh HISTORY
23747208ScaelThe
23847208Scael.Nm
23947208Scaelfunction call appeared in
24047208Scael.Bx 4.2 .
241