[LTP] [PATCH v4 1/5] Add SAFE_SYMLINKAT macro

Andrea Cervesato
2024-08-01T14:30:20Z

From: Andrea Cervesato <andrea.cervesato@suse.com>

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
include/safe_macros_fn.h | 4 ++++
include/tst_safe_macros.h | 3 +++
lib/safe_macros.c | 20 ++++++++++++++++++++
3 files changed, 27 insertions(+)

diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h
index d256091b7..6d9e72e4f 100644
--- a/include/safe_macros_fn.h
+++ b/include/safe_macros_fn.h
@@ -122,6 +122,10 @@ int safe_symlink(const char *file, const int lineno,
void (cleanup_fn)(void), const char *oldpath,
const char *newpath);

+int safe_symlinkat(const char *file, const int lineno,
+ void (cleanup_fn)(void), const char *oldpath,
+ const int newdirfd, const char *newpath);
+
ssize_t safe_write(const char *file, const int lineno,
void (cleanup_fn)(void), enum safe_write_opts len_strict,
int fildes, const void *buf, size_t nbyte);
diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 92b9bc119..47a6f99df 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -191,6 +191,9 @@ int safe_getgroups(const char *file, const int lineno, int size, gid_t list[]);
#define SAFE_SYMLINK(oldpath, newpath) \
safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))

+#define SAFE_SYMLINKAT(oldpath, newdirfd, newpath) \
+ safe_symlinkat(__FILE__, __LINE__, NULL, (oldpath), (newdirfd), (newpath))
+
#define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))

diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 109268587..f91e77022 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -527,6 +527,26 @@ int safe_symlink(const char *file, const int lineno,
return rval;
}

+int safe_symlinkat(const char *file, const int lineno,
+ void (cleanup_fn)(void), const char *oldpath,
+ const int newdirfd, const char *newpath)
+{
+ int rval;
+
+ rval = symlinkat(oldpath, newdirfd, newpath);
+
+ if (rval == -1) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+ "symlinkat(%s,%d,%s) failed", oldpath, newdirfd, newpath);
+ } else if (rval) {
+ tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
+ "Invalid symlinkat(%s,%d,%s) return value %d", oldpath,
+ newdirfd, newpath, rval);
+ }
+
+ return rval;
+}
+
ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
enum safe_write_opts len_strict, int fildes, const void *buf,
size_t nbyte)

--
2.43.0