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