blob: 5a05c84b1c5ac9130c770cd203cee67417635483 [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
Alexey Kodanevf3f4d332016-01-28 13:10:58 +0300394int safe_linkat(const char *file, const int lineno,
395 void (cleanup_fn)(void), int olddirfd, const char *oldpath,
396 int newdirfd, const char *newpath, int flags)
397{
398 int rval;
399
400 rval = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
401
402 if (rval == -1) {
403 tst_brkm(TBROK | TERRNO, cleanup_fn,
404 "%s:%d: linkat(%d,%s,%d,%s,%d) failed",
405 file, lineno, olddirfd, oldpath, newdirfd,
406 newpath, flags);
407 }
408
409 return rval;
410}
411
Alexey Kodanev8bbb1d52016-01-28 14:10:54 +0300412ssize_t safe_readlink(const char *file, const int lineno,
413 void (cleanup_fn)(void), const char *path,
414 char *buf, size_t bufsize)
415{
416 ssize_t rval;
417
418 rval = readlink(path, buf, bufsize);
419
420 if (rval == -1) {
421 tst_brkm(TBROK | TERRNO, cleanup_fn,
422 "%s:%d: readlink(%s,%p,%zu) failed",
423 file, lineno, path, buf, bufsize);
424 }
425
426 return rval;
427}
428
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200429int safe_symlink(const char *file, const int lineno,
430 void (cleanup_fn)(void), const char *oldpath,
431 const char *newpath)
432{
433 int rval;
434
435 rval = symlink(oldpath, newpath);
436
437 if (rval == -1) {
438 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200439 "%s:%d: symlink(%s,%s) failed",
440 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200441 }
442
443 return rval;
444}
445
Mats Liljegren49e86152014-04-16 19:27:58 +0200446ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
447 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800448{
449 ssize_t rval;
450
451 rval = write(fildes, buf, nbyte);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800452 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200453 tst_brkm(TBROK | TERRNO, cleanup_fn,
454 "%s:%d: write(%d,%p,%zu) failed",
455 file, lineno, fildes, buf, rval);
456 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800457
Mats Liljegren49e86152014-04-16 19:27:58 +0200458 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800459}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100460
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800461ssize_t safe_pwrite(const char *file, const int lineno,
462 void (cleanup_fn) (void), char len_strict, int fildes,
463 const void *buf, size_t nbyte, off_t offset)
464{
465 ssize_t rval;
466
467 rval = pwrite(fildes, buf, nbyte, offset);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800468 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800469 tst_brkm(TBROK | TERRNO, cleanup_fn,
470 "%s:%d: pwrite(%d,%p,%zu,%ld) failed",
471 file, lineno, fildes, buf, rval, offset);
472 }
473
474 return rval;
475}
476
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800477long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800478 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800479{
480 long rval;
481 char *endptr;
482
483 errno = 0;
484 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200485
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800486 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200487 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800488 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200489 "%s:%d: strtol(%s) failed", file, lineno, str);
490 }
491
492 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
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): Invalid value", file, lineno, str);
495 }
496
497 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800498 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200499 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
500 file, lineno, str, rval, min, max);
501 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800502
503 return rval;
504}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800505
Wanlong Gao354ebb42012-12-07 10:10:04 +0800506unsigned long safe_strtoul(const char *file, const int lineno,
507 void (cleanup_fn) (void), char *str,
508 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800509{
510 unsigned long rval;
511 char *endptr;
512
513 errno = 0;
514 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200515
Zhouping Liu2b73a152012-07-07 23:14:39 +0800516 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200517 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800518 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200519 "%s:%d: strtoul(%s) failed", file, lineno, str);
520 }
521
522 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800523 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200524 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
525 file, lineno, str, rval, min, max);
526 }
527
528 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800529 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800530 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200531 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800532
533 return rval;
534}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800535
536long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800537 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800538{
539 long rval;
540 errno = 0;
541
542 rval = sysconf(name);
543
544 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200545 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800546 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200547 "%s:%d: sysconf(%d) failed",
548 file, lineno, name);
549 } else {
550 tst_resm(TINFO, "%s:%d: sysconf(%d): "
551 "queried option is not available"
552 " or there is no definite limit",
553 file, lineno, name);
554 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800555 }
556
557 return rval;
558}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800559
Cyril Hrubisc4592352014-03-04 16:41:02 +0100560int safe_chmod(const char *file, const int lineno,
561 void (cleanup_fn)(void), const char *path, mode_t mode)
562{
563 int rval;
564
565 rval = chmod(path, mode);
566
567 if (rval == -1) {
568 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200569 "%s:%d: chmod(%s,0%o) failed",
570 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100571 }
572
573 return rval;
574}
575
576int safe_fchmod(const char *file, const int lineno,
577 void (cleanup_fn)(void), int fd, mode_t mode)
578{
579 int rval;
580
581 rval = fchmod(fd, mode);
582
583 if (rval == -1) {
584 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200585 "%s:%d: fchmod(%d,0%o) failed",
586 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100587 }
588
589 return rval;
590}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200591
Xing Gu92a14592014-06-13 10:53:17 +0800592int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
593 const char *path, uid_t owner, gid_t group)
594{
595 int rval;
596
597 rval = chown(path, owner, group);
598
599 if (rval == -1) {
600 tst_brkm(TBROK | TERRNO, cleanup_fn,
601 "%s:%d: chown(%s,%d,%d) failed",
602 file, lineno, path, owner, group);
603 }
604
605 return rval;
606}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200607
608int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
609 int fd, uid_t owner, gid_t group)
610{
611 int rval;
612
613 rval = fchown(fd, owner, group);
614
615 if (rval == -1) {
616 tst_brkm(TBROK | TERRNO, cleanup_fn,
617 "%s:%d: fchown(%d,%d,%d) failed",
618 file, lineno, fd, owner, group);
619 }
620
621 return rval;
622}
623
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200624pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
625 int *status)
626{
627 pid_t rval;
628
629 rval = wait(status);
630 if (rval == -1) {
631 tst_brkm(TBROK | TERRNO, cleanup_fn,
632 "%s:%d: wait(%p) failed",
633 file, lineno, status);
634 }
635
636 return rval;
637}
638
639pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
640 pid_t pid, int *status, int opts)
641{
642 pid_t rval;
643
644 rval = waitpid(pid, status, opts);
645 if (rval == -1) {
646 tst_brkm(TBROK | TERRNO, cleanup_fn,
647 "%s:%d: waitpid(%d,%p,%d) failed",
648 file, lineno, pid, status, opts);
649 }
650
651 return rval;
652}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800653
654void *safe_memalign(const char *file, const int lineno,
655 void (*cleanup_fn) (void), size_t alignment, size_t size)
656{
657 void *rval;
658
659 rval = memalign(alignment, size);
660 if (rval == NULL)
661 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
662 file, lineno);
663
664 return rval;
665}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800666
667int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
668 pid_t pid, int sig)
669{
670 int rval;
671
672 rval = kill(pid, sig);
673
674 if (rval == -1) {
675 tst_brkm(TBROK | TERRNO, cleanup_fn,
676 "%s:%d: kill(%d,%s) failed",
677 file, lineno, pid, tst_strsig(sig));
678 }
679
680 return rval;
681}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200682
683int safe_mkfifo(const char *file, const int lineno,
684 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
685{
686 int rval;
687
688 rval = mkfifo(pathname, mode);
689
690 if (rval == -1) {
691 tst_brkm(TBROK | TERRNO, cleanup_fn,
692 "%s:%d: mkfifo(%s, 0%o) failed",
693 file, lineno, pathname, mode);
694 }
695
696 return rval;
697}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800698
699int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
700 const char *oldpath, const char *newpath)
701{
702 int rval;
703
704 rval = rename(oldpath, newpath);
705
706 if (rval == -1) {
707 tst_brkm(TBROK | TERRNO, cleanup_fn,
708 "%s:%d: rename(%s, %s) failed",
709 file, lineno, oldpath, newpath);
710 }
711
712 return rval;
713}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200714
715int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
716 const char *source, const char *target,
717 const char *filesystemtype, unsigned long mountflags,
718 const void *data)
719{
720 int rval;
721
722 rval = mount(source, target, filesystemtype, mountflags, data);
723
724 if (rval == -1) {
725 tst_brkm(TBROK | TERRNO, cleanup_fn,
726 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
727 file, lineno, source, target, filesystemtype,
728 mountflags, data);
729 }
730
731 return rval;
732}
733
734int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
735 const char *target)
736{
737 int rval;
738
739 rval = umount(target);
740
741 if (rval == -1) {
742 tst_brkm(TBROK | TERRNO, cleanup_fn,
743 "%s:%d: umount(%s) failed",
744 file, lineno, target);
745 }
746
747 return rval;
748}
Cyril Hrubis9138a012015-02-10 15:44:45 +0100749
750DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
751 const char *name)
752{
753 DIR *rval;
754
755 rval = opendir(name);
756
757 if (!rval) {
758 tst_brkm(TBROK | TERRNO, cleanup_fn,
759 "%s:%d: opendir(%s) failed", file, lineno, name);
760 }
761
762 return rval;
763}
764
765int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
766 DIR *dirp)
767{
768 int rval;
769
770 rval = closedir(dirp);
771
772 if (rval) {
773 tst_brkm(TBROK | TERRNO, cleanup_fn,
774 "%s:%d: closedir(%p) failed", file, lineno, dirp);
775 }
776
777 return rval;
778}
779
780struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
781 DIR *dirp)
782{
783 struct dirent *rval;
784 int err = errno;
785
786 errno = 0;
787 rval = readdir(dirp);
788
789 if (!rval && errno) {
790 tst_brkm(TBROK | TERRNO, cleanup_fn,
791 "%s:%d: readdir(%p) failed", file, lineno, dirp);
792 }
793
794 errno = err;
795 return rval;
796}