5

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?

dhein
  • 6,431
  • 4
  • 42
  • 74
  • reacting on the result of `send()` at least is more portable and so for me it seems to be the better way. Anyway I'm curious if someone has an answer (+1). – Ingo Leonhardt Jul 16 '13 at 16:48
  • Well, maybe i expressed my question bad again, but as far im going to use the return of send(), I jsut want to know, where i have to put sig_t signal(SIGPIPE, SIG_IGN); to get the signal gets ignored. – dhein Jul 16 '13 at 16:57

2 Answers2

4

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 */
    }
}
jxh
  • 69,070
  • 8
  • 110
  • 193
1

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

KimKonkuk
  • 31
  • 2
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – jpw Nov 15 '13 at 08:35
  • @jpw Well it is not well explained at all, thats true, but it provides an answer to my question (even if it is not a good one, it is a (correct and) illustrative answer. – dhein Nov 16 '13 at 03:12