1
0
Fork 0

Windows service deletion - only remove the service entry if it matches our binary path.

This commit is contained in:
Willi Goesgens 2015-02-13 16:21:35 +01:00
parent 154882ae5f
commit da5f86f16b
1 changed files with 39 additions and 5 deletions

View File

@ -262,7 +262,14 @@ static void InstallService (int argc, char* argv[]) {
#ifdef _WIN32
static void DeleteService (int argc, char* argv[]) {
static void DeleteService (int argc, char* argv[], bool force) {
CHAR path[MAX_PATH] = "";
if(! GetModuleFileNameA(NULL, path, MAX_PATH)) {
std::cerr << "FATAL: GetModuleFileNameA failed" << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "INFO: removing service '" << ServiceName << "'" << std::endl;
SC_HANDLE schSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
@ -275,7 +282,33 @@ static void DeleteService (int argc, char* argv[]) {
SC_HANDLE schService = OpenServiceA(
schSCManager, // SCManager database
ServiceName.c_str(), // name of service
DELETE); // only need DELETE access
DELETE|SERVICE_QUERY_CONFIG); // first validate whether its us, then delete.
char serviceConfigMemory[8192]; // msdn says: 8k is enough.
DWORD bytesNeeded = 0;
if (QueryServiceConfig(schService,
(LPQUERY_SERVICE_CONFIGA)&serviceConfigMemory,
sizeof(serviceConfigMemory),
&bytesNeeded)) {
QUERY_SERVICE_CONFIG *cfg = (QUERY_SERVICE_CONFIG*) &serviceConfigMemory;
if (strcmp(cfg->lpBinaryPathName, path)) {
if (!force) {
std::cerr << "NOT removing service of other installation: " <<
cfg->lpBinaryPathName <<
"Our path is: " <<
path << std::endl;
CloseServiceHandle(schSCManager);
return;
}
else {
std::cerr << "Removing service of other installation because of FORCE: " <<
cfg->lpBinaryPathName <<
"Our path is: " <<
path << std::endl;
}
}
}
CloseServiceHandle(schSCManager);
@ -431,7 +464,8 @@ int main (int argc, char* argv[]) {
exit(EXIT_SUCCESS);
}
else if (TRI_EqualString(argv[1], "--uninstall-service")) {
DeleteService(argc, argv);
bool force = ((argc > 2) && !strcmp(argv[2], "--force"));
DeleteService(argc, argv, force);
exit(EXIT_SUCCESS);
}
else if (TRI_EqualString(argv[1], "--start-service")) {