Where to declare a sig_t signal for SIGPIPE

I am currently using kqueue to handle multiple clients in a thread in Serverprocess so I do not want the thread to be terminated when Signal SIGPIPE appears, I just would like to remove the corresponding socked id from kqueue. So my question is: is there a way to get the corresponding socketid inside the Signalhandle and parse it back to Process to remove it from the kqueue event, or do I have jsut for SIG_IGN SIGPIPE and handle the deletion by returning -1 from send? and will it return -1 after a timeout or immediately send -1?

And finally, if you ignore the signal - this is my decision: where id should place an ad

typedef void (*sig_t) (int); sig_t signal(int sig, sig_t func); 

Do I need to be in the main function? or at the beginning of the corresponding thread? or as a global element?

+4
c signals sockets kqueue
source share
2 answers

I cannot imagine an easy way for a signal handler to know that the current socket is being processed if you do not set any global state each time you perform a socket operation.

You can ignore SIGPIPE from the main. You do not define your own handler; instead, use SIG_IGN .

 signal(SIGPIPE, SIG_IGN); 

Or if you use sigaction :

 struct sigaction act; act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGPIPE, &act, NULL); 

Alternatively, you can issue the MSG_NOSIGNAL flag when calling send. This will suppress the generation of SIGPIPE and instead generate an EPIPE error (what will happen if you ignore SIGPIPE ):

 ssize_t sent = send(sock, buf, sizeof(buf), MSG_NOSIGNAL); if (sent > 0) { /* ... */ } else { assert(sent < 0); swtich (errno) { case EPIPE: /* ...handle sending on a closed socket */ /* ...handle other error cases */ } } 
+3
source share

'signal (...' must be in 'main'.

+1
source share

All Articles