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