diff mbox

[API-NEXT] linux-gen: _ishm: unlinking files asap for cleaner termination

Message ID 1480671764-10451-1-git-send-email-christophe.milard@linaro.org
State Superseded
Headers show

Commit Message

Christophe Milard Dec. 2, 2016, 9:42 a.m. UTC
_ishm now unlinks the created files as soon as possible, hence reducing
the chance to see left-overs, if ODP terminates abnormaly.
This does not provide 100% guaranty: if we are unlucky enough,
ODP may be killed between open() and unlink().
Also this method excludes exported files (flag _ODP_ISHM_EXPORT),
whose names shall be seen in the file system.

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

---
 platform/linux-generic/_ishm.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

-- 
2.7.4

Comments

Maxim Uvarov Dec. 6, 2016, 3:26 p.m. UTC | #1
On 12/02/16 12:42, Christophe Milard wrote:
> _ishm now unlinks the created files as soon as possible, hence reducing

> the chance to see left-overs, if ODP terminates abnormaly.

> This does not provide 100% guaranty: if we are unlucky enough,

> ODP may be killed between open() and unlink().

> Also this method excludes exported files (flag _ODP_ISHM_EXPORT),

> whose names shall be seen in the file system.

> 

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

> ---

>  platform/linux-generic/_ishm.c | 14 ++++++++++++--

>  1 file changed, 12 insertions(+), 2 deletions(-)

> 

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

> index 449e357..84d5225 100644

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

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

> @@ -71,6 +71,7 @@

>  #include <sys/types.h>

>  #include <inttypes.h>

>  #include <sys/wait.h>

> +#include <libgen.h>

>  

>  /*

>   * Maximum number of internal shared memory blocks.

> @@ -159,6 +160,7 @@ typedef struct ishm_block {

>  	char exptname[ISHM_FILENAME_MAXLEN]; /* name of the export file     */

>  	uint32_t user_flags;     /* any flags the user want to remember.    */

>  	uint32_t flags;          /* block creation flags.                   */

> +	uint32_t external_fd;    /* block FD was externaly provided         */



externaly -> externally

and maybe uint32_t external_fd:1; ?

Maxim.

>  	uint64_t user_len;	 /* length, as requested at reserve time.   */

>  	void *start;		 /* only valid if _ODP_ISHM_SINGLE_VA is set*/

>  	uint64_t len;		 /* length. multiple of page size. 0 if free*/

> @@ -452,15 +454,17 @@ static int create_file(int block_index, huge_flag_t huge, uint64_t len,

>  		ODP_ERR("ftruncate failed: fd=%d, err=%s.\n",

>  			fd, strerror(errno));

>  		close(fd);

> +		unlink(filename);

>  		return -1;

>  	}

>  

> -	strncpy(new_block->filename, filename, ISHM_FILENAME_MAXLEN - 1);

>  

>  	/* if _ODP_ISHM_EXPORT is set, create a description file for

>  	 * external ref:

>  	 */

>  	if (flags & _ODP_ISHM_EXPORT) {

> +		strncpy(new_block->filename, filename,

> +			ISHM_FILENAME_MAXLEN - 1);

>  		snprintf(new_block->exptname, ISHM_FILENAME_MAXLEN,

>  			 ISHM_EXPTNAME_FORMAT,

>  			 odp_global_data.main_pid,

> @@ -483,6 +487,8 @@ static int create_file(int block_index, huge_flag_t huge, uint64_t len,

>  		}

>  	} else {

>  		new_block->exptname[0] = 0;

> +		/* remove the file from the filesystem, keeping its fd open */

> +		unlink(filename);

>  	}

>  

>  	return fd;

> @@ -814,6 +820,9 @@ int _odp_ishm_reserve(const char *name, uint64_t size, int fd,

>  			return -1;

>  		}

>  		new_block->huge = EXTERNAL;

> +		new_block->external_fd = 1;

> +	} else {

> +		new_block->external_fd = 0;

>  	}

>  

>  	/* Otherwise, Try first huge pages when possible and needed: */

> @@ -865,8 +874,9 @@ int _odp_ishm_reserve(const char *name, uint64_t size, int fd,

>  

>  	/* if neither huge pages or normal pages works, we cannot proceed: */

>  	if ((fd < 0) || (addr == NULL) || (len == 0)) {

> -		if ((new_block->filename[0]) && (fd >= 0))

> +		if ((!new_block->external_fd) && (fd >= 0))

>  			close(fd);

> +		delete_file(new_block);

>  		odp_spinlock_unlock(&ishm_tbl->lock);

>  		ODP_ERR("_ishm_reserve failed.\n");

>  		return -1;

>
diff mbox

Patch

diff --git a/platform/linux-generic/_ishm.c b/platform/linux-generic/_ishm.c
index 449e357..84d5225 100644
--- a/platform/linux-generic/_ishm.c
+++ b/platform/linux-generic/_ishm.c
@@ -71,6 +71,7 @@ 
 #include <sys/types.h>
 #include <inttypes.h>
 #include <sys/wait.h>
+#include <libgen.h>
 
 /*
  * Maximum number of internal shared memory blocks.
@@ -159,6 +160,7 @@  typedef struct ishm_block {
 	char exptname[ISHM_FILENAME_MAXLEN]; /* name of the export file     */
 	uint32_t user_flags;     /* any flags the user want to remember.    */
 	uint32_t flags;          /* block creation flags.                   */
+	uint32_t external_fd;    /* block FD was externaly provided         */
 	uint64_t user_len;	 /* length, as requested at reserve time.   */
 	void *start;		 /* only valid if _ODP_ISHM_SINGLE_VA is set*/
 	uint64_t len;		 /* length. multiple of page size. 0 if free*/
@@ -452,15 +454,17 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 		ODP_ERR("ftruncate failed: fd=%d, err=%s.\n",
 			fd, strerror(errno));
 		close(fd);
+		unlink(filename);
 		return -1;
 	}
 
-	strncpy(new_block->filename, filename, ISHM_FILENAME_MAXLEN - 1);
 
 	/* if _ODP_ISHM_EXPORT is set, create a description file for
 	 * external ref:
 	 */
 	if (flags & _ODP_ISHM_EXPORT) {
+		strncpy(new_block->filename, filename,
+			ISHM_FILENAME_MAXLEN - 1);
 		snprintf(new_block->exptname, ISHM_FILENAME_MAXLEN,
 			 ISHM_EXPTNAME_FORMAT,
 			 odp_global_data.main_pid,
@@ -483,6 +487,8 @@  static int create_file(int block_index, huge_flag_t huge, uint64_t len,
 		}
 	} else {
 		new_block->exptname[0] = 0;
+		/* remove the file from the filesystem, keeping its fd open */
+		unlink(filename);
 	}
 
 	return fd;
@@ -814,6 +820,9 @@  int _odp_ishm_reserve(const char *name, uint64_t size, int fd,
 			return -1;
 		}
 		new_block->huge = EXTERNAL;
+		new_block->external_fd = 1;
+	} else {
+		new_block->external_fd = 0;
 	}
 
 	/* Otherwise, Try first huge pages when possible and needed: */
@@ -865,8 +874,9 @@  int _odp_ishm_reserve(const char *name, uint64_t size, int fd,
 
 	/* if neither huge pages or normal pages works, we cannot proceed: */
 	if ((fd < 0) || (addr == NULL) || (len == 0)) {
-		if ((new_block->filename[0]) && (fd >= 0))
+		if ((!new_block->external_fd) && (fd >= 0))
 			close(fd);
+		delete_file(new_block);
 		odp_spinlock_unlock(&ishm_tbl->lock);
 		ODP_ERR("_ishm_reserve failed.\n");
 		return -1;