blob: df858beb7909fe107dc38f698e6907434e9eee66 [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 Hrubis52006c92017-01-02 14:58:31 +0100141int safe_setpgid(const char *file, const int lineno, pid_t pid, pid_t pgid);
Cyril Hrubisd1ea8302016-06-07 14:18:18 +0200142
Cyril Hrubisd1ea8302016-06-07 14:18:18 +0200143#define SAFE_SETPGID(pid, pgid) \
144 safe_setpgid(__FILE__, __LINE__, (pid), (pgid));
145
Cyril Hrubis52006c92017-01-02 14:58:31 +0100146pid_t safe_getpgid(const char *file, const int lineno, pid_t pid);
Stanislav Kholmanskikh3953c372016-08-04 17:16:30 +0300147
Stanislav Kholmanskikh3953c372016-08-04 17:16:30 +0300148#define SAFE_GETPGID(pid) \
149 safe_getpgid(__FILE__, __LINE__, (pid))
150
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100151#define SAFE_UNLINK(pathname) \
152 safe_unlink(__FILE__, __LINE__, NULL, (pathname))
153
154#define SAFE_LINK(oldpath, newpath) \
155 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
156
157#define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
158 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
159 (newdirfd), (newpath), (flags))
160
161#define SAFE_READLINK(path, buf, bufsize) \
162 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
163
164#define SAFE_SYMLINK(oldpath, newpath) \
165 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
166
167#define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
168 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
169
Nikita Yushchenkoe8994572016-11-26 11:30:01 +0300170static inline ssize_t safe_pwrite(const char *file, const int lineno,
171 char len_strict, int fildes, const void *buf, size_t nbyte,
172 off_t offset)
173{
174 ssize_t rval;
175
176 rval = pwrite(fildes, buf, nbyte, offset);
177 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
178 tst_brk_(file, lineno, TBROK | TERRNO,
179 "pwrite(%d,%p,%zu,%lld) failed",
180 fildes, buf, nbyte, (long long)offset);
181 }
182
183 return rval;
184}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100185#define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \
Nikita Yushchenkoe8994572016-11-26 11:30:01 +0300186 safe_pwrite(__FILE__, __LINE__, (len_strict), (fildes), \
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100187 (buf), (nbyte), (offset))
188
189#define SAFE_STRTOL(str, min, max) \
190 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
191
192#define SAFE_STRTOUL(str, min, max) \
193 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
194
195#define SAFE_SYSCONF(name) \
196 safe_sysconf(__FILE__, __LINE__, NULL, name)
197
198#define SAFE_CHMOD(path, mode) \
199 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
200
201#define SAFE_FCHMOD(fd, mode) \
202 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
203
204#define SAFE_CHOWN(path, owner, group) \
205 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
206
207#define SAFE_FCHOWN(fd, owner, group) \
208 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
209
210#define SAFE_WAIT(status) \
211 safe_wait(__FILE__, __LINE__, NULL, (status))
212
213#define SAFE_WAITPID(pid, status, opts) \
214 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
215
216#define SAFE_KILL(pid, sig) \
217 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
218
219#define SAFE_MEMALIGN(alignment, size) \
220 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
221
222#define SAFE_MKFIFO(pathname, mode) \
223 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
224
225#define SAFE_RENAME(oldpath, newpath) \
226 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
227
228#define SAFE_MOUNT(source, target, filesystemtype, \
229 mountflags, data) \
230 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
231 (filesystemtype), (mountflags), (data))
232
233#define SAFE_UMOUNT(target) \
234 safe_umount(__FILE__, __LINE__, NULL, (target))
235
236#define SAFE_OPENDIR(name) \
237 safe_opendir(__FILE__, __LINE__, NULL, (name))
238
239#define SAFE_CLOSEDIR(dirp) \
240 safe_closedir(__FILE__, __LINE__, NULL, (dirp))
241
242#define SAFE_READDIR(dirp) \
243 safe_readdir(__FILE__, __LINE__, NULL, (dirp))
244
245#define SAFE_IOCTL(fd, request, ...) \
246 ({int ret = ioctl(fd, request, ##__VA_ARGS__); \
247 ret < 0 ? \
248 tst_brk(TBROK | TERRNO, \
249 "ioctl(%i,%s,...) failed", fd, #request) \
250 : ret;})
251
252#define SAFE_FCNTL(fd, cmd, ...) \
253 ({int ret = fcntl(fd, cmd, ##__VA_ARGS__); \
254 ret == -1 ? \
255 tst_brk(TBROK | TERRNO, \
256 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
257 : ret;})
258
259/*
260 * following functions are inline because the behaviour may depend on
261 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags
262 */
263
264static inline void *safe_mmap(const char *file, const int lineno,
265 void *addr, size_t length,
266 int prot, int flags, int fd, off_t offset)
267{
268 void *rval;
269
270 rval = mmap(addr, length, prot, flags, fd, offset);
271 if (rval == MAP_FAILED) {
272 tst_brk_(file, lineno, TBROK | TERRNO,
273 "mmap(%p,%zu,%d,%d,%d,%ld) failed",
274 addr, length, prot, flags, fd, (long) offset);
275 }
276
277 return rval;
278}
279#define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
280 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
281 (flags), (fd), (offset))
282
283static inline int safe_ftruncate(const char *file, const int lineno,
284 int fd, off_t length)
285{
286 int rval;
287
288 rval = ftruncate(fd, length);
289 if (rval == -1) {
290 tst_brk_(file, lineno, TBROK | TERRNO,
291 "ftruncate(%d,%ld) failed",
292 fd, (long)length);
293 }
294
295 return rval;
296}
297#define SAFE_FTRUNCATE(fd, length) \
298 safe_ftruncate(__FILE__, __LINE__, (fd), (length))
299
300static inline int safe_truncate(const char *file, const int lineno,
301 const char *path, off_t length)
302{
303 int rval;
304
305 rval = truncate(path, length);
306 if (rval == -1) {
307 tst_brk_(file, lineno, TBROK | TERRNO,
308 "truncate(%s,%ld) failed",
309 path, (long)length);
310 }
311
312 return rval;
313}
314#define SAFE_TRUNCATE(path, length) \
315 safe_truncate(__FILE__, __LINE__, (path), (length))
316
317static inline int safe_stat(const char *file, const int lineno,
318 const char *path, struct stat *buf)
319{
320 int rval;
321
322 rval = stat(path, buf);
323
324 if (rval == -1) {
325 tst_brk_(file, lineno, TBROK | TERRNO,
326 "stat(%s,%p) failed", path, buf);
327 }
328
329 return rval;
330}
331#define SAFE_STAT(path, buf) \
332 safe_stat(__FILE__, __LINE__, (path), (buf))
333
334static inline int safe_fstat(const char *file, const int lineno,
335 int fd, struct stat *buf)
336{
337 int rval;
338
339 rval = fstat(fd, buf);
340
341 if (rval == -1) {
342 tst_brk_(file, lineno, TBROK | TERRNO,
343 "fstat(%d,%p) failed", fd, buf);
344 }
345
346 return rval;
347}
348#define SAFE_FSTAT(fd, buf) \
349 safe_fstat(__FILE__, __LINE__, (fd), (buf))
350
351static inline int safe_lstat(const char *file, const int lineno,
352 const char *path, struct stat *buf)
353{
354 int rval;
355
356 rval = lstat(path, buf);
357
358 if (rval == -1) {
359 tst_brk_(file, lineno, TBROK | TERRNO,
360 "lstat(%s,%p) failed", path, buf);
361 }
362
363 return rval;
364}
365#define SAFE_LSTAT(path, buf) \
366 safe_lstat(__FILE__, __LINE__, (path), (buf))
367
368static inline off_t safe_lseek(const char *file, const int lineno,
369 int fd, off_t offset, int whence)
370{
371 off_t rval;
372
373 rval = lseek(fd, offset, whence);
374
375 if (rval == (off_t) -1) {
376 tst_brk_(file, lineno, TBROK | TERRNO,
377 "lseek(%d,%ld,%d) failed",
378 fd, (long)offset, whence);
379 }
380
381 return rval;
382}
383#define SAFE_LSEEK(fd, offset, whence) \
384 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
385
386static inline int safe_getrlimit(const char *file, const int lineno,
387 int resource, struct rlimit *rlim)
388{
389 int rval;
390
391 rval = getrlimit(resource, rlim);
392
393 if (rval == -1) {
394 tst_brk_(file, lineno, TBROK | TERRNO,
395 "getrlimit(%d,%p) failed",
396 resource, rlim);
397 }
398
399 return rval;
400}
401#define SAFE_GETRLIMIT(resource, rlim) \
402 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
403
404static inline int safe_setrlimit(const char *file, const int lineno,
405 int resource, const struct rlimit *rlim)
406{
407 int rval;
408
409 rval = setrlimit(resource, rlim);
410
411 if (rval == -1) {
412 tst_brk_(file, lineno, TBROK | TERRNO,
413 "setrlimit(%d,%p) failed",
414 resource, rlim);
415 }
416
417 return rval;
418}
419#define SAFE_SETRLIMIT(resource, rlim) \
420 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
421
Jan Stancek12a52302016-05-12 10:52:07 +0200422typedef void (*sighandler_t)(int);
423static inline sighandler_t safe_signal(const char *file, const int lineno,
424 int signum, sighandler_t handler)
425{
426 sighandler_t rval;
427
428 rval = signal(signum, handler);
429
430 if (rval == SIG_ERR) {
431 tst_brk_(file, lineno, TBROK | TERRNO,
432 "signal(%d,%p) failed",
433 signum, handler);
434 }
435
436 return rval;
437}
438
439#define SAFE_SIGNAL(signum, handler) \
440 safe_signal(__FILE__, __LINE__, (signum), (handler))
441
Cyril Hrubis4b8dd422016-11-01 10:12:03 +0100442#define SAFE_EXECLP(file, arg, ...) do { \
443 execlp((file), (arg), ##__VA_ARGS__); \
444 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
445 "execlp(%s, %s, ...) failed", file, arg); \
446 } while (0)
447
Cyril Hrubisaf5a4b12016-11-07 13:05:00 +0100448int safe_getpriority(const char *file, const int lineno, int which, id_t who);
449#define SAFE_GETPRIORITY(which, who) \
450 safe_getpriority(__FILE__, __LINE__, (which), (who))
451
Dejan Jovicevic10552812016-11-15 13:19:33 +0100452int safe_setxattr(const char *file, const int lineno, const char *path,
453 const char *name, const void *value, size_t size, int flags);
454#define SAFE_SETXATTR(path, name, value, size, flags) \
455 safe_setxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
456
457int safe_lsetxattr(const char *file, const int lineno, const char *path,
458 const char *name, const void *value, size_t size, int flags);
459#define SAFE_LSETXATTR(path, name, value, size, flags) \
460 safe_lsetxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
461
462int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
463 const void *value, size_t size, int flags);
464#define SAFE_FSETXATTR(fd, name, value, size, flags) \
465 safe_fsetxattr(__FILE__, __LINE__, (fd), (name), (value), (size), (flags))
466
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100467#endif /* SAFE_MACROS_H__ */