Return Code When Os Kills Your Process

Return code when OS kills your process

A process' return status (as returned by wait, waitpid and system) contains more or less the following:

  • Exit code, only applies if process terminated normally
  • whether normal/abnormal termination occured
  • Termination signal, only applies if process was terminated by a signal

The exit code is utterly meaningless if your process was killed by the OOM killer (which will apparently send you a SIGKILL signal)

for more information, see the man page for the wait command.

Return code when OOM killer kills a process

The Linux OOM killer works by sending SIGKILL. If your process is killed by the OOM it's fishy that WIFEXITED returns 1.

TLPI

To kill the selected process, the OOM killer delivers a SIGKILL
signal.

So you should be able to test this using:

if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGKILL)
printf("Killed by SIGKILL\n");
}

Process Exit Code When Process is Killed Forcibly

In general, a process is terminated using TerminateProcess. The exit code is passed as a parameter to this method.

In the case of the task manager, the exit code is set to 1, but I don't know if it's documented anywhere.

Linux: get the exit code from the kill command

The exit code you get is for the kill command itself. 0 means it succeeded, i.e. the other process got the signal you sent it. kill just doesn't report the exit status for the process, since it can't even be sure the other process exited as a result of the signal it sent.

Retrieve a value from subprocess after kill()

While not an elegant answer, using process.send_signal(signal.SIGUSR1) worked (as mentioned by @cg909). In the stop method we have:

def stop_keylogger():
process.send_signal(signal.SIGUSR1)
process.wait()
return process.stderr.read()

Then in the key logger to handle the signal we have:

def exit_and_return_counter(*args):
sys.stdout.write('%s'%counter)
exit()

if __name__ == '__main__':
signal.signal(signal.SIGUSR1, exit_and_return_counter)
try:
while 1:
main_loop()
except KeyboardInterrupt:
sys.exit()


Related Topics



Leave a reply



Submit