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
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
35 #define WIN32_NO_STATUS
46 /* list of memory ranges, used to store committed info */
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 */
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
);
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
);
143 /* check if the current directory allows exec mappings */
144 static int check_current_dir_for_exec(void)
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() );
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";
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 );
187 if (!grow_file( fd
, size
))
194 else file_set_error();
196 if (temp_dir_fd
!= server_dir_fd
) fchdir( server_dir_fd
);
200 /* find the shared PE mapping for a given mapping */
201 static struct file
*get_shared_file( struct mapping
*mapping
)
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
);
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
)
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
;
247 memmove( &ranges
[i
+ 1], &ranges
[j
], (mapping
->committed
->count
- j
) * sizeof(*ranges
) );
248 mapping
->committed
->count
-= j
- (i
+ 1);
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
;
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
)
276 struct range
*ranges
;
278 if (!mapping
->committed
) /* everything is committed */
280 *size
= mapping
->size
- start
;
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
;
290 if (ranges
[i
].end
> start
)
292 *size
= ranges
[i
].end
- start
;
296 *size
= mapping
->size
- start
;
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
)
305 mem_size_t total_size
;
306 size_t file_size
, map_size
, max_size
;
307 off_t shared_pos
, read_pos
, write_pos
;
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 )))
335 if (!(buffer
= malloc( max_size
))) goto error
;
337 /* copy the shared sections data into the temp file */
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;
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 */
357 if (res
<= 0) goto error
;
361 if (pwrite( shared_fd
, buffer
, file_size
, write_pos
) != file_size
) goto error
;
367 release_object( mapping
->shared_file
);
368 mapping
->shared_file
= NULL
;
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
;
381 IMAGE_FILE_HEADER FileHeader
;
384 IMAGE_OPTIONAL_HEADER32 hdr32
;
385 IMAGE_OPTIONAL_HEADER64 hdr64
;
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
;
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
)
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
);
497 return STATUS_INVALID_FILE_FOR_SECTION
;
500 static unsigned int get_mapping_flags( obj_handle_t handle
, unsigned int flags
)
502 switch (flags
& (SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_FILE
))
505 if (flags
& (SEC_WRITECOMBINE
| SEC_LARGE_PAGES
)) break;
506 if (handle
) return SEC_FILE
| SEC_IMAGE
;
507 set_error( STATUS_INVALID_FILE_FOR_SECTION
);
510 if (!handle
) return flags
;
513 if (flags
& SEC_LARGE_PAGES
) break;
514 if (handle
) return SEC_FILE
| (flags
& (SEC_NOCACHE
| SEC_WRITECOMBINE
));
517 set_error( STATUS_INVALID_PARAMETER
);
522 static struct object
*create_mapping( struct object
*root
, const struct unicode_str
*name
,
523 unsigned int attr
, mem_size_t size
, unsigned int flags
, int protect
,
524 obj_handle_t handle
, const struct security_descriptor
*sd
)
526 struct mapping
*mapping
;
533 if (!page_mask
) page_mask
= sysconf( _SC_PAGESIZE
) - 1;
535 if (!(mapping
= create_named_object( root
, &mapping_ops
, name
, attr
, sd
)))
537 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
538 return &mapping
->obj
; /* Nothing else to do */
540 mapping
->size
= size
;
541 mapping
->protect
= protect
;
543 mapping
->shared_file
= NULL
;
544 mapping
->committed
= NULL
;
546 if (!(mapping
->flags
= get_mapping_flags( handle
, flags
))) goto error
;
548 if (protect
& VPROT_READ
) access
|= FILE_READ_DATA
;
549 if (protect
& VPROT_WRITE
) access
|= FILE_WRITE_DATA
;
553 const unsigned int sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
554 unsigned int mapping_access
= FILE_MAPPING_ACCESS
;
556 if (!(file
= get_file_obj( current
->process
, handle
, access
))) goto error
;
557 fd
= get_obj_fd( (struct object
*)file
);
559 /* file sharing rules for mappings are different so we use magic the access rights */
560 if (flags
& SEC_IMAGE
) mapping_access
|= FILE_MAPPING_IMAGE
;
561 else if (protect
& VPROT_WRITE
) mapping_access
|= FILE_MAPPING_WRITE
;
563 if (!(mapping
->fd
= get_fd_object_for_mapping( fd
, mapping_access
, sharing
)))
565 mapping
->fd
= dup_fd_object( fd
, mapping_access
, sharing
, FILE_SYNCHRONOUS_IO_NONALERT
);
566 if (mapping
->fd
) set_fd_user( mapping
->fd
, &mapping_fd_ops
, NULL
);
568 release_object( file
);
569 release_object( fd
);
570 if (!mapping
->fd
) goto error
;
572 if ((unix_fd
= get_unix_fd( mapping
->fd
)) == -1) goto error
;
573 if (fstat( unix_fd
, &st
) == -1)
578 if (flags
& SEC_IMAGE
)
580 unsigned int err
= get_image_params( mapping
, st
.st_size
, unix_fd
);
581 if (!err
) return &mapping
->obj
;
587 if (!(mapping
->size
= st
.st_size
))
589 set_error( STATUS_MAPPED_FILE_SIZE_ZERO
);
593 else if (st
.st_size
< mapping
->size
)
595 if (!(access
& FILE_WRITE_DATA
))
597 set_error( STATUS_SECTION_TOO_BIG
);
600 if (!grow_file( unix_fd
, mapping
->size
)) goto error
;
603 else /* Anonymous mapping (no associated file) */
607 set_error( STATUS_INVALID_PARAMETER
);
610 if (flags
& SEC_RESERVE
)
612 if (!(mapping
->committed
= mem_alloc( offsetof(struct ranges
, ranges
[8]) ))) goto error
;
613 mapping
->committed
->count
= 0;
614 mapping
->committed
->max
= 8;
616 mapping
->size
= (mapping
->size
+ page_mask
) & ~((mem_size_t
)page_mask
);
617 if ((unix_fd
= create_temp_file( mapping
->size
)) == -1) goto error
;
618 if (!(mapping
->fd
= create_anonymous_fd( &mapping_fd_ops
, unix_fd
, &mapping
->obj
,
619 FILE_SYNCHRONOUS_IO_NONALERT
))) goto error
;
620 allow_fd_caching( mapping
->fd
);
622 return &mapping
->obj
;
625 release_object( mapping
);
629 struct mapping
*get_mapping_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
631 return (struct mapping
*)get_handle_obj( process
, handle
, access
, &mapping_ops
);
634 /* open a new file handle to the file backing the mapping */
635 obj_handle_t
open_mapping_file( struct process
*process
, struct mapping
*mapping
,
636 unsigned int access
, unsigned int sharing
)
639 struct file
*file
= create_file_for_fd_obj( mapping
->fd
, access
, sharing
);
642 handle
= alloc_handle( process
, file
, access
, 0 );
643 release_object( file
);
647 struct mapping
*grab_mapping_unless_removable( struct mapping
*mapping
)
649 if (is_fd_removable( mapping
->fd
)) return NULL
;
650 return (struct mapping
*)grab_object( mapping
);
653 static void mapping_dump( struct object
*obj
, int verbose
)
655 struct mapping
*mapping
= (struct mapping
*)obj
;
656 assert( obj
->ops
== &mapping_ops
);
657 fprintf( stderr
, "Mapping size=%08x%08x flags=%08x prot=%08x fd=%p shared_file=%p\n",
658 (unsigned int)(mapping
->size
>> 32), (unsigned int)mapping
->size
,
659 mapping
->flags
, mapping
->protect
, mapping
->fd
, mapping
->shared_file
);
662 static struct object_type
*mapping_get_type( struct object
*obj
)
664 static const WCHAR name
[] = {'S','e','c','t','i','o','n'};
665 static const struct unicode_str str
= { name
, sizeof(name
) };
666 return get_object_type( &str
);
669 static struct fd
*mapping_get_fd( struct object
*obj
)
671 struct mapping
*mapping
= (struct mapping
*)obj
;
672 return (struct fd
*)grab_object( mapping
->fd
);
675 static unsigned int mapping_map_access( struct object
*obj
, unsigned int access
)
677 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
;
678 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
;
679 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
;
680 if (access
& GENERIC_ALL
) access
|= SECTION_ALL_ACCESS
;
681 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
684 static void mapping_destroy( struct object
*obj
)
686 struct mapping
*mapping
= (struct mapping
*)obj
;
687 assert( obj
->ops
== &mapping_ops
);
688 if (mapping
->fd
) release_object( mapping
->fd
);
689 if (mapping
->shared_file
)
691 release_object( mapping
->shared_file
);
692 list_remove( &mapping
->shared_entry
);
694 free( mapping
->committed
);
697 static enum server_fd_type
mapping_get_fd_type( struct fd
*fd
)
702 int get_page_size(void)
704 if (!page_mask
) page_mask
= sysconf( _SC_PAGESIZE
) - 1;
705 return page_mask
+ 1;
708 /* create a file mapping */
709 DECL_HANDLER(create_mapping
)
711 struct object
*root
, *obj
;
712 struct unicode_str name
;
713 const struct security_descriptor
*sd
;
714 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
716 if (!objattr
) return;
718 if ((obj
= create_mapping( root
, &name
, objattr
->attributes
,
719 req
->size
, req
->flags
, req
->protect
, req
->file_handle
, sd
)))
721 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
722 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, objattr
->attributes
);
724 reply
->handle
= alloc_handle_no_access_check( current
->process
, obj
,
725 req
->access
, objattr
->attributes
);
726 release_object( obj
);
729 if (root
) release_object( root
);
732 /* open a handle to a mapping */
733 DECL_HANDLER(open_mapping
)
735 struct unicode_str name
= get_req_unicode_str();
737 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
738 &mapping_ops
, &name
, req
->attributes
);
741 /* get a mapping information */
742 DECL_HANDLER(get_mapping_info
)
744 struct mapping
*mapping
;
747 if (!(mapping
= get_mapping_obj( current
->process
, req
->handle
, req
->access
))) return;
749 reply
->size
= mapping
->size
;
750 reply
->flags
= mapping
->flags
;
751 reply
->protect
= mapping
->protect
;
753 if (mapping
->flags
& SEC_IMAGE
)
754 set_reply_data( &mapping
->image
, min( sizeof(mapping
->image
), get_reply_max_size() ));
756 if (!(req
->access
& (SECTION_MAP_READ
| SECTION_MAP_WRITE
))) /* query only */
758 release_object( mapping
);
762 if ((mapping
->flags
& SEC_IMAGE
) && mapping
->cpu
!= current
->process
->cpu
)
764 set_error( STATUS_INVALID_IMAGE_FORMAT
);
765 release_object( mapping
);
769 if ((fd
= get_obj_fd( &mapping
->obj
)))
771 if (!is_fd_removable(fd
)) reply
->mapping
= alloc_handle( current
->process
, mapping
, 0, 0 );
772 release_object( fd
);
774 if (mapping
->shared_file
)
776 if (!(reply
->shared_file
= alloc_handle( current
->process
, mapping
->shared_file
,
777 GENERIC_READ
|GENERIC_WRITE
, 0 )))
779 if (reply
->mapping
) close_handle( current
->process
, reply
->mapping
);
782 release_object( mapping
);
785 /* get a range of committed pages in a file mapping */
786 DECL_HANDLER(get_mapping_committed_range
)
788 struct mapping
*mapping
;
790 if ((mapping
= get_mapping_obj( current
->process
, req
->handle
, 0 )))
792 if (!(req
->offset
& page_mask
) && req
->offset
< mapping
->size
)
793 reply
->committed
= find_committed_range( mapping
, req
->offset
, &reply
->size
);
795 set_error( STATUS_INVALID_PARAMETER
);
797 release_object( mapping
);
801 /* add a range to the committed pages in a file mapping */
802 DECL_HANDLER(add_mapping_committed_range
)
804 struct mapping
*mapping
;
806 if ((mapping
= get_mapping_obj( current
->process
, req
->handle
, 0 )))
808 if (!(req
->size
& page_mask
) &&
809 !(req
->offset
& page_mask
) &&
810 req
->offset
< mapping
->size
&&
812 req
->size
<= mapping
->size
- req
->offset
)
813 add_committed_range( mapping
, req
->offset
, req
->offset
+ req
->size
);
815 set_error( STATUS_INVALID_PARAMETER
);
817 release_object( mapping
);