blob: fbee716fe79c07c3431602459202c8c05af82aab [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>
Cyril Hrubisf3e448f2014-04-24 14:24:49 +02005#include <sys/wait.h>
Vinson Lee9af0bd72012-02-09 15:05:50 -08006#include <errno.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -08007#include <fcntl.h>
8#include <libgen.h>
Caspar Zhangd6a1f252012-02-09 15:58:28 +08009#include <limits.h>
Garrett Cooperca435922010-12-20 12:26:32 -080010#include <pwd.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080011#include <stdarg.h>
Garrett Cooperca435922010-12-20 12:26:32 -080012#include <stdlib.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080013#include <unistd.h>
14#include "test.h"
Garrett Cooperca435922010-12-20 12:26:32 -080015#include "safe_macros.h"
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080016
Wanlong Gao354ebb42012-12-07 10:10:04 +080017char *safe_basename(const char *file, const int lineno,
18 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080019{
20 char *rval;
21
22 rval = basename(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020023 if (rval == NULL) {
24 tst_brkm(TBROK | TERRNO, cleanup_fn,
25 "%s:%d: basename(%s) failed",
26 file, lineno, path);
27 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080028
Mats Liljegren49e86152014-04-16 19:27:58 +020029 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080030}
31
32int
Wanlong Gao354ebb42012-12-07 10:10:04 +080033safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void),
34 const char *path)
Garrett Cooper4a3c6582010-12-21 07:07:01 -080035{
36 int rval;
37
38 rval = chdir(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020039 if (rval == -1) {
40 tst_brkm(TBROK | TERRNO, cleanup_fn,
41 "%s:%d: chdir(%s) failed",
42 file, lineno, path);
43 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -080044
Mats Liljegren49e86152014-04-16 19:27:58 +020045 return rval;
Garrett Cooper4a3c6582010-12-21 07:07:01 -080046}
47
48int
Wanlong Gao354ebb42012-12-07 10:10:04 +080049safe_close(const char *file, const int lineno, void (*cleanup_fn) (void),
50 int fildes)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080051{
52 int rval;
53
54 rval = close(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +020055 if (rval == -1) {
56 tst_brkm(TBROK | TERRNO, cleanup_fn,
57 "%s:%d: close(%d) failed",
58 file, lineno, fildes);
59 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080060
Mats Liljegren49e86152014-04-16 19:27:58 +020061 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080062}
63
64int
Wanlong Gao354ebb42012-12-07 10:10:04 +080065safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void),
66 char *pathname, mode_t mode)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080067{
68 int rval;
69
70 rval = creat(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +020071 if (rval == -1) {
72 tst_brkm(TBROK | TERRNO, cleanup_fn,
73 "%s:%d: creat(%s,0%o) failed",
74 file, lineno, pathname, mode);
75 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080076
Mats Liljegren49e86152014-04-16 19:27:58 +020077 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080078}
79
Wanlong Gao354ebb42012-12-07 10:10:04 +080080char *safe_dirname(const char *file, const int lineno,
81 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080082{
83 char *rval;
84
85 rval = dirname(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020086 if (rval == NULL) {
87 tst_brkm(TBROK | TERRNO, cleanup_fn,
88 "%s:%d: dirname(%s) failed",
89 file, lineno, path);
90 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080091
Mats Liljegren49e86152014-04-16 19:27:58 +020092 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080093}
94
Wanlong Gao354ebb42012-12-07 10:10:04 +080095char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
96 char *buf, size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -080097{
98 char *rval;
99
100 rval = getcwd(buf, size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200101 if (rval == NULL) {
102 tst_brkm(TBROK | TERRNO, cleanup_fn,
103 "%s:%d: getcwd(%p,%zu) failed",
104 file, lineno, buf, size);
105 }
Garrett Cooperca435922010-12-20 12:26:32 -0800106
Mats Liljegren49e86152014-04-16 19:27:58 +0200107 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800108}
109
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110struct passwd *safe_getpwnam(const char *file, const int lineno,
111 void (*cleanup_fn) (void), const char *name)
Garrett Cooperca435922010-12-20 12:26:32 -0800112{
113 struct passwd *rval;
114
115 rval = getpwnam(name);
Mats Liljegren49e86152014-04-16 19:27:58 +0200116 if (rval == NULL) {
117 tst_brkm(TBROK | TERRNO, cleanup_fn,
118 "%s:%d: getpwnam(%s) failed",
119 file, lineno, name);
120 }
Garrett Cooperca435922010-12-20 12:26:32 -0800121
Mats Liljegren49e86152014-04-16 19:27:58 +0200122 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800123}
124
Caspar Zhang817c7822011-06-30 01:50:33 +0800125int
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
127 int who, struct rusage *usage)
Caspar Zhang817c7822011-06-30 01:50:33 +0800128{
129 int rval;
130
131 rval = getrusage(who, usage);
Mats Liljegren49e86152014-04-16 19:27:58 +0200132 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200134 "%s:%d: getrusage(%d,%p) failed",
135 file, lineno, who, usage);
136 }
Caspar Zhang817c7822011-06-30 01:50:33 +0800137
138 return rval;
139}
140
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void),
142 size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800143{
144 void *rval;
145
146 rval = malloc(size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200147 if (rval == NULL) {
148 tst_brkm(TBROK | TERRNO, cleanup_fn,
149 "%s:%d: malloc(%zu) failed",
150 file, lineno, size);
151 }
Garrett Cooperca435922010-12-20 12:26:32 -0800152
Mats Liljegren49e86152014-04-16 19:27:58 +0200153 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800154}
155
Mats Liljegren49e86152014-04-16 19:27:58 +0200156int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void),
157 const char *pathname, mode_t mode)
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800158{
159 int rval;
160
161 rval = mkdir(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200162 if (rval == -1) {
163 tst_brkm(TBROK | TERRNO, cleanup_fn,
164 "%s:%d: mkdir(%s,0%o) failed",
165 file, lineno, pathname, mode);
166 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800167
168 return (rval);
169}
170
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171void *safe_mmap(const char *file, const int lineno, void (*cleanup_fn) (void),
172 void *addr, size_t length, int prot, int flags, int fd,
173 off_t offset)
Garrett Cooperca435922010-12-20 12:26:32 -0800174{
175 void *rval;
176
177 rval = mmap(addr, length, prot, flags, fd, offset);
Mats Liljegren49e86152014-04-16 19:27:58 +0200178 if (rval == MAP_FAILED) {
179 tst_brkm(TBROK | TERRNO, cleanup_fn,
180 "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed",
181 file, lineno, addr, length, prot, flags, fd,
182 (long) offset);
183 }
Garrett Cooperca435922010-12-20 12:26:32 -0800184
Mats Liljegren49e86152014-04-16 19:27:58 +0200185 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800186}
187
Mats Liljegren49e86152014-04-16 19:27:58 +0200188int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
189 void *addr, size_t length)
Garrett Cooperca435922010-12-20 12:26:32 -0800190{
191 int rval;
192
193 rval = munmap(addr, length);
Mats Liljegren49e86152014-04-16 19:27:58 +0200194 if (rval == -1) {
195 tst_brkm(TBROK | TERRNO, cleanup_fn,
196 "%s:%d: munmap(%p,%zu) failed",
197 file, lineno, addr, length);
198 }
Garrett Cooperca435922010-12-20 12:26:32 -0800199
Mats Liljegren49e86152014-04-16 19:27:58 +0200200 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800201}
202
Mats Liljegren49e86152014-04-16 19:27:58 +0200203int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
204 const char *pathname, int oflags, ...)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800205{
206 va_list ap;
207 int rval;
208 mode_t mode;
209
210 va_start(ap, oflags);
211 mode = va_arg(ap, mode_t);
212 va_end(ap);
213
214 rval = open(pathname, oflags, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200215 if (rval == -1) {
216 tst_brkm(TBROK | TERRNO, cleanup_fn,
217 "%s:%d: open(%s,%d,0%o) failed",
218 file, lineno, pathname, oflags, mode);
219 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800220
Mats Liljegren49e86152014-04-16 19:27:58 +0200221 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800222}
223
Mats Liljegren49e86152014-04-16 19:27:58 +0200224int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
225 int fildes[2])
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800226{
227 int rval;
228
229 rval = pipe(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +0200230 if (rval == -1) {
231 tst_brkm(TBROK | TERRNO, cleanup_fn,
232 "%s:%d: pipe({%d,%d}) failed",
233 file, lineno, fildes[0], fildes[1]);
234 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800235
Mats Liljegren49e86152014-04-16 19:27:58 +0200236 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800237}
238
Mats Liljegren49e86152014-04-16 19:27:58 +0200239ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
240 char len_strict, int fildes, void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800241{
242 ssize_t rval;
243
244 rval = read(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200245 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
246 tst_brkm(TBROK | TERRNO, cleanup_fn,
247 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
248 file, lineno, fildes, buf, nbyte, rval);
249 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800250
Mats Liljegren49e86152014-04-16 19:27:58 +0200251 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800252}
253
Mats Liljegren49e86152014-04-16 19:27:58 +0200254int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
255 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800256{
257 int rval;
258
259 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200260 if (rval == -1) {
261 tst_brkm(TBROK | TERRNO, cleanup_fn,
262 "%s:%d: setegid(%u) failed",
263 file, lineno, (unsigned) egid);
264 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800265
Mats Liljegren49e86152014-04-16 19:27:58 +0200266 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800267}
268
Mats Liljegren49e86152014-04-16 19:27:58 +0200269int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
270 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800271{
272 int rval;
273
274 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200275 if (rval == -1) {
276 tst_brkm(TBROK | TERRNO, cleanup_fn,
277 "%s:%d: seteuid(%u) failed",
278 file, lineno, (unsigned) euid);
279 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800280
Mats Liljegren49e86152014-04-16 19:27:58 +0200281 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800282}
283
Mats Liljegren49e86152014-04-16 19:27:58 +0200284int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
285 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800286{
287 int rval;
288
289 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200290 if (rval == -1) {
291 tst_brkm(TBROK | TERRNO, cleanup_fn,
292 "%s:%d: setgid(%u) failed",
293 file, lineno, (unsigned) gid);
294 }
Garrett Cooperca435922010-12-20 12:26:32 -0800295
Mats Liljegren49e86152014-04-16 19:27:58 +0200296 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800297}
298
Mats Liljegren49e86152014-04-16 19:27:58 +0200299int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
300 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800301{
302 int rval;
303
304 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200305 if (rval == -1) {
306 tst_brkm(TBROK | TERRNO, cleanup_fn,
307 "%s:%d: setuid(%u) failed",
308 file, lineno, (unsigned) uid);
309 }
Garrett Cooperca435922010-12-20 12:26:32 -0800310
Mats Liljegren49e86152014-04-16 19:27:58 +0200311 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800312}
313
Mats Liljegren49e86152014-04-16 19:27:58 +0200314int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
315 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800316{
317 int rval;
318
319 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200320 if (rval == -1) {
321 tst_brkm(TBROK | TERRNO, cleanup_fn,
322 "%s:%d: unlink(%s) failed",
323 file, lineno, pathname);
324 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800325
Mats Liljegren49e86152014-04-16 19:27:58 +0200326 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800327}
328
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200329
330int safe_link(const char *file, const int lineno,
331 void (cleanup_fn)(void), const char *oldpath,
332 const char *newpath)
333{
334 int rval;
335
336 rval = link(oldpath, newpath);
337
338 if (rval == -1) {
339 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200340 "%s:%d: link(%s,%s) failed",
341 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200342 }
343
344 return rval;
345}
346
Cyril Hrubisf095ccb2013-11-27 19:55:59 +0100347off_t safe_lseek(const char *file, const int lineno,
348 void (cleanup_fn)(void), int fd,
349 off_t offset, int whence)
350{
351 off_t rval;
352
353 rval = lseek(fd, offset, whence);
354
355 if (rval == (off_t) -1) {
356 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200357 "%s:%d: lseek(%d,%ld,%d) failed",
358 file, lineno, fd, (long)offset, whence);
Cyril Hrubisf095ccb2013-11-27 19:55:59 +0100359 }
360
361 return rval;
362}
363
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200364int safe_symlink(const char *file, const int lineno,
365 void (cleanup_fn)(void), const char *oldpath,
366 const char *newpath)
367{
368 int rval;
369
370 rval = symlink(oldpath, newpath);
371
372 if (rval == -1) {
373 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200374 "%s:%d: symlink(%s,%s) failed",
375 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200376 }
377
378 return rval;
379}
380
Mats Liljegren49e86152014-04-16 19:27:58 +0200381ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
382 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800383{
384 ssize_t rval;
385
386 rval = write(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200387 if ((len_strict == 0 && rval == -1) || (size_t)rval != nbyte) {
388 tst_brkm(TBROK | TERRNO, cleanup_fn,
389 "%s:%d: write(%d,%p,%zu) failed",
390 file, lineno, fildes, buf, rval);
391 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800392
Mats Liljegren49e86152014-04-16 19:27:58 +0200393 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800394}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100395
396int safe_ftruncate(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800397 void (cleanup_fn) (void), int fd, off_t length)
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100398{
399 int rval;
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800400
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100401 rval = ftruncate(fd, length);
402 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800403 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200404 "%s:%d: ftruncate(%d,%ld) failed",
405 file, lineno, fd, (long)length);
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100406 }
407
408 return rval;
409}
410
411int safe_truncate(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800412 void (cleanup_fn) (void), const char *path, off_t length)
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100413{
414 int rval;
415
416 rval = truncate(path, length);
417 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200418 tst_brkm(TBROK | TERRNO, cleanup_fn,
419 "%s:%d: truncate(%s,%ld) failed",
420 file, lineno, path, (long)length);
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100421 }
422
423 return rval;
424}
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800425
426long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800427 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800428{
429 long rval;
430 char *endptr;
431
432 errno = 0;
433 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200434
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800435 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200436 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800437 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200438 "%s:%d: strtol(%s) failed", file, lineno, str);
439 }
440
441 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800442 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200443 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
444 }
445
446 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800447 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200448 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
449 file, lineno, str, rval, min, max);
450 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800451
452 return rval;
453}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800454
Wanlong Gao354ebb42012-12-07 10:10:04 +0800455unsigned long safe_strtoul(const char *file, const int lineno,
456 void (cleanup_fn) (void), char *str,
457 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800458{
459 unsigned long rval;
460 char *endptr;
461
462 errno = 0;
463 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200464
Zhouping Liu2b73a152012-07-07 23:14:39 +0800465 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200466 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800467 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200468 "%s:%d: strtoul(%s) failed", file, lineno, str);
469 }
470
471 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800472 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200473 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
474 file, lineno, str, rval, min, max);
475 }
476
477 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800478 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800479 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200480 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800481
482 return rval;
483}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800484
485long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800486 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800487{
488 long rval;
489 errno = 0;
490
491 rval = sysconf(name);
492
493 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200494 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800495 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200496 "%s:%d: sysconf(%d) failed",
497 file, lineno, name);
498 } else {
499 tst_resm(TINFO, "%s:%d: sysconf(%d): "
500 "queried option is not available"
501 " or there is no definite limit",
502 file, lineno, name);
503 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800504 }
505
506 return rval;
507}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800508
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100509int safe_stat(const char *file, const int lineno,
510 void (cleanup_fn)(void), const char *path, struct stat *buf)
511{
512 int rval;
513
514 rval = stat(path, buf);
515
516 if (rval == -1) {
517 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200518 "%s:%d: stat(%s,%p) failed", file, lineno, path, buf);
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100519 }
520
521 return rval;
522}
523
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800524int safe_fstat(const char *file, const int lineno,
525 void (cleanup_fn)(void), int fd, struct stat *buf)
526{
527 int rval;
528
529 rval = fstat(fd, buf);
530
531 if (rval == -1) {
532 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200533 "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf);
Cyril Hrubis7ee63272014-02-25 16:38:24 +0100534 }
535
536 return rval;
537}
538
539int safe_lstat(const char *file, const int lineno,
540 void (cleanup_fn)(void), const char *path, struct stat *buf)
541{
542 int rval;
543
544 rval = lstat(path, buf);
545
546 if (rval == -1) {
547 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200548 "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf);
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800549 }
550
551 return rval;
552}
Zeng Linggangef9aac62014-03-03 19:22:55 +0800553
554int safe_getrlimit(const char *file, const int lineno,
555 void (cleanup_fn)(void), int resource, struct rlimit *rlim)
556{
557 int rval;
558
559 rval = getrlimit(resource, rlim);
560
561 if (rval == -1) {
562 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200563 "%s:%d: getrlimit(%d,%p) failed",
564 file, lineno, resource, rlim);
Zeng Linggangef9aac62014-03-03 19:22:55 +0800565 }
566
567 return rval;
568}
569
570int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void),
571 int resource, const struct rlimit *rlim)
572{
573 int rval;
574
575 rval = setrlimit(resource, rlim);
576
577 if (rval == -1) {
578 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200579 "%s:%d: setrlimit(%d,%p) failed",
580 file, lineno, resource, rlim);
Zeng Linggangef9aac62014-03-03 19:22:55 +0800581 }
582
583 return rval;
584}
Cyril Hrubisc4592352014-03-04 16:41:02 +0100585
586int safe_chmod(const char *file, const int lineno,
587 void (cleanup_fn)(void), const char *path, mode_t mode)
588{
589 int rval;
590
591 rval = chmod(path, mode);
592
593 if (rval == -1) {
594 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200595 "%s:%d: chmod(%s,0%o) failed",
596 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100597 }
598
599 return rval;
600}
601
602int safe_fchmod(const char *file, const int lineno,
603 void (cleanup_fn)(void), int fd, mode_t mode)
604{
605 int rval;
606
607 rval = fchmod(fd, mode);
608
609 if (rval == -1) {
610 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200611 "%s:%d: fchmod(%d,0%o) failed",
612 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100613 }
614
615 return rval;
616}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200617
618pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
619 int *status)
620{
621 pid_t rval;
622
623 rval = wait(status);
624 if (rval == -1) {
625 tst_brkm(TBROK | TERRNO, cleanup_fn,
626 "%s:%d: wait(%p) failed",
627 file, lineno, status);
628 }
629
630 return rval;
631}
632
633pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
634 pid_t pid, int *status, int opts)
635{
636 pid_t rval;
637
638 rval = waitpid(pid, status, opts);
639 if (rval == -1) {
640 tst_brkm(TBROK | TERRNO, cleanup_fn,
641 "%s:%d: waitpid(%d,%p,%d) failed",
642 file, lineno, pid, status, opts);
643 }
644
645 return rval;
646}