blob: d352d5a3c80c78d9b91bce1fe9fe895c7df9a420 [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
Nikita Yushchenkoe8994572016-11-26 11:30:01 +0300100/*
101 * inline function that uses off_t since sizeof(off_t) depends on compile flags
102 */
103static inline ssize_t safe_pread(const char *file, const int lineno,
104 char len_strict, int fildes, void *buf, size_t nbyte,
105 off_t offset)
106{
107 ssize_t rval;
108
109 rval = pread(fildes, buf, nbyte, offset);
110
111 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
112 tst_brk_(file, lineno, TBROK | TERRNO,
113 "pread(%d,%p,%zu,%lld) failed",
114 fildes, buf, nbyte, (long long)offset);
115 }
116
117 return rval;
118}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100119#define SAFE_PREAD(len_strict, fildes, buf, nbyte, offset) \
Nikita Yushchenkoe8994572016-11-26 11:30:01 +0300120 safe_pread(__FILE__, __LINE__, (len_strict), (fildes), \
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100121 (buf), (nbyte), (offset))
122
123#define SAFE_SETEGID(egid) \
124 safe_setegid(__FILE__, __LINE__, NULL, (egid))
125
126#define SAFE_SETEUID(euid) \
127 safe_seteuid(__FILE__, __LINE__, NULL, (euid))
128
129#define SAFE_SETGID(gid) \
130 safe_setgid(__FILE__, __LINE__, NULL, (gid))
131
132#define SAFE_SETUID(uid) \
133 safe_setuid(__FILE__, __LINE__, NULL, (uid))
134
135#define SAFE_GETRESUID(ruid, euid, suid) \
136 safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid))
137
138#define SAFE_GETRESGID(rgid, egid, sgid) \
139 safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid))
140
Cyril Hrubisd1ea8302016-06-07 14:18:18 +0200141static inline int safe_setpgid(const char *file, const int lineno,
142 pid_t pid, pid_t pgid)
143{
144 int rval;
145
146 rval = setpgid(pid, pgid);
147 if (rval) {
148 tst_brk_(file, lineno, TBROK | TERRNO,
149 "setpgid(%i, %i) failed", pid, pgid);
150 }
151
152 return rval;
153}
154#define SAFE_SETPGID(pid, pgid) \
155 safe_setpgid(__FILE__, __LINE__, (pid), (pgid));
156
Stanislav Kholmanskikh3953c372016-08-04 17:16:30 +0300157static inline pid_t safe_getpgid(const char *file, const int lineno,
158 pid_t pid)
159{
160 pid_t pgid;
161
162 pgid = getpgid(pid);
163 if (pgid == -1) {
164 tst_brk_(file, lineno, TBROK | TERRNO,
165 "getpgid(%i) failed", pid);
166 }
167
168 return pgid;
169}
170#define SAFE_GETPGID(pid) \
171 safe_getpgid(__FILE__, __LINE__, (pid))
172
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100173#define SAFE_UNLINK(pathname) \
174 safe_unlink(__FILE__, __LINE__, NULL, (pathname))
175
176#define SAFE_LINK(oldpath, newpath) \
177 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
178
179#define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
180 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
181 (newdirfd), (newpath), (flags))
182
183#define SAFE_READLINK(path, buf, bufsize) \
184 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
185
186#define SAFE_SYMLINK(oldpath, newpath) \
187 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
188
189#define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
190 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
191
Nikita Yushchenkoe8994572016-11-26 11:30:01 +0300192static inline ssize_t safe_pwrite(const char *file, const int lineno,
193 char len_strict, int fildes, const void *buf, size_t nbyte,
194 off_t offset)
195{
196 ssize_t rval;
197
198 rval = pwrite(fildes, buf, nbyte, offset);
199 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
200 tst_brk_(file, lineno, TBROK | TERRNO,
201 "pwrite(%d,%p,%zu,%lld) failed",
202 fildes, buf, nbyte, (long long)offset);
203 }
204
205 return rval;
206}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100207#define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \
Nikita Yushchenkoe8994572016-11-26 11:30:01 +0300208 safe_pwrite(__FILE__, __LINE__, (len_strict), (fildes), \
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100209 (buf), (nbyte), (offset))
210
211#define SAFE_STRTOL(str, min, max) \
212 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
213
214#define SAFE_STRTOUL(str, min, max) \
215 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
216
217#define SAFE_SYSCONF(name) \
218 safe_sysconf(__FILE__, __LINE__, NULL, name)
219
220#define SAFE_CHMOD(path, mode) \
221 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
222
223#define SAFE_FCHMOD(fd, mode) \
224 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
225
226#define SAFE_CHOWN(path, owner, group) \
227 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
228
229#define SAFE_FCHOWN(fd, owner, group) \
230 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
231
232#define SAFE_WAIT(status) \
233 safe_wait(__FILE__, __LINE__, NULL, (status))
234
235#define SAFE_WAITPID(pid, status, opts) \
236 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
237
238#define SAFE_KILL(pid, sig) \
239 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
240
241#define SAFE_MEMALIGN(alignment, size) \
242 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
243
244#define SAFE_MKFIFO(pathname, mode) \
245 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
246
247#define SAFE_RENAME(oldpath, newpath) \
248 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
249
250#define SAFE_MOUNT(source, target, filesystemtype, \
251 mountflags, data) \
252 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
253 (filesystemtype), (mountflags), (data))
254
255#define SAFE_UMOUNT(target) \
256 safe_umount(__FILE__, __LINE__, NULL, (target))
257
258#define SAFE_OPENDIR(name) \
259 safe_opendir(__FILE__, __LINE__, NULL, (name))
260
261#define SAFE_CLOSEDIR(dirp) \
262 safe_closedir(__FILE__, __LINE__, NULL, (dirp))
263
264#define SAFE_READDIR(dirp) \
265 safe_readdir(__FILE__, __LINE__, NULL, (dirp))
266
267#define SAFE_IOCTL(fd, request, ...) \
268 ({int ret = ioctl(fd, request, ##__VA_ARGS__); \
269 ret < 0 ? \
270 tst_brk(TBROK | TERRNO, \
271 "ioctl(%i,%s,...) failed", fd, #request) \
272 : ret;})
273
274#define SAFE_FCNTL(fd, cmd, ...) \
275 ({int ret = fcntl(fd, cmd, ##__VA_ARGS__); \
276 ret == -1 ? \
277 tst_brk(TBROK | TERRNO, \
278 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
279 : ret;})
280
281/*
282 * following functions are inline because the behaviour may depend on
283 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags
284 */
285
286static inline void *safe_mmap(const char *file, const int lineno,
287 void *addr, size_t length,
288 int prot, int flags, int fd, off_t offset)
289{
290 void *rval;
291
292 rval = mmap(addr, length, prot, flags, fd, offset);
293 if (rval == MAP_FAILED) {
294 tst_brk_(file, lineno, TBROK | TERRNO,
295 "mmap(%p,%zu,%d,%d,%d,%ld) failed",
296 addr, length, prot, flags, fd, (long) offset);
297 }
298
299 return rval;
300}
301#define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
302 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
303 (flags), (fd), (offset))
304
305static inline int safe_ftruncate(const char *file, const int lineno,
306 int fd, off_t length)
307{
308 int rval;
309
310 rval = ftruncate(fd, length);
311 if (rval == -1) {
312 tst_brk_(file, lineno, TBROK | TERRNO,
313 "ftruncate(%d,%ld) failed",
314 fd, (long)length);
315 }
316
317 return rval;
318}
319#define SAFE_FTRUNCATE(fd, length) \
320 safe_ftruncate(__FILE__, __LINE__, (fd), (length))
321
322static inline int safe_truncate(const char *file, const int lineno,
323 const char *path, off_t length)
324{
325 int rval;
326
327 rval = truncate(path, length);
328 if (rval == -1) {
329 tst_brk_(file, lineno, TBROK | TERRNO,
330 "truncate(%s,%ld) failed",
331 path, (long)length);
332 }
333
334 return rval;
335}
336#define SAFE_TRUNCATE(path, length) \
337 safe_truncate(__FILE__, __LINE__, (path), (length))
338
339static inline int safe_stat(const char *file, const int lineno,
340 const char *path, struct stat *buf)
341{
342 int rval;
343
344 rval = stat(path, buf);
345
346 if (rval == -1) {
347 tst_brk_(file, lineno, TBROK | TERRNO,
348 "stat(%s,%p) failed", path, buf);
349 }
350
351 return rval;
352}
353#define SAFE_STAT(path, buf) \
354 safe_stat(__FILE__, __LINE__, (path), (buf))
355
356static inline int safe_fstat(const char *file, const int lineno,
357 int fd, struct stat *buf)
358{
359 int rval;
360
361 rval = fstat(fd, buf);
362
363 if (rval == -1) {
364 tst_brk_(file, lineno, TBROK | TERRNO,
365 "fstat(%d,%p) failed", fd, buf);
366 }
367
368 return rval;
369}
370#define SAFE_FSTAT(fd, buf) \
371 safe_fstat(__FILE__, __LINE__, (fd), (buf))
372
373static inline int safe_lstat(const char *file, const int lineno,
374 const char *path, struct stat *buf)
375{
376 int rval;
377
378 rval = lstat(path, buf);
379
380 if (rval == -1) {
381 tst_brk_(file, lineno, TBROK | TERRNO,
382 "lstat(%s,%p) failed", path, buf);
383 }
384
385 return rval;
386}
387#define SAFE_LSTAT(path, buf) \
388 safe_lstat(__FILE__, __LINE__, (path), (buf))
389
390static inline off_t safe_lseek(const char *file, const int lineno,
391 int fd, off_t offset, int whence)
392{
393 off_t rval;
394
395 rval = lseek(fd, offset, whence);
396
397 if (rval == (off_t) -1) {
398 tst_brk_(file, lineno, TBROK | TERRNO,
399 "lseek(%d,%ld,%d) failed",
400 fd, (long)offset, whence);
401 }
402
403 return rval;
404}
405#define SAFE_LSEEK(fd, offset, whence) \
406 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
407
408static inline int safe_getrlimit(const char *file, const int lineno,
409 int resource, struct rlimit *rlim)
410{
411 int rval;
412
413 rval = getrlimit(resource, rlim);
414
415 if (rval == -1) {
416 tst_brk_(file, lineno, TBROK | TERRNO,
417 "getrlimit(%d,%p) failed",
418 resource, rlim);
419 }
420
421 return rval;
422}
423#define SAFE_GETRLIMIT(resource, rlim) \
424 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
425
426static inline int safe_setrlimit(const char *file, const int lineno,
427 int resource, const struct rlimit *rlim)
428{
429 int rval;
430
431 rval = setrlimit(resource, rlim);
432
433 if (rval == -1) {
434 tst_brk_(file, lineno, TBROK | TERRNO,
435 "setrlimit(%d,%p) failed",
436 resource, rlim);
437 }
438
439 return rval;
440}
441#define SAFE_SETRLIMIT(resource, rlim) \
442 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
443
Jan Stancek12a52302016-05-12 10:52:07 +0200444typedef void (*sighandler_t)(int);
445static inline sighandler_t safe_signal(const char *file, const int lineno,
446 int signum, sighandler_t handler)
447{
448 sighandler_t rval;
449
450 rval = signal(signum, handler);
451
452 if (rval == SIG_ERR) {
453 tst_brk_(file, lineno, TBROK | TERRNO,
454 "signal(%d,%p) failed",
455 signum, handler);
456 }
457
458 return rval;
459}
460
461#define SAFE_SIGNAL(signum, handler) \
462 safe_signal(__FILE__, __LINE__, (signum), (handler))
463
Cyril Hrubis4b8dd422016-11-01 10:12:03 +0100464#define SAFE_EXECLP(file, arg, ...) do { \
465 execlp((file), (arg), ##__VA_ARGS__); \
466 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
467 "execlp(%s, %s, ...) failed", file, arg); \
468 } while (0)
469
Cyril Hrubisaf5a4b12016-11-07 13:05:00 +0100470int safe_getpriority(const char *file, const int lineno, int which, id_t who);
471#define SAFE_GETPRIORITY(which, who) \
472 safe_getpriority(__FILE__, __LINE__, (which), (who))
473
Dejan Jovicevic10552812016-11-15 13:19:33 +0100474int safe_setxattr(const char *file, const int lineno, const char *path,
475 const char *name, const void *value, size_t size, int flags);
476#define SAFE_SETXATTR(path, name, value, size, flags) \
477 safe_setxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
478
479int safe_lsetxattr(const char *file, const int lineno, const char *path,
480 const char *name, const void *value, size_t size, int flags);
481#define SAFE_LSETXATTR(path, name, value, size, flags) \
482 safe_lsetxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
483
484int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
485 const void *value, size_t size, int flags);
486#define SAFE_FSETXATTR(fd, name, value, size, flags) \
487 safe_fsetxattr(__FILE__, __LINE__, (fd), (name), (value), (size), (flags))
488
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100489#endif /* SAFE_MACROS_H__ */