[1c2939]: include / safe_macros.h  Maximize  Restore  History

Download this file

181 lines (148 with data), 4.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Safe macros for commonly used syscalls to reduce code duplication in LTP
* testcases, and to ensure all errors are caught in said testcases as
* gracefully as possible.
*
* Also satiates some versions of gcc/glibc when the warn_unused_result
* attribute is applied to the function call.
*
* Licensed under the GPLv2.
*/
#ifndef __TEST_H__
#error "you must include test.h before this file"
#else
#ifndef __SAFE_MACROS_H__
#define __SAFE_MACROS_H__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdarg.h>
#include <unistd.h>
char *
safe_basename(const char *file, const int lineno, void (*cleanup_fn)(void),
char *path)
{
char *rval;
rval = basename(path);
if (rval == NULL)
tst_brkm(TBROK|TERRNO, cleanup_fn, "basename failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_BASENAME(cleanup_fn, path) \
safe_basename(__FILE__, __LINE__, (cleanup_fn), (path))
int
safe_close(const char *file, const int lineno, void (*cleanup_fn)(void),
int fildes)
{
int rval;
rval = close(fildes);
if (rval == -1)
tst_brkm(TBROK|TERRNO, cleanup_fn, "close failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_CLOSE(cleanup_fn, fildes) \
safe_close(__FILE__, __LINE__, (cleanup_fn), (fildes))
int
safe_creat(const char *file, const int lineno, void (*cleanup_fn)(void),
char *pathname, mode_t mode)
{
int rval;
rval = creat(pathname, mode);
if (rval == -1)
tst_brkm(TBROK|TERRNO, cleanup_fn, "pipe failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_CREAT(cleanup_fn, pathname, mode) \
safe_creat(__FILE__, __LINE__, cleanup_fn, (pathname), (mode))
char *
safe_dirname(const char *file, const int lineno, void (*cleanup_fn)(void),
char *path)
{
char *rval;
rval = dirname(path);
if (rval == NULL)
tst_brkm(TBROK|TERRNO, cleanup_fn, "dirname failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_DIRNAME(cleanup_fn, path) \
safe_dirname(__FILE__, __LINE__, (cleanup_fn), (path))
int
safe_open(const char *file, const int lineno, void (*cleanup_fn)(void),
const char *pathname, int oflags, ...)
{
va_list ap;
int rval;
mode_t mode;
va_start(ap, oflags);
mode = va_arg(ap, mode_t);
va_end(ap);
rval = open(pathname, oflags, mode);
if (rval == -1)
tst_brkm(TBROK|TERRNO, cleanup_fn, "open failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_OPEN(cleanup_fn, pathname, oflags, ...) \
safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \
##__VA_ARGS__)
int
safe_pipe(const char *file, const int lineno, void (*cleanup_fn)(void),
int fildes[2])
{
int rval;
rval = pipe(fildes);
if (rval == -1)
tst_brkm(TBROK|TERRNO, cleanup_fn, "pipe failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_PIPE(cleanup_fn, fildes) \
safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes))
ssize_t
safe_read(const char *file, const int lineno, void (*cleanup_fn)(void),
char len_strict, int fildes, void *buf, size_t nbyte)
{
ssize_t rval;
rval = read(fildes, buf, nbyte);
if ((len_strict == 0 && rval == -1) || rval != nbyte)
tst_brkm(TBROK|TERRNO, cleanup_fn, "read failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_READ(cleanup_fn, len_strict, fildes, buf, nbyte) \
safe_read(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
(buf), (nbyte))
int
safe_unlink(const char *file, const int lineno, void (*cleanup_fn)(void),
const char *pathname)
{
int rval;
rval = unlink(pathname);
if (rval == -1)
tst_brkm(TBROK|TERRNO, cleanup_fn, "unlink failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_UNLINK(cleanup_fn, pathname) \
safe_unlink(__FILE__, __LINE__, cleanup_fn, (pathname))
ssize_t
safe_write(const char *file, const int lineno, void (cleanup_fn)(void),
char len_strict, int fildes, const void *buf, size_t nbyte)
{
ssize_t rval;
rval = write(fildes, buf, nbyte);
if ((len_strict == 0 && rval == -1) || rval != nbyte)
tst_brkm(TBROK|TERRNO, cleanup_fn, "write failed at %s:%d",
file, lineno);
return (rval);
}
#define SAFE_WRITE(cleanup_fn, len_strict, fildes, buf, nbyte) \
safe_write(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
(buf), (nbyte))
#endif
#endif

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks