| 1 |
#include "utils/str_pile.h" |
| 2 |
#include "main.h" |
| 3 |
#include <assert.h> |
| 4 |
#include <linux/prctl.h> |
| 5 |
#include <stddef.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <string.h> |
| 8 |
#include <sys/mman.h> |
| 9 |
#include <sys/prctl.h> |
| 10 |
|
| 11 |
static char *str_pile; |
| 12 |
static char *pile_top; |
| 13 |
static char *pile_cursor; |
| 14 |
|
| 15 |
bool |
| 16 |
init_pile (size_t init_size) |
| 17 |
{ |
| 18 |
size_t aligned = (init_size + 0xfff) & ~0xfff; |
| 19 |
str_pile = mmap (NULL, aligned, PROT_READ | PROT_WRITE, |
| 20 |
MAP_ANON | MAP_PRIVATE, -1, 0); |
| 21 |
if (str_pile == MAP_FAILED) |
| 22 |
return false; |
| 23 |
#ifdef PR_SET_VMA |
| 24 |
prctl (PR_SET_VMA, PR_SET_VMA_ANON_NAME, str_pile, aligned, "str pile"); |
| 25 |
#endif |
| 26 |
pile_cursor = str_pile; |
| 27 |
pile_top = str_pile + aligned; |
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
string_t |
| 32 |
persist_object (const char *sysname, const string_t *arch) |
| 33 |
{ |
| 34 |
uint32_t syslen = strlen (sysname); |
| 35 |
uint32_t size; |
| 36 |
if (arch) |
| 37 |
size = arch->len + 1 + syslen; // arch.sysname |
| 38 |
else |
| 39 |
size = syslen; // sysname |
| 40 |
assert (pile_cursor + size <= pile_top); |
| 41 |
|
| 42 |
if (arch) |
| 43 |
{ |
| 44 |
memcpy (pile_cursor, arch->start, arch->len); |
| 45 |
pile_cursor += arch->len; |
| 46 |
*pile_cursor++ = '.'; |
| 47 |
} |
| 48 |
memcpy (pile_cursor, sysname, syslen); |
| 49 |
pile_cursor += syslen; |
| 50 |
|
| 51 |
return (string_t){ .start = pile_cursor - size, .len = size }; |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
free_pile (void) |
| 56 |
{ |
| 57 |
assert (!munmap (str_pile, pile_top - str_pile)); |
| 58 |
} |