Łukasz Lew
Łukasz Lew

Reputation: 50298

How to wait for process child?

I do the usual fork + exec combination:

int sockets [2];
socketpair (AF_LOCAL, SOCK_STREAM, 0, sockets);
int pid = fork ();

if (pid == 0) {
  // child
  dup2 (sockets[0], STDIN_FILENO);
  dup2 (sockets[0], STDOUT_FILENO);
  execvp (argv[0], argv);
  _exit (123);
}
// parent

close (sockets[0]);
// TODO wait and see if child crashes

Is it possible to wait until child crashes or begins waiting on read(...)?

Upvotes: 1

Views: 1175

Answers (3)

Alexey Feldgendler
Alexey Feldgendler

Reputation: 1810

You can use the ptrace(2) syscall to do what strace(1) does. In the child process, before exec'ing, you'll need to invoke PTRACE_TRACEME, and that will attach the parent process to it as a debugger. The parent will then have to resume execution with PTRACE_SYSCALL, and it will stop at each syscall. The parent should use wait(2) to wait until the child trips at a syscall, then use ptrace with PTRACE_PEEK* to examine the arguments to the syscall. Repeat that until the syscall you're waiting for (read) is invoked, then detach from the child process with PTRACE_DETACH. Look at the source of strace for details on how to recognize the syscall of interest. If the process crashes, the wait syscall will tell you that the process got SIGSEGV. Don't forget to detach in that case, too.

Upvotes: 1

Charles Ma
Charles Ma

Reputation: 49171

checkout waitpid

 pid_t waitpid(pid_t pid, int *stat_loc, int options);

from

#include <sys/wait.h>

Upvotes: 1

Artelius
Artelius

Reputation: 49089

Here's a suggestsion:

Open a pipe, the child process can send a message down the pipe to tell the parent that it is about to read() on stdin. If you do this correctly and the child dies, the parent will be notified that the pipe has been broken, I think.

Upvotes: 0

Related Questions