LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2006, 08:42 AM   #1
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
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.

Quote:
[root@sarin c]# ./a.out
Sigaction: 0
Setitimer: 0
Alarm clock
Can someone tell me what is wrong with this program?

Code:
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>

void sigact1(int slSigNum,siginfo_t *pstSigInfo,void *ptr)
{
printf("Got a Sigalrm \n");
printf("Sender: %d \n",pstSigInfo->si_pid);
}
void sigact(int slSigNum)
{
    printf("In signal handler %d\n",slSigNum);
}

int main()
{
const struct sigaction stAct={NULL,sigact1,0,SA_SIGINFO,NULL};
struct sigaction stOldAct;
struct itimerval stTimer={{5,0},{5,0}};


printf("Sigaction: %d \n",sigaction(SIGALRM,&stAct,&stOldAct));
printf("Setitimer: %d \n",setitimer(ITIMER_REAL,&stTimer,NULL));

while(1);
return 0;
}
Thanks,
Sarin
 
Old 12-26-2006, 04:53 AM   #2
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
There are at least 2 errors:

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.

Last edited by primo; 12-26-2006 at 05:13 AM.
 
Old 12-26-2006, 04:11 PM   #3
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354

Original Poster
Blog Entries: 2

Rep: Reputation: 34
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

Code:
const struct sigaction stAct={.sa_sigaction=sigact1,.sa_mask=0,.sa_flags=SA_SIGINFO,.sa_restorer=NULL};
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?

Thanks,
Sarin
 
Old 12-26-2006, 04:42 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Quote:
Originally Posted by sarin
I changed the initialization to

Code:
const struct sigaction stAct={.sa_sigaction=sigact1,.sa_mask=0,.sa_flags=SA_SIGINFO,.sa_restorer=NULL};
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.
 
Old 12-27-2006, 09:48 AM   #5
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354

Original Poster
Blog Entries: 2

Rep: Reputation: 34
Thanks. It is clear to me now.
 
Old 12-27-2006, 10:29 AM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sigaction() - how to create custom signals? rose_bud4201 Programming 22 08-20-2015 09:22 AM
RS232 programming (sigaction problem) lucky6969b Linux - Networking 2 04-05-2008 02:06 PM
Signal() sigaction() and IO interrupt Linuxprocess Programming 2 09-20-2006 09:38 AM
send arguments to a sigaction handler erwinfletch Programming 1 05-01-2006 07:19 PM
Should I use signal() or sigaction()? .... grupoapunte Programming 2 06-04-2005 11:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration