blob: d696a0b8dbee0be8c749a4760ecdc2a693db9511 [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);
Steven Jackson1750b472016-12-15 22:03:53 +0000213
214 /* Android's NDK's mode_t is smaller than an int, which results in
215 * SIGILL here when passing the mode_t type.
216 */
217 mode = va_arg(ap, int);
218
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800219 va_end(ap);
220
221 rval = open(pathname, oflags, mode);
Mats Liljegren49e86152014-04-16 19:27:58 +0200222 if (rval == -1) {
223 tst_brkm(TBROK | TERRNO, cleanup_fn,
224 "%s:%d: open(%s,%d,0%o) failed",
225 file, lineno, pathname, oflags, mode);
226 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800227
Mats Liljegren49e86152014-04-16 19:27:58 +0200228 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800229}
230
Mats Liljegren49e86152014-04-16 19:27:58 +0200231int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
232 int fildes[2])
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800233{
234 int rval;
235
236 rval = pipe(fildes);
Mats Liljegren49e86152014-04-16 19:27:58 +0200237 if (rval == -1) {
238 tst_brkm(TBROK | TERRNO, cleanup_fn,
239 "%s:%d: pipe({%d,%d}) failed",
240 file, lineno, fildes[0], fildes[1]);
241 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800242
Mats Liljegren49e86152014-04-16 19:27:58 +0200243 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800244}
245
Mats Liljegren49e86152014-04-16 19:27:58 +0200246ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
247 char len_strict, int fildes, void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800248{
249 ssize_t rval;
250
251 rval = read(fildes, buf, nbyte);
Mats Liljegren49e86152014-04-16 19:27:58 +0200252 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
253 tst_brkm(TBROK | TERRNO, cleanup_fn,
254 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
255 file, lineno, fildes, buf, nbyte, rval);
256 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800257
Mats Liljegren49e86152014-04-16 19:27:58 +0200258 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800259}
260
Mats Liljegren49e86152014-04-16 19:27:58 +0200261int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
262 gid_t egid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800263{
264 int rval;
265
266 rval = setegid(egid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200267 if (rval == -1) {
268 tst_brkm(TBROK | TERRNO, cleanup_fn,
269 "%s:%d: setegid(%u) failed",
270 file, lineno, (unsigned) egid);
271 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800272
Mats Liljegren49e86152014-04-16 19:27:58 +0200273 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800274}
275
Mats Liljegren49e86152014-04-16 19:27:58 +0200276int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
277 uid_t euid)
Garrett Cooper400c8362010-12-20 20:02:01 -0800278{
279 int rval;
280
281 rval = seteuid(euid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200282 if (rval == -1) {
283 tst_brkm(TBROK | TERRNO, cleanup_fn,
284 "%s:%d: seteuid(%u) failed",
285 file, lineno, (unsigned) euid);
286 }
Garrett Cooper400c8362010-12-20 20:02:01 -0800287
Mats Liljegren49e86152014-04-16 19:27:58 +0200288 return rval;
Garrett Cooper400c8362010-12-20 20:02:01 -0800289}
290
Mats Liljegren49e86152014-04-16 19:27:58 +0200291int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
292 gid_t gid)
Garrett Cooperca435922010-12-20 12:26:32 -0800293{
294 int rval;
295
296 rval = setgid(gid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200297 if (rval == -1) {
298 tst_brkm(TBROK | TERRNO, cleanup_fn,
299 "%s:%d: setgid(%u) failed",
300 file, lineno, (unsigned) gid);
301 }
Garrett Cooperca435922010-12-20 12:26:32 -0800302
Mats Liljegren49e86152014-04-16 19:27:58 +0200303 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800304}
305
Mats Liljegren49e86152014-04-16 19:27:58 +0200306int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
307 uid_t uid)
Garrett Cooperca435922010-12-20 12:26:32 -0800308{
309 int rval;
310
311 rval = setuid(uid);
Mats Liljegren49e86152014-04-16 19:27:58 +0200312 if (rval == -1) {
313 tst_brkm(TBROK | TERRNO, cleanup_fn,
314 "%s:%d: setuid(%u) failed",
315 file, lineno, (unsigned) uid);
316 }
Garrett Cooperca435922010-12-20 12:26:32 -0800317
Mats Liljegren49e86152014-04-16 19:27:58 +0200318 return rval;
Garrett Cooperca435922010-12-20 12:26:32 -0800319}
320
Zeng Linggang91d67422014-04-25 10:46:32 +0800321int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void),
322 uid_t *ruid, uid_t *euid, uid_t *suid)
323{
324 int rval;
325
326 rval = getresuid(ruid, euid, suid);
327 if (rval == -1) {
328 tst_brkm(TBROK | TERRNO, cleanup_fn,
329 "%s:%d: getresuid(%p, %p, %p) failed",
330 file, lineno, ruid, euid, suid);
331 }
332
333 return rval;
334}
335
336int safe_getresgid(const char *file, const int lineno, void (*cleanup_fn)(void),
337 gid_t *rgid, gid_t *egid, gid_t *sgid)
338{
339 int rval;
340
341 rval = getresgid(rgid, egid, sgid);
342 if (rval == -1) {
343 tst_brkm(TBROK | TERRNO, cleanup_fn,
344 "%s:%d: getresgid(%p, %p, %p) failed",
345 file, lineno, rgid, egid, sgid);
346 }
347
348 return rval;
349}
350
Mats Liljegren49e86152014-04-16 19:27:58 +0200351int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
352 const char *pathname)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800353{
354 int rval;
355
356 rval = unlink(pathname);
Mats Liljegren49e86152014-04-16 19:27:58 +0200357 if (rval == -1) {
358 tst_brkm(TBROK | TERRNO, cleanup_fn,
359 "%s:%d: unlink(%s) failed",
360 file, lineno, pathname);
361 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800362
Mats Liljegren49e86152014-04-16 19:27:58 +0200363 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800364}
365
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200366
367int safe_link(const char *file, const int lineno,
368 void (cleanup_fn)(void), const char *oldpath,
369 const char *newpath)
370{
371 int rval;
372
373 rval = link(oldpath, newpath);
374
375 if (rval == -1) {
376 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200377 "%s:%d: link(%s,%s) failed",
378 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200379 }
380
381 return rval;
382}
383
Alexey Kodanevf3f4d332016-01-28 13:10:58 +0300384int safe_linkat(const char *file, const int lineno,
385 void (cleanup_fn)(void), int olddirfd, const char *oldpath,
386 int newdirfd, const char *newpath, int flags)
387{
388 int rval;
389
390 rval = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
391
392 if (rval == -1) {
393 tst_brkm(TBROK | TERRNO, cleanup_fn,
394 "%s:%d: linkat(%d,%s,%d,%s,%d) failed",
395 file, lineno, olddirfd, oldpath, newdirfd,
396 newpath, flags);
397 }
398
399 return rval;
400}
401
Alexey Kodanev8bbb1d52016-01-28 14:10:54 +0300402ssize_t safe_readlink(const char *file, const int lineno,
403 void (cleanup_fn)(void), const char *path,
404 char *buf, size_t bufsize)
405{
406 ssize_t rval;
407
408 rval = readlink(path, buf, bufsize);
409
410 if (rval == -1) {
411 tst_brkm(TBROK | TERRNO, cleanup_fn,
412 "%s:%d: readlink(%s,%p,%zu) failed",
413 file, lineno, path, buf, bufsize);
414 }
415
416 return rval;
417}
418
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200419int safe_symlink(const char *file, const int lineno,
420 void (cleanup_fn)(void), const char *oldpath,
421 const char *newpath)
422{
423 int rval;
424
425 rval = symlink(oldpath, newpath);
426
427 if (rval == -1) {
428 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200429 "%s:%d: symlink(%s,%s) failed",
430 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200431 }
432
433 return rval;
434}
435
Mats Liljegren49e86152014-04-16 19:27:58 +0200436ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
437 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800438{
439 ssize_t rval;
440
441 rval = write(fildes, buf, nbyte);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800442 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200443 tst_brkm(TBROK | TERRNO, cleanup_fn,
444 "%s:%d: write(%d,%p,%zu) failed",
445 file, lineno, fildes, buf, rval);
446 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800447
Mats Liljegren49e86152014-04-16 19:27:58 +0200448 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800449}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100450
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800451long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800452 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800453{
454 long rval;
455 char *endptr;
456
457 errno = 0;
458 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200459
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800460 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200461 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800462 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200463 "%s:%d: strtol(%s) failed", file, lineno, str);
464 }
465
466 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
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): Invalid value", file, lineno, str);
469 }
470
471 if (rval > max || rval < min) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800472 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200473 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
474 file, lineno, str, rval, min, max);
475 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800476
477 return rval;
478}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800479
Wanlong Gao354ebb42012-12-07 10:10:04 +0800480unsigned long safe_strtoul(const char *file, const int lineno,
481 void (cleanup_fn) (void), char *str,
482 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800483{
484 unsigned long rval;
485 char *endptr;
486
487 errno = 0;
488 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200489
Zhouping Liu2b73a152012-07-07 23:14:39 +0800490 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200491 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800492 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200493 "%s:%d: strtoul(%s) failed", file, lineno, str);
494 }
495
496 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800497 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200498 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
499 file, lineno, str, rval, min, max);
500 }
501
502 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800503 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800504 "Invalid value: '%s' at %s:%d", str, file, lineno);
Mats Liljegren49e86152014-04-16 19:27:58 +0200505 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800506
507 return rval;
508}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800509
510long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800511 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800512{
513 long rval;
514 errno = 0;
515
516 rval = sysconf(name);
517
518 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200519 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800520 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200521 "%s:%d: sysconf(%d) failed",
522 file, lineno, name);
523 } else {
524 tst_resm(TINFO, "%s:%d: sysconf(%d): "
525 "queried option is not available"
526 " or there is no definite limit",
527 file, lineno, name);
528 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800529 }
530
531 return rval;
532}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800533
Cyril Hrubisc4592352014-03-04 16:41:02 +0100534int safe_chmod(const char *file, const int lineno,
535 void (cleanup_fn)(void), const char *path, mode_t mode)
536{
537 int rval;
538
539 rval = chmod(path, mode);
540
541 if (rval == -1) {
542 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200543 "%s:%d: chmod(%s,0%o) failed",
544 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100545 }
546
547 return rval;
548}
549
550int safe_fchmod(const char *file, const int lineno,
551 void (cleanup_fn)(void), int fd, mode_t mode)
552{
553 int rval;
554
555 rval = fchmod(fd, mode);
556
557 if (rval == -1) {
558 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200559 "%s:%d: fchmod(%d,0%o) failed",
560 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100561 }
562
563 return rval;
564}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200565
Xing Gu92a14592014-06-13 10:53:17 +0800566int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
567 const char *path, uid_t owner, gid_t group)
568{
569 int rval;
570
571 rval = chown(path, owner, group);
572
573 if (rval == -1) {
574 tst_brkm(TBROK | TERRNO, cleanup_fn,
575 "%s:%d: chown(%s,%d,%d) failed",
576 file, lineno, path, owner, group);
577 }
578
579 return rval;
580}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200581
582int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
583 int fd, uid_t owner, gid_t group)
584{
585 int rval;
586
587 rval = fchown(fd, owner, group);
588
589 if (rval == -1) {
590 tst_brkm(TBROK | TERRNO, cleanup_fn,
591 "%s:%d: fchown(%d,%d,%d) failed",
592 file, lineno, fd, owner, group);
593 }
594
595 return rval;
596}
597
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200598pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
599 int *status)
600{
601 pid_t rval;
602
603 rval = wait(status);
604 if (rval == -1) {
605 tst_brkm(TBROK | TERRNO, cleanup_fn,
606 "%s:%d: wait(%p) failed",
607 file, lineno, status);
608 }
609
610 return rval;
611}
612
613pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
614 pid_t pid, int *status, int opts)
615{
616 pid_t rval;
617
618 rval = waitpid(pid, status, opts);
619 if (rval == -1) {
620 tst_brkm(TBROK | TERRNO, cleanup_fn,
621 "%s:%d: waitpid(%d,%p,%d) failed",
622 file, lineno, pid, status, opts);
623 }
624
625 return rval;
626}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800627
628void *safe_memalign(const char *file, const int lineno,
629 void (*cleanup_fn) (void), size_t alignment, size_t size)
630{
631 void *rval;
632
633 rval = memalign(alignment, size);
634 if (rval == NULL)
635 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
636 file, lineno);
637
638 return rval;
639}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800640
641int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
642 pid_t pid, int sig)
643{
644 int rval;
645
646 rval = kill(pid, sig);
647
648 if (rval == -1) {
649 tst_brkm(TBROK | TERRNO, cleanup_fn,
650 "%s:%d: kill(%d,%s) failed",
651 file, lineno, pid, tst_strsig(sig));
652 }
653
654 return rval;
655}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200656
657int safe_mkfifo(const char *file, const int lineno,
658 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
659{
660 int rval;
661
662 rval = mkfifo(pathname, mode);
663
664 if (rval == -1) {
665 tst_brkm(TBROK | TERRNO, cleanup_fn,
666 "%s:%d: mkfifo(%s, 0%o) failed",
667 file, lineno, pathname, mode);
668 }
669
670 return rval;
671}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800672
673int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
674 const char *oldpath, const char *newpath)
675{
676 int rval;
677
678 rval = rename(oldpath, newpath);
679
680 if (rval == -1) {
681 tst_brkm(TBROK | TERRNO, cleanup_fn,
682 "%s:%d: rename(%s, %s) failed",
683 file, lineno, oldpath, newpath);
684 }
685
686 return rval;
687}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200688
689int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
690 const char *source, const char *target,
691 const char *filesystemtype, unsigned long mountflags,
692 const void *data)
693{
694 int rval;
695
696 rval = mount(source, target, filesystemtype, mountflags, data);
697
698 if (rval == -1) {
699 tst_brkm(TBROK | TERRNO, cleanup_fn,
700 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
701 file, lineno, source, target, filesystemtype,
702 mountflags, data);
703 }
704
705 return rval;
706}
707
708int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
709 const char *target)
710{
711 int rval;
712
713 rval = umount(target);
714
715 if (rval == -1) {
716 tst_brkm(TBROK | TERRNO, cleanup_fn,
717 "%s:%d: umount(%s) failed",
718 file, lineno, target);
719 }
720
721 return rval;
722}
Cyril Hrubis9138a012015-02-10 15:44:45 +0100723
724DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
725 const char *name)
726{
727 DIR *rval;
728
729 rval = opendir(name);
730
731 if (!rval) {
732 tst_brkm(TBROK | TERRNO, cleanup_fn,
733 "%s:%d: opendir(%s) failed", file, lineno, name);
734 }
735
736 return rval;
737}
738
739int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
740 DIR *dirp)
741{
742 int rval;
743
744 rval = closedir(dirp);
745
746 if (rval) {
747 tst_brkm(TBROK | TERRNO, cleanup_fn,
748 "%s:%d: closedir(%p) failed", file, lineno, dirp);
749 }
750
751 return rval;
752}
753
754struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
755 DIR *dirp)
756{
757 struct dirent *rval;
758 int err = errno;
759
760 errno = 0;
761 rval = readdir(dirp);
762
763 if (!rval && errno) {
764 tst_brkm(TBROK | TERRNO, cleanup_fn,
765 "%s:%d: readdir(%p) failed", file, lineno, dirp);
766 }
767
768 errno = err;
769 return rval;
770}
Cyril Hrubisaf5a4b12016-11-07 13:05:00 +0100771
772int safe_getpriority(const char *file, const int lineno, int which, id_t who)
773{
774 int rval, err = errno;
775
776 errno = 0;
777 rval = getpriority(which, who);
778 if (errno) {
779 tst_brkm(TBROK | TERRNO, NULL,
780 "%s:%d getpriority(%i, %i) failed",
781 file, lineno, which, who);
782 }
783
784 errno = err;
785 return rval;
786}
Dejan Jovicevic10552812016-11-15 13:19:33 +0100787
788int safe_setxattr(const char *file, const int lineno, const char *path,
789 const char *name, const void *value, size_t size, int flags)
790{
791 int rval;
792
793 rval = setxattr(path, name, value, size, flags);
794
795 if (rval) {
796 if (errno == ENOTSUP) {
797 tst_brkm(TCONF, NULL,
798 "%s:%d: no xattr support in fs or mounted "
799 "without user_xattr option", file, lineno);
800 }
801
802 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: setxattr() failed",
803 file, lineno);
804 }
805
806 return rval;
807}
808
809int safe_lsetxattr(const char *file, const int lineno, const char *path,
810 const char *name, const void *value, size_t size, int flags)
811{
812 int rval;
813
814 rval = lsetxattr(path, name, value, size, flags);
815
816 if (rval) {
817 if (errno == ENOTSUP) {
818 tst_brkm(TCONF, NULL,
819 "%s:%d: no xattr support in fs or mounted "
820 "without user_xattr option", file, lineno);
821 }
822
823 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: lsetxattr() failed",
824 file, lineno);
825 }
826
827 return rval;
828}
829
830int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
831 const void *value, size_t size, int flags)
832{
833 int rval;
834
835 rval = fsetxattr(fd, name, value, size, flags);
836
837 if (rval) {
838 if (errno == ENOTSUP) {
839 tst_brkm(TCONF, NULL,
840 "%s:%d: no xattr support in fs or mounted "
841 "without user_xattr option", file, lineno);
842 }
843
844 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: fsetxattr() failed",
845 file, lineno);
846 }
847
848 return rval;
849}