blob: 3a26df63474932c8dbc1f6ccc79c29a9259d134f [file] [log] [blame]
Garrett Cooper1c2939f2010-12-20 05:12:53 -08001/*
2 * Safe macros for commonly used syscalls to reduce code duplication in LTP
3 * testcases, and to ensure all errors are caught in said testcases as
4 * gracefully as possible.
5 *
6 * Also satiates some versions of gcc/glibc when the warn_unused_result
7 * attribute is applied to the function call.
8 *
9 * Licensed under the GPLv2.
10 */
11
12#ifndef __TEST_H__
13#error "you must include test.h before this file"
14#else
15
16#ifndef __SAFE_MACROS_H__
17#define __SAFE_MACROS_H__
18
19#include <sys/types.h>
Caspar Zhang1c9037b2011-11-15 21:43:59 +080020#include <sys/resource.h>
Garrett Cooper1c2939f2010-12-20 05:12:53 -080021#include <sys/stat.h>
22#include <fcntl.h>
23#include <libgen.h>
24#include <stdarg.h>
25#include <unistd.h>
Cyril Hrubisd2085ca2013-06-18 14:47:19 +020026#include <stdio.h>
Garrett Cooper1c2939f2010-12-20 05:12:53 -080027
Garrett Cooperc6743962010-12-20 05:38:46 -080028char* safe_basename(const char *file, const int lineno,
29 void (*cleanup_fn)(void), char *path);
Garrett Cooper1c2939f2010-12-20 05:12:53 -080030#define SAFE_BASENAME(cleanup_fn, path) \
31 safe_basename(__FILE__, __LINE__, (cleanup_fn), (path))
32
Garrett Cooper4a3c6582010-12-21 07:07:01 -080033int safe_chdir(const char *file, const int lineno,
34 void (*cleanup_fn)(void), const char *path);
35#define SAFE_CHDIR(cleanup_fn, path) \
36 safe_chdir(__FILE__, __LINE__, (cleanup_fn), (path))
37
Garrett Cooperc6743962010-12-20 05:38:46 -080038int safe_close(const char *file, const int lineno,
39 void (*cleanup_fn)(void), int fildes);
Garrett Cooper1c2939f2010-12-20 05:12:53 -080040#define SAFE_CLOSE(cleanup_fn, fildes) \
41 safe_close(__FILE__, __LINE__, (cleanup_fn), (fildes))
42
Garrett Cooperc6743962010-12-20 05:38:46 -080043int safe_creat(const char *file, const int lineno,
44 void (*cleanup_fn)(void), char *pathname, mode_t mode);
Garrett Cooper1c2939f2010-12-20 05:12:53 -080045#define SAFE_CREAT(cleanup_fn, pathname, mode) \
46 safe_creat(__FILE__, __LINE__, cleanup_fn, (pathname), (mode))
47
Garrett Cooperc6743962010-12-20 05:38:46 -080048char* safe_dirname(const char *file, const int lineno,
49 void (*cleanup_fn)(void), char *path);
Garrett Cooper1c2939f2010-12-20 05:12:53 -080050#define SAFE_DIRNAME(cleanup_fn, path) \
51 safe_dirname(__FILE__, __LINE__, (cleanup_fn), (path))
52
Garrett Cooperca435922010-12-20 12:26:32 -080053char* safe_getcwd(const char *file, const int lineno,
54 void (*cleanup_fn)(void), char *buf, size_t size);
55#define SAFE_GETCWD(cleanup_fn, buf, size) \
56 safe_getcwd(__FILE__, __LINE__, (cleanup_fn), (buf), (size))
57
58struct passwd* safe_getpwnam(const char *file, const int lineno,
59 void (*cleanup_fn)(void), const char *name);
60#define SAFE_GETPWNAM(cleanup_fn, name) \
61 safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name))
62
Caspar Zhang817c7822011-06-30 01:50:33 +080063int safe_getrusage(const char *file, const int lineno,
64 void (*cleanup_fn)(void), int who, struct rusage *usage);
65#define SAFE_GETRUSAGE(cleanup_fn, who, usage) \
66 safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage))
67
Garrett Cooperca435922010-12-20 12:26:32 -080068void* safe_malloc(const char *file, const int lineno,
69 void (*cleanup_fn)(void), size_t size);
70#define SAFE_MALLOC(cleanup_fn, size) \
71 safe_malloc(__FILE__, __LINE__, (cleanup_fn), (size))
72
Garrett Cooper4a3c6582010-12-21 07:07:01 -080073int safe_mkdir(const char *file, const int lineno,
74 void (*cleanup_fn)(void), const char *pathname, mode_t mode);
75#define SAFE_MKDIR(cleanup_fn, pathname, mode) \
76 safe_mkdir(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode))
77
Garrett Cooperca435922010-12-20 12:26:32 -080078void* safe_mmap(const char *file, const int lineno,
79 void (*cleanup_fn)(void), void *addr, size_t length, int prot,
80 int flags, int fd, off_t offset);
81#define SAFE_MMAP(cleanup_fn, addr, length, prot, flags, fd, offset) \
82 safe_mmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length), (prot), \
83 (flags), (fd), (offset))
84
85int safe_munmap(const char *file, const int lineno,
86 void (*cleanup_fn)(void), void *addr, size_t length);
87#define SAFE_MUNMAP(cleanup_fn, addr, length) \
88 safe_munmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length))
89
Garrett Cooperc6743962010-12-20 05:38:46 -080090int safe_open(const char *file, const int lineno,
91 void (*cleanup_fn)(void), const char *pathname, int oflags, ...);
Garrett Cooper1c2939f2010-12-20 05:12:53 -080092#define SAFE_OPEN(cleanup_fn, pathname, oflags, ...) \
93 safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \
94 ##__VA_ARGS__)
95
Garrett Cooperc6743962010-12-20 05:38:46 -080096int safe_pipe(const char *file, const int lineno,
97 void (*cleanup_fn)(void), int fildes[2]);
Garrett Cooper1c2939f2010-12-20 05:12:53 -080098#define SAFE_PIPE(cleanup_fn, fildes) \
99 safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes))
100
Garrett Cooperc6743962010-12-20 05:38:46 -0800101ssize_t safe_read(const char *file, const int lineno,
102 void (*cleanup_fn)(void), char len_strict, int fildes, void *buf,
103 size_t nbyte);
Garrett Cooper1c2939f2010-12-20 05:12:53 -0800104#define SAFE_READ(cleanup_fn, len_strict, fildes, buf, nbyte) \
105 safe_read(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
106 (buf), (nbyte))
107
Garrett Cooper400c8362010-12-20 20:02:01 -0800108int safe_setegid(const char *file, const int lineno,
109 void (*cleanup_fn)(void), gid_t egid);
110#define SAFE_SETEGID(cleanup_fn, egid) \
111 safe_setegid(__FILE__, __LINE__, cleanup_fn, (egid))
112
113int safe_seteuid(const char *file, const int lineno,
114 void (*cleanup_fn)(void), uid_t euid);
115#define SAFE_SETEUID(cleanup_fn, euid) \
116 safe_seteuid(__FILE__, __LINE__, cleanup_fn, (euid))
117
Garrett Cooperca435922010-12-20 12:26:32 -0800118int safe_setgid(const char *file, const int lineno,
119 void (*cleanup_fn)(void), gid_t gid);
120#define SAFE_SETGID(cleanup_fn, gid) \
121 safe_setgid(__FILE__, __LINE__, cleanup_fn, (gid))
122
123int safe_setuid(const char *file, const int lineno,
124 void (*cleanup_fn)(void), uid_t uid);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200125#define SAFE_SETUID(cleanup_fn, uid) \
Garrett Cooperca435922010-12-20 12:26:32 -0800126 safe_setuid(__FILE__, __LINE__, cleanup_fn, (uid))
127
Garrett Cooperc6743962010-12-20 05:38:46 -0800128int safe_unlink(const char *file, const int lineno,
129 void (*cleanup_fn)(void), const char *pathname);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200130#define SAFE_UNLINK(cleanup_fn, pathname) \
Garrett Cooper1c2939f2010-12-20 05:12:53 -0800131 safe_unlink(__FILE__, __LINE__, cleanup_fn, (pathname))
132
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200133int safe_link(const char *file, const int lineno,
134 void (cleanup_fn)(void), const char *oldpath,
135 const char *newpath);
136#define SAFE_LINK(cleanup_fn, oldpath, newpath) \
137 safe_link(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
138
139int safe_symlink(const char *file, const int lineno,
140 void (cleanup_fn)(void), const char *oldpath,
141 const char *newpath);
142#define SAFE_SYMLINK(cleanup_fn, oldpath, newpath) \
143 safe_symlink(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
144
Garrett Cooperc6743962010-12-20 05:38:46 -0800145ssize_t safe_write(const char *file, const int lineno,
146 void (cleanup_fn)(void), char len_strict, int fildes,
147 const void *buf, size_t nbyte);
Garrett Cooper1c2939f2010-12-20 05:12:53 -0800148#define SAFE_WRITE(cleanup_fn, len_strict, fildes, buf, nbyte) \
149 safe_write(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
150 (buf), (nbyte))
151
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100152int safe_ftruncate(const char *file, const int lineno,
153 void (cleanup_fn)(void), int fd, off_t length);
154#define SAFE_FTRUNCATE(cleanup_fn, fd, length) \
155 safe_ftruncate(__FILE__, __LINE__, cleanup_fn, (fd), (length))
156
157int safe_truncate(const char *file, const int lineno,
158 void (cleanup_fn)(void), const char *path, off_t length);
159#define SAFE_TRUNCATE(cleanup_fn, fd, length) \
160 safe_truncate(__FILE__, __LINE__, cleanup_fn, (path), (length))
161
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800162long safe_strtol(const char *file, const int lineno,
163 void (cleanup_fn)(void), char *str, long min, long max);
164#define SAFE_STRTOL(cleanup_fn, str, min, max) \
165 safe_strtol(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
166
Zhouping Liu2b73a152012-07-07 23:14:39 +0800167unsigned long safe_strtoul(const char *file, const int lineno, void (cleanup_fn)(void),
168 char *str, unsigned long min, unsigned long max);
169#define SAFE_STRTOUL(cleanup_fn, str, min, max) \
170 safe_strtoul(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
171
Wanlong Gao3b535a82012-10-18 16:35:14 +0800172long safe_sysconf(const char *file, const int lineno,
173 void (cleanup_fn)(void), int name);
174#define SAFE_SYSCONF(cleanup_fn, name) \
Cyril Hrubis75cb9112013-04-15 18:56:36 +0200175 safe_sysconf(__FILE__, __LINE__, cleanup_fn, name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800176
Cyril Hrubisd2085ca2013-06-18 14:47:19 +0200177FILE *safe_fopen(const char *file, const int lineno, void (cleanup_fn)(void),
178 const char *path, const char *mode);
179#define SAFE_FOPEN(cleanup_fn, path, mode) \
180 safe_fopen(__FILE__, __LINE__, cleanup_fn, path, mode)
181
182int safe_fclose(const char *file, const int lineno, void (cleanup_fn)(void),
183 FILE *f);
184#define SAFE_FCLOSE(cleanup_fn, f) \
185 safe_fclose(__FILE__, __LINE__, cleanup_fn, f)
186
Cyril Hrubis75cb9112013-04-15 18:56:36 +0200187#endif /* __SAFE_MACROS_H__ */
188#endif /* __TEST_H__ */