Hi I’m building a program that uses a signal handler shown below …
struct sigaction pipeIn;
pipeIn.sa_handler = updateServer;
sigemptyset(&pipeIn.sa_mask);
pipeIn.sa_flags = SA_ONESHOT;
if(sigaction(SIGUSR1, &pipeIn, NULL) == -1){
printf("We have a problem, sigaction is not working.\n");
perror("\n");
exit(1);
}
The problem is that this handler is getting tripped when it’s not supposed to. The only thing that should send the SIGUSR1 signal is my child process which exists inside an infinite while loop which listens for incoming connections. The child process is forked as you can see below. I redo the pipeIn handler to run a different function that the child process uses which the parent does not. The code is shown below.
while(1){
newSock = accept(listenSock,(struct sockaddr *)&their_addr,&addr_size);
printf("A\n");
if(!fork()){
// We want to redefine the interrupt
pid_t th;
th = getpid();
printf("child pid: %d\n",th);
pipeIn.sa_handler = setFlag;
if(sigaction(SIGUSR1, &pipeIn, NULL) == -1){
printf("We have a problem, sigaction is not working.\n");
perror("\n");
exit(1);
}
close(listenSock);
kill(getppid(),SIGUSR1);
waitForP();
}*/
close(newSock);
exit(0);
}
close(newSock);
//waitForP();
//break;
}
When I run this code, I will make a call from another computer to connect to my server program that you see here. It will accept() the one request from that computer just fine, but then the child process will eventually get around to sending SIGUSR1 to the parent process. However the parent process receives the SIGUSR1 signal before the child process even gets to send the signal. The handler trips the function before it should … then the child process finally gets to kill the signal and the handler goes off the 2nd time. Lastly the accept() function goes off again even if there are no new connections being produced and the incoming ip address is from a weird ipv6 address that is random. I don’t know what’s going on. Any help would be great.
Repeat after me: always check for error returned from a system call (which
accept(2)is – you are getting-1instead of a socket descriptor,EINTRinerrnoand undefined connecting address).