Skip to content

Commit

Permalink
RHEL-69069: Apply clang-format CI for Balloon driver
Browse files Browse the repository at this point in the history
Signed-off-by: Vitalii Chulak <[email protected]>
  • Loading branch information
Jedoku authored and YanVugenfirer committed Dec 2, 2024
1 parent 0aa9f50 commit 13de4f0
Show file tree
Hide file tree
Showing 20 changed files with 915 additions and 1,037 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
matrix:
path:
- check: '.'
exclude: './Balloon|./fwcfg64|./ivshmem|./NetKVM|./pciserial|./pvpanic|./viocrypt|./viofs|./viogpu|./vioinput|./viomem|./viorng|./vioscsi|./vioserial|./viosock|./viostor|./VirtIO'
exclude: './fwcfg64|./ivshmem|./NetKVM|./pciserial|./pvpanic|./viocrypt|./viofs|./viogpu|./vioinput|./viomem|./viorng|./vioscsi|./vioserial|./viosock|./viostor|./VirtIO'
- check: 'VirtIO'
exclude: ''

Expand Down
108 changes: 66 additions & 42 deletions Balloon/app/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ CDevice::~CDevice()
BOOL CDevice::Init(CService *Service)
{
m_pMemStat = new CMemStat();
if (!m_pMemStat || !m_pMemStat->Init()) {
if (!m_pMemStat || !m_pMemStat->Init())
{
return FALSE;
}

m_evtInitialized = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!m_evtInitialized) {
if (!m_evtInitialized)
{
return FALSE;
}

m_evtTerminate = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!m_evtTerminate) {
if (!m_evtTerminate)
{
return FALSE;
}

m_evtWrite = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!m_evtWrite) {
if (!m_evtWrite)
{
return FALSE;
}

Expand All @@ -46,17 +50,20 @@ VOID CDevice::Fini()
{
Stop();

if (m_evtWrite) {
if (m_evtWrite)
{
CloseHandle(m_evtWrite);
m_evtWrite = NULL;
}

if (m_evtInitialized) {
if (m_evtInitialized)
{
CloseHandle(m_evtInitialized);
m_evtInitialized = NULL;
}

if (m_evtTerminate) {
if (m_evtTerminate)
{
CloseHandle(m_evtTerminate);
m_evtTerminate = NULL;
}
Expand All @@ -70,25 +77,32 @@ VOID CDevice::Fini()
DWORD CDevice::Run()
{
PWCHAR DevicePath = GetDevicePath((LPGUID)&GUID_DEVINTERFACE_BALLOON);
if (DevicePath == NULL) {
if (DevicePath == NULL)
{
PrintMessage("File not found.");
return ERROR_FILE_NOT_FOUND;
}

HANDLE hDevice = CreateFile(DevicePath, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
HANDLE hDevice = CreateFile(DevicePath,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);

free(DevicePath);

if (hDevice == INVALID_HANDLE_VALUE) {
if (hDevice == INVALID_HANDLE_VALUE)
{
PrintMessage("Failed to create file.");
return GetLastError();
}

HCMNOTIFICATION devnotify = m_pService->RegisterDeviceHandleNotification(
hDevice);
HCMNOTIFICATION devnotify = m_pService->RegisterDeviceHandleNotification(hDevice);

if (!devnotify) {
if (!devnotify)
{
DWORD err = GetLastError();
PrintMessage("Failed to register handle notification.");
CloseHandle(hDevice);
Expand All @@ -107,7 +121,7 @@ DWORD CDevice::Run()

VOID CDevice::WriteLoop(HANDLE hDevice)
{
HANDLE waitfor[] = { m_evtTerminate, m_evtWrite };
HANDLE waitfor[] = {m_evtTerminate, m_evtWrite};
OVERLAPPED ovlp;
DWORD timeout;
DWORD written;
Expand All @@ -117,30 +131,32 @@ VOID CDevice::WriteLoop(HANDLE hDevice)
ZeroMemory(&ovlp, sizeof(ovlp));
ovlp.hEvent = m_evtWrite;

while (1) {
while (1)
{
// The old version of the balloon driver didn't block write requests
// until stats were requested. So in order not to consume too much CPU
// we keep the old 1s delay behavior and switch to infinite wait only
// if write result is pending.
timeout = 1000;

if (m_pMemStat->Update()) {
writerc = WriteFile(hDevice, m_pMemStat->GetBuffer(),
(DWORD)m_pMemStat->GetSize(), NULL, &ovlp);
if (!writerc && (GetLastError() == ERROR_IO_PENDING)) {
if (m_pMemStat->Update())
{
writerc = WriteFile(hDevice, m_pMemStat->GetBuffer(), (DWORD)m_pMemStat->GetSize(), NULL, &ovlp);
if (!writerc && (GetLastError() == ERROR_IO_PENDING))
{
timeout = INFINITE;
}
}

waitrc = WaitForMultipleObjects(sizeof(waitfor) / sizeof(waitfor[0]),
waitfor, FALSE, timeout);
waitrc = WaitForMultipleObjects(sizeof(waitfor) / sizeof(waitfor[0]), waitfor, FALSE, timeout);

if (waitrc == WAIT_OBJECT_0) {
if (waitrc == WAIT_OBJECT_0)
{
break;
}
else if (waitrc == WAIT_OBJECT_0 + 1) {
if (!GetOverlappedResult(hDevice, &ovlp, &written, FALSE) ||
(written != m_pMemStat->GetSize()))
else if (waitrc == WAIT_OBJECT_0 + 1)
{
if (!GetOverlappedResult(hDevice, &ovlp, &written, FALSE) || (written != m_pMemStat->GetSize()))
{
PrintMessage("Failed to write stats.");
}
Expand All @@ -152,17 +168,18 @@ BOOL CDevice::Start()
{
DWORD tid, waitrc;

if (!m_hThread) {
m_hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)DeviceThread, (LPVOID)this, 0, &tid);
if (!m_hThread) {
if (!m_hThread)
{
m_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DeviceThread, (LPVOID)this, 0, &tid);
if (!m_hThread)
{
return FALSE;
}

HANDLE waitfor[] = { m_evtInitialized, m_hThread };
waitrc = WaitForMultipleObjects(sizeof(waitfor) / sizeof(waitfor[0]),
waitfor, FALSE, INFINITE);
if (waitrc != WAIT_OBJECT_0) {
HANDLE waitfor[] = {m_evtInitialized, m_hThread};
waitrc = WaitForMultipleObjects(sizeof(waitfor) / sizeof(waitfor[0]), waitfor, FALSE, INFINITE);
if (waitrc != WAIT_OBJECT_0)
{
// the thread failed to initialize
CloseHandle(m_hThread);
m_hThread = NULL;
Expand All @@ -176,9 +193,11 @@ BOOL CDevice::Start()

VOID CDevice::Stop()
{
if (m_hThread) {
if (m_hThread)
{
SetEvent(m_evtTerminate);
if (WaitForSingleObject(m_hThread, 1000) == WAIT_TIMEOUT) {
if (WaitForSingleObject(m_hThread, 1000) == WAIT_TIMEOUT)
{
TerminateThread(m_hThread, 0);
}
CloseHandle(m_hThread);
Expand All @@ -188,7 +207,7 @@ VOID CDevice::Stop()

DWORD WINAPI CDevice::DeviceThread(LPDWORD lParam)
{
CDevice* pDev = reinterpret_cast<CDevice*>(lParam);
CDevice *pDev = reinterpret_cast<CDevice *>(lParam);
return pDev->Run();
}

Expand All @@ -202,7 +221,9 @@ PTCHAR CDevice::GetDevicePath(IN LPGUID InterfaceGuid)
do
{
cr = CM_Get_Device_Interface_List_Size(&DeviceInterfaceListLength,
InterfaceGuid, NULL, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
InterfaceGuid,
NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);

if (cr != CR_SUCCESS)
{
Expand All @@ -215,17 +236,20 @@ PTCHAR CDevice::GetDevicePath(IN LPGUID InterfaceGuid)
}

DeviceInterfaceList = (PWSTR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, DeviceInterfaceListLength * sizeof(WCHAR));
HEAP_ZERO_MEMORY,
DeviceInterfaceListLength * sizeof(WCHAR));

if (DeviceInterfaceList == NULL)
{
cr = CR_OUT_OF_MEMORY;
break;
}

cr = CM_Get_Device_Interface_List(InterfaceGuid, NULL,
DeviceInterfaceList, DeviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
cr = CM_Get_Device_Interface_List(InterfaceGuid,
NULL,
DeviceInterfaceList,
DeviceInterfaceListLength,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT);

} while (cr == CR_BUFFER_SMALL);

Expand Down
16 changes: 9 additions & 7 deletions Balloon/app/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
class CMemStat;
class CService;

class CDevice {
public:
class CDevice
{
public:
CDevice();
~CDevice();
BOOL Init(CService *Service);
VOID Fini();
BOOL Start();
VOID Stop();

protected:
PTCHAR GetDevicePath(IN LPGUID InterfaceGuid);
DWORD Run();
private:
protected:
PTCHAR GetDevicePath(IN LPGUID InterfaceGuid);
DWORD Run();

private:
static DWORD WINAPI DeviceThread(LPDWORD lParam);
VOID WriteLoop(HANDLE hDevice);
CMemStat* m_pMemStat;
CMemStat *m_pMemStat;
CService *m_pService;
HANDLE m_hThread;
HANDLE m_evtInitialized;
Expand Down
99 changes: 55 additions & 44 deletions Balloon/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,76 @@ CService srvc;

DWORD WINAPI HandlerEx(DWORD ctlcode, DWORD evtype, PVOID evdata, PVOID context)
{
CService *service = static_cast<CService*>(context);
CService *service = static_cast<CService *>(context);

return CService::HandlerExThunk(service, ctlcode, evtype, evdata);
}

void __stdcall ServiceMainEx(DWORD argc, TCHAR* argv[])
void __stdcall ServiceMainEx(DWORD argc, TCHAR *argv[])
{
srvc.m_StatusHandle = RegisterServiceCtrlHandlerEx(ServiceName, HandlerEx,
&srvc);
srvc.m_StatusHandle = RegisterServiceCtrlHandlerEx(ServiceName, HandlerEx, &srvc);
CService::ServiceMainThunk(&srvc, argc, argv);
}

SERVICE_TABLE_ENTRY serviceTableEx[] =
{
{ ServiceName, (LPSERVICE_MAIN_FUNCTION) ServiceMainEx},
{ NULL, NULL}
};
SERVICE_TABLE_ENTRY serviceTableEx[] = {{ServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMainEx}, {NULL, NULL}};

ULONG
_cdecl
wmain(
__in ULONG argc,
__in_ecount(argc) PWCHAR argv[]
)
_cdecl wmain(__in ULONG argc, __in_ecount(argc) PWCHAR argv[])
{
if(argc == 2)
if (argc == 2)
{
if (_tcsicmp(L"-i", argv[1]) == 0) {
InstallService();
} else if (_tcsicmp(L"-u", argv[1]) == 0) {
UninstallService();
} else if (_tcsicmp(L"-r", argv[1]) == 0) {
ServiceRun();
} else if (_tcsicmp(L"-s", argv[1]) == 0) {
ServiceControl(SERVICE_CONTROL_STOP);
} else if (_tcsicmp(L"status", argv[1]) == 0) {
SC_HANDLE scm, service;
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());
}
printf("STATUS: ");
srvc.GetStatus(service);
} else if (_tcsicmp(L"config", argv[1]) == 0) {
GetConfiguration();
} else if (_tcsicmp(L"help", argv[1]) == 0) {
ShowUsage();
} else {
ShowUsage();
if (_tcsicmp(L"-i", argv[1]) == 0)
{
InstallService();
}
else if (_tcsicmp(L"-u", argv[1]) == 0)
{
UninstallService();
}
else if (_tcsicmp(L"-r", argv[1]) == 0)
{
ServiceRun();
}
else if (_tcsicmp(L"-s", argv[1]) == 0)
{
ServiceControl(SERVICE_CONTROL_STOP);
}
else if (_tcsicmp(L"status", argv[1]) == 0)
{
SC_HANDLE scm, service;
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());
}
printf("STATUS: ");
srvc.GetStatus(service);
}
} else {
else if (_tcsicmp(L"config", argv[1]) == 0)
{
GetConfiguration();
}
else if (_tcsicmp(L"help", argv[1]) == 0)
{
ShowUsage();
}
else
{
ShowUsage();
}
}
else
{
BOOL success;
success = StartServiceCtrlDispatcher(serviceTableEx);
if (!success) {
ErrorHandler("StartServiceCtrlDispatcher",GetLastError());
if (!success)
{
ErrorHandler("StartServiceCtrlDispatcher", GetLastError());
}
}
return 0;
Expand Down
Loading

0 comments on commit 13de4f0

Please sign in to comment.