1. Introduction
In Linux, there is no exec () function. exec refers to a group of six functions, namely:
# Include <unistd. h>
Extern char ** environ;
Int execl (const char * path, const char * arg ,...);
Int execlp (const char * file, const char * arg ,...);
Int execle (const char * path, const char * arg,..., char * const envp []);
Int execv (const char * path, char * const argv []);
Int execvp (const char * file, char * const argv []);
Int execve (const char * path, char * const argv [], char * const envp []);
Detailed definition:
Execl (Execution file)
Related functions
Fork, execle, execlp, execv, execve, execvp
Header file
# Include <unistd. h>
Define functions
Int execl (const char * path, const char * arg ,....);
Function Description
Execl () is used to execute the file path represented by the path string parameter. The following parameter indicates that the previous argv (0), argv [1]…, The last parameter must end with a NULL pointer.
Return Value
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.
Example
# Include <unistd. h>
Main ()
{
Execl ("/bin/ls", "ls", "-al", "/etc/passwd", (char *) 0 );
}
Run
/* Run/bin/ls-al/etc/passwd */
-Rw-r -- 1 root 705 Sep 3 13: 52/etc/passwd
Execlp (find and execute files from PATH environment variables)
Related functions
Fork, execl, execle, execv, execve, execvp
Header file
# Include <unistd. h>
Define functions
Int execlp (const char * file, const char * arg ,......);
Function Description
Execlp () searches for the file name that matches the parameter file from the directory indicated by the PATH environment variable, and then runs the file, then, the second parameter is used as the argv [0], argv [1]…, The last parameter must end with a NULL pointer.
Return Value
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.
Error Code
See execve ().
Example
/* Execute ls-al/etc/passwd execlp () and find/bin/ls */According to/bin in the PATH variable */
# Include <unistd. h>
Main ()
{
Execlp ("ls", "ls", "-al", "/etc/passwd", (char *) 0 );
}
Run
-Rw-r -- 1 root 705 Sep 3 13: 52/etc/passwd
Execv (Execution file)
Related functions
Fork, execl, execle, execlp, execve, execvp
Header file
# Include <unistd. h>
Define functions
Int execv (const char * path, char * const argv []);
Function Description
Execv () is used to execute the file path represented by the path string parameter. Unlike execl (), execve () requires only two parameters, the second parameter uses an array pointer to pass to the execution file.
Return Value
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.
Error Code
See execve ().
Example
/* Run/bin/ls-al/etc/passwd */
# Include <unistd. h>
Main ()
{
Char * argv [] = {"ls", "-al", "/etc/passwd", (char *)}};
Execv ("/bin/ls", argv );
}
Run
-Rw-r -- 1 root 705 Sep 3 13: 52/etc/passwd
Execve (Execution file)
Related functions
Fork, execl, execle, execlp, execv, execvp
Header file
# Include <unistd. h>
Define functions
Int execve (const char * filename, char * const argv [], char * const envp []);
Function Description
Execve () is used to execute the file path represented by the filename string parameter. The second parameter is passed to the execution file using the array pointer,
The complete parameter list that argv will pass to the program, including argv [0]. It is generally the name of the execution program. The last parameter is the new environment variable array passed to the execution file.
Return Value
If the execution is successful, the function will not return. If the execution fails,-1 will be returned. The cause of the failure is stored in errno.
Error Code
EACCES
1. the file to be executed does not have the executable permissions of the user.
2. The file system of the file to be executed is mounted in noexec mode.
3. The file or script translator to be executed is not a general file.
EPERM
1. The process is in tracing mode, and the executor does not have the root permission. The file to be executed has the SUID or SGID bit.
2. The file system of the file to be executed is mounted in nosuid mode. The file to be executed has SUID or SGID, but the performer does not have root permission.
Among them, only execve is a real system call, and others are packaged library functions on this basis.
The exec function family finds an executable file based on the specified file name and uses it to replace the content of the calling process. In other words, it executes an executable file within the calling process. The executable file can be either a binary file or any script file that can be executed in Linux.