wbemdisp: Simplify and standardize the heap_xxx() declarations.
[wine.git] / server / mapping.c
blobf03ea7a6f6314bc91e187bfff649cd765cb381e9
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 pe_image_info_t image; /* image info (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 */
114 static struct list shared_list = LIST_INIT(shared_list);
116 static size_t page_mask;
118 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
121 /* extend a file beyond the current end of file */
122 static int grow_file( int unix_fd, file_pos_t new_size )
124 static const char zero;
125 off_t size = new_size;
127 if (sizeof(new_size) > sizeof(size) && size != new_size)
129 set_error( STATUS_INVALID_PARAMETER );
130 return 0;
132 /* extend the file one byte beyond the requested size and then truncate it */
133 /* this should work around ftruncate implementations that can't extend files */
134 if (pwrite( unix_fd, &zero, 1, size ) != -1)
136 ftruncate( unix_fd, size );
137 return 1;
139 file_set_error();
140 return 0;
143 /* check if the current directory allows exec mappings */
144 static int check_current_dir_for_exec(void)
146 int fd;
147 char tmpfn[] = "anonmap.XXXXXX";
148 void *ret = MAP_FAILED;
150 fd = mkstemps( tmpfn, 0 );
151 if (fd == -1) return 0;
152 if (grow_file( fd, 1 ))
154 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
155 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
157 close( fd );
158 unlink( tmpfn );
159 return (ret != MAP_FAILED);
162 /* create a temp file for anonymous mappings */
163 static int create_temp_file( file_pos_t size )
165 static int temp_dir_fd = -1;
166 char tmpfn[] = "anonmap.XXXXXX";
167 int fd;
169 if (temp_dir_fd == -1)
171 temp_dir_fd = server_dir_fd;
172 if (!check_current_dir_for_exec())
174 /* the server dir is noexec, try the config dir instead */
175 fchdir( config_dir_fd );
176 if (check_current_dir_for_exec())
177 temp_dir_fd = config_dir_fd;
178 else /* neither works, fall back to server dir */
179 fchdir( server_dir_fd );
182 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
184 fd = mkstemps( tmpfn, 0 );
185 if (fd != -1)
187 if (!grow_file( fd, size ))
189 close( fd );
190 fd = -1;
192 unlink( tmpfn );
194 else file_set_error();
196 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
197 return fd;
200 /* find the shared PE mapping for a given mapping */
201 static struct file *get_shared_file( struct mapping *mapping )
203 struct mapping *ptr;
205 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
206 if (is_same_file_fd( ptr->fd, mapping->fd ))
207 return (struct file *)grab_object( ptr->shared_file );
208 return NULL;
211 /* return the size of the memory mapping and file range of a given section */
212 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
213 off_t *file_start, size_t *file_size )
215 static const unsigned int sector_align = 0x1ff;
217 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
218 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
220 *file_start = sec->PointerToRawData & ~sector_align;
221 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
222 if (*file_size > *map_size) *file_size = *map_size;
225 /* add a range to the committed list */
226 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
228 unsigned int i, j;
229 struct range *ranges;
231 if (!mapping->committed) return; /* everything committed already */
233 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
235 if (ranges[i].start > end) break;
236 if (ranges[i].end < start) continue;
237 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
238 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
240 for (j = i + 1; j < mapping->committed->count; j++)
242 if (ranges[j].start > end) break;
243 if (ranges[j].end > end) end = ranges[j].end;
245 if (j > i + 1)
247 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
248 mapping->committed->count -= j - (i + 1);
250 ranges[i].end = end;
252 return;
255 /* now add a new range */
257 if (mapping->committed->count == mapping->committed->max)
259 unsigned int new_size = mapping->committed->max * 2;
260 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
261 if (!new_ptr) return;
262 new_ptr->max = new_size;
263 ranges = new_ptr->ranges;
264 mapping->committed = new_ptr;
266 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
267 ranges[i].start = start;
268 ranges[i].end = end;
269 mapping->committed->count++;
272 /* find the range containing start and return whether it's committed */
273 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
275 unsigned int i;
276 struct range *ranges;
278 if (!mapping->committed) /* everything is committed */
280 *size = mapping->size - start;
281 return 1;
283 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
285 if (ranges[i].start > start)
287 *size = ranges[i].start - start;
288 return 0;
290 if (ranges[i].end > start)
292 *size = ranges[i].end - start;
293 return 1;
296 *size = mapping->size - start;
297 return 0;
300 /* allocate and fill the temp file for a shared PE image mapping */
301 static int build_shared_mapping( struct mapping *mapping, int fd,
302 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
304 unsigned int i;
305 mem_size_t total_size;
306 size_t file_size, map_size, max_size;
307 off_t shared_pos, read_pos, write_pos;
308 char *buffer = NULL;
309 int shared_fd;
310 long toread;
312 /* compute the total size of the shared mapping */
314 total_size = max_size = 0;
315 for (i = 0; i < nb_sec; i++)
317 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
318 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
320 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
321 if (file_size > max_size) max_size = file_size;
322 total_size += map_size;
325 if (!total_size) return 1; /* nothing to do */
327 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
329 /* create a temp file for the mapping */
331 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
332 if (!(mapping->shared_file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 )))
333 return 0;
335 if (!(buffer = malloc( max_size ))) goto error;
337 /* copy the shared sections data into the temp file */
339 shared_pos = 0;
340 for (i = 0; i < nb_sec; i++)
342 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
343 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
344 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
345 write_pos = shared_pos;
346 shared_pos += map_size;
347 if (!sec[i].PointerToRawData || !file_size) continue;
348 toread = file_size;
349 while (toread)
351 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
352 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
354 file_size -= toread;
355 break;
357 if (res <= 0) goto error;
358 toread -= res;
359 read_pos += res;
361 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
363 free( buffer );
364 return 1;
366 error:
367 release_object( mapping->shared_file );
368 mapping->shared_file = NULL;
369 free( buffer );
370 return 0;
373 /* retrieve the mapping parameters for an executable (PE) image */
374 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
376 IMAGE_DOS_HEADER dos;
377 IMAGE_SECTION_HEADER *sec = NULL;
378 struct
380 DWORD Signature;
381 IMAGE_FILE_HEADER FileHeader;
382 union
384 IMAGE_OPTIONAL_HEADER32 hdr32;
385 IMAGE_OPTIONAL_HEADER64 hdr64;
386 } opt;
387 } nt;
388 off_t pos;
389 int size;
391 /* load the headers */
393 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
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 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader );
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->image.base = nt.opt.hdr32.ImageBase;
442 mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
443 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
444 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
445 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
446 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
447 mapping->image.subsystem_low = nt.opt.hdr32.MinorSubsystemVersion;
448 mapping->image.subsystem_high = nt.opt.hdr32.MajorSubsystemVersion;
449 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
450 mapping->image.loader_flags = nt.opt.hdr32.LoaderFlags;
451 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
452 mapping->image.checksum = nt.opt.hdr32.CheckSum;
453 break;
454 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
455 mapping->image.base = nt.opt.hdr64.ImageBase;
456 mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
457 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
458 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
459 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
460 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
461 mapping->image.subsystem_low = nt.opt.hdr64.MinorSubsystemVersion;
462 mapping->image.subsystem_high = nt.opt.hdr64.MajorSubsystemVersion;
463 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
464 mapping->image.loader_flags = nt.opt.hdr64.LoaderFlags;
465 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
466 mapping->image.checksum = nt.opt.hdr64.CheckSum;
467 break;
469 mapping->image.image_charact = nt.FileHeader.Characteristics;
470 mapping->image.machine = nt.FileHeader.Machine;
471 mapping->image.zerobits = 0; /* FIXME */
472 mapping->image.gp = 0; /* FIXME */
473 mapping->image.contains_code = 0; /* FIXME */
474 mapping->image.image_flags = 0; /* FIXME */
475 mapping->image.file_size = file_size;
477 /* load the section headers */
479 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
480 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
481 if (!mapping->size) mapping->size = mapping->image.map_size;
482 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
483 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
484 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
485 if (!(sec = malloc( size ))) goto error;
486 if (pread( unix_fd, sec, size, pos ) != size) goto error;
488 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
490 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
492 free( sec );
493 return 0;
495 error:
496 free( sec );
497 return STATUS_INVALID_FILE_FOR_SECTION;
500 static struct object *create_mapping( struct object *root, const struct unicode_str *name,
501 unsigned int attr, mem_size_t size, unsigned int flags, int protect,
502 obj_handle_t handle, const struct security_descriptor *sd )
504 struct mapping *mapping;
505 struct file *file;
506 struct fd *fd;
507 int access = 0;
508 int unix_fd;
509 struct stat st;
511 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
513 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
514 return NULL;
515 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
516 return &mapping->obj; /* Nothing else to do */
518 mapping->size = size;
519 mapping->flags = flags & (SEC_IMAGE | SEC_NOCACHE | SEC_WRITECOMBINE | SEC_LARGE_PAGES);
520 mapping->protect = protect;
521 mapping->fd = NULL;
522 mapping->shared_file = NULL;
523 mapping->committed = NULL;
525 if (protect & VPROT_READ) access |= FILE_READ_DATA;
526 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
528 if (handle)
530 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
531 unsigned int mapping_access = FILE_MAPPING_ACCESS;
533 if (flags & SEC_RESERVE)
535 set_error( STATUS_INVALID_PARAMETER );
536 goto error;
538 if (!(file = get_file_obj( current->process, handle, access ))) goto error;
539 fd = get_obj_fd( (struct object *)file );
541 /* file sharing rules for mappings are different so we use magic the access rights */
542 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
543 else if (protect & VPROT_WRITE) mapping_access |= FILE_MAPPING_WRITE;
545 mapping->flags |= SEC_FILE;
546 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
548 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
549 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
551 release_object( file );
552 release_object( fd );
553 if (!mapping->fd) goto error;
555 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
556 if (fstat( unix_fd, &st ) == -1)
558 file_set_error();
559 goto error;
561 if (flags & SEC_IMAGE)
563 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
564 if (!err) return &mapping->obj;
565 set_error( err );
566 goto error;
568 if (!mapping->size)
570 if (!(mapping->size = st.st_size))
572 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
573 goto error;
576 else if (st.st_size < mapping->size)
578 if (!(access & FILE_WRITE_DATA))
580 set_error( STATUS_SECTION_TOO_BIG );
581 goto error;
583 if (!grow_file( unix_fd, mapping->size )) goto error;
586 else /* Anonymous mapping (no associated file) */
588 if (!mapping->size || (flags & SEC_IMAGE))
590 set_error( STATUS_INVALID_PARAMETER );
591 goto error;
593 mapping->flags |= flags & (SEC_COMMIT | SEC_RESERVE);
594 if (flags & SEC_RESERVE)
596 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
597 mapping->committed->count = 0;
598 mapping->committed->max = 8;
600 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
601 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
602 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
603 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
604 allow_fd_caching( mapping->fd );
606 return &mapping->obj;
608 error:
609 release_object( mapping );
610 return NULL;
613 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
615 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
618 /* open a new file handle to the file backing the mapping */
619 obj_handle_t open_mapping_file( struct process *process, struct mapping *mapping,
620 unsigned int access, unsigned int sharing )
622 obj_handle_t handle;
623 struct file *file = create_file_for_fd_obj( mapping->fd, access, sharing );
625 if (!file) return 0;
626 handle = alloc_handle( process, file, access, 0 );
627 release_object( file );
628 return handle;
631 struct mapping *grab_mapping_unless_removable( struct mapping *mapping )
633 if (is_fd_removable( mapping->fd )) return NULL;
634 return (struct mapping *)grab_object( mapping );
637 static void mapping_dump( struct object *obj, int verbose )
639 struct mapping *mapping = (struct mapping *)obj;
640 assert( obj->ops == &mapping_ops );
641 fprintf( stderr, "Mapping size=%08x%08x flags=%08x prot=%08x fd=%p shared_file=%p\n",
642 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
643 mapping->flags, mapping->protect, mapping->fd, mapping->shared_file );
646 static struct object_type *mapping_get_type( struct object *obj )
648 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
649 static const struct unicode_str str = { name, sizeof(name) };
650 return get_object_type( &str );
653 static struct fd *mapping_get_fd( struct object *obj )
655 struct mapping *mapping = (struct mapping *)obj;
656 return (struct fd *)grab_object( mapping->fd );
659 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
661 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
662 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
663 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
664 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
665 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
668 static void mapping_destroy( struct object *obj )
670 struct mapping *mapping = (struct mapping *)obj;
671 assert( obj->ops == &mapping_ops );
672 if (mapping->fd) release_object( mapping->fd );
673 if (mapping->shared_file)
675 release_object( mapping->shared_file );
676 list_remove( &mapping->shared_entry );
678 free( mapping->committed );
681 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
683 return FD_TYPE_FILE;
686 int get_page_size(void)
688 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
689 return page_mask + 1;
692 /* create a file mapping */
693 DECL_HANDLER(create_mapping)
695 struct object *root, *obj;
696 struct unicode_str name;
697 const struct security_descriptor *sd;
698 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
700 if (!objattr) return;
702 if ((obj = create_mapping( root, &name, objattr->attributes,
703 req->size, req->flags, req->protect, req->file_handle, sd )))
705 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
706 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
707 else
708 reply->handle = alloc_handle_no_access_check( current->process, obj,
709 req->access, objattr->attributes );
710 release_object( obj );
713 if (root) release_object( root );
716 /* open a handle to a mapping */
717 DECL_HANDLER(open_mapping)
719 struct unicode_str name = get_req_unicode_str();
721 reply->handle = open_object( current->process, req->rootdir, req->access,
722 &mapping_ops, &name, req->attributes );
725 /* get a mapping information */
726 DECL_HANDLER(get_mapping_info)
728 struct mapping *mapping;
729 struct fd *fd;
731 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
733 reply->size = mapping->size;
734 reply->flags = mapping->flags;
735 reply->protect = mapping->protect;
737 if (mapping->flags & SEC_IMAGE)
738 set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
740 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
742 release_object( mapping );
743 return;
746 if ((mapping->flags & SEC_IMAGE) && mapping->cpu != current->process->cpu)
748 set_error( STATUS_INVALID_IMAGE_FORMAT );
749 release_object( mapping );
750 return;
753 if ((fd = get_obj_fd( &mapping->obj )))
755 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
756 release_object( fd );
758 if (mapping->shared_file)
760 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
761 GENERIC_READ|GENERIC_WRITE, 0 )))
763 if (reply->mapping) close_handle( current->process, reply->mapping );
766 release_object( mapping );
769 /* get a range of committed pages in a file mapping */
770 DECL_HANDLER(get_mapping_committed_range)
772 struct mapping *mapping;
774 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
776 if (!(req->offset & page_mask) && req->offset < mapping->size)
777 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
778 else
779 set_error( STATUS_INVALID_PARAMETER );
781 release_object( mapping );
785 /* add a range to the committed pages in a file mapping */
786 DECL_HANDLER(add_mapping_committed_range)
788 struct mapping *mapping;
790 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
792 if (!(req->size & page_mask) &&
793 !(req->offset & page_mask) &&
794 req->offset < mapping->size &&
795 req->size > 0 &&
796 req->size <= mapping->size - req->offset)
797 add_committed_range( mapping, req->offset, req->offset + req->size );
798 else
799 set_error( STATUS_INVALID_PARAMETER );
801 release_object( mapping );