1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Safe macros for commonly used syscalls to reduce code duplication in LTP
3*49cdfc7eSAndroid Build Coastguard Worker * testcases, and to ensure all errors are caught in said testcases as
4*49cdfc7eSAndroid Build Coastguard Worker * gracefully as possible.
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * Also satiates some versions of gcc/glibc when the warn_unused_result
7*49cdfc7eSAndroid Build Coastguard Worker * attribute is applied to the function call.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * Licensed under the GPLv2.
10*49cdfc7eSAndroid Build Coastguard Worker */
11*49cdfc7eSAndroid Build Coastguard Worker
12*49cdfc7eSAndroid Build Coastguard Worker #ifndef __TEST_H__
13*49cdfc7eSAndroid Build Coastguard Worker #error "you must include test.h before this file"
14*49cdfc7eSAndroid Build Coastguard Worker #else
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #ifndef __SAFE_MACROS_H__
17*49cdfc7eSAndroid Build Coastguard Worker #define __SAFE_MACROS_H__
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros_fn.h"
20*49cdfc7eSAndroid Build Coastguard Worker #include "old_safe_stdio.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "old_safe_net.h"
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_BASENAME(cleanup_fn, path) \
24*49cdfc7eSAndroid Build Coastguard Worker safe_basename(__FILE__, __LINE__, (cleanup_fn), (path))
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CHDIR(cleanup_fn, path) \
27*49cdfc7eSAndroid Build Coastguard Worker safe_chdir(__FILE__, __LINE__, (cleanup_fn), (path))
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CLOSE(cleanup_fn, fd) ({ \
30*49cdfc7eSAndroid Build Coastguard Worker int ret = safe_close(__FILE__, __LINE__, (cleanup_fn), (fd)); \
31*49cdfc7eSAndroid Build Coastguard Worker fd = -1; \
32*49cdfc7eSAndroid Build Coastguard Worker ret; \
33*49cdfc7eSAndroid Build Coastguard Worker })
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CREAT(cleanup_fn, pathname, mode) \
36*49cdfc7eSAndroid Build Coastguard Worker safe_creat(__FILE__, __LINE__, cleanup_fn, (pathname), (mode))
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_DIRNAME(cleanup_fn, path) \
39*49cdfc7eSAndroid Build Coastguard Worker safe_dirname(__FILE__, __LINE__, (cleanup_fn), (path))
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_GETCWD(cleanup_fn, buf, size) \
42*49cdfc7eSAndroid Build Coastguard Worker safe_getcwd(__FILE__, __LINE__, (cleanup_fn), (buf), (size))
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_GETPWNAM(cleanup_fn, name) \
45*49cdfc7eSAndroid Build Coastguard Worker safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name))
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \
48*49cdfc7eSAndroid Build Coastguard Worker safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage))
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MALLOC(cleanup_fn, size) \
51*49cdfc7eSAndroid Build Coastguard Worker safe_malloc(__FILE__, __LINE__, (cleanup_fn), (size))
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MKDIR(cleanup_fn, pathname, mode) \
54*49cdfc7eSAndroid Build Coastguard Worker safe_mkdir(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode))
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_RMDIR(cleanup_fn, pathname) \
57*49cdfc7eSAndroid Build Coastguard Worker safe_rmdir(__FILE__, __LINE__, (cleanup_fn), (pathname))
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MUNMAP(cleanup_fn, addr, length) \
60*49cdfc7eSAndroid Build Coastguard Worker safe_munmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length))
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_OPEN(cleanup_fn, pathname, oflags, ...) \
63*49cdfc7eSAndroid Build Coastguard Worker safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \
64*49cdfc7eSAndroid Build Coastguard Worker ##__VA_ARGS__)
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_PIPE(cleanup_fn, fildes) \
67*49cdfc7eSAndroid Build Coastguard Worker safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes))
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_READ(cleanup_fn, len_strict, fildes, buf, nbyte) \
70*49cdfc7eSAndroid Build Coastguard Worker safe_read(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
71*49cdfc7eSAndroid Build Coastguard Worker (buf), (nbyte))
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SETEGID(cleanup_fn, egid) \
74*49cdfc7eSAndroid Build Coastguard Worker safe_setegid(__FILE__, __LINE__, cleanup_fn, (egid))
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SETEUID(cleanup_fn, euid) \
77*49cdfc7eSAndroid Build Coastguard Worker safe_seteuid(__FILE__, __LINE__, cleanup_fn, (euid))
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SETGID(cleanup_fn, gid) \
80*49cdfc7eSAndroid Build Coastguard Worker safe_setgid(__FILE__, __LINE__, cleanup_fn, (gid))
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SETUID(cleanup_fn, uid) \
83*49cdfc7eSAndroid Build Coastguard Worker safe_setuid(__FILE__, __LINE__, cleanup_fn, (uid))
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_GETRESUID(cleanup_fn, ruid, euid, suid) \
86*49cdfc7eSAndroid Build Coastguard Worker safe_getresuid(__FILE__, __LINE__, cleanup_fn, (ruid), (euid), (suid))
87*49cdfc7eSAndroid Build Coastguard Worker
88*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_GETRESGID(cleanup_fn, rgid, egid, sgid) \
89*49cdfc7eSAndroid Build Coastguard Worker safe_getresgid(__FILE__, __LINE__, cleanup_fn, (rgid), (egid), (sgid))
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_UNLINK(cleanup_fn, pathname) \
92*49cdfc7eSAndroid Build Coastguard Worker safe_unlink(__FILE__, __LINE__, cleanup_fn, (pathname))
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_LINK(cleanup_fn, oldpath, newpath) \
95*49cdfc7eSAndroid Build Coastguard Worker safe_link(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_LINKAT(cleanup_fn, olddirfd, oldpath, newdirfd, newpath, flags) \
98*49cdfc7eSAndroid Build Coastguard Worker safe_linkat(__FILE__, __LINE__, cleanup_fn, (olddirfd), (oldpath), \
99*49cdfc7eSAndroid Build Coastguard Worker (newdirfd), (newpath), (flags))
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_READLINK(cleanup_fn, path, buf, bufsize) \
102*49cdfc7eSAndroid Build Coastguard Worker safe_readlink(__FILE__, __LINE__, cleanup_fn, (path), (buf), (bufsize))
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SYMLINK(cleanup_fn, oldpath, newpath) \
105*49cdfc7eSAndroid Build Coastguard Worker safe_symlink(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_WRITE(cleanup_fn, len_strict, fildes, buf, nbyte) \
108*49cdfc7eSAndroid Build Coastguard Worker safe_write(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
109*49cdfc7eSAndroid Build Coastguard Worker (buf), (nbyte))
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_STRTOL(cleanup_fn, str, min, max) \
112*49cdfc7eSAndroid Build Coastguard Worker safe_strtol(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_STRTOUL(cleanup_fn, str, min, max) \
115*49cdfc7eSAndroid Build Coastguard Worker safe_strtoul(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SYSCONF(cleanup_fn, name) \
118*49cdfc7eSAndroid Build Coastguard Worker safe_sysconf(__FILE__, __LINE__, cleanup_fn, name)
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CHMOD(cleanup_fn, path, mode) \
121*49cdfc7eSAndroid Build Coastguard Worker safe_chmod(__FILE__, __LINE__, (cleanup_fn), (path), (mode))
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_FCHMOD(cleanup_fn, fd, mode) \
124*49cdfc7eSAndroid Build Coastguard Worker safe_fchmod(__FILE__, __LINE__, (cleanup_fn), (fd), (mode))
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CHOWN(cleanup_fn, path, owner, group) \
127*49cdfc7eSAndroid Build Coastguard Worker safe_chown(__FILE__, __LINE__, (cleanup_fn), (path), (owner), (group))
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_FCHOWN(cleanup_fn, fd, owner, group) \
130*49cdfc7eSAndroid Build Coastguard Worker safe_fchown(__FILE__, __LINE__, (cleanup_fn), (fd), (owner), (group))
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_WAIT(cleanup_fn, status) \
133*49cdfc7eSAndroid Build Coastguard Worker safe_wait(__FILE__, __LINE__, (cleanup_fn), (status))
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_WAITPID(cleanup_fn, pid, status, opts) \
136*49cdfc7eSAndroid Build Coastguard Worker safe_waitpid(__FILE__, __LINE__, (cleanup_fn), (pid), (status), (opts))
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_KILL(cleanup_fn, pid, sig) \
139*49cdfc7eSAndroid Build Coastguard Worker safe_kill(__FILE__, __LINE__, (cleanup_fn), (pid), (sig))
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MEMALIGN(cleanup_fn, alignment, size) \
142*49cdfc7eSAndroid Build Coastguard Worker safe_memalign(__FILE__, __LINE__, (cleanup_fn), (alignment), (size))
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MKFIFO(cleanup_fn, pathname, mode) \
145*49cdfc7eSAndroid Build Coastguard Worker safe_mkfifo(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode))
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_RENAME(cleanup_fn, oldpath, newpath) \
148*49cdfc7eSAndroid Build Coastguard Worker safe_rename(__FILE__, __LINE__, (cleanup_fn), (oldpath), (newpath))
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \
151*49cdfc7eSAndroid Build Coastguard Worker mountflags, data) \
152*49cdfc7eSAndroid Build Coastguard Worker safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \
153*49cdfc7eSAndroid Build Coastguard Worker (filesystemtype), (mountflags), (data))
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_UMOUNT(cleanup_fn, target) \
156*49cdfc7eSAndroid Build Coastguard Worker safe_umount(__FILE__, __LINE__, (cleanup_fn), (target))
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker /*
159*49cdfc7eSAndroid Build Coastguard Worker * following functions are inline because the behaviour may depend on
160*49cdfc7eSAndroid Build Coastguard Worker * -D_FILE_OFFSET_BITS=64 compile flag
161*49cdfc7eSAndroid Build Coastguard Worker */
162*49cdfc7eSAndroid Build Coastguard Worker
safe_mmap(const char * file,const int lineno,void (* cleanup_fn)(void),void * addr,size_t length,int prot,int flags,int fd,off_t offset)163*49cdfc7eSAndroid Build Coastguard Worker static inline void *safe_mmap(const char *file, const int lineno,
164*49cdfc7eSAndroid Build Coastguard Worker void (*cleanup_fn)(void), void *addr, size_t length,
165*49cdfc7eSAndroid Build Coastguard Worker int prot, int flags, int fd, off_t offset)
166*49cdfc7eSAndroid Build Coastguard Worker {
167*49cdfc7eSAndroid Build Coastguard Worker void *rval;
168*49cdfc7eSAndroid Build Coastguard Worker
169*49cdfc7eSAndroid Build Coastguard Worker rval = mmap(addr, length, prot, flags, fd, offset);
170*49cdfc7eSAndroid Build Coastguard Worker if (rval == MAP_FAILED) {
171*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
172*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed",
173*49cdfc7eSAndroid Build Coastguard Worker file, lineno, addr, length, prot, flags, fd,
174*49cdfc7eSAndroid Build Coastguard Worker (long) offset);
175*49cdfc7eSAndroid Build Coastguard Worker }
176*49cdfc7eSAndroid Build Coastguard Worker
177*49cdfc7eSAndroid Build Coastguard Worker return rval;
178*49cdfc7eSAndroid Build Coastguard Worker }
179*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_MMAP(cleanup_fn, addr, length, prot, flags, fd, offset) \
180*49cdfc7eSAndroid Build Coastguard Worker safe_mmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length), (prot), \
181*49cdfc7eSAndroid Build Coastguard Worker (flags), (fd), (offset))
182*49cdfc7eSAndroid Build Coastguard Worker
safe_ftruncate(const char * file,const int lineno,void (cleanup_fn)(void),int fd,off_t length)183*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_ftruncate(const char *file, const int lineno,
184*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn) (void), int fd, off_t length)
185*49cdfc7eSAndroid Build Coastguard Worker {
186*49cdfc7eSAndroid Build Coastguard Worker int rval;
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker rval = ftruncate(fd, length);
189*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
190*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
191*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: ftruncate(%d,%ld) failed",
192*49cdfc7eSAndroid Build Coastguard Worker file, lineno, fd, (long)length);
193*49cdfc7eSAndroid Build Coastguard Worker }
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker return rval;
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_FTRUNCATE(cleanup_fn, fd, length) \
198*49cdfc7eSAndroid Build Coastguard Worker safe_ftruncate(__FILE__, __LINE__, cleanup_fn, (fd), (length))
199*49cdfc7eSAndroid Build Coastguard Worker
safe_truncate(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,off_t length)200*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_truncate(const char *file, const int lineno,
201*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn) (void), const char *path, off_t length)
202*49cdfc7eSAndroid Build Coastguard Worker {
203*49cdfc7eSAndroid Build Coastguard Worker int rval;
204*49cdfc7eSAndroid Build Coastguard Worker
205*49cdfc7eSAndroid Build Coastguard Worker rval = truncate(path, length);
206*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
207*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
208*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: truncate(%s,%ld) failed",
209*49cdfc7eSAndroid Build Coastguard Worker file, lineno, path, (long)length);
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker
212*49cdfc7eSAndroid Build Coastguard Worker return rval;
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_TRUNCATE(cleanup_fn, path, length) \
215*49cdfc7eSAndroid Build Coastguard Worker safe_truncate(__FILE__, __LINE__, cleanup_fn, (path), (length))
216*49cdfc7eSAndroid Build Coastguard Worker
safe_stat(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,struct stat * buf)217*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_stat(const char *file, const int lineno,
218*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), const char *path, struct stat *buf)
219*49cdfc7eSAndroid Build Coastguard Worker {
220*49cdfc7eSAndroid Build Coastguard Worker int rval;
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker rval = stat(path, buf);
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
225*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
226*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: stat(%s,%p) failed", file, lineno, path, buf);
227*49cdfc7eSAndroid Build Coastguard Worker }
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker return rval;
230*49cdfc7eSAndroid Build Coastguard Worker }
231*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_STAT(cleanup_fn, path, buf) \
232*49cdfc7eSAndroid Build Coastguard Worker safe_stat(__FILE__, __LINE__, (cleanup_fn), (path), (buf))
233*49cdfc7eSAndroid Build Coastguard Worker
safe_fstat(const char * file,const int lineno,void (cleanup_fn)(void),int fd,struct stat * buf)234*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_fstat(const char *file, const int lineno,
235*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), int fd, struct stat *buf)
236*49cdfc7eSAndroid Build Coastguard Worker {
237*49cdfc7eSAndroid Build Coastguard Worker int rval;
238*49cdfc7eSAndroid Build Coastguard Worker
239*49cdfc7eSAndroid Build Coastguard Worker rval = fstat(fd, buf);
240*49cdfc7eSAndroid Build Coastguard Worker
241*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
242*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
243*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf);
244*49cdfc7eSAndroid Build Coastguard Worker }
245*49cdfc7eSAndroid Build Coastguard Worker
246*49cdfc7eSAndroid Build Coastguard Worker return rval;
247*49cdfc7eSAndroid Build Coastguard Worker }
248*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_FSTAT(cleanup_fn, fd, buf) \
249*49cdfc7eSAndroid Build Coastguard Worker safe_fstat(__FILE__, __LINE__, (cleanup_fn), (fd), (buf))
250*49cdfc7eSAndroid Build Coastguard Worker
safe_lstat(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,struct stat * buf)251*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_lstat(const char *file, const int lineno,
252*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), const char *path, struct stat *buf)
253*49cdfc7eSAndroid Build Coastguard Worker {
254*49cdfc7eSAndroid Build Coastguard Worker int rval;
255*49cdfc7eSAndroid Build Coastguard Worker
256*49cdfc7eSAndroid Build Coastguard Worker rval = lstat(path, buf);
257*49cdfc7eSAndroid Build Coastguard Worker
258*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
259*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
260*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf);
261*49cdfc7eSAndroid Build Coastguard Worker }
262*49cdfc7eSAndroid Build Coastguard Worker
263*49cdfc7eSAndroid Build Coastguard Worker return rval;
264*49cdfc7eSAndroid Build Coastguard Worker }
265*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_LSTAT(cleanup_fn, path, buf) \
266*49cdfc7eSAndroid Build Coastguard Worker safe_lstat(__FILE__, __LINE__, (cleanup_fn), (path), (buf))
267*49cdfc7eSAndroid Build Coastguard Worker
safe_lseek(const char * file,const int lineno,void (cleanup_fn)(void),int fd,off_t offset,int whence)268*49cdfc7eSAndroid Build Coastguard Worker static inline off_t safe_lseek(const char *file, const int lineno,
269*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), int fd, off_t offset, int whence)
270*49cdfc7eSAndroid Build Coastguard Worker {
271*49cdfc7eSAndroid Build Coastguard Worker off_t rval;
272*49cdfc7eSAndroid Build Coastguard Worker
273*49cdfc7eSAndroid Build Coastguard Worker rval = lseek(fd, offset, whence);
274*49cdfc7eSAndroid Build Coastguard Worker
275*49cdfc7eSAndroid Build Coastguard Worker if (rval == (off_t) -1) {
276*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
277*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: lseek(%d,%ld,%d) failed",
278*49cdfc7eSAndroid Build Coastguard Worker file, lineno, fd, (long)offset, whence);
279*49cdfc7eSAndroid Build Coastguard Worker }
280*49cdfc7eSAndroid Build Coastguard Worker
281*49cdfc7eSAndroid Build Coastguard Worker return rval;
282*49cdfc7eSAndroid Build Coastguard Worker }
283*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_LSEEK(cleanup_fn, fd, offset, whence) \
284*49cdfc7eSAndroid Build Coastguard Worker safe_lseek(__FILE__, __LINE__, cleanup_fn, (fd), (offset), (whence))
285*49cdfc7eSAndroid Build Coastguard Worker
safe_getrlimit(const char * file,const int lineno,void (cleanup_fn)(void),int resource,struct rlimit * rlim)286*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_getrlimit(const char *file, const int lineno,
287*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), int resource, struct rlimit *rlim)
288*49cdfc7eSAndroid Build Coastguard Worker {
289*49cdfc7eSAndroid Build Coastguard Worker int rval;
290*49cdfc7eSAndroid Build Coastguard Worker
291*49cdfc7eSAndroid Build Coastguard Worker rval = getrlimit(resource, rlim);
292*49cdfc7eSAndroid Build Coastguard Worker
293*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
294*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
295*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: getrlimit(%d,%p) failed",
296*49cdfc7eSAndroid Build Coastguard Worker file, lineno, resource, rlim);
297*49cdfc7eSAndroid Build Coastguard Worker }
298*49cdfc7eSAndroid Build Coastguard Worker
299*49cdfc7eSAndroid Build Coastguard Worker return rval;
300*49cdfc7eSAndroid Build Coastguard Worker }
301*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_GETRLIMIT(cleanup_fn, resource, rlim) \
302*49cdfc7eSAndroid Build Coastguard Worker safe_getrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
303*49cdfc7eSAndroid Build Coastguard Worker
safe_setrlimit(const char * file,const int lineno,void (cleanup_fn)(void),int resource,const struct rlimit * rlim)304*49cdfc7eSAndroid Build Coastguard Worker static inline int safe_setrlimit(const char *file, const int lineno,
305*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), int resource, const struct rlimit *rlim)
306*49cdfc7eSAndroid Build Coastguard Worker {
307*49cdfc7eSAndroid Build Coastguard Worker int rval;
308*49cdfc7eSAndroid Build Coastguard Worker
309*49cdfc7eSAndroid Build Coastguard Worker rval = setrlimit(resource, rlim);
310*49cdfc7eSAndroid Build Coastguard Worker
311*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
312*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn,
313*49cdfc7eSAndroid Build Coastguard Worker "%s:%d: setrlimit(%d,%p) failed",
314*49cdfc7eSAndroid Build Coastguard Worker file, lineno, resource, rlim);
315*49cdfc7eSAndroid Build Coastguard Worker }
316*49cdfc7eSAndroid Build Coastguard Worker
317*49cdfc7eSAndroid Build Coastguard Worker return rval;
318*49cdfc7eSAndroid Build Coastguard Worker }
319*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_SETRLIMIT(cleanup_fn, resource, rlim) \
320*49cdfc7eSAndroid Build Coastguard Worker safe_setrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
321*49cdfc7eSAndroid Build Coastguard Worker
322*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_OPENDIR(cleanup_fn, name) \
323*49cdfc7eSAndroid Build Coastguard Worker safe_opendir(__FILE__, __LINE__, (cleanup_fn), (name))
324*49cdfc7eSAndroid Build Coastguard Worker
325*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_CLOSEDIR(cleanup_fn, dirp) \
326*49cdfc7eSAndroid Build Coastguard Worker safe_closedir(__FILE__, __LINE__, (cleanup_fn), (dirp))
327*49cdfc7eSAndroid Build Coastguard Worker
328*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_READDIR(cleanup_fn, dirp) \
329*49cdfc7eSAndroid Build Coastguard Worker safe_readdir(__FILE__, __LINE__, (cleanup_fn), (dirp))
330*49cdfc7eSAndroid Build Coastguard Worker
331*49cdfc7eSAndroid Build Coastguard Worker
332*49cdfc7eSAndroid Build Coastguard Worker #define SAFE_IOCTL(cleanup_fn, fd, request, ...) \
333*49cdfc7eSAndroid Build Coastguard Worker ({int ret = ioctl(fd, request, __VA_ARGS__); \
334*49cdfc7eSAndroid Build Coastguard Worker if (ret < 0) \
335*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup_fn, \
336*49cdfc7eSAndroid Build Coastguard Worker "ioctl(%i,%s,...) failed", fd, #request); \
337*49cdfc7eSAndroid Build Coastguard Worker ret;})
338*49cdfc7eSAndroid Build Coastguard Worker
339*49cdfc7eSAndroid Build Coastguard Worker #endif /* __SAFE_MACROS_H__ */
340*49cdfc7eSAndroid Build Coastguard Worker #endif /* __TEST_H__ */
341