blob: 7bc00a090941187467ce1f47354e2ce2cb0df286 [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>
Vinson Lee9af0bd72012-02-09 15:05:50 -08007#include <errno.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08008#include <fcntl.h>
9#include <libgen.h>
Caspar Zhangd6a1f252012-02-09 15:58:28 +080010#include <limits.h>
Garrett Cooperca435922010-12-20 12:26:32 -080011#include <pwd.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080012#include <stdarg.h>
Garrett Cooperca435922010-12-20 12:26:32 -080013#include <stdlib.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080014#include <unistd.h>
15#include "test.h"
Garrett Cooperca435922010-12-20 12:26:32 -080016#include "safe_macros.h"
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080017
Wanlong Gao354ebb42012-12-07 10:10:04 +080018char *safe_basename(const char *file, const int lineno,
19 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080020{
21 char *rval;
22
23 rval = basename(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020024 if (rval == NULL) {
25 tst_brkm(TBROK | TERRNO, cleanup_fn,
26 "%s:%d: basename(%s) failed",
27 file, lineno, path);
28 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080029
Mats Liljegren49e86152014-04-16 19:27:58 +020030 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080031}
32
33int
Wanlong Gao354ebb42012-12-07 10:10:04 +080034safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void),
35 const char *path)
Garrett Cooper4a3c6582010-12-21 07:07:01 -080036{
37 int rval;
38
39 rval = chdir(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020040 if (rval == -1) {
41 tst_brkm(TBROK | TERRNO, cleanup_fn,
42 "%s:%d: chdir(%s) failed",
43 file, lineno, path);
44 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -080045
Mats Liljegren49e86152014-04-16 19:27:58 +020046 return rval;
Garrett Cooper4a3c6582010-12-21 07:07:01 -080047}
48
49int
Wanlong Gao354ebb42012-12-07 10:10:04 +080050safe_close(const char *file, const int lineno, void (*cleanup_fn) (void),
51 int fildes)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080052{
53 int rval;
54
55 rval = close(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +020056 if (rval == -1) {
57 tst_brkm(TBROK | TERRNO, cleanup_fn,
58 "%s:%d: close(%d) failed",
59 file, lineno, fildes);
60 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080061
Mats Liljegren49e86152014-04-16 19:27:58 +020062 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080063}
64
65int
Wanlong Gao354ebb42012-12-07 10:10:04 +080066safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void),
67 char *pathname, mode_t mode)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080068{
69 int rval;
70
71 rval = creat(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +020072 if (rval == -1) {
73 tst_brkm(TBROK | TERRNO, cleanup_fn,
74 "%s:%d: creat(%s,0%o) failed",
75 file, lineno, pathname, mode);
76 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080077
Mats Liljegren49e86152014-04-16 19:27:58 +020078 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080079}
80
Wanlong Gao354ebb42012-12-07 10:10:04 +080081char *safe_dirname(const char *file, const int lineno,
82 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080083{
84 char *rval;
85
86 rval = dirname(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020087 if (rval == NULL) {
88 tst_brkm(TBROK | TERRNO, cleanup_fn,
89 "%s:%d: dirname(%s) failed",
90 file, lineno, path);
91 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080092
Mats Liljegren49e86152014-04-16 19:27:58 +020093 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080094}
95
Wanlong Gao354ebb42012-12-07 10:10:04 +080096char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
97 char *buf, size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -080098{
99 char *rval;
100
101 rval = getcwd(buf, size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200102 if (rval == NULL) {
103 tst_brkm(TBROK | TERRNO, cleanup_fn,
104 "%s:%d: getcwd(%p,%zu) failed",
105 file, lineno, buf, size);
106 }
Garrett Cooperca435922010-12-20 12:26:32 -0800107
Mats Liljegren49e86152014-04-16 19:27:58 +0200108 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800109}
110
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111struct passwd *safe_getpwnam(const char *file, const int lineno,
112 void (*cleanup_fn) (void), const char *name)
Garrett Cooperca435922010-12-20 12:26:32 -0800113{
114 struct passwd *rval;
115
116 rval = getpwnam(name);
Mats Liljegren49e86152014-04-16 19:27:58 +0200117 if (rval == NULL) {
118 tst_brkm(TBROK | TERRNO, cleanup_fn,
119 "%s:%d: getpwnam(%s) failed",
120 file, lineno, name);
121 }
Garrett Cooperca435922010-12-20 12:26:32 -0800122
Mats Liljegren49e86152014-04-16 19:27:58 +0200123 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800124}
125
Caspar Zhang817c7822011-06-30 01:50:33 +0800126int
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
128 int who, struct rusage *usage)
Caspar Zhang817c7822011-06-30 01:50:33 +0800129{
130 int rval;
131
132 rval = getrusage(who, usage);
Mats Liljegren49e86152014-04-16 19:27:58 +0200133 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200135 "%s:%d: getrusage(%d,%p) failed",
136 file, lineno, who, usage);
137 }
Caspar Zhang817c7822011-06-30 01:50:33 +0800138
139 return rval;
140}
141
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void),
143 size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800144{
145 void *rval;
146
147 rval = malloc(size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200148 if (rval == NULL) {
149 tst_brkm(TBROK | TERRNO, cleanup_fn,
150 "%s:%d: malloc(%zu) failed",
151 file, lineno, size);
152 }
Garrett Cooperca435922010-12-20 12:26:32 -0800153
Mats Liljegren49e86152014-04-16 19:27:58 +0200154 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800155}
156
Mats Liljegren49e86152014-04-16 19:27:58 +0200157int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void),
158 const char *pathname, mode_t mode)
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800159{
160 int rval;
161
162 rval = mkdir(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200163 if (rval == -1) {
164 tst_brkm(TBROK | TERRNO, cleanup_fn,
165 "%s:%d: mkdir(%s,0%o) failed",
166 file, lineno, pathname, mode);
167 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800168
169 return (rval);
170}
171
Cyril Hrubis97499c22014-05-07 14:54:22 +0200172int safe_rmdir(const char *file, const int lineno, void (*cleanup_fn) (void),
173 const char *pathname)
174{
175 int rval;
176
177 rval = rmdir(pathname);
178 if (rval == -1) {
179 tst_brkm(TBROK | TERRNO, cleanup_fn,
180 "%s:%d: rmdir(%s) failed",
181 file, lineno, pathname);
182 }
183
184 return (rval);
185}
186
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187void *safe_mmap(const char *file, const int lineno, void (*cleanup_fn) (void),
188 void *addr, size_t length, int prot, int flags, int fd,
189 off_t offset)
Garrett Cooperca435922010-12-20 12:26:32 -0800190{
191 void *rval;
192
193 rval = mmap(addr, length, prot, flags, fd, offset);
Mats Liljegren49e86152014-04-16 19:27:58 +0200194 if (rval == MAP_FAILED) {
195 tst_brkm(TBROK | TERRNO, cleanup_fn,
196 "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed",
197 file, lineno, addr, length, prot, flags, fd,
198 (long) offset);
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_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
205 void *addr, size_t length)
Garrett Cooperca435922010-12-20 12:26:32 -0800206{
207 int rval;
208
209 rval = munmap(addr, length);
Mats Liljegren49e86152014-04-16 19:27:58 +0200210 if (rval == -1) {
211 tst_brkm(TBROK | TERRNO, cleanup_fn,
212 "%s:%d: munmap(%p,%zu) failed",
213 file, lineno, addr, length);
214 }
Garrett Cooperca435922010-12-20 12:26:32 -0800215
Mats Liljegren49e86152014-04-16 19:27:58 +0200216 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800217}
218
Mats Liljegren49e86152014-04-16 19:27:58 +0200219int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
220 const char *pathname, int oflags, ...)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800221{
222 va_list ap;
223 int rval;
224 mode_t mode;
225
226 va_start(ap, oflags);
227 mode = va_arg(ap, mode_t);
228 va_end(ap);
229
230 rval = open(pathname, oflags, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200231 if (rval == -1) {
232 tst_brkm(TBROK | TERRNO, cleanup_fn,
233 "%s:%d: open(%s,%d,0%o) failed",
234 file, lineno, pathname, oflags, mode);
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 +0200240int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
241 int fildes[2])
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800242{
243 int rval;
244
245 rval = pipe(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +0200246 if (rval == -1) {
247 tst_brkm(TBROK | TERRNO, cleanup_fn,
248 "%s:%d: pipe({%d,%d}) failed",
249 file, lineno, fildes[0], fildes[1]);
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
Mats Liljegren49e86152014-04-16 19:27:58 +0200255ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
256 char len_strict, int fildes, void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800257{
258 ssize_t rval;
259
260 rval = read(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200261 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
262 tst_brkm(TBROK | TERRNO, cleanup_fn,
263 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
264 file, lineno, fildes, buf, nbyte, rval);
265 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800266
Mats Liljegren49e86152014-04-16 19:27:58 +0200267 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800268}
269
Mats Liljegren49e86152014-04-16 19:27:58 +0200270int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
271 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800272{
273 int rval;
274
275 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200276 if (rval == -1) {
277 tst_brkm(TBROK | TERRNO, cleanup_fn,
278 "%s:%d: setegid(%u) failed",
279 file, lineno, (unsigned) egid);
280 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800281
Mats Liljegren49e86152014-04-16 19:27:58 +0200282 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800283}
284
Mats Liljegren49e86152014-04-16 19:27:58 +0200285int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
286 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800287{
288 int rval;
289
290 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200291 if (rval == -1) {
292 tst_brkm(TBROK | TERRNO, cleanup_fn,
293 "%s:%d: seteuid(%u) failed",
294 file, lineno, (unsigned) euid);
295 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800296
Mats Liljegren49e86152014-04-16 19:27:58 +0200297 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800298}
299
Mats Liljegren49e86152014-04-16 19:27:58 +0200300int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
301 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800302{
303 int rval;
304
305 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200306 if (rval == -1) {
307 tst_brkm(TBROK | TERRNO, cleanup_fn,
308 "%s:%d: setgid(%u) failed",
309 file, lineno, (unsigned) gid);
310 }
Garrett Cooperca435922010-12-20 12:26:32 -0800311
Mats Liljegren49e86152014-04-16 19:27:58 +0200312 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800313}
314
Mats Liljegren49e86152014-04-16 19:27:58 +0200315int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
316 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800317{
318 int rval;
319
320 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200321 if (rval == -1) {
322 tst_brkm(TBROK | TERRNO, cleanup_fn,
323 "%s:%d: setuid(%u) failed",
324 file, lineno, (unsigned) uid);
325 }
Garrett Cooperca435922010-12-20 12:26:32 -0800326
Mats Liljegren49e86152014-04-16 19:27:58 +0200327 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800328}
329
Zeng Linggang91d67422014-04-25 10:46:32 +0800330int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void),
331 uid_t *ruid, uid_t *euid, uid_t *suid)
332{
333 int rval;
334
335 rval = getresuid(ruid, euid, suid);
336 if (rval == -1) {
337 tst_brkm(TBROK | TERRNO, cleanup_fn,
338 "%s:%d: getresuid(%p, %p, %p) failed",
339 file, lineno, ruid, euid, suid);
340 }
341
342 return rval;
343}
344
345int safe_getresgid(const char *file, const int lineno, void (*cleanup_fn)(void),
346 gid_t *rgid, gid_t *egid, gid_t *sgid)
347{
348 int rval;
349
350 rval = getresgid(rgid, egid, sgid);
351 if (rval == -1) {
352 tst_brkm(TBROK | TERRNO, cleanup_fn,
353 "%s:%d: getresgid(%p, %p, %p) failed",
354 file, lineno, rgid, egid, sgid);
355 }
356
357 return rval;
358}
359
Mats Liljegren49e86152014-04-16 19:27:58 +0200360int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
361 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800362{
363 int rval;
364
365 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200366 if (rval == -1) {
367 tst_brkm(TBROK | TERRNO, cleanup_fn,
368 "%s:%d: unlink(%s) failed",
369 file, lineno, pathname);
370 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800371
Mats Liljegren49e86152014-04-16 19:27:58 +0200372 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800373}
374
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200375
376int safe_link(const char *file, const int lineno,
377 void (cleanup_fn)(void), const char *oldpath,
378 const char *newpath)
379{
380 int rval;
381
382 rval = link(oldpath, newpath);
383
384 if (rval == -1) {
385 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200386 "%s:%d: link(%s,%s) failed",
387 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200388 }
389
390 return rval;
391}
392
Cyril Hrubisf095ccb2013-11-27 19:55:59 +0100393off_t safe_lseek(const char *file, const int lineno,
394 void (cleanup_fn)(void), int fd,
395 off_t offset, int whence)
396{
397 off_t rval;
398
399 rval = lseek(fd, offset, whence);
400
401 if (rval == (off_t) -1) {
402 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200403 "%s:%d: lseek(%d,%ld,%d) failed",
404 file, lineno, fd, (long)offset, whence);
Cyril Hrubisf095ccb2013-11-27 19:55:59 +0100405 }
406
407 return rval;
408}
409
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200410int safe_symlink(const char *file, const int lineno,
411 void (cleanup_fn)(void), const char *oldpath,
412 const char *newpath)
413{
414 int rval;
415
416 rval = symlink(oldpath, newpath);
417
418 if (rval == -1) {
419 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200420 "%s:%d: symlink(%s,%s) failed",
421 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200422 }
423
424 return rval;
425}
426
Mats Liljegren49e86152014-04-16 19:27:58 +0200427ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
428 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800429{
430 ssize_t rval;
431
432 rval = write(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200433 if ((len_strict == 0 && rval == -1) || (size_t)rval != nbyte) {
434 tst_brkm(TBROK | TERRNO, cleanup_fn,
435 "%s:%d: write(%d,%p,%zu) failed",
436 file, lineno, fildes, buf, rval);
437 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800438
Mats Liljegren49e86152014-04-16 19:27:58 +0200439 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800440}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100441
442int safe_ftruncate(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800443 void (cleanup_fn) (void), int fd, off_t length)
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100444{
445 int rval;
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800446
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100447 rval = ftruncate(fd, length);
448 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800449 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200450 "%s:%d: ftruncate(%d,%ld) failed",
451 file, lineno, fd, (long)length);
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100452 }
453
454 return rval;
455}
456
457int safe_truncate(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800458 void (cleanup_fn) (void), const char *path, off_t length)
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100459{
460 int rval;
461
462 rval = truncate(path, length);
463 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200464 tst_brkm(TBROK | TERRNO, cleanup_fn,
465 "%s:%d: truncate(%s,%ld) failed",
466 file, lineno, path, (long)length);
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100467 }
468
469 return rval;
470}
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800471
472long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800473 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800474{
475 long rval;
476 char *endptr;
477
478 errno = 0;
479 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200480
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800481 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
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: strtol(%s) failed", file, lineno, str);
485 }
486
487 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800488 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200489 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
490 }
491
492 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800493 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200494 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
495 file, lineno, str, rval, min, max);
496 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800497
498 return rval;
499}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800500
Wanlong Gao354ebb42012-12-07 10:10:04 +0800501unsigned long safe_strtoul(const char *file, const int lineno,
502 void (cleanup_fn) (void), char *str,
503 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800504{
505 unsigned long rval;
506 char *endptr;
507
508 errno = 0;
509 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200510
Zhouping Liu2b73a152012-07-07 23:14:39 +0800511 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200512 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800513 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200514 "%s:%d: strtoul(%s) failed", file, lineno, str);
515 }
516
517 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800518 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200519 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
520 file, lineno, str, rval, min, max);
521 }
522
523 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800524 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800525 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200526 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800527
528 return rval;
529}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800530
531long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800532 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800533{
534 long rval;
535 errno = 0;
536
537 rval = sysconf(name);
538
539 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200540 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800541 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200542 "%s:%d: sysconf(%d) failed",
543 file, lineno, name);
544 } else {
545 tst_resm(TINFO, "%s:%d: sysconf(%d): "
546 "queried option is not available"
547 " or there is no definite limit",
548 file, lineno, name);
549 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800550 }
551
552 return rval;
553}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800554
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100555int safe_stat(const char *file, const int lineno,
556 void (cleanup_fn)(void), const char *path, struct stat *buf)
557{
558 int rval;
559
560 rval = stat(path, buf);
561
562 if (rval == -1) {
563 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200564 "%s:%d: stat(%s,%p) failed", file, lineno, path, buf);
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100565 }
566
567 return rval;
568}
569
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800570int safe_fstat(const char *file, const int lineno,
571 void (cleanup_fn)(void), int fd, struct stat *buf)
572{
573 int rval;
574
575 rval = fstat(fd, buf);
576
577 if (rval == -1) {
578 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200579 "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf);
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100580 }
581
582 return rval;
583}
584
585int safe_lstat(const char *file, const int lineno,
586 void (cleanup_fn)(void), const char *path, struct stat *buf)
587{
588 int rval;
589
590 rval = lstat(path, buf);
591
592 if (rval == -1) {
593 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200594 "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf);
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800595 }
596
597 return rval;
598}
Zeng Linggangef9aac62014-03-03 19:22:55 +0800599
600int safe_getrlimit(const char *file, const int lineno,
601 void (cleanup_fn)(void), int resource, struct rlimit *rlim)
602{
603 int rval;
604
605 rval = getrlimit(resource, rlim);
606
607 if (rval == -1) {
608 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200609 "%s:%d: getrlimit(%d,%p) failed",
610 file, lineno, resource, rlim);
Zeng Linggangef9aac62014-03-03 19:22:55 +0800611 }
612
613 return rval;
614}
615
616int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
617 int resource, const struct rlimit *rlim)
618{
619 int rval;
620
621 rval = setrlimit(resource, rlim);
622
623 if (rval == -1) {
624 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200625 "%s:%d: setrlimit(%d,%p) failed",
626 file, lineno, resource, rlim);
Zeng Linggangef9aac62014-03-03 19:22:55 +0800627 }
628
629 return rval;
630}
Cyril Hrubisc4592352014-03-04 16:41:02 +0100631
632int safe_chmod(const char *file, const int lineno,
633 void (cleanup_fn)(void), const char *path, mode_t mode)
634{
635 int rval;
636
637 rval = chmod(path, mode);
638
639 if (rval == -1) {
640 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200641 "%s:%d: chmod(%s,0%o) failed",
642 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100643 }
644
645 return rval;
646}
647
648int safe_fchmod(const char *file, const int lineno,
649 void (cleanup_fn)(void), int fd, mode_t mode)
650{
651 int rval;
652
653 rval = fchmod(fd, mode);
654
655 if (rval == -1) {
656 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200657 "%s:%d: fchmod(%d,0%o) failed",
658 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100659 }
660
661 return rval;
662}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200663
664pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
665 int *status)
666{
667 pid_t rval;
668
669 rval = wait(status);
670 if (rval == -1) {
671 tst_brkm(TBROK | TERRNO, cleanup_fn,
672 "%s:%d: wait(%p) failed",
673 file, lineno, status);
674 }
675
676 return rval;
677}
678
679pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
680 pid_t pid, int *status, int opts)
681{
682 pid_t rval;
683
684 rval = waitpid(pid, status, opts);
685 if (rval == -1) {
686 tst_brkm(TBROK | TERRNO, cleanup_fn,
687 "%s:%d: waitpid(%d,%p,%d) failed",
688 file, lineno, pid, status, opts);
689 }
690
691 return rval;
692}