Recent

Author Topic: Sending a signal/message to a TProcess?  (Read 1199 times)

role_33

  • New Member
  • *
  • Posts: 19
Sending a signal/message to a TProcess?
« on: November 04, 2024, 03:43:37 am »
Hello all,

I'm trying to rewrite a front end for a console application that was originally written in C under Linux.
Unfortunately, developing this front-end in Linux is not an option due to company restrictions.

This (Linux) console application does some cleanup when it receives the SIGTERM or SIGQUIT signals (via Ctrl+C or Ctrl+\).
I recompiled this console application under Windows (using Cygwin), and is responding correctly to both key combinations when I run it from Powershell/Command prompt, so I know the "signals" are being processed correctly under Windows.

Here's my problem (or one of them): I'm running this consola applications from the front-end program I'm writing by using TProcess on a 2nd thread (essentially following the "executing external programs" wiki examples).
However, TProcess.Terminate just kills the process (skipping any processing).

Looking for information on how to send the signals before the thread is terminated -unsurprisingly- returns mostly Linux discussions (using fpKill). There is also discussion of using another console program (I don't recall the name right now) to send the signal to the 1st process, but that program needs Visual Studio to be compiled, and the link to the binaries doesn't work anymore.

So, my question is: is there a way to send signals (specifically SIGQUIT or SIGTERM) to a console app (running as a TProcess) from within Free Pascal?

Thanks!

J

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Sending a signal/message to a TProcess?
« Reply #1 on: November 04, 2024, 08:37:38 am »
Hello.

Not sure that it help, but here someone suggests to use GenerateConsoleCtrlEvent.

There is a Delphi example here.
My projects are on Gitlab and on Codeberg.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7999
Re: Sending a signal/message to a TProcess?
« Reply #2 on: November 04, 2024, 09:26:16 am »
Looking for information on how to send the signals before the thread is terminated -unsurprisingly- returns mostly Linux discussions (using fpKill). There is also discussion of using another console program (I don't recall the name right now) to send the signal to the 1st process, but that program needs Visual Studio to be compiled, and the link to the binaries doesn't work anymore.

From your POV, what makes fpKill wrong?

Quote
So, my question is: is there a way to send signals (specifically SIGQUIT or SIGTERM) to a console app (running as a TProcess) from within Free Pascal?

You use fpKill. There's a couple of caveats: first you'll need to find the PID of the process and second it can be a bit of a blunt instrument if the target program has internal threads.

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

role_33

  • New Member
  • *
  • Posts: 19
Re: Sending a signal/message to a TProcess?
« Reply #3 on: November 04, 2024, 07:34:04 pm »
Thank you people,

@Roland57 I'll take a look.

@MarkMLI: the main problem with fpKill() is that -as far as I know- it works only under Linux.
Is this incorrect?

Thx!

J

MarkMLl

  • Hero Member
  • *****
  • Posts: 7999
Re: Sending a signal/message to a TProcess?
« Reply #4 on: November 04, 2024, 07:58:42 pm »
@MarkMLI: the main problem with fpKill() is that -as far as I know- it works only under Linux.
Is this incorrect?

Ah, I see what you mean. So it's working fine on Linux, but you want to convert it to Windows possibly via Cygwin... OK, it's 20 years since I touched that but does Cygwin provide a kill program (i.e. I mean a separate program, like I think it provides ls, rm and so on) and what does that map to on Windows?

There's obviously lots of ways to do IPC, but it would even more obviously be worth trying to get the existing handler working rather than rearranging things to suit the OS change.

Apart from that- and noting which topic you've posted under- trust anything anybody else says before me: my Windows experience is /way/ out of date.

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

role_33

  • New Member
  • *
  • Posts: 19
Re: Sending a signal/message to a TProcess?
« Reply #5 on: November 04, 2024, 08:46:12 pm »
LOL... I was hoping I was wrong and fpKill worked under Windows :D

Apparently Cygwin takes care of the conversion of messages/interrupts (Windows) and signals (Linux).
All I know is that the console program works fine under Linux AND under Windows (after compiling it using Cygwin). I can close the app using Ctrl+C or Ctrl+\ and the correct code is executed. So, that part is sorted out - thankfully.

I tried modifying the code to respond to standard input (TProcess allows to send keystrokes through STDIN), but it didn't work.

Now I'm on plan B (or is it  C?), which is sending the signals before I terminate the process (from inside my GUI frontend), so it has time to clean up the handlers/whatnot before closing. I'll check to see if there's another console program to do the sending of the signals, but I agree -  the correct way of doing this is from within FPC.

J
 

MarkMLl

  • Hero Member
  • *****
  • Posts: 7999
Re: Sending a signal/message to a TProcess?
« Reply #6 on: November 04, 2024, 09:00:52 pm »
A unix shell (strictly, the terminal handler) will normally intercept ^C and convert it into (I think) a signal, unless set to raw mode. That's probably why trying to do anything with ^C directly doesn't work.

Apart from that I agree: send the signal which by default will be a SIGTERM which the target can intercept, and then if it continues to run send a SIGKILL or interact with the TProcess directly.

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

role_33

  • New Member
  • *
  • Posts: 19
Re: Sending a signal/message to a TProcess?
« Reply #7 on: November 04, 2024, 09:13:32 pm »
Alas... If I just knew how to send those signals! :D

Thx!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11929
  • FPC developer.
Re: Sending a signal/message to a TProcess?
« Reply #8 on: November 04, 2024, 09:15:07 pm »
Freepascal might have some rudimentary signals support for windows in unit signals.

But that was meant to deal with the GDB debugger when linked into the IDE, the FPC own app is still a fully native windows application, and doesn't know about mingw/cygwin Unix emulation modes.

For windows apps it probably means installing a message handler and monitoring WM_QUIT. But I'm not sure if that is called if you kill a process by hard means.
« Last Edit: November 05, 2024, 01:57:56 pm by marcov »

MarkMLl

  • Hero Member
  • *****
  • Posts: 7999
Re: Sending a signal/message to a TProcess?
« Reply #9 on: November 04, 2024, 09:42:23 pm »
Alas... If I just knew how to send those signals! :D

Terminology alert :-)

kill (from a shell) or fpKill (from an FPC program) sends a unix-style signal, there isn't a separate "signal" program or API.

A program (i.e. a process running a binary loaded from a file) has certain default responses to signals, but at least some of these may be overridden using a custom handler (SIGKILL implies extreme prejudice).

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

Roland57

  • Sr. Member
  • ****
  • Posts: 475
    • msegui.net
Re: Sending a signal/message to a TProcess?
« Reply #10 on: November 05, 2024, 08:20:56 am »
There is also discussion of using another console program (I don't recall the name right now) to send the signal to the 1st process, but that program needs Visual Studio to be compiled, and the link to the binaries doesn't work anymore.

It is certainly possible to find such a program, or to write it yourself in C.

Here is what I found:

« Last Edit: November 05, 2024, 09:50:15 pm by Roland57 »
My projects are on Gitlab and on Codeberg.

Thaddy

  • Hero Member
  • *****
  • Posts: 16138
  • Censorship about opinions does not belong here.
Re: Sending a signal/message to a TProcess?
« Reply #11 on: November 05, 2024, 11:42:51 am »
It is certainly possible to find such a program, or to write it yourself in C.
You can simply write it in Pascal.... Don't need to mention C.

In this case a named event is enough, but otherwise you can use a semaphore.
See the syncobjs unit.
That is, if both programs know about the sync object.
C as a language does not help you any further than Pascal can.

BTW the "easy" way out is use the process handles.
« Last Edit: November 05, 2024, 11:47:01 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

role_33

  • New Member
  • *
  • Posts: 19
Re: Sending a signal/message to a TProcess?
« Reply #12 on: November 06, 2024, 06:21:43 pm »
Hello all,

Thank you for keeping the discussion alive.

Just to be on the same page: I only have control over my end of the deal (sending the signals) - and that only after I figure out how to send them :/
I can compile the console app and I can do some basic modifications to it (in C), but anything bigger than that would require more time that I can reasonably invest.

The console app (btw, it's Covesa's DLT-Receive) has code to respond specifically to SIGTERM and SIGQUIT, and I know for a fact it's working correctly under Windows.

The links Roland posted look promising (after some digging). SendSignal doesn't work with x64, and Windows-Kill is the app I mentioned whose binaries are no longer available, and needs Visual Studio to compile. BUT WinSig and SendSignal Advanced may work. I need to do the testing.

@Thaddy: can you offer pointers on how to implement this in Pascal? My problem is not with the multithreading (not at this point, at least), and from what I can see the C app I'm trying to interface with appears to be relatively safe. The issue is sending the darn signal.

J

Thaddy

  • Hero Member
  • *****
  • Posts: 16138
  • Censorship about opinions does not belong here.
Re: Sending a signal/message to a TProcess?
« Reply #13 on: November 06, 2024, 07:28:41 pm »
Do you happen to know if the external program can handle stdin? That would greatly simplify my example.
If I smell bad code it usually is bad code and that includes my own code.

role_33

  • New Member
  • *
  • Posts: 19
Re: Sending a signal/message to a TProcess?
« Reply #14 on: November 06, 2024, 09:01:36 pm »
Hello Thaddy,

I don't think it handles STDIN, unfortunately.
I tried modifying it to add a keyboard input handler (put some handling code on what I thought was the main loop), but I failed miserably :/

The console app can be found on Github (that much I can share!): https://github.com/COVESA/dlt-daemon/blob/master/src/console/dlt-receive.c

The only change I've made was to modify the signal handling code (starting at line 97) to explicitly handle every signal, and put something on STDOUT to confirm it handled it.

J

 

TinyPortal © 2005-2018