Basic tutorial on the use of Linux exec function family

  
 

Invoke shell script commands: execlp("sh","sh","filename",(char*)0);




exec completely replaces the image of the program that called it with the program being executed. Fork creates a new process and generates a new PID. exec starts a new program and replaces the original process. Therefore, the PID of the new process executed by exec will not change, just like the process of calling exec.

int exec…Load and run other programs: int execl( char *pathname,char *arg0,char *arg1,...,char *argn,NULL)int execle( char *pathname,char * Arg0,char *arg1,...,char *argn,NULL,char *envp[])int execlp( char *pathname,char *arg0,char *arg1,...,NULL)int execlpe(char *pathname, Char *arg0,char *arg1,...,NULL,char *envp[])int execv( char *pathname,char *argv[])int execve( char *pathname,char *argv[],char *envp[ ,null,null,3],]) int execvp( char *pathname, char *argv[])int execvpe(char *pathname,char *argv[],char *envp[]) The exec family of functions loads and runs the program pathname and sets the argument arg0(arg1 , arg2, argv[], envp[]) passed to the subroutine, the error returns -1 in the exec function family, the suffix l, v, p, e is added to the exec, the specified function will have some operational ability When the suffix is ​​p, the function can use the PATH variable of DOS to find the subroutine file. If you want to execute the command /bin/cat /etc/passwd /etc/group,l, you want to receive a comma-separated list of arguments, with the NULL pointer as the end marker execl( "/bin/cat","/Etc/passed","/etc/group",NULL);v, you want to receive a pointer to a NULL-terminated string array char* argv[] = {"/bin/cat","/Etc/passed","/etc/group",NULL}execv( "/bin/cat", argv );e, the function passes the specified parameter envp, allowing the environment of the child process to be changed, without the suffix e The process uses the environment of the current program. Envp is also a NULL-terminated string array pointer execl (execution file) related functions fork, execle, execcl, execv, execve, execvp header file #include<unistd.h> define function int execl(const char * path, const Char * arg,....); Function Description execl() is used to execute the file path represented by the parameter path string. The next parameter represents the passed argv(0), argv[1]&hellip when the file is executed. ;…, the last argument must end with a null pointer (NULL). The return value will not return if the execution is successful. If the execution fails, it will return -1 directly. The reason for the failure is stored in errno. Example #include<unistd.h>main(){execl(“/bin/ls”,”ls”,”-al”,”/etc/passwd”,(char * )0);}Execute /*Execute /bin/ls -al /etc/passwd */-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

execlp (from the PATH environment variable Find the file and execute) related functions fork, execl, execle, execv, execve, execvp header file #include<unistd.h> define function int execlp(const char * file, const char * arg, ……); Explain that execlp() will look for the file name that matches the parameter file from the directory pointed to by the PATH environment variable. After the file name is found, the file will be executed, and the second and subsequent parameters will be treated as argv[0], argv[1] of the file. ……, the last argument must end with a null pointer (NULL). Return value If the execution is successful, the function will not return. If the execution fails, it will return -1 directly. The reason for the failure is stored in errno. The error code refers to execve(). Example /* Executing ls -al /etc/passwd execlp() will find /bin/ls */#include<unistd.h>main(){execlp(“ls”,”ls&rdquo) depending on /bin in the PATH variable ;,”-al”,”/etc/passwd”,(char *)0);}Execute -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

execv (execution file) related functions fork, execl, execle, execcl, execve, execvp header file #include<unistd.h> define function int execv (const char * path, char * const argv[ ]); function The execv() method is used to execute the path of the file represented by the parameter path string. The difference from execl() is that execve() takes only two parameters, and the second parameter is passed to the executable file using the array pointer. Return value If the execution is successful, the function will not return. If the execution fails, it will return -1 directly. The reason for the failure is stored in errno. Please refer to execve() for the error code. Example /* Execute /bin/ls -al /etc/passwd */#include<unistd.h>main(){char * argv[ ]={“ls”,”-al”,”/etc/Passwd”,(char*) }};execv(“/bin/ls”,argv);}Execute -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

execve (execute file) related functions fork, execl, execle, execcl, execv, execvp header file #include<unistd.h> define function int execve(const char * filename, char * const argv[ ], char * Const envp[]); function description execve () is used to execute the file path represented by the parameter filename string, the second parameter is passed to the executable file using the array pointer, argv is passed to the program's complete parameter list, including argv [0], which is typically the name of the executing program; the last parameter is an array of new environment variables passed to the executable. Return value If the execution is successful, the function will not return. If the execution fails, it will return -1 directly. The reason for the failure is stored in errno. Error code EACCES1. The file to be executed does not have the user-executable permissions. 2. The file system to which the file to be executed belongs is suspended in noexec mode. 3. The file or script translator to be executed is not a general file.

Copyright © Windows knowledge All Rights Reserved