Project

General

Profile

Download (3.19 KB) Statistics
| Branch: | Revision:

root / svctest.c @ master

1
/*
2
 * Test framework for win-svc
3
 *
4
 */
5

    
6
#include <windows.h>
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <getopt.h>
10

    
11
#include "scm.h"
12

    
13
/*
14
 * log a debug message
15
 */
16
int dprintf_level = 1;
17
int dprintf_to_stdout = 0;
18
int dprintf(unsigned char severity, const char *fmt, ...) {
19
        va_list args;
20
        char buf[1024];
21
        int i;
22

    
23
        if (severity > dprintf_level)
24
                return 0;
25

    
26
        va_start(args,fmt);
27
        i=vsnprintf(buf,sizeof(buf),fmt,args);
28
        va_end(args);
29

    
30
        if (dprintf_to_stdout) {
31
                printf("%s",buf);
32
        } else {
33
                OutputDebugStringA(buf);
34
        }
35
        return i;
36
}
37
#define trace(s) \
38
	dprintf(1,"%s:%i(%s) %s\n",__FILE__,__LINE__,__FUNCTION__,s)
39

    
40
int svctest_init(int argc, char **argv);
41
int svctest_main(int argc, char **argv);
42
int svctest_stop(void *);
43

    
44
struct SCM_def sd = {
45
        .name = "svctest",
46
        .desc = "svctest - test win-scm",
47
        .init = svctest_init,
48
        .main = svctest_main,
49
        .stop = svctest_stop,
50
};
51

    
52
static int do_getopt(const int argc, char **argv) {
53
	static struct option long_options[] = {
54
		{"install", 1, 0, 'i'},
55
		{"remove", 0, 0, 'r'},
56
		{"debug", 2, 0, 'd'},
57
		{0,0,0,0}
58
	};
59

    
60
	while(1) {
61
		int c = getopt_long(argc,argv, "ird::",
62
			long_options,NULL);
63
		if (c==-1)
64
			break;
65

    
66
		switch(c) {
67
			case 'i': {
68
				/* request service installation */
69
				char *path = SCM_Install(&sd,optarg);
70
				if (!path) {
71
					printf("Service installation failed\n");
72
					return 2;
73
				}
74
				printf("Service '%s' installed, binary path '%s'\n",sd.name,path);
75
				printf("You should now start the service using the service manager.\n");
76
				return 1;
77
			}
78
			case 'r':
79
				// request service removal
80
				if (SCM_Remove(&sd)==0) {
81
					printf("Deleted service '%s'\n",sd.name);
82
				} else {
83
					printf("Service removal failed\n");
84
				}
85
				return 1;
86
		}
87
	}
88
	return 0;
89
}
90

    
91

    
92
int svctest_init(int argc, char **argv) {
93
	dprintf(1,"%s:%i(%s) argc==%i\n",__FILE__,__LINE__,__FUNCTION__,argc);
94
	int i;
95
	for (i=0;i<argc;i++) {
96
		dprintf(1,"%s:%i: argv==%s\n",__FILE__,__LINE__,argv[i]);
97
	}
98
	dprintf(1,"%s:%i(%s) sd.mode==%i\n",__FILE__,__LINE__,__FUNCTION__,sd.mode);
99

    
100
	if (do_getopt(argc,argv)) {
101
		/* do_getopt returns nonzero if we should not continue */
102
		return 1;
103
	}
104

    
105
	char **env = environ;
106
	while(*env) {
107
		dprintf(1,"%s:%i: env==%s\n",__FILE__,__LINE__,*env);
108
		env++;
109
	}
110

    
111
	dprintf(1,"%s:%i: getenv(USERNAME)==%s\n",__FILE__,__LINE__,getenv("USERNAME"));
112

    
113
	char buf[100];
114
	int res = GetConsoleTitle(&buf,sizeof(buf));
115
	dprintf(1,"%s:%i: GetConsoleTitle()==%i, %s\n",__FILE__,__LINE__,
116
		res,buf);
117
	trace("return 0");
118
        return 0;
119
}
120

    
121
int run = 1;
122
int svctest_main(int argc, char **argv) {
123
	while(run) {
124
		1;
125
		/* FIXME - sleep */
126
	}
127
	trace("return 0");
128
        return 0;
129
}
130

    
131
int svctest_stop(void *param1) {
132
	run = 0;
133
	trace("return 0");
134
        return 0;
135
}
136

    
137
int main(int argc, char **argv)
138
{
139
	dprintf(1,"%s:%i: argc==%i\n",__FILE__,__LINE__,argc);
140
	char **arg = argv;
141
	while(*arg) {
142
		dprintf(1,"%s:%i: argv==%s\n",__FILE__,__LINE__,*arg);
143
		arg++;
144
	}
145

    
146
        if (SCM_Start(&sd,argc,argv)!=SVC_OK) {
147
		dprintf(1,"%s:%i: SCM_Start!=SVC_OK\n",__FILE__,__LINE__);
148
                return 1;
149
        }
150

    
151
	trace("return 0");
152
        return 0;
153
}
154

    
(11-11/14)