Where to declare sig_t signal for SIGPIPE

2019-06-27 09:11发布

问题:

I'm currently using a kqueue to handle multiple Clients per Thread in a Serverprocess so I don't want the thread to be terminated when the Signal SIGPIPE appears, i would just like to remove the according socked id from the kqueue. So My question is: Is there a way to get the according socketid inside a Signalhandle and parse it back to the Process to remove it from the event kqueue or would i have jsut to SIG_IGN the SIGPIPE and handle the remove by returning of -1 from send? and would it return the -1 value after a timeout time or returns send -1 instantly?

And finally, if the signal ignore is my solution: where id have to put the declaration of the

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

Would it have to be in the main function? or in the beginning of the corresponding thread? or just as global element?

回答1:

I can't think of an easy way for the signal handler to come to know the current socket being processed unless you are setting some global state each time you do a socket operation.

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

signal(SIGPIPE, SIG_IGN);

Or, if you are using 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 you call send. This will suppress the generation of SIGPIPE, and instead generate an EPIPE error (which is what would happen if you ignored 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 */
    }
}


回答2:

'signal( ...' code should be in 'main'.