summaryrefslogtreecommitdiff
path: root/tests/service/testservice.c
blob: 98d688da45fdf1d0b51467f6fc9319a7b7e527f5 (plain)
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
#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 <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_CANT_DISPATCH_SERVICE	WOLF_MSG_TESTSERVICE_BASE+1

#define SERVICE_NAME "testservice"
#define SERVICE_NAME_DESCR "Wolf Test Service"

void __cdecl _tmain( int argc, TCHAR *argv[] ) {
	char errbuf[512];

	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 ) {
		(void)wolf_service_start( SERVICE_NAME, &wolf_service_main );
		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_name_descr = SERVICE_NAME_DESCR;
		(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( );
}