Running CygWin application as a Windows Service

Johannes Thoma

I am working on WinDRBD (https://github.com/LINBIT/windrbd) a port of the Linux DRBD driver to Microsoft Windows.

We want to run the user mode helper as a Windows Service (DRBD sometimes calls user space applications with call_usermodehelper(), which we emulate by a daemon that retrieves those request from the kernel driver, runs them and returns the exit status to the kernel).

When we run the daemon in a cygwin shell, everything works fine. However when running the daemon as a Windows Service it seems that cygwin cannot find its installation directory (which is C:\cygwin64 on my machines).

The registry entry (HKLM/Software/CygWin/setup/rootdir) points to the correct location, but I am not sure if it can also be accessed by the Windows Service?

/bin/sh isn't found by the service, however /cygdrive/c/cygwin64/bin/sh exists, so when I run the shell with that path it can start (and also finds the DLLs it requires to run). However shell complains with:

bash.exe: warning: could not find /tmp, please create!

which definitely exists when running cygwin the normal way.

Has anyone ever tried to run a CygWin compiled EXE as a Windows Service? Here is the output of sc query windrbdum:

SERVICE_NAME: windrbdum 
    TYPE               : 10  WIN32_OWN_PROCESS  
    STATE              : 4  RUNNING 
                            (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

(um is for user mode).

Thanks for any insights,

  • Johannes
Johannes Thoma

As matzeri pointed out, cygrunsrv is the cygwin tool when it comes to running cygwin binaries as a service under Windows. It serves both as a wrapper (that does the Windows specific service API and event handling) as well as a tool to install, remove, start and stop services (this can still be done with the sc utility like

sc start <servicename>

).

To install a service (I) do:

cygrunsrv.exe -I windrbdlog -p /cygdrive/c/windrbd/usr/sbin/windrbd.exe \
  -a log-server \ 
  -1 /cygdrive/c/windrbd/windrbd-kernel.log \ 
  -2 /cygdrive/c/windrbd/windrbd-kernel.log

where windrbdlog is the Windows name of the service, /cygdrive ... is the full path to the cygwin application (no need to code any Windows Service API calls there, it's just a Cygwin/POSIX executable), log-server is the argument to the binary (so what is being started is windrbd log-server) and -1 and -2 are rediects for stdout and stderr. Exactly what I need, thanks to matzeri for pointing me to cygrunsrv.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Running an application as Windows Service requires Administrator rights or not?

Supervisord as Windows Service on Cygwin

How to Get Running Application with C# Windows Service

Error when Running an Excel Macro from a Windows Service Application

Running Docker under Windows/cygwin environment

.exe Generated by Cygwin not running directly from windows

Windows - running command as a service

Running a Windows Service as a statefull service in Service Fabric

Android service is not running in my application

Service not running when application exits

How to remove "CYGWIN sshd" service from service list in Windows 7?

How to pass a system property to a web application hosted in Tomcat 7 running as a Windows Service?

Sending SignalR Broadcasts that is running within ASP.NET MVC Application from a Windows Service on the same server

ASP.NET CORE : it is possible to query if running as a Windows service or as a console application?

Windows Service Application to perform long-running task only once and stop itself after completion

Always Running Threads on Windows Service

SolrCore is loading running as Windows Service

Determine if a running Windows Service is functioning

Windows Service is Running but not executing code

Is running a Windows .EXE inherently slower via Cygwin than via BAT?

Running a CygWin shell: what windows console (DOS) commands do disappear?

Opening Cygwin with Windows bat file and running script file

How do i get the Windows PPID of a procces running out of cygwin?

Running a python flask application on windows

.NET console application as Windows service

Instrumenting windows service with Application Insights

Windows Service application with a GUI frontend

Implement Application Insights for windows service

Session not working in Windows service application?