4

On a Linux system, the signal -KILLTERM sends a signal that allows applications to safely shut down. These questions might be a little theoretical, but I want to understand them.

  1. When a system sends a terminate signal, where is it sent?

  2. What allows time for the process or, application, to 'safely' terminate?

  3. Is there a child process, or something similar, that runs in the background of an application that looks for this signal?

These questions stem from the Linux watchdog, in reading the man page I saw that the process of the watchdog is to first send a terminate signal to a given PID, and then a KILL -9 signal to force it. I want to be able to utilize the safety built in to the watchdog.

Biffen
  • 6,249
  • 6
  • 28
  • 36
user5520530
  • 43
  • 1
  • 3
  • 2
    Signals dont kill processes, people (and their processes) send signals to kill processes. – joop Nov 03 '15 at 15:21
  • 2
    The TERM signal can be caught by a process's signal handler; KILL cannot. Therefore the watchdog tries sending SIGTERM first and waits for some time, so any process that uses a custom SIGTERM handler has time to execute it. "Safe" termination is something built into an application by the designer. – Hellmar Becker Nov 03 '15 at 15:35

1 Answers1

2

See this code,

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

void cleanUp(){ // Do whatever you want here
    printf("Safely terminating \n");
}

void hand(int sig){ // called when you are sent SIGTERM
    /*
    Here you can safely terminate..
    */
    atexit(cleanUp); // call cleanUp at exit.
    exit(0);
}

int main(){
    signal(SIGTERM, hand); //Assign function to be called on SIGTERM
    /*
    Your code goes here.
    I have put an infinite loop for demonstration.
    */
    printf("Started execution..\n");
    for(;;);
}

This shows how a function can be assigned to be called when a signal is delivered to your application.

To deliver the signal SIGTERM to this code, do this,

kill -SIGTERM <pid>

Here, <pid> id the process id of your running program.

Ghazanfar
  • 1,419
  • 13
  • 21
  • 1
    Note you should not use printf() and friends inside a signal handler. printf() is not signal-safe. – joop Nov 03 '15 at 15:47
  • @joop I agree. This was just to demonstrate how all this works as OP asked. – Ghazanfar Nov 03 '15 at 15:48
  • Well it is a bad demonstration, since it demonstrates bad practice. – joop Nov 03 '15 at 15:51
  • I've had it drilled into my head that one should only set a flag inside a signal handler and have the work loop check the flag, for the reason joop mentioned. – converter42 Nov 03 '15 at 15:52
  • @joop Okay, I'll change it. – Ghazanfar Nov 03 '15 at 16:04
  • @joop Is this better ? – Ghazanfar Nov 03 '15 at 16:14
  • I don't know; I can imagine that atexit() isn't signal safe either. (it *could* use a linked list + malloc internally) – joop Nov 03 '15 at 16:24
  • @joop Thought you might find this interesting, http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler – Ghazanfar Nov 03 '15 at 16:50
  • @MohammadGhazanfar thank you. I have a follow up question question now. After playing around with the signal function, I see that it interrupts the application no matter what in app process is being executed. Is there a method of blocking signals while handling data, etc, without being redirected to the signal handler? Essentially, it waits until safe to call the signal handle. I played with the idea of creating a user defined signal, and then blocking -SIGTERM. but this seems over complicated, and I wonder if there is a simpler way? – user5520530 Nov 04 '15 at 16:27