Hi

In the SIGCHLD signal handler of parent process, I need to get the pid of the child when the child dies. To do this I'm using sigaction with the SA_SIGINFO flag. But the siginfo_t->si_pid member does not seem to be set.

I found the following post from Otto that seems to say that what I'm trying to do may not work on OpenBSD.

http://www.archivum.info/fa.openbsd.tech/2008-02/msg00124.html

Ive tried the following code on linux and everything works great, but on OpenBSD it doesn't work. I've tried it on 3.8 and 4.2, and I'm currently downloading 4.4 to see if it works on there.

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>


void signalChild(
   int signal, siginfo_t* info, void* notUsed
){
   printf("Entering signal handler!\n");
   if ( info == NULL )
   {
       printf("siginfo is NULL...aborting!\n");
       exit(1);
   }
   printf("Handling signal %d at %p\n", signal, info->si_addr);
   printf("PID: %d\t \n", info->si_pid);
   printf("Signal code: %d\n", info->si_code );
}


int main()
{
   struct sigaction child;
   sigemptyset ( &child.sa_mask );
   child.sa_sigaction = &signalChild;
   child.sa_flags = 0;
   child.sa_flags |= SA_SIGINFO;
   child.sa_flags &= ~SA_NOCLDSTOP;

   if ( sigaction( SIGCHLD, &child, NULL ) == -1 )
   {
       fprintf( stderr, "Error with signal setup.  Errno: %i\n", errno );
       exit( 1 );
   }

   int pid = fork();

   if ( pid == 0 )
   {
       fprintf( stderr, "PID: %i\n", getpid() );
       exit(0);
   }

   sleep( 1 );
   exit(0);
}


Does anybody know how I'm to accomplish this, or why it doesn't work?

Thank You

Jonathan Steel

Reply via email to