Recent

Author Topic: [Solved] Daemon application: -r/--run parameter not working on Windows  (Read 407 times)

Nimral

  • Sr. Member
  • ****
  • Posts: 251
  • Keep it simple.
Hi all,

TDaemon executables do, according to the documentation, support three command line parameters: -install, -uninstall, -run.

It seems that -run, which is supposed to start the damon in user context,  does nothing on Windows. The parameter is, however, very useful during development, since it lets one run the current code from the Lazarus IDE and seamlessly debug most non-security related problems. Under Linux this works great, but unfortunately my main machine is Windows. Under Windows, both -r and --run will do nothing, the executable starts and terminates immedately, without error.

Is there a piece of code around showing how one may get the same behaviour for -r/--run on Windows?

Many thanks,

Armin.
« Last Edit: May 29, 2022, 03:01:42 pm by Nimral »
Lazarus 2.2.0 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Thaddy

  • Hero Member
  • *****
  • Posts: 11774
Re: Daemon application: -r/--run parameter not working on Windows
« Reply #1 on: May 28, 2022, 07:53:57 am »
On Windows it is not called a Daemon, but a Service.
You can use:
net start <your service>
net stop  <your service>
net pause  <your service>
net continue  <your service>
and some more..

to control your Daemon/Service. This is also documented. Windows needs an extra class you must implement.
MvC wrote an article that explains this: https://www.freepascal.org/~michael/articles/daemons/daemons.pdf that covers everything you have to implement.
The example that comes with FPC is a good starting point. it is in ./packages/fcl-extra and there is a unix and a windows version of the daemon library and a cross-platform example.
I recommend first looking at the example and then read the article.

« Last Edit: May 28, 2022, 08:03:39 am by Thaddy »
Black themes should be banned.

Nimral

  • Sr. Member
  • ****
  • Posts: 251
  • Keep it simple.
Re: Daemon application: -r/--run parameter not working on Windows
« Reply #2 on: May 28, 2022, 09:05:10 am »
Thaddy,

thanks once more for this valuable information, I'll look into the article you mentioned today or tomorrow, and if I find a solution include the infos into the wiki about Daemons.

Regarding the net commands ... that's not a good solution for the debugging scenario. For debugging, you need the PID of the daemon process, which is initialized only when the daemon is started. Once you get back to Lazarus from the console and provide the PID to attach to the process, most of the daemon intialization code has long been excuted, and it has had plenty of time to loop the daemon main exec routine.

I have found ways (and included the infos in the wiki) to make the executable wait at specific points in code until Lazarus catches up and attaches the debugger, which helps, but I found it still quite clumsy, compared with what I get under Linux.

Thnx, Armin.
« Last Edit: May 29, 2022, 02:39:21 pm by Nimral »
Lazarus 2.2.0 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Nimral

  • Sr. Member
  • ****
  • Posts: 251
  • Keep it simple.
Re: Daemon application: -r/--run parameter not working on Windows
« Reply #3 on: May 29, 2022, 03:00:56 pm »
Think I finally resolved the issue. It is by design, though not FCL's, but Windows.

In the Windows Variant of daemonapp.inc the -run parameter results in a call to the Windows API StartServiceCtrlDispatcherA function.

According to the Windows API docs, the call has been deliberately designed by Microsoft so it refuses to execute any code which has not been loaded by the Windows Service Control Manager. If one tries, the call fails with error 1063, which is exactly what happens, but unfortunately the TCustomDaemonApplication.SysEndRunDaemons code does not raise an execption, but more or less silently call Application.Terminate.

Now I guess it would not be too hard to wrap the daemon worker code into a thread and run it myself, but it would be a piece of ugly hack code stitched around the FCL TDaemon code for just the purpose of helping me debugging on Windows. Not  much to learn, ever reuse, or contribute to the community.

For my purposes I found a much easier way: now that I know that this is by Windows design, and I don't feel like I want to work around it, I move the main development platform to Linux [for this project]. Since the daemon is running under both OS'ses anyway, I won't care which one is the leading system for development and debugging.

Armin
Lazarus 2.2.0 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

 

TinyPortal © 2005-2018