/* Copyright (C) 2008 Andreas Baumann This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 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. If not, see . */ #include "log/log.h" /* for logging */ #include "log/messages.h" /* for i18n */ #include "port/string.h" /* for strcasecmp */ #include "port/gettext.h" /* for i18n */ #include "errors.h" #include "service/service.h" #include #include #include #include #define WOLF_CATEGORY_TESTSERVICE WOLF_LAST_INTERNAL_CATEGORY+1 #define WOLF_MSG_TESTSERVICE_BASE ( WOLF_CATEGORY_TESTSERVICE ) * 1000 #define WOLF_MSG_TESTSERVICE_STARTED_SERVICE WOLF_MSG_TESTSERVICE_BASE+1 #define WOLF_MSG_TESTSERVICE_SUSPENDING_ON_EVENT_ERROR WOLF_MSG_TESTSERVICE_BASE+2 #define WOLF_MSG_TESTSERVICE_GOT_TERMINATION_EVENT WOLF_MSG_TESTSERVICE_BASE+3 #define WOLF_MSG_TESTSERVICE_STOPPED_SERVICE WOLF_MSG_TESTSERVICE_BASE+4 #define SERVICE_NAME "testservice" #define SERVICE_DISPLAY_NAME "Wolf Test Service" #define SERVICE_DESCRIPTION "A test service doing nothing" static void WINAPI service_main( DWORD argc, LPTSTR *argv ) { wolf_error_t error; wolf_service_event_t event = WOLF_SERVICE_NO_EVENT; wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_STARTED_SERVICE, _( "Started %s service" ), SERVICE_NAME ); while( event != WOLF_SERVICE_EVENT_TERMINATE ) { event = wolf_service_events_suspend( 60, &error ); switch( event ) { case WOLF_SERVICE_NO_EVENT: if( error == WOLF_ERR_TIMEOUT ) { /* do something periodic here */ } else { /* an error occurred */ wolf_log( WOLF_LOG_CRIT, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_SUSPENDING_ON_EVENT_ERROR, _( "Suspending on a service event resulted in an error %d!" ), error ); } break; case WOLF_SERVICE_EVENT_TERMINATE: wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_GOT_TERMINATION_EVENT, _( "Got termination event, shutting down the service" ) ); break; } } wolf_log( WOLF_LOG_NOTICE, WOLF_CATEGORY_TESTSERVICE, WOLF_MSG_TESTSERVICE_STOPPED_SERVICE, _( "Stopped %s service" ), SERVICE_NAME ); } void __cdecl _tmain( int argc, TCHAR *argv[] ) { wolf_log_openlogtostderr( WOLF_LOG_DEBUG ); wolf_log_openlogtofile( "testservice.log", WOLF_LOG_DEBUG ); wolf_log_openlogtoeventlog( NULL, "Application", "testservice", "C:\\Temp\\testservicemsg.dll", WOLF_LAST_INTERNAL_CATEGORY+1, WOLF_LOG_DEBUG ); /* called as service, dispatch the main service thread */ if( argc < 2 ) { wolf_error_t error; error = wolf_service_start( SERVICE_NAME, &service_main ); if( error == WOLF_ERR_INVALID_STATE ) { /* running in console */ (void)wolf_service_start_console( SERVICE_NAME, &service_main, argc, argv ); } /* here we get when the service is stopping */ return; } /* not as service: provide functions like installation and * deinstallation of the service. Also starting or stopping * is an option here for easy handling from the shell. */ if( strcasecmp( argv[1], "/help" ) == 0 ) { printf( "testsrevice [options]\r\n" ); printf( " /help show this help page\r\n" ); printf( " /install install the service\r\n" ); printf( " /remove remove the service\r\n" ); } else if( strcasecmp( argv[1], "/install" ) == 0 ) { wolf_service_params_t params; params.service_name = SERVICE_NAME; params.service_display_name = SERVICE_DISPLAY_NAME; params.service_description = SERVICE_DESCRIPTION; (void)wolf_service_install( params ); } else if( strcasecmp( argv[1], "/remove" ) == 0 ) { (void)wolf_service_remove( SERVICE_NAME ); } else { fprintf( stderr, "Illegal option '%s'\r\n", argv[1] ); } wolf_log_closelogtoeventlog( ); wolf_log_closelogtofile( ); wolf_log_closelogtostderr( ); }