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

2016-11-24 Thread Christophe Milard
On 24 November 2016 at 15:36, Maxim Uvarov  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--") 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 
>> ---
>>   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 
>>   #include 
>>   #include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#define _ODP_FILES_FMT "odp-%d-"
>> +#define _ODP_TMPDIR"/tmp"
>> struct odp_global_data_s odp_global_data;
>>   +/* remove all files staring with "odp-" 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()) {
>
>


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

2016-11-24 Thread Maxim Uvarov

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--") 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 
---
  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 
  #include 
  #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define _ODP_FILES_FMT "odp-%d-"
+#define _ODP_TMPDIR"/tmp"
  
  struct odp_global_data_s odp_global_data;
  
+/* remove all files staring with "odp-" 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()) {




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

2016-11-23 Thread Christophe Milard
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--") 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 
---
 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 
 #include 
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define _ODP_FILES_FMT "odp-%d-"
+#define _ODP_TMPDIR"/tmp"
 
 struct odp_global_data_s odp_global_data;
 
+/* remove all files staring with "odp-" 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()) {
-- 
2.7.4