diff mbox

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

Message ID 1479975872-4945-3-git-send-email-christophe.milard@linaro.org
State Superseded
Headers show

Commit Message

Christophe Milard Nov. 24, 2016, 8:24 a.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 | 51 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

-- 
2.7.4

Comments

Maxim Uvarov Nov. 24, 2016, 2:36 p.m. UTC | #1
On 11/24/16 11:24, Christophe Milard wrote:
> 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 | 51 +++++++++++++++++++++++++++++++++++++++

>   1 file changed, 51 insertions(+)

>

> diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c

> index d4a8e09..6f63a4a 100644

> --- a/platform/linux-generic/odp_init.c

> +++ b/platform/linux-generic/odp_init.c

> @@ -10,15 +10,62 @@

>   #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);

> +	sprintf(prefix, _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)

close(dir)

> +				return -1;

> +			sprintf(fullpath, "%s/%s", dirpath, e->d_name);


snprintf

> +			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 +96,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()) {
Christophe Milard Nov. 24, 2016, 2:43 p.m. UTC | #2
On 24 November 2016 at 15:36, Maxim Uvarov <maxim.uvarov@linaro.org> wrote:
> On 11/24/16 11:24, Christophe Milard wrote:

>>

>> 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 | 51

>> +++++++++++++++++++++++++++++++++++++++

>>   1 file changed, 51 insertions(+)

>>

>> diff --git a/platform/linux-generic/odp_init.c

>> b/platform/linux-generic/odp_init.c

>> index d4a8e09..6f63a4a 100644

>> --- a/platform/linux-generic/odp_init.c

>> +++ b/platform/linux-generic/odp_init.c

>> @@ -10,15 +10,62 @@

>>   #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);

>> +       sprintf(prefix, _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)

>

> close(dir)


Argh!! =>V9

>

>> +                               return -1;

>> +                       sprintf(fullpath, "%s/%s", dirpath, e->d_name);

>

>

> snprintf


what for? fullpath is malloc'ed accordinaly and will neveer exceed
2*PATH_MAX... What n value would you give to snprintf()?

Christophe.
>

>

>> +                       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 +96,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()) {

>

>
diff mbox

Patch

diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index d4a8e09..6f63a4a 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -10,15 +10,62 @@ 
 #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);
+	sprintf(prefix, _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)
+				return -1;
+			sprintf(fullpath, "%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 +96,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()) {