server: Also store a file descriptor object for mappings.
[wine/hramrach.git] / server / mapping.c
blob2c4bc43a8e5294cb09427618190a768124bf2008
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 /* list of memory ranges, used to store committed info */
43 struct ranges
45 unsigned int count;
46 unsigned int max;
47 struct range
49 file_pos_t start;
50 file_pos_t end;
51 } ranges[1];
54 struct mapping
56 struct object obj; /* object header */
57 mem_size_t size; /* mapping size */
58 int protect; /* protection flags */
59 struct fd *fd; /* fd for mapped file */
60 struct file *file; /* file mapped */
61 int header_size; /* size of headers (for PE image mapping) */
62 client_ptr_t base; /* default base addr (for PE image mapping) */
63 struct ranges *committed; /* list of committed ranges in this mapping */
64 struct file *shared_file; /* temp file for shared PE mapping */
65 struct list shared_entry; /* entry in global shared PE mappings list */
68 static void mapping_dump( struct object *obj, int verbose );
69 static struct object_type *mapping_get_type( struct object *obj );
70 static struct fd *mapping_get_fd( struct object *obj );
71 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
72 static void mapping_destroy( struct object *obj );
74 static const struct object_ops mapping_ops =
76 sizeof(struct mapping), /* size */
77 mapping_dump, /* dump */
78 mapping_get_type, /* get_type */
79 no_add_queue, /* add_queue */
80 NULL, /* remove_queue */
81 NULL, /* signaled */
82 NULL, /* satisfied */
83 no_signal, /* signal */
84 mapping_get_fd, /* get_fd */
85 mapping_map_access, /* map_access */
86 default_get_sd, /* get_sd */
87 default_set_sd, /* set_sd */
88 no_lookup_name, /* lookup_name */
89 no_open_file, /* open_file */
90 fd_close_handle, /* close_handle */
91 mapping_destroy /* destroy */
94 static struct list shared_list = LIST_INIT(shared_list);
96 #ifdef __i386__
98 /* These are always the same on an i386, and it will be faster this way */
99 # define page_mask 0xfff
100 # define page_shift 12
101 # define init_page_size() do { /* nothing */ } while(0)
103 #else /* __i386__ */
105 static int page_shift, page_mask;
107 static void init_page_size(void)
109 int page_size;
110 # ifdef HAVE_GETPAGESIZE
111 page_size = getpagesize();
112 # else
113 # ifdef __svr4__
114 page_size = sysconf(_SC_PAGESIZE);
115 # else
116 # error Cannot get the page size on this platform
117 # endif
118 # endif
119 page_mask = page_size - 1;
120 /* Make sure we have a power of 2 */
121 assert( !(page_size & page_mask) );
122 page_shift = 0;
123 while ((1 << page_shift) != page_size) page_shift++;
125 #endif /* __i386__ */
127 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
130 /* extend a file beyond the current end of file */
131 static int extend_file( struct file *file, file_pos_t new_size )
133 static const char zero;
134 int unix_fd = get_file_unix_fd( file );
135 off_t size = new_size;
137 if (unix_fd == -1) return 0;
139 if (sizeof(new_size) > sizeof(size) && size != new_size)
141 set_error( STATUS_INVALID_PARAMETER );
142 return 0;
144 /* extend the file one byte beyond the requested size and then truncate it */
145 /* this should work around ftruncate implementations that can't extend files */
146 if (pwrite( unix_fd, &zero, 1, size ) != -1)
148 ftruncate( unix_fd, size );
149 return 1;
151 file_set_error();
152 return 0;
155 /* try to grow the file to the specified size */
156 static int grow_file( struct file *file, file_pos_t size )
158 struct stat st;
159 int unix_fd = get_file_unix_fd( file );
161 if (unix_fd == -1) return 0;
163 if (fstat( unix_fd, &st ) == -1)
165 file_set_error();
166 return 0;
168 if (st.st_size >= size) return 1; /* already large enough */
169 return extend_file( file, size );
172 /* find the shared PE mapping for a given mapping */
173 static struct file *get_shared_file( struct mapping *mapping )
175 struct mapping *ptr;
177 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
178 if (is_same_file( ptr->file, mapping->file ))
179 return (struct file *)grab_object( ptr->shared_file );
180 return NULL;
183 /* return the size of the memory mapping and file range of a given section */
184 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
185 off_t *file_start, size_t *file_size )
187 static const unsigned int sector_align = 0x1ff;
189 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
190 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
192 *file_start = sec->PointerToRawData & ~sector_align;
193 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
194 if (*file_size > *map_size) *file_size = *map_size;
197 /* add a range to the committed list */
198 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
200 unsigned int i, j;
201 struct range *ranges;
203 if (!mapping->committed) return; /* everything committed already */
205 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
207 if (ranges[i].start > end) break;
208 if (ranges[i].end < start) continue;
209 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
210 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
212 for (j = i + 1; j < mapping->committed->count; j++)
214 if (ranges[j].start > end) break;
215 if (ranges[j].end > end) end = ranges[j].end;
217 if (j > i + 1)
219 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
220 mapping->committed->count -= j - (i + 1);
222 ranges[i].end = end;
224 return;
227 /* now add a new range */
229 if (mapping->committed->count == mapping->committed->max)
231 unsigned int new_size = mapping->committed->max * 2;
232 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
233 if (!new_ptr) return;
234 new_ptr->max = new_size;
235 ranges = new_ptr->ranges;
236 mapping->committed = new_ptr;
238 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
239 ranges[i].start = start;
240 ranges[i].end = end;
241 mapping->committed->count++;
244 /* find the range containing start and return whether it's committed */
245 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
247 unsigned int i;
248 struct range *ranges;
250 if (!mapping->committed) /* everything is committed */
252 *size = mapping->size - start;
253 return 1;
255 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
257 if (ranges[i].start > start)
259 *size = ranges[i].start - start;
260 return 0;
262 if (ranges[i].end > start)
264 *size = ranges[i].end - start;
265 return 1;
268 *size = mapping->size - start;
269 return 0;
272 /* allocate and fill the temp file for a shared PE image mapping */
273 static int build_shared_mapping( struct mapping *mapping, int fd,
274 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
276 unsigned int i;
277 mem_size_t total_size;
278 size_t file_size, map_size, max_size;
279 off_t shared_pos, read_pos, write_pos;
280 char *buffer = NULL;
281 int shared_fd;
282 long toread;
284 /* compute the total size of the shared mapping */
286 total_size = max_size = 0;
287 for (i = 0; i < nb_sec; i++)
289 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
290 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
292 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
293 if (file_size > max_size) max_size = file_size;
294 total_size += map_size;
297 if (!total_size) return 1; /* nothing to do */
299 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
301 /* create a temp file for the mapping */
303 if (!(mapping->shared_file = create_temp_file( FILE_GENERIC_READ|FILE_GENERIC_WRITE ))) return 0;
304 if (!grow_file( mapping->shared_file, total_size )) goto error;
305 if ((shared_fd = get_file_unix_fd( mapping->shared_file )) == -1) goto error;
307 if (!(buffer = malloc( max_size ))) goto error;
309 /* copy the shared sections data into the temp file */
311 shared_pos = 0;
312 for (i = 0; i < nb_sec; i++)
314 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
315 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
316 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
317 write_pos = shared_pos;
318 shared_pos += map_size;
319 if (!sec[i].PointerToRawData || !file_size) continue;
320 toread = file_size;
321 while (toread)
323 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
324 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
326 file_size -= toread;
327 break;
329 if (res <= 0) goto error;
330 toread -= res;
331 read_pos += res;
333 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
335 free( buffer );
336 return 1;
338 error:
339 release_object( mapping->shared_file );
340 mapping->shared_file = NULL;
341 free( buffer );
342 return 0;
345 /* retrieve the mapping parameters for an executable (PE) image */
346 static int get_image_params( struct mapping *mapping )
348 IMAGE_DOS_HEADER dos;
349 IMAGE_SECTION_HEADER *sec = NULL;
350 struct
352 DWORD Signature;
353 IMAGE_FILE_HEADER FileHeader;
354 union
356 IMAGE_OPTIONAL_HEADER32 hdr32;
357 IMAGE_OPTIONAL_HEADER64 hdr64;
358 } opt;
359 } nt;
360 struct fd *fd;
361 off_t pos;
362 int unix_fd, size;
364 /* load the headers */
366 if (!(fd = mapping_get_fd( &mapping->obj ))) return 0;
367 if ((unix_fd = get_unix_fd( fd )) == -1) goto error;
368 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) goto error;
369 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
370 pos = dos.e_lfanew;
372 size = pread( unix_fd, &nt, sizeof(nt), pos );
373 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) goto error;
374 /* zero out Optional header in the case it's not present or partial */
375 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
376 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
378 switch (nt.opt.hdr32.Magic)
380 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
381 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
382 mapping->base = nt.opt.hdr32.ImageBase;
383 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
384 break;
385 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
386 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
387 mapping->base = nt.opt.hdr64.ImageBase;
388 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
389 break;
390 default:
391 goto error;
394 /* load the section headers */
396 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
397 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
398 if (pos + size > mapping->size) goto error;
399 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
400 if (!(sec = malloc( size ))) goto error;
401 if (pread( unix_fd, sec, size, pos ) != size) goto error;
403 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
405 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
407 mapping->protect = VPROT_IMAGE;
408 free( sec );
409 release_object( fd );
410 return 1;
412 error:
413 free( sec );
414 release_object( fd );
415 set_error( STATUS_INVALID_FILE_FOR_SECTION );
416 return 0;
419 /* get the size of the unix file associated with the mapping */
420 static inline int get_file_size( struct file *file, mem_size_t *size )
422 struct stat st;
423 int unix_fd = get_file_unix_fd( file );
425 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 0;
426 *size = st.st_size;
427 return 1;
430 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
431 unsigned int attr, mem_size_t size, int protect,
432 obj_handle_t handle, const struct security_descriptor *sd )
434 struct mapping *mapping;
435 int access = 0;
437 if (!page_mask) init_page_size();
439 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
440 return NULL;
441 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
442 return &mapping->obj; /* Nothing else to do */
444 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
445 GROUP_SECURITY_INFORMATION|
446 DACL_SECURITY_INFORMATION|
447 SACL_SECURITY_INFORMATION );
448 mapping->header_size = 0;
449 mapping->base = 0;
450 mapping->fd = NULL;
451 mapping->file = NULL;
452 mapping->shared_file = NULL;
453 mapping->committed = NULL;
455 if (protect & VPROT_READ) access |= FILE_READ_DATA;
456 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
458 if (handle)
460 if (!(protect & VPROT_COMMITTED))
462 set_error( STATUS_INVALID_PARAMETER );
463 goto error;
465 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
466 mapping->fd = get_obj_fd( (struct object *)mapping->file );
467 if (protect & VPROT_IMAGE)
469 if (!get_image_params( mapping )) goto error;
470 return &mapping->obj;
472 if (!size)
474 if (!get_file_size( mapping->file, &size )) goto error;
475 if (!size)
477 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
478 goto error;
481 else
483 if (!grow_file( mapping->file, size )) goto error;
486 else /* Anonymous mapping (no associated file) */
488 if (!size || (protect & VPROT_IMAGE))
490 set_error( STATUS_INVALID_PARAMETER );
491 goto error;
493 if (!(protect & VPROT_COMMITTED))
495 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
496 mapping->committed->count = 0;
497 mapping->committed->max = 8;
499 if (!(mapping->file = create_temp_file( access ))) goto error;
500 mapping->fd = get_obj_fd( (struct object *)mapping->file );
501 if (!grow_file( mapping->file, size )) goto error;
503 mapping->size = (size + page_mask) & ~((mem_size_t)page_mask);
504 mapping->protect = protect;
505 return &mapping->obj;
507 error:
508 release_object( mapping );
509 return NULL;
512 static void mapping_dump( struct object *obj, int verbose )
514 struct mapping *mapping = (struct mapping *)obj;
515 assert( obj->ops == &mapping_ops );
516 fprintf( stderr, "Mapping size=%08x%08x prot=%08x fd=%p header_size=%08x base=%08lx "
517 "shared_file=%p ",
518 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
519 mapping->protect, mapping->fd, mapping->header_size,
520 (unsigned long)mapping->base, mapping->shared_file );
521 dump_object_name( &mapping->obj );
522 fputc( '\n', stderr );
525 static struct object_type *mapping_get_type( struct object *obj )
527 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
528 static const struct unicode_str str = { name, sizeof(name) };
529 return get_object_type( &str );
532 static struct fd *mapping_get_fd( struct object *obj )
534 struct mapping *mapping = (struct mapping *)obj;
535 return (struct fd *)grab_object( mapping->fd );
538 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
540 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
541 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
542 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
543 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
544 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
547 static void mapping_destroy( struct object *obj )
549 struct mapping *mapping = (struct mapping *)obj;
550 assert( obj->ops == &mapping_ops );
551 if (mapping->file) release_object( mapping->file );
552 if (mapping->fd) release_object( mapping->fd );
553 if (mapping->shared_file)
555 release_object( mapping->shared_file );
556 list_remove( &mapping->shared_entry );
558 free( mapping->committed );
561 int get_page_size(void)
563 if (!page_mask) init_page_size();
564 return page_mask + 1;
567 /* create a file mapping */
568 DECL_HANDLER(create_mapping)
570 struct object *obj;
571 struct unicode_str name;
572 struct directory *root = NULL;
573 const struct object_attributes *objattr = get_req_data();
574 const struct security_descriptor *sd;
576 reply->handle = 0;
578 if (!objattr_is_valid( objattr, get_req_data_size() ))
579 return;
581 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
582 objattr_get_name( objattr, &name );
584 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
585 return;
587 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
589 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
590 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
591 else
592 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
593 release_object( obj );
596 if (root) release_object( root );
599 /* open a handle to a mapping */
600 DECL_HANDLER(open_mapping)
602 struct unicode_str name;
603 struct directory *root = NULL;
604 struct mapping *mapping;
606 get_req_unicode_str( &name );
607 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
608 return;
610 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
612 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
613 release_object( mapping );
616 if (root) release_object( root );
619 /* get a mapping information */
620 DECL_HANDLER(get_mapping_info)
622 struct mapping *mapping;
623 struct fd *fd;
625 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
626 req->access, &mapping_ops )))
628 reply->size = mapping->size;
629 reply->protect = mapping->protect;
630 reply->header_size = mapping->header_size;
631 reply->base = mapping->base;
632 reply->shared_file = 0;
633 if ((fd = get_obj_fd( &mapping->obj )))
635 if (!is_fd_removable(fd))
636 reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
637 release_object( fd );
639 if (mapping->shared_file)
641 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
642 GENERIC_READ|GENERIC_WRITE, 0 )))
644 if (reply->mapping) close_handle( current->process, reply->mapping );
647 release_object( mapping );
651 /* get a range of committed pages in a file mapping */
652 DECL_HANDLER(get_mapping_committed_range)
654 struct mapping *mapping;
656 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle, 0, &mapping_ops )))
658 if (!(req->offset & page_mask) && req->offset < mapping->size)
659 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
660 else
661 set_error( STATUS_INVALID_PARAMETER );
663 release_object( mapping );
667 /* add a range to the committed pages in a file mapping */
668 DECL_HANDLER(add_mapping_committed_range)
670 struct mapping *mapping;
672 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle, 0, &mapping_ops )))
674 if (!(req->size & page_mask) &&
675 !(req->offset & page_mask) &&
676 req->offset < mapping->size &&
677 req->size > 0 &&
678 req->size <= mapping->size - req->offset)
679 add_committed_range( mapping, req->offset, req->offset + req->size );
680 else
681 set_error( STATUS_INVALID_PARAMETER );
683 release_object( mapping );