Recent

Author Topic: Test Hook To OS Signals  (Read 1571 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1186
  • Professional amateur ;-P
Test Hook To OS Signals
« on: March 09, 2025, 10:39:48 pm »
Hey Y'all,

Just to let all know about my newest entry to the Test Star list of projects: https://github.com/gcarreno/TestHookToOSSignals

Nothing special. Just a basic CLI application that has Windows and Linux (and almost probably macOS, since it's still *nix) hooks to termination and/or kill signals.

Let me know if it needs either better documentation or better comments.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1360
Re: Test Hook To OS Signals
« Reply #1 on: March 10, 2025, 02:50:09 am »
Congrats  :)
Unfortunately GitHub is difficult for me to use but if you want to show some code here I’d love to look at it.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Khrys

  • Full Member
  • ***
  • Posts: 215
Re: Test Hook To OS Signals
« Reply #2 on: March 10, 2025, 06:48:30 am »
Unfortunately GitHub is difficult for me to use but if you want to show some code here I’d love to look at it.

If you refuse to run non-free Javascript like Richard Stallman, you could just clone the repository without using a browser at all:  git clone https://github.com/gcarreno/TestHookToOSSignals.git

MarkMLl

  • Hero Member
  • *****
  • Posts: 8377
Re: Test Hook To OS Signals
« Reply #3 on: March 10, 2025, 08:52:08 am »
Just to let all know about my newest entry to the Test Star list of projects: https://github.com/gcarreno/TestHookToOSSignals

Nothing special. Just a basic CLI application that has Windows and Linux (and almost probably macOS, since it's still *nix) hooks to termination and/or kill signals.

There's a great deal more that can be usefully captured, to avoid unecessary C&P see thread at https://forum.lazarus.freepascal.org/index.php/topic,67426.msg518817.html#msg518817 . Note kill's -q option.

There's also stuff relevant to WINCH in Lazarus's debugger/pseudoterminaldlg.pp and debugger/test/watchconsolesize.pas, hence the size information at the bottom of the IDE's console window.

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

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1360
Re: Test Hook To OS Signals
« Reply #4 on: March 10, 2025, 01:08:04 pm »
Unfortunately GitHub is difficult for me to use but if you want to show some code here I’d love to look at it.

If you refuse to run non-free Javascript like Richard Stallman, you could just clone the repository without using a browser at all:  git clone https://github.com/gcarreno/TestHookToOSSignals.git
thanks for the suggestion. I guess that’s one approach  :)
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

MarkMLl

  • Hero Member
  • *****
  • Posts: 8377
Re: Test Hook To OS Signals
« Reply #5 on: March 10, 2025, 01:59:30 pm »
thanks for the suggestion. I guess that’s one approach  :)

At present a browser with Javascript etc. disabled can see Github: it's not perfect and you can't e.g. generate a .zip of an entire project, but individual files can be read.

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

dbannon

  • Hero Member
  • *****
  • Posts: 3318
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Test Hook To OS Signals
« Reply #6 on: March 11, 2025, 04:13:10 am »
If you refuse to run non-free Javascript like Richard Stallman, you could just clone the repository without using a browser at all:  git clone https://github.com/gcarreno/TestHookToOSSignals.git

I am not all that sure that Richard Stallman is an ideal example to us all.
https://drewdevault.com/2024/09/25/2024-09-25-Neurodivergence-and-accountability-in-free-software.html
(a 'read' I found quite disappointing)

But your advise is excellent, Joanna could just clone the repo and leave no trace of her visit there doing so.

Davo

« Last Edit: March 11, 2025, 04:14:52 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1186
  • Professional amateur ;-P
Re: Test Hook To OS Signals
« Reply #7 on: March 11, 2025, 04:42:23 am »
Hey MarkMLl,

Just to let all know about my newest entry to the Test Star list of projects: https://github.com/gcarreno/TestHookToOSSignals

Nothing special. Just a basic CLI application that has Windows and Linux (and almost probably macOS, since it's still *nix) hooks to termination and/or kill signals.

There's a great deal more that can be usefully captured, to avoid unecessary C&P see thread at https://forum.lazarus.freepascal.org/index.php/topic,67426.msg518817.html#msg518817 . Note kill's -q option.

There's also stuff relevant to WINCH in Lazarus's debugger/pseudoterminaldlg.pp and debugger/test/watchconsolesize.pas, hence the size information at the bottom of the IDE's console window.

MarkMLl

Thanks for the tips!! That's a ton of info I'll need to digest.
I left SIGHUP in there because I copied the code from the IRC log bot. My main focus is in CTRL-C and kill. I'll probably remove SIGHUP... Still unsure.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

dbannon

  • Hero Member
  • *****
  • Posts: 3318
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Test Hook To OS Signals
« Reply #8 on: March 11, 2025, 05:57:10 am »
Nice work Gus.  I note your use of fpSigAction(SIGTERM, @act, nil) rather than FpSignal(SigInt, @HandleSigInt).  The latter is a touch simpler, avoiding the use of the sigactionrec.  Like this (and so Joanna can see some code) -

Code: Pascal  [Select][+][-]
  1. procedure HandleSigInt(aSignal: LongInt); cdecl;
  2. begin
  3.     case aSignal of
  4.         SigInt : Writeln('Ctrl + C used, will clean up and shutdown.');
  5.         SigTerm : writeln('TERM signal, will clean up and shutdown.');
  6.     else
  7.         writeln('Some signal received ??');
  8.     end;
  9.     if not Application.HasOption('n', 'no_socket') then begin
  10.         SocketThread.Terminate;
  11.         SocketThread.Free;
  12.     end;
  13.     ExitNow := True;        // Loop will see this and exit when it sees fit.
  14. end;
  15.  
  16. begin
  17.     ....
  18.     if FpSignal(SigInt, @HandleSigInt) = signalhandler(SIG_ERR) then begin
  19.       Writeln('Failed to install signal error: ', fpGetErrno);
  20.       Halt(1);
  21.     end;
  22.     if FpSignal(SigTerm, @HandleSigInt) = signalhandler(SIG_ERR) then begin
  23.       Writeln('Failed to install signal error: ', fpGetErrno);
  24.       Halt(1);
  25.     end;
  26.     .....
  27.  

I use a Global to indicate to my loop that we should shutdown, it could just as easily be watched by (eg) and LCL loop.

fpSigAction() can do more but if you don't need that 'more' (and thats normally the case) then fpSignal() fits in nicely.

Look away now Joanna, the whole code is at https://github.com/davidbannon/SolarHotWater/blob/master/Raspicapture/raspicapture.lpr

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MarkMLl

  • Hero Member
  • *****
  • Posts: 8377
Re: Test Hook To OS Signals
« Reply #9 on: March 11, 2025, 08:23:18 am »
Thanks for the tips!! That's a ton of info I'll need to digest.
I left SIGHUP in there because I copied the code from the IRC log bot. My main focus is in CTRL-C and kill. I'll probably remove SIGHUP... Still unsure.

Be warned that there's odd semantics associated with some of the lower-numbered (?) signals in conjunction with threads. At one point I was hoping to be able to use SIGHUP to reset one of a group of threads that were monitoring radio receivers, but then found that something like SIGKILL, SIGINT and SIGHUP were always promoted to signalling the entire process even if the original target was a thread.

It's documented, I forget where...

Updated: it looks like I allude to that stuff in the example code I quoted, I didn't check which project I'd pulled it from.

MarkMLl
« Last Edit: March 11, 2025, 11:57:06 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

Joanna from IRC

  • Hero Member
  • *****
  • Posts: 1360
Re: Test Hook To OS Signals
« Reply #10 on: March 11, 2025, 03:43:19 pm »
If you refuse to run non-free Javascript like Richard Stallman, you could just clone the repository without using a browser at all:  git clone https://github.com/gcarreno/TestHookToOSSignals.git

I am not all that sure that Richard Stallman is an ideal example to us all.
https://drewdevault.com/2024/09/25/2024-09-25-Neurodivergence-and-accountability-in-free-software.html
(a 'read' I found quite disappointing)

But your advise is excellent, Joanna could just clone the repo and leave no trace of her visit there doing so.

Davo
Thanks for showing some of the code. I think that GitHub changed something because I can now View code on the same old browser. Before gus’s link to GitHub looked mostly blank but now I can see a list of things. *crosses fingers*
About the code. On line 6 inside case statement, isn’t it better to use otherwise. It would make it more readable because otherwise is only used in case statements.


As for stallman, it looks like he stuck his foot in his mouth. It should obvious to anyone that many problems could be averted if people refrained getting “intimate” with others whom they are not married to...
« Last Edit: March 11, 2025, 04:12:18 pm by Joanna from IRC »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1186
  • Professional amateur ;-P
Re: Test Hook To OS Signals
« Reply #11 on: March 11, 2025, 10:11:01 pm »
Hey dbannon,

Nice work Gus.  I note your use of fpSigAction(SIGTERM, @act, nil) rather than FpSignal(SigInt, @HandleSigInt).  The latter is a touch simpler, avoiding the use of the sigactionrec.

I think that's not possible any more in more recent versions. When I researched this via Google, and I think also on ChatGPT, they mentioned FpSignal.
But I got an error and had to opt for fpSigAction.

Now... This might be due to my own stupidity and quick judgement. If this is the case, please tell me why it gave me an error, cuz I'm at a lost now that you mentioned both approaches.

Nonetheless, I thank you soooo very much for the kind words!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1186
  • Professional amateur ;-P
Re: Test Hook To OS Signals
« Reply #12 on: March 11, 2025, 10:13:12 pm »
Hey MarkMLI,

Thanks for the tips!! That's a ton of info I'll need to digest.
I left SIGHUP in there because I copied the code from the IRC log bot. My main focus is in CTRL-C and kill. I'll probably remove SIGHUP... Still unsure.

Be warned that there's odd semantics associated with some of the lower-numbered (?) signals in conjunction with threads. At one point I was hoping to be able to use SIGHUP to reset one of a group of threads that were monitoring radio receivers, but then found that something like SIGKILL, SIGINT and SIGHUP were always promoted to signalling the entire process even if the original target was a thread.

It's documented, I forget where...

Updated: it looks like I allude to that stuff in the example code I quoted, I didn't check which project I'd pulled it from.

MarkMLl

All good info that I'm putting away in a safe place!!
I cannot thank you enough for all this extra info. This is the kind of stuff that keeps us coders awake for ever when we hit a snag!!

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

dbannon

  • Hero Member
  • *****
  • Posts: 3318
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Test Hook To OS Signals
« Reply #13 on: March 12, 2025, 04:28:00 am »
@Gus
I think that's not possible any more in more recent versions. When I researched this via Google, and I think also on ChatGPT, they mentioned FpSignal.
But I got an error and had to opt for fpSigAction.
Hmm, works fine for me. I am using FPC324-branch and a "uses ... baseunix, ...

@Joanna
Thanks for showing some of the code. I think that GitHub changed something because I can now View code on the same old browser.
Ah, you could not see it ?  Sorry, I thought it was a case of you not wanting to go there. I see no reason for a read only page to to be running invasive JS stuff. My Firefox has DDG Privacy Essentials and AdBlocker Ultimate and they allow me to use both read and write Github. Hope it stays that way ....
Quote

On line 6 inside case statement, isn’t it better to use otherwise. It would make it more readable because otherwise is only used in case statements.
Yep, agree. Its just I can remember 'else' but not, usually 'otherwise' ! Maybe I will now ?
Quote
As for stallman, it looks like he stuck his foot in his mouth. It should obvious to anyone that many problems could be averted if people refrained getting “intimate” with others whom they are not married to...
What an interesting world that would make !  Seriously, I think RMS does have a condition where he says (and writes) things before considering how they will be read by other people. I value the work he has done with GNU and opensource but I would not want to be in his inner circle of friends.


Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

MarkMLl

  • Hero Member
  • *****
  • Posts: 8377
Re: Test Hook To OS Signals
« Reply #14 on: March 12, 2025, 09:18:28 am »
Ah, you could not see it ?  Sorry, I thought it was a case of you not wanting to go there. I see no reason for a read only page to to be running invasive JS stuff. My Firefox has DDG Privacy Essentials and AdBlocker Ultimate and they allow me to use both read and write Github. Hope it stays that way ....

As I believe I said, for some time Github refused to show file content unless JS was enabled but they appear to have changed that: at present you can see file content but can't get a .zip of the whole project.

Joanna could quite easily get herself a copy of Firefox etc. and use it /only/ for Github, or she could use multiple profiles which I've discussed in the past. I'm out of sympathy, particularly since she's still trying to convince us that she can't see my messages on the forum: which is we all know is not how the system works unless she's explicitly set her profile up to block me.

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

 

TinyPortal © 2005-2018