0

I met one issue of signal handling under Linux, my target is to let process ignore SIGTERM signal. But sometimes, process still exited, the probability of this issue will be 1/60.

Fake code of my application:

static int g_count_sig_old = 0;

static volatile int g_count_sig = 0;

void _signalhandler(){

   g_count_sig ++;

   printf(...); // Maybe not safe, just for debugging

   myTrace(...); // Write log to file1 is not signal safe, but just for debugging.

}

main(){

  sigaction(...) // Register signal handler for SIGTERM

  while(1){

    sleep(1000);  // wait one second

    myNewTrace(...); // Output value of g_count_sig to file2

    if( g_count_sig != g_count_sig_old ) {

       g_count_sig_old = g_count_sig;

       printf(...); // output value of g_count_sig

       myNewTrace(...); // Output value of g_count_sig to file2 
    } 

  }  
}

I suppose this application will not quit when receiving signal SIGTERM, but actual testing result didn't match my design. Some times, the process still exit after receiving signal SIGTERM. And I confirm the process received SIGTERM signal when issue occurred, I can observe console output and trace file.

So I feel puzzled, why does this application exit even if ignoring SIGTERM? I am not sure how to position the cause of this issue, or it is reasonable symptom under Linux.

Hope to get your help. Thanks!

  • 3
    Your code is inconsistent with your description -- you say you're ignoring SIGTERM, but the comment in your code says you're registering a signal handler for it. You don't show your actual code so its impossible to say what is actually happening. What is the exit code/status? – Chris Dodd Jan 15 '16 at 05:19
  • 1
    On Linux, `sleep(1000)` sleeps for a thousand seconds, or rather over a quarter of an hour. – Jonathan Leffler Jan 15 '16 at 06:23
  • I think (remember of what I learned about signals), for security issues, SIGTERM cannot be disabled. You should try to desactivate SIGKILL too. – Pierre Jan 15 '16 at 12:28

0 Answers0