forked from godspeed1989/WDUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv_run.c
206 lines (181 loc) · 4.15 KB
/
srv_run.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <windows.h>
#include <stdio.h>
char ____DEVICE_BASENAME[128];
//
// Get system error message string
//
PCSTR SystemMessage(
DWORD nError)
{
static CHAR msg[256];
if (!FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, 0, msg, sizeof(msg), NULL)) {
_snprintf(msg, sizeof(msg),
"Unknown system error %lu (0x%08x)\n", nError, nError);
}
return msg;
}
DWORD WINAPI StartDriver()
{
SC_HANDLE hScManager; // Service Control Manager
SC_HANDLE hService; // Service (= Driver)
SERVICE_STATUS stat;
DWORD ret = ERROR_SUCCESS;
int i;
// Connect to the Service Control Manager
hScManager = OpenSCManager(NULL, NULL, 0);
if (hScManager == NULL)
{
ret = GetLastError();
fprintf(stderr, "failed to connect scm: %s\n", SystemMessage(ret));
return ret;
}
// Open the driver entry in the service database
hService = OpenService(
hScManager, // Service control manager
____DEVICE_BASENAME, // service name
SERVICE_START
| SERVICE_QUERY_STATUS); // service access mode
if (hService == NULL)
{
ret = GetLastError();
fprintf(stderr, "failed to open service: %s\n", SystemMessage(ret));
goto cleanup;
}
// Start the driver
if (!StartService(hService, 0, NULL))
{
ret = GetLastError();
fprintf(stderr, "failed to start service: %s\n", SystemMessage(ret));
goto cleanup;
}
// Wait until the driver is properly running
for (i = 0; i < 5; ++i)
{
if (!QueryServiceStatus(hService, &stat))
{
ret = GetLastError();
fprintf(stderr, "failed to query service: %s\n", SystemMessage(ret));
break;
}
if (stat.dwCurrentState == SERVICE_RUNNING)
break;
Sleep(1000);
}
if (stat.dwCurrentState == SERVICE_RUNNING)
{
fprintf(stderr, "service is running now\n");
}
else
{
fprintf(stderr, "failed to run service\n");
ret = ERROR_SERVICE_NOT_ACTIVE;
}
cleanup:
// Close the service object handle
if (hService) {
CloseServiceHandle(hService);
}
// Close handle to the service control manager.
if (hScManager) {
CloseServiceHandle(hScManager);
}
return ret;
}
DWORD WINAPI StopDriver()
{
SC_HANDLE hScManager; // Service Control Manager
SC_HANDLE hService; // Service (= Driver)
SERVICE_STATUS stat;
DWORD ret = ERROR_SUCCESS;
int i;
// Connect to the Service Control Manager
hScManager = OpenSCManager(NULL, NULL, 0);
if (hScManager == NULL)
{
ret = GetLastError();
fprintf(stderr, "failed to connect scm: %s\n", SystemMessage(ret));
return ret;
}
// Open the VFD driver entry in the service database
hService = OpenService(
hScManager, // Service control manager
____DEVICE_BASENAME, // service name
SERVICE_STOP
| SERVICE_QUERY_STATUS); // service access mode
if (hService == NULL)
{
ret = GetLastError();
fprintf(stderr, "failed to open service: %s\n", SystemMessage(ret));
goto cleanup;
}
// Stop the driver
if (!ControlService(hService, SERVICE_CONTROL_STOP, &stat))
{
ret = GetLastError();
fprintf(stderr, "failed to stop service\n");
goto cleanup;
}
// Wait until the driver is stopped
for (i = 0; i < 5; ++i)
{
Sleep(1000);
if (!QueryServiceStatus(hService, &stat))
{
ret = GetLastError();
fprintf(stderr, "failed to query service: %s\n", SystemMessage(ret));
break;
}
if (stat.dwCurrentState != SERVICE_RUNNING)
break;
}
if (stat.dwCurrentState != SERVICE_RUNNING)
{
fprintf(stderr, "service is stopped now\n");
}
else
{
fprintf(stderr, "failed to stop service: %s\n", SystemMessage(ret));
ret = ERROR_SERVICE_NOT_ACTIVE;
}
cleanup:
// Close the service object handle
if (hService) {
CloseServiceHandle(hService);
}
// Close handle to the service control manager.
if (hScManager) {
CloseServiceHandle(hScManager);
}
return ret;
}
int main(int argc, const char* argv[])
{
if(argc < 3)
goto ret;
strcpy(____DEVICE_BASENAME, argv[2]);
printf("service_name=%s\n", ____DEVICE_BASENAME);
switch(argv[1][0])
{
case 'r':
if(StartDriver())
{
printf("install error\n");
return 1;
}
break;
case 's':
if(StopDriver())
{
printf("remove error\n");
return 1;
}
break;
}
return 0;
ret:
fprintf(stderr, "Usage: r|s service_name\n");
return 1;
}