msi: Break out the publishing tests into their respective actions.
[wine.git] / server / mapping.c
blob02b73b19715c5ccff480d32981c3260722d5f92a
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 struct mapping
44 struct object obj; /* object header */
45 file_pos_t size; /* mapping size */
46 int protect; /* protection flags */
47 struct file *file; /* file mapped */
48 int header_size; /* size of headers (for PE image mapping) */
49 void *base; /* default base addr (for PE image mapping) */
50 struct file *shared_file; /* temp file for shared PE mapping */
51 struct list shared_entry; /* entry in global shared PE mappings list */
54 static void mapping_dump( struct object *obj, int verbose );
55 static struct object_type *mapping_get_type( struct object *obj );
56 static struct fd *mapping_get_fd( struct object *obj );
57 static unsigned int mapping_map_access( struct object *obj, unsigned int access );
58 static void mapping_destroy( struct object *obj );
60 static const struct object_ops mapping_ops =
62 sizeof(struct mapping), /* size */
63 mapping_dump, /* dump */
64 mapping_get_type, /* get_type */
65 no_add_queue, /* add_queue */
66 NULL, /* remove_queue */
67 NULL, /* signaled */
68 NULL, /* satisfied */
69 no_signal, /* signal */
70 mapping_get_fd, /* get_fd */
71 mapping_map_access, /* map_access */
72 default_get_sd, /* get_sd */
73 default_set_sd, /* set_sd */
74 no_lookup_name, /* lookup_name */
75 no_open_file, /* open_file */
76 fd_close_handle, /* close_handle */
77 mapping_destroy /* destroy */
80 static struct list shared_list = LIST_INIT(shared_list);
82 #ifdef __i386__
84 /* These are always the same on an i386, and it will be faster this way */
85 # define page_mask 0xfff
86 # define page_shift 12
87 # define init_page_size() do { /* nothing */ } while(0)
89 #else /* __i386__ */
91 static int page_shift, page_mask;
93 static void init_page_size(void)
95 int page_size;
96 # ifdef HAVE_GETPAGESIZE
97 page_size = getpagesize();
98 # else
99 # ifdef __svr4__
100 page_size = sysconf(_SC_PAGESIZE);
101 # else
102 # error Cannot get the page size on this platform
103 # endif
104 # endif
105 page_mask = page_size - 1;
106 /* Make sure we have a power of 2 */
107 assert( !(page_size & page_mask) );
108 page_shift = 0;
109 while ((1 << page_shift) != page_size) page_shift++;
111 #endif /* __i386__ */
113 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
116 /* find the shared PE mapping for a given mapping */
117 static struct file *get_shared_file( struct mapping *mapping )
119 struct mapping *ptr;
121 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
122 if (is_same_file( ptr->file, mapping->file ))
123 return (struct file *)grab_object( ptr->shared_file );
124 return NULL;
127 /* return the size of the memory mapping and file range of a given section */
128 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
129 off_t *file_start, size_t *file_size )
131 static const unsigned int sector_align = 0x1ff;
133 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
134 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
136 *file_start = sec->PointerToRawData & ~sector_align;
137 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
138 if (*file_size > *map_size) *file_size = *map_size;
141 /* allocate and fill the temp file for a shared PE image mapping */
142 static int build_shared_mapping( struct mapping *mapping, int fd,
143 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
145 unsigned int i;
146 file_pos_t total_size;
147 size_t file_size, map_size, max_size;
148 off_t shared_pos, read_pos, write_pos;
149 char *buffer = NULL;
150 int shared_fd;
151 long toread;
153 /* compute the total size of the shared mapping */
155 total_size = max_size = 0;
156 for (i = 0; i < nb_sec; i++)
158 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
159 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
161 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
162 if (file_size > max_size) max_size = file_size;
163 total_size += map_size;
166 if (!total_size) return 1; /* nothing to do */
168 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
170 /* create a temp file for the mapping */
172 if (!(mapping->shared_file = create_temp_file( FILE_GENERIC_READ|FILE_GENERIC_WRITE ))) return 0;
173 if (!grow_file( mapping->shared_file, total_size )) goto error;
174 if ((shared_fd = get_file_unix_fd( mapping->shared_file )) == -1) goto error;
176 if (!(buffer = malloc( max_size ))) goto error;
178 /* copy the shared sections data into the temp file */
180 shared_pos = 0;
181 for (i = 0; i < nb_sec; i++)
183 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
184 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
185 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
186 write_pos = shared_pos;
187 shared_pos += map_size;
188 if (!sec[i].PointerToRawData || !file_size) continue;
189 toread = file_size;
190 while (toread)
192 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
193 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
195 file_size -= toread;
196 break;
198 if (res <= 0) goto error;
199 toread -= res;
200 read_pos += res;
202 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
204 free( buffer );
205 return 1;
207 error:
208 release_object( mapping->shared_file );
209 mapping->shared_file = NULL;
210 free( buffer );
211 return 0;
214 /* retrieve the mapping parameters for an executable (PE) image */
215 static int get_image_params( struct mapping *mapping )
217 IMAGE_DOS_HEADER dos;
218 IMAGE_NT_HEADERS nt;
219 IMAGE_SECTION_HEADER *sec = NULL;
220 struct fd *fd;
221 off_t pos;
222 int unix_fd, size, toread;
224 /* load the headers */
226 if (!(fd = mapping_get_fd( &mapping->obj ))) return 0;
227 if ((unix_fd = get_unix_fd( fd )) == -1) goto error;
228 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) goto error;
229 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
230 pos = dos.e_lfanew;
232 if (pread( unix_fd, &nt.Signature, sizeof(nt.Signature), pos ) != sizeof(nt.Signature))
233 goto error;
234 pos += sizeof(nt.Signature);
235 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
236 if (pread( unix_fd, &nt.FileHeader, sizeof(nt.FileHeader), pos ) != sizeof(nt.FileHeader))
237 goto error;
238 pos += sizeof(nt.FileHeader);
239 /* zero out Optional header in the case it's not present or partial */
240 memset(&nt.OptionalHeader, 0, sizeof(nt.OptionalHeader));
241 toread = min( sizeof(nt.OptionalHeader), nt.FileHeader.SizeOfOptionalHeader );
242 if (pread( unix_fd, &nt.OptionalHeader, toread, pos ) != toread) goto error;
243 pos += nt.FileHeader.SizeOfOptionalHeader;
245 /* load the section headers */
247 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
248 if (!(sec = malloc( size ))) goto error;
249 if (pread( unix_fd, sec, size, pos ) != size) goto error;
251 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
253 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
255 mapping->size = ROUND_SIZE( nt.OptionalHeader.SizeOfImage );
256 mapping->base = (void *)nt.OptionalHeader.ImageBase;
257 mapping->header_size = max( pos + size, nt.OptionalHeader.SizeOfHeaders );
258 mapping->protect = VPROT_IMAGE;
260 /* sanity check */
261 if (pos + size > mapping->size) goto error;
263 free( sec );
264 release_object( fd );
265 return 1;
267 error:
268 free( sec );
269 release_object( fd );
270 set_error( STATUS_INVALID_FILE_FOR_SECTION );
271 return 0;
274 /* get the size of the unix file associated with the mapping */
275 static inline int get_file_size( struct file *file, file_pos_t *size )
277 struct stat st;
278 int unix_fd = get_file_unix_fd( file );
280 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 0;
281 *size = st.st_size;
282 return 1;
285 static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
286 unsigned int attr, file_pos_t size, int protect,
287 obj_handle_t handle, const struct security_descriptor *sd )
289 struct mapping *mapping;
290 int access = 0;
292 if (!page_mask) init_page_size();
294 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
295 return NULL;
296 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
297 return &mapping->obj; /* Nothing else to do */
299 if (sd) default_set_sd( &mapping->obj, sd, OWNER_SECURITY_INFORMATION|
300 GROUP_SECURITY_INFORMATION|
301 DACL_SECURITY_INFORMATION|
302 SACL_SECURITY_INFORMATION );
303 mapping->header_size = 0;
304 mapping->base = NULL;
305 mapping->shared_file = NULL;
307 if (protect & VPROT_READ) access |= FILE_READ_DATA;
308 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
310 if (handle)
312 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
313 if (protect & VPROT_IMAGE)
315 if (!get_image_params( mapping )) goto error;
316 return &mapping->obj;
318 if (!size)
320 if (!get_file_size( mapping->file, &size )) goto error;
321 if (!size)
323 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
324 goto error;
327 else
329 if (!grow_file( mapping->file, size )) goto error;
332 else /* Anonymous mapping (no associated file) */
334 if (!size || (protect & VPROT_IMAGE))
336 set_error( STATUS_INVALID_PARAMETER );
337 mapping->file = NULL;
338 goto error;
340 if (!(mapping->file = create_temp_file( access ))) goto error;
341 if (!grow_file( mapping->file, size )) goto error;
343 mapping->size = (size + page_mask) & ~((file_pos_t)page_mask);
344 mapping->protect = protect;
345 return &mapping->obj;
347 error:
348 release_object( mapping );
349 return NULL;
352 static void mapping_dump( struct object *obj, int verbose )
354 struct mapping *mapping = (struct mapping *)obj;
355 assert( obj->ops == &mapping_ops );
356 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
357 "shared_file=%p ",
358 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
359 mapping->protect, mapping->file, mapping->header_size,
360 mapping->base, mapping->shared_file );
361 dump_object_name( &mapping->obj );
362 fputc( '\n', stderr );
365 static struct object_type *mapping_get_type( struct object *obj )
367 static const WCHAR name[] = {'S','e','c','t','i','o','n'};
368 static const struct unicode_str str = { name, sizeof(name) };
369 return get_object_type( &str );
372 static struct fd *mapping_get_fd( struct object *obj )
374 struct mapping *mapping = (struct mapping *)obj;
375 return get_obj_fd( (struct object *)mapping->file );
378 static unsigned int mapping_map_access( struct object *obj, unsigned int access )
380 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
381 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
382 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
383 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
384 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
387 static void mapping_destroy( struct object *obj )
389 struct mapping *mapping = (struct mapping *)obj;
390 assert( obj->ops == &mapping_ops );
391 if (mapping->file) release_object( mapping->file );
392 if (mapping->shared_file)
394 release_object( mapping->shared_file );
395 list_remove( &mapping->shared_entry );
399 int get_page_size(void)
401 if (!page_mask) init_page_size();
402 return page_mask + 1;
405 /* create a file mapping */
406 DECL_HANDLER(create_mapping)
408 struct object *obj;
409 struct unicode_str name;
410 struct directory *root = NULL;
411 const struct object_attributes *objattr = get_req_data();
412 const struct security_descriptor *sd;
414 reply->handle = 0;
416 if (!objattr_is_valid( objattr, get_req_data_size() ))
417 return;
419 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
420 objattr_get_name( objattr, &name );
422 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
423 return;
425 if ((obj = create_mapping( root, &name, req->attributes, req->size, req->protect, req->file_handle, sd )))
427 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
428 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
429 else
430 reply->handle = alloc_handle_no_access_check( current->process, obj, req->access, req->attributes );
431 release_object( obj );
434 if (root) release_object( root );
437 /* open a handle to a mapping */
438 DECL_HANDLER(open_mapping)
440 struct unicode_str name;
441 struct directory *root = NULL;
442 struct mapping *mapping;
444 get_req_unicode_str( &name );
445 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
446 return;
448 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
450 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
451 release_object( mapping );
454 if (root) release_object( root );
457 /* get a mapping information */
458 DECL_HANDLER(get_mapping_info)
460 struct mapping *mapping;
461 struct fd *fd;
463 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
464 0, &mapping_ops )))
466 reply->size = mapping->size;
467 reply->protect = mapping->protect;
468 reply->header_size = mapping->header_size;
469 reply->base = mapping->base;
470 reply->shared_file = 0;
471 if ((fd = get_obj_fd( &mapping->obj )))
473 if (!is_fd_removable(fd))
474 reply->mapping = alloc_handle( current->process, mapping, 0, 0 );
475 release_object( fd );
477 if (mapping->shared_file)
479 if (!(reply->shared_file = alloc_handle( current->process, mapping->shared_file,
480 GENERIC_READ|GENERIC_WRITE, 0 )))
482 if (reply->mapping) close_handle( current->process, reply->mapping );
485 release_object( mapping );