Use and comparison of execl functions in Linux

Source: Internet
Author: User


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 last parameter is an array of new environment variables 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.
The e2big parameter array is too large.
Enoexec cannot determine the format of the execution file to be executed. It may be a format error or cannot be executed on this platform.
The string address specified by the efault parameter filename is out of the accessible space range.
The length of the string specified by the enametoolong parameter filename is too long.
The file specified by the enoent parameter filename string does not exist.
Insufficient enomem core memory
The directory path contained in the filename string of the enotdir parameter is not a valid directory.
The directory path contained in the filename string of the eacces parameter cannot be accessed and the permission is insufficient.
Too many symbolic connections in eloop
The file to be executed by etxtbusy has been opened by other processes and is writing data to the file.
Eio I/O Access Error
Enfile has reached the total number of open files allowed by the system.
Emfile has reached the total number of files that the system allows a single process to open.
Einval: the elf execution format of the file to be executed is not only one pt_interp section.
The eisdir elf translator is a directory.
The elibbad elf translator has a problem.
Example # Include <unistd. h>
Main ()
{
Char * argv [] = {"ls", "-Al", "/etc/passwd", (char *) 0 };
Char * envp [] = {"Path =/bin", 0}
Execve ("/bin/ls", argv, envp );
}
Run -RW-r -- 1 Root 705 Sep 3 13: 52/etc/passwd
 




Execvp (Execution file)
Related functions Fork, execl, execle, execlp, execv, execve
Header file # Include <unistd. h>
Define functions Int execvp (const char * file, char * const argv []);
Function Description Execvp () searches for the file name that matches the parameter file from the directory indicated by the PATH environment variable, finds the file, executes the file, and passes the second parameter argv to the file to be executed.
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 /* Compare with the execlp () Example */
# Include <unistd. h>
Main ()
{
Char * argv [] = {"ls", "-Al", "/etc/passwd", 0 };
Execvp ("ls", argv );
}
Run -RW-r -- 1 Root 705 Sep 3 13: 52/etc/passwd

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.