Files
yttrium-virtio-gpu/Balloon/app/utils.cpp
T
Ladi Prosek 08c0ce54af balloon: Static analysis: blnsvr fixes
Static analysis was failing with:

app\device.cpp(190): warning C6258: Using TerminateThread does not allow proper thread clean up.
app\main.cpp(30): warning C28285: For function 'wmain' '_Param_(2)' syntax error in 'SAL_readableTo(elementCount(Argc))' near 'Argc))'.
app\main.cpp(51): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\memstat.cpp(28): warning C6214: Cast between semantically different integer types:  HRESULT to a Boolean type.
app\memstat.cpp(162): warning C6386: Buffer overrun while writing to 'this->m_Stats':  the writable size is '70' bytes, but '80' bytes might be written.
app\service.cpp(231): warning C6031: Return value ignored: 'QueryServiceStatus'.
app\service.cpp(257): warning C6001: Using uninitialized memory 'CurrentState'.
app\utils.cpp(28): warning C6387: 'pLog' could be '0':  this does not adhere to the specification for the function 'fprintf'.
app\utils.cpp(29): warning C6387: 'pLog' could be '0':  this does not adhere to the specification for the function 'fclose'. See line 28 for an earlier location where this can occur
app\utils.cpp(104): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 81 for an earlier location where this can occur
app\utils.cpp(80): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CreateServiceW'.
app\utils.cpp(140): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'DeleteService'. See line 126 for an earlier location where this can occur
app\utils.cpp(147): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 126 for an earlier location where this can occur
app\utils.cpp(148): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 121 for an earlier location where this can occur
app\utils.cpp(133): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'ControlService'. See line 126 for an earlier location where this can occur
app\utils.cpp(121): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(126): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'QueryServiceStatus'.
app\utils.cpp(178): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(197): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(200): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(221): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 167 for an earlier location where this can occur
app\utils.cpp(167): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(255): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 238 for an earlier location where this can occur
app\utils.cpp(254): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'.
app\utils.cpp(238): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(245): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'ControlService'.
app\utils.cpp(248): warning C6001: Using uninitialized memory 'res'.
app\utils.cpp(279): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'QueryServiceConfigW'.
app\utils.cpp(273): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(292): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 279 for an earlier location where this can occur
app\utils.cpp(293): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 273 for an earlier location where this can occur
app\utils.cpp(284): warning C28182: Dereferencing NULL pointer. 'buffer' contains the same NULL value as 'LocalAlloc()`278' did.
app\utils.cpp(317): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'ChangeServiceConfigW'.
app\utils.cpp(339): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 318 for an earlier location where this can occur
app\utils.cpp(335): warning C6387: 'lock' could be '0':  this does not adhere to the specification for the function 'UnlockServiceDatabase'.
app\utils.cpp(340): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 309 for an earlier location where this can occur
app\utils.cpp(309): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'LockServiceDatabase'.
app\utils.cpp(313): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'. See line 309 for an earlier location where this can occur

This commit fixes everything except for:

app\device.cpp(190): warning C6258: Using TerminateThread does not allow proper thread clean up.
app\utils.cpp(178): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(197): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(200): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem

Signed-off-by: Ladi Prosek <[email protected]>
2017-12-16 21:35:58 +02:00

346 lines
9.0 KiB
C++

#include "stdafx.h"
extern LPWSTR ServiceName;
extern LPWSTR DisplayName;
extern CService srvc;
void ErrorHandler(char *s, int err)
{
printf("Failed. Error %d ", err );
LPTSTR lpMsgBuf;
if (FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL) > 0) {
_tprintf(L"%s\n", lpMsgBuf);
LocalFree(lpMsgBuf);
} else {
printf("unknown error\n");
}
FILE* pLog;
if (fopen_s(&pLog, "balloon.log", "a") == 0 && pLog) {
fprintf(pLog, "%s failed, error code = %d\n", s, err);
fclose(pLog);
}
ExitProcess(err);
}
void PrintMessage(char *s)
{
#ifdef DBG
FILE* pLog;
if (fopen_s(&pLog, "balloon.log", "a") == 0) {
fprintf(pLog, "%s\n", s);
fclose(pLog);
}
#endif
}
void ShowUsage()
{
printf("\n");
printf("USAGE:\n");
printf("blnsvr -i\tInstall service\n");
printf("blnsvr -u\tUninstall service\n");
printf("blnsvr -r\tRun service\n");
printf("blnsvr -s\tStop service\n");
printf("blnsvr status\tCurrent status\n");
printf("\n");
}
BOOL InstallService()
{
SC_HANDLE newService;
SC_HANDLE scm;
TCHAR szBuffer[255];
TCHAR szPath[MAX_PATH];
GetModuleFileName( GetModuleHandle(NULL), szPath, MAX_PATH );
if (FAILED( StringCchCopy(szBuffer, 255, TEXT("\"")))) {
return FALSE;
}
if (FAILED( StringCchCat(szBuffer, 255, szPath))) {
return FALSE;
}
if (FAILED( StringCchCat(szBuffer, 255, TEXT("\"")))) {
return FALSE;
}
scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (scm == NULL) {
ErrorHandler("OpenSCManager", GetLastError());
}
newService = CreateService(
scm,
ServiceName,
DisplayName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
szBuffer,
NULL,
NULL,
NULL,
NULL,
NULL
);
if (!newService) {
ErrorHandler("CreateService", GetLastError());
return FALSE;
} else {
printf("Service Installed\n");
ServiceRun();
}
CloseServiceHandle(newService);
CloseServiceHandle(scm);
return TRUE;
}
BOOL UninstallService()
{
SC_HANDLE service;
SC_HANDLE scm;
BOOL res;
SERVICE_STATUS status;
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!scm) {
ErrorHandler("OpenSCManager", GetLastError());
}
service = OpenService(scm, ServiceName, SERVICE_ALL_ACCESS | DELETE);
if (!service) {
ErrorHandler("OpenService", GetLastError());
}
res = QueryServiceStatus(service, &status);
if (!res) {
ErrorHandler("QueryServiceStatus", GetLastError());
}
if (status.dwCurrentState != SERVICE_STOPPED) {
printf("Stopping service...\n");
res = ControlService(service, SERVICE_CONTROL_STOP, &status);
if (!res) {
ErrorHandler("ControlService", GetLastError());
}
Sleep(5000);
}
res = DeleteService(service);
if (res) {
printf("Service Uninstalled\n");
} else {
ErrorHandler("DeleteService", GetLastError());
}
CloseServiceHandle(service);
CloseServiceHandle(scm);
return TRUE;
}
BOOL ServiceRun()
{
SC_HANDLE scm, Service;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwStatus;
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!scm) {
ErrorHandler("OpenSCManager", GetLastError());
}
Service = OpenService(scm, ServiceName, SERVICE_ALL_ACCESS);
if (!Service) {
ErrorHandler("OpenService", GetLastError());
return FALSE;
} else {
StartService(Service, 0, NULL);
srvc.GetStatus(Service);
if (!QueryServiceStatus( Service, &ssStatus) ) {
ErrorHandler("QueryServiceStatus", GetLastError());
}
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING) {
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 ) {
dwWaitTime = 1000;
} else if ( dwWaitTime > 10000 ) {
dwWaitTime = 10000;
}
Sleep( dwWaitTime );
if (!QueryServiceStatus(Service, &ssStatus) ) {
break;
}
if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) {
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
} else {
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) {
break;
}
}
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING) {
srvc.GetStatus(Service);
dwStatus = NO_ERROR;
} else {
printf("\nService not started.\n");
printf(" Current State: %d\n", ssStatus.dwCurrentState);
printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
printf(" Service Specific Exit Code: %d\n", ssStatus.dwServiceSpecificExitCode);
printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
dwStatus = GetLastError();
}
}
CloseServiceHandle(scm);
CloseServiceHandle(Service);
return TRUE;
}
BOOL ServiceControl(int ctrl)
{
SC_HANDLE service;
SC_HANDLE scm;
BOOL res = TRUE;
SERVICE_STATUS status;
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!scm) {
ErrorHandler("OpenSCManager", GetLastError());
}
service = OpenService(scm, ServiceName, SERVICE_ALL_ACCESS);
if (!service) {
ErrorHandler("OpenService", GetLastError());
}
if (ctrl == SERVICE_CONTROL_STOP) {
printf("Service is stopping...\n");
res = ControlService(service, SERVICE_CONTROL_STOP, &status);
}
if (!res) {
ErrorHandler("ControlService", GetLastError());
} else {
srvc.GetStatus(service);
}
CloseServiceHandle(service);
CloseServiceHandle(scm);
return TRUE;
}
BOOL GetConfiguration()
{
SC_HANDLE service;
SC_HANDLE scm;
BOOL res;
LPQUERY_SERVICE_CONFIG buffer;
DWORD sizeNeeded;
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!scm) {
ErrorHandler("OpenSCManager", GetLastError());
}
service = OpenService(scm, ServiceName, SERVICE_QUERY_CONFIG);
if (!service) {
ErrorHandler("OpenService", GetLastError());
}
buffer = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, 4096);
if (!buffer) {
ErrorHandler("LocalAlloc", GetLastError());
}
res = QueryServiceConfig(service, buffer, 4096, &sizeNeeded);
if (!res) {
ErrorHandler("QueryServiceConfig", GetLastError());
}
printf("Service name:\t%S\n", buffer->lpDisplayName);
printf("Service type:\t%d\n", buffer->dwServiceType);
printf("Start type:\t%d\n",buffer->dwStartType);
printf("Start name:\t%S\n",buffer->lpServiceStartName);
printf("Path:\t\t%S\n",buffer->lpBinaryPathName);
LocalFree(buffer);
CloseServiceHandle(service);
CloseServiceHandle(scm);
return TRUE;
}
BOOL ChangeConfig()
{
SC_HANDLE service;
SC_HANDLE scm;
BOOL res;
SC_LOCK lock;
scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS | GENERIC_WRITE);
if (!scm) {
ErrorHandler("OpenSCManager", GetLastError());
}
lock = LockServiceDatabase(scm);
if (lock == 0) {
ErrorHandler("LockServiceDatabase", GetLastError());
}
service = OpenService(scm, ServiceName, SERVICE_ALL_ACCESS);
if (!service) {
ErrorHandler("OpenService", GetLastError());
}
res = ChangeServiceConfig(
service,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
);
if (!res) {
UnlockServiceDatabase(lock);
ErrorHandler("ChangeServiceConfig", GetLastError());
}
res = UnlockServiceDatabase(lock);
if (!res) {
ErrorHandler("UnlockServiceDatabase", GetLastError());
}
CloseServiceHandle(service);
CloseServiceHandle(scm);
return TRUE;
}