diff --git a/cpp/src/license/GetSysInfo.cpp b/cpp/src/license/GetSysInfo.cpp index 9c35b89864d3282f4376327125c8f117668ef441..added4436006af2835604c6c0067a0cc7df09b90 100644 --- a/cpp/src/license/GetSysInfo.cpp +++ b/cpp/src/license/GetSysInfo.cpp @@ -1,6 +1,51 @@ #include +#include +#include + +// Not provide path: current work path will be used and system.info. + +void +print_usage(const std::string &app_name) { + printf("\n Usage: %s [OPTIONS]\n\n", app_name.c_str()); + printf(" Options:\n"); + printf(" -h --help Print this help\n"); + printf(" -d --sysinfo filename Generate system info file as given name\n"); + printf("\n"); +} int main(int argc, char* argv[]) { - std::cout << "GetSysInfo" << std::endl; + std::string app_name = argv[0]; + if(argc != 1 && argc != 3) { + print_usage(app_name); + return EXIT_FAILURE; + } + + static struct option long_options[] = {{"system_info", required_argument, 0, 'd'}, + {"help", no_argument, 0, 'h'}, + {NULL, 0, 0, 0}}; + int value = 0; + int option_index = 0; + std::string system_info_filename = "./system.info"; + while ((value = getopt_long(argc, argv, "d:h", long_options, &option_index)) != -1) { + switch (value) { + case 'd': { + char *system_info_filename_ptr = strdup(optarg); + system_info_filename = system_info_filename_ptr; + free(system_info_filename_ptr); +// printf("Generate system info file: %s\n", system_info_filename.c_str()); + break; + } + case 'h': + print_usage(app_name); + return EXIT_SUCCESS; + case '?': + print_usage(app_name); + return EXIT_FAILURE; + default: + print_usage(app_name); + break; + } + } + return 0; } \ No newline at end of file diff --git a/cpp/src/license/License.cpp b/cpp/src/license/License.cpp deleted file mode 100644 index eb6c563e4f407478c6cabaff1ffb357823dc0200..0000000000000000000000000000000000000000 --- a/cpp/src/license/License.cpp +++ /dev/null @@ -1,371 +0,0 @@ -#include "License.h" - -namespace zilliz { -namespace vecwise { -namespace server { - -ServerError -LicenseSave(const std::string& path,const int& deviceCount,const std::vector& shas) -{ - std::ofstream file(path); - - boost::archive::binary_oarchive oa(file); - oa << deviceCount; - for(int i=0;i& shas) -{ - std::ifstream file(path); - - boost::archive::binary_iarchive ia(file); - ia >> deviceCount; - std::string sha; - for(int i=0;i> sha; - shas.push_back(sha); - } - file.close(); - return SERVER_SUCCESS; -} - -ServerError -Licensefileread(const std::string& path) -{ - std::ifstream fileread; - fileread.open(path,std::ios::in); - if(!fileread) - { - printf("NO License\n"); - return SERVER_UNEXPECTED_ERROR; - } - fileread.close(); - return SERVER_SUCCESS; -} - -ServerError -Licensefileread (const std::string& path,std::ifstream& fileread){ - fileread.open(path,std::ios::in); - if(!fileread) - { - printf("Can't open file\n"); - return SERVER_UNEXPECTED_ERROR; - } - return SERVER_SUCCESS; -} - -ServerError -Licensefilewrite (const std::string& path,std::ofstream& filewrite) { - filewrite.open(path,std::ios::out); - if(!filewrite) - { - printf("Can't write file\n"); - return SERVER_UNEXPECTED_ERROR; - } - return SERVER_SUCCESS; -} - -ServerError -LicenseGetCount(int &deviceCount) -{ - nvmlReturn_t result = nvmlInit(); - if (NVML_SUCCESS != result) - { - printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); - return SERVER_UNEXPECTED_ERROR; - } - cudaError_t error_id = cudaGetDeviceCount(&deviceCount); - if (error_id != cudaSuccess) - { - printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id)); - printf("Result = FAIL\n"); - return SERVER_UNEXPECTED_ERROR; - } - return SERVER_SUCCESS; -} - -ServerError -LicenseGetuuid(int& deviceCount, std::vector& uuids) -{ - nvmlReturn_t result = nvmlInit(); - if (NVML_SUCCESS != result) - { - printf("Failed to initialize NVML: %s\n", nvmlErrorString(result)); - return SERVER_UNEXPECTED_ERROR; - } - cudaError_t error_id = cudaGetDeviceCount(&deviceCount); - if (error_id != cudaSuccess) - { - printf("cudaGetDeviceCount returned %d\n-> %s\n", (int)error_id, cudaGetErrorString(error_id)); - printf("Result = FAIL\n"); - return SERVER_UNEXPECTED_ERROR; - } - - if (deviceCount == 0) - { - printf("There are no available device(s) that support CUDA\n"); - return SERVER_UNEXPECTED_ERROR; - } - - for (int dev = 0; dev < deviceCount; ++dev) - { - nvmlDevice_t device; - result = nvmlDeviceGetHandleByIndex(dev, &device); - printf("device id: %d\n", dev); - if (NVML_SUCCESS != result) - { - printf("Failed to get handle for device %i: %s\n", dev, nvmlErrorString(result)); - return SERVER_UNEXPECTED_ERROR; - } - - char* uuid = (char*)malloc(80); - unsigned int length = 80; - nvmlReturn_t err = nvmlDeviceGetUUID(device, uuid, length); - if(err != NVML_SUCCESS) { - printf("nvmlDeviceGetUUID error: %d\n", err); - return SERVER_UNEXPECTED_ERROR; - } - - printf("\n device: %d, uuid = %s \n", dev, uuid); - uuids.push_back(std::string(uuid)); - free(uuid); - uuid = NULL; - } - return SERVER_SUCCESS; -} - -ServerError -LicenseGetuuidmd5(const int& deviceCount,std::vector& uuids,std::vector& md5s) -{ - MD5_CTX ctx; - unsigned char outmd[16]; - char temp[2]; - std::string md5=""; - for(int dev=0;dev& uuids,std::vector& shas) -{ - SHA256_CTX ctx; - unsigned char outmd[32]; - char temp[2]; - std::string sha=""; - for(int dev=0;dev uuids; - LicenseGetuuid(deviceCount,uuids); - std::vector shas; - LicenseGetuuidsha(deviceCount,uuids,shas); - - - int deviceCountcheck; - std::vector shascheck; - LicenseLoad(path,deviceCountcheck,shascheck); - - if(deviceCount!=deviceCountcheck) - { - printf("deviceCount is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - for(int i=0;i> filemd5; - ia >> last_time; - file.close(); - return SERVER_SUCCESS; -} - -ServerError -LicenseIntegritycheck(const std::string& path,const std::string& path2) - { - std::string filemd5; - LicenseGetfilemd5(path,filemd5); - time_t last_time; - LicenseGetfiletime(path,last_time); - - time_t last_timecheck; - std::string filemd5check; - LicensefileLoad(path2,filemd5check,last_timecheck); - - if(filemd5!=filemd5check) - { - printf("This file has been modified\n"); - return SERVER_UNEXPECTED_ERROR; - } - if(last_time!=last_timecheck) - { - printf("last_time is wrong\n"); - return SERVER_UNEXPECTED_ERROR; - } - - return SERVER_SUCCESS; - } - -ServerError -LicenseValidate(const std::string& path) { - - - return SERVER_SUCCESS; -} - - - -} -} -} \ No newline at end of file diff --git a/cpp/src/license/License.h b/cpp/src/license/License.h deleted file mode 100644 index 6f67edb6f1f53ebfa949074f7fb0fac0c21b9718..0000000000000000000000000000000000000000 --- a/cpp/src/license/License.h +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once - -#include "utils/Error.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include "boost/archive/binary_oarchive.hpp" -#include "boost/archive/binary_iarchive.hpp" - -#include -#include -#include -#include - - -namespace zilliz { -namespace vecwise { -namespace server { - - -ServerError -LicenseSave(const std::string& path,const int& deviceCount,const std::vector& shas); - -ServerError -LicenseLoad(const std::string& path,int& deviceCount,std::vector& shas); - -ServerError -Licensefileread (const std::string& path,std::ifstream& fileread); - -ServerError -Licensefilewrite (const std::string& path,std::ofstream& filewrite); - -ServerError -LicenseGetuuid(int& deviceCount, std::vector& uuids); - -ServerError -LicenseGetuuidmd5(const int& deviceCount,std::vector& uuids,std::vector& md5s); - -ServerError -LicenseGetfilemd5(const std::string& path,std::string& filemd5); - -ServerError -LicenseGetuuidsha(const int& deviceCount,std::vector& uuids,std::vector& shas); - -ServerError -LicenseIntegritycheck(const std::string& path,const std::string& path2); - -ServerError -LicenseGetfiletime(const std::string& path,time_t& last_time); - -ServerError -LicenseLegalitycheck(const std::string& path); - -ServerError -LicensefileSave(const std::string& path,const std::string& path2); - -ServerError -LicensefileLoad(const std::string& path2,std::string& filemd5,time_t& last_time); - - -ServerError -LicenseGetfilesize(const std::string& path,off_t& file_size); - -ServerError -LicenseValidate(const std::string& path); - -ServerError -Licensefileread(const std::string& path); - -ServerError -LicenseGetCount(int &deviceCount); -} -} -} - - diff --git a/cpp/src/license/LicenseCheck.cpp b/cpp/src/license/LicenseCheck.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1566f4e9510aafe1577ab8fb8303a85683ecf527 --- /dev/null +++ b/cpp/src/license/LicenseCheck.cpp @@ -0,0 +1,29 @@ +#include "LicenseCheck.h" + +namespace zilliz { +namespace vecwise { +namespace server { + +class LicenseCheck { + public: + static LicenseCheck& + GetInstance() { + static LicenseCheck instance; + return instance; + } + + ServerError + Check(); + + ServerError + PeriodicUpdate(); + + private: + LicenseCheck() {}; + + +}; + +} +} +} \ No newline at end of file diff --git a/cpp/src/license/LicenseCheck.h b/cpp/src/license/LicenseCheck.h new file mode 100644 index 0000000000000000000000000000000000000000..4078a45e4cc207dfb47e309aeb203ac3d58a6e66 --- /dev/null +++ b/cpp/src/license/LicenseCheck.h @@ -0,0 +1,14 @@ +#pragma once + +#include "utils/Error.h" + +namespace zilliz { +namespace vecwise { +namespace server { + + +} +} +} + + diff --git a/cpp/src/license/LicenseCreateTime.cpp b/cpp/src/license/LicenseCreateTime.cpp index 0c2f9515111085e302e7ae7ce7f1b883afd1fdc3..f14d6bb7a562dbd960f57a60b076ecf498bd04a6 100644 --- a/cpp/src/license/LicenseCreateTime.cpp +++ b/cpp/src/license/LicenseCreateTime.cpp @@ -7,7 +7,7 @@ #include #include "utils/Error.h" -#include "license/License.h" +#include "license/LicenseCheck.h" #include "license/LicensePublic.h" using namespace zilliz::vecwise; diff --git a/cpp/src/license/LicenseCreateuuidshafile.cpp b/cpp/src/license/LicenseCreateuuidshafile.cpp index 597adb2cfcb580f31234481175e0b02e18087e1d..9476c89bd74b8c9433e112f4532ee514a5138888 100644 --- a/cpp/src/license/LicenseCreateuuidshafile.cpp +++ b/cpp/src/license/LicenseCreateuuidshafile.cpp @@ -5,7 +5,7 @@ #include #include "utils/Error.h" -#include "license/License.h" +#include "license/LicenseCheck.h" using namespace zilliz::vecwise; diff --git a/cpp/src/license/LicenseLibrary.cpp b/cpp/src/license/LicenseLibrary.cpp index f9cf6808d7044527383d9a7b5beab684257751dd..237f1679b837214cd68ce579b6b528a8a1a5fb1a 100644 --- a/cpp/src/license/LicenseLibrary.cpp +++ b/cpp/src/license/LicenseLibrary.cpp @@ -15,7 +15,9 @@ #include //#include //#include +#include #include +#include namespace zilliz { @@ -24,18 +26,23 @@ namespace server { constexpr int LicenseLibrary::sha256_length_; -ServerError -LicenseLibrary::OpenFile(const std::string &path) { - std::ifstream fileread; - fileread.open(path, std::ios::in); - if (!fileread) { - std::cout << path << " does not exist" << std::endl; - return SERVER_UNEXPECTED_ERROR; +bool +LicenseLibrary::IsFileExistent(const std::string& path) { + + boost::system::error_code error; + auto file_status = boost::filesystem::status(path, error); + if (error) { + return false; } - fileread.close(); - return SERVER_SUCCESS; + + if (!boost::filesystem::exists(file_status)) { + return false; + } + + return !boost::filesystem::is_directory(file_status); } + ServerError LicenseLibrary::GetDeviceCount(int &device_count) { nvmlReturn_t result = nvmlInit(); @@ -151,7 +158,8 @@ LicenseLibrary::LicenseFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map, int64_t &remaining_hour) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -190,7 +198,8 @@ LicenseLibrary::SecretFileDeserialization(const std::string &path, time_t &update_time, off_t &file_size, std::string &file_md5) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -208,7 +217,8 @@ LicenseLibrary::SecretFileDeserialization(const std::string &path, ServerError LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update_time, off_t &file_size) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + struct stat buf; int err_no = stat(path.c_str(), &buf); if (err_no != 0) { @@ -225,7 +235,8 @@ LicenseLibrary::GetFileUpdateTimeAndSize(const std::string &path, time_t &update ServerError LicenseLibrary::GetFileMD5(const std::string &path, std::string &filemd5) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + filemd5.clear(); std::ifstream file(path.c_str(), std::ifstream::binary); @@ -276,7 +287,8 @@ ServerError LicenseLibrary::GPUinfoFileDeserialization(const std::string &path, int &device_count, std::map &uuid_encrption_map) { - OpenFile(path); + if(!IsFileExistent(path)) return SERVER_LICENSE_FILE_NOT_EXIST; + std::ifstream file(path); boost::archive::binary_iarchive ia(file); ia.register_type(); @@ -317,7 +329,7 @@ LicenseLibrary::IntegrityCheck(const std::string &license_file_path, const std:: printf("file_size is wrong\n"); return SERVER_UNEXPECTED_ERROR; } - std::cout << "Integrity Check Success" << std::endl; + return SERVER_SUCCESS; } @@ -390,8 +402,9 @@ LicenseLibrary::AlterFile(const std::string &license_file_path, ServerError LicenseLibrary::StartCountingDown(const std::string &license_file_path, const std::string &secret_file_path) { - OpenFile(license_file_path); - OpenFile(secret_file_path); + if(!IsFileExistent(license_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; + if(!IsFileExistent(secret_file_path)) return SERVER_LICENSE_FILE_NOT_EXIST; + boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::hours(1)); t.async_wait(boost::bind(AlterFile, license_file_path, secret_file_path, boost::asio::placeholders::error, &t)); diff --git a/cpp/src/license/LicenseLibrary.h b/cpp/src/license/LicenseLibrary.h index da79a4770df02d648bfea1af719339bc68555b71..dd78c221ad984735af275b7a6f29c96d8e159f43 100644 --- a/cpp/src/license/LicenseLibrary.h +++ b/cpp/src/license/LicenseLibrary.h @@ -20,8 +20,10 @@ namespace server { class LicenseLibrary { public: - static ServerError - OpenFile(const std::string &path); + // Part 0: File check + static bool + IsFileExistent(const std::string& path); + // Part 1: Get GPU Info static ServerError GetDeviceCount(int &device_count); diff --git a/cpp/src/license/LicensePublic.cpp b/cpp/src/license/LicensePublic.cpp index 632b4cb73d0d71d7ffe8de17908c055549ef98a9..186c502ac19505c347922dab48b0b7cfefad81fb 100644 --- a/cpp/src/license/LicensePublic.cpp +++ b/cpp/src/license/LicensePublic.cpp @@ -3,7 +3,7 @@ // #include "LicensePublic.h" -#include "license/License.h" +#include "license/LicenseCheck.h" //#include "BoostArchive.h" #define RTime 100 diff --git a/cpp/src/license/LicenseRun.cpp b/cpp/src/license/LicenseRun.cpp index 93c6c463b06b0aaa2029bd0734c0f4024b070464..dd06a64540c0d7a7bc0e9efaf5d5ed726fafb7a4 100644 --- a/cpp/src/license/LicenseRun.cpp +++ b/cpp/src/license/LicenseRun.cpp @@ -6,7 +6,7 @@ #include #include "utils/Error.h" -#include "license/License.h" +#include "license/LicenseCheck.h" #include "license/LicensePublic.h" diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index 930c29fac9968bc83dd530787034cb5d34eb50cb..876771fafd7953b2acff781e1dd818167bfe7849 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -9,7 +9,7 @@ #include "utils/Log.h" #include "utils/SignalUtil.h" #include "utils/TimeRecorder.h" -#include "license/License.h" +#include "license/LicenseCheck.h" #include #include diff --git a/cpp/src/utils/Error.h b/cpp/src/utils/Error.h index 88e697ec2eb6ddd191cb598df764ec551d8fe4ed..8684f8607f2d09a14046b160339dcab62e41280a 100644 --- a/cpp/src/utils/Error.h +++ b/cpp/src/utils/Error.h @@ -32,6 +32,7 @@ constexpr ServerError SERVER_FILE_NOT_FOUND = ToGlobalServerErrorCode(0x005); constexpr ServerError SERVER_NOT_IMPLEMENT = ToGlobalServerErrorCode(0x006); constexpr ServerError SERVER_BLOCKING_QUEUE_EMPTY = ToGlobalServerErrorCode(0x007); constexpr ServerError SERVER_LICENSE_VALIDATION_FAIL = ToGlobalServerErrorCode(0x008); +constexpr ServerError SERVER_LICENSE_FILE_NOT_EXIST = ToGlobalServerErrorCode(0x009); class ServerException : public std::exception { public: diff --git a/cpp/unittest/license/license_library_tests.cpp b/cpp/unittest/license/license_library_tests.cpp index ddce463d9ab65d45ff23b3eec34ceba4ef1c2529..c990467f4489bcd85a44e9348cc2fb43425db012 100644 --- a/cpp/unittest/license/license_library_tests.cpp +++ b/cpp/unittest/license/license_library_tests.cpp @@ -14,6 +14,18 @@ using namespace zilliz::vecwise; +TEST(LicenseLibraryTest, FILE_EXISTENT_TEST) { + + std::string hosts_file = "/etc/hosts"; + ASSERT_EQ(server::LicenseLibrary::IsFileExistent(hosts_file), true); + + std::string no_exist_file = "/temp/asdaasd"; + ASSERT_EQ(server::LicenseLibrary::IsFileExistent(no_exist_file), false); + + std::string directory = "/tmp"; + ASSERT_EQ(server::LicenseLibrary::IsFileExistent(directory), false); +} + TEST(LicenseLibraryTest, GPU_INFO_TEST) { int device_count = 0; diff --git a/cpp/unittest/license/license_tests.cpp b/cpp/unittest/license/license_tests.cpp index e67352cde657322646125e4ecec89d000f902195..cff0beeb354afb3a8b0a0c99ccde119811fe3a6d 100644 --- a/cpp/unittest/license/license_tests.cpp +++ b/cpp/unittest/license/license_tests.cpp @@ -5,7 +5,7 @@ //////////////////////////////////////////////////////////////////////////////// #include #include -#include "license/License.h" +#include "license/LicenseCheck.h" #include "license/LicensePublic.h" #include "utils/Error.h"