Compare the three functions of the execv related executable in C

  • 2020-04-02 03:18:40
  • OfStack

C language execv() function: execute file function
The header file:


 #include <unistd.h>

Definition function:


int execv (const char * path, char * const argv[]);

Function description: execv() is used to execute the file path represented by the parameter path string. The difference from execl() is that execve() only needs two parameters, and the second parameter is passed to the execution file using an array pointer.

Return value: the function does not return on success, but returns -1 on failure. The reason for the failure is in errno.

Error code: refer to execve ().

sample


/*  perform /bin/ls -al /etc/passwd */
#include <unistd.h>
main()
{
  char * argv[] = {"ls", "-al", "/etc/passwd", (char*)};
  execv("/bin/ls", argv);
}

Perform:


-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

C language execve() function: execute file function
The header file:


 #include <unistd.h>

Definition 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 filename string, the second parameter is passed to the execution file using an array pointer, and the last parameter is passed to the execution file as an array of new environment variables.

Return value: the function does not return on success, but returns -1 on failure. The reason for the failure is in errno.

Error code:

EACCES:
1. The file to be executed does not have the permission that the user can execute.
2. The file system to which the file to be executed belongs is noexec mounted.
3. The file or script translator to be executed is not a normal file.

EPERM:
1. The process is in chase mode, the executor does not have root privileges, and the file to be executed has SUID or SGID bits.
2. The file system to be executed belongs to is hung by nosuid. The file to be executed has SUID or SGID bits, but the executor does not have root privileges.

E2BIG parameter array is too large

ENOEXEC cannot determine the format of the executable file to be executed. It may be malformed or unable to execute on this platform.

The string address referred to by the EFAULT parameter filename is out of the range of accessible space.

The string referred to by the ENAMETOOLONG parameter filename is too long.

The file specified by the ENOENT parameter filename string does not exist.

ENOMEM is out of core memory

The directory path contained in the ENOTDIR parameter filename string is not a valid directory

The directory path contained in the EACCES parameter filename string cannot be accessed.

Too many symbolic joins in ELOOP

The file that ETXTBUSY is about to execute has been opened by another process and is writing data to that 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 the system allows a single process to open.

EINVAL wants to execute more than one PT_INTERP section of the ELF execution format of the file

The EISDIR ELF translator is a directory

There is a problem with the ELIBBAD ELF translator.

sample


#include <unistd.h>
main()
{
  char * argv[] = {"ls", "-al", "/etc/passwd", (char *)0};
  char * envp[] = {"PATH=/bin", 0};
  execve("/bin/ls", argv, envp);
}

Perform:


-rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

C language execvp() function: execute file function
The header file:


 #include <unistd.h>

Definition function:


int execvp(const char *file, char * const argv []);

Function description: execvp() looks for the file name of the parameter file in the directory referred to by the PATH environment variable, finds it, executes it, and then passes the second parameter argv to the file to be executed.

Return value: the function does not return on success, but returns -1 on failure. The reason for the failure is in errno.

Error code: refer to execve ().

sample



#include <unistd.h>
main()
{
  char * argv[] = {"ls", "-al", "/etc/passwd", 0};
  execvp("ls", argv);
}

Perform:
-rw-r-r-1 root root 705 Sep 3 13:52:/etc/passwd


Related articles: