shm lock free
This commit is contained in:
@@ -6,7 +6,7 @@ target_link_libraries(hasp_update ${CMAKE_SOURCE_DIR}/lib/libhasp_linux_x86_64_2
|
||||
install(TARGETS hasp_update RUNTIME DESTINATION bin COMPONENT Program)
|
||||
|
||||
# compile lib hasp-tools
|
||||
add_library(hasp-tools SHARED src/hasp_verify.c src/hasp_shm.c)
|
||||
add_library(hasp-tools SHARED src/hasp_verify.c)
|
||||
target_include_directories(hasp-tools PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
|
||||
target_link_libraries(hasp-tools pthread rt)
|
||||
target_link_libraries(hasp-tools ${CMAKE_SOURCE_DIR}/lib/libhasp_linux_x86_64_25743.a)
|
||||
|
||||
@@ -10,36 +10,37 @@ extern "C"
|
||||
#include <stdio.h>
|
||||
|
||||
#if ENABLD_LOG_FIEL
|
||||
#define LOG_FILE(time, format, ...) \
|
||||
{ \
|
||||
FILE *fp = fopen("licenses.log", "a+"); \
|
||||
if (fp == NULL) \
|
||||
{ \
|
||||
break; \
|
||||
} \
|
||||
fprintf(fp, "%s " format "\n", time, ##__VA_ARGS__); \
|
||||
fflush(fp); \
|
||||
fclose(fp); \
|
||||
#define LOG_FILE(prefix, format, ...) \
|
||||
{ \
|
||||
FILE *fp = fopen("licenses.log", "a+"); \
|
||||
if (fp == NULL) \
|
||||
{ \
|
||||
break; \
|
||||
} \
|
||||
fprintf(fp, "%s " format "\n", prefix, ##__VA_ARGS__); \
|
||||
fflush(fp); \
|
||||
fclose(fp); \
|
||||
}
|
||||
#else
|
||||
#define LOG_FILE(time, format, ...)
|
||||
#define LOG_FILE(prefix, format, ...)
|
||||
#endif
|
||||
|
||||
#define LOG_STOUT(time, format, ...) \
|
||||
{ \
|
||||
fprintf(stderr, "%s " format "\n", time, ##__VA_ARGS__); \
|
||||
#define LOG_STOUT(prefix, format, ...) \
|
||||
{ \
|
||||
fprintf(stderr, "%s " format "\n", prefix, ##__VA_ARGS__); \
|
||||
}
|
||||
|
||||
#define LOG_INFO(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
char buffer[64] = {0}; \
|
||||
local_time_string(buffer, sizeof(buffer)); \
|
||||
LOG_STOUT(buffer, format, ##__VA_ARGS__); \
|
||||
LOG_FILE(buffer, format, ##__VA_ARGS__); \
|
||||
#define LOG_INFO(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
char buffer[128] = {0}; \
|
||||
int n = local_time_string(buffer, sizeof(buffer)); \
|
||||
snprintf(buffer + n, sizeof(buffer) - n, " tid: %ld", pthread_self()); \
|
||||
LOG_STOUT(buffer, format, ##__VA_ARGS__); \
|
||||
LOG_FILE(buffer, format, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
static void local_time_string(char *buff, int size)
|
||||
static int local_time_string(char *buff, int size)
|
||||
{
|
||||
static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
||||
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
@@ -49,17 +50,17 @@ static void local_time_string(char *buff, int size)
|
||||
time(&now);
|
||||
if (NULL == (localtime_r(&now, &local_time)))
|
||||
{
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
snprintf(buff, size,
|
||||
"%s %s %d %02d:%02d:%02d %d",
|
||||
weekday_str[local_time.tm_wday],
|
||||
month_str[local_time.tm_mon],
|
||||
local_time.tm_mday,
|
||||
local_time.tm_hour,
|
||||
local_time.tm_min,
|
||||
local_time.tm_sec,
|
||||
local_time.tm_year + 1900);
|
||||
return snprintf(buff, size,
|
||||
"%s %s %d %02d:%02d:%02d %d",
|
||||
weekday_str[local_time.tm_wday],
|
||||
month_str[local_time.tm_mon],
|
||||
local_time.tm_mday,
|
||||
local_time.tm_hour,
|
||||
local_time.tm_min,
|
||||
local_time.tm_sec,
|
||||
local_time.tm_year + 1900);
|
||||
}
|
||||
|
||||
#ifdef __cpluscplus
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef _HASP_SHM_H
|
||||
#define _HASP_SHM_H
|
||||
|
||||
#ifdef __cpluscplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct hasp_shm;
|
||||
|
||||
struct hasp_shm *hasp_shm_new(const char *name);
|
||||
void hasp_shm_free(struct hasp_shm *shm);
|
||||
|
||||
void hasp_shm_lock(struct hasp_shm *shm);
|
||||
void hasp_shm_unlock(struct hasp_shm *shm);
|
||||
void hasp_shm_write(struct hasp_shm *shm, char *data, int len);
|
||||
void hasp_shm_read(struct hasp_shm *shm, char *buff, int size);
|
||||
|
||||
#ifdef __cpluscplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,126 +0,0 @@
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "hasp_log.h"
|
||||
|
||||
#define MAX_SHM_BUFF_SIZE 4096
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
struct hasp_shm
|
||||
{
|
||||
pthread_mutex_t lock;
|
||||
void *buff;
|
||||
int size;
|
||||
};
|
||||
|
||||
static void hasp_shm_lock_init(struct hasp_shm *shm)
|
||||
{
|
||||
pthread_mutexattr_t ma;
|
||||
pthread_mutexattr_init(&ma);
|
||||
pthread_mutexattr_setpshared(&ma, PTHREAD_PROCESS_SHARED);
|
||||
pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST);
|
||||
pthread_mutex_init(&shm->lock, &ma);
|
||||
}
|
||||
|
||||
struct hasp_shm *hasp_shm_new(const char *name)
|
||||
{
|
||||
char path[256];
|
||||
char path_old[512];
|
||||
char path_new[512];
|
||||
|
||||
int size = sizeof(struct hasp_shm) + MAX_SHM_BUFF_SIZE;
|
||||
int fd = shm_open(name, O_RDWR, 0777);
|
||||
if (fd < 0)
|
||||
{
|
||||
sprintf(path, "%s.%d", name, getpid());
|
||||
fd = shm_open(path, O_CREAT | O_RDWR, 0777);
|
||||
if (fd < 0)
|
||||
{
|
||||
LOG_INFO("hasp_shm: Unable create shared memory %s: fd %d, error %d: %s", name, fd, errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (ftruncate(fd, size) < 0)
|
||||
{
|
||||
LOG_INFO("hasp_shm: Unable truncate file %s, error %d: %s", path, errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SEEK_SET);
|
||||
if (addr == NULL)
|
||||
{
|
||||
LOG_INFO("hasp_shm: Unable mmap memory for file %s, error %d: %s", path, errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(addr, 0, size);
|
||||
hasp_shm_lock_init((struct hasp_shm *)addr);
|
||||
munmap(addr, size);
|
||||
|
||||
sprintf(path_old, "/dev/shm/%s", path);
|
||||
sprintf(path_new, "/dev/shm/%s", name);
|
||||
|
||||
int r = link(path_old, path_new);
|
||||
LOG_INFO("hasp_shm: Create link(%s, %s): %d", path_old, path_new, r);
|
||||
unlink(path_old);
|
||||
|
||||
fd = shm_open(name, O_RDWR, 0777);
|
||||
if (fd < 0)
|
||||
{
|
||||
LOG_INFO("hasp_shm: Unable create shared memory %s: fd %d, error %d: %s", name, fd, errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct hasp_shm *shm = (struct hasp_shm *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SEEK_SET);
|
||||
if (shm == NULL)
|
||||
{
|
||||
LOG_INFO("hasp_shm: Unable mmap memory for fd %d, error %d: %s", fd, errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
shm->buff = (void *)shm + sizeof(struct hasp_shm);
|
||||
shm->size = MAX_SHM_BUFF_SIZE;
|
||||
|
||||
return shm;
|
||||
}
|
||||
|
||||
void hasp_shm_lock(struct hasp_shm *shm)
|
||||
{
|
||||
if (pthread_mutex_lock(&shm->lock) == EOWNERDEAD)
|
||||
{
|
||||
pthread_mutex_consistent(&shm->lock);
|
||||
LOG_INFO("hasp_shm: Mutex lock mark consistent");
|
||||
}
|
||||
}
|
||||
|
||||
void hasp_shm_unlock(struct hasp_shm *shm)
|
||||
{
|
||||
pthread_mutex_unlock(&shm->lock);
|
||||
}
|
||||
|
||||
void hasp_shm_write(struct hasp_shm *shm, char *data, int len)
|
||||
{
|
||||
int wlen = MIN(shm->size, len);
|
||||
memset(shm->buff, 0, shm->size);
|
||||
memcpy(shm->buff, data, wlen);
|
||||
}
|
||||
|
||||
void hasp_shm_read(struct hasp_shm *shm, char *buff, int size)
|
||||
{
|
||||
int rlen = MIN(shm->size, size);
|
||||
memset(buff, 0, size);
|
||||
memcpy(buff, shm->buff, rlen);
|
||||
}
|
||||
|
||||
void hasp_shm_free(struct hasp_shm *shm)
|
||||
{
|
||||
munmap(shm, sizeof(struct hasp_shm) + MAX_SHM_BUFF_SIZE);
|
||||
}
|
||||
@@ -1,21 +1,38 @@
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "hasp_api.h"
|
||||
#include "hasp_vcode.h"
|
||||
#include "hasp_log.h"
|
||||
#include "hasp_shm.h"
|
||||
|
||||
#define DEFAULT_INTERVAL_S (30 * 60)
|
||||
#define MAX_INTERVAL_S (24 * 60 * 60)
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#define ATOMIC_READ(x) __atomic_fetch_add(x, 0, __ATOMIC_RELAXED)
|
||||
#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)
|
||||
|
||||
static char *shm_key = "hasp_verify";
|
||||
|
||||
static uint64_t app_expect_feature_id = 0;
|
||||
static uint64_t hasp_monitor_feature_id = 0;
|
||||
static uint64_t hasp_monitor_interval = 0;
|
||||
|
||||
struct shm_data
|
||||
{
|
||||
uint64_t feature_id;
|
||||
uint64_t status;
|
||||
uint64_t timestamp;
|
||||
uint64_t interval;
|
||||
};
|
||||
|
||||
/******************************************************************************
|
||||
* Utils
|
||||
@@ -28,6 +45,10 @@ static uint64_t current_timestamp()
|
||||
return temp.tv_sec;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* For Hasp Verify Master Process
|
||||
******************************************************************************/
|
||||
|
||||
// return 0: error
|
||||
// reutrn 1: succes
|
||||
static int verify(uint64_t feature_id)
|
||||
@@ -44,76 +65,76 @@ static int verify(uint64_t feature_id)
|
||||
switch (status)
|
||||
{
|
||||
case HASP_STATUS_OK:
|
||||
LOG_INFO("hasp_verify: Request was successfully completed");
|
||||
LOG_INFO("hasp_monitor: Request was successfully completed");
|
||||
break;
|
||||
case HASP_HASP_NOT_FOUND:
|
||||
LOG_INFO("hasp_verify: Required Sentinel protection key not found");
|
||||
LOG_INFO("hasp_monitor: Required Sentinel protection key not found");
|
||||
break;
|
||||
case HASP_FEATURE_NOT_FOUND:
|
||||
LOG_INFO("hasp_verify: Cannot find requested Feature");
|
||||
LOG_INFO("hasp_monitor: Cannot find requested Feature");
|
||||
break;
|
||||
case HASP_FEATURE_TYPE_NOT_IMPL:
|
||||
LOG_INFO("hasp_verify: Requested Feature type not available");
|
||||
LOG_INFO("hasp_monitor: Requested Feature type not available");
|
||||
break;
|
||||
case HASP_TMOF:
|
||||
LOG_INFO("hasp_verify: Too many open login sessions");
|
||||
LOG_INFO("hasp_monitor: Too many open login sessions");
|
||||
break;
|
||||
case HASP_INSUF_MEM:
|
||||
LOG_INFO("hasp_verify: Out of memory");
|
||||
LOG_INFO("hasp_monitor: Out of memory");
|
||||
break;
|
||||
case HASP_INV_VCODE:
|
||||
LOG_INFO("hasp_verify: Invalid Vendor Code");
|
||||
LOG_INFO("hasp_monitor: Invalid Vendor Code");
|
||||
break;
|
||||
case HASP_NO_DRIVER:
|
||||
LOG_INFO("hasp_verify: Driver not installed");
|
||||
LOG_INFO("hasp_monitor: Driver not installed");
|
||||
break;
|
||||
case HASP_NO_VLIB:
|
||||
LOG_INFO("hasp_verify: Vendor library cannot be found");
|
||||
LOG_INFO("hasp_monitor: Vendor library cannot be found");
|
||||
break;
|
||||
case HASP_INV_VLIB:
|
||||
LOG_INFO("hasp_verify: Vendor library cannot be loaded");
|
||||
LOG_INFO("hasp_monitor: Vendor library cannot be loaded");
|
||||
break;
|
||||
case HASP_OLD_DRIVER:
|
||||
LOG_INFO("hasp_verify: Driver too old");
|
||||
LOG_INFO("hasp_monitor: Driver too old");
|
||||
break;
|
||||
case HASP_UNKNOWN_VCODE:
|
||||
LOG_INFO("hasp_verify: Vendor Code not recognized");
|
||||
LOG_INFO("hasp_monitor: Vendor Code not recognized");
|
||||
break;
|
||||
case HASP_FEATURE_EXPIRED:
|
||||
LOG_INFO("hasp_verify: Feature has expired");
|
||||
LOG_INFO("hasp_monitor: Feature has expired");
|
||||
break;
|
||||
case HASP_TOO_MANY_USERS:
|
||||
LOG_INFO("hasp_verify: Too many users currently connected");
|
||||
LOG_INFO("hasp_monitor: Too many users currently connected");
|
||||
break;
|
||||
case HASP_OLD_LM:
|
||||
LOG_INFO("hasp_verify: Sentinel License Manager version too old");
|
||||
LOG_INFO("hasp_monitor: Sentinel License Manager version too old");
|
||||
break;
|
||||
case HASP_DEVICE_ERR:
|
||||
LOG_INFO("hasp_verify: Input/Output error in Sentinel SL/SL-AdminMode/SL-UserMode secure storage, OR in case of a Sentinel HL key, USB communication error");
|
||||
LOG_INFO("hasp_monitor: Input/Output error in Sentinel SL/SL-AdminMode/SL-UserMode secure storage, OR in case of a Sentinel HL key, USB communication error");
|
||||
break;
|
||||
case HASP_TIME_ERR:
|
||||
LOG_INFO("hasp_verify: System time has been tampered with");
|
||||
LOG_INFO("hasp_monitor: System time has been tampered with");
|
||||
break;
|
||||
case HASP_HARDWARE_MODIFIED:
|
||||
LOG_INFO("hasp_verify: Sentinel SL key incompatible with machine hardware; Sentinel SL key is locked to different hardware");
|
||||
LOG_INFO("hasp_monitor: Sentinel SL key incompatible with machine hardware; Sentinel SL key is locked to different hardware");
|
||||
break;
|
||||
case HASP_TS_DETECTED:
|
||||
LOG_INFO("hasp_verify: Program is running on a Terminal Server");
|
||||
LOG_INFO("hasp_monitor: Program is running on a Terminal Server");
|
||||
break;
|
||||
case HASP_LOCAL_COMM_ERR:
|
||||
LOG_INFO("hasp_verify: Communication error between API and local Sentinel License Manager");
|
||||
LOG_INFO("hasp_monitor: Communication error between API and local Sentinel License Manager");
|
||||
break;
|
||||
case HASP_REMOTE_COMM_ERR:
|
||||
LOG_INFO("hasp_verify: Communication error between local and remote Sentinel License Manager");
|
||||
LOG_INFO("hasp_monitor: Communication error between local and remote Sentinel License Manager");
|
||||
break;
|
||||
case HASP_OLD_VLIB:
|
||||
LOG_INFO("hasp_verify: Vendor Library version too old");
|
||||
LOG_INFO("hasp_monitor: Vendor Library version too old");
|
||||
break;
|
||||
case HASP_CLONE_DETECTED:
|
||||
LOG_INFO("hasp_verify: Cloned Sentinel SL storage detected. Feature unavailable");
|
||||
LOG_INFO("hasp_monitor: Cloned Sentinel SL storage detected. Feature unavailable");
|
||||
break;
|
||||
default:
|
||||
LOG_INFO("hasp_verify: failed with status %u", status);
|
||||
LOG_INFO("hasp_monitor: failed with status %u", status);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -125,94 +146,231 @@ static int verify(uint64_t feature_id)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void write_status(uint64_t feature_id, uint64_t status, uint64_t timestamp, uint64_t interva)
|
||||
static int hasp_monitor_write(struct shm_data *data)
|
||||
{
|
||||
char buff[4096] = {0};
|
||||
struct hasp_shm *shm = hasp_shm_new(shm_key);
|
||||
snprintf(buff, sizeof(buff), "%ld\t%ld\t%ld\t%ld", feature_id, status, timestamp, interva);
|
||||
hasp_shm_write(shm, buff, strlen(buff));
|
||||
hasp_shm_free(shm);
|
||||
}
|
||||
char path[256];
|
||||
char path_old[512];
|
||||
char path_new[512];
|
||||
|
||||
static void read_status(char *buff, int size)
|
||||
{
|
||||
struct hasp_shm *shm = hasp_shm_new(shm_key);
|
||||
hasp_shm_read(shm, buff, size);
|
||||
hasp_shm_free(shm);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* For Hasp Verify Master Process
|
||||
******************************************************************************/
|
||||
|
||||
void hasp_monitor(uint64_t feature_id, uint64_t interval)
|
||||
{
|
||||
if (interval >= MAX_INTERVAL_S)
|
||||
int size = sizeof(struct shm_data);
|
||||
int fd = shm_open(shm_key, O_RDWR, 0777);
|
||||
if (fd < 0)
|
||||
{
|
||||
interval = MAX_INTERVAL_S;
|
||||
LOG_INFO("hasp_monitor: Could not find shared file '%s', try create it", shm_key);
|
||||
|
||||
sprintf(path, "%s.%d", shm_key, getpid());
|
||||
fd = shm_open(path, O_CREAT | O_RDWR, 0777);
|
||||
if (fd < 0)
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Could not create shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ftruncate(fd, size) < 0)
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Could not truncate shared file '%s', error %d: %s", path, errno, strerror(errno));
|
||||
shm_unlink(path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SEEK_SET);
|
||||
if (addr == NULL)
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Could not mmap shared file '%s', error %d: %s", path, errno, strerror(errno));
|
||||
shm_unlink(path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(addr, 0, size);
|
||||
munmap(addr, size);
|
||||
|
||||
sprintf(path_old, "/dev/shm/%s", path);
|
||||
sprintf(path_new, "/dev/shm/%s", shm_key);
|
||||
|
||||
int r = link(path_old, path_new);
|
||||
if (r == -1)
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Create link('%s', '%s'), error %d: %s", path_old, path_new, errno, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Create link('%s', '%s') success", path_old, path_new);
|
||||
}
|
||||
unlink(path_old);
|
||||
|
||||
fd = shm_open(shm_key, O_RDWR, 0777);
|
||||
if (fd < 0)
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Could not open shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Open shared file '%s' success", shm_key);
|
||||
}
|
||||
|
||||
if (interval == 0)
|
||||
struct shm_data *shm = (struct shm_data *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SEEK_SET);
|
||||
if (shm == NULL)
|
||||
{
|
||||
interval = DEFAULT_INTERVAL_S;
|
||||
LOG_INFO("hasp_monitor: Could not mmap shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
|
||||
shm_unlink(shm_key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
LOG_INFO("hasp_verify: Feature ID: %ld, Interval: %ld s", feature_id, interval);
|
||||
ATOMIC_SET(&shm->feature_id, data->feature_id);
|
||||
ATOMIC_SET(&shm->status, data->status);
|
||||
ATOMIC_SET(&shm->timestamp, data->timestamp);
|
||||
ATOMIC_SET(&shm->interval, data->interval);
|
||||
|
||||
/*
|
||||
* MAP_SHARED
|
||||
*
|
||||
* Share this mapping.
|
||||
* Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.
|
||||
* The file may not actually be updated until msync(2) or munmap() is called.
|
||||
*/
|
||||
munmap(shm, sizeof(struct shm_data));
|
||||
|
||||
/*
|
||||
* Unlink the shared memory object.
|
||||
* Even if the peer process is still using the object, this is okay.
|
||||
* The object will be removed only after all open references are closed.
|
||||
*/
|
||||
// shm_unlink(shm_key);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *hasp_monitor_cycle(void *arg)
|
||||
{
|
||||
struct shm_data data;
|
||||
if (hasp_monitor_interval >= MAX_INTERVAL_S)
|
||||
{
|
||||
hasp_monitor_interval = MAX_INTERVAL_S;
|
||||
}
|
||||
|
||||
if (hasp_monitor_interval == 0)
|
||||
{
|
||||
hasp_monitor_interval = DEFAULT_INTERVAL_S;
|
||||
}
|
||||
|
||||
LOG_INFO("hasp_monitor: Feature ID: %ld, Interval: %ld s", hasp_monitor_feature_id, hasp_monitor_interval);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (verify(feature_id) == 1)
|
||||
if (verify(hasp_monitor_feature_id) == 1)
|
||||
{
|
||||
write_status(feature_id, 1, current_timestamp(), interval);
|
||||
memset(&data, 0, sizeof(data));
|
||||
data.feature_id = hasp_monitor_feature_id;
|
||||
data.status = 1;
|
||||
data.timestamp = current_timestamp();
|
||||
data.interval = hasp_monitor_interval;
|
||||
if (hasp_monitor_write(&data) == -1)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
LOG_INFO("hasp_monitor: Set feature_id: %ld, timestamp: %ld, interval: %ld, status: %ld", data.feature_id, data.timestamp, data.interval, data.status);
|
||||
}
|
||||
|
||||
sleep(interval);
|
||||
sleep(hasp_monitor_interval);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void hasp_monitor(uint64_t feature_id, uint64_t interval)
|
||||
{
|
||||
pthread_t tid;
|
||||
hasp_monitor_feature_id = feature_id;
|
||||
hasp_monitor_interval = interval;
|
||||
|
||||
if (pthread_create(&tid, NULL, hasp_monitor_cycle, NULL) < 0)
|
||||
{
|
||||
LOG_INFO("hasp_monitor: Could not create hasp monitor thread, error %d: %s", errno, strerror(errno));
|
||||
exit(0);
|
||||
}
|
||||
|
||||
pthread_join(tid, NULL);
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* For Hasp Verify Slave Process
|
||||
******************************************************************************/
|
||||
|
||||
void *hasp_verify_cycle(void *arg)
|
||||
static int hasp_verify_read(struct shm_data *data)
|
||||
{
|
||||
char buff[4096] = {0};
|
||||
int size = sizeof(buff);
|
||||
memset(data, 0, sizeof(struct shm_data));
|
||||
int fd = shm_open(shm_key, O_RDONLY, 0644);
|
||||
if (fd < 0)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Could not open shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint64_t feature_id = 0;
|
||||
uint64_t status = 0;
|
||||
uint64_t timestamp = 0;
|
||||
uint64_t interva = 0;
|
||||
int size = sizeof(struct shm_data);
|
||||
struct shm_data *addr = (struct shm_data *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, SEEK_SET);
|
||||
if (addr == NULL)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Could not mmap shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
|
||||
shm_unlink(shm_key);
|
||||
return -1;
|
||||
}
|
||||
|
||||
data->feature_id = addr->feature_id;
|
||||
data->status = addr->status;
|
||||
data->timestamp = addr->timestamp;
|
||||
data->interval = addr->interval;
|
||||
|
||||
/*
|
||||
* MAP_SHARED
|
||||
*
|
||||
* Share this mapping.
|
||||
* Updates to the mapping are visible to other processes that map this file, and are carried through to the underlying file.
|
||||
* The file may not actually be updated until msync(2) or munmap() is called.
|
||||
*/
|
||||
munmap(addr, size);
|
||||
|
||||
/*
|
||||
* Unlink the shared memory object.
|
||||
* Even if the peer process is still using the object, this is okay.
|
||||
* The object will be removed only after all open references are closed.
|
||||
*/
|
||||
// shm_unlink(shm_key);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *hasp_verify_cycle(void *arg)
|
||||
{
|
||||
struct shm_data data;
|
||||
uint64_t expect_feature_id = *(uint64_t *)arg;
|
||||
|
||||
LOG_INFO("hasp_verify: Expect Feature ID: %ld", expect_feature_id);
|
||||
|
||||
while (1)
|
||||
{
|
||||
read_status(buff, size);
|
||||
|
||||
if (strlen(buff) == 0)
|
||||
if (hasp_verify_read(&data) == -1)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Could not get shared data");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (sscanf(buff, "%ld\t%ld\t%ld\t%ld", &feature_id, &status, ×tamp, &interva) != 4)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Invalid shared data");
|
||||
exit(0);
|
||||
}
|
||||
LOG_INFO("hasp_verify: Get feature_id: %ld, timestamp: %ld, interval: %ld, status: %ld", data.feature_id, data.timestamp, data.interval, data.status);
|
||||
|
||||
if (app_expect_feature_id != feature_id)
|
||||
if (expect_feature_id != data.feature_id)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Unexpected feature id");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (current_timestamp() - timestamp > interva)
|
||||
if (current_timestamp() - data.timestamp > data.interval * 2)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Timestamp not updated for a long time");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (status == 0)
|
||||
if (data.status == 0)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Invalid authorization information");
|
||||
exit(0);
|
||||
@@ -221,17 +379,19 @@ void *hasp_verify_cycle(void *arg)
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
free(arg);
|
||||
arg = NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void hasp_verify(uint64_t feature_id)
|
||||
{
|
||||
pthread_t tid;
|
||||
app_expect_feature_id = feature_id;
|
||||
LOG_INFO("hasp_verify: Feature ID: %ld", app_expect_feature_id);
|
||||
if (pthread_create(&tid, NULL, hasp_verify_cycle, NULL) < 0)
|
||||
uint64_t *hasp_verify_feature_id = (uint64_t *)calloc(1, sizeof(uint64_t));
|
||||
*hasp_verify_feature_id = feature_id;
|
||||
if (pthread_create(&tid, NULL, hasp_verify_cycle, hasp_verify_feature_id) < 0)
|
||||
{
|
||||
LOG_INFO("hasp_verify: Unable create hasp verify thread, error %d: %s", errno, strerror(errno));
|
||||
LOG_INFO("hasp_verify: Could not create hasp verify thread, error %d: %s", errno, strerror(errno));
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ static void usage(char *cmd)
|
||||
{
|
||||
fprintf(stderr, "USAGE: %s [OPTIONS]\n", cmd);
|
||||
fprintf(stderr, " -f -- set feature id\n");
|
||||
fprintf(stderr, " -n -- set thread number\n");
|
||||
fprintf(stderr, " -h -- show help\n");
|
||||
}
|
||||
|
||||
@@ -15,14 +16,18 @@ int main(int argc, char **argv)
|
||||
{
|
||||
int opt;
|
||||
uint64_t feature_id = 0;
|
||||
uint64_t thread_number = 1;
|
||||
|
||||
while ((opt = getopt(argc, argv, "f:h")) != -1)
|
||||
while ((opt = getopt(argc, argv, "f:n:h")) != -1)
|
||||
{
|
||||
switch (opt)
|
||||
{
|
||||
case 'f':
|
||||
feature_id = atoi(optarg);
|
||||
break;
|
||||
case 'n':
|
||||
thread_number = atoi(optarg);
|
||||
break;
|
||||
case 'h': /* fall through */
|
||||
default:
|
||||
usage(argv[0]);
|
||||
@@ -30,7 +35,10 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
hasp_verify(feature_id);
|
||||
for (int i = 0; i < thread_number; i++)
|
||||
{
|
||||
hasp_verify(feature_id);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user