d3dx8: Implement the C++ stuff of the D3DXVECTOR2 structure.
[wine/multimedia.git] / server / mapping.c
blob9f062d0b0f5136ca6c8d079aaaea5b4b0ba94a1a
1 /*
2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "file.h"
37 #include "handle.h"
38 #include "thread.h"
39 #include "request.h"
40 #include "security.h"
42 struct mapping
44 struct object obj; /* object header */
45 file_pos_t size; /* mapping size */
46 int protect; /* protection flags */
47 struct file *file; /* file mapped */
48 int header_size; /* size of headers (for PE image mapping) */
49 void *base; /* default base addr (for PE image mapping) */
50 struct file *shared_file; /* temp file for shared PE mapping */
51 struct list shared_entry; /* entry in global shared PE mappings list */
54 static void mapping_dump( struct object *obj, int verbose );
55 static struct fd *mapping_get_fd( struct object *obj );
56 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
57 static void mapping_destroy( struct object *obj );
59 static const struct object_ops mapping_ops =
61 sizeof(struct mapping), /* size */
62 mapping_dump, /* dump */
63 no_add_queue, /* add_queue */
64 NULL, /* remove_queue */
65 NULL, /* signaled */
66 NULL, /* satisfied */
67 no_signal, /* signal */
68 mapping_get_fd, /* get_fd */
69 mapping_map_access, /* map_access */
70 default_get_sd, /* get_sd */
71 default_set_sd, /* set_sd */
72 no_lookup_name, /* lookup_name */
73 no_open_file, /* open_file */
74 fd_close_handle, /* close_handle */
75 mapping_destroy /* destroy */
78 static struct list shared_list = LIST_INIT(shared_list);
80 #ifdef __i386__
82 /* These are always the same on an i386, and it will be faster this way */
83 # define page_mask 0xfff
84 # define page_shift 12
85 # define init_page_size() do { /* nothing */ } while(0)
87 #else /* __i386__ */
89 static int page_shift, page_mask;
91 static void init_page_size(void)
93 int page_size;
94 # ifdef HAVE_GETPAGESIZE
95 page_size = getpagesize();
96 # else
97 # ifdef __svr4__
98 page_size = sysconf(_SC_PAGESIZE);
99 # else
100 # error Cannot get the page size on this platform
101 # endif
102 # endif
103 page_mask = page_size - 1;
104 /* Make sure we have a power of 2 */
105 assert( !(page_size & page_mask) );
106 page_shift = 0;
107 while ((1 << page_shift) != page_size) page_shift++;
109 #endif /* __i386__ */
111 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
114 /* find the shared PE mapping for a given mapping */
115 static struct file *get_shared_file( struct mapping *mapping )
117 struct mapping *ptr;
119 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
120 if (is_same_file( ptr->file, mapping->file ))
121 return (struct file *)grab_object( ptr->shared_file );
122 return NULL;
125 /* return the size of the memory mapping and file range of a given section */
126 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
127 off_t *file_start, size_t *file_size )
129 static const unsigned int sector_align = 0x1ff;
131 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
132 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
134 *file_start = sec->PointerToRawData & ~sector_align;
135 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
136 if (*file_size > *map_size) *file_size = *map_size;
139 /* allocate and fill the temp file for a shared PE image mapping */
140 static int build_shared_mapping( struct mapping *mapping, int fd,
141 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
143 unsigned int i;
144 file_pos_t total_size;
145 size_t file_size, map_size, max_size;
146 off_t shared_pos, read_pos, write_pos;
147 char *buffer = NULL;
148 int shared_fd;
149 long toread;
151 /* compute the total size of the shared mapping */
153 total_size = max_size = 0;
154 for (i = 0; i < nb_sec; i++)
156 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
157 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
159 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
160 if (file_size > max_size) max_size = file_size;
161 total_size += map_size;
164 if (!total_size) return 1; /* nothing to do */
166 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
168 /* create a temp file for the mapping */
170 if (!(mapping->shared_file = create_temp_file( FILE_GENERIC_READ|FILE_GENERIC_WRITE ))) return 0;
171 if (!grow_file( mapping->shared_file, total_size )) goto error;
172 if ((shared_fd = get_file_unix_fd( mapping->shared_file )) == -1) goto error;
174 if (!(buffer = malloc( max_size ))) goto error;
176 /* copy the shared sections data into the temp file */
178 shared_pos = 0;
179 for (i = 0; i < nb_sec; i++)
181 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
182 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
183 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
184 write_pos = shared_pos;
185 shared_pos += map_size;
186 if (!sec[i].PointerToRawData || !file_size) continue;
187 toread = file_size;
188 while (toread)
190 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
191 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
193 file_size -= toread;
194 break;
196 if (res <= 0) goto error;
197 toread -= res;
198 read_pos += res;
200 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
202 free( buffer );
203 return 1;
205 error:
206 release_object( mapping->shared_file );
207 mapping->shared_file = NULL;
208 free( buffer );
209 return 0;
212 /* retrieve the mapping parameters for an executable (PE) image */
213 static int get_image_params( struct mapping *mapping )
215 IMAGE_DOS_HEADER dos;
216 IMAGE_NT_HEADERS nt;
217 IMAGE_SECTION_HEADER *sec = NULL;
218 struct fd *fd;
219 off_t pos;
220 int unix_fd, size, toread;
222 /* load the headers */
224 if (!(fd = mapping_get_fd( &mapping->obj ))) return 0;
225 if ((unix_fd = get_unix_fd( fd )) == -1) goto error;
226 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) goto error;
227 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
228 pos = dos.e_lfanew;
230 if (pread( unix_fd, &nt.Signature, sizeof(nt.Signature), pos ) != sizeof(nt.Signature))
231 goto error;
232 pos += sizeof(nt.Signature);
233 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
234 if (pread( unix_fd, &nt.FileHeader, sizeof(nt.FileHeader), pos ) != sizeof(nt.FileHeader))
235 goto error;
236 pos += sizeof(nt.FileHeader);
237 /* zero out Optional header in the case it's not present or partial */
238 memset(&nt.OptionalHeader, 0, sizeof(nt.OptionalHeader));
239 toread = min( sizeof(nt.OptionalHeader), nt.FileHeader.SizeOfOptionalHeader );
240 if (pread( unix_fd, &nt.OptionalHeader, toread, pos ) != toread) goto error;
241 pos += nt.FileHeader.SizeOfOptionalHeader;
243 /* load the section headers */
245 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
246 if (!(sec = malloc( size ))) goto error;
247 if (pread( unix_fd, sec, size, pos ) != size) goto error;
249 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
251 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
253 mapping->size = ROUND_SIZE( nt.OptionalHeader.SizeOfImage );
254 mapping->base = (void *)nt.OptionalHeader.ImageBase;
255 mapping->header_size = max( pos + size, nt.OptionalHeader.SizeOfHeaders );
256 mapping->protect = VPROT_IMAGE;
258 /* sanity check */
259 if (pos + size > mapping->size) goto error;
261 free( sec );
262 release_object( fd );
263 return 1;
265 error:
266 free( sec );
267 release_object( fd );
268 set_error( STATUS_INVALID_FILE_FOR_SECTION );
269 return 0;
272 /* get the size of the unix file associated with the mapping */
273 static inline int get_file_size( struct file *file, file_pos_t *size )
275 struct stat st;
276 int unix_fd = get_file_unix_fd( file );
278 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 0;
279 *size = st.st_size;
280 return 1;
283 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
284 unsigned int attr, file_pos_t size, int protect,
285 obj_handle_t handle, const struct security_descriptor *sd )
287 struct mapping *mapping;
288 int access = 0;
290 if (!page_mask) init_page_size();
292 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
293 return NULL;
294 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
295 return &mapping->obj; /* Nothing else to do */
297 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
298 GROUP_SECURITY_INFORMATION|
299 DACL_SECURITY_INFORMATION|
300 SACL_SECURITY_INFORMATION );
301 mapping->header_size = 0;
302 mapping->base = NULL;
303 mapping->shared_file = NULL;
305 if (protect & VPROT_READ) access |= FILE_READ_DATA;
306 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
308 if (handle)
310 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
311 if (protect & VPROT_IMAGE)
313 if (!get_image_params( mapping )) goto error;
314 return &mapping->obj;
316 if (!size)
318 if (!get_file_size( mapping->file, &size )) goto error;
319 if (!size)
321 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
322 goto error;
325 else
327 if (!grow_file( mapping->file, size )) goto error;
330 else /* Anonymous mapping (no associated file) */
332 if (!size || (protect & VPROT_IMAGE))
334 set_error( STATUS_INVALID_PARAMETER );
335 mapping->file = NULL;
336 goto error;
338 if (!(mapping->file = create_temp_file( access ))) goto error;
339 if (!grow_file( mapping->file, size )) goto error;
341 mapping->size = (size + page_mask) & ~((file_pos_t)page_mask);
342 mapping->protect = protect;
343 return &mapping->obj;
345 error:
346 release_object( mapping );
347 return NULL;
350 static void mapping_dump( struct object *obj, int verbose )
352 struct mapping *mapping = (struct mapping *)obj;
353 assert( obj->ops == &mapping_ops );
354 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
355 "shared_file=%p ",
356 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
357 mapping->protect, mapping->file, mapping->header_size,
358 mapping->base, mapping->shared_file );
359 dump_object_name( &mapping->obj );
360 fputc( '\n', stderr );
363 static struct fd *mapping_get_fd( struct object *obj )
365 struct mapping *mapping = (struct mapping *)obj;
366 return get_obj_fd( (struct object *)mapping->file );
369 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
371 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
372 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
373 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
374 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
375 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
378 static void mapping_destroy( struct object *obj )
380 struct mapping *mapping = (struct mapping *)obj;
381 assert( obj->ops == &mapping_ops );
382 if (mapping->file) release_object( mapping->file );
383 if (mapping->shared_file)
385 release_object( mapping->shared_file );
386 list_remove( &mapping->shared_entry );
390 int get_page_size(void)
392 if (!page_mask) init_page_size();
393 return page_mask + 1;
396 /* create a file mapping */
397 DECL_HANDLER(create_mapping)
399 struct object *obj;
400 struct unicode_str name;
401 struct directory *root = NULL;
402 const struct object_attributes *objattr = get_req_data();
403 const struct security_descriptor *sd;
405 reply->handle = 0;
407 if (!objattr_is_valid( objattr, get_req_data_size() ))
408 return;
410 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
411 objattr_get_name( objattr, &name );
413 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
414 return;
416 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
418 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
419 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
420 else
421 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
422 release_object( obj );
425 if (root) release_object( root );
428 /* open a handle to a mapping */
429 DECL_HANDLER(open_mapping)
431 struct unicode_str name;
432 struct directory *root = NULL;
433 struct mapping *mapping;
435 get_req_unicode_str( &name );
436 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
437 return;
439 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
441 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
442 release_object( mapping );
445 if (root) release_object( root );
448 /* get a mapping information */
449 DECL_HANDLER(get_mapping_info)
451 struct mapping *mapping;
452 struct fd *fd;
454 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
455 0, &mapping_ops )))
457 reply->size = mapping->size;
458 reply->protect = mapping->protect;
459 reply->header_size = mapping->header_size;
460 reply->base = mapping->base;
461 reply->shared_file = 0;
462 if ((fd = get_obj_fd( &mapping->obj )))
464 if (!is_fd_removable(fd))
465 reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
466 release_object( fd );
468 if (mapping->shared_file)
470 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
471 GENERIC_READ|GENERIC_WRITE, 0 )))
473 if (reply->mapping) close_handle( current->process, reply->mapping );
476 release_object( mapping );