Hi, as I am getting deeper and deeper into Free Pascal, and looking on how to perform tasks I normally perform in Python, signal handling is one thing that seems to be less developed in Free Pascal, at least when looking through the available documentation. I do not see a cross-platform method to trap signals. Trapping signals is rather critical when it comes to daemon processes, such as servers and services, which are the main types of programs I write. In Python, I normally trap the SIGTERM, so when my program's PID gets a
kill 4372, where the number there is the PID, it can gracefully shutdown, close any sockets, close database connections, flush log files, and finally stop itself. This is how web servers like Apache and nginx handle their shutdown routines, as it does not run in the foreground, and there usually isn't a way to stop the service otherwise.
import signal
def handler(signum, frame):
raise KeyboardInterrupt # Basically sends a Ctrl-C, so the shutdown routine is the same if SIGTERM or if Ctrl-C is pressed when it runs in the foreground.
signal.signal(signal.SIGTERM, handler)
The above Python code allows for KeyboardInterrupt exception to be caught during the main program event loop when a SIGTERM is sent to the process. It also allows the program to run in the foreground, and catch Ctrl-C during testing and performs the exact same process cleanup routines.
The most I found for Free Pascal is in the
baseunix unit, which I do not believe will be available on Windows:
https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.htmlIs there a cross-platform signal handler like the one which exists in Python available in Free Pascal? I'm sure there might be something in the Wiki, if it were currently available.
UPDATE: Lost my last update as my cookie expired on the forums, not really wanting to rewrite 2 paragraphs again, so I'll keep this short so my cookie doesn't expire again... I don't believe the same signal system is available on Windows, so I don't think cross-platform signals are possible. Also, since I write daemons on Linux, this should be fine. Is there any reason why
fpSignal is deprecated? This functions similar to the call in Python, and is less tedious than needing to create the C structure manually as the example shows. Otherwise, the example code I wrote seems to work fine when testing SIGINT, and SIGTERM.