Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1837
  • Last Modified:

exec() for Linux

Well I tried the following code:-

#include  <stdio.h>
int main ( ) {
        int i;
        printf("Before calling execl()\n");
        i = execl("/bin/sh","sh","shpg.sh",NULL);
        if(i<0) {
                perror("execl()");
                exit(0);
        }
        printf("After calling execl()\n");
        return 0;
}

My shpg.sh is:-

echo "Some Text"

I expect the output to be:-

Before calling execl()
Some Text
After calling execl()

But the output is:-

Before calling execl()
Some Text

Can somebody tell me why the code after the exec is not being executed? Thanks in advance.
0
pankajtiwary
Asked:
pankajtiwary
  • 2
1 Solution
 
grueCommented:
From execl man page:

       The exec family of functions replaces the current  process
       image  with  a new process image.  The functions described
       in this  manual  page  are  front-ends  for  the  function
       execve(2).   (See  the manual page for execve for detailed
       information about the replacement of the current process.)

When you call successfully call execl, the current process is REPLACED with the new process.  If successful, execl never returns.

You probably want something like this:

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

int main (int argc, char **argv)
{
  int i;
  printf("Before calling execl()\n");

  pid_t child_pid;
  if (0 == (child_pid = fork())) // fork a new copy of this process
    {
      i = execl("/bin/sh","sh","shpg.sh", NULL); // replace the new copy with sh shpg.sh
    }
  else
    {
      waitpid(child_pid, &i, 0);  // the original copy waits for child to finish
    }

  if(i<0)
    {
    printf("execl() or child error");
    exit(0);
    }

  printf("After calling execl()\n");
  return 0;
}

0
 
grueCommented:
Err, instead of just directly looking at the content of waitpid's status (as I did in my example), there are some macros you should be using...  Something like:

WEXITSTATUS(i);

but I think it happens to "work" in this case because you're just looking for a zero result and the program you run doesn't generate any signals or anything funky like that...  but you really shouldn't be looking at the value directly; use the macro.

see the man page for waitpid for more details.
0
 
pankajtiwaryAuthor Commented:
Thanx Grue,
     I appreciate your help in clarifying my confusion. Yeah, You got exactly what I wanted. I got your point. Actually I was thinking when completing the request execl returns and the rest of the code executes. But it's not like that. I case of success execl() never returns. Thanx again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now