On Sat, May 01, 2010 at 03:59:07PM +0200, Jesus Sanchez wrote:
> Hi, using 4.6 release.
>
> I'm doing some code on process forking and catching signals on
> OpenBSD. My interest here is to catch the SIGCHLD signal and do things
> with the pid which sended the signal on the function called to treat it.
>
> As said in "Advanced Programming in the Unix Environment" book, when
> calling a sigaction function there is a siginfo_t * with data about the
> process sending the signal. On this struct, the member int si_pid
> contains the PID of the process sending the signal. I tried in a very
> simple code to obtain the PID of the child process but si_pid member
> always contains 0 when I print it, and don't know what's wrong with it.
>
> I included a very simple source code to try this with the mail, what
> I'm missing?
On OpenBSD, only a few fields are filled for a few signals. Use any of
the wait(2) functions to get your info.
-Otto
>
> Google didn't helped at all.
>
> Thanks for your time.
> -J
> #include <signal.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/wait.h>
> #include <unistd.h>
>
> /* this func is called when SIGCHLD is received */
> void mysigaction(int nsig, siginfo_t * info , void *nothing){
>
> /* print some info values */
> printf("info->si_pid: %d info->si_code: %d info->si_status: %d --\n",
> info->si_pid , (*info). si_code, (*info).si_status);
> printf("nsig: %d\n",nsig);
>
> return ;
> }
>
> int main(){
>
> struct sigaction myaction;
> myaction.sa_sigaction=mysigaction;
> myaction.sa_flags=SA_SIGINFO ;
> /* use sa_sigaction instead sa_handler to
> * have siginfo_t * values */
>
> sigaction(SIGCHLD,&myaction,(void*)NULL);
>
> int pid=fork();
> if (pid > 0 ) { // father
> printf("father, child pid is: %d\n",pid);
> wait(NULL);
> }
>
> if (pid == 0){ // son
> printf("son, getpid() returns: %d\n",getpid());
> exit(0);
> }
>
> return 0;
> }