[LTP] [PATCH 1/4] lib: Add SAFE_LCHOWN

Ricardo B. Marlière rbm@suse.com
Wed Jul 2 12:25:41 CEST 2025


From: Ricardo B. Marlière <rbm@suse.com>

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
 include/safe_macros_fn.h  |  3 +++
 include/tst_safe_macros.h |  3 +++
 lib/safe_macros.c         | 19 +++++++++++++++++++
 3 files changed, 25 insertions(+)

diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h
index d256091b76ad10b06b29e3fd5fad8853faa14c08..07732957f4b7b037e3f19d2a5a9bde53e21fbd38 100644
--- a/include/safe_macros_fn.h
+++ b/include/safe_macros_fn.h
@@ -151,6 +151,9 @@ int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
 int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
                 int fd, uid_t owner, gid_t group);
 
+int safe_lchown(const char *file, const int lineno, void (cleanup_fn)(void),
+               const char *path, uid_t owner, gid_t group);
+
 pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
                 int *status);
 
diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 19504beb57ad379c835a13ed5d35fe06e42a6ed6..41d17b1ca4590458cf651a59a6e377c215727094 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -220,6 +220,9 @@ int safe_getgroups(const char *file, const int lineno, int size, gid_t list[]);
 #define SAFE_FCHOWN(fd, owner, group) \
 	safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
 
+#define SAFE_LCHOWN(path, owner, group) \
+	safe_lchown(__FILE__, __LINE__, NULL, (path), (owner), (group))
+
 #define SAFE_WAIT(status) \
 	safe_wait(__FILE__, __LINE__, NULL, (status))
 
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 6946cc5bcb94202a67fcb986a504e77ab0a27738..e971e5d90fc1625956940f7b501a8db44ef916dc 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -775,6 +775,25 @@ int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
 	return rval;
 }
 
+int safe_lchown(const char *file, const int lineno, void (cleanup_fn)(void),
+			const char *path, uid_t owner, gid_t group)
+{
+	int rval;
+
+	rval = lchown(path, owner, group);
+
+	if (rval == -1) {
+		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+			"lchown(%s,%d,%d) failed", path, owner, group);
+	} else if (rval) {
+		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+			"Invalid lchown(%s,%d,%d) return value %d", path,
+			owner, group, rval);
+	}
+
+	return rval;
+}
+
 pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
                 int *status)
 {

-- 
2.50.0



More information about the ltp mailing list