Skip to contentSkip to conversation

android: getcwd0[12]: use temp dir from $TMPDIR if present

PATCH New Sandeep Patil 2 participants 2 messages Aug 25, 2017 – Aug 28, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 android: getcwd0[12]: use temp dir from $TMPDIR if present

getcwd01/getcwd02 tests are broken on an Android system as the device doesn't have "/tmp". Instead of hardcoding the temporary directory to be "/tmp", look for it in 'TMPDIR' environment variable and fallback to "/tmp" if its not defined.

---
 testcases/kernel/syscalls/getcwd/getcwd01.c      |  4 ++-
 testcases/kernel/syscalls/getcwd/getcwd02.c      |  8 +++--
 testcases/kernel/syscalls/getcwd/getcwd_helper.h | 39 ++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 testcases/kernel/syscalls/getcwd/getcwd_helper.h
testcases/kernel/syscalls/getcwd/getcwd01.c +3 -1
index fdf306103..cde4b1641 100644
@@ -34,9 +34,11 @@
  */
 
 #include <errno.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <limits.h>
 #include "tst_test.h"
+#include "getcwd_helper.h"
 
 static char buffer[5];
 
@@ -76,7 +78,7 @@ static void verify_getcwd(unsigned int n)
 
 static void setup(void)
 {
-	SAFE_CHDIR("/tmp");
+	SAFE_CHDIR(get_tmpdir_path());
 }
 
 static struct tst_test test = {
testcases/kernel/syscalls/getcwd/getcwd02.c +5 -3
index 384157d42..11480f14f 100644
@@ -28,8 +28,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include "tst_test.h"
+#include "getcwd_helper.h"
 
-#define TMPDIR "/tmp"
 
 static char exp_buf[PATH_MAX];
 static char buffer[PATH_MAX];
@@ -71,9 +71,11 @@ end:
 
 static void setup(void)
 {
-	SAFE_CHDIR(TMPDIR);
+	const char *tmpdir = get_tmpdir_path();
 
-	if (!realpath(TMPDIR, exp_buf))
+	SAFE_CHDIR(tmpdir);
+
+	if (!realpath(tmpdir, exp_buf))
 		tst_brk(TBROK | TERRNO, "realpath() failed");
 
 	tst_res(TINFO, "Expected path '%s'", exp_buf);
testcases/kernel/syscalls/getcwd/getcwd_helper.h new file +39 -0
new file mode 100644
index 000000000..6a78ddbf5
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2017 Google Inc.
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GETCWD_HELPER_H
+#define GETCWD_HELPER_H
+
+#include <stdlib.h>
+
+#define GETCWD_TMPDIR_PATH	"/tmp"
+
+static const char *get_tmpdir_path(void)
+{
+	const char *tmpdir;
+
+	tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		return GETCWD_TMPDIR_PATH;
+
+	/* $TMPDIR must be absolute path.. */
+	if (tmpdir[0] != '/')
+		return GETCWD_TMPDIR_PATH;
+
+	return tmpdir;
+}
+
+#endif /* GETCWD_HELPER_H */
Signature · 1 line
2.14.1.342.g6490525c54-goog
#1 lore raw

PATCH 1/1 android: getcwd0[12]: use temp dir from $TMPDIR if present

Hi!

Quoted 30 lines

getcwd01/getcwd02 tests are broken on an Android system as the device doesn't have "/tmp". Instead of hardcoding the temporary directory to be "/tmp", look for it in 'TMPDIR' environment variable and fallback to "/tmp" if its not defined.

---
 testcases/kernel/syscalls/getcwd/getcwd01.c      |  4 ++-
 testcases/kernel/syscalls/getcwd/getcwd02.c      |  8 +++--
 testcases/kernel/syscalls/getcwd/getcwd_helper.h | 39 ++++++++++++++++++++++++
 3 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 testcases/kernel/syscalls/getcwd/getcwd_helper.h
testcases/kernel/syscalls/getcwd/getcwd01.c +2 -0
index fdf306103..cde4b1641 100644
@@ -34,9 +34,11 @@
  */
 
 #include <errno.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <limits.h>
 #include "tst_test.h"
+#include "getcwd_helper.h"
 
 static char buffer[5];
 

testcases/kernel/syscalls/getcwd/getcwd01.c static void verify_getcwd(unsigned int n) · line 82

Quoted 6 lines
testcases/kernel/syscalls/getcwd/getcwd01.c +1 -1
@@ -76,7 +78,7 @@ static void verify_getcwd(unsigned int n)
 
 static void setup(void)
 {
-	SAFE_CHDIR("/tmp");
+	SAFE_CHDIR(get_tmpdir_path());
 }

As far as I can tell we can simply remove the chdir() here. The only part of the test that may be afected by currect directory is the ERANGE test and even then we pass buffer size 1 there, so even if CWD was "/" we would have to write two characters to the buffer (including the terminating zero byte) so there is no chance that the test will fail no matter what CWD is set to.

Quoted 59 lines
testcases/kernel/syscalls/getcwd/getcwd01.c +0 -0
 static struct tst_test test = {
testcases/kernel/syscalls/getcwd/getcwd02.c +5 -3
index 384157d42..11480f14f 100644
@@ -28,8 +28,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include "tst_test.h"
+#include "getcwd_helper.h"
 
-#define TMPDIR "/tmp"
 
 static char exp_buf[PATH_MAX];
 static char buffer[PATH_MAX];
@@ -71,9 +71,11 @@ end:
 
 static void setup(void)
 {
-	SAFE_CHDIR(TMPDIR);
+	const char *tmpdir = get_tmpdir_path();
 
-	if (!realpath(TMPDIR, exp_buf))
+	SAFE_CHDIR(tmpdir);
+
+	if (!realpath(tmpdir, exp_buf))
 		tst_brk(TBROK | TERRNO, "realpath() failed");
 
 	tst_res(TINFO, "Expected path '%s'", exp_buf);
testcases/kernel/syscalls/getcwd/getcwd_helper.h new file +24 -0
new file mode 100644
index 000000000..6a78ddbf5
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2017 Google Inc.
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef GETCWD_HELPER_H
+#define GETCWD_HELPER_H
+
+#include <stdlib.h>
+
+#define GETCWD_TMPDIR_PATH	"/tmp"
+
+static const char *get_tmpdir_path(void)

testcases/kernel/syscalls/getcwd/getcwd_helper.h line 34

Quoted 10 lines
testcases/kernel/syscalls/getcwd/getcwd_helper.h new file +10 -0
+{
+	const char *tmpdir;
+
+	tmpdir = getenv("TMPDIR");
+	if (!tmpdir)
+		return GETCWD_TMPDIR_PATH;
+
+	/* $TMPDIR must be absolute path.. */
+	if (tmpdir[0] != '/')
+		return GETCWD_TMPDIR_PATH;

We may as well check that the "/tmp" exist here first, then fall back to $TMPDIR and finally call tst_brk() if $TMPDIR does not exist or if it's relative, which would be a bit more specific error message than chdir("/tmp") failed with ENOENT.

Quoted 7 lines
testcases/kernel/syscalls/getcwd/getcwd_helper.h new file +4 -0
+	return tmpdir;
+}
+
+#endif /* GETCWD_HELPER_H */
Signature · 1 line
2.14.1.342.g6490525c54-goog
Signature · 2 lines
Cyril Hrubis
[email protected]