Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

exec: execl, execv, execle, execve, execlp, execvp, execvpe - execute file

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>
extern char * *environ;

int execl (const char *path, const char *arg0, ... , (char *)0 );
int execv (const char *path, char *const argv[ ]);
int execle (const char *path, const char *arg0, ... , (char *)0, char *const envp[ ] );
int execve (const char *path, char *const argv[ ], const char *envp[ ]);
int execlp (const char *file, const char *arg0, ... , (char *)0 );
int execvp (const char *file, char *const argv[ ]);
int execvpe (const char *file, char *const argv[ ], const char *envp[ ]);

Syntax

#include <unistd.h>
extern char * *environ;

int execl (const char *path, const char *arg0, ... , (char *)0 );
int execv (const char *path, char *const argv[ ]);
int execle (const char *path, const char *arg0, ... , (char *)0, char *const envp[ ] );
int execve (const char *path, char *const argv[ ], const char *envp[ ]);
int execlp (const char *file, const char *arg0, ... , (char *)0 );
int execvp (const char *file, char *const argv[ ]);
int execvpe (const char *file, char *const argv[ ], const char *envp[ ]); 

Description

The exec family of functions replaces the current process image with a new process image.

The new image is constructed from a regular, executable file (path or file) called the new process image file. There is no return from a successful exec, because the calling process image is overlaid by the new process image.

When a C program is executed as a result of a call to an exec function, it is entered as a C function call as follows:

int main (int argc, char *argv[]);

where argc is the argument count and argv is an array of character pointers to the arguments themselves. argc is at least 1, and the first element of the array points to a string containing the name of the executable file.

In addition, the following variable is initialized as the address of an array of char pointers to the environment variables:

extern char **environ;

argv and environ are each terminated by a null pointer. The null pointer terminating the argv array is not counted in argc.

The arguments specified by a program with one of the exec functions are passed on to the new process image in the corresponding main() arguments.

path points to a pathname that identifies the new process image file.

file is used to construct a pathname that identifies the new process image file. If file contains a slash character, then the file argument is used as the pathname for the process image file. Otherwise, the path prefix for this file is obtained by a search of the directories defined by the environment variable PATH (see section “Environment variables”). The environment is typically supplied by the POSIX shell (see also the manual "POSIX Basics" [1 (Related publications)]). Other X/Open-compatible systems may define alternate mechanisms for this purpose.

If the process image file is not a valid executable object, execlp() and execvp() use the contents of that file as standard input to a command interpreter conforming to system(). In this case, the command interpreter becomes the new process image.

arg0, ... are pointers to null-terminated character strings. These strings constitute the argument list available to the new process image. The list is terminated by a null pointer. The argument arg0 should point to a filename that is associated with the process being started by one of the exec functions.

argv is an array of character pointers to null-terminated strings. The last element in this array must be a null pointer. These strings constitute the argument list for the new process image. The value in argv[0] should point to a filename that is associated with the process being started by one of the exec functions.

envp is an array of character pointers to null-terminated strings. These strings constitute the environment for the new process image. The envp array is terminated by a null pointer.

In the case of functions which do not pass envp as an argument (execl(), execv(), execlp() and execvp()), the environment for the new process image is taken from the external variable environ in the calling process.

The number of bytes available for the combined argument and environment lists of the process is {ARG_MAX}. In the POSIX subsystem, the {ARG_MAX} constant includes the space for null terminators, pointers, and/or any alignment bytes. This may be implemented differently on other X/Open-compatible systems.

File descriptors open in the calling process image remain open in the new process image, except for those whose close-on-exec flag FD_CLOEXEC is set (see also fcntl()). For those file descriptors that remain open, all attributes of the open file description, including file locks, remain unchanged.

The state of conversion descriptors and message catalog descriptors in the new process image is undefined. For the new process, the equivalent of:

setlocale(LC_ALL, "C")

is executed at startup.

Signals set to the signal action SIG_DFL in the calling process image are set to the default signal action in the new process image. Signals set to be ignored (SIG_IGN) by the calling process image are also ignored by the new process image. Signals set to be caught by the calling process image are set to the default signal action in the new process image (see also signal.h).

After a successful call to any of the exec functions, any functions previously registered by the atexit() function are no longer registered.

If the set-user-ID mode bit is set for the new process image file (see also chmod()), the effective user ID of the new process image is set to the user 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 image is set to the group ID of the new process image file. The real user ID, real group ID, and supplementary group IDs of the new process image remain the same as those of the calling process image. The effective user ID and effective group ID of the new process image are saved as the saved set-user-ID and the saved set-group-ID for use by setuid().

Any shared memory segments attached to the calling process image will not be attached to the new process image (see also shmat()).

The new process also inherits the following attributes from the calling process image:

  • nice value (see also nice())

  • semadj values (see also semop())

  • process ID

  • parent process ID

  • process group ID

  • session ID

  • real user ID

  • real group ID

  • supplementary group IDs

  • time left until an alarm clock signal (see also alarm())

  • current working directory

  • root directory

  • file mode creation mask (see also umask())

  • file size limit (see also ulimit())

  • process signal mask (see also sigprocmask())

  • pending signals (see also sigpending())

  • tms_utime, tms_stime, tms_cutime and tms_cstime (see also times())

All other process attributes of the XPG4-compliant library functions will be the same in the new and old process images.

Upon successful completion, the exec functions mark for update the st_atime field of the file. If an exec function failed but was able to locate the process image file, whether the st_atime field is marked for update is unspecified. Should the exec function succeed, the process image file is considered to have been opened with open(). The corresponding close() is considered to occur at a time after this open, but before process termination or successful completion of a subsequent call to one of the exec functions.

POSIX files are closed on calling an exec function only if the CLOSE_ON_EXEC flag is set.

If threads are used, then the function affects the process or a thread in the following manner:

  • When one of the exec() functions are called in a process with more than one thread, all threads are terminated and then the new executable program is loaded and executed. No destructor functions are called.

    BS2000

  • BS2000 files are always closed on calling an exec() function. (End)

Return val.

-1

if an error occurs. errno is set to indicate the error.

Errors

The exec functions will fail if:

 

E2BIG

The number of bytes used by the argument list and environment list of the new process image is greater than the system-imposed limit of {ARG_MAX} bytes.

 

EACCES

Search permission is denied for a directory listed in the path prefix of the new process image,
or the new process image file denies execution permission,
or the new process image file is not a regular file and the implementation does not support execution of files of its type.

 

Extension

 

EFAULT

EINTR

ELOOP

The program could not be loaded.

A signal was caught.

Too many symbolic links were encountered in resolving path or file. (End)

 

ENAMETOOLONG



The length of the path or file arguments, or an element of the environment variable PATH prefixed to a file, exceeds {PATH_MAX}, or a pathname component is longer than {NAME_MAX}.

 

Extension

 

ENOENT

One or more components of the pathname of the new process image file does not exist,
or path or file points to an empty string.

 

ENOMEM

A new process image requires more memory than is allowed by the hardware or system-imposed memory management constraints.

 

ENOTDIR

A component of the path prefix of the new process image file is not a directory. (End)


The exec functions - except for execlp() and execvp() - will fail if:

 

ENOEXEC

The new process image file has the appropriate access permissions, but is not in the proper format.

Notes

Since the state of conversion descriptors and message catalog descriptors in the new process image is undefined, portable applications should not rely on their use and should close them prior to calling one of the exec functions.

Before the program to be executed is loaded, the environment variables BLSLIBnn (where 00 <= nn <= 98) are evaluated in ascending order, starting with BLSLIB00. The contents of the variables are interpreted as BS2000 file names, and a link to each respective file name is set up using the variable name. The search is aborted at the first variable that does not exist; however, a link to the file $.SYSLNK.CRTE is created with the link name BLSLIB99 in any case. This mechanism allows incompletely linked programs, which need to load modules dynamically, to be executed in a child process that does not inherit the TFT (terminal file table) from its parent process. 

See also

alarm(), atexit(), exit(), fcntl(), fork(), getenv(), nice(), putenv(), semmop(), setlocale(), shmat(), sigaction(), system(), times(), ulimit(), umask(), unistd.h,  section “Environment variables”.