2

Its a sample program to check if sigprocmask actually blocks the SIGALRM signal associated with my timer. Turns out it doesn't. My handler gets called even when the signal is BLOCKED. Would really appreciate if someone can point out why that is. Here is the code:

#include <signal.h> 
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

timer_t timer_ID;
static void timer_handler(int signo, siginfo_t *sig_info, void *sig_ucontext){  
    if(sig_info->si_value.sival_ptr != &timer_ID){
        printf("This signal is stray and didnt come from the timer we setup. Exiting the program!\n");
        exit(1);
     }
     else{
        printf("Caught signal %d from timer\n", signo);
     }
     return;
}//func end

void main(void)
{   
    struct sigevent sev;         // for creating the timer  
    sigset_t sigmask;   
    struct sigaction sa = {0};          
    struct itimerspec its;  
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 500000; 

    its.it_value.tv_sec = its.it_interval.tv_sec;
    its.it_value.tv_nsec = its.it_interval.tv_nsec;

    sa.sa_flags = SA_SIGINFO | SA_RESTART; 
    sa.sa_sigaction = timer_handler;

    if (sigaction(SIGALRM, &sa, NULL) == -1){
       fprintf(stderr, "sigaction register error\n");
       exit(1);
    }   
    //BLOCK SIG
    sigemptyset (&sigmask);
    sigaddset (&sigmask, SIGALRM);
    if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {
        fprintf (stderr, "Failed to set signal mask!\n");
        exit (1);
    }   

    //CREATE TIMER
    sev.sigev_signo = SIGALRM; 
    sev.sigev_notify = SIGEV_SIGNAL; 
    sev.sigev_value.sival_ptr = &timer_ID; 

    if(timer_create(CLOCK_MONOTONIC, &sev, &timer_ID) != 0){ 
        printf("Create timer error!\n");        
        exit (1);
    } 
    // START TIMER
    if(timer_settime(timer_ID, 0, &its, NULL) !=0){
        printf("ERROR setting the timer!!!!\n");
        exit(1);
    }

    while(1);  //signal is blocked so should never go to handler
}//main end
selbie
  • 100,020
  • 15
  • 103
  • 173
emhr23
  • 31
  • 4
  • Have you checked the set of blocked signals after the calls to `timer_create()` and `timer_settime()`? Maybe they're unblocking it. – Ken Thomases May 06 '18 at 03:23
  • 2
    After I fixed your compile issues (like a missing declaration for `sa`), I can't repro. Your program just runs silently after being started. – selbie May 06 '18 at 03:42
  • Apologies for not giving all the #includes earlier. How come you cannot reproduce the error? Does it have anything to do with the POSIX version? Because mine just keeps calling the handler once inside the while. Also I did check the signal mask by inserting assert(sigismember(&sigmask, SIGALRM ) == 1) before and after both timer_create() and timer_settime() functions. It passes. I even printed out all the members of the sigmask. And SIGALRM is a member of the mask. But the code somehow keeps dropping into the handler. – emhr23 May 07 '18 at 05:07
  • I don't have any other handlers or timers setup in my application. This is the whole code. Working with linux kernel 3.10.0 on an x86_64 architecture – emhr23 May 07 '18 at 05:08

0 Answers0