aniztar aniztar - 1 year ago 73
Linux Question

Weird behaviour after calling execv

I am trying to execute a command using execv. After running this program, I can see the statement "Going to call execv!" being printed on the standard output.

I can also see the prints from the program "leaky". Actually everything is working as expected except that I can't see print statements in the if or else block i.e. neither "execv failed! error: " nor "Successfully executed valgrind!" is being printed on the output.

Am I missing some obvious point here about execv?

#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>

int main()
{
char *args[5] = {"/usr/bin/valgrind", "--leak-check=full", "--log-file=valgrindReport.txt", "./leaky", NULL};
printf("Going to call execv!\n");
if(execv("/usr/bin/valgrind", args) < 0)
{
printf("execv failed! error: %s\n", strerror(errno));
return 0;
}
else
{
printf("Successfully executed valgrind!\n");
}
return 0;
}

Answer Source

If you get the output of valgrind then obviously execve was successful. If execve is successful it replaces the current process image with the one to be started and does not return. If execve returns then it failed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download