mshtml: Fixed getElementsByTagName for comment elements.
[wine.git] / server / mapping.c
blob35381d7fe92caa454207c158671fb54d03159e86
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 struct fd *fd; /* fd for mapped file */
64 enum cpu_type cpu; /* client CPU (for PE image mapping) */
65 pe_image_info_t image; /* image info (for PE image mapping) */
66 struct ranges *committed; /* list of committed ranges in this mapping */
67 struct file *shared_file; /* temp file for shared PE mapping */
68 struct list shared_entry; /* entry in global shared PE mappings list */
71 static void mapping_dump( struct object *obj, int verbose );
72 static struct object_type *mapping_get_type( struct object *obj );
73 static struct fd *mapping_get_fd( struct object *obj );
74 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
75 static void mapping_destroy( struct object *obj );
76 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
78 static const struct object_ops mapping_ops =
80 sizeof(struct mapping), /* size */
81 mapping_dump, /* dump */
82 mapping_get_type, /* get_type */
83 no_add_queue, /* add_queue */
84 NULL, /* remove_queue */
85 NULL, /* signaled */
86 NULL, /* satisfied */
87 no_signal, /* signal */
88 mapping_get_fd, /* get_fd */
89 mapping_map_access, /* map_access */
90 default_get_sd, /* get_sd */
91 default_set_sd, /* set_sd */
92 no_lookup_name, /* lookup_name */
93 directory_link_name, /* link_name */
94 default_unlink_name, /* unlink_name */
95 no_open_file, /* open_file */
96 fd_close_handle, /* close_handle */
97 mapping_destroy /* destroy */
100 static const struct fd_ops mapping_fd_ops =
102 default_fd_get_poll_events, /* get_poll_events */
103 default_poll_event, /* poll_event */
104 mapping_get_fd_type, /* get_fd_type */
105 no_fd_read, /* read */
106 no_fd_write, /* write */
107 no_fd_flush, /* flush */
108 no_fd_ioctl, /* ioctl */
109 no_fd_queue_async, /* queue_async */
110 default_fd_reselect_async /* reselect_async */
113 static struct list shared_list = LIST_INIT(shared_list);
115 static size_t page_mask;
117 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
120 /* extend a file beyond the current end of file */
121 static int grow_file( int unix_fd, file_pos_t new_size )
123 static const char zero;
124 off_t size = new_size;
126 if (sizeof(new_size) > sizeof(size) && size != new_size)
128 set_error( STATUS_INVALID_PARAMETER );
129 return 0;
131 /* extend the file one byte beyond the requested size and then truncate it */
132 /* this should work around ftruncate implementations that can't extend files */
133 if (pwrite( unix_fd, &zero, 1, size ) != -1)
135 ftruncate( unix_fd, size );
136 return 1;
138 file_set_error();
139 return 0;
142 /* check if the current directory allows exec mappings */
143 static int check_current_dir_for_exec(void)
145 int fd;
146 char tmpfn[] = "anonmap.XXXXXX";
147 void *ret = MAP_FAILED;
149 fd = mkstemps( tmpfn, 0 );
150 if (fd == -1) return 0;
151 if (grow_file( fd, 1 ))
153 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
154 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
156 close( fd );
157 unlink( tmpfn );
158 return (ret != MAP_FAILED);
161 /* create a temp file for anonymous mappings */
162 static int create_temp_file( file_pos_t size )
164 static int temp_dir_fd = -1;
165 char tmpfn[] = "anonmap.XXXXXX";
166 int fd;
168 if (temp_dir_fd == -1)
170 temp_dir_fd = server_dir_fd;
171 if (!check_current_dir_for_exec())
173 /* the server dir is noexec, try the config dir instead */
174 fchdir( config_dir_fd );
175 if (check_current_dir_for_exec())
176 temp_dir_fd = config_dir_fd;
177 else /* neither works, fall back to server dir */
178 fchdir( server_dir_fd );
181 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
183 fd = mkstemps( tmpfn, 0 );
184 if (fd != -1)
186 if (!grow_file( fd, size ))
188 close( fd );
189 fd = -1;
191 unlink( tmpfn );
193 else file_set_error();
195 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
196 return fd;
199 /* find the shared PE mapping for a given mapping */
200 static struct file *get_shared_file( struct mapping *mapping )
202 struct mapping *ptr;
204 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
205 if (is_same_file_fd( ptr->fd, mapping->fd ))
206 return (struct file *)grab_object( ptr->shared_file );
207 return NULL;
210 /* return the size of the memory mapping and file range of a given section */
211 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
212 off_t *file_start, size_t *file_size )
214 static const unsigned int sector_align = 0x1ff;
216 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
217 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
219 *file_start = sec->PointerToRawData & ~sector_align;
220 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
221 if (*file_size > *map_size) *file_size = *map_size;
224 /* add a range to the committed list */
225 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
227 unsigned int i, j;
228 struct range *ranges;
230 if (!mapping->committed) return; /* everything committed already */
232 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
234 if (ranges[i].start > end) break;
235 if (ranges[i].end < start) continue;
236 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
237 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
239 for (j = i + 1; j < mapping->committed->count; j++)
241 if (ranges[j].start > end) break;
242 if (ranges[j].end > end) end = ranges[j].end;
244 if (j > i + 1)
246 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
247 mapping->committed->count -= j - (i + 1);
249 ranges[i].end = end;
251 return;
254 /* now add a new range */
256 if (mapping->committed->count == mapping->committed->max)
258 unsigned int new_size = mapping->committed->max * 2;
259 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
260 if (!new_ptr) return;
261 new_ptr->max = new_size;
262 ranges = new_ptr->ranges;
263 mapping->committed = new_ptr;
265 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
266 ranges[i].start = start;
267 ranges[i].end = end;
268 mapping->committed->count++;
271 /* find the range containing start and return whether it's committed */
272 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
274 unsigned int i;
275 struct range *ranges;
277 if (!mapping->committed) /* everything is committed */
279 *size = mapping->size - start;
280 return 1;
282 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
284 if (ranges[i].start > start)
286 *size = ranges[i].start - start;
287 return 0;
289 if (ranges[i].end > start)
291 *size = ranges[i].end - start;
292 return 1;
295 *size = mapping->size - start;
296 return 0;
299 /* allocate and fill the temp file for a shared PE image mapping */
300 static int build_shared_mapping( struct mapping *mapping, int fd,
301 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
303 unsigned int i;
304 mem_size_t total_size;
305 size_t file_size, map_size, max_size;
306 off_t shared_pos, read_pos, write_pos;
307 char *buffer = NULL;
308 int shared_fd;
309 long toread;
311 /* compute the total size of the shared mapping */
313 total_size = max_size = 0;
314 for (i = 0; i < nb_sec; i++)
316 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
317 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
319 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
320 if (file_size > max_size) max_size = file_size;
321 total_size += map_size;
324 if (!total_size) return 1; /* nothing to do */
326 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
328 /* create a temp file for the mapping */
330 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
331 if (!(mapping->shared_file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 )))
332 return 0;
334 if (!(buffer = malloc( max_size ))) goto error;
336 /* copy the shared sections data into the temp file */
338 shared_pos = 0;
339 for (i = 0; i < nb_sec; i++)
341 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
342 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
343 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
344 write_pos = shared_pos;
345 shared_pos += map_size;
346 if (!sec[i].PointerToRawData || !file_size) continue;
347 toread = file_size;
348 while (toread)
350 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
351 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
353 file_size -= toread;
354 break;
356 if (res <= 0) goto error;
357 toread -= res;
358 read_pos += res;
360 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
362 free( buffer );
363 return 1;
365 error:
366 release_object( mapping->shared_file );
367 mapping->shared_file = NULL;
368 free( buffer );
369 return 0;
372 /* retrieve the mapping parameters for an executable (PE) image */
373 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
375 IMAGE_DOS_HEADER dos;
376 IMAGE_SECTION_HEADER *sec = NULL;
377 struct
379 DWORD Signature;
380 IMAGE_FILE_HEADER FileHeader;
381 union
383 IMAGE_OPTIONAL_HEADER32 hdr32;
384 IMAGE_OPTIONAL_HEADER64 hdr64;
385 } opt;
386 } nt;
387 off_t pos;
388 int size;
390 /* load the headers */
392 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
393 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
394 if (dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
395 pos = dos.e_lfanew;
397 size = pread( unix_fd, &nt, sizeof(nt), pos );
398 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
399 /* zero out Optional header in the case it's not present or partial */
400 size = min( size, sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader );
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->image.base = nt.opt.hdr32.ImageBase;
441 mapping->image.entry_point = nt.opt.hdr32.ImageBase + nt.opt.hdr32.AddressOfEntryPoint;
442 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
443 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
444 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
445 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
446 mapping->image.subsystem_low = nt.opt.hdr32.MinorSubsystemVersion;
447 mapping->image.subsystem_high = nt.opt.hdr32.MajorSubsystemVersion;
448 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
449 mapping->image.loader_flags = nt.opt.hdr32.LoaderFlags;
450 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
451 mapping->image.checksum = nt.opt.hdr32.CheckSum;
452 break;
453 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
454 mapping->image.base = nt.opt.hdr64.ImageBase;
455 mapping->image.entry_point = nt.opt.hdr64.ImageBase + nt.opt.hdr64.AddressOfEntryPoint;
456 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
457 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
458 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
459 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
460 mapping->image.subsystem_low = nt.opt.hdr64.MinorSubsystemVersion;
461 mapping->image.subsystem_high = nt.opt.hdr64.MajorSubsystemVersion;
462 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
463 mapping->image.loader_flags = nt.opt.hdr64.LoaderFlags;
464 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
465 mapping->image.checksum = nt.opt.hdr64.CheckSum;
466 break;
468 mapping->image.image_charact = nt.FileHeader.Characteristics;
469 mapping->image.machine = nt.FileHeader.Machine;
470 mapping->image.zerobits = 0; /* FIXME */
471 mapping->image.gp = 0; /* FIXME */
472 mapping->image.contains_code = 0; /* FIXME */
473 mapping->image.image_flags = 0; /* FIXME */
474 mapping->image.file_size = file_size;
476 /* load the section headers */
478 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
479 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
480 if (!mapping->size) mapping->size = mapping->image.map_size;
481 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
482 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
483 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
484 if (!(sec = malloc( size ))) goto error;
485 if (pread( unix_fd, sec, size, pos ) != size) goto error;
487 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
489 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
491 free( sec );
492 return 0;
494 error:
495 free( sec );
496 return STATUS_INVALID_FILE_FOR_SECTION;
499 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
501 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
503 case SEC_IMAGE:
504 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
505 if (handle) return SEC_FILE | SEC_IMAGE;
506 set_error( STATUS_INVALID_FILE_FOR_SECTION );
507 return 0;
508 case SEC_COMMIT:
509 if (!handle) return flags;
510 /* fall through */
511 case SEC_RESERVE:
512 if (flags & SEC_LARGE_PAGES) break;
513 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
514 return flags;
516 set_error( STATUS_INVALID_PARAMETER );
517 return 0;
521 static struct object *create_mapping( struct object *root, const struct unicode_str *name,
522 unsigned int attr, mem_size_t size, unsigned int flags,
523 obj_handle_t handle, unsigned int file_access,
524 const struct security_descriptor *sd )
526 struct mapping *mapping;
527 struct file *file;
528 struct fd *fd;
529 int unix_fd;
530 struct stat st;
532 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
534 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
535 return NULL;
536 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
537 return &mapping->obj; /* Nothing else to do */
539 mapping->size = size;
540 mapping->fd = NULL;
541 mapping->shared_file = NULL;
542 mapping->committed = NULL;
544 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
546 if (handle)
548 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
549 unsigned int mapping_access = FILE_MAPPING_ACCESS;
551 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
552 fd = get_obj_fd( (struct object *)file );
554 /* file sharing rules for mappings are different so we use magic the access rights */
555 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
556 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
558 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
560 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
561 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
563 release_object( file );
564 release_object( fd );
565 if (!mapping->fd) goto error;
567 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
568 if (fstat( unix_fd, &st ) == -1)
570 file_set_error();
571 goto error;
573 if (flags & SEC_IMAGE)
575 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
576 if (!err) return &mapping->obj;
577 set_error( err );
578 goto error;
580 if (!mapping->size)
582 if (!(mapping->size = st.st_size))
584 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
585 goto error;
588 else if (st.st_size < mapping->size)
590 if (!(file_access & FILE_WRITE_DATA))
592 set_error( STATUS_SECTION_TOO_BIG );
593 goto error;
595 if (!grow_file( unix_fd, mapping->size )) goto error;
598 else /* Anonymous mapping (no associated file) */
600 if (!mapping->size)
602 set_error( STATUS_INVALID_PARAMETER );
603 goto error;
605 if (flags & SEC_RESERVE)
607 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
608 mapping->committed->count = 0;
609 mapping->committed->max = 8;
611 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
612 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
613 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
614 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
615 allow_fd_caching( mapping->fd );
617 return &mapping->obj;
619 error:
620 release_object( mapping );
621 return NULL;
624 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
626 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
629 /* open a new file handle to the file backing the mapping */
630 obj_handle_t open_mapping_file( struct process *process, struct mapping *mapping,
631 unsigned int access, unsigned int sharing )
633 obj_handle_t handle;
634 struct file *file = create_file_for_fd_obj( mapping->fd, access, sharing );
636 if (!file) return 0;
637 handle = alloc_handle( process, file, access, 0 );
638 release_object( file );
639 return handle;
642 struct mapping *grab_mapping_unless_removable( struct mapping *mapping )
644 if (is_fd_removable( mapping->fd )) return NULL;
645 return (struct mapping *)grab_object( mapping );
648 static void mapping_dump( struct object *obj, int verbose )
650 struct mapping *mapping = (struct mapping *)obj;
651 assert( obj->ops == &mapping_ops );
652 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared_file=%p\n",
653 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
654 mapping->flags, mapping->fd, mapping->shared_file );
657 static struct object_type *mapping_get_type( struct object *obj )
659 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
660 static const struct unicode_str str = { name, sizeof(name) };
661 return get_object_type( &str );
664 static struct fd *mapping_get_fd( struct object *obj )
666 struct mapping *mapping = (struct mapping *)obj;
667 return (struct fd *)grab_object( mapping->fd );
670 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
672 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
673 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
674 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
675 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
676 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
679 static void mapping_destroy( struct object *obj )
681 struct mapping *mapping = (struct mapping *)obj;
682 assert( obj->ops == &mapping_ops );
683 if (mapping->fd) release_object( mapping->fd );
684 if (mapping->shared_file)
686 release_object( mapping->shared_file );
687 list_remove( &mapping->shared_entry );
689 free( mapping->committed );
692 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
694 return FD_TYPE_FILE;
697 int get_page_size(void)
699 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
700 return page_mask + 1;
703 /* create a file mapping */
704 DECL_HANDLER(create_mapping)
706 struct object *root, *obj;
707 struct unicode_str name;
708 const struct security_descriptor *sd;
709 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
711 if (!objattr) return;
713 if ((obj = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
714 req->file_handle, req->file_access, sd )))
716 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
717 reply->handle = alloc_handle( current->process, obj, req->access, objattr->attributes );
718 else
719 reply->handle = alloc_handle_no_access_check( current->process, obj,
720 req->access, objattr->attributes );
721 release_object( obj );
724 if (root) release_object( root );
727 /* open a handle to a mapping */
728 DECL_HANDLER(open_mapping)
730 struct unicode_str name = get_req_unicode_str();
732 reply->handle = open_object( current->process, req->rootdir, req->access,
733 &mapping_ops, &name, req->attributes );
736 /* get a mapping information */
737 DECL_HANDLER(get_mapping_info)
739 struct mapping *mapping;
740 struct fd *fd;
742 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
744 reply->size = mapping->size;
745 reply->flags = mapping->flags;
747 if (mapping->flags & SEC_IMAGE)
748 set_reply_data( &mapping->image, min( sizeof(mapping->image), get_reply_max_size() ));
750 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
752 release_object( mapping );
753 return;
756 if ((mapping->flags & SEC_IMAGE) && mapping->cpu != current->process->cpu)
758 set_error( STATUS_INVALID_IMAGE_FORMAT );
759 release_object( mapping );
760 return;
763 if ((fd = get_obj_fd( &mapping->obj )))
765 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
766 release_object( fd );
768 if (mapping->shared_file)
770 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
771 GENERIC_READ|GENERIC_WRITE, 0 )))
773 if (reply->mapping) close_handle( current->process, reply->mapping );
776 release_object( mapping );
779 /* get a range of committed pages in a file mapping */
780 DECL_HANDLER(get_mapping_committed_range)
782 struct mapping *mapping;
784 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
786 if (!(req->offset & page_mask) && req->offset < mapping->size)
787 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
788 else
789 set_error( STATUS_INVALID_PARAMETER );
791 release_object( mapping );
795 /* add a range to the committed pages in a file mapping */
796 DECL_HANDLER(add_mapping_committed_range)
798 struct mapping *mapping;
800 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
802 if (!(req->size & page_mask) &&
803 !(req->offset & page_mask) &&
804 req->offset < mapping->size &&
805 req->size > 0 &&
806 req->size <= mapping->size - req->offset)
807 add_committed_range( mapping, req->offset, req->offset + req->size );
808 else
809 set_error( STATUS_INVALID_PARAMETER );
811 release_object( mapping );