blob: 90885fdaf6a8b92483e582ef08a2a68610f839c3 [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),
69 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
Mats Liljegren49e86152014-04-16 19:27:58 +0200255int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
256 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800257{
258 int rval;
259
260 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200261 if (rval == -1) {
262 tst_brkm(TBROK | TERRNO, cleanup_fn,
263 "%s:%d: setegid(%u) failed",
264 file, lineno, (unsigned) egid);
265 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800266
Mats Liljegren49e86152014-04-16 19:27:58 +0200267 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800268}
269
Mats Liljegren49e86152014-04-16 19:27:58 +0200270int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
271 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800272{
273 int rval;
274
275 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200276 if (rval == -1) {
277 tst_brkm(TBROK | TERRNO, cleanup_fn,
278 "%s:%d: seteuid(%u) failed",
279 file, lineno, (unsigned) euid);
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_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
286 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800287{
288 int rval;
289
290 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200291 if (rval == -1) {
292 tst_brkm(TBROK | TERRNO, cleanup_fn,
293 "%s:%d: setgid(%u) failed",
294 file, lineno, (unsigned) gid);
295 }
Garrett Cooperca435922010-12-20 12:26:32 -0800296
Mats Liljegren49e86152014-04-16 19:27:58 +0200297 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800298}
299
Mats Liljegren49e86152014-04-16 19:27:58 +0200300int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
301 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800302{
303 int rval;
304
305 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200306 if (rval == -1) {
307 tst_brkm(TBROK | TERRNO, cleanup_fn,
308 "%s:%d: setuid(%u) failed",
309 file, lineno, (unsigned) uid);
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
Zeng Linggang91d67422014-04-25 10:46:32 +0800315int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void),
316 uid_t *ruid, uid_t *euid, uid_t *suid)
317{
318 int rval;
319
320 rval = getresuid(ruid, euid, suid);
321 if (rval == -1) {
322 tst_brkm(TBROK | TERRNO, cleanup_fn,
323 "%s:%d: getresuid(%p, %p, %p) failed",
324 file, lineno, ruid, euid, suid);
325 }
326
327 return rval;
328}
329
330int safe_getresgid(const char *file, const int lineno, void (*cleanup_fn)(void),
331 gid_t *rgid, gid_t *egid, gid_t *sgid)
332{
333 int rval;
334
335 rval = getresgid(rgid, egid, sgid);
336 if (rval == -1) {
337 tst_brkm(TBROK | TERRNO, cleanup_fn,
338 "%s:%d: getresgid(%p, %p, %p) failed",
339 file, lineno, rgid, egid, sgid);
340 }
341
342 return rval;
343}
344
Mats Liljegren49e86152014-04-16 19:27:58 +0200345int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
346 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800347{
348 int rval;
349
350 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200351 if (rval == -1) {
352 tst_brkm(TBROK | TERRNO, cleanup_fn,
353 "%s:%d: unlink(%s) failed",
354 file, lineno, pathname);
355 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800356
Mats Liljegren49e86152014-04-16 19:27:58 +0200357 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800358}
359
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200360
361int safe_link(const char *file, const int lineno,
362 void (cleanup_fn)(void), const char *oldpath,
363 const char *newpath)
364{
365 int rval;
366
367 rval = link(oldpath, newpath);
368
369 if (rval == -1) {
370 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200371 "%s:%d: link(%s,%s) failed",
372 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200373 }
374
375 return rval;
376}
377
378int safe_symlink(const char *file, const int lineno,
379 void (cleanup_fn)(void), const char *oldpath,
380 const char *newpath)
381{
382 int rval;
383
384 rval = symlink(oldpath, newpath);
385
386 if (rval == -1) {
387 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200388 "%s:%d: symlink(%s,%s) failed",
389 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200390 }
391
392 return rval;
393}
394
Mats Liljegren49e86152014-04-16 19:27:58 +0200395ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
396 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800397{
398 ssize_t rval;
399
400 rval = write(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200401 if ((len_strict == 0 && rval == -1) || (size_t)rval != nbyte) {
402 tst_brkm(TBROK | TERRNO, cleanup_fn,
403 "%s:%d: write(%d,%p,%zu) failed",
404 file, lineno, fildes, buf, rval);
405 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800406
Mats Liljegren49e86152014-04-16 19:27:58 +0200407 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800408}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100409
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800410long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800411 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800412{
413 long rval;
414 char *endptr;
415
416 errno = 0;
417 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200418
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800419 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200420 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800421 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200422 "%s:%d: strtol(%s) failed", file, lineno, str);
423 }
424
425 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800426 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200427 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
428 }
429
430 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800431 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200432 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
433 file, lineno, str, rval, min, max);
434 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800435
436 return rval;
437}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800438
Wanlong Gao354ebb42012-12-07 10:10:04 +0800439unsigned long safe_strtoul(const char *file, const int lineno,
440 void (cleanup_fn) (void), char *str,
441 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800442{
443 unsigned long rval;
444 char *endptr;
445
446 errno = 0;
447 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200448
Zhouping Liu2b73a152012-07-07 23:14:39 +0800449 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200450 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800451 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200452 "%s:%d: strtoul(%s) failed", file, lineno, str);
453 }
454
455 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800456 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200457 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
458 file, lineno, str, rval, min, max);
459 }
460
461 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800462 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800463 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200464 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800465
466 return rval;
467}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800468
469long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800470 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800471{
472 long rval;
473 errno = 0;
474
475 rval = sysconf(name);
476
477 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200478 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800479 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200480 "%s:%d: sysconf(%d) failed",
481 file, lineno, name);
482 } else {
483 tst_resm(TINFO, "%s:%d: sysconf(%d): "
484 "queried option is not available"
485 " or there is no definite limit",
486 file, lineno, name);
487 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800488 }
489
490 return rval;
491}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800492
Cyril Hrubisc4592352014-03-04 16:41:02 +0100493int safe_chmod(const char *file, const int lineno,
494 void (cleanup_fn)(void), const char *path, mode_t mode)
495{
496 int rval;
497
498 rval = chmod(path, mode);
499
500 if (rval == -1) {
501 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200502 "%s:%d: chmod(%s,0%o) failed",
503 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100504 }
505
506 return rval;
507}
508
509int safe_fchmod(const char *file, const int lineno,
510 void (cleanup_fn)(void), int fd, mode_t mode)
511{
512 int rval;
513
514 rval = fchmod(fd, mode);
515
516 if (rval == -1) {
517 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200518 "%s:%d: fchmod(%d,0%o) failed",
519 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100520 }
521
522 return rval;
523}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200524
Xing Gu92a14592014-06-13 10:53:17 +0800525int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
526 const char *path, uid_t owner, gid_t group)
527{
528 int rval;
529
530 rval = chown(path, owner, group);
531
532 if (rval == -1) {
533 tst_brkm(TBROK | TERRNO, cleanup_fn,
534 "%s:%d: chown(%s,%d,%d) failed",
535 file, lineno, path, owner, group);
536 }
537
538 return rval;
539}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200540
541int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
542 int fd, uid_t owner, gid_t group)
543{
544 int rval;
545
546 rval = fchown(fd, owner, group);
547
548 if (rval == -1) {
549 tst_brkm(TBROK | TERRNO, cleanup_fn,
550 "%s:%d: fchown(%d,%d,%d) failed",
551 file, lineno, fd, owner, group);
552 }
553
554 return rval;
555}
556
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200557pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
558 int *status)
559{
560 pid_t rval;
561
562 rval = wait(status);
563 if (rval == -1) {
564 tst_brkm(TBROK | TERRNO, cleanup_fn,
565 "%s:%d: wait(%p) failed",
566 file, lineno, status);
567 }
568
569 return rval;
570}
571
572pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
573 pid_t pid, int *status, int opts)
574{
575 pid_t rval;
576
577 rval = waitpid(pid, status, opts);
578 if (rval == -1) {
579 tst_brkm(TBROK | TERRNO, cleanup_fn,
580 "%s:%d: waitpid(%d,%p,%d) failed",
581 file, lineno, pid, status, opts);
582 }
583
584 return rval;
585}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800586
587void *safe_memalign(const char *file, const int lineno,
588 void (*cleanup_fn) (void), size_t alignment, size_t size)
589{
590 void *rval;
591
592 rval = memalign(alignment, size);
593 if (rval == NULL)
594 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
595 file, lineno);
596
597 return rval;
598}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800599
600int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
601 pid_t pid, int sig)
602{
603 int rval;
604
605 rval = kill(pid, sig);
606
607 if (rval == -1) {
608 tst_brkm(TBROK | TERRNO, cleanup_fn,
609 "%s:%d: kill(%d,%s) failed",
610 file, lineno, pid, tst_strsig(sig));
611 }
612
613 return rval;
614}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200615
616int safe_mkfifo(const char *file, const int lineno,
617 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
618{
619 int rval;
620
621 rval = mkfifo(pathname, mode);
622
623 if (rval == -1) {
624 tst_brkm(TBROK | TERRNO, cleanup_fn,
625 "%s:%d: mkfifo(%s, 0%o) failed",
626 file, lineno, pathname, mode);
627 }
628
629 return rval;
630}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800631
632int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
633 const char *oldpath, const char *newpath)
634{
635 int rval;
636
637 rval = rename(oldpath, newpath);
638
639 if (rval == -1) {
640 tst_brkm(TBROK | TERRNO, cleanup_fn,
641 "%s:%d: rename(%s, %s) failed",
642 file, lineno, oldpath, newpath);
643 }
644
645 return rval;
646}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200647
648int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
649 const char *source, const char *target,
650 const char *filesystemtype, unsigned long mountflags,
651 const void *data)
652{
653 int rval;
654
655 rval = mount(source, target, filesystemtype, mountflags, data);
656
657 if (rval == -1) {
658 tst_brkm(TBROK | TERRNO, cleanup_fn,
659 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
660 file, lineno, source, target, filesystemtype,
661 mountflags, data);
662 }
663
664 return rval;
665}
666
667int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
668 const char *target)
669{
670 int rval;
671
672 rval = umount(target);
673
674 if (rval == -1) {
675 tst_brkm(TBROK | TERRNO, cleanup_fn,
676 "%s:%d: umount(%s) failed",
677 file, lineno, target);
678 }
679
680 return rval;
681}