[LTP] [PATCH 1/2] Add SAFE_ACCESS macro

Dai Shili daisl.fnst@fujitsu.com
Mon Mar 14 18:28:18 CET 2022


Signed-off-by: Dai Shili <daisl.fnst@fujitsu.com>
---
 include/old/safe_macros.h |  3 +++
 include/safe_macros_fn.h  |  3 +++
 include/tst_safe_macros.h |  3 +++
 lib/safe_macros.c         | 20 ++++++++++++++++++++
 4 files changed, 29 insertions(+)

diff --git a/include/old/safe_macros.h b/include/old/safe_macros.h
index fb1d7a1..1d5de49 100644
--- a/include/old/safe_macros.h
+++ b/include/old/safe_macros.h
@@ -20,6 +20,9 @@
 #include "old_safe_stdio.h"
 #include "old_safe_net.h"
 
+#define SAFE_ACCESS(cleanup_fn, pathname, mode)	\
+	safe_access(__FILE__, __LINE__, cleanup_fn, (pathname), (mode))
+
 #define SAFE_BASENAME(cleanup_fn, path)	\
 	safe_basename(__FILE__, __LINE__, (cleanup_fn), (path))
 
diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h
index 3df9528..2c8eb50 100644
--- a/include/safe_macros_fn.h
+++ b/include/safe_macros_fn.h
@@ -24,6 +24,9 @@
 #include <unistd.h>
 #include <dirent.h>
 
+int safe_access(const char *file, const int lineno,
+		void (*cleanup_fn)(void), const char *pathname, int mode);
+
 char* safe_basename(const char *file, const int lineno,
                     void (*cleanup_fn)(void), char *path);
 
diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 9a543e4..d8e56f5 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -24,6 +24,9 @@
 #include "safe_macros_fn.h"
 #include "tst_cmd.h"
 
+#define SAFE_ACCESS(pathname, mode) \
+	safe_access(__FILE__, __LINE__, NULL, (pathname), (mode))
+
 #define SAFE_BASENAME(path) \
 	safe_basename(__FILE__, __LINE__, NULL, (path))
 
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index a5b6bc5..006e583 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -24,6 +24,26 @@
 #include "test.h"
 #include "safe_macros.h"
 
+int
+safe_access(const char *file, const int lineno, void (*cleanup_fn) (void),
+	   const char *pathname, int mode)
+{
+	int rval;
+
+	rval = access(pathname, mode);
+
+	if (rval == -1) {
+		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+			"access(%s,%d) failed", pathname, mode);
+	} else if (rval) {
+		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+			"Invalid access(%s,%d) return value %d", pathname,
+			mode, rval);
+	}
+
+	return rval;
+}
+
 char *safe_basename(const char *file, const int lineno,
 		    void (*cleanup_fn) (void), char *path)
 {
-- 
1.8.3.1



More information about the ltp mailing list