rpcrt4: Use the Unicode version of InternetReadFileEx.
[wine.git] / server / mapping.c
blob16e7c1cb15a0ea9e39f2cac558a65ea8a0bb3ef2
1 /*
2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
32 #include <unistd.h>
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "windef.h"
37 #include "winternl.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "security.h"
46 /* list of memory ranges, used to store committed info */
47 struct ranges
49 unsigned int count;
50 unsigned int max;
51 struct range
53 file_pos_t start;
54 file_pos_t end;
55 } ranges[1];
58 struct mapping
60 struct object obj; /* object header */
61 mem_size_t size; /* mapping size */
62 int protect; /* protection flags */
63 struct fd *fd; /* fd for mapped file */
64 enum cpu_type cpu; /* client CPU (for PE image mapping) */
65 int header_size; /* size of headers (for PE image mapping) */
66 client_ptr_t base; /* default base addr (for PE image mapping) */
67 struct ranges *committed; /* list of committed ranges in this mapping */
68 struct file *shared_file; /* temp file for shared PE mapping */
69 struct list shared_entry; /* entry in global shared PE mappings list */
72 static void mapping_dump( struct object *obj, int verbose );
73 static struct object_type *mapping_get_type( struct object *obj );
74 static struct fd *mapping_get_fd( struct object *obj );
75 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
76 static void mapping_destroy( struct object *obj );
77 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
79 static const struct object_ops mapping_ops =
81 sizeof(struct mapping), /* size */
82 mapping_dump, /* dump */
83 mapping_get_type, /* get_type */
84 no_add_queue, /* add_queue */
85 NULL, /* remove_queue */
86 NULL, /* signaled */
87 NULL, /* satisfied */
88 no_signal, /* signal */
89 mapping_get_fd, /* get_fd */
90 mapping_map_access, /* map_access */
91 default_get_sd, /* get_sd */
92 default_set_sd, /* set_sd */
93 no_lookup_name, /* lookup_name */
94 no_open_file, /* open_file */
95 fd_close_handle, /* close_handle */
96 mapping_destroy /* destroy */
99 static const struct fd_ops mapping_fd_ops =
101 default_fd_get_poll_events, /* get_poll_events */
102 default_poll_event, /* poll_event */
103 mapping_get_fd_type, /* get_fd_type */
104 no_fd_read, /* read */
105 no_fd_write, /* write */
106 no_fd_flush, /* flush */
107 no_fd_ioctl, /* ioctl */
108 no_fd_queue_async, /* queue_async */
109 default_fd_reselect_async, /* reselect_async */
110 default_fd_cancel_async /* cancel_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, int unix_fd, int protect )
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 (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
393 if (dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
394 pos = dos.e_lfanew;
396 size = pread( unix_fd, &nt, sizeof(nt), pos );
397 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_FORMAT;
398 /* zero out Optional header in the case it's not present or partial */
399 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
400 if (nt.Signature != IMAGE_NT_SIGNATURE)
402 if (*(WORD *)&nt.Signature == IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_NE_FORMAT;
403 return STATUS_INVALID_IMAGE_PROTECT;
406 mapping->cpu = current->process->cpu;
407 switch (mapping->cpu)
409 case CPU_x86:
410 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) return STATUS_INVALID_IMAGE_FORMAT;
411 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
412 break;
413 case CPU_x86_64:
414 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) return STATUS_INVALID_IMAGE_FORMAT;
415 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
416 break;
417 case CPU_POWERPC:
418 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_POWERPC) return STATUS_INVALID_IMAGE_FORMAT;
419 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
420 break;
421 case CPU_ARM:
422 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM &&
423 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_THUMB &&
424 nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARMNT) return STATUS_INVALID_IMAGE_FORMAT;
425 if (nt.opt.hdr32.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
426 break;
427 case CPU_ARM64:
428 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_ARM64) return STATUS_INVALID_IMAGE_FORMAT;
429 if (nt.opt.hdr64.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return STATUS_INVALID_IMAGE_FORMAT;
430 break;
431 default:
432 return STATUS_INVALID_IMAGE_FORMAT;
435 switch (nt.opt.hdr32.Magic)
437 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
438 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
439 mapping->base = nt.opt.hdr32.ImageBase;
440 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
441 break;
442 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
443 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
444 mapping->base = nt.opt.hdr64.ImageBase;
445 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
446 break;
449 /* load the section headers */
451 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
452 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
453 if (pos + size > mapping->size) goto error;
454 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
455 if (!(sec = malloc( size ))) goto error;
456 if (pread( unix_fd, sec, size, pos ) != size) goto error;
458 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
460 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
462 mapping->protect = protect;
463 free( sec );
464 return 0;
466 error:
467 free( sec );
468 return STATUS_INVALID_FILE_FOR_SECTION;
471 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
472 unsigned int attr, mem_size_t size, int protect,
473 obj_handle_t handle, const struct security_descriptor *sd )
475 struct mapping *mapping;
476 struct file *file;
477 struct fd *fd;
478 int access = 0;
479 int unix_fd;
480 struct stat st;
482 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
484 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
485 return NULL;
486 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
487 return &mapping->obj; /* Nothing else to do */
489 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
490 GROUP_SECURITY_INFORMATION|
491 DACL_SECURITY_INFORMATION|
492 SACL_SECURITY_INFORMATION );
493 mapping->header_size = 0;
494 mapping->base = 0;
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 (!(protect & VPROT_COMMITTED))
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 (protect & VPROT_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
517 else if (protect & VPROT_WRITE) mapping_access |= FILE_MAPPING_WRITE;
519 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
521 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
522 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
524 release_object( file );
525 release_object( fd );
526 if (!mapping->fd) goto error;
528 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
529 if (protect & VPROT_IMAGE)
531 unsigned int err = get_image_params( mapping, unix_fd, protect );
532 if (!err) return &mapping->obj;
533 set_error( err );
534 goto error;
536 if (fstat( unix_fd, &st ) == -1)
538 file_set_error();
539 goto error;
541 if (!size)
543 if (!(size = st.st_size))
545 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
546 goto error;
549 else if (st.st_size < size && !grow_file( unix_fd, size )) goto error;
551 else /* Anonymous mapping (no associated file) */
553 if (!size || (protect & VPROT_IMAGE))
555 set_error( STATUS_INVALID_PARAMETER );
556 goto error;
558 if (!(protect & VPROT_COMMITTED))
560 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
561 mapping->committed->count = 0;
562 mapping->committed->max = 8;
564 if ((unix_fd = create_temp_file( size )) == -1) goto error;
565 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
566 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
567 allow_fd_caching( mapping->fd );
569 mapping->size = (size + page_mask) & ~((mem_size_t)page_mask);
570 mapping->protect = protect;
571 return &mapping->obj;
573 error:
574 release_object( mapping );
575 return NULL;
578 struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
580 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
583 /* open a new file handle to the file backing the mapping */
584 obj_handle_t open_mapping_file( struct process *process, struct mapping *mapping,
585 unsigned int access, unsigned int sharing )
587 obj_handle_t handle;
588 struct file *file = create_file_for_fd_obj( mapping->fd, access, sharing );
590 if (!file) return 0;
591 handle = alloc_handle( process, file, access, 0 );
592 release_object( file );
593 return handle;
596 struct mapping *grab_mapping_unless_removable( struct mapping *mapping )
598 if (is_fd_removable( mapping->fd )) return NULL;
599 return (struct mapping *)grab_object( mapping );
602 static void mapping_dump( struct object *obj, int verbose )
604 struct mapping *mapping = (struct mapping *)obj;
605 assert( obj->ops == &mapping_ops );
606 fprintf( stderr, "Mapping size=%08x%08x prot=%08x fd=%p header_size=%08x base=%08lx "
607 "shared_file=%p ",
608 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
609 mapping->protect, mapping->fd, mapping->header_size,
610 (unsigned long)mapping->base, mapping->shared_file );
611 dump_object_name( &mapping->obj );
612 fputc( '\n', stderr );
615 static struct object_type *mapping_get_type( struct object *obj )
617 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
618 static const struct unicode_str str = { name, sizeof(name) };
619 return get_object_type( &str );
622 static struct fd *mapping_get_fd( struct object *obj )
624 struct mapping *mapping = (struct mapping *)obj;
625 return (struct fd *)grab_object( mapping->fd );
628 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
630 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
631 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
632 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
633 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
634 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
637 static void mapping_destroy( struct object *obj )
639 struct mapping *mapping = (struct mapping *)obj;
640 assert( obj->ops == &mapping_ops );
641 if (mapping->fd) release_object( mapping->fd );
642 if (mapping->shared_file)
644 release_object( mapping->shared_file );
645 list_remove( &mapping->shared_entry );
647 free( mapping->committed );
650 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
652 return FD_TYPE_FILE;
655 int get_page_size(void)
657 if (!page_mask) page_mask = sysconf( _SC_PAGESIZE ) - 1;
658 return page_mask + 1;
661 /* create a file mapping */
662 DECL_HANDLER(create_mapping)
664 struct object *obj;
665 struct unicode_str name;
666 struct directory *root = NULL;
667 const struct object_attributes *objattr = get_req_data();
668 const struct security_descriptor *sd;
670 reply->handle = 0;
672 if (!objattr_is_valid( objattr, get_req_data_size() ))
673 return;
675 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
676 objattr_get_name( objattr, &name );
678 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
679 return;
681 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
683 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
684 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
685 else
686 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
687 release_object( obj );
690 if (root) release_object( root );
693 /* open a handle to a mapping */
694 DECL_HANDLER(open_mapping)
696 struct unicode_str name;
697 struct directory *root = NULL;
698 struct mapping *mapping;
700 get_req_unicode_str( &name );
701 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
702 return;
704 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
706 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
707 release_object( mapping );
710 if (root) release_object( root );
713 /* get a mapping information */
714 DECL_HANDLER(get_mapping_info)
716 struct mapping *mapping;
717 struct fd *fd;
719 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
721 if ((mapping->protect & VPROT_IMAGE) && mapping->cpu != current->process->cpu)
723 set_error( STATUS_INVALID_IMAGE_FORMAT );
724 release_object( mapping );
725 return;
728 reply->size = mapping->size;
729 reply->protect = mapping->protect;
730 reply->header_size = mapping->header_size;
731 reply->base = mapping->base;
732 reply->shared_file = 0;
733 if ((fd = get_obj_fd( &mapping->obj )))
735 if (!is_fd_removable(fd)) reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
736 release_object( fd );
738 if (mapping->shared_file)
740 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
741 GENERIC_READ|GENERIC_WRITE, 0 )))
743 if (reply->mapping) close_handle( current->process, reply->mapping );
746 release_object( mapping );
749 /* get a range of committed pages in a file mapping */
750 DECL_HANDLER(get_mapping_committed_range)
752 struct mapping *mapping;
754 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
756 if (!(req->offset & page_mask) && req->offset < mapping->size)
757 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
758 else
759 set_error( STATUS_INVALID_PARAMETER );
761 release_object( mapping );
765 /* add a range to the committed pages in a file mapping */
766 DECL_HANDLER(add_mapping_committed_range)
768 struct mapping *mapping;
770 if ((mapping = get_mapping_obj( current->process, req->handle, 0 )))
772 if (!(req->size & page_mask) &&
773 !(req->offset & page_mask) &&
774 req->offset < mapping->size &&
775 req->size > 0 &&
776 req->size <= mapping->size - req->offset)
777 add_committed_range( mapping, req->offset, req->offset + req->size );
778 else
779 set_error( STATUS_INVALID_PARAMETER );
781 release_object( mapping );