summaryrefslogtreecommitdiff
path: root/chip/host
diff options
context:
space:
mode:
Diffstat (limited to 'chip/host')
-rw-r--r--chip/host/persistence.c36
1 files changed, 22 insertions, 14 deletions
diff --git a/chip/host/persistence.c b/chip/host/persistence.c
index 4be2683bb1..dbd1ce812c 100644
--- a/chip/host/persistence.c
+++ b/chip/host/persistence.c
@@ -5,35 +5,43 @@
/* Persistence module for emulator */
+#include <linux/limits.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
-#define BUF_SIZE 1024
-
static void get_storage_path(char *out)
{
- char buf[BUF_SIZE];
+ char buf[PATH_MAX];
int sz;
+ char *current;
- sz = readlink("/proc/self/exe", buf, BUF_SIZE);
+ sz = readlink("/proc/self/exe", buf, PATH_MAX - 1);
buf[sz] = '\0';
- if (snprintf(out, BUF_SIZE, "%s_persist", buf) >= BUF_SIZE)
- out[BUF_SIZE - 1] = '\0';
+
+ /* replace / by underscores in the path to get the shared memory name */
+ current = strchr(buf, '/');
+ while (current) {
+ *current = '_';
+ current = strchr(current, '/');
+ }
+
+ snprintf(out, PATH_MAX - 1, "/dev/shm/EC_persist_%s", buf);
+ out[PATH_MAX - 1] = '\0';
}
FILE *get_persistent_storage(const char *tag, const char *mode)
{
- char buf[BUF_SIZE];
- char path[BUF_SIZE];
+ char buf[PATH_MAX];
+ char path[PATH_MAX];
/*
* The persistent storage with tag 'foo' for test 'bar' would
* be named 'bar_persist_foo'
*/
get_storage_path(buf);
- if (snprintf(path, BUF_SIZE, "%s_%s", buf, tag) >= BUF_SIZE)
- path[BUF_SIZE - 1] = '\0';
+ snprintf(path, PATH_MAX - 1, "%s_%s", buf, tag);
+ path[PATH_MAX - 1] = '\0';
return fopen(path, mode);
}
@@ -45,12 +53,12 @@ void release_persistent_storage(FILE *ps)
void remove_persistent_storage(const char *tag)
{
- char buf[BUF_SIZE];
- char path[BUF_SIZE];
+ char buf[PATH_MAX];
+ char path[PATH_MAX];
get_storage_path(buf);
- if (snprintf(path, BUF_SIZE, "%s_%s", buf, tag) >= BUF_SIZE)
- path[BUF_SIZE - 1] = '\0';
+ snprintf(path, PATH_MAX - 1, "%s_%s", buf, tag);
+ path[PATH_MAX - 1] = '\0';
unlink(path);
}