blob: 173d72f4c7e7672f224167e3ced4198107da8d2e [file] [log] [blame]
Zeng Linggang91d67422014-04-25 10:46:32 +08001#define _GNU_SOURCE
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08002#include <sys/types.h>
Garrett Cooperca435922010-12-20 12:26:32 -08003#include <sys/mman.h>
Caspar Zhang817c7822011-06-30 01:50:33 +08004#include <sys/resource.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08005#include <sys/stat.h>
Cyril Hrubisf3e448f2014-04-24 14:24:49 +02006#include <sys/wait.h>
Matus Marhefka8e9db8d2014-08-29 14:22:11 +02007#include <sys/mount.h>
Vinson Lee9af0bd72012-02-09 15:05:50 -08008#include <errno.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08009#include <fcntl.h>
10#include <libgen.h>
Caspar Zhangd6a1f252012-02-09 15:58:28 +080011#include <limits.h>
Garrett Cooperca435922010-12-20 12:26:32 -080012#include <pwd.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080013#include <stdarg.h>
Garrett Cooperca435922010-12-20 12:26:32 -080014#include <stdlib.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080015#include <unistd.h>
Zeng Linggang5ff25ef2014-05-06 15:49:42 +080016#include <malloc.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080017#include "test.h"
Garrett Cooperca435922010-12-20 12:26:32 -080018#include "safe_macros.h"
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080019
Wanlong Gao354ebb42012-12-07 10:10:04 +080020char *safe_basename(const char *file, const int lineno,
21 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080022{
23 char *rval;
24
25 rval = basename(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020026 if (rval == NULL) {
27 tst_brkm(TBROK | TERRNO, cleanup_fn,
28 "%s:%d: basename(%s) failed",
29 file, lineno, path);
30 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080031
Mats Liljegren49e86152014-04-16 19:27:58 +020032 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080033}
34
35int
Wanlong Gao354ebb42012-12-07 10:10:04 +080036safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void),
37 const char *path)
Garrett Cooper4a3c6582010-12-21 07:07:01 -080038{
39 int rval;
40
41 rval = chdir(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020042 if (rval == -1) {
43 tst_brkm(TBROK | TERRNO, cleanup_fn,
44 "%s:%d: chdir(%s) failed",
45 file, lineno, path);
46 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -080047
Mats Liljegren49e86152014-04-16 19:27:58 +020048 return rval;
Garrett Cooper4a3c6582010-12-21 07:07:01 -080049}
50
51int
Wanlong Gao354ebb42012-12-07 10:10:04 +080052safe_close(const char *file, const int lineno, void (*cleanup_fn) (void),
53 int fildes)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080054{
55 int rval;
56
57 rval = close(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +020058 if (rval == -1) {
59 tst_brkm(TBROK | TERRNO, cleanup_fn,
60 "%s:%d: close(%d) failed",
61 file, lineno, fildes);
62 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080063
Mats Liljegren49e86152014-04-16 19:27:58 +020064 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080065}
66
67int
Wanlong Gao354ebb42012-12-07 10:10:04 +080068safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void),
Alexey Kodanev5071db52015-04-15 11:03:29 +030069 const char *pathname, mode_t mode)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080070{
71 int rval;
72
73 rval = creat(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +020074 if (rval == -1) {
75 tst_brkm(TBROK | TERRNO, cleanup_fn,
76 "%s:%d: creat(%s,0%o) failed",
77 file, lineno, pathname, mode);
78 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080079
Mats Liljegren49e86152014-04-16 19:27:58 +020080 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080081}
82
Wanlong Gao354ebb42012-12-07 10:10:04 +080083char *safe_dirname(const char *file, const int lineno,
84 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080085{
86 char *rval;
87
88 rval = dirname(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020089 if (rval == NULL) {
90 tst_brkm(TBROK | TERRNO, cleanup_fn,
91 "%s:%d: dirname(%s) failed",
92 file, lineno, path);
93 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080094
Mats Liljegren49e86152014-04-16 19:27:58 +020095 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080096}
97
Wanlong Gao354ebb42012-12-07 10:10:04 +080098char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
99 char *buf, size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800100{
101 char *rval;
102
103 rval = getcwd(buf, size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200104 if (rval == NULL) {
105 tst_brkm(TBROK | TERRNO, cleanup_fn,
106 "%s:%d: getcwd(%p,%zu) failed",
107 file, lineno, buf, size);
108 }
Garrett Cooperca435922010-12-20 12:26:32 -0800109
Mats Liljegren49e86152014-04-16 19:27:58 +0200110 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800111}
112
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113struct passwd *safe_getpwnam(const char *file, const int lineno,
114 void (*cleanup_fn) (void), const char *name)
Garrett Cooperca435922010-12-20 12:26:32 -0800115{
116 struct passwd *rval;
117
118 rval = getpwnam(name);
Mats Liljegren49e86152014-04-16 19:27:58 +0200119 if (rval == NULL) {
120 tst_brkm(TBROK | TERRNO, cleanup_fn,
121 "%s:%d: getpwnam(%s) failed",
122 file, lineno, name);
123 }
Garrett Cooperca435922010-12-20 12:26:32 -0800124
Mats Liljegren49e86152014-04-16 19:27:58 +0200125 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800126}
127
Caspar Zhang817c7822011-06-30 01:50:33 +0800128int
Wanlong Gao354ebb42012-12-07 10:10:04 +0800129safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
130 int who, struct rusage *usage)
Caspar Zhang817c7822011-06-30 01:50:33 +0800131{
132 int rval;
133
134 rval = getrusage(who, usage);
Mats Liljegren49e86152014-04-16 19:27:58 +0200135 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800136 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200137 "%s:%d: getrusage(%d,%p) failed",
138 file, lineno, who, usage);
139 }
Caspar Zhang817c7822011-06-30 01:50:33 +0800140
141 return rval;
142}
143
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void),
145 size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800146{
147 void *rval;
148
149 rval = malloc(size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200150 if (rval == NULL) {
151 tst_brkm(TBROK | TERRNO, cleanup_fn,
152 "%s:%d: malloc(%zu) failed",
153 file, lineno, size);
154 }
Garrett Cooperca435922010-12-20 12:26:32 -0800155
Mats Liljegren49e86152014-04-16 19:27:58 +0200156 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800157}
158
Mats Liljegren49e86152014-04-16 19:27:58 +0200159int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void),
160 const char *pathname, mode_t mode)
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800161{
162 int rval;
163
164 rval = mkdir(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200165 if (rval == -1) {
166 tst_brkm(TBROK | TERRNO, cleanup_fn,
167 "%s:%d: mkdir(%s,0%o) failed",
168 file, lineno, pathname, mode);
169 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800170
171 return (rval);
172}
173
Cyril Hrubis97499c22014-05-07 14:54:22 +0200174int safe_rmdir(const char *file, const int lineno, void (*cleanup_fn) (void),
175 const char *pathname)
176{
177 int rval;
178
179 rval = rmdir(pathname);
180 if (rval == -1) {
181 tst_brkm(TBROK | TERRNO, cleanup_fn,
182 "%s:%d: rmdir(%s) failed",
183 file, lineno, pathname);
184 }
185
186 return (rval);
187}
188
Mats Liljegren49e86152014-04-16 19:27:58 +0200189int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
190 void *addr, size_t length)
Garrett Cooperca435922010-12-20 12:26:32 -0800191{
192 int rval;
193
194 rval = munmap(addr, length);
Mats Liljegren49e86152014-04-16 19:27:58 +0200195 if (rval == -1) {
196 tst_brkm(TBROK | TERRNO, cleanup_fn,
197 "%s:%d: munmap(%p,%zu) failed",
198 file, lineno, addr, length);
199 }
Garrett Cooperca435922010-12-20 12:26:32 -0800200
Mats Liljegren49e86152014-04-16 19:27:58 +0200201 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800202}
203
Mats Liljegren49e86152014-04-16 19:27:58 +0200204int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
205 const char *pathname, int oflags, ...)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800206{
207 va_list ap;
208 int rval;
209 mode_t mode;
210
211 va_start(ap, oflags);
212 mode = va_arg(ap, mode_t);
213 va_end(ap);
214
215 rval = open(pathname, oflags, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200216 if (rval == -1) {
217 tst_brkm(TBROK | TERRNO, cleanup_fn,
218 "%s:%d: open(%s,%d,0%o) failed",
219 file, lineno, pathname, oflags, mode);
220 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800221
Mats Liljegren49e86152014-04-16 19:27:58 +0200222 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800223}
224
Mats Liljegren49e86152014-04-16 19:27:58 +0200225int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
226 int fildes[2])
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800227{
228 int rval;
229
230 rval = pipe(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +0200231 if (rval == -1) {
232 tst_brkm(TBROK | TERRNO, cleanup_fn,
233 "%s:%d: pipe({%d,%d}) failed",
234 file, lineno, fildes[0], fildes[1]);
235 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800236
Mats Liljegren49e86152014-04-16 19:27:58 +0200237 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800238}
239
Mats Liljegren49e86152014-04-16 19:27:58 +0200240ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
241 char len_strict, int fildes, void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800242{
243 ssize_t rval;
244
245 rval = read(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200246 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
247 tst_brkm(TBROK | TERRNO, cleanup_fn,
248 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
249 file, lineno, fildes, buf, nbyte, rval);
250 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800251
Mats Liljegren49e86152014-04-16 19:27:58 +0200252 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800253}
254
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800255ssize_t safe_pread(const char *file, const int lineno, void (*cleanup_fn)(void),
256 char len_strict, int fildes, void *buf, size_t nbyte,
257 off_t offset)
258{
259 ssize_t rval;
260
261 rval = pread(fildes, buf, nbyte, offset);
262 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
263 tst_brkm(TBROK | TERRNO, cleanup_fn,
264 "%s:%d: read(%d,%p,%zu,%ld) failed, returned %zd",
265 file, lineno, fildes, buf, nbyte, offset, rval);
266 }
267
268 return rval;
269}
270
Mats Liljegren49e86152014-04-16 19:27:58 +0200271int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
272 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800273{
274 int rval;
275
276 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200277 if (rval == -1) {
278 tst_brkm(TBROK | TERRNO, cleanup_fn,
279 "%s:%d: setegid(%u) failed",
280 file, lineno, (unsigned) egid);
281 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800282
Mats Liljegren49e86152014-04-16 19:27:58 +0200283 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800284}
285
Mats Liljegren49e86152014-04-16 19:27:58 +0200286int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
287 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800288{
289 int rval;
290
291 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200292 if (rval == -1) {
293 tst_brkm(TBROK | TERRNO, cleanup_fn,
294 "%s:%d: seteuid(%u) failed",
295 file, lineno, (unsigned) euid);
296 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800297
Mats Liljegren49e86152014-04-16 19:27:58 +0200298 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800299}
300
Mats Liljegren49e86152014-04-16 19:27:58 +0200301int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
302 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800303{
304 int rval;
305
306 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200307 if (rval == -1) {
308 tst_brkm(TBROK | TERRNO, cleanup_fn,
309 "%s:%d: setgid(%u) failed",
310 file, lineno, (unsigned) gid);
311 }
Garrett Cooperca435922010-12-20 12:26:32 -0800312
Mats Liljegren49e86152014-04-16 19:27:58 +0200313 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800314}
315
Mats Liljegren49e86152014-04-16 19:27:58 +0200316int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
317 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800318{
319 int rval;
320
321 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200322 if (rval == -1) {
323 tst_brkm(TBROK | TERRNO, cleanup_fn,
324 "%s:%d: setuid(%u) failed",
325 file, lineno, (unsigned) uid);
326 }
Garrett Cooperca435922010-12-20 12:26:32 -0800327
Mats Liljegren49e86152014-04-16 19:27:58 +0200328 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800329}
330
Zeng Linggang91d67422014-04-25 10:46:32 +0800331int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void),
332 uid_t *ruid, uid_t *euid, uid_t *suid)
333{
334 int rval;
335
336 rval = getresuid(ruid, euid, suid);
337 if (rval == -1) {
338 tst_brkm(TBROK | TERRNO, cleanup_fn,
339 "%s:%d: getresuid(%p, %p, %p) failed",
340 file, lineno, ruid, euid, suid);
341 }
342
343 return rval;
344}
345
346int safe_getresgid(const char *file, const int lineno, void (*cleanup_fn)(void),
347 gid_t *rgid, gid_t *egid, gid_t *sgid)
348{
349 int rval;
350
351 rval = getresgid(rgid, egid, sgid);
352 if (rval == -1) {
353 tst_brkm(TBROK | TERRNO, cleanup_fn,
354 "%s:%d: getresgid(%p, %p, %p) failed",
355 file, lineno, rgid, egid, sgid);
356 }
357
358 return rval;
359}
360
Mats Liljegren49e86152014-04-16 19:27:58 +0200361int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
362 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800363{
364 int rval;
365
366 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200367 if (rval == -1) {
368 tst_brkm(TBROK | TERRNO, cleanup_fn,
369 "%s:%d: unlink(%s) failed",
370 file, lineno, pathname);
371 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800372
Mats Liljegren49e86152014-04-16 19:27:58 +0200373 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800374}
375
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200376
377int safe_link(const char *file, const int lineno,
378 void (cleanup_fn)(void), const char *oldpath,
379 const char *newpath)
380{
381 int rval;
382
383 rval = link(oldpath, newpath);
384
385 if (rval == -1) {
386 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200387 "%s:%d: link(%s,%s) failed",
388 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200389 }
390
391 return rval;
392}
393
394int safe_symlink(const char *file, const int lineno,
395 void (cleanup_fn)(void), const char *oldpath,
396 const char *newpath)
397{
398 int rval;
399
400 rval = symlink(oldpath, newpath);
401
402 if (rval == -1) {
403 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200404 "%s:%d: symlink(%s,%s) failed",
405 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200406 }
407
408 return rval;
409}
410
Mats Liljegren49e86152014-04-16 19:27:58 +0200411ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
412 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800413{
414 ssize_t rval;
415
416 rval = write(fildes, buf, nbyte);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800417 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200418 tst_brkm(TBROK | TERRNO, cleanup_fn,
419 "%s:%d: write(%d,%p,%zu) failed",
420 file, lineno, fildes, buf, rval);
421 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800422
Mats Liljegren49e86152014-04-16 19:27:58 +0200423 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800424}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100425
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800426ssize_t safe_pwrite(const char *file, const int lineno,
427 void (cleanup_fn) (void), char len_strict, int fildes,
428 const void *buf, size_t nbyte, off_t offset)
429{
430 ssize_t rval;
431
432 rval = pwrite(fildes, buf, nbyte, offset);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800433 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800434 tst_brkm(TBROK | TERRNO, cleanup_fn,
435 "%s:%d: pwrite(%d,%p,%zu,%ld) failed",
436 file, lineno, fildes, buf, rval, offset);
437 }
438
439 return rval;
440}
441
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800442long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800443 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800444{
445 long rval;
446 char *endptr;
447
448 errno = 0;
449 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200450
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800451 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200452 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800453 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200454 "%s:%d: strtol(%s) failed", file, lineno, str);
455 }
456
457 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800458 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200459 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
460 }
461
462 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800463 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200464 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
465 file, lineno, str, rval, min, max);
466 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800467
468 return rval;
469}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800470
Wanlong Gao354ebb42012-12-07 10:10:04 +0800471unsigned long safe_strtoul(const char *file, const int lineno,
472 void (cleanup_fn) (void), char *str,
473 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800474{
475 unsigned long rval;
476 char *endptr;
477
478 errno = 0;
479 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200480
Zhouping Liu2b73a152012-07-07 23:14:39 +0800481 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200482 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800483 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200484 "%s:%d: strtoul(%s) failed", file, lineno, str);
485 }
486
487 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800488 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200489 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
490 file, lineno, str, rval, min, max);
491 }
492
493 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800494 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800495 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200496 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800497
498 return rval;
499}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800500
501long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800502 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800503{
504 long rval;
505 errno = 0;
506
507 rval = sysconf(name);
508
509 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200510 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800511 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200512 "%s:%d: sysconf(%d) failed",
513 file, lineno, name);
514 } else {
515 tst_resm(TINFO, "%s:%d: sysconf(%d): "
516 "queried option is not available"
517 " or there is no definite limit",
518 file, lineno, name);
519 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800520 }
521
522 return rval;
523}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800524
Cyril Hrubisc4592352014-03-04 16:41:02 +0100525int safe_chmod(const char *file, const int lineno,
526 void (cleanup_fn)(void), const char *path, mode_t mode)
527{
528 int rval;
529
530 rval = chmod(path, mode);
531
532 if (rval == -1) {
533 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200534 "%s:%d: chmod(%s,0%o) failed",
535 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100536 }
537
538 return rval;
539}
540
541int safe_fchmod(const char *file, const int lineno,
542 void (cleanup_fn)(void), int fd, mode_t mode)
543{
544 int rval;
545
546 rval = fchmod(fd, mode);
547
548 if (rval == -1) {
549 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200550 "%s:%d: fchmod(%d,0%o) failed",
551 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100552 }
553
554 return rval;
555}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200556
Xing Gu92a14592014-06-13 10:53:17 +0800557int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
558 const char *path, uid_t owner, gid_t group)
559{
560 int rval;
561
562 rval = chown(path, owner, group);
563
564 if (rval == -1) {
565 tst_brkm(TBROK | TERRNO, cleanup_fn,
566 "%s:%d: chown(%s,%d,%d) failed",
567 file, lineno, path, owner, group);
568 }
569
570 return rval;
571}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200572
573int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
574 int fd, uid_t owner, gid_t group)
575{
576 int rval;
577
578 rval = fchown(fd, owner, group);
579
580 if (rval == -1) {
581 tst_brkm(TBROK | TERRNO, cleanup_fn,
582 "%s:%d: fchown(%d,%d,%d) failed",
583 file, lineno, fd, owner, group);
584 }
585
586 return rval;
587}
588
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200589pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
590 int *status)
591{
592 pid_t rval;
593
594 rval = wait(status);
595 if (rval == -1) {
596 tst_brkm(TBROK | TERRNO, cleanup_fn,
597 "%s:%d: wait(%p) failed",
598 file, lineno, status);
599 }
600
601 return rval;
602}
603
604pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
605 pid_t pid, int *status, int opts)
606{
607 pid_t rval;
608
609 rval = waitpid(pid, status, opts);
610 if (rval == -1) {
611 tst_brkm(TBROK | TERRNO, cleanup_fn,
612 "%s:%d: waitpid(%d,%p,%d) failed",
613 file, lineno, pid, status, opts);
614 }
615
616 return rval;
617}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800618
619void *safe_memalign(const char *file, const int lineno,
620 void (*cleanup_fn) (void), size_t alignment, size_t size)
621{
622 void *rval;
623
624 rval = memalign(alignment, size);
625 if (rval == NULL)
626 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
627 file, lineno);
628
629 return rval;
630}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800631
632int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
633 pid_t pid, int sig)
634{
635 int rval;
636
637 rval = kill(pid, sig);
638
639 if (rval == -1) {
640 tst_brkm(TBROK | TERRNO, cleanup_fn,
641 "%s:%d: kill(%d,%s) failed",
642 file, lineno, pid, tst_strsig(sig));
643 }
644
645 return rval;
646}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200647
648int safe_mkfifo(const char *file, const int lineno,
649 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
650{
651 int rval;
652
653 rval = mkfifo(pathname, mode);
654
655 if (rval == -1) {
656 tst_brkm(TBROK | TERRNO, cleanup_fn,
657 "%s:%d: mkfifo(%s, 0%o) failed",
658 file, lineno, pathname, mode);
659 }
660
661 return rval;
662}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800663
664int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
665 const char *oldpath, const char *newpath)
666{
667 int rval;
668
669 rval = rename(oldpath, newpath);
670
671 if (rval == -1) {
672 tst_brkm(TBROK | TERRNO, cleanup_fn,
673 "%s:%d: rename(%s, %s) failed",
674 file, lineno, oldpath, newpath);
675 }
676
677 return rval;
678}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200679
680int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
681 const char *source, const char *target,
682 const char *filesystemtype, unsigned long mountflags,
683 const void *data)
684{
685 int rval;
686
687 rval = mount(source, target, filesystemtype, mountflags, data);
688
689 if (rval == -1) {
690 tst_brkm(TBROK | TERRNO, cleanup_fn,
691 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
692 file, lineno, source, target, filesystemtype,
693 mountflags, data);
694 }
695
696 return rval;
697}
698
699int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
700 const char *target)
701{
702 int rval;
703
704 rval = umount(target);
705
706 if (rval == -1) {
707 tst_brkm(TBROK | TERRNO, cleanup_fn,
708 "%s:%d: umount(%s) failed",
709 file, lineno, target);
710 }
711
712 return rval;
713}
Cyril Hrubis9138a012015-02-10 15:44:45 +0100714
715DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
716 const char *name)
717{
718 DIR *rval;
719
720 rval = opendir(name);
721
722 if (!rval) {
723 tst_brkm(TBROK | TERRNO, cleanup_fn,
724 "%s:%d: opendir(%s) failed", file, lineno, name);
725 }
726
727 return rval;
728}
729
730int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
731 DIR *dirp)
732{
733 int rval;
734
735 rval = closedir(dirp);
736
737 if (rval) {
738 tst_brkm(TBROK | TERRNO, cleanup_fn,
739 "%s:%d: closedir(%p) failed", file, lineno, dirp);
740 }
741
742 return rval;
743}
744
745struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
746 DIR *dirp)
747{
748 struct dirent *rval;
749 int err = errno;
750
751 errno = 0;
752 rval = readdir(dirp);
753
754 if (!rval && errno) {
755 tst_brkm(TBROK | TERRNO, cleanup_fn,
756 "%s:%d: readdir(%p) failed", file, lineno, dirp);
757 }
758
759 errno = err;
760 return rval;
761}