From 6dd7902d6f148a4ac78e5478edb3a2ca8624f74c Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: 周日, 15 3月 2020 16:07:14 +0800 Subject: [PATCH] cpu brand --- src/library/CMakeLists.txt | 2 src/library/hw_identifier/hw_identifier_facade.cpp | 2 test/library/os_linux_test.cpp | 0 src/inspector/inspector.cpp | 9 +- src/library/os/linux/os_linux.cpp | 0 src/library/os/windows/cpu_info.cpp | 48 +++++++++--- CMakeLists.txt | 12 +- src/library/os/CMakeLists.txt | 6 src/library/os/windows/execution_environment.cpp | 51 ++++++++++++ src/library/os/windows/os_win.cpp | 4 - test/library/CMakeLists.txt | 2 src/library/os/cpu_info.hpp | 9 +- src/library/os/cpu_info_common.cpp | 2 src/library/os/linux/cpu_info.cpp | 35 ++++++-- 14 files changed, 135 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7159721..3c32f44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ ENDIF(NOT LCC_PROJECTS_BASE_DIR) cmake_policy(SET CMP0048 NEW) +#cmake_policy(SET CMP0091 NEW) project (licensecc VERSION 2.0.0 DESCRIPTION "Copy protection and licensing library" @@ -81,15 +82,14 @@ include(target_arch) target_architecture( TARGET_ARCHITECTURE ) message(STATUS "architecture detected: ${TARGET_ARCHITECTURE}") - #cmake > 3.15 handle the /MD flag more nicely than this - if(${STATIC_RUNTIME}) - string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") + if(STATIC_RUNTIME) + message(STATUS "Static runtime ${STATIC_RUNTIME}") string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") - string(REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") string(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + #list(APPEND CMAKE_CXX_FLAGS_DEBUG "/MTd") SET(Boost_USE_STATIC_RUNTIME ON) - endif(${STATIC_RUNTIME}) + endif(STATIC_RUNTIME) add_definitions("/D _CRT_SECURE_NO_WARNINGS") list(APPEND EXTERNAL_LIBS "bcrypt" "crypt32" "ws2_32" "iphlpapi") else(MSVC) @@ -116,8 +116,6 @@ include_directories( ${LCC_INCLUDE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/include" ) add_custom_target(project_initialize DEPENDS "${LCC_PROJECT_PUBLIC_KEY}" "${LCC_PROJECT_PRIVATE_KEY}") -message( STATUS "C compiler : " ${CMAKE_C_COMPILER}) -message( STATUS "C compiler flags : " ${CMAKE_C_FLAGS}) message( STATUS "CXX compiler : " ${CMAKE_CXX_COMPILER}) message( STATUS "CXX compiler flags : " ${CMAKE_CXX_FLAGS}) message( STATUS "CXX compiler flags debug : " ${CMAKE_CXX_FLAGS_DEBUG}) diff --git a/src/inspector/inspector.cpp b/src/inspector/inspector.cpp index 07330eb..5608ebb 100644 --- a/src/inspector/inspector.cpp +++ b/src/inspector/inspector.cpp @@ -54,10 +54,11 @@ int main(int argc, char* argv[]) { license::os::CpuInfo cpu; - cout << "CpuVendor :" << cpu.vendor() << endl; - cout << "Virtual machine :" << cpu.cpu_virtual() << endl; + cout << "Cpu Vendor :" << cpu.vendor() << endl; + cout << "Cpu Brand :" << cpu.brand() << endl; + cout << "Cpu is hypervis.:" << cpu.cpu_virtual() << endl; cout << "Cpu model :0x" << std::hex << ((long)cpu.model()) << std::dec << endl; - cout << "Virt. detail cpu:" << descByVirtDetail.find(cpu.getVirtualizationDetail())->second << endl; + cout << "Virt. detail cpu:" << descByVirtDetail.find(cpu.virtualization_details())->second << endl <<endl; ExecutionEnvironment execEnv; cout << "Running in cloud:" << execEnv.is_cloud() << endl; cout << "Docker :" << execEnv.is_docker() << endl; @@ -66,7 +67,7 @@ cout << "Bios vendor :" << execEnv.bios_vendor() << endl; cout << "Bios description:" << execEnv.bios_description() << endl; - cout << "System vendor :" << execEnv.sys_vendor() << endl; + cout << "System vendor :" << execEnv.sys_vendor() << endl <<endl; char hw_identifier[LCC_API_PC_IDENTIFIER_SIZE + 1]; size_t bufSize = LCC_API_PC_IDENTIFIER_SIZE + 1; diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt index 87693d2..f174976 100644 --- a/src/library/CMakeLists.txt +++ b/src/library/CMakeLists.txt @@ -31,6 +31,8 @@ target_link_libraries(licensecc_static PUBLIC ${EXTERNAL_LIBS}) ENDIF(UNIX OR OPENSSL_FOUND) +#set_property(TARGET licensecc_static PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") + target_include_directories(licensecc_static PUBLIC $<INSTALL_INTERFACE:include> diff --git a/src/library/hw_identifier/hw_identifier_facade.cpp b/src/library/hw_identifier/hw_identifier_facade.cpp index c7a2ec6..07808f6 100644 --- a/src/library/hw_identifier/hw_identifier_facade.cpp +++ b/src/library/hw_identifier/hw_identifier_facade.cpp @@ -67,7 +67,7 @@ pc_id.set_cloud_provider(exec.getCloudProvider()); } else { os::CpuInfo cpu; - pc_id.set_virtualization(cpu.getVirtualizationDetail()); + pc_id.set_virtualization(cpu.virtualization_details()); } } return pc_id.print(); diff --git a/src/library/os/CMakeLists.txt b/src/library/os/CMakeLists.txt index 42bf47d..7244e0d 100644 --- a/src/library/os/CMakeLists.txt +++ b/src/library/os/CMakeLists.txt @@ -7,7 +7,7 @@ cpu_info_common.cpp linux/cpu_info.cpp linux/network.cpp - linux/os-linux.cpp) + linux/os_linux.cpp) ELSE(UNIX) #windows and openssl add_library(os OBJECT @@ -16,7 +16,7 @@ execution_environment_common.cpp windows/execution_environment.cpp windows/isvm/Native.cpp windows/isvm/BIOSReader.cpp - windows/os-win.cpp + windows/os_win.cpp windows/network.cpp) ENDIF(UNIX) target_include_directories(os PUBLIC ${OPENSSL_INCLUDE_DIR}) @@ -29,7 +29,7 @@ execution_environment_common.cpp windows/execution_environment.cpp windows/isvm/Native.cpp windows/isvm/BIOSReader.cpp - windows/os-win.cpp + windows/os_win.cpp windows/network.cpp) ENDIF(UNIX OR OPENSSL_FOUND) diff --git a/src/library/os/cpu_info.hpp b/src/library/os/cpu_info.hpp index e0ec59f..a35de10 100644 --- a/src/library/os/cpu_info.hpp +++ b/src/library/os/cpu_info.hpp @@ -18,7 +18,8 @@ class CpuInfo { private: bool is_hypervisor_set() const; - + const std::string m_vendor; + const std::string m_brand; public: CpuInfo(); virtual ~CpuInfo(); @@ -28,9 +29,9 @@ */ bool cpu_virtual() const; uint32_t model() const; - std::string vendor() const; - // std::string brand() const; - VIRTUALIZATION_DETAIL getVirtualizationDetail() const; + const std::string& vendor() const { return m_vendor; } + const std::string& brand() const { return m_brand; } + VIRTUALIZATION_DETAIL virtualization_details() const; }; } // namespace os diff --git a/src/library/os/cpu_info_common.cpp b/src/library/os/cpu_info_common.cpp index 96ac6c6..1ddf5ba 100644 --- a/src/library/os/cpu_info_common.cpp +++ b/src/library/os/cpu_info_common.cpp @@ -30,7 +30,7 @@ return is_virtual; } -VIRTUALIZATION_DETAIL CpuInfo::getVirtualizationDetail() const { +VIRTUALIZATION_DETAIL CpuInfo::virtualization_details() const { string cpu_vendor = vendor(); auto it = virtual_cpu_names.find(cpu_vendor); VIRTUALIZATION_DETAIL result = BARE_TO_METAL; diff --git a/src/library/os/linux/cpu_info.cpp b/src/library/os/linux/cpu_info.cpp index b052a03..37620cb 100644 --- a/src/library/os/linux/cpu_info.cpp +++ b/src/library/os/linux/cpu_info.cpp @@ -8,6 +8,7 @@ #include <cpuid.h> #include <string> #include <unordered_set> +#include <memory.h> #include "../cpu_info.hpp" namespace license { @@ -22,7 +23,32 @@ string toString() const { return string(reinterpret_cast<const char *>(this), 12); } }; -CpuInfo::CpuInfo() {} +static string get_cpu_vendor() { + unsigned int level = 0, eax = 0, ebx = 0, ecx = 0, edx = 0; + // hypervisor flag false, try to get the vendor name, see if it's a virtual cpu + __get_cpuid(level, &eax, &ebx, &ecx, &edx); + CPUVendorID vendorID{.ebx = ebx, .edx = edx, .ecx = ecx}; + return vendorID.toString(); +} + +// https://en.wikipedia.org/wiki/CPUID +static string get_cpu_brand() { + string result; + uint32_t brand[0x10]; + + if (!__get_cpuid_max(0x80000004, NULL)) { + result = "NA"; + } else { + memset(brand, 0, sizeof(brand)); + __get_cpuid(0x80000002, brand + 0x0, brand + 0x1, brand + 0x2, brand + 0x3); + __get_cpuid(0x80000003, brand + 0x4, brand + 0x5, brand + 0x6, brand + 0x7); + __get_cpuid(0x80000004, brand + 0x8, brand + 0x9, brand + 0xa, brand + 0xb); + result = string(reinterpret_cast<char *>(brand)); + } + return result; +} + +CpuInfo::CpuInfo() : m_vendor(get_cpu_vendor()), m_brand(get_cpu_brand()) {} CpuInfo::~CpuInfo() {} /** @@ -46,12 +72,5 @@ return (eax & 0x3FFF) | (eax & 0x3FF8000) >> 2 | (ebx & 0xff) << 24; } -string CpuInfo::vendor() const { - unsigned int level = 0, eax = 0, ebx = 0, ecx = 0, edx = 0; - // hypervisor flag false, try to get the vendor name, see if it's a virtual cpu - __get_cpuid(level, &eax, &ebx, &ecx, &edx); - CPUVendorID vendorID{.ebx = ebx, .edx = edx, .ecx = ecx}; - return vendorID.toString(); -} } // namespace os } /* namespace license */ diff --git a/src/library/os/linux/os-linux.cpp b/src/library/os/linux/os_linux.cpp similarity index 100% rename from src/library/os/linux/os-linux.cpp rename to src/library/os/linux/os_linux.cpp diff --git a/src/library/os/windows/cpu_info.cpp b/src/library/os/windows/cpu_info.cpp index f4b0e81..a58cc6c 100644 --- a/src/library/os/windows/cpu_info.cpp +++ b/src/library/os/windows/cpu_info.cpp @@ -15,7 +15,42 @@ namespace os { using namespace std; -CpuInfo::CpuInfo() {} +static string get_cpu_vendor() { + // hypervisor flag false, try to get the vendor name, see if it's a virtual cpu + int cpui[4] = {0}; + __cpuidex(cpui, 0x0, 0x0); + + char vendor[13]; + memset(vendor, 0, sizeof(vendor)); + *reinterpret_cast<int *>(vendor) = cpui[1]; + *reinterpret_cast<int *>(vendor + 4) = cpui[3]; + *reinterpret_cast<int *>(vendor + 8) = cpui[2]; + return string(vendor, 12); +} + +static string get_cpu_brand() { + int cpui[4] = {0}; + __cpuid(cpui, 0x80000000); + int maxSupported = cpui[0]; + + char brand[0x41]; + memset(brand, 0, sizeof(brand)); + string result; + if (maxSupported >= 0x80000004) { + int instruction = 0x80000002; + for (int i = 0; i <= 2; ++i) { + __cpuidex(cpui, instruction + i, 0); + memcpy(&brand[i * sizeof(cpui)], cpui, sizeof(cpui)); + } + result = string(brand); + } else { + result = "NA"; + } + + return result; +} + +CpuInfo::CpuInfo() : m_vendor(get_cpu_vendor()), m_brand(get_cpu_brand()) {} CpuInfo::~CpuInfo() {} /** @@ -38,17 +73,6 @@ return (cpui[0] & 0x3FFF) | (cpui[0] & 0x3FF8000) >> 2 | (cpui[1] & 0xff) << 24; } -string CpuInfo::vendor() const { - // hypervisor flag false, try to get the vendor name, see if it's a virtual cpu - int cpui[4] = {0}; - __cpuidex(cpui, 0x0, 0x0); - char vendor[13]; - memset(vendor, 0, sizeof(vendor)); - *reinterpret_cast<int *>(vendor) = cpui[1]; - *reinterpret_cast<int *>(vendor +4) = cpui[3]; - *reinterpret_cast<int *>(vendor + 8) = cpui[2]; - return string(vendor, 12); -} } // namespace os } /* namespace license */ diff --git a/src/library/os/windows/execution_environment.cpp b/src/library/os/windows/execution_environment.cpp index 03c789e..3e65bc8 100644 --- a/src/library/os/windows/execution_environment.cpp +++ b/src/library/os/windows/execution_environment.cpp @@ -4,6 +4,7 @@ * Created on: Dec 15, 2019 * Author: GC */ +#include <windows.h> #include <sys/stat.h> #include <fstream> #include <iostream> @@ -31,8 +32,54 @@ } } -//TODO -bool ExecutionEnvironment::is_docker() const { return false; } +#define MAX_UNITS 20 +int wine_container() { +DWORD fileMaxLen; +size_t ndrives = 0; +DWORD fileFlags; +char volName[MAX_PATH], fileSysName[MAX_PATH]; +DWORD volSerial = 0; +const DWORD dwSize = MAX_PATH; +char szLogicalDrives[MAX_PATH] = {0}; + +int result = 0; +const DWORD dwResult = GetLogicalDriveStrings(dwSize, szLogicalDrives); + +if (dwResult > 0 && dwResult <= MAX_PATH) { + char* szSingleDrive = szLogicalDrives; + while (*szSingleDrive && ndrives < MAX_UNITS) { + // get the next drive + UINT driveType = GetDriveType(szSingleDrive); + if (driveType == DRIVE_FIXED) { + string name = szSingleDrive + string("/var/run/systemd/container"); + try { + ifstream systemd_container(name); + if (systemd_container.good()) { + result = 1; + for (string line; getline(systemd_container, line);) { + if (line.find("docker") != string::npos) { + result = 1; + break; + } else if (line.find("lxc") != string::npos) { + result = 2; + break; + } + } + } + } catch (...) { + //no problem,we're just guessing + } + } + } +} + return result; +} + +bool ExecutionEnvironment::is_docker() const { + // let's check we're not in linux under wine ;) ... +//int cont = wine_container(); + return false; +} //TODO bool ExecutionEnvironment::is_container() const { return is_docker(); } } // namespace os diff --git a/src/library/os/windows/os-win.cpp b/src/library/os/windows/os_win.cpp similarity index 97% rename from src/library/os/windows/os-win.cpp rename to src/library/os/windows/os_win.cpp index 48356f7..b6094da 100644 --- a/src/library/os/windows/os-win.cpp +++ b/src/library/os/windows/os_win.cpp @@ -21,10 +21,6 @@ return result; } -FUNCTION_RETURN getCpuId(unsigned char identifier[6]) { - return FUNC_RET_NOT_AVAIL; -} - //http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c //http://stackoverflow.com/questions/3098696/same-code-returns-diffrent-result-on-windows7-32-bit-system #define MAX_UNITS 30 diff --git a/test/library/CMakeLists.txt b/test/library/CMakeLists.txt index a1c86b1..ab9d134 100644 --- a/test/library/CMakeLists.txt +++ b/test/library/CMakeLists.txt @@ -14,7 +14,7 @@ ELSE(WIN32) add_executable( test_os_linux - Os_Linux_test.cpp + os_linux_test.cpp ) target_link_libraries( diff --git a/test/library/Os_Linux_test.cpp b/test/library/os_linux_test.cpp similarity index 100% rename from test/library/Os_Linux_test.cpp rename to test/library/os_linux_test.cpp -- Gitblit v1.9.1