summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2023-07-07 11:47:29 +0800
committerluwenpeng <[email protected]>2023-07-07 11:47:29 +0800
commit1c1186e36a4fe3cbe3d754b9c0e31cde30878277 (patch)
treeed275e2f37feb6016db7e93daffa124bd73390f5
parent05ad1fba9b25403118825c3b1bb510d7db6777ec (diff)
bugfix: open too many filesv1.0.9-20230707
-rw-r--r--platform/src/hasp_verify.c111
1 files changed, 49 insertions, 62 deletions
diff --git a/platform/src/hasp_verify.c b/platform/src/hasp_verify.c
index 8b2cc32..d38abf6 100644
--- a/platform/src/hasp_verify.c
+++ b/platform/src/hasp_verify.c
@@ -454,8 +454,23 @@ static hasp_status_t encrypt_decrypt(hasp_handle_t handle)
* For Hasp Verify Master Process
******************************************************************************/
-static int hasp_monitor_write(struct shm_data *data)
+static void *hasp_monitor_cycle(void *arg)
{
+ 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);
+
+ signal(SIGUSR1, signal_handler);
+ signal(SIGUSR2, signal_handler);
+
char path[256];
char path_old[512];
char path_new[512];
@@ -471,14 +486,14 @@ static int hasp_monitor_write(struct shm_data *data)
if (fd < 0)
{
LOG_ERROR("hasp_monitor: Could not create shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
- return -1;
+ return NULL;
}
if (ftruncate(fd, size) < 0)
{
LOG_ERROR("hasp_monitor: Could not truncate shared file '%s', error %d: %s", path, errno, strerror(errno));
shm_unlink(path);
- return -1;
+ return NULL;
}
void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SEEK_SET);
@@ -486,7 +501,7 @@ static int hasp_monitor_write(struct shm_data *data)
{
LOG_ERROR("hasp_monitor: Could not mmap shared file '%s', error %d: %s", path, errno, strerror(errno));
shm_unlink(path);
- return -1;
+ return NULL;
}
memset(addr, 0, size);
@@ -510,7 +525,7 @@ static int hasp_monitor_write(struct shm_data *data)
if (fd < 0)
{
LOG_ERROR("hasp_monitor: Could not open shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
- return -1;
+ return NULL;
}
}
else
@@ -523,51 +538,9 @@ static int hasp_monitor_write(struct shm_data *data)
{
LOG_ERROR("hasp_monitor: Could not mmap shared file '%s', error %d: %s", shm_key, errno, strerror(errno));
shm_unlink(shm_key);
- return -1;
+ return NULL;
}
- 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);
-
- signal(SIGUSR1, signal_handler);
- signal(SIGUSR2, signal_handler);
-
while (1)
{
hasp_handle_t handle;
@@ -578,25 +551,23 @@ static void *hasp_monitor_cycle(void *arg)
sleep(1);
continue;
}
+ else
+ {
+ LOG_INFO("hasp_monitor: Login success");
+ }
while (1)
{
status = encrypt_decrypt(handle);
if (status == HASP_STATUS_OK)
{
- 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) == 0)
- {
- LOG_DEBUG("hasp_monitor: Set feature_id: %ld, timestamp: %ld, interval: %ld, status: %ld", data.feature_id, data.timestamp, data.interval, data.status);
- }
- else
- {
- // continue while loop
- }
+ uint64_t timestamp = current_timestamp();
+ ATOMIC_SET(&shm->feature_id, hasp_monitor_feature_id);
+ ATOMIC_SET(&shm->status, 1);
+ ATOMIC_SET(&shm->timestamp, timestamp);
+ ATOMIC_SET(&shm->interval, hasp_monitor_interval);
+
+ LOG_DEBUG("hasp_monitor: Set feature_id: %ld, timestamp: %ld, interval: %ld, status: %ld", hasp_monitor_feature_id, timestamp, hasp_monitor_interval, 1LU);
}
else
{
@@ -612,6 +583,22 @@ static void *hasp_monitor_cycle(void *arg)
sleep(1);
}
+ /*
+ * 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 NULL;
}
@@ -712,7 +699,7 @@ error_out:
*/
if (fd > 0)
{
- // shm_unlink(shm_key);
+ shm_unlink(shm_key);
}
free(arg);