1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (c) 2010-2017 Linux Test Project
3 */
4
5 #ifndef TST_SAFE_PRW_H__
6 #define TST_SAFE_PRW_H__
7
safe_pread(const char * file,const int lineno,char len_strict,int fildes,void * buf,size_t nbyte,off_t offset)8 static inline ssize_t safe_pread(const char *file, const int lineno,
9 char len_strict, int fildes, void *buf, size_t nbyte,
10 off_t offset)
11 {
12 ssize_t rval;
13
14 rval = pread(fildes, buf, nbyte, offset);
15
16 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
17 tst_brk_(file, lineno, TBROK | TERRNO,
18 "pread(%d,%p,%zu,%lld) failed",
19 fildes, buf, nbyte, (long long)offset);
20 } else if (rval < 0) {
21 tst_brk_(file, lineno, TBROK | TERRNO,
22 "Invalid pread(%d,%p,%zu,%lld) return value %zd",
23 fildes, buf, nbyte, (long long)offset, rval);
24 }
25
26 return rval;
27 }
28 #define SAFE_PREAD(len_strict, fildes, buf, nbyte, offset) \
29 safe_pread(__FILE__, __LINE__, (len_strict), (fildes), \
30 (buf), (nbyte), (offset))
31
safe_pwrite(const char * file,const int lineno,char len_strict,int fildes,const void * buf,size_t nbyte,off_t offset)32 static inline ssize_t safe_pwrite(const char *file, const int lineno,
33 char len_strict, int fildes, const void *buf, size_t nbyte,
34 off_t offset)
35 {
36 ssize_t rval;
37
38 rval = pwrite(fildes, buf, nbyte, offset);
39 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
40 tst_brk_(file, lineno, TBROK | TERRNO,
41 "pwrite(%d,%p,%zu,%lld) failed",
42 fildes, buf, nbyte, (long long)offset);
43 } else if (rval < 0) {
44 tst_brk_(file, lineno, TBROK | TERRNO,
45 "Invalid pwrite(%d,%p,%zu,%lld) return value %zd",
46 fildes, buf, nbyte, (long long)offset, rval);
47 }
48
49 return rval;
50 }
51 #define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \
52 safe_pwrite(__FILE__, __LINE__, (len_strict), (fildes), \
53 (buf), (nbyte), (offset))
54
55 #endif /* SAFE_PRW_H__ */
56