1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>
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 <http://www.gnu.org/licenses/>.
*/
#include "log/log.h" /* for logging */
#include "log/messages.h" /* for i18n */
#include "port/string.h" /* for strcasecmp */
#include "errors.h"
#include "service/service.h"
#include <windows.h>
#include <tchar.h>
#include <WinSvc.h>
#include <stdio.h>
#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( );
}
|