Recent

Author Topic: Cross-platform signal handling  (Read 356 times)

kveroneau

  • Jr. Member
  • **
  • Posts: 72
Cross-platform signal handling
« on: June 07, 2019, 10:26:07 pm »
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.

Code: Python  [Select]
  1. import signal
  2.  
  3. def handler(signum, frame):
  4.   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.
  5. signal.signal(signal.SIGTERM, handler)
  6.  
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.html

Is 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.
« Last Edit: June 07, 2019, 11:24:48 pm by kveroneau »

mr-highball

  • New member
  • *
  • Posts: 45
Re: Cross-platform signal handling
« Reply #1 on: June 08, 2019, 12:09:29 am »
So, normally the fpc/delphi way of doing things is to abstract things like signal handling away from the caller with callbacks/events/virtual methods defined in some object. I'm away from a pc with fpc installed atm, so I can't inspect src right now, but if you take a look at the below links you'll see an example of this:

http://wiki.freepascal.org/Daemons_and_Services (Daemon methods)
and
https://forum.lazarus.freepascal.org/index.php/topic,22983.msg152303.html#msg152303 (older example with some discussion)
 
Because everything is open source, you could create a new service application (use the template project as a start) and could follow the paper trail to the base implementation to see how the authors handled signals on the different platforms.
-Highball

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7230
Re: Cross-platform signal handling
« Reply #2 on: June 08, 2019, 12:38:26 am »
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.html

Is 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.

Signals are an Unix concept. Some half ported software emulates them on Windows, but Free Pascal/Lazarus applications are native applications on Windows and don't run on top of an Unix/posix emulation.

Quote
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.

Signal as an kernel operation has been replaced by sigaction decades ago. While there is still a signal(3) call, Unix/Posix documentation recommends sigaction for new applications. IOW we follow official policy.

« Last Edit: June 08, 2019, 12:40:40 am by marcov »

kveroneau

  • Jr. Member
  • **
  • Posts: 72
Re: Cross-platform signal handling
« Reply #3 on: June 11, 2019, 02:34:22 am »
Thank you for those replies, I figured that signals was mainly a UNIX concept after some research.  Since I plan to only develop said services and daemons for UNIX-like systems, such as Linux, using signals from the baseunix unit shouldn't be a problem for what I am trying to do.

For others interested in Unix signals, and for a way to easily handle them within your program without needing to manage the handler and such yourself, I wrote a Pascal Unit which your program can just use, and it will provide a bunch of boolean variables you can monitor from an event loop to determine if a signal was sent to your process or not.  You can find the Units full source code here:  http://tech406.com/kdocs/signals.xml Click on the Signal handler unit Hyperlink at the top of the document to jump to the Unit code itself.  I hereby provide this source under a BSD license, so you can freely use it in your own Free Pascal programs without worry if you need to check for signals in your code.  Once the Wiki is back, feel free to provide a copy of this there as well.  I also wrote a decent write-up on the concept behind signals, and what they are mainly used for on UNIX-like systems for those unfamiliar with what signals are used for.