exec(2) CLIX exec(2)
NAME
exec: execl, execv, execle, execve, execlp, execvp - Executes a file
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
int execl(
char *path ,
char *arg0 ,
);
int execv(
char *path ,
char *argv[] );
int execle(
char *path ,
char *arg0 ,
char *arg1 ,
,
char *envp[] ,
);
int execve(
char *path ,
char *argv[] ,
char *envp[] );
int execlp(
char *file ,
char *arg0 ,
);
int execvp(
char *file ,
char *argv[] );
PARAMETERS
path Specifies a pointer to the pathname of the new process image file.
file Specifies a pointer to the name of the new process image file.
Unless the file parameter is a full pathname, the path prefix for
the file is obtained by searching the directories named in the PATH
environment variable. The initial environment is supplied by the
shell.
2/94 - Intergraph Corporation 1
exec(2) CLIX exec(2)
The execlp() and execvp() functions take file parameters, but the
rest of the exec() functions take path parameters.
Points to null-terminated character strings. The strings
constitute the argument list available to the new process. By
convention, at least the arg0 parameter must be present, and it
must point to a string that is the same as the path parameter or
its last component. The arg0 parameter can be repeated as an
optional parameter (as arg1 through argn), since the argument list
is marked with 0.
argv Specifies an array of pointers to null-terminated character
strings. These strings constitute the argument list available to
the new process. By convention, the argv parameter must have at
least one element, and it must point to a string that is the same
as the path parameter or its last component. The last element of
the argv parameter is a NULL pointer.
envp Specifies an array of pointers to null-terminated character
strings. These strings constitute the environment for the new
process. The last element of the envp parameter is a NULL pointer.
DESCRIPTION
The exec() function, in all its forms, executes a new program in the
calling process. The exec() function does not create a new process, but
overlays the current program with a new one, which is called the new
process image.
When a C program is run, it receives the following parameters:
main(
int argc ,
char *argv[] ,
char *envp[] );
In this example, the argc parameter is the argument count, and the argv
parameter is an array of character pointers to the arguments themselves.
By convention, the value of the argc parameter is at least 1, and the
argv[0] parameter points to a string containing the name of the new
process image file.
The main routine of a C language program automatically begins with a run-
time start-off routine. This routine sets the environ global variable so
that it points to the environment array passed to the program in envp.
For execl() and execv(), the C run-time start-off routine places a pointer
to the environment of the calling process in the global cell as follows:
extern char **environ;
This pointer is used to pass the environment of the calling process to the
2 Intergraph Corporation - 2/94
exec(2) CLIX exec(2)
new process.
File descriptors open in the calling process remain open, except for those
whose close-on-exec flag is set. For those file descriptors that remain
open, the file pointer is unchanged. (For information about file control,
see the <fcntl.h> header file.)
Signals set to terminate the calling process are set to terminate the new
process. Signals set to be ignored by the calling process are set to be
ignored by the new process. Signals set to be caught by the calling
process are set to terminate the new process.
For signals set by sigset, exec() ensures that the new process has the
same system signal action for each signal type whose action is SIG_DFL,
SIG_IGN, or SIG_HOLD (see signal.h) as the calling process. However, if
the action is to catch the signal, the action will be reset to SIG_DFL
(see signal.h), and any pending signal for this type is held.
If the set-user-ID mode bit of the new process image file is set (see the
chmod() function), the exec() function sets the effective user ID of the
new process to the owner ID of the new process image file. Similarly, if
the set-group-ID mode bit of the new process image file is set, the
effective group ID of the new process is set to the group ID of the new
process image file. The real user ID and real group ID of the new process
remain the same as those of the calling process.
The shared memory segments attached to the calling process will not be
attached to the new process. (See the shmop() function.)
Profiling is disabled for the new process. (For information about
profiling, see the profil() function.)
The new process inherits the following attributes from the calling
process:
⊕ The nice value (See the nice() function.)
⊕ The process ID
⊕ The parent process ID
⊕ The process group ID
⊕ The semadj values (See the semop() function.)
⊕ The tty group ID (See the exit() and sigaction() function.)
⊕ The trace flag (See request 0 of the ptrace() function.)
⊕ The time left until an alarm clock signal (See the alarm() function.)
2/94 - Intergraph Corporation 3
exec(2) CLIX exec(2)
⊕ The current directory
⊕ The root directory
⊕ The file mode creation mask (See the umask() function.)
⊕ The file size limit (See the ulimit() function.)
⊕ The utime, stime, cutime, and cstime members (See the times()
function.)
⊕ File locks (See the fcntl() and lock() functions.)
EXAMPLES
1. To run a command and pass it a parameter:
execlp("ls", "ls", "-al", 0);
Or, enter from the command line:
li -al
The execlp() function searches each of the directories listed in the
PATH environment variable for the ls command, and then it overlays the
current process image with this command. The execlp() function does
not return, unless the ls command cannot be executed. Note that this
example does not run the shell command processor, so operations
interpreted by the shell, such as using wildcard characters in
filenames, are not valid.
2. To run the shell to interpret a command:
execl("/bin/sh", "sh", "-c", "ls -l *.c", 0);
Or, enter from the command line:
sh -c ls -l *.c
This example runs the sh (shell) command with the -c flag, which
indicates that the following parameter is the command to be
interpreted. This example uses the execl() function instead of the
execlp() function because the full pathname /bin/sh is specified,
making a PATH search unnecessary.
Running a shell command in a child process is generally more useful
than simply using the exec() function, as shown in this example.
RETURN VALUES
Upon successful completion, the exec() functions do not return because the
4 Intergraph Corporation - 2/94
exec(2) CLIX exec(2)
calling process image is overlaid by the new process image. If the exec()
function returns to the calling process, the value of -1 is returned and
the global variable errno is set to identify the error.
The exec() function fails and returns to the calling process if one or
more of the following are true:
[EACCES]
The new process image file is not an ordinary file.
[EACCES]
The mode of the new process image file denies execute permission.
[ENOEXEC]
The exec() function is not an execlp() or execvp() function, and
the new process image file has the appropriate access permission
but the magic number in its header is not valid.
[ETXTBSY]
The new process image file is a pure procedure (shared text) file
that is currently open for writing by some process.
[ENOMEM]
The new process requires more memory than is allowed by the
system-imposed maximum MAXMEM (see sys/xio/xsky.h).
[E2BIG]
The number of bytes in the new process argument list is greater
than the system-imposed limit of ARG_MAX (see unistd.h) bytes.
[EFAULT]
The path, argv, or enviromentpointer parameter points outside of
the process address space.
[EACCES]
Search permission is denied on a component of the path prefix.
[ENAMETOOLONG]
The length of the path or file parameters, or an element of the
PATH environment variable prefixed to a file, exceeds PATH_MAX, or
a pathname component is longer than NAME_MAX and _POSIX_NO_TRUNC is
in effect for that file.
[ENOENT]
A component of the path prefix does not exist.
[ENOTDIR]
A component of the path prefix is not a directory.
[EFAULT]
Required hardware is not present.
2/94 - Intergraph Corporation 5
exec(2) CLIX exec(2)
[EAGAIN]
Not enough memory.
[ELIBACC]
Required shared library does not have execute permission.
[ELIBEXEC]
Trying to execute (with exec()) a shared library directly.
[EINTR]
A signal was caught during the exec() function.
[ENOLINK]
The path parameter points to a remote machine and the link to that
machine is no longer active.
[EMULTIHOP]
Components of path require hopping to multiple remote machines.
RELATED INFORMATION
Functions: exit(2), fcntl(2), fork(2), nice(2), signal(2), sigset(2),
times(2), alarm(2), ptrace(2), semop(2), sigaction(2), ulimit(2), umask(2)
Commands: sh(1)
Files: a.out(4), environ(4)
6 Intergraph Corporation - 2/94