ntoskrnl.exe: Add stub MmUnlockPagableImageSection.
[wine.git] / server / mapping.c
blob64b3003b7c9cb3eb8f9ae50a368edb880f7757d7
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 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
32 #include <unistd.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winternl.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "security.h"
46 /* list of memory ranges, used to store committed info */
47 struct ranges
49 unsigned int count;
50 unsigned int max;
51 struct range
53 file_pos_t start;
54 file_pos_t end;
55 } ranges[1];
58 struct mapping
60 struct object obj; /* object header */
61 mem_size_t size; /* mapping size */
62 int protect; /* protection flags */
63 struct fd *fd; /* fd for mapped file */
64 enum cpu_type cpu; /* client CPU (for PE image mapping) */
65 int header_size; /* size of headers (for PE image mapping) */
66 client_ptr_t base; /* default base addr (for PE image mapping) */
67 struct ranges *committed; /* list of committed ranges in this mapping */
68 struct file *shared_file; /* temp file for shared PE mapping */
69 struct list shared_entry; /* entry in global shared PE mappings list */
72 static void mapping_dump( struct object *obj, int verbose );
73 static struct object_type *mapping_get_type( struct object *obj );
74 static struct fd *mapping_get_fd( struct object *obj );
75 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
76 static void mapping_destroy( struct object *obj );
77 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
79 static const struct object_ops mapping_ops =
81 sizeof(struct mapping), /* size */
82 mapping_dump, /* dump */
83 mapping_get_type, /* get_type */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
86 NULL, /* signaled */
87 NULL, /* satisfied */
88 no_signal, /* signal */
89 mapping_get_fd, /* get_fd */
90 mapping_map_access, /* map_access */
91 default_get_sd, /* get_sd */
92 default_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 no_open_file, /* open_file */
95 fd_close_handle, /* close_handle */
96 mapping_destroy /* destroy */
99 static const struct fd_ops mapping_fd_ops =
101 default_fd_get_poll_events, /* get_poll_events */
102 default_poll_event, /* poll_event */
103 no_flush, /* flush */
104 mapping_get_fd_type, /* get_fd_type */
105 no_fd_ioctl, /* ioctl */
106 no_fd_queue_async, /* queue_async */
107 default_fd_reselect_async, /* reselect_async */
108 default_fd_cancel_async /* cancel_async */
111 static struct list shared_list = LIST_INIT(shared_list);
113 static size_t page_mask;
115 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
118 /* extend a file beyond the current end of file */
119 static int grow_file( int unix_fd, file_pos_t new_size )
121 static const char zero;
122 off_t size = new_size;
124 if (sizeof(new_size) > sizeof(size) && size != new_size)
126 set_error( STATUS_INVALID_PARAMETER );
127 return 0;
129 /* extend the file one byte beyond the requested size and then truncate it */
130 /* this should work around ftruncate implementations that can't extend files */
131 if (pwrite( unix_fd, &zero, 1, size ) != -1)
133 ftruncate( unix_fd, size );
134 return 1;
136 file_set_error();
137 return 0;
140 /* check if the current directory allows exec mappings */
141 static int check_current_dir_for_exec(void)
143 int fd;
144 char tmpfn[] = "anonmap.XXXXXX";
145 void *ret = MAP_FAILED;
147 fd = mkstemps( tmpfn, 0 );
148 if (fd == -1) return 0;
149 if (grow_file( fd, 1 ))
151 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
152 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
154 close( fd );
155 unlink( tmpfn );
156 return (ret != MAP_FAILED);
159 /* create a temp file for anonymous mappings */
160 static int create_temp_file( file_pos_t size )
162 static int temp_dir_fd = -1;
163 char tmpfn[] = "anonmap.XXXXXX";
164 int fd;
166 if (temp_dir_fd == -1)
168 temp_dir_fd = server_dir_fd;
169 if (!check_current_dir_for_exec())
171 /* the server dir is noexec, try the config dir instead */
172 fchdir( config_dir_fd );
173 if (check_current_dir_for_exec())
174 temp_dir_fd = config_dir_fd;
175 else /* neither works, fall back to server dir */
176 fchdir( server_dir_fd );
179 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
181 fd = mkstemps( tmpfn, 0 );
182 if (fd != -1)
184 if (!grow_file( fd, size ))
186 close( fd );
187 fd = -1;
189 unlink( tmpfn );
191 else file_set_error();
193 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
194 return fd;
197 /* find the shared PE mapping for a given mapping */
198 static struct file *get_shared_file( struct mapping *mapping )
200 struct mapping *ptr;
202 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
203 if (is_same_file_fd( ptr->fd, mapping->fd ))
204 return (struct file *)grab_object( ptr->shared_file );
205 return NULL;
208 /* return the size of the memory mapping and file range of a given section */
209 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
210 off_t *file_start, size_t *file_size )
212 static const unsigned int sector_align = 0x1ff;
214 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
215 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
217 *file_start = sec->PointerToRawData & ~sector_align;
218 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
219 if (*file_size > *map_size) *file_size = *map_size;
222 /* add a range to the committed list */
223 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
225 unsigned int i, j;
226 struct range *ranges;
228 if (!mapping->committed) return; /* everything committed already */
230 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
232 if (ranges[i].start > end) break;
233 if (ranges[i].end < start) continue;
234 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
235 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
237 for (j = i + 1; j < mapping->committed->count; j++)
239 if (ranges[j].start > end) break;
240 if (ranges[j].end > end) end = ranges[j].end;
242 if (j > i + 1)
244 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
245 mapping->committed->count -= j - (i + 1);
247 ranges[i].end = end;
249 return;
252 /* now add a new range */
254 if (mapping->committed->count == mapping->committed->max)
256 unsigned int new_size = mapping->committed->max * 2;
257 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
258 if (!new_ptr) return;
259 new_ptr->max = new_size;
260 ranges = new_ptr->ranges;
261 mapping->committed = new_ptr;
263 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
264 ranges[i].start = start;
265 ranges[i].end = end;
266 mapping->committed->count++;
269 /* find the range containing start and return whether it's committed */
270 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
272 unsigned int i;
273 struct range *ranges;
275 if (!mapping->committed) /* everything is committed */
277 *size = mapping->size - start;
278 return 1;
280 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
282 if (ranges[i].start > start)
284 *size = ranges[i].start - start;
285 return 0;
287 if (ranges[i].end > start)
289 *size = ranges[i].end - start;
290 return 1;
293 *size = mapping->size - start;
294 return 0;
297 /* allocate and fill the temp file for a shared PE image mapping */
298 static int build_shared_mapping( struct mapping *mapping, int fd,
299 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
301 unsigned int i;
302 mem_size_t total_size;
303 size_t file_size, map_size, max_size;
304 off_t shared_pos, read_pos, write_pos;
305 char *buffer = NULL;
306 int shared_fd;
307 long toread;
309 /* compute the total size of the shared mapping */
311 total_size = max_size = 0;
312 for (i = 0; i < nb_sec; i++)
314 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
315 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
317 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
318 if (file_size > max_size) max_size = file_size;
319 total_size += map_size;
322 if (!total_size) return 1; /* nothing to do */
324 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
326 /* create a temp file for the mapping */
328 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
329 if (!(mapping->shared_file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 )))
330 return 0;
332 if (!(buffer = malloc( max_size ))) goto error;
334 /* copy the shared sections data into the temp file */
336 shared_pos = 0;
337 for (i = 0; i < nb_sec; i++)
339 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
340 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
341 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
342 write_pos = shared_pos;
343 shared_pos += map_size;
344 if (!sec[i].PointerToRawData || !file_size) continue;
345 toread = file_size;
346 while (toread)
348 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
349 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
351 file_size -= toread;
352 break;
354 if (res <= 0) goto error;
355 toread -= res;
356 read_pos += res;
358 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
360 free( buffer );
361 return 1;
363 error:
364 release_object( mapping->shared_file );
365 mapping->shared_file = NULL;
366 free( buffer );
367 return 0;
370 /* retrieve the mapping parameters for an executable (PE) image */
371 static unsigned int get_image_params( struct mapping *mapping, int unix_fd, int protect )
373 IMAGE_DOS_HEADER dos;
374 IMAGE_SECTION_HEADER *sec = NULL;
375 struct
377 DWORD Signature;
378 IMAGE_FILE_HEADER FileHeader;
379 union
381 IMAGE_OPTIONAL_HEADER32 hdr32;
382 IMAGE_OPTIONAL_HEADER64 hdr64;
383 } opt;
384 } nt;
385 off_t pos;
386 int size;
388 /* load the headers */
390 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
391 if (dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
392 pos = dos.e_lfanew;
394 size = pread( unix_fd, &nt, sizeof(nt), pos );
395 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
396 /* zero out Optional header in the case it's not present or partial */
397 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
398 if (nt.Signature != IMAGE_NT_SIGNATURE)
400 if (*(WORD *)&nt.Signature == IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_NE_FORMAT;
401 return STATUS_INVALID_IMAGE_PROTECT;
404 mapping->cpu = current->process->cpu;
405 switch (mapping->cpu)
407 case CPU_x86:
408 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) return STATUS_INVALID_IMAGE_FORMAT;
409 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
410 break;
411 case CPU_x86_64:
412 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) return STATUS_INVALID_IMAGE_FORMAT;
413 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
414 break;
415 case CPU_POWERPC:
416 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_POWERPC) return STATUS_INVALID_IMAGE_FORMAT;
417 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
418 break;
419 case CPU_ARM:
420 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM &&
421 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_THUMB &&
422 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARMNT) return STATUS_INVALID_IMAGE_FORMAT;
423 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
424 break;
425 case CPU_ARM64:
426 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM64) return STATUS_INVALID_IMAGE_FORMAT;
427 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
428 break;
429 default:
430 return STATUS_INVALID_IMAGE_FORMAT;
433 switch (nt.opt.hdr32.Magic)
435 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
436 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
437 mapping->base = nt.opt.hdr32.ImageBase;
438 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
439 break;
440 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
441 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
442 mapping->base = nt.opt.hdr64.ImageBase;
443 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
444 break;
447 /* load the section headers */
449 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
450 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
451 if (pos + size > mapping->size) goto error;
452 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
453 if (!(sec = malloc( size ))) goto error;
454 if (pread( unix_fd, sec, size, pos ) != size) goto error;
456 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
458 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
460 mapping->protect = protect;
461 free( sec );
462 return 0;
464 error:
465 free( sec );
466 return STATUS_INVALID_FILE_FOR_SECTION;
469 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
470 unsigned int attr, mem_size_t size, int protect,
471 obj_handle_t handle, const struct security_descriptor *sd )
473 struct mapping *mapping;
474 struct file *file;
475 struct fd *fd;
476 int access = 0;
477 int unix_fd;
478 struct stat st;
480 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
482 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
483 return NULL;
484 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
485 return &mapping->obj; /* Nothing else to do */
487 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
488 GROUP_SECURITY_INFORMATION|
489 DACL_SECURITY_INFORMATION|
490 SACL_SECURITY_INFORMATION );
491 mapping->header_size = 0;
492 mapping->base = 0;
493 mapping->fd = NULL;
494 mapping->shared_file = NULL;
495 mapping->committed = NULL;
497 if (protect & VPROT_READ) access |= FILE_READ_DATA;
498 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
500 if (handle)
502 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
503 unsigned int mapping_access = FILE_MAPPING_ACCESS;
505 if (!(protect & VPROT_COMMITTED))
507 set_error( STATUS_INVALID_PARAMETER );
508 goto error;
510 if (!(file = get_file_obj( current->process, handle, access ))) goto error;
511 fd = get_obj_fd( (struct object *)file );
513 /* file sharing rules for mappings are different so we use magic the access rights */
514 if (protect & VPROT_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
515 else if (protect & VPROT_WRITE) mapping_access |= FILE_MAPPING_WRITE;
517 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
519 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
520 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
522 release_object( file );
523 release_object( fd );
524 if (!mapping->fd) goto error;
526 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
527 if (protect & VPROT_IMAGE)
529 unsigned int err = get_image_params( mapping, unix_fd, protect );
530 if (!err) return &mapping->obj;
531 set_error( err );
532 goto error;
534 if (fstat( unix_fd, &st ) == -1)
536 file_set_error();
537 goto error;
539 if (!size)
541 if (!(size = st.st_size))
543 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
544 goto error;
547 else if (st.st_size < size && !grow_file( unix_fd, size )) goto error;
549 else /* Anonymous mapping (no associated file) */
551 if (!size || (protect & VPROT_IMAGE))
553 set_error( STATUS_INVALID_PARAMETER );
554 goto error;
556 if (!(protect & VPROT_COMMITTED))
558 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
559 mapping->committed->count = 0;
560 mapping->committed->max = 8;
562 if ((unix_fd = create_temp_file( size )) == -1) goto error;
563 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
564 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
565 allow_fd_caching( mapping->fd );
567 mapping->size = (size + page_mask) & ~((mem_size_t)page_mask);
568 mapping->protect = protect;
569 return &mapping->obj;
571 error:
572 release_object( mapping );
573 return NULL;
576 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
578 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
581 /* open a new file handle to the file backing the mapping */
582 obj_handle_t open_mapping_file( struct process *process, struct mapping *mapping,
583 unsigned int access, unsigned int sharing )
585 obj_handle_t handle;
586 struct file *file = create_file_for_fd_obj( mapping->fd, access, sharing );
588 if (!file) return 0;
589 handle = alloc_handle( process, file, access, 0 );
590 release_object( file );
591 return handle;
594 struct mapping *grab_mapping_unless_removable( struct mapping *mapping )
596 if (is_fd_removable( mapping->fd )) return NULL;
597 return (struct mapping *)grab_object( mapping );
600 static void mapping_dump( struct object *obj, int verbose )
602 struct mapping *mapping = (struct mapping *)obj;
603 assert( obj->ops == &mapping_ops );
604 fprintf( stderr, "Mapping size=%08x%08x prot=%08x fd=%p header_size=%08x base=%08lx "
605 "shared_file=%p ",
606 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
607 mapping->protect, mapping->fd, mapping->header_size,
608 (unsigned long)mapping->base, mapping->shared_file );
609 dump_object_name( &mapping->obj );
610 fputc( '\n', stderr );
613 static struct object_type *mapping_get_type( struct object *obj )
615 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
616 static const struct unicode_str str = { name, sizeof(name) };
617 return get_object_type( &str );
620 static struct fd *mapping_get_fd( struct object *obj )
622 struct mapping *mapping = (struct mapping *)obj;
623 return (struct fd *)grab_object( mapping->fd );
626 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
628 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
629 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
630 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
631 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
632 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
635 static void mapping_destroy( struct object *obj )
637 struct mapping *mapping = (struct mapping *)obj;
638 assert( obj->ops == &mapping_ops );
639 if (mapping->fd) release_object( mapping->fd );
640 if (mapping->shared_file)
642 release_object( mapping->shared_file );
643 list_remove( &mapping->shared_entry );
645 free( mapping->committed );
648 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
650 return FD_TYPE_FILE;
653 int get_page_size(void)
655 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
656 return page_mask + 1;
659 /* create a file mapping */
660 DECL_HANDLER(create_mapping)
662 struct object *obj;
663 struct unicode_str name;
664 struct directory *root = NULL;
665 const struct object_attributes *objattr = get_req_data();
666 const struct security_descriptor *sd;
668 reply->handle = 0;
670 if (!objattr_is_valid( objattr, get_req_data_size() ))
671 return;
673 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
674 objattr_get_name( objattr, &name );
676 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
677 return;
679 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
681 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
682 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
683 else
684 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
685 release_object( obj );
688 if (root) release_object( root );
691 /* open a handle to a mapping */
692 DECL_HANDLER(open_mapping)
694 struct unicode_str name;
695 struct directory *root = NULL;
696 struct mapping *mapping;
698 get_req_unicode_str( &name );
699 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
700 return;
702 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
704 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
705 release_object( mapping );
708 if (root) release_object( root );
711 /* get a mapping information */
712 DECL_HANDLER(get_mapping_info)
714 struct mapping *mapping;
715 struct fd *fd;
717 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
719 if ((mapping->protect & VPROT_IMAGE) && mapping->cpu != current->process->cpu)
721 set_error( STATUS_INVALID_IMAGE_FORMAT );
722 release_object( mapping );
723 return;
726 reply->size = mapping->size;
727 reply->protect = mapping->protect;
728 reply->header_size = mapping->header_size;
729 reply->base = mapping->base;
730 reply->shared_file = 0;
731 if ((fd = get_obj_fd( &mapping->obj )))
733 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
734 release_object( fd );
736 if (mapping->shared_file)
738 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
739 GENERIC_READ|GENERIC_WRITE, 0 )))
741 if (reply->mapping) close_handle( current->process, reply->mapping );
744 release_object( mapping );
747 /* get a range of committed pages in a file mapping */
748 DECL_HANDLER(get_mapping_committed_range)
750 struct mapping *mapping;
752 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
754 if (!(req->offset & page_mask) && req->offset < mapping->size)
755 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
756 else
757 set_error( STATUS_INVALID_PARAMETER );
759 release_object( mapping );
763 /* add a range to the committed pages in a file mapping */
764 DECL_HANDLER(add_mapping_committed_range)
766 struct mapping *mapping;
768 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
770 if (!(req->size & page_mask) &&
771 !(req->offset & page_mask) &&
772 req->offset < mapping->size &&
773 req->size > 0 &&
774 req->size <= mapping->size - req->offset)
775 add_committed_range( mapping, req->offset, req->offset + req->size );
776 else
777 set_error( STATUS_INVALID_PARAMETER );
779 release_object( mapping );