Skip to content

Instantly share code, notes, and snippets.

@jul
Created November 2, 2012 00:58
Show Gist options
  • Select an option

  • Save jul/3997970 to your computer and use it in GitHub Desktop.

Select an option

Save jul/3997970 to your computer and use it in GitHub Desktop.
grrr
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <bits/signum.h>
#include <string.h>
int pipefd[2];
#define RPIPE 0
#define WPIPE 1
void send_sig(int signal) {
write(pipefd[WPIPE], &signal, 1);
}
void pr_pending(sigset_t * pendmask) {
int signo=_NSIG;
while(--signo) {
if( sigismember(pendmask, signo)) {
printf("%s is pending\n", _sys_siglist[signo]);
}
}
}
typedef struct {
int signo;
int offset;
} sig2off;
int main(void) {
sigset_t mask, oldmask, pendmask,zmask;
int signal_received=0;
struct sigaction sa,old_sa;
int sig2off[_NSIG];
int i = 0;
int val=0;
for(i=0;i<_NSIG;i++){
sig2off[i]=-1;
}
sig2off[SIGALRM]=0;
sig2off[SIGUSR2]=1;
sig2off[SIGINT]=2;
sa.sa_handler=&send_sig;
sa.sa_flags |= SA_INTERRUPT;
//sa.sa_flags |= SA_RESTART;
sigemptyset(&mask);
sigemptyset(&oldmask);
sigemptyset(&zmask);
sigaddset(&mask,SIGUSR1);
sigaddset(&mask,SIGHUP);
for(i=0;i<_NSIG;i++){
if(sig2off[i]>=0){
sigaddset(&mask,sig2off[i]);
}
}
pipe(pipefd);
fcntl(pipefd[RPIPE],F_SETFD, O_NONBLOCK);
fcntl(pipefd[WPIPE],F_SETFD, O_NONBLOCK);
i=SIGHUP;
printf("installing signal %d(%s)\n",i, strsignal(i));
sigaction(SIGHUP, &sa,NULL);//,&old_sa);
i=SIGUSR1;
printf("installing signal %d(%s)\n",i, strsignal(i));
sigaction(SIGUSR1,&sa,NULL);// &old_sa);
for(i=0;i<_NSIG;i++){
if(sig2off[i]>=0){
sigaction(i, &sa,NULL);// &old_sa);
printf("installing signal %d(%s)\n",i, strsignal(i));
}
}
//for(;;) {
while(pause()){
//sigsuspend(&zmask);
//pr_pending(&pendmask);
sigprocmask (SIG_BLOCK, &mask,NULL);// &oldmask);
printf("%s","processing\0");
char buf[1];
while(read(pipefd[RPIPE],buf,1) !=0 ){//|| errno != EINTR){
int signo=(int)*buf;
printf("[%d]received sig%d==%d\n",++signal_received, (int )*buf,signo);
printf("E:%d\n", errno);
if(SIGHUP==signo){
printf("ACK val=%d\n", val);
} else if(SIGUSR1==signo){
val=0;
printf("CLR val=%d\n",val);
}else{
val|=1<<sig2off[signo];
printf("SFT shift %d bit val is %d\n",sig2off[signo], val);
}
}
//pr_pending(&pendmask);
sigprocmask (SIG_UNBLOCK, &mask, NULL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment