How to receive the signal multiple times in user space without terminating it?
I want to write a program wher a process in user space sends its process id which is received in the kernel space and a
real time signal is send from the kernel space to that user space process.
The following program works fine; but the problem is the user-space process terminates immediately after receiving the
signal. Also, if we just write sig.sa_flags |=SA_RESTART;
instead of writing sig.sa_flags |= SA_SIGINFO;
sig.sa_flags |=SA_RESTART; then it is causing segmentation fault.
Q1: Is there any way we can receive the signal multiple times in the user space without terminating the process?
Q2: What is the reason for this segmentation fault?
/*======================part of Kernel code which sends the signal=======*/
#define SIG_TEST 44 // (real-time signal)
static int raise_mySignal()
{ struct siginfo info;
struct task_struct *t;
info.si_signo=SIG_TEST;
info.si_int=1;
info.si_code = SI_QUEUE;
printk(KERN_INFO "********Process id: %d", user_pid);
rcu_read_lock();
t= pid_task(find_vpid(user_pid),PIDTYPE_PID);//user_pid has been fetched successfully
if(t == NULL){
printk("no such pid\n");
rcu_read_unlock();
return -ENODEV;
}
rcu_read_unlock();
send_sig_info(SIG_TEST,&info,t);
return 0;
}
/*===========================Receiver Prg============================================================= */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define SIG_TEST 44
void receiveData(int n, siginfo_t * info, void *unused) {
printf("received value %i\n", info->si_int);
}
int main ( int argc, char **argv ) {
int configfd; char buf[10];
/* setup the signal handler for SIG_TEST
* SA_SIGINFO -> we want the signal handler function with 3 arguments */
struct sigaction sig;
sig.sa_sigaction = receiveData;
sig.sa_flags |= SA_SIGINFO;
sig.sa_flags |=SA_RESTART;
//sig.sa_flags = SA_RESTART;
sigaction(SIG_TEST, &sig, NULL);
configfd = open("/sys/kernel/debug/signalconfpid", O_WRONLY);
if(configfd < 0) {
perror("***unable to open");
return -1;
}
sprintf(buf, "%i", getpid());
if (write(configfd, buf, strlen(buf) + 1) < 0) {
perror("fwrite");
return -1;
}
sleep(100);
return 0;
}
|