winemac: Make show_window() and hide_window() tolerate being called when there's...
[wine.git] / server / mapping.c
blobfe30450454b82bda6f894f77d3ed204b418964e5
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 directory_link_name, /* link_name */
95 default_unlink_name, /* unlink_name */
96 no_open_file, /* open_file */
97 fd_close_handle, /* close_handle */
98 mapping_destroy /* destroy */
101 static const struct fd_ops mapping_fd_ops =
103 default_fd_get_poll_events, /* get_poll_events */
104 default_poll_event, /* poll_event */
105 mapping_get_fd_type, /* get_fd_type */
106 no_fd_read, /* read */
107 no_fd_write, /* write */
108 no_fd_flush, /* flush */
109 no_fd_ioctl, /* ioctl */
110 no_fd_queue_async, /* queue_async */
111 default_fd_reselect_async, /* reselect_async */
112 default_fd_cancel_async /* cancel_async */
115 static struct list shared_list = LIST_INIT(shared_list);
117 static size_t page_mask;
119 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
122 /* extend a file beyond the current end of file */
123 static int grow_file( int unix_fd, file_pos_t new_size )
125 static const char zero;
126 off_t size = new_size;
128 if (sizeof(new_size) > sizeof(size) && size != new_size)
130 set_error( STATUS_INVALID_PARAMETER );
131 return 0;
133 /* extend the file one byte beyond the requested size and then truncate it */
134 /* this should work around ftruncate implementations that can't extend files */
135 if (pwrite( unix_fd, &zero, 1, size ) != -1)
137 ftruncate( unix_fd, size );
138 return 1;
140 file_set_error();
141 return 0;
144 /* check if the current directory allows exec mappings */
145 static int check_current_dir_for_exec(void)
147 int fd;
148 char tmpfn[] = "anonmap.XXXXXX";
149 void *ret = MAP_FAILED;
151 fd = mkstemps( tmpfn, 0 );
152 if (fd == -1) return 0;
153 if (grow_file( fd, 1 ))
155 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
156 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
158 close( fd );
159 unlink( tmpfn );
160 return (ret != MAP_FAILED);
163 /* create a temp file for anonymous mappings */
164 static int create_temp_file( file_pos_t size )
166 static int temp_dir_fd = -1;
167 char tmpfn[] = "anonmap.XXXXXX";
168 int fd;
170 if (temp_dir_fd == -1)
172 temp_dir_fd = server_dir_fd;
173 if (!check_current_dir_for_exec())
175 /* the server dir is noexec, try the config dir instead */
176 fchdir( config_dir_fd );
177 if (check_current_dir_for_exec())
178 temp_dir_fd = config_dir_fd;
179 else /* neither works, fall back to server dir */
180 fchdir( server_dir_fd );
183 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
185 fd = mkstemps( tmpfn, 0 );
186 if (fd != -1)
188 if (!grow_file( fd, size ))
190 close( fd );
191 fd = -1;
193 unlink( tmpfn );
195 else file_set_error();
197 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
198 return fd;
201 /* find the shared PE mapping for a given mapping */
202 static struct file *get_shared_file( struct mapping *mapping )
204 struct mapping *ptr;
206 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
207 if (is_same_file_fd( ptr->fd, mapping->fd ))
208 return (struct file *)grab_object( ptr->shared_file );
209 return NULL;
212 /* return the size of the memory mapping and file range of a given section */
213 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
214 off_t *file_start, size_t *file_size )
216 static const unsigned int sector_align = 0x1ff;
218 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
219 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
221 *file_start = sec->PointerToRawData & ~sector_align;
222 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
223 if (*file_size > *map_size) *file_size = *map_size;
226 /* add a range to the committed list */
227 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
229 unsigned int i, j;
230 struct range *ranges;
232 if (!mapping->committed) return; /* everything committed already */
234 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
236 if (ranges[i].start > end) break;
237 if (ranges[i].end < start) continue;
238 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
239 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
241 for (j = i + 1; j < mapping->committed->count; j++)
243 if (ranges[j].start > end) break;
244 if (ranges[j].end > end) end = ranges[j].end;
246 if (j > i + 1)
248 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
249 mapping->committed->count -= j - (i + 1);
251 ranges[i].end = end;
253 return;
256 /* now add a new range */
258 if (mapping->committed->count == mapping->committed->max)
260 unsigned int new_size = mapping->committed->max * 2;
261 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
262 if (!new_ptr) return;
263 new_ptr->max = new_size;
264 ranges = new_ptr->ranges;
265 mapping->committed = new_ptr;
267 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
268 ranges[i].start = start;
269 ranges[i].end = end;
270 mapping->committed->count++;
273 /* find the range containing start and return whether it's committed */
274 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
276 unsigned int i;
277 struct range *ranges;
279 if (!mapping->committed) /* everything is committed */
281 *size = mapping->size - start;
282 return 1;
284 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
286 if (ranges[i].start > start)
288 *size = ranges[i].start - start;
289 return 0;
291 if (ranges[i].end > start)
293 *size = ranges[i].end - start;
294 return 1;
297 *size = mapping->size - start;
298 return 0;
301 /* allocate and fill the temp file for a shared PE image mapping */
302 static int build_shared_mapping( struct mapping *mapping, int fd,
303 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
305 unsigned int i;
306 mem_size_t total_size;
307 size_t file_size, map_size, max_size;
308 off_t shared_pos, read_pos, write_pos;
309 char *buffer = NULL;
310 int shared_fd;
311 long toread;
313 /* compute the total size of the shared mapping */
315 total_size = max_size = 0;
316 for (i = 0; i < nb_sec; i++)
318 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
319 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
321 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
322 if (file_size > max_size) max_size = file_size;
323 total_size += map_size;
326 if (!total_size) return 1; /* nothing to do */
328 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
330 /* create a temp file for the mapping */
332 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
333 if (!(mapping->shared_file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 )))
334 return 0;
336 if (!(buffer = malloc( max_size ))) goto error;
338 /* copy the shared sections data into the temp file */
340 shared_pos = 0;
341 for (i = 0; i < nb_sec; i++)
343 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
344 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
345 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
346 write_pos = shared_pos;
347 shared_pos += map_size;
348 if (!sec[i].PointerToRawData || !file_size) continue;
349 toread = file_size;
350 while (toread)
352 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
353 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
355 file_size -= toread;
356 break;
358 if (res <= 0) goto error;
359 toread -= res;
360 read_pos += res;
362 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
364 free( buffer );
365 return 1;
367 error:
368 release_object( mapping->shared_file );
369 mapping->shared_file = NULL;
370 free( buffer );
371 return 0;
374 /* retrieve the mapping parameters for an executable (PE) image */
375 static unsigned int get_image_params( struct mapping *mapping, int unix_fd, int protect )
377 IMAGE_DOS_HEADER dos;
378 IMAGE_SECTION_HEADER *sec = NULL;
379 struct
381 DWORD Signature;
382 IMAGE_FILE_HEADER FileHeader;
383 union
385 IMAGE_OPTIONAL_HEADER32 hdr32;
386 IMAGE_OPTIONAL_HEADER64 hdr64;
387 } opt;
388 } nt;
389 off_t pos;
390 int size;
392 /* load the headers */
394 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
395 if (dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
396 pos = dos.e_lfanew;
398 size = pread( unix_fd, &nt, sizeof(nt), pos );
399 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
400 /* zero out Optional header in the case it's not present or partial */
401 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
402 if (nt.Signature != IMAGE_NT_SIGNATURE)
404 if (*(WORD *)&nt.Signature == IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_NE_FORMAT;
405 return STATUS_INVALID_IMAGE_PROTECT;
408 mapping->cpu = current->process->cpu;
409 switch (mapping->cpu)
411 case CPU_x86:
412 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) return STATUS_INVALID_IMAGE_FORMAT;
413 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
414 break;
415 case CPU_x86_64:
416 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) return STATUS_INVALID_IMAGE_FORMAT;
417 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
418 break;
419 case CPU_POWERPC:
420 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_POWERPC) return STATUS_INVALID_IMAGE_FORMAT;
421 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
422 break;
423 case CPU_ARM:
424 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM &&
425 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_THUMB &&
426 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARMNT) return STATUS_INVALID_IMAGE_FORMAT;
427 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
428 break;
429 case CPU_ARM64:
430 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM64) return STATUS_INVALID_IMAGE_FORMAT;
431 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
432 break;
433 default:
434 return STATUS_INVALID_IMAGE_FORMAT;
437 switch (nt.opt.hdr32.Magic)
439 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
440 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
441 mapping->base = nt.opt.hdr32.ImageBase;
442 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
443 break;
444 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
445 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
446 mapping->base = nt.opt.hdr64.ImageBase;
447 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
448 break;
451 /* load the section headers */
453 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
454 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
455 if (pos + size > mapping->size) goto error;
456 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
457 if (!(sec = malloc( size ))) goto error;
458 if (pread( unix_fd, sec, size, pos ) != size) goto error;
460 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
462 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
464 mapping->protect = protect;
465 free( sec );
466 return 0;
468 error:
469 free( sec );
470 return STATUS_INVALID_FILE_FOR_SECTION;
473 static struct object *create_mapping( struct object *root, const struct unicode_str *name,
474 unsigned int attr, mem_size_t size, int protect,
475 obj_handle_t handle, const struct security_descriptor *sd )
477 struct mapping *mapping;
478 struct file *file;
479 struct fd *fd;
480 int access = 0;
481 int unix_fd;
482 struct stat st;
484 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
486 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
487 return NULL;
488 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
489 return &mapping->obj; /* Nothing else to do */
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\n",
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 );
611 static struct object_type *mapping_get_type( struct object *obj )
613 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
614 static const struct unicode_str str = { name, sizeof(name) };
615 return get_object_type( &str );
618 static struct fd *mapping_get_fd( struct object *obj )
620 struct mapping *mapping = (struct mapping *)obj;
621 return (struct fd *)grab_object( mapping->fd );
624 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
626 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
627 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
628 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
629 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
630 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
633 static void mapping_destroy( struct object *obj )
635 struct mapping *mapping = (struct mapping *)obj;
636 assert( obj->ops == &mapping_ops );
637 if (mapping->fd) release_object( mapping->fd );
638 if (mapping->shared_file)
640 release_object( mapping->shared_file );
641 list_remove( &mapping->shared_entry );
643 free( mapping->committed );
646 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
648 return FD_TYPE_FILE;
651 int get_page_size(void)
653 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
654 return page_mask + 1;
657 /* create a file mapping */
658 DECL_HANDLER(create_mapping)
660 struct object *root, *obj;
661 struct unicode_str name;
662 const struct security_descriptor *sd;
663 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
665 if (!objattr) return;
667 if ((obj = create_mapping( root, &name, objattr->attributes,
668 req->size, req->protect, req->file_handle, sd )))
670 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
671 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
672 else
673 reply->handle = alloc_handle_no_access_check( current->process, obj,
674 req->access, objattr->attributes );
675 release_object( obj );
678 if (root) release_object( root );
681 /* open a handle to a mapping */
682 DECL_HANDLER(open_mapping)
684 struct unicode_str name = get_req_unicode_str();
686 reply->handle = open_object( current->process, req->rootdir, req->access,
687 &mapping_ops, &name, req->attributes );
690 /* get a mapping information */
691 DECL_HANDLER(get_mapping_info)
693 struct mapping *mapping;
694 struct fd *fd;
696 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
698 if ((mapping->protect & VPROT_IMAGE) && mapping->cpu != current->process->cpu)
700 set_error( STATUS_INVALID_IMAGE_FORMAT );
701 release_object( mapping );
702 return;
705 reply->size = mapping->size;
706 reply->protect = mapping->protect;
707 reply->header_size = mapping->header_size;
708 reply->base = mapping->base;
709 reply->shared_file = 0;
710 if ((fd = get_obj_fd( &mapping->obj )))
712 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
713 release_object( fd );
715 if (mapping->shared_file)
717 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
718 GENERIC_READ|GENERIC_WRITE, 0 )))
720 if (reply->mapping) close_handle( current->process, reply->mapping );
723 release_object( mapping );
726 /* get a range of committed pages in a file mapping */
727 DECL_HANDLER(get_mapping_committed_range)
729 struct mapping *mapping;
731 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
733 if (!(req->offset & page_mask) && req->offset < mapping->size)
734 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
735 else
736 set_error( STATUS_INVALID_PARAMETER );
738 release_object( mapping );
742 /* add a range to the committed pages in a file mapping */
743 DECL_HANDLER(add_mapping_committed_range)
745 struct mapping *mapping;
747 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
749 if (!(req->size & page_mask) &&
750 !(req->offset & page_mask) &&
751 req->offset < mapping->size &&
752 req->size > 0 &&
753 req->size <= mapping->size - req->offset)
754 add_committed_range( mapping, req->offset, req->offset + req->size );
755 else
756 set_error( STATUS_INVALID_PARAMETER );
758 release_object( mapping );