testermaster
testermaster

Reputation: 1065

sigset_t is useful in this case?

I'm trying to implement an easy server/client with handler for signals.

My only use for signals will be sigaction(SIG...,&sig,NULL), where:

sig.sa_sigaction = &closeSig;
sig.sa_flags = SA_SIGINFO;


void closeSig{
   send(ds_sock,"close",1024,0);
   close(ds_sock);
   exit(1);
}

If the server (or the client) receives "close", it simply closes the connection.

In this situation, is it useful to use the sigset_t, and add in it all the signals I wanto to handle, and then use sigaction like up, instead using only e sigaction?

If it's useful, can you explain me the reason? I've a beta program, and it uses a sigset on the server, but it doesn't on the client.. Thanks!

edit: I've just found out that the server also uses sigprocmask with unblock. If I don't want to use it, and I want all signals to be unblocked and that they immediatly launch their handler, I don't have to use a sigset, right? Thanks!

I'm sorry, but I can't post the code. If you can help me, that's good, if you can't, thanks anyway!

Upvotes: 0

Views: 468

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 755074

The code shown for closeSig() is not valid C. To be usable with sigaction() and the SA_SIGINFO flag, the type of the function needs to be:

void closeSig(int signum, siginfo_t *info, void *context) { … }

The third argument is a void *, but can be cast to a ucontext_t * (see sigaction()).

When you call sigaction(), you should ensure that the sig.sa_mask is set to a known value, probably by using sigemptyset(&sig.sa_mask); to clear all the bits. This means that while a signal handler is called, other signals could still interrupt the signal handler. If you prefer not to let that happen, use sigfillset(&sig.sa_mask); instead; this blocks all signals (that are blockable) while a signal handler is in effect. Unblockable signals such as SIGKILL will still be delivered.


The comments have identified some other problems with the code fragments shown.

Upvotes: 0

Related Questions