blob: 7ca1849fa8993a758ac2334bd92dee8789056c67 [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);
Helge Dellerbb1477f2017-05-09 23:03:26 +0200414 } else {
415 /* readlink does not append a NUL byte to the buffer.
416 * Add it now. */
417 if ((size_t) rval < bufsize)
418 buf[rval] = '\0';
419 else
420 buf[bufsize-1] = '\0';
Alexey Kodanev8bbb1d52016-01-28 14:10:54 +0300421 }
422
423 return rval;
424}
425
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200426int safe_symlink(const char *file, const int lineno,
427 void (cleanup_fn)(void), const char *oldpath,
428 const char *newpath)
429{
430 int rval;
431
432 rval = symlink(oldpath, newpath);
433
434 if (rval == -1) {
435 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200436 "%s:%d: symlink(%s,%s) failed",
437 file, lineno, oldpath, newpath);
Cyril Hrubisaede40b2013-04-15 19:33:23 +0200438 }
439
440 return rval;
441}
442
Mats Liljegren49e86152014-04-16 19:27:58 +0200443ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
444 char len_strict, int fildes, const void *buf, size_t nbyte)
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800445{
446 ssize_t rval;
447
448 rval = write(fildes, buf, nbyte);
Guangwen Fengf3f10e02015-07-14 14:47:03 +0800449 if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200450 tst_brkm(TBROK | TERRNO, cleanup_fn,
451 "%s:%d: write(%d,%p,%zu) failed",
452 file, lineno, fildes, buf, rval);
453 }
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800454
Mats Liljegren49e86152014-04-16 19:27:58 +0200455 return rval;
Garrett Cooperdd3f47e2010-12-20 05:40:52 -0800456}
Cyril Hrubis3f75fe42011-12-28 15:33:12 +0100457
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800458long safe_strtol(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800459 void (cleanup_fn) (void), char *str, long min, long max)
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800460{
461 long rval;
462 char *endptr;
463
464 errno = 0;
465 rval = strtol(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200466
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800467 if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
Mats Liljegren49e86152014-04-16 19:27:58 +0200468 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800469 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200470 "%s:%d: strtol(%s) failed", file, lineno, str);
Cyril Hrubisd101cab2017-02-14 11:48:46 +0100471 return rval;
Mats Liljegren49e86152014-04-16 19:27:58 +0200472 }
473
474 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800475 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200476 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
Cyril Hrubisd101cab2017-02-14 11:48:46 +0100477 return 0;
Mats Liljegren49e86152014-04-16 19:27:58 +0200478 }
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);
Cyril Hrubisd101cab2017-02-14 11:48:46 +0100484 return 0;
Mats Liljegren49e86152014-04-16 19:27:58 +0200485 }
Caspar Zhangd6a1f252012-02-09 15:58:28 +0800486
487 return rval;
488}
Zhouping Liu2b73a152012-07-07 23:14:39 +0800489
Wanlong Gao354ebb42012-12-07 10:10:04 +0800490unsigned long safe_strtoul(const char *file, const int lineno,
491 void (cleanup_fn) (void), char *str,
492 unsigned long min, unsigned long max)
Zhouping Liu2b73a152012-07-07 23:14:39 +0800493{
494 unsigned long rval;
495 char *endptr;
496
497 errno = 0;
498 rval = strtoul(str, &endptr, 10);
Mats Liljegren49e86152014-04-16 19:27:58 +0200499
Zhouping Liu2b73a152012-07-07 23:14:39 +0800500 if ((errno == ERANGE && rval == ULONG_MAX)
Mats Liljegren49e86152014-04-16 19:27:58 +0200501 || (errno != 0 && rval == 0)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800502 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200503 "%s:%d: strtoul(%s) failed", file, lineno, str);
Cyril Hrubisd101cab2017-02-14 11:48:46 +0100504 return rval;
Mats Liljegren49e86152014-04-16 19:27:58 +0200505 }
506
507 if (rval > max || rval < min) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800508 tst_brkm(TBROK, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200509 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
510 file, lineno, str, rval, min, max);
Cyril Hrubisd101cab2017-02-14 11:48:46 +0100511 return 0;
Mats Liljegren49e86152014-04-16 19:27:58 +0200512 }
513
514 if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
Zhouping Liu2b73a152012-07-07 23:14:39 +0800515 tst_brkm(TBROK, cleanup_fn,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800516 "Invalid value: '%s' at %s:%d", str, file, lineno);
Cyril Hrubisd101cab2017-02-14 11:48:46 +0100517 return 0;
Mats Liljegren49e86152014-04-16 19:27:58 +0200518 }
Zhouping Liu2b73a152012-07-07 23:14:39 +0800519
520 return rval;
521}
Wanlong Gao3b535a82012-10-18 16:35:14 +0800522
523long safe_sysconf(const char *file, const int lineno,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800524 void (cleanup_fn) (void), int name)
Wanlong Gao3b535a82012-10-18 16:35:14 +0800525{
526 long rval;
527 errno = 0;
528
529 rval = sysconf(name);
530
531 if (rval == -1) {
Mats Liljegren49e86152014-04-16 19:27:58 +0200532 if (errno) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800533 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200534 "%s:%d: sysconf(%d) failed",
535 file, lineno, name);
536 } else {
537 tst_resm(TINFO, "%s:%d: sysconf(%d): "
538 "queried option is not available"
539 " or there is no definite limit",
540 file, lineno, name);
541 }
Wanlong Gao3b535a82012-10-18 16:35:14 +0800542 }
543
544 return rval;
545}
Zeng Linggang1030c9d2014-01-22 13:58:55 +0800546
Cyril Hrubisc4592352014-03-04 16:41:02 +0100547int safe_chmod(const char *file, const int lineno,
548 void (cleanup_fn)(void), const char *path, mode_t mode)
549{
550 int rval;
551
552 rval = chmod(path, mode);
553
554 if (rval == -1) {
555 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200556 "%s:%d: chmod(%s,0%o) failed",
557 file, lineno, path, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100558 }
559
560 return rval;
561}
562
563int safe_fchmod(const char *file, const int lineno,
564 void (cleanup_fn)(void), int fd, mode_t mode)
565{
566 int rval;
567
568 rval = fchmod(fd, mode);
569
570 if (rval == -1) {
571 tst_brkm(TBROK | TERRNO, cleanup_fn,
Mats Liljegren49e86152014-04-16 19:27:58 +0200572 "%s:%d: fchmod(%d,0%o) failed",
573 file, lineno, fd, mode);
Cyril Hrubisc4592352014-03-04 16:41:02 +0100574 }
575
576 return rval;
577}
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200578
Xing Gu92a14592014-06-13 10:53:17 +0800579int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
580 const char *path, uid_t owner, gid_t group)
581{
582 int rval;
583
584 rval = chown(path, owner, group);
585
586 if (rval == -1) {
587 tst_brkm(TBROK | TERRNO, cleanup_fn,
588 "%s:%d: chown(%s,%d,%d) failed",
589 file, lineno, path, owner, group);
590 }
591
592 return rval;
593}
Cyril Hrubis34ff2272014-06-04 15:58:50 +0200594
595int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
596 int fd, uid_t owner, gid_t group)
597{
598 int rval;
599
600 rval = fchown(fd, owner, group);
601
602 if (rval == -1) {
603 tst_brkm(TBROK | TERRNO, cleanup_fn,
604 "%s:%d: fchown(%d,%d,%d) failed",
605 file, lineno, fd, owner, group);
606 }
607
608 return rval;
609}
610
Cyril Hrubisf3e448f2014-04-24 14:24:49 +0200611pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
612 int *status)
613{
614 pid_t rval;
615
616 rval = wait(status);
617 if (rval == -1) {
618 tst_brkm(TBROK | TERRNO, cleanup_fn,
619 "%s:%d: wait(%p) failed",
620 file, lineno, status);
621 }
622
623 return rval;
624}
625
626pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
627 pid_t pid, int *status, int opts)
628{
629 pid_t rval;
630
631 rval = waitpid(pid, status, opts);
632 if (rval == -1) {
633 tst_brkm(TBROK | TERRNO, cleanup_fn,
634 "%s:%d: waitpid(%d,%p,%d) failed",
635 file, lineno, pid, status, opts);
636 }
637
638 return rval;
639}
Zeng Linggang5ff25ef2014-05-06 15:49:42 +0800640
641void *safe_memalign(const char *file, const int lineno,
642 void (*cleanup_fn) (void), size_t alignment, size_t size)
643{
644 void *rval;
645
646 rval = memalign(alignment, size);
647 if (rval == NULL)
648 tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
649 file, lineno);
650
651 return rval;
652}
Xiaoguang Wang6deba582014-05-16 12:52:53 +0800653
654int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
655 pid_t pid, int sig)
656{
657 int rval;
658
659 rval = kill(pid, sig);
660
661 if (rval == -1) {
662 tst_brkm(TBROK | TERRNO, cleanup_fn,
663 "%s:%d: kill(%d,%s) failed",
664 file, lineno, pid, tst_strsig(sig));
665 }
666
667 return rval;
668}
Cyril Hrubis3a150e72014-06-11 11:55:23 +0200669
670int safe_mkfifo(const char *file, const int lineno,
671 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
672{
673 int rval;
674
675 rval = mkfifo(pathname, mode);
676
677 if (rval == -1) {
678 tst_brkm(TBROK | TERRNO, cleanup_fn,
679 "%s:%d: mkfifo(%s, 0%o) failed",
680 file, lineno, pathname, mode);
681 }
682
683 return rval;
684}
Xiaoguang Wangf48552a2014-07-27 17:00:53 +0800685
686int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
687 const char *oldpath, const char *newpath)
688{
689 int rval;
690
691 rval = rename(oldpath, newpath);
692
693 if (rval == -1) {
694 tst_brkm(TBROK | TERRNO, cleanup_fn,
695 "%s:%d: rename(%s, %s) failed",
696 file, lineno, oldpath, newpath);
697 }
698
699 return rval;
700}
Matus Marhefka8e9db8d2014-08-29 14:22:11 +0200701
702int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
703 const char *source, const char *target,
704 const char *filesystemtype, unsigned long mountflags,
705 const void *data)
706{
707 int rval;
708
709 rval = mount(source, target, filesystemtype, mountflags, data);
710
711 if (rval == -1) {
712 tst_brkm(TBROK | TERRNO, cleanup_fn,
713 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
714 file, lineno, source, target, filesystemtype,
715 mountflags, data);
716 }
717
718 return rval;
719}
720
721int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
722 const char *target)
723{
724 int rval;
725
726 rval = umount(target);
727
728 if (rval == -1) {
729 tst_brkm(TBROK | TERRNO, cleanup_fn,
730 "%s:%d: umount(%s) failed",
731 file, lineno, target);
732 }
733
734 return rval;
735}
Cyril Hrubis9138a012015-02-10 15:44:45 +0100736
737DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
738 const char *name)
739{
740 DIR *rval;
741
742 rval = opendir(name);
743
744 if (!rval) {
745 tst_brkm(TBROK | TERRNO, cleanup_fn,
746 "%s:%d: opendir(%s) failed", file, lineno, name);
747 }
748
749 return rval;
750}
751
752int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
753 DIR *dirp)
754{
755 int rval;
756
757 rval = closedir(dirp);
758
759 if (rval) {
760 tst_brkm(TBROK | TERRNO, cleanup_fn,
761 "%s:%d: closedir(%p) failed", file, lineno, dirp);
762 }
763
764 return rval;
765}
766
767struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
768 DIR *dirp)
769{
770 struct dirent *rval;
771 int err = errno;
772
773 errno = 0;
774 rval = readdir(dirp);
775
776 if (!rval && errno) {
777 tst_brkm(TBROK | TERRNO, cleanup_fn,
778 "%s:%d: readdir(%p) failed", file, lineno, dirp);
779 }
780
781 errno = err;
782 return rval;
783}
Cyril Hrubisaf5a4b12016-11-07 13:05:00 +0100784
785int safe_getpriority(const char *file, const int lineno, int which, id_t who)
786{
787 int rval, err = errno;
788
789 errno = 0;
790 rval = getpriority(which, who);
791 if (errno) {
792 tst_brkm(TBROK | TERRNO, NULL,
793 "%s:%d getpriority(%i, %i) failed",
794 file, lineno, which, who);
795 }
796
797 errno = err;
798 return rval;
799}
Dejan Jovicevic10552812016-11-15 13:19:33 +0100800
801int safe_setxattr(const char *file, const int lineno, const char *path,
802 const char *name, const void *value, size_t size, int flags)
803{
804 int rval;
805
806 rval = setxattr(path, name, value, size, flags);
807
808 if (rval) {
809 if (errno == ENOTSUP) {
810 tst_brkm(TCONF, NULL,
811 "%s:%d: no xattr support in fs or mounted "
812 "without user_xattr option", file, lineno);
813 }
814
815 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: setxattr() failed",
816 file, lineno);
817 }
818
819 return rval;
820}
821
822int safe_lsetxattr(const char *file, const int lineno, const char *path,
823 const char *name, const void *value, size_t size, int flags)
824{
825 int rval;
826
827 rval = lsetxattr(path, name, value, size, flags);
828
829 if (rval) {
830 if (errno == ENOTSUP) {
831 tst_brkm(TCONF, NULL,
832 "%s:%d: no xattr support in fs or mounted "
833 "without user_xattr option", file, lineno);
834 }
835
836 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: lsetxattr() failed",
837 file, lineno);
838 }
839
840 return rval;
841}
842
843int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
844 const void *value, size_t size, int flags)
845{
846 int rval;
847
848 rval = fsetxattr(fd, name, value, size, flags);
849
850 if (rval) {
851 if (errno == ENOTSUP) {
852 tst_brkm(TCONF, NULL,
853 "%s:%d: no xattr support in fs or mounted "
854 "without user_xattr option", file, lineno);
855 }
856
857 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: fsetxattr() failed",
858 file, lineno);
859 }
860
861 return rval;
862}
Xiao Yangc46e2772017-03-02 18:37:35 +0800863
864int safe_removexattr(const char *file, const int lineno, const char *path,
865 const char *name)
866{
867 int rval;
868
869 rval = removexattr(path, name);
870
871 if (rval) {
872 if (errno == ENOTSUP) {
873 tst_brkm(TCONF, NULL,
874 "%s:%d: no xattr support in fs or mounted "
875 "without user_xattr option", file, lineno);
876 }
877
878 tst_brkm(TBROK | TERRNO, NULL, "%s:%d: removexattr() failed",
879 file, lineno);
880 }
881
882 return rval;
883}
Xiao Yang15d413e2017-04-13 10:31:12 +0800884
885int safe_fsync(const char *file, const int lineno, int fd)
886{
887 int rval;
888
889 rval = fsync(fd);
890
891 if (rval) {
892 tst_brkm(TBROK | TERRNO, NULL,
893 "%s:%d: fsync(%i) failed", file, lineno, fd);
894 }
895
896 return rval;
897}
Guangwen Fengcb265432017-06-15 17:57:34 +0800898
899pid_t safe_setsid(const char *file, const int lineno)
900{
901 pid_t rval;
902
903 rval = setsid();
904 if (rval == -1) {
905 tst_brkm(TBROK | TERRNO, NULL,
906 "%s:%d: setsid() failed", file, lineno);
907 }
908
909 return rval;
910}
Xiao Yang2ce24742017-06-21 14:12:19 +0800911
912int safe_mknod(const char *file, const int lineno, const char *pathname,
913 mode_t mode, dev_t dev)
914{
915 int rval;
916
917 rval = mknod(pathname, mode, dev);
918 if (rval == -1) {
919 tst_brkm(TBROK | TERRNO, NULL,
920 "%s:%d: mknod() failed", file, lineno);
921 }
922
923 return rval;
924}