blob: 9fa6ac47cf5f9ecbbc6ee5e650dda84deda38d70 [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
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200412int safe_symlink(const char *file, const int lineno,
413 void (cleanup_fn)(void), const char *oldpath,
414 const char *newpath)
415{
416 int rval;
417
418 rval = symlink(oldpath, newpath);
419
420 if (rval == -1) {
421 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200422 "%s:%d: symlink(%s,%s) failed",
423 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200424 }
425
426 return rval;
427}
428
Mats Liljegren49e86152014-04-16 19:27:58 +0200429ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
430 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800431{
432 ssize_t rval;
433
434 rval = write(fildes, buf, nbyte);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800435 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200436 tst_brkm(TBROK | TERRNO, cleanup_fn,
437 "%s:%d: write(%d,%p,%zu) failed",
438 file, lineno, fildes, buf, rval);
439 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800440
Mats Liljegren49e86152014-04-16 19:27:58 +0200441 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800442}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100443
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800444ssize_t safe_pwrite(const char *file, const int lineno,
445 void (cleanup_fn) (void), char len_strict, int fildes,
446 const void *buf, size_t nbyte, off_t offset)
447{
448 ssize_t rval;
449
450 rval = pwrite(fildes, buf, nbyte, offset);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800451 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Xiaoguang Wang34e26eb2015-02-04 16:59:46 +0800452 tst_brkm(TBROK | TERRNO, cleanup_fn,
453 "%s:%d: pwrite(%d,%p,%zu,%ld) failed",
454 file, lineno, fildes, buf, rval, offset);
455 }
456
457 return rval;
458}
459
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800460long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800461 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800462{
463 long rval;
464 char *endptr;
465
466 errno = 0;
467 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200468
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800469 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200470 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800471 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200472 "%s:%d: strtol(%s) failed", file, lineno, str);
473 }
474
475 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800476 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200477 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
478 }
479
480 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800481 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200482 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
483 file, lineno, str, rval, min, max);
484 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800485
486 return rval;
487}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800488
Wanlong Gao354ebb42012-12-07 10:10:04 +0800489unsigned long safe_strtoul(const char *file, const int lineno,
490 void (cleanup_fn) (void), char *str,
491 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800492{
493 unsigned long rval;
494 char *endptr;
495
496 errno = 0;
497 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200498
Zhouping Liu2b73a152012-07-07 23:14:39 +0800499 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200500 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800501 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200502 "%s:%d: strtoul(%s) failed", file, lineno, str);
503 }
504
505 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800506 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200507 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
508 file, lineno, str, rval, min, max);
509 }
510
511 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800512 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800513 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200514 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800515
516 return rval;
517}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800518
519long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800520 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800521{
522 long rval;
523 errno = 0;
524
525 rval = sysconf(name);
526
527 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200528 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800529 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200530 "%s:%d: sysconf(%d) failed",
531 file, lineno, name);
532 } else {
533 tst_resm(TINFO, "%s:%d: sysconf(%d): "
534 "queried option is not available"
535 " or there is no definite limit",
536 file, lineno, name);
537 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800538 }
539
540 return rval;
541}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800542
Cyril Hrubisc4592352014-03-04 16:41:02 +0100543int safe_chmod(const char *file, const int lineno,
544 void (cleanup_fn)(void), const char *path, mode_t mode)
545{
546 int rval;
547
548 rval = chmod(path, mode);
549
550 if (rval == -1) {
551 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200552 "%s:%d: chmod(%s,0%o) failed",
553 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100554 }
555
556 return rval;
557}
558
559int safe_fchmod(const char *file, const int lineno,
560 void (cleanup_fn)(void), int fd, mode_t mode)
561{
562 int rval;
563
564 rval = fchmod(fd, mode);
565
566 if (rval == -1) {
567 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200568 "%s:%d: fchmod(%d,0%o) failed",
569 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100570 }
571
572 return rval;
573}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200574
Xing Gu92a14592014-06-13 10:53:17 +0800575int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
576 const char *path, uid_t owner, gid_t group)
577{
578 int rval;
579
580 rval = chown(path, owner, group);
581
582 if (rval == -1) {
583 tst_brkm(TBROK | TERRNO, cleanup_fn,
584 "%s:%d: chown(%s,%d,%d) failed",
585 file, lineno, path, owner, group);
586 }
587
588 return rval;
589}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200590
591int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
592 int fd, uid_t owner, gid_t group)
593{
594 int rval;
595
596 rval = fchown(fd, owner, group);
597
598 if (rval == -1) {
599 tst_brkm(TBROK | TERRNO, cleanup_fn,
600 "%s:%d: fchown(%d,%d,%d) failed",
601 file, lineno, fd, owner, group);
602 }
603
604 return rval;
605}
606
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200607pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
608 int *status)
609{
610 pid_t rval;
611
612 rval = wait(status);
613 if (rval == -1) {
614 tst_brkm(TBROK | TERRNO, cleanup_fn,
615 "%s:%d: wait(%p) failed",
616 file, lineno, status);
617 }
618
619 return rval;
620}
621
622pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
623 pid_t pid, int *status, int opts)
624{
625 pid_t rval;
626
627 rval = waitpid(pid, status, opts);
628 if (rval == -1) {
629 tst_brkm(TBROK | TERRNO, cleanup_fn,
630 "%s:%d: waitpid(%d,%p,%d) failed",
631 file, lineno, pid, status, opts);
632 }
633
634 return rval;
635}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800636
637void *safe_memalign(const char *file, const int lineno,
638 void (*cleanup_fn) (void), size_t alignment, size_t size)
639{
640 void *rval;
641
642 rval = memalign(alignment, size);
643 if (rval == NULL)
644 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
645 file, lineno);
646
647 return rval;
648}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800649
650int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
651 pid_t pid, int sig)
652{
653 int rval;
654
655 rval = kill(pid, sig);
656
657 if (rval == -1) {
658 tst_brkm(TBROK | TERRNO, cleanup_fn,
659 "%s:%d: kill(%d,%s) failed",
660 file, lineno, pid, tst_strsig(sig));
661 }
662
663 return rval;
664}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200665
666int safe_mkfifo(const char *file, const int lineno,
667 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
668{
669 int rval;
670
671 rval = mkfifo(pathname, mode);
672
673 if (rval == -1) {
674 tst_brkm(TBROK | TERRNO, cleanup_fn,
675 "%s:%d: mkfifo(%s, 0%o) failed",
676 file, lineno, pathname, mode);
677 }
678
679 return rval;
680}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800681
682int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
683 const char *oldpath, const char *newpath)
684{
685 int rval;
686
687 rval = rename(oldpath, newpath);
688
689 if (rval == -1) {
690 tst_brkm(TBROK | TERRNO, cleanup_fn,
691 "%s:%d: rename(%s, %s) failed",
692 file, lineno, oldpath, newpath);
693 }
694
695 return rval;
696}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200697
698int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
699 const char *source, const char *target,
700 const char *filesystemtype, unsigned long mountflags,
701 const void *data)
702{
703 int rval;
704
705 rval = mount(source, target, filesystemtype, mountflags, data);
706
707 if (rval == -1) {
708 tst_brkm(TBROK | TERRNO, cleanup_fn,
709 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
710 file, lineno, source, target, filesystemtype,
711 mountflags, data);
712 }
713
714 return rval;
715}
716
717int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
718 const char *target)
719{
720 int rval;
721
722 rval = umount(target);
723
724 if (rval == -1) {
725 tst_brkm(TBROK | TERRNO, cleanup_fn,
726 "%s:%d: umount(%s) failed",
727 file, lineno, target);
728 }
729
730 return rval;
731}
Cyril Hrubis9138a012015-02-10 15:44:45 +0100732
733DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
734 const char *name)
735{
736 DIR *rval;
737
738 rval = opendir(name);
739
740 if (!rval) {
741 tst_brkm(TBROK | TERRNO, cleanup_fn,
742 "%s:%d: opendir(%s) failed", file, lineno, name);
743 }
744
745 return rval;
746}
747
748int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
749 DIR *dirp)
750{
751 int rval;
752
753 rval = closedir(dirp);
754
755 if (rval) {
756 tst_brkm(TBROK | TERRNO, cleanup_fn,
757 "%s:%d: closedir(%p) failed", file, lineno, dirp);
758 }
759
760 return rval;
761}
762
763struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
764 DIR *dirp)
765{
766 struct dirent *rval;
767 int err = errno;
768
769 errno = 0;
770 rval = readdir(dirp);
771
772 if (!rval && errno) {
773 tst_brkm(TBROK | TERRNO, cleanup_fn,
774 "%s:%d: readdir(%p) failed", file, lineno, dirp);
775 }
776
777 errno = err;
778 return rval;
779}