blob: 0d805e9996a2a8533cfefa3710e6228cd33bb38c [file] [log] [blame]
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08001#include <sys/types.h>
Garrett Cooperca435922010-12-20 12:26:32 -08002#include <sys/mman.h>
Caspar Zhang817c7822011-06-30 01:50:33 +08003#include <sys/resource.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08004#include <sys/stat.h>
Vinson Lee9af0bd72012-02-09 15:05:50 -08005#include <errno.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08006#include <fcntl.h>
7#include <libgen.h>
Caspar Zhangd6a1f252012-02-09 15:58:28 +08008#include <limits.h>
Garrett Cooperca435922010-12-20 12:26:32 -08009#include <pwd.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080010#include <stdarg.h>
Garrett Cooperca435922010-12-20 12:26:32 -080011#include <stdlib.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080012#include <unistd.h>
13#include "test.h"
Garrett Cooperca435922010-12-20 12:26:32 -080014#include "safe_macros.h"
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080015
Wanlong Gao354ebb42012-12-07 10:10:04 +080016char *safe_basename(const char *file, const int lineno,
17 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080018{
19 char *rval;
20
21 rval = basename(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020022 if (rval == NULL) {
23 tst_brkm(TBROK | TERRNO, cleanup_fn,
24 "%s:%d: basename(%s) failed",
25 file, lineno, path);
26 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080027
Mats Liljegren49e86152014-04-16 19:27:58 +020028 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080029}
30
31int
Wanlong Gao354ebb42012-12-07 10:10:04 +080032safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void),
33 const char *path)
Garrett Cooper4a3c6582010-12-21 07:07:01 -080034{
35 int rval;
36
37 rval = chdir(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020038 if (rval == -1) {
39 tst_brkm(TBROK | TERRNO, cleanup_fn,
40 "%s:%d: chdir(%s) failed",
41 file, lineno, path);
42 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -080043
Mats Liljegren49e86152014-04-16 19:27:58 +020044 return rval;
Garrett Cooper4a3c6582010-12-21 07:07:01 -080045}
46
47int
Wanlong Gao354ebb42012-12-07 10:10:04 +080048safe_close(const char *file, const int lineno, void (*cleanup_fn) (void),
49 int fildes)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080050{
51 int rval;
52
53 rval = close(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +020054 if (rval == -1) {
55 tst_brkm(TBROK | TERRNO, cleanup_fn,
56 "%s:%d: close(%d) failed",
57 file, lineno, fildes);
58 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080059
Mats Liljegren49e86152014-04-16 19:27:58 +020060 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080061}
62
63int
Wanlong Gao354ebb42012-12-07 10:10:04 +080064safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void),
65 char *pathname, mode_t mode)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080066{
67 int rval;
68
69 rval = creat(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +020070 if (rval == -1) {
71 tst_brkm(TBROK | TERRNO, cleanup_fn,
72 "%s:%d: creat(%s,0%o) failed",
73 file, lineno, pathname, mode);
74 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080075
Mats Liljegren49e86152014-04-16 19:27:58 +020076 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080077}
78
Wanlong Gao354ebb42012-12-07 10:10:04 +080079char *safe_dirname(const char *file, const int lineno,
80 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080081{
82 char *rval;
83
84 rval = dirname(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020085 if (rval == NULL) {
86 tst_brkm(TBROK | TERRNO, cleanup_fn,
87 "%s:%d: dirname(%s) failed",
88 file, lineno, path);
89 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080090
Mats Liljegren49e86152014-04-16 19:27:58 +020091 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080092}
93
Wanlong Gao354ebb42012-12-07 10:10:04 +080094char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
95 char *buf, size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -080096{
97 char *rval;
98
99 rval = getcwd(buf, size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200100 if (rval == NULL) {
101 tst_brkm(TBROK | TERRNO, cleanup_fn,
102 "%s:%d: getcwd(%p,%zu) failed",
103 file, lineno, buf, size);
104 }
Garrett Cooperca435922010-12-20 12:26:32 -0800105
Mats Liljegren49e86152014-04-16 19:27:58 +0200106 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800107}
108
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109struct passwd *safe_getpwnam(const char *file, const int lineno,
110 void (*cleanup_fn) (void), const char *name)
Garrett Cooperca435922010-12-20 12:26:32 -0800111{
112 struct passwd *rval;
113
114 rval = getpwnam(name);
Mats Liljegren49e86152014-04-16 19:27:58 +0200115 if (rval == NULL) {
116 tst_brkm(TBROK | TERRNO, cleanup_fn,
117 "%s:%d: getpwnam(%s) failed",
118 file, lineno, name);
119 }
Garrett Cooperca435922010-12-20 12:26:32 -0800120
Mats Liljegren49e86152014-04-16 19:27:58 +0200121 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800122}
123
Caspar Zhang817c7822011-06-30 01:50:33 +0800124int
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
126 int who, struct rusage *usage)
Caspar Zhang817c7822011-06-30 01:50:33 +0800127{
128 int rval;
129
130 rval = getrusage(who, usage);
Mats Liljegren49e86152014-04-16 19:27:58 +0200131 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200133 "%s:%d: getrusage(%d,%p) failed",
134 file, lineno, who, usage);
135 }
Caspar Zhang817c7822011-06-30 01:50:33 +0800136
137 return rval;
138}
139
Wanlong Gao354ebb42012-12-07 10:10:04 +0800140void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void),
141 size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800142{
143 void *rval;
144
145 rval = malloc(size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200146 if (rval == NULL) {
147 tst_brkm(TBROK | TERRNO, cleanup_fn,
148 "%s:%d: malloc(%zu) failed",
149 file, lineno, size);
150 }
Garrett Cooperca435922010-12-20 12:26:32 -0800151
Mats Liljegren49e86152014-04-16 19:27:58 +0200152 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800153}
154
Mats Liljegren49e86152014-04-16 19:27:58 +0200155int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void),
156 const char *pathname, mode_t mode)
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800157{
158 int rval;
159
160 rval = mkdir(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200161 if (rval == -1) {
162 tst_brkm(TBROK | TERRNO, cleanup_fn,
163 "%s:%d: mkdir(%s,0%o) failed",
164 file, lineno, pathname, mode);
165 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800166
167 return (rval);
168}
169
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170void *safe_mmap(const char *file, const int lineno, void (*cleanup_fn) (void),
171 void *addr, size_t length, int prot, int flags, int fd,
172 off_t offset)
Garrett Cooperca435922010-12-20 12:26:32 -0800173{
174 void *rval;
175
176 rval = mmap(addr, length, prot, flags, fd, offset);
Mats Liljegren49e86152014-04-16 19:27:58 +0200177 if (rval == MAP_FAILED) {
178 tst_brkm(TBROK | TERRNO, cleanup_fn,
179 "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed",
180 file, lineno, addr, length, prot, flags, fd,
181 (long) offset);
182 }
Garrett Cooperca435922010-12-20 12:26:32 -0800183
Mats Liljegren49e86152014-04-16 19:27:58 +0200184 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800185}
186
Mats Liljegren49e86152014-04-16 19:27:58 +0200187int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
188 void *addr, size_t length)
Garrett Cooperca435922010-12-20 12:26:32 -0800189{
190 int rval;
191
192 rval = munmap(addr, length);
Mats Liljegren49e86152014-04-16 19:27:58 +0200193 if (rval == -1) {
194 tst_brkm(TBROK | TERRNO, cleanup_fn,
195 "%s:%d: munmap(%p,%zu) failed",
196 file, lineno, addr, length);
197 }
Garrett Cooperca435922010-12-20 12:26:32 -0800198
Mats Liljegren49e86152014-04-16 19:27:58 +0200199 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800200}
201
Mats Liljegren49e86152014-04-16 19:27:58 +0200202int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
203 const char *pathname, int oflags, ...)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800204{
205 va_list ap;
206 int rval;
207 mode_t mode;
208
209 va_start(ap, oflags);
210 mode = va_arg(ap, mode_t);
211 va_end(ap);
212
213 rval = open(pathname, oflags, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200214 if (rval == -1) {
215 tst_brkm(TBROK | TERRNO, cleanup_fn,
216 "%s:%d: open(%s,%d,0%o) failed",
217 file, lineno, pathname, oflags, mode);
218 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800219
Mats Liljegren49e86152014-04-16 19:27:58 +0200220 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800221}
222
Mats Liljegren49e86152014-04-16 19:27:58 +0200223int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
224 int fildes[2])
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800225{
226 int rval;
227
228 rval = pipe(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +0200229 if (rval == -1) {
230 tst_brkm(TBROK | TERRNO, cleanup_fn,
231 "%s:%d: pipe({%d,%d}) failed",
232 file, lineno, fildes[0], fildes[1]);
233 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800234
Mats Liljegren49e86152014-04-16 19:27:58 +0200235 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800236}
237
Mats Liljegren49e86152014-04-16 19:27:58 +0200238ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
239 char len_strict, int fildes, void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800240{
241 ssize_t rval;
242
243 rval = read(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200244 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
245 tst_brkm(TBROK | TERRNO, cleanup_fn,
246 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
247 file, lineno, fildes, buf, nbyte, rval);
248 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800249
Mats Liljegren49e86152014-04-16 19:27:58 +0200250 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800251}
252
Mats Liljegren49e86152014-04-16 19:27:58 +0200253int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
254 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800255{
256 int rval;
257
258 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200259 if (rval == -1) {
260 tst_brkm(TBROK | TERRNO, cleanup_fn,
261 "%s:%d: setegid(%u) failed",
262 file, lineno, (unsigned) egid);
263 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800264
Mats Liljegren49e86152014-04-16 19:27:58 +0200265 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800266}
267
Mats Liljegren49e86152014-04-16 19:27:58 +0200268int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
269 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800270{
271 int rval;
272
273 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200274 if (rval == -1) {
275 tst_brkm(TBROK | TERRNO, cleanup_fn,
276 "%s:%d: seteuid(%u) failed",
277 file, lineno, (unsigned) euid);
278 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800279
Mats Liljegren49e86152014-04-16 19:27:58 +0200280 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800281}
282
Mats Liljegren49e86152014-04-16 19:27:58 +0200283int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
284 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800285{
286 int rval;
287
288 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200289 if (rval == -1) {
290 tst_brkm(TBROK | TERRNO, cleanup_fn,
291 "%s:%d: setgid(%u) failed",
292 file, lineno, (unsigned) gid);
293 }
Garrett Cooperca435922010-12-20 12:26:32 -0800294
Mats Liljegren49e86152014-04-16 19:27:58 +0200295 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800296}
297
Mats Liljegren49e86152014-04-16 19:27:58 +0200298int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
299 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800300{
301 int rval;
302
303 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200304 if (rval == -1) {
305 tst_brkm(TBROK | TERRNO, cleanup_fn,
306 "%s:%d: setuid(%u) failed",
307 file, lineno, (unsigned) uid);
308 }
Garrett Cooperca435922010-12-20 12:26:32 -0800309
Mats Liljegren49e86152014-04-16 19:27:58 +0200310 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800311}
312
Mats Liljegren49e86152014-04-16 19:27:58 +0200313int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
314 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800315{
316 int rval;
317
318 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200319 if (rval == -1) {
320 tst_brkm(TBROK | TERRNO, cleanup_fn,
321 "%s:%d: unlink(%s) failed",
322 file, lineno, pathname);
323 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800324
Mats Liljegren49e86152014-04-16 19:27:58 +0200325 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800326}
327
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200328
329int safe_link(const char *file, const int lineno,
330 void (cleanup_fn)(void), const char *oldpath,
331 const char *newpath)
332{
333 int rval;
334
335 rval = link(oldpath, newpath);
336
337 if (rval == -1) {
338 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200339 "%s:%d: link(%s,%s) failed",
340 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200341 }
342
343 return rval;
344}
345
Cyril Hrubisf095ccb2013-11-27 19:55:59 +0100346off_t safe_lseek(const char *file, const int lineno,
347 void (cleanup_fn)(void), int fd,
348 off_t offset, int whence)
349{
350 off_t rval;
351
352 rval = lseek(fd, offset, whence);
353
354 if (rval == (off_t) -1) {
355 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200356 "%s:%d: lseek(%d,%ld,%d) failed",
357 file, lineno, fd, (long)offset, whence);
Cyril Hrubisf095ccb2013-11-27 19:55:59 +0100358 }
359
360 return rval;
361}
362
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200363int safe_symlink(const char *file, const int lineno,
364 void (cleanup_fn)(void), const char *oldpath,
365 const char *newpath)
366{
367 int rval;
368
369 rval = symlink(oldpath, newpath);
370
371 if (rval == -1) {
372 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200373 "%s:%d: symlink(%s,%s) failed",
374 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200375 }
376
377 return rval;
378}
379
Mats Liljegren49e86152014-04-16 19:27:58 +0200380ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
381 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800382{
383 ssize_t rval;
384
385 rval = write(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200386 if ((len_strict == 0 && rval == -1) || (size_t)rval != nbyte) {
387 tst_brkm(TBROK | TERRNO, cleanup_fn,
388 "%s:%d: write(%d,%p,%zu) failed",
389 file, lineno, fildes, buf, rval);
390 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800391
Mats Liljegren49e86152014-04-16 19:27:58 +0200392 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800393}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100394
395int safe_ftruncate(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800396 void (cleanup_fn) (void), int fd, off_t length)
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100397{
398 int rval;
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800399
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100400 rval = ftruncate(fd, length);
401 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800402 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200403 "%s:%d: ftruncate(%d,%ld) failed",
404 file, lineno, fd, (long)length);
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100405 }
406
407 return rval;
408}
409
410int safe_truncate(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800411 void (cleanup_fn) (void), const char *path, off_t length)
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100412{
413 int rval;
414
415 rval = truncate(path, length);
416 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200417 tst_brkm(TBROK | TERRNO, cleanup_fn,
418 "%s:%d: truncate(%s,%ld) failed",
419 file, lineno, path, (long)length);
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100420 }
421
422 return rval;
423}
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800424
425long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800426 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800427{
428 long rval;
429 char *endptr;
430
431 errno = 0;
432 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200433
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800434 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200435 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800436 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200437 "%s:%d: strtol(%s) failed", file, lineno, str);
438 }
439
440 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800441 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200442 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
443 }
444
445 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800446 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200447 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
448 file, lineno, str, rval, min, max);
449 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800450
451 return rval;
452}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800453
Wanlong Gao354ebb42012-12-07 10:10:04 +0800454unsigned long safe_strtoul(const char *file, const int lineno,
455 void (cleanup_fn) (void), char *str,
456 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800457{
458 unsigned long rval;
459 char *endptr;
460
461 errno = 0;
462 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200463
Zhouping Liu2b73a152012-07-07 23:14:39 +0800464 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200465 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800466 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200467 "%s:%d: strtoul(%s) failed", file, lineno, str);
468 }
469
470 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800471 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200472 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
473 file, lineno, str, rval, min, max);
474 }
475
476 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800477 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800478 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200479 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800480
481 return rval;
482}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800483
484long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800485 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800486{
487 long rval;
488 errno = 0;
489
490 rval = sysconf(name);
491
492 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200493 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800494 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200495 "%s:%d: sysconf(%d) failed",
496 file, lineno, name);
497 } else {
498 tst_resm(TINFO, "%s:%d: sysconf(%d): "
499 "queried option is not available"
500 " or there is no definite limit",
501 file, lineno, name);
502 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800503 }
504
505 return rval;
506}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800507
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100508int safe_stat(const char *file, const int lineno,
509 void (cleanup_fn)(void), const char *path, struct stat *buf)
510{
511 int rval;
512
513 rval = stat(path, buf);
514
515 if (rval == -1) {
516 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200517 "%s:%d: stat(%s,%p) failed", file, lineno, path, buf);
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100518 }
519
520 return rval;
521}
522
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800523int safe_fstat(const char *file, const int lineno,
524 void (cleanup_fn)(void), int fd, struct stat *buf)
525{
526 int rval;
527
528 rval = fstat(fd, buf);
529
530 if (rval == -1) {
531 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200532 "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf);
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100533 }
534
535 return rval;
536}
537
538int safe_lstat(const char *file, const int lineno,
539 void (cleanup_fn)(void), const char *path, struct stat *buf)
540{
541 int rval;
542
543 rval = lstat(path, buf);
544
545 if (rval == -1) {
546 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200547 "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf);
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800548 }
549
550 return rval;
551}
Zeng Linggangef9aac62014-03-03 19:22:55 +0800552
553int safe_getrlimit(const char *file, const int lineno,
554 void (cleanup_fn)(void), int resource, struct rlimit *rlim)
555{
556 int rval;
557
558 rval = getrlimit(resource, rlim);
559
560 if (rval == -1) {
561 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200562 "%s:%d: getrlimit(%d,%p) failed",
563 file, lineno, resource, rlim);
Zeng Linggangef9aac62014-03-03 19:22:55 +0800564 }
565
566 return rval;
567}
568
569int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
570 int resource, const struct rlimit *rlim)
571{
572 int rval;
573
574 rval = setrlimit(resource, rlim);
575
576 if (rval == -1) {
577 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200578 "%s:%d: setrlimit(%d,%p) failed",
579 file, lineno, resource, rlim);
Zeng Linggangef9aac62014-03-03 19:22:55 +0800580 }
581
582 return rval;
583}
Cyril Hrubisc4592352014-03-04 16:41:02 +0100584
585int safe_chmod(const char *file, const int lineno,
586 void (cleanup_fn)(void), const char *path, mode_t mode)
587{
588 int rval;
589
590 rval = chmod(path, mode);
591
592 if (rval == -1) {
593 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200594 "%s:%d: chmod(%s,0%o) failed",
595 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100596 }
597
598 return rval;
599}
600
601int safe_fchmod(const char *file, const int lineno,
602 void (cleanup_fn)(void), int fd, mode_t mode)
603{
604 int rval;
605
606 rval = fchmod(fd, mode);
607
608 if (rval == -1) {
609 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200610 "%s:%d: fchmod(%d,0%o) failed",
611 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100612 }
613
614 return rval;
615}