ntdll: Implement the SectionBasicInformation class of NtQuerySection.
[wine.git] / server / mapping.c
blob5f593d969478f214667c58861cde4525763310ed
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 unsigned int flags; /* SEC_* flags */
63 int protect; /* protection flags */
64 struct fd *fd; /* fd for mapped file */
65 enum cpu_type cpu; /* client CPU (for PE image mapping) */
66 int header_size; /* size of headers (for PE image mapping) */
67 client_ptr_t base; /* default base addr (for PE image mapping) */
68 struct ranges *committed; /* list of committed ranges in this mapping */
69 struct file *shared_file; /* temp file for shared PE mapping */
70 struct list shared_entry; /* entry in global shared PE mappings list */
73 static void mapping_dump( struct object *obj, int verbose );
74 static struct object_type *mapping_get_type( struct object *obj );
75 static struct fd *mapping_get_fd( struct object *obj );
76 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
77 static void mapping_destroy( struct object *obj );
78 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
80 static const struct object_ops mapping_ops =
82 sizeof(struct mapping), /* size */
83 mapping_dump, /* dump */
84 mapping_get_type, /* get_type */
85 no_add_queue, /* add_queue */
86 NULL, /* remove_queue */
87 NULL, /* signaled */
88 NULL, /* satisfied */
89 no_signal, /* signal */
90 mapping_get_fd, /* get_fd */
91 mapping_map_access, /* map_access */
92 default_get_sd, /* get_sd */
93 default_set_sd, /* set_sd */
94 no_lookup_name, /* lookup_name */
95 directory_link_name, /* link_name */
96 default_unlink_name, /* unlink_name */
97 no_open_file, /* open_file */
98 fd_close_handle, /* close_handle */
99 mapping_destroy /* destroy */
102 static const struct fd_ops mapping_fd_ops =
104 default_fd_get_poll_events, /* get_poll_events */
105 default_poll_event, /* poll_event */
106 mapping_get_fd_type, /* get_fd_type */
107 no_fd_read, /* read */
108 no_fd_write, /* write */
109 no_fd_flush, /* flush */
110 no_fd_ioctl, /* ioctl */
111 no_fd_queue_async, /* queue_async */
112 default_fd_reselect_async, /* reselect_async */
113 default_fd_cancel_async /* cancel_async */
116 static struct list shared_list = LIST_INIT(shared_list);
118 static size_t page_mask;
120 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
123 /* extend a file beyond the current end of file */
124 static int grow_file( int unix_fd, file_pos_t new_size )
126 static const char zero;
127 off_t size = new_size;
129 if (sizeof(new_size) > sizeof(size) && size != new_size)
131 set_error( STATUS_INVALID_PARAMETER );
132 return 0;
134 /* extend the file one byte beyond the requested size and then truncate it */
135 /* this should work around ftruncate implementations that can't extend files */
136 if (pwrite( unix_fd, &zero, 1, size ) != -1)
138 ftruncate( unix_fd, size );
139 return 1;
141 file_set_error();
142 return 0;
145 /* check if the current directory allows exec mappings */
146 static int check_current_dir_for_exec(void)
148 int fd;
149 char tmpfn[] = "anonmap.XXXXXX";
150 void *ret = MAP_FAILED;
152 fd = mkstemps( tmpfn, 0 );
153 if (fd == -1) return 0;
154 if (grow_file( fd, 1 ))
156 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
157 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
159 close( fd );
160 unlink( tmpfn );
161 return (ret != MAP_FAILED);
164 /* create a temp file for anonymous mappings */
165 static int create_temp_file( file_pos_t size )
167 static int temp_dir_fd = -1;
168 char tmpfn[] = "anonmap.XXXXXX";
169 int fd;
171 if (temp_dir_fd == -1)
173 temp_dir_fd = server_dir_fd;
174 if (!check_current_dir_for_exec())
176 /* the server dir is noexec, try the config dir instead */
177 fchdir( config_dir_fd );
178 if (check_current_dir_for_exec())
179 temp_dir_fd = config_dir_fd;
180 else /* neither works, fall back to server dir */
181 fchdir( server_dir_fd );
184 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
186 fd = mkstemps( tmpfn, 0 );
187 if (fd != -1)
189 if (!grow_file( fd, size ))
191 close( fd );
192 fd = -1;
194 unlink( tmpfn );
196 else file_set_error();
198 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
199 return fd;
202 /* find the shared PE mapping for a given mapping */
203 static struct file *get_shared_file( struct mapping *mapping )
205 struct mapping *ptr;
207 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
208 if (is_same_file_fd( ptr->fd, mapping->fd ))
209 return (struct file *)grab_object( ptr->shared_file );
210 return NULL;
213 /* return the size of the memory mapping and file range of a given section */
214 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
215 off_t *file_start, size_t *file_size )
217 static const unsigned int sector_align = 0x1ff;
219 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
220 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
222 *file_start = sec->PointerToRawData & ~sector_align;
223 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
224 if (*file_size > *map_size) *file_size = *map_size;
227 /* add a range to the committed list */
228 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
230 unsigned int i, j;
231 struct range *ranges;
233 if (!mapping->committed) return; /* everything committed already */
235 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
237 if (ranges[i].start > end) break;
238 if (ranges[i].end < start) continue;
239 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
240 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
242 for (j = i + 1; j < mapping->committed->count; j++)
244 if (ranges[j].start > end) break;
245 if (ranges[j].end > end) end = ranges[j].end;
247 if (j > i + 1)
249 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
250 mapping->committed->count -= j - (i + 1);
252 ranges[i].end = end;
254 return;
257 /* now add a new range */
259 if (mapping->committed->count == mapping->committed->max)
261 unsigned int new_size = mapping->committed->max * 2;
262 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
263 if (!new_ptr) return;
264 new_ptr->max = new_size;
265 ranges = new_ptr->ranges;
266 mapping->committed = new_ptr;
268 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
269 ranges[i].start = start;
270 ranges[i].end = end;
271 mapping->committed->count++;
274 /* find the range containing start and return whether it's committed */
275 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
277 unsigned int i;
278 struct range *ranges;
280 if (!mapping->committed) /* everything is committed */
282 *size = mapping->size - start;
283 return 1;
285 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
287 if (ranges[i].start > start)
289 *size = ranges[i].start - start;
290 return 0;
292 if (ranges[i].end > start)
294 *size = ranges[i].end - start;
295 return 1;
298 *size = mapping->size - start;
299 return 0;
302 /* allocate and fill the temp file for a shared PE image mapping */
303 static int build_shared_mapping( struct mapping *mapping, int fd,
304 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
306 unsigned int i;
307 mem_size_t total_size;
308 size_t file_size, map_size, max_size;
309 off_t shared_pos, read_pos, write_pos;
310 char *buffer = NULL;
311 int shared_fd;
312 long toread;
314 /* compute the total size of the shared mapping */
316 total_size = max_size = 0;
317 for (i = 0; i < nb_sec; i++)
319 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
320 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
322 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
323 if (file_size > max_size) max_size = file_size;
324 total_size += map_size;
327 if (!total_size) return 1; /* nothing to do */
329 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
331 /* create a temp file for the mapping */
333 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
334 if (!(mapping->shared_file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 )))
335 return 0;
337 if (!(buffer = malloc( max_size ))) goto error;
339 /* copy the shared sections data into the temp file */
341 shared_pos = 0;
342 for (i = 0; i < nb_sec; i++)
344 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
345 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
346 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
347 write_pos = shared_pos;
348 shared_pos += map_size;
349 if (!sec[i].PointerToRawData || !file_size) continue;
350 toread = file_size;
351 while (toread)
353 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
354 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
356 file_size -= toread;
357 break;
359 if (res <= 0) goto error;
360 toread -= res;
361 read_pos += res;
363 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
365 free( buffer );
366 return 1;
368 error:
369 release_object( mapping->shared_file );
370 mapping->shared_file = NULL;
371 free( buffer );
372 return 0;
375 /* retrieve the mapping parameters for an executable (PE) image */
376 static unsigned int get_image_params( struct mapping *mapping, int unix_fd )
378 IMAGE_DOS_HEADER dos;
379 IMAGE_SECTION_HEADER *sec = NULL;
380 struct
382 DWORD Signature;
383 IMAGE_FILE_HEADER FileHeader;
384 union
386 IMAGE_OPTIONAL_HEADER32 hdr32;
387 IMAGE_OPTIONAL_HEADER64 hdr64;
388 } opt;
389 } nt;
390 off_t pos;
391 int size;
393 /* load the headers */
395 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
396 if (dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
397 pos = dos.e_lfanew;
399 size = pread( unix_fd, &nt, sizeof(nt), pos );
400 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
401 /* zero out Optional header in the case it's not present or partial */
402 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
403 if (nt.Signature != IMAGE_NT_SIGNATURE)
405 if (*(WORD *)&nt.Signature == IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_NE_FORMAT;
406 return STATUS_INVALID_IMAGE_PROTECT;
409 mapping->cpu = current->process->cpu;
410 switch (mapping->cpu)
412 case CPU_x86:
413 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) return STATUS_INVALID_IMAGE_FORMAT;
414 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
415 break;
416 case CPU_x86_64:
417 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) return STATUS_INVALID_IMAGE_FORMAT;
418 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
419 break;
420 case CPU_POWERPC:
421 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_POWERPC) return STATUS_INVALID_IMAGE_FORMAT;
422 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
423 break;
424 case CPU_ARM:
425 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM &&
426 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_THUMB &&
427 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARMNT) return STATUS_INVALID_IMAGE_FORMAT;
428 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
429 break;
430 case CPU_ARM64:
431 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM64) return STATUS_INVALID_IMAGE_FORMAT;
432 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
433 break;
434 default:
435 return STATUS_INVALID_IMAGE_FORMAT;
438 switch (nt.opt.hdr32.Magic)
440 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
441 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
442 mapping->base = nt.opt.hdr32.ImageBase;
443 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
444 break;
445 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
446 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
447 mapping->base = nt.opt.hdr64.ImageBase;
448 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
449 break;
452 /* load the section headers */
454 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
455 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
456 if (pos + size > mapping->size) goto error;
457 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
458 if (!(sec = malloc( size ))) goto error;
459 if (pread( unix_fd, sec, size, pos ) != size) goto error;
461 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
463 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
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, unsigned int flags, 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->flags = flags & (SEC_IMAGE | SEC_NOCACHE | SEC_WRITECOMBINE | SEC_LARGE_PAGES);
494 mapping->protect = protect;
495 mapping->fd = NULL;
496 mapping->shared_file = NULL;
497 mapping->committed = NULL;
499 if (protect & VPROT_READ) access |= FILE_READ_DATA;
500 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
502 if (handle)
504 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
505 unsigned int mapping_access = FILE_MAPPING_ACCESS;
507 if (flags & SEC_RESERVE)
509 set_error( STATUS_INVALID_PARAMETER );
510 goto error;
512 if (!(file = get_file_obj( current->process, handle, access ))) goto error;
513 fd = get_obj_fd( (struct object *)file );
515 /* file sharing rules for mappings are different so we use magic the access rights */
516 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
517 else if (protect & VPROT_WRITE) mapping_access |= FILE_MAPPING_WRITE;
519 mapping->flags |= SEC_FILE;
520 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
522 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
523 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
525 release_object( file );
526 release_object( fd );
527 if (!mapping->fd) goto error;
529 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
530 if (flags & SEC_IMAGE)
532 unsigned int err = get_image_params( mapping, unix_fd );
533 if (!err) return &mapping->obj;
534 set_error( err );
535 goto error;
537 if (fstat( unix_fd, &st ) == -1)
539 file_set_error();
540 goto error;
542 if (!size)
544 if (!(size = st.st_size))
546 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
547 goto error;
550 else if (st.st_size < size && !grow_file( unix_fd, size )) goto error;
552 else /* Anonymous mapping (no associated file) */
554 if (!size || (flags & SEC_IMAGE))
556 set_error( STATUS_INVALID_PARAMETER );
557 goto error;
559 mapping->flags |= flags & (SEC_COMMIT | SEC_RESERVE);
560 if (flags & SEC_RESERVE)
562 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
563 mapping->committed->count = 0;
564 mapping->committed->max = 8;
566 if ((unix_fd = create_temp_file( size )) == -1) goto error;
567 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
568 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
569 allow_fd_caching( mapping->fd );
571 mapping->size = (size + page_mask) & ~((mem_size_t)page_mask);
572 return &mapping->obj;
574 error:
575 release_object( mapping );
576 return NULL;
579 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
581 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
584 /* open a new file handle to the file backing the mapping */
585 obj_handle_t open_mapping_file( struct process *process, struct mapping *mapping,
586 unsigned int access, unsigned int sharing )
588 obj_handle_t handle;
589 struct file *file = create_file_for_fd_obj( mapping->fd, access, sharing );
591 if (!file) return 0;
592 handle = alloc_handle( process, file, access, 0 );
593 release_object( file );
594 return handle;
597 struct mapping *grab_mapping_unless_removable( struct mapping *mapping )
599 if (is_fd_removable( mapping->fd )) return NULL;
600 return (struct mapping *)grab_object( mapping );
603 static void mapping_dump( struct object *obj, int verbose )
605 struct mapping *mapping = (struct mapping *)obj;
606 assert( obj->ops == &mapping_ops );
607 fprintf( stderr, "Mapping size=%08x%08x flags=%08x prot=%08x fd=%p header_size=%08x base=%08lx "
608 "shared_file=%p\n",
609 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
610 mapping->flags, mapping->protect, mapping->fd, mapping->header_size,
611 (unsigned long)mapping->base, mapping->shared_file );
614 static struct object_type *mapping_get_type( struct object *obj )
616 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
617 static const struct unicode_str str = { name, sizeof(name) };
618 return get_object_type( &str );
621 static struct fd *mapping_get_fd( struct object *obj )
623 struct mapping *mapping = (struct mapping *)obj;
624 return (struct fd *)grab_object( mapping->fd );
627 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
629 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
630 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
631 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
632 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
633 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
636 static void mapping_destroy( struct object *obj )
638 struct mapping *mapping = (struct mapping *)obj;
639 assert( obj->ops == &mapping_ops );
640 if (mapping->fd) release_object( mapping->fd );
641 if (mapping->shared_file)
643 release_object( mapping->shared_file );
644 list_remove( &mapping->shared_entry );
646 free( mapping->committed );
649 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
651 return FD_TYPE_FILE;
654 int get_page_size(void)
656 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
657 return page_mask + 1;
660 /* create a file mapping */
661 DECL_HANDLER(create_mapping)
663 struct object *root, *obj;
664 struct unicode_str name;
665 const struct security_descriptor *sd;
666 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
668 if (!objattr) return;
670 if ((obj = create_mapping( root, &name, objattr->attributes,
671 req->size, req->flags, req->protect, req->file_handle, sd )))
673 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
674 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
675 else
676 reply->handle = alloc_handle_no_access_check( current->process, obj,
677 req->access, objattr->attributes );
678 release_object( obj );
681 if (root) release_object( root );
684 /* open a handle to a mapping */
685 DECL_HANDLER(open_mapping)
687 struct unicode_str name = get_req_unicode_str();
689 reply->handle = open_object( current->process, req->rootdir, req->access,
690 &mapping_ops, &name, req->attributes );
693 /* get a mapping information */
694 DECL_HANDLER(get_mapping_info)
696 struct mapping *mapping;
697 struct fd *fd;
699 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
701 reply->size = mapping->size;
702 reply->flags = mapping->flags;
703 reply->protect = mapping->protect;
704 reply->header_size = mapping->header_size;
705 reply->base = mapping->base;
707 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
709 release_object( mapping );
710 return;
713 if ((mapping->flags & SEC_IMAGE) && mapping->cpu != current->process->cpu)
715 set_error( STATUS_INVALID_IMAGE_FORMAT );
716 release_object( mapping );
717 return;
720 if ((fd = get_obj_fd( &mapping->obj )))
722 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
723 release_object( fd );
725 if (mapping->shared_file)
727 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
728 GENERIC_READ|GENERIC_WRITE, 0 )))
730 if (reply->mapping) close_handle( current->process, reply->mapping );
733 release_object( mapping );
736 /* get a range of committed pages in a file mapping */
737 DECL_HANDLER(get_mapping_committed_range)
739 struct mapping *mapping;
741 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
743 if (!(req->offset & page_mask) && req->offset < mapping->size)
744 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
745 else
746 set_error( STATUS_INVALID_PARAMETER );
748 release_object( mapping );
752 /* add a range to the committed pages in a file mapping */
753 DECL_HANDLER(add_mapping_committed_range)
755 struct mapping *mapping;
757 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
759 if (!(req->size & page_mask) &&
760 !(req->offset & page_mask) &&
761 req->offset < mapping->size &&
762 req->size > 0 &&
763 req->size <= mapping->size - req->offset)
764 add_committed_range( mapping, req->offset, req->offset + req->size );
765 else
766 set_error( STATUS_INVALID_PARAMETER );
768 release_object( mapping );