PDA

View Full Version : Any way to grab the 'kill'?



hakermania
September 9th, 2011, 06:59 PM
Hello.

Is there any way to grab the 'kill' signal from inside the program so as to do an action before quitting just like there's a way grabbing the Ctrl+C?

I am writing in C++ and using QtCreator, thanks in advance for any answers!):P

karlson
September 9th, 2011, 07:23 PM
Hello.

Is there any way to grab the 'kill' signal from inside the program so as to do an action before quitting just like there's a way grabbing the Ctrl+C?

I am writing in C++ and using QtCreator, thanks in advance for any answers!):P

If I recall correctly that is the only one that you can't catch.

lensman3
September 9th, 2011, 07:40 PM
Look at the signal() function. It should allow what you want. You can set up your own signal handlers.

kill -9 is the only one you can't catch in Linux/Unix. The others you can catch.

hakermania
September 9th, 2011, 08:07 PM
Look at the signal() function. It should allow what you want. You can set up your own signal handlers.

kill -9 is the only one you can't catch in Linux/Unix. The others you can catch.

Hey, thanks for the info, I'd like to catch the signal that the system sends to application when going e.g. for shutdown...
Any idea what type of signal it is?

karlson
September 9th, 2011, 08:20 PM
Hey, thanks for the info, I'd like to catch the signal that the system sends to application when going e.g. for shutdown...
Any idea what type of signal it is?

It sends SIGTERM (15 last I checked)

hakermania
September 9th, 2011, 08:26 PM
It sends SIGTERM (15 last I checked)
Thanks!

cgroza
September 9th, 2011, 09:29 PM
You may only catch SIGTERM. SIGKILL will terminate your program with cold blood.
As far as I know, "killall" and "kill" send SIGTERM by default.

dwhitney67
September 9th, 2011, 10:53 PM
You may only catch SIGTERM.

As indicated earlier, a program may catch any signal it pleases, with the exception if SIGKILL (9).

dwhitney67
September 9th, 2011, 10:57 PM
Look at the signal() function.

signal() has been deprecated; sigaction() is the preferred function to use.

For the OP's benefit, to capture SIGINT and SIGTERM:


#include <csignal>

class ProgramStatus
{
public:
ProgramStatus()
{
struct sigaction act;
memset(&act, 0, sizeof(act));

act.sa_handler = sigHandler;
sigaction(SIGINT, &act, 0);
sigaction(SIGTERM, &act, 0);
}

bool isDone() { return done; }

private:
static bool done;

static void sigHandler(int signo)
{
if (signo == SIGINT || signo == SIGTERM)
{
done = true;
}
}
};

bool ProgramStatus::done = false;

Lux Perpetua
September 10th, 2011, 03:28 AM
Look at the signal() function. It should allow what you want. You can set up your own signal handlers.

kill -9 is the only one you can't catch in Linux/Unix. The others you can catch.I don't think you can catch SIGSTOP, either, can you?


Thanks!Just keep in mind that your process will be killed with SIGKILL eventually anyway if it takes too long saying its goodbyes. ;-)

dwhitney67
September 10th, 2011, 11:06 AM
I don't think you can catch SIGSTOP, either, can you?

Upon examination of the man-pages for sigaction() and signal(), it seems that you are correct.

It should be noted that the application will not terminate when a SIGSTOP is sent; it will merely be suspended. A SIGCONT can be used to resume the process, or a SIGKILL can be issued to kill it. A SIGTERM, and for that fact, any other signal other than SIGKILL, is ignored when the process is suspended, but will be received when/if the process is resumed.