Hi All,

Newer versions of gcc use mkstemps64 and mkstemps, leading to lots of /tmp/cc* 
files being created and not deleted.

This patch resolves the issue.

thanks,

Mike
>From 735433b176a3631686de2280cb8c3c9d13ab8d1c Mon Sep 17 00:00:00 2001
From: Mike McCormack <[email protected]>
Date: Mon, 29 Nov 2010 14:03:26 +0900
Subject: [PATCH 1/3] Add mkstemps64 and mkstemps

Change-Id: I0140cc2d00bafc8b5cd505d04e27242b944aced3
---
 configure.ac             |    2 +
 preload/generate.py      |    2 +
 preload/interface.master |    8 +++++++
 preload/tmpnamegates.c   |   52 +++++++++++++++++++++++++++++++++++----------
 4 files changed, 52 insertions(+), 12 deletions(-)

diff --git a/configure.ac b/configure.ac
index cc5efa7..745949c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -121,7 +121,9 @@ mknodat \
 mkfifo \
 mkfifoat \
 mkstemp \
+mkstemps \
 mkstemp64 \
+mkstemps64 \
 mktemp \
 nftw \
 nftw64 \
diff --git a/preload/generate.py b/preload/generate.py
index 2d3f3ce..53e524c 100644
--- a/preload/generate.py
+++ b/preload/generate.py
@@ -151,7 +151,9 @@ W('int mkfifoat(int dirfd, const char *pathname, mode_t mode)', map_at("dirfd","
 W('int mknod(const char *pathname, mode_t mode, dev_t dev)', b_map("pathname"))
 W('int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev)', map_at("dirfd","pathname"))
 W('int mkstemp(char *template)', b_map("template"))
+W('int mkstemps(char *template, int suffixlen)', b_map("template"))
 W('int mkstemp64(char *template)', b_map("template"))
+W('int mkstemps64(char *template, int suffixlen)', b_map("template"))
 W('int nftw(const char *dir, int (*fn)(const char *file, const struct stat *sb, int flag, struct FTW *s), int nopenfd, int flags)', b_map("dir"),argnames=['dir','fn','flag','s'])
 W('int nftw64(const char *dir, int (*fn)(const char *file, const struct stat64 *sb, int flag, struct FTW *s), int nopenfd, int flags)', b_map("dir"))
 W('DIR *opendir(const char *name)', b_map("name"))
diff --git a/preload/interface.master b/preload/interface.master
index 0451369..7d7507b 100644
--- a/preload/interface.master
+++ b/preload/interface.master
@@ -578,6 +578,14 @@ WRAP: int mkstemp64(char *template) : \
 	map(template) \
 	postprocess(template) \
 	fail_if_readonly(template,-1,EROFS)
+WRAP: int mkstemps(char *template, int suffixlen) : \
+	map(template) \
+	postprocess(template) \
+	fail_if_readonly(template,-1,EROFS)
+WRAP: int mkstemps64(char *template, int suffixlen) : \
+	map(template) \
+	postprocess(template) \
+	fail_if_readonly(template,-1,EROFS)
 
 -- tmpnam(), tempnam() and mktemp() do not create files, they create file names 
 -- that did not exist when the function was called. These need to do path
diff --git a/preload/tmpnamegates.c b/preload/tmpnamegates.c
index ffa75dc..4337c80 100644
--- a/preload/tmpnamegates.c
+++ b/preload/tmpnamegates.c
@@ -39,14 +39,26 @@
  * was modified by the real function) to callers buffer.
 */
 static void postprocess_tempname_template(const char *realfnname,
-	char *mapped__template, char *template)
+	char *mapped__template, char *template, int suffixlen)
 {
 	char *X_ptr;
 	int mapped_len = strlen(mapped__template);
+	int template_len = strlen(template);
 	int num_x = 0;
 
-	X_ptr = strrchr(template, 'X'); /* point to last 'X' */
-	if (!X_ptr) {
+	if (suffixlen >= template_len) {
+		SB_LOG(SB_LOGLEVEL_WARNING,
+			"%s: template length too long %d >= %d, ignoring",
+			realfnname, suffixlen, template_len);
+		return;
+	}
+
+	/* find X, working backwards along the template from the suffix */
+	X_ptr = template + template_len - suffixlen;
+	while (*X_ptr != 'X' && X_ptr > template)
+		X_ptr --;
+
+	if (*X_ptr != 'X') {
 		SB_LOG(SB_LOGLEVEL_WARNING,
 			"%s: orig.template did not contain X (%s,%s), won't "
 			"do anything", realfnname, template, mapped__template);
@@ -54,16 +66,18 @@ static void postprocess_tempname_template(const char *realfnname,
 	}
 
 	/* the last 'X' should be the last character in the template: */
-	if (X_ptr[1] != '\0') {
+	if (X_ptr[suffixlen + 1] != '\0') {
 		SB_LOG(SB_LOGLEVEL_WARNING,
 			"%s: unknown orig.template format (%s,%s), "
-			"won't do anything", 
+			"won't do anything",
 			realfnname, template, mapped__template);
 		return;
 	}
 
+	num_x = 1;
 	while ((X_ptr > template) && (X_ptr[-1] == 'X')) {
 		X_ptr--;
+		num_x ++;
 	}
 
 	/* now "X_ptr" points to the first 'X' to be modified.
@@ -71,9 +85,8 @@ static void postprocess_tempname_template(const char *realfnname,
 	 * However, some systems seem to allow varying number of X characters
 	 * (see the manual pages)
 	*/
-	num_x = strlen(X_ptr);
 
-	if(mapped_len < num_x) {
+	if (mapped_len < num_x) {
 		SB_LOG(SB_LOGLEVEL_WARNING,
 			"%s: mapped.template is too short (%s,%s), won't "
 			"do anything", realfnname, template, mapped__template);
@@ -81,7 +94,7 @@ static void postprocess_tempname_template(const char *realfnname,
 	}
 
 	/* now copy last characters from mapping result to caller's buffer*/
-	strncpy(X_ptr, mapped__template + (mapped_len-num_x), num_x);
+	memcpy(X_ptr, mapped__template + (mapped_len-num_x-suffixlen), num_x);
 
 	SB_LOG(SB_LOGLEVEL_DEBUG,
 		"%s: template set to (%s)", realfnname, template);
@@ -91,30 +104,45 @@ void mkstemp_postprocess_template(const char *realfnname,
 	int ret, mapping_results_t *res, char *template)
 {
 	(void)ret;
-	postprocess_tempname_template(realfnname, res->mres_result_path, template);
+	postprocess_tempname_template(realfnname, res->mres_result_path, template, 0);
 }
 
 void mkstemp64_postprocess_template(const char *realfnname,
 	int ret, mapping_results_t *res, char *template)
 {
 	(void)ret;
-	postprocess_tempname_template(realfnname, res->mres_result_path, template);
+	postprocess_tempname_template(realfnname, res->mres_result_path, template, 0);
 }
 
 void mkdtemp_postprocess_template(const char *realfnname,
 	char *ret, mapping_results_t *res, char *template)
 {
 	(void)ret;
-	postprocess_tempname_template(realfnname, res->mres_result_path, template);
+	postprocess_tempname_template(realfnname, res->mres_result_path, template, 0);
 }
 
 void mktemp_postprocess_template(const char *realfnname,
 	char *ret, mapping_results_t *res, char *template)
 {
 	(void)ret;
-	postprocess_tempname_template(realfnname, res->mres_result_path, template);
+	postprocess_tempname_template(realfnname, res->mres_result_path, template, 0);
+}
+
+void mkstemps_postprocess_template(const char *realfnname,
+	int ret, mapping_results_t *res, char *template, int suffixlen)
+{
+	(void)ret;
+	postprocess_tempname_template(realfnname, res->mres_result_path, template, suffixlen);
 }
 
+void mkstemps64_postprocess_template(const char *realfnname,
+	int ret, mapping_results_t *res, char *template, int suffixlen)
+{
+	(void)ret;
+	postprocess_tempname_template(realfnname, res->mres_result_path, template, suffixlen);
+}
+
+
 /* the real tmpnam() can not be used at all, because the generated name must
  * be mapped before the name can be tested and that won't happen inside libc.
  * Istead, we'll use mktemp()..
-- 
1.7.0.4

_______________________________________________
Scratchbox-devel mailing list
[email protected]
http://lists.scratchbox.org/cgi-bin/mailman/listinfo/scratchbox-devel

Reply via email to