Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright (c) 2010-2017 Linux Test Project
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #ifndef TST_SAFE_PRW_H__
     19 #define TST_SAFE_PRW_H__
     20 
     21 static inline ssize_t safe_pread(const char *file, const int lineno,
     22 		char len_strict, int fildes, void *buf, size_t nbyte,
     23 		off_t offset)
     24 {
     25 	ssize_t rval;
     26 
     27 	rval = pread(fildes, buf, nbyte, offset);
     28 
     29 	if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
     30 		tst_brk_(file, lineno, TBROK | TERRNO,
     31 			 "pread(%d,%p,%zu,%lld) failed",
     32 			 fildes, buf, nbyte, (long long)offset);
     33 	}
     34 
     35 	return rval;
     36 }
     37 #define SAFE_PREAD(len_strict, fildes, buf, nbyte, offset) \
     38 	safe_pread(__FILE__, __LINE__, (len_strict), (fildes), \
     39 	           (buf), (nbyte), (offset))
     40 
     41 static inline ssize_t safe_pwrite(const char *file, const int lineno,
     42 		char len_strict, int fildes, const void *buf, size_t nbyte,
     43 		off_t offset)
     44 {
     45 	ssize_t rval;
     46 
     47 	rval = pwrite(fildes, buf, nbyte, offset);
     48 	if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
     49 		tst_brk_(file, lineno, TBROK | TERRNO,
     50 			 "pwrite(%d,%p,%zu,%lld) failed",
     51 			 fildes, buf, nbyte, (long long)offset);
     52 	}
     53 
     54 	return rval;
     55 }
     56 #define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \
     57 	safe_pwrite(__FILE__, __LINE__, (len_strict), (fildes), \
     58 	            (buf), (nbyte), (offset))
     59 
     60 #endif /* SAFE_PRW_H__ */
     61