vfs: pm_dumpcore: always clean up process
[minix.git] / lib / libelf / compat / mmap.c
blob8e2a8d4e1e6306de64f2df3877ed42300a93cb3a
1 #include <sys/cdefs.h>
3 #include <sys/types.h>
5 #include <err.h>
6 #include <errno.h>
7 #include <libelf.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "_libelf.h"
15 #include "../libelf_compat.h"
17 void *
18 libelf_mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
20 void *p; /* malloc'ed pointer */
21 size_t bufsize;
23 if ((addr != NULL) || (flags != MAP_PRIVATE) || (prot != PROT_READ)) {
24 LIBELF_SET_ERROR(ARGUMENT, 0);
25 return (MAP_FAILED);
29 * Fall back to malloc+read.
31 p = NULL;
32 bufsize = 1024 * 1024;
33 while (/*CONSTCOND*/true) {
34 void *newp = realloc(p, bufsize);
35 ssize_t rsz;
37 if (newp == NULL) {
38 free(p);
39 LIBELF_SET_ERROR(RESOURCE, 0);
40 return (MAP_FAILED);
42 p = newp;
43 rsz = pread(fd, p, bufsize, 0);
44 if (rsz == -1) {
45 free(p);
46 LIBELF_SET_ERROR(IO, errno);
47 return (MAP_FAILED);
48 } else if ((size_t) rsz > bufsize) {
49 free(p);
50 LIBELF_SET_ERROR(IO, EIO); /* XXX */
51 return (MAP_FAILED);
52 } else if ((size_t) rsz < bufsize) {
54 * try to shrink the buffer.
56 newp = realloc(p, (size_t) rsz);
57 if (newp != NULL) {
58 p = newp;
60 break;
62 bufsize *= 2;
65 return p;
68 int
69 libelf_munmap(void *addr, size_t len)
71 free(addr);
72 return 0;