I am trying to extract the PID from a signal by using the system call
sigaction().
The code (shown below) "registers" a signal handler for SIGUSR2 by using
sigaction() with the sa_flags set to SA_SIGINFO. The signal handler for
SIGUSR2 extracts the sender's PID (from si_pid) and displays it on the
screen. Then, by using raise(), the same program sends SIGURS2 to itself.
On Linux 2.6, the program works always displays the correct sender's
PID, but when I try to run the same code on FreeBSD 6.2, the PID is
ALWAYS displayed as 0.
What am I doing wrong?
Thankx in advance!
/** CODE **/
#include <signal.h>
#include <stdio.h>
#include <unistd.h> // for getpid()
/*
* Signal handler for SIGUSR2
*/
static void sigusr2Handler(int signo, siginfo_t *info, void *context)
{
if (info != NULL)
printf("Signal Number: %d (SIGUSR2) \tSender PID: %d\n",
signo, (info->si_pid) );
else
printf("WARNING: info = NULL\n");
}
int main(void)
{
struct sigaction action, old_action;
sigaction(SIGUSR2, NULL, &old_action);
action = old_action;
action.sa_handler = NULL;
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = sigusr2Handler;
sigaction(SIGUSR2, &action, NULL);
printf("PID: %d\n", (int) getpid() );
raise(SIGUSR2);
return 0;
}
[...]
> struct sigaction action, old_action;
>
>
> sigaction(SIGUSR2, NULL, &old_action);
>
>
> action = old_action;
This is useless. The purpose of the 'old' argument is to restore
a previous signal action without 'knowing' what it was.
>
> action.sa_handler = NULL;
> action.sa_flags = SA_SIGINFO;
> action.sa_sigaction = sigusr2Handler;
And this is wrong, cf sigaction/ SUS:
[...]
SA_SIGINFO
If cleared
[...]
the application shall use the sa_handler member to describe
the signal-catching function and the application shall not
modify the sa_sigaction member.
If SA_SIGINFO is set
[...]
the application shall use the sa_sigaction member to describe
the signal-catching function and the application shall not
modify the sa_handler member.