1

I'm working on a server-like program in xcode, and I want it to shut down gracefully between debug runs. Basically the following line is not working

std::signal(SIGKILL, [](int){
    printf("got sigkill here!\n"); 
    //shut-down code here
});

I've tried trapping other signals, but so far SIGINT, SIGTERM and SIGABRT have not worked. How is xcode terminating the program, if it prints

Program ended with exit code: 9

to the console?

EDIT Apparently SIGKILL can not be caught, see this wikipedia entry.

In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal

Gleno
  • 16,621
  • 12
  • 64
  • 85
  • 1
    What do you mean by *is not working*? – Filipe Gonçalves Jan 24 '14 at 18:03
  • I don't get "got sigkill here" printed to the console before I get "Program ended with exit code: 9". I do in terminal, which sends SIGINT on control+c, so I assumed I should be getting some output in the xcode console as well. – Gleno Jan 24 '14 at 18:07

1 Answers1

3

You can't catch SIGKILL. When a process receives SIGKILL, it immediately terminates. This is used to kill buggy processes that won't respond or ignore the regular SIGINT. If you could catch SIGKILL, and possibly ignore it, then there would be no way to kill a buggy process apart from rebooting the machine.

Also, note that you're calling printf() in a signal handler, which is not allowed. You can't expect this to work.

You can have a look at signal-safe functions in signal manpage: http://man7.org/linux/man-pages/man7/signal.7.html - printf is not part of it, so all bets are off.

From the manpage:

A signal handler function must be very careful, since processing elsewhere may be interrupted at some arbitrary point in the execution of the program. POSIX has the concept of "safe function". If a signal interrupts the execution of an unsafe function, and handler calls an unsafe function, then the behavior of the program is undefined.

See this question for safe alternatives: Print int from signal handler using write or async-safe functions

Community
  • 1
  • 1
Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • Hm, I see. Well, I've perused the manpage link, and it says that open/close are safe to call. Replacing my printf with close(open ("test", O_WRONLY | O_CREAT)); doesn't create the file if i press the stop button in xcode, although they are supposed to be safe to call. I'm guessing that maybe xcode is not signalling sigkill.. – Gleno Jan 24 '14 at 18:23
  • Apparently you can't catch sigkill. From wikipedia: "In contrast to SIGTERM and SIGINT, this signal cannot be caught or ignored, and the receiving process cannot perform any clean-up upon receiving this signal" – Gleno Jan 24 '14 at 18:29
  • @Gleno Ha! Of course! I didn't remember about that. You can't catch `SIGKILL`; this is the signal sent to force process shutdown (in Linux, with `kill -9 `. I'll update my answer for future reference. – Filipe Gonçalves Jan 24 '14 at 22:15