How to use execv system call in linux?

64,260

Solution 1

In order to see the difference, here is a line of code executing a ls -l -R -a

  • with execl(3):

    execl("/bin/ls", "ls", "-l", "-R", "-a", NULL);
    
  • with execv(3):

    char* arr[] = {"ls", "-l", "-R", "-a", NULL};
    execv("/bin/ls", arr);
    

The char(*)[] sent to execv will be passed to /bin/ls as argv (in int main(int argc, char **argv))

Solution 2

According to the man page the use of execv is quite simple. The first argument is the path as a string to the program you want to execute. The second is an array of string that will be used as the arguments of the program you want to execute. It is the kind of array you get if you get the argv array in your main function.

So the array you will pass as a parameter will be the array received in the main function of the program you execute with execv.

By convention, the first argument should be the program name (the one you try to execute) but it is not mandatory (but strongly recommended since it is the behaviour a lot of programs are expecting). Each other string in the array should be an individual argument.

And of course, the array should be terminated with a NULL pointer to mark the end.

Array example: ["prog_name", "arg1", "arg2", "arg3", NULL]

[] is your array, each string separated with a coma is a frame of your array and at the end you have the null frame.

I hope I am clear enough!

Share:
64,260
doe
Author by

doe

Updated on July 09, 2022

Comments

  • doe
    doe almost 2 years

    I am writing a program using execl to execute my exe file which is testing and it's work very well and display the output in the Linux CLI. But I have not idea how to change the execl to execv, although I know both of the system call will give the same value. I am confused with the array argument for execv system call

    This is my execl sample program

    int main(void)
    {
       int childpid;
       if((childpid = fork()) == -1 )
    {
       perror("can't fork");
       exit(1);
    }
     else if(childpid == 0)
    {
      execl("./testing","","",(char *)0);
      exit(0);
    }
    else
    {
    printf("finish");
    exit(0);
    }
    }
    

    can I know how to change the execl to execv. What I read from online, we must set the file path for my exe file and the argument of array . What type of argument need to set for the array in order to ask the program to execute the testing exe file ? https://support.sas.com/documentation/onlinedoc/sasc/doc/lr2/execv.htm

    Is it the link consist of the thing I want ? But what I read from it ,the command is request the list the file,not execute the file. Correct me I make any mistake

    • Kevin
      Kevin over 8 years
      Why do you want to use execv? What are you actually trying to accomplish?
    • alk
      alk over 8 years
      Did you try the example on the page you link? Here the offical Linux man-page: man7.org/linux/man-pages/man3/exec.3.html
    • tripleee
      tripleee over 8 years
      linux.die.net/man/3/execv -- the difference is that execv wants a single pointer to a char * array, whereas execl accepts a variadic list of char * arguments.
    • alk
      alk over 8 years
      Related if not a duplicate: stackoverflow.com/q/10790719/694576
    • Brian McFarland
      Brian McFarland over 8 years
      Like Kevin said, why change it? With the exception of execvpe, which AFAIK is essentially an alias for the "real" execve, it's basically all syntactic sugar. So use the one that already works.
    • doe
      doe over 8 years
      because I see the execv will bring out the same result for me, I just wanna try only
  • doe
    doe over 8 years
    thank you ,your guide absolute help me but what is the argument type I need to specify in the array ?Is it similar with the 4rzael answer ?
  • Roger
    Roger over 8 years
    Yes it is similar. Your array is a string (char *) array. So it is more like ["prog_name", "arg1", "arg2", "arg3", NULL]
  • sleighty
    sleighty about 6 years
    Does C support inline arrays like execv("/bin/ls", ["ls", "-l", "-R", "-a", NULL]);? When compiling on a linux machine with gcc --std=c99 file.c I get an error: error: expected expression before '[' token. Couldn't find anything about this elsewhere... Any ideas?
  • 4rzael
    4rzael about 6 years
    No, my bad, it was just to explain the difference. I will edit
  • sleighty
    sleighty about 6 years
    Oh thank you haha I was going nuts trying to look for a way to make that work.
  • ransh
    ransh over 5 years
    if the command has syntax of arg1 val1 arg2 val2 (such as iptables -A INPUT -p udp) is it that the valX is treated as argument ?
  • vpappano
    vpappano almost 2 years
    I know this is old, but shouldn't char* arr[] = {"ls", "-l", "-R", "-a", NULL}; execv("/bin/ls", arr); be char* arr[] = {"ls", "-l", "-R", "-a", NULL}; execv("/bin/", arr);?