server: Moved the create_temp_file function to mapping.c.
[wine/multimedia.git] / server / mapping.c
blobb6211bf72a38e6064518be6d7da585f0f923ce34
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 #include <unistd.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "file.h"
37 #include "handle.h"
38 #include "thread.h"
39 #include "request.h"
40 #include "security.h"
42 /* list of memory ranges, used to store committed info */
43 struct ranges
45 unsigned int count;
46 unsigned int max;
47 struct range
49 file_pos_t start;
50 file_pos_t end;
51 } ranges[1];
54 struct mapping
56 struct object obj; /* object header */
57 mem_size_t size; /* mapping size */
58 int protect; /* protection flags */
59 struct fd *fd; /* fd for mapped file */
60 struct file *file; /* file mapped */
61 int header_size; /* size of headers (for PE image mapping) */
62 client_ptr_t base; /* default base addr (for PE image mapping) */
63 struct ranges *committed; /* list of committed ranges in this mapping */
64 struct file *shared_file; /* temp file for shared PE mapping */
65 struct list shared_entry; /* entry in global shared PE mappings list */
68 static void mapping_dump( struct object *obj, int verbose );
69 static struct object_type *mapping_get_type( struct object *obj );
70 static struct fd *mapping_get_fd( struct object *obj );
71 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
72 static void mapping_destroy( struct object *obj );
73 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
75 static const struct object_ops mapping_ops =
77 sizeof(struct mapping), /* size */
78 mapping_dump, /* dump */
79 mapping_get_type, /* get_type */
80 no_add_queue, /* add_queue */
81 NULL, /* remove_queue */
82 NULL, /* signaled */
83 NULL, /* satisfied */
84 no_signal, /* signal */
85 mapping_get_fd, /* get_fd */
86 mapping_map_access, /* map_access */
87 default_get_sd, /* get_sd */
88 default_set_sd, /* set_sd */
89 no_lookup_name, /* lookup_name */
90 no_open_file, /* open_file */
91 fd_close_handle, /* close_handle */
92 mapping_destroy /* destroy */
95 static const struct fd_ops mapping_fd_ops =
97 default_fd_get_poll_events, /* get_poll_events */
98 default_poll_event, /* poll_event */
99 no_flush, /* flush */
100 mapping_get_fd_type, /* get_fd_type */
101 no_fd_ioctl, /* ioctl */
102 no_fd_queue_async, /* queue_async */
103 default_fd_reselect_async, /* reselect_async */
104 default_fd_cancel_async /* cancel_async */
107 static struct list shared_list = LIST_INIT(shared_list);
109 #ifdef __i386__
111 /* These are always the same on an i386, and it will be faster this way */
112 # define page_mask 0xfff
113 # define page_shift 12
114 # define init_page_size() do { /* nothing */ } while(0)
116 #else /* __i386__ */
118 static int page_shift, page_mask;
120 static void init_page_size(void)
122 int page_size;
123 # ifdef HAVE_GETPAGESIZE
124 page_size = getpagesize();
125 # else
126 # ifdef __svr4__
127 page_size = sysconf(_SC_PAGESIZE);
128 # else
129 # error Cannot get the page size on this platform
130 # endif
131 # endif
132 page_mask = page_size - 1;
133 /* Make sure we have a power of 2 */
134 assert( !(page_size & page_mask) );
135 page_shift = 0;
136 while ((1 << page_shift) != page_size) page_shift++;
138 #endif /* __i386__ */
140 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
143 /* extend a file beyond the current end of file */
144 static int grow_file( int unix_fd, file_pos_t new_size )
146 static const char zero;
147 off_t size = new_size;
149 if (sizeof(new_size) > sizeof(size) && size != new_size)
151 set_error( STATUS_INVALID_PARAMETER );
152 return 0;
154 /* extend the file one byte beyond the requested size and then truncate it */
155 /* this should work around ftruncate implementations that can't extend files */
156 if (pwrite( unix_fd, &zero, 1, size ) != -1)
158 ftruncate( unix_fd, size );
159 return 1;
161 file_set_error();
162 return 0;
165 /* create a temp file for anonymous mappings */
166 static struct file *create_temp_file( unsigned int access )
168 char tmpfn[16];
169 int fd;
171 sprintf( tmpfn, "anonmap.XXXXXX" ); /* create it in the server directory */
172 fd = mkstemps( tmpfn, 0 );
173 if (fd == -1)
175 file_set_error();
176 return NULL;
178 unlink( tmpfn );
179 return create_file_for_fd( fd, access, 0 );
182 /* find the shared PE mapping for a given mapping */
183 static struct file *get_shared_file( struct mapping *mapping )
185 struct mapping *ptr;
187 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
188 if (is_same_file_fd( ptr->fd, mapping->fd ))
189 return (struct file *)grab_object( ptr->shared_file );
190 return NULL;
193 /* return the size of the memory mapping and file range of a given section */
194 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
195 off_t *file_start, size_t *file_size )
197 static const unsigned int sector_align = 0x1ff;
199 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
200 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
202 *file_start = sec->PointerToRawData & ~sector_align;
203 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
204 if (*file_size > *map_size) *file_size = *map_size;
207 /* add a range to the committed list */
208 static void add_committed_range( struct mapping *mapping, file_pos_t start, file_pos_t end )
210 unsigned int i, j;
211 struct range *ranges;
213 if (!mapping->committed) return; /* everything committed already */
215 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
217 if (ranges[i].start > end) break;
218 if (ranges[i].end < start) continue;
219 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
220 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
222 for (j = i + 1; j < mapping->committed->count; j++)
224 if (ranges[j].start > end) break;
225 if (ranges[j].end > end) end = ranges[j].end;
227 if (j > i + 1)
229 memmove( &ranges[i + 1], &ranges[j], (mapping->committed->count - j) * sizeof(*ranges) );
230 mapping->committed->count -= j - (i + 1);
232 ranges[i].end = end;
234 return;
237 /* now add a new range */
239 if (mapping->committed->count == mapping->committed->max)
241 unsigned int new_size = mapping->committed->max * 2;
242 struct ranges *new_ptr = realloc( mapping->committed, offsetof( struct ranges, ranges[new_size] ));
243 if (!new_ptr) return;
244 new_ptr->max = new_size;
245 ranges = new_ptr->ranges;
246 mapping->committed = new_ptr;
248 memmove( &ranges[i + 1], &ranges[i], (mapping->committed->count - i) * sizeof(*ranges) );
249 ranges[i].start = start;
250 ranges[i].end = end;
251 mapping->committed->count++;
254 /* find the range containing start and return whether it's committed */
255 static int find_committed_range( struct mapping *mapping, file_pos_t start, mem_size_t *size )
257 unsigned int i;
258 struct range *ranges;
260 if (!mapping->committed) /* everything is committed */
262 *size = mapping->size - start;
263 return 1;
265 for (i = 0, ranges = mapping->committed->ranges; i < mapping->committed->count; i++)
267 if (ranges[i].start > start)
269 *size = ranges[i].start - start;
270 return 0;
272 if (ranges[i].end > start)
274 *size = ranges[i].end - start;
275 return 1;
278 *size = mapping->size - start;
279 return 0;
282 /* allocate and fill the temp file for a shared PE image mapping */
283 static int build_shared_mapping( struct mapping *mapping, int fd,
284 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
286 unsigned int i;
287 mem_size_t total_size;
288 size_t file_size, map_size, max_size;
289 off_t shared_pos, read_pos, write_pos;
290 char *buffer = NULL;
291 int shared_fd;
292 long toread;
294 /* compute the total size of the shared mapping */
296 total_size = max_size = 0;
297 for (i = 0; i < nb_sec; i++)
299 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
300 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
302 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
303 if (file_size > max_size) max_size = file_size;
304 total_size += map_size;
307 if (!total_size) return 1; /* nothing to do */
309 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
311 /* create a temp file for the mapping */
313 if (!(mapping->shared_file = create_temp_file( FILE_GENERIC_READ|FILE_GENERIC_WRITE ))) return 0;
314 if ((shared_fd = get_file_unix_fd( mapping->shared_file )) == -1) goto error;
315 if (!grow_file( shared_fd, total_size )) goto error;
317 if (!(buffer = malloc( max_size ))) goto error;
319 /* copy the shared sections data into the temp file */
321 shared_pos = 0;
322 for (i = 0; i < nb_sec; i++)
324 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
325 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
326 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
327 write_pos = shared_pos;
328 shared_pos += map_size;
329 if (!sec[i].PointerToRawData || !file_size) continue;
330 toread = file_size;
331 while (toread)
333 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
334 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
336 file_size -= toread;
337 break;
339 if (res <= 0) goto error;
340 toread -= res;
341 read_pos += res;
343 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
345 free( buffer );
346 return 1;
348 error:
349 release_object( mapping->shared_file );
350 mapping->shared_file = NULL;
351 free( buffer );
352 return 0;
355 /* retrieve the mapping parameters for an executable (PE) image */
356 static int get_image_params( struct mapping *mapping, int unix_fd )
358 IMAGE_DOS_HEADER dos;
359 IMAGE_SECTION_HEADER *sec = NULL;
360 struct
362 DWORD Signature;
363 IMAGE_FILE_HEADER FileHeader;
364 union
366 IMAGE_OPTIONAL_HEADER32 hdr32;
367 IMAGE_OPTIONAL_HEADER64 hdr64;
368 } opt;
369 } nt;
370 off_t pos;
371 int size;
373 /* load the headers */
375 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) goto error;
376 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
377 pos = dos.e_lfanew;
379 size = pread( unix_fd, &nt, sizeof(nt), pos );
380 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) goto error;
381 /* zero out Optional header in the case it's not present or partial */
382 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
383 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
385 switch (nt.opt.hdr32.Magic)
387 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
388 mapping->size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
389 mapping->base = nt.opt.hdr32.ImageBase;
390 mapping->header_size = nt.opt.hdr32.SizeOfHeaders;
391 break;
392 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
393 mapping->size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
394 mapping->base = nt.opt.hdr64.ImageBase;
395 mapping->header_size = nt.opt.hdr64.SizeOfHeaders;
396 break;
397 default:
398 goto error;
401 /* load the section headers */
403 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
404 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
405 if (pos + size > mapping->size) goto error;
406 if (pos + size > mapping->header_size) mapping->header_size = pos + size;
407 if (!(sec = malloc( size ))) goto error;
408 if (pread( unix_fd, sec, size, pos ) != size) goto error;
410 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
412 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
414 mapping->protect = VPROT_IMAGE;
415 free( sec );
416 return 1;
418 error:
419 free( sec );
420 set_error( STATUS_INVALID_FILE_FOR_SECTION );
421 return 0;
424 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
425 unsigned int attr, mem_size_t size, int protect,
426 obj_handle_t handle, const struct security_descriptor *sd )
428 struct mapping *mapping;
429 struct file *file;
430 struct fd *fd;
431 int access = 0;
432 int unix_fd;
433 struct stat st;
435 if (!page_mask) init_page_size();
437 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
438 return NULL;
439 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
440 return &mapping->obj; /* Nothing else to do */
442 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
443 GROUP_SECURITY_INFORMATION|
444 DACL_SECURITY_INFORMATION|
445 SACL_SECURITY_INFORMATION );
446 mapping->header_size = 0;
447 mapping->base = 0;
448 mapping->fd = NULL;
449 mapping->file = NULL;
450 mapping->shared_file = NULL;
451 mapping->committed = NULL;
453 if (protect & VPROT_READ) access |= FILE_READ_DATA;
454 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
456 if (handle)
458 if (!(protect & VPROT_COMMITTED))
460 set_error( STATUS_INVALID_PARAMETER );
461 goto error;
463 if (!(file = get_file_obj( current->process, handle, access ))) goto error;
464 fd = get_obj_fd( (struct object *)file );
465 mapping->fd = dup_fd_object( fd );
466 release_object( file );
467 release_object( fd );
468 if (!mapping->fd) goto error;
470 set_fd_user( mapping->fd, &mapping_fd_ops, &mapping->obj );
471 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
472 if (protect & VPROT_IMAGE)
474 if (!get_image_params( mapping, unix_fd )) goto error;
475 return &mapping->obj;
477 if (fstat( unix_fd, &st ) == -1)
479 file_set_error();
480 goto error;
482 if (!size)
484 if (!(size = st.st_size))
486 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
487 goto error;
490 else if (st.st_size < size && !grow_file( unix_fd, size )) goto error;
492 else /* Anonymous mapping (no associated file) */
494 if (!size || (protect & VPROT_IMAGE))
496 set_error( STATUS_INVALID_PARAMETER );
497 goto error;
499 if (!(protect & VPROT_COMMITTED))
501 if (!(mapping->committed = mem_alloc( offsetof(struct ranges, ranges[8]) ))) goto error;
502 mapping->committed->count = 0;
503 mapping->committed->max = 8;
505 if (!(mapping->file = create_temp_file( access ))) goto error;
506 mapping->fd = get_obj_fd( (struct object *)mapping->file );
507 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
508 if (!grow_file( unix_fd, size )) goto error;
510 mapping->size = (size + page_mask) & ~((mem_size_t)page_mask);
511 mapping->protect = protect;
512 return &mapping->obj;
514 error:
515 release_object( mapping );
516 return NULL;
519 static void mapping_dump( struct object *obj, int verbose )
521 struct mapping *mapping = (struct mapping *)obj;
522 assert( obj->ops == &mapping_ops );
523 fprintf( stderr, "Mapping size=%08x%08x prot=%08x fd=%p header_size=%08x base=%08lx "
524 "shared_file=%p ",
525 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
526 mapping->protect, mapping->fd, mapping->header_size,
527 (unsigned long)mapping->base, mapping->shared_file );
528 dump_object_name( &mapping->obj );
529 fputc( '\n', stderr );
532 static struct object_type *mapping_get_type( struct object *obj )
534 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
535 static const struct unicode_str str = { name, sizeof(name) };
536 return get_object_type( &str );
539 static struct fd *mapping_get_fd( struct object *obj )
541 struct mapping *mapping = (struct mapping *)obj;
542 return (struct fd *)grab_object( mapping->fd );
545 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
547 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
548 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
549 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
550 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
551 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
554 static void mapping_destroy( struct object *obj )
556 struct mapping *mapping = (struct mapping *)obj;
557 assert( obj->ops == &mapping_ops );
558 if (mapping->file) release_object( mapping->file );
559 if (mapping->fd) release_object( mapping->fd );
560 if (mapping->shared_file)
562 release_object( mapping->shared_file );
563 list_remove( &mapping->shared_entry );
565 free( mapping->committed );
568 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
570 return FD_TYPE_FILE;
573 int get_page_size(void)
575 if (!page_mask) init_page_size();
576 return page_mask + 1;
579 /* create a file mapping */
580 DECL_HANDLER(create_mapping)
582 struct object *obj;
583 struct unicode_str name;
584 struct directory *root = NULL;
585 const struct object_attributes *objattr = get_req_data();
586 const struct security_descriptor *sd;
588 reply->handle = 0;
590 if (!objattr_is_valid( objattr, get_req_data_size() ))
591 return;
593 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
594 objattr_get_name( objattr, &name );
596 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
597 return;
599 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
601 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
602 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
603 else
604 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
605 release_object( obj );
608 if (root) release_object( root );
611 /* open a handle to a mapping */
612 DECL_HANDLER(open_mapping)
614 struct unicode_str name;
615 struct directory *root = NULL;
616 struct mapping *mapping;
618 get_req_unicode_str( &name );
619 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
620 return;
622 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
624 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
625 release_object( mapping );
628 if (root) release_object( root );
631 /* get a mapping information */
632 DECL_HANDLER(get_mapping_info)
634 struct mapping *mapping;
635 struct fd *fd;
637 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
638 req->access, &mapping_ops )))
640 reply->size = mapping->size;
641 reply->protect = mapping->protect;
642 reply->header_size = mapping->header_size;
643 reply->base = mapping->base;
644 reply->shared_file = 0;
645 if ((fd = get_obj_fd( &mapping->obj )))
647 if (!is_fd_removable(fd))
648 reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
649 release_object( fd );
651 if (mapping->shared_file)
653 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
654 GENERIC_READ|GENERIC_WRITE, 0 )))
656 if (reply->mapping) close_handle( current->process, reply->mapping );
659 release_object( mapping );
663 /* get a range of committed pages in a file mapping */
664 DECL_HANDLER(get_mapping_committed_range)
666 struct mapping *mapping;
668 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle, 0, &mapping_ops )))
670 if (!(req->offset & page_mask) && req->offset < mapping->size)
671 reply->committed = find_committed_range( mapping, req->offset, &reply->size );
672 else
673 set_error( STATUS_INVALID_PARAMETER );
675 release_object( mapping );
679 /* add a range to the committed pages in a file mapping */
680 DECL_HANDLER(add_mapping_committed_range)
682 struct mapping *mapping;
684 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle, 0, &mapping_ops )))
686 if (!(req->size & page_mask) &&
687 !(req->offset & page_mask) &&
688 req->offset < mapping->size &&
689 req->size > 0 &&
690 req->size <= mapping->size - req->offset)
691 add_committed_range( mapping, req->offset, req->offset + req->size );
692 else
693 set_error( STATUS_INVALID_PARAMETER );
695 release_object( mapping );