sigaction always registers sa_handler even if SA_SIGINFO is specified
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
sigaction always registers sa_handler even if SA_SIGINFO is specified
I was just trying to learn sigaction and encountered the following problem. Sigaction call never registers sa_sigaction. It always registers default or sa_handler. Man page says if SA_SIGINFO is set in sa_flags, it will register sa_sigaction.(I am running 2.6.17-1.2142_FC4. So, I guess I can safely ignore the note that SA_SIGACTION is part of 2.2 or newer kernels)
The o/p of the program is as follows. It just calls default handler as if I never registered anything.
1- Take a look at struct sigaction in <bits/sigaction.h> and you will see that both sa_handler and sa_sigaction do exist at the same offset on the structure. This is even mentioned in the manpage when it says:
Quote:
On some architectures a union is involved - do not assign to both sa_handler and sa_sigaction
2- The way structures should be initialized is manually. The POSIX standard just defines which struct's members must be present while allowing room for others than operating systems can and do define... Standards don't even define the order at which they are set! The only proper way to initialize a struct is to use bzero() on the whole structure and then assign values to each member explicitly.
Thanks a lot primo. I had seen that and I even looked into the header file. But for some reason I assumed that setting SA_SIGINFO will take care of proper assignment. I changed the initialization to
Now it is working fine. However I am curious to know if bzero is really mandatory even if I initialize all members. I have this doubt because, if we accept that bzero is necessary to initialize a structure, won't we have to accept that there is no decent way to initialize constant structures?
Now it is working fine. However I am curious to know if bzero is really mandatory even if I initialize all members. I have this doubt because, if we accept that bzero is necessary to initialize a structure, won't we have to accept that there is no decent way to initialize constant structures?
In this case you're using a C99 feature so you're guaranteed that every other member is set to zero. In older C, the only way to achieve that is declaring the structure as static. For local structures, it's perfectly fine to initialize it if declaration occurs at the same time, for example:
Code:
struct {
int foo;
int bar;
} foobar = { 1, 2 };
Otherwise we can't expect other structures (specially extern structures) to have their members ordered in the way we're initializing them.
Primo -
bzero() is marked as legacy in POSIX.1-2001 i.e., use memset() instead for new code. Not that it matters, but you mentioned 'proper way <..>' which bzero() may not be.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.