Recent

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

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • 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

Khrys

  • Sr. Member
  • ****
  • Posts: 421
Re: Test Hook To OS Signals
« Reply #1 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: 8565
Re: Test Hook To OS Signals
« Reply #2 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

MarkMLl

  • Hero Member
  • *****
  • Posts: 8565
Re: Test Hook To OS Signals
« Reply #3 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: 3777
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Test Hook To OS Signals
« Reply #4 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: 1353
  • Professional amateur ;-P
Re: Test Hook To OS Signals
« Reply #5 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

dbannon

  • Hero Member
  • *****
  • Posts: 3777
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Test Hook To OS Signals
« Reply #6 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: 8565
Re: Test Hook To OS Signals
« Reply #7 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

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: Test Hook To OS Signals
« Reply #8 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

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: Test Hook To OS Signals
« Reply #9 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

dbannon

  • Hero Member
  • *****
  • Posts: 3777
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Test Hook To OS Signals
« Reply #10 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: 8565
Re: Test Hook To OS Signals
« Reply #11 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

Khrys

  • Sr. Member
  • ****
  • Posts: 421
Re: Test Hook To OS Signals
« Reply #12 on: March 12, 2025, 09:57:26 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.

I added the Stallman comparison to take a jab at those who demand special treatment because they refuse to use popular services for kooky reasons   :-X

MarkMLl

  • Hero Member
  • *****
  • Posts: 8565
Re: Test Hook To OS Signals
« Reply #13 on: March 12, 2025, 11:36:16 am »
I added the Stallman comparison to take a jab at those who demand special treatment because they refuse to use popular services for kooky reasons   :-X

I sympathise with the person being discussed here, but the bottom line is that she refuses to listen when people tell here that there's easy ways round the problems she's seeing.

I believe she's running Windows and that she protests that she can't afford to buy a "new" computer, which probably means v7 or later. I suspect that she's using whatever browser was shipped with it, which- if it really is an old computer- is probably so raddled with unfixed security flaws that she's far more at risk from that than from using even the most intrusive social media.

Firefox still supports OSes that "old", and is reasonably well-behaved when it comes to leaking embarassing details of ones personal life (whether or not the person being discussed has any, I prefer not to speculate).

It's possible- in fact this is how I have worked for the last 20 years or so- to have a default profile with absolutely everything locked down (install NoScript, disable more of the JavaScript etc. internal support and so on) and that will be /far/ more robust than anything shipped with an old version of Windows: and /far/ less intrusive than e.g. Chrome.

Then have a separate profile used /only/ for Github etc., another separate profile used only for banking, and so on.

There is absolutely no reason for even the most paranoid to not do something like that. And I write as somebody who has resolutely avoided Twitter, Facebook, Whatsapp and so on: sometimes to my detriment, but basically the only things they know about me have been acquired without my consent.

I don't like faceless multinationals. I like rapacious American companies masquerading as multinationals even less. But my dislike for those is nothing when compared with persistent messaging that I really /must/ join such-and-such a communications channel, and that extends all the way from Facebook to Joanna's execrable IRC.

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 8565
Re: Test Hook To OS Signals
« Reply #14 on: March 12, 2025, 02:26:56 pm »
I prefer Firefox because it has a night mode. I have tried all sorts of browsers on different computers and GitHub didn’t work on most of them. It was showing me a black screen with black font and to see what was there I had to copy and paste elsewhere. I’m not too pleased about who bought GitHub either..

That's reasonable, but in that case you need to investigate the multiple profiles facility.

I don't know about the black-on-black side, particularly since I think you're a Windows user. I've seen that sort of think with KDE on Linux using Intel display hardware, it mostly went away when I got an AMD card.

Quote
Otherwise has always been the default option for case statements Since I’ve used pascal. I'm not sure why else was added. It’s inconsistent because it’s supposed to be paired with an if then statement. By all means an else makes sense if there is an if then else statement as part of the case statement options.

Sorry but that's rubbish. J&W ed.2 doesn't document else and explicitly says that if there's no matching case label the result is undefined, I think (but am not sure) that else was a Borland thing and they subsequently added otherwise to allow for things like

Code: Pascal  [Select][+][-]
  1. case something of
  2.   foo: begin...end;
  3.   bar: if bletch then begin...end
  4. else
  5. ...
  6. end;
  7.  

where the compiler will incorrectly associate the else with the final if-then.

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