execv or execve?

This is a discussion on execv or execve? within the C Programming forums, part of the General Programming Boards category; What's the difference between the two? I want to execute the netcat command like so: Code: nc -l -p 1234 ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    execv or execve?

    What's the difference between the two? I want to execute the netcat command like so:
    Code:
    nc -l -p 1234 -e /bin/sh
    Is it better to use execv or execve and what would the C code look like using it?

    I tried this but got an invalid initializer
    Code:
    char *args[] = ("nc","-l","-p 1234", "-e /bin/sh", (char *) 0);
    execv("nc",args);
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    With execve() you can specify the environment to the new process image with the env argument.
    The env argument is the last argument passed to the execve() call.
    The execv() call cannot specify the the env argument.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    So passing NULL to execve's environment it's the same thing as passing execv then?

    Also how do I correctly send the netcat command with arguments?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by jcafaro10 View Post
    So passing NULL to execve's environment it's the same thing as passing execv then?

    Also how do I correctly send the netcat command with arguments?
    See the manpages of execv() and execve() which should it clear and those parenthesis should really be curly brackets.
    First argument to execv() should be the full path to netcat command, as in
    Code:
    char *args[] = {"nc","-l","-p 1234", "-e /bin/sh", (char *) 0};
    execv("/full/path/to/nc", args);

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Well I'm closer now. Here's what I have
    Code:
    char *args[] = {"nc","-l","-p 1234", "-e /bin/sh", (char *) 0};
    execv("/bin/nc", args);
    but I'm getting exec failed, no such file or directory.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    The error is for /bin/sh which is odd because I obviously have a shell...

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by jcafaro10 View Post
    Well I'm closer now. Here's what I have
    Code:
    char *args[] = {"nc","-l","-p 1234", "-e /bin/sh", (char *) 0};
    execv("/bin/nc", args);
    but I'm getting exec failed, no such file or directory.
    RTM on the exec() family of calls. Each option and its argument (if any) is a NULL terminated string without whitespace so concatenations like "-p 1234" are not semantically correct. Search the forums and the web as there are are plenty of code examples that show the correct usage of exec(). Here are some good ones: c-faq and cboard faq

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting stderr from an app launched via execv
    By KPexEA in forum Linux Programming
    Replies: 1
    Last Post: 08-16-2008, 11:36 PM
  2. No call to execv
    By protocol78 in forum C Programming
    Replies: 4
    Last Post: 05-08-2007, 12:43 PM
  3. execv and defunct process
    By groorj in forum C Programming
    Replies: 5
    Last Post: 08-05-2005, 08:37 AM
  4. execv extension list for shell
    By newbie29 in forum C Programming
    Replies: 1
    Last Post: 02-21-2005, 12:56 PM
  5. problems with fork() and execve()
    By kristy in forum C Programming
    Replies: 4
    Last Post: 12-21-2003, 08:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21