blob: 96c2f36eee99dee1b4f059248021924e4e349f75 [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>
Jan Stancek12a52302016-05-12 10:52:07 +020029#include <signal.h>
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010030#include <stdarg.h>
31#include <unistd.h>
32#include <dirent.h>
33
34#include "safe_macros_fn.h"
35
36#define SAFE_BASENAME(path) \
37 safe_basename(__FILE__, __LINE__, NULL, (path))
38
39#define SAFE_CHDIR(path) \
40 safe_chdir(__FILE__, __LINE__, NULL, (path))
41
42#define SAFE_CLOSE(fd) do { \
43 safe_close(__FILE__, __LINE__, NULL, (fd)); \
44 fd = -1; \
45 } while (0)
46
47#define SAFE_CREAT(pathname, mode) \
48 safe_creat(__FILE__, __LINE__, NULL, (pathname), (mode))
49
50#define SAFE_DIRNAME(path) \
51 safe_dirname(__FILE__, __LINE__, NULL, (path))
52
53#define SAFE_GETCWD(buf, size) \
54 safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
55
56#define SAFE_GETPWNAM(name) \
57 safe_getpwnam(__FILE__, __LINE__, NULL, (name))
58
59#define SAFE_GETRUSAGE(who, usage) \
60 safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
61
62#define SAFE_MALLOC(size) \
63 safe_malloc(__FILE__, __LINE__, NULL, (size))
64
65#define SAFE_MKDIR(pathname, mode) \
66 safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode))
67
68#define SAFE_RMDIR(pathname) \
69 safe_rmdir(__FILE__, __LINE__, NULL, (pathname))
70
71#define SAFE_MUNMAP(addr, length) \
72 safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
73
74#define SAFE_OPEN(pathname, oflags, ...) \
75 safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \
76 ##__VA_ARGS__)
77
78#define SAFE_PIPE(fildes) \
79 safe_pipe(__FILE__, __LINE__, NULL, (fildes))
80
81#define SAFE_READ(len_strict, fildes, buf, nbyte) \
82 safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
83
84#define SAFE_PREAD(len_strict, fildes, buf, nbyte, offset) \
85 safe_pread(__FILE__, __LINE__, NULL, (len_strict), (fildes), \
86 (buf), (nbyte), (offset))
87
88#define SAFE_SETEGID(egid) \
89 safe_setegid(__FILE__, __LINE__, NULL, (egid))
90
91#define SAFE_SETEUID(euid) \
92 safe_seteuid(__FILE__, __LINE__, NULL, (euid))
93
94#define SAFE_SETGID(gid) \
95 safe_setgid(__FILE__, __LINE__, NULL, (gid))
96
97#define SAFE_SETUID(uid) \
98 safe_setuid(__FILE__, __LINE__, NULL, (uid))
99
100#define SAFE_GETRESUID(ruid, euid, suid) \
101 safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid))
102
103#define SAFE_GETRESGID(rgid, egid, sgid) \
104 safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid))
105
106#define SAFE_UNLINK(pathname) \
107 safe_unlink(__FILE__, __LINE__, NULL, (pathname))
108
109#define SAFE_LINK(oldpath, newpath) \
110 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
111
112#define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
113 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
114 (newdirfd), (newpath), (flags))
115
116#define SAFE_READLINK(path, buf, bufsize) \
117 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
118
119#define SAFE_SYMLINK(oldpath, newpath) \
120 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
121
122#define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
123 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
124
125#define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \
126 safe_pwrite(__FILE__, __LINE__, NULL, (len_strict), (fildes), \
127 (buf), (nbyte), (offset))
128
129#define SAFE_STRTOL(str, min, max) \
130 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
131
132#define SAFE_STRTOUL(str, min, max) \
133 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
134
135#define SAFE_SYSCONF(name) \
136 safe_sysconf(__FILE__, __LINE__, NULL, name)
137
138#define SAFE_CHMOD(path, mode) \
139 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
140
141#define SAFE_FCHMOD(fd, mode) \
142 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
143
144#define SAFE_CHOWN(path, owner, group) \
145 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
146
147#define SAFE_FCHOWN(fd, owner, group) \
148 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
149
150#define SAFE_WAIT(status) \
151 safe_wait(__FILE__, __LINE__, NULL, (status))
152
153#define SAFE_WAITPID(pid, status, opts) \
154 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
155
156#define SAFE_KILL(pid, sig) \
157 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
158
159#define SAFE_MEMALIGN(alignment, size) \
160 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
161
162#define SAFE_MKFIFO(pathname, mode) \
163 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
164
165#define SAFE_RENAME(oldpath, newpath) \
166 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
167
168#define SAFE_MOUNT(source, target, filesystemtype, \
169 mountflags, data) \
170 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
171 (filesystemtype), (mountflags), (data))
172
173#define SAFE_UMOUNT(target) \
174 safe_umount(__FILE__, __LINE__, NULL, (target))
175
176#define SAFE_OPENDIR(name) \
177 safe_opendir(__FILE__, __LINE__, NULL, (name))
178
179#define SAFE_CLOSEDIR(dirp) \
180 safe_closedir(__FILE__, __LINE__, NULL, (dirp))
181
182#define SAFE_READDIR(dirp) \
183 safe_readdir(__FILE__, __LINE__, NULL, (dirp))
184
185#define SAFE_IOCTL(fd, request, ...) \
186 ({int ret = ioctl(fd, request, ##__VA_ARGS__); \
187 ret < 0 ? \
188 tst_brk(TBROK | TERRNO, \
189 "ioctl(%i,%s,...) failed", fd, #request) \
190 : ret;})
191
192#define SAFE_FCNTL(fd, cmd, ...) \
193 ({int ret = fcntl(fd, cmd, ##__VA_ARGS__); \
194 ret == -1 ? \
195 tst_brk(TBROK | TERRNO, \
196 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
197 : ret;})
198
199/*
200 * following functions are inline because the behaviour may depend on
201 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags
202 */
203
204static inline void *safe_mmap(const char *file, const int lineno,
205 void *addr, size_t length,
206 int prot, int flags, int fd, off_t offset)
207{
208 void *rval;
209
210 rval = mmap(addr, length, prot, flags, fd, offset);
211 if (rval == MAP_FAILED) {
212 tst_brk_(file, lineno, TBROK | TERRNO,
213 "mmap(%p,%zu,%d,%d,%d,%ld) failed",
214 addr, length, prot, flags, fd, (long) offset);
215 }
216
217 return rval;
218}
219#define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
220 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
221 (flags), (fd), (offset))
222
223static inline int safe_ftruncate(const char *file, const int lineno,
224 int fd, off_t length)
225{
226 int rval;
227
228 rval = ftruncate(fd, length);
229 if (rval == -1) {
230 tst_brk_(file, lineno, TBROK | TERRNO,
231 "ftruncate(%d,%ld) failed",
232 fd, (long)length);
233 }
234
235 return rval;
236}
237#define SAFE_FTRUNCATE(fd, length) \
238 safe_ftruncate(__FILE__, __LINE__, (fd), (length))
239
240static inline int safe_truncate(const char *file, const int lineno,
241 const char *path, off_t length)
242{
243 int rval;
244
245 rval = truncate(path, length);
246 if (rval == -1) {
247 tst_brk_(file, lineno, TBROK | TERRNO,
248 "truncate(%s,%ld) failed",
249 path, (long)length);
250 }
251
252 return rval;
253}
254#define SAFE_TRUNCATE(path, length) \
255 safe_truncate(__FILE__, __LINE__, (path), (length))
256
257static inline int safe_stat(const char *file, const int lineno,
258 const char *path, struct stat *buf)
259{
260 int rval;
261
262 rval = stat(path, buf);
263
264 if (rval == -1) {
265 tst_brk_(file, lineno, TBROK | TERRNO,
266 "stat(%s,%p) failed", path, buf);
267 }
268
269 return rval;
270}
271#define SAFE_STAT(path, buf) \
272 safe_stat(__FILE__, __LINE__, (path), (buf))
273
274static inline int safe_fstat(const char *file, const int lineno,
275 int fd, struct stat *buf)
276{
277 int rval;
278
279 rval = fstat(fd, buf);
280
281 if (rval == -1) {
282 tst_brk_(file, lineno, TBROK | TERRNO,
283 "fstat(%d,%p) failed", fd, buf);
284 }
285
286 return rval;
287}
288#define SAFE_FSTAT(fd, buf) \
289 safe_fstat(__FILE__, __LINE__, (fd), (buf))
290
291static inline int safe_lstat(const char *file, const int lineno,
292 const char *path, struct stat *buf)
293{
294 int rval;
295
296 rval = lstat(path, buf);
297
298 if (rval == -1) {
299 tst_brk_(file, lineno, TBROK | TERRNO,
300 "lstat(%s,%p) failed", path, buf);
301 }
302
303 return rval;
304}
305#define SAFE_LSTAT(path, buf) \
306 safe_lstat(__FILE__, __LINE__, (path), (buf))
307
308static inline off_t safe_lseek(const char *file, const int lineno,
309 int fd, off_t offset, int whence)
310{
311 off_t rval;
312
313 rval = lseek(fd, offset, whence);
314
315 if (rval == (off_t) -1) {
316 tst_brk_(file, lineno, TBROK | TERRNO,
317 "lseek(%d,%ld,%d) failed",
318 fd, (long)offset, whence);
319 }
320
321 return rval;
322}
323#define SAFE_LSEEK(fd, offset, whence) \
324 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
325
326static inline int safe_getrlimit(const char *file, const int lineno,
327 int resource, struct rlimit *rlim)
328{
329 int rval;
330
331 rval = getrlimit(resource, rlim);
332
333 if (rval == -1) {
334 tst_brk_(file, lineno, TBROK | TERRNO,
335 "getrlimit(%d,%p) failed",
336 resource, rlim);
337 }
338
339 return rval;
340}
341#define SAFE_GETRLIMIT(resource, rlim) \
342 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
343
344static inline int safe_setrlimit(const char *file, const int lineno,
345 int resource, const struct rlimit *rlim)
346{
347 int rval;
348
349 rval = setrlimit(resource, rlim);
350
351 if (rval == -1) {
352 tst_brk_(file, lineno, TBROK | TERRNO,
353 "setrlimit(%d,%p) failed",
354 resource, rlim);
355 }
356
357 return rval;
358}
359#define SAFE_SETRLIMIT(resource, rlim) \
360 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
361
Jan Stancek12a52302016-05-12 10:52:07 +0200362typedef void (*sighandler_t)(int);
363static inline sighandler_t safe_signal(const char *file, const int lineno,
364 int signum, sighandler_t handler)
365{
366 sighandler_t rval;
367
368 rval = signal(signum, handler);
369
370 if (rval == SIG_ERR) {
371 tst_brk_(file, lineno, TBROK | TERRNO,
372 "signal(%d,%p) failed",
373 signum, handler);
374 }
375
376 return rval;
377}
378
379#define SAFE_SIGNAL(signum, handler) \
380 safe_signal(__FILE__, __LINE__, (signum), (handler))
381
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100382#endif /* SAFE_MACROS_H__ */