diff mbox series

[2/2] include: Move inline functions to special header

Message ID 20240318162539.407214-2-pvorel@suse.cz
State Accepted
Headers show
Series [1/2] tst_safe_macros: Move implementations into C file, rename | expand

Commit Message

Petr Vorel March 18, 2024, 4:25 p.m. UTC
9120d8a22 ("safe_macros: turn functions with off_t parameter into static
inline") changed some functions to inline because they depend on
-D_FILE_OFFSET_BITS=64. Separate them into it's own header, because
normal functions should be in C files.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 include/tst_safe_macros.h        | 213 +---------------------------
 include/tst_safe_macros_inline.h | 229 +++++++++++++++++++++++++++++++
 2 files changed, 231 insertions(+), 211 deletions(-)
 create mode 100644 include/tst_safe_macros_inline.h

Comments

Jan Stancek March 19, 2024, 6:32 a.m. UTC | #1
On Mon, Mar 18, 2024 at 5:26 PM Petr Vorel <pvorel@suse.cz> wrote:
>
> 9120d8a22 ("safe_macros: turn functions with off_t parameter into static
> inline") changed some functions to inline because they depend on
> -D_FILE_OFFSET_BITS=64. Separate them into it's own header, because
> normal functions should be in C files.
>
> Signed-off-by: Petr Vorel <pvorel@suse.cz>

I'm for separating these too.

Acked-by: Jan Stancek <jstancek@redhat.com>
Cyril Hrubis March 26, 2024, 1:33 p.m. UTC | #2
Hi!
Good idea:

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Petr Vorel March 26, 2024, 2:28 p.m. UTC | #3
Hi Jan, Cyril,

thanks a lot, merged!

Kind regards,
Petr
diff mbox series

Patch

diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 1c2decadd..cfd1d89ad 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -25,6 +25,7 @@ 
 #include "safe_stdio_fn.h"
 #include "safe_macros_fn.h"
 #include "tst_cmd.h"
+#include "tst_safe_macros_inline.h"
 
 int safe_access(const char *filename, const int lineno, const char *pathname,
 		   int mode);
@@ -299,216 +300,6 @@  int safe_mprotect(const char *file, const int lineno,
 #define SAFE_MPROTECT(addr, len, prot) \
 	safe_mprotect(__FILE__, __LINE__, (addr), (len), (prot))
 
-/*
- * Following functions are inline because the behaviour may depend on
- * -D_FILE_OFFSET_BITS=64 compile flag.
- */
-
-static inline int safe_ftruncate(const char *file, const int lineno,
-                                 int fd, off_t length)
-{
-	int rval;
-
-	rval = ftruncate(fd, length);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"ftruncate(%d,%ld) failed", fd, (long)length);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid ftruncate(%d,%ld) return value %d", fd,
-			(long)length, rval);
-	}
-
-	return rval;
-}
-#define SAFE_FTRUNCATE(fd, length) \
-	safe_ftruncate(__FILE__, __LINE__, (fd), (length))
-
-static inline int safe_posix_fadvise(const char *file, const int lineno,
-                                int fd, off_t offset, off_t len, int advice)
-{
-	int rval;
-
-	rval = posix_fadvise(fd, offset, len, advice);
-
-	if (rval)
-		tst_brk_(file, lineno, TBROK,
-			"posix_fadvise(%d,%ld,%ld,%d) failed: %s",
-			fd, (long)offset, (long)len, advice, tst_strerrno(rval));
-
-	return rval;
-}
-#define SAFE_POSIX_FADVISE(fd, offset, len, advice) \
-	safe_posix_fadvise(__FILE__, __LINE__, (fd), (offset), (len), (advice))
-
-static inline int safe_truncate(const char *file, const int lineno,
-                                const char *path, off_t length)
-{
-	int rval;
-
-	rval = truncate(path, length);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"truncate(%s,%ld) failed", path, (long)length);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid truncate(%s,%ld) return value %d", path,
-			(long)length, rval);
-	}
-
-	return rval;
-}
-#define SAFE_TRUNCATE(path, length) \
-	safe_truncate(__FILE__, __LINE__, (path), (length))
-
-static inline int safe_stat(const char *file, const int lineno,
-                            const char *path, struct stat *buf)
-{
-	int rval;
-
-	rval = stat(path, buf);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"stat(%s,%p) failed", path, buf);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid stat(%s,%p) return value %d", path, buf,
-			rval);
-	}
-
-	return rval;
-}
-#define SAFE_STAT(path, buf) \
-	safe_stat(__FILE__, __LINE__, (path), (buf))
-
-static inline int safe_fstat(const char *file, const int lineno,
-                             int fd, struct stat *buf)
-{
-	int rval;
-
-	rval = fstat(fd, buf);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"fstat(%d,%p) failed", fd, buf);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid fstat(%d,%p) return value %d", fd, buf, rval);
-	}
-
-	return rval;
-}
-#define SAFE_FSTAT(fd, buf) \
-	safe_fstat(__FILE__, __LINE__, (fd), (buf))
-
-static inline int safe_lstat(const char *file, const int lineno,
-	const char *path, struct stat *buf)
-{
-	int rval;
-
-	rval = lstat(path, buf);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"lstat(%s,%p) failed", path, buf);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid lstat(%s,%p) return value %d", path, buf,
-			rval);
-	}
-
-	return rval;
-}
-#define SAFE_LSTAT(path, buf) \
-	safe_lstat(__FILE__, __LINE__, (path), (buf))
-
-static inline int safe_statfs(const char *file, const int lineno,
-                              const char *path, struct statfs *buf)
-{
-	int rval;
-
-	rval = statfs(path, buf);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"statfs(%s,%p) failed", path, buf);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid statfs(%s,%p) return value %d", path, buf,
-			rval);
-	}
-
-	return rval;
-}
-#define SAFE_STATFS(path, buf) \
-	safe_statfs(__FILE__, __LINE__, (path), (buf))
-
-static inline off_t safe_lseek(const char *file, const int lineno,
-                               int fd, off_t offset, int whence)
-{
-	off_t rval;
-
-	rval = lseek(fd, offset, whence);
-
-	if (rval == (off_t) -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"lseek(%d,%ld,%d) failed", fd, (long)offset, whence);
-	} else if (rval < 0) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid lseek(%d,%ld,%d) return value %ld", fd,
-			(long)offset, whence, (long)rval);
-	}
-
-	return rval;
-}
-#define SAFE_LSEEK(fd, offset, whence) \
-	safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
-
-static inline int safe_getrlimit(const char *file, const int lineno,
-                                 int resource, struct rlimit *rlim)
-{
-	int rval;
-
-	rval = getrlimit(resource, rlim);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"getrlimit(%d,%p) failed", resource, rlim);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid getrlimit(%d,%p) return value %d", resource,
-			rlim, rval);
-	}
-
-	return rval;
-}
-#define SAFE_GETRLIMIT(resource, rlim) \
-	safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
-
-static inline int safe_setrlimit(const char *file, const int lineno,
-                                 int resource, const struct rlimit *rlim)
-{
-	int rval;
-
-	rval = setrlimit(resource, rlim);
-
-	if (rval == -1) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"setrlimit(%d,%p) failed", resource, rlim);
-	} else if (rval) {
-		tst_brk_(file, lineno, TBROK | TERRNO,
-			"Invalid setrlimit(%d,%p) return value %d", resource,
-			rlim, rval);
-	}
-
-	return rval;
-}
-#define SAFE_SETRLIMIT(resource, rlim) \
-	safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
-
 typedef void (*sighandler_t)(int);
 sighandler_t safe_signal(const char *file, const int lineno,
 	int signum, sighandler_t handler);
@@ -702,4 +493,4 @@  int safe_sysinfo(const char *file, const int lineno, struct sysinfo *info);
 
 void safe_print_file(const char *file, const int lineno, char *path);
 
-#endif /* SAFE_MACROS_H__ */
+#endif /* TST_SAFE_MACROS_H__ */
diff --git a/include/tst_safe_macros_inline.h b/include/tst_safe_macros_inline.h
new file mode 100644
index 000000000..c497f6059
--- /dev/null
+++ b/include/tst_safe_macros_inline.h
@@ -0,0 +1,229 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright (c) 2010-2024 Linux Test Project
+ * Copyright (c) 2011-2015 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+#ifndef TST_SAFE_MACROS_INLINE_H__
+#define TST_SAFE_MACROS_INLINE_H__
+
+/*
+ * Following functions are inline because the behaviour may depend on
+ * -D_FILE_OFFSET_BITS=64 compile flag.
+ *
+ * Do not add other functions here.
+ */
+
+static inline int safe_ftruncate(const char *file, const int lineno,
+	int fd, off_t length)
+{
+	int rval;
+
+	rval = ftruncate(fd, length);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"ftruncate(%d,%ld) failed", fd, (long)length);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid ftruncate(%d,%ld) return value %d", fd,
+			(long)length, rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_FTRUNCATE(fd, length) \
+	safe_ftruncate(__FILE__, __LINE__, (fd), (length))
+
+static inline int safe_posix_fadvise(const char *file, const int lineno,
+	int fd, off_t offset, off_t len, int advice)
+{
+	int rval;
+
+	rval = posix_fadvise(fd, offset, len, advice);
+
+	if (rval)
+		tst_brk_(file, lineno, TBROK,
+			"posix_fadvise(%d,%ld,%ld,%d) failed: %s",
+			fd, (long)offset, (long)len, advice, tst_strerrno(rval));
+
+	return rval;
+}
+
+#define SAFE_POSIX_FADVISE(fd, offset, len, advice) \
+	safe_posix_fadvise(__FILE__, __LINE__, (fd), (offset), (len), (advice))
+
+static inline int safe_truncate(const char *file, const int lineno,
+	const char *path, off_t length)
+{
+	int rval;
+
+	rval = truncate(path, length);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"truncate(%s,%ld) failed", path, (long)length);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid truncate(%s,%ld) return value %d", path,
+			(long)length, rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_TRUNCATE(path, length) \
+	safe_truncate(__FILE__, __LINE__, (path), (length))
+
+static inline int safe_stat(const char *file, const int lineno,
+	const char *path, struct stat *buf)
+{
+	int rval;
+
+	rval = stat(path, buf);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"stat(%s,%p) failed", path, buf);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid stat(%s,%p) return value %d", path, buf,
+			rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_STAT(path, buf) \
+	safe_stat(__FILE__, __LINE__, (path), (buf))
+
+static inline int safe_fstat(const char *file, const int lineno,
+	int fd, struct stat *buf)
+{
+	int rval;
+
+	rval = fstat(fd, buf);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"fstat(%d,%p) failed", fd, buf);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid fstat(%d,%p) return value %d", fd, buf, rval);
+	}
+
+	return rval;
+}
+#define SAFE_FSTAT(fd, buf) \
+	safe_fstat(__FILE__, __LINE__, (fd), (buf))
+
+static inline int safe_lstat(const char *file, const int lineno,
+	const char *path, struct stat *buf)
+{
+	int rval;
+
+	rval = lstat(path, buf);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"lstat(%s,%p) failed", path, buf);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid lstat(%s,%p) return value %d", path, buf,
+			rval);
+	}
+
+	return rval;
+}
+#define SAFE_LSTAT(path, buf) \
+	safe_lstat(__FILE__, __LINE__, (path), (buf))
+
+static inline int safe_statfs(const char *file, const int lineno,
+	const char *path, struct statfs *buf)
+{
+	int rval;
+
+	rval = statfs(path, buf);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"statfs(%s,%p) failed", path, buf);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid statfs(%s,%p) return value %d", path, buf,
+			rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_STATFS(path, buf) \
+	safe_statfs(__FILE__, __LINE__, (path), (buf))
+
+static inline off_t safe_lseek(const char *file, const int lineno,
+	int fd, off_t offset, int whence)
+{
+	off_t rval;
+
+	rval = lseek(fd, offset, whence);
+
+	if (rval == (off_t) -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"lseek(%d,%ld,%d) failed", fd, (long)offset, whence);
+	} else if (rval < 0) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid lseek(%d,%ld,%d) return value %ld", fd,
+			(long)offset, whence, (long)rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_LSEEK(fd, offset, whence) \
+	safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
+
+static inline int safe_getrlimit(const char *file, const int lineno,
+	int resource, struct rlimit *rlim)
+{
+	int rval;
+
+	rval = getrlimit(resource, rlim);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"getrlimit(%d,%p) failed", resource, rlim);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid getrlimit(%d,%p) return value %d", resource,
+			rlim, rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_GETRLIMIT(resource, rlim) \
+	safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
+
+static inline int safe_setrlimit(const char *file, const int lineno,
+	int resource, const struct rlimit *rlim)
+{
+	int rval;
+
+	rval = setrlimit(resource, rlim);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"setrlimit(%d,%p) failed", resource, rlim);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid setrlimit(%d,%p) return value %d", resource,
+			rlim, rval);
+	}
+
+	return rval;
+}
+
+#define SAFE_SETRLIMIT(resource, rlim) \
+	safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
+
+#endif /* TST_SAFE_MACROS_INLINE_H__ */