Questions tagged [execvp]

execvp, is a POSIX specified function from the exec* family, which replaces the current process with one specified. Questions using this tag should be related to the use and semantics of this function.

execvp is a POSIX function, from the exec* family, which replaces the current process with one specified.

It is available on most every UNIX-like platform, including Linux and Mac OS X, where a unistd.h file exists, and many languates, such as Python, emulate this function on platforms, such as Windows, where this function does not normally exist. Many languages, including C, C++, and Python, have interfaces to this function.

If it succeeds, it will not return and the current process will be replaced. If it does not succeed, then it will return a -1.


POSIX specifies that this function has the signature:

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

Its arguments are interpreted as follows.:

  • path: This is a NULL terminated string which holds the name of an executable. It is specified that execvp will search the PATH environment variable for this name if it is not a path.

    If it is a path, then execvp will look for the executable where specified, using an absolute path if this argument begins with a /, or a relative one if this argument doesn't.

Example: ping will resolve to /bin/ping.

  • argv: This is an array of NULL terminated strings, which make up the argv of the process in the path argument.

    This array should end with a NULL, and should start with the name of the executable passed in the path argument.

Example: {"ping", "-c", "5", "www.google.com", NULL}


POSIX includes the following complete example for reference:

The following example illustrates the use of execvp to execute the ls shell command:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

main()
{    
     pid_t pid;
     char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL};

     if ((pid = fork()) == -1)
          perror("fork() error");    
     else if (pid == 0) {
          execvp("ls", parmList);
          printf("Return not expected. Must be an execvp() error.\n");    
     }
}
368 questions
7
votes
5 answers

Getting stdout from an executed application

Does anyone know how to catch the output (I think its stdout) from execvp instead of the system printing it (in c on linux) in the terminal?
topherg
  • 4,203
  • 4
  • 37
  • 72
7
votes
2 answers

Passing Vec from Rust to char** in C

I've been trying to write a shell in Rust that links directly to the libc library. I've used a Vec to hold the arguments to be passed to execvp(), but it seems that my conversion to char ** has not been successful. Upon execution, all the…
Jianzhong Liu
  • 85
  • 1
  • 7
6
votes
2 answers

Handling arguments array of execvp?

When I call execvp, for example execvp(echo, b) where b is an array of arguments for the command a, will changing this array later affect the execvp call made previously? When I try calling execp(echo, b), it ends up printing out (null) instead of…
Lucas
  • 1,577
  • 6
  • 18
  • 25
6
votes
1 answer

How to prevent inheriting CPU affinity by child forked process?

I have a server process that forks many child processes. The server process has affinity to a CPU core, but I don't want that affinity to be inherited by child process (rather OS should handle where to run these processes). Is there a way to delink…
user424060
  • 1,545
  • 3
  • 20
  • 29
6
votes
3 answers

execvp/fork -- how to catch unsuccessful executions?

Right now I'm writing a C program that must execute a child process. I'm not doing multiple child processes simultaneously or anything, so this is fairly straightforward. I am definitely executing the built-in shell programs (i.e. things like cat…
Kris H.
  • 97
  • 1
  • 6
5
votes
2 answers

How to "replace" `os.execvpe` on Windows efficiently - if the "child" process is an interactive command line application?

I am dealing with a Python script which does, after some preparation work, launch ssh. My script is actually a small CLI tool. On Unix-like systems, at the end of its life, the Python script replaces itself with the ssh client, so the user can now…
s-m-e
  • 3,433
  • 2
  • 34
  • 71
5
votes
1 answer

Error when spawning child process in node.js

I'm trying to get a little ffmpeg converter-service up and running, made pretty good progress so far. But when it comes to spawning the actual ffmpeg process for conversion, i'm hitting a brick wall. // options.ffmpegopts is an array containing…
schaermu
  • 13,478
  • 2
  • 39
  • 32
5
votes
2 answers

Reading from `forkpty` child process: `ls /` output yields `EIO`

I'm trying to use forkpty to execvp a ls /, and then from the parent process read its output and write to stdout. I got it working, and running it under Valgrind shows no errors either. EDIT: Valgrind shows no memory errors. The last read still…
Márcio
  • 677
  • 8
  • 15
5
votes
1 answer

fork/exec/waitpid issue

I'm trying to determine whether an execution failed by checking the result of waitpid(). However, even when I run a command that I know fails and writes the issue to stderr, the check below never registers. What could possibly be wrong with this…
Jim Ruffian
  • 53
  • 1
  • 4
5
votes
2 answers

execvp and type of parameters - ansi c

I am having trouble with using execvp(). execvp() expects type char * const* as second parameter. I want to parse arguments passed to application (in argv) and make an array of that type. For example, user is invoking the binary as given…
marxin
  • 3,692
  • 3
  • 31
  • 44
5
votes
2 answers

Building a C shell. execvp returns 'No such file' error. creating argv array on-the-fly with malloc

I am building a shell and am having some trouble with the system call 'execvp'. I saw some other questions on this topic but they were vague, and did not appear to be completely addressed (whoever asked the questions didn't provide much information…
matchdav
  • 715
  • 1
  • 7
  • 16
4
votes
1 answer

Why am I not seeing the printf buffer flush?

I have a simple program (working example): #include #include int main() { pid_t my_pid = getpid(); char str[30]; sprintf(str, "/proc/%d/fd", my_pid); printf("hello, I am gonna print out: %s", str); execvp( "ls",…
user129393192
  • 797
  • 1
  • 8
4
votes
1 answer

String literals in argv

I mean to ask about the preservation of string literals within a call to execve family. From OSTEP Chp.4: #include #include #include #include #include int main (int argc, char *argv[]) { …
user129393192
  • 797
  • 1
  • 8
4
votes
3 answers

After execvp returns, why doesn't my program pick up where it left off?

I have a block of code like this that runs as a child thread: if(someVar == 1){ doSomeStuff; _exit(0) } else execvp(*(temp->_arguments), temp->_arguments); printf("I'm done\n"); When I run the program with someVar == 1, I understand that the…
samoz
  • 56,849
  • 55
  • 141
  • 195
4
votes
3 answers

How to pass a vector to execvp

I want to pass a vector in as the second argument to execvp. Is it possible?
neuromancer
  • 53,769
  • 78
  • 166
  • 223
1
2
3
24 25