Exec System Call in C

29/12/2020
Chưa phân loại
The exec family has many functions in C. These C functions are basically used to run a system command in a separate process that the main program and print the output.

In this article, I am going talk about the exec family of functions and show you how to use each one of these exec family function in C. So, let’s get started.

C System Functions in Exec Family:

The exec function families are defined in the header unistd.h. So, you must use this header on the C program where you want to use these functions.

The available exec functions along with their function parameters are given below:

  • int execl(const char *path, const char *arg, …, NULL);
  • int execlp(const char *file, const char *arg, …, NULL );
  • int execv(const char *path, char *const argv[]);
  • int execvp(const char *file, char *const argv[]);
  • int execle(const char *path, const char *arg, …, NULL, char * const envp[] );
  • int execve(const char *file, char *const argv[], char *const envp[]);

Let’s see what each of these functions do and how to use them.

execl() System Function:

In execl() system function takes the path of the executable binary file (i.e. /bin/ls) as the first and second argument. Then, the arguments (i.e. -lh, /home) that you want to pass to the executable followed by NULL. Then execl() system function runs the command and prints the output. If any error occurs, then execl() returns -1. Otherwise, it returns nothing.

Syntax:

int execl(const char *path, const char *arg,, NULL);

An example of the execl() system function is given below:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/ls";
  char *arg1 = "-lh";
  char *arg2 = "/home";
 
  execl(binaryPath, binaryPath, arg1, arg2, NULL);
 
  return 0;
}

I ran the ls -lh /home command using execl() system function. As you can see, the correct result is displayed.

execlp() System Function:

execl() does not use the PATH environment variable. So, the full path of the executable file is required to run it with execl(). execlp() uses the PATH environment variable. So, if an executable file or command is available in the PATH, then the command or the filename is enough to run it, the full path is not needed.

Syntax:

int execlp(const char *file, const char *arg,, NULL );

We can rewrite the execl() example using the execlp() system function as follows:

#include <unistd.h>
 
int main(void) {
  char *programName = "ls";
  char *arg1 = "-lh";
  char *arg2 = "/home";
 
  execlp(programName, programName, arg1, arg2, NULL);
 
  return 0;
}

I only passed the command name ls, not the full path /bin/ls. As you can see, I got the same output as before.

execv() System Function:

In execl() function, the parameters of the executable file is passed to the function as different arguments. With execv(), you can pass all the parameters in a NULL terminated array argv. The first element of the array should be the path of the executable file. Otherwise, execv() function works just as execl() function.

Syntax:

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

We can rewrite the execl() example as follows:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/ls";
  char *args[] = {binaryPath, "-lh", "/home", NULL};
 
  execv(binaryPath, args);
 
  return 0;
}

As you can see, I am getting the correct output.

execvp() System Function:

Works the same way as execv() system function. But, the PATH environment variable is used. So, the full path of the executable file is not required just as in execlp().

Syntax:

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

We can rewrite the execv() example as follows:

#include <unistd.h>
 
int main(void) {
  char *programName = "ls";
  char *args[] = {programName, "-lh", "/home", NULL};
 
  execvp(programName, args);
 
  return 0;
}

As you can see, the correct output is displayed.

execle() System Function:

Works just like execl() but you can provide your own environment variables along with it. The environment variables are passed as an array envp. The last element of the envp array should be NULL. All the other elements contain the key-value pairs as string.

Syntax:

int execle(const char *path, const char *arg,, NULL, char * const envp[] );

An example of the execle() system function is given below:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/bash";
  char *arg1 = "-c";
  char *arg2 = "echo "Visit $HOSTNAME:$PORT from your browser."";
  char *const env[] = {"HOSTNAME=www.linuxhint.com", "PORT=8080", NULL};
 
  execle(binaryPath, binaryPath,arg1, arg2, NULL, env);
 
  return 0;
}

I passed two environment variables HOSTNAME and PORT to the execle() function. As you can see, I can access them from the executable /bin/bash.

execve() System Function:

Just like execle() you can provide your own environment variables along with execve(). You can also pass arguments as arrays as you did in execv().

Syntax:

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

The execle() example can be rewritten as follows:

#include <unistd.h>
 
int main(void) {
  char *binaryPath = "/bin/bash";
  char *const args[] = {binaryPath, "-c", "echo "Visit $HOSTNAME:$PORT
    from your browser."", NULL};
  char *const env[] = {"HOSTNAME=www.linuxhint.com", "PORT=8080", NULL};
 
  execve(binaryPath, args, env);
 
  return 0;
}

As you can see, we get the same output as in the execle() example.

So, that’s how you use the exec function family in C for system programming in Linux. Thanks for reading this article.

Sandclock IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, Sandclock IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn cấu hình bản ghi SPF, DKIM, DMARC nâng cấp bảo mật G Suite

Nhằm nâng cấp bảo mật của hệ thống G Suite trước các tình trạng email nặc danh (phishing email),...
27/01/2021

How to install Parrot Sec OS

Parrot Security OS is an open source and free GNU/LINUX distribution that is made for developers, security researchers,...
29/12/2020

Ubuntu User Management

User management is an important part of any Ubuntu/Linux system administrator’s job. It is also one of the most common...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn chuyển đổi windows server windows evaluation to standard và active windows server 2008 + 2012 + 2016 + 2019
26/10/2021

How to Update Ubuntu Linux
24/10/2021

Squid Proxy Manager cài đặt và quản lý Proxy Squid tự động trên ubuntu
20/10/2021

Hướng dẫn cài đặt Apache CloudStack 4.15.2.0
19/10/2021

Hướng dẫn ký file PDF bằng chữ ký số (chữ ký điện tử) và sửa lỗi mới nhất 2021 foxit reader
19/10/2021