Page 1 of 2

Running a Cygwin compiled app as an NT service

Posted: Wed Aug 25, 2004 6:07 am
by Ron2K
How do you do it?

What I'm trying to do is create a Win32 installer for IRC Services. Now, I'd like to put in the option to run IRC Services as an NT service (sort of like the Win32 installer for UnrealIRCd 3.2.1). Obviously, I need to know how to run Services as a service (pardon the pun) - if I know that, I'm pretty sure that I can figure out what to do with the installer.

Posted: Wed Aug 25, 2004 10:16 am
by VegetaSan
yeah and I would like to know how this DLL works. and how I need to use it. please tell ^^ I could do usefull things with it.

Re: Running a Cygwin compiled app as an NT service

Posted: Wed Aug 25, 2004 3:32 pm
by aquanight
Ron2K wrote:How do you do it?

What I'm trying to do is create a Win32 installer for IRC Services. Now, I'd like to put in the option to run IRC Services as an NT service (sort of like the Win32 installer for UnrealIRCd 3.2.1). Obviously, I need to know how to run Services as a service (pardon the pun) - if I know that, I'm pretty sure that I can figure out what to do with the installer.
I think the full Cygwin installation has a tool to install it as a service, and even do things like SIGTERM on SCM Stop or Shutdown notifications. I think it's cygrunsrv.exe but I can't be 100% sure on this :/ .

Posted: Wed Aug 25, 2004 4:08 pm
by Ron2K
Don't have the full installation. Is there another way, or do I have no choice except to dowload it at home on my 56K connection?

Posted: Wed Aug 25, 2004 4:58 pm
by VegetaSan
hm... does installing a/the service work with register keys/values ?.. you could allways try it.
register key... ? wrote:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\UnrealIRCd
check that

Iam just guessing. or you could allways use "firedeamon.exe" but virus-scanners will remove that app.

Posted: Wed Aug 25, 2004 5:46 pm
by codemastr
You would need to use something like firedaemon. To make something run as a service, you have to add quite a bit of code to the exe to make it support running as a service. It has to register itself, it has to start itself, it has to return status control query results, etc. If you want an example, look at src/win32/service.c. You can't simply run any app as a service, it must be designed to run as a service.

Posted: Wed Aug 25, 2004 7:37 pm
by VegetaSan
ok. I uploaded firedeamon for you if you dont have it yet.

DOWNLOAD HERE (http://vegetasan.zidev.com/firedaemon.zip)

I only know how to install a service with a batch file.
with batch file .... wrote:@echo off
c:\yourdir\firedaemon.exe -i Servicename "c:\yourappdirectory\linux" "c:\yourappdirectory\linux\app.extention" "" Y 0 0 0 Y
net start Servicename
for example:
you want to install.. UnrealIRCD as a service and you install UnrealIRC in "C:\Unreal3.2" and firedeamon is also in that same directory and service name is "IRCD" . then...
With batch file .. example UnrealIRCD wrote:@echo off
c:\Unreal3.2\firedaemon.exe -i IRCD "c:\UnrealIRCD" "c:\UnrealIRCD\wircd.exe" "" Y 0 0 0 Y
net start IRCD

Posted: Thu Aug 26, 2004 6:12 am
by Ron2K
Thanks, will try it out when I get home this evening.

Posted: Thu Aug 26, 2004 7:12 pm
by VegetaSan
tell us the results

Posted: Mon Aug 30, 2004 5:45 am
by Ron2K
Nope, no success. Gave me some error message which I've forgotten now (will post it later though). Oh well, looks like firedaemon doesn't seem to like Cygwin. In which case, it's time for me to try aquanight's solution.

Posted: Mon Aug 30, 2004 5:36 pm
by codemastr
cygrunsrv is still going to require the application to be coded with NT Service support. It's just a wrapper for the builtin service commands.

Posted: Mon Aug 30, 2004 6:37 pm
by aquanight
Hm... well another option (you might still need to support NT Service stuff):

WinXP (dunno about other OSs) has a commandline application to control services called sc.exe.

To create a service:
sc create <service name> binPath=<binary path> start=auto DisplayName=<displayname>

Of course, you could just invoke the WinAPI calls in the installer :/ .

As a suggestion, if this ever becomes reality, put an option to make the IRCservices NT Service dependant on another service, such as the Unreal IRCd NT Service. This ensures that the IRCd will be started and ready before the services are started, and that the services are stopped before the IRCd is stopped (at least it does so when you go through the Services Control Panel, dunno about invoking SCM directly).

Posted: Tue Aug 31, 2004 2:36 am
by codemastr
Once again, it still requires it to be a service! How can the SCM control something that doesn't tell it how to control it? A service exe knows to stop because it receives the STOP query from the SCM. At this point it sends back the STOPPING reply. After completing its cleanup, it then returns STOPPED. Now the SCM stops the process. A ServiceCtrlHandler function is required to deal with such processing.

An NT service isn't even run like a normal exe. A console app has a main() function, a Windows GUI app has WinMain(), and a service has ServiceMain(). When you try to tell sc to run the service, it won't find ServiceMain and it will fail.

Other than using a go between such as firedaemon, there is NO way to make a normal exe run as a service. And even when using firedaemon, it still isn't truly a service since it has no direct contact with the SCM. The SCM can not inform it of user requests (pauses, stops, etc.) and the exe can not respond to any queries.

Posted: Tue Aug 31, 2004 2:58 am
by w00t
Question: How can Unreal run either as a service or a GUI app?

Posted: Tue Aug 31, 2004 3:13 am
by katsklaw