Recent

Author Topic: [Solved] Handle SIGTERM in Lazarus app  (Read 2847 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2681
    • UVviewsoft
[Solved] Handle SIGTERM in Lazarus app
« on: April 03, 2025, 10:21:41 am »
If I run "killall -SIGTERM cudatext", my text editor shuts down badly. TForm.OnClose is not called, it seems. So app cannot properly finalize and save history.

Why OnClose is not called on SIGTERM?
Can I call it on SIGTERM?
What other signal should I send?
« Last Edit: April 03, 2025, 06:25:54 pm by AlexTP »

Zvoni

  • Hero Member
  • *****
  • Posts: 3307
Re: killall with GUI app
« Reply #1 on: April 03, 2025, 10:40:09 am »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: killall with GUI app
« Reply #2 on: April 03, 2025, 10:53:57 am »
hmmmm.... --> https://www.freepascal.org/docs-html/rtl/baseunix/fpsigaction.html

I agree. Them as don't ask, don't get.

However: /why/ are you using SIGTERM? SIGINT (by default raised by ^C) is far more common for this sort of thing.

See https://www.computerhope.com/unix/signals.htm, in particular the section "Sending signals from the keyboard", where ^C handling depends on terminal setup.

Also see the example handler I posted at https://forum.lazarus.freepascal.org/index.php/topic,67426.msg518817.html#msg518817

MarkMLl
« Last Edit: April 03, 2025, 11:04:14 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlexTP

  • Hero Member
  • *****
  • Posts: 2681
    • UVviewsoft
Re: killall with GUI app
« Reply #3 on: April 03, 2025, 01:18:46 pm »
Thanks.
I also asked this from AI and got this example:

[DELETED because code was wrong]
« Last Edit: April 04, 2025, 10:22:36 am by AlexTP »

Fred vS

  • Hero Member
  • *****
  • Posts: 3826
    • StrumPract is the musicians best friend
Re: killall with GUI app
« Reply #4 on: April 03, 2025, 01:29:39 pm »
Hello Alex.

If you are asking to kill a app in a Wayland session, afaik Wayland does not allow it, one killed app = all the Wayland session killed.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Zvoni

  • Hero Member
  • *****
  • Posts: 3307
Re: killall with GUI app
« Reply #5 on: April 03, 2025, 02:05:55 pm »
Thanks.
I also asked this from AI and got this example:

Code: Pascal  [Select][+][-]
  1. program GracefulShutdownExample;
  2.  
  3. uses
  4.   SysUtils, Classes, Signal;
  5.  
  6. procedure HandleSignal(Sender: TObject);
  7. begin
  8.   Writeln('Received termination signal. Shutting down gracefully...');
  9.   // Perform cleanup tasks here
  10.   // For example, save data, close files, release resources, etc.
  11.  
  12.   // Exit the application
  13.   Halt;
  14. end;
  15.  
  16. var
  17.   SignalHandler: TSignalHandler;
  18.  
  19. begin
  20.   // Create a signal handler for SIGTERM and SIGINT
  21.   SignalHandler := TSignalHandler.Create(nil);
  22.   try
  23.     SignalHandler.Signals := [sigTERM, sigINT];
  24.     SignalHandler.OnSignal := HandleSignal;
  25.  
  26.     Writeln('Application is running. Press Ctrl+C to terminate.');
  27.     // Simulate application running
  28.     while True do
  29.     begin
  30.       Sleep(1000);
  31.     end;
  32.   finally
  33.     SignalHandler.Free;
  34.   end;
  35. end.
  36.  

Now I don't know where is the unit Signal?
I got reply:

The Signal unit in Free Pascal/Lazarus is part of the Free Pascal RTL (Run-Time Library) and is designed to provide signal handling capabilities. The unit is located in the rtl/win32 directory for Windows, and it aims to offer similar signal handling functionalities across different platforms[..]

Maybe because the unit-name is in fact "signals"...

On my machine at:
C:\lazarus\fpc\3.2.2\source\rtl\win32

Code: Pascal  [Select][+][-]
  1. {
  2.     This file is part of the Free Pascal run time library.
  3.     This unit implements unix like signal handling for win32
  4.     Copyright (c) 1999-2006 by the Free Pascal development team.
  5.  
  6.     See the file COPYING.FPC, included in this distribution,
  7.     for details about the copyright.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.  
  13.  **********************************************************************}
  14.  
  15. unit signals;
  16.  
  17. interface
  18.  
  19. {$PACKRECORDS C}
  20.  
  21.   { Signals }
  22.   const
  23.     SIGABRT   = 288;
  24.     SIGFPE    = 289;
  25.     SIGILL    = 290;
  26.     SIGSEGV   = 291;
  27.     SIGTERM   = 292;
  28.     SIGALRM   = 293;
  29.     SIGHUP    = 294;
  30.     SIGINT    = 295;
  31.     SIGKILL   = 296;
  32.     SIGPIPE   = 297;
  33.     SIGQUIT   = 298;
  34.     SIGUSR1   = 299;
  35.     SIGUSR2   = 300;
  36.     SIGNOFP   = 301;
  37.     SIGTRAP   = 302;
  38.     SIGTIMR   = 303;    { Internal for setitimer (SIGALRM, SIGPROF) }
  39.     SIGPROF   = 304;
  40.     SIGMAX    = 320;
  41.  
  42.     SIG_BLOCK   = 1;
  43.     SIG_SETMASK = 2;
  44.     SIG_UNBLOCK = 3;
  45.  
  46.   function SIG_DFL( x: longint) : longint; cdecl;
  47.  
  48.   function SIG_ERR( x: longint) : longint; cdecl;
  49.  
  50.   function SIG_IGN( x: longint) : longint; cdecl;
  51.  
  52.   type
  53.  
  54.     SignalHandler  = function (v : longint) : longint;cdecl;
  55.  
  56.     PSignalHandler = ^SignalHandler; { to be compatible with linux.pp }
  57.  
  58.   function signal(sig : longint;func : SignalHandler) : SignalHandler;
  59.  
  60.   const
  61.  
  62.      EXCEPTION_MAXIMUM_PARAMETERS = 15;
  63.  
  64.   type
  65. //and so on....
« Last Edit: April 03, 2025, 02:07:27 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

AlexTP

  • Hero Member
  • *****
  • Posts: 2681
    • UVviewsoft
Re: killall with GUI app
« Reply #6 on: April 03, 2025, 02:57:50 pm »
I guess the French AI gave false info.

1. Unit it not 'Signal' but 'SignalS'.
2. In the unit, I don't see TSignalHandler type which is shown in AI example.

Fred vS

  • Hero Member
  • *****
  • Posts: 3826
    • StrumPract is the musicians best friend
Re: killall with GUI app
« Reply #7 on: April 03, 2025, 03:12:01 pm »
I guess the French AI gave false info.

1. Unit it not 'Signal' but 'SignalS'.
2. In the unit, I don't see TSignalHandler type which is shown in AI example.

Could be a nice game to test the different AI, what did you ask to the AI?
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Zvoni

  • Hero Member
  • *****
  • Posts: 3307
Re: killall with GUI app
« Reply #8 on: April 03, 2025, 03:17:14 pm »
I guess the French AI gave false info.

2. In the unit, I don't see TSignalHandler type which is shown in AI example.
Ignore what AI said, und use the types as shown in the Unit?

Why are we discussing such things?!?!?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Fred vS

  • Hero Member
  • *****
  • Posts: 3826
    • StrumPract is the musicians best friend
Re: killall with GUI app
« Reply #9 on: April 03, 2025, 03:22:22 pm »
Why are we discussing such things?!?!?
Perhaps to show that AI still needs the Lazarus forum to be usable?  ;)
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: killall with GUI app
« Reply #10 on: April 03, 2025, 04:00:14 pm »
Why are we discussing such things?!?!?
Because some people (still) do not realize that all AI wants from a user is engagement, and one that lasts as long and intensive as possible (for reasons that are well-known)
Today is tomorrow's yesterday.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: killall with GUI app
« Reply #11 on: April 03, 2025, 05:23:55 pm »
I must say though that if somebody prefers to use an "AI" to find a half-correct answer to his question, I'm left wondering why I bother to try answering it and pointing to code that I know works.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlexTP

  • Hero Member
  • *****
  • Posts: 2681
    • UVviewsoft
Re: killall with GUI app
« Reply #12 on: April 03, 2025, 06:25:14 pm »
AI was mentioned just to show ppl, that Mistral AI is bad for Lazarus.
I took forum's answer, about using fpSigaction.
Mistral AI was helpful even WITH FORUM, it gave (in 2nd answer) ok example with fpSigaction.

Solved.

Quote
Also see the example handler I posted at https://forum.lazarus.freepascal.org/index.php/topic,67426.msg518817.html#msg518817
Mark, that example was less useful for me. AI's example (2nd one) was better and shorter 3x times.
« Last Edit: April 03, 2025, 06:29:39 pm by AlexTP »

AlexTP

  • Hero Member
  • *****
  • Posts: 2681
    • UVviewsoft
Re: [Solved] Handle SIGTERM in Lazarus app
« Reply #13 on: April 03, 2025, 06:34:59 pm »
I must post my final solved code:
Code: Pascal  [Select][+][-]
  1. //1. main form's unit
  2. uses BaseUnix;
  3.  
  4. {$ifndef windows}
  5. procedure DoShutdown(Sig: Longint; Info: PSigInfo; Context: PSigContext); cdecl;
  6. begin
  7.   Writeln('CudaText got signal: '+IntToStr(Sig));
  8.   if Assigned(fmMain) then
  9.     fmMain.Close;
  10. end;
  11.  
  12. procedure RegisterSignalHandler;
  13. var
  14.   New, Old: SigactionRec;
  15. begin
  16.   New.sa_handler:= @DoShutdown;
  17.   FPSigaction(SIGTERM, @New, @Old);
  18.   FPSigaction(SIGINT, @New, @Old);
  19. end;
  20. {$endif}
  21.  
  22.  
  23. //2. LPR file
  24.  
  25.   {$IFDEF WINDOWS}
  26.   //..
  27.   {$ELSE}
  28.   RegisterSignalHandler;
  29.   {$IFEND}
  30.  
« Last Edit: April 03, 2025, 06:36:43 pm by AlexTP »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6355
  • Compiler Developer
Re: killall with GUI app
« Reply #14 on: April 03, 2025, 08:59:17 pm »
If you are asking to kill a app in a Wayland session, afaik Wayland does not allow it, one killed app = all the Wayland session killed.

This is wrong. One can KILL or TERM a Wayland application without issues for the remainder of the system. Would be insane if this wouldn't work, because especially SIGKILL can't be intercepted.

However: /why/ are you using SIGTERM? SIGINT (by default raised by ^C) is far more common for this sort of thing.

Because SIGTERM is the default signal of kill or killall maybe? And users might use that to terminate AlexTP's application if need be.

 

TinyPortal © 2005-2018