| 1 |
#include <sys/time.h> |
| 2 |
#include <sys/resource.h> |
| 3 |
#include <unistd.h> |
| 4 |
#include <sys/mman.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <errno.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include "harness.h" |
| 10 |
|
| 11 |
void process_init() |
| 12 |
{ |
| 13 |
struct rlimit limit = {0}; |
| 14 |
void *res; |
| 15 |
static void *const areas[] = {(void*)0x7000, (void*)0, (void*)0xf000}; |
| 16 |
int i; |
| 17 |
|
| 18 |
/* Default to 1 second of CPU time. That should be enough for |
| 19 |
anyone... */ |
| 20 |
set_cpu_limit(1); |
| 21 |
|
| 22 |
/* Stop us dumping core, just because the V86 died. */ |
| 23 |
if (setrlimit (RLIMIT_CORE, &limit)) |
| 24 |
{ |
| 25 |
perror("setrlimit(RLIMIT_CORE)"); |
| 26 |
exit(1); |
| 27 |
} |
| 28 |
|
| 29 |
/* Claim the whole V86 area (except NULL). */ |
| 30 |
res = mmap ((void*)0x1000, 0x10f000, 0, |
| 31 |
MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 32 |
if (res != (void*)0x1000) |
| 33 |
{ |
| 34 |
if (res == (void*)-1) |
| 35 |
fprintf(stderr, "Failed to claim V86 area: %s\n", strerror(errno)); |
| 36 |
else |
| 37 |
fprintf(stderr, "V86 area already occupied.\n"); |
| 38 |
exit(1); |
| 39 |
} |
| 40 |
|
| 41 |
for (i=0; i<sizeof(areas)/sizeof(*areas); i++) |
| 42 |
{ |
| 43 |
if (mmap (areas[i], 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 44 |
MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == (void*)-1) |
| 45 |
{ |
| 46 |
fprintf(stderr, "Failed to map area at 0x%x: %s\n", (int)areas[i], |
| 47 |
strerror(errno)); |
| 48 |
exit(1); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
void set_cpu_limit(unsigned long time) |
| 54 |
{ |
| 55 |
struct rlimit limit = {0}; |
| 56 |
|
| 57 |
if (getrlimit (RLIMIT_CPU, &limit)) |
| 58 |
{ |
| 59 |
perror("getrlimit(RLIMIT_CPU)"); |
| 60 |
exit(1); |
| 61 |
} |
| 62 |
|
| 63 |
limit.rlim_cur = time; |
| 64 |
|
| 65 |
if (setrlimit (RLIMIT_CPU, &limit)) |
| 66 |
{ |
| 67 |
perror("setrlimit(RLIMIT_CPU)"); |
| 68 |
exit(1); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/* */ |
| 73 |
/* Local Variables: */ |
| 74 |
/* mode:c */ |
| 75 |
/* c-indentation-style: "whitesmith" */ |
| 76 |
/* c-basic-offset: 2 */ |
| 77 |
/* End: */ |