-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmojocrash_unix.c
582 lines (469 loc) · 13.7 KB
/
mojocrash_unix.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#define __MOJOCRASH_INTERNAL__ 1
#include "mojocrash_platform.h"
#if MOJOCRASH_PLATFORM_UNIX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "mojocrash_internal.h"
static int crashlogfd = -1;
static struct timeval starttime;
typedef void (*MOJOCRASH_sighandler)(int sig);
static MOJOCRASH_sighandler orig_SIGSEGV_handler = NULL;
static MOJOCRASH_sighandler orig_SIGBUS_handler = NULL;
static MOJOCRASH_sighandler orig_SIGFPE_handler = NULL;
static MOJOCRASH_sighandler orig_SIGILL_handler = NULL;
static MOJOCRASH_sighandler orig_SIGABRT_handler = NULL;
int MOJOCRASH_platform_install_crash_catcher(MOJOCRASH_catcher catcher)
{
#define INSTALL_SIGHANDLER(x) orig_##x##_handler = signal(x, catcher)
INSTALL_SIGHANDLER(SIGSEGV);
INSTALL_SIGHANDLER(SIGBUS);
INSTALL_SIGHANDLER(SIGFPE);
INSTALL_SIGHANDLER(SIGILL);
INSTALL_SIGHANDLER(SIGABRT);
#undef INSTALL_SIGHANDLER
return 1;
} /* MOJOCRASH_platform_install_crash_catcher */
void MOJOCRASH_platform_die(int force)
{
if (force)
_exit(86);
else
exit(86);
} /* MOJOCRASH_platform_die */
static int build_dirs(char *path)
{
int rc = 0;
char *ptr = path;
if (*ptr == '/')
ptr++;
while (1)
{
const char ch = *ptr;
if ((ch == '/') || (ch == '\0'))
{
*ptr = '\0';
rc = mkdir(path, S_IRWXU);
*ptr = ch;
if ((rc == -1) && (errno != EEXIST))
return 0;
} /* if */
if (ch == '\0')
break;
ptr++;
} /* while */
return 1;
} /* build_dirs */
int MOJOCRASH_platform_start_crashlog(const char *appname)
{
char logpath[PATH_MAX+1];
char *sep = NULL;
int len = 0;
int num = 0;
MOJOCRASH_unix_get_logpath(logpath, sizeof (logpath), appname);
len = strlen(logpath);
if (len > (sizeof (logpath) - 16))
return 0;
sep = &logpath[len];
*sep = '/';
sep++;
*sep = '\0';
/*
* if crashlog isn't -1, we might be in a double-fault, but it might be
* that the crashing program wrote over the static variable, too...
* close it (ignore failure) and start again. If the file was really
* there, it was useless in the double-fault anyhow.
*/
if (crashlogfd != -1)
{
close(crashlogfd);
crashlogfd = -1;
} /* if */
/*
* If there are 1000+ crashlogs, either they aren't being reported, or
* we have a problem here that is causing an infinite loop. Either way,
* give up at that point.
*/
while ((crashlogfd == -1) && (num < 1000))
{
/*
* Dir won't exist before first crash, and the reporter app may
* remove the storage dir at any time as it cleans up after emptying
* out old reports, so try to (re)create it on each iteration.
*/
build_dirs(logpath);
snprintf(sep, sizeof (logpath) - len, "%d", num);
crashlogfd = open(logpath, O_WRONLY | O_CREAT | O_EXCL, 0600);
num++;
} /* while */
return (crashlogfd != -1);
} /* MOJOCRASH_platform_start_crashlog */
int MOJOCRASH_platform_new_crashlog_line(const char *str)
{
const int len = strlen(str);
if (write(crashlogfd, str, len) != len)
return 0;
if (write(crashlogfd, "\n", 1) != 1)
return 0;
return 1;
} /* MOJOCRASH_platform_new_crashlog_line */
int MOJOCRASH_platform_end_crashlog(void)
{
if (close(crashlogfd) == -1)
return 0;
crashlogfd = -1;
return 1;
} /* MOJOCRASH_platform_end_crashlog */
const char *MOJOCRASH_platform_version(void)
{
static char osversion[64];
MOJOCRASH_unix_get_osver(osversion, sizeof (osversion));
return osversion;
} /* MOJOCRASH_platform_version */
long MOJOCRASH_platform_appuptime(void)
{
struct timeval tv;
long retval = 0;
gettimeofday(&tv, NULL);
retval = ( (tv.tv_sec - starttime.tv_sec) +
((tv.tv_usec - starttime.tv_usec) / 1000000) );
return ((retval >= 0) ? retval : -1);
} /* MOJOCRASH_platform_appuptime */
long MOJOCRASH_platform_now(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec;
} /* MOJOCRASH_platform_now */
static char **loaded_reports = NULL;
const char **MOJOCRASH_platform_load_reports(const char *appname, int *total)
{
char logpath[PATH_MAX+1];
char **retval = NULL;
int count = 0;
DIR *dirp = NULL;
struct dirent *dent = NULL;
MOJOCRASH_unix_get_logpath(logpath, sizeof (logpath), appname);
dirp = opendir(logpath);
if (dirp == NULL)
return NULL;
while ((dent = readdir(dirp)) != NULL)
{
char path[PATH_MAX+16];
const char *name = dent->d_name;
char *data = NULL;
char **ptr = NULL;
struct stat statbuf;
int rc = 0;
int io = -1;
if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0))
continue;
snprintf(path, sizeof (path), "%s/%s", logpath, name);
if (stat(path, &statbuf) == -1)
continue;
if (S_ISDIR(statbuf.st_mode))
continue;
ptr = (char **) realloc(retval, sizeof (char *) * (count+1));
if (ptr == NULL)
continue;
retval = ptr;
ptr = (char **) realloc(loaded_reports, sizeof (char *) * (count+1));
if (ptr == NULL)
continue;
loaded_reports = ptr;
io = open(path, O_RDONLY);
if (io == -1)
continue;
data = (char *) malloc(statbuf.st_size + 1);
if (data == NULL)
{
close(io);
continue;
} /* if */
rc = read(io, data, statbuf.st_size);
close(io);
if (rc != statbuf.st_size)
{
free(data);
continue;
} /* if */
data[statbuf.st_size] = '\0';
loaded_reports[count] = (char *) malloc(strlen(path) + 1);
if (loaded_reports[count] == NULL)
{
free(data);
continue;
} /* if */
strcpy(loaded_reports[count], path);
retval[count] = data;
count++;
} /* while */
closedir(dirp);
if (count == 0)
{
free(retval);
free(loaded_reports);
retval = NULL;
loaded_reports = NULL;
} /* if */
*total = count;
return (const char **) retval;
} /* MOJOCRASH_platform_load_reports */
void MOJOCRASH_platform_delete_report(const char *appname, const int idx)
{
unlink(loaded_reports[idx]);
} /* MOJOCRASH_platform_delete_report */
void MOJOCRASH_platform_free_reports(const char **reports, const int total)
{
int i;
for (i = 0; i < total; i++)
{
free((void *) reports[i]);
free(loaded_reports[i]);
} /* for */
free(reports);
free(loaded_reports);
loaded_reports = NULL;
} /* MOJOCRASH_platform_free_reports */
typedef struct
{
MOJOCRASH_thread_entry fn;
void *arg;
volatile int done;
} thread_data;
static void *thread_bridge(void *_arg)
{
thread_data *data = (thread_data *) _arg;
MOJOCRASH_thread_entry fn = data->fn;
void *arg = data->arg;
data->done = 1;
fn(arg);
return NULL;
} /* thread_bridge */
int MOJOCRASH_platform_spin_thread(MOJOCRASH_thread_entry fn, void *arg)
{
thread_data data = { fn, arg, 0 };
pthread_t thread;
if (pthread_create(&thread, NULL, thread_bridge, arg) != 0)
return 0;
pthread_detach(thread);
while (!data.done) { /* spin. */ }
return 1;
} /* MOJOCRASH_platform_spin_thread */
int MOJOCRASH_platform_init_network(void) { /* no-op. */ return 1; }
void MOJOCRASH_platform_deinit_network(void) { /* no-op. */ }
typedef struct DnsResolve
{
char *host;
int port;
pthread_mutex_t mutex;
struct addrinfo *addr;
volatile int status;
volatile int app_done;
} DnsResolve;
static void free_dns(DnsResolve *dns)
{
pthread_mutex_destroy(&dns->mutex);
if (dns->addr != NULL)
freeaddrinfo(dns->addr);
free(dns->host);
free(dns);
} /* free_dns */
static void dns_resolver_thread(void *_dns)
{
char portstr[64];
int app_done = 0;
struct addrinfo hints;
DnsResolve *dns = (DnsResolve *) _dns;
int rc = -1;
#ifndef AI_ADDRCONFIG
#define AI_ADDRCONFIG 0
#endif
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
#ifndef AI_V4MAPPED
#define AI_V4MAPPED 0
#endif
memset(&hints, '\0', sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICSERV | AI_V4MAPPED | AI_ADDRCONFIG;
MOJOCRASH_LongToString(dns->port, portstr);
/* this blocks, hence all the thread tapdancing. */
rc = getaddrinfo(dns->host, portstr, &hints, &dns->addr);
pthread_mutex_lock(&dns->mutex);
dns->status = (rc == 0) ? 1 : -1;
app_done = dns->app_done;
pthread_mutex_unlock(&dns->mutex);
/* we free if the app is done, otherwise, app will do it. */
if (app_done)
free_dns(dns);
} /* dns_resolver_thread */
void *MOJOCRASH_platform_begin_dns(const char *host, const int port,
const int blocking)
{
DnsResolve *retval = NULL;
int mutex_initted = 0;
retval = (DnsResolve *) malloc(sizeof (DnsResolve));
if (retval == NULL)
goto begin_dns_failed;
retval->host = (char *) malloc(strlen(host) + 1);
if (retval->host == NULL)
goto begin_dns_failed;
if (pthread_mutex_init(&retval->mutex, NULL) != 0)
goto begin_dns_failed;
mutex_initted = 1;
strcpy(retval->host, host);
retval->port = port;
retval->addr = NULL;
retval->status = 0;
retval->app_done = 0;
if (blocking)
dns_resolver_thread(retval);
else
{
if (!MOJOCRASH_platform_spin_thread(dns_resolver_thread, retval))
goto begin_dns_failed;
} /* else */
return retval;
begin_dns_failed:
if (retval != NULL)
{
if (mutex_initted)
pthread_mutex_destroy(&retval->mutex);
free(retval->host);
free(retval);
} /* if */
return NULL;
} /* MOJOCRASH_platform_begin_dns */
int MOJOCRASH_platform_check_dns(void *_dns)
{
DnsResolve *dns = (DnsResolve *) _dns;
int retval;
if (dns == NULL)
return -1;
pthread_mutex_lock(&dns->mutex);
retval = dns->status;
pthread_mutex_unlock(&dns->mutex);
return retval;
} /* MOJOCRASH_platform_check_dns */
void MOJOCRASH_platform_free_dns(void *_dns)
{
int thread_done = 0;
DnsResolve *dns = (DnsResolve *) _dns;
if (dns == NULL)
return;
pthread_mutex_lock(&dns->mutex);
dns->app_done = 1;
thread_done = (dns->status != 0);
pthread_mutex_unlock(&dns->mutex);
/* we free if the thread is done, otherwise, thread will do it. */
if (thread_done)
free_dns(dns);
} /* MOJOCRASH_platform_free_dns */
void *MOJOCRASH_platform_open_socket(void *_dns, const int blocking)
{
DnsResolve *dns = (DnsResolve *) _dns;
const struct addrinfo *addr = NULL;
int *retval = NULL;
int flags = 0;
int fd = -1;
int success = 0;
if (MOJOCRASH_platform_check_dns(dns) != 1)
return NULL; /* dns resolve isn't done or it failed. */
retval = (int *) malloc(sizeof (int));
if (retval == NULL)
return NULL;
for (addr = dns->addr; addr != NULL; addr = addr->ai_next)
{
if (fd != -1)
close(fd);
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (fd == -1)
continue;
if (!blocking)
{
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
continue;
else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
continue;
} /* if */
if (connect(fd, addr->ai_addr, addr->ai_addrlen) == -1)
{
if (errno != EINPROGRESS)
continue;
} /* if */
success = 1;
break;
} /* for */
if (success)
*retval = fd;
else
{
if (fd != -1)
close(fd);
free(retval);
retval = NULL;
} /* else */
return retval;
} /* MOJOCRASH_platform_open_socket */
int MOJOCRASH_platform_check_socket(void *sock)
{
const int fd = *((int *) sock);
fd_set wfds;
struct timeval tv;
int retval;
/* one handle, no wait. */
FD_ZERO(&wfds);
FD_SET(fd, &wfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
retval = select(fd+1, NULL, &wfds, NULL, &tv);
return ((retval > 0) && (FD_ISSET(fd, &wfds) != 0));
} /* MOJOCRASH_platform_check_socket */
void MOJOCRASH_platform_close_socket(void *sock)
{
close(*((int *) sock));
free(sock);
} /* MOJOCRASH_platform_close_socket */
int MOJOCRASH_platform_read_socket(void *sock, char *buf, const int l)
{
const int fd = *((int *) sock);
const int retval = (int) recv(fd, buf, l, 0);
if (retval < 0)
return (errno == EAGAIN) ? -2 : -1;
return retval;
} /* MOJOCRASH_platform_read_socket */
int MOJOCRASH_platform_write_socket(void *sock, const char *buf, const int l)
{
const int fd = *((int *) sock);
const int retval = (int) send(fd, buf, l, 0);
if (retval < 0)
return (errno == EAGAIN) ? -2 : -1;
return retval;
} /* MOJOCRASH_platform_write_socket */
int MOJOCRASH_platform_init(void)
{
static int already_initialized = 0;
if (already_initialized)
return 1;
already_initialized = 1;
gettimeofday(&starttime, NULL);
return MOJOCRASH_unix_init();
} /* MOJOCRASH_platform_init */
#endif /* MOJOCRASH_PLATFORM_UNIX */
/* end of mojocrash_unix.c ... */