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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
char *
libgeome_util_areadlink(struct libgeome_context *ctx, const char *path)
{
char *ret = NULL, *new;
size_t size = 0;
ssize_t r;
do {
new = realloc(ret, size += 128U);
if (!new) {
ctx->print_error(ctx, "realloc: %s\n", strerror(errno));
free(ret);
return NULL;
}
ret = new;
r = readlink(path, ret, size - 1U);
if (r < 0) {
ctx->print_error(ctx, "readlink %s: %s\n", path, strerror(errno));
free(ret);
return NULL;
}
} while ((size_t)r >= size - 2U);
ret[r] = '\0';
return ret;
}
int
libgeome_util_safe_pipe(struct libgeome_context *ctx, int fds[2])
{
int i, old_fd, new_fd, nul_fd = -1;
if (pipe(fds)) {
ctx->print_error(ctx, "pipe: %s\n", strerror(errno));
return -1;
}
if (fds[0] > 2 && fds[1] > 2)
return 0;
for (i = 0; i < 2; i++) {
if (fds[i] > 2)
continue;
new_fd = fcntl(fds[i], F_DUPFD, 3);
if (new_fd < 0) {
ctx->print_error(ctx, "fcntl F_DUPFD 3: %s\n", strerror(errno));
goto fail;
}
old_fd = fds[i];
close(fds[i]);
fds[i] = new_fd;
nul_fd = open("/dev/null", O_RDWR);
if (nul_fd != old_fd) {
ctx->print_error(ctx, "open /dev/null O_RDWR: %s\n", strerror(errno));
goto fail;
}
}
if (old_fd < 2) {
new_fd = dup(nul_fd);
if (new_fd < 0) {
ctx->print_error(ctx, "duo: %s\n", strerror(errno));
goto fail;
}
if (new_fd > 2)
close(new_fd);
}
return 0;
fail:
close(fds[0]);
close(fds[1]);
return -1;
}
void
libgeome_util_spawn_async_kill(struct libgeome_context *ctx, const struct spawn_join_info *info, int signum)
{
(void) ctx;
kill(info->pid, signum);
}
int
libgeome_util_spawn_async_read(struct libgeome_context *ctx, const char *path, const char *const *argv,
unsigned int alarm_seconds, struct spawn_join_info *info_out, int *fd_out)
{
struct sigaction sa, old_sa;
sigset_t mask, old_mask;
int fds[2], ignore_sigchld;
pid_t pid;
if (sigemptyset(&mask)) {
ctx->print_error(ctx, "sigemptyset: %s\n", strerror(errno));
return -1;
}
if (sigaddset(&mask, SIGCHLD)) {
ctx->print_error(ctx, "sigaddset SIGCHLD: %s\n", strerror(errno));
return -1;
}
if (sigprocmask(SIG_BLOCK, &mask, &old_mask)) {
ctx->print_error(ctx, "sigprocmask SIG_BLOCK {SIGCHLD}: %s\n", strerror(errno));
return -1;
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_DFL;
if (sigaction(SIGCHLD, &sa, &old_sa)) {
ctx->print_error(ctx, "sigaction SIGCHLD: %s\n", strerror(errno));
if (sigprocmask(SIG_SETMASK, &old_mask, NULL)) {
ctx->print_abort(ctx, "sigprocmask SIG_SETMASK <old mask>: %s\n", strerror(errno));
abort();
}
return -1;
}
ignore_sigchld = !(old_sa.sa_flags & SA_SIGINFO) && old_sa.sa_handler == SIG_IGN;
if (!ignore_sigchld && sigaction(SIGCHLD, &old_sa, NULL)) {
ctx->print_abort(ctx, "sigaction SIGCHLD: %s\n", strerror(errno));
abort();
}
if (sigprocmask(SIG_SETMASK, &old_mask, NULL)) {
ctx->print_abort(ctx, "sigaction SIG_SETMASK <old mask>: %s\n", strerror(errno));
abort();
}
if (libgeome_util_safe_pipe(ctx, fds))
goto out;
pid = fork();
switch (pid) {
case -1:
ctx->print_error(ctx, "fork: %s\n", strerror(errno));
close(fds[0]);
close(fds[1]);
goto out;
case 0:
close(fds[0]);
if (dup2(fds[1], STDOUT_FILENO) != STDOUT_FILENO) {
ctx->print_error(ctx, "dup2 <pipe> <stdout>: %s\n", strerror(errno));
_exit(127);
}
close(fds[1]);
alarm(alarm_seconds);
execvp(path, (const void *)argv);
ctx->print_error(ctx, "execvp %s: %s\n", path, strerror(errno));
_exit(127);
default:
close(fds[1]);
break;
}
info_out->pid = pid;
info_out->ignore_sigchld = ignore_sigchld;
*fd_out = fds[0];
return 0;
out:
if (ignore_sigchld) {
sa.sa_handler = SIG_IGN;
if (sigaction(SIGCHLD, &sa, NULL)) {
ctx->print_abort(ctx, "sigaction SIGCHLD: %s\n", strerror(errno));
abort();
}
while (waitpid(-1, NULL, WNOHANG) != -1);
}
return -1;
}
int
libgeome_util_spawn_async_join(struct libgeome_context *ctx, const struct spawn_join_info *info, int *status_out)
{
struct sigaction sa;
int ret = 0;
if (waitpid(info->pid, status_out, 0) != info->pid) {
ctx->print_error(ctx, "waitpid <subprocess> 0: %s\n", strerror(errno));
ret = -1;
}
if (info->ignore_sigchld) {
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
if (sigaction(SIGCHLD, &sa, NULL)) {
ctx->print_abort(ctx, "sigaction SIGCHLD: %s\n", strerror(errno));
abort();
}
while (waitpid(-1, NULL, WNOHANG) != -1);
}
return ret;
}
uint64_t
libgeome_util_parse_xml(char *s, struct location *location_out)
{
uint64_t ret = 0;
char *p = s;
errno = 0;
p = strstr(s, "<lat>");
if (!p)
p = strstr(s, "<latitude>");
if (!p)
goto skip_latitude;
p = &strchr(p, '>')[1];
while (isspace(*p))
p++;
location_out->latitude = strtod(p, &p);
if (errno)
goto skip_latitude;
while (isspace(*p))
p++;
if (*p != '<')
goto skip_latitude;
ret |= LIBGEOME_DATUM_LATITUDE;
skip_latitude:
p = strstr(s, "<lon>");
if (!p)
p = strstr(s, "<long>");
if (!p)
p = strstr(s, "<lng>");
if (!p)
p = strstr(s, "<longitude>");
if (!p)
goto skip_longitude;
p = &strchr(p, '>')[1];
while (isspace(*p))
p++;
location_out->longitude = strtod(p, &p);
if (errno)
goto skip_longitude;
while (isspace(*p))
p++;
if (*p != '<')
goto skip_longitude;
ret |= LIBGEOME_DATUM_LONGITUDE;
skip_longitude:
return ret;
}
uint64_t
libgeome_util_parse_json(char *s, struct location *location_out)
{
uint64_t ret = 0;
char *p = s;
errno = 0;
p = strstr(s, "\"lat\"");
if (!p)
p = strstr(s, "\"latitude\"");
if (!p)
goto skip_latitude;
p = &strchr(&p[1], '"')[1];
while (isspace(*p))
p++;
if (*p++ != ':')
goto skip_latitude;
while (isspace(*p))
p++;
location_out->latitude = strtod(p, &p);
if (errno)
goto skip_latitude;
while (isspace(*p))
p++;
if (*p != ',' && *p != '}')
goto skip_latitude;
ret |= LIBGEOME_DATUM_LATITUDE;
skip_latitude:
p = strstr(s, "\"lon\"");
if (!p)
p = strstr(s, "\"long\"");
if (!p)
p = strstr(s, "\"lng\"");
if (!p)
p = strstr(s, "\"longitude\"");
if (!p)
goto skip_longitude;
p = &strchr(&p[1], '"')[1];
while (isspace(*p))
p++;
if (*p++ != ':')
goto skip_longitude;
while (isspace(*p))
p++;
location_out->longitude = strtod(p, &p);
if (errno)
goto skip_longitude;
while (isspace(*p))
p++;
if (*p != ',' && *p != '}')
goto skip_longitude;
ret |= LIBGEOME_DATUM_LONGITUDE;
skip_longitude:
return ret;
}
uint64_t
libgeome_util_parse_plain(char *s, struct location *location_out)
{
errno = 0;
location_out->latitude = strtod(s, &s);
if (errno)
return 0;
while (isspace(*s))
s++;
if (*s == ',')
s++;
while (isspace(*s))
s++;
location_out->longitude = strtod(s, &s);
if (errno)
return 0;
while (isspace(*s))
s++;
if (*s)
return 0;
return LIBGEOME_DATUM_LATITUDE | LIBGEOME_DATUM_LONGITUDE;
}
uint64_t
libgeome_util_parse_output(char *s, struct location *location_out)
{
if (s[0] == '<')
return libgeome_util_parse_xml(s, location_out);
else if (s[0] == '{')
return libgeome_util_parse_json(s, location_out);
else
return libgeome_util_parse_plain(s, location_out);
}
|