GeoffreyB
GeoffreyB

Reputation: 1839

Unix - Waitpid 'status'

I'm working on an Unix-Shell, using C langage. I use waitpid to wait my processes to terminate and i want to know if my son process (created with fork() ) received a signal like SIGSEGV.

Current code :

static const t_error    error[ERROR_NBR]= {
  {SIGHUP, "Hangup"},
  {SIGQUIT, "Quit (core dumped)"},
  {SIGABRT, "Abort (core dumped)"},
  {SIGBUS, "Bus error (core dumped)"},
  {SIGFPE, "Floating exception (core dumped)"},
  {SIGKILL, "Killed"},
  {SIGUSR1, "User signal 1"},
  {SIGSEGV, "Segmentation fault (core dumped)"},
  {SIGUSR2, "User signal 2"},
  {SIGPIPE, "Broken pipe"},
  {SIGTERM, "Terminated"},
};

void print_signal_message(int status)
{
 int i;
 if (WIFSIGNALED(status))
    status = WTERMSIG(status);
 else
    return ;
 i = -1;
 while (++i < ERROR_NBR)
   {
     if (status == error[i].error_status)
       {
         fprintf(stderr, "%s\n", error[i].error);
         return ;
       }
   }
 return ;
}
int             xwaitpid(int pid, int *status, int opt)
{
  int           ret;

  ret = waitpid(pid, status, opt);
  if (ret == -1)
    fprintf(stderr, "Can't perfom waitpid (pid = %d)\n", pid);
  if (WIFEXITED(*status))
    *status = WEXITSTATUS(*status);
  else
    **print_sigerr(*status);**
  return (ret == -1 ? (1) : ret);
}

xwaitpid is called in the parent process.

It seems that WIFSIGNALED always returns TRUE, even when i execute a command like false || true.

Someone has a good idea that could help me ?

-> found my answer, thanks.

Upvotes: 1

Views: 3026

Answers (1)

Mat
Mat

Reputation: 206831

if (WIFEXITED(*status))
    *status = WEXITSTATUS(*status);

That's your problem. You can't WIFSIGNALED on *status after that, you've lost the required bits in that int.

Upvotes: 3

Related Questions