blob: 094e5c749b8d141cdfcfe62834fed4f4fad01be6 [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>
Dejan Jovicevic10552812016-11-15 13:19:33 +01008#include <sys/xattr.h>
Vinson Lee9af0bd72012-02-09 15:05:50 -08009#include <errno.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080010#include <fcntl.h>
11#include <libgen.h>
Caspar Zhangd6a1f252012-02-09 15:58:28 +080012#include <limits.h>
Garrett Cooperca435922010-12-20 12:26:32 -080013#include <pwd.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080014#include <stdarg.h>
Garrett Cooperca435922010-12-20 12:26:32 -080015#include <stdlib.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080016#include <unistd.h>
Zeng Linggang5ff25ef2014-05-06 15:49:42 +080017#include <malloc.h>
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080018#include "test.h"
Garrett Cooperca435922010-12-20 12:26:32 -080019#include "safe_macros.h"
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080020
Wanlong Gao354ebb42012-12-07 10:10:04 +080021char *safe_basename(const char *file, const int lineno,
22 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080023{
24 char *rval;
25
26 rval = basename(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020027 if (rval == NULL) {
28 tst_brkm(TBROK | TERRNO, cleanup_fn,
29 "%s:%d: basename(%s) failed",
30 file, lineno, path);
31 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080032
Mats Liljegren49e86152014-04-16 19:27:58 +020033 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080034}
35
36int
Wanlong Gao354ebb42012-12-07 10:10:04 +080037safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void),
38 const char *path)
Garrett Cooper4a3c6582010-12-21 07:07:01 -080039{
40 int rval;
41
42 rval = chdir(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020043 if (rval == -1) {
44 tst_brkm(TBROK | TERRNO, cleanup_fn,
45 "%s:%d: chdir(%s) failed",
46 file, lineno, path);
47 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -080048
Mats Liljegren49e86152014-04-16 19:27:58 +020049 return rval;
Garrett Cooper4a3c6582010-12-21 07:07:01 -080050}
51
52int
Wanlong Gao354ebb42012-12-07 10:10:04 +080053safe_close(const char *file, const int lineno, void (*cleanup_fn) (void),
54 int fildes)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080055{
56 int rval;
57
58 rval = close(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +020059 if (rval == -1) {
60 tst_brkm(TBROK | TERRNO, cleanup_fn,
61 "%s:%d: close(%d) failed",
62 file, lineno, fildes);
63 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080064
Mats Liljegren49e86152014-04-16 19:27:58 +020065 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080066}
67
68int
Wanlong Gao354ebb42012-12-07 10:10:04 +080069safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void),
Alexey Kodanev5071db52015-04-15 11:03:29 +030070 const char *pathname, mode_t mode)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080071{
72 int rval;
73
74 rval = creat(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +020075 if (rval == -1) {
76 tst_brkm(TBROK | TERRNO, cleanup_fn,
77 "%s:%d: creat(%s,0%o) failed",
78 file, lineno, pathname, mode);
79 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080080
Mats Liljegren49e86152014-04-16 19:27:58 +020081 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080082}
83
Wanlong Gao354ebb42012-12-07 10:10:04 +080084char *safe_dirname(const char *file, const int lineno,
85 void (*cleanup_fn) (void), char *path)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080086{
87 char *rval;
88
89 rval = dirname(path);
Mats Liljegren49e86152014-04-16 19:27:58 +020090 if (rval == NULL) {
91 tst_brkm(TBROK | TERRNO, cleanup_fn,
92 "%s:%d: dirname(%s) failed",
93 file, lineno, path);
94 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080095
Mats Liljegren49e86152014-04-16 19:27:58 +020096 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -080097}
98
Wanlong Gao354ebb42012-12-07 10:10:04 +080099char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
100 char *buf, size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800101{
102 char *rval;
103
104 rval = getcwd(buf, size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200105 if (rval == NULL) {
106 tst_brkm(TBROK | TERRNO, cleanup_fn,
107 "%s:%d: getcwd(%p,%zu) failed",
108 file, lineno, buf, size);
109 }
Garrett Cooperca435922010-12-20 12:26:32 -0800110
Mats Liljegren49e86152014-04-16 19:27:58 +0200111 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800112}
113
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114struct passwd *safe_getpwnam(const char *file, const int lineno,
115 void (*cleanup_fn) (void), const char *name)
Garrett Cooperca435922010-12-20 12:26:32 -0800116{
117 struct passwd *rval;
118
119 rval = getpwnam(name);
Mats Liljegren49e86152014-04-16 19:27:58 +0200120 if (rval == NULL) {
121 tst_brkm(TBROK | TERRNO, cleanup_fn,
122 "%s:%d: getpwnam(%s) failed",
123 file, lineno, name);
124 }
Garrett Cooperca435922010-12-20 12:26:32 -0800125
Mats Liljegren49e86152014-04-16 19:27:58 +0200126 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800127}
128
Caspar Zhang817c7822011-06-30 01:50:33 +0800129int
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
131 int who, struct rusage *usage)
Caspar Zhang817c7822011-06-30 01:50:33 +0800132{
133 int rval;
134
135 rval = getrusage(who, usage);
Mats Liljegren49e86152014-04-16 19:27:58 +0200136 if (rval == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200138 "%s:%d: getrusage(%d,%p) failed",
139 file, lineno, who, usage);
140 }
Caspar Zhang817c7822011-06-30 01:50:33 +0800141
142 return rval;
143}
144
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void),
146 size_t size)
Garrett Cooperca435922010-12-20 12:26:32 -0800147{
148 void *rval;
149
150 rval = malloc(size);
Mats Liljegren49e86152014-04-16 19:27:58 +0200151 if (rval == NULL) {
152 tst_brkm(TBROK | TERRNO, cleanup_fn,
153 "%s:%d: malloc(%zu) failed",
154 file, lineno, size);
155 }
Garrett Cooperca435922010-12-20 12:26:32 -0800156
Mats Liljegren49e86152014-04-16 19:27:58 +0200157 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800158}
159
Mats Liljegren49e86152014-04-16 19:27:58 +0200160int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void),
161 const char *pathname, mode_t mode)
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800162{
163 int rval;
164
165 rval = mkdir(pathname, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200166 if (rval == -1) {
167 tst_brkm(TBROK | TERRNO, cleanup_fn,
168 "%s:%d: mkdir(%s,0%o) failed",
169 file, lineno, pathname, mode);
170 }
Garrett Cooper4a3c6582010-12-21 07:07:01 -0800171
172 return (rval);
173}
174
Cyril Hrubis97499c22014-05-07 14:54:22 +0200175int safe_rmdir(const char *file, const int lineno, void (*cleanup_fn) (void),
176 const char *pathname)
177{
178 int rval;
179
180 rval = rmdir(pathname);
181 if (rval == -1) {
182 tst_brkm(TBROK | TERRNO, cleanup_fn,
183 "%s:%d: rmdir(%s) failed",
184 file, lineno, pathname);
185 }
186
187 return (rval);
188}
189
Mats Liljegren49e86152014-04-16 19:27:58 +0200190int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
191 void *addr, size_t length)
Garrett Cooperca435922010-12-20 12:26:32 -0800192{
193 int rval;
194
195 rval = munmap(addr, length);
Mats Liljegren49e86152014-04-16 19:27:58 +0200196 if (rval == -1) {
197 tst_brkm(TBROK | TERRNO, cleanup_fn,
198 "%s:%d: munmap(%p,%zu) failed",
199 file, lineno, addr, length);
200 }
Garrett Cooperca435922010-12-20 12:26:32 -0800201
Mats Liljegren49e86152014-04-16 19:27:58 +0200202 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800203}
204
Mats Liljegren49e86152014-04-16 19:27:58 +0200205int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
206 const char *pathname, int oflags, ...)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800207{
208 va_list ap;
209 int rval;
210 mode_t mode;
211
212 va_start(ap, oflags);
213 mode = va_arg(ap, mode_t);
214 va_end(ap);
215
216 rval = open(pathname, oflags, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200217 if (rval == -1) {
218 tst_brkm(TBROK | TERRNO, cleanup_fn,
219 "%s:%d: open(%s,%d,0%o) failed",
220 file, lineno, pathname, oflags, mode);
221 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800222
Mats Liljegren49e86152014-04-16 19:27:58 +0200223 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800224}
225
Mats Liljegren49e86152014-04-16 19:27:58 +0200226int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
227 int fildes[2])
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800228{
229 int rval;
230
231 rval = pipe(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +0200232 if (rval == -1) {
233 tst_brkm(TBROK | TERRNO, cleanup_fn,
234 "%s:%d: pipe({%d,%d}) failed",
235 file, lineno, fildes[0], fildes[1]);
236 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800237
Mats Liljegren49e86152014-04-16 19:27:58 +0200238 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800239}
240
Mats Liljegren49e86152014-04-16 19:27:58 +0200241ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
242 char len_strict, int fildes, void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800243{
244 ssize_t rval;
245
246 rval = read(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200247 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
248 tst_brkm(TBROK | TERRNO, cleanup_fn,
249 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
250 file, lineno, fildes, buf, nbyte, rval);
251 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800252
Mats Liljegren49e86152014-04-16 19:27:58 +0200253 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800254}
255
Mats Liljegren49e86152014-04-16 19:27:58 +0200256int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
257 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800258{
259 int rval;
260
261 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200262 if (rval == -1) {
263 tst_brkm(TBROK | TERRNO, cleanup_fn,
264 "%s:%d: setegid(%u) failed",
265 file, lineno, (unsigned) egid);
266 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800267
Mats Liljegren49e86152014-04-16 19:27:58 +0200268 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800269}
270
Mats Liljegren49e86152014-04-16 19:27:58 +0200271int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
272 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800273{
274 int rval;
275
276 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200277 if (rval == -1) {
278 tst_brkm(TBROK | TERRNO, cleanup_fn,
279 "%s:%d: seteuid(%u) failed",
280 file, lineno, (unsigned) euid);
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_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
287 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800288{
289 int rval;
290
291 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200292 if (rval == -1) {
293 tst_brkm(TBROK | TERRNO, cleanup_fn,
294 "%s:%d: setgid(%u) failed",
295 file, lineno, (unsigned) gid);
296 }
Garrett Cooperca435922010-12-20 12:26:32 -0800297
Mats Liljegren49e86152014-04-16 19:27:58 +0200298 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800299}
300
Mats Liljegren49e86152014-04-16 19:27:58 +0200301int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
302 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800303{
304 int rval;
305
306 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200307 if (rval == -1) {
308 tst_brkm(TBROK | TERRNO, cleanup_fn,
309 "%s:%d: setuid(%u) failed",
310 file, lineno, (unsigned) uid);
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
Zeng Linggang91d67422014-04-25 10:46:32 +0800316int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void),
317 uid_t *ruid, uid_t *euid, uid_t *suid)
318{
319 int rval;
320
321 rval = getresuid(ruid, euid, suid);
322 if (rval == -1) {
323 tst_brkm(TBROK | TERRNO, cleanup_fn,
324 "%s:%d: getresuid(%p, %p, %p) failed",
325 file, lineno, ruid, euid, suid);
326 }
327
328 return rval;
329}
330
331int safe_getresgid(const char *file, const int lineno, void (*cleanup_fn)(void),
332 gid_t *rgid, gid_t *egid, gid_t *sgid)
333{
334 int rval;
335
336 rval = getresgid(rgid, egid, sgid);
337 if (rval == -1) {
338 tst_brkm(TBROK | TERRNO, cleanup_fn,
339 "%s:%d: getresgid(%p, %p, %p) failed",
340 file, lineno, rgid, egid, sgid);
341 }
342
343 return rval;
344}
345
Mats Liljegren49e86152014-04-16 19:27:58 +0200346int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
347 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800348{
349 int rval;
350
351 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200352 if (rval == -1) {
353 tst_brkm(TBROK | TERRNO, cleanup_fn,
354 "%s:%d: unlink(%s) failed",
355 file, lineno, pathname);
356 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800357
Mats Liljegren49e86152014-04-16 19:27:58 +0200358 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800359}
360
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200361
362int safe_link(const char *file, const int lineno,
363 void (cleanup_fn)(void), const char *oldpath,
364 const char *newpath)
365{
366 int rval;
367
368 rval = link(oldpath, newpath);
369
370 if (rval == -1) {
371 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200372 "%s:%d: link(%s,%s) failed",
373 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200374 }
375
376 return rval;
377}
378
Alexey Kodanevf3f4d332016-01-28 13:10:58 +0300379int safe_linkat(const char *file, const int lineno,
380 void (cleanup_fn)(void), int olddirfd, const char *oldpath,
381 int newdirfd, const char *newpath, int flags)
382{
383 int rval;
384
385 rval = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
386
387 if (rval == -1) {
388 tst_brkm(TBROK | TERRNO, cleanup_fn,
389 "%s:%d: linkat(%d,%s,%d,%s,%d) failed",
390 file, lineno, olddirfd, oldpath, newdirfd,
391 newpath, flags);
392 }
393
394 return rval;
395}
396
Alexey Kodanev8bbb1d52016-01-28 14:10:54 +0300397ssize_t safe_readlink(const char *file, const int lineno,
398 void (cleanup_fn)(void), const char *path,
399 char *buf, size_t bufsize)
400{
401 ssize_t rval;
402
403 rval = readlink(path, buf, bufsize);
404
405 if (rval == -1) {
406 tst_brkm(TBROK | TERRNO, cleanup_fn,
407 "%s:%d: readlink(%s,%p,%zu) failed",
408 file, lineno, path, buf, bufsize);
409 }
410
411 return rval;
412}
413
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200414int safe_symlink(const char *file, const int lineno,
415 void (cleanup_fn)(void), const char *oldpath,
416 const char *newpath)
417{
418 int rval;
419
420 rval = symlink(oldpath, newpath);
421
422 if (rval == -1) {
423 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200424 "%s:%d: symlink(%s,%s) failed",
425 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200426 }
427
428 return rval;
429}
430
Mats Liljegren49e86152014-04-16 19:27:58 +0200431ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
432 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800433{
434 ssize_t rval;
435
436 rval = write(fildes, buf, nbyte);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800437 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200438 tst_brkm(TBROK | TERRNO, cleanup_fn,
439 "%s:%d: write(%d,%p,%zu) failed",
440 file, lineno, fildes, buf, rval);
441 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800442
Mats Liljegren49e86152014-04-16 19:27:58 +0200443 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800444}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100445
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800446long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800447 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800448{
449 long rval;
450 char *endptr;
451
452 errno = 0;
453 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200454
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800455 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200456 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800457 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200458 "%s:%d: strtol(%s) failed", file, lineno, str);
459 }
460
461 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800462 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200463 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
464 }
465
466 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800467 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200468 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
469 file, lineno, str, rval, min, max);
470 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800471
472 return rval;
473}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800474
Wanlong Gao354ebb42012-12-07 10:10:04 +0800475unsigned long safe_strtoul(const char *file, const int lineno,
476 void (cleanup_fn) (void), char *str,
477 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800478{
479 unsigned long rval;
480 char *endptr;
481
482 errno = 0;
483 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200484
Zhouping Liu2b73a152012-07-07 23:14:39 +0800485 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200486 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800487 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200488 "%s:%d: strtoul(%s) failed", file, lineno, str);
489 }
490
491 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800492 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200493 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
494 file, lineno, str, rval, min, max);
495 }
496
497 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800498 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800499 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200500 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800501
502 return rval;
503}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800504
505long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800506 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800507{
508 long rval;
509 errno = 0;
510
511 rval = sysconf(name);
512
513 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200514 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800515 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200516 "%s:%d: sysconf(%d) failed",
517 file, lineno, name);
518 } else {
519 tst_resm(TINFO, "%s:%d: sysconf(%d): "
520 "queried option is not available"
521 " or there is no definite limit",
522 file, lineno, name);
523 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800524 }
525
526 return rval;
527}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800528
Cyril Hrubisc4592352014-03-04 16:41:02 +0100529int safe_chmod(const char *file, const int lineno,
530 void (cleanup_fn)(void), const char *path, mode_t mode)
531{
532 int rval;
533
534 rval = chmod(path, mode);
535
536 if (rval == -1) {
537 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200538 "%s:%d: chmod(%s,0%o) failed",
539 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100540 }
541
542 return rval;
543}
544
545int safe_fchmod(const char *file, const int lineno,
546 void (cleanup_fn)(void), int fd, mode_t mode)
547{
548 int rval;
549
550 rval = fchmod(fd, mode);
551
552 if (rval == -1) {
553 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200554 "%s:%d: fchmod(%d,0%o) failed",
555 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100556 }
557
558 return rval;
559}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200560
Xing Gu92a14592014-06-13 10:53:17 +0800561int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
562 const char *path, uid_t owner, gid_t group)
563{
564 int rval;
565
566 rval = chown(path, owner, group);
567
568 if (rval == -1) {
569 tst_brkm(TBROK | TERRNO, cleanup_fn,
570 "%s:%d: chown(%s,%d,%d) failed",
571 file, lineno, path, owner, group);
572 }
573
574 return rval;
575}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200576
577int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
578 int fd, uid_t owner, gid_t group)
579{
580 int rval;
581
582 rval = fchown(fd, owner, group);
583
584 if (rval == -1) {
585 tst_brkm(TBROK | TERRNO, cleanup_fn,
586 "%s:%d: fchown(%d,%d,%d) failed",
587 file, lineno, fd, owner, group);
588 }
589
590 return rval;
591}
592
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200593pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
594 int *status)
595{
596 pid_t rval;
597
598 rval = wait(status);
599 if (rval == -1) {
600 tst_brkm(TBROK | TERRNO, cleanup_fn,
601 "%s:%d: wait(%p) failed",
602 file, lineno, status);
603 }
604
605 return rval;
606}
607
608pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
609 pid_t pid, int *status, int opts)
610{
611 pid_t rval;
612
613 rval = waitpid(pid, status, opts);
614 if (rval == -1) {
615 tst_brkm(TBROK | TERRNO, cleanup_fn,
616 "%s:%d: waitpid(%d,%p,%d) failed",
617 file, lineno, pid, status, opts);
618 }
619
620 return rval;
621}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800622
623void *safe_memalign(const char *file, const int lineno,
624 void (*cleanup_fn) (void), size_t alignment, size_t size)
625{
626 void *rval;
627
628 rval = memalign(alignment, size);
629 if (rval == NULL)
630 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
631 file, lineno);
632
633 return rval;
634}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800635
636int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
637 pid_t pid, int sig)
638{
639 int rval;
640
641 rval = kill(pid, sig);
642
643 if (rval == -1) {
644 tst_brkm(TBROK | TERRNO, cleanup_fn,
645 "%s:%d: kill(%d,%s) failed",
646 file, lineno, pid, tst_strsig(sig));
647 }
648
649 return rval;
650}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200651
652int safe_mkfifo(const char *file, const int lineno,
653 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
654{
655 int rval;
656
657 rval = mkfifo(pathname, mode);
658
659 if (rval == -1) {
660 tst_brkm(TBROK | TERRNO, cleanup_fn,
661 "%s:%d: mkfifo(%s, 0%o) failed",
662 file, lineno, pathname, mode);
663 }
664
665 return rval;
666}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800667
668int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
669 const char *oldpath, const char *newpath)
670{
671 int rval;
672
673 rval = rename(oldpath, newpath);
674
675 if (rval == -1) {
676 tst_brkm(TBROK | TERRNO, cleanup_fn,
677 "%s:%d: rename(%s, %s) failed",
678 file, lineno, oldpath, newpath);
679 }
680
681 return rval;
682}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200683
684int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
685 const char *source, const char *target,
686 const char *filesystemtype, unsigned long mountflags,
687 const void *data)
688{
689 int rval;
690
691 rval = mount(source, target, filesystemtype, mountflags, data);
692
693 if (rval == -1) {
694 tst_brkm(TBROK | TERRNO, cleanup_fn,
695 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
696 file, lineno, source, target, filesystemtype,
697 mountflags, data);
698 }
699
700 return rval;
701}
702
703int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
704 const char *target)
705{
706 int rval;
707
708 rval = umount(target);
709
710 if (rval == -1) {
711 tst_brkm(TBROK | TERRNO, cleanup_fn,
712 "%s:%d: umount(%s) failed",
713 file, lineno, target);
714 }
715
716 return rval;
717}
Cyril Hrubis9138a012015-02-10 15:44:45 +0100718
719DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
720 const char *name)
721{
722 DIR *rval;
723
724 rval = opendir(name);
725
726 if (!rval) {
727 tst_brkm(TBROK | TERRNO, cleanup_fn,
728 "%s:%d: opendir(%s) failed", file, lineno, name);
729 }
730
731 return rval;
732}
733
734int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
735 DIR *dirp)
736{
737 int rval;
738
739 rval = closedir(dirp);
740
741 if (rval) {
742 tst_brkm(TBROK | TERRNO, cleanup_fn,
743 "%s:%d: closedir(%p) failed", file, lineno, dirp);
744 }
745
746 return rval;
747}
748
749struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
750 DIR *dirp)
751{
752 struct dirent *rval;
753 int err = errno;
754
755 errno = 0;
756 rval = readdir(dirp);
757
758 if (!rval && errno) {
759 tst_brkm(TBROK | TERRNO, cleanup_fn,
760 "%s:%d: readdir(%p) failed", file, lineno, dirp);
761 }
762
763 errno = err;
764 return rval;
765}
Cyril Hrubisaf5a4b12016-11-07 13:05:00 +0100766
767int safe_getpriority(const char *file, const int lineno, int which, id_t who)
768{
769 int rval, err = errno;
770
771 errno = 0;
772 rval = getpriority(which, who);
773 if (errno) {
774 tst_brkm(TBROK | TERRNO, NULL,
775 "%s:%d getpriority(%i, %i) failed",
776 file, lineno, which, who);
777 }
778
779 errno = err;
780 return rval;
781}
Dejan Jovicevic10552812016-11-15 13:19:33 +0100782
783int safe_setxattr(const char *file, const int lineno, const char *path,
784 const char *name, const void *value, size_t size, int flags)
785{
786 int rval;
787
788 rval = setxattr(path, name, value, size, flags);
789
790 if (rval) {
791 if (errno == ENOTSUP) {
792 tst_brkm(TCONF, NULL,
793 "%s:%d: no xattr support in fs or mounted "
794 "without user_xattr option", file, lineno);
795 }
796
797 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: setxattr() failed",
798 file, lineno);
799 }
800
801 return rval;
802}
803
804int safe_lsetxattr(const char *file, const int lineno, const char *path,
805 const char *name, const void *value, size_t size, int flags)
806{
807 int rval;
808
809 rval = lsetxattr(path, name, value, size, flags);
810
811 if (rval) {
812 if (errno == ENOTSUP) {
813 tst_brkm(TCONF, NULL,
814 "%s:%d: no xattr support in fs or mounted "
815 "without user_xattr option", file, lineno);
816 }
817
818 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: lsetxattr() failed",
819 file, lineno);
820 }
821
822 return rval;
823}
824
825int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
826 const void *value, size_t size, int flags)
827{
828 int rval;
829
830 rval = fsetxattr(fd, name, value, size, flags);
831
832 if (rval) {
833 if (errno == ENOTSUP) {
834 tst_brkm(TCONF, NULL,
835 "%s:%d: no xattr support in fs or mounted "
836 "without user_xattr option", file, lineno);
837 }
838
839 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: fsetxattr() failed",
840 file, lineno);
841 }
842
843 return rval;
844}