2018-10-03 22:55:25 +02:00
|
|
|
#include <errno.h>
|
2018-08-21 21:23:22 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-10-19 13:25:15 +02:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <async_safe/log.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-21 21:23:22 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2023-09-19 17:18:01 +02:00
|
|
|
#ifndef __ANDROID__
|
2018-10-03 22:55:25 +02:00
|
|
|
static int write_full(int fd, const char *buf, size_t length) {
|
|
|
|
do {
|
|
|
|
ssize_t bytes_written = write(fd, buf, length);
|
|
|
|
if (bytes_written == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf += bytes_written;
|
|
|
|
length -= bytes_written;
|
|
|
|
} while (length);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-09-19 17:18:01 +02:00
|
|
|
#endif
|
2018-10-03 22:55:25 +02:00
|
|
|
|
2018-08-21 21:23:22 +02:00
|
|
|
COLD noreturn void fatal_error(const char *s) {
|
2023-09-19 17:18:01 +02:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
async_safe_fatal("hardened_malloc: fatal allocator error: %s", s);
|
|
|
|
#else
|
2018-10-03 22:22:28 +02:00
|
|
|
const char *prefix = "fatal allocator error: ";
|
2018-10-03 22:55:25 +02:00
|
|
|
(void)(write_full(STDERR_FILENO, prefix, strlen(prefix)) != -1 &&
|
|
|
|
write_full(STDERR_FILENO, s, strlen(s)) != -1 &&
|
|
|
|
write_full(STDERR_FILENO, "\n", 1));
|
2018-08-21 21:23:22 +02:00
|
|
|
abort();
|
2023-09-19 17:18:01 +02:00
|
|
|
#endif
|
2018-08-21 21:23:22 +02:00
|
|
|
}
|