diff mbox

[API-NEXT,PATCHv9,02/15] linux-gen: init: removing possible obsolete ODP files at startup

Message ID 1480004553-46760-3-git-send-email-christophe.milard@linaro.org
State New
Headers show

Commit Message

Christophe Milard Nov. 24, 2016, 4:22 p.m. UTC
When an ODP program is killed, some odp files may remain in /tmp and
the huge page mount point. As signal KILL cannot be caught, there is not
much one can do to prevent that.
But when an new odp session is started, all files prefixed with the opd
prefix ("odp-<PID>-") can be safely removed as the PID is unique and
therefore, there cannot be another ODP instance with the same PID.
This patch does this cleanup at startup.

Signed-off-by: Christophe Milard <christophe.milard@linaro.org>

---
 platform/linux-generic/odp_init.c | 60 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

-- 
2.7.4
diff mbox

Patch

diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index d4a8e09..6e58ab6 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -10,15 +10,71 @@ 
 #include <odp_internal.h>
 #include <odp_schedule_if.h>
 #include <string.h>
+#include <stdio.h>
+#include <linux/limits.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#define _ODP_FILES_FMT "odp-%d-"
+#define _ODP_TMPDIR    "/tmp"
 
 struct odp_global_data_s odp_global_data;
 
+/* remove all files staring with "odp-<pid>" from a directory "dir" */
+static int cleanup_files(const char *dirpath, int odp_pid)
+{
+	struct dirent *e;
+	DIR *dir;
+	char prefix[PATH_MAX];
+	char *fullpath;
+	int d_len = strlen(dirpath);
+	int p_len;
+	int f_len;
+
+	dir = opendir(dirpath);
+	if (!dir) {
+		/* ok if the dir does not exist. no much to delete then! */
+		ODP_DBG("opendir failed for %s: %s\n",
+			dirpath, strerror(errno));
+		return 0;
+	}
+	snprintf(prefix, PATH_MAX, _ODP_FILES_FMT, odp_pid);
+	p_len = strlen(prefix);
+	while ((e = readdir(dir)) != NULL) {
+		if (strncmp(e->d_name, prefix, p_len) == 0) {
+			f_len = strlen(e->d_name);
+			fullpath = malloc(d_len + f_len + 2);
+			if (fullpath == NULL) {
+				closedir(dir);
+				return -1;
+			}
+			snprintf(fullpath, PATH_MAX, "%s/%s",
+				 dirpath, e->d_name);
+			ODP_DBG("deleting obsolete file: %s\n", fullpath);
+			if (unlink(fullpath))
+				ODP_ERR("unlink failed for %s: %s\n",
+					fullpath, strerror(errno));
+			free(fullpath);
+		}
+	}
+	closedir(dir);
+
+	return 0;
+}
+
 int odp_init_global(odp_instance_t *instance,
 		    const odp_init_t *params,
 		    const odp_platform_init_t *platform_params)
 {
+	char *hpdir;
+
 	memset(&odp_global_data, 0, sizeof(struct odp_global_data_s));
 	odp_global_data.main_pid = getpid();
+	cleanup_files(_ODP_TMPDIR, odp_global_data.main_pid);
+
 	if (platform_params)
 		odp_global_data.ipc_ns = platform_params->ipc_ns;
 
@@ -49,6 +105,10 @@  int odp_init_global(odp_instance_t *instance,
 		ODP_ERR("ODP system_info init failed.\n");
 		goto init_failed;
 	}
+	hpdir = odp_global_data.hugepage_info.default_huge_page_dir;
+	/* cleanup obsolete huge page files, if any */
+	if (hpdir)
+		cleanup_files(hpdir, odp_global_data.main_pid);
 	stage = SYSINFO_INIT;
 
 	if (_odp_fdserver_init_global()) {