jfclere 02/02/10 10:02:12 Modified: native/winnt/moni vdmonisvc.c Added: native/winnt Makefile native/winnt/moni Makefile native/winnt/service Makefile Log: Add Makefile for cygwin and arrange vdmonisvc.c. Revision Changes Path 1.1 jakarta-tomcat-service/native/winnt/Makefile Index: Makefile =================================================================== all: (cd service; make) (cd moni; make) 1.3 +242 -0 jakarta-tomcat-service/native/winnt/moni/vdmonisvc.c Index: vdmonisvc.c =================================================================== RCS file: /home/cvs/jakarta-tomcat-service/native/winnt/moni/vdmonisvc.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- vdmonisvc.c 31 Aug 2001 08:33:55 -0000 1.2 +++ vdmonisvc.c 10 Feb 2002 18:02:12 -0000 1.3 @@ -15,8 +15,104 @@ #include <stdio.h> #include <stdlib.h> #include <process.h> +#ifdef CYGWIN +#else #include <tchar.h> +#endif #include "moni_inst.h" + +/* globals */ +SERVICE_STATUS ssStatus; +SERVICE_STATUS_HANDLE sshStatusHandle; +DWORD dwErr; + +/* Event logger routines */ + +VOID AddToMessageLog(LPTSTR lpszMsg) +{ + TCHAR szMsg[256]; + HANDLE hEventSource; + LPTSTR lpszStrings[2]; + + + dwErr = GetLastError(); + + // Use event logging to log the error. + // + hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME)); + +#ifdef CYGWIN + sprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr); +#else + _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr); +#endif + lpszStrings[0] = szMsg; + lpszStrings[1] = lpszMsg; + + if (hEventSource != NULL) { + ReportEvent(hEventSource, // handle of event source + EVENTLOG_ERROR_TYPE, // event 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); + } +} + + +// +// 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 (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(TEXT("SetServiceStatus")); + } + return fResult; +} // this event is signalled when the // service should end @@ -222,4 +318,150 @@ { if ( hServerStopEvent ) SetEvent(hServerStopEvent); +} + + +// +// 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: 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) +{ + + AddToMessageLog(TEXT("service_main:starting")); + // register our service control handler: + // + sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), service_ctrl); + + if (!sshStatusHandle) { + AddToMessageLog(TEXT("service_main:RegisterServiceCtrlHandler failed")); + 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,NO_ERROR,3000)) { + AddToMessageLog(TEXT("service_main:ReportStatusToSCMgr failed")); + 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); + + AddToMessageLog(TEXT("service_main:stopped")); + return; +} + +// +// 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. +// +#ifdef CYGWIN +int main(int argc, char **argv) +#else +void _CRTAPI1 main(int argc, char **argv) +#endif +{ + SERVICE_TABLE_ENTRY dispatchTable[] = + { + { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main }, + { NULL, NULL } + }; + + AddToMessageLog(TEXT("StartService starting")); + if (!StartServiceCtrlDispatcher(dispatchTable)) { + AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed.")); + return; + } + AddToMessageLog(TEXT("StartService started")); } 1.1 jakarta-tomcat-service/native/winnt/moni/Makefile Index: Makefile =================================================================== INCLUDE=-I../lib all: ../bin/vdmoniadm ../bin/vdmonisvc ../bin/vdmoniadm: vdmoniadm.c gcc $(INCLUDE) -I../executables/vdmoniadm vdmoniadm.c \ -o ../bin/vdmoniadm -lgdi32 ../bin/vdmonisvc: vdmonisvc.c vdenv.o kills.o gcc $(INCLUDE) -I../executables/vdmonisvc \ -DCYGWIN vdmonisvc.c \ -o ../bin/vdmonisvc vdenv.o kills.o -lm vdenv.o: ../supcalls_nt/vdenv.c gcc $(INCLUDE) -c ../supcalls_nt/vdenv.c kills.o: ../signals/kills.c gcc $(INCLUDE) -c ../signals/kills.c 1.1 jakarta-tomcat-service/native/winnt/service/Makefile Index: Makefile =================================================================== INCLUDE=-I../lib ../bin/INSTSVC: instmain.cpp g++ $(INCLUDE) instmain.cpp -o ../bin/INSTSVC
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>