Skip to content

Commit

Permalink
Version 1.1.2: added two missing services from the kill routine (issue
Browse files Browse the repository at this point in the history
…#26)

added test
  • Loading branch information
Paolo committed Jan 5, 2021
1 parent 4845f09 commit e877c63
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 42 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.vs
/exe
/out
CMakeSettings.json
/testing
/LIBS
/debug.log
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ IF (MSVC)
ENDIF()
ENDIF(MSVC)

set (TEST_SOURCES tests/test_patch.cpp /
src/debugutils.cpp /
src/patchutils.cpp /
)

add_executable( TestPatch ${TEST_SOURCES} )

set_target_properties( TestPatch PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS ON)

enable_testing()
add_test(NAME TestPatchTest COMMAND TestPatch "${PROJECT_SOURCE_DIR}")
4 changes: 2 additions & 2 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

// Program options
#define PROG_VERSION "v1.1.1"
#define PROG_VERSION "v1.1.2"

// Install - Default option
#define INSTALL_OPTION "--install"
Expand Down Expand Up @@ -58,7 +58,7 @@
#define HKEY_QUERY_VALUE_PRODUCTVERSION "ProductVersion"

// Windows services to stop
#define VM_KILL_SERVICES {"vmware-view-usbd", "VMwareHostd", "VMAuthdService", "VMUSBArbService"}
#define VM_KILL_SERVICES {"vmware-view-usbd", "VMwareHostd", "VMAuthdService", "VMUSBArbService", "VMnetDHCP", "VMware NAT Service"}
#define VM_KILL_PROCESSES {"vmware-tray.exe"}

// Files to backup (win) { source, destination }
Expand Down
6 changes: 3 additions & 3 deletions include/servicestoputils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class ServiceStopper
class ServiceStopException : public std::exception
{
public:
ServiceStopException(const char* msg, ...) {
ServiceStopException(const char* msg, ...) {
char formatted[512];
va_list args;
va_start(args, msg);
sprintf(formatted, msg, args);
vsprintf(formatted, msg, args);
va_end(args);
sprintf(message, "Error: %s", formatted);
sprintf(message, "Error: %s", formatted);
}
const char* what() const noexcept { return message; }
private:
Expand Down
27 changes: 17 additions & 10 deletions src/servicestoputils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void ServiceStopper::StopService_s(std::string serviceName)

if (schService == NULL)
{
throw ServiceStopException("OpenService failed (%d)", GetLastError());
CloseServiceHandle(schSCManager);
throw ServiceStopException("OpenService failed (%d)", GetLastError());
return;
}

Expand All @@ -53,14 +53,16 @@ void ServiceStopper::StopService_s(std::string serviceName)
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("QueryServiceStatusEx failed (%d)", GetLastError());
goto stop_cleanup;
}

if (ssp.dwCurrentState == SERVICE_STOPPED)
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("Service is already stopped.");
goto stop_cleanup;
}

// If a stop is pending, wait for it.
Expand All @@ -87,20 +89,23 @@ void ServiceStopper::StopService_s(std::string serviceName)
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("QueryServiceStatusEx failed (%d)", GetLastError());
goto stop_cleanup;
}

if (ssp.dwCurrentState == SERVICE_STOPPED)
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("Service stopped successfully.");
goto stop_cleanup;
}

if (GetTickCount64() - dwStartTime > dwTimeout)
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("Service stop timed out.");
goto stop_cleanup;
}
}

Expand All @@ -115,8 +120,9 @@ void ServiceStopper::StopService_s(std::string serviceName)
SERVICE_CONTROL_STOP,
(LPSERVICE_STATUS)&ssp))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("ControlService failed (%d)", GetLastError());
goto stop_cleanup;
}

// Wait for the service to stop.
Expand All @@ -131,21 +137,22 @@ void ServiceStopper::StopService_s(std::string serviceName)
sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("QueryServiceStatusEx failed (%d)", GetLastError());
goto stop_cleanup;
}

if (ssp.dwCurrentState == SERVICE_STOPPED)
break;

if (GetTickCount64() - dwStartTime > dwTimeout)
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
throw ServiceStopException("Wait timed out");
goto stop_cleanup;
}
}

stop_cleanup:
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
#endif
Expand Down
55 changes: 28 additions & 27 deletions src/unlocker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Unlocker - Patcher + Tools Downloader
Created by Paolo Infante
Based on "Unlocker" by DrDonk for a native solution to python errors and
Based on "Unlocker" by DrDonk for a native solution to python errors and
virus warnings.
I coded from scratch all the patch routines (I tried to be consistent with C++ library usage
though you'll probably find a few memcpy here and there...) and tools download (using
though you'll probably find a few memcpy here and there...) and tools download (using
libcurl for download and libarchive to uncompress the archives)
Should be cross-platform. Tested on Windows, I'm planning to test it on linux too
Expand Down Expand Up @@ -75,7 +75,7 @@ int main(int argc, const char* argv[])
logd("The program is not running as root, the patch may not work properly.");
std::cout << "Running the program with sudo/as root is recommended, in most cases required... Do you want to continue? (y/N) ";
char c = getc(stdin);

if (c != 'y' && c != 'Y')
{
logd("Aborting...");
Expand Down Expand Up @@ -106,14 +106,15 @@ int main(int argc, const char* argv[])
{
std::cout << "A backup folder has been found. Do you wish to uninstall the previous patch? Type y to uninstall, n to continue with installation." << std::endl
<< "(Y/n) ";

char c = getc(stdin);

if (c == 'n' || c == 'N')
install();
else
uninstall();
} else install();
}
else install();
}

#ifdef _WIN32
Expand Down Expand Up @@ -329,19 +330,19 @@ void copyTools(fs::path toolspath)
else
logerr("File \"" + (toolsfrom / FUSION_ZIP_TOOLS_NAME).string() + "\" could not be copied.");
}
catch (const std::exception & e)
catch (const std::exception& e)
{
logerr(e.what());
}

try
{
if (fs::copy_file(toolsfrom / FUSION_ZIP_PRE15_TOOLS_NAME, copyto / FUSION_ZIP_PRE15_TOOLS_NAME))
logd("File \"" + (toolsfrom / FUSION_ZIP_PRE15_TOOLS_NAME).string() + "\" copy done.");
else
logerr("File \"" + (toolsfrom / FUSION_ZIP_PRE15_TOOLS_NAME).string() + "\" could not be copied.");
}
catch (const std::exception & e)
catch (const std::exception& e)
{
logerr(e.what());
}
Expand All @@ -356,7 +357,7 @@ void copyTools(fs::path toolspath)
else
logerr("File \"" + (toolsfrom / FUSION_ZIP_TOOLS_NAME).string() + "\" could not be copied.");
}
catch (const std::exception & e)
catch (const std::exception& e)
{
logerr(e.what());
}
Expand All @@ -368,7 +369,7 @@ void copyTools(fs::path toolspath)
else
logerr("File \"" + (toolsfrom / FUSION_ZIP_PRE15_TOOLS_NAME).string() + "\" could not be copied.");
}
catch (const std::exception & e)
catch (const std::exception& e)
{
logerr(e.what());
}
Expand All @@ -381,7 +382,7 @@ void doPatch()
#ifdef _WIN32
// Setup paths
VMWareInfoRetriever vmInfo;

std::string binList[] = VM_WIN_PATCH_FILES;

fs::path vmwarebase_path = vmInfo.getInstallPath();
Expand All @@ -391,17 +392,17 @@ void doPatch()
fs::path vmx_stats = vmx_path / binList[2];
fs::path vmwarebase = vmwarebase_path / binList[3];

if(!fs::exists(vmx))
if (!fs::exists(vmx))
{
KILL(logerr("Vmx file not found"));
}
if(!fs::exists(vmx_debug))
if (!fs::exists(vmx_debug))
{
KILL(logerr("Vmx file not found"));
KILL(logerr("Vmx-debug file not found"));
}
if(!fs::exists(vmwarebase))
if (!fs::exists(vmwarebase))
{
KILL(logerr("Vmx file not found"));
KILL(logerr("vmwarebase.dll file not found"));
}

logd("File: " + vmx.filename().string());
Expand Down Expand Up @@ -439,15 +440,15 @@ void doPatch()
vmxso = false;
}

if(!fs::exists(vmx))
if (!fs::exists(vmx))
{
KILL(logerr("Vmx file not found"));
}
if(!fs::exists(vmx_debug))
if (!fs::exists(vmx_debug))
{
KILL(logerr("Vmx-debug file not found"));
}
if(!fs::exists(vmlib))
if (!fs::exists(vmlib))
{
KILL(logerr("Vmlib file not found"));
}
Expand Down Expand Up @@ -501,7 +502,7 @@ void stopServices()
else
logerr("Could not kill process \"" + process + "\".");
}
catch (const ServiceStopper::ServiceStopException & ex)
catch (const ServiceStopper::ServiceStopException& ex)
{
logerr(ex.what());
}
Expand All @@ -521,7 +522,7 @@ void restartServices()
ServiceStopper::StartService_s(*it);
logd("Service \"" + *it + "\" started successfully.");
}
catch (const ServiceStopper::ServiceStopException & ex)
catch (const ServiceStopper::ServiceStopException& ex)
{
// There is no need to inform the user that the service cannot be started if that service does not exist in the current version.
//logerr("Couldn't start service " + *it);
Expand Down Expand Up @@ -581,7 +582,7 @@ void preparePatch(fs::path backupPath)
else
logerr("File \"" + fPath.string() + "\" could not be copied.");
}
catch (const std::exception & e)
catch (const std::exception& e)
{
logerr(e.what());
}
Expand All @@ -603,7 +604,7 @@ void preparePatch(fs::path backupPath)

break;
}
catch (const std::exception & e)
catch (const std::exception& e)
{
logerr(e.what());
}
Expand All @@ -622,7 +623,7 @@ void downloadTools(fs::path path)
fs::path temppath = fs::temp_directory_path(); // extract files in the temp folder first

fs::create_directory(path); // create destination directory if it doesn't exist

curl_global_init(CURL_GLOBAL_ALL);

std::string url = FUSION_BASE_URL;
Expand Down Expand Up @@ -702,14 +703,14 @@ void downloadTools(fs::path path)
// No tools available, try getting them from core fusion file
std::string coreurl = buildurl + FUSION_DEF_CORE_LOC;
std::string core_diskpath = (temppath / FUSION_DEF_CORE_NAME).string();

if (Curl::curlDownload(coreurl, core_diskpath) == CURLE_OK) // If the core package was successfully downloaded, extract the tools from it
{
logd("Extracting from .tar to temp folder ...");

fs::path temppath = fs::temp_directory_path();

success = Archive::extract_s(temppath/FUSION_DEF_CORE_NAME, FUSION_DEF_CORE_NAME_ZIP, temppath/FUSION_DEF_CORE_NAME_ZIP);
success = Archive::extract_s(temppath / FUSION_DEF_CORE_NAME, FUSION_DEF_CORE_NAME_ZIP, temppath / FUSION_DEF_CORE_NAME_ZIP);
if (!success) {
logerr("Couldn't extract from the tar file");
// Error in the tar file, try the next version number
Expand Down
Loading

0 comments on commit e877c63

Please sign in to comment.