提交 7c512bf7 编写于 作者: J jinhai 提交者: xj.lin

Update on license check


Former-commit-id: ef039b5663e0a68c1dfdea7244de4d2e7e16c10e
上级 128bc2b4
#include <iostream>
#include <getopt.h>
#include <memory.h>
// 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
#include "License.h"
namespace zilliz {
namespace vecwise {
namespace server {
ServerError
LicenseSave(const std::string& path,const int& deviceCount,const std::vector<std::string>& shas)
{
std::ofstream file(path);
boost::archive::binary_oarchive oa(file);
oa << deviceCount;
for(int i=0;i<deviceCount;i++)
{
oa << shas[i];
}
file.close();
return SERVER_SUCCESS;
}
ServerError
LicenseLoad(const std::string& path,int& deviceCount,std::vector<std::string>& shas)
{
std::ifstream file(path);
boost::archive::binary_iarchive ia(file);
ia >> deviceCount;
std::string sha;
for(int i=0;i<deviceCount;i++)
{
ia >> 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<std::string>& 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<std::string>& uuids,std::vector<std::string>& md5s)
{
MD5_CTX ctx;
unsigned char outmd[16];
char temp[2];
std::string md5="";
for(int dev=0;dev<deviceCount;dev++)
{
md5.clear();
memset(outmd,0, sizeof(outmd));
MD5_Init(&ctx);
MD5_Update(&ctx,uuids[dev].c_str(),uuids[dev].size());
MD5_Final(outmd,&ctx);
for(int i=0;i<16;i++)
{
std::sprintf(temp,"%02X",outmd[i]);
md5+=temp;
}
md5s.push_back(md5);
}
return SERVER_SUCCESS;
}
ServerError
LicenseGetfilemd5(const std::string& path,std::string& filemd5)
{
// MD5_CTX ctx;
// unsigned char outmd[16];
// char temp[2];
// char* buffer = (char*)malloc(1024);
// std::ifstream fileread;
// Licensefileread(path,fileread);
// memset(outmd,0,sizeof(outmd));
// memset(buffer,0,sizeof(buffer));
// MD5_Init(&ctx);
// while(!fileread.eof()) {
// fileread.get(buffer,1024,EOF);
// MD5_Update(&ctx, buffer, strlen(buffer));
// memset(buffer, 0, sizeof(buffer));
// }
// MD5_Final(outmd,&ctx);
// for(int i=0;i<16;i++)
// {
// std::sprintf(temp,"%02X",outmd[i]);
// filemd5+=temp;
// }
// free(buffer);
// buffer=NULL;
// fileread.close();
// return SERVER_SUCCESS;
filemd5.clear();
std::ifstream file(path.c_str(), std::ifstream::binary);
if (!file)
{
return -1;
}
MD5_CTX md5Context;
MD5_Init(&md5Context);
char buf[1024 * 16];
while (file.good()) {
file.read(buf, sizeof(buf));
MD5_Update(&md5Context, buf, file.gcount());
}
unsigned char result[MD5_DIGEST_LENGTH];
MD5_Final(result, &md5Context);
char hex[35];
memset(hex, 0, sizeof(hex));
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i)
{
sprintf(hex + i * 2, "%02X", result[i]);
}
hex[32] = '\0';
filemd5 = std::string(hex);
return SERVER_SUCCESS;
}
ServerError
LicenseGetuuidsha(const int& deviceCount,std::vector<std::string>& uuids,std::vector<std::string>& shas)
{
SHA256_CTX ctx;
unsigned char outmd[32];
char temp[2];
std::string sha="";
for(int dev=0;dev<deviceCount;dev++)
{
sha.clear();
memset(outmd,0, sizeof(outmd));
SHA256_Init(&ctx);
SHA256_Update(&ctx,uuids[dev].c_str(),uuids[dev].size());
SHA256_Final(outmd,&ctx);
for(int i=0;i<32;i++)
{
std::sprintf(temp,"%02X",outmd[i]);
sha += temp;
}
shas.push_back(sha);
}
return SERVER_SUCCESS;
}
ServerError
LicenseGetfiletime(const std::string& path,time_t& last_time)
{
struct stat buf;
stat(path.c_str(),&buf);
last_time =buf.st_mtime;
return SERVER_SUCCESS;
}
ServerError
LicenseGetfilesize(const std::string& path,off_t& file_size)
{
struct stat buf;
if(stat(path.c_str(),&buf)==-1)
{
printf(" GET fiel_size is wrong");
return SERVER_UNEXPECTED_ERROR;
}
file_size =buf.st_size;
return SERVER_SUCCESS;
}
ServerError
LicenseLegalitycheck(const std::string& path)
{
int deviceCount;
std::vector<std::string> uuids;
LicenseGetuuid(deviceCount,uuids);
std::vector<std::string> shas;
LicenseGetuuidsha(deviceCount,uuids,shas);
int deviceCountcheck;
std::vector<std::string> shascheck;
LicenseLoad(path,deviceCountcheck,shascheck);
if(deviceCount!=deviceCountcheck)
{
printf("deviceCount is wrong\n");
return SERVER_UNEXPECTED_ERROR;
}
for(int i=0;i<deviceCount;i++)
{
if(shascheck[i]!=shas[i])
{
printf("uuidsha %d is wrong\n",i);
return SERVER_UNEXPECTED_ERROR;
}
}
return SERVER_SUCCESS;
}
ServerError
LicensefileSave(const std::string& path,const std::string& path2){
std::string filemd5;
LicenseGetfilemd5(path,filemd5);
std::ofstream file(path2);
time_t last_time;
LicenseGetfiletime(path,last_time);
boost::archive::binary_oarchive oa(file);
oa << filemd5;
oa << last_time;
file.close();
return SERVER_SUCCESS;
}
ServerError
LicensefileLoad(const std::string& path2,std::string& filemd5,time_t& last_time){
std::ifstream file(path2);
boost::archive::binary_iarchive ia(file);
ia >> 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
#pragma once
#include "utils/Error.h"
#include <cuda.h>
#include <memory>
#include <iostream>
#include <vector>
#include <string.h>
#include <sstream>
#include <fstream>
#include <sys/stat.h>
#include "boost/archive/binary_oarchive.hpp"
#include "boost/archive/binary_iarchive.hpp"
#include <cuda_runtime.h>
#include <nvml.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
namespace zilliz {
namespace vecwise {
namespace server {
ServerError
LicenseSave(const std::string& path,const int& deviceCount,const std::vector<std::string>& shas);
ServerError
LicenseLoad(const std::string& path,int& deviceCount,std::vector<std::string>& 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<std::string>& uuids);
ServerError
LicenseGetuuidmd5(const int& deviceCount,std::vector<std::string>& uuids,std::vector<std::string>& md5s);
ServerError
LicenseGetfilemd5(const std::string& path,std::string& filemd5);
ServerError
LicenseGetuuidsha(const int& deviceCount,std::vector<std::string>& uuids,std::vector<std::string>& 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);
}
}
}
#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
#pragma once
#include "utils/Error.h"
namespace zilliz {
namespace vecwise {
namespace server {
}
}
}
......@@ -7,7 +7,7 @@
#include <map>
#include "utils/Error.h"
#include "license/License.h"
#include "license/LicenseCheck.h"
#include "license/LicensePublic.h"
using namespace zilliz::vecwise;
......
......@@ -5,7 +5,7 @@
#include <iostream>
#include "utils/Error.h"
#include "license/License.h"
#include "license/LicenseCheck.h"
using namespace zilliz::vecwise;
......
......@@ -15,7 +15,9 @@
#include <boost/archive/binary_iarchive.hpp>
//#include <boost/foreach.hpp>
//#include <boost/serialization/vector.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/serialization/map.hpp>
#include <boost/filesystem/operations.hpp>
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<int, std::string> &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<LicenseFile>();
......@@ -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<SecretFile>();
......@@ -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<int, std::string> &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<GPUInfoFile>();
......@@ -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));
......
......@@ -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);
......
......@@ -3,7 +3,7 @@
//
#include "LicensePublic.h"
#include "license/License.h"
#include "license/LicenseCheck.h"
//#include "BoostArchive.h"
#define RTime 100
......
......@@ -6,7 +6,7 @@
#include <map>
#include "utils/Error.h"
#include "license/License.h"
#include "license/LicenseCheck.h"
#include "license/LicensePublic.h"
......
......@@ -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 <fcntl.h>
#include <sys/stat.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:
......
......@@ -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;
......
......@@ -5,7 +5,7 @@
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include <license/LicenseLibrary.h>
#include "license/License.h"
#include "license/LicenseCheck.h"
#include "license/LicensePublic.h"
#include "utils/Error.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册