[sheepdog] [PATCH 1/3] use fcntl to set FD_CLOSEXEC instead of type "e" in popen()

Robin Dong robin.k.dong at gmail.com
Thu Aug 22 11:51:01 CEST 2013


From: Robin Dong <sanbai at taobao.com>

In glibc < 2.9 environment (rhel5), it dose not support type "e" in popen(), so
we should replace it by calling fcntl(xxx, FD_CLOSEXEC).

If pass NULL into pclose(), it would make program coredump, therefore add check
for argument.

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 sheepfs/core.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/sheepfs/core.c b/sheepfs/core.c
index fecfb98..71253aa 100644
--- a/sheepfs/core.c
+++ b/sheepfs/core.c
@@ -388,13 +388,18 @@ int main(int argc, char **argv)
 struct strbuf *sheepfs_run_cmd(const char *command)
 {
 	struct strbuf *buf = xmalloc(sizeof(*buf));
-	FILE *f = popen(command, "re");
+	FILE *f = popen(command, "r");
 
 	if (!f) {
 		sheepfs_pr("popen failed\n");
 		goto err;
 	}
 
+	if (fcntl(fileno(f), F_SETFD, FD_CLOEXEC) < 0) {
+		sheepfs_pr("fcntl failed\n");
+		goto err;
+	}
+
 	strbuf_init(buf, 4096);
 
 	while (!feof(f))
@@ -404,7 +409,8 @@ struct strbuf *sheepfs_run_cmd(const char *command)
 	return buf;
 err:
 	strbuf_release(buf);
-	pclose(f);
+	if (f)
+		pclose(f);
 	free(buf);
 	return NULL;
 }
-- 
1.7.3.2




More information about the sheepdog mailing list