• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 SAFE_MACROS_FN_H__
13 #define SAFE_MACROS_FN_H__
14 
15 #include <sys/mman.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/resource.h>
19 #include <sys/stat.h>
20 #include <sys/ioctl.h>
21 #include <fcntl.h>
22 #include <libgen.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <dirent.h>
26 
27 char* safe_basename(const char *file, const int lineno,
28                     void (*cleanup_fn)(void), char *path);
29 
30 int safe_chdir(const char *file, const int lineno,
31                void (*cleanup_fn)(void), const char *path);
32 
33 int safe_close(const char *file, const int lineno,
34                void (*cleanup_fn)(void), int fildes);
35 
36 int safe_creat(const char *file, const int lineno,
37                void (*cleanup_fn)(void), const char *pathname, mode_t mode);
38 
39 char* safe_dirname(const char *file, const int lineno,
40                    void (*cleanup_fn)(void), char *path);
41 
42 char* safe_getcwd(const char *file, const int lineno,
43                   void (*cleanup_fn)(void), char *buf, size_t size);
44 
45 struct passwd* safe_getpwnam(const char *file, const int lineno,
46                              void (*cleanup_fn)(void), const char *name);
47 
48 int safe_getrusage(const char *file, const int lineno,
49                    void (*cleanup_fn)(void), int who, struct rusage *usage);
50 
51 void* safe_malloc(const char *file, const int lineno,
52                   void (*cleanup_fn)(void), size_t size);
53 
54 int safe_mkdir(const char *file, const int lineno,
55                void (*cleanup_fn)(void), const char *pathname, mode_t mode);
56 
57 int safe_rmdir(const char *file, const int lineno,
58                void (*cleanup_fn)(void), const char *pathname);
59 
60 
61 int safe_munmap(const char *file, const int lineno,
62                 void (*cleanup_fn)(void), void *addr, size_t length);
63 
64 int safe_open(const char *file, const int lineno,
65               void (*cleanup_fn)(void), const char *pathname, int oflags, ...);
66 
67 int safe_pipe(const char *file, const int lineno,
68               void (*cleanup_fn)(void), int fildes[2]);
69 
70 ssize_t	safe_read(const char *file, const int lineno,
71                   void (*cleanup_fn)(void), char len_strict, int fildes,
72                   void *buf, size_t nbyte);
73 
74 ssize_t safe_pread(const char *file, const int lineno,
75                    void (*cleanup_fn)(void), char len_strict,
76                    int fildes, void *buf, size_t nbyte, off_t offset);
77 
78 int safe_setegid(const char *file, const int lineno,
79                  void (*cleanup_fn)(void), gid_t egid);
80 
81 int safe_seteuid(const char *file, const int lineno,
82                  void (*cleanup_fn)(void), uid_t euid);
83 
84 int safe_setgid(const char *file, const int lineno,
85                 void (*cleanup_fn)(void), gid_t gid);
86 
87 int safe_setuid(const char *file, const int lineno,
88                 void (*cleanup_fn)(void), uid_t uid);
89 
90 int safe_getresuid(const char *file, const int lineno,
91                    void (*cleanup_fn)(void),
92                    uid_t *ruid, uid_t *euid, uid_t *suid);
93 
94 int safe_getresgid(const char *file, const int lineno,
95                    void (*cleanup_fn)(void),
96                    gid_t *rgid, gid_t *egid, gid_t *sgid);
97 
98 int safe_unlink(const char *file, const int lineno,
99                 void (*cleanup_fn)(void), const char *pathname);
100 
101 int safe_link(const char *file, const int lineno,
102               void (cleanup_fn)(void), const char *oldpath,
103               const char *newpath);
104 
105 int safe_linkat(const char *file, const int lineno,
106 		void (cleanup_fn)(void), int olddirfd, const char *oldpath,
107 		int newdirfd, const char *newpath, int flags);
108 
109 ssize_t safe_readlink(const char *file, const int lineno,
110 		  void (cleanup_fn)(void), const char *path,
111 		  char *buf, size_t bufsize);
112 
113 int safe_symlink(const char *file, const int lineno,
114                  void (cleanup_fn)(void), const char *oldpath,
115                  const char *newpath);
116 
117 ssize_t	safe_write(const char *file, const int lineno,
118                    void (cleanup_fn)(void), char len_strict, int fildes,
119                    const void *buf, size_t nbyte);
120 
121 ssize_t safe_pwrite(const char *file, const int lineno,
122                     void (cleanup_fn)(void), char len_strict, int fildes,
123 		    const void *buf, size_t nbyte, off_t offset);
124 
125 long safe_strtol(const char *file, const int lineno,
126                  void (cleanup_fn)(void), char *str, long min, long max);
127 
128 unsigned long safe_strtoul(const char *file, const int lineno,
129                            void (cleanup_fn)(void),
130                            char *str, unsigned long min, unsigned long max);
131 
132 long safe_sysconf(const char *file, const int lineno,
133 		  void (cleanup_fn)(void), int name);
134 
135 int safe_chmod(const char *file, const int lineno, void (cleanup_fn)(void),
136 	       const char *path, mode_t mode);
137 
138 int safe_fchmod(const char *file, const int lineno, void (cleanup_fn)(void),
139 	        int fd, mode_t mode);
140 
141 int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
142                const char *path, uid_t owner, gid_t group);
143 
144 int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
145                 int fd, uid_t owner, gid_t group);
146 
147 pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
148                 int *status);
149 
150 pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
151                    pid_t pid, int *status, int opts);
152 
153 int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
154               pid_t pid, int sig);
155 
156 void *safe_memalign(const char *file, const int lineno,
157 		    void (*cleanup_fn)(void), size_t alignment, size_t size);
158 
159 int safe_mkfifo(const char *file, const int lineno,
160 		void (*cleanup_fn)(void), const char *pathname, mode_t mode);
161 
162 int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
163 		const char *oldpath, const char *newpath);
164 
165 int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
166 	       const char *source, const char *target,
167 	       const char *filesystemtype, unsigned long mountflags,
168 	       const void *data);
169 
170 int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
171 		const char *target);
172 
173 DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
174                   const char *name);
175 
176 int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
177                   DIR *dirp);
178 
179 struct dirent *safe_readdir(const char *file, const int lineno,
180                             void (cleanup_fn)(void),
181                             DIR *dirp);
182 
183 DIR* safe_opendir(const char *file, const int lineno,
184                   void (cleanup_fn)(void),
185                   const char *name);
186 
187 struct dirent *safe_readdir(const char *file, const int lineno,
188                             void (cleanup_fn)(void),
189                             DIR *dirp);
190 
191 int safe_closedir(const char *file, const int lineno,
192                   void (cleanup_fn)(void),
193                   DIR *dirp);
194 
195 #endif /* SAFE_MACROS_FN_H__ */
196