Running a Cygwin compiled app as an NT service

Talk about pretty much anything here, but DO NOT USE FOR SUPPORT.

Moderator: Supporters

Ron2K

Running a Cygwin compiled app as an NT service

Post 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.
VegetaSan
Posts: 18
Joined: Wed Aug 25, 2004 9:11 am
Location: The Netherlands
Contact:

Post 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.
Image
aquanight
Official supporter
Posts: 862
Joined: Tue Mar 09, 2004 10:47 pm
Location: Boise, ID

Re: Running a Cygwin compiled app as an NT service

Post 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 :/ .
Ron2K

Post 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?
VegetaSan
Posts: 18
Joined: Wed Aug 25, 2004 9:11 am
Location: The Netherlands
Contact:

Post 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.
Image
codemastr
Former UnrealIRCd head coder
Posts: 811
Joined: Sat Mar 06, 2004 8:47 pm
Location: United States
Contact:

Post 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.
-- codemastr
VegetaSan
Posts: 18
Joined: Wed Aug 25, 2004 9:11 am
Location: The Netherlands
Contact:

Post 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
Image
Ron2K

Post by Ron2K »

Thanks, will try it out when I get home this evening.
VegetaSan
Posts: 18
Joined: Wed Aug 25, 2004 9:11 am
Location: The Netherlands
Contact:

Post by VegetaSan »

tell us the results
Image
Ron2K

Post 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.
codemastr
Former UnrealIRCd head coder
Posts: 811
Joined: Sat Mar 06, 2004 8:47 pm
Location: United States
Contact:

Post 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.
-- codemastr
aquanight
Official supporter
Posts: 862
Joined: Tue Mar 09, 2004 10:47 pm
Location: Boise, ID

Post 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).
codemastr
Former UnrealIRCd head coder
Posts: 811
Joined: Sat Mar 06, 2004 8:47 pm
Location: United States
Contact:

Post 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.
-- codemastr
w00t
Posts: 1136
Joined: Thu Mar 25, 2004 3:31 am
Location: Nowra, Australia

Post by w00t »

Question: How can Unreal run either as a service or a GUI app?
-ChatSpike IRC Network [http://www.chatspike.net]
-Denora Stats [http://denora.nomadirc.net]
-Omerta [http://www.barafranca.com]
katsklaw
Official supporter
Posts: 1124
Joined: Sun Apr 18, 2004 5:06 pm
Contact:

Post by katsklaw »

Locked