Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Initialization for access to a mmap'd malloc managed region. |
2 | Copyright 1992 Free Software Foundation, Inc. | |
3 | ||
4 | Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com | |
5 | ||
6 | This file is part of the GNU C Library. | |
7 | ||
8 | The GNU C Library is free software; you can redistribute it and/or | |
9 | modify it under the terms of the GNU Library General Public License as | |
10 | published by the Free Software Foundation; either version 2 of the | |
11 | License, or (at your option) any later version. | |
12 | ||
13 | The GNU C Library is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Library General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Library General Public | |
19 | License along with the GNU C Library; see the file COPYING.LIB. If | |
20 | not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include <sys/types.h> | |
24 | #include <fcntl.h> /* After sys/types.h, at least for dpx/2. */ | |
25 | #include <sys/stat.h> | |
26 | #include <string.h> | |
27 | #include "mmprivate.h" | |
28 | ||
29 | #ifndef SEEK_SET | |
30 | #define SEEK_SET 0 | |
31 | #endif | |
32 | ||
33 | ||
34 | #if defined(HAVE_MMAP) | |
35 | ||
36 | /* Forward declarations/prototypes for local functions */ | |
37 | ||
38 | static struct mdesc *reuse PARAMS ((int)); | |
39 | ||
40 | /* Initialize access to a mmalloc managed region. | |
41 | ||
42 | If FD is a valid file descriptor for an open file then data for the | |
43 | mmalloc managed region is mapped to that file, otherwise "/dev/zero" | |
44 | is used and the data will not exist in any filesystem object. | |
45 | ||
46 | If the open file corresponding to FD is from a previous use of | |
47 | mmalloc and passes some basic sanity checks to ensure that it is | |
48 | compatible with the current mmalloc package, then it's data is | |
49 | mapped in and is immediately accessible at the same addresses in | |
50 | the current process as the process that created the file. | |
51 | ||
52 | If BASEADDR is not NULL, the mapping is established starting at the | |
53 | specified address in the process address space. If BASEADDR is NULL, | |
54 | the mmalloc package chooses a suitable address at which to start the | |
55 | mapped region, which will be the value of the previous mapping if | |
56 | opening an existing file which was previously built by mmalloc, or | |
57 | for new files will be a value chosen by mmap. | |
58 | ||
59 | Specifying BASEADDR provides more control over where the regions | |
60 | start and how big they can be before bumping into existing mapped | |
61 | regions or future mapped regions. | |
62 | ||
63 | On success, returns a "malloc descriptor" which is used in subsequent | |
64 | calls to other mmalloc package functions. It is explicitly "void *" | |
65 | ("char *" for systems that don't fully support void) so that users | |
66 | of the package don't have to worry about the actual implementation | |
67 | details. | |
68 | ||
69 | On failure returns NULL. */ | |
70 | ||
71 | PTR | |
72 | mmalloc_attach (fd, baseaddr) | |
73 | int fd; | |
74 | PTR baseaddr; | |
75 | { | |
76 | struct mdesc mtemp; | |
77 | struct mdesc *mdp; | |
78 | PTR mbase; | |
79 | struct stat sbuf; | |
80 | ||
81 | /* First check to see if FD is a valid file descriptor, and if so, see | |
82 | if the file has any current contents (size > 0). If it does, then | |
83 | attempt to reuse the file. If we can't reuse the file, either | |
84 | because it isn't a valid mmalloc produced file, was produced by an | |
85 | obsolete version, or any other reason, then we fail to attach to | |
86 | this file. */ | |
87 | ||
88 | if (fd >= 0) | |
89 | { | |
90 | if (fstat (fd, &sbuf) < 0) | |
91 | { | |
92 | return (NULL); | |
93 | } | |
94 | else if (sbuf.st_size > 0) | |
95 | { | |
96 | return ((PTR) reuse (fd)); | |
97 | } | |
98 | } | |
99 | ||
100 | /* We start off with the malloc descriptor allocated on the stack, until | |
101 | we build it up enough to call _mmalloc_mmap_morecore() to allocate the | |
102 | first page of the region and copy it there. Ensure that it is zero'd and | |
103 | then initialize the fields that we know values for. */ | |
104 | ||
105 | mdp = &mtemp; | |
106 | memset ((char *) mdp, 0, sizeof (mtemp)); | |
107 | strncpy (mdp -> magic, MMALLOC_MAGIC, MMALLOC_MAGIC_SIZE); | |
108 | mdp -> headersize = sizeof (mtemp); | |
109 | mdp -> version = MMALLOC_VERSION; | |
110 | mdp -> morecore = __mmalloc_mmap_morecore; | |
111 | mdp -> fd = fd; | |
112 | mdp -> base = mdp -> breakval = mdp -> top = baseaddr; | |
113 | ||
114 | /* If we have not been passed a valid open file descriptor for the file | |
115 | to map to, then open /dev/zero and use that to map to. */ | |
116 | ||
117 | if (mdp -> fd < 0) | |
118 | { | |
119 | if ((mdp -> fd = open ("/dev/zero", O_RDWR)) < 0) | |
120 | { | |
121 | return (NULL); | |
122 | } | |
123 | else | |
124 | { | |
125 | mdp -> flags |= MMALLOC_DEVZERO; | |
126 | } | |
127 | } | |
128 | ||
129 | /* Now try to map in the first page, copy the malloc descriptor structure | |
130 | there, and arrange to return a pointer to this new copy. If the mapping | |
131 | fails, then close the file descriptor if it was opened by us, and arrange | |
132 | to return a NULL. */ | |
133 | ||
134 | if ((mbase = mdp -> morecore (mdp, sizeof (mtemp))) != NULL) | |
135 | { | |
136 | memcpy (mbase, mdp, sizeof (mtemp)); | |
137 | mdp = (struct mdesc *) mbase; | |
138 | } | |
139 | else | |
140 | { | |
141 | if (mdp -> flags & MMALLOC_DEVZERO) | |
142 | { | |
143 | close (mdp -> fd); | |
144 | } | |
145 | mdp = NULL; | |
146 | } | |
147 | ||
148 | return ((PTR) mdp); | |
149 | } | |
150 | ||
151 | /* Given an valid file descriptor on an open file, test to see if that file | |
152 | is a valid mmalloc produced file, and if so, attempt to remap it into the | |
153 | current process at the same address to which it was previously mapped. | |
154 | ||
155 | Note that we have to update the file descriptor number in the malloc- | |
156 | descriptor read from the file to match the current valid one, before | |
157 | trying to map the file in, and again after a successful mapping and | |
158 | after we've switched over to using the mapped in malloc descriptor | |
159 | rather than the temporary one on the stack. | |
160 | ||
161 | Once we've switched over to using the mapped in malloc descriptor, we | |
162 | have to update the pointer to the morecore function, since it almost | |
163 | certainly will be at a different address if the process reusing the | |
164 | mapped region is from a different executable. | |
165 | ||
166 | Also note that if the heap being remapped previously used the mmcheckf() | |
167 | routines, we need to update the hooks since their target functions | |
168 | will have certainly moved if the executable has changed in any way. | |
169 | We do this by calling mmcheckf() internally. | |
170 | ||
171 | Returns a pointer to the malloc descriptor if successful, or NULL if | |
172 | unsuccessful for some reason. */ | |
173 | ||
174 | static struct mdesc * | |
175 | reuse (fd) | |
176 | int fd; | |
177 | { | |
178 | struct mdesc mtemp; | |
179 | struct mdesc *mdp = NULL; | |
180 | ||
181 | if ((lseek (fd, 0L, SEEK_SET) == 0) && | |
182 | (read (fd, (char *) &mtemp, sizeof (mtemp)) == sizeof (mtemp)) && | |
183 | (mtemp.headersize == sizeof (mtemp)) && | |
184 | (strcmp (mtemp.magic, MMALLOC_MAGIC) == 0) && | |
185 | (mtemp.version <= MMALLOC_VERSION)) | |
186 | { | |
187 | mtemp.fd = fd; | |
188 | if (__mmalloc_remap_core (&mtemp) == mtemp.base) | |
189 | { | |
190 | mdp = (struct mdesc *) mtemp.base; | |
191 | mdp -> fd = fd; | |
192 | mdp -> morecore = __mmalloc_mmap_morecore; | |
193 | if (mdp -> mfree_hook != NULL) | |
194 | { | |
195 | mmcheckf ((PTR) mdp, (void (*) PARAMS ((void))) NULL, 1); | |
196 | } | |
197 | } | |
198 | } | |
199 | return (mdp); | |
200 | } | |
201 | ||
202 | #else /* !defined (HAVE_MMAP) */ | |
203 | ||
204 | /* For systems without mmap, the library still supplies an entry point | |
205 | to link to, but trying to initialize access to an mmap'd managed region | |
206 | always fails. */ | |
207 | ||
208 | /* ARGSUSED */ | |
209 | PTR | |
210 | mmalloc_attach (fd, baseaddr) | |
211 | int fd; | |
212 | PTR baseaddr; | |
213 | { | |
214 | return (NULL); | |
215 | } | |
216 | ||
217 | #endif /* defined (HAVE_MMAP) */ | |
218 |