blob: a8634a0e0f3c6a9a20b8e07701c0e3b82d2f3330 [file] [log] [blame]
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001/*
2 * Copyright (c) 2010-2015 Linux Test Project
3 * Copyright (c) 2011-2015 Cyril Hrubis <chrubis@suse.cz>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef TST_SAFE_MACROS_H__
20#define TST_SAFE_MACROS_H__
21
22#include <sys/mman.h>
23#include <sys/types.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <libgen.h>
29#include <stdarg.h>
30#include <unistd.h>
31#include <dirent.h>
32
33#include "safe_macros_fn.h"
34
35#define SAFE_BASENAME(path) \
36 safe_basename(__FILE__, __LINE__, NULL, (path))
37
38#define SAFE_CHDIR(path) \
39 safe_chdir(__FILE__, __LINE__, NULL, (path))
40
41#define SAFE_CLOSE(fd) do { \
42 safe_close(__FILE__, __LINE__, NULL, (fd)); \
43 fd = -1; \
44 } while (0)
45
46#define SAFE_CREAT(pathname, mode) \
47 safe_creat(__FILE__, __LINE__, NULL, (pathname), (mode))
48
49#define SAFE_DIRNAME(path) \
50 safe_dirname(__FILE__, __LINE__, NULL, (path))
51
52#define SAFE_GETCWD(buf, size) \
53 safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
54
55#define SAFE_GETPWNAM(name) \
56 safe_getpwnam(__FILE__, __LINE__, NULL, (name))
57
58#define SAFE_GETRUSAGE(who, usage) \
59 safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
60
61#define SAFE_MALLOC(size) \
62 safe_malloc(__FILE__, __LINE__, NULL, (size))
63
64#define SAFE_MKDIR(pathname, mode) \
65 safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode))
66
67#define SAFE_RMDIR(pathname) \
68 safe_rmdir(__FILE__, __LINE__, NULL, (pathname))
69
70#define SAFE_MUNMAP(addr, length) \
71 safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
72
73#define SAFE_OPEN(pathname, oflags, ...) \
74 safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \
75 ##__VA_ARGS__)
76
77#define SAFE_PIPE(fildes) \
78 safe_pipe(__FILE__, __LINE__, NULL, (fildes))
79
80#define SAFE_READ(len_strict, fildes, buf, nbyte) \
81 safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
82
83#define SAFE_PREAD(len_strict, fildes, buf, nbyte, offset) \
84 safe_pread(__FILE__, __LINE__, NULL, (len_strict), (fildes), \
85 (buf), (nbyte), (offset))
86
87#define SAFE_SETEGID(egid) \
88 safe_setegid(__FILE__, __LINE__, NULL, (egid))
89
90#define SAFE_SETEUID(euid) \
91 safe_seteuid(__FILE__, __LINE__, NULL, (euid))
92
93#define SAFE_SETGID(gid) \
94 safe_setgid(__FILE__, __LINE__, NULL, (gid))
95
96#define SAFE_SETUID(uid) \
97 safe_setuid(__FILE__, __LINE__, NULL, (uid))
98
99#define SAFE_GETRESUID(ruid, euid, suid) \
100 safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid))
101
102#define SAFE_GETRESGID(rgid, egid, sgid) \
103 safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid))
104
105#define SAFE_UNLINK(pathname) \
106 safe_unlink(__FILE__, __LINE__, NULL, (pathname))
107
108#define SAFE_LINK(oldpath, newpath) \
109 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
110
111#define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
112 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
113 (newdirfd), (newpath), (flags))
114
115#define SAFE_READLINK(path, buf, bufsize) \
116 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
117
118#define SAFE_SYMLINK(oldpath, newpath) \
119 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
120
121#define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
122 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
123
124#define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \
125 safe_pwrite(__FILE__, __LINE__, NULL, (len_strict), (fildes), \
126 (buf), (nbyte), (offset))
127
128#define SAFE_STRTOL(str, min, max) \
129 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
130
131#define SAFE_STRTOUL(str, min, max) \
132 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
133
134#define SAFE_SYSCONF(name) \
135 safe_sysconf(__FILE__, __LINE__, NULL, name)
136
137#define SAFE_CHMOD(path, mode) \
138 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
139
140#define SAFE_FCHMOD(fd, mode) \
141 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
142
143#define SAFE_CHOWN(path, owner, group) \
144 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
145
146#define SAFE_FCHOWN(fd, owner, group) \
147 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
148
149#define SAFE_WAIT(status) \
150 safe_wait(__FILE__, __LINE__, NULL, (status))
151
152#define SAFE_WAITPID(pid, status, opts) \
153 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
154
155#define SAFE_KILL(pid, sig) \
156 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
157
158#define SAFE_MEMALIGN(alignment, size) \
159 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
160
161#define SAFE_MKFIFO(pathname, mode) \
162 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
163
164#define SAFE_RENAME(oldpath, newpath) \
165 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
166
167#define SAFE_MOUNT(source, target, filesystemtype, \
168 mountflags, data) \
169 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
170 (filesystemtype), (mountflags), (data))
171
172#define SAFE_UMOUNT(target) \
173 safe_umount(__FILE__, __LINE__, NULL, (target))
174
175#define SAFE_OPENDIR(name) \
176 safe_opendir(__FILE__, __LINE__, NULL, (name))
177
178#define SAFE_CLOSEDIR(dirp) \
179 safe_closedir(__FILE__, __LINE__, NULL, (dirp))
180
181#define SAFE_READDIR(dirp) \
182 safe_readdir(__FILE__, __LINE__, NULL, (dirp))
183
184#define SAFE_IOCTL(fd, request, ...) \
185 ({int ret = ioctl(fd, request, ##__VA_ARGS__); \
186 ret < 0 ? \
187 tst_brk(TBROK | TERRNO, \
188 "ioctl(%i,%s,...) failed", fd, #request) \
189 : ret;})
190
191#define SAFE_FCNTL(fd, cmd, ...) \
192 ({int ret = fcntl(fd, cmd, ##__VA_ARGS__); \
193 ret == -1 ? \
194 tst_brk(TBROK | TERRNO, \
195 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
196 : ret;})
197
198/*
199 * following functions are inline because the behaviour may depend on
200 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags
201 */
202
203static inline void *safe_mmap(const char *file, const int lineno,
204 void *addr, size_t length,
205 int prot, int flags, int fd, off_t offset)
206{
207 void *rval;
208
209 rval = mmap(addr, length, prot, flags, fd, offset);
210 if (rval == MAP_FAILED) {
211 tst_brk_(file, lineno, TBROK | TERRNO,
212 "mmap(%p,%zu,%d,%d,%d,%ld) failed",
213 addr, length, prot, flags, fd, (long) offset);
214 }
215
216 return rval;
217}
218#define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
219 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
220 (flags), (fd), (offset))
221
222static inline int safe_ftruncate(const char *file, const int lineno,
223 int fd, off_t length)
224{
225 int rval;
226
227 rval = ftruncate(fd, length);
228 if (rval == -1) {
229 tst_brk_(file, lineno, TBROK | TERRNO,
230 "ftruncate(%d,%ld) failed",
231 fd, (long)length);
232 }
233
234 return rval;
235}
236#define SAFE_FTRUNCATE(fd, length) \
237 safe_ftruncate(__FILE__, __LINE__, (fd), (length))
238
239static inline int safe_truncate(const char *file, const int lineno,
240 const char *path, off_t length)
241{
242 int rval;
243
244 rval = truncate(path, length);
245 if (rval == -1) {
246 tst_brk_(file, lineno, TBROK | TERRNO,
247 "truncate(%s,%ld) failed",
248 path, (long)length);
249 }
250
251 return rval;
252}
253#define SAFE_TRUNCATE(path, length) \
254 safe_truncate(__FILE__, __LINE__, (path), (length))
255
256static inline int safe_stat(const char *file, const int lineno,
257 const char *path, struct stat *buf)
258{
259 int rval;
260
261 rval = stat(path, buf);
262
263 if (rval == -1) {
264 tst_brk_(file, lineno, TBROK | TERRNO,
265 "stat(%s,%p) failed", path, buf);
266 }
267
268 return rval;
269}
270#define SAFE_STAT(path, buf) \
271 safe_stat(__FILE__, __LINE__, (path), (buf))
272
273static inline int safe_fstat(const char *file, const int lineno,
274 int fd, struct stat *buf)
275{
276 int rval;
277
278 rval = fstat(fd, buf);
279
280 if (rval == -1) {
281 tst_brk_(file, lineno, TBROK | TERRNO,
282 "fstat(%d,%p) failed", fd, buf);
283 }
284
285 return rval;
286}
287#define SAFE_FSTAT(fd, buf) \
288 safe_fstat(__FILE__, __LINE__, (fd), (buf))
289
290static inline int safe_lstat(const char *file, const int lineno,
291 const char *path, struct stat *buf)
292{
293 int rval;
294
295 rval = lstat(path, buf);
296
297 if (rval == -1) {
298 tst_brk_(file, lineno, TBROK | TERRNO,
299 "lstat(%s,%p) failed", path, buf);
300 }
301
302 return rval;
303}
304#define SAFE_LSTAT(path, buf) \
305 safe_lstat(__FILE__, __LINE__, (path), (buf))
306
307static inline off_t safe_lseek(const char *file, const int lineno,
308 int fd, off_t offset, int whence)
309{
310 off_t rval;
311
312 rval = lseek(fd, offset, whence);
313
314 if (rval == (off_t) -1) {
315 tst_brk_(file, lineno, TBROK | TERRNO,
316 "lseek(%d,%ld,%d) failed",
317 fd, (long)offset, whence);
318 }
319
320 return rval;
321}
322#define SAFE_LSEEK(fd, offset, whence) \
323 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
324
325static inline int safe_getrlimit(const char *file, const int lineno,
326 int resource, struct rlimit *rlim)
327{
328 int rval;
329
330 rval = getrlimit(resource, rlim);
331
332 if (rval == -1) {
333 tst_brk_(file, lineno, TBROK | TERRNO,
334 "getrlimit(%d,%p) failed",
335 resource, rlim);
336 }
337
338 return rval;
339}
340#define SAFE_GETRLIMIT(resource, rlim) \
341 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
342
343static inline int safe_setrlimit(const char *file, const int lineno,
344 int resource, const struct rlimit *rlim)
345{
346 int rval;
347
348 rval = setrlimit(resource, rlim);
349
350 if (rval == -1) {
351 tst_brk_(file, lineno, TBROK | TERRNO,
352 "setrlimit(%d,%p) failed",
353 resource, rlim);
354 }
355
356 return rval;
357}
358#define SAFE_SETRLIMIT(resource, rlim) \
359 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
360
361#endif /* SAFE_MACROS_H__ */