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