blob: 02f6d48bc5120108d5c95fb5e9684e9c2d062dca [file] [log] [blame]
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +00001/*
2 * Server-side file mapping management
3 *
4 * Copyright (C) 1999 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000019 */
20
Francois Gougete5ddd262001-10-14 16:18:52 +000021#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000022#include "wine/port.h"
Francois Gougete5ddd262001-10-14 16:18:52 +000023
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000024#include <assert.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000025#include <stdarg.h>
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000026#include <stdio.h>
27#include <stdlib.h>
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +000028#include <sys/stat.h>
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000029#include <unistd.h>
30
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010031#include "ntstatus.h"
32#define WIN32_NO_STATUS
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000033#include "windef.h"
Vitaliy Margolena9960002005-10-27 18:30:37 +000034#include "winternl.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000035
Alexandre Julliard863637b2003-01-30 00:26:44 +000036#include "file.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000037#include "handle.h"
38#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000039#include "request.h"
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000040
41struct mapping
42{
Alexandre Julliard84fdfd02001-04-13 22:38:39 +000043 struct object obj; /* object header */
Alexandre Julliarda4334a62005-04-19 11:59:13 +000044 file_pos_t size; /* mapping size */
Alexandre Julliard84fdfd02001-04-13 22:38:39 +000045 int protect; /* protection flags */
46 struct file *file; /* file mapped */
47 int header_size; /* size of headers (for PE image mapping) */
48 void *base; /* default base addr (for PE image mapping) */
49 struct file *shared_file; /* temp file for shared PE mapping */
50 int shared_size; /* shared mapping total size */
Alexandre Julliardbb82cbb2005-07-07 12:04:55 +000051 struct list shared_entry; /* entry in global shared PE mappings list */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000052};
53
54static void mapping_dump( struct object *obj, int verbose );
Alexandre Julliarde66207e2003-02-19 00:33:32 +000055static struct fd *mapping_get_fd( struct object *obj );
Alexandre Julliarda510a7e2005-12-12 16:46:17 +010056static unsigned int mapping_map_access( struct object *obj, unsigned int access );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000057static void mapping_destroy( struct object *obj );
58
59static const struct object_ops mapping_ops =
60{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000061 sizeof(struct mapping), /* size */
62 mapping_dump, /* dump */
63 no_add_queue, /* add_queue */
64 NULL, /* remove_queue */
65 NULL, /* signaled */
66 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +000067 no_signal, /* signal */
Alexandre Julliarde66207e2003-02-19 00:33:32 +000068 mapping_get_fd, /* get_fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +010069 mapping_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +000070 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +000071 no_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000072 mapping_destroy /* destroy */
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +000073};
74
Alexandre Julliardbb82cbb2005-07-07 12:04:55 +000075static struct list shared_list = LIST_INIT(shared_list);
Alexandre Julliard84fdfd02001-04-13 22:38:39 +000076
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +000077#ifdef __i386__
78
79/* These are always the same on an i386, and it will be faster this way */
80# define page_mask 0xfff
81# define page_shift 12
Alexandre Julliard908464d2000-11-01 03:11:12 +000082# define init_page_size() do { /* nothing */ } while(0)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +000083
84#else /* __i386__ */
85
86static int page_shift, page_mask;
87
88static void init_page_size(void)
89{
90 int page_size;
91# ifdef HAVE_GETPAGESIZE
92 page_size = getpagesize();
93# else
94# ifdef __svr4__
95 page_size = sysconf(_SC_PAGESIZE);
96# else
97# error Cannot get the page size on this platform
98# endif
99# endif
100 page_mask = page_size - 1;
101 /* Make sure we have a power of 2 */
102 assert( !(page_size & page_mask) );
103 page_shift = 0;
104 while ((1 << page_shift) != page_size) page_shift++;
105}
106#endif /* __i386__ */
107
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000108#define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000109
110
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000111/* find the shared PE mapping for a given mapping */
112static struct file *get_shared_file( struct mapping *mapping )
113{
114 struct mapping *ptr;
115
Alexandre Julliardbb82cbb2005-07-07 12:04:55 +0000116 LIST_FOR_EACH_ENTRY( ptr, &shared_list, struct mapping, shared_entry )
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000117 if (is_same_file( ptr->file, mapping->file ))
118 return (struct file *)grab_object( ptr->shared_file );
119 return NULL;
120}
121
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000122/* return the size of the memory mapping of a given section */
123static inline unsigned int get_section_map_size( const IMAGE_SECTION_HEADER *sec )
124{
125 if (!sec->Misc.VirtualSize) return ROUND_SIZE( sec->SizeOfRawData );
126 else return ROUND_SIZE( sec->Misc.VirtualSize );
127}
128
129/* return the size of the file mapping of a given section */
130static inline unsigned int get_section_filemap_size( const IMAGE_SECTION_HEADER *sec )
131{
132 if (!sec->Misc.VirtualSize) return sec->SizeOfRawData;
133 else return min( sec->SizeOfRawData, ROUND_SIZE( sec->Misc.VirtualSize ) );
134}
135
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000136/* allocate and fill the temp file for a shared PE image mapping */
137static int build_shared_mapping( struct mapping *mapping, int fd,
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000138 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000139{
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000140 unsigned int i, size, max_size, total_size;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000141 off_t shared_pos, read_pos, write_pos;
Alexandre Julliard63411db2000-12-22 21:12:36 +0000142 char *buffer = NULL;
143 int shared_fd;
Andreas Mohr1c99af42000-12-03 04:02:09 +0000144 long toread;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000145
146 /* compute the total size of the shared mapping */
147
148 total_size = max_size = 0;
149 for (i = 0; i < nb_sec; i++)
150 {
151 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
152 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
153 {
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000154 size = get_section_filemap_size( &sec[i] );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000155 if (size > max_size) max_size = size;
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000156 total_size += get_section_map_size( &sec[i] );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000157 }
158 }
159 if (!(mapping->shared_size = total_size)) return 1; /* nothing to do */
160
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000161 if ((mapping->shared_file = get_shared_file( mapping ))) return 1;
162
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000163 /* create a temp file for the mapping */
164
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100165 if (!(mapping->shared_file = create_temp_file( FILE_GENERIC_READ|FILE_GENERIC_WRITE ))) return 0;
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000166 if (!grow_file( mapping->shared_file, total_size )) goto error;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000167 if ((shared_fd = get_file_unix_fd( mapping->shared_file )) == -1) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000168
169 if (!(buffer = malloc( max_size ))) goto error;
170
171 /* copy the shared sections data into the temp file */
172
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000173 shared_pos = 0;
174 for (i = 0; i < nb_sec; i++)
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000175 {
176 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
177 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000178 write_pos = shared_pos;
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000179 shared_pos += get_section_map_size( &sec[i] );
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000180 read_pos = sec[i].PointerToRawData;
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000181 size = get_section_filemap_size( &sec[i] );
182 if (!read_pos || !size) continue;
183 toread = size;
Andreas Mohr1c99af42000-12-03 04:02:09 +0000184 while (toread)
185 {
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000186 long res = pread( fd, buffer + sec[i].SizeOfRawData - toread, toread, read_pos );
Andreas Mohr1c99af42000-12-03 04:02:09 +0000187 if (res <= 0) goto error;
188 toread -= res;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000189 read_pos += res;
Andreas Mohr1c99af42000-12-03 04:02:09 +0000190 }
Alexandre Julliard4ebd2fc2005-08-15 14:50:06 +0000191 if (pwrite( shared_fd, buffer, size, write_pos ) != size) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000192 }
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000193 free( buffer );
194 return 1;
195
196 error:
Alexandre Julliardca18f152002-04-20 21:07:24 +0000197 release_object( mapping->shared_file );
198 mapping->shared_file = NULL;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000199 if (buffer) free( buffer );
200 return 0;
201}
202
203/* retrieve the mapping parameters for an executable (PE) image */
204static int get_image_params( struct mapping *mapping )
205{
206 IMAGE_DOS_HEADER dos;
207 IMAGE_NT_HEADERS nt;
208 IMAGE_SECTION_HEADER *sec = NULL;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000209 struct fd *fd;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000210 off_t pos;
Marcus Meissner4b811d92005-10-31 21:03:05 +0000211 int unix_fd, size, toread;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000212
213 /* load the headers */
214
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000215 if (!(fd = mapping_get_fd( &mapping->obj ))) return 0;
Alexandre Julliard964815b2005-08-08 15:11:03 +0000216 if ((unix_fd = get_unix_fd( fd )) == -1) goto error;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000217 if (pread( unix_fd, &dos, sizeof(dos), 0 ) != sizeof(dos)) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000218 if (dos.e_magic != IMAGE_DOS_SIGNATURE) goto error;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000219 pos = dos.e_lfanew;
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000220
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000221 if (pread( unix_fd, &nt.Signature, sizeof(nt.Signature), pos ) != sizeof(nt.Signature))
222 goto error;
223 pos += sizeof(nt.Signature);
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000224 if (nt.Signature != IMAGE_NT_SIGNATURE) goto error;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000225 if (pread( unix_fd, &nt.FileHeader, sizeof(nt.FileHeader), pos ) != sizeof(nt.FileHeader))
226 goto error;
227 pos += sizeof(nt.FileHeader);
Dmitry Timoshkov1467bbd2002-08-27 00:34:41 +0000228 /* zero out Optional header in the case it's not present or partial */
229 memset(&nt.OptionalHeader, 0, sizeof(nt.OptionalHeader));
Marcus Meissner4b811d92005-10-31 21:03:05 +0000230 toread = min( sizeof(nt.OptionalHeader), nt.FileHeader.SizeOfOptionalHeader );
231 if (pread( unix_fd, &nt.OptionalHeader, toread, pos ) != toread) goto error;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000232 pos += nt.FileHeader.SizeOfOptionalHeader;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000233
234 /* load the section headers */
235
236 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
237 if (!(sec = malloc( size ))) goto error;
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000238 if (pread( unix_fd, sec, size, pos ) != size) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000239
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000240 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections )) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000241
Alexandre Julliardbb82cbb2005-07-07 12:04:55 +0000242 if (mapping->shared_file) list_add_head( &shared_list, &mapping->shared_entry );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000243
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000244 mapping->size = ROUND_SIZE( nt.OptionalHeader.SizeOfImage );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000245 mapping->base = (void *)nt.OptionalHeader.ImageBase;
Alexandre Julliardd59fa772006-04-21 16:16:11 +0200246 mapping->header_size = pos + size;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000247 mapping->protect = VPROT_IMAGE;
248
249 /* sanity check */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000250 if (mapping->header_size > mapping->size) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000251
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000252 free( sec );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000253 release_object( fd );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000254 return 1;
255
256 error:
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000257 if (sec) free( sec );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000258 release_object( fd );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000259 set_error( STATUS_INVALID_FILE_FOR_SECTION );
260 return 0;
261}
262
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000263/* get the size of the unix file associated with the mapping */
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000264inline static int get_file_size( struct file *file, file_pos_t *size )
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000265{
266 struct stat st;
Alexandre Julliardd0e4a762003-02-17 01:50:40 +0000267 int unix_fd = get_file_unix_fd( file );
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000268
Alexandre Julliard964815b2005-08-08 15:11:03 +0000269 if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 0;
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000270 *size = st.st_size;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000271 return 1;
272}
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000273
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100274static struct object *create_mapping( struct directory *root, const struct unicode_str *name,
275 unsigned int attr, file_pos_t size, int protect,
276 obj_handle_t handle )
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000277{
278 struct mapping *mapping;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000279 int access = 0;
280
281 if (!page_mask) init_page_size();
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000282
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100283 if (!(mapping = create_named_object_dir( root, name, attr, &mapping_ops )))
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000284 return NULL;
Vitaliy Margolen893987b2005-11-21 16:27:03 +0000285 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000286 return &mapping->obj; /* Nothing else to do */
287
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000288 mapping->header_size = 0;
289 mapping->base = NULL;
290 mapping->shared_file = NULL;
291 mapping->shared_size = 0;
292
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100293 if (protect & VPROT_READ) access |= FILE_READ_DATA;
294 if (protect & VPROT_WRITE) access |= FILE_WRITE_DATA;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000295
Alexandre Julliard8081e5a2001-01-05 04:08:07 +0000296 if (handle)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000297 {
298 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000299 if (protect & VPROT_IMAGE)
300 {
301 if (!get_image_params( mapping )) goto error;
302 return &mapping->obj;
303 }
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000304 if (!size)
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000305 {
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000306 if (!get_file_size( mapping->file, &size )) goto error;
307 if (!size)
Mike McCormackdc827692004-01-09 00:34:14 +0000308 {
Alexandre Julliard20175552005-12-12 13:38:43 +0100309 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
Mike McCormackdc827692004-01-09 00:34:14 +0000310 goto error;
311 }
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000312 }
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000313 else
314 {
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000315 if (!grow_file( mapping->file, size )) goto error;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +0000316 }
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000317 }
318 else /* Anonymous mapping (no associated file) */
319 {
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000320 if (!size || (protect & VPROT_IMAGE))
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000321 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000322 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000323 mapping->file = NULL;
324 goto error;
325 }
326 if (!(mapping->file = create_temp_file( access ))) goto error;
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000327 if (!grow_file( mapping->file, size )) goto error;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000328 }
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000329 mapping->size = (size + page_mask) & ~((file_pos_t)page_mask);
330 mapping->protect = protect;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000331 return &mapping->obj;
Alexandre Julliardcb28bdc1999-02-28 10:13:59 +0000332
333 error:
334 release_object( mapping );
335 return NULL;
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000336}
337
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000338static void mapping_dump( struct object *obj, int verbose )
339{
340 struct mapping *mapping = (struct mapping *)obj;
341 assert( obj->ops == &mapping_ops );
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000342 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p header_size=%08x base=%p "
343 "shared_file=%p shared_size=%08x ",
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000344 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
345 mapping->protect, mapping->file, mapping->header_size,
346 mapping->base, mapping->shared_file, mapping->shared_size );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000347 dump_object_name( &mapping->obj );
348 fputc( '\n', stderr );
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000349}
350
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000351static struct fd *mapping_get_fd( struct object *obj )
352{
353 struct mapping *mapping = (struct mapping *)obj;
354 return get_obj_fd( (struct object *)mapping->file );
355}
356
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100357static unsigned int mapping_map_access( struct object *obj, unsigned int access )
358{
359 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ;
360 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE;
361 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE;
362 if (access & GENERIC_ALL) access |= SECTION_ALL_ACCESS;
363 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
364}
365
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000366static void mapping_destroy( struct object *obj )
367{
368 struct mapping *mapping = (struct mapping *)obj;
369 assert( obj->ops == &mapping_ops );
370 if (mapping->file) release_object( mapping->file );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000371 if (mapping->shared_file)
372 {
373 release_object( mapping->shared_file );
Alexandre Julliardbb82cbb2005-07-07 12:04:55 +0000374 list_remove( &mapping->shared_entry );
Alexandre Julliard84fdfd02001-04-13 22:38:39 +0000375 }
Alexandre Julliarda8b8d9c1999-01-01 16:59:27 +0000376}
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000377
Alexandre Julliardc76ad351999-11-13 19:49:05 +0000378int get_page_size(void)
379{
380 if (!page_mask) init_page_size();
381 return page_mask + 1;
382}
383
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000384/* create a file mapping */
385DECL_HANDLER(create_mapping)
386{
387 struct object *obj;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000388 struct unicode_str name;
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100389 struct directory *root = NULL;
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000390 file_pos_t size = ((file_pos_t)req->size_high << 32) | req->size_low;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000391
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000392 reply->handle = 0;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000393 get_req_unicode_str( &name );
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100394 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
395 return;
396
397 if ((obj = create_mapping( root, &name, req->attributes, size, req->protect, req->file_handle )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000398 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100399 reply->handle = alloc_handle( current->process, obj, req->access, req->attributes );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000400 release_object( obj );
401 }
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100402
403 if (root) release_object( root );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000404}
405
406/* open a handle to a mapping */
407DECL_HANDLER(open_mapping)
408{
Alexandre Julliardead9b062005-11-18 16:31:18 +0000409 struct unicode_str name;
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100410 struct directory *root = NULL;
Alexandre Julliard3764da62005-12-05 12:52:05 +0100411 struct mapping *mapping;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000412
413 get_req_unicode_str( &name );
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100414 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
415 return;
416
Alexandre Julliard3764da62005-12-05 12:52:05 +0100417 if ((mapping = open_object_dir( root, &name, req->attributes, &mapping_ops )))
418 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100419 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, req->attributes );
Alexandre Julliard3764da62005-12-05 12:52:05 +0100420 release_object( mapping );
421 }
Vitaliy Margolen348a3d92005-12-02 16:13:13 +0100422
423 if (root) release_object( root );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000424}
425
426/* get a mapping information */
427DECL_HANDLER(get_mapping_info)
428{
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000429 struct mapping *mapping;
430
431 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
432 0, &mapping_ops )))
433 {
Alexandre Julliarda4334a62005-04-19 11:59:13 +0000434 reply->size_high = (unsigned int)(mapping->size >> 32);
435 reply->size_low = (unsigned int)mapping->size;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000436 reply->protect = mapping->protect;
437 reply->header_size = mapping->header_size;
438 reply->base = mapping->base;
439 reply->shared_file = 0;
440 reply->shared_size = mapping->shared_size;
Alexandre Julliardc19e1a72000-08-14 20:20:01 +0000441 if (mapping->shared_file)
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000442 reply->shared_file = alloc_handle( current->process, mapping->shared_file,
443 GENERIC_READ|GENERIC_WRITE, 0 );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000444 release_object( mapping );
445 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000446}