1

Assume I have the following code.

int main(int argc, char* argv[])
{
  char* program[3] = {"/bin/ls", NULL, NULL};

  pid_t pid = fork();
  if (pid == 0) {
    char* envp[1] = {NULL};
    execve(program[0], program, envp);
  }

  int status;
  printf("Going to wait for process with pid: %d\n", pid);
  waitpid(pid, &status, 0);

  return 0;
}

if I execute it, it terminates just fine.

But, if I do "ls -l" (just changing the program array) instead of "ls" it halts forever. So this program

int main(int argc, char* argv[])
{
  char* program[3] = {"/bin/ls", "-l", NULL};

  pid_t pid = fork();
  if (pid == 0) {
    char* envp[1] = {NULL};
    execve(program[0], program, envp);
  }

  int status;
  printf("Going to wait for process with pid: %d\n", pid);
  waitpid(pid, &status, 0);

  return 0;
}

Any idea on why this is happening?

insumity
  • 5,311
  • 8
  • 36
  • 64
  • and does the program print the "Going to wait..." ? And is the process with the printed PID running? – Zaboj Campula Dec 06 '16 at 19:35
  • Yes it does in both cases. – insumity Dec 06 '16 at 19:36
  • I can't reproduce this. Are you running it with the cwd on some sort of network share or other non-ordinary filesystem? (Like maybe NFS??) – BadZen Dec 06 '16 at 19:37
  • In any case the problem is very likely not `waitpid()` but your `ls` actually hanging. (What state is the `ls` process in?) – BadZen Dec 06 '16 at 19:37
  • @BadZen maybe you are right about `ls` hanging since I can see `ls` with `ps` but stays there forever. I'm not running it on an NFS. What do you mean by cwd? – insumity Dec 06 '16 at 19:39
  • If it is not a Z state process - (and I likely suspect you will see a D), your problem is not `waitpid()` not doing its job. cwd == current working directory. – BadZen Dec 06 '16 at 19:39
  • what if you run `/bin/ls -l` from command line? Does it terminate normally? – Zaboj Campula Dec 06 '16 at 19:40
  • @ZabojCampula, yes "/bin/ls -l" from command line works just fine. @BadZen Checking with `ps aux` i see a `S+` next to the hanging `/bin/ls -l` – insumity Dec 06 '16 at 19:43
  • Try run `pstack `. What is output? – Zaboj Campula Dec 06 '16 at 19:46
  • @ZabojCampula I didn't have pstack in my computer, so I tried in a different one (the same source file) and there it worked. This is even more confusing! BadZen I verified I'm not running it on NFS. – insumity Dec 06 '16 at 19:58
  • 1
    Changing `execve` to `execv` makes it work. – insumity Dec 06 '16 at 20:06
  • It is really weird. I can only speculate what could be the reason. Plain `ls` does not need any environment. `ls -l` may read environment to get local timezone and your locale to correctly present datetimes. When there is no environment than `ls -l` hangs because there is something wrong with default locales, timezone or whatever. If you find what was the real reason I am interested in it. – Zaboj Campula Dec 06 '16 at 20:22
  • If _Changing `execve` to `execv` makes it work_, the weird `/bin/ls -l` behaviour should also be reproducible by `env -i ls -l` - is it? – Armali May 04 '18 at 14:03

0 Answers0