aboutsummaryrefslogtreecommitdiff
path: root/service-win32
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--service-win32/.svnignore8
-rwxr-xr-xservice-win32/Makefile25
-rw-r--r--service-win32/Makefile.am41
-rwxr-xr-xservice-win32/mkpatch4
-rwxr-xr-xservice-win32/openvpnserv.c13
-rw-r--r--service-win32/service.c693
-rw-r--r--service-win32/service.h141
-rwxr-xr-xservice-win32/service.patch359
8 files changed, 892 insertions, 392 deletions
diff --git a/service-win32/.svnignore b/service-win32/.svnignore
new file mode 100644
index 0000000..df25b75
--- /dev/null
+++ b/service-win32/.svnignore
@@ -0,0 +1,8 @@
+*.exe
+*.obj
+*.o
+.deps
+Makefile.in
+Makefile
+service.h
+service.c
diff --git a/service-win32/Makefile b/service-win32/Makefile
deleted file mode 100755
index 9a3cb5d..0000000
--- a/service-win32/Makefile
+++ /dev/null
@@ -1,25 +0,0 @@
-# This makefile builds the OpenVPN win32 service
-# wrapper using the mingw environment.
-#
-# service.c and service.h should be generated by
-# applying service.patch to the Platform
-# SDK service sample.
-
-EXE = ${PRODUCT_UNIX_NAME}serv.exe
-
-HEADERS = service.h
-
-OBJS = openvpnserv.o service.o
-
-INCLUDE_DIRS =
-
-CC = gcc -g -O2 -Wall -Wno-unused-function -Wno-unused-variable -mno-cygwin
-
-all : ${OBJS}
- ${CC} -o ${EXE} ${OBJS}
-
-clean :
- rm -f ${OBJS} ${EXE}
-
-%.o : %.c ${HEADERS}
- ${CC} ${INCLUDE_DIRS} -c $< -o $@
diff --git a/service-win32/Makefile.am b/service-win32/Makefile.am
new file mode 100644
index 0000000..97c5ef6
--- /dev/null
+++ b/service-win32/Makefile.am
@@ -0,0 +1,41 @@
+#
+# OpenVPN -- An application to securely tunnel IP networks
+# over a single UDP port, with support for SSL/TLS-based
+# session authentication and key exchange,
+# packet encryption, packet authentication, and
+# packet compression.
+#
+# Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program (see the file COPYING included with this
+# distribution); if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
+
+if WIN32
+
+sbin_PROGRAMS = openvpnserv
+
+openvpnserv_SOURCES = \
+ openvpnserv.c \
+ service.h service.c
+
+else
+
+dist_noinst_DATA = \
+ openvpnserv.c \
+ service.h service.c
+
+endif
diff --git a/service-win32/mkpatch b/service-win32/mkpatch
deleted file mode 100755
index 83652e1..0000000
--- a/service-win32/mkpatch
+++ /dev/null
@@ -1,4 +0,0 @@
-# build service.[ch] patch against original
-# SDK sample
-diff -ub service.c.orig service.c | u2d >service.patch
-diff -ub service.h.orig service.h | u2d >>service.patch
diff --git a/service-win32/openvpnserv.c b/service-win32/openvpnserv.c
index 76323ca..6cdce26 100755
--- a/service-win32/openvpnserv.c
+++ b/service-win32/openvpnserv.c
@@ -33,6 +33,11 @@
* This code is designed to be built with the mingw compiler.
*/
+#ifdef _MSC_VER
+#include "config-win32.h"
+#else
+#include "config.h"
+#endif
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
@@ -65,13 +70,13 @@ struct security_attributes
* Control Manager which will cause an asynchronous call
* of ServiceStop below.
*/
-#define EXIT_EVENT_NAME PRODUCT_UNIX_NAME "_exit_1"
+#define EXIT_EVENT_NAME PACKAGE "_exit_1"
/*
* Which registry key in HKLM should
* we get config info from?
*/
-#define REG_KEY "SOFTWARE\\" PRODUCT_NAME
+#define REG_KEY "SOFTWARE\\" PACKAGE_NAME
static HANDLE exit_event = NULL;
@@ -398,7 +403,7 @@ VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
mysnprintf (log_path, "%s\\%s", log_dir, log_file);
/* construct command line */
- mysnprintf (command_line, PRODUCT_UNIX_NAME " --service %s 1 --config \"%s\"",
+ mysnprintf (command_line, PACKAGE " --service %s 1 --config \"%s\"",
EXIT_EVENT_NAME,
find_obj.cFileName);
@@ -406,7 +411,7 @@ VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
be inherited. */
if (!init_security_attributes_allow_all (&sa))
{
- MSG (M_SYSERR, "InitializeSecurityDescriptor start_" PRODUCT_UNIX_NAME " failed");
+ MSG (M_SYSERR, "InitializeSecurityDescriptor start_" PACKAGE " failed");
goto finish;
}
diff --git a/service-win32/service.c b/service-win32/service.c
new file mode 100644
index 0000000..d5211bd
--- /dev/null
+++ b/service-win32/service.c
@@ -0,0 +1,693 @@
+/*---------------------------------------------------------------------------
+THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
+ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
+PARTICULAR PURPOSE.
+
+Copyright (C) 1993 - 2000. Microsoft Corporation. All rights reserved.
+
+MODULE: service.c
+
+PURPOSE: Implements functions required by all Windows NT services
+
+FUNCTIONS:
+ main(int argc, char **argv);
+ service_ctrl(DWORD dwCtrlCode);
+ service_main(DWORD dwArgc, LPTSTR *lpszArgv);
+ CmdInstallService();
+ CmdRemoveService();
+ CmdStartService();
+ CmdDebugService(int argc, char **argv);
+ ControlHandler ( DWORD dwCtrlType );
+ GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
+
+---------------------------------------------------------------------------*/
+
+#include <windows.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <process.h>
+#include <tchar.h>
+
+#include "service.h"
+
+// internal variables
+SERVICE_STATUS ssStatus; // current status of the service
+SERVICE_STATUS_HANDLE sshStatusHandle;
+DWORD dwErr = 0;
+BOOL bDebug = FALSE;
+TCHAR szErr[256];
+
+// internal function prototypes
+VOID WINAPI service_ctrl(DWORD dwCtrlCode);
+VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv);
+int CmdInstallService();
+int CmdRemoveService();
+int CmdStartService();
+VOID CmdDebugService(int argc, char **argv);
+BOOL WINAPI ControlHandler ( DWORD dwCtrlType );
+LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
+
+//
+// FUNCTION: main
+//
+// PURPOSE: entrypoint for service
+//
+// PARAMETERS:
+// argc - number of command line arguments
+// argv - array of command line arguments
+//
+// RETURN VALUE:
+// none
+//
+// COMMENTS:
+// main() either performs the command line task, or
+// call StartServiceCtrlDispatcher to register the
+// main service thread. When the this call returns,
+// the service has stopped, so exit.
+//
+int __cdecl main(int argc, char **argv)
+{
+ SERVICE_TABLE_ENTRY dispatchTable[] =
+ {
+ { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
+ { NULL, NULL}
+ };
+
+ if ( (argc > 1) &&
+ ((*argv[1] == '-') || (*argv[1] == '/')) )
+ {
+ if ( _stricmp( "install", argv[1]+1 ) == 0 )
+ {
+ return CmdInstallService();
+ }
+ else if ( _stricmp( "remove", argv[1]+1 ) == 0 )
+ {
+ return CmdRemoveService();
+ }
+ else if ( _stricmp( "start", argv[1]+1 ) == 0)
+ {
+ return CmdStartService();
+ }
+ else if ( _stricmp( "debug", argv[1]+1 ) == 0 )
+ {
+ bDebug = TRUE;
+ CmdDebugService(argc, argv);
+ }
+ else
+ {
+ goto dispatch;
+ }
+ return 0;
+ }
+
+ // if it doesn't match any of the above parameters
+ // the service control manager may be starting the service
+ // so we must call StartServiceCtrlDispatcher
+ dispatch:
+ // this is just to be friendly
+ printf( "%s -install to install the service\n", SZAPPNAME );
+ printf( "%s -start to start the service\n", SZAPPNAME );
+ printf( "%s -remove to remove the service\n", SZAPPNAME );
+ printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME );
+ printf( "\nStartServiceCtrlDispatcher being called.\n" );
+ printf( "This may take several seconds. Please wait.\n" );
+
+ if (!StartServiceCtrlDispatcher(dispatchTable))
+ AddToMessageLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed."));
+
+ return 0;
+}
+
+
+
+//
+// FUNCTION: service_main
+//
+// PURPOSE: To perform actual initialization of the service
+//
+// PARAMETERS:
+// dwArgc - number of command line arguments
+// lpszArgv - array of command line arguments
+//
+// RETURN VALUE:
+// none
+//
+// COMMENTS:
+// This routine performs the service initialization and then calls
+// the user defined ServiceStart() routine to perform majority
+// of the work.
+//
+void WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv)
+{
+
+ // register our service control handler:
+ //
+ sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), service_ctrl);
+
+ if (!sshStatusHandle)
+ goto cleanup;
+
+ // SERVICE_STATUS members that don't change in example
+ //
+ ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+ ssStatus.dwServiceSpecificExitCode = 0;
+
+
+ // report the status to the service control manager.
+ //
+ if (!ReportStatusToSCMgr(
+ SERVICE_START_PENDING, // service state
+ NO_ERROR, // exit code
+ 3000)) // wait hint
+ goto cleanup;
+
+
+ ServiceStart( dwArgc, lpszArgv );
+
+ cleanup:
+
+ // try to report the stopped status to the service control manager.
+ //
+ if (sshStatusHandle)
+ (VOID)ReportStatusToSCMgr(
+ SERVICE_STOPPED,
+ dwErr,
+ 0);
+
+ return;
+}
+
+
+
+//
+// FUNCTION: service_ctrl
+//
+// PURPOSE: This function is called by the SCM whenever
+// ControlService() is called on this service.
+//
+// PARAMETERS:
+// dwCtrlCode - type of control requested
+//
+// RETURN VALUE:
+// none
+//
+// COMMENTS:
+//
+VOID WINAPI service_ctrl(DWORD dwCtrlCode)
+{
+ // Handle the requested control code.
+ //
+ switch (dwCtrlCode)
+ {
+ // Stop the service.
+ //
+ // SERVICE_STOP_PENDING should be reported before
+ // setting the Stop Event - hServerStopEvent - in
+ // ServiceStop(). This avoids a race condition
+ // which may result in a 1053 - The Service did not respond...
+ // error.
+ case SERVICE_CONTROL_STOP:
+ ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
+ ServiceStop();
+ return;
+
+ // Update the service status.
+ //
+ case SERVICE_CONTROL_INTERROGATE:
+ break;
+
+ // invalid control code
+ //
+ default:
+ break;
+
+ }
+
+ ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
+}
+
+
+
+//
+// FUNCTION: ReportStatusToSCMgr()
+//
+// PURPOSE: Sets the current status of the service and
+// reports it to the Service Control Manager
+//
+// PARAMETERS:
+// dwCurrentState - the state of the service
+// dwWin32ExitCode - error code to report
+// dwWaitHint - worst case estimate to next checkpoint
+//
+// RETURN VALUE:
+// TRUE - success
+// FALSE - failure
+//
+// COMMENTS:
+//
+BOOL ReportStatusToSCMgr(DWORD dwCurrentState,
+ DWORD dwWin32ExitCode,
+ DWORD dwWaitHint)
+{
+ static DWORD dwCheckPoint = 1;
+ BOOL fResult = TRUE;
+
+
+ if ( !bDebug ) // when debugging we don't report to the SCM
+ {
+ if (dwCurrentState == SERVICE_START_PENDING)
+ ssStatus.dwControlsAccepted = 0;
+ else
+ ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+
+ ssStatus.dwCurrentState = dwCurrentState;
+ ssStatus.dwWin32ExitCode = dwWin32ExitCode;
+ ssStatus.dwWaitHint = dwWaitHint;
+
+ if ( ( dwCurrentState == SERVICE_RUNNING ) ||
+ ( dwCurrentState == SERVICE_STOPPED ) )
+ ssStatus.dwCheckPoint = 0;
+ else
+ ssStatus.dwCheckPoint = dwCheckPoint++;
+
+
+ // Report the status of the service to the service control manager.
+ //
+ if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus)))
+ {
+ AddToMessageLog(MSG_FLAGS_ERROR, TEXT("SetServiceStatus"));
+ }
+ }
+ return fResult;
+}
+
+
+
+//
+// FUNCTION: AddToMessageLog(LPTSTR lpszMsg)
+//
+// PURPOSE: Allows any thread to log an error message
+//
+// PARAMETERS:
+// lpszMsg - text for message
+//
+// RETURN VALUE:
+// none
+//
+// COMMENTS:
+//
+void AddToMessageLog(DWORD flags, LPTSTR lpszMsg)
+{
+ TCHAR szMsg [(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100 ];
+ HANDLE hEventSource;
+ LPCSTR lpszStrings[2];
+
+ if ( !bDebug )
+ {
+ if (flags & MSG_FLAGS_SYS_CODE)
+ dwErr = GetLastError();
+ else
+ dwErr = 0;
+
+ // Use event logging to log the error.
+ //
+ hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
+
+ _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), (int)dwErr);
+ lpszStrings[0] = szMsg;
+ lpszStrings[1] = lpszMsg;
+
+ if (hEventSource != NULL)
+ {
+ ReportEvent(hEventSource, // handle of event source
+ // event type
+ (flags & MSG_FLAGS_ERROR)
+ ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
+ 0, // event category
+ 0, // event ID
+ NULL, // current user's SID
+ 2, // strings in lpszStrings
+ 0, // no bytes of raw data
+ lpszStrings, // array of error strings
+ NULL); // no raw data
+
+ (VOID) DeregisterEventSource(hEventSource);
+ }
+ }
+}
+
+void ResetError (void)
+{
+ dwErr = 0;
+}
+
+///////////////////////////////////////////////////////////////////
+//
+// The following code handles service installation and removal
+//
+
+
+//
+// FUNCTION: CmdInstallService()
+//
+// PURPOSE: Installs the service
+//
+// PARAMETERS:
+// none
+//
+// RETURN VALUE:
+// 0 if success
+//
+// COMMENTS:
+//
+int CmdInstallService()
+{
+ SC_HANDLE schService;
+ SC_HANDLE schSCManager;
+
+ TCHAR szPath[512];
+
+ int ret = 0;
+
+ if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
+ {
+ _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256));
+ return 1;
+ }
+
+ schSCManager = OpenSCManager(
+ NULL, // machine (NULL == local)
+ NULL, // database (NULL == default)
+ SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE // access required
+ );
+ if ( schSCManager )
+ {
+ schService = CreateService(
+ schSCManager, // SCManager database
+ TEXT(SZSERVICENAME), // name of service
+ TEXT(SZSERVICEDISPLAYNAME), // name to display
+ SERVICE_QUERY_STATUS, // desired access
+ SERVICE_WIN32_OWN_PROCESS, // service type
+ SERVICE_DEMAND_START, // start type -- alternative: SERVICE_AUTO_START
+ SERVICE_ERROR_NORMAL, // error control type
+ szPath, // service's binary
+ NULL, // no load ordering group
+ NULL, // no tag identifier
+ TEXT(SZDEPENDENCIES), // dependencies
+ NULL, // LocalSystem account
+ NULL); // no password
+
+ if ( schService )
+ {
+ _tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
+ CloseServiceHandle(schService);
+ }
+ else
+ {
+ _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256));
+ ret = 1;
+ }
+
+ CloseServiceHandle(schSCManager);
+ }
+ else
+ {
+ _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+ return ret;
+}
+
+//
+// FUNCTION: CmdStartService()
+//
+// PURPOSE: Start the service
+//
+// PARAMETERS:
+// none
+//
+// RETURN VALUE:
+// 0 if success
+//
+// COMMENTS:
+
+int CmdStartService()
+{
+ int ret = 0;
+
+ SC_HANDLE schSCManager;
+ SC_HANDLE schService;
+
+
+ // Open a handle to the SC Manager database.
+ schSCManager = OpenSCManager(
+ NULL, // local machine
+ NULL, // ServicesActive database
+ SC_MANAGER_ALL_ACCESS); // full access rights
+
+ if (NULL == schSCManager) {
+ _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+
+ schService = OpenService(
+ schSCManager, // SCM database
+ SZSERVICENAME, // service name
+ SERVICE_ALL_ACCESS);
+
+ if (schService == NULL) {
+ _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+
+ if (!StartService(
+ schService, // handle to service
+ 0, // number of arguments
+ NULL) ) // no arguments
+ {
+ _tprintf(TEXT("StartService failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+ else
+ {
+ _tprintf(TEXT("Service Started\n"));
+ ret = 0;
+ }
+ CloseServiceHandle(schService);
+ CloseServiceHandle(schSCManager);
+ return ret;
+}
+
+//
+// FUNCTION: CmdRemoveService()
+//
+// PURPOSE: Stops and removes the service
+//
+// PARAMETERS:
+// none
+//
+// RETURN VALUE:
+// 0 if success
+//
+// COMMENTS:
+//
+int CmdRemoveService()
+{
+ SC_HANDLE schService;
+ SC_HANDLE schSCManager;
+
+ int ret = 0;
+
+ schSCManager = OpenSCManager(
+ NULL, // machine (NULL == local)
+ NULL, // database (NULL == default)
+ SC_MANAGER_CONNECT // access required
+ );
+ if ( schSCManager )
+ {
+ schService = OpenService(schSCManager, TEXT(SZSERVICENAME), DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
+
+ if (schService)
+ {
+ // try to stop the service
+ if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
+ {
+ _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME));
+ Sleep( 1000 );
+
+ while ( QueryServiceStatus( schService, &ssStatus ) )
+ {
+ if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
+ {
+ _tprintf(TEXT("."));
+ Sleep( 1000 );
+ }
+ else
+ break;
+ }
+
+ if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
+ _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
+ else
+ {
+ _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );
+ ret = 1;
+ }
+
+ }
+
+ // now remove the service
+ if ( DeleteService(schService) )
+ _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
+ else
+ {
+ _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+
+
+ CloseServiceHandle(schService);
+ }
+ else
+ {
+ _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+
+ CloseServiceHandle(schSCManager);
+ }
+ else
+ {
+ _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
+ ret = 1;
+ }
+ return ret;
+}
+
+
+
+
+///////////////////////////////////////////////////////////////////
+//
+// The following code is for running the service as a console app
+//
+
+
+//
+// FUNCTION: CmdDebugService(int argc, char ** argv)
+//
+// PURPOSE: Runs the service as a console application
+//
+// PARAMETERS:
+// argc - number of command line arguments
+// argv - array of command line arguments
+//
+// RETURN VALUE:
+// none
+//
+// COMMENTS:
+//
+void CmdDebugService(int argc, char ** argv)
+{
+ DWORD dwArgc;
+ LPTSTR *lpszArgv;
+
+#ifdef UNICODE
+ lpszArgv = CommandLineToArgvW(GetCommandLineW(), &(dwArgc) );
+ if (NULL == lpszArgv)
+ {
+ // CommandLineToArvW failed!!
+ _tprintf(TEXT("CmdDebugService CommandLineToArgvW returned NULL\n"));
+ return;
+ }
+#else
+ dwArgc = (DWORD) argc;
+ lpszArgv = argv;
+#endif
+
+ _tprintf(TEXT("Debugging %s.\n"), TEXT(SZSERVICEDISPLAYNAME));
+
+ SetConsoleCtrlHandler( ControlHandler, TRUE );
+
+ ServiceStart( dwArgc, lpszArgv );
+
+#ifdef UNICODE
+// Must free memory allocated for arguments
+
+ GlobalFree(lpszArgv);
+#endif // UNICODE
+
+}
+
+
+//
+// FUNCTION: ControlHandler ( DWORD dwCtrlType )
+//
+// PURPOSE: Handled console control events
+//
+// PARAMETERS:
+// dwCtrlType - type of control event
+//
+// RETURN VALUE:
+// True - handled
+// False - unhandled
+//
+// COMMENTS:
+//
+BOOL WINAPI ControlHandler ( DWORD dwCtrlType )
+{
+ switch ( dwCtrlType )
+ {
+ case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate
+ case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode
+ _tprintf(TEXT("Stopping %s.\n"), TEXT(SZSERVICEDISPLAYNAME));
+ ServiceStop();
+ return TRUE;
+ break;
+
+ }
+ return FALSE;
+}
+
+//
+// FUNCTION: GetLastErrorText
+//
+// PURPOSE: copies error message text to string
+//
+// PARAMETERS:
+// lpszBuf - destination buffer
+// dwSize - size of buffer
+//
+// RETURN VALUE:
+// destination buffer
+//
+// COMMENTS:
+//
+LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize )
+{
+ DWORD dwRet;
+ LPTSTR lpszTemp = NULL;
+
+ dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
+ NULL,
+ GetLastError(),
+ LANG_NEUTRAL,
+ (LPTSTR)&lpszTemp,
+ 0,
+ NULL );
+
+ // supplied buffer is not long enough
+ if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) )
+ lpszBuf[0] = TEXT('\0');
+ else
+ {
+ lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
+ _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, (int)GetLastError() );
+ }
+
+ if ( lpszTemp )
+ LocalFree((HLOCAL) lpszTemp );
+
+ return lpszBuf;
+}
diff --git a/service-win32/service.h b/service-win32/service.h
new file mode 100644
index 0000000..028d075
--- /dev/null
+++ b/service-win32/service.h
@@ -0,0 +1,141 @@
+/*---------------------------------------------------------------------------
+THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
+ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
+PARTICULAR PURPOSE.
+
+Copyright (C) 1993 - 2000. Microsoft Corporation. All rights reserved.
+
+ MODULE: service.h
+
+ Comments: The use of this header file and the accompanying service.c
+ file simplifies the process of writting a service. You as a developer
+ simply need to follow the TODO's outlined in this header file, and
+ implement the ServiceStart() and ServiceStop() functions.
+
+ There is no need to modify the code in service.c. Just add service.c
+ to your project and link with the following libraries...
+
+ libcmt.lib kernel32.lib advapi.lib shell32.lib
+
+ This code also supports unicode. Be sure to compile both service.c and
+ and code #include "service.h" with the same Unicode setting.
+
+ Upon completion, your code will have the following command line interface
+
+ <service exe> -? to display this list
+ <service exe> -install to install the service
+ <service exe> -remove to remove the service
+ <service exe> -debug <params> to run as a console app for debugging
+
+ Note: This code also implements Ctrl+C and Ctrl+Break handlers
+ when using the debug option. These console events cause
+ your ServiceStop routine to be called
+
+ Also, this code only handles the OWN_SERVICE service type
+ running in the LOCAL_SYSTEM security context.
+
+ To control your service ( start, stop, etc ) you may use the
+ Services control panel applet or the NET.EXE program.
+
+ To aid in writing/debugging service, the
+ SDK contains a utility (MSTOOLS\BIN\SC.EXE) that
+ can be used to control, configure, or obtain service status.
+ SC displays complete status for any service/driver
+ in the service database, and allows any of the configuration
+ parameters to be easily changed at the command line.
+ For more information on SC.EXE, type SC at the command line.
+
+
+------------------------------------------------------------------------------*/
+
+#ifndef _SERVICE_H
+#define _SERVICE_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "config.h"
+
+//////////////////////////////////////////////////////////////////////////////
+//// todo: change to desired strings
+////
+// name of the executable
+#define SZAPPNAME PACKAGE "serv"
+// internal name of the service
+#define SZSERVICENAME PACKAGE_NAME "Service"
+// displayed name of the service
+#define SZSERVICEDISPLAYNAME PACKAGE_NAME " Service"
+// list of service dependencies - "dep1\0dep2\0\0"
+#define SZDEPENDENCIES TAP_ID "\0Dhcp\0\0"
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+//// todo: ServiceStart()must be defined by in your code.
+//// The service should use ReportStatusToSCMgr to indicate
+//// progress. This routine must also be used by StartService()
+//// to report to the SCM when the service is running.
+////
+//// If a ServiceStop procedure is going to take longer than
+//// 3 seconds to execute, it should spawn a thread to
+//// execute the stop code, and return. Otherwise, the
+//// ServiceControlManager will believe that the service has
+//// stopped responding
+////
+ VOID ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv);
+ VOID ServiceStop();
+//////////////////////////////////////////////////////////////////////////////
+
+
+
+//////////////////////////////////////////////////////////////////////////////
+//// The following are procedures which
+//// may be useful to call within the above procedures,
+//// but require no implementation by the user.
+//// They are implemented in service.c
+
+//
+// FUNCTION: ReportStatusToSCMgr()
+//
+// PURPOSE: Sets the current status of the service and
+// reports it to the Service Control Manager
+//
+// PARAMETERS:
+// dwCurrentState - the state of the service
+// dwWin32ExitCode - error code to report
+// dwWaitHint - worst case estimate to next checkpoint
+//
+// RETURN VALUE:
+// TRUE - success
+// FALSE - failure
+//
+ BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);
+
+
+//
+// FUNCTION: AddToMessageLog(LPTSTR lpszMsg)
+//
+// PURPOSE: Allows any thread to log an error message
+//
+// PARAMETERS:
+// lpszMsg - text for message
+//
+// RETURN VALUE:
+// none
+//
+# define MSG_FLAGS_ERROR (1<<0)
+# define MSG_FLAGS_SYS_CODE (1<<1)
+ void AddToMessageLog(DWORD flags, LPTSTR lpszMsg);
+ void ResetError (void);
+//////////////////////////////////////////////////////////////////////////////
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/service-win32/service.patch b/service-win32/service.patch
deleted file mode 100755
index 8e4ddf9..0000000
--- a/service-win32/service.patch
+++ /dev/null
@@ -1,359 +0,0 @@
---- service.c.orig Tue Apr 24 14:49:30 2007
-+++ service.c Tue Apr 24 12:20:08 2007
-@@ -16,6 +16,7 @@
- service_main(DWORD dwArgc, LPTSTR *lpszArgv);
- CmdInstallService();
- CmdRemoveService();
-+ CmdStartService();
- CmdDebugService(int argc, char **argv);
- ControlHandler ( DWORD dwCtrlType );
- GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
-@@ -40,8 +41,9 @@
- // internal function prototypes
- VOID WINAPI service_ctrl(DWORD dwCtrlCode);
- VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv);
--VOID CmdInstallService();
--VOID CmdRemoveService();
-+int CmdInstallService();
-+int CmdRemoveService();
-+int CmdStartService();
- VOID CmdDebugService(int argc, char **argv);
- BOOL WINAPI ControlHandler ( DWORD dwCtrlType );
- LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
-@@ -64,7 +66,7 @@
- // main service thread. When the this call returns,
- // the service has stopped, so exit.
- //
--void __cdecl main(int argc, char **argv)
-+int __cdecl main(int argc, char **argv)
- {
- SERVICE_TABLE_ENTRY dispatchTable[] =
- {
-@@ -77,11 +79,15 @@
- {
- if ( _stricmp( "install", argv[1]+1 ) == 0 )
- {
-- CmdInstallService();
-+ return CmdInstallService();
- }
- else if ( _stricmp( "remove", argv[1]+1 ) == 0 )
- {
-- CmdRemoveService();
-+ return CmdRemoveService();
-+ }
-+ else if ( _stricmp( "start", argv[1]+1 ) == 0)
-+ {
-+ return CmdStartService();
- }
- else if ( _stricmp( "debug", argv[1]+1 ) == 0 )
- {
-@@ -92,7 +98,7 @@
- {
- goto dispatch;
- }
-- exit(0);
-+ return 0;
- }
-
- // if it doesn't match any of the above parameters
-@@ -101,13 +107,16 @@
- dispatch:
- // this is just to be friendly
- printf( "%s -install to install the service\n", SZAPPNAME );
-+ printf( "%s -start to start the service\n", SZAPPNAME );
- printf( "%s -remove to remove the service\n", SZAPPNAME );
- printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME );
- printf( "\nStartServiceCtrlDispatcher being called.\n" );
- printf( "This may take several seconds. Please wait.\n" );
-
- if (!StartServiceCtrlDispatcher(dispatchTable))
-- AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed."));
-+ AddToMessageLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed."));
-+
-+ return 0;
- }
-
-
-@@ -267,7 +276,7 @@
- //
- if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus)))
- {
-- AddToMessageLog(TEXT("SetServiceStatus"));
-+ AddToMessageLog(MSG_FLAGS_ERROR, TEXT("SetServiceStatus"));
- }
- }
- return fResult;
-@@ -288,28 +297,33 @@
- //
- // COMMENTS:
- //
--VOID AddToMessageLog(LPTSTR lpszMsg)
-+void AddToMessageLog(DWORD flags, LPTSTR lpszMsg)
- {
- TCHAR szMsg [(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100 ];
- HANDLE hEventSource;
-- LPTSTR lpszStrings[2];
-+ LPCSTR lpszStrings[2];
-
- if ( !bDebug )
- {
-+ if (flags & MSG_FLAGS_SYS_CODE)
- dwErr = GetLastError();
-+ else
-+ dwErr = 0;
-
- // Use event logging to log the error.
- //
- hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
-
-- _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr);
-+ _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), (int)dwErr);
- lpszStrings[0] = szMsg;
- lpszStrings[1] = lpszMsg;
-
- if (hEventSource != NULL)
- {
- ReportEvent(hEventSource, // handle of event source
-- EVENTLOG_ERROR_TYPE, // event type
-+ // event type
-+ (flags & MSG_FLAGS_ERROR)
-+ ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
- 0, // event category
- 0, // event ID
- NULL, // current user's SID
-@@ -323,8 +337,10 @@
- }
- }
-
--
--
-+void ResetError (void)
-+{
-+ dwErr = 0;
-+}
-
- ///////////////////////////////////////////////////////////////////
- //
-@@ -341,21 +357,23 @@
- // none
- //
- // RETURN VALUE:
--// none
-+// 0 if success
- //
- // COMMENTS:
- //
--void CmdInstallService()
-+int CmdInstallService()
- {
- SC_HANDLE schService;
- SC_HANDLE schSCManager;
-
- TCHAR szPath[512];
-
-+ int ret = 0;
-+
- if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
- {
- _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256));
-- return;
-+ return 1;
- }
-
- schSCManager = OpenSCManager(
-@@ -371,7 +389,7 @@
- TEXT(SZSERVICEDISPLAYNAME), // name to display
- SERVICE_QUERY_STATUS, // desired access
- SERVICE_WIN32_OWN_PROCESS, // service type
-- SERVICE_DEMAND_START, // start type
-+ SERVICE_DEMAND_START, // start type -- alternative: SERVICE_AUTO_START
- SERVICE_ERROR_NORMAL, // error control type
- szPath, // service's binary
- NULL, // no load ordering group
-@@ -388,16 +406,79 @@
- else
- {
- _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256));
-+ ret = 1;
- }
-
- CloseServiceHandle(schSCManager);
- }
- else
-+ {
- _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-+ return ret;
- }
-
-+//
-+// FUNCTION: CmdStartService()
-+//
-+// PURPOSE: Start the service
-+//
-+// PARAMETERS:
-+// none
-+//
-+// RETURN VALUE:
-+// 0 if success
-+//
-+// COMMENTS:
-+
-+int CmdStartService()
-+{
-+ int ret = 0;
-+
-+ SC_HANDLE schSCManager;
-+ SC_HANDLE schService;
-
-
-+ // Open a handle to the SC Manager database.
-+ schSCManager = OpenSCManager(
-+ NULL, // local machine
-+ NULL, // ServicesActive database
-+ SC_MANAGER_ALL_ACCESS); // full access rights
-+
-+ if (NULL == schSCManager) {
-+ _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-+
-+ schService = OpenService(
-+ schSCManager, // SCM database
-+ SZSERVICENAME, // service name
-+ SERVICE_ALL_ACCESS);
-+
-+ if (schService == NULL) {
-+ _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-+
-+ if (!StartService(
-+ schService, // handle to service
-+ 0, // number of arguments
-+ NULL) ) // no arguments
-+ {
-+ _tprintf(TEXT("StartService failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-+ else
-+ {
-+ _tprintf(TEXT("Service Started\n"));
-+ ret = 0;
-+ }
-+ CloseServiceHandle(schService);
-+ CloseServiceHandle(schSCManager);
-+ return ret;
-+}
-+
- //
- // FUNCTION: CmdRemoveService()
- //
-@@ -407,15 +488,17 @@
- // none
- //
- // RETURN VALUE:
--// none
-+// 0 if success
- //
- // COMMENTS:
- //
--void CmdRemoveService()
-+int CmdRemoveService()
- {
- SC_HANDLE schService;
- SC_HANDLE schSCManager;
-
-+ int ret = 0;
-+
- schSCManager = OpenSCManager(
- NULL, // machine (NULL == local)
- NULL, // database (NULL == default)
-@@ -447,7 +530,10 @@
- if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
- _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
- else
-+ {
- _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );
-+ ret = 1;
-+ }
-
- }
-
-@@ -455,18 +541,28 @@
- if ( DeleteService(schService) )
- _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
- else
-+ {
- _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-
-
- CloseServiceHandle(schService);
- }
- else
-+ {
- _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-
- CloseServiceHandle(schSCManager);
- }
- else
-+ {
- _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
-+ ret = 1;
-+ }
-+ return ret;
- }
-
-
-@@ -587,7 +683,7 @@
- else
- {
- lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
-- _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
-+ _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, (int)GetLastError() );
- }
-
- if ( lpszTemp )
---- service.h.orig Tue Apr 24 14:49:30 2007
-+++ service.h Tue Apr 24 11:58:48 2007
-@@ -57,18 +57,19 @@
- extern "C" {
- #endif
-
-+#include "../autodefs/defs.h"
-
- //////////////////////////////////////////////////////////////////////////////
- //// todo: change to desired strings
- ////
- // name of the executable
--#define SZAPPNAME "Simple"
-+#define SZAPPNAME PRODUCT_UNIX_NAME "serv"
- // internal name of the service
--#define SZSERVICENAME "SimpleService"
-+#define SZSERVICENAME PRODUCT_NAME "Service"
- // displayed name of the service
--#define SZSERVICEDISPLAYNAME "Simple Service"
-+#define SZSERVICEDISPLAYNAME PRODUCT_NAME " Service"
- // list of service dependencies - "dep1\0dep2\0\0"
--#define SZDEPENDENCIES ""
-+#define SZDEPENDENCIES PRODUCT_TAP_ID "\0Dhcp\0\0"
- //////////////////////////////////////////////////////////////////////////////
-
-
-@@ -126,7 +127,10 @@
- // RETURN VALUE:
- // none
- //
-- void AddToMessageLog(LPTSTR lpszMsg);
-+# define MSG_FLAGS_ERROR (1<<0)
-+# define MSG_FLAGS_SYS_CODE (1<<1)
-+ void AddToMessageLog(DWORD flags, LPTSTR lpszMsg);
-+ void ResetError (void);
- //////////////////////////////////////////////////////////////////////////////
-
-