summaryrefslogtreecommitdiff
path: root/tests/service
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2009-03-26 17:26:00 +0100
committerAndreas Baumann <abaumann@yahoo.com>2009-03-26 17:26:00 +0100
commita92ee231c47556681311ae27fdfdb5e36d3e2582 (patch)
tree31c067d164b43d5c641aeaffd731473cf34777df /tests/service
parentcbc6aaca6dd7ac57a6115b3512a85045e51303dd (diff)
downloadwolfbones-a92ee231c47556681311ae27fdfdb5e36d3e2582.tar.gz
wolfbones-a92ee231c47556681311ae27fdfdb5e36d3e2582.tar.bz2
code moved, works again somehow, now cleanup in the service
Diffstat (limited to 'tests/service')
-rw-r--r--tests/service/testservice.c218
-rw-r--r--tests/service/testservicemsg.mc133
2 files changed, 10 insertions, 341 deletions
diff --git a/tests/service/testservice.c b/tests/service/testservice.c
index e229704..32796b8 100644
--- a/tests/service/testservice.c
+++ b/tests/service/testservice.c
@@ -4,6 +4,8 @@
#include "port/gettext.h" /* for i18n */
#include "errors.h"
+#include "service/service.h"
+
#include <windows.h>
#include <tchar.h>
#include <WinSvc.h>
@@ -13,220 +15,11 @@
#define WOLF_MSG_TESTSERVICE_BASE ( WOLF_CATEGORY_TESTSERVICE ) * 1000
-#define WOLF_MSG_TESTSERVICE_CANT_DISPATCH_SERVICE WOLF_MSG_TESTSERVICE_BASE+11
-#define WOLF_MSG_TESTSERVICE_CANT_REGISTER_SERVICE_CTRL_HANDLER WOLF_MSG_TESTSERVICE_BASE+12
-#define WOLF_MSG_TESTSERVICE_HANDLING_EVENT WOLF_MSG_TESTSERVICE_BASE+13
-#define WOLF_MSG_TESTSERVICE_STARTED_SERVICE WOLF_MSG_TESTSERVICE_BASE+14
-#define WOLF_MSG_TESTSERVICE_STOPPED_SERVICE WOLF_MSG_TESTSERVICE_BASE+15
-#define WOLF_MSG_TESTSERVICE_CANT_CREATE_STOP_EVENT WOLF_MSG_TESTSERVICE_BASE+16
-#define WOLF_MSG_TESTSERVICE_CANT_REPORT_STATUS WOLF_MSG_TESTSERVICE_BASE+17
-#define WOLF_MSG_TESTSERVICE_WAIT_FOR_OBJECT_FAILED WOLF_MSG_TESTSERVICE_BASE+18
-#define WOLF_MSG_TESTSERVICE_REPORT_STATUS WOLF_MSG_TESTSERVICE_BASE+19
+#define WOLF_MSG_TESTSERVICE_CANT_DISPATCH_SERVICE WOLF_MSG_TESTSERVICE_BASE+1
#define SERVICE_NAME "testservice"
#define SERVICE_NAME_DESCR "Wolf Test Service"
-/* FIXME: should be passed in the event contect as struct */
-static SERVICE_STATUS_HANDLE service_status_handle;
-static SERVICE_STATUS service_status;
-static HANDLE service_stop_event = NULL;
-
-/* constants and values from WinSvc.h: */
-static const char *wolf_service_state_to_str( DWORD current_state ) {
- switch( current_state ) {
- case SERVICE_STOPPED: return "SERVICE_STOPPED";
- case SERVICE_START_PENDING: return "SERVICE_START_PENDING";
- case SERVICE_STOP_PENDING: return "SERVICE_STOP_PENDING";
- case SERVICE_RUNNING: return "SERVICE_RUNNING";
- case SERVICE_CONTINUE_PENDING: return "SERVICE_CONTINUE_PENDING";
- case SERVICE_PAUSE_PENDING: return "SERVICE_PAUSE_PENDING";
- case SERVICE_PAUSED: return "SERVICE_PAUSED";
- default: return "<unknown service state>";
- }
-}
-
-/* constants and values from WinSvc.h: */
-static const char *wolf_service_control_to_str( DWORD control ) {
- switch( control ) {
- case SERVICE_CONTROL_STOP: return "SERVICE_CONTROL_STOP";
- case SERVICE_CONTROL_PAUSE: return "SERVICE_CONTROL_PAUSE";
- case SERVICE_CONTROL_CONTINUE: return "SERVICE_CONTROL_CONTINUE";
- case SERVICE_CONTROL_INTERROGATE: return "SERVICE_CONTROL_INTERROGATE";
- case SERVICE_CONTROL_SHUTDOWN: return "SERVICE_CONTROL_SHUTDOWN";
- case SERVICE_CONTROL_PARAMCHANGE: return "SERVICE_CONTROL_PARAMCHANGE";
- case SERVICE_CONTROL_NETBINDADD: return "SERVICE_CONTROL_NETBINDADD";
- case SERVICE_CONTROL_NETBINDREMOVE: return "SERVICE_CONTROL_NETBINDREMOVE";
- case SERVICE_CONTROL_NETBINDENABLE: return "SERVICE_CONTROL_NETBINDENABLE";
- case SERVICE_CONTROL_NETBINDDISABLE: return "SERVICE_CONTROL_NETBINDDISABLE";
- case SERVICE_CONTROL_DEVICEEVENT: return "SERVICE_CONTROL_DEVICEEVENT";
- case SERVICE_CONTROL_HARDWAREPROFILECHANGE: return "SERVICE_CONTROL_HARDWAREPROFILECHANGE";
- case SERVICE_CONTROL_POWEREVENT: return "SERVICE_CONTROL_POWEREVENT";
- case SERVICE_CONTROL_SESSIONCHANGE: return "SERVICE_CONTROL_SESSIONCHANGE";
- default: return "<unknown service control>";
- }
-}
-
-static void wolf_service_report_status( DWORD current_state,
- DWORD exit_code,
- DWORD wait_hint ) {
- BOOL res;
- char errbuf[512];
-
- wolf_log( WOLF_LOG_DEBUG, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_REPORT_STATUS,
- _( "reporting status with new state '%s' (%d) (currently in state '%s' (%d))" ),
- wolf_service_state_to_str( current_state ), current_state,
- wolf_service_state_to_str( service_status.dwCurrentState ), service_status.dwCurrentState );
-
- service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
- service_status.dwCurrentState = current_state;
- service_status.dwWin32ExitCode = exit_code;
- service_status.dwServiceSpecificExitCode = 0;
- service_status.dwWaitHint = wait_hint;
- service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
-
- switch( current_state ) {
- case SERVICE_START_PENDING:
- /* during startup we should not accept events, otherwise our
- * state machine could get troubled!
- */
- service_status.dwControlsAccepted = 0;
- break;
-
- case SERVICE_RUNNING:
- case SERVICE_STOPPED:
- /* reset checkpoint for status bar in final states */
- service_status.dwCheckPoint = 0;
- break;
-
- defaut:
- /* increate the tick for transient states */
- service_status.dwCheckPoint++;
- break;
- }
-
- res = SetServiceStatus( service_status_handle, &service_status );
- if( !res ) {
- WOLF_LOG_GET_LAST_ERROR( GetLastError( ), errbuf, 512 );
- wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_CANT_REPORT_STATUS,
- _( "Unable to report state '%s' (%d) of service '%s' to SCM '%s (%d)" ),
- wolf_service_state_to_str( current_state ), current_state,
- SERVICE_NAME, errbuf, GetLastError( ) );
- return;
- }
-}
-
-/* we get called here by the system and the service control manager,
- * be as short as possible, report states and trigger new events at
- * most!
- */
-void WINAPI wolf_service_ctrl_handler( DWORD control ) {
- char errbuf[512];
- BOOL res;
-
- wolf_log( WOLF_LOG_DEBUG, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_HANDLING_EVENT,
- _( "service handler received status change '%s' (%d)" ),
- wolf_service_control_to_str( control ),
- control );
-
- switch( control ) {
- case SERVICE_CONTROL_STOP:
- wolf_service_report_status( SERVICE_STOP_PENDING, NO_ERROR, 1000 );
- SetEvent( service_stop_event );
- break;
-
- case SERVICE_CONTROL_INTERROGATE:
- /* fall through to send current status */
- break;
- }
-
- /* report current state */
- wolf_service_report_status( service_status.dwCurrentState, NO_ERROR, 1000 );
-}
-
-
-void WINAPI wolf_service_main( DWORD argc, LPTSTR *argv ) {
- char errbuf[512];
- BOOL res;
-
- /* all other loggers make no sense: a service is not supposed
- * to have a logfile or interaction with the user in any way!
- */
- wolf_log_openlogtoeventlog( NULL, "Application", "testservice",
- "C:\\Temp\\testservicemsg.dll", WOLF_LAST_INTERNAL_CATEGORY+1, WOLF_LOG_DEBUG );
-
- /* register the event callback where we get called by the service
- * manager and the system
- */
- service_status_handle = RegisterServiceCtrlHandler(
- SERVICE_NAME, wolf_service_ctrl_handler );
-
- if( service_status_handle == 0 ) {
- WOLF_LOG_GET_LAST_ERROR( GetLastError( ), errbuf, 512 );
- wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_CANT_REGISTER_SERVICE_CTRL_HANDLER,
- _( "Unable to register service control handler function for service '%s': %s (%d)" ),
- SERVICE_NAME, errbuf, GetLastError( ) );
- return;
- }
-
- /* signal that we are now up and running */
- wolf_service_report_status( SERVICE_START_PENDING, NO_ERROR, 3000 );
-
- /* register a stop event, the service control handler will send
- * the event. From now on we have a service event handler installed,
- * so if something goes wrong we can set the service state to
- * SERVICE_STOPPED and terminate gracefully.
- */
- service_stop_event = CreateEvent(
- NULL, /* default security attributes */
- TRUE, /* manual reset event */
- FALSE, /* not signalled */
- NULL ); /* no name */
- if( service_stop_event == NULL ) {
- WOLF_LOG_GET_LAST_ERROR( GetLastError( ), errbuf, 512 );
- wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_CANT_CREATE_STOP_EVENT,
- _( "Unable to create the stop event for service '%s': %s (%d)" ),
- SERVICE_NAME, errbuf, GetLastError( ) );
- wolf_service_report_status( SERVICE_STOPPED, NO_ERROR, 1000 );
- }
-
- wolf_service_report_status( SERVICE_RUNNING, NO_ERROR, 1000 );
- wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_STARTED_SERVICE,
- _( "Started %s service" ), SERVICE_NAME );
-
- /* main loop */
- while( 1 ) {
- DWORD r;
- r = WaitForSingleObject( service_stop_event, 1000 );
- switch( r ) {
- case WAIT_OBJECT_0:
- /* stop signal received */
- goto SERVICE_END;
-
- case WAIT_TIMEOUT:
- /* we can do periodic things here */
- break;
-
- default:
- WOLF_LOG_GET_LAST_ERROR( GetLastError( ), errbuf, 512 );
- wolf_log( WOLF_LOG_ERR, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_WAIT_FOR_OBJECT_FAILED,
- _( "Waiting for the stop event for service '%s' failed: %s (%d)" ),
- SERVICE_NAME, errbuf, GetLastError( ) );
- wolf_service_report_status( SERVICE_STOPPED, NO_ERROR, 1000 );
- return;
- }
- }
-
-SERVICE_END:
- wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_STOPPED_SERVICE,
- _( "Stopped %s service" ), SERVICE_NAME );
-
- wolf_service_report_status( SERVICE_STOPPED, NO_ERROR, 1000 );
-
- wolf_log_closelogtoeventlog( );
-
- return;
-}
-
void __cdecl _tmain( int argc, TCHAR *argv[] ) {
char errbuf[512];
@@ -263,7 +56,10 @@ void __cdecl _tmain( int argc, TCHAR *argv[] ) {
printf( " /install install the service\r\n" );
printf( " /remove remove the service\r\n" );
} else if( strcasecmp( argv[1], "/install" ) == 0 ) {
- (void)wolf_service_install( SERVICE_NAME, SERVICE_NAME_DESCR );
+ wolf_service_params_t params;
+ params.service_name = SERVICE_NAME;
+ params.service_name_descr = SERVICE_NAME_DESCR;
+ (void)wolf_service_install( params );
} else if( strcasecmp( argv[1], "/remove" ) == 0 ) {
(void)wolf_service_remove( SERVICE_NAME );
} else {
diff --git a/tests/service/testservicemsg.mc b/tests/service/testservicemsg.mc
index d90e415..cb0230f 100644
--- a/tests/service/testservicemsg.mc
+++ b/tests/service/testservicemsg.mc
@@ -37,11 +37,10 @@ Daemon
MessageId = 0x3
SymbolicName = WOLF_CATEGORY_SERVICE
Language = English
-Service
-.
+test service
;Language = German
;Service
-;.
+.
MessageId = 0x4
SymbolicName = WOLF_CATEGORY_TESTSERVICE
@@ -70,133 +69,7 @@ FacilityNames = (
MessageIdTypedef = DWORD
-MessageId = 3002
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_INSTALLING_SERVICE
-Language = English
-Installing service '%1'
-.
-MessageId = 3001
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_OPEN_SCM_FOR_INSTALL
-Language = English
-Unable to open the service control manager to register service '%1': %2 (%3)
-.
-MessageId = 3003
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_GET_PATH_OF_BINARY
-Language = English
-Unable to get full path of the binary of service '%1': %2 (%3)
-.
-MessageId = 3004
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_CREATE_SERVICE
-Language = English
-Unable to create service '%1': %2 (%3)
-.
-MessageId = 3005
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_SERVICE_INSTALLED
-Language = English
-Service '%1' installed successfully
-.
-MessageId = 3006
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_UNINSTALLING_SERVICE
-Language = English
-Unistalling service '%1'
-.
-MessageId = 3008
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_OPEN_SCM_FOR_UNINSTALL
-Language = English
-Unable to open the service control manager to uninstall '%1': %2 (%3)
-.
-MessageId = 3009
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_OPEN_SERVICE
-Language = English
-Unable to open service '%1': %2 (%3)
-.
-MessageId = 3010
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_DELETE_SERVICE
-Language = English
-Unable to uninstall service '%1': %2 (%3)
-.
-MessageId = 3007
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_SERVICE_UNINSTALLED
-Language = English
-Service '%1' uninstalled successfully
-.
-MessageId = 3019
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_REPORT_STATUS
-Language = English
-reporting status with new state '%1' (%2) (currently in state '%3' (%4))
-.
-MessageId = 3017
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_REPORT_STATUS
-Language = English
-Unable to report state '%1' (%2) of service '%3' to SCM '%4 (%5)
-.
-MessageId = 3013
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_HANDLING_EVENT
-Language = English
-service handler received status change '%1' (%2)
-.
-MessageId = 3012
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_REGISTER_SERVICE_CTRL_HANDLER
-Language = English
-Unable to register service control handler function for service '%1': %2 (%3)
-.
-MessageId = 3016
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_CANT_CREATE_STOP_EVENT
-Language = English
-Unable to create the stop event for service '%1': %2 (%3)
-.
-MessageId = 3014
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_STARTED_SERVICE
-Language = English
-Started %1 service
-.
-MessageId = 3018
-Severity = Error
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_WAIT_FOR_OBJECT_FAILED
-Language = English
-Waiting for the stop event for service '%1' failed: %2 (%3)
-.
-MessageId = 3015
-Severity = Informational
-Facility = Application
-SymbolicName = WOLF_MSG_TESTSERVICE_STOPPED_SERVICE
-Language = English
-Stopped %1 service
-.
-MessageId = 3011
+MessageId = 4001
Severity = Error
Facility = Application
SymbolicName = WOLF_MSG_TESTSERVICE_CANT_DISPATCH_SERVICE