-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.c
337 lines (280 loc) · 5.7 KB
/
functions.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#if defined(__cplusplus)
extern "C" {
#endif
#include "functions.h"
int check_fd(int fd);
static int safe_dup(int fd);
static int safe_close(int fd);
static int safe_cloexec(int fd);
int override_error_output(void *data);
int restore_error_output(void *data);
inline int
check_fd(int fd)
{
errno = 0;
if (fd < 0 || (fcntl(fd, F_GETFD) < 0 && errno == EBADF)) {
errno = EBADF;
return -EBADF;
}
return 0;
}
static int
safe_dup(int fd)
{
int new_fd;
int local_errno;
int flags = F_DUPFD;
#if defined(HAVE_F_DUPFD_CLOEXEC)
flags = F_DUPFD_CLOEXEC;
#endif
new_fd = fcntl(fd, flags, fileno(stderr) + 1);
if (new_fd < 0 && errno == EINVAL) {
new_fd = dup(fd);
if (new_fd < 0) {
local_errno = errno;
goto error;
}
}
if (safe_cloexec(new_fd) < 0) {
local_errno = errno;
goto error;
}
return new_fd;
error:
errno = local_errno;
return -1;
}
static int
safe_close(int fd)
{
int rv;
#if defined(HAVE_POSIX_CLOSE_RESTART)
rv = posix_close(fd, 0);
#else
rv = close(fd);
if (rv < 0 && errno == EINTR)
errno = EINPROGRESS;
#endif
return rv;
}
static inline int
safe_cloexec(int fd)
{
int local_errno;
int flags = fcntl(fd, F_GETFD);
if (flags < 0) {
local_errno = errno;
goto error;
}
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
local_errno = errno;
goto error;
}
return 0;
error:
errno = local_errno;
return -1;
}
int
override_error_output(void *data)
{
int local_errno;
mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
save_t *s = data;
#if defined(HAVE_O_CLOEXEC)
mode |= O_CLOEXEC;
#endif
assert(s != NULL && \
"Must be a valid pointer to `save_t' type");
s->file.old_fd = -1;
s->file.new_fd = -1;
s->status = -1;
fflush(stderr);
fgetpos(stderr, &s->file.position);
s->file.old_fd = safe_dup(fileno(stderr));
if (s->file.old_fd < 0) {
local_errno = errno;
goto error;
}
s->file.new_fd = open("/dev/null", O_WRONLY | O_APPEND, mode);
if (s->file.new_fd < 0) {
local_errno = errno;
if (dup2(s->file.old_fd, fileno(stderr)) < 0) {
local_errno = errno;
goto error;
}
safe_close(s->file.old_fd);
goto error;
}
if (safe_cloexec(s->file.new_fd) < 0) {
local_errno = errno;
goto error;
}
if (dup2(s->file.new_fd, fileno(stderr)) < 0) {
local_errno = errno;
goto error;
}
safe_close(s->file.new_fd);
return 0;
error:
s->status = local_errno;
errno = s->status;
return -1;
}
int
restore_error_output(void *data)
{
int local_errno;
save_t *s = data;
assert(s != NULL && \
"Must be a valid pointer to `save_t' type");
if (s->file.old_fd < 0 && s->status != 0)
return -1;
fflush(stderr);
if (dup2(s->file.old_fd, fileno(stderr)) < 0) {
local_errno = errno;
goto error;
}
safe_close(s->file.old_fd);
clearerr(stderr);
fsetpos(stderr, &s->file.position);
if (setvbuf(stderr, NULL, _IONBF, 0) != 0) {
local_errno = errno;
goto error;
}
return 0;
error:
s->status = local_errno;
errno = s->status;
return -1;
}
inline magic_t
magic_open_wrapper(int flags)
{
return magic_open(flags);
}
inline void
magic_close_wrapper(magic_t magic)
{
magic_close(magic);
}
inline const char*
magic_error_wrapper(magic_t magic)
{
return magic_error(magic);
}
inline int
magic_errno_wrapper(magic_t magic)
{
return magic_errno(magic);
}
inline const char*
magic_getpath_wrapper(void)
{
return magic_getpath(NULL, 0);
}
inline int
magic_getparam_wrapper(magic_t magic, int parameter, void *value)
{
return magic_getparam(magic, parameter, value);
}
inline int
magic_setparam_wrapper(magic_t magic, int parameter, const void *value)
{
if (*(const int *)value < 0 || *(const size_t *)value > UINT_MAX) {
errno = EOVERFLOW;
return -EOVERFLOW;
}
if (parameter == MAGIC_PARAM_BYTES_MAX)
return magic_setparam(magic, parameter, value);
if (*(const size_t *)value > USHRT_MAX) {
errno = EOVERFLOW;
return -EOVERFLOW;
}
return magic_setparam(magic, parameter, value);
}
inline int
magic_getflags_wrapper(magic_t magic)
{
#if defined(HAVE_MAGIC_GETFLAGS)
return magic_getflags(magic);
#else
UNUSED(magic);
errno = ENOSYS;
return -ENOSYS;
#endif
}
inline int
magic_setflags_wrapper(magic_t magic, int flags)
{
if (flags < 0 || flags > 0xfffffff) {
errno = EINVAL;
return -EINVAL;
}
return magic_setflags(magic, flags);
}
inline int
magic_load_wrapper(magic_t magic, const char *magic_file, int flags)
{
int rv;
MAGIC_FUNCTION(magic_load, rv, flags, magic, magic_file);
return rv;
}
inline int
magic_load_buffers_wrapper(magic_t magic, void **buffers, size_t *sizes, size_t count, int flags)
{
int rv;
MAGIC_FUNCTION(magic_load_buffers, rv, flags, magic, buffers, sizes, count);
return rv;
}
inline int
magic_compile_wrapper(magic_t magic, const char *magic_file, int flags)
{
int rv;
MAGIC_FUNCTION(magic_compile, rv, flags, magic, magic_file);
return rv;
}
inline int
magic_check_wrapper(magic_t magic, const char *magic_file, int flags)
{
int rv;
MAGIC_FUNCTION(magic_check, rv, flags, magic, magic_file);
return rv;
}
inline const char*
magic_file_wrapper(magic_t magic, const char* filename, int flags)
{
const char *cstring;
MAGIC_FUNCTION(magic_file, cstring, flags, magic, filename);
return cstring;
}
inline const char*
magic_buffer_wrapper(magic_t magic, const void *buffer, size_t size, int flags)
{
const char *cstring;
MAGIC_FUNCTION(magic_buffer, cstring, flags, magic, buffer, size);
return cstring;
}
inline const char*
magic_descriptor_wrapper(magic_t magic, int fd, int flags)
{
int local_errno;
const char *cstring;
if (check_fd(fd) < 0) {
local_errno = errno;
goto error;
}
MAGIC_FUNCTION(magic_descriptor, cstring, flags, magic, fd);
return cstring;
error:
errno = local_errno;
return NULL;
}
inline int
magic_version_wrapper(void)
{
return magic_version();
}
#if defined(__cplusplus)
}
#endif