math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / base / lmem.c
1 /* -*-c-*-
2 *
3 * Locked memory allocation (Unix-specific)
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 #ifdef HAVE_MLOCK
42 # include <sys/mman.h>
43 #endif
44
45 #include <mLib/arena.h>
46 #include <mLib/dstr.h>
47 #include <mLib/sub.h>
48
49 #include "lmem.h"
50
51 /*----- Arena operations --------------------------------------------------*/
52
53 static void *aalloc(arena *a, size_t sz) { return l_alloc((lmem *)a, sz); }
54 static void afree(arena *a, void *p) { l_free((lmem *)a, p); }
55 static void apurge(arena *a) { l_purge((lmem *)a); }
56
57 static const arena_ops l_ops = { aalloc, arena_fakerealloc, afree, apurge };
58
59 /*----- Main code ---------------------------------------------------------*/
60
61 /* --- @l_init@ --- *
62 *
63 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
64 * @size_t sz@ = size of locked memory area requested
65 *
66 * Returns: Zero if everything is fine, @+1@ if some insecure memory was
67 * allocated, and @-1@ if everything went horribly wrong.
68 *
69 * Use: Initializes the locked memory manager. This function is safe
70 * to call in a privileged program; privileges should usually be
71 * dropped after allocating the locked memory block.
72 *
73 * You must call @sub_init@ before allocating locked memory
74 * buffers.
75 */
76
77 int l_init(lmem *lm, size_t sz)
78 {
79 char *p;
80 int rc = 0;
81 l_node *l;
82
83 /* --- Preliminaries --- */
84
85 lm->a.ops = &l_ops;
86 lm->err = 0;
87 lm->f = 0;
88
89 /* --- Try making a secure locked passphrase buffer --- *
90 *
91 * Drop privileges before emitting diagnostic messages.
92 */
93
94 #ifdef HAVE_MLOCK
95
96 /* --- Memory-map a page from somewhere --- */
97
98 # ifdef MAP_ANON
99 p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
100 # else
101 {
102 int fd;
103 if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
104 p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
105 close(fd);
106 }
107 }
108 # endif
109
110 /* --- Lock the page in memory --- *
111 *
112 * Why does @mmap@ return such a stupid result if it fails?
113 */
114
115 if (p == 0 || p == MAP_FAILED) {
116 lm->emsg = "couldn't map locked memory area: %s";
117 lm->err = errno;
118 p = 0;
119 } else if (mlock(p, sz)) {
120 lm->emsg = "error locking memory area: %s";
121 lm->err = errno;
122 munmap(p, sz);
123 p = 0;
124 } else
125 lm->f |= LF_LOCKED;
126
127 #endif
128
129 /* --- Make a standard passphrase buffer --- */
130
131 #ifdef HAVE_MLOCK
132 if (!p)
133 #else
134 lm->err = 0;
135 lm->emsg = "locked memory not available on this system";
136 #endif
137 {
138 if ((p = malloc(sz)) == 0) {
139 lm->emsg = "not enough standard memory!";
140 lm->err = ENOMEM;
141 return (-1);
142 }
143 rc = +1;
144 }
145
146 /* --- Initialize the buffer --- */
147
148 lm->sz = lm->free = sz;
149 lm->p = p;
150
151 /* --- Initialize the free list --- */
152
153 l = CREATE(l_node);
154 l->next = 0;
155 l->p = p;
156 l->sz = sz;
157 l->f = 0;
158 lm->l = l;
159
160 /* --- Done --- */
161
162 return (rc);
163 }
164
165 /* --- @l_alloc@ --- *
166 *
167 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
168 * @size_t sz@ = size requested
169 *
170 * Returns: Pointer to allocated memory.
171 *
172 * Use: Allocates @sz@ bytes of locked memory.
173 */
174
175 void *l_alloc(lmem *lm, size_t sz)
176 {
177 l_node *l;
178
179 sz = (sz + 3u) & ~3u;
180 for (l = lm->l; l; l = l->next) {
181 if (l->f & LF_ALLOC)
182 continue;
183 if (l->sz < sz)
184 continue;
185 l->f |= LF_ALLOC;
186 if (l->sz > sz) {
187 l_node *n = CREATE(l_node);
188 n->next = l->next;
189 n->p = l->p + sz;
190 n->sz = l->sz - sz;
191 l->sz = sz;
192 n->f = 0;
193 l->next = n;
194 }
195 assert(((void)"Locked buffer space has vanished", lm->free >= sz));
196 lm->free -= sz;
197 return (l->p);
198 }
199 return (0);
200 }
201
202 /* --- @l_free@ --- *
203 *
204 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
205 * @void *p@ = pointer to block
206 *
207 * Returns: ---
208 *
209 * Use: Releases a block of locked memory.
210 */
211
212 void l_free(lmem *lm, void *p)
213 {
214 l_node *l;
215 l_node *ll = 0;
216
217 for (l = lm->l; l; l = l->next) {
218 size_t sz;
219
220 /* --- If this isn't the block, skip it --- */
221
222 if (l->p != p) {
223 ll = l;
224 continue;
225 }
226 assert(((void)"Block is already free", l->f & LF_ALLOC));
227
228 /* --- Coalesce with adjacent free blocks --- */
229
230 l->f &= ~LF_ALLOC;
231 sz = l->sz;
232 memset(p, 0, sz);
233
234 if (ll && !(ll->f & LF_ALLOC)) {
235 assert(((void)"Previous block doesn't fit", ll->p + ll->sz == p));
236 ll->sz += sz;
237 ll->next = l->next;
238 DESTROY(l);
239 l = ll;
240 }
241
242 ll = l->next;
243 if (ll && !(ll->f & LF_ALLOC)) {
244 assert(((void)"Next block doesn't fit", ll->p == l->p + l->sz));
245 l->sz += ll->sz;
246 l->next = ll->next;
247 DESTROY(ll);
248 }
249
250 lm->free += sz;
251 assert(((void)"Free lunch", lm->free <= lm->sz));
252 return;
253 }
254 assert(((void)"Not a locked block", 0));
255 }
256
257 /* --- @l_purge@ --- *
258 *
259 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
260 *
261 * Returns: ---
262 *
263 * Use: Purges all the free blocks in the buffer, and clears all of
264 * the locked memory. Memory is not freed back to the system.
265 */
266
267 void l_purge(lmem *lm)
268 {
269 l_node *l;
270
271 l = lm->l;
272 while (l) {
273 l_node *ll = l->next;
274 DESTROY(l);
275 l = ll;
276 }
277 memset(lm->p, 0, lm->sz);
278 l = CREATE(l_node);
279 l->next = 0;
280 l->p = lm->p;
281 l->sz = lm->sz;
282 l->f = 0;
283 lm->l = l;
284 lm->free = l->sz;
285 }
286
287 /* --- @l_destroy@ --- *
288 *
289 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
290 *
291 * Returns: ---
292 *
293 * Use: Disposes of a locked memory arena permanently.
294 */
295
296 void l_destroy(lmem *lm)
297 {
298 l_node *l;
299
300 l = lm->l;
301 while (l) {
302 l_node *ll = l->next;
303 DESTROY(l);
304 l = ll;
305 }
306 memset(lm->p, 0, lm->sz);
307
308 #ifdef HAVE_MLOCK
309 if (lm->f & LF_LOCKED)
310 munmap(lm->p, lm->sz);
311 else
312 #endif
313 free(lm->p); /*sic*/
314 }
315
316 /* --- @l_report@ --- *
317 *
318 * Arguments: @lmem *lm@ = pointer to locked memory descriptor
319 * @dstr *d@ = string to write the error message on
320 *
321 * Returns: Zero if the buffer is fine, @+1@ if there was a problem
322 * getting locked memory but insecure stuff could be allocated,
323 * and @-1@ if not even insecure memory could be found.
324 *
325 * Use: Returns a user-digestable explanation for the state of a
326 * locked memory buffer. If the return code is zero, no message
327 * is emitted to the string @d@.
328 */
329
330 int l_report(lmem *lm, dstr *d)
331 {
332 int rc;
333 if (lm->err)
334 dstr_putf(d, lm->emsg, strerror(lm->err));
335 if (!lm->p)
336 rc = -1;
337 else if (lm->err)
338 rc = +1;
339 else
340 rc = 0;
341 return (rc);
342 }
343
344 /*----- That's all, folks -------------------------------------------------*/