unlock and close cachefile if we bail out of socket creation.
[trinity.git] / maps.c
blobf4b080eee21528f79409a298842d6036d91c26c5
1 #include <stdlib.h>
2 #include <string.h>
3 #include <sys/mman.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include "arch.h"
7 #include "list.h"
8 #include "child.h"
9 #include "maps.h"
10 #include "random.h"
11 #include "shm.h"
12 #include "trinity.h" // page_size
14 /* Walk the list, get the j'th element */
15 static struct map * __get_map(struct list_head *head, unsigned int max)
17 struct list_head *node;
19 unsigned int i, j = 0;
21 i = rand() % max;
23 list_for_each(node, head) {
24 struct map *m;
26 m = (struct map *) node;
28 if (i == j)
29 return m;
30 j++;
32 return 0;
35 struct map * get_map(void)
37 struct map *map;
38 bool local = FALSE;
40 /* If we're not running in child context, just do shared mappings. */
41 if (this_child == 0)
42 return __get_map(&shared_mappings->list, num_shared_mappings);
44 /* Only toss the dice if we actually have local mappings. */
45 if (shm->num_mappings[this_child] > 0)
46 local = rand_bool();
48 if (local == TRUE)
49 map = __get_map(&shm->mappings[this_child]->list, shm->num_mappings[this_child]);
50 else
51 map = __get_map(&shared_mappings->list, num_shared_mappings);
53 return map;
56 static void delete_local_mapping(int childno, struct map *map)
58 list_del(&map->list);
59 shm->num_mappings[childno]--;
62 void delete_mapping(int childno, struct map *map)
64 if (map->type == MAP_LOCAL)
65 delete_local_mapping(childno, map);
67 /* Right now, we don't want to delete MAP_GLOBAL mappings */
70 struct map * common_set_mmap_ptr_len(int childno)
72 struct map *map;
74 map = (struct map *) shm->a1[childno];
75 shm->scratch[childno] = (unsigned long) map; /* Save this for ->post */
76 if (map == NULL) {
77 shm->a1[childno] = 0;
78 shm->a2[childno] = 0;
79 return NULL;
82 shm->a1[childno] = (unsigned long) map->ptr;
83 shm->a2[childno] = map->size; //TODO: Munge this.
85 return map;
88 void dirty_mapping(struct map *map)
90 char *p = map->ptr;
91 unsigned int i;
92 unsigned int num_pages = map->size / page_size;
94 /* Check mapping is writable. */
95 if (!(map->prot & PROT_WRITE))
96 return;
98 switch (rand() % 6) {
99 case 0:
100 /* Just fault in one page. */
101 p[rand() % map->size] = rand();
102 break;
104 case 1:
105 /* fault in the whole mapping. */
106 for (i = 0; i < map->size; i += page_size)
107 p[i] = rand();
108 break;
110 case 2:
111 /* every other page. */
112 for (i = 0; i < map->size; i += (page_size * 2))
113 p[i] = rand();
114 break;
116 case 3:
117 /* whole mapping in reverse */
118 for (i = map->size; i > 0; i -= page_size)
119 p[i] = rand();
120 break;
122 case 4:
123 /* fault in map->size pages. (some may be faulted >once) */
124 for (i = 0; i < num_pages; i++);
125 p[rand() % (num_pages + 1)] = rand();
126 break;
128 case 5:
129 /* fault in the last page in a mapping
130 * Fill it with ascii, in the hope we do something like
131 * a strlen and go off the end. */
132 memset((void *) p + (map->size - page_size), 'A', page_size);
133 break;
135 default:
136 break;